Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

464 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { Claim, Decision, G1tEvent, WorkSession } from "@g1t/contracts";
5
6import {
7 ENDED_BY_HAND,
8 checkHref,
9 endReasonText,
10 evidenceOf,
11 expiresIn,
12 feedLine,
13 harnessName,
14 intentTitle,
15 awakeAndIdle,
16 liveByItem,
17 sessionTitle,
18 duration,
19 mayEndSession,
20 mergeSessions,
21 numberedParts,
22 parseBlockedBy,
23 pullHrefs,
24 pendingLines,
25 resolutionOf,
26 sessionTree,
27 span,
28 titleHint,
29 titleIsProvisional,
30 standingFailure,
31 touchedByKind,
32 treeSize,
33 viaLabel,
34} from "./coordination.ts";
35
36const ana = { id: "usr_ana", username: "ana" };
37const bo = { id: "usr_bo", username: "bo" };
38
39function claim(item: number, over: Partial<Claim> = {}): Claim {
40 return {
41 id: `clm_${item}_${over.actor?.username ?? "ana"}`,
42 repoId: "rep_1",
43 workspace: "acme",
44 item,
45 sessionId: null,
46 actor: ana,
47 harness: "claude_code",
48 note: null,
49 state: "active",
50 createdAt: "2026-10-12T10:00:00Z",
51 expiresAt: "2026-10-12T10:30:00Z",
52 endedAt: null,
53 endReason: null,
54 handoffTo: null,
55 ...over,
56 };
57}
58
59function session(id: string, over: Partial<WorkSession> = {}): WorkSession {
60 return {
61 id,
62 repoId: "rep_1",
63 item: null,
64 pull: null,
65 branch: null,
66 actor: bo,
67 harness: "codex",
68 harnessSessionId: null,
69 title: null,
70 state: "active",
71 headCommit: null,
72 startedAt: "2026-10-12T10:00:00Z",
73 lastSeenAt: "2026-10-12T10:05:00Z",
74 endedAt: null,
75 endReason: null,
76 ...over,
77 };
78}
79
80function decision(kind: Decision["kind"], at: string, over: Partial<Decision> = {}): Decision {
81 return {
82 id: `dec_${kind}_${at}`,
83 item: 2,
84 pull: 5,
85 sessionId: null,
86 actor: null,
87 kind,
88 confidence: "tested",
89 summary: kind,
90 evidence: {},
91 createdAt: at,
92 ...over,
93 };
94}
95
96const event = (type: string, data: unknown): G1tEvent =>
97 ({ id: `evt_${type}`, type, source: "work", time: "2026-10-12T10:00:00Z", repoId: "rep_1", actor: "usr_ana", data }) as G1tEvent;
98
99test("harnesses are named as people know them", () => {
100 assert.equal(harnessName("claude_code"), "Claude Code");
101 assert.equal(harnessName("codex"), "Codex");
102 assert.equal(harnessName("claude_managed_agents"), "Claude Managed Agents");
103 assert.equal(harnessName(null), null);
104});
105
106test("a claim says when it lapses; one that ended says nothing", () => {
107 const now = Date.parse("2026-10-12T10:07:00Z");
108 assert.equal(expiresIn(claim(1), now), "expires in 23m");
109 assert.equal(expiresIn(claim(1, { expiresAt: "2026-10-12T12:00:00Z" }), now), "expires in 1h 53m");
110 assert.equal(expiresIn(claim(1, { expiresAt: "2026-10-12T10:00:00Z" }), now), "expiring now");
111 assert.equal(expiresIn(claim(1, { state: "released" }), now), null);
112 assert.equal(span(30_000), "less than a minute");
113 assert.equal(span(2 * 3_600_000), "2h");
114});
115
116test("Live now lists sessions heard from lately and folds away the idle, the latest first", () => {
117 const { awake, idle } = awakeAndIdle([
118 session("ses_a", { state: "active" }),
119 session("ses_b", { state: "idle", title: "Tidy the README", lastSeenAt: "2026-10-12T10:00:00Z" }),
120 session("ses_c", { state: "idle", lastSeenAt: "2026-10-12T10:10:00Z" }),
121 ]);
122 assert.deepEqual(
123 awake.map((one) => one.id),
124 ["ses_a"],
125 );
126 assert.deepEqual(
127 idle.map((one) => one.id),
128 ["ses_c", "ses_b"],
129 );
130 // A tab opened and never typed in is in neither; once it says what it is for, it shows.
131 const opened = session("ses_d", { state: "active", title: null, startedAt: "2026-10-12T10:00:00Z", lastSeenAt: "2026-10-12T10:00:00Z" });
132 assert.deepEqual(awakeAndIdle([opened]), { awake: [], idle: [] });
133 assert.equal(awakeAndIdle([{ ...opened, lastSeenAt: "2026-10-12T10:00:05Z" }]).awake.length, 1);
134 assert.equal(awakeAndIdle([{ ...opened, title: "Fix the build" }]).awake.length, 1);
135});
136
137test("live work is grouped by work item, most recent first, with sessions on no work item apart", () => {
138 const { items, loose } = liveByItem(
139 [claim(1), claim(2, { createdAt: "2026-10-12T10:20:00Z" }), claim(2, { actor: bo })],
140 [session("ses_a", { item: 1, lastSeenAt: "2026-10-12T10:30:00Z" }), session("ses_b")],
141 );
142 assert.deepEqual(
143 items.map((group) => [group.item, group.claims.length, group.sessions.length]),
144 [
145 [1, 1, 1],
146 [2, 2, 0],
147 ],
148 );
149 assert.deepEqual(
150 loose.map((one) => one.id),
151 ["ses_b"],
152 );
153});
154
155test("sessions on no work item are grouped by pull request, then by a branch of their own, then the rest together", () => {
156 const seen = (minute: number) => `2026-10-12T10:${String(minute).padStart(2, "0")}:00Z`;
157 const { items, groups, loose } = liveByItem(
158 [],
159 [
160 session("ses_main", { branch: "main", lastSeenAt: seen(50) }),
161 session("ses_fix1", { branch: "fix-previews", lastSeenAt: seen(10) }),
162 session("ses_pr", { pull: 12, branch: "tidy-readme", lastSeenAt: seen(20) }),
163 session("ses_fix2", { branch: "fix-previews", lastSeenAt: seen(40) }),
164 session("ses_none", { lastSeenAt: seen(30) }),
165 session("ses_issue", { item: 3, branch: "3-x", lastSeenAt: seen(5) }),
166 ],
167 "main",
168 );
169 assert.deepEqual(
170 items.map((group) => group.item),
171 [3],
172 );
173 assert.deepEqual(
174 groups.map((group) => [group.kind === "pull" ? `#${group.pull}` : group.kind === "branch" ? group.branch : "none", group.sessions.map((one) => one.id)]),
175 [
176 // The most recently heard from first; the rest, on no branch of their own (the default branch is none), last.
177 ["fix-previews", ["ses_fix2", "ses_fix1"]],
178 ["#12", ["ses_pr"]],
179 ["none", ["ses_main", "ses_none"]],
180 ],
181 );
182 assert.deepEqual(
183 loose.map((one) => one.id),
184 ["ses_fix2", "ses_fix1", "ses_pr", "ses_main", "ses_none"],
185 );
186 // Without the default branch known, its sessions are a branch's.
187 assert.equal(liveByItem([], [session("ses_main", { branch: "main" })]).groups[0].kind, "branch");
188});
189
190test("a session is never title-less: its title, its first prompt's intent, its branch, or untitled", () => {
191 assert.equal(sessionTitle({ title: "Rewrite the auth flow", branch: "auth", firstPrompt: "fix it" }), "Rewrite the auth flow");
192 // A title that is only a pasted block's tag reads as none: the first prompt's words title it.
193 assert.equal(
194 sessionTitle({
195 title: '<pasted_content id="5c50">',
196 branch: "main",
197 firstPrompt: '<pasted_content id="5c50">\nlog\n</pasted_content>\nship the runner setup',
198 }),
199 "Ship the runner setup",
200 );
201 assert.equal(sessionTitle({ title: '<pasted_content id="5c50">', branch: "main", firstPrompt: null }), "Session on main");
202 assert.equal(sessionTitle({ title: null, branch: "auth", firstPrompt: "fix the login redirect. It loops." }), "Fix the login redirect");
203 assert.equal(sessionTitle({ title: " ", branch: "auth", firstPrompt: "/clear" }), "Session on auth");
204 assert.equal(sessionTitle({ title: null, branch: null }), "Untitled session");
205 assert.equal(sessionTitle({ title: null, branch: null }, "look at the CI"), "Look at the CI", "a first prompt given stands in");
206});
207
208test("a title from the first prompt is provisional until g1t, the agent or a person names the session", () => {
209 assert.equal(titleIsProvisional({ title: "This project used to be in syntaqx/g1t", titleSource: "prompt", parentId: null }), true);
210 assert.equal(titleIsProvisional({ title: null, titleSource: null, parentId: null }), true);
211 for (const titleSource of ["agent", "person", "item", "pull", "summary"] as const) {
212 assert.equal(titleIsProvisional({ title: "Move issues to Work items", titleSource, parentId: null }), false, titleSource);
213 }
214 // From before titles said where they came from: as it is.
215 assert.equal(titleIsProvisional({ title: "Rewrite the auth flow", parentId: null }), false);
216 // A subagent goes by its task.
217 assert.equal(titleIsProvisional({ title: null, titleSource: null, parentId: "ses_1" }), false);
218 assert.match(titleHint({ title: "Mine", titleSource: "person", parentId: null }) ?? "", /keeps this title/);
219 assert.match(titleHint({ title: "x", titleSource: "prompt", parentId: null }) ?? "", /first prompt/);
220 assert.equal(titleHint({ title: "x", titleSource: "agent", parentId: "ses_1" }), undefined);
221});
222
223test("a prompt's intent is its first line or sentence, plain, clipped on a word", () => {
224 assert.equal(intentTitle("Make preview builds faster. They time out on big repositories."), "Make preview builds faster");
225 // What an editor puts ahead of the prompt is not the prompt.
226 assert.equal(intentTitle("<ide_opened_file>The user opened a.ts in the IDE.</ide_opened_file>\nWhy is the page crashing?"), "Why is the page crashing?");
227 assert.equal(intentTitle("Why does `src/app.ts` fail? It worked yesterday."), "Why does src/app.ts fail?");
228 assert.equal(intentTitle("```js\nconst a = 1;\n```\n## Clean **this** up\nmore"), "Clean this up");
229 assert.equal(intentTitle("\n\n- look at [the docs](https://docs.g1t.sh) for g1t.mjs"), "Look at the docs for g1t.mjs");
230 const long = intentTitle("Tidy up the README and then look at the CI pipeline, which keeps failing on the Windows runners every night")!;
231 assert.ok(long.length <= 80 && long.endsWith("…") && !long.endsWith(" …"), long);
232 for (const prompt of ["/clear", "/g1t:status 4", "<command-name>/clear</command-name>", "```\ncode only\n```", "?", "", null]) {
233 assert.equal(intentTitle(prompt), null, String(prompt));
234 }
235});
236
237test("evidence is read as far as it goes", () => {
238 const failed = decision("integration_failure", "2026-10-12T10:00:00Z", {
239 evidence: {
240 commit: "abc1234",
241 check: { name: "CI / merge_group", conclusion: "failure", url: "https://g1t.sh/acme/web/actions/runs/run_1" },
242 combination: [4, 5],
243 suspects: [4],
244 bisected: false,
245 },
246 });
247 assert.deepEqual(evidenceOf(failed), {
248 commit: "abc1234",
249 check: { name: "CI / merge_group", conclusion: "failure", url: "https://g1t.sh/acme/web/actions/runs/run_1" },
250 combination: [4, 5],
251 suspects: [4],
252 bisected: false,
253 });
254 assert.deepEqual(evidenceOf(decision("note", "2026-10-12T10:00:00Z", { evidence: { combination: "nope" } })).combination, []);
255 assert.equal(checkHref("https://g1t.sh/acme/web/actions/runs/run_1"), "/acme/web/actions/runs/run_1");
256 assert.equal(checkHref("https://ci.example.com/build/9"), "https://ci.example.com/build/9");
257 assert.equal(checkHref(null), null);
258});
259
260test("a failure stands until the pull request passes in the queue or merges, and a fix after it answers it", () => {
261 const failed = decision("integration_failure", "2026-10-12T10:00:00Z");
262 const fix = decision("fix", "2026-10-12T10:10:00Z", { confidence: "claimed" });
263 const passed = decision("integration_passed", "2026-10-12T10:20:00Z");
264 assert.equal(standingFailure([failed], 5), failed);
265 assert.equal(standingFailure([failed, fix], 5), failed);
266 assert.equal(standingFailure([passed, failed], 5), null);
267 assert.equal(standingFailure([failed], 6), null);
268 assert.deepEqual(resolutionOf([failed, fix, passed], failed), { fix, passed });
269 assert.deepEqual(resolutionOf([fix, failed], { ...failed, createdAt: "2026-10-12T10:15:00Z" }), { fix: null, passed: null });
270});
271
272test("coordination events read as lines of the feed", () => {
273 const claimed = feedLine(
274 event("work.claimed", { repoId: "rep_1", number: 2, item: 2, claim: claim(2, { actor: bo, harness: "codex" }), others: [claim(2)] }),
275 )!;
276 assert.equal(claimed.who, "bo");
277 assert.equal(claimed.harness, "codex");
278 assert.equal(claimed.text, "claimed #2 (ana is on it too)");
279 assert.equal(claimed.tone, "warn");
280
281 const failed = feedLine(
282 event("integration.failed", {
283 repoId: "rep_1",
284 pullId: "pul_5",
285 number: 5,
286 pull: 5,
287 item: 2,
288 combination: [4, 5],
289 check: { name: "CI / merge_group", conclusion: "failure", url: null },
290 bisected: false,
291 suspects: [4],
292 otherPulls: [4],
293 commit: null,
294 }),
295 )!;
296 assert.equal(failed.who, null);
297 assert.equal(failed.text, "#5 failed in the merge queue combined as #4, #5: CI / merge_group; fails with #4");
298 assert.equal(failed.tone, "danger");
299
300 const unblocked = feedLine(
301 event("dependency.changed", { repoId: "rep_1", number: 2, item: 2, blockedBy: [1], added: [], removed: [], cleared: [1], remaining: [], mergedPull: 4 }),
302 )!;
303 assert.equal(unblocked.text, "#2 #1 landed (#4 merged); unblocked");
304
305 const overlap = feedLine(event("overlap.detected", { repoId: "rep_1", number: 5, pull: 5, otherPull: 4, files: ["src/auth.rs"] }))!;
306 assert.equal(overlap.text, "#5 and #4 change the same file: src/auth.rs");
307 assert.equal(overlap.session, undefined);
308
309 const sessions = feedLine(
310 event("overlap.detected", {
311 repoId: "rep_1",
312 sessionId: "ses_a",
313 otherSessionId: "ses_b",
314 otherActor: "bo",
315 files: ["a.rs", "b.rs", "c.rs", "d.rs"],
316 item: 2,
317 }),
318 )!;
319 assert.equal(sessions.who, "bo");
320 assert.equal(sessions.text, "touched the same files in a session as another session: a.rs, b.rs, c.rs and 1 more");
321 assert.equal(sessions.session, "ses_a");
322 assert.equal(sessions.number, 2);
323 assert.equal(feedLine(event("session.progressed", { repoId: "rep_1", number: 2, sessionId: "ses_a" })), null);
324
325 const started = feedLine(event("session.started", { repoId: "rep_1", session: session("ses_a", { item: 2 }) }))!;
326 assert.equal(started.text, "started a session on #2");
327 assert.equal(feedLine(event("session.appended", { repoId: "rep_1", number: 5, pullId: "p", count: 3 })), null);
328});
329
330test("a session is ended by hand by whoever runs it, or by anyone who can write", () => {
331 const live = session("ses_a");
332 assert.equal(mayEndSession(live, bo.id, false), true);
333 assert.equal(mayEndSession(live, ana.id, false), false);
334 assert.equal(mayEndSession(live, ana.id, true), true);
335 assert.equal(mayEndSession(session("ses_b", { state: "idle" }), ana.id, true), true);
336 // Ended already, or nobody signed in: nothing to offer.
337 assert.equal(mayEndSession(session("ses_c", { state: "ended" }), bo.id, true), false);
338 assert.equal(mayEndSession(live, null, true), false);
339 // One g1t runs is stopped from its page, which stops the run too.
340 assert.equal(mayEndSession(session("ses_d", { harness: "claude_managed_agents" }), bo.id, true), false);
341 assert.equal(endReasonText("timed_out"), "timed out");
342 assert.equal(endReasonText(`${ENDED_BY_HAND}ana`), "ended by hand by @ana");
343 assert.equal(endReasonText(null), null);
344});
345
346test("a session ended by hand says who ended it in the feed", () => {
347 const own = feedLine(event("session.ended", { repoId: "rep_1", session: session("ses_a", { item: 2, state: "ended", endReason: `${ENDED_BY_HAND}bo` }) }))!;
348 assert.equal(own.who, "bo");
349 assert.equal(own.text, "ended a session on #2 by hand");
350 const theirs = feedLine(event("session.ended", { repoId: "rep_1", session: session("ses_a", { state: "ended", endReason: `${ENDED_BY_HAND}ana` }) }))!;
351 assert.equal(theirs.who, "ana");
352 assert.equal(theirs.harness, null);
353 assert.equal(theirs.text, "ended bo's session by hand");
354 const timedOut = feedLine(event("session.ended", { repoId: "rep_1", session: session("ses_a", { state: "ended", endReason: "timed_out" }) }))!;
355 assert.equal(timedOut.text, "ended a session (timed out)");
356});
357
358test("sessions become trees by their parents; one whose parent is missing is a root", () => {
359 const roots = sessionTree([
360 session("ses_root"),
361 session("ses_a", { parentId: "ses_root" }),
362 session("ses_a1", { parentId: "ses_a" }),
363 session("ses_b", { parentId: "ses_root" }),
364 session("ses_orphan", { parentId: "ses_gone" }),
365 session("ses_other"),
366 ]);
367 const shape = (nodes: ReturnType<typeof sessionTree>): unknown[] => nodes.map((node) => [node.session.id, shape(node.children)]);
368 assert.deepEqual(shape(roots), [
369 [
370 "ses_root",
371 [
372 ["ses_a", [["ses_a1", []]]],
373 ["ses_b", []],
374 ],
375 ],
376 ["ses_orphan", []],
377 ["ses_other", []],
378 ]);
379 assert.equal(treeSize(roots[0]!), 3);
380 // A child listed before its parent still lands under it.
381 assert.deepEqual(shape(sessionTree([session("ses_c", { parentId: "ses_p" }), session("ses_p")])), [["ses_p", [["ses_c", []]]]]);
382 // A cycle does not lose sessions.
383 const cycle = sessionTree([session("ses_x", { parentId: "ses_y" }), session("ses_y", { parentId: "ses_x" })]);
384 assert.equal(cycle.length + cycle.reduce((n, node) => n + treeSize(node), 0), 2);
385 assert.deepEqual(sessionTree([]), []);
386 assert.deepEqual(
387 mergeSessions([session("ses_a"), session("ses_b")], null, [session("ses_b", { title: "later" }), session("ses_c")]).map((one) => [one.id, one.title]),
388 [
389 ["ses_a", null],
390 ["ses_b", null],
391 ["ses_c", null],
392 ],
393 );
394});
395
396test("how a session was tied to its work, what it touched, and how long it ran", () => {
397 assert.equal(viaLabel("attached"), "attached");
398 assert.equal(viaLabel("claim"), "claimed");
399 assert.equal(viaLabel("branch"), "its branch");
400 assert.equal(viaLabel("commit"), "its commit");
401 assert.equal(viaLabel("trailer"), "commit trailer");
402 assert.equal(viaLabel("opened"), "opened it");
403 assert.equal(viaLabel("ref"), "named it");
404 assert.deepEqual(
405 touchedByKind([
406 { kind: "commit", value: "abc" },
407 { kind: "file", value: "b.rs" },
408 { kind: "file", value: "a.rs" },
409 { kind: "file", value: "b.rs" },
410 ]),
411 [
412 { kind: "file", values: ["b.rs", "a.rs"] },
413 { kind: "commit", values: ["abc"] },
414 ],
415 );
416 const now = Date.parse("2026-10-12T10:30:00Z");
417 assert.equal(duration(session("ses_a"), now), "30m");
418 assert.equal(duration(session("ses_a", { state: "ended", endedAt: "2026-10-12T10:00:42Z" }), now), "42s");
419 assert.equal(duration(session("ses_a", { state: "ended", endedAt: null }), now), "5m");
420});
421
422test("numbers in a sentence are found for linking", () => {
423 assert.deepEqual(numberedParts("#5 fails with #4."), [5, " fails with ", 4, "."]);
424 assert.deepEqual(numberedParts("no numbers"), ["no numbers"]);
425});
426
427test("blocked_by is typed as numbers", () => {
428 assert.deepEqual(parseBlockedBy("#12, 14 #12"), [12, 14]);
429 assert.deepEqual(parseBlockedBy(""), []);
430 assert.equal(parseBlockedBy("#12, soon"), null);
431 assert.equal(parseBlockedBy("0"), null);
432});
433
434test("a session's pending updates read as the feed does, leaving out what it does not show", () => {
435 const lines = pendingLines([
436 {
437 id: "upd_1",
438 kind: "work.claimed",
439 at: "2026-10-14T10:00:00Z",
440 data: { repoId: "rep_1", number: 4, item: 4, claim: claim(4, { actor: bo, harness: "codex" }) },
441 },
442 { id: "upd_2", kind: "session.progressed", at: "2026-10-14T10:00:01Z", data: {} },
443 ]);
444 assert.equal(lines.length, 1);
445 assert.equal(lines[0].id, "upd_1");
446 assert.equal(lines[0].who, "bo");
447 assert.match(lines[0].text, /claimed #4/);
448});
449
450test("a work item's pull requests are linked in the repository that has them, and not at all when none or two do", () => {
451 const href = pullHrefs([
452 { path: "acme/web", pulls: [{ number: 41 }] },
453 { path: "acme/api", pulls: [{ number: 44 }, { number: 7 }] },
454 { path: "acme/docs", pulls: [{ number: 7 }] },
455 // A repository deleted before work items kept no path.
456 { path: "", pulls: [{ number: 50 }] },
457 ]);
458 assert.equal(href(41), "/acme/web/pull/41");
459 // A target's pull request is the target's, not the origin's.
460 assert.equal(href(44), "/acme/api/pull/44");
461 assert.equal(href(7), null, "two repositories have a #7: neither is guessed");
462 assert.equal(href(99), null, "unknown");
463 assert.equal(href(50), null, "a deleted repository");
464});