tracehub 0.0.0-stage → 0.5.0

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 (48) hide show
  1. package/README.md +424 -2
  2. package/dist/cli/args.js +28 -0
  3. package/dist/cli/clipboard.js +46 -0
  4. package/dist/cli/commands/context.js +115 -0
  5. package/dist/cli/commands/hook.js +209 -0
  6. package/dist/cli/commands/providers.js +55 -0
  7. package/dist/cli/commands/setup.js +448 -0
  8. package/dist/cli/commands/uninstall.js +156 -0
  9. package/dist/cli/io.js +26 -0
  10. package/dist/cli/remote.js +81 -0
  11. package/dist/cli/repo.js +65 -0
  12. package/dist/cli/signin.js +90 -0
  13. package/dist/cli/ui.js +130 -0
  14. package/dist/cli/usage.js +151 -0
  15. package/dist/cli.js +38 -0
  16. package/dist/client/auth.js +70 -0
  17. package/dist/client/consent.js +94 -0
  18. package/dist/client/errors.js +4 -0
  19. package/dist/client/github.js +129 -0
  20. package/dist/client/registry.js +112 -0
  21. package/dist/client/tracehub.js +98 -0
  22. package/dist/client/upload.js +106 -0
  23. package/dist/client/uploadPolicy.js +60 -0
  24. package/dist/git/git.js +166 -0
  25. package/dist/git/global-hooks.js +221 -0
  26. package/dist/git/hooks.js +67 -0
  27. package/dist/git/markers.js +93 -0
  28. package/dist/git/trailers.js +36 -0
  29. package/dist/local/commitBoundary.js +38 -0
  30. package/dist/local/discover.js +283 -0
  31. package/dist/local/files.js +55 -0
  32. package/dist/local/lines.js +16 -0
  33. package/dist/local/opencode.js +124 -0
  34. package/dist/local/paths.js +51 -0
  35. package/dist/local/providers.js +171 -0
  36. package/dist/local/subagents.js +63 -0
  37. package/dist/protocol/context.js +202 -0
  38. package/dist/protocol/sessionRef.js +24 -0
  39. package/dist/protocol/trace.js +111 -0
  40. package/dist/redact/deny.js +25 -0
  41. package/dist/redact/redact.js +189 -0
  42. package/dist/trace/build.js +256 -0
  43. package/dist/transcript/parseClaude.js +105 -0
  44. package/dist/transcript/parseCodex.js +164 -0
  45. package/dist/transcript/parseOpencode.js +84 -0
  46. package/dist/transcript/title.js +27 -0
  47. package/dist/transcript/transcript.js +120 -0
  48. package/package.json +46 -4
@@ -0,0 +1,171 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import { chmodSync, existsSync, mkdirSync, readFileSync } from "node:fs";
4
+ import { writeFileSync } from "node:fs";
5
+ import { dirname } from "node:path";
6
+ import { conversationOf, parseContextEntries, parseContextRef, PROVIDER_NAME, } from "../protocol/context.js";
7
+ import { providersFile } from "./paths.js";
8
+ export const DEFAULT_TIMEOUT_MS = 10_000;
9
+ export const MAX_TIMEOUT_MS = 120_000;
10
+ const MAX_OUTPUT = 8 * 1024 * 1024;
11
+ function isProvider(raw) {
12
+ if (typeof raw !== "object" || raw === null)
13
+ return false;
14
+ const { command, timeoutMs } = raw;
15
+ return (Array.isArray(command) &&
16
+ command.length > 0 &&
17
+ command.every((a) => typeof a === "string") &&
18
+ command.some((a) => a.includes("{id}")) &&
19
+ (timeoutMs === undefined ||
20
+ (Number.isInteger(timeoutMs) &&
21
+ timeoutMs > 0 &&
22
+ timeoutMs <= MAX_TIMEOUT_MS)));
23
+ }
24
+ // The registered providers; an entry that is not one counts as none.
25
+ export function readProviders(file = providersFile()) {
26
+ if (!existsSync(file))
27
+ return {};
28
+ let raw;
29
+ try {
30
+ raw = JSON.parse(readFileSync(file, "utf8"));
31
+ }
32
+ catch {
33
+ return {};
34
+ }
35
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw))
36
+ return {};
37
+ const providers = {};
38
+ for (const [name, value] of Object.entries(raw))
39
+ if (PROVIDER_NAME.test(name) && isProvider(value))
40
+ providers[name] = value;
41
+ return providers;
42
+ }
43
+ export function writeProviders(providers, file = providersFile()) {
44
+ mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
45
+ writeFileSync(file, `${JSON.stringify(providers, null, 2)}\n`, {
46
+ mode: 0o600,
47
+ });
48
+ chmodSync(file, 0o600);
49
+ }
50
+ // The provider's argv for an id: every "{id}" replaced, no shell.
51
+ export function providerArgv(provider, id) {
52
+ return provider.command.map((arg) => arg.split("{id}").join(id));
53
+ }
54
+ // An entry without an id is named by its content, so attaching it twice
55
+ // replaces it rather than adding it again.
56
+ export function contentId(entry) {
57
+ const key = JSON.stringify([
58
+ entry.author,
59
+ entry.at,
60
+ entry.text,
61
+ entry.source.url ?? null,
62
+ ]);
63
+ return `h:${createHash("sha256").update(key).digest("hex").slice(0, 16)}`;
64
+ }
65
+ // Entries read from a file: one conversation per source.
66
+ export function manualConversations(text) {
67
+ let raw;
68
+ try {
69
+ raw = JSON.parse(text);
70
+ }
71
+ catch {
72
+ throw new Error("the messages are not JSON; expected a list of entries");
73
+ }
74
+ const entries = parseContextEntries(raw, contentId);
75
+ if (!entries)
76
+ throw new Error("expected a list of at most 1000 entries {id?, author, text, at, source: {kind, url?, label?}} with distinct ids");
77
+ const bySource = new Map();
78
+ for (const entry of entries) {
79
+ const key = JSON.stringify([
80
+ entry.source.kind,
81
+ entry.source.url ?? null,
82
+ entry.source.label ?? null,
83
+ ]);
84
+ bySource.set(key, [...(bySource.get(key) ?? []), entry]);
85
+ }
86
+ return [...bySource].map(([key, list]) => conversationOf(`manual:${createHash("sha256").update(key).digest("hex").slice(0, 16)}`, list));
87
+ }
88
+ export class ProviderError extends Error {
89
+ }
90
+ function run(argv, timeoutMs) {
91
+ return new Promise((resolve, reject) => {
92
+ const child = spawn(argv[0], argv.slice(1), {
93
+ stdio: ["ignore", "pipe", "pipe"],
94
+ shell: false,
95
+ });
96
+ const out = [];
97
+ const err = [];
98
+ let size = 0;
99
+ let failed;
100
+ const timer = setTimeout(() => {
101
+ failed = `timed out after ${timeoutMs} ms`;
102
+ child.kill("SIGKILL");
103
+ }, timeoutMs);
104
+ child.stdout.on("data", (chunk) => {
105
+ size += chunk.length;
106
+ if (size > MAX_OUTPUT && !failed) {
107
+ failed = "printed more than 8 MB";
108
+ child.kill("SIGKILL");
109
+ }
110
+ else
111
+ out.push(chunk);
112
+ });
113
+ child.stderr.on("data", (chunk) => {
114
+ if (err.length < 64)
115
+ err.push(chunk);
116
+ });
117
+ child.on("error", (e) => {
118
+ clearTimeout(timer);
119
+ reject(new ProviderError(`could not run ${argv[0]}: ${e.message}`));
120
+ });
121
+ child.on("close", (code) => {
122
+ clearTimeout(timer);
123
+ if (failed)
124
+ return reject(new ProviderError(failed));
125
+ if (code !== 0) {
126
+ const line = Buffer.concat(err).toString("utf8").trim().split("\n")[0];
127
+ return reject(new ProviderError(`exited with ${code}${line ? `: ${line}` : ""}`));
128
+ }
129
+ resolve(Buffer.concat(out).toString("utf8"));
130
+ });
131
+ });
132
+ }
133
+ // The conversation a ref names, from its provider. `allowed` is the
134
+ // permission rule of the repository it is for: without one nothing runs.
135
+ export async function fetchContext(ref, allowed, providers = readProviders()) {
136
+ if (!allowed)
137
+ throw new ProviderError("no permission rule allows this repository");
138
+ const parsed = parseContextRef(ref);
139
+ if (!parsed)
140
+ throw new ProviderError("not a <provider>:<id> reference");
141
+ const provider = providers[parsed.provider];
142
+ if (!provider)
143
+ throw new ProviderError(`no provider "${parsed.provider}"; add one with "tracehub providers add"`);
144
+ const output = await run(providerArgv(provider, parsed.id), provider.timeoutMs ?? DEFAULT_TIMEOUT_MS);
145
+ let raw;
146
+ try {
147
+ raw = JSON.parse(output);
148
+ }
149
+ catch {
150
+ throw new ProviderError("printed no list of messages");
151
+ }
152
+ const entries = parseContextEntries(raw, contentId);
153
+ if (!entries)
154
+ throw new ProviderError("printed no list of messages");
155
+ return conversationOf(ref, entries);
156
+ }
157
+ // Fetches each ref once; a failure is reported and leaves the ref out.
158
+ export async function fetchAll(refs, allowed, cache, report, providers = readProviders()) {
159
+ const found = [];
160
+ for (const ref of refs) {
161
+ if (!cache.has(ref))
162
+ cache.set(ref, fetchContext(ref, allowed, providers));
163
+ try {
164
+ found.push(await cache.get(ref));
165
+ }
166
+ catch (err) {
167
+ report(ref, err instanceof Error ? err.message : String(err));
168
+ }
169
+ }
170
+ return found;
171
+ }
@@ -0,0 +1,63 @@
1
+ import { basename, dirname, join } from "node:path";
2
+ import { codexSessionMeta } from "../transcript/parseCodex.js";
3
+ import { walkCodexFiles } from "./discover.js";
4
+ import { firstLineOf, listDir, readJsonObject } from "./files.js";
5
+ import { listOpencodeSubagents } from "./opencode.js";
6
+ const DEFAULT_KIND = "subagent";
7
+ // Claude Code keeps subagent transcripts beside the parent session file, in
8
+ // <project>/<session id>/subagents/agent-<id>.jsonl.
9
+ export function claudeSubagentDir(sessionFile) {
10
+ const session = basename(sessionFile, ".jsonl");
11
+ return join(dirname(sessionFile), session, "subagents");
12
+ }
13
+ function stringField(object, key) {
14
+ const value = object[key];
15
+ return typeof value === "string" && value ? value : undefined;
16
+ }
17
+ // The description its parent gave a subagent, from agent-<id>.meta.json.
18
+ export function claudeSubagentLabel(file) {
19
+ const meta = readJsonObject(file.replace(/\.jsonl$/, ".meta.json"));
20
+ return stringField(meta, "description") ?? stringField(meta, "agentType");
21
+ }
22
+ // An optional agent-<id>.meta.json says how the parent launched the subagent.
23
+ export function listClaudeSubagents(sessionFile) {
24
+ const dir = claudeSubagentDir(sessionFile);
25
+ const subagents = [];
26
+ for (const name of listDir(dir).sort()) {
27
+ const id = /^agent-([A-Za-z0-9_-]+)\.jsonl$/.exec(name)?.[1];
28
+ if (!id)
29
+ continue;
30
+ const meta = readJsonObject(join(dir, `agent-${id}.meta.json`));
31
+ const toolUseId = stringField(meta, "toolUseId");
32
+ subagents.push({
33
+ id,
34
+ kind: stringField(meta, "agentType") ?? DEFAULT_KIND,
35
+ file: join(dir, name),
36
+ ...(toolUseId ? { toolUseId } : {}),
37
+ });
38
+ }
39
+ return subagents;
40
+ }
41
+ // A Codex sub-thread is a session of its own whose first line names the
42
+ // thread that spawned it.
43
+ export function listCodexSubthreads(parentId, files) {
44
+ const subthreads = [];
45
+ for (const { file, firstLine } of files) {
46
+ const meta = codexSessionMeta(firstLine);
47
+ if (meta?.parentId !== parentId)
48
+ continue;
49
+ subthreads.push({ id: meta.id, kind: meta.nickname ?? DEFAULT_KIND, file });
50
+ }
51
+ return subthreads.sort((a, b) => a.file.localeCompare(b.file));
52
+ }
53
+ export function listSubagents(ref, sessionFile) {
54
+ if (ref.agent === "claude")
55
+ return listClaudeSubagents(sessionFile);
56
+ if (ref.agent === "opencode")
57
+ return listOpencodeSubagents(ref.id);
58
+ const files = [...walkCodexFiles(0)].map(({ file }) => ({
59
+ file,
60
+ firstLine: firstLineOf(file),
61
+ }));
62
+ return listCodexSubthreads(ref.id, files);
63
+ }
@@ -0,0 +1,202 @@
1
+ // Context conversations: what asked for the work behind a commit (a nomOS
2
+ // thread, a Slack thread, pasted prompts), fetched from a provider by
3
+ // reference. Shared by the CLI and the Tracehub API, so no node built-ins.
4
+ const MAX_TEXT = 32_000;
5
+ export const MAX_MESSAGES = 1000;
6
+ export const MAX_CONVERSATIONS = 20;
7
+ export const PROVIDER_NAME = /^[a-z][a-z0-9-]{0,39}$/;
8
+ // No leading "-", so an id can never pass for a flag of the provider.
9
+ export const CONTEXT_ID = /^[A-Za-z0-9._:/#@][A-Za-z0-9._:/#@-]{0,199}$/;
10
+ const MESSAGE_ID = /^[A-Za-z0-9._:/#@-]{1,200}$/;
11
+ export function parseContextRef(raw) {
12
+ const at = raw.indexOf(":");
13
+ if (at < 0)
14
+ return undefined;
15
+ const provider = raw.slice(0, at);
16
+ const id = raw.slice(at + 1);
17
+ return PROVIDER_NAME.test(provider) && CONTEXT_ID.test(id)
18
+ ? { provider, id }
19
+ : undefined;
20
+ }
21
+ export const formatContextRef = (ref) => `${ref.provider}:${ref.id}`;
22
+ const isString = (value) => typeof value === "string";
23
+ const isTime = (value) => isString(value) && !Number.isNaN(Date.parse(value));
24
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
25
+ export function parseUrl(value) {
26
+ if (!isString(value))
27
+ return undefined;
28
+ try {
29
+ const url = new URL(value);
30
+ return url.protocol === "https:" || url.protocol === "http:"
31
+ ? url.href
32
+ : undefined;
33
+ }
34
+ catch {
35
+ return undefined;
36
+ }
37
+ }
38
+ function parseAuthor(value) {
39
+ return isString(value) && value.trim() && value.length <= 100
40
+ ? value.trim()
41
+ : undefined;
42
+ }
43
+ function parseMessage(raw) {
44
+ if (!isObject(raw))
45
+ return undefined;
46
+ const { id, text, at, url } = raw;
47
+ const author = parseAuthor(raw["author"]);
48
+ if (!isString(id) || !MESSAGE_ID.test(id) || !author)
49
+ return undefined;
50
+ if (!isString(text) || !isTime(at))
51
+ return undefined;
52
+ if (url !== undefined && !parseUrl(url))
53
+ return undefined;
54
+ const href = parseUrl(url);
55
+ return {
56
+ id,
57
+ author,
58
+ text: text.slice(0, MAX_TEXT),
59
+ at: new Date(at).toISOString(),
60
+ ...(href ? { url: href } : {}),
61
+ };
62
+ }
63
+ export function parseConversation(raw) {
64
+ if (!isObject(raw) || !isString(raw["ref"]))
65
+ return undefined;
66
+ const ref = raw["ref"];
67
+ if (!parseContextRef(ref))
68
+ return undefined;
69
+ const { title, url } = raw;
70
+ if (title !== undefined && !isString(title))
71
+ return undefined;
72
+ if (url !== undefined && !parseUrl(url))
73
+ return undefined;
74
+ if (!Array.isArray(raw["messages"]))
75
+ return undefined;
76
+ if (raw["messages"].length > MAX_MESSAGES)
77
+ return undefined;
78
+ const messages = raw["messages"].map(parseMessage);
79
+ if (!messages.every((m) => m !== undefined))
80
+ return undefined;
81
+ const ids = new Set(messages.map((m) => m.id));
82
+ if (ids.size !== messages.length)
83
+ return undefined;
84
+ const href = parseUrl(url);
85
+ return {
86
+ ref,
87
+ ...(title ? { title: title.slice(0, 200) } : {}),
88
+ ...(href ? { url: href } : {}),
89
+ messages: messages,
90
+ };
91
+ }
92
+ // A list of conversations with distinct refs.
93
+ export function parseContext(raw) {
94
+ if (!Array.isArray(raw) || raw.length > MAX_CONVERSATIONS)
95
+ return undefined;
96
+ const parsed = raw.map(parseConversation);
97
+ if (!parsed.every((c) => c !== undefined))
98
+ return undefined;
99
+ const conversations = parsed;
100
+ return new Set(conversations.map((c) => c.ref)).size === conversations.length
101
+ ? conversations
102
+ : undefined;
103
+ }
104
+ // A provider's output, or the file of `context add --from`. Entries
105
+ // without an id need one from `idOf` (the CLI hashes their content).
106
+ export function parseContextEntries(raw, idOf) {
107
+ if (!Array.isArray(raw) || raw.length > MAX_MESSAGES)
108
+ return undefined;
109
+ const entries = [];
110
+ for (const item of raw) {
111
+ if (!isObject(item) || !isObject(item["source"]))
112
+ return undefined;
113
+ const { text, at, source } = item;
114
+ const author = parseAuthor(item["author"]);
115
+ if (!author || !isString(text) || !isTime(at))
116
+ return undefined;
117
+ const kind = source["kind"];
118
+ if (!isString(kind) || !PROVIDER_NAME.test(kind))
119
+ return undefined;
120
+ if (source["url"] !== undefined && !parseUrl(source["url"]))
121
+ return undefined;
122
+ if (source["label"] !== undefined && !isString(source["label"]))
123
+ return undefined;
124
+ const url = parseUrl(source["url"]);
125
+ const label = isString(source["label"])
126
+ ? source["label"].slice(0, 200)
127
+ : "";
128
+ const entry = {
129
+ author,
130
+ text: text.slice(0, MAX_TEXT),
131
+ at: new Date(at).toISOString(),
132
+ source: { kind, ...(url ? { url } : {}), ...(label ? { label } : {}) },
133
+ };
134
+ const id = item["id"] === undefined ? idOf(entry) : item["id"];
135
+ if (!isString(id) || !MESSAGE_ID.test(id))
136
+ return undefined;
137
+ entries.push({ id, ...entry });
138
+ }
139
+ return new Set(entries.map((e) => e.id)).size === entries.length
140
+ ? entries
141
+ : undefined;
142
+ }
143
+ // Entries as one conversation: its title and url are the first entry's.
144
+ export function conversationOf(ref, entries) {
145
+ const first = entries[0]?.source;
146
+ const url = first?.url;
147
+ return {
148
+ ref,
149
+ ...(first?.label ? { title: first.label } : {}),
150
+ ...(url ? { url } : {}),
151
+ messages: entries.map((e) => ({
152
+ id: e.id,
153
+ author: e.author,
154
+ text: e.text,
155
+ at: e.at,
156
+ ...(e.source.url && e.source.url !== url ? { url: e.source.url } : {}),
157
+ })),
158
+ };
159
+ }
160
+ // Conversations by ref, each message once by id (a later one replaces),
161
+ // in time order; the title and url of the latest.
162
+ export function mergeConversations(lists) {
163
+ const byRef = new Map();
164
+ for (const list of lists)
165
+ for (const c of list) {
166
+ const known = byRef.get(c.ref);
167
+ const messages = new Map((known?.messages ?? []).map((m) => [m.id, m]));
168
+ for (const m of c.messages)
169
+ messages.set(m.id, m);
170
+ const title = c.title ?? known?.title;
171
+ const url = c.url ?? known?.url;
172
+ byRef.set(c.ref, {
173
+ ref: c.ref,
174
+ ...(title ? { title } : {}),
175
+ ...(url ? { url } : {}),
176
+ messages: [...messages.values()].sort((a, b) => a.at.localeCompare(b.at)),
177
+ });
178
+ }
179
+ return [...byRef.values()];
180
+ }
181
+ const ID_CHARS = "[A-Za-z0-9._:/#@-]+";
182
+ const TAG = new RegExp(`tracehub-context:[ \\t]*([a-z][a-z0-9-]*:${ID_CHARS}(?:[ \\t]*,[ \\t]*[a-z][a-z0-9-]*:${ID_CHARS})*)`, "gi");
183
+ const THREAD = new RegExp(`\\b([a-z][a-z0-9-]{0,39})-thread:[ \\t]*(${ID_CHARS})`, "g");
184
+ // An id as written in prose: a trailing "." or ":" ends the sentence.
185
+ const trimmed = (id) => id.replace(/[.:]+$/, "");
186
+ // The context refs a message names: `tracehub-context: <ref>[, <ref>…]`,
187
+ // and `<provider>-thread: <id>` for the providers given.
188
+ export function contextRefsIn(text, providers) {
189
+ const refs = [];
190
+ const add = (raw) => {
191
+ const ref = parseContextRef(trimmed(raw));
192
+ if (ref)
193
+ refs.push(formatContextRef(ref));
194
+ };
195
+ for (const match of text.matchAll(TAG))
196
+ for (const word of match[1].split(/[ \t]*,[ \t]*/))
197
+ add(word);
198
+ for (const match of text.matchAll(THREAD))
199
+ if (providers.includes(match[1]))
200
+ add(`${match[1]}:${match[2]}`);
201
+ return [...new Set(refs)];
202
+ }
@@ -0,0 +1,24 @@
1
+ export const AGENTS = ["claude", "codex", "opencode"];
2
+ const AGENT = AGENTS.join("|");
3
+ const REF = new RegExp(`^(${AGENT}):([A-Za-z0-9._-]+)$`);
4
+ const NAME = new RegExp(`^(${AGENT})-([A-Za-z0-9._-]+)\\.jsonl$`);
5
+ // "claude:<id>", as the CLI prints and accepts a session.
6
+ export function parseSessionRef(input) {
7
+ const m = REF.exec(input.trim());
8
+ return m ? { agent: m[1], id: m[2] } : undefined;
9
+ }
10
+ export function formatSessionRef(ref) {
11
+ return `${ref.agent}:${ref.id}`;
12
+ }
13
+ // "claude-<id>.jsonl", as tracehub names a session.
14
+ export function parseSessionName(name) {
15
+ const m = NAME.exec(name);
16
+ return m ? { agent: m[1], id: m[2] } : undefined;
17
+ }
18
+ export function sessionName(ref) {
19
+ return `${ref.agent}-${ref.id}.jsonl`;
20
+ }
21
+ export function uniqueRefs(refs) {
22
+ const byKey = new Map(refs.map((ref) => [formatSessionRef(ref), ref]));
23
+ return [...byKey.values()];
24
+ }
@@ -0,0 +1,111 @@
1
+ import { parseContext } from "./context.js";
2
+ import { AGENTS } from "./sessionRef.js";
3
+ // FNV-1a 64 of the trimmed line, as 16 hex characters; blank lines have none.
4
+ export function lineHash(line) {
5
+ const text = line.trim();
6
+ if (!text)
7
+ return undefined;
8
+ let hash = 0xcbf29ce484222325n;
9
+ const bytes = new TextEncoder().encode(text);
10
+ for (const byte of bytes) {
11
+ hash ^= BigInt(byte);
12
+ hash = (hash * 0x100000001b3n) & 0xffffffffffffffffn;
13
+ }
14
+ return hash.toString(16).padStart(16, "0");
15
+ }
16
+ const TURN_PATH = /^\d+(?:\.\d+\.\d+)*$/;
17
+ const MAX_TEXT = 32_000;
18
+ const MAX_PATH = 1000;
19
+ const MAX_DEPTH = 4;
20
+ const isString = (value) => typeof value === "string";
21
+ const isTime = (value) => isString(value) && !Number.isNaN(Date.parse(value));
22
+ const isCount = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0;
23
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
24
+ function parseMessage(raw) {
25
+ if (!isObject(raw))
26
+ return undefined;
27
+ const { role, at, text } = raw;
28
+ if ((role !== "user" && role !== "agent") || !isTime(at) || !isString(text))
29
+ return undefined;
30
+ return { role, at, text: text.slice(0, MAX_TEXT) };
31
+ }
32
+ function parseChange(raw) {
33
+ if (!isObject(raw))
34
+ return undefined;
35
+ const { path, added, removed } = raw;
36
+ if (!isString(path) || !path || path.length > MAX_PATH)
37
+ return undefined;
38
+ if (!isCount(added) || !isCount(removed))
39
+ return undefined;
40
+ return { path, added, removed };
41
+ }
42
+ function all(raw, parse) {
43
+ if (!Array.isArray(raw))
44
+ return undefined;
45
+ const parsed = raw.map(parse);
46
+ return parsed.every((item) => item !== undefined)
47
+ ? parsed
48
+ : undefined;
49
+ }
50
+ function parseTurn(raw, depth) {
51
+ if (!isObject(raw) || !isTime(raw["at"]))
52
+ return undefined;
53
+ const messages = all(raw["messages"], parseMessage);
54
+ const changes = all(raw["changes"], parseChange);
55
+ const runs = depth < MAX_DEPTH ? all(raw["runs"], (r) => parseRun(r, depth + 1)) : [];
56
+ if (!messages || !changes || !runs)
57
+ return undefined;
58
+ return { at: raw["at"], messages, changes, runs };
59
+ }
60
+ function parseRun(raw, depth) {
61
+ if (!isObject(raw) || !isString(raw["label"]))
62
+ return undefined;
63
+ const turns = all(raw["turns"], (t) => parseTurn(t, depth));
64
+ return turns ? { label: raw["label"].slice(0, 200), turns } : undefined;
65
+ }
66
+ function parseSourcemap(raw) {
67
+ if (!isObject(raw))
68
+ return undefined;
69
+ const map = {};
70
+ for (const [path, lines] of Object.entries(raw)) {
71
+ if (!path || path.length > MAX_PATH || !isObject(lines))
72
+ return undefined;
73
+ const entries = {};
74
+ for (const [hash, turn] of Object.entries(lines)) {
75
+ if (!/^[0-9a-f]{16}$/.test(hash))
76
+ return undefined;
77
+ if (!isString(turn) || !TURN_PATH.test(turn))
78
+ return undefined;
79
+ entries[hash] = turn;
80
+ }
81
+ map[path] = entries;
82
+ }
83
+ return map;
84
+ }
85
+ export function parseTraceUpload(raw) {
86
+ if (!isObject(raw))
87
+ return undefined;
88
+ const { agent, title, model, committedAt } = raw;
89
+ if (!AGENTS.includes(agent) || !isTime(committedAt))
90
+ return undefined;
91
+ if (title !== undefined && !isString(title))
92
+ return undefined;
93
+ if (model !== undefined && !isString(model))
94
+ return undefined;
95
+ const turns = all(raw["turns"], (t) => parseTurn(t, 0));
96
+ const sourcemap = parseSourcemap(raw["sourcemap"]);
97
+ if (!turns || !sourcemap)
98
+ return undefined;
99
+ const context = raw["context"] === undefined ? [] : parseContext(raw["context"]);
100
+ if (!context)
101
+ return undefined;
102
+ return {
103
+ agent: agent,
104
+ ...(title ? { title: title.slice(0, 200) } : {}),
105
+ ...(model ? { model: model.slice(0, 100) } : {}),
106
+ committedAt,
107
+ turns,
108
+ sourcemap,
109
+ ...(context.length ? { context } : {}),
110
+ };
111
+ }
@@ -0,0 +1,25 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { configDir } from "../local/paths.js";
4
+ // The deny list is edited by hand: one literal per line. A shorter line would
5
+ // mask ordinary words, so it is ignored.
6
+ export const DENY_MIN_LENGTH = 6;
7
+ export function denyFile() {
8
+ return join(configDir(), "deny.txt");
9
+ }
10
+ export function parseDenyList(text) {
11
+ return [
12
+ ...new Set(text
13
+ .split("\n")
14
+ .map((l) => l.replace(/\r$/, ""))
15
+ .filter((l) => l.length >= DENY_MIN_LENGTH)),
16
+ ];
17
+ }
18
+ export function readDenyList(file = denyFile()) {
19
+ try {
20
+ return existsSync(file) ? parseDenyList(readFileSync(file, "utf8")) : [];
21
+ }
22
+ catch {
23
+ return [];
24
+ }
25
+ }