@abelxiaoxing/cadence 2.0.21 → 2.0.23
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/README.de.md +5 -11
- package/README.es.md +5 -11
- package/README.fr.md +5 -11
- package/README.ja.md +5 -11
- package/README.ko.md +5 -11
- package/README.md +6 -14
- package/README.pt-BR.md +5 -11
- package/README.ru.md +5 -11
- package/README.zh-CN.md +5 -13
- package/docs/conversation-mode.md +20 -9
- package/docs/history.md +50 -0
- package/docs/service-management.md +1 -1
- package/package.json +8 -7
- package/src/agent-panel-cadence.ts +302 -10
- package/src/cadence-instructions.ts +1 -1
- package/src/cadence-mode.ts +682 -226
- package/src/cadence-preferences.ts +43 -8
- package/src/data-shapes.ts +16 -0
- package/src/history-shell.ts +370 -0
- package/src/index.ts +3 -3
- package/src/native-build.ts +1 -1
- package/src/native-client.ts +45 -2
- package/src/native-workflow-history.ts +132 -0
- package/src/operator-cli.mjs +93 -65
- package/src/service/checkout-lock.ts +142 -0
- package/src/service/contracts/workbench.ts +82 -26
- package/src/service/contracts/workflow.ts +43 -13
- package/src/service/coordination.ts +6 -7
- package/src/service/private-files.ts +2 -1
- package/src/service-cli.mjs +244 -423
- package/src/service-host.mjs +5722 -7097
- package/src/workspace.ts +302 -0
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +15 -15
- package/web/dist/manifest.json +7 -7
- package/config/workflow-task.example.json +0 -7
- package/src/plan-proposal.ts +0 -87
- package/src/task-shell.ts +0 -1059
- package/src/task-store.ts +0 -593
- package/src/task-types.ts +0 -184
package/src/service-cli.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { spawn } from "node:child_process";
|
|
|
6
6
|
import { mkdirSync as mkdirSync6 } from "node:fs";
|
|
7
7
|
import { request } from "node:http";
|
|
8
8
|
import { homedir as homedir6 } from "node:os";
|
|
9
|
-
import { dirname as
|
|
9
|
+
import { dirname as dirname14, join as join17, resolve as resolve7 } from "node:path";
|
|
10
10
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
11
11
|
|
|
12
12
|
// src/install-config.mjs
|
|
@@ -46,70 +46,13 @@ function ensureUserConfig(home = homedir()) {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
// src/
|
|
50
|
-
var TASK_SCHEMA = "cadence-workflow-assistant/1";
|
|
51
|
-
var MAX_RECORD_BYTES = 128 * 1024;
|
|
49
|
+
// src/data-shapes.ts
|
|
52
50
|
function object(value) {
|
|
53
51
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
54
52
|
}
|
|
55
|
-
function boundedString(value, max) {
|
|
56
|
-
return typeof value === "string" && Buffer.byteLength(value) <= max && !value.includes("\x00");
|
|
57
|
-
}
|
|
58
53
|
function validId(value) {
|
|
59
54
|
return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value);
|
|
60
55
|
}
|
|
61
|
-
function decodeCheck(value) {
|
|
62
|
-
if (!object(value) || !boundedString(value.name, 128) || !value.name.trim() || !boundedString(value.command, 4096) || !value.command.trim() || !Number.isInteger(value.timeoutMs) || Number(value.timeoutMs) < 100 || Number(value.timeoutMs) > 1200000)
|
|
63
|
-
throw new Error("Invalid check definition");
|
|
64
|
-
const controls = /[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/u;
|
|
65
|
-
if (controls.test(value.command) || controls.test(value.name) || /[\n\t]/u.test(value.name))
|
|
66
|
-
throw new Error("Check commands/names must be visibly representable in the terminal");
|
|
67
|
-
return {
|
|
68
|
-
name: value.name,
|
|
69
|
-
command: value.command,
|
|
70
|
-
timeoutMs: Number(value.timeoutMs)
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function decodeTask(value) {
|
|
74
|
-
if (!object(value) || value.schema !== TASK_SCHEMA || typeof value.id !== "string" || !validId(value.id) || !Number.isSafeInteger(value.revision) || Number(value.revision) < 1 || typeof value.kind !== "string" || !["code", "research", "diagnosis"].includes(value.kind) || typeof value.phase !== "string" || ![
|
|
75
|
-
"planning",
|
|
76
|
-
"running",
|
|
77
|
-
"review",
|
|
78
|
-
"interrupted",
|
|
79
|
-
"failed",
|
|
80
|
-
"done"
|
|
81
|
-
].includes(value.phase) || !boundedString(value.goal, 8192) || !value.goal.trim() || !boundedString(value.plan, 32768) || !boundedString(value.cwd, 4096) || !boundedString(value.result, 16384) || !boundedString(value.baseline, 16384) || !Array.isArray(value.checks) || value.checks.length > 32 || !Array.isArray(value.activity) || value.activity.length > 64)
|
|
82
|
-
throw new Error("Unknown or corrupt task record");
|
|
83
|
-
for (const check of value.checks) {
|
|
84
|
-
decodeCheck(check);
|
|
85
|
-
if (!object(check) || !boundedString(check.cwd, 4096) || !(check.exitCode === null || Number.isInteger(check.exitCode)) || typeof check.durationMs !== "number" || !Number.isFinite(check.durationMs) || check.durationMs < 0 || typeof check.status !== "string" || !["passed", "failed", "canceled", "stale"].includes(check.status) || !boundedString(check.fingerprint, 128) || check.log !== undefined && (typeof check.log !== "string" || !/^[a-f0-9-]+\.log$/.test(check.log)))
|
|
86
|
-
throw new Error("Corrupt check record");
|
|
87
|
-
}
|
|
88
|
-
for (const entry of value.activity) {
|
|
89
|
-
if (!object(entry) || !boundedString(entry.at, 64) || !boundedString(entry.text, 512))
|
|
90
|
-
throw new Error("Corrupt activity");
|
|
91
|
-
}
|
|
92
|
-
if (value.review !== undefined && (!object(value.review) || !boundedString(value.review.fingerprint, 128) || typeof value.review.outcome !== "string" || !["reviewed", "reviewed-with-issues"].includes(value.review.outcome)))
|
|
93
|
-
throw new Error("Corrupt review");
|
|
94
|
-
const allowed = new Set([
|
|
95
|
-
"schema",
|
|
96
|
-
"id",
|
|
97
|
-
"revision",
|
|
98
|
-
"kind",
|
|
99
|
-
"goal",
|
|
100
|
-
"plan",
|
|
101
|
-
"cwd",
|
|
102
|
-
"phase",
|
|
103
|
-
"result",
|
|
104
|
-
"baseline",
|
|
105
|
-
"checks",
|
|
106
|
-
"review",
|
|
107
|
-
"activity"
|
|
108
|
-
]);
|
|
109
|
-
if (Object.keys(value).some((key) => !allowed.has(key)))
|
|
110
|
-
throw new Error("Unknown task fields");
|
|
111
|
-
return structuredClone(value);
|
|
112
|
-
}
|
|
113
56
|
|
|
114
57
|
// src/sdk-network.ts
|
|
115
58
|
function safeChildEnv() {
|
|
@@ -2084,25 +2027,20 @@ class CaptureBudgetLease {
|
|
|
2084
2027
|
this.forget();
|
|
2085
2028
|
}
|
|
2086
2029
|
}
|
|
2087
|
-
// src/
|
|
2030
|
+
// src/workspace.ts
|
|
2088
2031
|
import { spawnSync } from "node:child_process";
|
|
2089
|
-
import { createHash
|
|
2032
|
+
import { createHash } from "node:crypto";
|
|
2090
2033
|
import {
|
|
2091
2034
|
closeSync as closeSync2,
|
|
2092
2035
|
constants as constants2,
|
|
2093
2036
|
fstatSync,
|
|
2094
|
-
fsyncSync,
|
|
2095
2037
|
lstatSync,
|
|
2096
2038
|
mkdirSync as mkdirSync2,
|
|
2097
2039
|
openSync as openSync2,
|
|
2098
|
-
readdirSync,
|
|
2099
2040
|
readSync,
|
|
2100
|
-
realpathSync
|
|
2101
|
-
renameSync,
|
|
2102
|
-
unlinkSync,
|
|
2103
|
-
writeFileSync
|
|
2041
|
+
realpathSync
|
|
2104
2042
|
} from "node:fs";
|
|
2105
|
-
import { homedir as homedir2
|
|
2043
|
+
import { homedir as homedir2 } from "node:os";
|
|
2106
2044
|
import {
|
|
2107
2045
|
dirname,
|
|
2108
2046
|
isAbsolute,
|
|
@@ -2112,23 +2050,6 @@ import {
|
|
|
2112
2050
|
resolve,
|
|
2113
2051
|
sep
|
|
2114
2052
|
} from "node:path";
|
|
2115
|
-
|
|
2116
|
-
// src/verification-diagnostics.ts
|
|
2117
|
-
function diagnosticText(value, maximum = 4096, secrets = []) {
|
|
2118
|
-
maximum = Math.max(0, Math.floor(maximum));
|
|
2119
|
-
let clean = value;
|
|
2120
|
-
for (const secret of [...new Set(secrets)].filter(Boolean).sort((a, b) => b.length - a.length))
|
|
2121
|
-
clean = clean.split(secret).join("[redacted]");
|
|
2122
|
-
clean = clean.replace(/\x1b\][\s\S]*?(?:\x07|\x1b\\)/g, "").replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "").replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/g, "").replace(/(Bearer\s+)\S+/gi, "$1[redacted]").replace(/((?:api[_-]?key|token|password|secret)\s*[:=]\s*)[^\s,;]+/gi, "$1[redacted]").replace(/(https?:\/\/)[^\s/@]+:[^\s/@]+@/gi, "$1[redacted]@");
|
|
2123
|
-
if (Buffer.byteLength(clean) <= maximum)
|
|
2124
|
-
return clean;
|
|
2125
|
-
const marker = maximum >= 12 ? `
|
|
2126
|
-
[truncated]` : ".".repeat(maximum);
|
|
2127
|
-
const bytes = Buffer.from(clean).subarray(0, Math.max(0, maximum - Buffer.byteLength(marker)));
|
|
2128
|
-
return bytes.toString("utf8").replace(/\ufffd$/, "") + marker;
|
|
2129
|
-
}
|
|
2130
|
-
|
|
2131
|
-
// src/task-store.ts
|
|
2132
2053
|
function parseRecord(data) {
|
|
2133
2054
|
try {
|
|
2134
2055
|
return JSON.parse(data.toString("utf8"));
|
|
@@ -2264,13 +2185,6 @@ function defaultStateHome() {
|
|
|
2264
2185
|
throw new Error("State home must be absolute");
|
|
2265
2186
|
return candidate ?? join(homedir2(), process.platform === "win32" ? "AppData/Local" : ".local/state");
|
|
2266
2187
|
}
|
|
2267
|
-
function recordText(record) {
|
|
2268
|
-
return `${JSON.stringify(record, null, 2)}
|
|
2269
|
-
`;
|
|
2270
|
-
}
|
|
2271
|
-
function contentIdentity(text2) {
|
|
2272
|
-
return createHash("sha256").update(text2).digest("hex");
|
|
2273
|
-
}
|
|
2274
2188
|
function canonicalAmbientRoot(path2) {
|
|
2275
2189
|
const full = resolve(path2);
|
|
2276
2190
|
try {
|
|
@@ -2311,213 +2225,6 @@ function effectiveWorkspace(requested, stateHome, _home) {
|
|
|
2311
2225
|
return { workspace: requested, redirected: false };
|
|
2312
2226
|
}
|
|
2313
2227
|
|
|
2314
|
-
class TaskStore {
|
|
2315
|
-
root;
|
|
2316
|
-
tasks;
|
|
2317
|
-
privateRoot;
|
|
2318
|
-
constructor(root, stateHome = defaultStateHome()) {
|
|
2319
|
-
this.root = realpathSync(root);
|
|
2320
|
-
if (privateWorkspace(this.root, stateHome))
|
|
2321
|
-
throw new Error("Private state cannot be a workspace");
|
|
2322
|
-
this.tasks = join(this.root, ".cadence", "assistant", "tasks");
|
|
2323
|
-
this.privateRoot = privateRootFor(this.root, stateHome);
|
|
2324
|
-
if (contains(this.root, this.privateRoot))
|
|
2325
|
-
throw new Error("Private state home must be outside the working copy");
|
|
2326
|
-
}
|
|
2327
|
-
get lockPath() {
|
|
2328
|
-
return join(this.privateRoot, "checkout.lock");
|
|
2329
|
-
}
|
|
2330
|
-
inspectPrivate() {
|
|
2331
|
-
const namespace = dirname(this.privateRoot);
|
|
2332
|
-
for (const path2 of [namespace, this.privateRoot]) {
|
|
2333
|
-
if (!safeDirectory(path2))
|
|
2334
|
-
continue;
|
|
2335
|
-
const st = lstatSync(path2);
|
|
2336
|
-
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
2337
|
-
throw new Error("Private state permissions refused");
|
|
2338
|
-
}
|
|
2339
|
-
}
|
|
2340
|
-
inspectPrivateFile(path2, max) {
|
|
2341
|
-
this.inspectPrivate();
|
|
2342
|
-
if (!safeDirectory(this.privateRoot))
|
|
2343
|
-
return;
|
|
2344
|
-
try {
|
|
2345
|
-
const st = lstatSync(path2);
|
|
2346
|
-
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1 || st.size > max || process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
2347
|
-
throw new Error("Unsafe private state file");
|
|
2348
|
-
} catch (error) {
|
|
2349
|
-
if (!absent(error))
|
|
2350
|
-
throw error;
|
|
2351
|
-
}
|
|
2352
|
-
}
|
|
2353
|
-
inspectLock() {
|
|
2354
|
-
this.inspectPrivateFile(this.lockPath, 8192);
|
|
2355
|
-
const data = readBounded(this.lockPath, 8192);
|
|
2356
|
-
if (!data)
|
|
2357
|
-
return;
|
|
2358
|
-
const lock = parseRecord(data);
|
|
2359
|
-
if (!object(lock) || lock.schema !== "cadence-assistant-lock/1" || lock.root !== this.root || typeof lock.token !== "string" || !validId(lock.token) || typeof lock.runId !== "string" || !validId(lock.runId) || typeof lock.host !== "string" || !Number.isInteger(lock.pid) || Number(lock.pid) < 1)
|
|
2360
|
-
throw new Error("Unknown or corrupt coordination lock");
|
|
2361
|
-
return lock;
|
|
2362
|
-
}
|
|
2363
|
-
acquire() {
|
|
2364
|
-
this.inspectPrivate();
|
|
2365
|
-
safeDirectory(this.privateRoot, true);
|
|
2366
|
-
const lock = {
|
|
2367
|
-
schema: "cadence-assistant-lock/1",
|
|
2368
|
-
token: randomUUID(),
|
|
2369
|
-
runId: randomUUID(),
|
|
2370
|
-
pid: process.pid,
|
|
2371
|
-
host: hostname(),
|
|
2372
|
-
root: this.root
|
|
2373
|
-
};
|
|
2374
|
-
let fd;
|
|
2375
|
-
try {
|
|
2376
|
-
fd = retryWindowsFileOperation(() => openSync2(this.lockPath, "wx", 384));
|
|
2377
|
-
} catch {
|
|
2378
|
-
throw new Error("Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner");
|
|
2379
|
-
}
|
|
2380
|
-
try {
|
|
2381
|
-
writeFileSync(fd, JSON.stringify(lock));
|
|
2382
|
-
fsyncSync(fd);
|
|
2383
|
-
} finally {
|
|
2384
|
-
closeSync2(fd);
|
|
2385
|
-
}
|
|
2386
|
-
return lock;
|
|
2387
|
-
}
|
|
2388
|
-
assertOwner(lock) {
|
|
2389
|
-
if (JSON.stringify(this.inspectLock()) !== JSON.stringify(lock))
|
|
2390
|
-
throw new Error("Coordination lock changed");
|
|
2391
|
-
}
|
|
2392
|
-
release(lock) {
|
|
2393
|
-
if (lock.pid !== process.pid || lock.host !== hostname())
|
|
2394
|
-
throw new Error("Only the owning process may release a lock");
|
|
2395
|
-
this.assertOwner(lock);
|
|
2396
|
-
retryWindowsFileOperation(() => unlinkSync(this.lockPath), () => this.assertOwner(lock));
|
|
2397
|
-
}
|
|
2398
|
-
recover(expected, acknowledgeEscapedProcesses) {
|
|
2399
|
-
if (!acknowledgeEscapedProcesses || expected.host !== hostname())
|
|
2400
|
-
throw new Error("Recovery requires informed local-owner confirmation");
|
|
2401
|
-
this.assertOwner(expected);
|
|
2402
|
-
try {
|
|
2403
|
-
process.kill(expected.pid, 0);
|
|
2404
|
-
} catch (error) {
|
|
2405
|
-
if (!object(error) || error.code !== "ESRCH")
|
|
2406
|
-
throw new Error("Owner status unknown");
|
|
2407
|
-
this.assertOwner(expected);
|
|
2408
|
-
unlinkSync(this.lockPath);
|
|
2409
|
-
return;
|
|
2410
|
-
}
|
|
2411
|
-
throw new Error("Owner is live; stop it first");
|
|
2412
|
-
}
|
|
2413
|
-
load(id) {
|
|
2414
|
-
return this.loadSnapshot(id)?.record;
|
|
2415
|
-
}
|
|
2416
|
-
loadSnapshot(id) {
|
|
2417
|
-
if (!validId(id))
|
|
2418
|
-
throw new Error("Invalid task id");
|
|
2419
|
-
const directory = join(this.tasks, id);
|
|
2420
|
-
if (safeDirectory(directory) && readdirSync(directory).some((name) => name !== "task.json"))
|
|
2421
|
-
throw new Error("Unknown task directory content; preserved without reset");
|
|
2422
|
-
const data = readBounded(join(this.tasks, id, "task.json"), MAX_RECORD_BYTES);
|
|
2423
|
-
if (!data)
|
|
2424
|
-
return;
|
|
2425
|
-
const record = decodeTask(parseRecord(data));
|
|
2426
|
-
if (record.id !== id)
|
|
2427
|
-
throw new Error("Task identity mismatch");
|
|
2428
|
-
return { record, identity: contentIdentity(data) };
|
|
2429
|
-
}
|
|
2430
|
-
list() {
|
|
2431
|
-
if (!safeDirectory(this.tasks))
|
|
2432
|
-
return [];
|
|
2433
|
-
const entries = readdirSync(this.tasks);
|
|
2434
|
-
if (entries.length > 1000)
|
|
2435
|
-
throw new Error("Task listing limit exceeded");
|
|
2436
|
-
return entries.map((id) => {
|
|
2437
|
-
const task = this.load(id);
|
|
2438
|
-
if (!task)
|
|
2439
|
-
throw new Error("Unknown task directory");
|
|
2440
|
-
return task;
|
|
2441
|
-
});
|
|
2442
|
-
}
|
|
2443
|
-
save(record, expectedRevision, held, expectedIdentity) {
|
|
2444
|
-
const next = decodeTask(record);
|
|
2445
|
-
if (next.revision !== expectedRevision + 1)
|
|
2446
|
-
throw new Error("Revision conflict");
|
|
2447
|
-
const text2 = recordText(next);
|
|
2448
|
-
if (Buffer.byteLength(text2) > MAX_RECORD_BYTES)
|
|
2449
|
-
throw new Error("Task record limit");
|
|
2450
|
-
const lock = held ?? this.acquire();
|
|
2451
|
-
if (lock.pid !== process.pid || lock.host !== hostname())
|
|
2452
|
-
throw new Error("Not the lock owner");
|
|
2453
|
-
try {
|
|
2454
|
-
this.assertOwner(lock);
|
|
2455
|
-
const current = this.loadSnapshot(next.id);
|
|
2456
|
-
if ((current?.record.revision ?? 0) !== expectedRevision)
|
|
2457
|
-
throw new Error("Revision conflict");
|
|
2458
|
-
if (current?.identity !== expectedIdentity)
|
|
2459
|
-
throw new Error("Task content changed; reopen it");
|
|
2460
|
-
const dir = join(this.tasks, next.id);
|
|
2461
|
-
safeDirectory(dir, true);
|
|
2462
|
-
if (!current && readdirSync(dir).length)
|
|
2463
|
-
throw new Error("Unknown task directory");
|
|
2464
|
-
const temporary = join(dir, `task.${randomUUID()}.tmp`);
|
|
2465
|
-
const fd = openSync2(temporary, "wx", 384);
|
|
2466
|
-
try {
|
|
2467
|
-
writeFileSync(fd, text2);
|
|
2468
|
-
fsyncSync(fd);
|
|
2469
|
-
} finally {
|
|
2470
|
-
closeSync2(fd);
|
|
2471
|
-
}
|
|
2472
|
-
this.assertOwner(lock);
|
|
2473
|
-
const latest = readBounded(join(dir, "task.json"), MAX_RECORD_BYTES);
|
|
2474
|
-
if ((latest ? contentIdentity(latest) : undefined) !== expectedIdentity)
|
|
2475
|
-
throw new Error("Task content changed; replacement preserved without overwrite");
|
|
2476
|
-
retryWindowsFileOperation(() => renameSync(temporary, join(dir, "task.json")), () => {
|
|
2477
|
-
this.assertOwner(lock);
|
|
2478
|
-
const now = readBounded(join(dir, "task.json"), MAX_RECORD_BYTES);
|
|
2479
|
-
if ((now ? contentIdentity(now) : undefined) !== expectedIdentity)
|
|
2480
|
-
throw new Error("Task content changed; replacement preserved without overwrite");
|
|
2481
|
-
});
|
|
2482
|
-
return next;
|
|
2483
|
-
} finally {
|
|
2484
|
-
if (!held)
|
|
2485
|
-
this.release(lock);
|
|
2486
|
-
}
|
|
2487
|
-
}
|
|
2488
|
-
checks() {
|
|
2489
|
-
const data = readBounded(join(this.root, ".cadence", "assistant", "config.json"), 32768);
|
|
2490
|
-
if (!data)
|
|
2491
|
-
return [];
|
|
2492
|
-
const value = parseRecord(data);
|
|
2493
|
-
if (!object(value) || value.schema !== TASK_CONFIG_SCHEMA || !Array.isArray(value.checks) || value.checks.length > 32 || Object.keys(value).some((key) => !["schema", "checks"].includes(key)))
|
|
2494
|
-
throw new Error("Unknown check configuration");
|
|
2495
|
-
const checks = value.checks.map(decodeCheck);
|
|
2496
|
-
if (new Set(checks.map((check) => check.name)).size !== checks.length || checks.some((check) => check.name === "Manual command"))
|
|
2497
|
-
throw new Error("Duplicate or reserved check name");
|
|
2498
|
-
return checks;
|
|
2499
|
-
}
|
|
2500
|
-
readLog(name) {
|
|
2501
|
-
if (!/^[a-f0-9-]{36}\.log$/.test(name))
|
|
2502
|
-
throw new Error("Invalid diagnostic name");
|
|
2503
|
-
this.inspectPrivateFile(join(this.privateRoot, name), 65536);
|
|
2504
|
-
const data = readBounded(join(this.privateRoot, name), 65536);
|
|
2505
|
-
return data ? diagnosticText(data.toString("utf8"), 65536) : "Private diagnostic unavailable";
|
|
2506
|
-
}
|
|
2507
|
-
writeLog(lock, output) {
|
|
2508
|
-
if (lock.pid !== process.pid || lock.host !== hostname())
|
|
2509
|
-
throw new Error("Not the lock owner");
|
|
2510
|
-
this.assertOwner(lock);
|
|
2511
|
-
const name = `${randomUUID()}.log`;
|
|
2512
|
-
writeFileSync(join(this.privateRoot, name), diagnosticText(output, 65536), {
|
|
2513
|
-
flag: "wx",
|
|
2514
|
-
mode: 384
|
|
2515
|
-
});
|
|
2516
|
-
return name;
|
|
2517
|
-
}
|
|
2518
|
-
}
|
|
2519
|
-
var TASK_CONFIG_SCHEMA = "cadence-assistant-checks/1";
|
|
2520
|
-
|
|
2521
2228
|
// src/service/autostart.ts
|
|
2522
2229
|
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
2523
2230
|
import {
|
|
@@ -2525,10 +2232,10 @@ import {
|
|
|
2525
2232
|
mkdirSync as mkdirSync5,
|
|
2526
2233
|
readFileSync,
|
|
2527
2234
|
realpathSync as realpathSync3,
|
|
2528
|
-
renameSync as
|
|
2235
|
+
renameSync as renameSync2,
|
|
2529
2236
|
rmSync,
|
|
2530
2237
|
statSync as statSync2,
|
|
2531
|
-
writeFileSync as
|
|
2238
|
+
writeFileSync as writeFileSync2
|
|
2532
2239
|
} from "node:fs";
|
|
2533
2240
|
import { homedir as homedir3 } from "node:os";
|
|
2534
2241
|
import {
|
|
@@ -2585,19 +2292,19 @@ function parseEnvFile(content) {
|
|
|
2585
2292
|
}
|
|
2586
2293
|
|
|
2587
2294
|
// src/service/private-files.ts
|
|
2588
|
-
import { createHash as createHash2, randomUUID
|
|
2295
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
2589
2296
|
import {
|
|
2590
2297
|
closeSync as closeSync3,
|
|
2591
2298
|
constants as constants3,
|
|
2592
2299
|
fstatSync as fstatSync2,
|
|
2593
|
-
fsyncSync
|
|
2300
|
+
fsyncSync,
|
|
2594
2301
|
lstatSync as lstatSync2,
|
|
2595
2302
|
mkdirSync as mkdirSync3,
|
|
2596
2303
|
openSync as openSync3,
|
|
2597
2304
|
readSync as readSync2,
|
|
2598
|
-
renameSync
|
|
2599
|
-
unlinkSync
|
|
2600
|
-
writeFileSync
|
|
2305
|
+
renameSync,
|
|
2306
|
+
unlinkSync,
|
|
2307
|
+
writeFileSync
|
|
2601
2308
|
} from "node:fs";
|
|
2602
2309
|
import { dirname as dirname2, isAbsolute as isAbsolute2, join as join2, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
|
|
2603
2310
|
var METADATA_BYTES = 16 * 1024;
|
|
@@ -2767,8 +2474,8 @@ class PrivateFiles {
|
|
|
2767
2474
|
this.directory(dirname2(path2), true);
|
|
2768
2475
|
const fd = retryWindowsFileOperation(() => openSync3(path2, "wx", 384));
|
|
2769
2476
|
try {
|
|
2770
|
-
|
|
2771
|
-
|
|
2477
|
+
writeFileSync(fd, bytes);
|
|
2478
|
+
fsyncSync(fd);
|
|
2772
2479
|
} finally {
|
|
2773
2480
|
closeSync3(fd);
|
|
2774
2481
|
}
|
|
@@ -2780,10 +2487,10 @@ class PrivateFiles {
|
|
|
2780
2487
|
}
|
|
2781
2488
|
replace(path2, expected, bytes, maximum = METADATA_BYTES) {
|
|
2782
2489
|
this.assert(path2, expected, maximum);
|
|
2783
|
-
const temporary = join2(dirname2(path2), `pending-${
|
|
2490
|
+
const temporary = join2(dirname2(path2), `pending-${randomUUID()}.json`);
|
|
2784
2491
|
this.create(temporary, bytes, maximum);
|
|
2785
2492
|
this.assert(path2, expected, maximum);
|
|
2786
|
-
retryWindowsFileOperation(() =>
|
|
2493
|
+
retryWindowsFileOperation(() => renameSync(temporary, path2), () => this.assert(path2, expected, maximum));
|
|
2787
2494
|
this.syncDirectory(dirname2(path2));
|
|
2788
2495
|
const result = this.read(path2, maximum);
|
|
2789
2496
|
if (!result || result.identity !== hash(bytes))
|
|
@@ -2792,7 +2499,7 @@ class PrivateFiles {
|
|
|
2792
2499
|
}
|
|
2793
2500
|
remove(path2, expected, maximum = METADATA_BYTES) {
|
|
2794
2501
|
this.assert(path2, expected, maximum);
|
|
2795
|
-
retryWindowsFileOperation(() =>
|
|
2502
|
+
retryWindowsFileOperation(() => unlinkSync(path2), () => this.assert(path2, expected, maximum));
|
|
2796
2503
|
this.syncDirectory(dirname2(path2));
|
|
2797
2504
|
}
|
|
2798
2505
|
syncDirectory(path2) {
|
|
@@ -2800,7 +2507,7 @@ class PrivateFiles {
|
|
|
2800
2507
|
return;
|
|
2801
2508
|
const fd = openSync3(path2, constants3.O_RDONLY | constants3.O_DIRECTORY);
|
|
2802
2509
|
try {
|
|
2803
|
-
|
|
2510
|
+
fsyncSync(fd);
|
|
2804
2511
|
} finally {
|
|
2805
2512
|
closeSync3(fd);
|
|
2806
2513
|
}
|
|
@@ -3285,8 +2992,8 @@ function write(path2, content) {
|
|
|
3285
2992
|
mkdirSync5(dirname4(path2), { recursive: true });
|
|
3286
2993
|
const temporary = `${path2}.${process.pid}.tmp`;
|
|
3287
2994
|
try {
|
|
3288
|
-
|
|
3289
|
-
|
|
2995
|
+
writeFileSync2(temporary, content, { mode: 420, flag: "wx" });
|
|
2996
|
+
renameSync2(temporary, path2);
|
|
3290
2997
|
} finally {
|
|
3291
2998
|
rmSync(temporary, { force: true });
|
|
3292
2999
|
}
|
|
@@ -3429,7 +3136,106 @@ function autostartStatus(s) {
|
|
|
3429
3136
|
import { randomBytes as randomBytes2, randomUUID as randomUUID3 } from "node:crypto";
|
|
3430
3137
|
import { opendirSync } from "node:fs";
|
|
3431
3138
|
import { hostname as hostname2 } from "node:os";
|
|
3432
|
-
import { dirname as
|
|
3139
|
+
import { dirname as dirname6, isAbsolute as isAbsolute4, join as join6, relative as relative3, sep as sep4 } from "node:path";
|
|
3140
|
+
|
|
3141
|
+
// src/service/checkout-lock.ts
|
|
3142
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
3143
|
+
import {
|
|
3144
|
+
closeSync as closeSync4,
|
|
3145
|
+
fsyncSync as fsyncSync2,
|
|
3146
|
+
lstatSync as lstatSync5,
|
|
3147
|
+
openSync as openSync4,
|
|
3148
|
+
realpathSync as realpathSync4,
|
|
3149
|
+
unlinkSync as unlinkSync2,
|
|
3150
|
+
writeFileSync as writeFileSync3
|
|
3151
|
+
} from "node:fs";
|
|
3152
|
+
import { hostname } from "node:os";
|
|
3153
|
+
import { dirname as dirname5, join as join5 } from "node:path";
|
|
3154
|
+
class CheckoutStore {
|
|
3155
|
+
root;
|
|
3156
|
+
privateRoot;
|
|
3157
|
+
constructor(root, stateHome = defaultStateHome()) {
|
|
3158
|
+
this.root = realpathSync4(root);
|
|
3159
|
+
if (privateWorkspace(this.root, stateHome))
|
|
3160
|
+
throw new Error("Private state cannot be a workspace");
|
|
3161
|
+
this.privateRoot = privateRootFor(this.root, stateHome);
|
|
3162
|
+
if (contains(this.root, this.privateRoot))
|
|
3163
|
+
throw new Error("Private state home must be outside the working copy");
|
|
3164
|
+
}
|
|
3165
|
+
get lockPath() {
|
|
3166
|
+
return join5(this.privateRoot, "checkout.lock");
|
|
3167
|
+
}
|
|
3168
|
+
inspectPrivate() {
|
|
3169
|
+
const namespace = dirname5(this.privateRoot);
|
|
3170
|
+
for (const path2 of [namespace, this.privateRoot]) {
|
|
3171
|
+
if (!safeDirectory(path2))
|
|
3172
|
+
continue;
|
|
3173
|
+
const st = lstatSync5(path2);
|
|
3174
|
+
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
3175
|
+
throw new Error("Private state permissions refused");
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
inspectPrivateFile(path2, max) {
|
|
3179
|
+
this.inspectPrivate();
|
|
3180
|
+
if (!safeDirectory(this.privateRoot))
|
|
3181
|
+
return;
|
|
3182
|
+
try {
|
|
3183
|
+
const st = lstatSync5(path2);
|
|
3184
|
+
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1 || st.size > max || process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
3185
|
+
throw new Error("Unsafe private state file");
|
|
3186
|
+
} catch (error) {
|
|
3187
|
+
if (!absent(error))
|
|
3188
|
+
throw error;
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
inspectLock() {
|
|
3192
|
+
this.inspectPrivateFile(this.lockPath, 8192);
|
|
3193
|
+
const data = readBounded(this.lockPath, 8192);
|
|
3194
|
+
if (!data)
|
|
3195
|
+
return;
|
|
3196
|
+
const lock = parseRecord(data);
|
|
3197
|
+
if (!object(lock) || lock.schema !== "cadence-assistant-lock/1" || lock.root !== this.root || typeof lock.token !== "string" || !validId(lock.token) || typeof lock.runId !== "string" || !validId(lock.runId) || typeof lock.host !== "string" || !Number.isInteger(lock.pid) || Number(lock.pid) < 1)
|
|
3198
|
+
throw new Error("Unknown or corrupt coordination lock");
|
|
3199
|
+
return lock;
|
|
3200
|
+
}
|
|
3201
|
+
acquire() {
|
|
3202
|
+
this.inspectPrivate();
|
|
3203
|
+
safeDirectory(this.privateRoot, true);
|
|
3204
|
+
const lock = {
|
|
3205
|
+
schema: "cadence-assistant-lock/1",
|
|
3206
|
+
token: randomUUID2(),
|
|
3207
|
+
runId: randomUUID2(),
|
|
3208
|
+
pid: process.pid,
|
|
3209
|
+
host: hostname(),
|
|
3210
|
+
root: this.root
|
|
3211
|
+
};
|
|
3212
|
+
let fd;
|
|
3213
|
+
try {
|
|
3214
|
+
fd = retryWindowsFileOperation(() => openSync4(this.lockPath, "wx", 384));
|
|
3215
|
+
} catch {
|
|
3216
|
+
throw new Error("Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner");
|
|
3217
|
+
}
|
|
3218
|
+
try {
|
|
3219
|
+
writeFileSync3(fd, JSON.stringify(lock));
|
|
3220
|
+
fsyncSync2(fd);
|
|
3221
|
+
} finally {
|
|
3222
|
+
closeSync4(fd);
|
|
3223
|
+
}
|
|
3224
|
+
return lock;
|
|
3225
|
+
}
|
|
3226
|
+
assertOwner(lock) {
|
|
3227
|
+
if (JSON.stringify(this.inspectLock()) !== JSON.stringify(lock))
|
|
3228
|
+
throw new Error("Coordination lock changed");
|
|
3229
|
+
}
|
|
3230
|
+
release(lock) {
|
|
3231
|
+
if (lock.pid !== process.pid || lock.host !== hostname())
|
|
3232
|
+
throw new Error("Only the owning process may release a lock");
|
|
3233
|
+
this.assertOwner(lock);
|
|
3234
|
+
retryWindowsFileOperation(() => unlinkSync2(this.lockPath), () => this.assertOwner(lock));
|
|
3235
|
+
}
|
|
3236
|
+
}
|
|
3237
|
+
|
|
3238
|
+
// src/service/coordination.ts
|
|
3433
3239
|
var registrationKeys = [
|
|
3434
3240
|
"schema",
|
|
3435
3241
|
"root",
|
|
@@ -3568,11 +3374,11 @@ class ServiceCoordination {
|
|
|
3568
3374
|
registryDirectory;
|
|
3569
3375
|
stateHome;
|
|
3570
3376
|
constructor(workspace, stateHome) {
|
|
3571
|
-
this.project = workspace === null ? undefined : new
|
|
3572
|
-
this.stateHome = this.project ?
|
|
3573
|
-
const namespace =
|
|
3377
|
+
this.project = workspace === null ? undefined : new CheckoutStore(resolveWorkspace(workspace).root, stateHome);
|
|
3378
|
+
this.stateHome = this.project ? dirname6(dirname6(this.project.privateRoot)) : canonicalAmbientRoot(stateHome ?? defaultStateHome());
|
|
3379
|
+
const namespace = join6(this.stateHome, "cadence-assistant-v1");
|
|
3574
3380
|
this.files = new PrivateFiles(namespace);
|
|
3575
|
-
this.registryDirectory =
|
|
3381
|
+
this.registryDirectory = join6(namespace, "service-registry-v1");
|
|
3576
3382
|
}
|
|
3577
3383
|
get store() {
|
|
3578
3384
|
if (!this.project)
|
|
@@ -3580,13 +3386,13 @@ class ServiceCoordination {
|
|
|
3580
3386
|
return this.project;
|
|
3581
3387
|
}
|
|
3582
3388
|
get directory() {
|
|
3583
|
-
return
|
|
3389
|
+
return join6(this.store.privateRoot, "service-v1");
|
|
3584
3390
|
}
|
|
3585
3391
|
path(name) {
|
|
3586
|
-
return
|
|
3392
|
+
return join6(this.directory, name);
|
|
3587
3393
|
}
|
|
3588
3394
|
registryPath(name) {
|
|
3589
|
-
return
|
|
3395
|
+
return join6(this.registryDirectory, name);
|
|
3590
3396
|
}
|
|
3591
3397
|
registry() {
|
|
3592
3398
|
if (!this.files.directory(this.registryDirectory))
|
|
@@ -3633,14 +3439,14 @@ class ServiceCoordination {
|
|
|
3633
3439
|
"conversation-workflows-v1",
|
|
3634
3440
|
"workflows-v1"
|
|
3635
3441
|
]);
|
|
3636
|
-
this.files.directory(
|
|
3442
|
+
this.files.directory(join6(directory2, "history-v2"), false, walks);
|
|
3637
3443
|
}
|
|
3638
3444
|
selected(directory2 = this.directory, root = this.store.root, stoppedOnly = false) {
|
|
3639
3445
|
const walks = new DirectoryWalks;
|
|
3640
3446
|
if (!this.files.directory(directory2, false, walks))
|
|
3641
3447
|
return;
|
|
3642
3448
|
this.assertNamespace(walks, directory2);
|
|
3643
|
-
const path2 = (name) =>
|
|
3449
|
+
const path2 = (name) => join6(directory2, name);
|
|
3644
3450
|
const manifest = this.files.read(path2("format.json"), undefined, walks);
|
|
3645
3451
|
const config = this.files.read(path2("config.json"), undefined, walks);
|
|
3646
3452
|
if (!manifest || !config)
|
|
@@ -3662,7 +3468,7 @@ class ServiceCoordination {
|
|
|
3662
3468
|
const registration = this.files.read(path2("registration.json"), undefined, walks);
|
|
3663
3469
|
const owner = this.files.read(path2("owner.lock"), undefined, walks);
|
|
3664
3470
|
const capability = this.files.read(path2("local-capability.json"), undefined, walks);
|
|
3665
|
-
const checkout = this.files.read(
|
|
3471
|
+
const checkout = this.files.read(join6(dirname6(directory2), "checkout.lock"), 8192, walks);
|
|
3666
3472
|
const value = registration ? decodeRegistration(json(registration)) : undefined;
|
|
3667
3473
|
if (value && (value.root !== root || value.stateHome !== this.stateHome || value.configIdentity !== config.identity || !endpointMatches(c.endpoint, value.endpoint)))
|
|
3668
3474
|
refused();
|
|
@@ -3952,10 +3758,10 @@ class ServiceCoordination {
|
|
|
3952
3758
|
throw new ContractError("capacity");
|
|
3953
3759
|
if (!/^[a-f0-9]{64}$/.test(entry.name) || protectedRoots.has(entry.name))
|
|
3954
3760
|
continue;
|
|
3955
|
-
const service =
|
|
3761
|
+
const service = join6(this.files.namespace, entry.name, "service-v1");
|
|
3956
3762
|
if (!this.files.directory(service))
|
|
3957
3763
|
continue;
|
|
3958
|
-
const manifest = this.files.read(
|
|
3764
|
+
const manifest = this.files.read(join6(service, "format.json"));
|
|
3959
3765
|
if (!manifest)
|
|
3960
3766
|
refused();
|
|
3961
3767
|
const f = closed(json(manifest), [
|
|
@@ -3992,13 +3798,13 @@ class ServiceCoordination {
|
|
|
3992
3798
|
proofs
|
|
3993
3799
|
]));
|
|
3994
3800
|
const epoch = hash(encoded([lock.identity, lock.stamp]));
|
|
3995
|
-
const archive = this.registryPath(
|
|
3801
|
+
const archive = this.registryPath(join6("recovery-v1", epoch));
|
|
3996
3802
|
const attempts = [];
|
|
3997
3803
|
let previousResolver = null;
|
|
3998
3804
|
if (this.files.directory(archive)) {
|
|
3999
3805
|
names(archive, Array.from({ length: 128 }, (_, n) => `${n}.json`));
|
|
4000
3806
|
for (let n = 0;n < 128; n++) {
|
|
4001
|
-
const attempt = this.files.read(
|
|
3807
|
+
const attempt = this.files.read(join6(archive, `${n}.json`), 32768);
|
|
4002
3808
|
if (!attempt) {
|
|
4003
3809
|
names(archive, attempts.map((_, i) => `${i}.json`));
|
|
4004
3810
|
break;
|
|
@@ -4042,7 +3848,7 @@ class ServiceCoordination {
|
|
|
4042
3848
|
if (state.attempts.length >= 128)
|
|
4043
3849
|
throw new ContractError("capacity");
|
|
4044
3850
|
assertAuthority();
|
|
4045
|
-
const receiptPath =
|
|
3851
|
+
const receiptPath = join6(state.archive, `${state.attempts.length}.json`);
|
|
4046
3852
|
const receipt = this.files.create(receiptPath, encoded({
|
|
4047
3853
|
schema: "cadence-registry-recovery/1",
|
|
4048
3854
|
lock: state.lock.bytes.toString("utf8"),
|
|
@@ -4055,7 +3861,7 @@ class ServiceCoordination {
|
|
|
4055
3861
|
if (current.stateDigest !== state.stateDigest || current.inspection.owner !== "absent" || current.attempts.length !== state.attempts.length + 1)
|
|
4056
3862
|
throw new ContractError("conflict");
|
|
4057
3863
|
for (const [n, attempt] of state.attempts.entries())
|
|
4058
|
-
this.files.assert(
|
|
3864
|
+
this.files.assert(join6(state.archive, `${n}.json`), attempt, 32768);
|
|
4059
3865
|
this.files.assert(receiptPath, receipt, 32768);
|
|
4060
3866
|
assertAuthority();
|
|
4061
3867
|
this.files.remove(this.registryPath("registry.lock"), state.lock);
|
|
@@ -4217,7 +4023,7 @@ class OwnerLease {
|
|
|
4217
4023
|
|
|
4218
4024
|
// src/service/gateway-cli.ts
|
|
4219
4025
|
import { homedir as homedir5 } from "node:os";
|
|
4220
|
-
import { dirname as
|
|
4026
|
+
import { dirname as dirname11, join as join13, resolve as resolve6 } from "node:path";
|
|
4221
4027
|
import { fileURLToPath } from "node:url";
|
|
4222
4028
|
|
|
4223
4029
|
// src/service/config.ts
|
|
@@ -4272,7 +4078,7 @@ function localOrigin(policy, actualPort) {
|
|
|
4272
4078
|
import { randomBytes as randomBytes4, randomUUID as randomUUID5 } from "node:crypto";
|
|
4273
4079
|
import { opendirSync as opendirSync2 } from "node:fs";
|
|
4274
4080
|
import { hostname as hostname3 } from "node:os";
|
|
4275
|
-
import { join as
|
|
4081
|
+
import { join as join8, resolve as resolve4 } from "node:path";
|
|
4276
4082
|
|
|
4277
4083
|
// src/service/auth.ts
|
|
4278
4084
|
import {
|
|
@@ -4281,7 +4087,7 @@ import {
|
|
|
4281
4087
|
randomUUID as randomUUID4,
|
|
4282
4088
|
timingSafeEqual
|
|
4283
4089
|
} from "node:crypto";
|
|
4284
|
-
import { dirname as
|
|
4090
|
+
import { dirname as dirname7, isAbsolute as isAbsolute5, join as join7 } from "node:path";
|
|
4285
4091
|
function sameSecret(candidate, expected) {
|
|
4286
4092
|
const a = createHash3("sha256").update(candidate).digest();
|
|
4287
4093
|
const b = createHash3("sha256").update(expected).digest();
|
|
@@ -4341,7 +4147,7 @@ function selectedMaterial(files, directory2, source) {
|
|
|
4341
4147
|
const config = sourceConfig(source);
|
|
4342
4148
|
if (config.source !== "config")
|
|
4343
4149
|
throw new ContractError("invalid");
|
|
4344
|
-
const marker =
|
|
4150
|
+
const marker = join7(directory2, "access-config.json"), access = join7(directory2, "access.json");
|
|
4345
4151
|
const markerFile = files.read(marker), accessFile = files.read(access);
|
|
4346
4152
|
if (!markerFile || !accessFile)
|
|
4347
4153
|
throw new ContractError("corrupt-selected");
|
|
@@ -4382,8 +4188,8 @@ class AccessMaterial {
|
|
|
4382
4188
|
}
|
|
4383
4189
|
static initialize(files, directory2, source = {}) {
|
|
4384
4190
|
const config = sourceConfig(source);
|
|
4385
|
-
const marker =
|
|
4386
|
-
if (files.read(marker) || files.read(access) || files.read(
|
|
4191
|
+
const marker = join7(directory2, "access-config.json"), access = join7(directory2, "access.json");
|
|
4192
|
+
if (files.read(marker) || files.read(access) || files.read(join7(directory2, "registration.json")) || files.read(join7(directory2, "owner.lock")))
|
|
4387
4193
|
throw new ContractError("conflict");
|
|
4388
4194
|
readSource(config);
|
|
4389
4195
|
files.create(marker, encoded(config));
|
|
@@ -4412,8 +4218,8 @@ class AccessMaterial {
|
|
|
4412
4218
|
this.directory = directory2;
|
|
4413
4219
|
this.assertOwner = assertOwner;
|
|
4414
4220
|
this.config = sourceConfig(source);
|
|
4415
|
-
const configFile = files.read(
|
|
4416
|
-
const accessFile = files.read(
|
|
4221
|
+
const configFile = files.read(join7(directory2, "access-config.json"));
|
|
4222
|
+
const accessFile = files.read(join7(directory2, "access.json"));
|
|
4417
4223
|
if (!configFile || !accessFile)
|
|
4418
4224
|
throw new ContractError("corrupt-selected");
|
|
4419
4225
|
const c = closed(json(configFile), ["schema", "source", "path"]);
|
|
@@ -4473,8 +4279,8 @@ class AccessMaterial {
|
|
|
4473
4279
|
assert() {
|
|
4474
4280
|
if (this.fenced)
|
|
4475
4281
|
throw new ContractError("quarantine");
|
|
4476
|
-
this.files.assert(
|
|
4477
|
-
this.files.assert(
|
|
4282
|
+
this.files.assert(join7(this.directory, "access-config.json"), this.configFile);
|
|
4283
|
+
this.files.assert(join7(this.directory, "access.json"), this.accessFile);
|
|
4478
4284
|
if (this.config.source === "file" && this.config.path)
|
|
4479
4285
|
this.external?.assert(this.config.path, this.externalFile, TOKEN_BYTES);
|
|
4480
4286
|
}
|
|
@@ -4516,7 +4322,7 @@ class AccessMaterial {
|
|
|
4516
4322
|
token: this.config.source === "generated" ? secret : null
|
|
4517
4323
|
};
|
|
4518
4324
|
this.assertRotation();
|
|
4519
|
-
this.accessFile = this.files.replace(
|
|
4325
|
+
this.accessFile = this.files.replace(join7(this.directory, "access.json"), this.accessFile, encoded(record));
|
|
4520
4326
|
this.assertRotation();
|
|
4521
4327
|
this.record = record;
|
|
4522
4328
|
this.secret = secret;
|
|
@@ -4531,7 +4337,7 @@ class AccessMaterial {
|
|
|
4531
4337
|
}
|
|
4532
4338
|
}
|
|
4533
4339
|
function readExternal(path2) {
|
|
4534
|
-
const files = new PrivateFiles(
|
|
4340
|
+
const files = new PrivateFiles(dirname7(path2));
|
|
4535
4341
|
const file = files.read(path2, TOKEN_BYTES);
|
|
4536
4342
|
if (!file)
|
|
4537
4343
|
throw new ContractError("corrupt-selected");
|
|
@@ -4701,11 +4507,11 @@ class GatewayStore {
|
|
|
4701
4507
|
constructor(serverConfig, stateHome = defaultStateHome()) {
|
|
4702
4508
|
this.serverConfig = serverConfig;
|
|
4703
4509
|
this.stateHome = canonicalAmbientRoot(stateHome);
|
|
4704
|
-
this.files = new PrivateFiles(
|
|
4705
|
-
this.directory =
|
|
4510
|
+
this.files = new PrivateFiles(join8(this.stateHome, "cadence-assistant-v1"));
|
|
4511
|
+
this.directory = join8(this.files.namespace, "workbenches-v1", hash(resolve4(serverConfig)));
|
|
4706
4512
|
}
|
|
4707
4513
|
path(name) {
|
|
4708
|
-
return
|
|
4514
|
+
return join8(this.directory, name);
|
|
4709
4515
|
}
|
|
4710
4516
|
read() {
|
|
4711
4517
|
const file = this.files.read(this.path("gateway.json"));
|
|
@@ -4797,7 +4603,7 @@ class GatewayStore {
|
|
|
4797
4603
|
for (let entry = stream.readSync();entry; entry = stream.readSync()) {
|
|
4798
4604
|
if (!entry.isFile() || !/^[a-f0-9]{64}\.json$/.test(entry.name) || out.length >= 128)
|
|
4799
4605
|
throw new ContractError("corrupt-selected");
|
|
4800
|
-
const file = this.files.read(
|
|
4606
|
+
const file = this.files.read(join8(directory2, entry.name));
|
|
4801
4607
|
if (!file)
|
|
4802
4608
|
throw new ContractError("corrupt-selected");
|
|
4803
4609
|
const project = decodeGatewayProject(json(file));
|
|
@@ -4845,8 +4651,8 @@ import { isatty } from "node:tty";
|
|
|
4845
4651
|
|
|
4846
4652
|
// src/history/recovery.ts
|
|
4847
4653
|
import { createHash as createHash6, randomUUID as randomUUID7 } from "node:crypto";
|
|
4848
|
-
import { lstatSync as
|
|
4849
|
-
import { join as
|
|
4654
|
+
import { lstatSync as lstatSync8, opendirSync as opendirSync4 } from "node:fs";
|
|
4655
|
+
import { join as join10, relative as relative4 } from "node:path";
|
|
4850
4656
|
|
|
4851
4657
|
// src/service/recovery.ts
|
|
4852
4658
|
async function inspectRecovery(coordination, history) {
|
|
@@ -4896,17 +4702,17 @@ async function resolveRecovery(coordination, confirmation, history) {
|
|
|
4896
4702
|
// src/history/files.ts
|
|
4897
4703
|
import { createHash as createHash5, randomBytes as randomBytes5, randomUUID as randomUUID6 } from "node:crypto";
|
|
4898
4704
|
import {
|
|
4899
|
-
closeSync as
|
|
4705
|
+
closeSync as closeSync6,
|
|
4900
4706
|
constants as constants5,
|
|
4901
4707
|
fstatSync as fstatSync4,
|
|
4902
4708
|
fsyncSync as fsyncSync3,
|
|
4903
|
-
lstatSync as
|
|
4709
|
+
lstatSync as lstatSync7,
|
|
4904
4710
|
opendirSync as opendirSync3,
|
|
4905
|
-
openSync as
|
|
4711
|
+
openSync as openSync6,
|
|
4906
4712
|
statfsSync
|
|
4907
4713
|
} from "node:fs";
|
|
4908
4714
|
import { open } from "node:fs/promises";
|
|
4909
|
-
import { dirname as
|
|
4715
|
+
import { dirname as dirname9, join as join9 } from "node:path";
|
|
4910
4716
|
|
|
4911
4717
|
// src/history/admission.ts
|
|
4912
4718
|
function decodeRunAdmission(value) {
|
|
@@ -4927,34 +4733,34 @@ function decodeRunAdmission(value) {
|
|
|
4927
4733
|
|
|
4928
4734
|
// src/history/read-io.ts
|
|
4929
4735
|
import {
|
|
4930
|
-
closeSync as
|
|
4736
|
+
closeSync as closeSync5,
|
|
4931
4737
|
constants as constants4,
|
|
4932
4738
|
fstatSync as fstatSync3,
|
|
4933
|
-
lstatSync as
|
|
4934
|
-
openSync as
|
|
4739
|
+
lstatSync as lstatSync6,
|
|
4740
|
+
openSync as openSync5,
|
|
4935
4741
|
readSync as readSync3
|
|
4936
4742
|
} from "node:fs";
|
|
4937
|
-
import { dirname as
|
|
4743
|
+
import { dirname as dirname8 } from "node:path";
|
|
4938
4744
|
function bad() {
|
|
4939
4745
|
throw new ContractError("corrupt-selected");
|
|
4940
4746
|
}
|
|
4941
4747
|
var stamp2 = (s) => [s.dev, s.ino, s.size, s.mode, s.uid, s.nlink, s.ctimeMs, s.mtimeMs].join(":");
|
|
4942
4748
|
function selected(files, path2, read, walks) {
|
|
4943
|
-
if (!files.files.directory(
|
|
4749
|
+
if (!files.files.directory(dirname8(path2), false, walks))
|
|
4944
4750
|
bad();
|
|
4945
|
-
const before =
|
|
4751
|
+
const before = lstatSync6(path2);
|
|
4946
4752
|
if (!before.isFile() || before.nlink !== 1 || process.platform !== "win32" && (before.mode & 63 || process.getuid && before.uid !== process.getuid()))
|
|
4947
4753
|
bad();
|
|
4948
|
-
const fd =
|
|
4754
|
+
const fd = openSync5(path2, constants4.O_RDONLY | (constants4.O_NOFOLLOW ?? 0));
|
|
4949
4755
|
try {
|
|
4950
4756
|
if (stamp2(fstatSync3(fd)) !== stamp2(before))
|
|
4951
4757
|
bad();
|
|
4952
4758
|
const result = read(fd, before.size, stamp2(before));
|
|
4953
|
-
if (stamp2(fstatSync3(fd)) !== stamp2(before) || stamp2(
|
|
4759
|
+
if (stamp2(fstatSync3(fd)) !== stamp2(before) || stamp2(lstatSync6(path2)) !== stamp2(before))
|
|
4954
4760
|
bad();
|
|
4955
4761
|
return result;
|
|
4956
4762
|
} finally {
|
|
4957
|
-
|
|
4763
|
+
closeSync5(fd);
|
|
4958
4764
|
}
|
|
4959
4765
|
}
|
|
4960
4766
|
function assertIncarnation(files, path2, expected) {
|
|
@@ -5437,14 +5243,14 @@ class HistoryFiles {
|
|
|
5437
5243
|
this.coordination = coordination;
|
|
5438
5244
|
this.format = format;
|
|
5439
5245
|
this.formatSnapshot = formatSnapshot;
|
|
5440
|
-
this.directory =
|
|
5246
|
+
this.directory = join9(coordination.directory, "history-v2");
|
|
5441
5247
|
this.files = coordination.files;
|
|
5442
5248
|
}
|
|
5443
5249
|
static open(coordination) {
|
|
5444
|
-
const dir =
|
|
5250
|
+
const dir = join9(coordination.directory, "history-v2");
|
|
5445
5251
|
if (!coordination.files.directory(dir))
|
|
5446
5252
|
return;
|
|
5447
|
-
const file = coordination.files.read(
|
|
5253
|
+
const file = coordination.files.read(join9(dir, "format.json"));
|
|
5448
5254
|
if (!file)
|
|
5449
5255
|
corrupt();
|
|
5450
5256
|
const format = decodeFormat(json(file));
|
|
@@ -5460,7 +5266,7 @@ class HistoryFiles {
|
|
|
5460
5266
|
existing.assertWritable();
|
|
5461
5267
|
return existing;
|
|
5462
5268
|
}
|
|
5463
|
-
const dir =
|
|
5269
|
+
const dir = join9(c.directory, "history-v2");
|
|
5464
5270
|
const format = {
|
|
5465
5271
|
schema: FORMAT,
|
|
5466
5272
|
storeId: randomUUID6(),
|
|
@@ -5470,19 +5276,19 @@ class HistoryFiles {
|
|
|
5470
5276
|
c.files.directory(dir, true);
|
|
5471
5277
|
for (const name of ["commands", "ledger", "runs", "indexes"]) {
|
|
5472
5278
|
lease.assert();
|
|
5473
|
-
c.files.directory(
|
|
5279
|
+
c.files.directory(join9(dir, name), true);
|
|
5474
5280
|
}
|
|
5475
5281
|
lease.assert();
|
|
5476
|
-
const keyPath =
|
|
5282
|
+
const keyPath = join9(dir, "read-proof-key.json");
|
|
5477
5283
|
c.files.create(keyPath, encoded({
|
|
5478
5284
|
schema: "cadence-history-read-key/1",
|
|
5479
5285
|
storeId: format.storeId,
|
|
5480
5286
|
key: randomBytes5(32).toString("base64url")
|
|
5481
5287
|
}));
|
|
5482
5288
|
lease.assert();
|
|
5483
|
-
const snapshot = c.files.create(
|
|
5289
|
+
const snapshot = c.files.create(join9(dir, "format.json"), encoded(format));
|
|
5484
5290
|
const result = new HistoryFiles(c, format, snapshot);
|
|
5485
|
-
result.syncParents(
|
|
5291
|
+
result.syncParents(join9(dir, "format.json"));
|
|
5486
5292
|
result.assertWritable();
|
|
5487
5293
|
return result;
|
|
5488
5294
|
}
|
|
@@ -5517,7 +5323,7 @@ class HistoryFiles {
|
|
|
5517
5323
|
throw new ContractError("quarantine");
|
|
5518
5324
|
}
|
|
5519
5325
|
assertFormat() {
|
|
5520
|
-
this.files.assert(
|
|
5326
|
+
this.files.assert(join9(this.directory, "format.json"), this.formatSnapshot);
|
|
5521
5327
|
}
|
|
5522
5328
|
readProofMaterial(budget) {
|
|
5523
5329
|
if (this.format.schema !== FORMAT)
|
|
@@ -5526,7 +5332,7 @@ class HistoryFiles {
|
|
|
5526
5332
|
this.assertReadFormat(budget);
|
|
5527
5333
|
else
|
|
5528
5334
|
this.assertFormat();
|
|
5529
|
-
const selected2 = this.readBudgeted(
|
|
5335
|
+
const selected2 = this.readBudgeted(join9(this.directory, "read-proof-key.json"), 512, budget);
|
|
5530
5336
|
if (!selected2 || this.proofSnapshot && (selected2.identity !== this.proofSnapshot.identity || selected2.stamp !== this.proofSnapshot.stamp))
|
|
5531
5337
|
corrupt();
|
|
5532
5338
|
try {
|
|
@@ -5554,17 +5360,17 @@ class HistoryFiles {
|
|
|
5554
5360
|
return;
|
|
5555
5361
|
}
|
|
5556
5362
|
this.assertFormat();
|
|
5557
|
-
assertIncarnation(this,
|
|
5363
|
+
assertIncarnation(this, join9(this.directory, "read-proof-key.json"), this.proofSnapshot.stamp);
|
|
5558
5364
|
}
|
|
5559
5365
|
segmentPath(segment) {
|
|
5560
|
-
return
|
|
5366
|
+
return join9(this.directory, "ledger", `${String(segment).padStart(12, "0")}.jsonl`);
|
|
5561
5367
|
}
|
|
5562
5368
|
artifactPath(ref) {
|
|
5563
|
-
return
|
|
5369
|
+
return join9(this.directory, "runs", ref.runId, "artifacts", ref.artifactId, `${ref.part}.part`);
|
|
5564
5370
|
}
|
|
5565
5371
|
admissionPath(runId) {
|
|
5566
5372
|
decodeId(runId);
|
|
5567
|
-
return
|
|
5373
|
+
return join9(this.directory, "runs", runId, "admission.json");
|
|
5568
5374
|
}
|
|
5569
5375
|
writeAdmission(lease, input) {
|
|
5570
5376
|
lease.assert();
|
|
@@ -5578,7 +5384,7 @@ class HistoryFiles {
|
|
|
5578
5384
|
return this.files.read(path2, maximum);
|
|
5579
5385
|
let size;
|
|
5580
5386
|
try {
|
|
5581
|
-
size =
|
|
5387
|
+
size = lstatSync7(path2).size;
|
|
5582
5388
|
} catch {
|
|
5583
5389
|
return this.files.read(path2, 0, budget.walks);
|
|
5584
5390
|
}
|
|
@@ -5590,7 +5396,7 @@ class HistoryFiles {
|
|
|
5590
5396
|
readCommand(commandId, budget) {
|
|
5591
5397
|
decodeId(commandId);
|
|
5592
5398
|
this.assertReadFormat(budget);
|
|
5593
|
-
return this.readBudgeted(
|
|
5399
|
+
return this.readBudgeted(join9(this.directory, "commands", commandId, "intent.json"), RECORD_BYTES, budget);
|
|
5594
5400
|
}
|
|
5595
5401
|
readAdmission(runId, identity, budget) {
|
|
5596
5402
|
return this.readAdmissionIdentity(runId, hash(canonicalJson(identity)), budget);
|
|
@@ -5611,24 +5417,24 @@ class HistoryFiles {
|
|
|
5611
5417
|
syncParents(file) {
|
|
5612
5418
|
if (process.platform === "win32")
|
|
5613
5419
|
return;
|
|
5614
|
-
for (let path2 =
|
|
5420
|
+
for (let path2 = dirname9(file);; path2 = dirname9(path2)) {
|
|
5615
5421
|
if (!this.files.directory(path2))
|
|
5616
5422
|
corrupt();
|
|
5617
|
-
const fd =
|
|
5423
|
+
const fd = openSync6(path2, constants5.O_RDONLY | constants5.O_DIRECTORY);
|
|
5618
5424
|
try {
|
|
5619
5425
|
fsyncSync3(fd);
|
|
5620
5426
|
} finally {
|
|
5621
|
-
|
|
5427
|
+
closeSync6(fd);
|
|
5622
5428
|
}
|
|
5623
5429
|
if (path2 === this.coordination.directory)
|
|
5624
5430
|
break;
|
|
5625
|
-
if (path2 ===
|
|
5431
|
+
if (path2 === dirname9(path2))
|
|
5626
5432
|
corrupt();
|
|
5627
5433
|
}
|
|
5628
5434
|
}
|
|
5629
5435
|
assertReadFormat(budget) {
|
|
5630
5436
|
budget.reserve(this.formatSnapshot.bytes.length);
|
|
5631
|
-
this.files.assert(
|
|
5437
|
+
this.files.assert(join9(this.directory, "format.json"), this.formatSnapshot, this.formatSnapshot.bytes.length);
|
|
5632
5438
|
}
|
|
5633
5439
|
assertPublished(segment, end, seq) {
|
|
5634
5440
|
const fence = publicationFences.get(this.directory);
|
|
@@ -5666,7 +5472,7 @@ class HistoryFiles {
|
|
|
5666
5472
|
const dir = opendirSync3(path2);
|
|
5667
5473
|
try {
|
|
5668
5474
|
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
5669
|
-
const p =
|
|
5475
|
+
const p = join9(path2, e.name), st = lstatSync7(p);
|
|
5670
5476
|
if (st.isDirectory())
|
|
5671
5477
|
bytes += walk(p, depth + 1);
|
|
5672
5478
|
else {
|
|
@@ -5701,7 +5507,7 @@ class HistoryFiles {
|
|
|
5701
5507
|
state.bytes = 0;
|
|
5702
5508
|
state.identity = null;
|
|
5703
5509
|
let count = 0;
|
|
5704
|
-
const ledger =
|
|
5510
|
+
const ledger = join9(this.directory, "ledger");
|
|
5705
5511
|
if (!this.files.directory(ledger))
|
|
5706
5512
|
corrupt();
|
|
5707
5513
|
const dir = opendirSync3(ledger);
|
|
@@ -5866,7 +5672,7 @@ class HistoryFiles {
|
|
|
5866
5672
|
let committed;
|
|
5867
5673
|
const fd = await open(path2, constants5.O_WRONLY | constants5.O_APPEND | (constants5.O_NOFOLLOW ?? 0));
|
|
5868
5674
|
const assertOriginal = (bytes) => {
|
|
5869
|
-
const original = fstatSync4(fd.fd), selected2 =
|
|
5675
|
+
const original = fstatSync4(fd.fd), selected2 = lstatSync7(path2);
|
|
5870
5676
|
if (original.dev !== selected2.dev || original.ino !== selected2.ino || original.nlink !== 1)
|
|
5871
5677
|
corrupt();
|
|
5872
5678
|
const snapshot = this.files.read(path2, LIMITS.segmentBytes);
|
|
@@ -5983,7 +5789,7 @@ class HistoryFiles {
|
|
|
5983
5789
|
try {
|
|
5984
5790
|
lease.assert();
|
|
5985
5791
|
this.files.assert(path2, before, LIMITS.segmentBytes);
|
|
5986
|
-
const opened = await fd.stat(), selected2 =
|
|
5792
|
+
const opened = await fd.stat(), selected2 = lstatSync7(path2);
|
|
5987
5793
|
if (opened.ino !== selected2.ino || opened.dev !== selected2.dev || opened.nlink !== 1)
|
|
5988
5794
|
corrupt();
|
|
5989
5795
|
beforeEffect?.();
|
|
@@ -6018,7 +5824,7 @@ class HistoryFiles {
|
|
|
6018
5824
|
checkpoint(lease, state) {
|
|
6019
5825
|
lease.assert();
|
|
6020
5826
|
this.assertWritable();
|
|
6021
|
-
const path2 =
|
|
5827
|
+
const path2 = join9(this.directory, "checkpoint.json"), old = this.files.read(path2);
|
|
6022
5828
|
const body = encoded({
|
|
6023
5829
|
schema: "cadence-history-checkpoint/2",
|
|
6024
5830
|
storeId: this.format.storeId,
|
|
@@ -6431,7 +6237,7 @@ function digestHistory(files, exclude) {
|
|
|
6431
6237
|
const walk = (path2, depth) => {
|
|
6432
6238
|
if (depth > 8 || !files.files.directory(path2))
|
|
6433
6239
|
throw new ContractError("corrupt-selected");
|
|
6434
|
-
const directory2 =
|
|
6240
|
+
const directory2 = lstatSync8(path2);
|
|
6435
6241
|
digest.update(`incarnation:${directory2.dev}:${directory2.ino}
|
|
6436
6242
|
`);
|
|
6437
6243
|
const dir = opendirSync4(path2), names2 = [];
|
|
@@ -6445,11 +6251,11 @@ function digestHistory(files, exclude) {
|
|
|
6445
6251
|
dir.closeSync();
|
|
6446
6252
|
}
|
|
6447
6253
|
for (const name of names2.sort()) {
|
|
6448
|
-
const selected2 =
|
|
6254
|
+
const selected2 = join10(path2, name);
|
|
6449
6255
|
if (selected2 === exclude)
|
|
6450
6256
|
continue;
|
|
6451
6257
|
const rel = relative4(files.directory, selected2);
|
|
6452
|
-
if (
|
|
6258
|
+
if (lstatSync8(selected2).isDirectory()) {
|
|
6453
6259
|
digest.update(`directory:${Buffer.byteLength(rel)}:${rel}
|
|
6454
6260
|
`);
|
|
6455
6261
|
walk(selected2, depth + 1);
|
|
@@ -6633,10 +6439,25 @@ class HistoryRecovery {
|
|
|
6633
6439
|
}
|
|
6634
6440
|
}
|
|
6635
6441
|
|
|
6442
|
+
// src/verification-diagnostics.ts
|
|
6443
|
+
function diagnosticText(value, maximum = 4096, secrets = []) {
|
|
6444
|
+
maximum = Math.max(0, Math.floor(maximum));
|
|
6445
|
+
let clean = value;
|
|
6446
|
+
for (const secret of [...new Set(secrets)].filter(Boolean).sort((a, b) => b.length - a.length))
|
|
6447
|
+
clean = clean.split(secret).join("[redacted]");
|
|
6448
|
+
clean = clean.replace(/\x1b\][\s\S]*?(?:\x07|\x1b\\)/g, "").replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "").replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/g, "").replace(/(Bearer\s+)\S+/gi, "$1[redacted]").replace(/((?:api[_-]?key|token|password|secret)\s*[:=]\s*)[^\s,;]+/gi, "$1[redacted]").replace(/(https?:\/\/)[^\s/@]+:[^\s/@]+@/gi, "$1[redacted]@");
|
|
6449
|
+
if (Buffer.byteLength(clean) <= maximum)
|
|
6450
|
+
return clean;
|
|
6451
|
+
const marker = maximum >= 12 ? `
|
|
6452
|
+
[truncated]` : ".".repeat(maximum);
|
|
6453
|
+
const bytes = Buffer.from(clean).subarray(0, Math.max(0, maximum - Buffer.byteLength(marker)));
|
|
6454
|
+
return bytes.toString("utf8").replace(/\ufffd$/, "") + marker;
|
|
6455
|
+
}
|
|
6456
|
+
|
|
6636
6457
|
// src/service/gateway-recovery.ts
|
|
6637
6458
|
import { opendirSync as opendirSync5 } from "node:fs";
|
|
6638
6459
|
import { hostname as hostname4 } from "node:os";
|
|
6639
|
-
import { join as
|
|
6460
|
+
import { join as join11 } from "node:path";
|
|
6640
6461
|
function required(resources, name) {
|
|
6641
6462
|
const file = resources.get(name);
|
|
6642
6463
|
if (!file)
|
|
@@ -6738,7 +6559,7 @@ function state(store) {
|
|
|
6738
6559
|
for (let n = 0;n < entries.length; n++) {
|
|
6739
6560
|
if (!entries.includes(`${n}.json`))
|
|
6740
6561
|
throw new ContractError("corrupt-selected");
|
|
6741
|
-
const file = store.files.read(
|
|
6562
|
+
const file = store.files.read(join11(archive, `${n}.json`), 65536);
|
|
6742
6563
|
if (!file)
|
|
6743
6564
|
throw new ContractError("corrupt-selected");
|
|
6744
6565
|
const row = closed(json(file, 65536), [
|
|
@@ -6819,7 +6640,7 @@ function resolveGatewayRecovery(store, expected, assertAuthority, legacyHostConf
|
|
|
6819
6640
|
throw new ContractError("quarantine");
|
|
6820
6641
|
if (selected2.attempts.length >= 128)
|
|
6821
6642
|
throw new ContractError("capacity");
|
|
6822
|
-
const receiptPath =
|
|
6643
|
+
const receiptPath = join11(selected2.archive, `${selected2.attempts.length}.json`);
|
|
6823
6644
|
const receipt = store.files.create(receiptPath, encoded({
|
|
6824
6645
|
schema: "cadence-workbench-recovery/1",
|
|
6825
6646
|
lock: selected2.lock.bytes.toString("utf8"),
|
|
@@ -6844,7 +6665,7 @@ function resolveGatewayRecovery(store, expected, assertAuthority, legacyHostConf
|
|
|
6844
6665
|
if (names2(store, selected2.archive, 128).length !== selected2.attempts.length + 1)
|
|
6845
6666
|
throw new ContractError("conflict");
|
|
6846
6667
|
for (const [n, file] of selected2.attempts.entries())
|
|
6847
|
-
store.files.assert(
|
|
6668
|
+
store.files.assert(join11(selected2.archive, `${n}.json`), file, 65536);
|
|
6848
6669
|
store.files.assert(receiptPath, receipt, 65536);
|
|
6849
6670
|
};
|
|
6850
6671
|
assert();
|
|
@@ -7080,7 +6901,7 @@ async function localGatewayRecovery(store, operation) {
|
|
|
7080
6901
|
|
|
7081
6902
|
// src/service/runtime.ts
|
|
7082
6903
|
import { homedir as homedir4 } from "node:os";
|
|
7083
|
-
import { dirname as
|
|
6904
|
+
import { dirname as dirname10, isAbsolute as isAbsolute6, join as join12, resolve as resolve5 } from "node:path";
|
|
7084
6905
|
var STARTUP_INPUT_BYTES = 16 * 1024;
|
|
7085
6906
|
function absolute(value) {
|
|
7086
6907
|
const result = text(value, 4096, true);
|
|
@@ -7165,7 +6986,7 @@ function selectStart(options, ambient) {
|
|
|
7165
6986
|
throw new ContractError("invalid");
|
|
7166
6987
|
let fromFile;
|
|
7167
6988
|
if (options.config) {
|
|
7168
|
-
const path2 = resolve5(options.config), f = new PrivateFiles(
|
|
6989
|
+
const path2 = resolve5(options.config), f = new PrivateFiles(dirname10(path2));
|
|
7169
6990
|
const bytes = f.read(path2);
|
|
7170
6991
|
if (!bytes)
|
|
7171
6992
|
throw new ContractError("corrupt-selected");
|
|
@@ -7180,7 +7001,7 @@ function selectStart(options, ambient) {
|
|
|
7180
7001
|
port: options.port ?? 17423,
|
|
7181
7002
|
proxy: null
|
|
7182
7003
|
},
|
|
7183
|
-
agentDir: resolve5(options.agentDir ?? ambient.PI_CODING_AGENT_DIR ??
|
|
7004
|
+
agentDir: resolve5(options.agentDir ?? ambient.PI_CODING_AGENT_DIR ?? join12(ambient.HOME ?? ambient.USERPROFILE ?? homedir4(), ".pi", "agent")),
|
|
7184
7005
|
tokenSource: options.tokenFile ? "file" : env ? "environment" : "generated",
|
|
7185
7006
|
tokenFile: options.tokenFile ? resolve5(options.tokenFile) : null
|
|
7186
7007
|
});
|
|
@@ -7303,7 +7124,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7303
7124
|
proxy: null
|
|
7304
7125
|
});
|
|
7305
7126
|
if (policyFile) {
|
|
7306
|
-
const file = new PrivateFiles(
|
|
7127
|
+
const file = new PrivateFiles(dirname11(policyFile)).read(policyFile);
|
|
7307
7128
|
if (!file)
|
|
7308
7129
|
throw new ContractError("invalid");
|
|
7309
7130
|
policy = decodeHttpPolicy(json(file));
|
|
@@ -7312,7 +7133,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7312
7133
|
kind: "workbench",
|
|
7313
7134
|
serverConfig: config,
|
|
7314
7135
|
stateHome: store.stateHome,
|
|
7315
|
-
agentDir: agentDir ?? selected2?.agentDir ?? resolve6(process.env.PI_CODING_AGENT_DIR ??
|
|
7136
|
+
agentDir: agentDir ?? selected2?.agentDir ?? resolve6(process.env.PI_CODING_AGENT_DIR ?? join13(homedir5(), ".pi", "agent")),
|
|
7316
7137
|
policy: { ...policy, port: server.port }
|
|
7317
7138
|
});
|
|
7318
7139
|
const status = await serviceStatus(store);
|
|
@@ -7321,7 +7142,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7321
7142
|
throw new ContractError("conflict");
|
|
7322
7143
|
result = status;
|
|
7323
7144
|
} else
|
|
7324
|
-
result = await launchHost(input, store, foreground, hostUrl,
|
|
7145
|
+
result = await launchHost(input, store, foreground, hostUrl, dirname11(dirname11(fileURLToPath(hostUrl))), env, network);
|
|
7325
7146
|
if (!foreground && result && typeof result === "object" && "status" in result && result.status === "alive") {
|
|
7326
7147
|
const reply = closed(await nativeCall(store, "token-link", true), [
|
|
7327
7148
|
"link"
|
|
@@ -7353,12 +7174,12 @@ No project or default workspace was created.
|
|
|
7353
7174
|
|
|
7354
7175
|
// src/service/launchd.ts
|
|
7355
7176
|
import { rmSync as rmSync2 } from "node:fs";
|
|
7356
|
-
import { dirname as
|
|
7177
|
+
import { dirname as dirname12, join as join14 } from "node:path";
|
|
7357
7178
|
var LABEL = "com.abelxiaoxing.cadence";
|
|
7358
7179
|
var MARKER2 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
7359
7180
|
<!-- Generated by @abelxiaoxing/cadence`;
|
|
7360
7181
|
function plistPath(s) {
|
|
7361
|
-
return
|
|
7182
|
+
return join14(s.home, "Library", "LaunchAgents", `${LABEL}.plist`);
|
|
7362
7183
|
}
|
|
7363
7184
|
var xml = (value) => checked(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
7364
7185
|
function buildPlist(u) {
|
|
@@ -7383,7 +7204,7 @@ function buildPlist(u) {
|
|
|
7383
7204
|
"\t</array>",
|
|
7384
7205
|
...u.workspace === null ? ["<!-- Cadence workbench gateway; no project workspace -->"] : [],
|
|
7385
7206
|
"\t<key>WorkingDirectory</key>",
|
|
7386
|
-
` <string>${xml(u.workspace ??
|
|
7207
|
+
` <string>${xml(u.workspace ?? dirname12(dirname12(u.cli)))}</string>`,
|
|
7387
7208
|
"\t<key>EnvironmentVariables</key>",
|
|
7388
7209
|
"\t<dict>",
|
|
7389
7210
|
...u.environment.flatMap(([k, v]) => [
|
|
@@ -7438,11 +7259,11 @@ async function configureLaunchAgent(s, mode, requested) {
|
|
|
7438
7259
|
s.launchctl(["bootout", target(s)]);
|
|
7439
7260
|
write(path2, buildPlist({
|
|
7440
7261
|
node: s.node,
|
|
7441
|
-
cli:
|
|
7262
|
+
cli: join14(s.root, "src", "service-cli.mjs"),
|
|
7442
7263
|
workspace,
|
|
7443
7264
|
serverConfig: serverConfigPath(s.home),
|
|
7444
7265
|
environment: serviceEnvironment(s),
|
|
7445
|
-
log:
|
|
7266
|
+
log: join14(s.home, "Library", "Logs", "cadence.log")
|
|
7446
7267
|
}));
|
|
7447
7268
|
const result = {
|
|
7448
7269
|
unit: path2,
|
|
@@ -7453,7 +7274,7 @@ async function configureLaunchAgent(s, mode, requested) {
|
|
|
7453
7274
|
s.launchctl(["enable", target(s)]);
|
|
7454
7275
|
else if (disabled(s))
|
|
7455
7276
|
return { status: "disabled", ...result };
|
|
7456
|
-
if (workspace !== null && !regularFile(
|
|
7277
|
+
if (workspace !== null && !regularFile(join14(agentDirectory(s), "auth.json")))
|
|
7457
7278
|
return { status: "waiting-for-pi-login", ...result };
|
|
7458
7279
|
const started = s.launchctl(["bootstrap", domain(s), path2]).status === 0;
|
|
7459
7280
|
return { status: started ? "started" : "failed", ...result };
|
|
@@ -7502,14 +7323,14 @@ function launchAgentStatus(s) {
|
|
|
7502
7323
|
|
|
7503
7324
|
// src/service/login-run.ts
|
|
7504
7325
|
import { rmSync as rmSync3 } from "node:fs";
|
|
7505
|
-
import { dirname as
|
|
7326
|
+
import { dirname as dirname13, isAbsolute as isAbsolute7, join as join15 } from "node:path";
|
|
7506
7327
|
var RUN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
|
7507
7328
|
var APPROVED_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run";
|
|
7508
7329
|
var RUN_VALUE = "Cadence";
|
|
7509
7330
|
var MARKER3 = "' Generated by @abelxiaoxing/cadence";
|
|
7510
7331
|
function launcherPath(s) {
|
|
7511
7332
|
const local = s.env.LOCALAPPDATA;
|
|
7512
|
-
return
|
|
7333
|
+
return join15(local && isAbsolute7(local) ? local : join15(s.home, "AppData", "Local"), "cadence-server", "cadence.vbs");
|
|
7513
7334
|
}
|
|
7514
7335
|
var literal2 = (value) => `"${checked(value).replaceAll('"', '""')}"`;
|
|
7515
7336
|
function argument2(value) {
|
|
@@ -7526,7 +7347,7 @@ function buildLauncher(u) {
|
|
|
7526
7347
|
...u.environment.length ? ['Set env = shell.Environment("Process")'] : [],
|
|
7527
7348
|
...u.environment.map(([k, v]) => `env(${literal2(k)}) = ${literal2(v)}`),
|
|
7528
7349
|
...u.workspace === null ? ["' Cadence workbench gateway; no project workspace"] : [],
|
|
7529
|
-
`shell.CurrentDirectory = ${literal2(u.workspace ??
|
|
7350
|
+
`shell.CurrentDirectory = ${literal2(u.workspace ?? dirname13(dirname13(u.cli)))}`,
|
|
7530
7351
|
`shell.Run ${literal2(command)}, 0, False`,
|
|
7531
7352
|
""
|
|
7532
7353
|
].join(`\r
|
|
@@ -7565,7 +7386,7 @@ async function configureLoginRun(s, mode, requested) {
|
|
|
7565
7386
|
const environment = s.env.PI_CODING_AGENT_DIR ? [["PI_CODING_AGENT_DIR", agentDirectory(s)]] : [];
|
|
7566
7387
|
write(path2, utf16(buildLauncher({
|
|
7567
7388
|
node: s.node,
|
|
7568
|
-
cli:
|
|
7389
|
+
cli: join15(s.root, "src", "service-cli.mjs"),
|
|
7569
7390
|
workspace,
|
|
7570
7391
|
serverConfig: serverConfigPath(s.home),
|
|
7571
7392
|
environment
|
|
@@ -7581,7 +7402,7 @@ async function configureLoginRun(s, mode, requested) {
|
|
|
7581
7402
|
return { status: "failed", ...result };
|
|
7582
7403
|
s.reg(["delete", APPROVED_KEY, "/v", RUN_VALUE, "/f"]);
|
|
7583
7404
|
}
|
|
7584
|
-
if (workspace !== null && !regularFile(
|
|
7405
|
+
if (workspace !== null && !regularFile(join15(agentDirectory(s), "auth.json")))
|
|
7585
7406
|
return { status: "waiting-for-pi-login", ...result };
|
|
7586
7407
|
return { status: s.launch(path2) ? "started" : "failed", ...result };
|
|
7587
7408
|
}
|
|
@@ -7630,8 +7451,8 @@ async function loginRunStatus(s) {
|
|
|
7630
7451
|
}
|
|
7631
7452
|
|
|
7632
7453
|
// src/service/system-service.ts
|
|
7633
|
-
import { existsSync as existsSync2, realpathSync as
|
|
7634
|
-
import { isAbsolute as isAbsolute8, join as
|
|
7454
|
+
import { existsSync as existsSync2, realpathSync as realpathSync5, rmSync as rmSync4 } from "node:fs";
|
|
7455
|
+
import { isAbsolute as isAbsolute8, join as join16 } from "node:path";
|
|
7635
7456
|
var MARKER4 = "# Generated by @abelxiaoxing/cadence";
|
|
7636
7457
|
function boundary(s) {
|
|
7637
7458
|
if (s.platform !== "linux" || s.scope !== "system")
|
|
@@ -7712,12 +7533,12 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7712
7533
|
const selected2 = selectWorkspace(s, requested, mode === "install" && requested === undefined ? null : unitWorkspace(previous), false);
|
|
7713
7534
|
if (selected2.workspace !== null && !sdk)
|
|
7714
7535
|
return { status: "missing-pi-sdk", scope: "system" };
|
|
7715
|
-
const selectedReal = selected2.workspace && existsSync2(selected2.workspace) ?
|
|
7536
|
+
const selectedReal = selected2.workspace && existsSync2(selected2.workspace) ? realpathSync5(selected2.workspace) : undefined;
|
|
7716
7537
|
const agentDir = agentDirectory(s);
|
|
7717
7538
|
const content = buildUnit({
|
|
7718
7539
|
system: true,
|
|
7719
7540
|
node: s.node,
|
|
7720
|
-
cli:
|
|
7541
|
+
cli: join16(s.root, "src", "service-cli.mjs"),
|
|
7721
7542
|
workspace: selected2.workspace,
|
|
7722
7543
|
serverConfig: serviceConfigPath(s),
|
|
7723
7544
|
agentDir,
|
|
@@ -7749,7 +7570,7 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7749
7570
|
return { status: "foreign", ...result };
|
|
7750
7571
|
if (selectedReal && selected2.workspace) {
|
|
7751
7572
|
try {
|
|
7752
|
-
if (
|
|
7573
|
+
if (realpathSync5(selected2.workspace) !== selectedReal)
|
|
7753
7574
|
return { status: "workspace-changed", ...result };
|
|
7754
7575
|
} catch {
|
|
7755
7576
|
return { status: "workspace-changed", ...result };
|
|
@@ -7766,7 +7587,7 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7766
7587
|
return { status: "failed", ...result, ...selected2 };
|
|
7767
7588
|
if (mode === "install" && s.systemctl(["enable", s.unit]).status !== 0)
|
|
7768
7589
|
return { status: "failed", ...result, ...selected2 };
|
|
7769
|
-
if (selected2.workspace !== null && !regularFile(
|
|
7590
|
+
if (selected2.workspace !== null && !regularFile(join16(agentDir, "auth.json")))
|
|
7770
7591
|
return { status: "waiting-for-pi-login", ...result, ...selected2 };
|
|
7771
7592
|
s.systemctl(["reset-failed", s.unit]);
|
|
7772
7593
|
return {
|
|
@@ -7995,10 +7816,10 @@ function startupGuidance(result, input) {
|
|
|
7995
7816
|
return `Pi's agent directory ${agent} must be an existing real directory (not a symlink) owned by you.
|
|
7996
7817
|
`;
|
|
7997
7818
|
case "pi-auth":
|
|
7998
|
-
return `${
|
|
7819
|
+
return `${join17(agent, "auth.json")} must be your own single-link regular file holding valid Pi credentials; log in to Pi first.
|
|
7999
7820
|
`;
|
|
8000
7821
|
case "pi-models":
|
|
8001
|
-
return `${
|
|
7822
|
+
return `${join17(agent, "models.json")} must be your own single-link regular file using only fields Cadence's pinned Pi SDK supports; a field only newer Pi understands is refused.
|
|
8002
7823
|
`;
|
|
8003
7824
|
case "pi-runtime":
|
|
8004
7825
|
return `Loading Pi's SDK model runtime failed after the owner registered (optional SDK packages missing, or sources the SDK refuses); it stays quarantined.
|
|
@@ -8216,7 +8037,7 @@ async function serverAction(s, action, requested, linger) {
|
|
|
8216
8037
|
}
|
|
8217
8038
|
}
|
|
8218
8039
|
function serverGuidance(s, action, result, link) {
|
|
8219
|
-
const cli = piUserInstall(s) ? `node ${JSON.stringify(
|
|
8040
|
+
const cli = piUserInstall(s) ? `node ${JSON.stringify(join17(s.root, "src", "service-cli.mjs"))}` : "cadence";
|
|
8220
8041
|
const note = result.redirectedFrom ? `Cadence workspace: ${result.workspace} (${result.redirectedFrom} contains Cadence's private state)
|
|
8221
8042
|
` : "";
|
|
8222
8043
|
return note + serverStatusGuidance(s, action, result, cli, link);
|
|
@@ -8283,7 +8104,7 @@ ${manage}
|
|
|
8283
8104
|
${linger}`;
|
|
8284
8105
|
}
|
|
8285
8106
|
case "waiting-for-pi-login":
|
|
8286
|
-
return `Cadence is registered for ${result.workspace}; it starts once Pi has ${
|
|
8107
|
+
return `Cadence is registered for ${result.workspace}; it starts once Pi has ${join17(agentDirectory(s), "auth.json")}.
|
|
8287
8108
|
Log in to Pi, then: ${cli} server restart${flags}
|
|
8288
8109
|
${config}
|
|
8289
8110
|
${lingerGuidance(result.linger)}`;
|
|
@@ -8390,7 +8211,7 @@ async function serverMain(argv, hostUrl) {
|
|
|
8390
8211
|
} else
|
|
8391
8212
|
throw new ContractError("invalid");
|
|
8392
8213
|
}
|
|
8393
|
-
const s = hostSystem(
|
|
8214
|
+
const s = hostSystem(dirname14(dirname14(fileURLToPath2(hostUrl))), ownerControl, {
|
|
8394
8215
|
scope,
|
|
8395
8216
|
name
|
|
8396
8217
|
});
|