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.

341 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 checkHref,
8 evidenceOf,
9 expiresIn,
10 feedLine,
11 harnessName,
12 intentTitle,
13 awakeAndIdle,
14 liveByIssue,
15 sessionTitle,
16 duration,
17 mergeSessions,
18 numberedParts,
19 parseBlockedBy,
20 pendingLines,
21 resolutionOf,
22 sessionTree,
23 span,
24 standingFailure,
25 touchedByKind,
26 treeSize,
27 viaLabel,
28} from "./coordination.ts";
29
30const ana = { id: "usr_ana", username: "ana" };
31const bo = { id: "usr_bo", username: "bo" };
32
33function claim(issue: number, over: Partial<Claim> = {}): Claim {
34 return {
35 id: `clm_${issue}_${over.actor?.username ?? "ana"}`,
36 repoId: "rep_1",
37 issue,
38 sessionId: null,
39 actor: ana,
40 harness: "claude_code",
41 note: null,
42 state: "active",
43 createdAt: "2026-10-12T10:00:00Z",
44 expiresAt: "2026-10-12T10:30:00Z",
45 endedAt: null,
46 endReason: null,
47 handoffTo: null,
48 ...over,
49 };
50}
51
52function session(id: string, over: Partial<WorkSession> = {}): WorkSession {
53 return {
54 id,
55 repoId: "rep_1",
56 issue: null,
57 pull: null,
58 branch: null,
59 actor: bo,
60 harness: "codex",
61 harnessSessionId: null,
62 title: null,
63 state: "active",
64 headCommit: null,
65 startedAt: "2026-10-12T10:00:00Z",
66 lastSeenAt: "2026-10-12T10:05:00Z",
67 endedAt: null,
68 endReason: null,
69 ...over,
70 };
71}
72
73function decision(kind: Decision["kind"], at: string, over: Partial<Decision> = {}): Decision {
74 return {
75 id: `dec_${kind}_${at}`,
76 issue: 2,
77 pull: 5,
78 sessionId: null,
79 actor: null,
80 kind,
81 confidence: "tested",
82 summary: kind,
83 evidence: {},
84 createdAt: at,
85 ...over,
86 };
87}
88
89const event = (type: string, data: unknown): G1tEvent =>
90 ({ id: `evt_${type}`, type, source: "work", time: "2026-10-12T10:00:00Z", repoId: "rep_1", actor: "usr_ana", data }) as G1tEvent;
91
92test("harnesses are named as people know them", () => {
93 assert.equal(harnessName("claude_code"), "Claude Code");
94 assert.equal(harnessName("codex"), "Codex");
95 assert.equal(harnessName("claude_managed_agents"), "Claude Managed Agents");
96 assert.equal(harnessName(null), null);
97});
98
99test("a claim says when it lapses; one that ended says nothing", () => {
100 const now = Date.parse("2026-10-12T10:07:00Z");
101 assert.equal(expiresIn(claim(1), now), "expires in 23m");
102 assert.equal(expiresIn(claim(1, { expiresAt: "2026-10-12T12:00:00Z" }), now), "expires in 1h 53m");
103 assert.equal(expiresIn(claim(1, { expiresAt: "2026-10-12T10:00:00Z" }), now), "expiring now");
104 assert.equal(expiresIn(claim(1, { state: "released" }), now), null);
105 assert.equal(span(30_000), "less than a minute");
106 assert.equal(span(2 * 3_600_000), "2h");
107});
108
109test("Live now lists sessions heard from lately and folds away the idle, the latest first", () => {
110 const { awake, idle } = awakeAndIdle([
111 session("ses_a", { state: "active" }),
112 session("ses_b", { state: "idle", title: "Tidy the README", lastSeenAt: "2026-10-12T10:00:00Z" }),
113 session("ses_c", { state: "idle", lastSeenAt: "2026-10-12T10:10:00Z" }),
114 ]);
115 assert.deepEqual(awake.map((one) => one.id), ["ses_a"]);
116 assert.deepEqual(idle.map((one) => one.id), ["ses_c", "ses_b"]);
117 // A tab opened and never typed in is in neither; once it says what it is for, it shows.
118 const opened = session("ses_d", { state: "active", title: null, startedAt: "2026-10-12T10:00:00Z", lastSeenAt: "2026-10-12T10:00:00Z" });
119 assert.deepEqual(awakeAndIdle([opened]), { awake: [], idle: [] });
120 assert.equal(awakeAndIdle([{ ...opened, lastSeenAt: "2026-10-12T10:00:05Z" }]).awake.length, 1);
121 assert.equal(awakeAndIdle([{ ...opened, title: "Fix the build" }]).awake.length, 1);
122});
123
124test("live work is grouped by issue, most recent first, with sessions on no issue apart", () => {
125 const { issues, loose } = liveByIssue(
126 [claim(1), claim(2, { createdAt: "2026-10-12T10:20:00Z" }), claim(2, { actor: bo })],
127 [session("ses_a", { issue: 1, lastSeenAt: "2026-10-12T10:30:00Z" }), session("ses_b")],
128 );
129 assert.deepEqual(
130 issues.map((group) => [group.issue, group.claims.length, group.sessions.length]),
131 [
132 [1, 1, 1],
133 [2, 2, 0],
134 ],
135 );
136 assert.deepEqual(loose.map((one) => one.id), ["ses_b"]);
137});
138
139test("sessions on no issue are grouped by pull request, then by a branch of their own, then the rest together", () => {
140 const seen = (minute: number) => `2026-10-12T10:${String(minute).padStart(2, "0")}:00Z`;
141 const { issues, groups, loose } = liveByIssue(
142 [],
143 [
144 session("ses_main", { branch: "main", lastSeenAt: seen(50) }),
145 session("ses_fix1", { branch: "fix-previews", lastSeenAt: seen(10) }),
146 session("ses_pr", { pull: 12, branch: "tidy-readme", lastSeenAt: seen(20) }),
147 session("ses_fix2", { branch: "fix-previews", lastSeenAt: seen(40) }),
148 session("ses_none", { lastSeenAt: seen(30) }),
149 session("ses_issue", { issue: 3, branch: "3-x", lastSeenAt: seen(5) }),
150 ],
151 "main",
152 );
153 assert.deepEqual(issues.map((group) => group.issue), [3]);
154 assert.deepEqual(
155 groups.map((group) => [group.kind === "pull" ? `#${group.pull}` : group.kind === "branch" ? group.branch : "none", group.sessions.map((one) => one.id)]),
156 [
157 // The most recently heard from first; the rest, on no branch of their own (the default branch is none), last.
158 ["fix-previews", ["ses_fix2", "ses_fix1"]],
159 ["#12", ["ses_pr"]],
160 ["none", ["ses_main", "ses_none"]],
161 ],
162 );
163 assert.deepEqual(loose.map((one) => one.id), ["ses_fix2", "ses_fix1", "ses_pr", "ses_main", "ses_none"]);
164 // Without the default branch known, its sessions are a branch's.
165 assert.equal(liveByIssue([], [session("ses_main", { branch: "main" })]).groups[0].kind, "branch");
166});
167
168test("a session is never title-less: its title, its first prompt's intent, its branch, or untitled", () => {
169 assert.equal(sessionTitle({ title: "Rewrite the auth flow", branch: "auth", firstPrompt: "fix it" }), "Rewrite the auth flow");
170 // A title that is only a pasted block's tag reads as none: the first prompt's words title it.
171 assert.equal(sessionTitle({ title: '<pasted_content id="5c50">', branch: "main", firstPrompt: '<pasted_content id="5c50">\nlog\n</pasted_content>\nship the runner setup' }), "Ship the runner setup");
172 assert.equal(sessionTitle({ title: '<pasted_content id="5c50">', branch: "main", firstPrompt: null }), "Session on main");
173 assert.equal(sessionTitle({ title: null, branch: "auth", firstPrompt: "fix the login redirect. It loops." }), "Fix the login redirect");
174 assert.equal(sessionTitle({ title: " ", branch: "auth", firstPrompt: "/clear" }), "Session on auth");
175 assert.equal(sessionTitle({ title: null, branch: null }), "Untitled session");
176 assert.equal(sessionTitle({ title: null, branch: null }, "look at the CI"), "Look at the CI", "a first prompt given stands in");
177});
178
179test("a prompt's intent is its first line or sentence, plain, clipped on a word", () => {
180 assert.equal(intentTitle("Make preview builds faster. They time out on big repositories."), "Make preview builds faster");
181 // What an editor puts ahead of the prompt is not the prompt.
182 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?");
183 assert.equal(intentTitle("Why does `src/app.ts` fail? It worked yesterday."), "Why does src/app.ts fail?");
184 assert.equal(intentTitle("```js\nconst a = 1;\n```\n## Clean **this** up\nmore"), "Clean this up");
185 assert.equal(intentTitle("\n\n- look at [the docs](https://docs.g1t.sh) for g1t.mjs"), "Look at the docs for g1t.mjs");
186 const long = intentTitle("Tidy up the README and then look at the CI pipeline, which keeps failing on the Windows runners every night")!;
187 assert.ok(long.length <= 80 && long.endsWith("…") && !long.endsWith(" …"), long);
188 for (const prompt of ["/clear", "/g1t:status 4", "<command-name>/clear</command-name>", "```\ncode only\n```", "?", "", null]) {
189 assert.equal(intentTitle(prompt), null, String(prompt));
190 }
191});
192
193test("evidence is read as far as it goes", () => {
194 const failed = decision("integration_failure", "2026-10-12T10:00:00Z", {
195 evidence: { commit: "abc1234", check: { name: "CI / merge_group", conclusion: "failure", url: "https://g1t.sh/acme/web/actions/runs/run_1" }, combination: [4, 5], suspects: [4], bisected: false },
196 });
197 assert.deepEqual(evidenceOf(failed), {
198 commit: "abc1234",
199 check: { name: "CI / merge_group", conclusion: "failure", url: "https://g1t.sh/acme/web/actions/runs/run_1" },
200 combination: [4, 5],
201 suspects: [4],
202 bisected: false,
203 });
204 assert.deepEqual(evidenceOf(decision("note", "2026-10-12T10:00:00Z", { evidence: { combination: "nope" } })).combination, []);
205 assert.equal(checkHref("https://g1t.sh/acme/web/actions/runs/run_1"), "/acme/web/actions/runs/run_1");
206 assert.equal(checkHref("https://ci.example.com/build/9"), "https://ci.example.com/build/9");
207 assert.equal(checkHref(null), null);
208});
209
210test("a failure stands until the pull request passes in the queue or merges, and a fix after it answers it", () => {
211 const failed = decision("integration_failure", "2026-10-12T10:00:00Z");
212 const fix = decision("fix", "2026-10-12T10:10:00Z", { confidence: "claimed" });
213 const passed = decision("integration_passed", "2026-10-12T10:20:00Z");
214 assert.equal(standingFailure([failed], 5), failed);
215 assert.equal(standingFailure([failed, fix], 5), failed);
216 assert.equal(standingFailure([passed, failed], 5), null);
217 assert.equal(standingFailure([failed], 6), null);
218 assert.deepEqual(resolutionOf([failed, fix, passed], failed), { fix, passed });
219 assert.deepEqual(resolutionOf([fix, failed], { ...failed, createdAt: "2026-10-12T10:15:00Z" }), { fix: null, passed: null });
220});
221
222test("coordination events read as lines of the feed", () => {
223 const claimed = feedLine(event("work.claimed", { repoId: "rep_1", number: 2, issue: 2, claim: claim(2, { actor: bo, harness: "codex" }), others: [claim(2)] }))!;
224 assert.equal(claimed.who, "bo");
225 assert.equal(claimed.harness, "codex");
226 assert.equal(claimed.text, "claimed #2 (ana is on it too)");
227 assert.equal(claimed.tone, "warn");
228
229 const failed = feedLine(
230 event("integration.failed", {
231 repoId: "rep_1", pullId: "pul_5", number: 5, pull: 5, issue: 2, combination: [4, 5],
232 check: { name: "CI / merge_group", conclusion: "failure", url: null }, bisected: false, suspects: [4], otherPulls: [4], commit: null,
233 }),
234 )!;
235 assert.equal(failed.who, null);
236 assert.equal(failed.text, "#5 failed in the merge queue combined as #4, #5: CI / merge_group; fails with #4");
237 assert.equal(failed.tone, "danger");
238
239 const unblocked = feedLine(
240 event("dependency.changed", { repoId: "rep_1", number: 2, issue: 2, blockedBy: [1], added: [], removed: [], cleared: [1], remaining: [], mergedPull: 4 }),
241 )!;
242 assert.equal(unblocked.text, "#2 #1 landed (#4 merged); unblocked");
243
244 const overlap = feedLine(event("overlap.detected", { repoId: "rep_1", number: 5, pull: 5, otherPull: 4, files: ["src/auth.rs"] }))!;
245 assert.equal(overlap.text, "#5 and #4 change the same file: src/auth.rs");
246 assert.equal(overlap.session, undefined);
247
248 const sessions = feedLine(
249 event("overlap.detected", { repoId: "rep_1", sessionId: "ses_a", otherSessionId: "ses_b", otherActor: "bo", files: ["a.rs", "b.rs", "c.rs", "d.rs"], issue: 2 }),
250 )!;
251 assert.equal(sessions.who, "bo");
252 assert.equal(sessions.text, "touched the same files in a session as another session: a.rs, b.rs, c.rs and 1 more");
253 assert.equal(sessions.session, "ses_a");
254 assert.equal(sessions.number, 2);
255 assert.equal(feedLine(event("session.progressed", { repoId: "rep_1", number: 2, sessionId: "ses_a" })), null);
256
257 const started = feedLine(event("session.started", { repoId: "rep_1", session: session("ses_a", { issue: 2 }) }))!;
258 assert.equal(started.text, "started a session on #2");
259 assert.equal(feedLine(event("session.appended", { repoId: "rep_1", number: 5, pullId: "p", count: 3 })), null);
260});
261
262test("sessions become trees by their parents; one whose parent is missing is a root", () => {
263 const roots = sessionTree([
264 session("ses_root"),
265 session("ses_a", { parentId: "ses_root" }),
266 session("ses_a1", { parentId: "ses_a" }),
267 session("ses_b", { parentId: "ses_root" }),
268 session("ses_orphan", { parentId: "ses_gone" }),
269 session("ses_other"),
270 ]);
271 const shape = (nodes: ReturnType<typeof sessionTree>): unknown[] => nodes.map((node) => [node.session.id, shape(node.children)]);
272 assert.deepEqual(shape(roots), [
273 ["ses_root", [["ses_a", [["ses_a1", []]]], ["ses_b", []]]],
274 ["ses_orphan", []],
275 ["ses_other", []],
276 ]);
277 assert.equal(treeSize(roots[0]!), 3);
278 // A child listed before its parent still lands under it.
279 assert.deepEqual(shape(sessionTree([session("ses_c", { parentId: "ses_p" }), session("ses_p")])), [["ses_p", [["ses_c", []]]]]);
280 // A cycle does not lose sessions.
281 const cycle = sessionTree([session("ses_x", { parentId: "ses_y" }), session("ses_y", { parentId: "ses_x" })]);
282 assert.equal(cycle.length + cycle.reduce((n, node) => n + treeSize(node), 0), 2);
283 assert.deepEqual(sessionTree([]), []);
284 assert.deepEqual(
285 mergeSessions([session("ses_a"), session("ses_b")], null, [session("ses_b", { title: "later" }), session("ses_c")]).map((one) => [one.id, one.title]),
286 [
287 ["ses_a", null],
288 ["ses_b", null],
289 ["ses_c", null],
290 ],
291 );
292});
293
294test("how a session was tied to its work, what it touched, and how long it ran", () => {
295 assert.equal(viaLabel("attached"), "attached");
296 assert.equal(viaLabel("claim"), "claimed");
297 assert.equal(viaLabel("branch"), "its branch");
298 assert.equal(viaLabel("commit"), "its commit");
299 assert.equal(viaLabel("trailer"), "commit trailer");
300 assert.equal(viaLabel("opened"), "opened it");
301 assert.equal(viaLabel("ref"), "named it");
302 assert.deepEqual(
303 touchedByKind([
304 { kind: "commit", value: "abc" },
305 { kind: "file", value: "b.rs" },
306 { kind: "file", value: "a.rs" },
307 { kind: "file", value: "b.rs" },
308 ]),
309 [
310 { kind: "file", values: ["b.rs", "a.rs"] },
311 { kind: "commit", values: ["abc"] },
312 ],
313 );
314 const now = Date.parse("2026-10-12T10:30:00Z");
315 assert.equal(duration(session("ses_a"), now), "30m");
316 assert.equal(duration(session("ses_a", { state: "ended", endedAt: "2026-10-12T10:00:42Z" }), now), "42s");
317 assert.equal(duration(session("ses_a", { state: "ended", endedAt: null }), now), "5m");
318});
319
320test("numbers in a sentence are found for linking", () => {
321 assert.deepEqual(numberedParts("#5 fails with #4."), [5, " fails with ", 4, "."]);
322 assert.deepEqual(numberedParts("no numbers"), ["no numbers"]);
323});
324
325test("blocked_by is typed as numbers", () => {
326 assert.deepEqual(parseBlockedBy("#12, 14 #12"), [12, 14]);
327 assert.deepEqual(parseBlockedBy(""), []);
328 assert.equal(parseBlockedBy("#12, soon"), null);
329 assert.equal(parseBlockedBy("0"), null);
330});
331
332test("a session's pending updates read as the feed does, leaving out what it does not show", () => {
333 const lines = pendingLines([
334 { id: "upd_1", kind: "work.claimed", at: "2026-10-14T10:00:00Z", data: { repoId: "rep_1", number: 4, issue: 4, claim: claim(4, { actor: bo, harness: "codex" }) } },
335 { id: "upd_2", kind: "session.progressed", at: "2026-10-14T10:00:01Z", data: {} },
336 ]);
337 assert.equal(lines.length, 1);
338 assert.equal(lines[0].id, "upd_1");
339 assert.equal(lines[0].who, "bo");
340 assert.match(lines[0].text, /claimed #4/);
341});