@agentsky/sky 3.42.0 → 3.43.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.
@@ -1,15 +1,41 @@
1
1
  import {
2
- ApiError,
2
+ importPathAllowed,
3
3
  uploadLargeFile
4
- } from "./chunk-ALJRHTMX.js";
4
+ } from "./chunk-AB7SUDX4.js";
5
+ import {
6
+ installClaudeHooks
7
+ } from "./chunk-4MVM65ET.js";
8
+ import {
9
+ IncompleteSourceCaptureError,
10
+ SOURCE_CAPTURE_VERSION,
11
+ beginPendingSource,
12
+ canonicalSourceApiBase,
13
+ captureTraceSource,
14
+ completePendingCapture,
15
+ excludedBy,
16
+ listPendingSources,
17
+ loadExclusions,
18
+ recoverPendingSource,
19
+ removePendingSource,
20
+ savePendingSource,
21
+ stillExcluded,
22
+ writePrivateJson
23
+ } from "./chunk-UJ7IXSJX.js";
5
24
  import {
6
25
  SessionSourceCollector,
7
- USER_AGENT,
8
- UnsafeImportError,
9
26
  publicRemote,
10
- redactLine,
11
27
  redactTraceText
12
- } from "./chunk-YY2ZSZDH.js";
28
+ } from "./chunk-BIZJ4VZL.js";
29
+ import {
30
+ ApiError
31
+ } from "./chunk-44XF4EGE.js";
32
+ import {
33
+ loadLocalSkillState,
34
+ revisionAt
35
+ } from "./chunk-J3KIEZXM.js";
36
+ import {
37
+ USER_AGENT
38
+ } from "./chunk-4A4PDUT5.js";
13
39
  import {
14
40
  apiBase,
15
41
  apiToken
@@ -17,159 +43,19 @@ import {
17
43
  import "./chunk-MLKGABMK.js";
18
44
 
19
45
  // src/trace-library.ts
20
- import { createHash as createHash3 } from "crypto";
21
- import { createReadStream as createReadStream3, existsSync as existsSync4, mkdirSync as mkdirSync3, mkdtempSync, readdirSync as readdirSync2, readFileSync as readFileSync2, rmSync as rmSync3, statSync as statSync3, writeFileSync as writeFileSync2 } from "fs";
46
+ import { createHash as createHash2 } from "crypto";
47
+ import { createReadStream, existsSync as existsSync2, mkdirSync, mkdtempSync, readdirSync, readFileSync as readFileSync2, rmSync, statSync as statSync2, writeFileSync } from "fs";
22
48
  import { createInterface } from "readline";
23
- import { homedir as homedir2, hostname } from "os";
24
- import { basename as basename3, dirname as dirname4, join as join3, sep } from "path";
49
+ import { homedir as homedir3, hostname } from "os";
50
+ import { basename as basename2, dirname as dirname3, join as join2, sep as sep2 } from "path";
25
51
  import { fileURLToPath, pathToFileURL } from "url";
26
- import { createGunzip as createGunzip2 } from "zlib";
27
-
28
- // src/trace-source.ts
29
- import { createHash } from "crypto";
30
- import { createReadStream, createWriteStream, closeSync, existsSync, fsyncSync, mkdirSync, openSync, realpathSync, renameSync, rmSync, statSync } from "fs";
31
- import { once } from "events";
32
- import { basename, dirname, join, resolve } from "path";
33
52
  import { createGunzip } from "zlib";
34
- var SOURCE_CAPTURE_VERSION = 2;
35
- function canonicalSourceApiBase(value) {
36
- const url = new URL(value);
37
- url.username = "";
38
- url.password = "";
39
- url.search = "";
40
- url.hash = "";
41
- return url.toString().replace(/\/$/, "");
42
- }
43
- async function captureTraceSource(session, directory, context) {
44
- const originals = session.trace?.source_files;
45
- const files = originals?.length ? originals : session.files;
46
- for (const file of files) {
47
- const allowed = session.files.some((collected) => resolve(file) === resolve(collected) || resolve(file) === resolve(collected.replace(/\.json$/, ".source.jsonl")) && dirname(realpathSync(file)) === dirname(realpathSync(collected)));
48
- if (!allowed) throw new Error("Source reference is outside this collected session.");
49
- }
50
- const gaps = [...session.trace?.source_gaps ?? [], ...session.trace && !originals?.length ? ["Only the collector-normalized export is available; original source records were not retained."] : []];
51
- mkdirSync(directory, { recursive: true, mode: 448 });
52
- const key = createHash("sha256").update(`${session.harness}\0${session.sessionId}`).digest("hex");
53
- const temporary = join(directory, `${key}.pending`);
54
- const output = createWriteStream(temporary, { mode: 384 });
55
- let streamError;
56
- output.on("error", (error) => {
57
- streamError = error;
58
- });
59
- const hash = createHash("sha256");
60
- let bytes = 0, records = 0;
61
- const write = async (value) => {
62
- const line = `${JSON.stringify(value)}
63
- `;
64
- hash.update(line);
65
- bytes += Buffer.byteLength(line);
66
- if (!output.write(line)) await once(output, "drain");
67
- if (streamError) throw streamError;
68
- };
69
- const redact = (text) => {
70
- const safe = redactLine(text, { requireStructured: true, preserveTraceSessionIds: true, expectedTraceSessionId: session.sessionId }).line;
71
- JSON.parse(safe);
72
- return safe;
73
- };
74
- let omitted = 0;
75
- const writeRecord = async (sourceFile, text) => {
76
- let safe;
77
- try {
78
- safe = redact(text);
79
- } catch (error) {
80
- if (!(error instanceof UnsafeImportError)) throw error;
81
- omitted++;
82
- await write({ source_file: sourceFile, omitted_record: { reason: "redaction_not_provable" } });
83
- return;
84
- }
85
- await write({ source_file: sourceFile, record_json: safe });
86
- records++;
87
- };
88
- try {
89
- await write({
90
- format: `agentsky.trace-source/v${SOURCE_CAPTURE_VERSION}`,
91
- record_encoding: "json-text",
92
- harness: session.harness,
93
- session_id: session.sessionId,
94
- ...context ? { workspace: context.workspace, api_base: canonicalSourceApiBase(context.apiBase), source_signature: session.signature } : {},
95
- redacted_before_upload: true,
96
- representation: session.trace && !originals?.length ? "collector-normalized" : "native",
97
- source_files: files.length,
98
- gaps
99
- });
100
- for (const [index, file] of files.entries()) {
101
- const before = statSync(file);
102
- const sourceHash = createHash("sha256");
103
- for await (const chunk of createReadStream(file)) sourceHash.update(chunk);
104
- await write({ source_file: index, name: basename(file), source_sha256: sourceHash.digest("hex"), source_bytes: before.size });
105
- const raw = createReadStream(file);
106
- const input = file.endsWith(".gz") ? raw.pipe(createGunzip()) : raw;
107
- const decoder = new TextDecoder("utf-8", { fatal: true });
108
- async function* textChunks() {
109
- for await (const chunk of input) yield decoder.decode(chunk, { stream: true });
110
- yield decoder.decode();
111
- }
112
- try {
113
- if (/\.json(?:\.gz)?$/.test(file)) {
114
- let text = "";
115
- for await (const chunk of textChunks()) text += chunk;
116
- await writeRecord(index, text);
117
- } else {
118
- let pending = "";
119
- const recordLine = async (line) => {
120
- if (!line.trim()) return;
121
- await writeRecord(index, line);
122
- };
123
- for await (const chunk of textChunks()) {
124
- pending += chunk;
125
- let newline;
126
- while ((newline = pending.indexOf("\n")) !== -1) {
127
- await recordLine(pending.slice(0, newline));
128
- pending = pending.slice(newline + 1);
129
- }
130
- }
131
- await recordLine(pending);
132
- }
133
- } finally {
134
- raw.destroy();
135
- input.destroy();
136
- }
137
- const after = statSync(file);
138
- if (before.size !== after.size || before.mtimeMs !== after.mtimeMs) throw new Error("Transcript changed while capturing; retry on the next sync.");
139
- }
140
- if (omitted) gaps.push(`${omitted} record${omitted === 1 ? "" : "s"} omitted: redaction could not be proven safe.`);
141
- await write({ capture_summary: { records, complete: gaps.length === 0, gaps } });
142
- output.end();
143
- await once(output, "finish");
144
- const descriptor = openSync(temporary, "r");
145
- try {
146
- fsyncSync(descriptor);
147
- } finally {
148
- closeSync(descriptor);
149
- }
150
- const sha256 = hash.digest("hex");
151
- const path = join(directory, `${sha256}.jsonl`);
152
- if (existsSync(path)) rmSync(temporary);
153
- else renameSync(temporary, path);
154
- const parent = openSync(directory, "r");
155
- try {
156
- fsyncSync(parent);
157
- } finally {
158
- closeSync(parent);
159
- }
160
- return { path, sha256, bytes, records, sourceFiles: files.length, complete: gaps.length === 0, gaps };
161
- } catch (error) {
162
- output.destroy();
163
- rmSync(temporary, { force: true });
164
- throw error;
165
- }
166
- }
167
53
 
168
54
  // src/trace-references.ts
169
55
  import { execFileSync } from "child_process";
170
- import { existsSync as existsSync2 } from "fs";
56
+ import { existsSync } from "fs";
171
57
  import { homedir } from "os";
172
- import { dirname as dirname2 } from "path";
58
+ import { dirname } from "path";
173
59
  var PULL_URL = /github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/([1-9][0-9]{0,8})/g;
174
60
  var GMAIL_ID = /(?:message_?id|thread_?id)\\*"?\s*[:=]\s*\\*"?([0-9a-f]{16})\b/gi;
175
61
  var GMAIL_PATH = /(?:\/messages\/|\/threads\/|mail\.google\.com\/[^\s"\\]*[#/])([0-9a-f]{16})\b/g;
@@ -214,8 +100,8 @@ function gitRemoteAt(cwd) {
214
100
  if (remotes.has(cwd)) return remotes.get(cwd);
215
101
  let remote;
216
102
  const home = homedir();
217
- for (let folder = cwd; folder !== home && folder !== dirname2(folder); folder = dirname2(folder)) {
218
- if (!existsSync2(folder)) continue;
103
+ for (let folder = cwd; folder !== home && folder !== dirname(folder); folder = dirname(folder)) {
104
+ if (!existsSync(folder)) continue;
219
105
  try {
220
106
  remote = execFileSync("git", ["-C", folder, "remote", "get-url", "origin"], { encoding: "utf8", timeout: 3e3, stdio: ["ignore", "pipe", "ignore"] }).trim() || void 0;
221
107
  } catch {
@@ -227,165 +113,320 @@ function gitRemoteAt(cwd) {
227
113
  return remote;
228
114
  }
229
115
 
230
- // src/trace-pending.ts
231
- import { createHash as createHash2, randomBytes } from "crypto";
232
- import { createReadStream as createReadStream2, closeSync as closeSync2, existsSync as existsSync3, fsyncSync as fsyncSync2, lstatSync, mkdirSync as mkdirSync2, openSync as openSync2, readFileSync, readdirSync, realpathSync as realpathSync2, renameSync as renameSync2, rmSync as rmSync2, statSync as statSync2, writeFileSync } from "fs";
233
- import { basename as basename2, dirname as dirname3, join as join2 } from "path";
234
- var IncompleteSourceCaptureError = class extends Error {
116
+ // src/session-results.ts
117
+ import { createHash } from "crypto";
118
+ import { readFileSync, statSync } from "fs";
119
+ import { homedir as homedir2 } from "os";
120
+ import { basename, dirname as dirname2, extname, isAbsolute, join, normalize, resolve, sep } from "path";
121
+ var MAX_RESULT_BYTES = 15 * 1024 * 1024;
122
+ var RESULT_TYPES = {
123
+ ".mp4": "video/mp4",
124
+ ".mov": "video/quicktime",
125
+ ".webm": "video/webm",
126
+ ".m4v": "video/x-m4v",
127
+ ".mp3": "audio/mpeg",
128
+ ".wav": "audio/wav",
129
+ ".m4a": "audio/mp4",
130
+ ".aac": "audio/aac",
131
+ ".ogg": "audio/ogg",
132
+ ".flac": "audio/flac",
133
+ ".png": "image/png",
134
+ ".jpg": "image/jpeg",
135
+ ".jpeg": "image/jpeg",
136
+ ".gif": "image/gif",
137
+ ".webp": "image/webp",
138
+ ".svg": "image/svg+xml",
139
+ ".pdf": "application/pdf",
140
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
141
+ ".doc": "application/msword",
142
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
143
+ ".xls": "application/vnd.ms-excel",
144
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
145
+ ".ppt": "application/vnd.ms-powerpoint",
146
+ ".csv": "text/csv",
147
+ ".tsv": "text/tab-separated-values",
148
+ ".html": "text/html",
149
+ ".htm": "text/html",
150
+ ".md": "text/markdown",
151
+ ".markdown": "text/markdown",
152
+ ".txt": "text/plain",
153
+ ".json": "application/json"
235
154
  };
236
- var digest = /^[a-f0-9]{64}$/;
237
- var record = (value) => !!value && typeof value === "object" && !Array.isArray(value);
238
- var strings = (value) => Array.isArray(value) && value.every((v) => typeof v === "string");
239
- var natural = (value) => Number.isSafeInteger(value) && Number(value) >= 0;
240
- var keyOf = (manifest) => createHash2("sha256").update(JSON.stringify([manifest.apiBase, manifest.workspace, manifest.local.harness, manifest.local.sessionId])).digest("hex");
241
- function writePrivateJson(path, value) {
242
- mkdirSync2(dirname3(path), { recursive: true, mode: 448 });
243
- if (existsSync3(path) && lstatSync(path).isSymbolicLink()) throw new Error("Refusing a symlink metadata file.");
244
- const temporary = `${path}.${randomBytes(8).toString("hex")}.pending`;
245
- try {
246
- const fd = openSync2(temporary, "wx", 384);
247
- try {
248
- writeFileSync(fd, `${JSON.stringify(value)}
249
- `);
250
- fsyncSync2(fd);
251
- } finally {
252
- closeSync2(fd);
155
+ var OTHER_TYPES = { ".css": "text/css", ".js": "text/javascript", ".zip": "application/zip", ".heic": "image/heic", ".pages": "application/vnd.apple.pages", ".numbers": "application/vnd.apple.numbers" };
156
+ var PUBLISHING_TOOLS = /* @__PURE__ */ new Set(["Artifact"]);
157
+ var SCRATCH_ROOTS = ["/tmp/", "/private/tmp/", "/var/folders/", "/private/var/folders/"];
158
+ var REBUILT = /* @__PURE__ */ new Set(["node_modules", ".git", ".venv", "venv", "__pycache__", ".next", ".nuxt", ".turbo", ".cache", "dist", "build", "out", "target", "DerivedData", "Library", ".Trash"]);
159
+ var PATH_LIKE = /(?:~\/|\.{1,2}\/|\/)?(?:[^\s'"`()<>|;&*?,:=]+\/)*[^\s'"`()<>|;&*?,:=/]+\.[A-Za-z0-9]{1,8}(?![A-Za-z0-9])/g;
160
+ var ATTACHED = /^Attached files:\n((?:- .+(?:\n|$))+)/m;
161
+ var LOCAL_REFERENCE = /(\s(?:src|href)\s*=\s*)(["'])([^"'<>]+)\2/gi;
162
+ var CODEX_CONTEXT = /^\s*(?:<environment_context>|<user_instructions>|<turn_aborted>|# AGENTS\.md instructions)/;
163
+ var obj = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : null;
164
+ var str = (value) => typeof value === "string" ? value : "";
165
+ function typeOf(name, any) {
166
+ const ext = extname(name).toLowerCase();
167
+ return RESULT_TYPES[ext] ?? (any ? OTHER_TYPES[ext] ?? "application/octet-stream" : null);
168
+ }
169
+ function pictureOf(block) {
170
+ const source = obj(block.source);
171
+ if (block.type === "image" && source?.type === "base64" && typeof source.data === "string") return { mediaType: str(source.media_type) || "image/png", data: source.data };
172
+ const inline = block.type === "input_image" ? /^data:(image\/[a-z0-9.+-]+);base64,(.+)$/i.exec(str(block.image_url)) : null;
173
+ return inline ? { mediaType: inline[1], data: inline[2] } : null;
174
+ }
175
+ function turnsOf(records, cwd = "") {
176
+ const turns = [];
177
+ let context = cwd;
178
+ const start = (at, texts, pictures) => turns.push({ since: Date.parse(str(at)) || 0, texts, pictures, said: [], tools: [] });
179
+ for (const raw of records) {
180
+ const record = obj(raw);
181
+ if (!record) continue;
182
+ const payload = obj(record.payload);
183
+ if (record.type === "turn_context" || record.type === "session_meta") context = str(payload?.cwd) || context;
184
+ const here = str(record.cwd) || context;
185
+ const content = obj(record.message)?.content;
186
+ if (record.type === "user") {
187
+ const blocks = typeof content === "string" ? [{ type: "text", text: content }] : Array.isArray(content) ? content.map(obj).filter(Boolean) : [];
188
+ if (blocks.some((block) => block.type === "tool_result") || record.isMeta === true) continue;
189
+ const texts = blocks.filter((block) => block.type === "text").map((block) => str(block.text));
190
+ if (!texts.some((text) => text.trim()) && !blocks.some((block) => block.type === "image")) continue;
191
+ start(record.timestamp, texts, blocks.map(pictureOf).filter(Boolean));
192
+ continue;
193
+ }
194
+ if (record.type === "assistant" && Array.isArray(content)) {
195
+ const turn = turns.at(-1);
196
+ for (const block of content.map(obj)) {
197
+ if (!turn || !block) continue;
198
+ if (block.type === "text") turn.said.push(str(block.text));
199
+ if (block.type === "tool_use") turn.tools.push({ name: str(block.name), input: block.input, cwd: here });
200
+ }
201
+ continue;
202
+ }
203
+ if (record.type !== "response_item" || !payload) continue;
204
+ if (payload.type === "message") {
205
+ const blocks = Array.isArray(payload.content) ? payload.content.map(obj).filter(Boolean) : [];
206
+ const texts = blocks.filter((block) => block.type === "input_text" || block.type === "output_text" || block.type === "text").map((block) => str(block.text));
207
+ if (payload.role === "user") {
208
+ if (texts.every((text) => !text.trim() || CODEX_CONTEXT.test(text)) && !blocks.some((block) => block.type === "input_image")) continue;
209
+ start(record.timestamp, texts.filter((text) => !CODEX_CONTEXT.test(text)), blocks.map(pictureOf).filter(Boolean));
210
+ } else if (payload.role === "assistant") turns.at(-1)?.said.push(...texts);
211
+ continue;
212
+ }
213
+ if (payload.type === "function_call" || payload.type === "custom_tool_call" || payload.type === "local_shell_call") {
214
+ let input = payload.arguments ?? payload.input ?? payload.action ?? "";
215
+ if (typeof input === "string") {
216
+ try {
217
+ input = JSON.parse(input);
218
+ } catch {
219
+ }
220
+ }
221
+ turns.at(-1)?.tools.push({ name: str(payload.name), input, cwd: str(obj(input)?.workdir) || here });
253
222
  }
254
- renameSync2(temporary, path);
255
- const directory = openSync2(dirname3(path), "r");
223
+ }
224
+ return turns;
225
+ }
226
+ function strings(value, into = []) {
227
+ if (typeof value === "string") into.push(value);
228
+ else if (Array.isArray(value)) for (const item of value) strings(item, into);
229
+ else if (value && typeof value === "object") for (const item of Object.values(value)) strings(item, into);
230
+ return into;
231
+ }
232
+ function resolved(candidate, cwd, home) {
233
+ const text = candidate.replace(/^file:\/\//, "");
234
+ if (text.startsWith("~/")) return join(home, text.slice(2));
235
+ if (isAbsolute(text)) return normalize(text);
236
+ return cwd ? resolve(cwd, text) : null;
237
+ }
238
+ function credential(file) {
239
+ return !importPathAllowed(file);
240
+ }
241
+ function kept(file, home) {
242
+ const inHome = file.startsWith(`${home}/`);
243
+ if (!inHome && SCRATCH_ROOTS.some((root) => file.startsWith(root))) return false;
244
+ if (!importPathAllowed(file)) return false;
245
+ const relative = inHome ? file.slice(home.length + 1) : file.replace(/^\//, "");
246
+ return !relative.split("/").slice(0, -1).some((part) => REBUILT.has(part));
247
+ }
248
+ function inWebProject(file, home, stat) {
249
+ for (let dir = dirname2(file); dir.startsWith(`${home}/`); dir = dirname2(dir)) {
256
250
  try {
257
- fsyncSync2(directory);
258
- } finally {
259
- closeSync2(directory);
251
+ if (stat(join(dir, "package.json")).isFile()) return true;
252
+ } catch {
260
253
  }
261
- } catch (error) {
262
- rmSync2(temporary, { force: true });
263
- throw error;
264
254
  }
265
- }
266
- function rootPath(root) {
267
- mkdirSync2(root, { recursive: true, mode: 448 });
268
- if (lstatSync(root).isSymbolicLink()) throw new Error("Source queue must not be a symlink.");
269
- return realpathSync2(root);
270
- }
271
- function validateManifest(value) {
272
- if (!record(value) || value.version !== 1 || typeof value.apiBase !== "string" || typeof value.workspace !== "string" || !value.workspace || !record(value.local) || typeof value.local.harness !== "string" || typeof value.local.sessionId !== "string" || !/^[a-z0-9_-]{1,40}$/.test(value.local.harness) || !/^[A-Za-z0-9._:@-]{1,200}$/.test(value.local.sessionId) || typeof value.local.signature !== "string" || !strings(value.local.files) || !record(value.body) || !["capturing", "ready"].includes(String(value.stage))) throw new Error("Invalid pending source descriptor.");
273
- if (value.apiBase !== canonicalSourceApiBase(value.apiBase)) throw new Error("Pending endpoint identity is not canonical.");
274
- if (value.fileId !== void 0 && (typeof value.fileId !== "string" || !/^file_[A-Za-z0-9_-]{1,200}$/.test(value.fileId))) throw new Error("Invalid pending source file id.");
275
- if (value.previewSaved !== void 0 && typeof value.previewSaved !== "boolean") throw new Error("Invalid pending preview receipt.");
276
- if (value.link !== void 0 && value.link !== null && (!record(value.link) || typeof value.link.session_id !== "string" || typeof value.link.task_id !== "string")) throw new Error("Invalid pending task link.");
277
- if (value.source !== void 0 && (!record(value.source) || typeof value.source.sha256 !== "string" || !digest.test(value.source.sha256) || !natural(value.source.bytes) || !natural(value.source.records) || !natural(value.source.sourceFiles) || typeof value.source.complete !== "boolean" || !strings(value.source.gaps))) throw new Error("Invalid pending source receipt.");
278
- if (value.stage === "ready" && !value.source) throw new Error("Pending source has no capture receipt.");
279
- }
280
- function listPendingSources(root) {
281
- if (!existsSync3(root)) return [];
282
- const canonical = rootPath(root);
283
- const keys = readdirSync(canonical).sort();
284
- for (const key of keys.filter((name) => /^\.done-[a-f0-9]{64}-[a-f0-9]{16}$/.test(name))) {
285
- const path = join2(canonical, key);
286
- if (lstatSync(path).isSymbolicLink() || dirname3(realpathSync2(path)) !== canonical) throw new Error("Unsafe completed source directory.");
287
- rmSync2(path, { recursive: true });
255
+ return false;
256
+ }
257
+ function publishedPaths(tool) {
258
+ if (!PUBLISHING_TOOLS.has(tool.name)) return [];
259
+ const input = obj(tool.input) ?? {};
260
+ const files = obj(input.files) ? Object.values(input.files) : [];
261
+ return [input.file_path, ...Array.isArray(input.file_paths) ? input.file_paths : [], ...files.map((entry) => typeof entry === "string" ? entry : obj(entry)?.from)].filter((entry) => typeof entry === "string" && entry.length > 0);
262
+ }
263
+ function sessionResults(records, options = {}) {
264
+ const home = options.home ?? homedir2();
265
+ const stat = options.stat ?? statSync;
266
+ const turns = turnsOf(records, options.cwd ?? "");
267
+ const found = [];
268
+ for (const turn of options.all ? turns : turns.slice(-1)) {
269
+ const said = turn.said.join("\n");
270
+ if (!said.trim()) continue;
271
+ const seen = /* @__PURE__ */ new Set();
272
+ const add = (file, origin, mediaType, fresh) => {
273
+ if (seen.has(file)) return;
274
+ seen.add(file);
275
+ let info;
276
+ try {
277
+ info = stat(file);
278
+ } catch {
279
+ return;
280
+ }
281
+ if (!info.isFile() || info.size === 0 || info.size > MAX_RESULT_BYTES || fresh && !(info.mtimeMs >= turn.since - 1e3)) return;
282
+ found.push({ path: file, name: basename(file), mediaType, size: info.size, mtimeMs: Math.floor(info.mtimeMs), origin });
283
+ };
284
+ turn.pictures.forEach((picture, index) => {
285
+ const data = Buffer.from(picture.data, "base64");
286
+ if (!data.length || data.length > MAX_RESULT_BYTES) return;
287
+ const ext = picture.mediaType.split("/")[1]?.replace("jpeg", "jpg").replace(/[^a-z0-9]/gi, "") || "png";
288
+ found.push({ name: `pasted-${new Date(turn.since || 0).toISOString().slice(0, 10)}-${index + 1}.${ext}`, mediaType: picture.mediaType, size: data.length, origin: "sent", data });
289
+ });
290
+ for (const line of (ATTACHED.exec(turn.texts.join("\n"))?.[1] ?? "").split("\n")) {
291
+ const file = line.replace(/^- /, "").trim();
292
+ if (isAbsolute(file) && !credential(normalize(file))) add(normalize(file), "sent", typeOf(file, true), false);
293
+ }
294
+ for (const tool of turn.tools) for (const raw of publishedPaths(tool)) {
295
+ const file = resolved(raw, tool.cwd, home);
296
+ if (file && !credential(file)) add(file, "published", typeOf(file, true), false);
297
+ }
298
+ const texts = [...turn.tools.flatMap((tool) => strings(tool.input)), ...turn.said];
299
+ for (const text of texts) {
300
+ for (const match of text.matchAll(PATH_LIKE)) {
301
+ const tool = turn.tools.find((entry) => strings(entry.input).includes(text));
302
+ const file = resolved(match[0], tool?.cwd ?? options.cwd ?? "", home);
303
+ if (!file || seen.has(file)) continue;
304
+ const name = basename(file);
305
+ const mediaType = typeOf(name, false);
306
+ if (!mediaType || !said.includes(name) || !kept(file, home)) continue;
307
+ if (mediaType === "text/html" && inWebProject(file, home, stat)) continue;
308
+ add(file, "delivered", mediaType, true);
309
+ }
310
+ }
288
311
  }
289
- return keys.filter((key) => !/^\.done-[a-f0-9]{64}-[a-f0-9]{16}$/.test(key)).map((key) => {
290
- if (!digest.test(key)) throw new Error("Unexpected entry in the private source queue.");
291
- const directory = join2(canonical, key);
292
- if (!lstatSync(directory).isDirectory() || lstatSync(directory).isSymbolicLink() || dirname3(realpathSync2(directory)) !== canonical) throw new Error("Unsafe pending source directory.");
293
- const path = join2(directory, "manifest.json");
294
- if (!existsSync3(path) || !lstatSync(path).isFile() || lstatSync(path).isSymbolicLink()) throw new Error("Pending source is missing a safe descriptor.");
295
- const manifest = JSON.parse(readFileSync(path, "utf8"));
296
- validateManifest(manifest);
297
- if (key !== keyOf(manifest)) throw new Error("Pending source routing does not match its directory.");
298
- return { directory, manifest };
299
- });
312
+ return found;
300
313
  }
301
- function beginPendingSource(root, manifest) {
302
- const data = { ...manifest, version: 1, stage: "capturing" };
303
- validateManifest(data);
304
- const directory = join2(rootPath(root), keyOf(data));
305
- mkdirSync2(directory, { mode: 448 });
306
- const pending = { directory, manifest: data };
307
- try {
308
- savePendingSource(pending, {});
309
- } catch (error) {
310
- rmSync2(directory, { recursive: true, force: true });
311
- throw error;
312
- }
313
- return pending;
314
- }
315
- function savePendingSource(pending, patch) {
316
- const manifest = { ...pending.manifest, ...patch };
317
- validateManifest(manifest);
318
- if (basename2(pending.directory) !== keyOf(manifest)) throw new Error("Pending source identity cannot be changed.");
319
- writePrivateJson(join2(pending.directory, "manifest.json"), manifest);
320
- pending.manifest = manifest;
321
- }
322
- async function recoverPendingSource(pending) {
323
- const { manifest, directory } = pending;
324
- if (lstatSync(directory).isSymbolicLink()) throw new Error("Unsafe pending source directory.");
325
- const names = readdirSync(directory).filter((name2) => /^[a-f0-9]{64}\.jsonl$/.test(name2));
326
- const name = manifest.source ? `${manifest.source.sha256}.jsonl` : names.length === 1 ? names[0] : void 0;
327
- if (!name || names.length !== 1) throw new IncompleteSourceCaptureError("Source capture was interrupted before a complete archive was finalized; pending data retained.");
328
- const path = join2(directory, name);
329
- const before = lstatSync(path);
330
- if (!before.isFile() || before.isSymbolicLink() || dirname3(realpathSync2(path)) !== realpathSync2(directory)) throw new Error("Unsafe pending archive path.");
331
- const hash = createHash2("sha256"), decoder = new TextDecoder("utf-8", { fatal: true });
332
- let bytes = 0, records = 0, lineBuffer = "", header, summary;
333
- const files = /* @__PURE__ */ new Set();
334
- const line = (text) => {
335
- if (!text) return;
336
- const row = JSON.parse(text);
337
- if (!record(row) || summary) throw new Error("Invalid pending archive record ordering.");
338
- if (!header) {
339
- header = row;
340
- return;
314
+ function pageWithImages(bytes, file, home = homedir2(), explicit = false) {
315
+ const html = bytes.toString("utf8");
316
+ const dir = dirname2(file);
317
+ const images = /* @__PURE__ */ new Map();
318
+ let total = bytes.length;
319
+ for (const match of html.matchAll(LOCAL_REFERENCE)) {
320
+ const reference = match[3].trim();
321
+ if (images.has(reference) || /^(?:[a-z][a-z0-9+.-]*:|\/\/|#|\/)/i.test(reference)) continue;
322
+ let target;
323
+ try {
324
+ target = resolve(dir, decodeURI(reference.split(/[?#]/, 1)[0]));
325
+ } catch {
326
+ continue;
341
327
  }
342
- if (record(row.capture_summary)) {
343
- summary = row.capture_summary;
344
- return;
328
+ const mediaType = typeOf(target, false);
329
+ if (!mediaType?.startsWith("image/") || !(explicit ? target.startsWith(`${dir}${sep}`) : kept(target, home))) continue;
330
+ let image;
331
+ try {
332
+ image = readFileSync(target);
333
+ } catch {
334
+ continue;
345
335
  }
346
- if (!natural(row.source_file) || !natural(header.source_files) || row.source_file >= header.source_files) throw new Error("Invalid source file index.");
347
- if (record(row.omitted_record) && files.has(row.source_file)) return;
348
- if (typeof row.record_json === "string") {
349
- if (!files.has(row.source_file)) throw new Error("Pending record precedes its source metadata.");
350
- records++;
351
- } else if (typeof row.source_sha256 === "string" && digest.test(row.source_sha256) && natural(row.source_bytes)) files.add(row.source_file);
352
- else throw new Error("Invalid pending source record.");
353
- };
354
- for await (const chunk of createReadStream2(path)) {
355
- hash.update(chunk);
356
- bytes += chunk.length;
357
- lineBuffer += decoder.decode(chunk, { stream: true });
358
- let newline;
359
- while ((newline = lineBuffer.indexOf("\n")) !== -1) {
360
- line(lineBuffer.slice(0, newline));
361
- lineBuffer = lineBuffer.slice(newline + 1);
336
+ const uri = `data:${mediaType};base64,${image.toString("base64")}`;
337
+ total += uri.length - reference.length;
338
+ if (total > MAX_RESULT_BYTES) return bytes;
339
+ images.set(reference, uri);
340
+ }
341
+ if (!images.size) return bytes;
342
+ return Buffer.from(html.replace(LOCAL_REFERENCE, (whole, lead, quote, reference) => images.has(reference.trim()) ? `${lead}${quote}${images.get(reference.trim())}${quote}` : whole), "utf8");
343
+ }
344
+ function resultRequestId(...parts) {
345
+ const hex = createHash("sha256").update(JSON.stringify(parts)).digest("hex");
346
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-8${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
347
+ }
348
+ var RateLimited = class extends Error {
349
+ };
350
+ async function call(fetcher, method, path, workspace, body, multipart2) {
351
+ const token = apiToken();
352
+ if (!token) throw new Error("not logged in \u2014 run `sky auth login`");
353
+ const response = await fetcher(`${apiBase()}${path}`, {
354
+ method,
355
+ headers: { authorization: `Bearer ${token}`, "x-universe": workspace, ...multipart2 ? { "content-type": multipart2.type } : body ? { "content-type": "application/json" } : {} },
356
+ body: multipart2 ? new Uint8Array(multipart2.body) : body ? JSON.stringify(body) : void 0,
357
+ signal: AbortSignal.timeout(12e4)
358
+ });
359
+ if (response.status === 429) throw new RateLimited("rate limited");
360
+ const answer = await response.json().catch(() => null);
361
+ if (!response.ok) throw new Error(str(obj(answer?.error)?.message) || `${method} ${path} failed (${response.status})`);
362
+ return answer;
363
+ }
364
+ function multipart(fields) {
365
+ const boundary = `----sky${createHash("sha256").update(fields.requestId).digest("hex").slice(0, 24)}`;
366
+ const filename = fields.name.replace(/[\r\n]/g, " ").replace(/"/g, "%22");
367
+ const field = (key, value) => Buffer.from(`--${boundary}\r
368
+ Content-Disposition: form-data; name="${key}"\r
369
+ \r
370
+ ${value}\r
371
+ `, "utf8");
372
+ return { type: `multipart/form-data; boundary=${boundary}`, body: Buffer.concat([
373
+ Buffer.from(`--${boundary}\r
374
+ Content-Disposition: form-data; name="file"; filename="${filename}"\r
375
+ Content-Type: ${fields.mediaType}\r
376
+ \r
377
+ `, "utf8"),
378
+ fields.bytes,
379
+ Buffer.from("\r\n", "utf8"),
380
+ field("requestId", fields.requestId),
381
+ field("sessionId", fields.sessionId),
382
+ Buffer.from(`--${boundary}--\r
383
+ `, "utf8")
384
+ ]) };
385
+ }
386
+ function readRecords(files) {
387
+ const records = [];
388
+ for (const file of files) {
389
+ if (file.endsWith(".gz")) continue;
390
+ for (const line of readFileSync(file, "utf8").split("\n")) {
391
+ if (!line.trim()) continue;
392
+ try {
393
+ records.push(JSON.parse(line));
394
+ } catch {
395
+ }
362
396
  }
363
397
  }
364
- lineBuffer += decoder.decode();
365
- if (lineBuffer) line(lineBuffer);
366
- const sha256 = hash.digest("hex");
367
- const after = statSync2(path);
368
- if (before.size !== after.size || before.mtimeMs !== after.mtimeMs || sha256 !== name.slice(0, -6)) throw new Error("Pending archive bytes or hash changed.");
369
- if (!header || header.format !== `agentsky.trace-source/v${SOURCE_CAPTURE_VERSION}` || header.harness !== manifest.local.harness || header.session_id !== manifest.local.sessionId || header.workspace !== manifest.workspace || header.api_base !== manifest.apiBase || header.source_signature !== manifest.local.signature || !natural(header.source_files) || files.size !== header.source_files || !summary || summary.records !== records || typeof summary.complete !== "boolean" || !strings(summary.gaps) || summary.complete !== (summary.gaps.length === 0)) throw new Error("Pending archive identity or completion receipt does not match its descriptor.");
370
- const source = { path, sha256, bytes, records, sourceFiles: header.source_files, complete: summary.complete, gaps: summary.gaps };
371
- if (manifest.source && JSON.stringify(manifest.source) !== JSON.stringify({ sha256, bytes, records, sourceFiles: source.sourceFiles, complete: source.complete, gaps: source.gaps })) throw new Error("Pending archive receipt was changed.");
372
- return source;
373
- }
374
- function completePendingCapture(pending, source) {
375
- const { path: _path, ...receipt } = source;
376
- savePendingSource(pending, { stage: "ready", source: receipt });
377
- }
378
- function removePendingSource(pending) {
379
- if (basename2(pending.directory) !== keyOf(pending.manifest) || lstatSync(pending.directory).isSymbolicLink()) throw new Error("Unsafe source cleanup path.");
380
- const completed = join2(dirname3(pending.directory), `.done-${basename2(pending.directory)}-${randomBytes(8).toString("hex")}`);
381
- renameSync2(pending.directory, completed);
382
- const fd = openSync2(dirname3(completed), "r");
383
- try {
384
- fsyncSync2(fd);
385
- } finally {
386
- closeSync2(fd);
398
+ return records;
399
+ }
400
+ async function captureSessionResults(input, state, options = {}) {
401
+ const fetcher = options.fetcher ?? fetch;
402
+ const home = options.home ?? homedir2();
403
+ const key = `${input.harness}:${input.nativeId}`;
404
+ const saved = new Set(state.saved);
405
+ let count = 0;
406
+ let limited = false;
407
+ for (const result of sessionResults(readRecords(input.files), { cwd: input.cwd, home, all: !state.read.includes(key) })) {
408
+ const version = result.path ? [`native:${input.nativeId}`, result.path, result.size, result.mtimeMs] : [`native:${input.nativeId}`, result.name, createHash("sha256").update(result.data).digest("hex")];
409
+ const id = JSON.stringify(version);
410
+ if (saved.has(id)) continue;
411
+ try {
412
+ const read = result.data ?? readFileSync(result.path);
413
+ if (read.length !== result.size) continue;
414
+ const bytes = result.path && result.mediaType === "text/html" ? pageWithImages(read, result.path, home, result.origin !== "delivered") : read;
415
+ await call(fetcher, "POST", "/library/artifacts", input.workspace, void 0, multipart({ name: result.name, mediaType: result.mediaType, bytes, requestId: resultRequestId(...version), sessionId: input.sessionId }));
416
+ saved.add(id);
417
+ state.saved.push(id);
418
+ count++;
419
+ } catch (error) {
420
+ if (error instanceof RateLimited) {
421
+ limited = true;
422
+ break;
423
+ }
424
+ }
387
425
  }
388
- rmSync2(completed, { recursive: true });
426
+ if (!limited && !state.read.includes(key)) state.read.push(key);
427
+ if (state.saved.length > 2e4) state.saved.splice(0, state.saved.length - 2e4);
428
+ if (state.read.length > 5e3) state.read.splice(0, state.read.length - 5e3);
429
+ return { saved: count };
389
430
  }
390
431
 
391
432
  // src/trace-session.ts
@@ -396,6 +437,53 @@ var BATCH_MESSAGES = 200;
396
437
  var BATCH_BYTES = 3 * 1024 * 1024;
397
438
  var MESSAGE_BYTES = 384 * 1024;
398
439
  var HARNESS_BLOCKS = /<(system-reminder|local-command-stdout|local-command-stderr|command-message|command-args|task-notification)>[\s\S]*?<\/\1>/g;
440
+ var SKILL_FILE = /(?:^|[/\s'"=])skills\/([a-z0-9][a-z0-9-]*)\/SKILL\.md/g;
441
+ var WRITING_TOOL = /write|edit|create|delete|patch|apply/i;
442
+ var WRITES_SKILL_FILE = /(?:>|\btee\b|\bsed\s+-i|\brm\b|\bmv\b|\bcp\b|\btouch\b)[^|;&]*SKILL\.md/;
443
+ var TOOL_CALL = /^([A-Za-z_][\w.:-]*) ([{[][\s\S]*)$/;
444
+ function skillNamesRead(text) {
445
+ const call2 = TOOL_CALL.exec(text);
446
+ if (!call2) return [];
447
+ const tool = call2[1];
448
+ const args = call2[2];
449
+ if (tool === "Skill") {
450
+ try {
451
+ const parsed = JSON.parse(args);
452
+ return [parsed.skill, parsed.name].filter((name) => typeof name === "string").map((name) => name.split(":").at(-1));
453
+ } catch {
454
+ return [];
455
+ }
456
+ }
457
+ if (WRITING_TOOL.test(tool) || WRITES_SKILL_FILE.test(args)) return [];
458
+ return [...args.matchAll(SKILL_FILE)].map((match) => match[1]);
459
+ }
460
+ function skillReadsIn(rows, messages, state) {
461
+ const kept2 = new Set(messages.map((message) => message.id));
462
+ const reads = /* @__PURE__ */ new Map();
463
+ let current = null;
464
+ let waiting = [];
465
+ const settle = (messageId) => {
466
+ for (const read of waiting) {
467
+ const revision = revisionAt(state, read.name, read.at);
468
+ if (revision && kept2.has(messageId) && !reads.has(revision)) reads.set(revision, { revision_id: revision, message_id: messageId });
469
+ }
470
+ waiting = [];
471
+ };
472
+ rows.forEach((row, index) => {
473
+ if (row.role === "tool") {
474
+ const names = skillNamesRead(row.text).filter((name) => state.skills[name]);
475
+ waiting.push(...names.map((name) => ({ name, ...row.at ? { at: row.at } : {} })));
476
+ if (current?.role === "agent") settle(current.id);
477
+ return;
478
+ }
479
+ const role = row.role === "user" ? "user" : "agent";
480
+ const text = role === "user" ? row.text.replace(HARNESS_BLOCKS, "").trim() : row.text;
481
+ if (!text) return;
482
+ if (!current || current.role !== role) current = { id: `${UPLOADED_MESSAGE_PREFIX}${index}`, role };
483
+ if (role === "agent") settle(current.id);
484
+ });
485
+ return [...reads.values()];
486
+ }
399
487
  function sessionHarness(harness) {
400
488
  return SESSION_HARNESSES.includes(harness);
401
489
  }
@@ -456,10 +544,15 @@ async function saveTraceSession(request, input, now = Date.now()) {
456
544
  });
457
545
  if (opened.workspace) return { status: "elsewhere", sessionId: opened.sessionId, workspace: opened.workspace };
458
546
  let stored = 0;
459
- for (const [index, messages] of messageBatches(transcriptMessages(body.messages, body.started_at)).entries()) {
547
+ const transcript = transcriptMessages(body.messages, body.started_at);
548
+ const reads = input.skills ? skillReadsIn(body.messages, transcript, input.skills).slice(0, 200) : [];
549
+ const batches = messageBatches(transcript);
550
+ for (const [index, messages] of batches.entries()) {
551
+ const last2 = index === batches.length - 1;
460
552
  const answer = await request("PUT", `/sessions/${encodeURIComponent(opened.sessionId)}/mirror`, workspace, {
461
553
  messages,
462
- ...index === 0 && body.title ? { title: body.title.slice(0, 120) } : {}
554
+ ...index === 0 && body.title ? { title: body.title.slice(0, 120) } : {},
555
+ ...last2 && reads.length ? { skills_read: reads } : {}
463
556
  });
464
557
  stored += answer.stored;
465
558
  }
@@ -477,6 +570,8 @@ var MESSAGES_MAX = 2e4;
477
570
  var BODY_MAX_BYTES = 7 * 1024 * 1024;
478
571
  var UPLOAD_CONCURRENCY = 4;
479
572
  var FULL_SWEEP_MS = 7 * 24 * 60 * 60 * 1e3;
573
+ var RESULTS_BACKFILL_MS = SESSION_BACKFILL_DAYS * 24 * 60 * 60 * 1e3;
574
+ var RESULTS_TRIES = 3;
480
575
  var RETRIES = 3;
481
576
  var RATE_LIMIT_RETRIES = 30;
482
577
  var HARNESS_PREAMBLES = ["<environment_context>", "<user_instructions>", "<permissions instructions>", "# AGENTS.md instructions", "<INSTRUCTIONS>", "<external_codex_apps_open_page>"];
@@ -484,13 +579,16 @@ var titleOf = (text) => /^\s*</.test(text) ? void 0 : text.trim().slice(0, 120)
484
579
  var sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
485
580
  var backoffMs = (attempt) => Math.min(3e4, 1e3 * 2 ** attempt);
486
581
  function skyDir() {
487
- return process.env.SKY_CONFIG_DIR ?? join3(homedir2(), ".config", "sky");
582
+ return process.env.SKY_CONFIG_DIR ?? join2(homedir3(), ".config", "sky");
488
583
  }
489
584
  function syncConfigPath() {
490
- return join3(skyDir(), "trace-sync.json");
585
+ return join2(skyDir(), "trace-sync.json");
586
+ }
587
+ function resultsStatePath() {
588
+ return join2(skyDir(), "session-results.json");
491
589
  }
492
590
  function statePath() {
493
- return join3(skyDir(), "trace-sync-state.json");
591
+ return join2(skyDir(), "trace-sync-state.json");
494
592
  }
495
593
  function readJson(path, fallback) {
496
594
  try {
@@ -500,8 +598,8 @@ function readJson(path, fallback) {
500
598
  }
501
599
  }
502
600
  function writeJson(path, value) {
503
- mkdirSync3(dirname4(path), { recursive: true });
504
- writeFileSync2(path, `${JSON.stringify(value, null, 2)}
601
+ mkdirSync(dirname3(path), { recursive: true });
602
+ writeFileSync(path, `${JSON.stringify(value, null, 2)}
505
603
  `, { mode: 384 });
506
604
  }
507
605
  function loadSyncConfig() {
@@ -511,19 +609,19 @@ function saveSyncConfig(config) {
511
609
  writeJson(syncConfigPath(), config);
512
610
  }
513
611
  function claudeRoot() {
514
- return join3(process.env.CLAUDE_CONFIG_DIR ?? join3(homedir2(), ".claude"), "projects");
612
+ return join2(process.env.CLAUDE_CONFIG_DIR ?? join2(homedir3(), ".claude"), "projects");
515
613
  }
516
614
  function codexRoot() {
517
- return process.env.CODEX_HOME ?? join3(homedir2(), ".codex");
615
+ return process.env.CODEX_HOME ?? join2(homedir3(), ".codex");
518
616
  }
519
617
  var SKY_PARSED = /* @__PURE__ */ new Set(["claude-code", "codex"]);
520
618
  var COLLECTOR_HARNESS = { "claude-code": "claude_code", codex: "codex" };
521
619
  var collectorModules;
522
620
  async function traceCollector() {
523
- const dir = join3(dirname4(fileURLToPath(import.meta.url)), "..", "vendor", "trace-collect");
621
+ const dir = join2(dirname3(fileURLToPath(import.meta.url)), "..", "vendor", "trace-collect");
524
622
  collectorModules ??= Promise.all([
525
- import(pathToFileURL(join3(dir, "collect.mjs")).href),
526
- import(pathToFileURL(join3(dir, "sinks", "agentsky.mjs")).href)
623
+ import(pathToFileURL(join2(dir, "collect.mjs")).href),
624
+ import(pathToFileURL(join2(dir, "sinks", "agentsky.mjs")).href)
527
625
  ]).then(([collector, sink]) => ({ collector, sink }));
528
626
  return collectorModules;
529
627
  }
@@ -533,7 +631,7 @@ function collectorHarness(harness) {
533
631
  async function sourceFolders() {
534
632
  const { collector } = await traceCollector();
535
633
  const others = (await collector.describeSources()).filter((source) => !SKY_PARSED.has(source.id)).flatMap((source) => source.found);
536
- return [claudeRoot(), join3(codexRoot(), "sessions"), join3(codexRoot(), "archived_sessions"), ...others, collector.STORE];
634
+ return [claudeRoot(), join2(codexRoot(), "sessions"), join2(codexRoot(), "archived_sessions"), ...others, collector.STORE];
537
635
  }
538
636
  async function supportedAgents() {
539
637
  const { collector } = await traceCollector();
@@ -549,7 +647,7 @@ function signatureOf(files) {
549
647
  const parts = [];
550
648
  for (const file of files) {
551
649
  try {
552
- const stat = statSync3(file);
650
+ const stat = statSync2(file);
553
651
  parts.push(`${file}:${stat.size}:${Math.floor(stat.mtimeMs)}`);
554
652
  } catch {
555
653
  return null;
@@ -562,25 +660,25 @@ function claudeSessions() {
562
660
  const found = [];
563
661
  let projects;
564
662
  try {
565
- projects = readdirSync2(root);
663
+ projects = readdirSync(root);
566
664
  } catch {
567
665
  return found;
568
666
  }
569
667
  for (const project of projects) {
570
- const dir = join3(root, project);
668
+ const dir = join2(root, project);
571
669
  let entries;
572
670
  try {
573
- entries = readdirSync2(dir);
671
+ entries = readdirSync(dir);
574
672
  } catch {
575
673
  continue;
576
674
  }
577
675
  for (const entry of entries) {
578
676
  if (!entry.endsWith(".jsonl")) continue;
579
677
  const sessionId = entry.slice(0, -".jsonl".length);
580
- const files = [join3(dir, entry)];
581
- const subagents = join3(dir, sessionId, "subagents");
678
+ const files = [join2(dir, entry)];
679
+ const subagents = join2(dir, sessionId, "subagents");
582
680
  try {
583
- files.push(...readdirSync2(subagents).filter((name) => name.endsWith(".jsonl")).sort().map((name) => join3(subagents, name)));
681
+ files.push(...readdirSync(subagents).filter((name) => name.endsWith(".jsonl")).sort().map((name) => join2(subagents, name)));
584
682
  } catch {
585
683
  }
586
684
  const signature = signatureOf(files);
@@ -590,8 +688,8 @@ function claudeSessions() {
590
688
  return found;
591
689
  }
592
690
  async function* readLines(file) {
593
- const raw = createReadStream3(file);
594
- const input = file.endsWith(".gz") ? raw.pipe(createGunzip2()) : raw;
691
+ const raw = createReadStream(file);
692
+ const input = file.endsWith(".gz") ? raw.pipe(createGunzip()) : raw;
595
693
  const reader = createInterface({ input, crlfDelay: Infinity });
596
694
  try {
597
695
  for await (const line of reader) yield line;
@@ -605,7 +703,7 @@ async function firstLine(file) {
605
703
  return "";
606
704
  }
607
705
  function codexIdCachePath() {
608
- return join3(skyDir(), "trace-sync-codex-ids.json");
706
+ return join2(skyDir(), "trace-sync-codex-ids.json");
609
707
  }
610
708
  async function codexSessions() {
611
709
  const byId = /* @__PURE__ */ new Map();
@@ -614,12 +712,12 @@ async function codexSessions() {
614
712
  const walk = async (dir) => {
615
713
  let entries;
616
714
  try {
617
- entries = readdirSync2(dir, { withFileTypes: true });
715
+ entries = readdirSync(dir, { withFileTypes: true });
618
716
  } catch {
619
717
  return;
620
718
  }
621
719
  for (const entry of entries) {
622
- const full = join3(dir, entry.name);
720
+ const full = join2(dir, entry.name);
623
721
  if (entry.isDirectory()) {
624
722
  await walk(full);
625
723
  continue;
@@ -638,13 +736,13 @@ async function codexSessions() {
638
736
  }
639
737
  if (!id) continue;
640
738
  seen[full] = { signature, id };
641
- const mtime = statSync3(full).mtimeMs;
739
+ const mtime = statSync2(full).mtimeMs;
642
740
  const known = byId.get(id);
643
741
  if (!known || mtime > known.mtime) byId.set(id, { harness: "codex", sessionId: id, files: [full], signature, mtime });
644
742
  }
645
743
  };
646
- await walk(join3(codexRoot(), "sessions"));
647
- await walk(join3(codexRoot(), "archived_sessions"));
744
+ await walk(join2(codexRoot(), "sessions"));
745
+ await walk(join2(codexRoot(), "archived_sessions"));
648
746
  try {
649
747
  writeJson(codexIdCachePath(), seen);
650
748
  } catch {
@@ -653,12 +751,12 @@ async function codexSessions() {
653
751
  }
654
752
  function claudeSessionAt(path) {
655
753
  const root = claudeRoot();
656
- if (!path.startsWith(`${root}${sep}`) || !path.endsWith(".jsonl") || basename3(dirname4(dirname4(path))) !== basename3(root)) return null;
657
- const sessionId = basename3(path, ".jsonl");
754
+ if (!path.startsWith(`${root}${sep2}`) || !path.endsWith(".jsonl") || basename2(dirname3(dirname3(path))) !== basename2(root)) return null;
755
+ const sessionId = basename2(path, ".jsonl");
658
756
  const files = [path];
659
- const subagents = join3(dirname4(path), sessionId, "subagents");
757
+ const subagents = join2(dirname3(path), sessionId, "subagents");
660
758
  try {
661
- files.push(...readdirSync2(subagents).filter((name) => name.endsWith(".jsonl")).sort().map((name) => join3(subagents, name)));
759
+ files.push(...readdirSync(subagents).filter((name) => name.endsWith(".jsonl")).sort().map((name) => join2(subagents, name)));
662
760
  } catch {
663
761
  }
664
762
  const signature = signatureOf(files);
@@ -692,12 +790,12 @@ async function discoverSessions(onCollectorError) {
692
790
  function asRecord(value) {
693
791
  return value && typeof value === "object" && !Array.isArray(value) ? value : null;
694
792
  }
695
- function str(value) {
793
+ function str2(value) {
696
794
  return typeof value === "string" && value.length ? value : void 0;
697
795
  }
698
796
  function toolResultText(content) {
699
797
  if (typeof content === "string") return content;
700
- if (Array.isArray(content)) return content.map((part) => str(asRecord(part)?.text) ?? "").filter(Boolean).join("\n");
798
+ if (Array.isArray(content)) return content.map((part) => str2(asRecord(part)?.text) ?? "").filter(Boolean).join("\n");
701
799
  return "";
702
800
  }
703
801
  function stringify(value) {
@@ -728,7 +826,7 @@ var Collector = class {
728
826
  }
729
827
  };
730
828
  function isoOrUndefined(value) {
731
- const text = str(value);
829
+ const text = str2(value);
732
830
  if (!text) return void 0;
733
831
  const time = Date.parse(text);
734
832
  return Number.isFinite(time) ? new Date(time).toISOString() : void 0;
@@ -747,29 +845,29 @@ async function parseClaudeSession(session) {
747
845
  for await (const line of readLines(file)) {
748
846
  if (!line.trim()) continue;
749
847
  references.scan(line);
750
- let record2;
848
+ let record;
751
849
  try {
752
- record2 = asRecord(JSON.parse(line));
850
+ record = asRecord(JSON.parse(line));
753
851
  } catch {
754
852
  out.partial = true;
755
853
  continue;
756
854
  }
757
- if (!record2) continue;
758
- source.push(index === 0 ? record2 : { ...record2, isSidechain: true });
759
- const at = isoOrUndefined(record2.timestamp);
855
+ if (!record) continue;
856
+ source.push(index === 0 ? record : { ...record, isSidechain: true });
857
+ const at = isoOrUndefined(record.timestamp);
760
858
  if (index === 0) {
761
- out.cwd ??= str(record2.cwd);
762
- out.gitBranch ??= str(record2.gitBranch);
859
+ out.cwd ??= str2(record.cwd);
860
+ out.gitBranch ??= str2(record.gitBranch);
763
861
  if (at) {
764
862
  out.startedAt ??= at;
765
863
  out.endedAt = at;
766
864
  }
767
865
  }
768
- const message = asRecord(record2.message);
769
- if (!message || record2.isMeta === true) continue;
770
- const own = index === 0 && record2.isSidechain !== true && record2.parent_tool_use_id == null;
771
- if (record2.type === "user") {
772
- const spoken = own && asRecord(record2.origin)?.kind !== "task-notification" && record2.promptSource !== "system" && typeof record2.wakeupSource !== "string";
866
+ const message = asRecord(record.message);
867
+ if (!message || record.isMeta === true) continue;
868
+ const own = index === 0 && record.isSidechain !== true && record.parent_tool_use_id == null;
869
+ if (record.type === "user") {
870
+ const spoken = own && asRecord(record.origin)?.kind !== "task-notification" && record.promptSource !== "system" && typeof record.wakeupSource !== "string";
773
871
  const content = message.content;
774
872
  if (typeof content === "string") {
775
873
  collector.push("user", content, at, spoken);
@@ -780,7 +878,7 @@ async function parseClaudeSession(session) {
780
878
  if (!Array.isArray(content)) continue;
781
879
  let typed = false;
782
880
  for (const part of content.map(asRecord)) {
783
- if (part?.type === "text" && str(part.text)) {
881
+ if (part?.type === "text" && str2(part.text)) {
784
882
  collector.push("user", part.text, at, spoken);
785
883
  typed = true;
786
884
  if (index === 0) out.title ??= titleOf(part.text);
@@ -789,13 +887,13 @@ async function parseClaudeSession(session) {
789
887
  }
790
888
  }
791
889
  if (typed && index === 0) out.userTurns += 1;
792
- } else if (record2.type === "assistant") {
890
+ } else if (record.type === "assistant") {
793
891
  const content = Array.isArray(message.content) ? message.content.map(asRecord) : [];
794
892
  for (const part of content) {
795
- if (part?.type === "text") collector.push("assistant", str(part.text) ?? "", at, own);
893
+ if (part?.type === "text") collector.push("assistant", str2(part.text) ?? "", at, own);
796
894
  else if (part?.type === "tool_use") {
797
895
  out.toolCalls += 1;
798
- collector.push("tool", `${str(part.name) ?? "tool"} ${stringify(part.input)}`, at);
896
+ collector.push("tool", `${str2(part.name) ?? "tool"} ${stringify(part.input)}`, at);
799
897
  }
800
898
  }
801
899
  }
@@ -811,30 +909,30 @@ async function parseCodexSession(session) {
811
909
  for await (const line of readLines(session.files[0])) {
812
910
  if (!line.trim()) continue;
813
911
  references.scan(line);
814
- let record2;
912
+ let record;
815
913
  try {
816
- record2 = asRecord(JSON.parse(line));
914
+ record = asRecord(JSON.parse(line));
817
915
  } catch {
818
916
  out.partial = true;
819
917
  continue;
820
918
  }
821
- source.push(record2);
822
- const payload = asRecord(record2?.payload);
823
- if (!record2 || !payload) continue;
824
- const at = isoOrUndefined(record2.timestamp);
919
+ source.push(record);
920
+ const payload = asRecord(record?.payload);
921
+ if (!record || !payload) continue;
922
+ const at = isoOrUndefined(record.timestamp);
825
923
  if (at) {
826
924
  out.startedAt ??= at;
827
925
  out.endedAt = at;
828
926
  }
829
- if (record2.type === "session_meta") {
830
- out.cwd ??= str(payload.cwd);
831
- } else if (record2.type === "turn_context") {
832
- out.cwd ??= str(payload.cwd);
833
- } else if (record2.type === "response_item") {
927
+ if (record.type === "session_meta") {
928
+ out.cwd ??= str2(payload.cwd);
929
+ } else if (record.type === "turn_context") {
930
+ out.cwd ??= str2(payload.cwd);
931
+ } else if (record.type === "response_item") {
834
932
  if (payload.type === "message") {
835
933
  const role = payload.role === "user" ? "user" : payload.role === "assistant" ? "assistant" : null;
836
934
  if (!role) continue;
837
- const text = (Array.isArray(payload.content) ? payload.content : []).map((part) => str(asRecord(part)?.text) ?? "").filter(Boolean).join("\n");
935
+ const text = (Array.isArray(payload.content) ? payload.content : []).map((part) => str2(asRecord(part)?.text) ?? "").filter(Boolean).join("\n");
838
936
  if (role === "user" && HARNESS_PREAMBLES.some((prefix) => text.trimStart().startsWith(prefix))) continue;
839
937
  collector.push(role, text, at);
840
938
  if (role === "user" && text.trim()) {
@@ -843,10 +941,10 @@ async function parseCodexSession(session) {
843
941
  }
844
942
  } else if (payload.type === "function_call" || payload.type === "custom_tool_call" || payload.type === "local_shell_call") {
845
943
  out.toolCalls += 1;
846
- collector.push("tool", `${str(payload.name) ?? payload.type} ${stringify(payload.arguments ?? payload.input ?? payload.action)}`, at);
944
+ collector.push("tool", `${str2(payload.name) ?? payload.type} ${stringify(payload.arguments ?? payload.input ?? payload.action)}`, at);
847
945
  } else if (payload.type === "function_call_output" || payload.type === "custom_tool_call_output") {
848
946
  const output = asRecord(payload.output);
849
- collector.push("tool", str(output?.content) ?? stringify(payload.output), at);
947
+ collector.push("tool", str2(output?.content) ?? stringify(payload.output), at);
850
948
  }
851
949
  }
852
950
  }
@@ -895,13 +993,13 @@ function isTrivial(session) {
895
993
  return session.toolCalls === 0 && session.messages.length <= 2 && chars < 200;
896
994
  }
897
995
  function sessionDigest(session) {
898
- const hash = createHash3("sha256");
996
+ const hash = createHash2("sha256");
899
997
  hash.update(`${session.cwd ?? ""}${session.model ?? ""}`);
900
998
  for (const message of session.messages) hash.update(`${message.role}\0${message.text}`);
901
999
  return hash.digest("hex");
902
1000
  }
903
1001
  function projectOf(cwd) {
904
- const parts = cwd.split(sep).filter(Boolean);
1002
+ const parts = cwd.split(sep2).filter(Boolean);
905
1003
  for (const marker of [".worktrees", "worktrees"]) {
906
1004
  const at = parts.lastIndexOf(marker);
907
1005
  if (at > 0) {
@@ -910,42 +1008,42 @@ function projectOf(cwd) {
910
1008
  }
911
1009
  }
912
1010
  let dir = cwd;
913
- while (dir && dir !== dirname4(dir)) {
914
- if (existsSync4(join3(dir, ".git"))) return basename3(dir);
915
- dir = dirname4(dir);
1011
+ while (dir && dir !== dirname3(dir)) {
1012
+ if (existsSync2(join2(dir, ".git"))) return basename2(dir);
1013
+ dir = dirname3(dir);
916
1014
  }
917
1015
  return parts.at(-1);
918
1016
  }
919
1017
  function workspaceFile(cwd) {
920
1018
  let dir = cwd;
921
- while (dir && dir !== dirname4(dir)) {
922
- const file = join3(dir, ".agentsky.json");
923
- if (existsSync4(file)) {
924
- const workspace = str(readJson(file, {}).workspace);
1019
+ while (dir && dir !== dirname3(dir)) {
1020
+ const file = join2(dir, ".agentsky.json");
1021
+ if (existsSync2(file)) {
1022
+ const workspace = str2(readJson(file, {}).workspace);
925
1023
  if (workspace) return workspace;
926
1024
  }
927
- if (existsSync4(join3(dir, ".git"))) return void 0;
928
- dir = dirname4(dir);
1025
+ if (existsSync2(join2(dir, ".git"))) return void 0;
1026
+ dir = dirname3(dir);
929
1027
  }
930
1028
  return void 0;
931
1029
  }
932
1030
  function gitConfigPath(root) {
933
- const dotGit = join3(root, ".git");
1031
+ const dotGit = join2(root, ".git");
934
1032
  try {
935
- if (statSync3(dotGit).isDirectory()) return join3(dotGit, "config");
1033
+ if (statSync2(dotGit).isDirectory()) return join2(dotGit, "config");
936
1034
  const pointer = readFileSync2(dotGit, "utf8").match(/^gitdir:\s*(.+)$/m)?.[1]?.trim();
937
1035
  if (!pointer) return void 0;
938
- const gitdir = pointer.startsWith("/") ? pointer : join3(root, pointer);
939
- const common = existsSync4(join3(gitdir, "commondir")) ? readFileSync2(join3(gitdir, "commondir"), "utf8").trim() : ".";
940
- return join3(common.startsWith("/") ? common : join3(gitdir, common), "config");
1036
+ const gitdir = pointer.startsWith("/") ? pointer : join2(root, pointer);
1037
+ const common = existsSync2(join2(gitdir, "commondir")) ? readFileSync2(join2(gitdir, "commondir"), "utf8").trim() : ".";
1038
+ return join2(common.startsWith("/") ? common : join2(gitdir, common), "config");
941
1039
  } catch {
942
1040
  return void 0;
943
1041
  }
944
1042
  }
945
1043
  function repoRemote(cwd) {
946
1044
  let dir = cwd;
947
- while (dir && dir !== dirname4(dir)) {
948
- if (existsSync4(join3(dir, ".git"))) {
1045
+ while (dir && dir !== dirname3(dir)) {
1046
+ if (existsSync2(join2(dir, ".git"))) {
949
1047
  const config = gitConfigPath(dir);
950
1048
  if (!config) return void 0;
951
1049
  try {
@@ -955,7 +1053,7 @@ function repoRemote(cwd) {
955
1053
  return void 0;
956
1054
  }
957
1055
  }
958
- dir = dirname4(dir);
1056
+ dir = dirname3(dir);
959
1057
  }
960
1058
  return void 0;
961
1059
  }
@@ -972,8 +1070,8 @@ ${repoRemote(cwd) ?? ""}`.toLowerCase();
972
1070
  }
973
1071
  function homeLabel(path) {
974
1072
  if (!path) return void 0;
975
- const home = homedir2();
976
- return path === home || path.startsWith(`${home}${sep}`) ? `~${path.slice(home.length)}` : path;
1073
+ const home = homedir3();
1074
+ return path === home || path.startsWith(`${home}${sep2}`) ? `~${path.slice(home.length)}` : path;
977
1075
  }
978
1076
  async function libraryRequest(method, path, workspace, body, timeoutMs = 12e4) {
979
1077
  const token = apiToken();
@@ -1046,15 +1144,15 @@ function uploadBody(session, sha256, device) {
1046
1144
  };
1047
1145
  }
1048
1146
  function lockPath() {
1049
- return join3(skyDir(), "trace-sync.lock");
1147
+ return join2(skyDir(), "trace-sync.lock");
1050
1148
  }
1051
1149
  function takeLock() {
1052
1150
  const path = lockPath();
1053
- mkdirSync3(skyDir(), { recursive: true });
1151
+ mkdirSync(skyDir(), { recursive: true });
1054
1152
  try {
1055
- mkdirSync3(path);
1153
+ mkdirSync(path);
1056
1154
  } catch {
1057
- const pid = Number(readJson(join3(path, "pid.json"), {}).pid);
1155
+ const pid = Number(readJson(join2(path, "pid.json"), {}).pid);
1058
1156
  let alive = false;
1059
1157
  try {
1060
1158
  if (pid) {
@@ -1065,18 +1163,18 @@ function takeLock() {
1065
1163
  alive = false;
1066
1164
  }
1067
1165
  if (alive) return false;
1068
- rmSync3(path, { recursive: true, force: true });
1166
+ rmSync(path, { recursive: true, force: true });
1069
1167
  try {
1070
- mkdirSync3(path);
1168
+ mkdirSync(path);
1071
1169
  } catch {
1072
1170
  return false;
1073
1171
  }
1074
1172
  }
1075
- writeJson(join3(path, "pid.json"), { pid: process.pid });
1173
+ writeJson(join2(path, "pid.json"), { pid: process.pid });
1076
1174
  return true;
1077
1175
  }
1078
1176
  function releaseLock() {
1079
- rmSync3(lockPath(), { recursive: true, force: true });
1177
+ rmSync(lockPath(), { recursive: true, force: true });
1080
1178
  }
1081
1179
  async function pool(items, size, fn) {
1082
1180
  let next = 0;
@@ -1106,7 +1204,7 @@ async function verifyPendingFile(item, source) {
1106
1204
  response = await fetch(url, { redirect: "manual", headers: { "user-agent": USER_AGENT } });
1107
1205
  }
1108
1206
  if (!response.ok || !response.body) throw new Error(`Cannot verify stored source file (${response.status}).`);
1109
- const hash = createHash3("sha256");
1207
+ const hash = createHash2("sha256");
1110
1208
  let bytes = 0;
1111
1209
  const reader = response.body.getReader();
1112
1210
  try {
@@ -1125,22 +1223,42 @@ async function runSync(config, options) {
1125
1223
  const state = readJson(statePath(), { sessions: {} });
1126
1224
  const full = options.full || !state.lastFullAt || Date.now() - Date.parse(state.lastFullAt) > FULL_SWEEP_MS;
1127
1225
  const device = config.device ?? hostname();
1128
- const report = { scanned: 0, changed: 0, uploaded: 0, unchanged: 0, local: 0, owned: 0, empty: 0, sessions: 0, failed: 0, legacySources: 0, byWorkspace: {}, errors: [] };
1226
+ const report = { scanned: 0, changed: 0, uploaded: 0, unchanged: 0, local: 0, owned: 0, empty: 0, sessions: 0, excluded: 0, failed: 0, results: 0, legacySources: 0, byWorkspace: {}, errors: [] };
1227
+ const results = readJson(resultsStatePath(), { read: [], saved: [] });
1228
+ const saveResults = async (local, workspace, sessionId, cwd) => {
1229
+ if (!sessionHarness(local.harness)) return;
1230
+ try {
1231
+ report.results += (await captureSessionResults({ harness: local.harness, nativeId: local.sessionId, sessionId, workspace, files: local.files, cwd: cwd?.replace(/^~(?=\/|$)/, homedir3()) }, results)).saved;
1232
+ } catch (error) {
1233
+ (results.tries ??= {})[key(local)] = (results.tries[key(local)] ?? 0) + 1;
1234
+ report.errors.push(`${key(local)} \u2192 ${workspace}: results not saved yet: ${error.message}`);
1235
+ } finally {
1236
+ writePrivateJson(resultsStatePath(), results);
1237
+ }
1238
+ };
1129
1239
  const key = (s) => `${s.harness}:${s.sessionId}`;
1130
1240
  const selected = (s) => !options.only?.length || options.only.includes(s.sessionId) || s.files.some((file) => options.only.includes(file));
1131
- const record2 = (s, value) => {
1241
+ const record = (s, value) => {
1132
1242
  state.sessions[key(s)] = { captureVersion: SOURCE_CAPTURE_VERSION, signature: s.signature, at: (/* @__PURE__ */ new Date()).toISOString(), ...value };
1133
1243
  };
1134
1244
  const fail = (label, error) => {
1135
1245
  report.failed++;
1136
1246
  report.errors.push(`${label}: ${error.message}`);
1137
1247
  };
1248
+ const exclusions = loadExclusions(skyDir());
1249
+ const leftOut = (local, cwd) => {
1250
+ const hit = excludedBy(exclusions, { harness: local.harness, sessionId: local.sessionId, cwd });
1251
+ if (!hit) return false;
1252
+ report.excluded++;
1253
+ record(local, { workspace: null, status: "excluded", excludedBy: hit.by });
1254
+ return true;
1255
+ };
1138
1256
  try {
1139
- report.legacySources = readdirSync2(join3(skyDir(), "trace-sources")).filter((name) => /^[a-f0-9]{64}\.jsonl$/.test(name)).length;
1257
+ report.legacySources = readdirSync(join2(skyDir(), "trace-sources")).filter((name) => /^[a-f0-9]{64}\.jsonl$/.test(name)).length;
1140
1258
  } catch {
1141
1259
  }
1142
1260
  if (report.legacySources) options.onProgress?.(`${report.legacySources} unbound legacy source archives remain local; they are not uploaded or removed.`);
1143
- const queueRoot = join3(skyDir(), "trace-pending");
1261
+ const queueRoot = join2(skyDir(), "trace-pending");
1144
1262
  const endpoint = canonicalSourceApiBase(apiBase());
1145
1263
  const restored = /* @__PURE__ */ new Map();
1146
1264
  let blocked = false;
@@ -1153,6 +1271,13 @@ async function runSync(config, options) {
1153
1271
  const { workspace } = item.manifest;
1154
1272
  let capturing = false;
1155
1273
  let { local } = item.manifest;
1274
+ const queuedCwd = typeof item.manifest.body.cwd === "string" ? item.manifest.body.cwd : void 0;
1275
+ if (leftOut(local, queuedCwd)) {
1276
+ writePrivateJson(statePath(), state);
1277
+ removePendingSource(item);
1278
+ restored.set(key(local), local.signature);
1279
+ return;
1280
+ }
1156
1281
  try {
1157
1282
  if (item.manifest.apiBase !== endpoint) throw new Error("Pending source belongs to another API endpoint; restore that endpoint before syncing new sources.");
1158
1283
  let source;
@@ -1197,15 +1322,21 @@ async function runSync(config, options) {
1197
1322
  harness: local.harness,
1198
1323
  sessionId: local.sessionId,
1199
1324
  workspace,
1200
- body: { ...body, messages: body.messages.filter((_, index) => !left.has(index)) }
1325
+ body: { ...body, messages: body.messages.filter((_, index) => !left.has(index)) },
1326
+ skills: loadLocalSkillState()
1201
1327
  });
1202
1328
  if (saved2.status === "saved") report.sessions++;
1329
+ if (saved2.status === "saved" || saved2.status === "elsewhere") await saveResults(local, saved2.status === "elsewhere" ? saved2.workspace : workspace, saved2.sessionId, body.cwd);
1203
1330
  else if (saved2.status === "refused") options.onProgress?.(`${key(local)}: kept in the trace library only (${saved2.code})`);
1331
+ if (saved2.status !== "saved" && saved2.status !== "elsewhere" && !results.read.includes(key(local))) {
1332
+ results.read.push(key(local));
1333
+ writePrivateJson(resultsStatePath(), results);
1334
+ }
1204
1335
  }
1205
1336
  const saved = await libraryRequest("PUT", `/trace-library/${local.harness}/${encodeURIComponent(local.sessionId)}`, workspace, { ...previewBody, sha256: source.sha256 });
1206
1337
  savePendingSource(item, { previewSaved: true, link: saved.linked_task ?? null });
1207
1338
  const link = item.manifest.link;
1208
- const linkKey = createHash3("sha256").update(JSON.stringify(link ?? null)).digest("hex").slice(0, 16);
1339
+ const linkKey = createHash2("sha256").update(JSON.stringify(link ?? null)).digest("hex").slice(0, 16);
1209
1340
  await libraryRequest("POST", "/events", workspace, { events: [{
1210
1341
  idempotency_key: `trace-source:${source.sha256}:${linkKey}`,
1211
1342
  ...link ? { session_id: link.session_id, task_id: link.task_id } : {},
@@ -1228,7 +1359,7 @@ async function runSync(config, options) {
1228
1359
  },
1229
1360
  bodies: [{ role: "transcript", media_type: "application/x-ndjson", file_id: item.manifest.fileId, complete: source.complete }]
1230
1361
  }] });
1231
- record2(local, { workspace, sha256: source.sha256, status: source.complete ? "stored" : "stored_with_gaps" });
1362
+ record(local, { workspace, sha256: source.sha256, status: source.complete ? "stored" : "stored_with_gaps" });
1232
1363
  writePrivateJson(statePath(), state);
1233
1364
  removePendingSource(item);
1234
1365
  restored.set(key(local), local.signature);
@@ -1258,25 +1389,35 @@ async function runSync(config, options) {
1258
1389
  const direct = options.only?.length ? options.only.map(claudeSessionAt) : [];
1259
1390
  const sessions = (direct.length && direct.every(Boolean) ? direct : await discover()).filter(selected);
1260
1391
  report.scanned = Math.max(report.scanned, sessions.length);
1261
- let pending = sessions.filter((s) => !unreadable.has(key(s)) && restored.get(key(s)) !== s.signature && (full || state.sessions[key(s)]?.captureVersion !== SOURCE_CAPTURE_VERSION || state.sessions[key(s)]?.signature !== s.signature));
1392
+ const lifted = (s) => state.sessions[key(s)]?.status === "excluded" && !stillExcluded(exclusions, state.sessions[key(s)]?.excludedBy ?? "");
1393
+ const awaitingResults = (s) => config.sessions !== false && sessionHarness(s.harness) && !results.read.includes(key(s)) && ["stored", "stored_with_gaps"].includes(state.sessions[key(s)]?.status ?? "") && (results.tries?.[key(s)] ?? 0) < RESULTS_TRIES && s.files.some((file) => {
1394
+ try {
1395
+ return Date.now() - statSync2(file).mtimeMs < RESULTS_BACKFILL_MS;
1396
+ } catch {
1397
+ return false;
1398
+ }
1399
+ });
1400
+ let pending = sessions.filter((s) => !unreadable.has(key(s)) && restored.get(key(s)) !== s.signature && (full || lifted(s) || state.sessions[key(s)]?.captureVersion !== SOURCE_CAPTURE_VERSION || state.sessions[key(s)]?.signature !== s.signature || awaitingResults(s)));
1262
1401
  if (options.limit) pending = pending.slice(0, options.limit);
1263
1402
  report.changed += pending.length;
1264
1403
  const prepare = async (local) => {
1265
1404
  if (local.trace) {
1405
+ if (leftOut(local, local.trace.cwd)) return null;
1266
1406
  const workspace2 = routeSession(local.trace.cwd, config);
1267
1407
  if (!workspace2) {
1268
1408
  report.local++;
1269
- record2(local, { workspace: null, status: "local" });
1409
+ record(local, { workspace: null, status: "local" });
1270
1410
  return null;
1271
1411
  }
1272
1412
  scrubber ??= (await traceCollector()).collector.scrubberFor();
1273
1413
  return { workspace: workspace2, body: await collectorBody(local.trace, device, scrubber) };
1274
1414
  }
1275
1415
  const parsed = local.harness === "claude_code" ? await parseClaudeSession(local) : await parseCodexSession(local);
1416
+ if (leftOut(local, parsed.cwd)) return null;
1276
1417
  const workspace = routeSession(parsed.cwd, config);
1277
1418
  if (!workspace) {
1278
1419
  report.local++;
1279
- record2(local, { workspace: null, status: "local" });
1420
+ record(local, { workspace: null, status: "local" });
1280
1421
  return null;
1281
1422
  }
1282
1423
  return { workspace, body: uploadBody(parsed, sessionDigest(parsed), device) };
@@ -1288,7 +1429,7 @@ async function runSync(config, options) {
1288
1429
  const prepared = await prepare(local);
1289
1430
  if (!prepared) return;
1290
1431
  if (options.dryRun) {
1291
- const directory = mkdtempSync(join3(skyDir(), "trace-dry-run-"));
1432
+ const directory = mkdtempSync(join2(skyDir(), "trace-dry-run-"));
1292
1433
  try {
1293
1434
  const source2 = await captureTraceSource(local, directory, { workspace: prepared.workspace, apiBase: endpoint });
1294
1435
  const checked = await libraryRequest("POST", "/trace-library/check", prepared.workspace, { sessions: [{ harness: local.harness, session_id: local.sessionId, sha256: source2.sha256 }] });
@@ -1299,7 +1440,7 @@ async function runSync(config, options) {
1299
1440
  report.uploaded++;
1300
1441
  report.byWorkspace[prepared.workspace] = (report.byWorkspace[prepared.workspace] ?? 0) + 1;
1301
1442
  } finally {
1302
- rmSync3(directory, { recursive: true, force: true });
1443
+ rmSync(directory, { recursive: true, force: true });
1303
1444
  }
1304
1445
  return;
1305
1446
  }
@@ -1329,14 +1470,14 @@ async function runSync(config, options) {
1329
1470
  function hookSessionRef(payload) {
1330
1471
  try {
1331
1472
  const value = asRecord(JSON.parse(payload));
1332
- return str(value?.transcript_path) ?? str(value?.session_id) ?? str(value?.["thread-id"]) ?? str(value?.thread_id);
1473
+ return str2(value?.transcript_path) ?? str2(value?.session_id) ?? str2(value?.["thread-id"]) ?? str2(value?.thread_id);
1333
1474
  } catch {
1334
1475
  return void 0;
1335
1476
  }
1336
1477
  }
1337
1478
  var LAUNCHD_LABEL = "dev.agentsky.trace-sync";
1338
- function launchdPlistPath(home = homedir2()) {
1339
- return join3(home, "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
1479
+ function launchdPlistPath(home = homedir3()) {
1480
+ return join2(home, "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
1340
1481
  }
1341
1482
  function launchdPlist(command, logPath) {
1342
1483
  const escape = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
@@ -1361,21 +1502,43 @@ ${command.map((part) => ` <string>${escape(part)}</string>`).join("\n")}
1361
1502
  function installLaunchd(command, logPath) {
1362
1503
  if (process.platform !== "darwin") throw new Error("the schedule installer covers macOS; on Linux add `*/15 * * * * sky trace sync --quiet` to your crontab");
1363
1504
  const plist = launchdPlistPath();
1364
- mkdirSync3(dirname4(plist), { recursive: true });
1365
- writeFileSync2(plist, launchdPlist(command, logPath));
1505
+ mkdirSync(dirname3(plist), { recursive: true });
1506
+ writeFileSync(plist, launchdPlist(command, logPath));
1366
1507
  return plist;
1367
1508
  }
1509
+ var SYNC_HOOK_COMMAND = "sky trace sync --hook";
1510
+ function installSyncHooks(home = homedir3(), codexHome = process.env.CODEX_HOME || join2(home, ".codex")) {
1511
+ const lines = [];
1512
+ const claudeSettings = join2(home, ".claude", "settings.json");
1513
+ installClaudeHooks(claudeSettings, SYNC_HOOK_COMMAND, { Stop: [{ hooks: [{ type: "command", command: SYNC_HOOK_COMMAND, timeout: 10 }] }] });
1514
+ lines.push(`Claude Code: syncs at the end of every turn (${claudeSettings}).`);
1515
+ const codexConfig = join2(codexHome, "config.toml");
1516
+ const text = existsSync2(codexConfig) ? readFileSync2(codexConfig, "utf8") : "";
1517
+ const topLevel = text.split(/^\s*\[/m, 1)[0] ?? "";
1518
+ const notify = /^\s*notify\s*=.*$/m.exec(topLevel)?.[0];
1519
+ if (notify && !notify.includes("trace")) lines.push(`Codex: left as it is, because ${codexConfig} already has ${notify.trim()}; the 15-minute schedule (--install-schedule) still picks its sessions up.`);
1520
+ else if (notify) lines.push(`Codex: already syncs at the end of every turn (${codexConfig}).`);
1521
+ else {
1522
+ mkdirSync(dirname3(codexConfig), { recursive: true });
1523
+ writeFileSync(codexConfig, `notify = ["sky", "trace", "sync", "--hook"]
1524
+ ${text}`);
1525
+ lines.push(`Codex: syncs at the end of every turn (${codexConfig}).`);
1526
+ }
1527
+ return lines;
1528
+ }
1368
1529
  function syncDir() {
1369
1530
  return skyDir();
1370
1531
  }
1371
1532
  export {
1372
1533
  LAUNCHD_LABEL,
1534
+ SYNC_HOOK_COMMAND,
1373
1535
  collectorBody,
1374
1536
  collectorHarness,
1375
1537
  discoverSessions,
1376
1538
  hookSessionRef,
1377
1539
  importExport,
1378
1540
  installLaunchd,
1541
+ installSyncHooks,
1379
1542
  isTrivial,
1380
1543
  launchdPlist,
1381
1544
  launchdPlistPath,