tracehub 0.5.9 → 0.5.11

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.
@@ -80,28 +80,40 @@ function turnsOf(events, cwd, prefix, ctx, children) {
80
80
  for (const event of events) {
81
81
  if (event.kind === "tool_result")
82
82
  continue;
83
- const opens = event.kind === "user" && !event.steering;
83
+ const opens = (event.kind === "user" && !event.steering) || event.kind === "system";
84
84
  if (opens || !turns.length)
85
85
  turns.push({ at: event.ts, messages: [], changes: [], runs: [] });
86
86
  const turn = turns.at(-1);
87
87
  const index = turns.length - 1;
88
88
  eventTurn.set(event, index);
89
- if (event.kind === "user")
90
- turn.messages.push({ role: "user", at: event.ts, text: event.text });
89
+ if (event.kind === "user" || event.kind === "system")
90
+ turn.messages.push({ role: event.kind, at: event.ts, text: event.text });
91
91
  else if (event.kind === "assistant")
92
92
  turn.messages.push({ role: "agent", at: event.ts, text: event.text });
93
93
  else {
94
94
  if (event.callId)
95
95
  callTurn.set(event.callId, index);
96
96
  for (const edit of event.edits ?? []) {
97
- const abs = isAbsolute(edit.path) ? edit.path : resolve(cwd, edit.path);
98
- const path = repoPath(abs, cwd, ctx.roots(dirname(abs)));
99
- if (!path)
97
+ const file = ctx.file(edit.path, cwd);
98
+ if (!file)
100
99
  continue;
101
- addChange(turn, path, edit.added.length, edit.removed.length);
100
+ const { path, home } = file;
101
+ const change = {
102
+ turn,
103
+ added: edit.added.length,
104
+ removed: edit.removed.length,
105
+ };
106
+ if (home)
107
+ addChange(turn, path, change.added, change.removed);
102
108
  const hashes = new Set(edit.added.map(lineHash).filter((h) => h !== undefined));
103
109
  const ts = Date.parse(event.ts);
104
- ctx.written.push({ turn: `${prefix}${index}`, ts, path, hashes });
110
+ ctx.written.push({
111
+ turn: `${prefix}${index}`,
112
+ ts,
113
+ path,
114
+ hashes,
115
+ ...(!home ? { change } : {}),
116
+ });
105
117
  }
106
118
  }
107
119
  }
@@ -123,6 +135,7 @@ function turnsOf(events, cwd, prefix, ctx, children) {
123
135
  continue;
124
136
  const runPrefix = `${prefix}${index}.${turn.runs.length}.`;
125
137
  const childCwd = child.transcript.cwd || cwd;
138
+ const start = ctx.written.length;
126
139
  const runTurns = turnsOf(child.transcript.events, childCwd, runPrefix, ctx, child.children);
127
140
  const first = runTurns[0];
128
141
  if (child.prompt && !first?.messages.some((m) => m.role === "user")) {
@@ -147,6 +160,9 @@ function turnsOf(events, cwd, prefix, ctx, children) {
147
160
  .filter((ms) => !Number.isNaN(ms));
148
161
  if (times.length)
149
162
  ctx.ends.set(run, Math.max(...times));
163
+ const written = ctx.written.slice(start);
164
+ if (written.length && written.every((w) => w.change))
165
+ ctx.foreignOnly.add(run);
150
166
  turn.runs.push(run);
151
167
  }
152
168
  return turns;
@@ -161,6 +177,7 @@ function sourcemapOf(written, added) {
161
177
  for (const w of ordered)
162
178
  byPath.set(w.path, [...(byPath.get(w.path) ?? []), w]);
163
179
  const map = {};
180
+ const attributed = new Set();
164
181
  for (const [path, lines] of added) {
165
182
  const edits = byPath.get(path);
166
183
  if (!edits)
@@ -170,49 +187,94 @@ function sourcemapOf(written, added) {
170
187
  if (!hash)
171
188
  continue;
172
189
  const last = [...edits].reverse().find((w) => w.hashes.has(hash));
173
- if (last)
190
+ if (last) {
174
191
  (map[path] ??= {})[hash] = last.turn;
192
+ attributed.add(last);
193
+ }
175
194
  }
176
195
  }
196
+ for (const { path, change } of attributed)
197
+ if (change)
198
+ addChange(change.turn, path, change.added, change.removed);
177
199
  return map;
178
200
  }
179
201
  // The part of a session that made one commit, so a long session that
180
202
  // commits many times shows each commit its own work: the turns and runs
181
203
  // that wrote the commit's lines, and the turns with messages since the
182
204
  // session's previous commit. A turn kept only for a run keeps its prompt
183
- // and its messages since that commit. Without attributed lines, the runs
184
- // active since that commit stay. The sourcemap's turn paths follow the
185
- // kept turns.
186
- function scopeToCommit(turns, sourcemap, sinceMs, ends) {
205
+ // and its messages since that commit. Without attributed lines, a run stays
206
+ // when it was active since that commit, did not work only in another
207
+ // checkout, and either changed no file or changed a path of this commit in
208
+ // its own turns or nested runs.
209
+ // A kept prompt keeps the reply it answers, the last agent message
210
+ // of the turn before, in a turn of its own when that turn is out of scope.
211
+ // The sourcemap's turn paths follow the kept turns.
212
+ function scopeToCommit(turns, sourcemap, sinceMs, ends, foreignOnly, changedPaths) {
187
213
  const hits = new Set(Object.values(sourcemap).flatMap((lines) => Object.values(lines)));
188
- const paths = new Map();
189
- const kept = [];
190
- turns.forEach((turn, i) => {
214
+ const decisions = turns.map((turn, i) => {
191
215
  const own = hits.has(`${i}`);
192
216
  const recent = i === turns.length - 1 ||
193
217
  [turn.at, ...turn.messages.map((m) => m.at)].some((at) => Date.parse(at) >= sinceMs);
194
- const runs = [];
195
- turn.runs.forEach((run, j) => {
218
+ const runs = turn.runs.flatMap((run, j) => {
196
219
  const prefix = `${i}.${j}.`;
197
220
  const wrote = [...hits].filter((h) => h.startsWith(prefix));
198
221
  if (!wrote.length &&
199
- (hits.size > 0 || !((ends.get(run) ?? -Infinity) > sinceMs)))
200
- return;
201
- for (const h of wrote)
202
- paths.set(h, `${kept.length}.${runs.length}.${h.slice(prefix.length)}`);
203
- runs.push(run);
222
+ (hits.size > 0 ||
223
+ foreignOnly.has(run) ||
224
+ (changed(run.turns) && !changed(run.turns, changedPaths)) ||
225
+ !((ends.get(run) ?? -Infinity) > sinceMs)))
226
+ return [];
227
+ return [{ run, index: j, wrote }];
204
228
  });
205
229
  if (!own && !recent && !runs.length)
230
+ return undefined;
231
+ return {
232
+ own,
233
+ recent,
234
+ messages: own
235
+ ? turn.messages
236
+ : turn.messages.filter((m, k) => (k === 0 && m.role !== "agent") || Date.parse(m.at) >= sinceMs),
237
+ runs,
238
+ };
239
+ });
240
+ decisions.forEach((decision, i) => {
241
+ if (!decision || i === 0 || decision.messages[0]?.role !== "user")
206
242
  return;
207
- if (own)
243
+ const previous = turns[i - 1];
244
+ const reply = [...previous.messages]
245
+ .reverse()
246
+ .find((m) => m.role === "agent");
247
+ if (!reply)
248
+ return;
249
+ const prior = decisions[i - 1];
250
+ if (prior)
251
+ prior.messages = previous.messages.filter((m) => m === reply || prior.messages.includes(m));
252
+ else
253
+ decisions[i - 1] = {
254
+ own: false,
255
+ recent: false,
256
+ messages: [reply],
257
+ runs: [],
258
+ };
259
+ });
260
+ const paths = new Map();
261
+ const kept = [];
262
+ decisions.forEach((decision, i) => {
263
+ if (!decision)
264
+ return;
265
+ const turn = turns[i];
266
+ if (decision.own)
208
267
  paths.set(`${i}`, `${kept.length}`);
268
+ decision.runs.forEach(({ index, wrote }, j) => {
269
+ const prefix = `${i}.${index}.`;
270
+ for (const h of wrote)
271
+ paths.set(h, `${kept.length}.${j}.${h.slice(prefix.length)}`);
272
+ });
209
273
  kept.push({
210
274
  at: turn.at,
211
- messages: own
212
- ? turn.messages
213
- : turn.messages.filter((m, k) => (k === 0 && m.role === "user") || Date.parse(m.at) >= sinceMs),
214
- changes: own || recent ? turn.changes : [],
215
- runs,
275
+ messages: decision.messages,
276
+ changes: decision.own || decision.recent ? turn.changes : [],
277
+ runs: decision.runs.map(({ run }) => run),
216
278
  });
217
279
  });
218
280
  const scoped = {};
@@ -230,8 +292,9 @@ function briefsOf(turns) {
230
292
  ];
231
293
  }
232
294
  // Whether any of the turns, or their runs, changed a file.
233
- function changed(turns) {
234
- return turns.some((t) => t.changes.length > 0 || t.runs.some((r) => changed(r.turns)));
295
+ function changed(turns, paths) {
296
+ return turns.some((t) => t.changes.some((c) => !paths || paths.has(c.path)) ||
297
+ t.runs.some((r) => changed(r.turns, paths)));
235
298
  }
236
299
  // What leaves the machine for one session at one commit: its part of the
237
300
  // turns up to the commit (and the agent's replies that finish the commit's
@@ -246,7 +309,7 @@ export function buildTrace(input, opts = {}) {
246
309
  const before = parseSessionLines(agent, id, lines.slice(0, cut), sidechain);
247
310
  const events = [...before.events];
248
311
  for (const event of whole.events.slice(before.events.length)) {
249
- if (event.kind === "user" && !event.steering)
312
+ if ((event.kind === "user" && !event.steering) || event.kind === "system")
250
313
  break;
251
314
  if (event.kind === "assistant")
252
315
  events.push(event);
@@ -267,6 +330,7 @@ export function buildTrace(input, opts = {}) {
267
330
  };
268
331
  });
269
332
  const session = { ...before, events };
333
+ const transcripts = [session, ...children.map((child) => child.transcript)];
270
334
  const owners = [
271
335
  ...children.map((child) => ({
272
336
  transcript: child.transcript,
@@ -293,6 +357,7 @@ export function buildTrace(input, opts = {}) {
293
357
  const transcript = sliceTranscript(parseSessionLines(delegated.agent, delegated.id, delegated.lines, false), cutoffMs);
294
358
  if (!transcript.events.length)
295
359
  continue;
360
+ transcripts.push(transcript);
296
361
  owner.children.push({
297
362
  transcript,
298
363
  label: delegated.agent,
@@ -316,21 +381,47 @@ export function buildTrace(input, opts = {}) {
316
381
  return worktrees.get(dir);
317
382
  };
318
383
  const ownCheckout = (cwd) => !!cwd && (ranInside(cwd, input.root) || worktreeOf(cwd) !== undefined);
384
+ const recordedCwds = new Set(transcripts
385
+ .flatMap((t) => t.cwds)
386
+ .filter((cwd) => cwd && ranInside(cwd, input.root))
387
+ .map((cwd) => resolve(cwd)));
319
388
  // The checkout a file is in, when it is one of the repository's.
320
389
  const nearest = (dir) => {
390
+ let missing = "";
321
391
  for (let at = dir;; at = dirname(at)) {
322
392
  try {
323
393
  realpathSync(at);
394
+ if (missing &&
395
+ ranInside(missing, input.root) &&
396
+ [...recordedCwds].some((cwd) => ranInside(cwd, missing)))
397
+ return missing;
324
398
  return worktreeOf(at);
325
399
  }
326
400
  catch {
401
+ missing = at;
327
402
  if (dirname(at) === at)
328
403
  return undefined;
329
404
  }
330
405
  }
331
406
  };
332
- const roots = (dir) => [nearest(dir), input.root, realOrSelf(input.root)].filter((r) => !!r);
333
- const ctx = { roots, written: [], ends: new Map() };
407
+ const commitRoot = realOrSelf(input.root);
408
+ const ctx = {
409
+ file: (path, cwd) => {
410
+ const abs = isAbsolute(path) ? path : resolve(cwd, path);
411
+ const checkout = nearest(dirname(abs));
412
+ const roots = [checkout, input.root, commitRoot].filter((r) => !!r);
413
+ const relative = repoPath(abs, cwd, roots);
414
+ return relative
415
+ ? {
416
+ path: relative,
417
+ home: !checkout || realOrSelf(checkout) === commitRoot,
418
+ }
419
+ : undefined;
420
+ },
421
+ written: [],
422
+ ends: new Map(),
423
+ foreignOnly: new Set(),
424
+ };
334
425
  const cwd = before.cwd || input.root;
335
426
  let turns = turnsOf(events, cwd, "", ctx, children.filter((c) => c.transcript.events.length));
336
427
  // A session that ran outside the repository (a coordinator whose
@@ -340,6 +431,9 @@ export function buildTrace(input, opts = {}) {
340
431
  // whatever cwd its lines carry over from its parent, and so does a
341
432
  // session that made the commit itself (`cd <checkout> && git commit`).
342
433
  const outside = !sidechain && !input.committer && !ownCheckout(cwd);
434
+ if (outside)
435
+ ctx.written = ctx.written.filter((w) => w.turn.includes("."));
436
+ const sourcemap = sourcemapOf(ctx.written, input.added);
343
437
  if (outside) {
344
438
  turns = turns.map((turn) => ({
345
439
  at: turn.at,
@@ -350,7 +444,6 @@ export function buildTrace(input, opts = {}) {
350
444
  // its label: the label is the coordinator's own description.
351
445
  changed(run.turns) ? run : { label: "", turns: [] }),
352
446
  }));
353
- ctx.written = ctx.written.filter((w) => w.turn.includes("."));
354
447
  if (!turns.some((turn) => turn.runs.some((run) => run.turns.length)))
355
448
  return undefined;
356
449
  }
@@ -360,7 +453,7 @@ export function buildTrace(input, opts = {}) {
360
453
  ? sessionTitle(lines.slice(0, cut))
361
454
  : undefined;
362
455
  const since = Date.parse(input.since ?? "");
363
- const scoped = scopeToCommit(turns, sourcemapOf(ctx.written, input.added), Number.isNaN(since) ? -Infinity : since, ctx.ends);
456
+ const scoped = scopeToCommit(turns, sourcemap, Number.isNaN(since) ? -Infinity : since, ctx.ends, ctx.foreignOnly, new Set(input.added.keys()));
364
457
  // The session's first message names its conversation even when its turn
365
458
  // is out of scope; a run's brief counts only for the runs in scope.
366
459
  const first = turns[0]?.messages.find((m) => m.role === "user");
@@ -1,5 +1,8 @@
1
+ import { harnessEvent } from "../protocol/harness.js";
1
2
  import { editsOf, firstArg, parseJsonl, } from "./transcript.js";
2
- const INJECTED_PREFIX = /^\s*<(command-message|command-name|local-command-stdout|local-command-caveat|system-reminder|task-notification|bash-input|bash-stdout)/;
3
+ // What the harness injected that stays on the machine: local output and
4
+ // reminders. Other harness text (a CI event) is uploaded as the system's.
5
+ const INJECTED_PREFIX = /^\s*<(command-message|command-name|command-args|local-command-stdout|local-command-stderr|local-command-caveat|system-reminder|task-notification|bash-input|bash-stdout|bash-stderr|user-prompt-submit-hook)/;
3
6
  function cleanUserText(text) {
4
7
  return text
5
8
  .replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "")
@@ -35,9 +38,9 @@ function readUserLine(t, line) {
35
38
  t.events.push({ kind: "tool_result", ts, callId, text });
36
39
  }
37
40
  const text = cleanUserText(blockText(content));
38
- if (text && !INJECTED_PREFIX.test(text)) {
39
- t.events.push({ kind: "user", ts, text });
40
- }
41
+ if (!text || INJECTED_PREFIX.test(text))
42
+ return;
43
+ t.events.push({ kind: harnessEvent(text) ? "system" : "user", ts, text });
41
44
  }
42
45
  const SPAWN_TOOLS = ["Task", "Agent"];
43
46
  function readToolCall(t, block, ts, turnId) {
@@ -91,6 +94,7 @@ export function parseClaude(jsonl, id, opts = {}) {
91
94
  agent: "claude",
92
95
  id,
93
96
  cwd: "",
97
+ cwds: [],
94
98
  model: undefined,
95
99
  events: [],
96
100
  spawns: [],
@@ -101,6 +105,8 @@ export function parseClaude(jsonl, id, opts = {}) {
101
105
  continue;
102
106
  if (!transcript.cwd && line.cwd)
103
107
  transcript.cwd = line.cwd;
108
+ if (line.cwd && !transcript.cwds.includes(line.cwd))
109
+ transcript.cwds.push(line.cwd);
104
110
  if (!line.message)
105
111
  continue;
106
112
  if (line.type === "user")
@@ -181,6 +181,9 @@ function readLine(r, line) {
181
181
  const ts = line.timestamp ?? "";
182
182
  const payload = line.payload ?? {};
183
183
  const t = r.transcript;
184
+ const cwd = stringOf(payload["cwd"]);
185
+ if (cwd && !t.cwds.includes(cwd))
186
+ t.cwds.push(cwd);
184
187
  switch (line.type) {
185
188
  case "session_meta":
186
189
  t.cwd = String(payload["cwd"] ?? "");
@@ -215,6 +218,7 @@ export function parseCodex(jsonl, id) {
215
218
  agent: "codex",
216
219
  id,
217
220
  cwd: "",
221
+ cwds: [],
218
222
  model: undefined,
219
223
  events: [],
220
224
  spawns: [],
@@ -48,6 +48,8 @@ function readTool(r, part) {
48
48
  }
49
49
  function readLine(r, line) {
50
50
  const t = r.transcript;
51
+ if (line.cwd && !t.cwds.includes(line.cwd))
52
+ t.cwds.push(line.cwd);
51
53
  const time = typeof line.time === "object" ? line.time : line.state?.time;
52
54
  const created = typeof line.time === "number" ? line.time : undefined;
53
55
  r.ts = isoTime(created ?? time?.end ?? time?.start) ?? r.ts;
@@ -71,6 +73,7 @@ export function parseOpencode(jsonl, id) {
71
73
  agent: "opencode",
72
74
  id,
73
75
  cwd: "",
76
+ cwds: [],
74
77
  model: undefined,
75
78
  events: [],
76
79
  spawns: [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tracehub",
3
- "version": "0.5.9",
3
+ "version": "0.5.11",
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",