@nightloom/isy 1.0.1 → 1.0.2
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.
- package/README.md +1 -1
- package/dist/agents/claude.js +18 -7
- package/dist/agents/claude.js.map +1 -1
- package/dist/agents/codex-records.js +3 -1
- package/dist/agents/codex-records.js.map +1 -1
- package/dist/agents/index.d.ts +16 -5
- package/dist/agents/index.js +21 -6
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/kimi-cli-wire.d.ts +17 -0
- package/dist/agents/kimi-cli-wire.js +312 -0
- package/dist/agents/kimi-cli-wire.js.map +1 -0
- package/dist/agents/kimi-config.d.ts +29 -0
- package/dist/agents/kimi-config.js +193 -0
- package/dist/agents/kimi-config.js.map +1 -0
- package/dist/agents/kimi-records.d.ts +0 -6
- package/dist/agents/kimi-records.js +51 -31
- package/dist/agents/kimi-records.js.map +1 -1
- package/dist/agents/kimi-tools.d.ts +30 -0
- package/dist/agents/kimi-tools.js +100 -0
- package/dist/agents/kimi-tools.js.map +1 -0
- package/dist/agents/kimi-wire.d.ts +8 -0
- package/dist/agents/kimi-wire.js +245 -80
- package/dist/agents/kimi-wire.js.map +1 -1
- package/dist/agents/kimi.d.ts +13 -2
- package/dist/agents/kimi.js +633 -114
- package/dist/agents/kimi.js.map +1 -1
- package/dist/agents/timeline.d.ts +36 -0
- package/dist/agents/timeline.js +61 -0
- package/dist/agents/timeline.js.map +1 -0
- package/dist/agents/types.d.ts +12 -0
- package/dist/commands/analyze.d.ts +2 -0
- package/dist/commands/analyze.js +63 -6
- package/dist/commands/analyze.js.map +1 -1
- package/dist/commands/check.js +11 -0
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/init.js +17 -2
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/upload.js +8 -4
- package/dist/commands/upload.js.map +1 -1
- package/dist/detectors.js +40 -5
- package/dist/detectors.js.map +1 -1
- package/dist/hook.d.ts +12 -5
- package/dist/hook.js +73 -17
- package/dist/hook.js.map +1 -1
- package/dist/lines.d.ts +8 -0
- package/dist/lines.js +32 -0
- package/dist/lines.js.map +1 -0
- package/dist/parser.d.ts +41 -0
- package/dist/parser.js +328 -32
- package/dist/parser.js.map +1 -1
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +39 -39
- package/dist/paths.js.map +1 -1
- package/dist/redact.d.ts +10 -0
- package/dist/redact.js +49 -8
- package/dist/redact.js.map +1 -1
- package/dist/state.d.ts +6 -3
- package/dist/state.js +21 -5
- package/dist/state.js.map +1 -1
- package/dist/subagents.d.ts +49 -0
- package/dist/subagents.js +237 -0
- package/dist/subagents.js.map +1 -0
- package/dist/test/check.test.js +25 -3
- package/dist/test/check.test.js.map +1 -1
- package/dist/test/codex.test.js +6 -0
- package/dist/test/codex.test.js.map +1 -1
- package/dist/test/commit.test.js +9 -1
- package/dist/test/commit.test.js.map +1 -1
- package/dist/test/hook.test.js +66 -2
- package/dist/test/hook.test.js.map +1 -1
- package/dist/test/init.test.d.ts +1 -0
- package/dist/test/init.test.js +94 -0
- package/dist/test/init.test.js.map +1 -0
- package/dist/test/kimi-cli.test.d.ts +1 -0
- package/dist/test/kimi-cli.test.js +231 -0
- package/dist/test/kimi-cli.test.js.map +1 -0
- package/dist/test/kimi-code.test.d.ts +1 -0
- package/dist/test/kimi-code.test.js +302 -0
- package/dist/test/kimi-code.test.js.map +1 -0
- package/dist/test/kimi-config.test.d.ts +1 -0
- package/dist/test/kimi-config.test.js +224 -0
- package/dist/test/kimi-config.test.js.map +1 -0
- package/dist/test/kimi-hook.test.d.ts +1 -0
- package/dist/test/kimi-hook.test.js +163 -0
- package/dist/test/kimi-hook.test.js.map +1 -0
- package/dist/test/kimi-listing.test.d.ts +1 -0
- package/dist/test/kimi-listing.test.js +193 -0
- package/dist/test/kimi-listing.test.js.map +1 -0
- package/dist/test/kimi.test.js +52 -7
- package/dist/test/kimi.test.js.map +1 -1
- package/dist/test/parser.test.js +192 -5
- package/dist/test/parser.test.js.map +1 -1
- package/dist/test/redact.test.js +71 -7
- package/dist/test/redact.test.js.map +1 -1
- package/dist/test/stage0.test.js +49 -0
- package/dist/test/stage0.test.js.map +1 -1
- package/dist/test/subagents.test.d.ts +1 -0
- package/dist/test/subagents.test.js +272 -0
- package/dist/test/subagents.test.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/package.json +3 -2
- package/src/agents/claude.ts +17 -7
- package/src/agents/codex-records.ts +3 -1
- package/src/agents/index.ts +21 -6
- package/src/agents/kimi-cli-wire.ts +350 -0
- package/src/agents/kimi-config.ts +210 -0
- package/src/agents/kimi-records.ts +52 -29
- package/src/agents/kimi-tools.ts +128 -0
- package/src/agents/kimi-wire.ts +270 -82
- package/src/agents/kimi.ts +653 -113
- package/src/agents/timeline.ts +68 -0
- package/src/agents/types.ts +12 -0
- package/src/commands/analyze.ts +62 -6
- package/src/commands/check.ts +11 -0
- package/src/commands/init.ts +18 -2
- package/src/commands/upload.ts +8 -4
- package/src/detectors.ts +39 -5
- package/src/hook.ts +81 -24
- package/src/lines.ts +31 -0
- package/src/parser.ts +343 -31
- package/src/paths.ts +36 -33
- package/src/redact.ts +53 -10
- package/src/state.ts +24 -5
- package/src/subagents.ts +283 -0
- package/src/types.ts +16 -0
package/src/redact.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { createReadStream } from "node:fs";
|
|
2
1
|
import { homedir } from "node:os";
|
|
3
2
|
import { basename } from "node:path";
|
|
4
|
-
import {
|
|
3
|
+
import { fileLines } from "./lines.js";
|
|
5
4
|
import {
|
|
6
5
|
PATH_KEYS,
|
|
7
6
|
REDACTION_MARKER,
|
|
@@ -69,6 +68,12 @@ export const BUILTIN_RULES: RedactionRule[] = [
|
|
|
69
68
|
pattern: /\b(postgres|postgresql|mysql|mongodb\+srv|mongodb|redis|amqp):\/\/([^\s:/@]+):([^\s/@]+)@/g,
|
|
70
69
|
replacement: `$1://$2:${marker("connection_string")}@`,
|
|
71
70
|
},
|
|
71
|
+
{
|
|
72
|
+
// After the connection string: its password sits right before an `@` too.
|
|
73
|
+
type: "email",
|
|
74
|
+
pattern: /\b[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/g,
|
|
75
|
+
replacement: marker("email"),
|
|
76
|
+
},
|
|
72
77
|
{
|
|
73
78
|
type: "assignment",
|
|
74
79
|
pattern:
|
|
@@ -225,12 +230,42 @@ function learnRoot(record: Record<string, unknown>, context: RedactContext): voi
|
|
|
225
230
|
context.roots = { ...context.roots, ...resolveRoots({ cwd }) };
|
|
226
231
|
}
|
|
227
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Claude Code's `attachment` records are the harness's context for the model:
|
|
235
|
+
* the account's email (`session_context`), its organisation (`credential_org`),
|
|
236
|
+
* the whole system prompt with memory and CLAUDE.md (`prompt_snapshot`), the
|
|
237
|
+
* connectors' instructions. Nothing downstream reads past their `type`, so that
|
|
238
|
+
* is all that goes. The record itself stays: the next record names it as its
|
|
239
|
+
* `parentUuid`, and the parser walks that chain.
|
|
240
|
+
*
|
|
241
|
+
* Claude Code 2.1.30x also records the request it sends: `api-request-shape`
|
|
242
|
+
* carries the system prompt and the tool definitions, `api-request-blob` the
|
|
243
|
+
* context blocks sent beside the conversation — memory, CLAUDE.md, the email
|
|
244
|
+
* again. The parser reads neither, so only the fields that name them go.
|
|
245
|
+
*/
|
|
246
|
+
const REQUEST_RECORD_KEYS = new Set(["type", "uuid", "parentUuid", "sessionId", "timestamp", "version", "shapeHash", "hash"]);
|
|
247
|
+
|
|
248
|
+
function stripAttachment(record: Record<string, unknown>, context: RedactContext): void {
|
|
249
|
+
if (record.type === "api-request-shape" || record.type === "api-request-blob") {
|
|
250
|
+
for (const key of Object.keys(record)) if (!REQUEST_RECORD_KEYS.has(key)) delete record[key];
|
|
251
|
+
count(context, "attachment", 1);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (record.type !== "attachment") return;
|
|
255
|
+
const kind = isObject(record.attachment) ? record.attachment.type : undefined;
|
|
256
|
+
record.attachment = typeof kind === "string" ? { type: kind } : {};
|
|
257
|
+
delete record.rendered;
|
|
258
|
+
delete record.renderedRole;
|
|
259
|
+
count(context, "attachment", 1);
|
|
260
|
+
}
|
|
261
|
+
|
|
228
262
|
function redactRecord(
|
|
229
263
|
record: Record<string, unknown>,
|
|
230
264
|
context: RedactContext,
|
|
231
265
|
): Record<string, unknown> {
|
|
232
266
|
learnRoot(record, context);
|
|
233
267
|
scanDotenv(record, context);
|
|
268
|
+
stripAttachment(record, context);
|
|
234
269
|
return redactValue(record, context) as Record<string, unknown>;
|
|
235
270
|
}
|
|
236
271
|
|
|
@@ -308,22 +343,23 @@ export function redactLines(
|
|
|
308
343
|
return { lines: output, summary: summarize(context, total, unparsed, invalid) };
|
|
309
344
|
}
|
|
310
345
|
|
|
311
|
-
|
|
312
|
-
|
|
346
|
+
/**
|
|
347
|
+
* `redactLines` for lines that arrive one at a time — a file read as it streams,
|
|
348
|
+
* or a Claude session with its subagents spliced in (`subagents.ts`). One
|
|
349
|
+
* context for the whole stream, so the working directory the first record names
|
|
350
|
+
* roots every path after it, the subagents' included.
|
|
351
|
+
*/
|
|
352
|
+
export async function redactLineStream(
|
|
353
|
+
lines: AsyncIterable<string>,
|
|
313
354
|
options: RedactOptions = {},
|
|
314
355
|
): Promise<{ lines: string[]; summary: RedactionSummary }> {
|
|
315
356
|
const invalid: string[] = [];
|
|
316
357
|
const context = createContext(options, invalid);
|
|
317
|
-
const reader = createInterface({
|
|
318
|
-
input: createReadStream(filePath, { encoding: "utf8" }),
|
|
319
|
-
crlfDelay: Infinity,
|
|
320
|
-
});
|
|
321
|
-
|
|
322
358
|
const output: string[] = [];
|
|
323
359
|
let total = 0;
|
|
324
360
|
let unparsed = 0;
|
|
325
361
|
|
|
326
|
-
for await (const line of
|
|
362
|
+
for await (const line of lines) {
|
|
327
363
|
total += 1;
|
|
328
364
|
const result = redactOneLine(line, context);
|
|
329
365
|
if (result.line.length === 0) continue;
|
|
@@ -333,3 +369,10 @@ export async function redactTranscriptFile(
|
|
|
333
369
|
|
|
334
370
|
return { lines: output, summary: summarize(context, total, unparsed, invalid) };
|
|
335
371
|
}
|
|
372
|
+
|
|
373
|
+
export function redactTranscriptFile(
|
|
374
|
+
filePath: string,
|
|
375
|
+
options: RedactOptions = {},
|
|
376
|
+
): Promise<{ lines: string[]; summary: RedactionSummary }> {
|
|
377
|
+
return redactLineStream(fileLines(filePath), options);
|
|
378
|
+
}
|
package/src/state.ts
CHANGED
|
@@ -159,25 +159,44 @@ export async function updateSession(
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
/**
|
|
162
|
-
* Identity of one candidate across runs
|
|
163
|
-
* record
|
|
164
|
-
*
|
|
162
|
+
* Identity of one candidate across runs: the call it points at, or else the
|
|
163
|
+
* record, both of which keep their ids as the session grows — the same finding
|
|
164
|
+
* produces the same key at every later commit. Not the record's index: a
|
|
165
|
+
* subagent is merged into its session by time (`subagents.ts`), so a background
|
|
166
|
+
* one that goes on writing puts records ahead of later ones and moves every
|
|
167
|
+
* index after them. A candidate that points at neither has only the index.
|
|
165
168
|
*/
|
|
166
169
|
export function candidateKey(candidate: Candidate): string {
|
|
170
|
+
const anchor = candidate.toolUseId ?? candidate.uuid;
|
|
171
|
+
return [
|
|
172
|
+
candidate.category,
|
|
173
|
+
candidate.filePath ?? "",
|
|
174
|
+
anchor ?? "",
|
|
175
|
+
anchor === undefined ? candidate.recordIndex : "",
|
|
176
|
+
// Escaped, never a raw byte: a NUL in the source makes git treat the file as binary.
|
|
177
|
+
].join("\x00");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The key isy 1.0 wrote, index and all. Still matched, so the first commit
|
|
182
|
+
* after an upgrade does not print again what the one before it printed.
|
|
183
|
+
*/
|
|
184
|
+
function keyWithIndex(candidate: Candidate): string {
|
|
167
185
|
return [
|
|
168
186
|
candidate.category,
|
|
169
187
|
candidate.filePath ?? "",
|
|
170
188
|
candidate.uuid ?? "",
|
|
171
189
|
candidate.toolUseId ?? "",
|
|
172
190
|
candidate.recordIndex,
|
|
173
|
-
// Escaped, never a raw byte: a NUL in the source makes git treat the file as binary.
|
|
174
191
|
].join("\x00");
|
|
175
192
|
}
|
|
176
193
|
|
|
177
194
|
/** Candidates this session has not printed before. */
|
|
178
195
|
export function unseen(state: SessionState, candidates: readonly Candidate[]): Candidate[] {
|
|
179
196
|
const shown = new Set(state.shown ?? []);
|
|
180
|
-
return candidates.filter(
|
|
197
|
+
return candidates.filter(
|
|
198
|
+
(candidate) => !shown.has(candidateKey(candidate)) && !shown.has(keyWithIndex(candidate)),
|
|
199
|
+
);
|
|
181
200
|
}
|
|
182
201
|
|
|
183
202
|
/**
|
package/src/subagents.ts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import type { Dirent } from "node:fs";
|
|
2
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { fileLines } from "./lines.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Claude Code 2.1 writes every subagent to a file of its own beside the session:
|
|
8
|
+
* `<session>/subagents/agent-<id>.jsonl`, with `agent-<id>.meta.json` next to it
|
|
9
|
+
* naming the `Agent` call that started it (`toolUseId`). Workflow agents go one
|
|
10
|
+
* level deeper (`subagents/workflows/<run>/`). The session file keeps only the
|
|
11
|
+
* call and its result, so read alone it hid most of the work — in one session
|
|
12
|
+
* 12 calls stood in the session file and 134 in its subagents — and a session
|
|
13
|
+
* whose files were all edited by a subagent read as one that edited nothing.
|
|
14
|
+
*
|
|
15
|
+
* So the session is read as one transcript, each subagent's records merged in
|
|
16
|
+
* as sidechain records carrying `parentToolUseID`: by time, and never ahead of
|
|
17
|
+
* the record holding the call that started it. By time, because the detectors
|
|
18
|
+
* read order as time, and a subagent does not always run inside its call — one
|
|
19
|
+
* started in the background works alongside the session, and one resumed later
|
|
20
|
+
* appends to the same file. Spliced in whole after its call, a background
|
|
21
|
+
* agent's test run stood before an edit the session made a minute before it, and
|
|
22
|
+
* read as the check of that edit.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface Subagent {
|
|
26
|
+
agentId: string;
|
|
27
|
+
path: string;
|
|
28
|
+
/** The `Agent` call that started it, from the meta file. */
|
|
29
|
+
toolUseId?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function subagentsDir(sessionPath: string): string {
|
|
37
|
+
return join(dirname(sessionPath), basename(sessionPath, ".jsonl"), "subagents");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether a transcript file is itself a subagent's, filed under some session. */
|
|
41
|
+
export function isSubagentTranscript(path: string): boolean {
|
|
42
|
+
return /^agent-.+\.jsonl$/.test(basename(path)) && path.split(/[/\\]/).includes("subagents");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Every subagent transcript under a directory, at any depth, in name order. */
|
|
46
|
+
async function agentFiles(dir: string): Promise<string[]> {
|
|
47
|
+
let entries: Dirent[];
|
|
48
|
+
try {
|
|
49
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
50
|
+
} catch {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const found: string[] = [];
|
|
55
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
56
|
+
const path = join(dir, entry.name);
|
|
57
|
+
if (entry.isDirectory()) found.push(...(await agentFiles(path)));
|
|
58
|
+
else if (entry.isFile() && /^agent-.+\.jsonl$/.test(entry.name)) found.push(path);
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function readMeta(path: string): Promise<Record<string, unknown> | undefined> {
|
|
64
|
+
try {
|
|
65
|
+
const parsed: unknown = JSON.parse(await readFile(path.replace(/\.jsonl$/, ".meta.json"), "utf8"));
|
|
66
|
+
return isObject(parsed) ? parsed : undefined;
|
|
67
|
+
} catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function subagentsOf(sessionPath: string): Promise<Subagent[]> {
|
|
73
|
+
const agents: Subagent[] = [];
|
|
74
|
+
for (const path of await agentFiles(subagentsDir(sessionPath))) {
|
|
75
|
+
const meta = await readMeta(path);
|
|
76
|
+
const toolUseId = typeof meta?.toolUseId === "string" && meta.toolUseId.length > 0 ? meta.toolUseId : undefined;
|
|
77
|
+
agents.push({ agentId: basename(path, ".jsonl").slice("agent-".length), path, toolUseId });
|
|
78
|
+
}
|
|
79
|
+
return agents;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Size and mtime of a session as a whole, subagents included. What decides
|
|
84
|
+
* whether a session changed since its last upload (`state.ts:looksUnchanged`):
|
|
85
|
+
* a background subagent keeps writing its own file after the session file went
|
|
86
|
+
* quiet, and by the session file alone that work would never count as new.
|
|
87
|
+
*/
|
|
88
|
+
export async function sessionFingerprint(
|
|
89
|
+
sessionPath: string,
|
|
90
|
+
): Promise<{ sizeBytes: number; modifiedAt: Date }> {
|
|
91
|
+
const info = await stat(sessionPath);
|
|
92
|
+
let sizeBytes = info.size;
|
|
93
|
+
let modified = info.mtime.getTime();
|
|
94
|
+
|
|
95
|
+
for (const path of await agentFiles(subagentsDir(sessionPath))) {
|
|
96
|
+
try {
|
|
97
|
+
const agent = await stat(path);
|
|
98
|
+
sizeBytes += agent.size;
|
|
99
|
+
modified = Math.max(modified, agent.mtime.getTime());
|
|
100
|
+
} catch {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { sizeBytes, modifiedAt: new Date(modified) };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A subagent's lines, as many as can be read. Its file can go, or stop being
|
|
109
|
+
* readable, between the listing and the read — and the session is worth sending
|
|
110
|
+
* without it: one file that would not open stopped the whole upload, and every
|
|
111
|
+
* sweep after it at the same session.
|
|
112
|
+
*/
|
|
113
|
+
async function* subagentLines(path: string): AsyncGenerator<string> {
|
|
114
|
+
try {
|
|
115
|
+
yield* fileLines(path);
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function recordOf(line: string): Record<string, unknown> | undefined {
|
|
122
|
+
try {
|
|
123
|
+
const parsed: unknown = JSON.parse(line);
|
|
124
|
+
return isObject(parsed) ? parsed : undefined;
|
|
125
|
+
} catch {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function timeOf(record: Record<string, unknown> | undefined): number | undefined {
|
|
131
|
+
if (typeof record?.timestamp !== "string") return undefined;
|
|
132
|
+
const at = Date.parse(record.timestamp);
|
|
133
|
+
return Number.isNaN(at) ? undefined : at;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The record as a subagent's: a sidechain, linked to the call that started it
|
|
138
|
+
* when that call is known. A line that does not parse passes as it is.
|
|
139
|
+
*/
|
|
140
|
+
function asSidechain(line: string, record: Record<string, unknown> | undefined, call: string | undefined): string {
|
|
141
|
+
if (!record) return line;
|
|
142
|
+
return JSON.stringify({
|
|
143
|
+
...record,
|
|
144
|
+
isSidechain: true,
|
|
145
|
+
...(call !== undefined && record.parentToolUseID === undefined ? { parentToolUseID: call } : {}),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Ids of the calls a record starts. */
|
|
150
|
+
function callsIn(record: Record<string, unknown> | undefined): string[] {
|
|
151
|
+
const content = isObject(record?.message) ? record.message.content : undefined;
|
|
152
|
+
if (!Array.isArray(content)) return [];
|
|
153
|
+
return content
|
|
154
|
+
.filter((block) => isObject(block) && block.type === "tool_use" && typeof block.id === "string")
|
|
155
|
+
.map((block) => (block as { id: string }).id);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The call a subagent without a meta file answered: its result in the session
|
|
160
|
+
* names the agent (`toolUseResult.agentId`), and the result names the call.
|
|
161
|
+
*/
|
|
162
|
+
function answeredCall(record: Record<string, unknown> | undefined, agentId: string): string | undefined {
|
|
163
|
+
if (!isObject(record?.toolUseResult) || record.toolUseResult.agentId !== agentId) return undefined;
|
|
164
|
+
const content = isObject(record.message) ? record.message.content : undefined;
|
|
165
|
+
const result = Array.isArray(content)
|
|
166
|
+
? content.find((block) => isObject(block) && block.type === "tool_result")
|
|
167
|
+
: undefined;
|
|
168
|
+
return isObject(result) && typeof result.tool_use_id === "string" ? result.tool_use_id : undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Where a stream of lines comes from: the session itself, or a subagent and the call behind it. */
|
|
172
|
+
type Source = { subagent: false } | { subagent: true; call?: string };
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A line and the moment it stands for: its own timestamp, or — for a record
|
|
176
|
+
* written without one — the last moment its stream named.
|
|
177
|
+
*/
|
|
178
|
+
interface Timed {
|
|
179
|
+
line: string;
|
|
180
|
+
at: number;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
interface Head {
|
|
184
|
+
next: Timed;
|
|
185
|
+
rest: AsyncIterator<Timed>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The subagents one stream has started, each waiting with its next line.
|
|
190
|
+
* `until` hands over every line due by a moment, earliest first, and on a tie
|
|
191
|
+
* the one from the subagent that started first.
|
|
192
|
+
*/
|
|
193
|
+
class Running {
|
|
194
|
+
private readonly heads: Head[] = [];
|
|
195
|
+
|
|
196
|
+
async add(stream: AsyncIterator<Timed>): Promise<void> {
|
|
197
|
+
const first = await stream.next();
|
|
198
|
+
if (!first.done) this.heads.push({ next: first.value, rest: stream });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async *until(at: number): AsyncGenerator<Timed> {
|
|
202
|
+
for (;;) {
|
|
203
|
+
let earliest: Head | undefined;
|
|
204
|
+
for (const head of this.heads) {
|
|
205
|
+
if (head.next.at <= at && (earliest === undefined || head.next.at < earliest.next.at)) earliest = head;
|
|
206
|
+
}
|
|
207
|
+
if (earliest === undefined) return;
|
|
208
|
+
yield earliest.next;
|
|
209
|
+
const following = await earliest.rest.next();
|
|
210
|
+
if (following.done) this.heads.splice(this.heads.indexOf(earliest), 1);
|
|
211
|
+
else earliest.next = following.value;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* A Claude session's lines with every subagent's records merged in by time,
|
|
218
|
+
* nested subagents into theirs the same way. A subagent whose call is nowhere
|
|
219
|
+
* to be found — its meta file lost, its call in a file that was not kept —
|
|
220
|
+
* still did the work, so it follows the session rather than vanishing.
|
|
221
|
+
*
|
|
222
|
+
* Streams: sessions reach tens of megabytes, and a subagent's file is opened
|
|
223
|
+
* only once its call has been read.
|
|
224
|
+
*/
|
|
225
|
+
export async function* sessionLines(sessionPath: string): AsyncGenerator<string> {
|
|
226
|
+
const agents = await subagentsOf(sessionPath);
|
|
227
|
+
if (agents.length === 0) {
|
|
228
|
+
yield* fileLines(sessionPath);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const byCall = new Map<string, Subagent[]>();
|
|
233
|
+
for (const agent of agents) {
|
|
234
|
+
if (agent.toolUseId === undefined) continue;
|
|
235
|
+
byCall.set(agent.toolUseId, [...(byCall.get(agent.toolUseId) ?? []), agent]);
|
|
236
|
+
}
|
|
237
|
+
const unlinked = agents.filter((agent) => agent.toolUseId === undefined);
|
|
238
|
+
const placed = new Set<string>();
|
|
239
|
+
|
|
240
|
+
/** The subagents a record starts: by the call it makes, or by the result that names one with no meta file. */
|
|
241
|
+
const startedBy = (record: Record<string, unknown> | undefined): { agent: Subagent; call: string }[] => {
|
|
242
|
+
const started: { agent: Subagent; call: string }[] = [];
|
|
243
|
+
for (const call of callsIn(record)) {
|
|
244
|
+
for (const agent of byCall.get(call) ?? []) started.push({ agent, call });
|
|
245
|
+
}
|
|
246
|
+
for (const agent of unlinked) {
|
|
247
|
+
if (placed.has(agent.agentId)) continue;
|
|
248
|
+
const call = answeredCall(record, agent.agentId);
|
|
249
|
+
if (call !== undefined) started.push({ agent, call });
|
|
250
|
+
}
|
|
251
|
+
return started;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
async function* merged(lines: AsyncIterable<string>, source: Source, from: number): AsyncGenerator<Timed> {
|
|
255
|
+
const running = new Running();
|
|
256
|
+
let at = from;
|
|
257
|
+
for await (const line of lines) {
|
|
258
|
+
const record = recordOf(line);
|
|
259
|
+
at = timeOf(record) ?? at;
|
|
260
|
+
// What the subagents started here did up to this record goes first. A
|
|
261
|
+
// stream that has named no moment yet bounds nothing — a transcript
|
|
262
|
+
// without timestamps puts each subagent right after its call, whole.
|
|
263
|
+
yield* running.until(at === -Infinity ? Infinity : at);
|
|
264
|
+
yield { line: source.subagent ? asSidechain(line, record, source.call) : line, at };
|
|
265
|
+
|
|
266
|
+
for (const { agent, call } of startedBy(record)) {
|
|
267
|
+
if (placed.has(agent.agentId)) continue;
|
|
268
|
+
placed.add(agent.agentId);
|
|
269
|
+
await running.add(merged(subagentLines(agent.path), { subagent: true, call }, at));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
yield* running.until(Infinity);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
for await (const { line } of merged(fileLines(sessionPath), { subagent: false }, -Infinity)) yield line;
|
|
276
|
+
for (const agent of agents) {
|
|
277
|
+
if (placed.has(agent.agentId)) continue;
|
|
278
|
+
placed.add(agent.agentId);
|
|
279
|
+
for await (const { line } of merged(subagentLines(agent.path), { subagent: true, call: agent.toolUseId }, -Infinity)) {
|
|
280
|
+
yield line;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -9,12 +9,16 @@ export interface TranscriptRecord {
|
|
|
9
9
|
type: string;
|
|
10
10
|
uuid?: string;
|
|
11
11
|
parentUuid?: string | null;
|
|
12
|
+
/** Set where a compaction restarted the chain: the record the boundary follows. */
|
|
13
|
+
logicalParentUuid?: string | null;
|
|
12
14
|
sessionId?: string;
|
|
13
15
|
timestamp?: string;
|
|
14
16
|
cwd?: string;
|
|
15
17
|
gitBranch?: string;
|
|
16
18
|
version?: string;
|
|
17
19
|
isSidechain?: boolean;
|
|
20
|
+
/** On a subagent's records: the `Agent` call that started it (`subagents.ts`). */
|
|
21
|
+
parentToolUseID?: string;
|
|
18
22
|
requestId?: string;
|
|
19
23
|
message?: TranscriptMessage;
|
|
20
24
|
toolUseResult?: unknown;
|
|
@@ -106,7 +110,10 @@ export interface SessionMeta {
|
|
|
106
110
|
startedAt?: string;
|
|
107
111
|
endedAt?: string;
|
|
108
112
|
assistantRecords: number;
|
|
113
|
+
/** Reasoning with text in it: what makes `known_gap` reachable. */
|
|
109
114
|
thinkingBlocks: number;
|
|
115
|
+
/** Reasoning recorded without its text — emptied `thinking`, `redacted_thinking`. */
|
|
116
|
+
hiddenThinkingBlocks: number;
|
|
110
117
|
editToolUses: number;
|
|
111
118
|
/** Touched a file by any means — see parser.ts:shellWrites. Gates analysis. */
|
|
112
119
|
hasFileEdits: boolean;
|
|
@@ -130,8 +137,17 @@ export interface ParsedSession {
|
|
|
130
137
|
export interface SessionFile {
|
|
131
138
|
sessionId: string;
|
|
132
139
|
path: string;
|
|
140
|
+
/** Size and mtime of the session as a whole, subagents included: what decides whether it grew. */
|
|
133
141
|
sizeBytes: number;
|
|
134
142
|
modifiedAt: Date;
|
|
143
|
+
/**
|
|
144
|
+
* When the session's own transcript was last written, its subagents aside —
|
|
145
|
+
* what "the newest session here" is ordered by (`paths.ts:lastActive`). A
|
|
146
|
+
* subagent sent to the background goes on writing while its session sits
|
|
147
|
+
* idle, and by `modifiedAt` an idle session outranked the one that just
|
|
148
|
+
* made a commit. Absent where the two cannot differ.
|
|
149
|
+
*/
|
|
150
|
+
activeAt?: Date;
|
|
135
151
|
}
|
|
136
152
|
|
|
137
153
|
/**
|