@nightloom/isy 1.0.1 → 1.0.3

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 (152) hide show
  1. package/README.md +1 -1
  2. package/dist/agents/claude.js +18 -7
  3. package/dist/agents/claude.js.map +1 -1
  4. package/dist/agents/codex-records.js +16 -2
  5. package/dist/agents/codex-records.js.map +1 -1
  6. package/dist/agents/index.d.ts +16 -5
  7. package/dist/agents/index.js +21 -6
  8. package/dist/agents/index.js.map +1 -1
  9. package/dist/agents/kimi-cli-wire.d.ts +17 -0
  10. package/dist/agents/kimi-cli-wire.js +312 -0
  11. package/dist/agents/kimi-cli-wire.js.map +1 -0
  12. package/dist/agents/kimi-config.d.ts +29 -0
  13. package/dist/agents/kimi-config.js +193 -0
  14. package/dist/agents/kimi-config.js.map +1 -0
  15. package/dist/agents/kimi-records.d.ts +0 -6
  16. package/dist/agents/kimi-records.js +51 -31
  17. package/dist/agents/kimi-records.js.map +1 -1
  18. package/dist/agents/kimi-tools.d.ts +30 -0
  19. package/dist/agents/kimi-tools.js +100 -0
  20. package/dist/agents/kimi-tools.js.map +1 -0
  21. package/dist/agents/kimi-wire.d.ts +8 -0
  22. package/dist/agents/kimi-wire.js +245 -80
  23. package/dist/agents/kimi-wire.js.map +1 -1
  24. package/dist/agents/kimi.d.ts +13 -2
  25. package/dist/agents/kimi.js +633 -114
  26. package/dist/agents/kimi.js.map +1 -1
  27. package/dist/agents/timeline.d.ts +36 -0
  28. package/dist/agents/timeline.js +61 -0
  29. package/dist/agents/timeline.js.map +1 -0
  30. package/dist/agents/types.d.ts +12 -0
  31. package/dist/api.d.ts +3 -0
  32. package/dist/api.js +1 -1
  33. package/dist/api.js.map +1 -1
  34. package/dist/commands/analyze.d.ts +2 -0
  35. package/dist/commands/analyze.js +63 -6
  36. package/dist/commands/analyze.js.map +1 -1
  37. package/dist/commands/check.js +11 -0
  38. package/dist/commands/check.js.map +1 -1
  39. package/dist/commands/health.js +2 -2
  40. package/dist/commands/health.js.map +1 -1
  41. package/dist/commands/init.js +25 -2
  42. package/dist/commands/init.js.map +1 -1
  43. package/dist/commands/upload.d.ts +2 -0
  44. package/dist/commands/upload.js +38 -7
  45. package/dist/commands/upload.js.map +1 -1
  46. package/dist/detectors.js +40 -5
  47. package/dist/detectors.js.map +1 -1
  48. package/dist/hook.d.ts +12 -5
  49. package/dist/hook.js +73 -17
  50. package/dist/hook.js.map +1 -1
  51. package/dist/lines.d.ts +8 -0
  52. package/dist/lines.js +32 -0
  53. package/dist/lines.js.map +1 -0
  54. package/dist/mask-paths.js +31 -15
  55. package/dist/mask-paths.js.map +1 -1
  56. package/dist/parser.d.ts +41 -0
  57. package/dist/parser.js +345 -36
  58. package/dist/parser.js.map +1 -1
  59. package/dist/paths.d.ts +4 -0
  60. package/dist/paths.js +39 -39
  61. package/dist/paths.js.map +1 -1
  62. package/dist/plan.d.ts +3 -1
  63. package/dist/plan.js +5 -1
  64. package/dist/plan.js.map +1 -1
  65. package/dist/queue.d.ts +14 -2
  66. package/dist/queue.js +25 -3
  67. package/dist/queue.js.map +1 -1
  68. package/dist/redact.d.ts +17 -0
  69. package/dist/redact.js +202 -12
  70. package/dist/redact.js.map +1 -1
  71. package/dist/state.d.ts +15 -6
  72. package/dist/state.js +25 -9
  73. package/dist/state.js.map +1 -1
  74. package/dist/subagents.d.ts +53 -0
  75. package/dist/subagents.js +243 -0
  76. package/dist/subagents.js.map +1 -0
  77. package/dist/test/check.test.js +25 -3
  78. package/dist/test/check.test.js.map +1 -1
  79. package/dist/test/codex.test.js +21 -0
  80. package/dist/test/codex.test.js.map +1 -1
  81. package/dist/test/commit.test.js +9 -1
  82. package/dist/test/commit.test.js.map +1 -1
  83. package/dist/test/hook.test.js +66 -2
  84. package/dist/test/hook.test.js.map +1 -1
  85. package/dist/test/init.test.d.ts +1 -0
  86. package/dist/test/init.test.js +102 -0
  87. package/dist/test/init.test.js.map +1 -0
  88. package/dist/test/kimi-cli.test.d.ts +1 -0
  89. package/dist/test/kimi-cli.test.js +231 -0
  90. package/dist/test/kimi-cli.test.js.map +1 -0
  91. package/dist/test/kimi-code.test.d.ts +1 -0
  92. package/dist/test/kimi-code.test.js +302 -0
  93. package/dist/test/kimi-code.test.js.map +1 -0
  94. package/dist/test/kimi-config.test.d.ts +1 -0
  95. package/dist/test/kimi-config.test.js +224 -0
  96. package/dist/test/kimi-config.test.js.map +1 -0
  97. package/dist/test/kimi-hook.test.d.ts +1 -0
  98. package/dist/test/kimi-hook.test.js +163 -0
  99. package/dist/test/kimi-hook.test.js.map +1 -0
  100. package/dist/test/kimi-listing.test.d.ts +1 -0
  101. package/dist/test/kimi-listing.test.js +193 -0
  102. package/dist/test/kimi-listing.test.js.map +1 -0
  103. package/dist/test/kimi.test.js +52 -7
  104. package/dist/test/kimi.test.js.map +1 -1
  105. package/dist/test/mask-paths.test.js +31 -0
  106. package/dist/test/mask-paths.test.js.map +1 -1
  107. package/dist/test/parser.test.js +208 -5
  108. package/dist/test/parser.test.js.map +1 -1
  109. package/dist/test/plan.test.js +7 -6
  110. package/dist/test/plan.test.js.map +1 -1
  111. package/dist/test/redact.test.js +245 -8
  112. package/dist/test/redact.test.js.map +1 -1
  113. package/dist/test/stage0.test.js +49 -0
  114. package/dist/test/stage0.test.js.map +1 -1
  115. package/dist/test/subagents.test.d.ts +1 -0
  116. package/dist/test/subagents.test.js +290 -0
  117. package/dist/test/subagents.test.js.map +1 -0
  118. package/dist/test/sweep.test.js +25 -0
  119. package/dist/test/sweep.test.js.map +1 -1
  120. package/dist/test/upload.test.js +68 -6
  121. package/dist/test/upload.test.js.map +1 -1
  122. package/dist/types.d.ts +16 -0
  123. package/package.json +5 -3
  124. package/src/agents/claude.ts +17 -7
  125. package/src/agents/codex-records.ts +17 -2
  126. package/src/agents/index.ts +21 -6
  127. package/src/agents/kimi-cli-wire.ts +350 -0
  128. package/src/agents/kimi-config.ts +210 -0
  129. package/src/agents/kimi-records.ts +52 -29
  130. package/src/agents/kimi-tools.ts +128 -0
  131. package/src/agents/kimi-wire.ts +270 -82
  132. package/src/agents/kimi.ts +653 -113
  133. package/src/agents/timeline.ts +68 -0
  134. package/src/agents/types.ts +12 -0
  135. package/src/api.ts +5 -2
  136. package/src/commands/analyze.ts +62 -6
  137. package/src/commands/check.ts +11 -0
  138. package/src/commands/health.ts +2 -2
  139. package/src/commands/init.ts +26 -2
  140. package/src/commands/upload.ts +46 -7
  141. package/src/detectors.ts +39 -5
  142. package/src/hook.ts +81 -24
  143. package/src/lines.ts +31 -0
  144. package/src/mask-paths.ts +31 -14
  145. package/src/parser.ts +356 -35
  146. package/src/paths.ts +36 -33
  147. package/src/plan.ts +4 -1
  148. package/src/queue.ts +30 -4
  149. package/src/redact.ts +218 -15
  150. package/src/state.ts +34 -9
  151. package/src/subagents.ts +292 -0
  152. package/src/types.ts +16 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Subagent logs merged into their session's by time — the rule `subagents.ts`
3
+ * applies to Claude Code's files as they stream, here for the Kimi readers,
4
+ * which hold every log in memory already.
5
+ *
6
+ * A subagent's records go in when they happened, never ahead of the event that
7
+ * started it: the detectors read order as time, and a subagent does not always
8
+ * run inside its call. One started in the background works alongside the
9
+ * session, and one resumed later goes on in the same log.
10
+ */
11
+
12
+ /** An item and the moment it stands for, in epoch milliseconds. */
13
+ export interface Timed<T> {
14
+ at: number;
15
+ item: T;
16
+ }
17
+
18
+ /**
19
+ * Each item's moment: its own, or — for one recorded without a time — the last
20
+ * moment before it in the same log, and `from` for those ahead of any.
21
+ */
22
+ export function stamped<T>(items: Iterable<T>, timeOf: (item: T) => number | undefined, from: number): Timed<T>[] {
23
+ const out: Timed<T>[] = [];
24
+ let at = from;
25
+ for (const item of items) {
26
+ at = timeOf(item) ?? at;
27
+ out.push({ at, item });
28
+ }
29
+ return out;
30
+ }
31
+
32
+ /**
33
+ * The bound a session's event puts on the subagents it started. A session that
34
+ * has named no moment yet bounds nothing: a log without times puts a subagent
35
+ * right after the event that started it, whole.
36
+ */
37
+ export function boundOf(at: number): number {
38
+ return at === -Infinity ? Infinity : at;
39
+ }
40
+
41
+ /** The logs a session has started, each waiting at its next item. */
42
+ export class Timelines<T> {
43
+ private readonly heads: { items: Timed<T>[]; next: number }[] = [];
44
+
45
+ add(items: Timed<T>[]): void {
46
+ if (items.length > 0) this.heads.push({ items, next: 0 });
47
+ }
48
+
49
+ /**
50
+ * Every item due by `at`, earliest first — on a tie, the log that started
51
+ * first. A log added while this runs is read too.
52
+ */
53
+ *until(at: number): Generator<Timed<T>> {
54
+ for (;;) {
55
+ let earliest: { items: Timed<T>[]; next: number } | undefined;
56
+ for (const head of this.heads) {
57
+ const next = head.items[head.next]!;
58
+ if (next.at <= at && (earliest === undefined || next.at < earliest.items[earliest.next]!.at)) earliest = head;
59
+ }
60
+ if (earliest === undefined) return;
61
+
62
+ const next = earliest.items[earliest.next]!;
63
+ earliest.next += 1;
64
+ if (earliest.next >= earliest.items.length) this.heads.splice(this.heads.indexOf(earliest), 1);
65
+ yield next;
66
+ }
67
+ }
68
+ }
@@ -20,6 +20,11 @@ export interface HookInput {
20
20
  export interface AgentHook {
21
21
  event: string;
22
22
  command: string;
23
+ /**
24
+ * Seconds the CLI should wait for the hook, written beside the command. A hook
25
+ * with the right command and the wrong timeout is not installed yet.
26
+ */
27
+ timeout?: number;
23
28
  /** Commands an older isy wrote for the same job: replace, never stack. */
24
29
  superseded?: string[];
25
30
  }
@@ -82,6 +87,13 @@ export interface Agent {
82
87
  hooksInstalled(): Promise<string[]>;
83
88
  installHooks(): Promise<"installed" | "already-present">;
84
89
  removeHooks(): Promise<"removed" | "absent">;
90
+ /**
91
+ * Bring isy's own hooks an older isy wrote up to date in place, adding none,
92
+ * with no sign-in — what a SessionStart hook runs, so an upgrade does not
93
+ * wait on `isy init`. Whether anything changed. Absent where rewriting a hook
94
+ * takes the user's say-so: Codex asks again for every hook it has not seen.
95
+ */
96
+ repairHooks?(): Promise<boolean>;
85
97
 
86
98
  /**
87
99
  * Show one line to the user. Each CLI displays text its own way, and the same
package/src/api.ts CHANGED
@@ -66,7 +66,10 @@ export type UploadOutcome =
66
66
  // Too many uploads this hour. Not a failure: the session is kept as it is,
67
67
  // and the run stops rather than spending the retries of every session behind
68
68
  // it on an answer that will not change.
69
- | { status: "throttled" };
69
+ | { status: "throttled" }
70
+ // The key, not the session: missing, unknown or revoked. Handled like
71
+ // "throttled", so what was waiting still goes up once `isy init` has run.
72
+ | { status: "unauthorized"; error: string };
70
73
 
71
74
  export interface ApiOptions {
72
75
  baseUrl: string;
@@ -117,7 +120,7 @@ function accepted(body: string): UploadOutcome {
117
120
  function classify(status: number, body: string): UploadOutcome {
118
121
  if (status >= 200 && status < 300) return accepted(body);
119
122
  if (status === 401 || status === 403) {
120
- return { status: "permanent", error: `authentication rejected (${status}), run: isy init` };
123
+ return { status: "unauthorized", error: `authentication rejected (${status}), run: isy init` };
121
124
  }
122
125
  if (status === 413) return { status: "permanent", error: "transcript rejected as too large (413)" };
123
126
  // A backlog is drained over hours rather than given up on: a sweep can find
@@ -1,13 +1,15 @@
1
1
  import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
2
- import { basename, join } from "node:path";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
3
  import { codexAgent, kimiAgent } from "../agents/index.js";
4
4
  import { isCodexTranscript } from "../agents/codex-records.js";
5
+ import { isKimiCliWire } from "../agents/kimi-cli-wire.js";
5
6
  import { isKimiTranscript } from "../agents/kimi-records.js";
6
7
  import { isKimiWireTranscript } from "../agents/kimi-wire.js";
7
8
  import { readConfig } from "../config.js";
8
9
  import { parseLines } from "../parser.js";
9
- import { firstLine, packageVersion } from "../paths.js";
10
- import { redactTranscriptFile } from "../redact.js";
10
+ import { firstLine, firstLines, packageVersion } from "../paths.js";
11
+ import { redactLineStream } from "../redact.js";
12
+ import { isSubagentTranscript, sessionLines } from "../subagents.js";
11
13
  import { countByCategory, runStage0 } from "../stage0.js";
12
14
  import type { IneligibleReason, Stage0Result } from "../stage0.js";
13
15
  import type { Candidate, SignalCategory } from "../types.js";
@@ -21,6 +23,8 @@ export interface AnalyzedSession {
21
23
  assistantRecords: number;
22
24
  editToolUses: number;
23
25
  thinkingBlocks: number;
26
+ /** Reasoning recorded with its text left out: why `known_gap` can be out of reach. */
27
+ hiddenThinkingBlocks: number;
24
28
  eligible: boolean;
25
29
  reason?: IneligibleReason;
26
30
  knownGapReachable: boolean;
@@ -63,7 +67,52 @@ export async function expandInputs(inputs: readonly string[]): Promise<string[]>
63
67
  files.push(input);
64
68
  }
65
69
 
66
- return [...new Set(files)].sort();
70
+ // One file however it was named: `./a/s.jsonl` beside `a/s.jsonl`, or a
71
+ // directory walked with the platform's separator beside a path typed with `/`.
72
+ const unique = new Map<string, string>();
73
+ for (const file of files) if (!unique.has(resolve(file))) unique.set(resolve(file), file);
74
+ const kept: string[] = [];
75
+ for (const [path, file] of unique) {
76
+ // A subagent is part of its session, which reads it in — Claude's from
77
+ // `subagents/` (`subagents.ts`), Kimi's from its own event log: analysed
78
+ // beside it, it would count twice, once as a session of its own. Asked
79
+ // about without its session, it is all there is to read.
80
+ if (owningSessions(path).some((session) => unique.has(session))) continue;
81
+ // Kimi CLI keeps one session twice: the event log and the model's context.
82
+ // The log is the session; the context beside it would be a second copy.
83
+ if (basename(file) === "context.jsonl" && (await isKimiCliSession(join(dirname(file), "wire.jsonl")))) continue;
84
+ kept.push(file);
85
+ }
86
+ return kept.sort();
87
+ }
88
+
89
+ /**
90
+ * The transcripts a subagent's file belongs to: Claude's
91
+ * `<session>/subagents/…/agent-<id>.jsonl` is `<session>.jsonl`'s, Kimi CLI's
92
+ * `<session>/subagents/<id>/wire.jsonl` is `<session>/wire.jsonl`'s, and Kimi
93
+ * Code's `<session>/agents/<id>/wire.jsonl` is `<session>/agents/main/wire.jsonl`'s.
94
+ */
95
+ function owningSessions(file: string): string[] {
96
+ const agent = dirname(file);
97
+ if (basename(file) === "wire.jsonl" && basename(dirname(agent)) === "agents" && basename(agent) !== "main") {
98
+ return [join(dirname(agent), "main", "wire.jsonl")];
99
+ }
100
+ let subagents = dirname(file);
101
+ while (basename(subagents) !== "subagents") {
102
+ if (dirname(subagents) === subagents) return [];
103
+ subagents = dirname(subagents);
104
+ }
105
+ const session = dirname(subagents);
106
+ if (isSubagentTranscript(file)) return [`${session}.jsonl`];
107
+ return [join(session, "wire.jsonl"), join(session, "context.jsonl")];
108
+ }
109
+
110
+ async function isKimiCliSession(wire: string): Promise<boolean> {
111
+ try {
112
+ return isKimiCliWire(await firstLines(wire, 3));
113
+ } catch {
114
+ return false;
115
+ }
67
116
  }
68
117
 
69
118
  /**
@@ -82,7 +131,8 @@ async function transcriptLines(
82
131
  }
83
132
  if (isCodexTranscript(head)) return codexAgent.redactedLines(file, { extraPatterns });
84
133
 
85
- const { lines } = await redactTranscriptFile(file, { extraPatterns });
134
+ // Claude Code: the session with its subagents read in, as the upload sends it.
135
+ const { lines } = await redactLineStream(sessionLines(file), { extraPatterns });
86
136
  return lines;
87
137
  }
88
138
 
@@ -104,6 +154,7 @@ export async function analyzeFiles(
104
154
  assistantRecords: session.meta.assistantRecords,
105
155
  editToolUses: session.meta.editToolUses,
106
156
  thinkingBlocks: result.thinkingBlocks,
157
+ hiddenThinkingBlocks: session.meta.hiddenThinkingBlocks,
107
158
  eligible: result.eligible,
108
159
  reason: result.reason,
109
160
  knownGapReachable: result.knownGapReachable,
@@ -236,7 +287,12 @@ export function formatReport(report: AnalyzeReport, elapsedMs?: number): string
236
287
  );
237
288
  }
238
289
  if (!session.knownGapReachable) {
239
- lines.push(" note: no thinking blocks, known_gap is unreachable for this session");
290
+ lines.push(
291
+ session.hiddenThinkingBlocks > 0
292
+ ? ` note: ${session.hiddenThinkingBlocks} thinking block(s) recorded without their text, ` +
293
+ "known_gap is unreachable for this session"
294
+ : " note: no thinking blocks, known_gap is unreachable for this session",
295
+ );
240
296
  }
241
297
  lines.push("");
242
298
  }
@@ -211,6 +211,17 @@ export async function runCheck(
211
211
  // A SessionStart hook must never delay or break the session it announces:
212
212
  // no network, and any failure means silence rather than a broken launch.
213
213
  try {
214
+ // Hooks an older isy wrote are rewritten where they stand before they are
215
+ // judged: `isy init` would do the same, but it signs in again first, and
216
+ // on a plan with one key that means revoking the key in use.
217
+ for (const agent of await presentAgents()) {
218
+ try {
219
+ await agent.repairHooks?.();
220
+ } catch {
221
+ // A config that will not parse is reported by the line below, as before.
222
+ }
223
+ }
224
+
214
225
  // Deliberately without `cwd`: the post-commit hook is per repository and
215
226
  // only an accelerator — SessionEnd still uploads without it. Nagging about
216
227
  // it at the start of every session in every repo would be noise, so it is
@@ -190,7 +190,7 @@ async function stateProblems(verified: string[]): Promise<HealthProblem[]> {
190
190
  }
191
191
  }
192
192
 
193
- /** Uploads that used up all three attempts. Their stored `lastError` is the evidence. */
193
+ /** Uploads that used up every attempt. Their stored `lastError` is the evidence. */
194
194
  async function queueProblems(): Promise<HealthProblem[]> {
195
195
  const failed = await listFailed();
196
196
  if (failed.length === 0) return [];
@@ -199,7 +199,7 @@ async function queueProblems(): Promise<HealthProblem[]> {
199
199
  {
200
200
  id: "uploads-abandoned",
201
201
  summary: `${failed.length} session${failed.length === 1 ? "" : "s"} never reached the server`,
202
- attempted: "POST /api/v1/sessions for each queued session, three times",
202
+ attempted: "POST /api/v1/sessions for each queued session, until the queue gave up on it",
203
203
  evidence: failed
204
204
  .map((item) => `${item.entry.sessionId}: ${item.entry.lastError ?? "no error recorded"}`)
205
205
  .join("\n"),
@@ -1,5 +1,6 @@
1
1
  import { execFile } from "node:child_process";
2
2
  import { createInterface } from "node:readline/promises";
3
+ import { drainAlerts, parkAlert } from "../alert.js";
3
4
  import { DEFAULT_API_BASE_URL, verifyToken } from "../api.js";
4
5
  import { readConfig, writeConfig } from "../config.js";
5
6
  import { presentAgents } from "../agents/index.js";
@@ -117,6 +118,21 @@ export async function runInit(options: { token?: string }, cwd: string): Promise
117
118
  const authUrl = new URL("/auth/github", baseUrl).toString();
118
119
 
119
120
  let token = options.token;
121
+ let verified: Awaited<ReturnType<typeof verifyToken>> | undefined;
122
+
123
+ // A key this machine already holds, and the server still takes, is kept.
124
+ // `init` is also how hooks are refreshed and a CLI installed since is added,
125
+ // and pairing again for that issued a second key — which a plan with one key
126
+ // refuses until the reader revokes the key in use.
127
+ if (!token && config.token) {
128
+ const held = await verifyToken({ baseUrl, token: config.token });
129
+ if (held.ok) {
130
+ token = config.token;
131
+ verified = held;
132
+ console.log(`Using the key already in ${configPath()}. To sign in as someone else, run isy logout first.`);
133
+ }
134
+ }
135
+
120
136
  if (!token) {
121
137
  // Only when the pairing could not be opened at all — a server that predates
122
138
  // it, or one with no dashboard address. A pairing that was opened and then
@@ -140,11 +156,19 @@ export async function runInit(options: { token?: string }, cwd: string): Promise
140
156
  );
141
157
  }
142
158
 
143
- const verified = await verifyToken({ baseUrl, token });
159
+ const kept = verified !== undefined;
160
+ verified ??= await verifyToken({ baseUrl, token });
144
161
  if (!verified.ok) throw new Error(verified.error);
145
162
 
146
163
  await writeConfig({ ...config, token, githubLogin: verified.githubLogin });
147
- console.log(`Token stored in ${configPath()}`);
164
+ if (!kept) console.log(`Token stored in ${configPath()}`);
165
+
166
+ // A hook that found the key missing or refused parked "run: isy init", and
167
+ // this is that run. Left parked, the next session start would send the reader
168
+ // back here for nothing; anything else waiting stays for it.
169
+ for (const line of (await drainAlerts()).split("\n")) {
170
+ if (line && !line.endsWith("run: isy init")) await parkAlert(line);
171
+ }
148
172
 
149
173
  // Hooks go into every CLI actually installed here, and none that is not.
150
174
  const agents = await presentAgents();
@@ -1,4 +1,3 @@
1
- import { stat } from "node:fs/promises";
2
1
  import { detectAgent, presentAgents } from "../agents/index.js";
3
2
  import type { Agent, AgentId, HookInput } from "../agents/index.js";
4
3
  import { DEFAULT_API_BASE_URL, buildTranscriptFields, uploadSession } from "../api.js";
@@ -11,11 +10,22 @@ import type { DiscoveredSession, IsyConfig } from "../types.js";
11
10
  import { logError, logLine } from "../log.js";
12
11
  import { parseLines } from "../parser.js";
13
12
  import { packageVersion } from "../paths.js";
14
- import { MAX_ATTEMPTS, abandon, drop, enqueue, isAbandoned, listPending, recordFailure } from "../queue.js";
13
+ import {
14
+ MAX_ATTEMPTS,
15
+ abandon,
16
+ drop,
17
+ enqueue,
18
+ forget,
19
+ isAbandoned,
20
+ isDue,
21
+ listPending,
22
+ recordFailure,
23
+ } from "../queue.js";
15
24
  import { updateNotice } from "../update.js";
16
25
  import { planNotice } from "../plan.js";
17
26
  import type { PlanStatus } from "../plan.js";
18
27
  import { looksUnchanged, readState, updateSession } from "../state.js";
28
+ import { sessionFingerprint } from "../subagents.js";
19
29
  import type { ClientState, CommitMark } from "../state.js";
20
30
 
21
31
  export const TOTAL_BUDGET_MS = 30_000;
@@ -42,6 +52,8 @@ export interface UploadReport {
42
52
  scanned?: number;
43
53
  /** The server's hourly upload allowance ran out; nothing was lost by it. */
44
54
  throttled?: boolean;
55
+ /** Why the server refused the key. Nothing was lost by that either. */
56
+ unauthorized?: string;
45
57
  }
46
58
 
47
59
  export async function readHookInput(
@@ -129,6 +141,7 @@ async function notePlan(plan: PlanStatus | undefined, report: UploadReport): Pro
129
141
  async function drainQueue(api: ApiOptions, deadline: number, report: UploadReport): Promise<void> {
130
142
  for (const item of await listPending()) {
131
143
  if (Date.now() >= deadline) return;
144
+ if (!isDue(item.entry)) continue;
132
145
 
133
146
  const outcome = await uploadSession(item.entry.payload, api);
134
147
  if (outcome.status === "ok") {
@@ -145,6 +158,11 @@ async function drainQueue(api: ApiOptions, deadline: number, report: UploadRepor
145
158
  logLine("upload: rate limited, the queue keeps its place for the next run");
146
159
  return;
147
160
  }
161
+ if (outcome.status === "unauthorized") {
162
+ report.unauthorized = outcome.error;
163
+ logError(`upload: the queue waits for a key the server takes: ${outcome.error}`);
164
+ return;
165
+ }
148
166
 
149
167
  if (outcome.status === "permanent") {
150
168
  await abandon(item, outcome.error);
@@ -258,13 +276,17 @@ async function sweptSessions(state: ClientState, report: UploadReport): Promise<
258
276
  return targets.sort((a, b) => a.file!.sizeBytes - b.file!.sizeBytes);
259
277
  }
260
278
 
261
- /** Size and mtime of a transcript, for the "did it change" check on the next run. */
279
+ /**
280
+ * Size and mtime of a transcript, for the "did it change" check on the next run.
281
+ * The same measure a listing takes (`paths.ts`), subagents included, or the next
282
+ * scan would read every session a hook uploaded as changed.
283
+ */
262
284
  async function fingerprint(target: Target): Promise<{ sizeBytes: number; modifiedMs: number }> {
263
285
  if (target.file) {
264
286
  return { sizeBytes: target.file.sizeBytes, modifiedMs: target.file.modifiedAt.getTime() };
265
287
  }
266
- const info = await stat(target.path);
267
- return { sizeBytes: info.size, modifiedMs: info.mtime.getTime() };
288
+ const whole = await sessionFingerprint(target.path);
289
+ return { sizeBytes: whole.sizeBytes, modifiedMs: whole.modifiedAt.getTime() };
268
290
  }
269
291
 
270
292
  async function sendOne(
@@ -304,6 +326,7 @@ async function sendOne(
304
326
  await noteClientVersion(outcome.clientVersion, payload.isyVersion, report);
305
327
  await notePlan(outcome.plan, report);
306
328
  if (outcome.deduplicated !== true) report.fresh += 1;
329
+ await forget(payload.sessionId, payload.contentHash);
307
330
  // Recorded only once the server has it: a failed send must stay "changed"
308
331
  // so the next commit tries again rather than skipping it forever.
309
332
  const seen = await fingerprint(target);
@@ -320,6 +343,11 @@ async function sendOne(
320
343
  // only burn its attempts.
321
344
  report.throttled = true;
322
345
  logLine(`upload: ${payload.sessionId} not sent, this hour's uploads are spent`);
346
+ } else if (outcome.status === "unauthorized") {
347
+ // Neither queued nor dropped either: the transcript still reads as changed,
348
+ // so the first run with a key the server takes sends it.
349
+ report.unauthorized = outcome.error;
350
+ logError(`upload: ${payload.sessionId} not sent: ${outcome.error}`);
323
351
  } else if (outcome.status === "retry") {
324
352
  await enqueue(payload, outcome.error);
325
353
  report.queued += 1;
@@ -327,6 +355,15 @@ async function sendOne(
327
355
  } else {
328
356
  report.abandoned += 1;
329
357
  logError(`upload: dropped ${payload.sessionId}: ${outcome.error}`);
358
+ // Refused for what it holds, so building it again only to hear the same is
359
+ // all the next sweep would do: it is passed over until it changes.
360
+ const seen = await fingerprint(target);
361
+ await updateSession(target.path, (current) => ({
362
+ ...current,
363
+ rejectedHash: payload.contentHash,
364
+ uploadedCommit: payload.commits?.at(-1)?.sha,
365
+ ...seen,
366
+ }));
330
367
  }
331
368
  }
332
369
 
@@ -440,8 +477,8 @@ export async function runUpload(
440
477
  logLine(`upload: out of budget with ${targets.length - report.sent} session(s) left`);
441
478
  break;
442
479
  }
443
- // One "throttled" answers for every session in the run.
444
- if (report.throttled) break;
480
+ // One "throttled", or one refused key, answers for every session in the run.
481
+ if (report.throttled || report.unauthorized) break;
445
482
 
446
483
  const git = await gitFor(target.cwd ?? workingDir);
447
484
  if (!git.ok) {
@@ -482,6 +519,7 @@ export function formatUploadReport(report: UploadReport): string {
482
519
  const parts: string[] = [];
483
520
  if (report.sent > 0) parts.push("session uploaded");
484
521
  if (report.throttled) parts.push("rate limited, the rest go out on the next run");
522
+ if (report.unauthorized) parts.push(report.unauthorized);
485
523
  if (report.queued > 0) parts.push("session queued for retry");
486
524
  if (report.drained > 0) parts.push(`${report.drained} queued session(s) sent`);
487
525
  if (report.abandoned > 0) parts.push(`${report.abandoned} session(s) given up on`);
@@ -512,6 +550,7 @@ export function formatUploadAlert(report: UploadReport): string | undefined {
512
550
  if (report.drained > 0) parts.push(`${report.drained} queued session(s) sent`);
513
551
  if (report.queued > 0) parts.push("upload failed, queued for retry");
514
552
  if (report.throttled) parts.push("rate limited, the rest upload on the next run");
553
+ if (report.unauthorized) parts.push(report.unauthorized);
515
554
  if (report.abandoned > 0) parts.push(`${report.abandoned} session(s) given up on`);
516
555
  if (report.gitHook === "installed") parts.push("commit and push hooks added to this repository");
517
556
  if (report.gitHook === "shared-hooks") {
package/src/detectors.ts CHANGED
@@ -97,6 +97,26 @@ export function detectTestModifiedToPass(session: ParsedSession): Candidate[] {
97
97
  if (!isTestFile(filePath)) continue;
98
98
 
99
99
  for (const edit of edits) {
100
+ if (edit.tool === "Bash") {
101
+ // `sed -i` on a test says where, never what (`parser.ts:shellEditPaths`),
102
+ // so whether it weakened the test is the model's to read. Right after a
103
+ // failure it is worth the read; any other time it is noise.
104
+ const failure = failedBefore(session, edit.recordIndex);
105
+ if (!failure) continue;
106
+ candidates.push({
107
+ category: "test_modified_to_pass",
108
+ sessionId: session.sessionId,
109
+ uuid: edit.uuid,
110
+ toolUseId: edit.toolUseId,
111
+ filePath,
112
+ recordIndex: edit.recordIndex,
113
+ startedAt: failure.timestamp,
114
+ endedAt: edit.timestamp,
115
+ weight: 0.5,
116
+ detail: "test rewritten in place by a shell command shortly after a failing command",
117
+ });
118
+ continue;
119
+ }
100
120
  if (edit.oldString === undefined || edit.newString === undefined) continue;
101
121
 
102
122
  const reason = weakensTest(edit.oldString, edit.newString);
@@ -690,17 +710,30 @@ export function detectExternalDependency(session: ParsedSession): Candidate[] {
690
710
  }
691
711
 
692
712
  /**
693
- * Whether two paths name the same file. Reads are recorded as the agent typed
694
- * them — `src/api.ts` from a shell, `/repo/src/api.ts` from the Read tool — so
695
- * a relative path matches any absolute one ending in it, and a directory
696
- * matches everything under it.
713
+ * Whether two paths name the same file. Reads and shell edits are recorded as
714
+ * the agent typed them — `src/api.ts` from a shell, `/repo/src/api.ts` from the
715
+ * Read tool — so a relative path matches any absolute one ending in it, either
716
+ * way round, a directory matches everything under it, and a glob from
717
+ * `cat src/*.js` matches every file it could have expanded to.
697
718
  */
698
719
  function samePath(known: string, filePath: string): boolean {
699
720
  if (known === filePath) return true;
721
+ if (/[*?]/.test(known)) return globMatches(known, filePath);
700
722
  if (filePath.startsWith(known.endsWith("/") ? known : `${known}/`)) return true;
723
+ if (!filePath.startsWith("/") && known.endsWith(`/${filePath}`)) return true;
701
724
  return !known.startsWith("/") && filePath.endsWith(`/${known}`);
702
725
  }
703
726
 
727
+ const GLOB_PARTS: Record<string, string> = { "?": "[^/]", "*": "[^/]*", "**": ".*", "**/": "(?:.*/)?" };
728
+
729
+ /** `*` and `?` within one path segment, `**` across them; a relative glob matches by suffix, like a relative path. */
730
+ function globMatches(glob: string, filePath: string): boolean {
731
+ const body = glob
732
+ .replace(/[.+^${}()|[\]\\]/g, "\\$&")
733
+ .replace(/\*\*\/?|\*|\?/g, (wildcard) => GLOB_PARTS[wildcard]!);
734
+ return new RegExp(glob.startsWith("/") ? `^${body}$` : `(?:^|/)${body}$`).test(filePath);
735
+ }
736
+
704
737
  function wasLookedUp(session: ParsedSession, filePath: string): boolean {
705
738
  for (const known of session.filesRead) {
706
739
  if (samePath(known, filePath)) return true;
@@ -714,7 +747,8 @@ export function detectUnverifiedAssumption(session: ParsedSession): Candidate[]
714
747
  for (const [filePath, edits] of session.fileEdits) {
715
748
  const first = edits[0];
716
749
  if (!first) continue;
717
- if (first.tool !== "Edit" && first.tool !== "MultiEdit") continue;
750
+ // `sed -i` edits in place like Edit does, on the same picture of the file.
751
+ if (first.tool !== "Edit" && first.tool !== "MultiEdit" && first.tool !== "Bash") continue;
718
752
  if (wasLookedUp(session, filePath)) continue;
719
753
 
720
754
  candidates.push({