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.
@@ -0,0 +1,262 @@
1
+ import { readFileSync, statSync } from "node:fs";
2
+ import { isAbsolute, relative, resolve, sep } from "node:path";
3
+ import { readPage, webUrl } from "../../client/tracehub.js";
4
+ import { repoRoot, tryGit } from "../../git/git.js";
5
+ import { lineHash } from "../../protocol/trace.js";
6
+ import { isTraceId } from "../../protocol/trailers.js";
7
+ import { prOf } from "../args.js";
8
+ import { out } from "../io.js";
9
+ import { signedInClient } from "../remote.js";
10
+ import { uploadTargetFor } from "../repo.js";
11
+ function pagePath(raw) {
12
+ let url;
13
+ try {
14
+ url = new URL(raw);
15
+ }
16
+ catch {
17
+ throw new Error("invalid TraceHub page URL");
18
+ }
19
+ if (url.origin !== new URL(webUrl()).origin)
20
+ throw new Error(`page URL must be on ${new URL(webUrl()).origin}`);
21
+ const match = /^\/(t|c|pr)\/([A-Za-z0-9._-]{1,100})\/([A-Za-z0-9._-]{1,100})\/([A-Za-z0-9]{1,40})(\.json|\/json)?\/?$/.exec(url.pathname);
22
+ if (match) {
23
+ const [, kind, owner, repo, rawId] = match;
24
+ const id = kind === "c" ? rawId.toLowerCase() : rawId;
25
+ if (![owner, repo].some((s) => s === "." || s === "..") &&
26
+ (kind === "t"
27
+ ? isTraceId(id)
28
+ : kind === "c"
29
+ ? /^[0-9a-f]{7,40}$/.test(id)
30
+ : /^[1-9]\d{0,9}$/.test(id)))
31
+ return `/${kind}/${owner.toLowerCase()}/${repo.toLowerCase()}/${id}`;
32
+ }
33
+ throw new Error("URL must name a TraceHub trace, commit or pull request page");
34
+ }
35
+ const time = (iso) => `${new Date(iso).toISOString().slice(0, 16).replace("T", " ")} UTC`;
36
+ function blameLine(raw, cwd, root) {
37
+ const match = /^(.+):([1-9]\d*)$/.exec(raw);
38
+ if (!match || isAbsolute(match[1]))
39
+ return undefined;
40
+ const file = resolve(cwd, match[1]);
41
+ const path = relative(root, file);
42
+ if (path === ".." || path.startsWith(`..${sep}`))
43
+ return undefined;
44
+ const stat = statSync(file, { throwIfNoEntry: false });
45
+ if (!stat)
46
+ throw new Error(`file "${match[1]}" does not exist`);
47
+ if (!stat.isFile())
48
+ return undefined;
49
+ const line = Number(match[2]);
50
+ const contents = readFileSync(file, "utf8");
51
+ const count = contents
52
+ ? contents.split("\n").length - Number(contents.endsWith("\n"))
53
+ : 0;
54
+ if (!Number.isSafeInteger(line) || line > count)
55
+ throw new Error(`${raw} is past the end of the file`);
56
+ const blame = tryGit([
57
+ "-c",
58
+ "core.quotePath=false",
59
+ "blame",
60
+ "--porcelain",
61
+ "-L",
62
+ `${line},${line}`,
63
+ "--",
64
+ path,
65
+ ], { cwd: root });
66
+ if (!blame) {
67
+ if (!tryGit(["ls-files", "--error-unmatch", "--", path], { cwd: root }) ||
68
+ !tryGit(["rev-parse", "--verify", "--quiet", "HEAD"], { cwd: root }))
69
+ throw new Error(`${raw} is not yet committed`);
70
+ throw new Error(`could not blame ${raw}`);
71
+ }
72
+ const commit = blame.split(" ", 1)[0];
73
+ if (/^0+$/.test(commit))
74
+ throw new Error(`${raw} is not yet committed`);
75
+ const filename = /^filename (.+)$/m.exec(blame)?.[1];
76
+ const text = /^\t(.*)$/m.exec(blame)?.[1] ?? "";
77
+ if (!filename)
78
+ throw new Error(`could not read the filename for ${raw}`);
79
+ return {
80
+ commit,
81
+ path: filename.startsWith('"')
82
+ ? JSON.parse(filename)
83
+ : filename,
84
+ line,
85
+ text,
86
+ };
87
+ }
88
+ export function renderTurnPath(path) {
89
+ return path
90
+ .split(".")
91
+ .map((index, depth) => {
92
+ const number = Number(index) + 1;
93
+ return depth === 0
94
+ ? String(number)
95
+ : `${depth % 2 ? "run" : "turn"} ${number}`;
96
+ })
97
+ .join(" › ");
98
+ }
99
+ function messageBody(message, level) {
100
+ const indent = " ".repeat(level);
101
+ const lines = (message.withheld ? "[withheld: looked sensitive]" : message.text)
102
+ .split(/\r?\n/)
103
+ .map((line) => indent + line);
104
+ if (message.translation) {
105
+ lines.push(`${indent}in English (from ${message.translation.from}):`);
106
+ lines.push(...message.translation.text
107
+ .split(/\r?\n/)
108
+ .map((line) => `${indent} ${line}`));
109
+ }
110
+ return lines.join("\n");
111
+ }
112
+ function contextText(context, login) {
113
+ return [
114
+ `Context ${context.ref}${context.title ? ` · ${context.title}` : ""}${login ? ` · attached by @${login}` : ""}`,
115
+ ...(context.url ? [context.url] : []),
116
+ ...context.messages.map((message) => ` ${message.author} · ${time(message.at)}\n${messageBody(message, 2)}`),
117
+ ].join("\n");
118
+ }
119
+ function turnText(turn, index, level = 0) {
120
+ const indent = " ".repeat(level);
121
+ return [
122
+ `${indent}Turn ${index + 1} · ${time(turn.at)}`,
123
+ ...turn.messages.map((message) => `${indent} ${message.role} · ${time(message.at)}\n${messageBody(message, level + 2)}`),
124
+ ...(turn.changes.length
125
+ ? [
126
+ `${indent} changed ${turn.changes.map((change) => `${change.path} +${change.added} -${change.removed}`).join(", ")}`,
127
+ ]
128
+ : []),
129
+ ...turn.runs.map((run, runIndex) => [
130
+ `${indent} Run ${runIndex + 1} · ${run.label}${run.agent ? ` · ${run.agent}` : ""}`,
131
+ ...(run.turns.length ? [turnsText(run.turns, level + 2)] : []),
132
+ ].join("\n")),
133
+ ].join("\n");
134
+ }
135
+ function turnsText(turns, level = 0) {
136
+ return turns.map((turn, index) => turnText(turn, index, level)).join("\n\n");
137
+ }
138
+ function sessionHeader(session) {
139
+ return `Session ${session.agent}${session.title ? ` · ${session.title}` : ""}${session.model ? ` · ${session.model}` : ""}${session.uploader.login ? ` · @${session.uploader.login}` : ""}`;
140
+ }
141
+ function documentBlocks(doc) {
142
+ return [
143
+ ...doc.context.map((context) => contextText(context)),
144
+ ...doc.sessions.map((session) => [
145
+ sessionHeader(session),
146
+ ...(session.turns.length ? [turnsText(session.turns)] : []),
147
+ ].join("\n")),
148
+ ];
149
+ }
150
+ export function renderShow(doc, path) {
151
+ const repo = `${doc.owner}/${doc.repo}`;
152
+ const title = "number" in doc
153
+ ? `${repo}#${doc.number}${doc.title ? ` ${doc.title}` : ""}`
154
+ : "sha" in doc
155
+ ? `${repo}@${doc.sha.slice(0, 7)}`
156
+ : `${repo} trace ${doc.traceId}`;
157
+ const blocks = [`${title}\n${webUrl()}${path}`];
158
+ if ("traceId" in doc)
159
+ blocks.push(...documentBlocks(doc));
160
+ else {
161
+ const commits = "number" in doc
162
+ ? doc.commits
163
+ : [
164
+ {
165
+ sha: doc.sha,
166
+ traceIds: doc.traces.map((trace) => trace.traceId),
167
+ },
168
+ ];
169
+ if (!commits.some((commit) => doc.traces.some((trace) => commit.traceIds.includes(trace.traceId))))
170
+ blocks.push("no traces");
171
+ if ("number" in doc)
172
+ for (const attached of doc.context)
173
+ blocks.push(...attached.context.map((context) => contextText(context, attached.uploader.login)));
174
+ for (const commit of commits) {
175
+ const traces = commit.traceIds.flatMap((id) => doc.traces.filter((trace) => trace.traceId === id));
176
+ if (!traces.length) {
177
+ if ("number" in doc)
178
+ blocks.push(`Commit ${commit.sha.slice(0, 7)} · no trace`);
179
+ continue;
180
+ }
181
+ for (const trace of traces) {
182
+ blocks.push(`Commit ${commit.sha.slice(0, 7)} · trace ${trace.traceId}\n${webUrl()}/t/${doc.owner}/${doc.repo}/${trace.traceId}`, ...documentBlocks(trace));
183
+ }
184
+ }
185
+ }
186
+ return blocks.join("\n\n");
187
+ }
188
+ export async function cmdShow(args, cwd) {
189
+ const hasPr = args.flags["pr"] !== undefined;
190
+ const positional = args.positional.slice(1);
191
+ if (positional.length !== (hasPr ? 0 : 1) ||
192
+ (hasPr && typeof args.flags["pr"] !== "string") ||
193
+ args.rest?.length ||
194
+ Object.keys(args.flags).some((flag) => flag !== "pr" && flag !== "json") ||
195
+ (args.flags["json"] !== undefined && args.flags["json"] !== true))
196
+ throw new Error('name exactly one commit, <path>:<line>, --pr <n> or page URL; see "tracehub show --help"');
197
+ const pr = prOf(args);
198
+ const revision = positional[0];
199
+ let path;
200
+ let line;
201
+ if (revision && /^https?:\/\//.test(revision))
202
+ path = pagePath(revision);
203
+ else {
204
+ const root = repoRoot(cwd);
205
+ if (!root)
206
+ throw new Error("not a git repository; run it in a clone of the repository");
207
+ const target = uploadTargetFor(cwd);
208
+ if (!target)
209
+ throw new Error("this clone's origin is not a repository TraceHub serves (assistant-ui)");
210
+ line = revision ? blameLine(revision, cwd, root) : undefined;
211
+ const sha = line?.commit ??
212
+ (pr === undefined
213
+ ? tryGit([
214
+ "rev-parse",
215
+ "--verify",
216
+ "--quiet",
217
+ "--end-of-options",
218
+ `${revision}^{commit}`,
219
+ ], { cwd })
220
+ : undefined);
221
+ if (pr === undefined && !sha)
222
+ throw new Error(`unknown commit revision "${revision}"`);
223
+ path = `/${pr === undefined ? "c" : "pr"}/${target.owner}/${target.repo}/${pr ?? sha}`;
224
+ }
225
+ const { auth } = await signedInClient();
226
+ const doc = await readPage(path, auth.accessToken);
227
+ if (line) {
228
+ if (!("sha" in doc))
229
+ throw new Error("expected a commit page");
230
+ const hash = lineHash(line.text);
231
+ const matches = doc.traces.flatMap((trace) => trace.sessions.flatMap((session) => {
232
+ const turn = hash ? session.sourcemap[line.path]?.[hash] : undefined;
233
+ return turn === undefined
234
+ ? []
235
+ : [{ traceId: trace.traceId, session, turn }];
236
+ }));
237
+ if (args.flags["json"])
238
+ out(JSON.stringify({
239
+ commit: line.commit,
240
+ path: line.path,
241
+ line: line.line,
242
+ matches: matches.map(({ traceId, session, turn }) => ({
243
+ traceId,
244
+ session: session.name,
245
+ turn,
246
+ })),
247
+ page: doc,
248
+ }, null, 2));
249
+ else if (matches.length)
250
+ out([
251
+ `${doc.owner}/${doc.repo}@${line.commit.slice(0, 7)}\n${webUrl()}${path}`,
252
+ ...matches.map(({ traceId, session, turn }) => {
253
+ const index = Number(turn.split(".", 1)[0]);
254
+ return `${revision} was written in ${traceId} · ${sessionHeader(session)} · turn ${renderTurnPath(turn)}\n${turnText(session.turns[index], index)}`;
255
+ }),
256
+ ].join("\n\n"));
257
+ else
258
+ out(`${revision} is not attributed to a turn; the commit's conversations follow\n\n${renderShow(doc, path)}`);
259
+ return;
260
+ }
261
+ out(args.flags["json"] ? JSON.stringify(doc, null, 2) : renderShow(doc, path));
262
+ }
package/dist/cli/usage.js CHANGED
@@ -68,6 +68,19 @@ export const COMMANDS = [
68
68
  "tracehub backfill 1a2b3c4d",
69
69
  ],
70
70
  },
71
+ {
72
+ name: "show",
73
+ usage: "show (<commit> | <path>:<line> | --pr <n> | <url>) [--json]",
74
+ summary: "print the conversations behind a commit, a pull request or a trace",
75
+ about: "A commit reads the conversations behind that revision in this clone; --pr <n> reads those behind a pull request of this clone's repository. <path>:<line> finds the prompt that wrote a committed line in this clone, or shows the commit's conversations when the line is unattributed. A TraceHub page URL reads a trace, commit or pull request, even outside a clone. Needs a sign-in and shows only what the web page would show you. --json prints the page's document as served, with the commit, historical path, line and matches for a line lookup.",
76
+ examples: [
77
+ "tracehub show HEAD",
78
+ "tracehub show src/cli/commands/show.ts:42",
79
+ "tracehub show --pr 52",
80
+ "tracehub show https://tracehub.aui.dev/t/assistant-ui/tracehub/0123456789abcdef",
81
+ "tracehub show HEAD --json",
82
+ ],
83
+ },
71
84
  ];
72
85
  export const ENV = [
73
86
  {
package/dist/cli.js CHANGED
@@ -5,6 +5,7 @@ import { cmdHook } from "./cli/commands/hook.js";
5
5
  import { cmdContext } from "./cli/commands/context.js";
6
6
  import { cmdProviders } from "./cli/commands/providers.js";
7
7
  import { cmdSetup } from "./cli/commands/setup.js";
8
+ import { cmdShow } from "./cli/commands/show.js";
8
9
  import { cmdUninstall } from "./cli/commands/uninstall.js";
9
10
  import { fail, Reported } from "./cli/io.js";
10
11
  import { commandUsage, usage } from "./cli/usage.js";
@@ -14,6 +15,7 @@ const COMMAND_HANDLERS = {
14
15
  providers: cmdProviders,
15
16
  context: cmdContext,
16
17
  backfill: cmdBackfill,
18
+ show: cmdShow,
17
19
  };
18
20
  async function main() {
19
21
  const args = parseArgs(process.argv.slice(2));
@@ -119,10 +119,12 @@ export class TracehubClient {
119
119
  return (await res.json());
120
120
  }
121
121
  }
122
- // Whether TraceHub has any session of the trace, asked of the site's
123
- // `.json` page with the user's GitHub token.
124
- export async function traceUploaded(target, traceId, token) {
125
- const res = await fetch(`${webUrl()}/t/${target.owner}/${target.repo}/${traceId}.json`, {
122
+ export async function readPage(path, token) {
123
+ const origin = new URL(webUrl()).origin;
124
+ const url = new URL(`${webUrl()}${path}.json`);
125
+ if (url.origin !== origin)
126
+ throw new Error(`page URL must be on ${origin}`);
127
+ const res = await fetch(url.href, {
126
128
  headers: {
127
129
  authorization: `Bearer ${token}`,
128
130
  "user-agent": "tracehub-cli",
@@ -130,9 +132,20 @@ export async function traceUploaded(target, traceId, token) {
130
132
  redirect: "manual",
131
133
  signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
132
134
  });
133
- if (res.status === 404)
134
- return false;
135
- if (res.ok)
135
+ if (!res.ok)
136
+ throw await errorOf(res);
137
+ return (await res.json());
138
+ }
139
+ // Whether TraceHub has any session of the trace, asked of the site's
140
+ // `.json` page with the user's GitHub token.
141
+ export async function traceUploaded(target, traceId, token) {
142
+ try {
143
+ await readPage(`/t/${target.owner}/${target.repo}/${traceId}`, token);
136
144
  return true;
137
- throw new Error(`TraceHub answered ${res.status}`);
145
+ }
146
+ catch (err) {
147
+ if (err instanceof TracehubError && err.status === 404)
148
+ return false;
149
+ throw err;
150
+ }
138
151
  }
@@ -73,6 +73,7 @@ export async function uploadMarkers(client, target, stateDir, cwd, root, markers
73
73
  }
74
74
  try {
75
75
  const sha = marker.commit ?? commitOfTrace(cwd, marker.traceId);
76
+ const resolved = sha ? { ...marker, commit: sha } : marker;
76
77
  const lines = sessionLines(ref, file);
77
78
  const subagents = subagentsOf(ref, file);
78
79
  const transcripts = [
@@ -108,7 +109,7 @@ export async function uploadMarkers(client, target, stateDir, cwd, root, markers
108
109
  if (!built) {
109
110
  // Ran outside the repository and changed nothing in it: nothing to
110
111
  // share, and nothing to retry.
111
- saveUploaded(stateDir, [marker]);
112
+ saveUploaded(stateDir, [resolved]);
112
113
  result.outside.push(marker);
113
114
  continue;
114
115
  }
@@ -122,7 +123,7 @@ export async function uploadMarkers(client, target, stateDir, cwd, root, markers
122
123
  }
123
124
  const stored = await client.putTrace(target, marker.traceId, sessionName(ref), built.upload);
124
125
  result.uploaded.push(marker);
125
- saveUploaded(stateDir, [marker]);
126
+ saveUploaded(stateDir, [resolved]);
126
127
  if (findings.length || stored.withheld)
127
128
  result.masked.push({
128
129
  session: formatSessionRef(ref),
@@ -138,6 +139,6 @@ export async function uploadMarkers(client, target, stateDir, cwd, root, markers
138
139
  break;
139
140
  }
140
141
  }
141
- result.skipped = pending.filter((marker) => !result.uploaded.includes(marker));
142
+ result.skipped = pending.filter((marker) => !result.uploaded.includes(marker) && !result.outside.includes(marker));
142
143
  return result;
143
144
  }
package/dist/git/git.js CHANGED
@@ -1,12 +1,26 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { traceIds } from "../protocol/trailers.js";
3
3
  const MAX_BUFFER = 256 * 1024 * 1024;
4
+ // A hook inherits the variables that pin git to the repository it runs for
5
+ // (GIT_DIR in a linked worktree); with them, any other directory reads as
6
+ // that repository's checkout.
7
+ let localEnvVars;
4
8
  export function git(args, opts = {}) {
9
+ localEnvVars ??= execFileSync("git", ["rev-parse", "--local-env-vars"], {
10
+ encoding: "utf8",
11
+ stdio: ["ignore", "pipe", "pipe"],
12
+ })
13
+ .trim()
14
+ .split("\n");
15
+ const env = { ...(opts.env ?? process.env) };
16
+ for (const name of localEnvVars)
17
+ delete env[name];
5
18
  const out = execFileSync("git", args, {
6
19
  encoding: "utf8",
7
20
  stdio: ["ignore", "pipe", "pipe"],
8
21
  maxBuffer: MAX_BUFFER,
9
22
  ...opts,
23
+ env,
10
24
  });
11
25
  return out.toString().trimEnd();
12
26
  }
@@ -68,13 +82,22 @@ function pushedRefs(input) {
68
82
  return { local, previous };
69
83
  });
70
84
  }
71
- // The trace ids of the commits a push sends.
72
- export function pushedTraceIds(cwd, remote, input) {
73
- const ids = new Set();
85
+ function tracesOfCommits(cwd, args) {
86
+ const fields = git(["log", "--format=%H%x00%B%x00", ...args], { cwd }).split("\0");
87
+ const commits = new Map();
88
+ for (let i = 0; i + 1 < fields.length; i += 2)
89
+ for (const id of traceIds(fields[i + 1]))
90
+ if (!commits.has(id))
91
+ commits.set(id, fields[i].trim());
92
+ return commits;
93
+ }
94
+ // The trace ids and SHAs of the commits a push sends.
95
+ export function pushedTraces(cwd, remote, input) {
96
+ const commits = new Map();
74
97
  for (const { local, previous } of pushedRefs(input)) {
75
98
  if (/^0+$/.test(local))
76
99
  continue;
77
- const args = ["log", "--format=%B", local];
100
+ const args = [local];
78
101
  if (!/^0+$/.test(previous)) {
79
102
  if (!tryGit(["rev-parse", "--verify", `${previous}^{commit}`], { cwd }))
80
103
  throw new Error("remote base commit is not available locally; traces stay pending until the next push");
@@ -82,10 +105,11 @@ export function pushedTraceIds(cwd, remote, input) {
82
105
  }
83
106
  else if (tryGit(["remote"], { cwd })?.split("\n").includes(remote))
84
107
  args.push("--not", `--remotes=${remote}`);
85
- for (const id of traceIds(git(args, { cwd })))
86
- ids.add(id);
108
+ for (const [id, sha] of tracesOfCommits(cwd, args))
109
+ if (!commits.has(id))
110
+ commits.set(id, sha);
87
111
  }
88
- return ids;
112
+ return commits;
89
113
  }
90
114
  // Which of `wanted` are trace ids of commits in the history of the pushed
91
115
  // refs, including commits pushed before: their traces may still be pending.
@@ -94,15 +118,13 @@ export function tracesInPushedHistory(cwd, input, wanted) {
94
118
  .map((ref) => ref.local)
95
119
  .filter((local) => !/^0+$/.test(local));
96
120
  if (!wanted.length || !locals.length)
97
- return new Set();
98
- const messages = git([
99
- "log",
100
- "--format=%B",
121
+ return new Map();
122
+ const commits = tracesOfCommits(cwd, [
101
123
  "--fixed-strings",
102
124
  ...wanted.map((id) => `--grep=${id}`),
103
125
  ...locals,
104
- ], { cwd });
105
- return new Set(traceIds(messages).filter((id) => wanted.includes(id)));
126
+ ]);
127
+ return new Map([...commits].filter(([id]) => wanted.includes(id)));
106
128
  }
107
129
  // The newest commit whose message names the trace id.
108
130
  export function commitOfTrace(cwd, traceId) {
@@ -116,7 +138,7 @@ export function commitOfTrace(cwd, traceId) {
116
138
  "1",
117
139
  ], { cwd }) || undefined);
118
140
  }
119
- // The lines a commit adds, per path, against its first parent.
141
+ // The lines a commit adds per path, including deleted paths, against its first parent.
120
142
  export function addedLines(cwd, sha) {
121
143
  const diff = tryGit([
122
144
  "-c",
@@ -131,13 +153,16 @@ export function addedLines(cwd, sha) {
131
153
  ], { cwd }) ?? "";
132
154
  const added = new Map();
133
155
  let lines;
156
+ let previous;
134
157
  let inHunk = false;
135
158
  for (const line of diff.split("\n")) {
136
159
  if (line.startsWith("diff --git ")) {
137
160
  inHunk = false;
138
161
  lines = undefined;
162
+ previous = undefined;
139
163
  }
140
- else if (!inHunk && line.startsWith("+++ ")) {
164
+ else if (!inHunk &&
165
+ (line.startsWith("--- ") || line.startsWith("+++ "))) {
141
166
  let path = line.slice(4).replace(/\t$/, "");
142
167
  if (path.startsWith('"')) {
143
168
  try {
@@ -147,11 +172,19 @@ export function addedLines(cwd, sha) {
147
172
  /* left as it is */
148
173
  }
149
174
  }
150
- lines = undefined;
151
- if (path.startsWith("b/")) {
152
- const file = path.slice(2);
153
- lines = added.get(file) ?? [];
154
- added.set(file, lines);
175
+ if (line.startsWith("--- "))
176
+ previous = path.startsWith("a/") ? path.slice(2) : undefined;
177
+ else {
178
+ lines = undefined;
179
+ const file = path.startsWith("b/")
180
+ ? path.slice(2)
181
+ : path === "/dev/null"
182
+ ? previous
183
+ : undefined;
184
+ if (file) {
185
+ lines = added.get(file) ?? [];
186
+ added.set(file, lines);
187
+ }
155
188
  }
156
189
  }
157
190
  else if (line.startsWith("@@"))
@@ -65,8 +65,17 @@ export function pendingMarkers(markers) {
65
65
  return markers.filter((m) => !m.uploaded);
66
66
  }
67
67
  export function markUploaded(markers, done) {
68
- const keys = new Set(done.map(keyOf));
69
- return markers.map((m) => keys.has(keyOf(m)) ? { ...m, uploaded: true } : m);
68
+ const byKey = new Map(done.map((m) => [keyOf(m), m]));
69
+ return markers.map((m) => {
70
+ const uploaded = byKey.get(keyOf(m));
71
+ return uploaded
72
+ ? {
73
+ ...m,
74
+ ...(uploaded.commit ? { commit: uploaded.commit } : {}),
75
+ uploaded: true,
76
+ }
77
+ : m;
78
+ });
70
79
  }
71
80
  export function saveUploaded(stateDir, done) {
72
81
  if (!done.length)
@@ -0,0 +1,56 @@
1
+ // Text an agent's harness wrote into the user's side of a session: a CI
2
+ // event, a task notification, a reminder, a command's output. It is not
3
+ // what the person typed, so it reads as the system's. A message counts
4
+ // only when it starts with one of these wrappers, closed; a tag a person
5
+ // wrote mid-text leaves the message theirs.
6
+ const LABELS = {
7
+ "ci-monitor-event": "CI monitor",
8
+ "task-notification": "Task notification",
9
+ "system-reminder": "System reminder",
10
+ "command-name": "Command",
11
+ "command-message": "Command",
12
+ "command-args": "Command",
13
+ "local-command-stdout": "Command output",
14
+ "local-command-stderr": "Command output",
15
+ "local-command-caveat": "Command output",
16
+ "bash-input": "Shell",
17
+ "bash-stdout": "Shell output",
18
+ "bash-stderr": "Shell output",
19
+ "user-prompt-submit-hook": "Hook",
20
+ };
21
+ const WRAPPER = /^\s*<([a-z]+(?:-[a-z]+)+)(?:\s[^>]*)?>/;
22
+ const INTERRUPT = /^\s*\[Request interrupted by user[^\]]*\]\s*$/;
23
+ const HOOK = /^\s*(Stop|SubagentStop|PreToolUse|PostToolUse|UserPromptSubmit|SessionStart|PreCompact|Notification) hook (?:feedback|blocking error)[^:\n]*:/;
24
+ const oneLine = (text) => text
25
+ .replace(/<[^>]+>/g, " ")
26
+ .split("\n")
27
+ .map((l) => l.replace(/\s+/g, " ").trim())
28
+ .find(Boolean) ?? "";
29
+ const inner = (text, tag) => new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)</${tag}>`).exec(text)?.[1];
30
+ function summaryOf(tag, text) {
31
+ if (tag === "task-notification")
32
+ return oneLine(inner(text, "summary") ?? inner(text, "status") ?? "");
33
+ if (tag.startsWith("command-")) {
34
+ const name = inner(text, "command-name")?.trim();
35
+ const args = inner(text, "command-args")?.trim();
36
+ if (name)
37
+ return [name, args].filter(Boolean).join(" ");
38
+ }
39
+ return oneLine(inner(text, tag) ?? text);
40
+ }
41
+ // The event a user message is, when the harness wrote it.
42
+ export function harnessEvent(text) {
43
+ if (INTERRUPT.test(text))
44
+ return { label: "Interrupted", summary: "Request interrupted by user" };
45
+ const hook = HOOK.exec(text);
46
+ if (hook)
47
+ return {
48
+ label: "Hook",
49
+ summary: oneLine(text.slice(hook[0].length)) || `${hook[1]} hook`,
50
+ };
51
+ const tag = WRAPPER.exec(text)?.[1];
52
+ const label = tag && LABELS[tag];
53
+ if (!tag || !label || !text.includes(`</${tag}>`))
54
+ return undefined;
55
+ return { label, summary: summaryOf(tag, text) };
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,5 @@
1
1
  import { parseContext } from "./context.js";
2
+ import { harnessEvent } from "./harness.js";
2
3
  import { AGENTS } from "./sessionRef.js";
3
4
  // FNV-1a 64 of the trimmed line, as 16 hex characters; blank lines have none.
4
5
  export function lineHash(line) {
@@ -25,10 +26,26 @@ function parseMessage(raw) {
25
26
  if (!isObject(raw))
26
27
  return undefined;
27
28
  const { role, at, text } = raw;
28
- if ((role !== "user" && role !== "agent") || !isTime(at) || !isString(text))
29
+ if ((role !== "user" && role !== "agent" && role !== "system") ||
30
+ !isTime(at) ||
31
+ !isString(text))
29
32
  return undefined;
30
33
  return { role, at, text: text.slice(0, MAX_TEXT) };
31
34
  }
35
+ // The turns with each user message the harness wrote as the system's, for
36
+ // traces uploaded before the CLI told them apart.
37
+ export function withSystemMessages(turns) {
38
+ return turns.map((turn) => ({
39
+ ...turn,
40
+ messages: turn.messages.map((m) => m.role === "user" && harnessEvent(m.text)
41
+ ? { ...m, role: "system" }
42
+ : m),
43
+ runs: turn.runs.map((run) => ({
44
+ ...run,
45
+ turns: withSystemMessages(run.turns),
46
+ })),
47
+ }));
48
+ }
32
49
  function parseChange(raw) {
33
50
  if (!isObject(raw))
34
51
  return undefined;