@agentsky/sky 3.42.0 → 3.44.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.
- package/dist/chunk-3FQZZNQP.js +1969 -0
- package/dist/chunk-BCSZO4BR.js +555 -0
- package/dist/{chunk-YY2ZSZDH.js → chunk-BIZJ4VZL.js} +8 -10
- package/dist/chunk-CXRMG3HQ.js +8 -0
- package/dist/chunk-EBCVJDIL.js +497 -0
- package/dist/chunk-F6V2GGMP.js +585 -0
- package/dist/chunk-HGLVOMHL.js +2124 -0
- package/dist/{trace-ZZLFGVWA.js → chunk-KTSUD2BU.js} +23 -21
- package/dist/chunk-UJ7IXSJX.js +404 -0
- package/dist/{chunk-ALJRHTMX.js → chunk-W2WEEVIM.js} +2 -108
- package/dist/chunk-ZVNQJPMS.js +193 -0
- package/dist/compute-KNUZU4AO.js +35 -0
- package/dist/compute-report-AT642XES.js +34 -0
- package/dist/index.js +5230 -7048
- package/dist/local-skills-HHTAWGYV.js +28 -0
- package/dist/local-skills-run-OYPW36ZG.js +52 -0
- package/dist/mcp-serve-EJ7YKBMM.js +103 -0
- package/dist/trace-754MK5OE.js +54 -0
- package/dist/trace-exclusions-MYECULMA.js +20 -0
- package/dist/trace-library-WE3C5OBP.js +67 -0
- package/package.json +2 -1
- package/dist/trace-library-536NQQJJ.js +0 -1396
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
TRACE_CONTENT_SCOPES,
|
|
3
|
-
USER_AGENT,
|
|
4
3
|
redactTraceText
|
|
5
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-BIZJ4VZL.js";
|
|
5
|
+
import {
|
|
6
|
+
USER_AGENT
|
|
7
|
+
} from "./chunk-CXRMG3HQ.js";
|
|
6
8
|
import {
|
|
7
9
|
configPath
|
|
8
10
|
} from "./chunk-XHZJG3QT.js";
|
|
9
|
-
import "./chunk-MLKGABMK.js";
|
|
10
11
|
|
|
11
12
|
// src/trace.ts
|
|
12
13
|
import { randomBytes } from "crypto";
|
|
@@ -348,28 +349,29 @@ function chosenPaths(files, dirs) {
|
|
|
348
349
|
};
|
|
349
350
|
return { files: files.map((path) => check(path, "file")), dirs: dirs.map((path) => check(path, "folder")) };
|
|
350
351
|
}
|
|
352
|
+
|
|
351
353
|
export {
|
|
352
|
-
BATCH_MAX,
|
|
353
|
-
FinalRefusal,
|
|
354
|
-
RETRY_WINDOW_MS,
|
|
355
354
|
SPOOL_MAX_BYTES,
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
contentEvent,
|
|
360
|
-
contentForClaudeHook,
|
|
361
|
-
eventsForClaudeHook,
|
|
362
|
-
fileAllowed,
|
|
363
|
-
fileEvent,
|
|
364
|
-
flush,
|
|
365
|
-
grantSender,
|
|
366
|
-
installClaudeHooks,
|
|
367
|
-
parseContentScopes,
|
|
355
|
+
RETRY_WINDOW_MS,
|
|
356
|
+
BATCH_MAX,
|
|
357
|
+
traceDir,
|
|
368
358
|
readState,
|
|
359
|
+
writeState,
|
|
360
|
+
clearState,
|
|
361
|
+
stamp,
|
|
369
362
|
record,
|
|
363
|
+
grantSender,
|
|
364
|
+
FinalRefusal,
|
|
365
|
+
flush,
|
|
370
366
|
spoolStatus,
|
|
371
|
-
|
|
372
|
-
|
|
367
|
+
eventsForClaudeHook,
|
|
368
|
+
claudeHooksConfig,
|
|
369
|
+
installClaudeHooks,
|
|
373
370
|
uninstallClaudeHooks,
|
|
374
|
-
|
|
371
|
+
parseContentScopes,
|
|
372
|
+
fileAllowed,
|
|
373
|
+
contentEvent,
|
|
374
|
+
fileEvent,
|
|
375
|
+
contentForClaudeHook,
|
|
376
|
+
chosenPaths
|
|
375
377
|
};
|
|
@@ -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
|
+
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
USER_AGENT
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-CXRMG3HQ.js";
|
|
4
4
|
import {
|
|
5
5
|
apiBase,
|
|
6
6
|
apiToken,
|
|
7
|
-
configPath,
|
|
8
7
|
currentUniverse
|
|
9
8
|
} from "./chunk-XHZJG3QT.js";
|
|
10
9
|
|
|
@@ -197,108 +196,6 @@ async function apiRaw(method, path, init = {}) {
|
|
|
197
196
|
});
|
|
198
197
|
}
|
|
199
198
|
|
|
200
|
-
// src/resumable-upload.ts
|
|
201
|
-
import { createHash } from "crypto";
|
|
202
|
-
import { mkdirSync, openSync, readFileSync, readSync, closeSync, rmSync, statSync, writeFileSync as writeFileSync2 } from "fs";
|
|
203
|
-
import { basename, dirname, join, resolve } from "path";
|
|
204
|
-
var SINGLE_REQUEST_MAX_BYTES = 32 * 1024 * 1024;
|
|
205
|
-
var MAX_ATTEMPTS = 8;
|
|
206
|
-
function statePath(path, size, mtimeMs, universe) {
|
|
207
|
-
const key = createHash("sha256").update(`${resolve(path)}\0${size}\0${mtimeMs}\0${universe ?? ""}`).digest("hex").slice(0, 32);
|
|
208
|
-
return join(dirname(configPath()), "uploads", `${key}.json`);
|
|
209
|
-
}
|
|
210
|
-
function loadSession(file, expiresInSeconds, universe) {
|
|
211
|
-
try {
|
|
212
|
-
const saved = JSON.parse(readFileSync(file, "utf8"));
|
|
213
|
-
if (saved.expiresInSeconds !== expiresInSeconds || saved.universe !== universe) return null;
|
|
214
|
-
if (Date.parse(saved.expires_at) - Date.now() < 3600 * 1e3) return null;
|
|
215
|
-
return saved;
|
|
216
|
-
} catch {
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
function acknowledged(res) {
|
|
221
|
-
const range = res.headers.get("range")?.match(/^bytes=0-(\d+)$/);
|
|
222
|
-
return range ? Number(range[1]) + 1 : 0;
|
|
223
|
-
}
|
|
224
|
-
async function probe(session) {
|
|
225
|
-
const res = await fetch(session.upload_url, {
|
|
226
|
-
method: "PUT",
|
|
227
|
-
headers: { "user-agent": USER_AGENT, "content-range": `bytes */${session.size_bytes}` }
|
|
228
|
-
});
|
|
229
|
-
if (res.status === 200 || res.status === 201) return "done";
|
|
230
|
-
if (res.status === 308) return acknowledged(res);
|
|
231
|
-
if (res.status === 404 || res.status === 410) throw new ApiError(res.status, "upload_expired", "the upload session is gone");
|
|
232
|
-
throw new ApiError(res.status, "upload_failed", `upload status check failed: ${res.status} ${res.statusText}`);
|
|
233
|
-
}
|
|
234
|
-
async function sendChunk(session, fd, offset) {
|
|
235
|
-
const length = Math.min(session.chunk_size, session.size_bytes - offset);
|
|
236
|
-
const chunk = Buffer.alloc(length);
|
|
237
|
-
let read = 0;
|
|
238
|
-
while (read < length) read += readSync(fd, chunk, read, length - read, offset + read);
|
|
239
|
-
const res = await fetch(session.upload_url, {
|
|
240
|
-
method: "PUT",
|
|
241
|
-
headers: {
|
|
242
|
-
"user-agent": USER_AGENT,
|
|
243
|
-
"content-range": `bytes ${offset}-${offset + length - 1}/${session.size_bytes}`
|
|
244
|
-
},
|
|
245
|
-
body: chunk
|
|
246
|
-
});
|
|
247
|
-
if (res.status === 200 || res.status === 201) return "done";
|
|
248
|
-
if (res.status === 308) return acknowledged(res);
|
|
249
|
-
if (res.status >= 500 || res.status === 429) throw new Error(`transient ${res.status}`);
|
|
250
|
-
throw new ApiError(res.status, "upload_failed", `chunk upload failed: ${res.status} ${(await res.text()).slice(0, 200)}`);
|
|
251
|
-
}
|
|
252
|
-
async function transfer(session, path, onProgress) {
|
|
253
|
-
const fd = openSync(path, "r");
|
|
254
|
-
try {
|
|
255
|
-
let failures = 0;
|
|
256
|
-
let state = await probe(session);
|
|
257
|
-
while (state !== "done") {
|
|
258
|
-
onProgress(state, session.size_bytes);
|
|
259
|
-
try {
|
|
260
|
-
state = await sendChunk(session, fd, state);
|
|
261
|
-
failures = 0;
|
|
262
|
-
} catch (error) {
|
|
263
|
-
if (error instanceof ApiError || ++failures >= MAX_ATTEMPTS) throw error;
|
|
264
|
-
await new Promise((done) => setTimeout(done, Math.min(3e4, 500 * 2 ** failures)));
|
|
265
|
-
state = await probe(session);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
onProgress(session.size_bytes, session.size_bytes);
|
|
269
|
-
} finally {
|
|
270
|
-
closeSync(fd);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
async function uploadLargeFile(path, mediaType, expiresInSeconds, onProgress, universe) {
|
|
274
|
-
const { size, mtimeMs } = statSync(path);
|
|
275
|
-
const stateFile = statePath(path, size, mtimeMs, universe);
|
|
276
|
-
let session = loadSession(stateFile, expiresInSeconds, universe);
|
|
277
|
-
if (session) {
|
|
278
|
-
try {
|
|
279
|
-
await probe(session);
|
|
280
|
-
} catch (error) {
|
|
281
|
-
if (!(error instanceof ApiError) || error.status !== 404 && error.status !== 410) throw error;
|
|
282
|
-
session = null;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (!session) {
|
|
286
|
-
session = await api("POST", "/files/uploads", {
|
|
287
|
-
filename: basename(path),
|
|
288
|
-
size_bytes: size,
|
|
289
|
-
mime_type: mediaType,
|
|
290
|
-
...expiresInSeconds ? { expires_in_seconds: expiresInSeconds } : {}
|
|
291
|
-
}, { universe });
|
|
292
|
-
mkdirSync(dirname(stateFile), { recursive: true });
|
|
293
|
-
const saved = { ...session, path: resolve(path), mtimeMs, expiresInSeconds, universe };
|
|
294
|
-
writeFileSync2(stateFile, JSON.stringify(saved), { mode: 384 });
|
|
295
|
-
}
|
|
296
|
-
await transfer(session, path, onProgress);
|
|
297
|
-
const file = await api("POST", `/files/uploads/${encodeURIComponent(session.id)}/complete`, void 0, { universe });
|
|
298
|
-
rmSync(stateFile, { force: true });
|
|
299
|
-
return file;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
199
|
export {
|
|
303
200
|
ApiError,
|
|
304
201
|
parseError,
|
|
@@ -311,8 +208,5 @@ export {
|
|
|
311
208
|
apiBytes,
|
|
312
209
|
resolveSessionRef,
|
|
313
210
|
streamEvents,
|
|
314
|
-
apiRaw
|
|
315
|
-
SINGLE_REQUEST_MAX_BYTES,
|
|
316
|
-
transfer,
|
|
317
|
-
uploadLargeFile
|
|
211
|
+
apiRaw
|
|
318
212
|
};
|