tracehub 0.5.7 → 0.5.9

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.
@@ -3,6 +3,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve, sep, } from "no
3
3
  import { gitDir, repoRoot } from "../git/git.js";
4
4
  import { ranInside } from "../local/discover.js";
5
5
  import { contextRefsIn } from "../protocol/context.js";
6
+ import { AGENTS, formatSessionRef, } from "../protocol/sessionRef.js";
6
7
  import { lineHash, } from "../protocol/trace.js";
7
8
  import { redactValue, } from "../redact/redact.js";
8
9
  import { parseClaude } from "../transcript/parseClaude.js";
@@ -20,6 +21,21 @@ export function parseSessionLines(agent, id, lines, sidechain = false) {
20
21
  return parseOpencode(lines, id);
21
22
  }
22
23
  }
24
+ export function namedSessions(transcript) {
25
+ const refs = new Map();
26
+ const pattern = new RegExp(`^(${AGENTS.join("|")})-session: ([A-Za-z0-9_-]+)\\s*$`, "gm");
27
+ for (const event of transcript.events) {
28
+ if (event.kind !== "tool_result" && event.kind !== "assistant")
29
+ continue;
30
+ for (const match of event.text.matchAll(pattern)) {
31
+ const ref = { agent: match[1], id: match[2] };
32
+ if (ref.agent === transcript.agent && ref.id === transcript.id)
33
+ continue;
34
+ refs.set(formatSessionRef(ref), ref);
35
+ }
36
+ }
37
+ return [...refs.values()];
38
+ }
23
39
  function realOrSelf(path) {
24
40
  try {
25
41
  return realpathSync(path);
@@ -60,6 +76,7 @@ function addChange(turn, path, added, removed) {
60
76
  function turnsOf(events, cwd, prefix, ctx, children) {
61
77
  const turns = [];
62
78
  const callTurn = new Map();
79
+ const eventTurn = new Map();
63
80
  for (const event of events) {
64
81
  if (event.kind === "tool_result")
65
82
  continue;
@@ -68,6 +85,7 @@ function turnsOf(events, cwd, prefix, ctx, children) {
68
85
  turns.push({ at: event.ts, messages: [], changes: [], runs: [] });
69
86
  const turn = turns.at(-1);
70
87
  const index = turns.length - 1;
88
+ eventTurn.set(event, index);
71
89
  if (event.kind === "user")
72
90
  turn.messages.push({ role: "user", at: event.ts, text: event.text });
73
91
  else if (event.kind === "assistant")
@@ -90,7 +108,11 @@ function turnsOf(events, cwd, prefix, ctx, children) {
90
108
  // A subagent goes under the turn whose call launched it, else under the
91
109
  // last turn that started before it did.
92
110
  for (const child of children) {
93
- let index = child.callId === undefined ? undefined : callTurn.get(child.callId);
111
+ let index = child.event && child.event.kind !== "tool_result"
112
+ ? eventTurn.get(child.event)
113
+ : child.callId === undefined
114
+ ? undefined
115
+ : callTurn.get(child.callId);
94
116
  if (index === undefined) {
95
117
  index = turns.length - 1;
96
118
  while (index > 0 && Date.parse(turns[index].at) > child.startMs)
@@ -101,7 +123,7 @@ function turnsOf(events, cwd, prefix, ctx, children) {
101
123
  continue;
102
124
  const runPrefix = `${prefix}${index}.${turn.runs.length}.`;
103
125
  const childCwd = child.transcript.cwd || cwd;
104
- const runTurns = turnsOf(child.transcript.events, childCwd, runPrefix, ctx, []);
126
+ const runTurns = turnsOf(child.transcript.events, childCwd, runPrefix, ctx, child.children);
105
127
  const first = runTurns[0];
106
128
  if (child.prompt && !first?.messages.some((m) => m.role === "user")) {
107
129
  const at = first?.at ?? turn.at;
@@ -115,7 +137,11 @@ function turnsOf(events, cwd, prefix, ctx, children) {
115
137
  runs: [],
116
138
  });
117
139
  }
118
- const run = { label: child.label, turns: runTurns };
140
+ const run = {
141
+ label: child.label,
142
+ turns: runTurns,
143
+ ...(child.agent ? { agent: child.agent } : {}),
144
+ };
119
145
  const times = child.transcript.events
120
146
  .map((e) => Date.parse(e.ts))
121
147
  .filter((ms) => !Number.isNaN(ms));
@@ -237,8 +263,47 @@ export function buildTrace(input, opts = {}) {
237
263
  prompt: spawn?.prompt ?? "",
238
264
  callId,
239
265
  startMs: Number.isNaN(start) ? Infinity : start,
266
+ children: [],
240
267
  };
241
268
  });
269
+ const session = { ...before, events };
270
+ const owners = [
271
+ ...children.map((child) => ({
272
+ transcript: child.transcript,
273
+ children: child.children,
274
+ })),
275
+ { transcript: session, children },
276
+ ].map((owner) => ({ ...owner, named: namedSessions(owner.transcript) }));
277
+ for (const delegated of input.delegated ?? []) {
278
+ const matches = owners.flatMap((owner) => {
279
+ if (!owner.named.some((r) => r.agent === delegated.agent && r.id === delegated.id))
280
+ return [];
281
+ const pattern = new RegExp(`^${delegated.agent}-session: ${delegated.id}\\s*$`, "m");
282
+ const event = owner.transcript.events.find((e) => (e.kind === "tool_result" || e.kind === "assistant") &&
283
+ pattern.test(e.text));
284
+ return event ? [{ ...owner, event }] : [];
285
+ });
286
+ const subagents = matches
287
+ .filter((m) => m.transcript !== session)
288
+ .sort((a, b) => Date.parse(a.event.ts) - Date.parse(b.event.ts));
289
+ const owner = subagents[0] ?? matches.find((m) => m.transcript === session);
290
+ if (!owner ||
291
+ owner.children.some((c) => c.agent === delegated.agent && c.transcript.id === delegated.id))
292
+ continue;
293
+ const transcript = sliceTranscript(parseSessionLines(delegated.agent, delegated.id, delegated.lines, false), cutoffMs);
294
+ if (!transcript.events.length)
295
+ continue;
296
+ owner.children.push({
297
+ transcript,
298
+ label: delegated.agent,
299
+ agent: delegated.agent,
300
+ prompt: "",
301
+ callId: owner.event.kind === "tool_result" ? owner.event.callId : undefined,
302
+ startMs: Date.parse(owner.event.ts),
303
+ event: owner.event,
304
+ children: [],
305
+ });
306
+ }
242
307
  // A file counts as the repository's when it is in a checkout of it: this
243
308
  // one, or a worktree that shares its git directory.
244
309
  const common = gitDir(input.root);
@@ -264,7 +329,7 @@ export function buildTrace(input, opts = {}) {
264
329
  }
265
330
  }
266
331
  };
267
- const roots = (dir) => [input.root, realOrSelf(input.root), nearest(dir)].filter((r) => !!r);
332
+ const roots = (dir) => [nearest(dir), input.root, realOrSelf(input.root)].filter((r) => !!r);
268
333
  const ctx = { roots, written: [], ends: new Map() };
269
334
  const cwd = before.cwd || input.root;
270
335
  let turns = turnsOf(events, cwd, "", ctx, children.filter((c) => c.transcript.events.length));
@@ -74,7 +74,15 @@ function readAssistantLine(t, line) {
74
74
  t.events.push({ kind: "assistant", ts, text: block.text, turnId });
75
75
  }
76
76
  else if (block.type === "tool_use") {
77
- readToolCall(t, block, ts, turnId);
77
+ const message = block.name === "SubagentHandback" && block.input
78
+ ? block.input.message
79
+ : undefined;
80
+ if (typeof message === "string" && message !== "") {
81
+ t.events.push({ kind: "assistant", ts, text: message, turnId });
82
+ }
83
+ else {
84
+ readToolCall(t, block, ts, turnId);
85
+ }
78
86
  }
79
87
  }
80
88
  }
@@ -102,6 +102,69 @@ function readToolResult(r, ts, payload) {
102
102
  text: textOf(payload["output"]),
103
103
  });
104
104
  }
105
+ function contentLines(value) {
106
+ if (typeof value !== "string")
107
+ return [];
108
+ const lines = value.split("\n");
109
+ if (lines.at(-1) === "")
110
+ lines.pop();
111
+ return lines;
112
+ }
113
+ function readFileChange(r, ts, payload) {
114
+ const item = payload["item"];
115
+ if (!item || typeof item !== "object")
116
+ return;
117
+ const change = item;
118
+ if (change["type"] !== "FileChange" || change["status"] !== "completed")
119
+ return;
120
+ const changes = change["changes"];
121
+ if (!changes || typeof changes !== "object" || Array.isArray(changes))
122
+ return;
123
+ const entries = Object.entries(changes);
124
+ const edits = [];
125
+ for (const [path, value] of entries) {
126
+ if (!value || typeof value !== "object")
127
+ continue;
128
+ const file = value;
129
+ const edit = {
130
+ path: stringOf(file["move_path"]) ?? path,
131
+ removed: [],
132
+ added: [],
133
+ };
134
+ switch (file["type"]) {
135
+ case "add":
136
+ edit.added = contentLines(file["content"]);
137
+ break;
138
+ case "delete":
139
+ edit.removed = contentLines(file["content"]);
140
+ break;
141
+ case "update": {
142
+ const diff = file["unified_diff"];
143
+ if (typeof diff !== "string")
144
+ continue;
145
+ for (const line of diff.split("\n")) {
146
+ if (line.startsWith("+"))
147
+ edit.added.push(line.slice(1));
148
+ else if (line.startsWith("-"))
149
+ edit.removed.push(line.slice(1));
150
+ }
151
+ break;
152
+ }
153
+ default:
154
+ continue;
155
+ }
156
+ edits.push(edit);
157
+ }
158
+ r.transcript.events.push({
159
+ kind: "tool_call",
160
+ ts,
161
+ name: "apply_patch",
162
+ arg: entries[0]?.[0] ?? "",
163
+ callId: String(change["id"] ?? ""),
164
+ turnId: r.turnId,
165
+ edits,
166
+ });
167
+ }
105
168
  function readResponseItem(r, ts, payload) {
106
169
  switch (payload["type"]) {
107
170
  case "message":
@@ -129,6 +192,9 @@ function readLine(r, line) {
129
192
  }
130
193
  return;
131
194
  case "event_msg":
195
+ if (payload["type"] === "item_completed") {
196
+ readFileChange(r, ts, payload);
197
+ }
132
198
  if (payload["type"] === "task_started") {
133
199
  r.turnId = String(payload["turn_id"] ?? r.turnId);
134
200
  r.active = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tracehub",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "Attach coding-agent sessions (Claude Code, Codex, OpenCode) to git commits and pull requests, redacted.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -14,17 +14,6 @@
14
14
  "dist",
15
15
  "README.md"
16
16
  ],
17
- "scripts": {
18
- "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc && chmod +x dist/cli.js",
19
- "typecheck": "tsc --noEmit -p tsconfig.test.json",
20
- "format": "oxfmt --check",
21
- "format:fix": "oxfmt",
22
- "test": "tsc -p tsconfig.test.json && vitest run",
23
- "test:coverage": "tsc -p tsconfig.test.json && vitest run --coverage --coverage.reporter=text --coverage.reporter=html --coverage.reporter=json-summary",
24
- "prepack": "npm run build",
25
- "docs:install": "pnpm --dir docs install",
26
- "docs:dev": "pnpm --dir docs dev"
27
- },
28
17
  "devDependencies": {
29
18
  "@types/node": "^26.6.6",
30
19
  "@vitest/coverage-v8": "^5.0.5",
@@ -46,14 +35,20 @@
46
35
  "dependencies": {
47
36
  "@clack/prompts": "^1.8.1"
48
37
  },
49
- "allowScripts": {
50
- "fsevents@2.3.3": true
51
- },
52
38
  "devEngines": {
53
39
  "packageManager": {
54
- "name": "npm",
55
- "version": ">=11.17.0",
40
+ "name": "pnpm",
41
+ "version": ">=12",
56
42
  "onFail": "error"
57
43
  }
44
+ },
45
+ "scripts": {
46
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc && chmod +x dist/cli.js",
47
+ "typecheck": "tsc --noEmit -p tsconfig.test.json && tsc -p tsconfig.shared.json",
48
+ "format": "oxfmt --check",
49
+ "format:fix": "oxfmt",
50
+ "test": "tsc -p tsconfig.test.json && vitest run",
51
+ "test:coverage": "tsc -p tsconfig.test.json && vitest run --coverage --coverage.reporter=text --coverage.reporter=html --coverage.reporter=json-summary",
52
+ "docs:dev": "pnpm --filter tracehub-docs dev"
58
53
  }
59
- }
54
+ }