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.

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