@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.
@@ -0,0 +1,404 @@
1
+ import {
2
+ UnsafeImportError,
3
+ redactLine
4
+ } from "./chunk-BIZJ4VZL.js";
5
+
6
+ // src/trace-exclusions.ts
7
+ import { existsSync as existsSync3, readFileSync as readFileSync2 } from "fs";
8
+ import { homedir } from "os";
9
+ import { join as join3, resolve as resolve2 } from "path";
10
+
11
+ // src/trace-pending.ts
12
+ import { createHash as createHash2, randomBytes } from "crypto";
13
+ import { createReadStream as createReadStream2, closeSync as closeSync2, existsSync as existsSync2, 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";
14
+ import { basename as basename2, dirname as dirname2, join as join2 } from "path";
15
+
16
+ // src/trace-source.ts
17
+ import { createHash } from "crypto";
18
+ import { createReadStream, createWriteStream, closeSync, existsSync, fsyncSync, mkdirSync, openSync, realpathSync, renameSync, rmSync, statSync } from "fs";
19
+ import { once } from "events";
20
+ import { basename, dirname, join, resolve } from "path";
21
+ import { createGunzip } from "zlib";
22
+ var SOURCE_CAPTURE_VERSION = 2;
23
+ function canonicalSourceApiBase(value) {
24
+ const url = new URL(value);
25
+ url.username = "";
26
+ url.password = "";
27
+ url.search = "";
28
+ url.hash = "";
29
+ return url.toString().replace(/\/$/, "");
30
+ }
31
+ async function captureTraceSource(session, directory, context) {
32
+ const originals = session.trace?.source_files;
33
+ const files = originals?.length ? originals : session.files;
34
+ for (const file of files) {
35
+ const allowed = session.files.some((collected) => resolve(file) === resolve(collected) || resolve(file) === resolve(collected.replace(/\.json$/, ".source.jsonl")) && dirname(realpathSync(file)) === dirname(realpathSync(collected)));
36
+ if (!allowed) throw new Error("Source reference is outside this collected session.");
37
+ }
38
+ const gaps = [...session.trace?.source_gaps ?? [], ...session.trace && !originals?.length ? ["Only the collector-normalized export is available; original source records were not retained."] : []];
39
+ mkdirSync(directory, { recursive: true, mode: 448 });
40
+ const key = createHash("sha256").update(`${session.harness}\0${session.sessionId}`).digest("hex");
41
+ const temporary = join(directory, `${key}.pending`);
42
+ const output = createWriteStream(temporary, { mode: 384 });
43
+ let streamError;
44
+ output.on("error", (error) => {
45
+ streamError = error;
46
+ });
47
+ const hash = createHash("sha256");
48
+ let bytes = 0, records = 0;
49
+ const write = async (value) => {
50
+ const line = `${JSON.stringify(value)}
51
+ `;
52
+ hash.update(line);
53
+ bytes += Buffer.byteLength(line);
54
+ if (!output.write(line)) await once(output, "drain");
55
+ if (streamError) throw streamError;
56
+ };
57
+ const redact = (text) => {
58
+ const safe = redactLine(text, { requireStructured: true, preserveTraceSessionIds: true, expectedTraceSessionId: session.sessionId }).line;
59
+ JSON.parse(safe);
60
+ return safe;
61
+ };
62
+ let omitted = 0;
63
+ const writeRecord = async (sourceFile, text) => {
64
+ let safe;
65
+ try {
66
+ safe = redact(text);
67
+ } catch (error) {
68
+ if (!(error instanceof UnsafeImportError)) throw error;
69
+ omitted++;
70
+ await write({ source_file: sourceFile, omitted_record: { reason: "redaction_not_provable" } });
71
+ return;
72
+ }
73
+ await write({ source_file: sourceFile, record_json: safe });
74
+ records++;
75
+ };
76
+ try {
77
+ await write({
78
+ format: `agentsky.trace-source/v${SOURCE_CAPTURE_VERSION}`,
79
+ record_encoding: "json-text",
80
+ harness: session.harness,
81
+ session_id: session.sessionId,
82
+ ...context ? { workspace: context.workspace, api_base: canonicalSourceApiBase(context.apiBase), source_signature: session.signature } : {},
83
+ redacted_before_upload: true,
84
+ representation: session.trace && !originals?.length ? "collector-normalized" : "native",
85
+ source_files: files.length,
86
+ gaps
87
+ });
88
+ for (const [index, file] of files.entries()) {
89
+ const before = statSync(file);
90
+ const sourceHash = createHash("sha256");
91
+ for await (const chunk of createReadStream(file)) sourceHash.update(chunk);
92
+ await write({ source_file: index, name: basename(file), source_sha256: sourceHash.digest("hex"), source_bytes: before.size });
93
+ const raw = createReadStream(file);
94
+ const input = file.endsWith(".gz") ? raw.pipe(createGunzip()) : raw;
95
+ const decoder = new TextDecoder("utf-8", { fatal: true });
96
+ async function* textChunks() {
97
+ for await (const chunk of input) yield decoder.decode(chunk, { stream: true });
98
+ yield decoder.decode();
99
+ }
100
+ try {
101
+ if (/\.json(?:\.gz)?$/.test(file)) {
102
+ let text = "";
103
+ for await (const chunk of textChunks()) text += chunk;
104
+ await writeRecord(index, text);
105
+ } else {
106
+ let pending = "";
107
+ const recordLine = async (line) => {
108
+ if (!line.trim()) return;
109
+ await writeRecord(index, line);
110
+ };
111
+ for await (const chunk of textChunks()) {
112
+ pending += chunk;
113
+ let newline;
114
+ while ((newline = pending.indexOf("\n")) !== -1) {
115
+ await recordLine(pending.slice(0, newline));
116
+ pending = pending.slice(newline + 1);
117
+ }
118
+ }
119
+ await recordLine(pending);
120
+ }
121
+ } finally {
122
+ raw.destroy();
123
+ input.destroy();
124
+ }
125
+ const after = statSync(file);
126
+ if (before.size !== after.size || before.mtimeMs !== after.mtimeMs) throw new Error("Transcript changed while capturing; retry on the next sync.");
127
+ }
128
+ if (omitted) gaps.push(`${omitted} record${omitted === 1 ? "" : "s"} omitted: redaction could not be proven safe.`);
129
+ await write({ capture_summary: { records, complete: gaps.length === 0, gaps } });
130
+ output.end();
131
+ await once(output, "finish");
132
+ const descriptor = openSync(temporary, "r");
133
+ try {
134
+ fsyncSync(descriptor);
135
+ } finally {
136
+ closeSync(descriptor);
137
+ }
138
+ const sha256 = hash.digest("hex");
139
+ const path = join(directory, `${sha256}.jsonl`);
140
+ if (existsSync(path)) rmSync(temporary);
141
+ else renameSync(temporary, path);
142
+ const parent = openSync(directory, "r");
143
+ try {
144
+ fsyncSync(parent);
145
+ } finally {
146
+ closeSync(parent);
147
+ }
148
+ return { path, sha256, bytes, records, sourceFiles: files.length, complete: gaps.length === 0, gaps };
149
+ } catch (error) {
150
+ output.destroy();
151
+ rmSync(temporary, { force: true });
152
+ throw error;
153
+ }
154
+ }
155
+
156
+ // src/trace-pending.ts
157
+ var IncompleteSourceCaptureError = class extends Error {
158
+ };
159
+ var digest = /^[a-f0-9]{64}$/;
160
+ var record = (value) => !!value && typeof value === "object" && !Array.isArray(value);
161
+ var strings = (value) => Array.isArray(value) && value.every((v) => typeof v === "string");
162
+ var natural = (value) => Number.isSafeInteger(value) && Number(value) >= 0;
163
+ var keyOf = (manifest) => createHash2("sha256").update(JSON.stringify([manifest.apiBase, manifest.workspace, manifest.local.harness, manifest.local.sessionId])).digest("hex");
164
+ function writePrivateJson(path, value) {
165
+ mkdirSync2(dirname2(path), { recursive: true, mode: 448 });
166
+ if (existsSync2(path) && lstatSync(path).isSymbolicLink()) throw new Error("Refusing a symlink metadata file.");
167
+ const temporary = `${path}.${randomBytes(8).toString("hex")}.pending`;
168
+ try {
169
+ const fd = openSync2(temporary, "wx", 384);
170
+ try {
171
+ writeFileSync(fd, `${JSON.stringify(value)}
172
+ `);
173
+ fsyncSync2(fd);
174
+ } finally {
175
+ closeSync2(fd);
176
+ }
177
+ renameSync2(temporary, path);
178
+ const directory = openSync2(dirname2(path), "r");
179
+ try {
180
+ fsyncSync2(directory);
181
+ } finally {
182
+ closeSync2(directory);
183
+ }
184
+ } catch (error) {
185
+ rmSync2(temporary, { force: true });
186
+ throw error;
187
+ }
188
+ }
189
+ function rootPath(root) {
190
+ mkdirSync2(root, { recursive: true, mode: 448 });
191
+ if (lstatSync(root).isSymbolicLink()) throw new Error("Source queue must not be a symlink.");
192
+ return realpathSync2(root);
193
+ }
194
+ function validateManifest(value) {
195
+ 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.");
196
+ if (value.apiBase !== canonicalSourceApiBase(value.apiBase)) throw new Error("Pending endpoint identity is not canonical.");
197
+ 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.");
198
+ if (value.previewSaved !== void 0 && typeof value.previewSaved !== "boolean") throw new Error("Invalid pending preview receipt.");
199
+ 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.");
200
+ 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.");
201
+ if (value.stage === "ready" && !value.source) throw new Error("Pending source has no capture receipt.");
202
+ }
203
+ function listPendingSources(root) {
204
+ if (!existsSync2(root)) return [];
205
+ const canonical = rootPath(root);
206
+ const keys = readdirSync(canonical).sort();
207
+ for (const key of keys.filter((name) => /^\.done-[a-f0-9]{64}-[a-f0-9]{16}$/.test(name))) {
208
+ const path = join2(canonical, key);
209
+ if (lstatSync(path).isSymbolicLink() || dirname2(realpathSync2(path)) !== canonical) throw new Error("Unsafe completed source directory.");
210
+ rmSync2(path, { recursive: true });
211
+ }
212
+ return keys.filter((key) => !/^\.done-[a-f0-9]{64}-[a-f0-9]{16}$/.test(key)).map((key) => {
213
+ if (!digest.test(key)) throw new Error("Unexpected entry in the private source queue.");
214
+ const directory = join2(canonical, key);
215
+ if (!lstatSync(directory).isDirectory() || lstatSync(directory).isSymbolicLink() || dirname2(realpathSync2(directory)) !== canonical) throw new Error("Unsafe pending source directory.");
216
+ const path = join2(directory, "manifest.json");
217
+ if (!existsSync2(path) || !lstatSync(path).isFile() || lstatSync(path).isSymbolicLink()) throw new Error("Pending source is missing a safe descriptor.");
218
+ const manifest = JSON.parse(readFileSync(path, "utf8"));
219
+ validateManifest(manifest);
220
+ if (key !== keyOf(manifest)) throw new Error("Pending source routing does not match its directory.");
221
+ return { directory, manifest };
222
+ });
223
+ }
224
+ function beginPendingSource(root, manifest) {
225
+ const data = { ...manifest, version: 1, stage: "capturing" };
226
+ validateManifest(data);
227
+ const directory = join2(rootPath(root), keyOf(data));
228
+ mkdirSync2(directory, { mode: 448 });
229
+ const pending = { directory, manifest: data };
230
+ try {
231
+ savePendingSource(pending, {});
232
+ } catch (error) {
233
+ rmSync2(directory, { recursive: true, force: true });
234
+ throw error;
235
+ }
236
+ return pending;
237
+ }
238
+ function savePendingSource(pending, patch) {
239
+ const manifest = { ...pending.manifest, ...patch };
240
+ validateManifest(manifest);
241
+ if (basename2(pending.directory) !== keyOf(manifest)) throw new Error("Pending source identity cannot be changed.");
242
+ writePrivateJson(join2(pending.directory, "manifest.json"), manifest);
243
+ pending.manifest = manifest;
244
+ }
245
+ async function recoverPendingSource(pending) {
246
+ const { manifest, directory } = pending;
247
+ if (lstatSync(directory).isSymbolicLink()) throw new Error("Unsafe pending source directory.");
248
+ const names = readdirSync(directory).filter((name2) => /^[a-f0-9]{64}\.jsonl$/.test(name2));
249
+ const name = manifest.source ? `${manifest.source.sha256}.jsonl` : names.length === 1 ? names[0] : void 0;
250
+ if (!name || names.length !== 1) throw new IncompleteSourceCaptureError("Source capture was interrupted before a complete archive was finalized; pending data retained.");
251
+ const path = join2(directory, name);
252
+ const before = lstatSync(path);
253
+ if (!before.isFile() || before.isSymbolicLink() || dirname2(realpathSync2(path)) !== realpathSync2(directory)) throw new Error("Unsafe pending archive path.");
254
+ const hash = createHash2("sha256"), decoder = new TextDecoder("utf-8", { fatal: true });
255
+ let bytes = 0, records = 0, lineBuffer = "", header, summary;
256
+ const files = /* @__PURE__ */ new Set();
257
+ const line = (text) => {
258
+ if (!text) return;
259
+ const row = JSON.parse(text);
260
+ if (!record(row) || summary) throw new Error("Invalid pending archive record ordering.");
261
+ if (!header) {
262
+ header = row;
263
+ return;
264
+ }
265
+ if (record(row.capture_summary)) {
266
+ summary = row.capture_summary;
267
+ return;
268
+ }
269
+ if (!natural(row.source_file) || !natural(header.source_files) || row.source_file >= header.source_files) throw new Error("Invalid source file index.");
270
+ if (record(row.omitted_record) && files.has(row.source_file)) return;
271
+ if (typeof row.record_json === "string") {
272
+ if (!files.has(row.source_file)) throw new Error("Pending record precedes its source metadata.");
273
+ records++;
274
+ } else if (typeof row.source_sha256 === "string" && digest.test(row.source_sha256) && natural(row.source_bytes)) files.add(row.source_file);
275
+ else throw new Error("Invalid pending source record.");
276
+ };
277
+ for await (const chunk of createReadStream2(path)) {
278
+ hash.update(chunk);
279
+ bytes += chunk.length;
280
+ lineBuffer += decoder.decode(chunk, { stream: true });
281
+ let newline;
282
+ while ((newline = lineBuffer.indexOf("\n")) !== -1) {
283
+ line(lineBuffer.slice(0, newline));
284
+ lineBuffer = lineBuffer.slice(newline + 1);
285
+ }
286
+ }
287
+ lineBuffer += decoder.decode();
288
+ if (lineBuffer) line(lineBuffer);
289
+ const sha256 = hash.digest("hex");
290
+ const after = statSync2(path);
291
+ if (before.size !== after.size || before.mtimeMs !== after.mtimeMs || sha256 !== name.slice(0, -6)) throw new Error("Pending archive bytes or hash changed.");
292
+ 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.");
293
+ const source = { path, sha256, bytes, records, sourceFiles: header.source_files, complete: summary.complete, gaps: summary.gaps };
294
+ 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.");
295
+ return source;
296
+ }
297
+ function completePendingCapture(pending, source) {
298
+ const { path: _path, ...receipt } = source;
299
+ savePendingSource(pending, { stage: "ready", source: receipt });
300
+ }
301
+ function removePendingSource(pending) {
302
+ if (basename2(pending.directory) !== keyOf(pending.manifest) || lstatSync(pending.directory).isSymbolicLink()) throw new Error("Unsafe source cleanup path.");
303
+ const completed = join2(dirname2(pending.directory), `.done-${basename2(pending.directory)}-${randomBytes(8).toString("hex")}`);
304
+ renameSync2(pending.directory, completed);
305
+ const fd = openSync2(dirname2(completed), "r");
306
+ try {
307
+ fsyncSync2(fd);
308
+ } finally {
309
+ closeSync2(fd);
310
+ }
311
+ rmSync2(completed, { recursive: true });
312
+ }
313
+
314
+ // src/trace-exclusions.ts
315
+ var strings2 = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
316
+ function macSettingsPath() {
317
+ return process.env.SKY_MAC_SETTINGS ?? join3(homedir(), "Library", "Application Support", "AgentSky", "settings.json");
318
+ }
319
+ function ownListPath(configDir) {
320
+ return join3(configDir, "trace-exclusions.json");
321
+ }
322
+ function readObject(path) {
323
+ if (!existsSync3(path)) return null;
324
+ let value;
325
+ try {
326
+ value = JSON.parse(readFileSync2(path, "utf8"));
327
+ } catch {
328
+ throw new Error(`Cannot read the exclusions in ${path}; nothing is uploaded until it can be read.`);
329
+ }
330
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
331
+ }
332
+ function macList() {
333
+ const settings = readObject(macSettingsPath());
334
+ if (!settings) return null;
335
+ const sync = settings.localSync && typeof settings.localSync === "object" ? settings.localSync : {};
336
+ return { folders: strings2(sync.excludedFolders), chats: strings2(sync.excludedChats) };
337
+ }
338
+ function ownList(configDir) {
339
+ const saved = readObject(ownListPath(configDir)) ?? {};
340
+ return { folders: strings2(saved.folders), chats: strings2(saved.chats) };
341
+ }
342
+ function loadExclusions(configDir) {
343
+ const mac = macList();
344
+ const own = ownList(configDir);
345
+ return { folders: [.../* @__PURE__ */ new Set([...mac?.folders ?? [], ...own.folders])], chats: [.../* @__PURE__ */ new Set([...mac?.chats ?? [], ...own.chats])] };
346
+ }
347
+ function expand(path, home) {
348
+ return path === "~" ? home : path.startsWith("~/") ? join3(home, path.slice(2)) : path;
349
+ }
350
+ function inFolder(cwd, folder, home = homedir()) {
351
+ if (!cwd) return false;
352
+ const root = expand(folder, home).replace(/\/+$/, "");
353
+ const at = expand(cwd, home);
354
+ return Boolean(root) && (at === root || at.startsWith(`${root}/`));
355
+ }
356
+ function excludedBy(exclusions, session) {
357
+ const key = `${session.harness}:${session.sessionId}`;
358
+ if (exclusions.chats.includes(key)) return { by: key };
359
+ const folder = exclusions.folders.find((entry) => inFolder(session.cwd, entry));
360
+ return folder ? { by: folder } : null;
361
+ }
362
+ function stillExcluded(exclusions, by) {
363
+ return exclusions.chats.includes(by) || exclusions.folders.includes(by);
364
+ }
365
+ function folderOf(input) {
366
+ return resolve2(expand(input.trim(), homedir())).replace(/(.)\/+$/, "$1");
367
+ }
368
+ function changeExclusions(configDir, change) {
369
+ const add = (change.add ?? []).map(folderOf);
370
+ const remove = new Set((change.remove ?? []).map(folderOf));
371
+ const next = (current) => [.../* @__PURE__ */ new Set([...current.filter((folder) => !remove.has(folderOf(folder))), ...add])];
372
+ const macPath = macSettingsPath();
373
+ const settings = readObject(macPath);
374
+ if (settings) {
375
+ const sync = settings.localSync && typeof settings.localSync === "object" ? settings.localSync : {};
376
+ writePrivateJson(macPath, { ...settings, localSync: { ...sync, excludedFolders: next(strings2(sync.excludedFolders)) } });
377
+ }
378
+ const own = readObject(ownListPath(configDir)) ?? {};
379
+ const ownFolders = strings2(own.folders);
380
+ const keptOwn = settings ? ownFolders.filter((folder) => !remove.has(folderOf(folder))) : next(ownFolders);
381
+ if (!settings || keptOwn.length !== ownFolders.length) writePrivateJson(ownListPath(configDir), { ...own, folders: keptOwn });
382
+ return { written: settings ? "mac" : "cli", exclusions: loadExclusions(configDir) };
383
+ }
384
+
385
+ export {
386
+ SOURCE_CAPTURE_VERSION,
387
+ canonicalSourceApiBase,
388
+ captureTraceSource,
389
+ IncompleteSourceCaptureError,
390
+ writePrivateJson,
391
+ listPendingSources,
392
+ beginPendingSource,
393
+ savePendingSource,
394
+ recoverPendingSource,
395
+ completePendingCapture,
396
+ removePendingSource,
397
+ macSettingsPath,
398
+ loadExclusions,
399
+ inFolder,
400
+ excludedBy,
401
+ stillExcluded,
402
+ folderOf,
403
+ changeExclusions
404
+ };