@nightloom/isy 1.0.4 → 1.0.6-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +18 -2
  2. package/dist/agents/claude.js +3 -2
  3. package/dist/agents/claude.js.map +1 -1
  4. package/dist/agents/codex.js +14 -5
  5. package/dist/agents/codex.js.map +1 -1
  6. package/dist/agents/index.d.ts +14 -10
  7. package/dist/agents/index.js +65 -15
  8. package/dist/agents/index.js.map +1 -1
  9. package/dist/agents/kimi.js +26 -8
  10. package/dist/agents/kimi.js.map +1 -1
  11. package/dist/agents/types.d.ts +12 -3
  12. package/dist/api.d.ts +1 -0
  13. package/dist/api.js +1 -1
  14. package/dist/api.js.map +1 -1
  15. package/dist/commands/commit.d.ts +6 -0
  16. package/dist/commands/commit.js +326 -16
  17. package/dist/commands/commit.js.map +1 -1
  18. package/dist/commands/health.js +1 -1
  19. package/dist/commands/health.js.map +1 -1
  20. package/dist/commands/init.js +40 -9
  21. package/dist/commands/init.js.map +1 -1
  22. package/dist/commands/status.d.ts +2 -0
  23. package/dist/commands/status.js +7 -4
  24. package/dist/commands/status.js.map +1 -1
  25. package/dist/commands/upload.d.ts +8 -1
  26. package/dist/commands/upload.js +65 -48
  27. package/dist/commands/upload.js.map +1 -1
  28. package/dist/detectors.d.ts +32 -0
  29. package/dist/detectors.js +279 -72
  30. package/dist/detectors.js.map +1 -1
  31. package/dist/git.d.ts +52 -0
  32. package/dist/git.js +166 -9
  33. package/dist/git.js.map +1 -1
  34. package/dist/githook.d.ts +18 -7
  35. package/dist/githook.js +147 -17
  36. package/dist/githook.js.map +1 -1
  37. package/dist/hook.d.ts +22 -2
  38. package/dist/hook.js +28 -3
  39. package/dist/hook.js.map +1 -1
  40. package/dist/index.js +23 -7
  41. package/dist/index.js.map +1 -1
  42. package/dist/mask-paths.d.ts +4 -3
  43. package/dist/mask-paths.js +4 -3
  44. package/dist/mask-paths.js.map +1 -1
  45. package/dist/parser.d.ts +58 -9
  46. package/dist/parser.js +337 -58
  47. package/dist/parser.js.map +1 -1
  48. package/dist/paths.d.ts +19 -1
  49. package/dist/paths.js +28 -3
  50. package/dist/paths.js.map +1 -1
  51. package/dist/redact.d.ts +2 -0
  52. package/dist/redact.js +987 -95
  53. package/dist/redact.js.map +1 -1
  54. package/dist/stage0.js +27 -8
  55. package/dist/stage0.js.map +1 -1
  56. package/dist/state.d.ts +15 -0
  57. package/dist/state.js +8 -0
  58. package/dist/state.js.map +1 -1
  59. package/dist/test/check.test.js +1 -1
  60. package/dist/test/check.test.js.map +1 -1
  61. package/dist/test/cli.test.js +11 -1
  62. package/dist/test/cli.test.js.map +1 -1
  63. package/dist/test/codex.test.js +27 -4
  64. package/dist/test/codex.test.js.map +1 -1
  65. package/dist/test/commit.test.js +1466 -6
  66. package/dist/test/commit.test.js.map +1 -1
  67. package/dist/test/e2e-harness.test.d.ts +1 -0
  68. package/dist/test/e2e-harness.test.js +53 -0
  69. package/dist/test/e2e-harness.test.js.map +1 -0
  70. package/dist/test/git.test.js +72 -2
  71. package/dist/test/git.test.js.map +1 -1
  72. package/dist/test/hook.test.js +138 -3
  73. package/dist/test/hook.test.js.map +1 -1
  74. package/dist/test/init.test.js +61 -2
  75. package/dist/test/init.test.js.map +1 -1
  76. package/dist/test/kimi-config.test.js +27 -5
  77. package/dist/test/kimi-config.test.js.map +1 -1
  78. package/dist/test/kimi-hook.test.js +1 -1
  79. package/dist/test/kimi-hook.test.js.map +1 -1
  80. package/dist/test/kimi-listing.test.js +42 -2
  81. package/dist/test/kimi-listing.test.js.map +1 -1
  82. package/dist/test/kimi.test.js +26 -2
  83. package/dist/test/kimi.test.js.map +1 -1
  84. package/dist/test/parser.test.js +527 -1
  85. package/dist/test/parser.test.js.map +1 -1
  86. package/dist/test/redact.test.js +2472 -14
  87. package/dist/test/redact.test.js.map +1 -1
  88. package/dist/test/stage0.test.js +617 -1
  89. package/dist/test/stage0.test.js.map +1 -1
  90. package/dist/test/sweep.test.js +110 -7
  91. package/dist/test/sweep.test.js.map +1 -1
  92. package/package.json +1 -1
  93. package/src/agents/claude.ts +3 -2
  94. package/src/agents/codex.ts +14 -5
  95. package/src/agents/index.ts +71 -16
  96. package/src/agents/kimi.ts +28 -8
  97. package/src/agents/types.ts +12 -3
  98. package/src/api.ts +4 -2
  99. package/src/commands/commit.ts +354 -18
  100. package/src/commands/health.ts +1 -1
  101. package/src/commands/init.ts +45 -10
  102. package/src/commands/status.ts +9 -4
  103. package/src/commands/upload.ts +73 -63
  104. package/src/detectors.ts +284 -75
  105. package/src/git.ts +175 -9
  106. package/src/githook.ts +151 -19
  107. package/src/hook.ts +30 -3
  108. package/src/index.ts +25 -7
  109. package/src/mask-paths.ts +5 -4
  110. package/src/parser.ts +355 -51
  111. package/src/paths.ts +30 -3
  112. package/src/redact.ts +991 -98
  113. package/src/stage0.ts +28 -8
  114. package/src/state.ts +18 -0
@@ -1,8 +1,9 @@
1
1
  import { claudeAgent } from "./claude.js";
2
2
  import { codexAgent } from "./codex.js";
3
3
  import { kimiAgent } from "./kimi.js";
4
- import { lastActive } from "../paths.js";
5
- import type { SessionFile } from "../types.js";
4
+ import { workTreeTop } from "../git.js";
5
+ import { SWEEP_WINDOW_MS, isWithin, lastActive } from "../paths.js";
6
+ import type { DiscoveredSession } from "../types.js";
6
7
  import type { Agent, AgentId, HookInput } from "./types.js";
7
8
 
8
9
  export { claudeAgent } from "./claude.js";
@@ -48,26 +49,80 @@ export async function presentAgents(): Promise<Agent[]> {
48
49
  }
49
50
 
50
51
  /**
51
- * Newest session for this directory across every CLI installed here, by when
52
- * the session itself last wrote.
52
+ * One CLI's sessions from the work tree `cwd` stands in, newest first: the ones
53
+ * recorded for `cwd` itself, however old, and those from any other directory of
54
+ * the tree, as far back as a sweep looks and with that directory set as `cwd`.
53
55
  *
54
- * ponytail: a commit a subagent made, while its session waited on it, is
55
- * credited by the session's own time, which that subagent does not move; a
56
- * busier session in the same directory then wins. Read the subagent meta's
57
- * `requestShape` and count foreground subagents' writes if that shows up.
56
+ * git runs its hooks at the top of the tree, whatever directory the session
57
+ * started in and the commit ran from, so a listing by the hook's directory
58
+ * alone never sees a session started further down. A submodule or a worktree
59
+ * nested inside has a top of its own and stays out. Lazy because a session's
60
+ * directory costs a read, and a commit only wants the first one; `readsElsewhere`
61
+ * limits those reads for a caller nobody can keep waiting.
58
62
  */
59
- export async function newestSession(
63
+ export async function* sessionsInTree(
64
+ agent: Agent,
60
65
  cwd: string,
61
- ): Promise<{ agent: Agent; file: SessionFile } | undefined> {
62
- let best: { agent: Agent; file: SessionFile } | undefined;
66
+ readsElsewhere = Infinity,
67
+ ): AsyncGenerator<DiscoveredSession> {
68
+ const top = await workTreeTop(cwd);
69
+ if (!top) {
70
+ yield* await agent.sessionsIn(cwd);
71
+ return;
72
+ }
73
+
74
+ const [recorded, around] = agent.sessionsAround
75
+ ? await agent.sessionsAround(cwd, top)
76
+ : [await agent.sessionsIn(cwd), await agent.allSessions(top)];
77
+ const here = new Set(recorded.map((file) => file.path));
78
+ const oldest = Date.now() - SWEEP_WINDOW_MS;
79
+ const elsewhere = around.filter((file) => !here.has(file.path) && file.modifiedAt.getTime() >= oldest);
80
+
81
+ const listed: DiscoveredSession[] = [...recorded, ...elsewhere];
82
+ const tops = new Map<string, string | undefined>();
83
+ for (const file of listed.sort((a, b) => lastActive(b) - lastActive(a))) {
84
+ if (here.has(file.path)) {
85
+ yield file;
86
+ continue;
87
+ }
88
+ if (readsElsewhere <= 0) continue;
89
+ readsElsewhere -= 1;
90
+ // Listed a moment ago, and one that cannot be opened now says nothing of the rest.
91
+ const directory = file.cwd ?? (await agent.cwdOf(file.path).catch(() => undefined));
92
+ if (!directory || !isWithin(top, directory)) continue;
93
+ if (!tops.has(directory)) tops.set(directory, await workTreeTop(directory));
94
+ if (tops.get(directory) === top) yield { ...file, cwd: directory };
95
+ }
96
+ }
97
+
98
+ /**
99
+ * This work tree's sessions across every CLI installed here, newest first by when
100
+ * the session itself last wrote: the ones that wrote within `withinMs`, and the
101
+ * newest however long ago that was.
102
+ *
103
+ * ponytail: a subagent working for a session does not move that time, so one that
104
+ * commits after its session has waited on it for longer than `withinMs` is not
105
+ * looked for. Walk by `modifiedAt`, which counts subagents, if that shows up. And
106
+ * a hook is waiting on this, so with other repositories' sessions all through the
107
+ * tree, as with dotfiles at $HOME, only `READS_FROM_A_HOOK` of those further down
108
+ * are looked into, and a session of this tree behind them is not found.
109
+ */
110
+ const READS_FROM_A_HOOK = 32;
111
+
112
+ export async function latestSessions(
113
+ cwd: string,
114
+ withinMs: number,
115
+ ): Promise<{ agent: Agent; file: DiscoveredSession }[]> {
116
+ const oldest = Date.now() - withinMs;
117
+ const found: { agent: Agent; file: DiscoveredSession }[] = [];
63
118
 
64
119
  for (const agent of await presentAgents()) {
65
- const file = (await agent.sessionsIn(cwd))[0];
66
- if (!file) continue;
67
- if (!best || lastActive(file) > lastActive(best.file)) {
68
- best = { agent, file };
120
+ for await (const file of sessionsInTree(agent, cwd, READS_FROM_A_HOOK)) {
121
+ found.push({ agent, file });
122
+ if (lastActive(file) < oldest) break;
69
123
  }
70
124
  }
71
125
 
72
- return best;
126
+ found.sort((a, b) => lastActive(b.file) - lastActive(a.file));
127
+ return found.filter(({ file }, index) => index === 0 || lastActive(file) >= oldest);
73
128
  }
@@ -4,8 +4,10 @@ import { access, mkdir, readFile, readdir, realpath, stat, writeFile } from "nod
4
4
  import { homedir } from "node:os";
5
5
  import { basename, delimiter, dirname, join } from "node:path";
6
6
  import { parkAlert } from "../alert.js";
7
+ import { maskRootsFor } from "../git.js";
8
+ import { NPX_ISY, NPX_ISY_CACHED } from "../hook.js";
7
9
  import { parseLines } from "../parser.js";
8
- import { firstLine, firstLines, lastActive, pendingAlertPath } from "../paths.js";
10
+ import { firstLine, firstLines, isWithin, lastActive, pendingAlertPath } from "../paths.js";
9
11
  import { redactLines } from "../redact.js";
10
12
  import type { DiscoveredSession, ParsedSession, SessionFile } from "../types.js";
11
13
  import { isKimiCliWire, kimiCliWireToClaudeRecords } from "./kimi-cli-wire.js";
@@ -403,11 +405,19 @@ const OLDER_UPLOADS = [
403
405
  "npx isy upload --hook",
404
406
  "npx isy upload --hook --agent kimi",
405
407
  "npx @nightloom/isy upload --hook --agent kimi",
408
+ "exec 3<&0; nohup npx @nightloom/isy upload --hook --agent kimi <&3 >/dev/null 2>&1 &",
406
409
  ];
407
410
 
408
- const START_COMMAND = "npx @nightloom/isy check --hook --agent kimi";
409
- const KIMI_CLI_UPLOAD = "exec 3<&0; nohup npx @nightloom/isy upload --hook --agent kimi <&3 >/dev/null 2>&1 &";
410
- const KIMI_CODE_UPLOAD = "npx @nightloom/isy upload --hook --agent kimi";
411
+ /** The commands an older isy wrote for the session start, in any home. */
412
+ const OLDER_STARTS = [
413
+ "npx isy check --hook --agent kimi",
414
+ "npx @nightloom/isy check --hook --agent kimi",
415
+ `${NPX_ISY} check --hook --agent kimi`,
416
+ ];
417
+
418
+ const START_COMMAND = `${NPX_ISY_CACHED} check --hook --agent kimi`;
419
+ const KIMI_CLI_UPLOAD = `exec 3<&0; nohup ${NPX_ISY} upload --hook --agent kimi <&3 >/dev/null 2>&1 &`;
420
+ const KIMI_CODE_UPLOAD = `${NPX_ISY} upload --hook --agent kimi`;
411
421
 
412
422
  /**
413
423
  * Every command an isy has written into a Kimi config, for either CLI — Kimi
@@ -416,7 +426,7 @@ const KIMI_CODE_UPLOAD = "npx @nightloom/isy upload --hook --agent kimi";
416
426
  */
417
427
  const ISY_COMMANDS = new Set([
418
428
  START_COMMAND,
419
- "npx isy check --hook --agent kimi",
429
+ ...OLDER_STARTS,
420
430
  KIMI_CLI_UPLOAD,
421
431
  KIMI_CODE_UPLOAD,
422
432
  ...OLDER_UPLOADS,
@@ -435,7 +445,7 @@ function kimiHooks(home = kimiHome()): AgentHook[] {
435
445
  const start: AgentHook = {
436
446
  event: "SessionStart",
437
447
  command: START_COMMAND,
438
- superseded: ["npx isy check --hook --agent kimi"],
448
+ superseded: OLDER_STARTS,
439
449
  };
440
450
 
441
451
  if (!isKimiCodeHome(home)) {
@@ -469,7 +479,7 @@ function kimiHooks(home = kimiHome()): AgentHook[] {
469
479
  {
470
480
  event: "SessionEnd",
471
481
  command: KIMI_CODE_UPLOAD,
472
- superseded: OLDER_UPLOADS.slice(0, 2),
482
+ superseded: OLDER_UPLOADS,
473
483
  },
474
484
  // SessionStart and SessionEnd are observation-only in Kimi Code and their
475
485
  // stdout is discarded, so an alert that could not reach the terminal
@@ -821,6 +831,13 @@ export const kimiAgent: Agent = {
821
831
  return discovered(await allSessions());
822
832
  },
823
833
 
834
+ // Each session and its subagents' logs cost a stat, so what ran elsewhere goes first.
835
+ async sessionsAround(cwd: string, top: string): Promise<[SessionFile[], DiscoveredSession[]]> {
836
+ const near = (await allSessions()).filter((session) => session.cwd === undefined || session.cwd === cwd || isWithin(top, session.cwd));
837
+ const around = await discovered(near);
838
+ return [around.filter((file) => file.cwd === cwd), around];
839
+ },
840
+
824
841
  async cwdOf(path: string): Promise<string | undefined> {
825
842
  const dir = sessionDirFor(path);
826
843
  return cwdOfSession(dir, await readState(dir));
@@ -844,7 +861,10 @@ export const kimiAgent: Agent = {
844
861
  options: { extraPatterns?: readonly string[]; cwd?: string },
845
862
  ): Promise<string[]> {
846
863
  const records = await kimiRecords(path, options.cwd);
847
- return redactLines(records, { extraPatterns: options.extraPatterns, cwd: options.cwd }).lines;
864
+ return redactLines(records, {
865
+ extraPatterns: options.extraPatterns,
866
+ ...(await maskRootsFor(options.cwd)),
867
+ }).lines;
848
868
  },
849
869
 
850
870
  // Every Kimi home gets its own hooks, as its CLI runs them: a hook counts as
@@ -59,12 +59,20 @@ export interface Agent {
59
59
  * and names no working directory, so there is nothing to scope the scan to.
60
60
  *
61
61
  * `cwd` comes back set only where listing already had to read it (Kimi).
62
+ * `under` lets a listing leave out what it can tell, unread, ran elsewhere; it
63
+ * is a saving and no promise that the rest ran there.
62
64
  */
63
- allSessions(): Promise<DiscoveredSession[]>;
65
+ allSessions(under?: string): Promise<DiscoveredSession[]>;
66
+ /**
67
+ * `sessionsIn(cwd)` and what `allSessions(top)` would say, from one listing, for a
68
+ * CLI that reads every session on the machine to answer either. A commit asks both.
69
+ */
70
+ sessionsAround?(cwd: string, top: string): Promise<[SessionFile[], DiscoveredSession[]]>;
64
71
  /**
65
72
  * Where one session ran, for the adapters whose listing does not know. Separate
66
73
  * from `allSessions` on purpose: it costs a read, and a sweep only needs it for
67
- * the sessions that grew since last time.
74
+ * the sessions that grew since last time. Undefined for a file that cannot be
75
+ * read, too: its callers walk every session there is, and one must not end that.
68
76
  */
69
77
  cwdOf(path: string): Promise<string | undefined>;
70
78
  /** Parse one transcript without redacting it — for local inspection only. */
@@ -74,7 +82,8 @@ export interface Agent {
74
82
  /**
75
83
  * Redacted, Claude-record-shaped JSONL lines, ready for parseLines and upload.
76
84
  * Agents whose native format differs normalize here, so everything downstream
77
- * — including the server's own parseLines — sees one shape.
85
+ * — including the server's own parseLines — sees one shape. `cwd` is where the
86
+ * session ran; paths come out from the top of the repository around it.
78
87
  */
79
88
  redactedLines(
80
89
  path: string,
package/src/api.ts CHANGED
@@ -61,7 +61,9 @@ export type UploadOutcome =
61
61
  /** Where the account stands with its plan, as of this upload. */
62
62
  plan?: PlanStatus;
63
63
  }
64
- | { status: "retry"; error: string }
64
+ // `rejected`: the server read the payload and refused it, so the same bytes
65
+ // would get the same answer at once.
66
+ | { status: "retry"; error: string; rejected?: boolean }
65
67
  | { status: "permanent"; error: string }
66
68
  // Too many uploads this hour. Not a failure: the session is kept as it is,
67
69
  // and the run stops rather than spending the retries of every session behind
@@ -130,7 +132,7 @@ function classify(status: number, body: string): UploadOutcome {
130
132
  // reads the same as one in the payload, and a session passed over until it
131
133
  // changes would be lost with every other that ended while the bug was live.
132
134
  if (status === 400 || status === 422) {
133
- return { status: "retry", error: `server rejected the payload (${status}) ${body}`.trim() };
135
+ return { status: "retry", rejected: true, error: `server rejected the payload (${status}) ${body}`.trim() };
134
136
  }
135
137
  return { status: "retry", error: `server returned ${status} ${body}`.trim() };
136
138
  }
@@ -1,13 +1,18 @@
1
- import { relative } from "node:path";
2
- import { newestSession } from "../agents/index.js";
3
- import type { AgentId } from "../agents/index.js";
1
+ import { realpath } from "node:fs/promises";
2
+ import { basename, dirname, relative, resolve } from "node:path";
3
+ import { latestSessions } from "../agents/index.js";
4
+ import type { Agent, AgentId } from "../agents/index.js";
4
5
  import { readConfig } from "../config.js";
5
- import { headSha } from "../git.js";
6
- import { parseLines } from "../parser.js";
6
+ import { addedLines, blobsOf, branchExists, commitHeadReplaced, currentBranch, filesInHead, filesMergedIntoHead, headSha, maskRootsFor } from "../git.js";
7
+ import { errorMessage, logError } from "../log.js";
8
+ import { maskPath } from "../mask-paths.js";
9
+ import { gitCommands, mayWrite, mcpOnlyReads, parseLines, shellOnlyLooks, shellWrites } from "../parser.js";
7
10
  import { spawnIsy } from "../spawn.js";
8
11
  import { runStage0 } from "../stage0.js";
9
- import { candidateKey, readState, unseen, updateSession, withCommit } from "../state.js";
10
- import type { Candidate } from "../types.js";
12
+ import { candidateKey, namedBranch, readState, unseen, updateSession, withCommit } from "../state.js";
13
+ import type { ClientState, SessionState } from "../state.js";
14
+ import type { MaskRoots } from "../mask-paths.js";
15
+ import type { Candidate, DiscoveredSession, ParsedSession, ToolUse } from "../types.js";
11
16
 
12
17
  export interface CommitReport {
13
18
  agent?: AgentId;
@@ -26,6 +31,320 @@ const COLUMN_GAP = 2;
26
31
  const MAX_LINES = 6;
27
32
  /** Past this, a category is one counted line rather than one line per file. */
28
33
  const MAX_PER_CATEGORY = 2;
34
+ /** The longest Claude Code lets a shell call run, so how long one can read as running. */
35
+ const SHELL_CALL_MS = 10 * 60_000;
36
+ /**
37
+ * What telling sessions apart may add to a hook someone is waiting on, at every
38
+ * pick of a rebase. Time is checked between reads and only size before one, so
39
+ * both: 16 MB of transcript takes about half a second to read as it is.
40
+ */
41
+ const TELLING_APART_MS = 1_000;
42
+ const TELLING_APART_BYTES = 16 * 1024 * 1024;
43
+ /** How much of the smaller of two pieces of work they must share to be the same work. */
44
+ const SAME_WORK_SHARE = 0.1;
45
+
46
+ /** The transcript holds a shell call before the command starts, and its answer only after. */
47
+ function isRunning(use: ToolUse): boolean {
48
+ return (
49
+ (use.name === "Bash" || use.name === "PowerShell") &&
50
+ use.result === undefined &&
51
+ Date.now() - Date.parse(use.timestamp ?? "") < SHELL_CALL_MS
52
+ );
53
+ }
54
+
55
+ /** A call that wrote files without naming them the way an edit tool does. */
56
+ function writesUnnamed(use: ToolUse): boolean {
57
+ const { command } = use.input;
58
+ if (use.name === "Bash" && typeof command === "string" && shellWrites(command)) return true;
59
+ return mayWrite(use.name, use.input);
60
+ }
61
+
62
+ /**
63
+ * The words of a tool call's input that could be a file's name. By word, and
64
+ * looked up rather than searched for, because a commit of thousands of files
65
+ * against a long session is a search of minutes in a hook someone is waiting on.
66
+ */
67
+ export function wordsOf(input: unknown): string[] {
68
+ return JSON.stringify(input).split(/\\.|[\s/"'`$=:;,|&<>(){}[\]*?!#]+/);
69
+ }
70
+
71
+ /** What `wordsOf` takes a name apart at, so a name holding one has to be looked for whole. */
72
+ const NAME_BREAK = /[\s"'`$=:;,|&<>(){}[\]*?!#]/;
73
+
74
+ /** By the file's own name, or the name of the submodule it lies in, whose pointer is all a commit shows of it. */
75
+ function holdsFile(committed: Set<string>, file: string): boolean {
76
+ for (let path = file; path !== dirname(path); path = dirname(path)) {
77
+ if (committed.has(path)) return true;
78
+ }
79
+ return false;
80
+ }
81
+
82
+ /**
83
+ * Whether HEAD holds what the session left: an edit past `since` to a file HEAD
84
+ * changed, or a write it could not name that mentions one.
85
+ *
86
+ * ponytail: by file, not by line, and for the unnamed writes by base name, so a
87
+ * commit that happens to touch such a file counts, and a script that writes files
88
+ * it never mentions does not, nor a shell-written file a human renames before
89
+ * committing, nor what a script left behind that a human commits on a new branch
90
+ * after the session's last mark. Compare with `FileEdit.newString` and read the
91
+ * redirect targets if any of them shows up.
92
+ */
93
+ async function carriesItsWork(
94
+ session: ParsedSession,
95
+ since: number,
96
+ committed: Set<string>,
97
+ names: Set<string>,
98
+ directory: string,
99
+ rootsIn: (directory: string) => Promise<MaskRoots>,
100
+ ): Promise<boolean> {
101
+ // A transcript read as it is holds paths as the agent wrote them, from where it
102
+ // stood or in full, and git names a file from the top of the tree.
103
+ const real = (path: string) => realpath(path).catch(() => path);
104
+ for (const [path, edits] of session.fileEdits) {
105
+ if (!edits.some((edit) => edit.recordIndex >= since)) continue;
106
+ const roots = await rootsIn(directory);
107
+ if (holdsFile(committed, maskPath(resolve(directory, path), roots))) return true;
108
+ // Through a link, git names the file where it really is.
109
+ if (roots.cwd && holdsFile(committed, relative(await real(roots.cwd), await real(resolve(directory, path))))) return true;
110
+ }
111
+
112
+ const broken = [...names].filter((name) => NAME_BREAK.test(name));
113
+ return session.toolUses.some((use) => {
114
+ if (use.recordIndex < since || !writesUnnamed(use)) return false;
115
+ if (wordsOf(use.input).some((word) => names.has(word))) return true;
116
+ if (broken.length === 0) return false;
117
+ // Whether quoted or with a backslash in front of what breaks it, the whole name is there.
118
+ const text = JSON.stringify(use.input).replace(/\\+/g, "");
119
+ return broken.some((name) => text.includes(name));
120
+ });
121
+ }
122
+
123
+ /** What a commit holds, asked once however many sessions it is held up against. */
124
+ interface HeadWork {
125
+ cwd: string;
126
+ files: string[];
127
+ blobs: () => Promise<Map<string, Set<string>>>;
128
+ lines: () => Promise<Set<string> | undefined>;
129
+ replaced: () => Promise<string | undefined>;
130
+ rootsIn: (directory: string) => Promise<MaskRoots>;
131
+ }
132
+
133
+ function once<T>(ask: () => Promise<T>): () => Promise<T> {
134
+ let answer: Promise<T> | undefined;
135
+ return () => (answer ??= ask());
136
+ }
137
+
138
+ /** Lines by what they say: indentation, line endings and a formatter's punctuation change without changing that, and `}` says nothing. */
139
+ function linesSaying(lines: string[]): Set<string> {
140
+ return new Set(lines.map((line) => line.replace(/[^\p{L}\p{N}]+/gu, "")).filter((line) => line.length > 0));
141
+ }
142
+
143
+ /**
144
+ * Whether HEAD's lines are in good part lines the session wrote, or its marks added,
145
+ * in any file: work moved here and changed on the way, by a human's touch, a formatter,
146
+ * a rename, or a side that changed the same file. When git cannot say, the session
147
+ * keeps the commit, as it did before branches were looked at.
148
+ *
149
+ * ponytail: a share of whole lines. Work rewritten down to under a tenth of both sides
150
+ * is not recognised, nor a redo by hand that changes the only line a session wrote, and
151
+ * small commits that happen to share `return nil` are, moving the session to that branch
152
+ * as it always did. Weigh lines by how rare they are if that shows up.
153
+ */
154
+ async function sharesItsLines(session: ParsedSession, marks: string[], head: HeadWork): Promise<boolean> {
155
+ const [inHead, inMarks] = await Promise.all([head.lines(), marks.length > 0 ? addedLines(head.cwd, marks, TELLING_APART_MS) : []]);
156
+ if (!inHead || !inMarks) return true;
157
+ const written = [...session.fileEdits.values()].flat().flatMap((edit) => (edit.newString ?? edit.content ?? "").split("\n"));
158
+ const work = linesSaying([...inMarks, ...written]);
159
+ const shared = [...inHead].filter((line) => work.has(line)).length;
160
+ return shared > 0 && shared >= SAME_WORK_SHARE * Math.min(inHead.size, work.size);
161
+ }
162
+
163
+ /**
164
+ * Whether HEAD holds work from behind the session's last mark, however it got here:
165
+ * an amend or a pick of one of its marks, by git's own record; a blob one of its marks
166
+ * left, which is an amend, a redo, a pick or a rebase on another branch, or a rename of
167
+ * one; a file of a mark that a merge settled to other than what stood on its branch; a
168
+ * file it edited that no mark took, which is its edits split over commits; or lines it
169
+ * wrote (`sharesItsLines`). A clean merge shows none of these: it changes no file of its own.
170
+ */
171
+ async function holdsMovedWork(
172
+ session: ParsedSession,
173
+ state: SessionState,
174
+ directory: string,
175
+ head: HeadWork,
176
+ ): Promise<boolean> {
177
+ const marks = state.commits?.map((mark) => mark.sha) ?? [];
178
+ if (marks.length > 0) {
179
+ const replaced = await head.replaced();
180
+ if (replaced && marks.includes(replaced)) return true;
181
+ const marked = await blobsOf(head.cwd, marks);
182
+ // A deleted file's blob is all zeros, and says something only of that file.
183
+ const left = new Set([...marked.values()].flatMap((blobs) => [...blobs]).filter((blob) => !/^0+$/.test(blob)));
184
+ for (const [file, blobs] of await head.blobs()) {
185
+ if ([...blobs].some((blob) => left.has(blob) || marked.get(file)?.has(blob))) return true;
186
+ }
187
+ const settled = head.files.filter((file) => marked.has(file));
188
+ if (settled.length > 0) {
189
+ const merged = new Set(await filesMergedIntoHead(head.cwd));
190
+ if (settled.some((file) => merged.has(file))) return true;
191
+ }
192
+ const unmarked = head.files.filter((file) => !marked.has(file));
193
+ if (await carriesItsWork(session, 0, new Set(unmarked), new Set(unmarked.map((file) => basename(file))), directory, head.rootsIn)) return true;
194
+ }
195
+ return sharesItsLines(session, marks, head);
196
+ }
197
+
198
+ /** How an agent leaves for another branch itself. A rename is told by the old name being gone. */
199
+ const BRANCH_MOVERS = new Set(["switch", "checkout"]);
200
+
201
+ function movesBranch(use: ToolUse): boolean {
202
+ const { command } = use.input;
203
+ return typeof command === "string" && gitCommands(command).some((subcommand) => BRANCH_MOVERS.has(subcommand));
204
+ }
205
+
206
+ /** A call after which nobody can say what it left in the tree. */
207
+ function leavesUnknown(use: ToolUse): boolean {
208
+ const { command } = use.input;
209
+ if (use.name === "Bash") return typeof command !== "string" || !shellOnlyLooks(command);
210
+ return mayWrite(use.name, use.input) || (use.name.startsWith("mcp__") && !mcpOnlyReads(use.name));
211
+ }
212
+
213
+ /**
214
+ * Whether a commit that does not show itself to be this session's can still be. A
215
+ * commit is what moves a session to another pull request, so one that ended on
216
+ * another branch must not take every commit made here afterwards. It is refused only
217
+ * on evidence: the session stands on another branch, and since its last mark it has
218
+ * done nothing but look and write files it named, which are not these.
219
+ *
220
+ * Its own branch is the one its row stands on (`SessionState.branch`) before the
221
+ * one its transcript last named, which a resume that only read has moved. The
222
+ * transcript wins when the row has fallen behind it: the agent left for that branch
223
+ * itself, or the name in the row is on no branch any more. No branch known, as with
224
+ * a session on a detached HEAD, is no reason to refuse.
225
+ *
226
+ * ponytail: a session that ran a build or `npm test` since its last mark takes an
227
+ * unrelated commit on another branch, as it did before branches were looked at,
228
+ * since a command line cannot say what those wrote. List such a runner in
229
+ * `shellOnlyLooks` if one proves to move sessions to the wrong pull request.
230
+ */
231
+ async function isItsCommit(
232
+ session: ParsedSession,
233
+ state: SessionState,
234
+ branch: string | undefined,
235
+ cwd: string,
236
+ ): Promise<boolean> {
237
+ const own = state.branch ?? session.meta.gitBranch;
238
+ if (!branch || !own || own === "HEAD" || own === branch) return true;
239
+ const since = state.commits?.at(-1)?.records ?? 0;
240
+ const calls = session.toolUses.filter((use) => use.recordIndex >= since);
241
+ if (calls.some(leavesUnknown)) return true;
242
+ // A CLI that records no branch leaves the row as all there is to go on.
243
+ if ((session.meta.gitBranch ?? branch) !== branch) return false;
244
+ return calls.some(movesBranch) || !(await branchExists(cwd, own));
245
+ }
246
+
247
+ /** The git commands that fire the post-commit hook, each pick of a rebase included. */
248
+ const COMMIT_MAKERS = new Set(["commit", "rebase", "cherry-pick", "revert", "am", "pull"]);
249
+
250
+ /** A shell call still running whose command makes commits, so this one may have come out of it. */
251
+ function isCommitting(use: ToolUse): boolean {
252
+ const { command } = use.input;
253
+ if (typeof command !== "string" || !isRunning(use)) return false;
254
+ return gitCommands(command).some((subcommand) => COMMIT_MAKERS.has(subcommand));
255
+ }
256
+
257
+ /**
258
+ * The session a commit just made goes to, of the ones in the tree newest first.
259
+ * The hook is not told who made it, and the newest is not always that one: an
260
+ * agent waiting on its `git commit` writes nothing while another session goes on
261
+ * writing. So one making a commit this moment comes first, then one whose work the
262
+ * commit holds, as when a human commits what an agent left, and only then the newest
263
+ * that can take it (`isItsCommit`) or whose marked work moved here (`holdsMovedWork`).
264
+ *
265
+ * Read as they are, because a transcript's redaction costs five times its read and
266
+ * only the session this ends on goes up. Past the first, one the budget does not
267
+ * reach is left out rather than waited for.
268
+ *
269
+ * ponytail: left out by size, so a long session running `git commit` beside a
270
+ * shorter one that goes on writing loses the commit to it, as it always did. Two
271
+ * sessions running git at once go the same way, and so does a commit one of them is
272
+ * making in another repository. Read the tail of a long one if that shows up.
273
+ *
274
+ * ponytail: the budget is per commit, so each pick of a rebase pays it afresh, and tens
275
+ * of sessions active in one tree add about half a second a pick: a rebase of 12 with
276
+ * 100 such sessions holds 9 s against 2.6 s for no telling apart. A budget per rebase
277
+ * would leave the later picks of any long one untold, so keep it there only if that
278
+ * many sessions in one tree turn out to be common.
279
+ */
280
+ async function sessionOfCommit(
281
+ latest: Awaited<ReturnType<typeof latestSessions>>,
282
+ branch: string | undefined,
283
+ cwd: string,
284
+ states: ClientState,
285
+ ): Promise<{ agent: Agent; file: DiscoveredSession } | { skipped: string }> {
286
+ // Alone and on its own branch there is nothing to tell apart.
287
+ const alone = latest.length === 1 && latest[0]!;
288
+ if (alone && branch && states[alone.file.path]?.branch === branch) return alone;
289
+
290
+ const deadline = Date.now() + TELLING_APART_MS;
291
+ let bytes = 0;
292
+ const read = [];
293
+ for (const { agent, file } of latest) {
294
+ if (read.length > 0) {
295
+ if (Date.now() >= deadline) break;
296
+ if (bytes + file.sizeBytes > TELLING_APART_BYTES) continue;
297
+ bytes += file.sizeBytes;
298
+ }
299
+ let session: ParsedSession;
300
+ try {
301
+ session = await agent.parsedSession(file.path, file.cwd ?? cwd);
302
+ } catch {
303
+ // Listed a moment ago: gone since, or never this user's to read. One such
304
+ // file says nothing about the commit, and the others are still there to ask.
305
+ continue;
306
+ }
307
+ const entry = { agent, file, session };
308
+ if (session.toolUses.some(isCommitting)) return entry;
309
+ read.push(entry);
310
+ }
311
+ if (read.length === 0) return { skipped: "no session here could be read" };
312
+
313
+ const files = await filesInHead(cwd);
314
+ const committed = new Set(files);
315
+ const names = new Set(files.map((file) => basename(file)));
316
+ const roots = new Map<string, Promise<MaskRoots>>();
317
+ const rootsIn = (directory: string) => {
318
+ if (!roots.has(directory)) roots.set(directory, maskRootsFor(directory));
319
+ return roots.get(directory)!;
320
+ };
321
+ const head: HeadWork = {
322
+ cwd,
323
+ files,
324
+ blobs: once(() => blobsOf(cwd, ["HEAD"])),
325
+ lines: once(async () => {
326
+ const lines = await addedLines(cwd, ["HEAD"], TELLING_APART_MS);
327
+ return lines && linesSaying(lines);
328
+ }),
329
+ replaced: once(() => commitHeadReplaced(cwd)),
330
+ rootsIn,
331
+ };
332
+ // Asking takes the git calls above and what a directory or a branch adds, so
333
+ // however many were read, it gets the same again and stops there.
334
+ const askUntil = deadline + TELLING_APART_MS;
335
+ for (const entry of read) {
336
+ const since = states[entry.file.path]?.commits?.at(-1)?.records ?? 0;
337
+ if (await carriesItsWork(entry.session, since, committed, names, entry.file.cwd ?? cwd, rootsIn)) return entry;
338
+ if (Date.now() >= askUntil) break;
339
+ }
340
+ for (const entry of read) {
341
+ const state = states[entry.file.path] ?? {};
342
+ if (await isItsCommit(entry.session, state, branch, cwd)) return entry;
343
+ if (await holdsMovedWork(entry.session, state, entry.file.cwd ?? cwd, head)) return entry;
344
+ if (Date.now() >= askUntil) break;
345
+ }
346
+ return { skipped: "the sessions here are on another branch and this commit holds none of their work" };
347
+ }
29
348
 
30
349
  export async function runCommit(
31
350
  options: { json?: boolean; noUpload?: boolean },
@@ -33,33 +352,50 @@ export async function runCommit(
33
352
  ): Promise<CommitReport> {
34
353
  const report: CommitReport = { cwd, records: 0, candidates: [], uploaded: "not-attempted" };
35
354
 
36
- const newest = await newestSession(cwd);
37
- if (!newest) {
355
+ const latest = await latestSessions(cwd, SHELL_CALL_MS);
356
+ if (latest.length === 0) {
38
357
  report.skipped = "no session recorded for this directory";
39
358
  return report;
40
359
  }
41
360
 
42
- report.agent = newest.agent.id;
43
- report.sessionId = newest.file.sessionId;
44
361
  report.sha = await headSha(cwd);
45
362
 
46
363
  const config = await readConfig();
47
- const lines = await newest.agent.redactedLines(newest.file.path, {
48
- extraPatterns: config.extraRedactPatterns,
49
- cwd,
50
- });
51
- const session = parseLines(lines);
364
+ const states = await readState();
365
+ const branch = await currentBranch(cwd);
366
+ const chosen = await sessionOfCommit(latest, branch, cwd, states);
367
+ if ("skipped" in chosen) {
368
+ report.skipped = chosen.skipped;
369
+ return report;
370
+ }
371
+ const { agent, file } = chosen;
372
+ report.agent = agent.id;
373
+ report.sessionId = file.sessionId;
374
+
375
+ let session: ParsedSession;
376
+ try {
377
+ const lines = await agent.redactedLines(file.path, {
378
+ extraPatterns: config.extraRedactPatterns,
379
+ cwd: file.cwd ?? cwd,
380
+ });
381
+ session = parseLines(lines);
382
+ } catch (error) {
383
+ report.skipped = "the session this commit goes to could not be read";
384
+ logError(`commit: ${file.sessionId} could not be read: ${errorMessage(error)}`);
385
+ return report;
386
+ }
52
387
  report.records = session.records.length;
388
+ const state = states[file.path] ?? {};
53
389
 
54
390
  // Stage 0 always sees the whole transcript: the detectors reason across the
55
391
  // session — an abandoned branch forks early, a revert compares a late edit
56
392
  // with an early one — so a window would cost exactly the signals worth having.
57
393
  const stage0 = runStage0(session);
58
- const state = (await readState())[newest.file.path] ?? {};
59
394
  report.candidates = unseen(state, stage0.candidates);
60
395
 
61
- await updateSession(newest.file.path, (current) => ({
396
+ await updateSession(file.path, (current) => ({
62
397
  ...current,
398
+ branch: namedBranch(branch, current.branch),
63
399
  shown: [...new Set([...(current.shown ?? []), ...stage0.candidates.map(candidateKey)])],
64
400
  // What the transcript held at this commit, so a record index can later be
65
401
  // told which commit it belongs to.
@@ -101,7 +101,7 @@ function checkProblems(check: CheckReport, verified: string[]): HealthProblem[]
101
101
  files: [],
102
102
  });
103
103
  } else if (check.gitHook === "installed") {
104
- verified.push("the repository's post-commit and pre-push hooks carry the isy block");
104
+ verified.push("the repository's post-commit and pre-push hooks carry the isy block, or are not shell scripts and were left alone");
105
105
  }
106
106
 
107
107
  if (check.api !== undefined && check.api !== "ok") {