@abelxiaoxing/cadence 2.0.22 → 2.0.24
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 +4 -10
- package/README.es.md +4 -10
- package/README.fr.md +4 -10
- package/README.ja.md +4 -10
- package/README.ko.md +4 -10
- package/README.md +5 -13
- package/README.pt-BR.md +4 -10
- package/README.ru.md +4 -10
- package/README.zh-CN.md +4 -12
- package/docs/conversation-mode.md +22 -2
- package/docs/history.md +50 -0
- package/docs/service-management.md +1 -1
- package/package.json +9 -7
- package/src/agent-panel-cadence.ts +4 -4
- package/src/cadence-mode.ts +284 -166
- package/src/coordinator-tools.mjs +9 -0
- 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 +1 -1
- package/src/native-workflow-history.ts +134 -0
- package/src/operator-cli.mjs +93 -65
- package/src/research-tools.mjs +365 -0
- package/src/sdk-worker.mjs +59 -9
- package/src/service/checkout-lock.ts +142 -0
- package/src/service/contracts/workbench.ts +43 -23
- package/src/service/contracts/workflow.ts +137 -2
- package/src/service/coordination.ts +6 -7
- package/src/service/private-files.ts +2 -1
- package/src/service-cli.mjs +259 -425
- package/src/service-host.mjs +3680 -6048
- package/src/worker-public.mjs +16 -1
- package/src/workspace.ts +302 -0
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +16 -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() {
|
|
@@ -1130,10 +1073,23 @@ function decodeCaptureEvent(v) {
|
|
|
1130
1073
|
const kind = tag(v);
|
|
1131
1074
|
switch (kind) {
|
|
1132
1075
|
case "ready": {
|
|
1133
|
-
const o = closed(v, [
|
|
1076
|
+
const o = closed(v, [
|
|
1077
|
+
"kind",
|
|
1078
|
+
"requestProtocol",
|
|
1079
|
+
...Object.hasOwn(v ?? {}, "runtime") ? ["runtime"] : []
|
|
1080
|
+
]);
|
|
1134
1081
|
return {
|
|
1135
1082
|
kind,
|
|
1136
|
-
requestProtocol: literal(o.requestProtocol, WORKER_PROTOCOL)
|
|
1083
|
+
requestProtocol: literal(o.requestProtocol, WORKER_PROTOCOL),
|
|
1084
|
+
...o.runtime === undefined ? {} : {
|
|
1085
|
+
runtime: (() => {
|
|
1086
|
+
const r = closed(o.runtime, ["sdkVersion", "workerHash"]);
|
|
1087
|
+
return {
|
|
1088
|
+
sdkVersion: text(r.sdkVersion, 128, true),
|
|
1089
|
+
workerHash: decodeHash(r.workerHash)
|
|
1090
|
+
};
|
|
1091
|
+
})()
|
|
1092
|
+
}
|
|
1137
1093
|
};
|
|
1138
1094
|
}
|
|
1139
1095
|
case "message-start": {
|
|
@@ -2084,25 +2040,20 @@ class CaptureBudgetLease {
|
|
|
2084
2040
|
this.forget();
|
|
2085
2041
|
}
|
|
2086
2042
|
}
|
|
2087
|
-
// src/
|
|
2043
|
+
// src/workspace.ts
|
|
2088
2044
|
import { spawnSync } from "node:child_process";
|
|
2089
|
-
import { createHash
|
|
2045
|
+
import { createHash } from "node:crypto";
|
|
2090
2046
|
import {
|
|
2091
2047
|
closeSync as closeSync2,
|
|
2092
2048
|
constants as constants2,
|
|
2093
2049
|
fstatSync,
|
|
2094
|
-
fsyncSync,
|
|
2095
2050
|
lstatSync,
|
|
2096
2051
|
mkdirSync as mkdirSync2,
|
|
2097
2052
|
openSync as openSync2,
|
|
2098
|
-
readdirSync,
|
|
2099
2053
|
readSync,
|
|
2100
|
-
realpathSync
|
|
2101
|
-
renameSync,
|
|
2102
|
-
unlinkSync,
|
|
2103
|
-
writeFileSync
|
|
2054
|
+
realpathSync
|
|
2104
2055
|
} from "node:fs";
|
|
2105
|
-
import { homedir as homedir2
|
|
2056
|
+
import { homedir as homedir2 } from "node:os";
|
|
2106
2057
|
import {
|
|
2107
2058
|
dirname,
|
|
2108
2059
|
isAbsolute,
|
|
@@ -2112,23 +2063,6 @@ import {
|
|
|
2112
2063
|
resolve,
|
|
2113
2064
|
sep
|
|
2114
2065
|
} 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
2066
|
function parseRecord(data) {
|
|
2133
2067
|
try {
|
|
2134
2068
|
return JSON.parse(data.toString("utf8"));
|
|
@@ -2264,13 +2198,6 @@ function defaultStateHome() {
|
|
|
2264
2198
|
throw new Error("State home must be absolute");
|
|
2265
2199
|
return candidate ?? join(homedir2(), process.platform === "win32" ? "AppData/Local" : ".local/state");
|
|
2266
2200
|
}
|
|
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
2201
|
function canonicalAmbientRoot(path2) {
|
|
2275
2202
|
const full = resolve(path2);
|
|
2276
2203
|
try {
|
|
@@ -2311,213 +2238,6 @@ function effectiveWorkspace(requested, stateHome, _home) {
|
|
|
2311
2238
|
return { workspace: requested, redirected: false };
|
|
2312
2239
|
}
|
|
2313
2240
|
|
|
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
2241
|
// src/service/autostart.ts
|
|
2522
2242
|
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
2523
2243
|
import {
|
|
@@ -2525,10 +2245,10 @@ import {
|
|
|
2525
2245
|
mkdirSync as mkdirSync5,
|
|
2526
2246
|
readFileSync,
|
|
2527
2247
|
realpathSync as realpathSync3,
|
|
2528
|
-
renameSync as
|
|
2248
|
+
renameSync as renameSync2,
|
|
2529
2249
|
rmSync,
|
|
2530
2250
|
statSync as statSync2,
|
|
2531
|
-
writeFileSync as
|
|
2251
|
+
writeFileSync as writeFileSync2
|
|
2532
2252
|
} from "node:fs";
|
|
2533
2253
|
import { homedir as homedir3 } from "node:os";
|
|
2534
2254
|
import {
|
|
@@ -2585,19 +2305,19 @@ function parseEnvFile(content) {
|
|
|
2585
2305
|
}
|
|
2586
2306
|
|
|
2587
2307
|
// src/service/private-files.ts
|
|
2588
|
-
import { createHash as createHash2, randomUUID
|
|
2308
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
2589
2309
|
import {
|
|
2590
2310
|
closeSync as closeSync3,
|
|
2591
2311
|
constants as constants3,
|
|
2592
2312
|
fstatSync as fstatSync2,
|
|
2593
|
-
fsyncSync
|
|
2313
|
+
fsyncSync,
|
|
2594
2314
|
lstatSync as lstatSync2,
|
|
2595
2315
|
mkdirSync as mkdirSync3,
|
|
2596
2316
|
openSync as openSync3,
|
|
2597
2317
|
readSync as readSync2,
|
|
2598
|
-
renameSync
|
|
2599
|
-
unlinkSync
|
|
2600
|
-
writeFileSync
|
|
2318
|
+
renameSync,
|
|
2319
|
+
unlinkSync,
|
|
2320
|
+
writeFileSync
|
|
2601
2321
|
} from "node:fs";
|
|
2602
2322
|
import { dirname as dirname2, isAbsolute as isAbsolute2, join as join2, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
|
|
2603
2323
|
var METADATA_BYTES = 16 * 1024;
|
|
@@ -2767,8 +2487,8 @@ class PrivateFiles {
|
|
|
2767
2487
|
this.directory(dirname2(path2), true);
|
|
2768
2488
|
const fd = retryWindowsFileOperation(() => openSync3(path2, "wx", 384));
|
|
2769
2489
|
try {
|
|
2770
|
-
|
|
2771
|
-
|
|
2490
|
+
writeFileSync(fd, bytes);
|
|
2491
|
+
fsyncSync(fd);
|
|
2772
2492
|
} finally {
|
|
2773
2493
|
closeSync3(fd);
|
|
2774
2494
|
}
|
|
@@ -2780,10 +2500,10 @@ class PrivateFiles {
|
|
|
2780
2500
|
}
|
|
2781
2501
|
replace(path2, expected, bytes, maximum = METADATA_BYTES) {
|
|
2782
2502
|
this.assert(path2, expected, maximum);
|
|
2783
|
-
const temporary = join2(dirname2(path2), `pending-${
|
|
2503
|
+
const temporary = join2(dirname2(path2), `pending-${randomUUID()}.json`);
|
|
2784
2504
|
this.create(temporary, bytes, maximum);
|
|
2785
2505
|
this.assert(path2, expected, maximum);
|
|
2786
|
-
retryWindowsFileOperation(() =>
|
|
2506
|
+
retryWindowsFileOperation(() => renameSync(temporary, path2), () => this.assert(path2, expected, maximum));
|
|
2787
2507
|
this.syncDirectory(dirname2(path2));
|
|
2788
2508
|
const result = this.read(path2, maximum);
|
|
2789
2509
|
if (!result || result.identity !== hash(bytes))
|
|
@@ -2792,7 +2512,7 @@ class PrivateFiles {
|
|
|
2792
2512
|
}
|
|
2793
2513
|
remove(path2, expected, maximum = METADATA_BYTES) {
|
|
2794
2514
|
this.assert(path2, expected, maximum);
|
|
2795
|
-
retryWindowsFileOperation(() =>
|
|
2515
|
+
retryWindowsFileOperation(() => unlinkSync(path2), () => this.assert(path2, expected, maximum));
|
|
2796
2516
|
this.syncDirectory(dirname2(path2));
|
|
2797
2517
|
}
|
|
2798
2518
|
syncDirectory(path2) {
|
|
@@ -2800,7 +2520,7 @@ class PrivateFiles {
|
|
|
2800
2520
|
return;
|
|
2801
2521
|
const fd = openSync3(path2, constants3.O_RDONLY | constants3.O_DIRECTORY);
|
|
2802
2522
|
try {
|
|
2803
|
-
|
|
2523
|
+
fsyncSync(fd);
|
|
2804
2524
|
} finally {
|
|
2805
2525
|
closeSync3(fd);
|
|
2806
2526
|
}
|
|
@@ -3285,8 +3005,8 @@ function write(path2, content) {
|
|
|
3285
3005
|
mkdirSync5(dirname4(path2), { recursive: true });
|
|
3286
3006
|
const temporary = `${path2}.${process.pid}.tmp`;
|
|
3287
3007
|
try {
|
|
3288
|
-
|
|
3289
|
-
|
|
3008
|
+
writeFileSync2(temporary, content, { mode: 420, flag: "wx" });
|
|
3009
|
+
renameSync2(temporary, path2);
|
|
3290
3010
|
} finally {
|
|
3291
3011
|
rmSync(temporary, { force: true });
|
|
3292
3012
|
}
|
|
@@ -3429,7 +3149,106 @@ function autostartStatus(s) {
|
|
|
3429
3149
|
import { randomBytes as randomBytes2, randomUUID as randomUUID3 } from "node:crypto";
|
|
3430
3150
|
import { opendirSync } from "node:fs";
|
|
3431
3151
|
import { hostname as hostname2 } from "node:os";
|
|
3432
|
-
import { dirname as
|
|
3152
|
+
import { dirname as dirname6, isAbsolute as isAbsolute4, join as join6, relative as relative3, sep as sep4 } from "node:path";
|
|
3153
|
+
|
|
3154
|
+
// src/service/checkout-lock.ts
|
|
3155
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
3156
|
+
import {
|
|
3157
|
+
closeSync as closeSync4,
|
|
3158
|
+
fsyncSync as fsyncSync2,
|
|
3159
|
+
lstatSync as lstatSync5,
|
|
3160
|
+
openSync as openSync4,
|
|
3161
|
+
realpathSync as realpathSync4,
|
|
3162
|
+
unlinkSync as unlinkSync2,
|
|
3163
|
+
writeFileSync as writeFileSync3
|
|
3164
|
+
} from "node:fs";
|
|
3165
|
+
import { hostname } from "node:os";
|
|
3166
|
+
import { dirname as dirname5, join as join5 } from "node:path";
|
|
3167
|
+
class CheckoutStore {
|
|
3168
|
+
root;
|
|
3169
|
+
privateRoot;
|
|
3170
|
+
constructor(root, stateHome = defaultStateHome()) {
|
|
3171
|
+
this.root = realpathSync4(root);
|
|
3172
|
+
if (privateWorkspace(this.root, stateHome))
|
|
3173
|
+
throw new Error("Private state cannot be a workspace");
|
|
3174
|
+
this.privateRoot = privateRootFor(this.root, stateHome);
|
|
3175
|
+
if (contains(this.root, this.privateRoot))
|
|
3176
|
+
throw new Error("Private state home must be outside the working copy");
|
|
3177
|
+
}
|
|
3178
|
+
get lockPath() {
|
|
3179
|
+
return join5(this.privateRoot, "checkout.lock");
|
|
3180
|
+
}
|
|
3181
|
+
inspectPrivate() {
|
|
3182
|
+
const namespace = dirname5(this.privateRoot);
|
|
3183
|
+
for (const path2 of [namespace, this.privateRoot]) {
|
|
3184
|
+
if (!safeDirectory(path2))
|
|
3185
|
+
continue;
|
|
3186
|
+
const st = lstatSync5(path2);
|
|
3187
|
+
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
3188
|
+
throw new Error("Private state permissions refused");
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
inspectPrivateFile(path2, max) {
|
|
3192
|
+
this.inspectPrivate();
|
|
3193
|
+
if (!safeDirectory(this.privateRoot))
|
|
3194
|
+
return;
|
|
3195
|
+
try {
|
|
3196
|
+
const st = lstatSync5(path2);
|
|
3197
|
+
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1 || st.size > max || process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
3198
|
+
throw new Error("Unsafe private state file");
|
|
3199
|
+
} catch (error) {
|
|
3200
|
+
if (!absent(error))
|
|
3201
|
+
throw error;
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
inspectLock() {
|
|
3205
|
+
this.inspectPrivateFile(this.lockPath, 8192);
|
|
3206
|
+
const data = readBounded(this.lockPath, 8192);
|
|
3207
|
+
if (!data)
|
|
3208
|
+
return;
|
|
3209
|
+
const lock = parseRecord(data);
|
|
3210
|
+
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)
|
|
3211
|
+
throw new Error("Unknown or corrupt coordination lock");
|
|
3212
|
+
return lock;
|
|
3213
|
+
}
|
|
3214
|
+
acquire() {
|
|
3215
|
+
this.inspectPrivate();
|
|
3216
|
+
safeDirectory(this.privateRoot, true);
|
|
3217
|
+
const lock = {
|
|
3218
|
+
schema: "cadence-assistant-lock/1",
|
|
3219
|
+
token: randomUUID2(),
|
|
3220
|
+
runId: randomUUID2(),
|
|
3221
|
+
pid: process.pid,
|
|
3222
|
+
host: hostname(),
|
|
3223
|
+
root: this.root
|
|
3224
|
+
};
|
|
3225
|
+
let fd;
|
|
3226
|
+
try {
|
|
3227
|
+
fd = retryWindowsFileOperation(() => openSync4(this.lockPath, "wx", 384));
|
|
3228
|
+
} catch {
|
|
3229
|
+
throw new Error("Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner");
|
|
3230
|
+
}
|
|
3231
|
+
try {
|
|
3232
|
+
writeFileSync3(fd, JSON.stringify(lock));
|
|
3233
|
+
fsyncSync2(fd);
|
|
3234
|
+
} finally {
|
|
3235
|
+
closeSync4(fd);
|
|
3236
|
+
}
|
|
3237
|
+
return lock;
|
|
3238
|
+
}
|
|
3239
|
+
assertOwner(lock) {
|
|
3240
|
+
if (JSON.stringify(this.inspectLock()) !== JSON.stringify(lock))
|
|
3241
|
+
throw new Error("Coordination lock changed");
|
|
3242
|
+
}
|
|
3243
|
+
release(lock) {
|
|
3244
|
+
if (lock.pid !== process.pid || lock.host !== hostname())
|
|
3245
|
+
throw new Error("Only the owning process may release a lock");
|
|
3246
|
+
this.assertOwner(lock);
|
|
3247
|
+
retryWindowsFileOperation(() => unlinkSync2(this.lockPath), () => this.assertOwner(lock));
|
|
3248
|
+
}
|
|
3249
|
+
}
|
|
3250
|
+
|
|
3251
|
+
// src/service/coordination.ts
|
|
3433
3252
|
var registrationKeys = [
|
|
3434
3253
|
"schema",
|
|
3435
3254
|
"root",
|
|
@@ -3568,11 +3387,11 @@ class ServiceCoordination {
|
|
|
3568
3387
|
registryDirectory;
|
|
3569
3388
|
stateHome;
|
|
3570
3389
|
constructor(workspace, stateHome) {
|
|
3571
|
-
this.project = workspace === null ? undefined : new
|
|
3572
|
-
this.stateHome = this.project ?
|
|
3573
|
-
const namespace =
|
|
3390
|
+
this.project = workspace === null ? undefined : new CheckoutStore(resolveWorkspace(workspace).root, stateHome);
|
|
3391
|
+
this.stateHome = this.project ? dirname6(dirname6(this.project.privateRoot)) : canonicalAmbientRoot(stateHome ?? defaultStateHome());
|
|
3392
|
+
const namespace = join6(this.stateHome, "cadence-assistant-v1");
|
|
3574
3393
|
this.files = new PrivateFiles(namespace);
|
|
3575
|
-
this.registryDirectory =
|
|
3394
|
+
this.registryDirectory = join6(namespace, "service-registry-v1");
|
|
3576
3395
|
}
|
|
3577
3396
|
get store() {
|
|
3578
3397
|
if (!this.project)
|
|
@@ -3580,13 +3399,13 @@ class ServiceCoordination {
|
|
|
3580
3399
|
return this.project;
|
|
3581
3400
|
}
|
|
3582
3401
|
get directory() {
|
|
3583
|
-
return
|
|
3402
|
+
return join6(this.store.privateRoot, "service-v1");
|
|
3584
3403
|
}
|
|
3585
3404
|
path(name) {
|
|
3586
|
-
return
|
|
3405
|
+
return join6(this.directory, name);
|
|
3587
3406
|
}
|
|
3588
3407
|
registryPath(name) {
|
|
3589
|
-
return
|
|
3408
|
+
return join6(this.registryDirectory, name);
|
|
3590
3409
|
}
|
|
3591
3410
|
registry() {
|
|
3592
3411
|
if (!this.files.directory(this.registryDirectory))
|
|
@@ -3633,14 +3452,14 @@ class ServiceCoordination {
|
|
|
3633
3452
|
"conversation-workflows-v1",
|
|
3634
3453
|
"workflows-v1"
|
|
3635
3454
|
]);
|
|
3636
|
-
this.files.directory(
|
|
3455
|
+
this.files.directory(join6(directory2, "history-v2"), false, walks);
|
|
3637
3456
|
}
|
|
3638
3457
|
selected(directory2 = this.directory, root = this.store.root, stoppedOnly = false) {
|
|
3639
3458
|
const walks = new DirectoryWalks;
|
|
3640
3459
|
if (!this.files.directory(directory2, false, walks))
|
|
3641
3460
|
return;
|
|
3642
3461
|
this.assertNamespace(walks, directory2);
|
|
3643
|
-
const path2 = (name) =>
|
|
3462
|
+
const path2 = (name) => join6(directory2, name);
|
|
3644
3463
|
const manifest = this.files.read(path2("format.json"), undefined, walks);
|
|
3645
3464
|
const config = this.files.read(path2("config.json"), undefined, walks);
|
|
3646
3465
|
if (!manifest || !config)
|
|
@@ -3662,7 +3481,7 @@ class ServiceCoordination {
|
|
|
3662
3481
|
const registration = this.files.read(path2("registration.json"), undefined, walks);
|
|
3663
3482
|
const owner = this.files.read(path2("owner.lock"), undefined, walks);
|
|
3664
3483
|
const capability = this.files.read(path2("local-capability.json"), undefined, walks);
|
|
3665
|
-
const checkout = this.files.read(
|
|
3484
|
+
const checkout = this.files.read(join6(dirname6(directory2), "checkout.lock"), 8192, walks);
|
|
3666
3485
|
const value = registration ? decodeRegistration(json(registration)) : undefined;
|
|
3667
3486
|
if (value && (value.root !== root || value.stateHome !== this.stateHome || value.configIdentity !== config.identity || !endpointMatches(c.endpoint, value.endpoint)))
|
|
3668
3487
|
refused();
|
|
@@ -3952,10 +3771,10 @@ class ServiceCoordination {
|
|
|
3952
3771
|
throw new ContractError("capacity");
|
|
3953
3772
|
if (!/^[a-f0-9]{64}$/.test(entry.name) || protectedRoots.has(entry.name))
|
|
3954
3773
|
continue;
|
|
3955
|
-
const service =
|
|
3774
|
+
const service = join6(this.files.namespace, entry.name, "service-v1");
|
|
3956
3775
|
if (!this.files.directory(service))
|
|
3957
3776
|
continue;
|
|
3958
|
-
const manifest = this.files.read(
|
|
3777
|
+
const manifest = this.files.read(join6(service, "format.json"));
|
|
3959
3778
|
if (!manifest)
|
|
3960
3779
|
refused();
|
|
3961
3780
|
const f = closed(json(manifest), [
|
|
@@ -3992,13 +3811,13 @@ class ServiceCoordination {
|
|
|
3992
3811
|
proofs
|
|
3993
3812
|
]));
|
|
3994
3813
|
const epoch = hash(encoded([lock.identity, lock.stamp]));
|
|
3995
|
-
const archive = this.registryPath(
|
|
3814
|
+
const archive = this.registryPath(join6("recovery-v1", epoch));
|
|
3996
3815
|
const attempts = [];
|
|
3997
3816
|
let previousResolver = null;
|
|
3998
3817
|
if (this.files.directory(archive)) {
|
|
3999
3818
|
names(archive, Array.from({ length: 128 }, (_, n) => `${n}.json`));
|
|
4000
3819
|
for (let n = 0;n < 128; n++) {
|
|
4001
|
-
const attempt = this.files.read(
|
|
3820
|
+
const attempt = this.files.read(join6(archive, `${n}.json`), 32768);
|
|
4002
3821
|
if (!attempt) {
|
|
4003
3822
|
names(archive, attempts.map((_, i) => `${i}.json`));
|
|
4004
3823
|
break;
|
|
@@ -4042,7 +3861,7 @@ class ServiceCoordination {
|
|
|
4042
3861
|
if (state.attempts.length >= 128)
|
|
4043
3862
|
throw new ContractError("capacity");
|
|
4044
3863
|
assertAuthority();
|
|
4045
|
-
const receiptPath =
|
|
3864
|
+
const receiptPath = join6(state.archive, `${state.attempts.length}.json`);
|
|
4046
3865
|
const receipt = this.files.create(receiptPath, encoded({
|
|
4047
3866
|
schema: "cadence-registry-recovery/1",
|
|
4048
3867
|
lock: state.lock.bytes.toString("utf8"),
|
|
@@ -4055,7 +3874,7 @@ class ServiceCoordination {
|
|
|
4055
3874
|
if (current.stateDigest !== state.stateDigest || current.inspection.owner !== "absent" || current.attempts.length !== state.attempts.length + 1)
|
|
4056
3875
|
throw new ContractError("conflict");
|
|
4057
3876
|
for (const [n, attempt] of state.attempts.entries())
|
|
4058
|
-
this.files.assert(
|
|
3877
|
+
this.files.assert(join6(state.archive, `${n}.json`), attempt, 32768);
|
|
4059
3878
|
this.files.assert(receiptPath, receipt, 32768);
|
|
4060
3879
|
assertAuthority();
|
|
4061
3880
|
this.files.remove(this.registryPath("registry.lock"), state.lock);
|
|
@@ -4217,7 +4036,7 @@ class OwnerLease {
|
|
|
4217
4036
|
|
|
4218
4037
|
// src/service/gateway-cli.ts
|
|
4219
4038
|
import { homedir as homedir5 } from "node:os";
|
|
4220
|
-
import { dirname as
|
|
4039
|
+
import { dirname as dirname11, join as join13, resolve as resolve6 } from "node:path";
|
|
4221
4040
|
import { fileURLToPath } from "node:url";
|
|
4222
4041
|
|
|
4223
4042
|
// src/service/config.ts
|
|
@@ -4272,7 +4091,7 @@ function localOrigin(policy, actualPort) {
|
|
|
4272
4091
|
import { randomBytes as randomBytes4, randomUUID as randomUUID5 } from "node:crypto";
|
|
4273
4092
|
import { opendirSync as opendirSync2 } from "node:fs";
|
|
4274
4093
|
import { hostname as hostname3 } from "node:os";
|
|
4275
|
-
import { join as
|
|
4094
|
+
import { join as join8, resolve as resolve4 } from "node:path";
|
|
4276
4095
|
|
|
4277
4096
|
// src/service/auth.ts
|
|
4278
4097
|
import {
|
|
@@ -4281,7 +4100,7 @@ import {
|
|
|
4281
4100
|
randomUUID as randomUUID4,
|
|
4282
4101
|
timingSafeEqual
|
|
4283
4102
|
} from "node:crypto";
|
|
4284
|
-
import { dirname as
|
|
4103
|
+
import { dirname as dirname7, isAbsolute as isAbsolute5, join as join7 } from "node:path";
|
|
4285
4104
|
function sameSecret(candidate, expected) {
|
|
4286
4105
|
const a = createHash3("sha256").update(candidate).digest();
|
|
4287
4106
|
const b = createHash3("sha256").update(expected).digest();
|
|
@@ -4341,7 +4160,7 @@ function selectedMaterial(files, directory2, source) {
|
|
|
4341
4160
|
const config = sourceConfig(source);
|
|
4342
4161
|
if (config.source !== "config")
|
|
4343
4162
|
throw new ContractError("invalid");
|
|
4344
|
-
const marker =
|
|
4163
|
+
const marker = join7(directory2, "access-config.json"), access = join7(directory2, "access.json");
|
|
4345
4164
|
const markerFile = files.read(marker), accessFile = files.read(access);
|
|
4346
4165
|
if (!markerFile || !accessFile)
|
|
4347
4166
|
throw new ContractError("corrupt-selected");
|
|
@@ -4382,8 +4201,8 @@ class AccessMaterial {
|
|
|
4382
4201
|
}
|
|
4383
4202
|
static initialize(files, directory2, source = {}) {
|
|
4384
4203
|
const config = sourceConfig(source);
|
|
4385
|
-
const marker =
|
|
4386
|
-
if (files.read(marker) || files.read(access) || files.read(
|
|
4204
|
+
const marker = join7(directory2, "access-config.json"), access = join7(directory2, "access.json");
|
|
4205
|
+
if (files.read(marker) || files.read(access) || files.read(join7(directory2, "registration.json")) || files.read(join7(directory2, "owner.lock")))
|
|
4387
4206
|
throw new ContractError("conflict");
|
|
4388
4207
|
readSource(config);
|
|
4389
4208
|
files.create(marker, encoded(config));
|
|
@@ -4412,8 +4231,8 @@ class AccessMaterial {
|
|
|
4412
4231
|
this.directory = directory2;
|
|
4413
4232
|
this.assertOwner = assertOwner;
|
|
4414
4233
|
this.config = sourceConfig(source);
|
|
4415
|
-
const configFile = files.read(
|
|
4416
|
-
const accessFile = files.read(
|
|
4234
|
+
const configFile = files.read(join7(directory2, "access-config.json"));
|
|
4235
|
+
const accessFile = files.read(join7(directory2, "access.json"));
|
|
4417
4236
|
if (!configFile || !accessFile)
|
|
4418
4237
|
throw new ContractError("corrupt-selected");
|
|
4419
4238
|
const c = closed(json(configFile), ["schema", "source", "path"]);
|
|
@@ -4473,8 +4292,8 @@ class AccessMaterial {
|
|
|
4473
4292
|
assert() {
|
|
4474
4293
|
if (this.fenced)
|
|
4475
4294
|
throw new ContractError("quarantine");
|
|
4476
|
-
this.files.assert(
|
|
4477
|
-
this.files.assert(
|
|
4295
|
+
this.files.assert(join7(this.directory, "access-config.json"), this.configFile);
|
|
4296
|
+
this.files.assert(join7(this.directory, "access.json"), this.accessFile);
|
|
4478
4297
|
if (this.config.source === "file" && this.config.path)
|
|
4479
4298
|
this.external?.assert(this.config.path, this.externalFile, TOKEN_BYTES);
|
|
4480
4299
|
}
|
|
@@ -4516,7 +4335,7 @@ class AccessMaterial {
|
|
|
4516
4335
|
token: this.config.source === "generated" ? secret : null
|
|
4517
4336
|
};
|
|
4518
4337
|
this.assertRotation();
|
|
4519
|
-
this.accessFile = this.files.replace(
|
|
4338
|
+
this.accessFile = this.files.replace(join7(this.directory, "access.json"), this.accessFile, encoded(record));
|
|
4520
4339
|
this.assertRotation();
|
|
4521
4340
|
this.record = record;
|
|
4522
4341
|
this.secret = secret;
|
|
@@ -4531,7 +4350,7 @@ class AccessMaterial {
|
|
|
4531
4350
|
}
|
|
4532
4351
|
}
|
|
4533
4352
|
function readExternal(path2) {
|
|
4534
|
-
const files = new PrivateFiles(
|
|
4353
|
+
const files = new PrivateFiles(dirname7(path2));
|
|
4535
4354
|
const file = files.read(path2, TOKEN_BYTES);
|
|
4536
4355
|
if (!file)
|
|
4537
4356
|
throw new ContractError("corrupt-selected");
|
|
@@ -4701,11 +4520,11 @@ class GatewayStore {
|
|
|
4701
4520
|
constructor(serverConfig, stateHome = defaultStateHome()) {
|
|
4702
4521
|
this.serverConfig = serverConfig;
|
|
4703
4522
|
this.stateHome = canonicalAmbientRoot(stateHome);
|
|
4704
|
-
this.files = new PrivateFiles(
|
|
4705
|
-
this.directory =
|
|
4523
|
+
this.files = new PrivateFiles(join8(this.stateHome, "cadence-assistant-v1"));
|
|
4524
|
+
this.directory = join8(this.files.namespace, "workbenches-v1", hash(resolve4(serverConfig)));
|
|
4706
4525
|
}
|
|
4707
4526
|
path(name) {
|
|
4708
|
-
return
|
|
4527
|
+
return join8(this.directory, name);
|
|
4709
4528
|
}
|
|
4710
4529
|
read() {
|
|
4711
4530
|
const file = this.files.read(this.path("gateway.json"));
|
|
@@ -4797,7 +4616,7 @@ class GatewayStore {
|
|
|
4797
4616
|
for (let entry = stream.readSync();entry; entry = stream.readSync()) {
|
|
4798
4617
|
if (!entry.isFile() || !/^[a-f0-9]{64}\.json$/.test(entry.name) || out.length >= 128)
|
|
4799
4618
|
throw new ContractError("corrupt-selected");
|
|
4800
|
-
const file = this.files.read(
|
|
4619
|
+
const file = this.files.read(join8(directory2, entry.name));
|
|
4801
4620
|
if (!file)
|
|
4802
4621
|
throw new ContractError("corrupt-selected");
|
|
4803
4622
|
const project = decodeGatewayProject(json(file));
|
|
@@ -4845,8 +4664,8 @@ import { isatty } from "node:tty";
|
|
|
4845
4664
|
|
|
4846
4665
|
// src/history/recovery.ts
|
|
4847
4666
|
import { createHash as createHash6, randomUUID as randomUUID7 } from "node:crypto";
|
|
4848
|
-
import { lstatSync as
|
|
4849
|
-
import { join as
|
|
4667
|
+
import { lstatSync as lstatSync8, opendirSync as opendirSync4 } from "node:fs";
|
|
4668
|
+
import { join as join10, relative as relative4 } from "node:path";
|
|
4850
4669
|
|
|
4851
4670
|
// src/service/recovery.ts
|
|
4852
4671
|
async function inspectRecovery(coordination, history) {
|
|
@@ -4896,17 +4715,17 @@ async function resolveRecovery(coordination, confirmation, history) {
|
|
|
4896
4715
|
// src/history/files.ts
|
|
4897
4716
|
import { createHash as createHash5, randomBytes as randomBytes5, randomUUID as randomUUID6 } from "node:crypto";
|
|
4898
4717
|
import {
|
|
4899
|
-
closeSync as
|
|
4718
|
+
closeSync as closeSync6,
|
|
4900
4719
|
constants as constants5,
|
|
4901
4720
|
fstatSync as fstatSync4,
|
|
4902
4721
|
fsyncSync as fsyncSync3,
|
|
4903
|
-
lstatSync as
|
|
4722
|
+
lstatSync as lstatSync7,
|
|
4904
4723
|
opendirSync as opendirSync3,
|
|
4905
|
-
openSync as
|
|
4724
|
+
openSync as openSync6,
|
|
4906
4725
|
statfsSync
|
|
4907
4726
|
} from "node:fs";
|
|
4908
4727
|
import { open } from "node:fs/promises";
|
|
4909
|
-
import { dirname as
|
|
4728
|
+
import { dirname as dirname9, join as join9 } from "node:path";
|
|
4910
4729
|
|
|
4911
4730
|
// src/history/admission.ts
|
|
4912
4731
|
function decodeRunAdmission(value) {
|
|
@@ -4927,34 +4746,34 @@ function decodeRunAdmission(value) {
|
|
|
4927
4746
|
|
|
4928
4747
|
// src/history/read-io.ts
|
|
4929
4748
|
import {
|
|
4930
|
-
closeSync as
|
|
4749
|
+
closeSync as closeSync5,
|
|
4931
4750
|
constants as constants4,
|
|
4932
4751
|
fstatSync as fstatSync3,
|
|
4933
|
-
lstatSync as
|
|
4934
|
-
openSync as
|
|
4752
|
+
lstatSync as lstatSync6,
|
|
4753
|
+
openSync as openSync5,
|
|
4935
4754
|
readSync as readSync3
|
|
4936
4755
|
} from "node:fs";
|
|
4937
|
-
import { dirname as
|
|
4756
|
+
import { dirname as dirname8 } from "node:path";
|
|
4938
4757
|
function bad() {
|
|
4939
4758
|
throw new ContractError("corrupt-selected");
|
|
4940
4759
|
}
|
|
4941
4760
|
var stamp2 = (s) => [s.dev, s.ino, s.size, s.mode, s.uid, s.nlink, s.ctimeMs, s.mtimeMs].join(":");
|
|
4942
4761
|
function selected(files, path2, read, walks) {
|
|
4943
|
-
if (!files.files.directory(
|
|
4762
|
+
if (!files.files.directory(dirname8(path2), false, walks))
|
|
4944
4763
|
bad();
|
|
4945
|
-
const before =
|
|
4764
|
+
const before = lstatSync6(path2);
|
|
4946
4765
|
if (!before.isFile() || before.nlink !== 1 || process.platform !== "win32" && (before.mode & 63 || process.getuid && before.uid !== process.getuid()))
|
|
4947
4766
|
bad();
|
|
4948
|
-
const fd =
|
|
4767
|
+
const fd = openSync5(path2, constants4.O_RDONLY | (constants4.O_NOFOLLOW ?? 0));
|
|
4949
4768
|
try {
|
|
4950
4769
|
if (stamp2(fstatSync3(fd)) !== stamp2(before))
|
|
4951
4770
|
bad();
|
|
4952
4771
|
const result = read(fd, before.size, stamp2(before));
|
|
4953
|
-
if (stamp2(fstatSync3(fd)) !== stamp2(before) || stamp2(
|
|
4772
|
+
if (stamp2(fstatSync3(fd)) !== stamp2(before) || stamp2(lstatSync6(path2)) !== stamp2(before))
|
|
4954
4773
|
bad();
|
|
4955
4774
|
return result;
|
|
4956
4775
|
} finally {
|
|
4957
|
-
|
|
4776
|
+
closeSync5(fd);
|
|
4958
4777
|
}
|
|
4959
4778
|
}
|
|
4960
4779
|
function assertIncarnation(files, path2, expected) {
|
|
@@ -5437,14 +5256,14 @@ class HistoryFiles {
|
|
|
5437
5256
|
this.coordination = coordination;
|
|
5438
5257
|
this.format = format;
|
|
5439
5258
|
this.formatSnapshot = formatSnapshot;
|
|
5440
|
-
this.directory =
|
|
5259
|
+
this.directory = join9(coordination.directory, "history-v2");
|
|
5441
5260
|
this.files = coordination.files;
|
|
5442
5261
|
}
|
|
5443
5262
|
static open(coordination) {
|
|
5444
|
-
const dir =
|
|
5263
|
+
const dir = join9(coordination.directory, "history-v2");
|
|
5445
5264
|
if (!coordination.files.directory(dir))
|
|
5446
5265
|
return;
|
|
5447
|
-
const file = coordination.files.read(
|
|
5266
|
+
const file = coordination.files.read(join9(dir, "format.json"));
|
|
5448
5267
|
if (!file)
|
|
5449
5268
|
corrupt();
|
|
5450
5269
|
const format = decodeFormat(json(file));
|
|
@@ -5460,7 +5279,7 @@ class HistoryFiles {
|
|
|
5460
5279
|
existing.assertWritable();
|
|
5461
5280
|
return existing;
|
|
5462
5281
|
}
|
|
5463
|
-
const dir =
|
|
5282
|
+
const dir = join9(c.directory, "history-v2");
|
|
5464
5283
|
const format = {
|
|
5465
5284
|
schema: FORMAT,
|
|
5466
5285
|
storeId: randomUUID6(),
|
|
@@ -5470,19 +5289,19 @@ class HistoryFiles {
|
|
|
5470
5289
|
c.files.directory(dir, true);
|
|
5471
5290
|
for (const name of ["commands", "ledger", "runs", "indexes"]) {
|
|
5472
5291
|
lease.assert();
|
|
5473
|
-
c.files.directory(
|
|
5292
|
+
c.files.directory(join9(dir, name), true);
|
|
5474
5293
|
}
|
|
5475
5294
|
lease.assert();
|
|
5476
|
-
const keyPath =
|
|
5295
|
+
const keyPath = join9(dir, "read-proof-key.json");
|
|
5477
5296
|
c.files.create(keyPath, encoded({
|
|
5478
5297
|
schema: "cadence-history-read-key/1",
|
|
5479
5298
|
storeId: format.storeId,
|
|
5480
5299
|
key: randomBytes5(32).toString("base64url")
|
|
5481
5300
|
}));
|
|
5482
5301
|
lease.assert();
|
|
5483
|
-
const snapshot = c.files.create(
|
|
5302
|
+
const snapshot = c.files.create(join9(dir, "format.json"), encoded(format));
|
|
5484
5303
|
const result = new HistoryFiles(c, format, snapshot);
|
|
5485
|
-
result.syncParents(
|
|
5304
|
+
result.syncParents(join9(dir, "format.json"));
|
|
5486
5305
|
result.assertWritable();
|
|
5487
5306
|
return result;
|
|
5488
5307
|
}
|
|
@@ -5517,7 +5336,7 @@ class HistoryFiles {
|
|
|
5517
5336
|
throw new ContractError("quarantine");
|
|
5518
5337
|
}
|
|
5519
5338
|
assertFormat() {
|
|
5520
|
-
this.files.assert(
|
|
5339
|
+
this.files.assert(join9(this.directory, "format.json"), this.formatSnapshot);
|
|
5521
5340
|
}
|
|
5522
5341
|
readProofMaterial(budget) {
|
|
5523
5342
|
if (this.format.schema !== FORMAT)
|
|
@@ -5526,7 +5345,7 @@ class HistoryFiles {
|
|
|
5526
5345
|
this.assertReadFormat(budget);
|
|
5527
5346
|
else
|
|
5528
5347
|
this.assertFormat();
|
|
5529
|
-
const selected2 = this.readBudgeted(
|
|
5348
|
+
const selected2 = this.readBudgeted(join9(this.directory, "read-proof-key.json"), 512, budget);
|
|
5530
5349
|
if (!selected2 || this.proofSnapshot && (selected2.identity !== this.proofSnapshot.identity || selected2.stamp !== this.proofSnapshot.stamp))
|
|
5531
5350
|
corrupt();
|
|
5532
5351
|
try {
|
|
@@ -5554,17 +5373,17 @@ class HistoryFiles {
|
|
|
5554
5373
|
return;
|
|
5555
5374
|
}
|
|
5556
5375
|
this.assertFormat();
|
|
5557
|
-
assertIncarnation(this,
|
|
5376
|
+
assertIncarnation(this, join9(this.directory, "read-proof-key.json"), this.proofSnapshot.stamp);
|
|
5558
5377
|
}
|
|
5559
5378
|
segmentPath(segment) {
|
|
5560
|
-
return
|
|
5379
|
+
return join9(this.directory, "ledger", `${String(segment).padStart(12, "0")}.jsonl`);
|
|
5561
5380
|
}
|
|
5562
5381
|
artifactPath(ref) {
|
|
5563
|
-
return
|
|
5382
|
+
return join9(this.directory, "runs", ref.runId, "artifacts", ref.artifactId, `${ref.part}.part`);
|
|
5564
5383
|
}
|
|
5565
5384
|
admissionPath(runId) {
|
|
5566
5385
|
decodeId(runId);
|
|
5567
|
-
return
|
|
5386
|
+
return join9(this.directory, "runs", runId, "admission.json");
|
|
5568
5387
|
}
|
|
5569
5388
|
writeAdmission(lease, input) {
|
|
5570
5389
|
lease.assert();
|
|
@@ -5578,7 +5397,7 @@ class HistoryFiles {
|
|
|
5578
5397
|
return this.files.read(path2, maximum);
|
|
5579
5398
|
let size;
|
|
5580
5399
|
try {
|
|
5581
|
-
size =
|
|
5400
|
+
size = lstatSync7(path2).size;
|
|
5582
5401
|
} catch {
|
|
5583
5402
|
return this.files.read(path2, 0, budget.walks);
|
|
5584
5403
|
}
|
|
@@ -5590,7 +5409,7 @@ class HistoryFiles {
|
|
|
5590
5409
|
readCommand(commandId, budget) {
|
|
5591
5410
|
decodeId(commandId);
|
|
5592
5411
|
this.assertReadFormat(budget);
|
|
5593
|
-
return this.readBudgeted(
|
|
5412
|
+
return this.readBudgeted(join9(this.directory, "commands", commandId, "intent.json"), RECORD_BYTES, budget);
|
|
5594
5413
|
}
|
|
5595
5414
|
readAdmission(runId, identity, budget) {
|
|
5596
5415
|
return this.readAdmissionIdentity(runId, hash(canonicalJson(identity)), budget);
|
|
@@ -5611,24 +5430,24 @@ class HistoryFiles {
|
|
|
5611
5430
|
syncParents(file) {
|
|
5612
5431
|
if (process.platform === "win32")
|
|
5613
5432
|
return;
|
|
5614
|
-
for (let path2 =
|
|
5433
|
+
for (let path2 = dirname9(file);; path2 = dirname9(path2)) {
|
|
5615
5434
|
if (!this.files.directory(path2))
|
|
5616
5435
|
corrupt();
|
|
5617
|
-
const fd =
|
|
5436
|
+
const fd = openSync6(path2, constants5.O_RDONLY | constants5.O_DIRECTORY);
|
|
5618
5437
|
try {
|
|
5619
5438
|
fsyncSync3(fd);
|
|
5620
5439
|
} finally {
|
|
5621
|
-
|
|
5440
|
+
closeSync6(fd);
|
|
5622
5441
|
}
|
|
5623
5442
|
if (path2 === this.coordination.directory)
|
|
5624
5443
|
break;
|
|
5625
|
-
if (path2 ===
|
|
5444
|
+
if (path2 === dirname9(path2))
|
|
5626
5445
|
corrupt();
|
|
5627
5446
|
}
|
|
5628
5447
|
}
|
|
5629
5448
|
assertReadFormat(budget) {
|
|
5630
5449
|
budget.reserve(this.formatSnapshot.bytes.length);
|
|
5631
|
-
this.files.assert(
|
|
5450
|
+
this.files.assert(join9(this.directory, "format.json"), this.formatSnapshot, this.formatSnapshot.bytes.length);
|
|
5632
5451
|
}
|
|
5633
5452
|
assertPublished(segment, end, seq) {
|
|
5634
5453
|
const fence = publicationFences.get(this.directory);
|
|
@@ -5666,7 +5485,7 @@ class HistoryFiles {
|
|
|
5666
5485
|
const dir = opendirSync3(path2);
|
|
5667
5486
|
try {
|
|
5668
5487
|
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
5669
|
-
const p =
|
|
5488
|
+
const p = join9(path2, e.name), st = lstatSync7(p);
|
|
5670
5489
|
if (st.isDirectory())
|
|
5671
5490
|
bytes += walk(p, depth + 1);
|
|
5672
5491
|
else {
|
|
@@ -5701,7 +5520,7 @@ class HistoryFiles {
|
|
|
5701
5520
|
state.bytes = 0;
|
|
5702
5521
|
state.identity = null;
|
|
5703
5522
|
let count = 0;
|
|
5704
|
-
const ledger =
|
|
5523
|
+
const ledger = join9(this.directory, "ledger");
|
|
5705
5524
|
if (!this.files.directory(ledger))
|
|
5706
5525
|
corrupt();
|
|
5707
5526
|
const dir = opendirSync3(ledger);
|
|
@@ -5866,7 +5685,7 @@ class HistoryFiles {
|
|
|
5866
5685
|
let committed;
|
|
5867
5686
|
const fd = await open(path2, constants5.O_WRONLY | constants5.O_APPEND | (constants5.O_NOFOLLOW ?? 0));
|
|
5868
5687
|
const assertOriginal = (bytes) => {
|
|
5869
|
-
const original = fstatSync4(fd.fd), selected2 =
|
|
5688
|
+
const original = fstatSync4(fd.fd), selected2 = lstatSync7(path2);
|
|
5870
5689
|
if (original.dev !== selected2.dev || original.ino !== selected2.ino || original.nlink !== 1)
|
|
5871
5690
|
corrupt();
|
|
5872
5691
|
const snapshot = this.files.read(path2, LIMITS.segmentBytes);
|
|
@@ -5983,7 +5802,7 @@ class HistoryFiles {
|
|
|
5983
5802
|
try {
|
|
5984
5803
|
lease.assert();
|
|
5985
5804
|
this.files.assert(path2, before, LIMITS.segmentBytes);
|
|
5986
|
-
const opened = await fd.stat(), selected2 =
|
|
5805
|
+
const opened = await fd.stat(), selected2 = lstatSync7(path2);
|
|
5987
5806
|
if (opened.ino !== selected2.ino || opened.dev !== selected2.dev || opened.nlink !== 1)
|
|
5988
5807
|
corrupt();
|
|
5989
5808
|
beforeEffect?.();
|
|
@@ -6018,7 +5837,7 @@ class HistoryFiles {
|
|
|
6018
5837
|
checkpoint(lease, state) {
|
|
6019
5838
|
lease.assert();
|
|
6020
5839
|
this.assertWritable();
|
|
6021
|
-
const path2 =
|
|
5840
|
+
const path2 = join9(this.directory, "checkpoint.json"), old = this.files.read(path2);
|
|
6022
5841
|
const body = encoded({
|
|
6023
5842
|
schema: "cadence-history-checkpoint/2",
|
|
6024
5843
|
storeId: this.format.storeId,
|
|
@@ -6431,7 +6250,7 @@ function digestHistory(files, exclude) {
|
|
|
6431
6250
|
const walk = (path2, depth) => {
|
|
6432
6251
|
if (depth > 8 || !files.files.directory(path2))
|
|
6433
6252
|
throw new ContractError("corrupt-selected");
|
|
6434
|
-
const directory2 =
|
|
6253
|
+
const directory2 = lstatSync8(path2);
|
|
6435
6254
|
digest.update(`incarnation:${directory2.dev}:${directory2.ino}
|
|
6436
6255
|
`);
|
|
6437
6256
|
const dir = opendirSync4(path2), names2 = [];
|
|
@@ -6445,11 +6264,11 @@ function digestHistory(files, exclude) {
|
|
|
6445
6264
|
dir.closeSync();
|
|
6446
6265
|
}
|
|
6447
6266
|
for (const name of names2.sort()) {
|
|
6448
|
-
const selected2 =
|
|
6267
|
+
const selected2 = join10(path2, name);
|
|
6449
6268
|
if (selected2 === exclude)
|
|
6450
6269
|
continue;
|
|
6451
6270
|
const rel = relative4(files.directory, selected2);
|
|
6452
|
-
if (
|
|
6271
|
+
if (lstatSync8(selected2).isDirectory()) {
|
|
6453
6272
|
digest.update(`directory:${Buffer.byteLength(rel)}:${rel}
|
|
6454
6273
|
`);
|
|
6455
6274
|
walk(selected2, depth + 1);
|
|
@@ -6633,10 +6452,25 @@ class HistoryRecovery {
|
|
|
6633
6452
|
}
|
|
6634
6453
|
}
|
|
6635
6454
|
|
|
6455
|
+
// src/verification-diagnostics.ts
|
|
6456
|
+
function diagnosticText(value, maximum = 4096, secrets = []) {
|
|
6457
|
+
maximum = Math.max(0, Math.floor(maximum));
|
|
6458
|
+
let clean = value;
|
|
6459
|
+
for (const secret of [...new Set(secrets)].filter(Boolean).sort((a, b) => b.length - a.length))
|
|
6460
|
+
clean = clean.split(secret).join("[redacted]");
|
|
6461
|
+
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]@");
|
|
6462
|
+
if (Buffer.byteLength(clean) <= maximum)
|
|
6463
|
+
return clean;
|
|
6464
|
+
const marker = maximum >= 12 ? `
|
|
6465
|
+
[truncated]` : ".".repeat(maximum);
|
|
6466
|
+
const bytes = Buffer.from(clean).subarray(0, Math.max(0, maximum - Buffer.byteLength(marker)));
|
|
6467
|
+
return bytes.toString("utf8").replace(/\ufffd$/, "") + marker;
|
|
6468
|
+
}
|
|
6469
|
+
|
|
6636
6470
|
// src/service/gateway-recovery.ts
|
|
6637
6471
|
import { opendirSync as opendirSync5 } from "node:fs";
|
|
6638
6472
|
import { hostname as hostname4 } from "node:os";
|
|
6639
|
-
import { join as
|
|
6473
|
+
import { join as join11 } from "node:path";
|
|
6640
6474
|
function required(resources, name) {
|
|
6641
6475
|
const file = resources.get(name);
|
|
6642
6476
|
if (!file)
|
|
@@ -6738,7 +6572,7 @@ function state(store) {
|
|
|
6738
6572
|
for (let n = 0;n < entries.length; n++) {
|
|
6739
6573
|
if (!entries.includes(`${n}.json`))
|
|
6740
6574
|
throw new ContractError("corrupt-selected");
|
|
6741
|
-
const file = store.files.read(
|
|
6575
|
+
const file = store.files.read(join11(archive, `${n}.json`), 65536);
|
|
6742
6576
|
if (!file)
|
|
6743
6577
|
throw new ContractError("corrupt-selected");
|
|
6744
6578
|
const row = closed(json(file, 65536), [
|
|
@@ -6819,7 +6653,7 @@ function resolveGatewayRecovery(store, expected, assertAuthority, legacyHostConf
|
|
|
6819
6653
|
throw new ContractError("quarantine");
|
|
6820
6654
|
if (selected2.attempts.length >= 128)
|
|
6821
6655
|
throw new ContractError("capacity");
|
|
6822
|
-
const receiptPath =
|
|
6656
|
+
const receiptPath = join11(selected2.archive, `${selected2.attempts.length}.json`);
|
|
6823
6657
|
const receipt = store.files.create(receiptPath, encoded({
|
|
6824
6658
|
schema: "cadence-workbench-recovery/1",
|
|
6825
6659
|
lock: selected2.lock.bytes.toString("utf8"),
|
|
@@ -6844,7 +6678,7 @@ function resolveGatewayRecovery(store, expected, assertAuthority, legacyHostConf
|
|
|
6844
6678
|
if (names2(store, selected2.archive, 128).length !== selected2.attempts.length + 1)
|
|
6845
6679
|
throw new ContractError("conflict");
|
|
6846
6680
|
for (const [n, file] of selected2.attempts.entries())
|
|
6847
|
-
store.files.assert(
|
|
6681
|
+
store.files.assert(join11(selected2.archive, `${n}.json`), file, 65536);
|
|
6848
6682
|
store.files.assert(receiptPath, receipt, 65536);
|
|
6849
6683
|
};
|
|
6850
6684
|
assert();
|
|
@@ -7080,7 +6914,7 @@ async function localGatewayRecovery(store, operation) {
|
|
|
7080
6914
|
|
|
7081
6915
|
// src/service/runtime.ts
|
|
7082
6916
|
import { homedir as homedir4 } from "node:os";
|
|
7083
|
-
import { dirname as
|
|
6917
|
+
import { dirname as dirname10, isAbsolute as isAbsolute6, join as join12, resolve as resolve5 } from "node:path";
|
|
7084
6918
|
var STARTUP_INPUT_BYTES = 16 * 1024;
|
|
7085
6919
|
function absolute(value) {
|
|
7086
6920
|
const result = text(value, 4096, true);
|
|
@@ -7165,7 +6999,7 @@ function selectStart(options, ambient) {
|
|
|
7165
6999
|
throw new ContractError("invalid");
|
|
7166
7000
|
let fromFile;
|
|
7167
7001
|
if (options.config) {
|
|
7168
|
-
const path2 = resolve5(options.config), f = new PrivateFiles(
|
|
7002
|
+
const path2 = resolve5(options.config), f = new PrivateFiles(dirname10(path2));
|
|
7169
7003
|
const bytes = f.read(path2);
|
|
7170
7004
|
if (!bytes)
|
|
7171
7005
|
throw new ContractError("corrupt-selected");
|
|
@@ -7180,7 +7014,7 @@ function selectStart(options, ambient) {
|
|
|
7180
7014
|
port: options.port ?? 17423,
|
|
7181
7015
|
proxy: null
|
|
7182
7016
|
},
|
|
7183
|
-
agentDir: resolve5(options.agentDir ?? ambient.PI_CODING_AGENT_DIR ??
|
|
7017
|
+
agentDir: resolve5(options.agentDir ?? ambient.PI_CODING_AGENT_DIR ?? join12(ambient.HOME ?? ambient.USERPROFILE ?? homedir4(), ".pi", "agent")),
|
|
7184
7018
|
tokenSource: options.tokenFile ? "file" : env ? "environment" : "generated",
|
|
7185
7019
|
tokenFile: options.tokenFile ? resolve5(options.tokenFile) : null
|
|
7186
7020
|
});
|
|
@@ -7303,7 +7137,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7303
7137
|
proxy: null
|
|
7304
7138
|
});
|
|
7305
7139
|
if (policyFile) {
|
|
7306
|
-
const file = new PrivateFiles(
|
|
7140
|
+
const file = new PrivateFiles(dirname11(policyFile)).read(policyFile);
|
|
7307
7141
|
if (!file)
|
|
7308
7142
|
throw new ContractError("invalid");
|
|
7309
7143
|
policy = decodeHttpPolicy(json(file));
|
|
@@ -7312,7 +7146,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7312
7146
|
kind: "workbench",
|
|
7313
7147
|
serverConfig: config,
|
|
7314
7148
|
stateHome: store.stateHome,
|
|
7315
|
-
agentDir: agentDir ?? selected2?.agentDir ?? resolve6(process.env.PI_CODING_AGENT_DIR ??
|
|
7149
|
+
agentDir: agentDir ?? selected2?.agentDir ?? resolve6(process.env.PI_CODING_AGENT_DIR ?? join13(homedir5(), ".pi", "agent")),
|
|
7316
7150
|
policy: { ...policy, port: server.port }
|
|
7317
7151
|
});
|
|
7318
7152
|
const status = await serviceStatus(store);
|
|
@@ -7321,7 +7155,7 @@ async function gatewayMain(argv, hostUrl) {
|
|
|
7321
7155
|
throw new ContractError("conflict");
|
|
7322
7156
|
result = status;
|
|
7323
7157
|
} else
|
|
7324
|
-
result = await launchHost(input, store, foreground, hostUrl,
|
|
7158
|
+
result = await launchHost(input, store, foreground, hostUrl, dirname11(dirname11(fileURLToPath(hostUrl))), env, network);
|
|
7325
7159
|
if (!foreground && result && typeof result === "object" && "status" in result && result.status === "alive") {
|
|
7326
7160
|
const reply = closed(await nativeCall(store, "token-link", true), [
|
|
7327
7161
|
"link"
|
|
@@ -7353,12 +7187,12 @@ No project or default workspace was created.
|
|
|
7353
7187
|
|
|
7354
7188
|
// src/service/launchd.ts
|
|
7355
7189
|
import { rmSync as rmSync2 } from "node:fs";
|
|
7356
|
-
import { dirname as
|
|
7190
|
+
import { dirname as dirname12, join as join14 } from "node:path";
|
|
7357
7191
|
var LABEL = "com.abelxiaoxing.cadence";
|
|
7358
7192
|
var MARKER2 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
7359
7193
|
<!-- Generated by @abelxiaoxing/cadence`;
|
|
7360
7194
|
function plistPath(s) {
|
|
7361
|
-
return
|
|
7195
|
+
return join14(s.home, "Library", "LaunchAgents", `${LABEL}.plist`);
|
|
7362
7196
|
}
|
|
7363
7197
|
var xml = (value) => checked(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
7364
7198
|
function buildPlist(u) {
|
|
@@ -7383,7 +7217,7 @@ function buildPlist(u) {
|
|
|
7383
7217
|
"\t</array>",
|
|
7384
7218
|
...u.workspace === null ? ["<!-- Cadence workbench gateway; no project workspace -->"] : [],
|
|
7385
7219
|
"\t<key>WorkingDirectory</key>",
|
|
7386
|
-
` <string>${xml(u.workspace ??
|
|
7220
|
+
` <string>${xml(u.workspace ?? dirname12(dirname12(u.cli)))}</string>`,
|
|
7387
7221
|
"\t<key>EnvironmentVariables</key>",
|
|
7388
7222
|
"\t<dict>",
|
|
7389
7223
|
...u.environment.flatMap(([k, v]) => [
|
|
@@ -7438,11 +7272,11 @@ async function configureLaunchAgent(s, mode, requested) {
|
|
|
7438
7272
|
s.launchctl(["bootout", target(s)]);
|
|
7439
7273
|
write(path2, buildPlist({
|
|
7440
7274
|
node: s.node,
|
|
7441
|
-
cli:
|
|
7275
|
+
cli: join14(s.root, "src", "service-cli.mjs"),
|
|
7442
7276
|
workspace,
|
|
7443
7277
|
serverConfig: serverConfigPath(s.home),
|
|
7444
7278
|
environment: serviceEnvironment(s),
|
|
7445
|
-
log:
|
|
7279
|
+
log: join14(s.home, "Library", "Logs", "cadence.log")
|
|
7446
7280
|
}));
|
|
7447
7281
|
const result = {
|
|
7448
7282
|
unit: path2,
|
|
@@ -7453,7 +7287,7 @@ async function configureLaunchAgent(s, mode, requested) {
|
|
|
7453
7287
|
s.launchctl(["enable", target(s)]);
|
|
7454
7288
|
else if (disabled(s))
|
|
7455
7289
|
return { status: "disabled", ...result };
|
|
7456
|
-
if (workspace !== null && !regularFile(
|
|
7290
|
+
if (workspace !== null && !regularFile(join14(agentDirectory(s), "auth.json")))
|
|
7457
7291
|
return { status: "waiting-for-pi-login", ...result };
|
|
7458
7292
|
const started = s.launchctl(["bootstrap", domain(s), path2]).status === 0;
|
|
7459
7293
|
return { status: started ? "started" : "failed", ...result };
|
|
@@ -7502,14 +7336,14 @@ function launchAgentStatus(s) {
|
|
|
7502
7336
|
|
|
7503
7337
|
// src/service/login-run.ts
|
|
7504
7338
|
import { rmSync as rmSync3 } from "node:fs";
|
|
7505
|
-
import { dirname as
|
|
7339
|
+
import { dirname as dirname13, isAbsolute as isAbsolute7, join as join15 } from "node:path";
|
|
7506
7340
|
var RUN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
|
7507
7341
|
var APPROVED_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run";
|
|
7508
7342
|
var RUN_VALUE = "Cadence";
|
|
7509
7343
|
var MARKER3 = "' Generated by @abelxiaoxing/cadence";
|
|
7510
7344
|
function launcherPath(s) {
|
|
7511
7345
|
const local = s.env.LOCALAPPDATA;
|
|
7512
|
-
return
|
|
7346
|
+
return join15(local && isAbsolute7(local) ? local : join15(s.home, "AppData", "Local"), "cadence-server", "cadence.vbs");
|
|
7513
7347
|
}
|
|
7514
7348
|
var literal2 = (value) => `"${checked(value).replaceAll('"', '""')}"`;
|
|
7515
7349
|
function argument2(value) {
|
|
@@ -7526,7 +7360,7 @@ function buildLauncher(u) {
|
|
|
7526
7360
|
...u.environment.length ? ['Set env = shell.Environment("Process")'] : [],
|
|
7527
7361
|
...u.environment.map(([k, v]) => `env(${literal2(k)}) = ${literal2(v)}`),
|
|
7528
7362
|
...u.workspace === null ? ["' Cadence workbench gateway; no project workspace"] : [],
|
|
7529
|
-
`shell.CurrentDirectory = ${literal2(u.workspace ??
|
|
7363
|
+
`shell.CurrentDirectory = ${literal2(u.workspace ?? dirname13(dirname13(u.cli)))}`,
|
|
7530
7364
|
`shell.Run ${literal2(command)}, 0, False`,
|
|
7531
7365
|
""
|
|
7532
7366
|
].join(`\r
|
|
@@ -7565,7 +7399,7 @@ async function configureLoginRun(s, mode, requested) {
|
|
|
7565
7399
|
const environment = s.env.PI_CODING_AGENT_DIR ? [["PI_CODING_AGENT_DIR", agentDirectory(s)]] : [];
|
|
7566
7400
|
write(path2, utf16(buildLauncher({
|
|
7567
7401
|
node: s.node,
|
|
7568
|
-
cli:
|
|
7402
|
+
cli: join15(s.root, "src", "service-cli.mjs"),
|
|
7569
7403
|
workspace,
|
|
7570
7404
|
serverConfig: serverConfigPath(s.home),
|
|
7571
7405
|
environment
|
|
@@ -7581,7 +7415,7 @@ async function configureLoginRun(s, mode, requested) {
|
|
|
7581
7415
|
return { status: "failed", ...result };
|
|
7582
7416
|
s.reg(["delete", APPROVED_KEY, "/v", RUN_VALUE, "/f"]);
|
|
7583
7417
|
}
|
|
7584
|
-
if (workspace !== null && !regularFile(
|
|
7418
|
+
if (workspace !== null && !regularFile(join15(agentDirectory(s), "auth.json")))
|
|
7585
7419
|
return { status: "waiting-for-pi-login", ...result };
|
|
7586
7420
|
return { status: s.launch(path2) ? "started" : "failed", ...result };
|
|
7587
7421
|
}
|
|
@@ -7630,8 +7464,8 @@ async function loginRunStatus(s) {
|
|
|
7630
7464
|
}
|
|
7631
7465
|
|
|
7632
7466
|
// src/service/system-service.ts
|
|
7633
|
-
import { existsSync as existsSync2, realpathSync as
|
|
7634
|
-
import { isAbsolute as isAbsolute8, join as
|
|
7467
|
+
import { existsSync as existsSync2, realpathSync as realpathSync5, rmSync as rmSync4 } from "node:fs";
|
|
7468
|
+
import { isAbsolute as isAbsolute8, join as join16 } from "node:path";
|
|
7635
7469
|
var MARKER4 = "# Generated by @abelxiaoxing/cadence";
|
|
7636
7470
|
function boundary(s) {
|
|
7637
7471
|
if (s.platform !== "linux" || s.scope !== "system")
|
|
@@ -7712,12 +7546,12 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7712
7546
|
const selected2 = selectWorkspace(s, requested, mode === "install" && requested === undefined ? null : unitWorkspace(previous), false);
|
|
7713
7547
|
if (selected2.workspace !== null && !sdk)
|
|
7714
7548
|
return { status: "missing-pi-sdk", scope: "system" };
|
|
7715
|
-
const selectedReal = selected2.workspace && existsSync2(selected2.workspace) ?
|
|
7549
|
+
const selectedReal = selected2.workspace && existsSync2(selected2.workspace) ? realpathSync5(selected2.workspace) : undefined;
|
|
7716
7550
|
const agentDir = agentDirectory(s);
|
|
7717
7551
|
const content = buildUnit({
|
|
7718
7552
|
system: true,
|
|
7719
7553
|
node: s.node,
|
|
7720
|
-
cli:
|
|
7554
|
+
cli: join16(s.root, "src", "service-cli.mjs"),
|
|
7721
7555
|
workspace: selected2.workspace,
|
|
7722
7556
|
serverConfig: serviceConfigPath(s),
|
|
7723
7557
|
agentDir,
|
|
@@ -7749,7 +7583,7 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7749
7583
|
return { status: "foreign", ...result };
|
|
7750
7584
|
if (selectedReal && selected2.workspace) {
|
|
7751
7585
|
try {
|
|
7752
|
-
if (
|
|
7586
|
+
if (realpathSync5(selected2.workspace) !== selectedReal)
|
|
7753
7587
|
return { status: "workspace-changed", ...result };
|
|
7754
7588
|
} catch {
|
|
7755
7589
|
return { status: "workspace-changed", ...result };
|
|
@@ -7766,7 +7600,7 @@ async function configureSystemService(s, mode, requested) {
|
|
|
7766
7600
|
return { status: "failed", ...result, ...selected2 };
|
|
7767
7601
|
if (mode === "install" && s.systemctl(["enable", s.unit]).status !== 0)
|
|
7768
7602
|
return { status: "failed", ...result, ...selected2 };
|
|
7769
|
-
if (selected2.workspace !== null && !regularFile(
|
|
7603
|
+
if (selected2.workspace !== null && !regularFile(join16(agentDir, "auth.json")))
|
|
7770
7604
|
return { status: "waiting-for-pi-login", ...result, ...selected2 };
|
|
7771
7605
|
s.systemctl(["reset-failed", s.unit]);
|
|
7772
7606
|
return {
|
|
@@ -7995,10 +7829,10 @@ function startupGuidance(result, input) {
|
|
|
7995
7829
|
return `Pi's agent directory ${agent} must be an existing real directory (not a symlink) owned by you.
|
|
7996
7830
|
`;
|
|
7997
7831
|
case "pi-auth":
|
|
7998
|
-
return `${
|
|
7832
|
+
return `${join17(agent, "auth.json")} must be your own single-link regular file holding valid Pi credentials; log in to Pi first.
|
|
7999
7833
|
`;
|
|
8000
7834
|
case "pi-models":
|
|
8001
|
-
return `${
|
|
7835
|
+
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
7836
|
`;
|
|
8003
7837
|
case "pi-runtime":
|
|
8004
7838
|
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 +8050,7 @@ async function serverAction(s, action, requested, linger) {
|
|
|
8216
8050
|
}
|
|
8217
8051
|
}
|
|
8218
8052
|
function serverGuidance(s, action, result, link) {
|
|
8219
|
-
const cli = piUserInstall(s) ? `node ${JSON.stringify(
|
|
8053
|
+
const cli = piUserInstall(s) ? `node ${JSON.stringify(join17(s.root, "src", "service-cli.mjs"))}` : "cadence";
|
|
8220
8054
|
const note = result.redirectedFrom ? `Cadence workspace: ${result.workspace} (${result.redirectedFrom} contains Cadence's private state)
|
|
8221
8055
|
` : "";
|
|
8222
8056
|
return note + serverStatusGuidance(s, action, result, cli, link);
|
|
@@ -8283,7 +8117,7 @@ ${manage}
|
|
|
8283
8117
|
${linger}`;
|
|
8284
8118
|
}
|
|
8285
8119
|
case "waiting-for-pi-login":
|
|
8286
|
-
return `Cadence is registered for ${result.workspace}; it starts once Pi has ${
|
|
8120
|
+
return `Cadence is registered for ${result.workspace}; it starts once Pi has ${join17(agentDirectory(s), "auth.json")}.
|
|
8287
8121
|
Log in to Pi, then: ${cli} server restart${flags}
|
|
8288
8122
|
${config}
|
|
8289
8123
|
${lingerGuidance(result.linger)}`;
|
|
@@ -8390,7 +8224,7 @@ async function serverMain(argv, hostUrl) {
|
|
|
8390
8224
|
} else
|
|
8391
8225
|
throw new ContractError("invalid");
|
|
8392
8226
|
}
|
|
8393
|
-
const s = hostSystem(
|
|
8227
|
+
const s = hostSystem(dirname14(dirname14(fileURLToPath2(hostUrl))), ownerControl, {
|
|
8394
8228
|
scope,
|
|
8395
8229
|
name
|
|
8396
8230
|
});
|