@abelxiaoxing/cadence 2.0.28 → 2.0.30
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 +2 -1
- package/README.es.md +2 -1
- package/README.fr.md +2 -1
- package/README.ja.md +3 -1
- package/README.ko.md +5 -1
- package/README.md +2 -1
- package/README.pt-BR.md +2 -1
- package/README.ru.md +2 -1
- package/README.zh-CN.md +2 -1
- package/docs/service-management.md +23 -1
- package/package.json +1 -1
- package/src/native-build.ts +1 -1
- package/src/service/contracts/workbench.ts +237 -44
- package/src/service-cli.mjs +390 -64
- package/src/service-host.mjs +1408 -645
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +16 -16
- package/web/dist/manifest.json +7 -7
package/src/service-host.mjs
CHANGED
|
@@ -9,7 +9,9 @@ import {
|
|
|
9
9
|
createServer as createServer2,
|
|
10
10
|
request as request2
|
|
11
11
|
} from "node:http";
|
|
12
|
-
import { homedir as
|
|
12
|
+
import { homedir as homedir6 } from "node:os";
|
|
13
|
+
import { dirname as dirname12 } from "node:path";
|
|
14
|
+
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
13
15
|
import { WebSocket, WebSocketServer as WebSocketServer2 } from "ws";
|
|
14
16
|
|
|
15
17
|
// src/service-assets.ts
|
|
@@ -4070,6 +4072,56 @@ function decodeCadenceConversation(v) {
|
|
|
4070
4072
|
...degraded ? { notice: text(o.notice, 1024, true) } : {}
|
|
4071
4073
|
};
|
|
4072
4074
|
}
|
|
4075
|
+
var STEP_TEXT_BYTES = 16 * 1024;
|
|
4076
|
+
var TARGET_BYTES = 1024;
|
|
4077
|
+
var STEP_READ = 24;
|
|
4078
|
+
function clipText(value, bytes) {
|
|
4079
|
+
const size = (s) => new TextEncoder().encode(JSON.stringify(s)).length;
|
|
4080
|
+
if (size(value) <= bytes)
|
|
4081
|
+
return value;
|
|
4082
|
+
let low = 0, high = Math.min(value.length, bytes);
|
|
4083
|
+
while (low < high) {
|
|
4084
|
+
const mid = Math.ceil((low + high) / 2);
|
|
4085
|
+
if (size(value.slice(0, mid)) <= bytes)
|
|
4086
|
+
low = mid;
|
|
4087
|
+
else
|
|
4088
|
+
high = mid - 1;
|
|
4089
|
+
}
|
|
4090
|
+
return /[\uD800-\uDBFF]/u.test(value.slice(low - 1, low)) ? value.slice(0, low - 1) : value.slice(0, low);
|
|
4091
|
+
}
|
|
4092
|
+
function toolTarget(args) {
|
|
4093
|
+
let value;
|
|
4094
|
+
try {
|
|
4095
|
+
value = JSON.parse(args);
|
|
4096
|
+
} catch {
|
|
4097
|
+
return "";
|
|
4098
|
+
}
|
|
4099
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
4100
|
+
return "";
|
|
4101
|
+
const field = (name) => {
|
|
4102
|
+
const v = Object.hasOwn(value, name) ? value[name] : undefined;
|
|
4103
|
+
return typeof v === "string" ? v.trim() : "";
|
|
4104
|
+
};
|
|
4105
|
+
const command = field("command");
|
|
4106
|
+
if (command)
|
|
4107
|
+
return command.split(`
|
|
4108
|
+
`, 1)[0];
|
|
4109
|
+
return [field("pattern"), field("path")].filter(Boolean).join(" ");
|
|
4110
|
+
}
|
|
4111
|
+
function chatStep(item, parts, more = false) {
|
|
4112
|
+
const tool = item.role === "tool";
|
|
4113
|
+
const text2 = tool ? "" : clipText(item.text, STEP_TEXT_BYTES);
|
|
4114
|
+
return {
|
|
4115
|
+
id: item.id,
|
|
4116
|
+
role: item.role,
|
|
4117
|
+
text: text2,
|
|
4118
|
+
clipped: !tool && (more || text2 !== item.text),
|
|
4119
|
+
name: item.name,
|
|
4120
|
+
target: tool ? clipText(toolTarget(item.args), TARGET_BYTES) : "",
|
|
4121
|
+
error: item.error,
|
|
4122
|
+
parts
|
|
4123
|
+
};
|
|
4124
|
+
}
|
|
4073
4125
|
function decodeChatCursor(value) {
|
|
4074
4126
|
const o = closed(value, ["id", "updatedAt"]);
|
|
4075
4127
|
return { id: decodeId(o.id), updatedAt: integer(o.updatedAt, 0) };
|
|
@@ -4086,13 +4138,23 @@ var statuses = [
|
|
|
4086
4138
|
function decodeWorkbenchRead(v) {
|
|
4087
4139
|
const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4088
4140
|
switch (kind) {
|
|
4089
|
-
case "conversation-
|
|
4090
|
-
const o = closed(v, ["kind", "id", "
|
|
4141
|
+
case "conversation-turn": {
|
|
4142
|
+
const o = closed(v, ["kind", "id", "ordinal", "before"]);
|
|
4143
|
+
return {
|
|
4144
|
+
kind,
|
|
4145
|
+
id: decodeId(o.id),
|
|
4146
|
+
ordinal: integer(o.ordinal, 0),
|
|
4147
|
+
before: nullable(o.before, (b) => integer(b, 0))
|
|
4148
|
+
};
|
|
4149
|
+
}
|
|
4150
|
+
case "conversation-step": {
|
|
4151
|
+
const o = closed(v, ["kind", "id", "turnId", "step", "part"]);
|
|
4091
4152
|
return {
|
|
4092
4153
|
kind,
|
|
4093
4154
|
id: decodeId(o.id),
|
|
4094
4155
|
turnId: decodeId(o.turnId),
|
|
4095
|
-
|
|
4156
|
+
step: integer(o.step, 0),
|
|
4157
|
+
part: integer(o.part, 0)
|
|
4096
4158
|
};
|
|
4097
4159
|
}
|
|
4098
4160
|
case "cadence-conversation": {
|
|
@@ -4142,8 +4204,8 @@ function decodeWorkbenchRead(v) {
|
|
|
4142
4204
|
return { kind, taskId: decodeId(o.taskId) };
|
|
4143
4205
|
}
|
|
4144
4206
|
case "conversation": {
|
|
4145
|
-
const o = closed(v, ["kind", "id"
|
|
4146
|
-
return { kind, id: decodeId(o.id)
|
|
4207
|
+
const o = closed(v, ["kind", "id"]);
|
|
4208
|
+
return { kind, id: decodeId(o.id) };
|
|
4147
4209
|
}
|
|
4148
4210
|
default: {
|
|
4149
4211
|
const o = closed(v, ["kind", "path"]);
|
|
@@ -4291,17 +4353,69 @@ function decodeChatTurn(v) {
|
|
|
4291
4353
|
media: omitted ? bool(omitted.media) : false,
|
|
4292
4354
|
other: omitted ? bool(omitted.other) : false
|
|
4293
4355
|
},
|
|
4294
|
-
items: list(o.items, 256,
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4356
|
+
items: list(o.items, 256, decodeChatItem)
|
|
4357
|
+
};
|
|
4358
|
+
}
|
|
4359
|
+
function decodeChatItem(v) {
|
|
4360
|
+
const i = closed(v, ["id", "role", "text", "name", "args", "error"]);
|
|
4361
|
+
return {
|
|
4362
|
+
id: text(i.id, 160, true),
|
|
4363
|
+
role: oneOf(i.role, ["user", "assistant", "tool"]),
|
|
4364
|
+
text: text(i.text, 256 * 1024),
|
|
4365
|
+
name: text(i.name, 128),
|
|
4366
|
+
args: text(i.args, 64 * 1024),
|
|
4367
|
+
error: bool(i.error)
|
|
4368
|
+
};
|
|
4369
|
+
}
|
|
4370
|
+
function decodeChatStep(v) {
|
|
4371
|
+
const o = closed(v, [
|
|
4372
|
+
"id",
|
|
4373
|
+
"role",
|
|
4374
|
+
"text",
|
|
4375
|
+
"clipped",
|
|
4376
|
+
"name",
|
|
4377
|
+
"target",
|
|
4378
|
+
"error",
|
|
4379
|
+
"parts"
|
|
4380
|
+
]);
|
|
4381
|
+
const step = {
|
|
4382
|
+
id: text(o.id, 160, true),
|
|
4383
|
+
role: oneOf(o.role, ["user", "assistant", "tool"]),
|
|
4384
|
+
text: text(o.text, STEP_TEXT_BYTES),
|
|
4385
|
+
clipped: bool(o.clipped),
|
|
4386
|
+
name: text(o.name, 128),
|
|
4387
|
+
target: text(o.target, TARGET_BYTES),
|
|
4388
|
+
error: bool(o.error),
|
|
4389
|
+
parts: integer(o.parts, 0)
|
|
4390
|
+
};
|
|
4391
|
+
if (step.role === "tool" && (step.text || step.clipped))
|
|
4392
|
+
throw new ContractError("invalid");
|
|
4393
|
+
return step;
|
|
4394
|
+
}
|
|
4395
|
+
function decodeChatTurnHeader(v) {
|
|
4396
|
+
const o = closed(v, [
|
|
4397
|
+
"id",
|
|
4398
|
+
"epoch",
|
|
4399
|
+
"status",
|
|
4400
|
+
"notice",
|
|
4401
|
+
"omitted",
|
|
4402
|
+
"steps",
|
|
4403
|
+
"stable"
|
|
4404
|
+
]);
|
|
4405
|
+
const omitted = closed(o.omitted, ["thinking", "media", "other"]);
|
|
4406
|
+
const steps = integer(o.steps, 0);
|
|
4407
|
+
return {
|
|
4408
|
+
id: decodeId(o.id),
|
|
4409
|
+
epoch: decodeId(o.epoch),
|
|
4410
|
+
status: oneOf(o.status, statuses),
|
|
4411
|
+
notice: text(o.notice, 1024),
|
|
4412
|
+
omitted: {
|
|
4413
|
+
thinking: bool(omitted.thinking),
|
|
4414
|
+
media: bool(omitted.media),
|
|
4415
|
+
other: bool(omitted.other)
|
|
4416
|
+
},
|
|
4417
|
+
steps,
|
|
4418
|
+
stable: integer(o.stable, 0, steps)
|
|
4305
4419
|
};
|
|
4306
4420
|
}
|
|
4307
4421
|
function decodeChatSummary(v) {
|
|
@@ -4326,13 +4440,6 @@ function decodeChatSummary(v) {
|
|
|
4326
4440
|
function decodeWorkbenchResult(v) {
|
|
4327
4441
|
canonicalJson(v, 512 * 1024);
|
|
4328
4442
|
const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4329
|
-
if (k === "conversation-transcript") {
|
|
4330
|
-
const o2 = closed(v, ["kind", "conversationId", "page", "turn"]);
|
|
4331
|
-
const turn = decodeChatTurn(o2.turn), page = integer(o2.page, 0);
|
|
4332
|
-
if (!turn.transcript || page >= turn.transcript.pages)
|
|
4333
|
-
throw new ContractError("invalid");
|
|
4334
|
-
return { kind: k, conversationId: decodeId(o2.conversationId), page, turn };
|
|
4335
|
-
}
|
|
4336
4443
|
if (k === "workflow-transcript") {
|
|
4337
4444
|
const o2 = closed(v, ["kind", "runId", "stepId", "turn"]);
|
|
4338
4445
|
return {
|
|
@@ -4440,12 +4547,49 @@ function decodeWorkbenchResult(v) {
|
|
|
4440
4547
|
};
|
|
4441
4548
|
}
|
|
4442
4549
|
if (kind === "conversation") {
|
|
4443
|
-
const o2 = closed(v, ["kind", "summary", "turns"
|
|
4550
|
+
const o2 = closed(v, ["kind", "summary", "turns"]);
|
|
4551
|
+
const summary = nullable(o2.summary, decodeChatSummary), turns = integer(o2.turns, 0);
|
|
4552
|
+
if (!summary && turns)
|
|
4553
|
+
throw new ContractError("invalid");
|
|
4554
|
+
return { kind, summary, turns };
|
|
4555
|
+
}
|
|
4556
|
+
if (kind === "conversation-turn") {
|
|
4557
|
+
const o2 = closed(v, [
|
|
4558
|
+
"kind",
|
|
4559
|
+
"conversationId",
|
|
4560
|
+
"ordinal",
|
|
4561
|
+
"turn",
|
|
4562
|
+
"start",
|
|
4563
|
+
"steps"
|
|
4564
|
+
]);
|
|
4565
|
+
const turn = decodeChatTurnHeader(o2.turn), start = integer(o2.start, 0), steps = list(o2.steps, STEP_READ, decodeChatStep);
|
|
4566
|
+
if (start + steps.length > turn.steps)
|
|
4567
|
+
throw new ContractError("invalid");
|
|
4444
4568
|
return {
|
|
4445
4569
|
kind,
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4570
|
+
conversationId: decodeId(o2.conversationId),
|
|
4571
|
+
ordinal: integer(o2.ordinal, 0),
|
|
4572
|
+
turn,
|
|
4573
|
+
start,
|
|
4574
|
+
steps
|
|
4575
|
+
};
|
|
4576
|
+
}
|
|
4577
|
+
if (kind === "conversation-step") {
|
|
4578
|
+
const o2 = closed(v, [
|
|
4579
|
+
"kind",
|
|
4580
|
+
"conversationId",
|
|
4581
|
+
"turnId",
|
|
4582
|
+
"step",
|
|
4583
|
+
"part",
|
|
4584
|
+
"item"
|
|
4585
|
+
]);
|
|
4586
|
+
return {
|
|
4587
|
+
kind,
|
|
4588
|
+
conversationId: decodeId(o2.conversationId),
|
|
4589
|
+
turnId: decodeId(o2.turnId),
|
|
4590
|
+
step: integer(o2.step, 0),
|
|
4591
|
+
part: integer(o2.part, 0),
|
|
4592
|
+
item: decodeChatItem(o2.item)
|
|
4449
4593
|
};
|
|
4450
4594
|
}
|
|
4451
4595
|
if (kind === "files" || kind === "directories") {
|
|
@@ -5598,11 +5742,167 @@ class HttpAuthentication {
|
|
|
5598
5742
|
}
|
|
5599
5743
|
}
|
|
5600
5744
|
|
|
5745
|
+
// src/service/autostart.ts
|
|
5746
|
+
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
5747
|
+
import {
|
|
5748
|
+
lstatSync as lstatSync5,
|
|
5749
|
+
mkdirSync as mkdirSync4,
|
|
5750
|
+
readFileSync,
|
|
5751
|
+
realpathSync as realpathSync3,
|
|
5752
|
+
renameSync as renameSync2,
|
|
5753
|
+
rmSync,
|
|
5754
|
+
statSync as statSync2,
|
|
5755
|
+
writeFileSync as writeFileSync2
|
|
5756
|
+
} from "node:fs";
|
|
5757
|
+
import { homedir as homedir2 } from "node:os";
|
|
5758
|
+
import {
|
|
5759
|
+
delimiter,
|
|
5760
|
+
dirname as dirname5,
|
|
5761
|
+
isAbsolute as isAbsolute4,
|
|
5762
|
+
join as join6,
|
|
5763
|
+
resolve as resolve3,
|
|
5764
|
+
sep as sep3,
|
|
5765
|
+
win32
|
|
5766
|
+
} from "node:path";
|
|
5767
|
+
var UNIT = "cadence.service";
|
|
5768
|
+
var MARKER = "# Generated by @abelxiaoxing/cadence";
|
|
5769
|
+
function wscript(env) {
|
|
5770
|
+
const root = env.SystemRoot;
|
|
5771
|
+
return win32.join(root && win32.isAbsolute(root) ? root : "C:\\Windows", "System32", "wscript.exe");
|
|
5772
|
+
}
|
|
5773
|
+
function hostSystem(root, owner, selection = {}) {
|
|
5774
|
+
const scope = selection.scope ?? (process.platform === "linux" && process.getuid?.() === 0 ? "system" : "user");
|
|
5775
|
+
const unit = serviceUnit(selection.name);
|
|
5776
|
+
const run = (command, args) => {
|
|
5777
|
+
const r = spawnSync2(command, args, {
|
|
5778
|
+
encoding: "utf8",
|
|
5779
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
5780
|
+
windowsHide: true
|
|
5781
|
+
});
|
|
5782
|
+
return { status: r.error ? null : r.status, stdout: r.stdout ?? "" };
|
|
5783
|
+
};
|
|
5784
|
+
return {
|
|
5785
|
+
platform: process.platform,
|
|
5786
|
+
env: process.env,
|
|
5787
|
+
home: homedir2(),
|
|
5788
|
+
node: process.execPath,
|
|
5789
|
+
root,
|
|
5790
|
+
unit,
|
|
5791
|
+
scope,
|
|
5792
|
+
uid: process.getuid?.(),
|
|
5793
|
+
cgroup: () => {
|
|
5794
|
+
try {
|
|
5795
|
+
return readFileSync("/proc/self/cgroup", "utf8");
|
|
5796
|
+
} catch {
|
|
5797
|
+
return;
|
|
5798
|
+
}
|
|
5799
|
+
},
|
|
5800
|
+
systemctl: (args) => run("systemctl", [scope === "system" ? "--system" : "--user", ...args]),
|
|
5801
|
+
linger: () => {
|
|
5802
|
+
const r = run("loginctl", [
|
|
5803
|
+
"show-user",
|
|
5804
|
+
String(process.getuid?.()),
|
|
5805
|
+
"--property=Linger",
|
|
5806
|
+
"--value"
|
|
5807
|
+
]);
|
|
5808
|
+
return r.status === 0 ? r.stdout.trim() === "yes" : undefined;
|
|
5809
|
+
},
|
|
5810
|
+
enableLinger: (interactive) => {
|
|
5811
|
+
const r = interactive ? spawnSync2("loginctl", ["enable-linger"], { stdio: "inherit" }) : spawnSync2("busctl", [
|
|
5812
|
+
"--system",
|
|
5813
|
+
"--allow-interactive-authorization=no",
|
|
5814
|
+
"--timeout=15",
|
|
5815
|
+
"call",
|
|
5816
|
+
"org.freedesktop.login1",
|
|
5817
|
+
"/org/freedesktop/login1",
|
|
5818
|
+
"org.freedesktop.login1.Manager",
|
|
5819
|
+
"SetUserLinger",
|
|
5820
|
+
"ubb",
|
|
5821
|
+
String(process.getuid?.()),
|
|
5822
|
+
"true",
|
|
5823
|
+
"false"
|
|
5824
|
+
], { stdio: "ignore", timeout: 20000 });
|
|
5825
|
+
return !r.error && r.status === 0;
|
|
5826
|
+
},
|
|
5827
|
+
launchctl: (args) => run("launchctl", args),
|
|
5828
|
+
reg: (args) => run("reg", args),
|
|
5829
|
+
launch: (path) => {
|
|
5830
|
+
const r = spawnSync2(wscript(process.env), [path], {
|
|
5831
|
+
stdio: "ignore",
|
|
5832
|
+
windowsHide: true,
|
|
5833
|
+
timeout: 20000
|
|
5834
|
+
});
|
|
5835
|
+
return !r.error && r.status === 0;
|
|
5836
|
+
},
|
|
5837
|
+
...owner
|
|
5838
|
+
};
|
|
5839
|
+
}
|
|
5840
|
+
function serviceUnit(name) {
|
|
5841
|
+
if (name === undefined)
|
|
5842
|
+
return UNIT;
|
|
5843
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,47}$/.test(name))
|
|
5844
|
+
throw new ContractError("invalid");
|
|
5845
|
+
return `cadence-${name}.service`;
|
|
5846
|
+
}
|
|
5847
|
+
function serviceName(s) {
|
|
5848
|
+
if (s.unit === UNIT)
|
|
5849
|
+
return;
|
|
5850
|
+
const name = /^cadence-([A-Za-z0-9][A-Za-z0-9_-]{0,47})\.service$/.exec(s.unit)?.[1];
|
|
5851
|
+
if (!name)
|
|
5852
|
+
throw new ContractError("invalid");
|
|
5853
|
+
return name;
|
|
5854
|
+
}
|
|
5855
|
+
function serviceConfigPath(s) {
|
|
5856
|
+
return serverConfigPath(s.home, serviceName(s));
|
|
5857
|
+
}
|
|
5858
|
+
function unitPath(s) {
|
|
5859
|
+
serviceName(s);
|
|
5860
|
+
if (s.scope === "system")
|
|
5861
|
+
return join6(s.unitDirectory ?? "/etc/systemd/system", s.unit);
|
|
5862
|
+
const data = s.env.XDG_DATA_HOME;
|
|
5863
|
+
return join6(data && isAbsolute4(data) ? data : join6(s.home, ".local", "share"), "systemd", "user", s.unit);
|
|
5864
|
+
}
|
|
5865
|
+
function readGenerated(path, marker) {
|
|
5866
|
+
try {
|
|
5867
|
+
if (!lstatSync5(path).isFile())
|
|
5868
|
+
return null;
|
|
5869
|
+
const bytes = readFileSync(path);
|
|
5870
|
+
const text2 = bytes[0] === 255 && bytes[1] === 254 ? bytes.subarray(2).toString("utf16le") : bytes.toString("utf8");
|
|
5871
|
+
return text2.startsWith(marker) ? text2 : null;
|
|
5872
|
+
} catch (error) {
|
|
5873
|
+
if (error.code === "ENOENT")
|
|
5874
|
+
return;
|
|
5875
|
+
throw error;
|
|
5876
|
+
}
|
|
5877
|
+
}
|
|
5878
|
+
function unitWorkspace(text2) {
|
|
5879
|
+
if (text2?.includes("# Cadence workbench gateway; no project workspace"))
|
|
5880
|
+
return null;
|
|
5881
|
+
return text2?.match(/^WorkingDirectory=(.+)$/m)?.[1].replaceAll("%%", "%");
|
|
5882
|
+
}
|
|
5883
|
+
function unitEnvironment(text2) {
|
|
5884
|
+
const out = [];
|
|
5885
|
+
for (const line of text2.split(`
|
|
5886
|
+
`)) {
|
|
5887
|
+
if (!line.startsWith("Environment="))
|
|
5888
|
+
continue;
|
|
5889
|
+
const value = JSON.parse(line.slice("Environment=".length));
|
|
5890
|
+
if (typeof value !== "string" || value.indexOf("=") < 1)
|
|
5891
|
+
throw new ContractError("invalid");
|
|
5892
|
+
const at = value.indexOf("=");
|
|
5893
|
+
out.push([value.slice(0, at), value.slice(at + 1).replaceAll("%%", "%")]);
|
|
5894
|
+
}
|
|
5895
|
+
return out;
|
|
5896
|
+
}
|
|
5897
|
+
function generatedUnit(s) {
|
|
5898
|
+
return readGenerated(unitPath(s), MARKER);
|
|
5899
|
+
}
|
|
5900
|
+
|
|
5601
5901
|
// src/service/cli.ts
|
|
5602
|
-
import { spawn } from "node:child_process";
|
|
5902
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
5603
5903
|
import { request } from "node:http";
|
|
5604
|
-
import { homedir as
|
|
5605
|
-
import { dirname as
|
|
5904
|
+
import { homedir as homedir4 } from "node:os";
|
|
5905
|
+
import { dirname as dirname10, join as join15, resolve as resolve6 } from "node:path";
|
|
5606
5906
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
5607
5907
|
|
|
5608
5908
|
// src/sdk-network.ts
|
|
@@ -5740,29 +6040,103 @@ function sdkNetworkEnvironment(ambient, scoped) {
|
|
|
5740
6040
|
return { env, redactionLiterals: route ? [route] : [] };
|
|
5741
6041
|
}
|
|
5742
6042
|
|
|
6043
|
+
// src/service/contracts/gateway.ts
|
|
6044
|
+
function decodeProjectEntry(value) {
|
|
6045
|
+
const o = closed(value, ["id", "path", "cwd", "epoch"]);
|
|
6046
|
+
return {
|
|
6047
|
+
id: decodeHash(o.id),
|
|
6048
|
+
path: text(o.path, 4096, true),
|
|
6049
|
+
cwd: text(o.cwd, 4096, true),
|
|
6050
|
+
epoch: decodeId(o.epoch)
|
|
6051
|
+
};
|
|
6052
|
+
}
|
|
6053
|
+
var RECOVERY_SCOPES = ["registry", "workbench", "project"];
|
|
6054
|
+
var RECOVERY_CAUSES = [
|
|
6055
|
+
"reboot",
|
|
6056
|
+
"crash",
|
|
6057
|
+
"restart",
|
|
6058
|
+
"storage"
|
|
6059
|
+
];
|
|
6060
|
+
var RECOVERY_ACTIONS = [
|
|
6061
|
+
"released-locks",
|
|
6062
|
+
"stopped-workers",
|
|
6063
|
+
"stopped-owner",
|
|
6064
|
+
"continued-history",
|
|
6065
|
+
"archived-history",
|
|
6066
|
+
"archived-state",
|
|
6067
|
+
"rebuilt-registry",
|
|
6068
|
+
"removed-temporaries"
|
|
6069
|
+
];
|
|
6070
|
+
var MAINTENANCE_ACTIONS = ["restart", "upgrade"];
|
|
6071
|
+
var MAINTENANCE_STATES = [
|
|
6072
|
+
"running",
|
|
6073
|
+
"ready",
|
|
6074
|
+
"failed",
|
|
6075
|
+
"rolled-back"
|
|
6076
|
+
];
|
|
6077
|
+
var MAINTENANCE_REASONS = [
|
|
6078
|
+
"fetch-failed",
|
|
6079
|
+
"stop-unconfirmed",
|
|
6080
|
+
"install-failed",
|
|
6081
|
+
"not-ready",
|
|
6082
|
+
"refused",
|
|
6083
|
+
"interrupted"
|
|
6084
|
+
];
|
|
6085
|
+
var VERSION = /^(?:0|[1-9][0-9]{0,8})\.(?:0|[1-9][0-9]{0,8})\.(?:0|[1-9][0-9]{0,8})(?:-[0-9A-Za-z.-]{1,64})?$/;
|
|
6086
|
+
function decodeVersion(value) {
|
|
6087
|
+
const version = text(value, 96, true);
|
|
6088
|
+
if (!VERSION.test(version))
|
|
6089
|
+
throw new ContractError("invalid");
|
|
6090
|
+
return version;
|
|
6091
|
+
}
|
|
6092
|
+
function decodeMaintenanceJob(value) {
|
|
6093
|
+
const o = closed(value, [
|
|
6094
|
+
"id",
|
|
6095
|
+
"action",
|
|
6096
|
+
"from",
|
|
6097
|
+
"to",
|
|
6098
|
+
"state",
|
|
6099
|
+
"reason",
|
|
6100
|
+
"version",
|
|
6101
|
+
"at",
|
|
6102
|
+
"endedAt"
|
|
6103
|
+
]);
|
|
6104
|
+
return {
|
|
6105
|
+
id: decodeId(o.id),
|
|
6106
|
+
action: oneOf(o.action, MAINTENANCE_ACTIONS),
|
|
6107
|
+
from: decodeVersion(o.from),
|
|
6108
|
+
to: o.to === null ? null : decodeVersion(o.to),
|
|
6109
|
+
state: oneOf(o.state, MAINTENANCE_STATES),
|
|
6110
|
+
reason: o.reason === null ? null : oneOf(o.reason, MAINTENANCE_REASONS),
|
|
6111
|
+
version: o.version === null ? null : decodeVersion(o.version),
|
|
6112
|
+
at: integer(o.at, 0),
|
|
6113
|
+
endedAt: o.endedAt === null ? null : integer(o.endedAt, 0)
|
|
6114
|
+
};
|
|
6115
|
+
}
|
|
6116
|
+
|
|
5743
6117
|
// src/service/coordination.ts
|
|
5744
6118
|
import { randomBytes as randomBytes2, randomUUID as randomUUID4 } from "node:crypto";
|
|
5745
6119
|
import { opendirSync } from "node:fs";
|
|
5746
6120
|
import { hostname as hostname3 } from "node:os";
|
|
5747
|
-
import { dirname as
|
|
6121
|
+
import { dirname as dirname7, isAbsolute as isAbsolute5, join as join8, relative as relative3, sep as sep4 } from "node:path";
|
|
5748
6122
|
|
|
5749
6123
|
// src/service/checkout-lock.ts
|
|
5750
6124
|
import { randomUUID as randomUUID3 } from "node:crypto";
|
|
5751
6125
|
import {
|
|
5752
6126
|
closeSync as closeSync3,
|
|
5753
6127
|
fsyncSync as fsyncSync2,
|
|
5754
|
-
lstatSync as
|
|
6128
|
+
lstatSync as lstatSync6,
|
|
5755
6129
|
openSync as openSync3,
|
|
5756
|
-
realpathSync as
|
|
6130
|
+
realpathSync as realpathSync4,
|
|
5757
6131
|
unlinkSync as unlinkSync2,
|
|
5758
|
-
writeFileSync as
|
|
6132
|
+
writeFileSync as writeFileSync3
|
|
5759
6133
|
} from "node:fs";
|
|
5760
6134
|
import { hostname as hostname2 } from "node:os";
|
|
5761
|
-
import { dirname as
|
|
6135
|
+
import { dirname as dirname6, join as join7 } from "node:path";
|
|
5762
6136
|
|
|
5763
6137
|
// src/service/liveness.ts
|
|
5764
|
-
import { spawnSync as
|
|
5765
|
-
import { readFileSync } from "node:fs";
|
|
6138
|
+
import { spawnSync as spawnSync3 } from "node:child_process";
|
|
6139
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
5766
6140
|
import { hostname, uptime } from "node:os";
|
|
5767
6141
|
var LEGACY_BOOT_MARGIN_MS = 10 * 60 * 1000;
|
|
5768
6142
|
var REUSE_SLACK_MS = 5000;
|
|
@@ -5774,11 +6148,11 @@ function bootId() {
|
|
|
5774
6148
|
cachedBoot = null;
|
|
5775
6149
|
try {
|
|
5776
6150
|
if (process.platform === "linux") {
|
|
5777
|
-
const id =
|
|
6151
|
+
const id = readFileSync2("/proc/sys/kernel/random/boot_id", "utf8").trim();
|
|
5778
6152
|
if (/^[0-9a-f-]{36}$/.test(id))
|
|
5779
6153
|
cachedBoot = id;
|
|
5780
6154
|
} else if (process.platform === "darwin") {
|
|
5781
|
-
const r =
|
|
6155
|
+
const r = spawnSync3("sysctl", ["-n", "kern.bootsessionuuid"], {
|
|
5782
6156
|
encoding: "utf8",
|
|
5783
6157
|
timeout: 2000
|
|
5784
6158
|
});
|
|
@@ -5794,7 +6168,7 @@ function bootId() {
|
|
|
5794
6168
|
function bootTime() {
|
|
5795
6169
|
if (process.platform === "linux")
|
|
5796
6170
|
try {
|
|
5797
|
-
const line = /^btime (\d+)$/m.exec(
|
|
6171
|
+
const line = /^btime (\d+)$/m.exec(readFileSync2("/proc/stat", "utf8"));
|
|
5798
6172
|
if (line)
|
|
5799
6173
|
return Number(line[1]) * 1000;
|
|
5800
6174
|
} catch {}
|
|
@@ -5802,7 +6176,7 @@ function bootTime() {
|
|
|
5802
6176
|
}
|
|
5803
6177
|
function linuxStat(pid) {
|
|
5804
6178
|
try {
|
|
5805
|
-
const stat =
|
|
6179
|
+
const stat = readFileSync2(`/proc/${pid}/stat`, "utf8");
|
|
5806
6180
|
return stat.slice(stat.lastIndexOf(")") + 2).trim().split(" ");
|
|
5807
6181
|
} catch {
|
|
5808
6182
|
return null;
|
|
@@ -5817,7 +6191,7 @@ function processStart(pid) {
|
|
|
5817
6191
|
return ticks && /^\d+$/.test(ticks) ? `ticks:${ticks}` : null;
|
|
5818
6192
|
}
|
|
5819
6193
|
if (process.platform === "darwin") {
|
|
5820
|
-
const r =
|
|
6194
|
+
const r = spawnSync3("ps", ["-o", "lstart=", "-p", String(pid)], {
|
|
5821
6195
|
encoding: "utf8",
|
|
5822
6196
|
timeout: 2000,
|
|
5823
6197
|
env: { ...process.env, LC_ALL: "C" }
|
|
@@ -5926,14 +6300,14 @@ async function stopProcessGroup(pgid, waitMs = 5000) {
|
|
|
5926
6300
|
send("SIGTERM");
|
|
5927
6301
|
const deadline = Date.now() + waitMs;
|
|
5928
6302
|
while (Date.now() < deadline) {
|
|
5929
|
-
await new Promise((
|
|
6303
|
+
await new Promise((resolve4) => setTimeout(resolve4, 100));
|
|
5930
6304
|
if (gone())
|
|
5931
6305
|
return true;
|
|
5932
6306
|
if (Date.now() > deadline - waitMs / 2)
|
|
5933
6307
|
send("SIGKILL");
|
|
5934
6308
|
}
|
|
5935
6309
|
send("SIGKILL");
|
|
5936
|
-
await new Promise((
|
|
6310
|
+
await new Promise((resolve4) => setTimeout(resolve4, 200));
|
|
5937
6311
|
return gone();
|
|
5938
6312
|
}
|
|
5939
6313
|
|
|
@@ -5942,7 +6316,7 @@ class CheckoutStore {
|
|
|
5942
6316
|
root;
|
|
5943
6317
|
privateRoot;
|
|
5944
6318
|
constructor(root, stateHome = defaultStateHome()) {
|
|
5945
|
-
this.root =
|
|
6319
|
+
this.root = realpathSync4(root);
|
|
5946
6320
|
if (privateWorkspace(this.root, stateHome))
|
|
5947
6321
|
throw new Error("Private state cannot be a workspace");
|
|
5948
6322
|
this.privateRoot = privateRootFor(this.root, stateHome);
|
|
@@ -5950,14 +6324,14 @@ class CheckoutStore {
|
|
|
5950
6324
|
throw new Error("Private state home must be outside the working copy");
|
|
5951
6325
|
}
|
|
5952
6326
|
get lockPath() {
|
|
5953
|
-
return
|
|
6327
|
+
return join7(this.privateRoot, "checkout.lock");
|
|
5954
6328
|
}
|
|
5955
6329
|
inspectPrivate() {
|
|
5956
|
-
const namespace =
|
|
6330
|
+
const namespace = dirname6(this.privateRoot);
|
|
5957
6331
|
for (const path of [namespace, this.privateRoot]) {
|
|
5958
6332
|
if (!safeDirectory(path))
|
|
5959
6333
|
continue;
|
|
5960
|
-
const st =
|
|
6334
|
+
const st = lstatSync6(path);
|
|
5961
6335
|
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
5962
6336
|
throw new Error("Private state permissions refused");
|
|
5963
6337
|
}
|
|
@@ -5967,7 +6341,7 @@ class CheckoutStore {
|
|
|
5967
6341
|
if (!safeDirectory(this.privateRoot))
|
|
5968
6342
|
return;
|
|
5969
6343
|
try {
|
|
5970
|
-
const st =
|
|
6344
|
+
const st = lstatSync6(path);
|
|
5971
6345
|
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1 || st.size > max || process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
5972
6346
|
throw new Error("Unsafe private state file");
|
|
5973
6347
|
} catch (error) {
|
|
@@ -6006,7 +6380,7 @@ class CheckoutStore {
|
|
|
6006
6380
|
throw new Error("Checkout busy or unresolved lock; inspect and explicitly recover only a dead owner");
|
|
6007
6381
|
}
|
|
6008
6382
|
try {
|
|
6009
|
-
|
|
6383
|
+
writeFileSync3(fd, JSON.stringify(lock));
|
|
6010
6384
|
fsyncSync2(fd);
|
|
6011
6385
|
} finally {
|
|
6012
6386
|
closeSync3(fd);
|
|
@@ -6042,7 +6416,7 @@ var registrationKeys = [
|
|
|
6042
6416
|
var reservationKeys = registrationKeys.filter((k) => !["schema", "phase", "checkoutToken"].includes(k));
|
|
6043
6417
|
function pathValue(value) {
|
|
6044
6418
|
const path = text(value, 4096, true);
|
|
6045
|
-
if (!
|
|
6419
|
+
if (!isAbsolute5(path))
|
|
6046
6420
|
refused();
|
|
6047
6421
|
return path;
|
|
6048
6422
|
}
|
|
@@ -6099,7 +6473,7 @@ function decodeRegistration(value) {
|
|
|
6099
6473
|
function rootsOverlap(a, b) {
|
|
6100
6474
|
const within = (x, y) => {
|
|
6101
6475
|
const r = relative3(x, y);
|
|
6102
|
-
return r === "" || r !== ".." && !r.startsWith(`..${
|
|
6476
|
+
return r === "" || r !== ".." && !r.startsWith(`..${sep4}`) && !isAbsolute5(r);
|
|
6103
6477
|
};
|
|
6104
6478
|
return within(a, b) || within(b, a);
|
|
6105
6479
|
}
|
|
@@ -6148,10 +6522,10 @@ class ServiceCoordination {
|
|
|
6148
6522
|
stateHome;
|
|
6149
6523
|
constructor(workspace, stateHome) {
|
|
6150
6524
|
this.project = workspace === null ? undefined : new CheckoutStore(resolveWorkspace(workspace).root, stateHome);
|
|
6151
|
-
this.stateHome = this.project ?
|
|
6152
|
-
const namespace =
|
|
6525
|
+
this.stateHome = this.project ? dirname7(dirname7(this.project.privateRoot)) : canonicalAmbientRoot(stateHome ?? defaultStateHome());
|
|
6526
|
+
const namespace = join8(this.stateHome, "cadence-assistant-v1");
|
|
6153
6527
|
this.files = new PrivateFiles(namespace);
|
|
6154
|
-
this.registryDirectory =
|
|
6528
|
+
this.registryDirectory = join8(namespace, "service-registry-v1");
|
|
6155
6529
|
}
|
|
6156
6530
|
get store() {
|
|
6157
6531
|
if (!this.project)
|
|
@@ -6159,13 +6533,13 @@ class ServiceCoordination {
|
|
|
6159
6533
|
return this.project;
|
|
6160
6534
|
}
|
|
6161
6535
|
get directory() {
|
|
6162
|
-
return
|
|
6536
|
+
return join8(this.store.privateRoot, "service-v1");
|
|
6163
6537
|
}
|
|
6164
6538
|
path(name) {
|
|
6165
|
-
return
|
|
6539
|
+
return join8(this.directory, name);
|
|
6166
6540
|
}
|
|
6167
6541
|
registryPath(name) {
|
|
6168
|
-
return
|
|
6542
|
+
return join8(this.registryDirectory, name);
|
|
6169
6543
|
}
|
|
6170
6544
|
registry() {
|
|
6171
6545
|
if (!this.files.directory(this.registryDirectory))
|
|
@@ -6212,14 +6586,14 @@ class ServiceCoordination {
|
|
|
6212
6586
|
"conversation-workflows-v1",
|
|
6213
6587
|
"workflows-v1"
|
|
6214
6588
|
]);
|
|
6215
|
-
this.files.directory(
|
|
6589
|
+
this.files.directory(join8(directory, "history-v2"), false, walks);
|
|
6216
6590
|
}
|
|
6217
6591
|
selected(directory = this.directory, root = this.store.root, stoppedOnly = false) {
|
|
6218
6592
|
const walks = new DirectoryWalks;
|
|
6219
6593
|
if (!this.files.directory(directory, false, walks))
|
|
6220
6594
|
return;
|
|
6221
6595
|
this.assertNamespace(walks, directory);
|
|
6222
|
-
const path = (name) =>
|
|
6596
|
+
const path = (name) => join8(directory, name);
|
|
6223
6597
|
const manifest = this.files.read(path("format.json"), undefined, walks);
|
|
6224
6598
|
const config = this.files.read(path("config.json"), undefined, walks);
|
|
6225
6599
|
if (!manifest || !config)
|
|
@@ -6241,7 +6615,7 @@ class ServiceCoordination {
|
|
|
6241
6615
|
const registration = this.files.read(path("registration.json"), undefined, walks);
|
|
6242
6616
|
const owner = this.files.read(path("owner.lock"), undefined, walks);
|
|
6243
6617
|
const capability = this.files.read(path("local-capability.json"), undefined, walks);
|
|
6244
|
-
const checkout = this.files.read(
|
|
6618
|
+
const checkout = this.files.read(join8(dirname7(directory), "checkout.lock"), 8192, walks);
|
|
6245
6619
|
const value = registration ? decodeRegistration(json(registration)) : undefined;
|
|
6246
6620
|
if (value && (value.root !== root || value.stateHome !== this.stateHome || value.configIdentity !== config.identity || !endpointMatches(c.endpoint, value.endpoint)))
|
|
6247
6621
|
refused();
|
|
@@ -6590,37 +6964,7 @@ import { isatty } from "node:tty";
|
|
|
6590
6964
|
import { randomBytes as randomBytes3, randomUUID as randomUUID5 } from "node:crypto";
|
|
6591
6965
|
import { opendirSync as opendirSync2 } from "node:fs";
|
|
6592
6966
|
import { hostname as hostname4 } from "node:os";
|
|
6593
|
-
import { join as
|
|
6594
|
-
|
|
6595
|
-
// src/service/contracts/gateway.ts
|
|
6596
|
-
function decodeProjectEntry(value) {
|
|
6597
|
-
const o = closed(value, ["id", "path", "cwd", "epoch"]);
|
|
6598
|
-
return {
|
|
6599
|
-
id: decodeHash(o.id),
|
|
6600
|
-
path: text(o.path, 4096, true),
|
|
6601
|
-
cwd: text(o.cwd, 4096, true),
|
|
6602
|
-
epoch: decodeId(o.epoch)
|
|
6603
|
-
};
|
|
6604
|
-
}
|
|
6605
|
-
var RECOVERY_SCOPES = ["registry", "workbench", "project"];
|
|
6606
|
-
var RECOVERY_CAUSES = [
|
|
6607
|
-
"reboot",
|
|
6608
|
-
"crash",
|
|
6609
|
-
"restart",
|
|
6610
|
-
"storage"
|
|
6611
|
-
];
|
|
6612
|
-
var RECOVERY_ACTIONS = [
|
|
6613
|
-
"released-locks",
|
|
6614
|
-
"stopped-workers",
|
|
6615
|
-
"stopped-owner",
|
|
6616
|
-
"continued-history",
|
|
6617
|
-
"archived-history",
|
|
6618
|
-
"archived-state",
|
|
6619
|
-
"rebuilt-registry",
|
|
6620
|
-
"removed-temporaries"
|
|
6621
|
-
];
|
|
6622
|
-
|
|
6623
|
-
// src/service/gateway-store.ts
|
|
6967
|
+
import { join as join9, resolve as resolve4 } from "node:path";
|
|
6624
6968
|
function decodeGatewayInput(value) {
|
|
6625
6969
|
const o = closed(value, [
|
|
6626
6970
|
"kind",
|
|
@@ -6631,7 +6975,7 @@ function decodeGatewayInput(value) {
|
|
|
6631
6975
|
]);
|
|
6632
6976
|
const absolute = (v) => {
|
|
6633
6977
|
const p = text(v, 4096, true);
|
|
6634
|
-
if (
|
|
6978
|
+
if (resolve4(p) !== p)
|
|
6635
6979
|
throw new ContractError("invalid");
|
|
6636
6980
|
return p;
|
|
6637
6981
|
};
|
|
@@ -6645,7 +6989,7 @@ function decodeGatewayInput(value) {
|
|
|
6645
6989
|
}
|
|
6646
6990
|
function decodeGatewayProject(value) {
|
|
6647
6991
|
const project = decodeProjectEntry(value);
|
|
6648
|
-
if (
|
|
6992
|
+
if (resolve4(project.path) !== project.path || resolve4(project.cwd) !== project.cwd)
|
|
6649
6993
|
throw new ContractError("corrupt-selected");
|
|
6650
6994
|
return project;
|
|
6651
6995
|
}
|
|
@@ -6705,11 +7049,11 @@ class GatewayStore {
|
|
|
6705
7049
|
constructor(serverConfig, stateHome = defaultStateHome()) {
|
|
6706
7050
|
this.serverConfig = serverConfig;
|
|
6707
7051
|
this.stateHome = canonicalAmbientRoot(stateHome);
|
|
6708
|
-
this.files = new PrivateFiles(
|
|
6709
|
-
this.directory =
|
|
7052
|
+
this.files = new PrivateFiles(join9(this.stateHome, "cadence-assistant-v1"));
|
|
7053
|
+
this.directory = join9(this.files.namespace, "workbenches-v1", hash2(resolve4(serverConfig)));
|
|
6710
7054
|
}
|
|
6711
7055
|
path(name) {
|
|
6712
|
-
return
|
|
7056
|
+
return join9(this.directory, name);
|
|
6713
7057
|
}
|
|
6714
7058
|
read() {
|
|
6715
7059
|
const file = this.files.read(this.path("gateway.json"));
|
|
@@ -6808,7 +7152,7 @@ class GatewayStore {
|
|
|
6808
7152
|
for (let entry = stream.readSync();entry; entry = stream.readSync()) {
|
|
6809
7153
|
if (!entry.isFile() || !/^[a-f0-9]{64}\.json$/.test(entry.name) || out.length >= 128)
|
|
6810
7154
|
throw new ContractError("corrupt-selected");
|
|
6811
|
-
const file = this.files.read(
|
|
7155
|
+
const file = this.files.read(join9(directory, entry.name));
|
|
6812
7156
|
if (!file)
|
|
6813
7157
|
throw new ContractError("corrupt-selected");
|
|
6814
7158
|
const project = decodeGatewayProject(json(file));
|
|
@@ -6854,12 +7198,12 @@ class GatewayStore {
|
|
|
6854
7198
|
// src/service/recovery-log.ts
|
|
6855
7199
|
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
6856
7200
|
import {
|
|
6857
|
-
lstatSync as
|
|
7201
|
+
lstatSync as lstatSync7,
|
|
6858
7202
|
opendirSync as opendirSync3,
|
|
6859
|
-
renameSync as
|
|
6860
|
-
rmSync
|
|
7203
|
+
renameSync as renameSync3,
|
|
7204
|
+
rmSync as rmSync2
|
|
6861
7205
|
} from "node:fs";
|
|
6862
|
-
import { basename, join as
|
|
7206
|
+
import { basename, join as join10, relative as relative4 } from "node:path";
|
|
6863
7207
|
var RECOVERY_NOTICE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
6864
7208
|
var RECOVERY_EVENTS = 50;
|
|
6865
7209
|
var RECOVERY_ARCHIVE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
@@ -6912,7 +7256,7 @@ function entries(path, maximum = 4096) {
|
|
|
6912
7256
|
function size(path, limit = { left: 1e5 }) {
|
|
6913
7257
|
let st;
|
|
6914
7258
|
try {
|
|
6915
|
-
st =
|
|
7259
|
+
st = lstatSync7(path);
|
|
6916
7260
|
} catch {
|
|
6917
7261
|
return 0;
|
|
6918
7262
|
}
|
|
@@ -6922,7 +7266,7 @@ function size(path, limit = { left: 1e5 }) {
|
|
|
6922
7266
|
return st.size;
|
|
6923
7267
|
let total = 0;
|
|
6924
7268
|
for (const name of entries(path, 1e5))
|
|
6925
|
-
total += size(
|
|
7269
|
+
total += size(join10(path, name), limit);
|
|
6926
7270
|
return total;
|
|
6927
7271
|
}
|
|
6928
7272
|
|
|
@@ -6932,12 +7276,12 @@ class RecoveryLog {
|
|
|
6932
7276
|
archiveDirectory;
|
|
6933
7277
|
constructor(files) {
|
|
6934
7278
|
this.files = files;
|
|
6935
|
-
this.logDirectory =
|
|
6936
|
-
this.archiveDirectory =
|
|
7279
|
+
this.logDirectory = join10(files.namespace, "recovery-log-v1");
|
|
7280
|
+
this.archiveDirectory = join10(files.namespace, "recovery-archive-v1");
|
|
6937
7281
|
}
|
|
6938
7282
|
archive() {
|
|
6939
7283
|
const name = `${Date.now()}-${randomUUID6()}`;
|
|
6940
|
-
const directory =
|
|
7284
|
+
const directory = join10(this.archiveDirectory, name);
|
|
6941
7285
|
let used = false;
|
|
6942
7286
|
return {
|
|
6943
7287
|
name,
|
|
@@ -6946,8 +7290,8 @@ class RecoveryLog {
|
|
|
6946
7290
|
if (!rel || rel.startsWith(".."))
|
|
6947
7291
|
throw new ContractError("forbidden");
|
|
6948
7292
|
this.files.directory(directory, true);
|
|
6949
|
-
const target =
|
|
6950
|
-
retryWindowsFileOperation(() =>
|
|
7293
|
+
const target = join10(directory, as ?? rel.split(/[\\/]/).join("--"));
|
|
7294
|
+
retryWindowsFileOperation(() => renameSync3(path, target));
|
|
6951
7295
|
used = true;
|
|
6952
7296
|
},
|
|
6953
7297
|
used: () => used
|
|
@@ -6975,7 +7319,7 @@ class RecoveryLog {
|
|
|
6975
7319
|
};
|
|
6976
7320
|
decodeRecoveryEvent(value);
|
|
6977
7321
|
try {
|
|
6978
|
-
this.files.create(
|
|
7322
|
+
this.files.create(join10(this.logDirectory, `${value.at}-${value.id}.json`), encoded(value), EVENT_BYTES);
|
|
6979
7323
|
this.prune();
|
|
6980
7324
|
} catch {}
|
|
6981
7325
|
return value;
|
|
@@ -6984,7 +7328,7 @@ class RecoveryLog {
|
|
|
6984
7328
|
for (const name of entries(this.logDirectory).reverse()) {
|
|
6985
7329
|
if (!/^[0-9]{13}-[0-9a-f-]{36}\.json$/.test(name))
|
|
6986
7330
|
continue;
|
|
6987
|
-
const path =
|
|
7331
|
+
const path = join10(this.logDirectory, name);
|
|
6988
7332
|
const snapshot = this.files.read(path, EVENT_BYTES);
|
|
6989
7333
|
if (!snapshot)
|
|
6990
7334
|
return;
|
|
@@ -7004,7 +7348,7 @@ class RecoveryLog {
|
|
|
7004
7348
|
if (!/^[0-9]{13}-[0-9a-f-]{36}\.json$/.test(name))
|
|
7005
7349
|
continue;
|
|
7006
7350
|
try {
|
|
7007
|
-
const file = this.files.read(
|
|
7351
|
+
const file = this.files.read(join10(this.logDirectory, name), EVENT_BYTES);
|
|
7008
7352
|
if (file)
|
|
7009
7353
|
out.push(decodeRecoveryEvent(json(file, EVENT_BYTES)));
|
|
7010
7354
|
} catch {}
|
|
@@ -7014,13 +7358,13 @@ class RecoveryLog {
|
|
|
7014
7358
|
prune(now = Date.now()) {
|
|
7015
7359
|
const names2 = entries(this.logDirectory);
|
|
7016
7360
|
for (const name of names2.slice(0, Math.max(0, names2.length - RECOVERY_EVENTS)))
|
|
7017
|
-
|
|
7361
|
+
rmSync2(join10(this.logDirectory, name), { force: true });
|
|
7018
7362
|
const archived = entries(this.archiveDirectory).filter((name) => /^[0-9]{13}-[0-9a-f-]{36}$/.test(name));
|
|
7019
7363
|
const kept = [];
|
|
7020
7364
|
for (const name of archived) {
|
|
7021
|
-
const path =
|
|
7365
|
+
const path = join10(this.archiveDirectory, name);
|
|
7022
7366
|
if (now - Number(name.slice(0, 13)) > RECOVERY_ARCHIVE_MS)
|
|
7023
|
-
|
|
7367
|
+
rmSync2(path, { recursive: true, force: true });
|
|
7024
7368
|
else
|
|
7025
7369
|
kept.push({ name, bytes: size(path) });
|
|
7026
7370
|
}
|
|
@@ -7028,7 +7372,7 @@ class RecoveryLog {
|
|
|
7028
7372
|
for (const entry of kept) {
|
|
7029
7373
|
if (total <= RECOVERY_ARCHIVE_BYTES)
|
|
7030
7374
|
break;
|
|
7031
|
-
|
|
7375
|
+
rmSync2(join10(this.archiveDirectory, entry.name), {
|
|
7032
7376
|
recursive: true,
|
|
7033
7377
|
force: true
|
|
7034
7378
|
});
|
|
@@ -7051,23 +7395,23 @@ class RecoveryLog {
|
|
|
7051
7395
|
const names2 = entries(this.archiveDirectory);
|
|
7052
7396
|
return {
|
|
7053
7397
|
entries: names2.length,
|
|
7054
|
-
bytes: names2.reduce((sum, name) => sum + size(
|
|
7398
|
+
bytes: names2.reduce((sum, name) => sum + size(join10(this.archiveDirectory, name)), 0)
|
|
7055
7399
|
};
|
|
7056
7400
|
}
|
|
7057
7401
|
}
|
|
7058
7402
|
function strayTemporaries(directory) {
|
|
7059
|
-
return entries(directory).filter((name) => /^pending-[0-9a-f-]{36}\.json$/.test(name)).map((name) =>
|
|
7403
|
+
return entries(directory).filter((name) => /^pending-[0-9a-f-]{36}\.json$/.test(name)).map((name) => join10(directory, name));
|
|
7060
7404
|
}
|
|
7061
7405
|
|
|
7062
7406
|
// src/service/self-heal.ts
|
|
7063
7407
|
import { createHash as createHash5, randomUUID as randomUUID7 } from "node:crypto";
|
|
7064
|
-
import { lstatSync as
|
|
7408
|
+
import { lstatSync as lstatSync8, opendirSync as opendirSync4 } from "node:fs";
|
|
7065
7409
|
import { hostname as hostname5 } from "node:os";
|
|
7066
|
-
import { join as
|
|
7410
|
+
import { join as join12 } from "node:path";
|
|
7067
7411
|
|
|
7068
7412
|
// src/service/process-ledger.ts
|
|
7069
|
-
import { rmSync as
|
|
7070
|
-
import { join as
|
|
7413
|
+
import { rmSync as rmSync3 } from "node:fs";
|
|
7414
|
+
import { join as join11 } from "node:path";
|
|
7071
7415
|
var LEDGER_BYTES = 64 * 1024;
|
|
7072
7416
|
var LEDGER_WORKERS = 512;
|
|
7073
7417
|
function decode2(value) {
|
|
@@ -7092,10 +7436,10 @@ class ProcessLedger {
|
|
|
7092
7436
|
directory;
|
|
7093
7437
|
constructor(files) {
|
|
7094
7438
|
this.files = files;
|
|
7095
|
-
this.directory =
|
|
7439
|
+
this.directory = join11(files.namespace, "process-ledger-v1");
|
|
7096
7440
|
}
|
|
7097
7441
|
path(epoch) {
|
|
7098
|
-
return
|
|
7442
|
+
return join11(this.directory, `${decodeId(epoch)}.json`);
|
|
7099
7443
|
}
|
|
7100
7444
|
file(epoch) {
|
|
7101
7445
|
try {
|
|
@@ -7114,7 +7458,7 @@ class ProcessLedger {
|
|
|
7114
7458
|
}
|
|
7115
7459
|
remove(epoch) {
|
|
7116
7460
|
try {
|
|
7117
|
-
|
|
7461
|
+
rmSync3(this.path(epoch), { force: true });
|
|
7118
7462
|
} catch {}
|
|
7119
7463
|
}
|
|
7120
7464
|
open(epoch) {
|
|
@@ -7289,7 +7633,7 @@ async function stopOwnerProcess(pid) {
|
|
|
7289
7633
|
return gone();
|
|
7290
7634
|
}
|
|
7291
7635
|
for (let n = 0;n < 50; n++) {
|
|
7292
|
-
await new Promise((
|
|
7636
|
+
await new Promise((resolve5) => setTimeout(resolve5, 100));
|
|
7293
7637
|
if (gone())
|
|
7294
7638
|
return true;
|
|
7295
7639
|
}
|
|
@@ -7297,7 +7641,7 @@ async function stopOwnerProcess(pid) {
|
|
|
7297
7641
|
process.kill(pid, "SIGKILL");
|
|
7298
7642
|
} catch {}
|
|
7299
7643
|
for (let n = 0;n < 20; n++) {
|
|
7300
|
-
await new Promise((
|
|
7644
|
+
await new Promise((resolve5) => setTimeout(resolve5, 100));
|
|
7301
7645
|
if (gone())
|
|
7302
7646
|
return true;
|
|
7303
7647
|
}
|
|
@@ -7314,7 +7658,7 @@ class RegistryHealer {
|
|
|
7314
7658
|
this.ledger = new ProcessLedger(c.files);
|
|
7315
7659
|
}
|
|
7316
7660
|
path(name) {
|
|
7317
|
-
return
|
|
7661
|
+
return join12(this.c.registryDirectory, name);
|
|
7318
7662
|
}
|
|
7319
7663
|
async heal(options = {}) {
|
|
7320
7664
|
const lockPath = this.path("registry.lock");
|
|
@@ -7325,7 +7669,7 @@ class RegistryHealer {
|
|
|
7325
7669
|
lock = undefined;
|
|
7326
7670
|
const age = (() => {
|
|
7327
7671
|
try {
|
|
7328
|
-
return Date.now() -
|
|
7672
|
+
return Date.now() - lstatSync8(lockPath).mtimeMs;
|
|
7329
7673
|
} catch {
|
|
7330
7674
|
return 0;
|
|
7331
7675
|
}
|
|
@@ -7369,7 +7713,7 @@ class RegistryHealer {
|
|
|
7369
7713
|
pid: mark?.pid ?? null
|
|
7370
7714
|
};
|
|
7371
7715
|
if (holder.state === "live") {
|
|
7372
|
-
await new Promise((
|
|
7716
|
+
await new Promise((resolve5) => setTimeout(resolve5, 250));
|
|
7373
7717
|
if (!this.c.files.read(lockPath))
|
|
7374
7718
|
return { status: "clean" };
|
|
7375
7719
|
return { status: "live" };
|
|
@@ -7479,14 +7823,14 @@ function liveReservations(c) {
|
|
|
7479
7823
|
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
7480
7824
|
if (!/^[a-f0-9]{64}$/.test(e.name))
|
|
7481
7825
|
continue;
|
|
7482
|
-
const service =
|
|
7483
|
-
const registration2 = readFile(c.files,
|
|
7826
|
+
const service = join12(c.files.namespace, e.name, "service-v1");
|
|
7827
|
+
const registration2 = readFile(c.files, join12(service, "registration.json"), decodeRegistrationLenient);
|
|
7484
7828
|
if (!registration2 || registration2 === "unreadable")
|
|
7485
7829
|
continue;
|
|
7486
7830
|
const r = registration2.value;
|
|
7487
7831
|
if (!r || r.phase === "stopped")
|
|
7488
7832
|
continue;
|
|
7489
|
-
const owner = readFile(c.files,
|
|
7833
|
+
const owner = readFile(c.files, join12(service, "owner.lock"), decodeOwnerLock);
|
|
7490
7834
|
const mark = owner && owner !== "unreadable" && owner.value ? owner.value : r;
|
|
7491
7835
|
if (holderState(mark, stampTime(registration2.snapshot.stamp)).state !== "live")
|
|
7492
7836
|
continue;
|
|
@@ -7507,20 +7851,20 @@ function liveReservations(c) {
|
|
|
7507
7851
|
return out;
|
|
7508
7852
|
}
|
|
7509
7853
|
function projectFiles(c, root) {
|
|
7510
|
-
const privateRoot =
|
|
7854
|
+
const privateRoot = join12(c.files.namespace, createHash5("sha256").update(root).digest("hex"));
|
|
7511
7855
|
return {
|
|
7512
7856
|
root,
|
|
7513
7857
|
privateRoot,
|
|
7514
|
-
service:
|
|
7515
|
-
checkoutPath:
|
|
7858
|
+
service: join12(privateRoot, "service-v1"),
|
|
7859
|
+
checkoutPath: join12(privateRoot, "checkout.lock")
|
|
7516
7860
|
};
|
|
7517
7861
|
}
|
|
7518
7862
|
function snapshotProject(c, p) {
|
|
7519
7863
|
const s = { temporaries: [] };
|
|
7520
|
-
s.registration = readFile(c.files,
|
|
7521
|
-
s.owner = readFile(c.files,
|
|
7864
|
+
s.registration = readFile(c.files, join12(p.service, "registration.json"), decodeRegistrationLenient);
|
|
7865
|
+
s.owner = readFile(c.files, join12(p.service, "owner.lock"), decodeOwnerLock);
|
|
7522
7866
|
try {
|
|
7523
|
-
s.capability = c.files.read(
|
|
7867
|
+
s.capability = c.files.read(join12(p.service, "local-capability.json"));
|
|
7524
7868
|
} catch {
|
|
7525
7869
|
s.capability = "unreadable";
|
|
7526
7870
|
}
|
|
@@ -7535,7 +7879,7 @@ function snapshotProject(c, p) {
|
|
|
7535
7879
|
} catch {}
|
|
7536
7880
|
s.temporaries = [
|
|
7537
7881
|
...strayTemporaries(p.service),
|
|
7538
|
-
...strayTemporaries(
|
|
7882
|
+
...strayTemporaries(join12(p.service, "history-v2"))
|
|
7539
7883
|
];
|
|
7540
7884
|
return s;
|
|
7541
7885
|
}
|
|
@@ -7648,8 +7992,8 @@ function metadataDamaged(c, p) {
|
|
|
7648
7992
|
return false;
|
|
7649
7993
|
}
|
|
7650
7994
|
try {
|
|
7651
|
-
const format = c.files.read(
|
|
7652
|
-
const config = c.files.read(
|
|
7995
|
+
const format = c.files.read(join12(p.service, "format.json"));
|
|
7996
|
+
const config = c.files.read(join12(p.service, "config.json"));
|
|
7653
7997
|
if (!format || !config)
|
|
7654
7998
|
return true;
|
|
7655
7999
|
const f = closed(json(format), [
|
|
@@ -7661,10 +8005,10 @@ function metadataDamaged(c, p) {
|
|
|
7661
8005
|
if (f.schema !== "cadence-service-format/1" || f.root !== p.root || f.stateHome !== c.stateHome || f.configIdentity !== config.identity)
|
|
7662
8006
|
return true;
|
|
7663
8007
|
const selected = decodeOwnerConfig(json(config));
|
|
7664
|
-
const host = c.files.read(
|
|
8008
|
+
const host = c.files.read(join12(p.service, "host-config.json"));
|
|
7665
8009
|
if (host && host.identity !== selected.policyIdentity)
|
|
7666
8010
|
return true;
|
|
7667
|
-
const registration2 = c.files.read(
|
|
8011
|
+
const registration2 = c.files.read(join12(p.service, "registration.json"));
|
|
7668
8012
|
if (registration2) {
|
|
7669
8013
|
const r = decodeRegistration(json(registration2));
|
|
7670
8014
|
if (r.root !== p.root || r.stateHome !== c.stateHome || r.configIdentity !== config.identity)
|
|
@@ -7687,7 +8031,7 @@ function reclaim(c, p, expected, archive) {
|
|
|
7687
8031
|
mutate();
|
|
7688
8032
|
const registration2 = s.registration;
|
|
7689
8033
|
if (registration2 === "unreadable")
|
|
7690
|
-
archive.keep(
|
|
8034
|
+
archive.keep(join12(p.service, "registration.json"));
|
|
7691
8035
|
else if (registration2) {
|
|
7692
8036
|
if (!registration2.value)
|
|
7693
8037
|
archive.keep(registration2.path);
|
|
@@ -7695,7 +8039,7 @@ function reclaim(c, p, expected, archive) {
|
|
|
7695
8039
|
c.files.replace(registration2.path, registration2.snapshot, encoded({ ...registration2.value, phase: "stopped" }));
|
|
7696
8040
|
}
|
|
7697
8041
|
if (s.row)
|
|
7698
|
-
c.files.replace(
|
|
8042
|
+
c.files.replace(join12(c.registryDirectory, "roots.json"), view.snapshot, encoded({
|
|
7699
8043
|
...view.value,
|
|
7700
8044
|
entries: view.value.entries.filter((row) => row.root !== p.root)
|
|
7701
8045
|
}), 1024 * 1024);
|
|
@@ -7708,8 +8052,8 @@ function reclaim(c, p, expected, archive) {
|
|
|
7708
8052
|
}
|
|
7709
8053
|
c.files.remove(path, "snapshot" in value ? value.snapshot : value, maximum);
|
|
7710
8054
|
};
|
|
7711
|
-
drop(
|
|
7712
|
-
drop(
|
|
8055
|
+
drop(join12(p.service, "local-capability.json"), s.capability);
|
|
8056
|
+
drop(join12(p.service, "owner.lock"), s.owner);
|
|
7713
8057
|
drop(p.checkoutPath, s.checkout, 8192);
|
|
7714
8058
|
});
|
|
7715
8059
|
}
|
|
@@ -7876,7 +8220,7 @@ async function confirm(question) {
|
|
|
7876
8220
|
return false;
|
|
7877
8221
|
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
7878
8222
|
try {
|
|
7879
|
-
const answer = await new Promise((
|
|
8223
|
+
const answer = await new Promise((resolve5) => rl.question(`${question} [y/N] `, resolve5));
|
|
7880
8224
|
return /^y(es)?$/i.test(answer.trim());
|
|
7881
8225
|
} finally {
|
|
7882
8226
|
rl.close();
|
|
@@ -7918,12 +8262,12 @@ async function healProjectForStart(c, responsive) {
|
|
|
7918
8262
|
}
|
|
7919
8263
|
|
|
7920
8264
|
// src/service/runtime.ts
|
|
7921
|
-
import { homedir as
|
|
7922
|
-
import { dirname as
|
|
8265
|
+
import { homedir as homedir3 } from "node:os";
|
|
8266
|
+
import { dirname as dirname8, isAbsolute as isAbsolute6, join as join13, resolve as resolve5 } from "node:path";
|
|
7923
8267
|
var STARTUP_INPUT_BYTES = 16 * 1024;
|
|
7924
8268
|
function absolute(value) {
|
|
7925
8269
|
const result = text(value, 4096, true);
|
|
7926
|
-
if (!
|
|
8270
|
+
if (!isAbsolute6(result))
|
|
7927
8271
|
throw new ContractError("invalid");
|
|
7928
8272
|
return result;
|
|
7929
8273
|
}
|
|
@@ -8013,7 +8357,7 @@ function selectStart(options, ambient) {
|
|
|
8013
8357
|
throw new ContractError("invalid");
|
|
8014
8358
|
let fromFile;
|
|
8015
8359
|
if (options.config) {
|
|
8016
|
-
const path =
|
|
8360
|
+
const path = resolve5(options.config), f = new PrivateFiles(dirname8(path));
|
|
8017
8361
|
const bytes = f.read(path);
|
|
8018
8362
|
if (!bytes)
|
|
8019
8363
|
throw new ContractError("corrupt-selected");
|
|
@@ -8028,11 +8372,11 @@ function selectStart(options, ambient) {
|
|
|
8028
8372
|
port: options.port ?? 17423,
|
|
8029
8373
|
proxy: null
|
|
8030
8374
|
},
|
|
8031
|
-
agentDir:
|
|
8375
|
+
agentDir: resolve5(options.agentDir ?? ambient.PI_CODING_AGENT_DIR ?? join13(ambient.HOME ?? ambient.USERPROFILE ?? homedir3(), ".pi", "agent")),
|
|
8032
8376
|
tokenSource: options.tokenFile ? "file" : env ? "environment" : "generated",
|
|
8033
|
-
tokenFile: options.tokenFile ?
|
|
8377
|
+
tokenFile: options.tokenFile ? resolve5(options.tokenFile) : null
|
|
8034
8378
|
});
|
|
8035
|
-
const server = options.serverConfig !== undefined ?
|
|
8379
|
+
const server = options.serverConfig !== undefined ? resolve5(options.serverConfig) : old?.tokenSource === "config" ? old.tokenFile : null;
|
|
8036
8380
|
let selected = base;
|
|
8037
8381
|
if (server) {
|
|
8038
8382
|
if (options.tokenFile !== undefined)
|
|
@@ -8045,7 +8389,7 @@ function selectStart(options, ambient) {
|
|
|
8045
8389
|
tokenFile: server
|
|
8046
8390
|
});
|
|
8047
8391
|
}
|
|
8048
|
-
if (fromFile && encoded(fromFile) !== encoded(selected) || options.port !== undefined && options.port !== selected.http.port || options.host !== undefined && options.host !== selected.http.host || options.agentDir !== undefined &&
|
|
8392
|
+
if (fromFile && encoded(fromFile) !== encoded(selected) || options.port !== undefined && options.port !== selected.http.port || options.host !== undefined && options.host !== selected.http.host || options.agentDir !== undefined && resolve5(options.agentDir) !== selected.agentDir || options.tokenFile !== undefined && resolve5(options.tokenFile) !== selected.tokenFile || env && selected.tokenSource !== "environment" || selected.tokenSource === "environment" && !env || resolveWorkspace(selected.cwd).root !== workspace.root)
|
|
8049
8393
|
throw new ContractError("conflict");
|
|
8050
8394
|
return {
|
|
8051
8395
|
workspace: workspace.root,
|
|
@@ -8112,13 +8456,175 @@ function decodeStartupReport(value) {
|
|
|
8112
8456
|
};
|
|
8113
8457
|
}
|
|
8114
8458
|
|
|
8459
|
+
// src/service/maintenance.ts
|
|
8460
|
+
import { spawn, spawnSync as spawnSync4 } from "node:child_process";
|
|
8461
|
+
import { accessSync, constants as constants3, readFileSync as readFileSync3, realpathSync as realpathSync5 } from "node:fs";
|
|
8462
|
+
import { dirname as dirname9, join as join14 } from "node:path";
|
|
8463
|
+
var PACKAGE = "@abelxiaoxing/cadence";
|
|
8464
|
+
var JOB_SECONDS = 1800;
|
|
8465
|
+
var VIEW_MS = 30000;
|
|
8466
|
+
var INSTALL_MS = 15 * 60000;
|
|
8467
|
+
var RESTART_MS = 5 * 60000;
|
|
8468
|
+
function cgroupPath(text2) {
|
|
8469
|
+
const lines = text2?.split(`
|
|
8470
|
+
`) ?? [];
|
|
8471
|
+
const find = (pattern) => lines.map((line) => pattern.exec(line)?.[1]).find(Boolean);
|
|
8472
|
+
return find(/^\d+:name=systemd:(\/.*)$/) ?? find(/^0::(\/.*)$/);
|
|
8473
|
+
}
|
|
8474
|
+
function supervisingUnit(env, system) {
|
|
8475
|
+
const path = cgroupPath(system({}).cgroup());
|
|
8476
|
+
const match = path && /\/cadence(?:-([A-Za-z0-9][A-Za-z0-9_-]{0,47}))?\.service$/.exec(path);
|
|
8477
|
+
if (!path || !match || !env.INVOCATION_ID)
|
|
8478
|
+
return;
|
|
8479
|
+
const s = system({
|
|
8480
|
+
scope: /\/user@[0-9]+\.service\//.test(path) ? "user" : "system",
|
|
8481
|
+
name: match[1]
|
|
8482
|
+
});
|
|
8483
|
+
const invocation = s.systemctl(["show", "--property=InvocationID", "--value", s.unit]).stdout.trim();
|
|
8484
|
+
if (invocation !== env.INVOCATION_ID)
|
|
8485
|
+
return;
|
|
8486
|
+
const text2 = generatedUnit(s);
|
|
8487
|
+
return typeof text2 === "string" && unitWorkspace(text2) === null ? s : undefined;
|
|
8488
|
+
}
|
|
8489
|
+
function maintenanceUnit(s) {
|
|
8490
|
+
return s.unit.replace(/\.service$/, ".maintenance.service");
|
|
8491
|
+
}
|
|
8492
|
+
function installedVersion(root) {
|
|
8493
|
+
const value = JSON.parse(readFileSync3(join14(root, "package.json"), "utf8"));
|
|
8494
|
+
if (!value || typeof value !== "object" || !("name" in value))
|
|
8495
|
+
throw new ContractError("invalid");
|
|
8496
|
+
literal(value.name, PACKAGE);
|
|
8497
|
+
return decodeVersion("version" in value ? value.version : undefined);
|
|
8498
|
+
}
|
|
8499
|
+
|
|
8500
|
+
class MaintenanceRecord {
|
|
8501
|
+
store;
|
|
8502
|
+
constructor(store) {
|
|
8503
|
+
this.store = store;
|
|
8504
|
+
}
|
|
8505
|
+
get path() {
|
|
8506
|
+
return this.store.path("maintenance.json");
|
|
8507
|
+
}
|
|
8508
|
+
read() {
|
|
8509
|
+
const file = this.store.files.read(this.path);
|
|
8510
|
+
if (!file)
|
|
8511
|
+
return null;
|
|
8512
|
+
const o = closed(json(file), ["schema", "job"]);
|
|
8513
|
+
literal(o.schema, "cadence-maintenance-job/1");
|
|
8514
|
+
return decodeMaintenanceJob(o.job);
|
|
8515
|
+
}
|
|
8516
|
+
write(job2) {
|
|
8517
|
+
const bytes = encoded({ schema: "cadence-maintenance-job/1", job: job2 });
|
|
8518
|
+
const current = this.store.files.read(this.path);
|
|
8519
|
+
if (current)
|
|
8520
|
+
this.store.files.replace(this.path, current, bytes);
|
|
8521
|
+
else
|
|
8522
|
+
this.store.files.create(this.path, bytes);
|
|
8523
|
+
}
|
|
8524
|
+
finish(id, state, reason, version) {
|
|
8525
|
+
const job2 = this.read();
|
|
8526
|
+
if (job2?.id !== id || job2.state !== "running")
|
|
8527
|
+
return;
|
|
8528
|
+
this.write({ ...job2, state, reason, version, endedAt: Date.now() });
|
|
8529
|
+
}
|
|
8530
|
+
}
|
|
8531
|
+
function launchJob(s, job2) {
|
|
8532
|
+
const text2 = generatedUnit(s);
|
|
8533
|
+
if (typeof text2 !== "string")
|
|
8534
|
+
return false;
|
|
8535
|
+
const scope = s.scope === "system" ? "--system" : "--user";
|
|
8536
|
+
const name = serviceName(s);
|
|
8537
|
+
const result = spawnSync4("systemd-run", [
|
|
8538
|
+
scope,
|
|
8539
|
+
`--unit=${maintenanceUnit(s)}`,
|
|
8540
|
+
`--description=Cadence ${job2.action} requested from the WebUI`,
|
|
8541
|
+
"--collect",
|
|
8542
|
+
"--quiet",
|
|
8543
|
+
`--property=RuntimeMaxSec=${JOB_SECONDS}`,
|
|
8544
|
+
...[...new Map([["HOME", s.home], ...unitEnvironment(text2)])].map(([key, value]) => `--setenv=${key}=${value}`),
|
|
8545
|
+
"--",
|
|
8546
|
+
s.node,
|
|
8547
|
+
join14(s.root, "src", "service-cli.mjs"),
|
|
8548
|
+
"server",
|
|
8549
|
+
job2.action,
|
|
8550
|
+
scope,
|
|
8551
|
+
...name ? ["--name", name] : [],
|
|
8552
|
+
...job2.to ? ["--to", job2.to] : [],
|
|
8553
|
+
"--job",
|
|
8554
|
+
job2.id
|
|
8555
|
+
], { stdio: "ignore", timeout: 20000 });
|
|
8556
|
+
return !result.error && result.status === 0;
|
|
8557
|
+
}
|
|
8558
|
+
function jobRunning(s) {
|
|
8559
|
+
return s.systemctl(["is-active", "--quiet", maintenanceUnit(s)]).status === 0;
|
|
8560
|
+
}
|
|
8561
|
+
var npm = (args, show = false) => run("npm", args, show ? INSTALL_MS : VIEW_MS, show);
|
|
8562
|
+
function run(command, args, timeout, show = false) {
|
|
8563
|
+
return new Promise((resolve6) => {
|
|
8564
|
+
const child = spawn(command, args, {
|
|
8565
|
+
stdio: ["ignore", show ? 2 : "pipe", show ? 2 : "ignore"],
|
|
8566
|
+
timeout,
|
|
8567
|
+
windowsHide: true
|
|
8568
|
+
});
|
|
8569
|
+
let stdout = "";
|
|
8570
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk) => {
|
|
8571
|
+
if (stdout.length < 1 << 20)
|
|
8572
|
+
stdout += chunk;
|
|
8573
|
+
});
|
|
8574
|
+
child.once("error", () => resolve6({ status: null, stdout: "" }));
|
|
8575
|
+
child.once("close", (status) => resolve6({ status, stdout }));
|
|
8576
|
+
});
|
|
8577
|
+
}
|
|
8578
|
+
async function upgradeRoute(root, npmRun = npm) {
|
|
8579
|
+
const result = await npmRun(["root", "--global"]);
|
|
8580
|
+
const global = result.status === 0 ? result.stdout.trim() : "";
|
|
8581
|
+
let installed;
|
|
8582
|
+
try {
|
|
8583
|
+
installed = realpathSync5(join14(global, ...PACKAGE.split("/")));
|
|
8584
|
+
if (!global || installed !== realpathSync5(root))
|
|
8585
|
+
return "not-npm-global";
|
|
8586
|
+
} catch {
|
|
8587
|
+
return "not-npm-global";
|
|
8588
|
+
}
|
|
8589
|
+
try {
|
|
8590
|
+
accessSync(dirname9(installed), constants3.W_OK);
|
|
8591
|
+
accessSync(global, constants3.W_OK);
|
|
8592
|
+
} catch {
|
|
8593
|
+
return "not-writable";
|
|
8594
|
+
}
|
|
8595
|
+
return "npm";
|
|
8596
|
+
}
|
|
8597
|
+
async function release(spec = "latest", npmRun = npm) {
|
|
8598
|
+
const result = await npmRun([
|
|
8599
|
+
"view",
|
|
8600
|
+
`${PACKAGE}@${spec}`,
|
|
8601
|
+
"version",
|
|
8602
|
+
"deprecated",
|
|
8603
|
+
"--json"
|
|
8604
|
+
]);
|
|
8605
|
+
if (result.status !== 0)
|
|
8606
|
+
return;
|
|
8607
|
+
try {
|
|
8608
|
+
const value = JSON.parse(result.stdout);
|
|
8609
|
+
if (typeof value === "string")
|
|
8610
|
+
return { version: decodeVersion(value), deprecated: false };
|
|
8611
|
+
const o = closed(value, ["version", "deprecated"]);
|
|
8612
|
+
return {
|
|
8613
|
+
version: decodeVersion(o.version),
|
|
8614
|
+
deprecated: typeof o.deprecated === "string"
|
|
8615
|
+
};
|
|
8616
|
+
} catch {
|
|
8617
|
+
return;
|
|
8618
|
+
}
|
|
8619
|
+
}
|
|
8620
|
+
|
|
8115
8621
|
// src/service/cli.ts
|
|
8116
8622
|
function nativeCall(c, operation, reveal = false, controlBody) {
|
|
8117
8623
|
const access = c.localAccess();
|
|
8118
8624
|
const url = new URL(`${access.endpoint}/local/v1/admin/${operation}`);
|
|
8119
8625
|
const write2 = !["status", "diagnose"].includes(operation);
|
|
8120
8626
|
const body = write2 ? JSON.stringify(controlBody ?? (reveal ? { reveal: true } : {})) : undefined;
|
|
8121
|
-
return new Promise((
|
|
8627
|
+
return new Promise((resolve7, reject) => {
|
|
8122
8628
|
let timer;
|
|
8123
8629
|
const fail = () => {
|
|
8124
8630
|
clearTimeout(timer);
|
|
@@ -8159,7 +8665,7 @@ function nativeCall(c, operation, reveal = false, controlBody) {
|
|
|
8159
8665
|
throw new ContractError("stale");
|
|
8160
8666
|
}
|
|
8161
8667
|
clearTimeout(timer);
|
|
8162
|
-
|
|
8668
|
+
resolve7(reply.value);
|
|
8163
8669
|
} catch {
|
|
8164
8670
|
fail();
|
|
8165
8671
|
}
|
|
@@ -8174,7 +8680,7 @@ function nativeCall(c, operation, reveal = false, controlBody) {
|
|
|
8174
8680
|
req.end(body);
|
|
8175
8681
|
});
|
|
8176
8682
|
}
|
|
8177
|
-
var pause = (ms) => new Promise((
|
|
8683
|
+
var pause = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
|
|
8178
8684
|
async function serviceStatus(c, diagnose = false) {
|
|
8179
8685
|
const registryLock = c.registryLockDiagnostic?.();
|
|
8180
8686
|
if (registryLock)
|
|
@@ -8274,10 +8780,10 @@ function startupGuidance(result, input) {
|
|
|
8274
8780
|
return `Pi's agent directory ${agent} must be an existing real directory (not a symlink) owned by you.
|
|
8275
8781
|
`;
|
|
8276
8782
|
case "pi-auth":
|
|
8277
|
-
return `${
|
|
8783
|
+
return `${join15(agent, "auth.json")} must be your own single-link regular file holding valid Pi credentials; log in to Pi first.
|
|
8278
8784
|
`;
|
|
8279
8785
|
case "pi-models":
|
|
8280
|
-
return `${
|
|
8786
|
+
return `${join15(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.
|
|
8281
8787
|
`;
|
|
8282
8788
|
case "pi-runtime":
|
|
8283
8789
|
return `Loading Pi's SDK model runtime failed (optional SDK packages missing, or sources the SDK refuses). Fix that and start again; the next start recovers the owner's registration automatically.
|
|
@@ -8337,7 +8843,7 @@ async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
|
|
|
8337
8843
|
throw new ContractError("oversize");
|
|
8338
8844
|
if (!sameNetwork(network, networkSnapshot(process.env)))
|
|
8339
8845
|
throw new ContractError("stale");
|
|
8340
|
-
const child =
|
|
8846
|
+
const child = spawn2(process.execPath, [
|
|
8341
8847
|
...env.NODE_USE_ENV_PROXY === "1" ? ["--use-env-proxy"] : [],
|
|
8342
8848
|
fileURLToPath2(hostUrl)
|
|
8343
8849
|
], {
|
|
@@ -8348,18 +8854,18 @@ async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
|
|
|
8348
8854
|
stdio: ["pipe", "pipe", "ignore"]
|
|
8349
8855
|
});
|
|
8350
8856
|
let completed = false;
|
|
8351
|
-
const closure = new Promise((
|
|
8857
|
+
const closure = new Promise((resolve7) => child.once("close", (code2, signal) => {
|
|
8352
8858
|
completed = true;
|
|
8353
8859
|
if (foreground && (code2 !== 0 || signal !== null))
|
|
8354
8860
|
process.exitCode = 1;
|
|
8355
|
-
|
|
8861
|
+
resolve7();
|
|
8356
8862
|
}));
|
|
8357
|
-
const startup = new Promise((
|
|
8863
|
+
const startup = new Promise((resolve7) => {
|
|
8358
8864
|
let bytes = "", resolved = false;
|
|
8359
8865
|
const finish = (value) => {
|
|
8360
8866
|
if (!resolved) {
|
|
8361
8867
|
resolved = true;
|
|
8362
|
-
|
|
8868
|
+
resolve7(value);
|
|
8363
8869
|
}
|
|
8364
8870
|
};
|
|
8365
8871
|
child.on("error", () => finish({ status: "unconfirmed" }));
|
|
@@ -8399,8 +8905,8 @@ async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
|
|
|
8399
8905
|
let timer;
|
|
8400
8906
|
const result = await Promise.race([
|
|
8401
8907
|
startup,
|
|
8402
|
-
new Promise((
|
|
8403
|
-
timer = setTimeout(() =>
|
|
8908
|
+
new Promise((resolve7) => {
|
|
8909
|
+
timer = setTimeout(() => resolve7({ status: "unconfirmed" }), LIMITS.startWaitMs);
|
|
8404
8910
|
})
|
|
8405
8911
|
]);
|
|
8406
8912
|
clearTimeout(timer);
|
|
@@ -8425,12 +8931,34 @@ async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
|
|
|
8425
8931
|
}
|
|
8426
8932
|
async function privateLink(workspace, stateHome, config) {
|
|
8427
8933
|
try {
|
|
8428
|
-
const value = closed(await nativeCall(workspace === null ? new GatewayStore(config ?? serverConfigPath(
|
|
8934
|
+
const value = closed(await nativeCall(workspace === null ? new GatewayStore(config ?? serverConfigPath(homedir4()), stateHome) : new ServiceCoordination(workspace, stateHome), "token-link", true), ["link"]);
|
|
8429
8935
|
return text(value.link, 4096, true);
|
|
8430
8936
|
} catch {
|
|
8431
8937
|
return;
|
|
8432
8938
|
}
|
|
8433
8939
|
}
|
|
8940
|
+
var ownerControl = {
|
|
8941
|
+
async ownerStatus(workspace, config) {
|
|
8942
|
+
try {
|
|
8943
|
+
const observed = await serviceStatus(workspace === null ? new GatewayStore(config ?? serverConfigPath(homedir4())) : new ServiceCoordination(workspace));
|
|
8944
|
+
return observed && typeof observed === "object" && "status" in observed ? String(observed.status) : "unavailable";
|
|
8945
|
+
} catch {
|
|
8946
|
+
return "unavailable";
|
|
8947
|
+
}
|
|
8948
|
+
},
|
|
8949
|
+
async stopOwner(workspace, config) {
|
|
8950
|
+
try {
|
|
8951
|
+
const c = workspace === null ? new GatewayStore(config ?? serverConfigPath(homedir4())) : new ServiceCoordination(workspace);
|
|
8952
|
+
const observed = await serviceStatus(c);
|
|
8953
|
+
const status = observed && typeof observed === "object" && "status" in observed ? String(observed.status) : "unavailable";
|
|
8954
|
+
if (status === "absent" || status === "stopped")
|
|
8955
|
+
return "stopped";
|
|
8956
|
+
return status === "alive" ? (await stopService(c)).status : status;
|
|
8957
|
+
} catch {
|
|
8958
|
+
return "unavailable";
|
|
8959
|
+
}
|
|
8960
|
+
}
|
|
8961
|
+
};
|
|
8434
8962
|
class ProjectOpenRefusal extends ContractError {
|
|
8435
8963
|
noOwner;
|
|
8436
8964
|
constructor(noOwner, code2 = "unavailable") {
|
|
@@ -8466,6 +8994,7 @@ import { createHash as createHash6, randomUUID as randomUUID8 } from "node:crypt
|
|
|
8466
8994
|
import {
|
|
8467
8995
|
createServer
|
|
8468
8996
|
} from "node:http";
|
|
8997
|
+
import { gzipSync, constants as zlib } from "node:zlib";
|
|
8469
8998
|
import { WebSocketServer } from "ws";
|
|
8470
8999
|
|
|
8471
9000
|
// src/service/workbench-send-refusal.ts
|
|
@@ -8486,12 +9015,24 @@ function fixedHeaders(response) {
|
|
|
8486
9015
|
response.setHeader("X-Frame-Options", "DENY");
|
|
8487
9016
|
response.setHeader("Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self'; style-src-elem 'self'; style-src-attr 'unsafe-inline'; font-src 'self'; img-src 'self'; connect-src 'self'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'");
|
|
8488
9017
|
}
|
|
8489
|
-
function send(response, status, value) {
|
|
9018
|
+
function send(response, status, value, gzip = false) {
|
|
8490
9019
|
if (response.destroyed || response.writableEnded)
|
|
8491
9020
|
return;
|
|
8492
9021
|
response.statusCode = status;
|
|
8493
9022
|
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
8494
|
-
|
|
9023
|
+
const body = canonicalJson(value, LIMITS.controlBytes);
|
|
9024
|
+
if (gzip && body.length >= 1024) {
|
|
9025
|
+
response.setHeader("Content-Encoding", "gzip");
|
|
9026
|
+
response.end(gzipSync(body, { level: zlib.Z_BEST_SPEED }));
|
|
9027
|
+
} else
|
|
9028
|
+
response.end(body);
|
|
9029
|
+
}
|
|
9030
|
+
function acceptsGzip(header) {
|
|
9031
|
+
return (header ?? "").split(",").some((entry) => {
|
|
9032
|
+
const [name, ...params] = entry.split(";").map((p) => p.trim());
|
|
9033
|
+
const q = params.find((p) => p.toLowerCase().startsWith("q="));
|
|
9034
|
+
return name.toLowerCase() === "gzip" && (!q || Number(q.slice(2)) > 0);
|
|
9035
|
+
});
|
|
8495
9036
|
}
|
|
8496
9037
|
function unavailable() {
|
|
8497
9038
|
throw new ContractError("unavailable");
|
|
@@ -8627,14 +9168,14 @@ Content-Length: 0\r
|
|
|
8627
9168
|
async listen() {
|
|
8628
9169
|
if (this.origin || this.stopped)
|
|
8629
9170
|
throw new ContractError("conflict");
|
|
8630
|
-
await new Promise((
|
|
9171
|
+
await new Promise((resolve7, reject) => {
|
|
8631
9172
|
const failed = () => {
|
|
8632
9173
|
this.server.off("listening", ready);
|
|
8633
9174
|
reject(new ContractError("unavailable"));
|
|
8634
9175
|
};
|
|
8635
9176
|
const ready = () => {
|
|
8636
9177
|
this.server.off("error", failed);
|
|
8637
|
-
|
|
9178
|
+
resolve7();
|
|
8638
9179
|
};
|
|
8639
9180
|
this.server.once("error", failed);
|
|
8640
9181
|
this.server.once("listening", ready);
|
|
@@ -8664,13 +9205,13 @@ Content-Length: 0\r
|
|
|
8664
9205
|
close() {
|
|
8665
9206
|
if (this.closePromise)
|
|
8666
9207
|
return this.closePromise;
|
|
8667
|
-
let
|
|
9208
|
+
let resolve7;
|
|
8668
9209
|
let reject;
|
|
8669
9210
|
this.closePromise = new Promise((yes, no) => {
|
|
8670
|
-
|
|
9211
|
+
resolve7 = yes;
|
|
8671
9212
|
reject = no;
|
|
8672
9213
|
});
|
|
8673
|
-
this.closeTransport().then(
|
|
9214
|
+
this.closeTransport().then(resolve7, () => reject(new ContractError("unavailable")));
|
|
8674
9215
|
return this.closePromise;
|
|
8675
9216
|
}
|
|
8676
9217
|
async closeTransport() {
|
|
@@ -8682,18 +9223,18 @@ Content-Length: 0\r
|
|
|
8682
9223
|
request2.abort();
|
|
8683
9224
|
for (const attachment of [...this.attachments.values()])
|
|
8684
9225
|
this.detach(attachment);
|
|
8685
|
-
const closedSockets = [...this.sockets].map((socket) => new Promise((
|
|
9226
|
+
const closedSockets = [...this.sockets].map((socket) => new Promise((resolve7) => {
|
|
8686
9227
|
if (socket.closed)
|
|
8687
|
-
|
|
9228
|
+
resolve7();
|
|
8688
9229
|
else {
|
|
8689
|
-
socket.once("close", () =>
|
|
9230
|
+
socket.once("close", () => resolve7());
|
|
8690
9231
|
socket.destroy();
|
|
8691
9232
|
}
|
|
8692
9233
|
}));
|
|
8693
|
-
const listener = new Promise((
|
|
8694
|
-
this.server.close((error) => error ? reject(new ContractError("unavailable")) :
|
|
9234
|
+
const listener = new Promise((resolve7, reject) => {
|
|
9235
|
+
this.server.close((error) => error ? reject(new ContractError("unavailable")) : resolve7());
|
|
8695
9236
|
});
|
|
8696
|
-
const upgrades = new Promise((
|
|
9237
|
+
const upgrades = new Promise((resolve7) => this.wss.close(() => resolve7()));
|
|
8697
9238
|
await Promise.all([listener, upgrades, ...closedSockets]);
|
|
8698
9239
|
}
|
|
8699
9240
|
attachment(caller) {
|
|
@@ -8877,7 +9418,10 @@ Content-Length: 0\r
|
|
|
8877
9418
|
this.attachment(caller);
|
|
8878
9419
|
const decoded2 = decodeHttpOutput(route.operation, value);
|
|
8879
9420
|
const status = value && typeof value === "object" && "kind" in value && value.kind === "error" && "error" in value ? errorStatus(decodePublicError(value.error).code) : 200;
|
|
8880
|
-
|
|
9421
|
+
const compressible = route.operation === "workbenchRead";
|
|
9422
|
+
if (compressible)
|
|
9423
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
9424
|
+
send(res, status, decoded2, compressible && acceptsGzip(boundary.headers.get("accept-encoding")));
|
|
8881
9425
|
} catch (e) {
|
|
8882
9426
|
if (createdAttachment)
|
|
8883
9427
|
this.detach(createdAttachment);
|
|
@@ -9101,7 +9645,7 @@ Content-Length: 0\r
|
|
|
9101
9645
|
if (signal.aborted || res.destroyed)
|
|
9102
9646
|
unavailable();
|
|
9103
9647
|
const chunk = data.subarray(offset, offset + LIMITS.detailPageBytes);
|
|
9104
|
-
await new Promise((
|
|
9648
|
+
await new Promise((resolve7, reject) => {
|
|
9105
9649
|
const stop = () => {
|
|
9106
9650
|
res.destroy();
|
|
9107
9651
|
reject(new ContractError("unavailable"));
|
|
@@ -9109,7 +9653,7 @@ Content-Length: 0\r
|
|
|
9109
9653
|
signal.addEventListener("abort", stop, { once: true });
|
|
9110
9654
|
res.write(chunk, (error) => {
|
|
9111
9655
|
signal.removeEventListener("abort", stop);
|
|
9112
|
-
error ? reject(new ContractError("unavailable")) :
|
|
9656
|
+
error ? reject(new ContractError("unavailable")) : resolve7();
|
|
9113
9657
|
});
|
|
9114
9658
|
});
|
|
9115
9659
|
activity();
|
|
@@ -9331,16 +9875,16 @@ Content-Length: 0\r
|
|
|
9331
9875
|
// src/service/workbench-files.ts
|
|
9332
9876
|
import {
|
|
9333
9877
|
closeSync as closeSync4,
|
|
9334
|
-
constants as
|
|
9878
|
+
constants as constants4,
|
|
9335
9879
|
fstatSync as fstatSync3,
|
|
9336
|
-
lstatSync as
|
|
9880
|
+
lstatSync as lstatSync9,
|
|
9337
9881
|
opendirSync as opendirSync5,
|
|
9338
9882
|
openSync as openSync4,
|
|
9339
9883
|
readSync as readSync3,
|
|
9340
|
-
realpathSync as
|
|
9884
|
+
realpathSync as realpathSync6
|
|
9341
9885
|
} from "node:fs";
|
|
9342
|
-
import { homedir as
|
|
9343
|
-
import { dirname as
|
|
9886
|
+
import { homedir as homedir5 } from "node:os";
|
|
9887
|
+
import { dirname as dirname11, isAbsolute as isAbsolute7, join as join16, relative as relative5, resolve as resolve7, sep as sep5 } from "node:path";
|
|
9344
9888
|
|
|
9345
9889
|
// src/worker-redaction.mjs
|
|
9346
9890
|
var marker = "[redacted]";
|
|
@@ -9799,7 +10343,7 @@ class DetailSanitizer {
|
|
|
9799
10343
|
// src/service/workbench-files.ts
|
|
9800
10344
|
function inside(root, path) {
|
|
9801
10345
|
const r = relative5(root, path);
|
|
9802
|
-
return r === "" || !
|
|
10346
|
+
return r === "" || !isAbsolute7(r) && r !== ".." && !r.startsWith(`..${sep5}`);
|
|
9803
10347
|
}
|
|
9804
10348
|
var sensitive2 = (name) => /^\.(?:git|ssh|aws|azure|gnupg|kube|npmrc|pypirc|netrc)$/i.test(name) || /^\.env(?:\.|$)/i.test(name) || /^(?:auth|models|credentials|service-account)\.json$/i.test(name) || /^id_(?:rsa|dsa|ecdsa|ed25519)(?:\.|$)/i.test(name) || /\.(?:pem|key|p12|pfx)$/i.test(name);
|
|
9805
10349
|
|
|
@@ -9809,39 +10353,39 @@ class WorkbenchFiles {
|
|
|
9809
10353
|
excluded;
|
|
9810
10354
|
constructor(root, excluded, secrets) {
|
|
9811
10355
|
this.secrets = secrets;
|
|
9812
|
-
this.root =
|
|
10356
|
+
this.root = realpathSync6(root);
|
|
9813
10357
|
this.excluded = excluded.flatMap((path) => [
|
|
9814
|
-
|
|
10358
|
+
resolve7(path),
|
|
9815
10359
|
canonicalAmbientRoot(path)
|
|
9816
10360
|
]);
|
|
9817
10361
|
}
|
|
9818
10362
|
allowed(path) {
|
|
9819
10363
|
if (path.split(/[\\/]/).some(sensitive2) || this.excluded.some((root) => inside(root, path)))
|
|
9820
10364
|
throw new ContractError("forbidden");
|
|
9821
|
-
const stat =
|
|
10365
|
+
const stat = lstatSync9(path);
|
|
9822
10366
|
if (stat.isSymbolicLink() || stat.isFile() && stat.nlink !== 1 || process.platform !== "win32" && process.getuid && stat.uid !== process.getuid())
|
|
9823
10367
|
throw new ContractError("forbidden");
|
|
9824
10368
|
}
|
|
9825
10369
|
directory(path) {
|
|
9826
|
-
if (!
|
|
10370
|
+
if (!isAbsolute7(path) || !safeDirectory(path))
|
|
9827
10371
|
throw new ContractError("invalid");
|
|
9828
|
-
const canonical =
|
|
10372
|
+
const canonical = realpathSync6(path);
|
|
9829
10373
|
this.allowed(canonical);
|
|
9830
10374
|
return canonical;
|
|
9831
10375
|
}
|
|
9832
10376
|
selected(path) {
|
|
9833
|
-
if (
|
|
10377
|
+
if (isAbsolute7(path) || path.split(/[\\/]/).some((part) => part === ".." || sensitive2(part)))
|
|
9834
10378
|
throw new ContractError("forbidden");
|
|
9835
|
-
const target =
|
|
9836
|
-
if (!inside(this.root, target) || !safeDirectory(
|
|
10379
|
+
const target = resolve7(this.root, path);
|
|
10380
|
+
if (!inside(this.root, target) || !safeDirectory(dirname11(target)))
|
|
9837
10381
|
throw new ContractError("forbidden");
|
|
9838
10382
|
this.allowed(target);
|
|
9839
|
-
if (
|
|
10383
|
+
if (lstatSync9(target).isSymbolicLink())
|
|
9840
10384
|
throw new ContractError("forbidden");
|
|
9841
10385
|
return target;
|
|
9842
10386
|
}
|
|
9843
10387
|
browsed(path) {
|
|
9844
|
-
const full = /^~(?:[\\/]|$)/.test(path) ?
|
|
10388
|
+
const full = /^~(?:[\\/]|$)/.test(path) ? join16(homedir5(), path.slice(1)) : path || this.root;
|
|
9845
10389
|
try {
|
|
9846
10390
|
return this.directory(full);
|
|
9847
10391
|
} catch (error) {
|
|
@@ -9864,13 +10408,13 @@ class WorkbenchFiles {
|
|
|
9864
10408
|
}
|
|
9865
10409
|
if (sensitive2(entry.name) || entry.isSymbolicLink() || !entry.isFile() && !entry.isDirectory() || directories && !entry.isDirectory())
|
|
9866
10410
|
continue;
|
|
9867
|
-
const absolute2 =
|
|
10411
|
+
const absolute2 = join16(selected, entry.name);
|
|
9868
10412
|
try {
|
|
9869
10413
|
this.allowed(absolute2);
|
|
9870
10414
|
} catch {
|
|
9871
10415
|
continue;
|
|
9872
10416
|
}
|
|
9873
|
-
const wire = directories ? absolute2 : relative5(this.root, absolute2).split(
|
|
10417
|
+
const wire = directories ? absolute2 : relative5(this.root, absolute2).split(sep5).join("/");
|
|
9874
10418
|
if (sanitizeText(wire, this.secrets()) !== wire)
|
|
9875
10419
|
continue;
|
|
9876
10420
|
if (entries2.length >= 200) {
|
|
@@ -9887,20 +10431,20 @@ class WorkbenchFiles {
|
|
|
9887
10431
|
dir.closeSync();
|
|
9888
10432
|
}
|
|
9889
10433
|
entries2.sort((a, b) => Number(b.directory) - Number(a.directory) || a.name.localeCompare(b.name));
|
|
9890
|
-
const parent =
|
|
10434
|
+
const parent = dirname11(selected);
|
|
9891
10435
|
return {
|
|
9892
10436
|
kind: directories ? "directories" : "files",
|
|
9893
10437
|
path: directories ? selected : path,
|
|
9894
|
-
parent: directories ? parent === selected ? null : parent : selected === this.root ? null : relative5(this.root, parent).split(
|
|
10438
|
+
parent: directories ? parent === selected ? null : parent : selected === this.root ? null : relative5(this.root, parent).split(sep5).join("/"),
|
|
9895
10439
|
entries: entries2,
|
|
9896
10440
|
limited
|
|
9897
10441
|
};
|
|
9898
10442
|
}
|
|
9899
10443
|
read(path) {
|
|
9900
|
-
const selected = this.selected(path), before =
|
|
10444
|
+
const selected = this.selected(path), before = lstatSync9(selected);
|
|
9901
10445
|
if (!before.isFile())
|
|
9902
10446
|
throw new ContractError("forbidden");
|
|
9903
|
-
const fd = openSync4(selected,
|
|
10447
|
+
const fd = openSync4(selected, constants4.O_RDONLY | (constants4.O_NOFOLLOW ?? 0));
|
|
9904
10448
|
try {
|
|
9905
10449
|
const opened = fstatSync3(fd);
|
|
9906
10450
|
if (!opened.isFile() || opened.nlink !== 1 || before.dev !== opened.dev || before.ino !== opened.ino)
|
|
@@ -9913,7 +10457,7 @@ class WorkbenchFiles {
|
|
|
9913
10457
|
break;
|
|
9914
10458
|
at += n;
|
|
9915
10459
|
}
|
|
9916
|
-
const after = fstatSync3(fd), current =
|
|
10460
|
+
const after = fstatSync3(fd), current = lstatSync9(selected);
|
|
9917
10461
|
if (after.size !== opened.size || after.mtimeMs !== opened.mtimeMs || after.nlink !== 1 || current.isSymbolicLink() || current.nlink !== 1 || current.ino !== opened.ino || current.dev !== opened.dev)
|
|
9918
10462
|
throw new ContractError("stale");
|
|
9919
10463
|
const decoded2 = new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, at), { stream: at < opened.size });
|
|
@@ -9947,7 +10491,7 @@ async function createGateway(value, hostUrl, source) {
|
|
|
9947
10491
|
const input = decodeGatewayInput(value), store = new GatewayStore(input.serverConfig, input.stateHome);
|
|
9948
10492
|
const web = loadWebAssets(source?.webDirectory);
|
|
9949
10493
|
const build2 = {
|
|
9950
|
-
contractHash: "
|
|
10494
|
+
contractHash: "d0edc9d93f682dd7c637bd28cdb756b823ad8eafeabebc63479bee1bac5b2b55",
|
|
9951
10495
|
assets: {
|
|
9952
10496
|
sourceHash: web.manifest.sourceHash,
|
|
9953
10497
|
contractHash: web.manifest.contractHash,
|
|
@@ -9999,11 +10543,11 @@ Content-Length: 0\r
|
|
|
9999
10543
|
\r
|
|
10000
10544
|
`));
|
|
10001
10545
|
server.on("checkContinue", (_req, res) => reply(res, 400, { kind: "error", error: publicError("invalid") }));
|
|
10002
|
-
await new Promise((
|
|
10546
|
+
await new Promise((resolve8, reject) => {
|
|
10003
10547
|
server.once("error", reject);
|
|
10004
10548
|
server.listen(input.policy.port, input.policy.host, () => {
|
|
10005
10549
|
server.off("error", reject);
|
|
10006
|
-
|
|
10550
|
+
resolve8();
|
|
10007
10551
|
});
|
|
10008
10552
|
});
|
|
10009
10553
|
const address = server.address();
|
|
@@ -10049,12 +10593,90 @@ Content-Length: 0\r
|
|
|
10049
10593
|
server.close();
|
|
10050
10594
|
throw error;
|
|
10051
10595
|
}
|
|
10052
|
-
const files = new WorkbenchFiles(
|
|
10596
|
+
const files = new WorkbenchFiles(homedir6(), [store.files.namespace, input.agentDir], () => acquired.access.redactionLiterals());
|
|
10053
10597
|
const assert = () => {
|
|
10054
10598
|
store.assert();
|
|
10055
10599
|
if (!accepting)
|
|
10056
10600
|
throw new ContractError("quarantine");
|
|
10057
10601
|
};
|
|
10602
|
+
const root = dirname12(dirname12(fileURLToPath3(hostUrl))), version = installedVersion(root), record2 = new MaintenanceRecord(store);
|
|
10603
|
+
const managed = (() => {
|
|
10604
|
+
const s = supervisingUnit(process.env, (selection) => hostSystem(root, ownerControl, selection));
|
|
10605
|
+
return s && serviceConfigPath(s) === input.serverConfig ? s : undefined;
|
|
10606
|
+
})();
|
|
10607
|
+
let route, requesting = false;
|
|
10608
|
+
async function associatedWork() {
|
|
10609
|
+
const counts = await Promise.all(store.associatedProjects().map(async (p) => {
|
|
10610
|
+
try {
|
|
10611
|
+
const observed = await serviceStatus(new ServiceCoordination(p.path, input.stateHome));
|
|
10612
|
+
return observed && typeof observed === "object" && "status" in observed && observed.status === "alive" && "work" in observed && typeof observed.work === "number" ? observed.work : 0;
|
|
10613
|
+
} catch {
|
|
10614
|
+
return 0;
|
|
10615
|
+
}
|
|
10616
|
+
}));
|
|
10617
|
+
return counts.reduce((sum, count) => sum + count, 0);
|
|
10618
|
+
}
|
|
10619
|
+
function latestJob() {
|
|
10620
|
+
const job2 = record2.read();
|
|
10621
|
+
return job2?.state === "running" && managed && !jobRunning(managed) ? { ...job2, state: "failed", reason: "interrupted" } : job2;
|
|
10622
|
+
}
|
|
10623
|
+
async function serviceEntry() {
|
|
10624
|
+
route ??= upgradeRoute(root);
|
|
10625
|
+
return {
|
|
10626
|
+
kind: "service",
|
|
10627
|
+
version,
|
|
10628
|
+
unit: managed ? { name: managed.unit, scope: managed.scope ?? "user" } : null,
|
|
10629
|
+
upgrade: await route,
|
|
10630
|
+
work: await associatedWork(),
|
|
10631
|
+
job: latestJob()
|
|
10632
|
+
};
|
|
10633
|
+
}
|
|
10634
|
+
async function requestJob(value2, current) {
|
|
10635
|
+
const o = closed(value2, ["action", "from", "to", "work", "epoch"]);
|
|
10636
|
+
const action = oneOf(o.action, MAINTENANCE_ACTIONS), to = o.to === null ? null : decodeVersion(o.to);
|
|
10637
|
+
if (action === "upgrade" !== (to !== null))
|
|
10638
|
+
throw new ContractError("invalid");
|
|
10639
|
+
if (decodeId(o.epoch) !== auth.epoch || decodeVersion(o.from) !== version)
|
|
10640
|
+
throw new ContractError("stale");
|
|
10641
|
+
if (!managed)
|
|
10642
|
+
throw new ContractError("unsupported-context");
|
|
10643
|
+
route ??= upgradeRoute(root);
|
|
10644
|
+
if (action === "upgrade" && await route !== "npm")
|
|
10645
|
+
throw new ContractError("unsupported-context");
|
|
10646
|
+
if (requesting || jobRunning(managed))
|
|
10647
|
+
throw new ContractError("conflict");
|
|
10648
|
+
requesting = true;
|
|
10649
|
+
try {
|
|
10650
|
+
if (await associatedWork() !== integer(o.work, 0, 1e6))
|
|
10651
|
+
throw new ContractError("stale");
|
|
10652
|
+
current();
|
|
10653
|
+
assert();
|
|
10654
|
+
const job2 = {
|
|
10655
|
+
id: randomUUID9(),
|
|
10656
|
+
action,
|
|
10657
|
+
from: version,
|
|
10658
|
+
to,
|
|
10659
|
+
state: "running",
|
|
10660
|
+
reason: null,
|
|
10661
|
+
version: null,
|
|
10662
|
+
at: Date.now(),
|
|
10663
|
+
endedAt: null
|
|
10664
|
+
};
|
|
10665
|
+
record2.write(job2);
|
|
10666
|
+
if (!launchJob(managed, job2)) {
|
|
10667
|
+
record2.finish(job2.id, "failed", "refused", version);
|
|
10668
|
+
throw new ContractError("unavailable");
|
|
10669
|
+
}
|
|
10670
|
+
return job2;
|
|
10671
|
+
} finally {
|
|
10672
|
+
requesting = false;
|
|
10673
|
+
}
|
|
10674
|
+
}
|
|
10675
|
+
function browserBuild(boundary) {
|
|
10676
|
+
const clientBuild = decodeClientBuild(parseWireJson(boundary.headers.get("x-cadence-build") ?? "null", 1024));
|
|
10677
|
+
if (clientBuild.kind !== "browser" || !compatibleBuild(build2, clientBuild))
|
|
10678
|
+
throw new ContractError("unsupported-context");
|
|
10679
|
+
}
|
|
10058
10680
|
async function open(path, id, epoch) {
|
|
10059
10681
|
assert();
|
|
10060
10682
|
if (epoch !== auth.epoch)
|
|
@@ -10137,14 +10759,14 @@ Content-Length: 0\r
|
|
|
10137
10759
|
const secret = link.searchParams.get("token");
|
|
10138
10760
|
if (!secret)
|
|
10139
10761
|
throw new ContractError("unavailable");
|
|
10140
|
-
const cookie = await new Promise((
|
|
10762
|
+
const cookie = await new Promise((resolve8, reject) => {
|
|
10141
10763
|
const req = request2(`${local.endpoint}/?token=${encodeURIComponent(secret)}`, { agent: false }, (res) => {
|
|
10142
10764
|
const cookie2 = res.headers["set-cookie"];
|
|
10143
10765
|
res.resume();
|
|
10144
10766
|
if (res.statusCode !== 303 || cookie2?.length !== 1)
|
|
10145
10767
|
reject(new ContractError("unavailable"));
|
|
10146
10768
|
else
|
|
10147
|
-
|
|
10769
|
+
resolve8(cookie2[0].split(";")[0]);
|
|
10148
10770
|
});
|
|
10149
10771
|
req.setTimeout(2000, () => req.destroy(new Error));
|
|
10150
10772
|
req.on("error", () => reject(new ContractError("unavailable")));
|
|
@@ -10310,20 +10932,20 @@ Content-Length: 0\r
|
|
|
10310
10932
|
const project = target(raw);
|
|
10311
10933
|
const caller = auth.authenticate(boundary, randomUUID9(), !project && req.method === "POST");
|
|
10312
10934
|
if (project) {
|
|
10313
|
-
const
|
|
10314
|
-
if (!
|
|
10935
|
+
const route2 = readRoute(project.tail, req.method ?? "");
|
|
10936
|
+
if (!route2)
|
|
10315
10937
|
throw new ContractError("invalid");
|
|
10316
10938
|
const attachment = boundary.headers.get("x-cadence-attachment");
|
|
10317
10939
|
if (attachment && attachments.get(attachment) !== project.id || req.method === "POST" && !attachment)
|
|
10318
10940
|
throw new ContractError("stale");
|
|
10319
|
-
if (
|
|
10941
|
+
if (route2.operation === "session" && !attachment && attachments.size >= 2048)
|
|
10320
10942
|
throw new ContractError("capacity");
|
|
10321
10943
|
if (req.method === "POST" && boundary.headers.get("origin") !== boundary.origin)
|
|
10322
10944
|
throw new ContractError("forbidden");
|
|
10323
10945
|
if (req.headers["content-encoding"] || req.headers["transfer-encoding"] || Number(req.headers["content-length"] ?? 0) > LIMITS.controlBytes)
|
|
10324
10946
|
throw new ContractError("oversize");
|
|
10325
10947
|
let commandBody;
|
|
10326
|
-
if (
|
|
10948
|
+
if (route2.operation === "workbenchCommand") {
|
|
10327
10949
|
const command = decodeWorkbenchCommand(await body(req, LIMITS.controlBytes));
|
|
10328
10950
|
if (command.kind === "open-project")
|
|
10329
10951
|
throw new ContractError("forbidden");
|
|
@@ -10337,6 +10959,7 @@ Content-Length: 0\r
|
|
|
10337
10959
|
Origin: b.endpoint
|
|
10338
10960
|
};
|
|
10339
10961
|
for (const key of [
|
|
10962
|
+
"accept-encoding",
|
|
10340
10963
|
"content-type",
|
|
10341
10964
|
"content-length",
|
|
10342
10965
|
"x-cadence-build",
|
|
@@ -10349,7 +10972,7 @@ Content-Length: 0\r
|
|
|
10349
10972
|
}
|
|
10350
10973
|
if (commandBody !== undefined)
|
|
10351
10974
|
headers["content-length"] = String(Buffer.byteLength(commandBody));
|
|
10352
|
-
await new Promise((
|
|
10975
|
+
await new Promise((resolve8) => {
|
|
10353
10976
|
const upstream = request2(`${b.endpoint}${project.tail}`, { method: req.method, headers, agent: false }, (incoming) => {
|
|
10354
10977
|
try {
|
|
10355
10978
|
auth.current(caller);
|
|
@@ -10357,7 +10980,7 @@ Content-Length: 0\r
|
|
|
10357
10980
|
close();
|
|
10358
10981
|
return;
|
|
10359
10982
|
}
|
|
10360
|
-
if (
|
|
10983
|
+
if (route2.operation === "session" && incoming.statusCode === 200) {
|
|
10361
10984
|
try {
|
|
10362
10985
|
attachments.set(decodeId(incoming.headers["x-cadence-attachment"]), project.id);
|
|
10363
10986
|
} catch {
|
|
@@ -10365,12 +10988,14 @@ Content-Length: 0\r
|
|
|
10365
10988
|
return;
|
|
10366
10989
|
}
|
|
10367
10990
|
}
|
|
10368
|
-
if (
|
|
10991
|
+
if (route2.operation === "detach" && incoming.statusCode === 200 && attachment)
|
|
10369
10992
|
attachments.delete(attachment);
|
|
10370
10993
|
res.statusCode = incoming.statusCode ?? 502;
|
|
10371
10994
|
for (const key of [
|
|
10372
10995
|
"content-type",
|
|
10996
|
+
"content-encoding",
|
|
10373
10997
|
"content-disposition",
|
|
10998
|
+
"vary",
|
|
10374
10999
|
"x-cadence-attachment"
|
|
10375
11000
|
]) {
|
|
10376
11001
|
const value2 = incoming.headers[key];
|
|
@@ -10385,7 +11010,7 @@ Content-Length: 0\r
|
|
|
10385
11010
|
relays.delete(close);
|
|
10386
11011
|
res.off("close", close);
|
|
10387
11012
|
res.off("finish", done);
|
|
10388
|
-
|
|
11013
|
+
resolve8();
|
|
10389
11014
|
}
|
|
10390
11015
|
function close() {
|
|
10391
11016
|
upstream.destroy();
|
|
@@ -10417,7 +11042,7 @@ Content-Length: 0\r
|
|
|
10417
11042
|
kind: "workbench",
|
|
10418
11043
|
epoch: auth.epoch,
|
|
10419
11044
|
csrf: auth.csrf,
|
|
10420
|
-
home:
|
|
11045
|
+
home: homedir6(),
|
|
10421
11046
|
projects: store.projects(),
|
|
10422
11047
|
recoveries: new RecoveryLog(store.files).notices(),
|
|
10423
11048
|
build: build2
|
|
@@ -10428,10 +11053,27 @@ Content-Length: 0\r
|
|
|
10428
11053
|
reply(res, 200, files.list(text(url.searchParams.get("path"), 4096, true), true));
|
|
10429
11054
|
return;
|
|
10430
11055
|
}
|
|
11056
|
+
if (raw === "/workbench/v1/service" && req.method === "GET") {
|
|
11057
|
+
reply(res, 200, await serviceEntry());
|
|
11058
|
+
return;
|
|
11059
|
+
}
|
|
11060
|
+
if (raw === "/workbench/v1/service/release" && req.method === "GET") {
|
|
11061
|
+
const latest = await release();
|
|
11062
|
+
if (!latest)
|
|
11063
|
+
throw new ContractError("unavailable");
|
|
11064
|
+
reply(res, 200, { kind: "release", ...latest });
|
|
11065
|
+
return;
|
|
11066
|
+
}
|
|
11067
|
+
if (raw === "/workbench/v1/service" && req.method === "POST") {
|
|
11068
|
+
browserBuild(boundary);
|
|
11069
|
+
const value2 = await body(req);
|
|
11070
|
+
auth.current(caller);
|
|
11071
|
+
const job2 = await requestJob(value2, () => auth.current(caller));
|
|
11072
|
+
reply(res, 200, { kind: "service-job", job: job2 });
|
|
11073
|
+
return;
|
|
11074
|
+
}
|
|
10431
11075
|
if (raw === "/workbench/v1/open" && req.method === "POST") {
|
|
10432
|
-
|
|
10433
|
-
if (clientBuild.kind !== "browser" || !compatibleBuild(build2, clientBuild))
|
|
10434
|
-
throw new ContractError("unsupported-context");
|
|
11076
|
+
browserBuild(boundary);
|
|
10435
11077
|
const o = closed(await body(req), ["commandId", "path", "epoch"]);
|
|
10436
11078
|
const id = decodeId(o.commandId), path = text(o.path, 4096, true), epoch = decodeId(o.epoch);
|
|
10437
11079
|
auth.current(caller);
|
|
@@ -10531,14 +11173,14 @@ Content-Length: 0\r
|
|
|
10531
11173
|
}
|
|
10532
11174
|
|
|
10533
11175
|
// src/service/host.ts
|
|
10534
|
-
import { spawn as
|
|
10535
|
-
import { lstatSync as
|
|
10536
|
-
import { join as
|
|
10537
|
-
import { fileURLToPath as
|
|
11176
|
+
import { spawn as spawn5 } from "node:child_process";
|
|
11177
|
+
import { lstatSync as lstatSync22, opendirSync as opendirSync15 } from "node:fs";
|
|
11178
|
+
import { join as join34 } from "node:path";
|
|
11179
|
+
import { fileURLToPath as fileURLToPath7 } from "node:url";
|
|
10538
11180
|
|
|
10539
11181
|
// src/agent-registry.ts
|
|
10540
11182
|
import { createHash as createHash7 } from "node:crypto";
|
|
10541
|
-
import { fileURLToPath as
|
|
11183
|
+
import { fileURLToPath as fileURLToPath4 } from "node:url";
|
|
10542
11184
|
var AGENT_FILES = [
|
|
10543
11185
|
"design-explorer.md",
|
|
10544
11186
|
"implementation-worker.md",
|
|
@@ -10546,7 +11188,7 @@ var AGENT_FILES = [
|
|
|
10546
11188
|
];
|
|
10547
11189
|
function loadAgentDefinitions() {
|
|
10548
11190
|
return AGENT_FILES.map((fileName) => {
|
|
10549
|
-
const path =
|
|
11191
|
+
const path = fileURLToPath4(new URL(`../agents/${fileName}`, import.meta.url));
|
|
10550
11192
|
const data = readBounded(path, 32768, true);
|
|
10551
11193
|
if (!data)
|
|
10552
11194
|
throw new Error("Missing package Agent");
|
|
@@ -10562,18 +11204,18 @@ function loadAgentDefinitions() {
|
|
|
10562
11204
|
}
|
|
10563
11205
|
|
|
10564
11206
|
// src/cadence-preferences.ts
|
|
10565
|
-
import { realpathSync as
|
|
10566
|
-
import { basename as basename2, dirname as
|
|
11207
|
+
import { realpathSync as realpathSync7 } from "node:fs";
|
|
11208
|
+
import { basename as basename2, dirname as dirname13, join as join17, resolve as resolve8 } from "node:path";
|
|
10567
11209
|
function agentNamespace(agentDir) {
|
|
10568
|
-
let at =
|
|
11210
|
+
let at = resolve8(agentDir);
|
|
10569
11211
|
const missing2 = [];
|
|
10570
11212
|
for (;; ) {
|
|
10571
11213
|
try {
|
|
10572
|
-
return
|
|
11214
|
+
return join17(realpathSync7(at), ...missing2.reverse());
|
|
10573
11215
|
} catch {
|
|
10574
|
-
const parent =
|
|
11216
|
+
const parent = dirname13(at);
|
|
10575
11217
|
if (parent === at)
|
|
10576
|
-
return
|
|
11218
|
+
return resolve8(agentDir);
|
|
10577
11219
|
missing2.push(basename2(at));
|
|
10578
11220
|
at = parent;
|
|
10579
11221
|
}
|
|
@@ -10585,7 +11227,7 @@ class CadencePreferences {
|
|
|
10585
11227
|
files;
|
|
10586
11228
|
constructor(agentDir) {
|
|
10587
11229
|
const namespace = agentNamespace(agentDir);
|
|
10588
|
-
this.path =
|
|
11230
|
+
this.path = join17(namespace, "cadence", "settings.json");
|
|
10589
11231
|
this.files = new PrivateFiles(namespace, false);
|
|
10590
11232
|
}
|
|
10591
11233
|
read() {
|
|
@@ -10633,12 +11275,12 @@ class CadencePreferences {
|
|
|
10633
11275
|
function scopedSnapshot(source, scope) {
|
|
10634
11276
|
if (scope.kind === "task" || source.scope.kind === "child")
|
|
10635
11277
|
return;
|
|
10636
|
-
if (!source.runs.some((
|
|
11278
|
+
if (!source.runs.some((run2) => scope.kind === "run" ? run2.runId === scope.id && run2.childRunId === null : run2.childRunId === scope.id))
|
|
10637
11279
|
return;
|
|
10638
11280
|
return decodeSnapshot({
|
|
10639
11281
|
...source,
|
|
10640
11282
|
scope,
|
|
10641
|
-
runs: source.runs.filter((
|
|
11283
|
+
runs: source.runs.filter((run2) => scope.kind === "run" ? run2.runId === scope.id : run2.childRunId === scope.id),
|
|
10642
11284
|
horizon: {
|
|
10643
11285
|
...source.horizon,
|
|
10644
11286
|
historicalRuns: { ...source.horizon.historicalRuns, scope }
|
|
@@ -10878,40 +11520,40 @@ class HistoryLive {
|
|
|
10878
11520
|
|
|
10879
11521
|
// src/history/navigation.ts
|
|
10880
11522
|
import { createHash as createHash11 } from "node:crypto";
|
|
10881
|
-
import { lstatSync as
|
|
10882
|
-
import { dirname as
|
|
11523
|
+
import { lstatSync as lstatSync13 } from "node:fs";
|
|
11524
|
+
import { dirname as dirname16, join as join20 } from "node:path";
|
|
10883
11525
|
|
|
10884
11526
|
// src/history/index-tree.ts
|
|
10885
|
-
import { lstatSync as
|
|
10886
|
-
import { join as
|
|
11527
|
+
import { lstatSync as lstatSync11 } from "node:fs";
|
|
11528
|
+
import { join as join18 } from "node:path";
|
|
10887
11529
|
|
|
10888
11530
|
// src/history/read-io.ts
|
|
10889
11531
|
import { createHash as createHash8 } from "node:crypto";
|
|
10890
11532
|
import {
|
|
10891
11533
|
closeSync as closeSync5,
|
|
10892
|
-
constants as
|
|
11534
|
+
constants as constants5,
|
|
10893
11535
|
fstatSync as fstatSync4,
|
|
10894
|
-
lstatSync as
|
|
11536
|
+
lstatSync as lstatSync10,
|
|
10895
11537
|
openSync as openSync5,
|
|
10896
11538
|
readSync as readSync4
|
|
10897
11539
|
} from "node:fs";
|
|
10898
|
-
import { dirname as
|
|
11540
|
+
import { dirname as dirname14 } from "node:path";
|
|
10899
11541
|
function bad() {
|
|
10900
11542
|
throw new ContractError("corrupt-selected");
|
|
10901
11543
|
}
|
|
10902
11544
|
var stamp2 = (s) => [s.dev, s.ino, s.size, s.mode, s.uid, s.nlink, s.ctimeMs, s.mtimeMs].join(":");
|
|
10903
11545
|
function selected(files, path, read, walks) {
|
|
10904
|
-
if (!files.files.directory(
|
|
11546
|
+
if (!files.files.directory(dirname14(path), false, walks))
|
|
10905
11547
|
bad();
|
|
10906
|
-
const before =
|
|
11548
|
+
const before = lstatSync10(path);
|
|
10907
11549
|
if (!before.isFile() || before.nlink !== 1 || process.platform !== "win32" && (before.mode & 63 || process.getuid && before.uid !== process.getuid()))
|
|
10908
11550
|
bad();
|
|
10909
|
-
const fd = openSync5(path,
|
|
11551
|
+
const fd = openSync5(path, constants5.O_RDONLY | (constants5.O_NOFOLLOW ?? 0));
|
|
10910
11552
|
try {
|
|
10911
11553
|
if (stamp2(fstatSync4(fd)) !== stamp2(before))
|
|
10912
11554
|
bad();
|
|
10913
11555
|
const result = read(fd, before.size, stamp2(before));
|
|
10914
|
-
if (stamp2(fstatSync4(fd)) !== stamp2(before) || stamp2(
|
|
11556
|
+
if (stamp2(fstatSync4(fd)) !== stamp2(before) || stamp2(lstatSync10(path)) !== stamp2(before))
|
|
10915
11557
|
bad();
|
|
10916
11558
|
return result;
|
|
10917
11559
|
} finally {
|
|
@@ -11012,7 +11654,7 @@ class IndexTree {
|
|
|
11012
11654
|
this.files = files;
|
|
11013
11655
|
}
|
|
11014
11656
|
path(id) {
|
|
11015
|
-
return
|
|
11657
|
+
return join18(this.files.directory, "indexes", "nodes", id.slice(0, 2), `${id}.json`);
|
|
11016
11658
|
}
|
|
11017
11659
|
load(id, budget, unit) {
|
|
11018
11660
|
decodeHash(id);
|
|
@@ -11027,7 +11669,7 @@ class IndexTree {
|
|
|
11027
11669
|
if (budget) {
|
|
11028
11670
|
const path = this.path(id);
|
|
11029
11671
|
budget.reserve(0);
|
|
11030
|
-
const selected2 =
|
|
11672
|
+
const selected2 = lstatSync11(path).size;
|
|
11031
11673
|
if (selected2 > MAX_NODE)
|
|
11032
11674
|
throw new ContractError("receipt-inconclusive");
|
|
11033
11675
|
bytes = readRange(this.files, path, 0, selected2, budget);
|
|
@@ -11147,16 +11789,16 @@ class IndexTree {
|
|
|
11147
11789
|
import { createHash as createHash10, randomBytes as randomBytes4, randomUUID as randomUUID10 } from "node:crypto";
|
|
11148
11790
|
import {
|
|
11149
11791
|
closeSync as closeSync6,
|
|
11150
|
-
constants as
|
|
11792
|
+
constants as constants6,
|
|
11151
11793
|
fstatSync as fstatSync5,
|
|
11152
11794
|
fsyncSync as fsyncSync3,
|
|
11153
|
-
lstatSync as
|
|
11795
|
+
lstatSync as lstatSync12,
|
|
11154
11796
|
opendirSync as opendirSync7,
|
|
11155
11797
|
openSync as openSync6,
|
|
11156
11798
|
statfsSync
|
|
11157
11799
|
} from "node:fs";
|
|
11158
11800
|
import { open } from "node:fs/promises";
|
|
11159
|
-
import { dirname as
|
|
11801
|
+
import { dirname as dirname15, join as join19 } from "node:path";
|
|
11160
11802
|
|
|
11161
11803
|
// src/history/admission.ts
|
|
11162
11804
|
function decodeRunAdmission(value) {
|
|
@@ -11641,7 +12283,7 @@ async function retryStalled(read) {
|
|
|
11641
12283
|
} catch (error) {
|
|
11642
12284
|
if (!(error instanceof StalledQuantum) || retry >= STALL_RETRIES)
|
|
11643
12285
|
throw error;
|
|
11644
|
-
await new Promise((
|
|
12286
|
+
await new Promise((resolve9) => setImmediate(resolve9));
|
|
11645
12287
|
}
|
|
11646
12288
|
}
|
|
11647
12289
|
}
|
|
@@ -11662,14 +12304,14 @@ class HistoryFiles {
|
|
|
11662
12304
|
this.coordination = coordination;
|
|
11663
12305
|
this.format = format;
|
|
11664
12306
|
this.formatSnapshot = formatSnapshot;
|
|
11665
|
-
this.directory =
|
|
12307
|
+
this.directory = join19(coordination.directory, "history-v2");
|
|
11666
12308
|
this.files = coordination.files;
|
|
11667
12309
|
}
|
|
11668
12310
|
static open(coordination) {
|
|
11669
|
-
const dir =
|
|
12311
|
+
const dir = join19(coordination.directory, "history-v2");
|
|
11670
12312
|
if (!coordination.files.directory(dir))
|
|
11671
12313
|
return;
|
|
11672
|
-
const file = coordination.files.read(
|
|
12314
|
+
const file = coordination.files.read(join19(dir, "format.json"));
|
|
11673
12315
|
if (!file)
|
|
11674
12316
|
corrupt();
|
|
11675
12317
|
const format = decodeFormat(json(file));
|
|
@@ -11685,7 +12327,7 @@ class HistoryFiles {
|
|
|
11685
12327
|
existing.assertWritable();
|
|
11686
12328
|
return existing;
|
|
11687
12329
|
}
|
|
11688
|
-
const dir =
|
|
12330
|
+
const dir = join19(c.directory, "history-v2");
|
|
11689
12331
|
const format = {
|
|
11690
12332
|
schema: FORMAT,
|
|
11691
12333
|
storeId: randomUUID10(),
|
|
@@ -11695,19 +12337,19 @@ class HistoryFiles {
|
|
|
11695
12337
|
c.files.directory(dir, true);
|
|
11696
12338
|
for (const name of ["commands", "ledger", "runs", "indexes"]) {
|
|
11697
12339
|
lease.assert();
|
|
11698
|
-
c.files.directory(
|
|
12340
|
+
c.files.directory(join19(dir, name), true);
|
|
11699
12341
|
}
|
|
11700
12342
|
lease.assert();
|
|
11701
|
-
const keyPath =
|
|
12343
|
+
const keyPath = join19(dir, "read-proof-key.json");
|
|
11702
12344
|
c.files.create(keyPath, encoded({
|
|
11703
12345
|
schema: "cadence-history-read-key/1",
|
|
11704
12346
|
storeId: format.storeId,
|
|
11705
12347
|
key: randomBytes4(32).toString("base64url")
|
|
11706
12348
|
}));
|
|
11707
12349
|
lease.assert();
|
|
11708
|
-
const snapshot = c.files.create(
|
|
12350
|
+
const snapshot = c.files.create(join19(dir, "format.json"), encoded(format));
|
|
11709
12351
|
const result = new HistoryFiles(c, format, snapshot);
|
|
11710
|
-
result.syncParents(
|
|
12352
|
+
result.syncParents(join19(dir, "format.json"));
|
|
11711
12353
|
result.assertWritable();
|
|
11712
12354
|
return result;
|
|
11713
12355
|
}
|
|
@@ -11742,7 +12384,7 @@ class HistoryFiles {
|
|
|
11742
12384
|
throw new ContractError("quarantine");
|
|
11743
12385
|
}
|
|
11744
12386
|
assertFormat() {
|
|
11745
|
-
this.files.assert(
|
|
12387
|
+
this.files.assert(join19(this.directory, "format.json"), this.formatSnapshot);
|
|
11746
12388
|
}
|
|
11747
12389
|
readProofMaterial(budget) {
|
|
11748
12390
|
if (this.format.schema !== FORMAT)
|
|
@@ -11751,7 +12393,7 @@ class HistoryFiles {
|
|
|
11751
12393
|
this.assertReadFormat(budget);
|
|
11752
12394
|
else
|
|
11753
12395
|
this.assertFormat();
|
|
11754
|
-
const selected2 = this.readBudgeted(
|
|
12396
|
+
const selected2 = this.readBudgeted(join19(this.directory, "read-proof-key.json"), 512, budget);
|
|
11755
12397
|
if (!selected2 || this.proofSnapshot && (selected2.identity !== this.proofSnapshot.identity || selected2.stamp !== this.proofSnapshot.stamp))
|
|
11756
12398
|
corrupt();
|
|
11757
12399
|
try {
|
|
@@ -11779,17 +12421,17 @@ class HistoryFiles {
|
|
|
11779
12421
|
return;
|
|
11780
12422
|
}
|
|
11781
12423
|
this.assertFormat();
|
|
11782
|
-
assertIncarnation(this,
|
|
12424
|
+
assertIncarnation(this, join19(this.directory, "read-proof-key.json"), this.proofSnapshot.stamp);
|
|
11783
12425
|
}
|
|
11784
12426
|
segmentPath(segment) {
|
|
11785
|
-
return
|
|
12427
|
+
return join19(this.directory, "ledger", `${String(segment).padStart(12, "0")}.jsonl`);
|
|
11786
12428
|
}
|
|
11787
12429
|
artifactPath(ref) {
|
|
11788
|
-
return
|
|
12430
|
+
return join19(this.directory, "runs", ref.runId, "artifacts", ref.artifactId, `${ref.part}.part`);
|
|
11789
12431
|
}
|
|
11790
12432
|
admissionPath(runId) {
|
|
11791
12433
|
decodeId(runId);
|
|
11792
|
-
return
|
|
12434
|
+
return join19(this.directory, "runs", runId, "admission.json");
|
|
11793
12435
|
}
|
|
11794
12436
|
writeAdmission(lease, input) {
|
|
11795
12437
|
lease.assert();
|
|
@@ -11803,7 +12445,7 @@ class HistoryFiles {
|
|
|
11803
12445
|
return this.files.read(path, maximum);
|
|
11804
12446
|
let size2;
|
|
11805
12447
|
try {
|
|
11806
|
-
size2 =
|
|
12448
|
+
size2 = lstatSync12(path).size;
|
|
11807
12449
|
} catch {
|
|
11808
12450
|
return this.files.read(path, 0, budget.walks);
|
|
11809
12451
|
}
|
|
@@ -11815,7 +12457,7 @@ class HistoryFiles {
|
|
|
11815
12457
|
readCommand(commandId, budget) {
|
|
11816
12458
|
decodeId(commandId);
|
|
11817
12459
|
this.assertReadFormat(budget);
|
|
11818
|
-
return this.readBudgeted(
|
|
12460
|
+
return this.readBudgeted(join19(this.directory, "commands", commandId, "intent.json"), RECORD_BYTES, budget);
|
|
11819
12461
|
}
|
|
11820
12462
|
readAdmission(runId, identity, budget) {
|
|
11821
12463
|
return this.readAdmissionIdentity(runId, hash2(canonicalJson(identity)), budget);
|
|
@@ -11836,10 +12478,10 @@ class HistoryFiles {
|
|
|
11836
12478
|
syncParents(file) {
|
|
11837
12479
|
if (process.platform === "win32")
|
|
11838
12480
|
return;
|
|
11839
|
-
for (let path =
|
|
12481
|
+
for (let path = dirname15(file);; path = dirname15(path)) {
|
|
11840
12482
|
if (!this.files.directory(path))
|
|
11841
12483
|
corrupt();
|
|
11842
|
-
const fd = openSync6(path,
|
|
12484
|
+
const fd = openSync6(path, constants6.O_RDONLY | constants6.O_DIRECTORY);
|
|
11843
12485
|
try {
|
|
11844
12486
|
fsyncSync3(fd);
|
|
11845
12487
|
} finally {
|
|
@@ -11847,13 +12489,13 @@ class HistoryFiles {
|
|
|
11847
12489
|
}
|
|
11848
12490
|
if (path === this.coordination.directory)
|
|
11849
12491
|
break;
|
|
11850
|
-
if (path ===
|
|
12492
|
+
if (path === dirname15(path))
|
|
11851
12493
|
corrupt();
|
|
11852
12494
|
}
|
|
11853
12495
|
}
|
|
11854
12496
|
assertReadFormat(budget) {
|
|
11855
12497
|
budget.reserve(this.formatSnapshot.bytes.length);
|
|
11856
|
-
this.files.assert(
|
|
12498
|
+
this.files.assert(join19(this.directory, "format.json"), this.formatSnapshot, this.formatSnapshot.bytes.length);
|
|
11857
12499
|
}
|
|
11858
12500
|
assertPublished(segment, end, seq) {
|
|
11859
12501
|
const fence = publicationFences.get(this.directory);
|
|
@@ -11891,7 +12533,7 @@ class HistoryFiles {
|
|
|
11891
12533
|
const dir = opendirSync7(path);
|
|
11892
12534
|
try {
|
|
11893
12535
|
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
11894
|
-
const p =
|
|
12536
|
+
const p = join19(path, e.name), st = lstatSync12(p);
|
|
11895
12537
|
if (st.isDirectory())
|
|
11896
12538
|
bytes += walk(p, depth + 1);
|
|
11897
12539
|
else {
|
|
@@ -11926,7 +12568,7 @@ class HistoryFiles {
|
|
|
11926
12568
|
state.bytes = 0;
|
|
11927
12569
|
state.identity = null;
|
|
11928
12570
|
let count = 0;
|
|
11929
|
-
const ledger =
|
|
12571
|
+
const ledger = join19(this.directory, "ledger");
|
|
11930
12572
|
if (!this.files.directory(ledger))
|
|
11931
12573
|
corrupt();
|
|
11932
12574
|
const dir = opendirSync7(ledger);
|
|
@@ -12089,9 +12731,9 @@ class HistoryFiles {
|
|
|
12089
12731
|
this.activity().generation++;
|
|
12090
12732
|
const before = this.files.create(path, "", LIMITS.segmentBytes);
|
|
12091
12733
|
let committed;
|
|
12092
|
-
const fd = await open(path,
|
|
12734
|
+
const fd = await open(path, constants6.O_WRONLY | constants6.O_APPEND | (constants6.O_NOFOLLOW ?? 0));
|
|
12093
12735
|
const assertOriginal = (bytes) => {
|
|
12094
|
-
const original = fstatSync5(fd.fd), selected2 =
|
|
12736
|
+
const original = fstatSync5(fd.fd), selected2 = lstatSync12(path);
|
|
12095
12737
|
if (original.dev !== selected2.dev || original.ino !== selected2.ino || original.nlink !== 1)
|
|
12096
12738
|
corrupt();
|
|
12097
12739
|
const snapshot = this.files.read(path, LIMITS.segmentBytes);
|
|
@@ -12204,11 +12846,11 @@ class HistoryFiles {
|
|
|
12204
12846
|
}
|
|
12205
12847
|
if (!before || segment === state.segment && (before.bytes.length !== state.bytes || before.identity !== state.identity))
|
|
12206
12848
|
corrupt();
|
|
12207
|
-
const fd = await open(path,
|
|
12849
|
+
const fd = await open(path, constants6.O_WRONLY | constants6.O_APPEND | (constants6.O_NOFOLLOW ?? 0));
|
|
12208
12850
|
try {
|
|
12209
12851
|
lease.assert();
|
|
12210
12852
|
this.files.assert(path, before, LIMITS.segmentBytes);
|
|
12211
|
-
const opened = await fd.stat(), selected2 =
|
|
12853
|
+
const opened = await fd.stat(), selected2 = lstatSync12(path);
|
|
12212
12854
|
if (opened.ino !== selected2.ino || opened.dev !== selected2.dev || opened.nlink !== 1)
|
|
12213
12855
|
corrupt();
|
|
12214
12856
|
beforeEffect?.();
|
|
@@ -12243,7 +12885,7 @@ class HistoryFiles {
|
|
|
12243
12885
|
checkpoint(lease, state) {
|
|
12244
12886
|
lease.assert();
|
|
12245
12887
|
this.assertWritable();
|
|
12246
|
-
const path =
|
|
12888
|
+
const path = join19(this.directory, "checkpoint.json"), old = this.files.read(path);
|
|
12247
12889
|
const body = encoded({
|
|
12248
12890
|
schema: "cadence-history-checkpoint/2",
|
|
12249
12891
|
storeId: this.format.storeId,
|
|
@@ -12952,7 +13594,7 @@ class HistoryNavigation {
|
|
|
12952
13594
|
cacheBytes = 0;
|
|
12953
13595
|
blocks = new Map;
|
|
12954
13596
|
block(path, at, size2, budget) {
|
|
12955
|
-
const start = Math.floor(at / (1024 * 1024)) * 1024 * 1024, key = `${path}/${start}`, st = stamp3(
|
|
13597
|
+
const start = Math.floor(at / (1024 * 1024)) * 1024 * 1024, key = `${path}/${start}`, st = stamp3(lstatSync13(path));
|
|
12956
13598
|
let old = this.blocks.get(key);
|
|
12957
13599
|
if (!old || old.budget !== budget || old.stamp !== st) {
|
|
12958
13600
|
const bytes = readRange(this.files, path, start, Math.min(1024 * 1024, size2 - start), budget);
|
|
@@ -12990,9 +13632,9 @@ class HistoryNavigation {
|
|
|
12990
13632
|
if (row.value.kind === "auth-preparation")
|
|
12991
13633
|
auth.set(row.value.intent.operationId, row.value.intent.runId);
|
|
12992
13634
|
if (row.value.kind === "auth-settlement") {
|
|
12993
|
-
const
|
|
12994
|
-
if (
|
|
12995
|
-
keys2.add(
|
|
13635
|
+
const run2 = auth.get(row.value.result.operationId);
|
|
13636
|
+
if (run2)
|
|
13637
|
+
keys2.add(run2);
|
|
12996
13638
|
auth.delete(row.value.result.operationId);
|
|
12997
13639
|
}
|
|
12998
13640
|
}
|
|
@@ -13030,7 +13672,7 @@ class HistoryNavigation {
|
|
|
13030
13672
|
}
|
|
13031
13673
|
}
|
|
13032
13674
|
indexPath(group, id) {
|
|
13033
|
-
return
|
|
13675
|
+
return join20(this.files.directory, "indexes", group, `${id}.json`);
|
|
13034
13676
|
}
|
|
13035
13677
|
hintKey(group, id) {
|
|
13036
13678
|
return `${this.files.directory}/${this.files.format.storeId}/${group}/${id}`;
|
|
@@ -13087,9 +13729,9 @@ class HistoryNavigation {
|
|
|
13087
13729
|
return structuredClone(source.value);
|
|
13088
13730
|
}
|
|
13089
13731
|
try {
|
|
13090
|
-
if (!this.files.files.directory(
|
|
13732
|
+
if (!this.files.files.directory(dirname16(path), false, budget.walks))
|
|
13091
13733
|
return source ? structuredClone(source.value) : undefined;
|
|
13092
|
-
const size2 =
|
|
13734
|
+
const size2 = lstatSync13(path).size;
|
|
13093
13735
|
if (size2 > RECORD_BYTES)
|
|
13094
13736
|
return source ? structuredClone(source.value) : undefined;
|
|
13095
13737
|
return parseWireJson(readRange(this.files, path, 0, size2, budget).toString("utf8"), RECORD_BYTES);
|
|
@@ -13168,10 +13810,10 @@ class HistoryNavigation {
|
|
|
13168
13810
|
if (row.value.kind === "auth-preparation")
|
|
13169
13811
|
this.authRuns.set(row.value.intent.operationId, row.value.intent.runId);
|
|
13170
13812
|
if (row.value.kind === "auth-settlement") {
|
|
13171
|
-
const
|
|
13172
|
-
if (
|
|
13173
|
-
keys2.add(
|
|
13174
|
-
runKeys.add(
|
|
13813
|
+
const run2 = this.authRuns.get(row.value.result.operationId);
|
|
13814
|
+
if (run2) {
|
|
13815
|
+
keys2.add(run2);
|
|
13816
|
+
runKeys.add(run2);
|
|
13175
13817
|
}
|
|
13176
13818
|
this.authRuns.delete(row.value.result.operationId);
|
|
13177
13819
|
}
|
|
@@ -13221,7 +13863,7 @@ class HistoryNavigation {
|
|
|
13221
13863
|
}
|
|
13222
13864
|
batch(p, budget) {
|
|
13223
13865
|
const key = canonicalJson(p), cached = this.cache.get(key), path = this.files.segmentPath(p.segment);
|
|
13224
|
-
if (cached && cached.budget === budget && cached.stamp === stamp3(
|
|
13866
|
+
if (cached && cached.budget === budget && cached.stamp === stamp3(lstatSync13(path))) {
|
|
13225
13867
|
this.validateBridge(cached.batch.rows, p, budget);
|
|
13226
13868
|
this.rememberBoundary(p, cached.batch.navigationRoot, budget);
|
|
13227
13869
|
return cached.batch;
|
|
@@ -13232,7 +13874,7 @@ class HistoryNavigation {
|
|
|
13232
13874
|
if (cached.identity !== identity)
|
|
13233
13875
|
bad2();
|
|
13234
13876
|
cached.budget = budget;
|
|
13235
|
-
cached.stamp = stamp3(
|
|
13877
|
+
cached.stamp = stamp3(lstatSync13(path));
|
|
13236
13878
|
this.files.assertPublished(p.segment, p.offset + p.bytes, p.seq);
|
|
13237
13879
|
this.validateBridge(cached.batch.rows, p, budget);
|
|
13238
13880
|
this.rememberBoundary(p, cached.batch.navigationRoot, budget);
|
|
@@ -13283,7 +13925,7 @@ class HistoryNavigation {
|
|
|
13283
13925
|
identity,
|
|
13284
13926
|
batch,
|
|
13285
13927
|
bytes: bytes.length,
|
|
13286
|
-
stamp: stamp3(
|
|
13928
|
+
stamp: stamp3(lstatSync13(path)),
|
|
13287
13929
|
budget
|
|
13288
13930
|
});
|
|
13289
13931
|
this.cacheBytes += bytes.length;
|
|
@@ -13300,7 +13942,7 @@ class HistoryNavigation {
|
|
|
13300
13942
|
const path = this.files.segmentPath(segment);
|
|
13301
13943
|
let size2;
|
|
13302
13944
|
try {
|
|
13303
|
-
size2 =
|
|
13945
|
+
size2 = lstatSync13(path).size;
|
|
13304
13946
|
} catch (e) {
|
|
13305
13947
|
if (e instanceof Error && "code" in e && e.code === "ENOENT")
|
|
13306
13948
|
return null;
|
|
@@ -14161,7 +14803,7 @@ function materializeEntity(s, sources) {
|
|
|
14161
14803
|
|
|
14162
14804
|
// src/history/export.ts
|
|
14163
14805
|
import { createHash as createHash15, randomUUID as randomUUID11 } from "node:crypto";
|
|
14164
|
-
import { readFileSync as
|
|
14806
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
14165
14807
|
|
|
14166
14808
|
// src/history/search.ts
|
|
14167
14809
|
import { createHash as createHash14 } from "node:crypto";
|
|
@@ -16014,14 +16656,14 @@ async function searchQuantum(reader, value, limit = LIMITS.pageEntries) {
|
|
|
16014
16656
|
// src/history/export.ts
|
|
16015
16657
|
var exporterIdentity;
|
|
16016
16658
|
function buildIdentity() {
|
|
16017
|
-
exporterIdentity ??= createHash15("sha256").update(
|
|
16659
|
+
exporterIdentity ??= createHash15("sha256").update(readFileSync4(new URL(import.meta.url))).digest("hex");
|
|
16018
16660
|
return exporterIdentity;
|
|
16019
16661
|
}
|
|
16020
16662
|
function aborted(signal) {
|
|
16021
16663
|
if (signal.aborted)
|
|
16022
16664
|
throw new ContractError("unavailable");
|
|
16023
16665
|
}
|
|
16024
|
-
var yieldTurn = () => new Promise((
|
|
16666
|
+
var yieldTurn = () => new Promise((resolve9) => setImmediate(resolve9));
|
|
16025
16667
|
function* textChunks(text2) {
|
|
16026
16668
|
const bytes = Buffer.from(text2);
|
|
16027
16669
|
for (let at = 0;at < bytes.length; ) {
|
|
@@ -16749,14 +17391,14 @@ function decodeState(value) {
|
|
|
16749
17391
|
legacyIdentity: o.legacyIdentity
|
|
16750
17392
|
};
|
|
16751
17393
|
}
|
|
16752
|
-
function target(entity,
|
|
17394
|
+
function target(entity, run2, stop = null) {
|
|
16753
17395
|
return {
|
|
16754
17396
|
phase: "scan",
|
|
16755
17397
|
proof: freshValidator(),
|
|
16756
17398
|
row: 0,
|
|
16757
17399
|
event: 0,
|
|
16758
17400
|
entity,
|
|
16759
|
-
run,
|
|
17401
|
+
run: run2,
|
|
16760
17402
|
stop
|
|
16761
17403
|
};
|
|
16762
17404
|
}
|
|
@@ -17860,7 +18502,7 @@ class HistoryProjection {
|
|
|
17860
18502
|
// src/history/reader.ts
|
|
17861
18503
|
import { createHash as createHash18 } from "node:crypto";
|
|
17862
18504
|
import { existsSync as existsSync2 } from "node:fs";
|
|
17863
|
-
import { join as
|
|
18505
|
+
import { join as join21 } from "node:path";
|
|
17864
18506
|
|
|
17865
18507
|
// src/history/saved-graph-state.ts
|
|
17866
18508
|
var freshSavedGraph = () => ({
|
|
@@ -18259,12 +18901,12 @@ class HistoryReader {
|
|
|
18259
18901
|
}
|
|
18260
18902
|
reviewSelection(taskId2, runId, snapshotSeq) {
|
|
18261
18903
|
const budget = HistoryReadBudget.authority();
|
|
18262
|
-
const
|
|
18263
|
-
if (!
|
|
18904
|
+
const run2 = this.reviewRun(taskId2, runId, snapshotSeq, budget);
|
|
18905
|
+
if (!run2.admission)
|
|
18264
18906
|
throw new ContractError("corrupt-selected");
|
|
18265
|
-
const { graph } = this.files.readAdmission(runId,
|
|
18907
|
+
const { graph } = this.files.readAdmission(runId, run2.admission, budget);
|
|
18266
18908
|
if (!graph)
|
|
18267
|
-
return { run, graph, readablePlan: "" };
|
|
18909
|
+
return { run: run2, graph, readablePlan: "" };
|
|
18268
18910
|
const ref = graph.plan;
|
|
18269
18911
|
const { detail, bytes } = this.planText({
|
|
18270
18912
|
storeId: this.storeId,
|
|
@@ -18278,7 +18920,7 @@ class HistoryReader {
|
|
|
18278
18920
|
}, budget);
|
|
18279
18921
|
if (ref.bytes > 32768 || canonicalJson(detail.content) !== canonicalJson(ref) || detail.owner.taskId !== taskId2 || detail.owner.runId !== null || detail.owner.childRunId !== null || detail.owner.entity.kind !== "task" || detail.owner.entity.id !== taskId2 || detail.owner.entity.version !== graph.planRevision)
|
|
18280
18922
|
throw new ContractError("corrupt-selected");
|
|
18281
|
-
return { run, graph, readablePlan: bytes.toString("utf8") };
|
|
18923
|
+
return { run: run2, graph, readablePlan: bytes.toString("utf8") };
|
|
18282
18924
|
}
|
|
18283
18925
|
async graph(value) {
|
|
18284
18926
|
const request3 = decodeGraphReadRequest(value);
|
|
@@ -18628,7 +19270,7 @@ class HistoryReader {
|
|
|
18628
19270
|
}
|
|
18629
19271
|
async readDetail(value) {
|
|
18630
19272
|
const cursor = decodeCursor(value);
|
|
18631
|
-
if (cursor.navigation !== undefined || this.files.format.schema === FORMAT && !existsSync2(
|
|
19273
|
+
if (cursor.navigation !== undefined || this.files.format.schema === FORMAT && !existsSync2(join21(this.files.directory, "indexes", "heads", "latest.json")))
|
|
18632
19274
|
return readRetainedDetail(this, cursor);
|
|
18633
19275
|
if (this.files.format.schema !== FORMAT)
|
|
18634
19276
|
return this.detail(cursor);
|
|
@@ -18830,12 +19472,12 @@ class HistoryReader {
|
|
|
18830
19472
|
|
|
18831
19473
|
// src/history/recovery.ts
|
|
18832
19474
|
import { createHash as createHash22, randomUUID as randomUUID15 } from "node:crypto";
|
|
18833
|
-
import { lstatSync as
|
|
18834
|
-
import { join as
|
|
19475
|
+
import { lstatSync as lstatSync14, opendirSync as opendirSync8 } from "node:fs";
|
|
19476
|
+
import { join as join25, relative as relative7 } from "node:path";
|
|
18835
19477
|
|
|
18836
19478
|
// src/history/writer.ts
|
|
18837
19479
|
import { createHash as createHash21, randomUUID as randomUUID14 } from "node:crypto";
|
|
18838
|
-
import { join as
|
|
19480
|
+
import { join as join24 } from "node:path";
|
|
18839
19481
|
|
|
18840
19482
|
// src/task-store.ts
|
|
18841
19483
|
import { createHash as createHash19, randomUUID as randomUUID12 } from "node:crypto";
|
|
@@ -18844,12 +19486,12 @@ import {
|
|
|
18844
19486
|
fsyncSync as fsyncSync4,
|
|
18845
19487
|
openSync as openSync7,
|
|
18846
19488
|
readdirSync as readdirSync2,
|
|
18847
|
-
renameSync as
|
|
19489
|
+
renameSync as renameSync4,
|
|
18848
19490
|
unlinkSync as unlinkSync3,
|
|
18849
|
-
writeFileSync as
|
|
19491
|
+
writeFileSync as writeFileSync4
|
|
18850
19492
|
} from "node:fs";
|
|
18851
19493
|
import { hostname as hostname6 } from "node:os";
|
|
18852
|
-
import { join as
|
|
19494
|
+
import { join as join22 } from "node:path";
|
|
18853
19495
|
// src/task-types.ts
|
|
18854
19496
|
var TASK_SCHEMA = "cadence-workflow-assistant/1";
|
|
18855
19497
|
var MAX_RECORD_BYTES = 128 * 1024;
|
|
@@ -18936,7 +19578,7 @@ class TaskStore extends CheckoutStore {
|
|
|
18936
19578
|
tasks;
|
|
18937
19579
|
constructor(root, stateHome = defaultStateHome()) {
|
|
18938
19580
|
super(root, stateHome);
|
|
18939
|
-
this.tasks =
|
|
19581
|
+
this.tasks = join22(this.root, ".cadence", "assistant", "tasks");
|
|
18940
19582
|
}
|
|
18941
19583
|
recover(expected, acknowledgeEscapedProcesses) {
|
|
18942
19584
|
if (!acknowledgeEscapedProcesses || expected.host !== hostname6())
|
|
@@ -18959,10 +19601,10 @@ class TaskStore extends CheckoutStore {
|
|
|
18959
19601
|
loadSnapshot(id) {
|
|
18960
19602
|
if (!validId(id))
|
|
18961
19603
|
throw new Error("Invalid task id");
|
|
18962
|
-
const directory =
|
|
19604
|
+
const directory = join22(this.tasks, id);
|
|
18963
19605
|
if (safeDirectory(directory) && readdirSync2(directory).some((name) => name !== "task.json"))
|
|
18964
19606
|
throw new Error("Unknown task directory content; preserved without reset");
|
|
18965
|
-
const data = readBounded(
|
|
19607
|
+
const data = readBounded(join22(this.tasks, id, "task.json"), MAX_RECORD_BYTES);
|
|
18966
19608
|
if (!data)
|
|
18967
19609
|
return;
|
|
18968
19610
|
const record2 = decodeTask(parseRecord(data));
|
|
@@ -19000,25 +19642,25 @@ class TaskStore extends CheckoutStore {
|
|
|
19000
19642
|
throw new Error("Revision conflict");
|
|
19001
19643
|
if (current2?.identity !== expectedIdentity)
|
|
19002
19644
|
throw new Error("Task content changed; reopen it");
|
|
19003
|
-
const dir =
|
|
19645
|
+
const dir = join22(this.tasks, next.id);
|
|
19004
19646
|
safeDirectory(dir, true);
|
|
19005
19647
|
if (!current2 && readdirSync2(dir).length)
|
|
19006
19648
|
throw new Error("Unknown task directory");
|
|
19007
|
-
const temporary =
|
|
19649
|
+
const temporary = join22(dir, `task.${randomUUID12()}.tmp`);
|
|
19008
19650
|
const fd = openSync7(temporary, "wx", 384);
|
|
19009
19651
|
try {
|
|
19010
|
-
|
|
19652
|
+
writeFileSync4(fd, text2);
|
|
19011
19653
|
fsyncSync4(fd);
|
|
19012
19654
|
} finally {
|
|
19013
19655
|
closeSync7(fd);
|
|
19014
19656
|
}
|
|
19015
19657
|
this.assertOwner(lock);
|
|
19016
|
-
const latest = readBounded(
|
|
19658
|
+
const latest = readBounded(join22(dir, "task.json"), MAX_RECORD_BYTES);
|
|
19017
19659
|
if ((latest ? contentIdentity(latest) : undefined) !== expectedIdentity)
|
|
19018
19660
|
throw new Error("Task content changed; replacement preserved without overwrite");
|
|
19019
|
-
retryWindowsFileOperation(() =>
|
|
19661
|
+
retryWindowsFileOperation(() => renameSync4(temporary, join22(dir, "task.json")), () => {
|
|
19020
19662
|
this.assertOwner(lock);
|
|
19021
|
-
const now = readBounded(
|
|
19663
|
+
const now = readBounded(join22(dir, "task.json"), MAX_RECORD_BYTES);
|
|
19022
19664
|
if ((now ? contentIdentity(now) : undefined) !== expectedIdentity)
|
|
19023
19665
|
throw new Error("Task content changed; replacement preserved without overwrite");
|
|
19024
19666
|
});
|
|
@@ -19029,7 +19671,7 @@ class TaskStore extends CheckoutStore {
|
|
|
19029
19671
|
}
|
|
19030
19672
|
}
|
|
19031
19673
|
checks() {
|
|
19032
|
-
const data = readBounded(
|
|
19674
|
+
const data = readBounded(join22(this.root, ".cadence", "assistant", "config.json"), 32768);
|
|
19033
19675
|
if (!data)
|
|
19034
19676
|
return [];
|
|
19035
19677
|
const value = parseRecord(data);
|
|
@@ -19043,8 +19685,8 @@ class TaskStore extends CheckoutStore {
|
|
|
19043
19685
|
readLog(name) {
|
|
19044
19686
|
if (!/^[a-f0-9-]{36}\.log$/.test(name))
|
|
19045
19687
|
throw new Error("Invalid diagnostic name");
|
|
19046
|
-
this.inspectPrivateFile(
|
|
19047
|
-
const data = readBounded(
|
|
19688
|
+
this.inspectPrivateFile(join22(this.privateRoot, name), 65536);
|
|
19689
|
+
const data = readBounded(join22(this.privateRoot, name), 65536);
|
|
19048
19690
|
return data ? diagnosticText(data.toString("utf8"), 65536) : "Private diagnostic unavailable";
|
|
19049
19691
|
}
|
|
19050
19692
|
writeLog(lock, output) {
|
|
@@ -19052,7 +19694,7 @@ class TaskStore extends CheckoutStore {
|
|
|
19052
19694
|
throw new Error("Not the lock owner");
|
|
19053
19695
|
this.assertOwner(lock);
|
|
19054
19696
|
const name = `${randomUUID12()}.log`;
|
|
19055
|
-
|
|
19697
|
+
writeFileSync4(join22(this.privateRoot, name), diagnosticText(output, 65536), {
|
|
19056
19698
|
flag: "wx",
|
|
19057
19699
|
mode: 384
|
|
19058
19700
|
});
|
|
@@ -19130,12 +19772,12 @@ function planRecording(plan, graph, operationId, epoch) {
|
|
|
19130
19772
|
// src/history/publication.ts
|
|
19131
19773
|
import {
|
|
19132
19774
|
closeSync as closeSync8,
|
|
19133
|
-
constants as
|
|
19775
|
+
constants as constants7,
|
|
19134
19776
|
fsyncSync as fsyncSync5,
|
|
19135
19777
|
openSync as openSync8,
|
|
19136
|
-
realpathSync as
|
|
19778
|
+
realpathSync as realpathSync8
|
|
19137
19779
|
} from "node:fs";
|
|
19138
|
-
import { dirname as
|
|
19780
|
+
import { dirname as dirname17, isAbsolute as isAbsolute8, join as join23, relative as relative6, sep as sep6 } from "node:path";
|
|
19139
19781
|
function summary(text2, maximum, runId) {
|
|
19140
19782
|
const label = `[Cadence summary${runId ? `; run ${runId}` : ""}; retained history is separate]
|
|
19141
19783
|
`;
|
|
@@ -19150,12 +19792,12 @@ function summary(text2, maximum, runId) {
|
|
|
19150
19792
|
end--;
|
|
19151
19793
|
return label + bytes.subarray(0, end).toString("utf8") + tail;
|
|
19152
19794
|
}
|
|
19153
|
-
function publicationPhase(
|
|
19154
|
-
if (!
|
|
19795
|
+
function publicationPhase(run2) {
|
|
19796
|
+
if (!run2.settlement)
|
|
19155
19797
|
return "running";
|
|
19156
|
-
switch (settlementOutcome(
|
|
19798
|
+
switch (settlementOutcome(run2.settlement)) {
|
|
19157
19799
|
case "completed":
|
|
19158
|
-
return
|
|
19800
|
+
return run2.admission?.kind === "generate-plan" ? "planning" : "review";
|
|
19159
19801
|
case "failed":
|
|
19160
19802
|
case "blocked":
|
|
19161
19803
|
return "failed";
|
|
@@ -19168,7 +19810,7 @@ function publicationPhase(run) {
|
|
|
19168
19810
|
function bad11() {
|
|
19169
19811
|
throw new ContractError("conflict");
|
|
19170
19812
|
}
|
|
19171
|
-
function publicationRecord(store, reader, intent, link,
|
|
19813
|
+
function publicationRecord(store, reader, intent, link, run2, request3, expected, facts, snapshotSeq) {
|
|
19172
19814
|
const identity = intent.identity;
|
|
19173
19815
|
if (identity.kind !== "action")
|
|
19174
19816
|
bad11();
|
|
@@ -19184,8 +19826,8 @@ function publicationRecord(store, reader, intent, link, run, request3, expected,
|
|
|
19184
19826
|
else {
|
|
19185
19827
|
if (action.kind !== "create" || !request3.cwd || store.list().length >= 1000)
|
|
19186
19828
|
bad11();
|
|
19187
|
-
const cwd =
|
|
19188
|
-
if (
|
|
19829
|
+
const cwd = realpathSync8(request3.cwd), within = relative6(store.root, cwd);
|
|
19830
|
+
if (isAbsolute8(within) || within === ".." || within.startsWith(`..${sep6}`))
|
|
19189
19831
|
bad11();
|
|
19190
19832
|
record2 = {
|
|
19191
19833
|
schema: TASK_SCHEMA,
|
|
@@ -19218,7 +19860,7 @@ function publicationRecord(store, reader, intent, link, run, request3, expected,
|
|
|
19218
19860
|
if (mode === "baseline") {
|
|
19219
19861
|
if (!facts.some(({ link: l, fact: f }) => l.taskId === link.taskId && l.runId === link.runId && f.kind === "changes" && f.stage === "before" && canonicalJson(f.content) === canonicalJson(ref)))
|
|
19220
19862
|
bad11();
|
|
19221
|
-
} else if (!
|
|
19863
|
+
} else if (!run2 || ![...run2.children.values()].some((c) => c.completion?.recording.status === "committed" && canonicalJson(c.completion.recording.detail.content) === canonicalJson(ref)))
|
|
19222
19864
|
bad11();
|
|
19223
19865
|
let preview = "";
|
|
19224
19866
|
for (const part of reader.parts(detail, snapshotSeq))
|
|
@@ -19251,12 +19893,12 @@ function publicationRecord(store, reader, intent, link, run, request3, expected,
|
|
|
19251
19893
|
record2.phase = "planning";
|
|
19252
19894
|
delete record2.review;
|
|
19253
19895
|
}
|
|
19254
|
-
if (
|
|
19255
|
-
record2.phase = publicationPhase(
|
|
19896
|
+
if (run2) {
|
|
19897
|
+
record2.phase = publicationPhase(run2);
|
|
19256
19898
|
delete record2.review;
|
|
19257
19899
|
let result = request3.result ? retained(request3.result, "result") : "";
|
|
19258
19900
|
if (!request3.result)
|
|
19259
|
-
for (const child of
|
|
19901
|
+
for (const child of run2.children.values()) {
|
|
19260
19902
|
if (child.completion?.recording.status === "committed") {
|
|
19261
19903
|
const text2 = retained(child.completion.recording.detail.content, "result");
|
|
19262
19904
|
if (Buffer.byteLength(result) < 32768)
|
|
@@ -19265,24 +19907,24 @@ ${text2}
|
|
|
19265
19907
|
`;
|
|
19266
19908
|
}
|
|
19267
19909
|
}
|
|
19268
|
-
if (
|
|
19269
|
-
result = `Execution: ${settlementOutcome(
|
|
19910
|
+
if (run2.settlement)
|
|
19911
|
+
result = `Execution: ${settlementOutcome(run2.settlement)} (checks and human review are separate).
|
|
19270
19912
|
${result}`;
|
|
19271
|
-
if (
|
|
19913
|
+
if (run2.admission?.kind === "generate-plan")
|
|
19272
19914
|
result = `Advisory plan proposal only; not saved or authorized for Start. Explicitly edit/accept and Save a plan before a separate Start.
|
|
19273
19915
|
${result}`;
|
|
19274
19916
|
record2.result = summary(result, 16384, link.runId);
|
|
19275
|
-
if (action.kind === "check" &&
|
|
19917
|
+
if (action.kind === "check" && run2.check && run2.settlement?.kind === "check") {
|
|
19276
19918
|
if (record2.checks.length >= 32)
|
|
19277
19919
|
bad11();
|
|
19278
|
-
const s =
|
|
19920
|
+
const s = run2.settlement;
|
|
19279
19921
|
const fact = facts.findLast(({ link: l, fact: f }) => l.runId === link.runId && f.kind === "check-settled")?.fact;
|
|
19280
19922
|
if (fact?.kind !== "check-settled")
|
|
19281
19923
|
bad11();
|
|
19282
19924
|
record2.checks.push({
|
|
19283
|
-
name:
|
|
19284
|
-
command:
|
|
19285
|
-
timeoutMs:
|
|
19925
|
+
name: run2.check.selection.name,
|
|
19926
|
+
command: run2.check.selection.command,
|
|
19927
|
+
timeoutMs: run2.check.selection.timeoutMs,
|
|
19286
19928
|
cwd: record2.cwd,
|
|
19287
19929
|
exitCode: s.native.exitCode,
|
|
19288
19930
|
durationMs: s.native.durationMs,
|
|
@@ -19320,10 +19962,10 @@ ${result}`;
|
|
|
19320
19962
|
function syncTaskPublication(store, taskId2) {
|
|
19321
19963
|
if (process.platform === "win32")
|
|
19322
19964
|
return;
|
|
19323
|
-
for (let path =
|
|
19965
|
+
for (let path = join23(store.tasks, taskId2);; path = dirname17(path)) {
|
|
19324
19966
|
if (!safeDirectory(path))
|
|
19325
19967
|
throw new ContractError("conflict");
|
|
19326
|
-
const fd = openSync8(path,
|
|
19968
|
+
const fd = openSync8(path, constants7.O_RDONLY | constants7.O_DIRECTORY);
|
|
19327
19969
|
try {
|
|
19328
19970
|
fsyncSync5(fd);
|
|
19329
19971
|
} finally {
|
|
@@ -19331,17 +19973,17 @@ function syncTaskPublication(store, taskId2) {
|
|
|
19331
19973
|
}
|
|
19332
19974
|
if (path === store.root)
|
|
19333
19975
|
return;
|
|
19334
|
-
if (
|
|
19976
|
+
if (dirname17(path) === path)
|
|
19335
19977
|
throw new ContractError("conflict");
|
|
19336
19978
|
}
|
|
19337
19979
|
}
|
|
19338
19980
|
|
|
19339
19981
|
// src/history/review.ts
|
|
19340
|
-
function reviewIssues(
|
|
19982
|
+
function reviewIssues(run2, current2, code2, epoch) {
|
|
19341
19983
|
const issues = new Set;
|
|
19342
19984
|
if (!current2.observation.complete)
|
|
19343
19985
|
issues.add("incomplete-observation");
|
|
19344
|
-
const projections =
|
|
19986
|
+
const projections = run2.projections(epoch);
|
|
19345
19987
|
if (projections.some((p) => p.outcome !== "completed"))
|
|
19346
19988
|
issues.add("partial-results");
|
|
19347
19989
|
if (projections.some((p) => p.coverage.observed !== "complete" || p.coverage.source !== "complete" || p.coverage.omissions.length))
|
|
@@ -19717,29 +20359,29 @@ class HistoryWriter {
|
|
|
19717
20359
|
settled?.();
|
|
19718
20360
|
return Promise.resolve(failure(false));
|
|
19719
20361
|
}
|
|
19720
|
-
let
|
|
20362
|
+
let release2;
|
|
19721
20363
|
try {
|
|
19722
|
-
|
|
20364
|
+
release2 = this.files.retainOriginal(this.writeGeneration);
|
|
19723
20365
|
} catch {
|
|
19724
20366
|
this.fence();
|
|
19725
20367
|
settled?.();
|
|
19726
20368
|
return Promise.resolve(failure(false));
|
|
19727
20369
|
}
|
|
19728
|
-
let
|
|
20370
|
+
let resolve9;
|
|
19729
20371
|
const original = new Promise((yes) => {
|
|
19730
|
-
|
|
20372
|
+
resolve9 = yes;
|
|
19731
20373
|
});
|
|
19732
20374
|
this.originals.add(original);
|
|
19733
20375
|
original.then(() => {
|
|
19734
20376
|
this.originals.delete(original);
|
|
19735
|
-
|
|
20377
|
+
release2();
|
|
19736
20378
|
settled?.();
|
|
19737
20379
|
});
|
|
19738
20380
|
try {
|
|
19739
20381
|
this.options.onOriginal?.(original, safety || continuation);
|
|
19740
20382
|
} catch {
|
|
19741
20383
|
this.fence();
|
|
19742
|
-
|
|
20384
|
+
resolve9(failure(false));
|
|
19743
20385
|
return original;
|
|
19744
20386
|
}
|
|
19745
20387
|
if (this.queue.length && this.queue.reduce((n, j) => n + j.bytes, 0) + bytes > LIMITS.progressBatchBytes)
|
|
@@ -19747,7 +20389,7 @@ class HistoryWriter {
|
|
|
19747
20389
|
this.queue.push({
|
|
19748
20390
|
build: build2,
|
|
19749
20391
|
bytes,
|
|
19750
|
-
resolve:
|
|
20392
|
+
resolve: resolve9,
|
|
19751
20393
|
safety,
|
|
19752
20394
|
continuation,
|
|
19753
20395
|
control: immediate
|
|
@@ -19975,9 +20617,9 @@ class HistoryWriter {
|
|
|
19975
20617
|
if (state.admission?.context.task)
|
|
19976
20618
|
tasks.add(state.admission.context.task.id);
|
|
19977
20619
|
for (const row of rows) {
|
|
19978
|
-
const v = row.value,
|
|
19979
|
-
if (
|
|
19980
|
-
runs.add(
|
|
20620
|
+
const v = row.value, run2 = this.rowRunId(v);
|
|
20621
|
+
if (run2)
|
|
20622
|
+
runs.add(run2);
|
|
19981
20623
|
if (v.kind === "owner" || v.kind === "capture" || v.kind === "launch" || v.kind === "settlement")
|
|
19982
20624
|
tasks.add(v.link.taskId);
|
|
19983
20625
|
if (v.kind === "admission" && v.intent.identity.kind === "action") {
|
|
@@ -20073,10 +20715,10 @@ class HistoryWriter {
|
|
|
20073
20715
|
try {
|
|
20074
20716
|
const budget = HistoryReadBudget.authority();
|
|
20075
20717
|
const intent = this.files.readCommand(commandId, budget);
|
|
20076
|
-
const directoryExists = this.files.files.directory(
|
|
20718
|
+
const directoryExists = this.files.files.directory(join24(this.files.directory, "commands", commandId));
|
|
20077
20719
|
const wanted = intent ? decodeAdmissionIntent(json(intent, RECORD_BYTES)) : null;
|
|
20078
20720
|
let found = null;
|
|
20079
|
-
let
|
|
20721
|
+
let run2 = null;
|
|
20080
20722
|
let operation = null;
|
|
20081
20723
|
const scan = emptyScan();
|
|
20082
20724
|
for (const batch of this.files.scan(scan, false, budget)) {
|
|
@@ -20086,19 +20728,19 @@ class HistoryWriter {
|
|
|
20086
20728
|
return { status: "inconclusive" };
|
|
20087
20729
|
found = row.value.record;
|
|
20088
20730
|
if (row.value.intent.identity.kind === "action" && row.value.intent.runId)
|
|
20089
|
-
|
|
20731
|
+
run2 = new RunState(row.value.intent.runId);
|
|
20090
20732
|
else if (row.value.intent.identity.kind === "action")
|
|
20091
20733
|
operation = new OperationState(row.value.intent.operationId);
|
|
20092
20734
|
}
|
|
20093
20735
|
for (const row of batch.rows) {
|
|
20094
|
-
|
|
20736
|
+
run2?.apply(row, batch.marker.last);
|
|
20095
20737
|
operation?.apply(row, batch.marker.last);
|
|
20096
20738
|
}
|
|
20097
20739
|
}
|
|
20098
20740
|
if (found)
|
|
20099
20741
|
return {
|
|
20100
20742
|
status: "found",
|
|
20101
|
-
record: decodeStoredCommand(
|
|
20743
|
+
record: decodeStoredCommand(run2?.receipt() ?? operation?.receipt() ?? found)
|
|
20102
20744
|
};
|
|
20103
20745
|
if (wanted || directoryExists || scan.tail)
|
|
20104
20746
|
return { status: "inconclusive" };
|
|
@@ -20219,8 +20861,8 @@ class HistoryWriter {
|
|
|
20219
20861
|
this.assertOrdinary();
|
|
20220
20862
|
const bytes = encoded(intent);
|
|
20221
20863
|
this.charge(Buffer.byteLength(bytes), intent.runId, true);
|
|
20222
|
-
this.files.files.create(
|
|
20223
|
-
this.files.syncParents(
|
|
20864
|
+
this.files.files.create(join24(this.files.directory, "commands", intent.commandId, "intent.json"), bytes, RECORD_BYTES);
|
|
20865
|
+
this.files.syncParents(join24(this.files.directory, "commands", intent.commandId, "intent.json"));
|
|
20224
20866
|
await this.options.observeWrite?.("intent");
|
|
20225
20867
|
const policy = this.resourcePolicy(intent.identity.kind === "action" ? intent.identity.expected.configRev : 1);
|
|
20226
20868
|
return { kind: "admission", intent, record: record2, policy };
|
|
@@ -20354,11 +20996,11 @@ class HistoryWriter {
|
|
|
20354
20996
|
continue;
|
|
20355
20997
|
try {
|
|
20356
20998
|
const checks = checkFold(this.files, taskId2, head.seq, HistoryReadBudget.authority()).state();
|
|
20357
|
-
const
|
|
20999
|
+
const run2 = latestTaskRun(this.files, taskId2, head.seq, HistoryReadBudget.authority());
|
|
20358
21000
|
const plan = planFold(this.files, taskId2, head.seq, HistoryReadBudget.authority(), new HistoryNavigation(this.files), true).state();
|
|
20359
21001
|
taskFoldMemory(this.files).remember(taskId2, head, {
|
|
20360
21002
|
checks,
|
|
20361
|
-
run,
|
|
21003
|
+
run: run2,
|
|
20362
21004
|
plan
|
|
20363
21005
|
});
|
|
20364
21006
|
} catch {}
|
|
@@ -20418,10 +21060,10 @@ class HistoryWriter {
|
|
|
20418
21060
|
}
|
|
20419
21061
|
};
|
|
20420
21062
|
assertContext();
|
|
20421
|
-
const
|
|
20422
|
-
if (
|
|
21063
|
+
const run2 = intent.runId ? this.retainedRun(intent.runId) : null;
|
|
21064
|
+
if (run2 && !run2.admission)
|
|
20423
21065
|
throw new ContractError("conflict");
|
|
20424
|
-
expected =
|
|
21066
|
+
expected = run2 ? run2.publicationExpected() : operation.state.publicationExpected();
|
|
20425
21067
|
link = {
|
|
20426
21068
|
epoch: identity.expected.epoch,
|
|
20427
21069
|
taskId: taskId2,
|
|
@@ -20431,7 +21073,7 @@ class HistoryWriter {
|
|
|
20431
21073
|
const reader = HistoryReader.open(this.lease.coordination);
|
|
20432
21074
|
if (!reader)
|
|
20433
21075
|
throw new ContractError("unavailable");
|
|
20434
|
-
record2 = publicationRecord(legacyTaskStore(this.lease.coordination), reader, intent, link,
|
|
21076
|
+
record2 = publicationRecord(legacyTaskStore(this.lease.coordination), reader, intent, link, run2, request3, expected, operation.facts, this.state.committedSeq);
|
|
20435
21077
|
this.charge(2 * Buffer.byteLength(`${JSON.stringify(record2, null, 2)}
|
|
20436
21078
|
`), intent.runId, true);
|
|
20437
21079
|
return this.ownerValue({
|
|
@@ -20576,9 +21218,9 @@ class HistoryWriter {
|
|
|
20576
21218
|
}
|
|
20577
21219
|
childSlots() {
|
|
20578
21220
|
let slots = 0;
|
|
20579
|
-
for (const
|
|
20580
|
-
for (const auth of
|
|
20581
|
-
const child =
|
|
21221
|
+
for (const run2 of this.runs.values())
|
|
21222
|
+
for (const auth of run2.auth.values()) {
|
|
21223
|
+
const child = run2.children.get(auth.intent.childRunId);
|
|
20582
21224
|
if (child ? child.completion === null : auth.result === null || auth.result.outcome === "resolved" && !auth.result.admissionClosed)
|
|
20583
21225
|
slots++;
|
|
20584
21226
|
}
|
|
@@ -20954,10 +21596,10 @@ class HistoryWriter {
|
|
|
20954
21596
|
const checks = decodeCheckObligations(review.checks);
|
|
20955
21597
|
if (checks.taskId !== input.link.taskId || checks.revision !== fact.checksRevision || canonicalJson(review.observation) !== canonicalJson(changes.observation))
|
|
20956
21598
|
throw new ContractError("conflict");
|
|
20957
|
-
const
|
|
20958
|
-
if (!
|
|
21599
|
+
const run2 = this.retainedRun(fact.runId);
|
|
21600
|
+
if (!run2.drained || run2.admission?.context.task?.id !== input.link.taskId)
|
|
20959
21601
|
throw new ContractError("conflict");
|
|
20960
|
-
const issues = reviewIssues(
|
|
21602
|
+
const issues = reviewIssues(run2, { ...review, checks }, identity.expected.task?.kind === "code", input.link.epoch);
|
|
20961
21603
|
if (issues.length && (fact.outcome !== "reviewed-with-issues" || !fact.acceptIssues))
|
|
20962
21604
|
throw new ContractError("conflict");
|
|
20963
21605
|
}
|
|
@@ -21015,11 +21657,11 @@ class HistoryWriter {
|
|
|
21015
21657
|
throw new ContractError("conflict");
|
|
21016
21658
|
}
|
|
21017
21659
|
if (fact.kind === "gate" && fact.reservationId && fact.lane !== "check") {
|
|
21018
|
-
const
|
|
21019
|
-
if (!
|
|
21660
|
+
const run2 = value.link.runId ? this.runs.get(value.link.runId) ?? this.retainedRun(value.link.runId) : null;
|
|
21661
|
+
if (!run2?.admission)
|
|
21020
21662
|
throw new ContractError("conflict");
|
|
21021
|
-
const selected2 = this.files.readAdmission(
|
|
21022
|
-
const allocation =
|
|
21663
|
+
const selected2 = this.files.readAdmission(run2.runId, run2.admission);
|
|
21664
|
+
const allocation = run2.admission.assignments.find((a) => a.childRunId === fact.reservationId);
|
|
21023
21665
|
const assignment = selected2.graph?.assignments.find((a) => a.id === allocation?.assignmentId);
|
|
21024
21666
|
if (!allocation || (assignment?.purpose === "edit" ? "writer" : "reader") !== fact.lane)
|
|
21025
21667
|
throw new ContractError("conflict");
|
|
@@ -21123,7 +21765,7 @@ function digestHistory(files, exclude) {
|
|
|
21123
21765
|
const walk = (path, depth) => {
|
|
21124
21766
|
if (depth > 8 || !files.files.directory(path))
|
|
21125
21767
|
throw new ContractError("corrupt-selected");
|
|
21126
|
-
const directory =
|
|
21768
|
+
const directory = lstatSync14(path);
|
|
21127
21769
|
digest.update(`incarnation:${directory.dev}:${directory.ino}
|
|
21128
21770
|
`);
|
|
21129
21771
|
const dir = opendirSync8(path), names2 = [];
|
|
@@ -21137,11 +21779,11 @@ function digestHistory(files, exclude) {
|
|
|
21137
21779
|
dir.closeSync();
|
|
21138
21780
|
}
|
|
21139
21781
|
for (const name of names2.sort()) {
|
|
21140
|
-
const selected2 =
|
|
21782
|
+
const selected2 = join25(path, name);
|
|
21141
21783
|
if (selected2 === exclude)
|
|
21142
21784
|
continue;
|
|
21143
21785
|
const rel = relative7(files.directory, selected2);
|
|
21144
|
-
if (
|
|
21786
|
+
if (lstatSync14(selected2).isDirectory()) {
|
|
21145
21787
|
digest.update(`directory:${Buffer.byteLength(rel)}:${rel}
|
|
21146
21788
|
`);
|
|
21147
21789
|
walk(selected2, depth + 1);
|
|
@@ -21198,7 +21840,7 @@ function damaged(error) {
|
|
|
21198
21840
|
}
|
|
21199
21841
|
async function healHistory(lease, log) {
|
|
21200
21842
|
lease.assert();
|
|
21201
|
-
const c = lease.coordination, directory =
|
|
21843
|
+
const c = lease.coordination, directory = join25(c.directory, "history-v2");
|
|
21202
21844
|
let files;
|
|
21203
21845
|
let broken = false;
|
|
21204
21846
|
try {
|
|
@@ -21229,10 +21871,10 @@ async function healHistory(lease, log) {
|
|
|
21229
21871
|
}
|
|
21230
21872
|
|
|
21231
21873
|
// src/subagent-process.ts
|
|
21232
|
-
import { spawn as
|
|
21233
|
-
import { join as
|
|
21874
|
+
import { spawn as spawn3 } from "node:child_process";
|
|
21875
|
+
import { join as join26 } from "node:path";
|
|
21234
21876
|
import { StringDecoder } from "node:string_decoder";
|
|
21235
|
-
import { fileURLToPath as
|
|
21877
|
+
import { fileURLToPath as fileURLToPath5 } from "node:url";
|
|
21236
21878
|
|
|
21237
21879
|
// src/conversation-context.mjs
|
|
21238
21880
|
var CONTEXT_BYTES = 2 * 1024 * 1024;
|
|
@@ -21838,11 +22480,11 @@ class WorkerRecording {
|
|
|
21838
22480
|
try {
|
|
21839
22481
|
await Promise.race([
|
|
21840
22482
|
...this.pending,
|
|
21841
|
-
new Promise((
|
|
21842
|
-
abort =
|
|
22483
|
+
new Promise((resolve9) => {
|
|
22484
|
+
abort = resolve9;
|
|
21843
22485
|
signal.addEventListener("abort", abort, { once: true });
|
|
21844
22486
|
if (signal.aborted)
|
|
21845
|
-
|
|
22487
|
+
resolve9();
|
|
21846
22488
|
})
|
|
21847
22489
|
]);
|
|
21848
22490
|
} finally {
|
|
@@ -21870,11 +22512,11 @@ class WorkerRecording {
|
|
|
21870
22512
|
let timer;
|
|
21871
22513
|
await Promise.race([
|
|
21872
22514
|
Promise.all([...this.pending]),
|
|
21873
|
-
new Promise((
|
|
22515
|
+
new Promise((resolve9) => {
|
|
21874
22516
|
timer = setTimeout(() => {
|
|
21875
22517
|
this.failure = "uncertain";
|
|
21876
22518
|
this.stop();
|
|
21877
|
-
|
|
22519
|
+
resolve9();
|
|
21878
22520
|
}, LIMITS.recordingStallMs);
|
|
21879
22521
|
})
|
|
21880
22522
|
]);
|
|
@@ -21904,7 +22546,7 @@ class WorkerRecording {
|
|
|
21904
22546
|
// src/subagent-process.ts
|
|
21905
22547
|
function terminateWindowsTree(pid, fallback) {
|
|
21906
22548
|
const root = process.env.SystemRoot;
|
|
21907
|
-
const taskkill =
|
|
22549
|
+
const taskkill = spawn3(root ? join26(root, "System32", "taskkill.exe") : "taskkill.exe", ["/F", "/T", "/PID", String(pid)], { stdio: "ignore", windowsHide: true });
|
|
21908
22550
|
taskkill.once("error", fallback);
|
|
21909
22551
|
taskkill.once("close", (code2) => {
|
|
21910
22552
|
if (code2 !== 0)
|
|
@@ -21942,8 +22584,8 @@ function runProcess(spec) {
|
|
|
21942
22584
|
throw new Error("Invalid service check context");
|
|
21943
22585
|
if (spec.input !== undefined && Buffer.byteLength(spec.input) > 128 * 1024)
|
|
21944
22586
|
return Promise.resolve(early("failed", "Worker input exceeds bound"));
|
|
21945
|
-
return new Promise((
|
|
21946
|
-
const child =
|
|
22587
|
+
return new Promise((resolve9) => {
|
|
22588
|
+
const child = spawn3(spec.command, spec.args, {
|
|
21947
22589
|
cwd: spec.cwd,
|
|
21948
22590
|
env: spec.env ?? safeChildEnv(),
|
|
21949
22591
|
detached: process.platform !== "win32",
|
|
@@ -22032,7 +22674,7 @@ function runProcess(spec) {
|
|
|
22032
22674
|
omissions: ["gap"]
|
|
22033
22675
|
}
|
|
22034
22676
|
}, capture);
|
|
22035
|
-
|
|
22677
|
+
resolve9({
|
|
22036
22678
|
outcome,
|
|
22037
22679
|
closed: closed2,
|
|
22038
22680
|
exitCode,
|
|
@@ -22380,7 +23022,7 @@ function launchEncoded(encoded2, signal, timeoutMs, extra = {}) {
|
|
|
22380
23022
|
throw new Error("network");
|
|
22381
23023
|
return runProcess({
|
|
22382
23024
|
command: process.execPath,
|
|
22383
|
-
args: [
|
|
23025
|
+
args: [fileURLToPath5(new URL("./sdk-worker.mjs", import.meta.url))],
|
|
22384
23026
|
cwd: encoded2.material.cwd,
|
|
22385
23027
|
input: encoded2.input,
|
|
22386
23028
|
env: encoded2.selected.env,
|
|
@@ -22562,7 +23204,7 @@ class CheckoutGate {
|
|
|
22562
23204
|
throw new ContractError("conflict");
|
|
22563
23205
|
if (this.waiting.length >= CHECKOUT_WAITERS)
|
|
22564
23206
|
throw new ContractError("capacity");
|
|
22565
|
-
const result = new Promise((
|
|
23207
|
+
const result = new Promise((resolve9, reject) => {
|
|
22566
23208
|
const abort = () => {
|
|
22567
23209
|
const at = this.waiting.indexOf(waiter);
|
|
22568
23210
|
if (at >= 0)
|
|
@@ -22572,7 +23214,7 @@ class CheckoutGate {
|
|
|
22572
23214
|
id,
|
|
22573
23215
|
resolve: (permit) => {
|
|
22574
23216
|
signal.removeEventListener("abort", abort);
|
|
22575
|
-
|
|
23217
|
+
resolve9(permit);
|
|
22576
23218
|
},
|
|
22577
23219
|
reject: (error) => {
|
|
22578
23220
|
signal.removeEventListener("abort", abort);
|
|
@@ -22704,8 +23346,8 @@ class OwnerModelChoice {
|
|
|
22704
23346
|
}
|
|
22705
23347
|
|
|
22706
23348
|
// src/service/model-config.ts
|
|
22707
|
-
import { lstatSync as
|
|
22708
|
-
import { dirname as
|
|
23349
|
+
import { lstatSync as lstatSync15, realpathSync as realpathSync9 } from "node:fs";
|
|
23350
|
+
import { dirname as dirname18, isAbsolute as isAbsolute9, join as join27, resolve as resolve9 } from "node:path";
|
|
22709
23351
|
var CONFIG_BYTES2 = 1024 * 1024;
|
|
22710
23352
|
var ENTRY_LIMIT = 256;
|
|
22711
23353
|
function refuse2() {
|
|
@@ -22770,16 +23412,16 @@ function captureEnvironment(value) {
|
|
|
22770
23412
|
return Object.freeze(out);
|
|
22771
23413
|
}
|
|
22772
23414
|
function piSource(agentDir, home, cwd, env) {
|
|
22773
|
-
if (!
|
|
23415
|
+
if (!isAbsolute9(home) || !isAbsolute9(cwd))
|
|
22774
23416
|
refuse2();
|
|
22775
|
-
let selected2 = agentDir ?? env.PI_CODING_AGENT_DIR ??
|
|
23417
|
+
let selected2 = agentDir ?? env.PI_CODING_AGENT_DIR ?? join27(home, ".pi", "agent");
|
|
22776
23418
|
if (!selected2 || selected2.includes("\x00"))
|
|
22777
23419
|
refuse2();
|
|
22778
23420
|
if (selected2 === "~" || selected2.startsWith("~/"))
|
|
22779
23421
|
selected2 = home + selected2.slice(1);
|
|
22780
|
-
selected2 =
|
|
22781
|
-
const directory =
|
|
22782
|
-
if (!
|
|
23422
|
+
selected2 = resolve9(cwd, selected2);
|
|
23423
|
+
const directory = join27(realpathSync9(dirname18(selected2)), selected2.slice(dirname18(selected2).length + 1));
|
|
23424
|
+
if (!lstatSync15(directory).isDirectory() || lstatSync15(directory).isSymbolicLink())
|
|
22783
23425
|
refuse2();
|
|
22784
23426
|
const files = new PrivateFiles(directory, false);
|
|
22785
23427
|
if (!files.directory(directory))
|
|
@@ -22787,8 +23429,8 @@ function piSource(agentDir, home, cwd, env) {
|
|
|
22787
23429
|
return {
|
|
22788
23430
|
directory,
|
|
22789
23431
|
files,
|
|
22790
|
-
authPath:
|
|
22791
|
-
modelsPath:
|
|
23432
|
+
authPath: join27(directory, "auth.json"),
|
|
23433
|
+
modelsPath: join27(directory, "models.json")
|
|
22792
23434
|
};
|
|
22793
23435
|
}
|
|
22794
23436
|
function templateNames(value) {
|
|
@@ -23736,10 +24378,10 @@ class OwnerModelContext {
|
|
|
23736
24378
|
closed2 = true;
|
|
23737
24379
|
};
|
|
23738
24380
|
let reportClosed;
|
|
23739
|
-
const closedWait = new Promise((
|
|
24381
|
+
const closedWait = new Promise((resolve10) => {
|
|
23740
24382
|
reportClosed = () => {
|
|
23741
24383
|
close();
|
|
23742
|
-
|
|
24384
|
+
resolve10(refusal2(new ContractError("auth-unavailable"), effectStarted ? "uncertain" : "none"));
|
|
23743
24385
|
};
|
|
23744
24386
|
});
|
|
23745
24387
|
input.signal.addEventListener("abort", reportClosed, { once: true });
|
|
@@ -24173,8 +24815,8 @@ class ServiceOwner {
|
|
|
24173
24815
|
if (!this.attempt) {
|
|
24174
24816
|
this.reportingExpired = false;
|
|
24175
24817
|
let complete;
|
|
24176
|
-
this.attempt = new Promise((
|
|
24177
|
-
complete =
|
|
24818
|
+
this.attempt = new Promise((resolve10) => {
|
|
24819
|
+
complete = resolve10;
|
|
24178
24820
|
});
|
|
24179
24821
|
const finish = () => {
|
|
24180
24822
|
this.attempt = undefined;
|
|
@@ -24192,11 +24834,11 @@ class ServiceOwner {
|
|
|
24192
24834
|
try {
|
|
24193
24835
|
await Promise.race([
|
|
24194
24836
|
this.attempt,
|
|
24195
|
-
new Promise((
|
|
24837
|
+
new Promise((resolve10) => {
|
|
24196
24838
|
timer = setTimeout(() => {
|
|
24197
24839
|
this.reportingExpired = true;
|
|
24198
24840
|
this.persistPhase("quarantined");
|
|
24199
|
-
|
|
24841
|
+
resolve10();
|
|
24200
24842
|
}, waitMs);
|
|
24201
24843
|
})
|
|
24202
24844
|
]);
|
|
@@ -24227,12 +24869,12 @@ class ServiceOwner {
|
|
|
24227
24869
|
|
|
24228
24870
|
// src/service/workbench.ts
|
|
24229
24871
|
import { opendirSync as opendirSync14 } from "node:fs";
|
|
24230
|
-
import { join as
|
|
24872
|
+
import { join as join33 } from "node:path";
|
|
24231
24873
|
|
|
24232
24874
|
// src/service/conversation-workflow.ts
|
|
24233
24875
|
import { randomUUID as randomUUID21 } from "node:crypto";
|
|
24234
24876
|
import { opendirSync as opendirSync11 } from "node:fs";
|
|
24235
|
-
import { dirname as
|
|
24877
|
+
import { dirname as dirname20, join as join29 } from "node:path";
|
|
24236
24878
|
|
|
24237
24879
|
// src/cadence-instructions.ts
|
|
24238
24880
|
var CADENCE_INSTRUCTIONS = `CADENCE CONVERSATION MODE — stays enabled for this entire conversation, including after a goal completes, until the user explicitly exits by typing 退出 Cadence or exit cadence. Only the user can exit; leaving never cancels accepted work. When asked how to leave, say exactly this.
|
|
@@ -24244,12 +24886,12 @@ During accepted work answer questions, report actual retained progress, and let
|
|
|
24244
24886
|
|
|
24245
24887
|
// src/service/conversations.ts
|
|
24246
24888
|
import { randomUUID as randomUUID20 } from "node:crypto";
|
|
24247
|
-
import { lstatSync as
|
|
24248
|
-
import { dirname as
|
|
24889
|
+
import { lstatSync as lstatSync17, opendirSync as opendirSync10, realpathSync as realpathSync10, statfsSync as statfsSync2 } from "node:fs";
|
|
24890
|
+
import { dirname as dirname19, join as join28 } from "node:path";
|
|
24249
24891
|
|
|
24250
24892
|
// src/service/conversation-history.ts
|
|
24251
24893
|
import { randomUUID as randomUUID19 } from "node:crypto";
|
|
24252
|
-
import { lstatSync as
|
|
24894
|
+
import { lstatSync as lstatSync16, opendirSync as opendirSync9 } from "node:fs";
|
|
24253
24895
|
var CONVERSATION_PAGE = 50;
|
|
24254
24896
|
var SCAN_ENTRIES = 8;
|
|
24255
24897
|
var SCANS = 16;
|
|
@@ -24268,7 +24910,7 @@ class ConversationHistory {
|
|
|
24268
24910
|
this.summary = summary2;
|
|
24269
24911
|
}
|
|
24270
24912
|
incarnation() {
|
|
24271
|
-
const st =
|
|
24913
|
+
const st = lstatSync16(this.path, { bigint: true });
|
|
24272
24914
|
if (!st.isDirectory() || st.isSymbolicLink())
|
|
24273
24915
|
throw new ContractError("corrupt-selected");
|
|
24274
24916
|
return `${st.dev}:${st.ino}`;
|
|
@@ -24362,24 +25004,110 @@ class ConversationHistory {
|
|
|
24362
25004
|
}
|
|
24363
25005
|
}
|
|
24364
25006
|
|
|
25007
|
+
// src/service/conversation-steps.ts
|
|
25008
|
+
var TARGET_ARGS = 1024 * 1024;
|
|
25009
|
+
var INDEXED_TURNS = 32;
|
|
25010
|
+
function itemStep(item) {
|
|
25011
|
+
const step = chatStep(item, 0);
|
|
25012
|
+
const more = step.clipped || item.role === "tool" && !!(item.args || item.text);
|
|
25013
|
+
return { ...step, parts: more ? 1 : 0 };
|
|
25014
|
+
}
|
|
25015
|
+
|
|
25016
|
+
class ConversationSteps {
|
|
25017
|
+
page;
|
|
25018
|
+
indexes = new Map;
|
|
25019
|
+
constructor(page) {
|
|
25020
|
+
this.page = page;
|
|
25021
|
+
}
|
|
25022
|
+
list(turn) {
|
|
25023
|
+
const settled = !["queued", "running"].includes(turn.status);
|
|
25024
|
+
if (!turn.transcript) {
|
|
25025
|
+
const steps2 = turn.items.map((item, index2) => ({
|
|
25026
|
+
step: itemStep(item),
|
|
25027
|
+
source: { kind: "item", index: index2 }
|
|
25028
|
+
}));
|
|
25029
|
+
return { steps: steps2, stable: settled ? steps2.length : 0 };
|
|
25030
|
+
}
|
|
25031
|
+
const index = this.index(turn.id, turn.transcript.pages);
|
|
25032
|
+
const steps = [
|
|
25033
|
+
...index.steps,
|
|
25034
|
+
...turn.items.filter((item) => !index.ids.has(item.id)).map((item) => ({
|
|
25035
|
+
step: itemStep(item),
|
|
25036
|
+
source: { kind: "preview", id: item.id }
|
|
25037
|
+
}))
|
|
25038
|
+
];
|
|
25039
|
+
return { steps, stable: settled ? steps.length : index.steps.length };
|
|
25040
|
+
}
|
|
25041
|
+
detail(turn, step, part) {
|
|
25042
|
+
const listed = this.list(turn).steps[step];
|
|
25043
|
+
if (!listed || part >= listed.step.parts)
|
|
25044
|
+
throw new ContractError("stale");
|
|
25045
|
+
const source = listed.source;
|
|
25046
|
+
if (source.kind === "pages")
|
|
25047
|
+
return this.page(turn.id, source.first + part);
|
|
25048
|
+
const item = source.kind === "item" ? turn.items[source.index] : turn.items.find((i) => i.id === source.id);
|
|
25049
|
+
if (!item)
|
|
25050
|
+
throw new ContractError("stale");
|
|
25051
|
+
return item;
|
|
25052
|
+
}
|
|
25053
|
+
index(turnId, pages) {
|
|
25054
|
+
let index = this.indexes.get(turnId);
|
|
25055
|
+
this.indexes.delete(turnId);
|
|
25056
|
+
if (!index || index.pages > pages)
|
|
25057
|
+
index = { pages: 0, steps: [], ids: new Set, last: null };
|
|
25058
|
+
for (;index.pages < pages; index.pages++) {
|
|
25059
|
+
const fragment = this.page(turnId, index.pages);
|
|
25060
|
+
const last = index.last;
|
|
25061
|
+
const tail = index.steps.at(-1);
|
|
25062
|
+
if (last && tail && last.item.id === fragment.id) {
|
|
25063
|
+
const text2 = last.item.text + fragment.text, head2 = clipText(text2, STEP_TEXT_BYTES);
|
|
25064
|
+
last.more ||= head2.length < text2.length;
|
|
25065
|
+
last.args = last.args !== null && last.args.length + fragment.args.length <= TARGET_ARGS ? last.args + fragment.args : null;
|
|
25066
|
+
last.item = { ...fragment, text: head2, args: last.args ?? "" };
|
|
25067
|
+
tail.step = chatStep(last.item, tail.step.parts + 1, last.more);
|
|
25068
|
+
continue;
|
|
25069
|
+
}
|
|
25070
|
+
const head = clipText(fragment.text, STEP_TEXT_BYTES);
|
|
25071
|
+
index.last = {
|
|
25072
|
+
item: { ...fragment, text: head },
|
|
25073
|
+
more: head.length < fragment.text.length,
|
|
25074
|
+
args: fragment.args
|
|
25075
|
+
};
|
|
25076
|
+
index.steps.push({
|
|
25077
|
+
step: chatStep(index.last.item, 1, index.last.more),
|
|
25078
|
+
source: { kind: "pages", first: index.pages }
|
|
25079
|
+
});
|
|
25080
|
+
index.ids.add(fragment.id);
|
|
25081
|
+
}
|
|
25082
|
+
this.indexes.set(turnId, index);
|
|
25083
|
+
if (this.indexes.size > INDEXED_TURNS)
|
|
25084
|
+
this.indexes.delete(this.indexes.keys().next().value);
|
|
25085
|
+
return index;
|
|
25086
|
+
}
|
|
25087
|
+
}
|
|
25088
|
+
|
|
24365
25089
|
// src/service/conversation-transcript.ts
|
|
24366
25090
|
var TRANSCRIPT_PAGE_BYTES = 384 * 1024;
|
|
24367
25091
|
var PREVIEW_BYTES = 300 * 1024;
|
|
24368
25092
|
var FIELD_CHARS = 16 * 1024;
|
|
24369
25093
|
var PREVIEW_MARK = `
|
|
24370
|
-
[Preview only;
|
|
25094
|
+
[Preview only; the rest appears once this step completes.]`;
|
|
24371
25095
|
function part(value, offset, chars = FIELD_CHARS) {
|
|
24372
25096
|
let end = Math.min(value.length, offset + chars);
|
|
24373
25097
|
if (end < value.length && /[\uD800-\uDBFF]/u.test(value[end - 1]))
|
|
24374
25098
|
end--;
|
|
24375
25099
|
return value.slice(offset, end);
|
|
24376
25100
|
}
|
|
25101
|
+
function previewHead(head, added) {
|
|
25102
|
+
return head.length > FIELD_CHARS ? head : head + added.slice(0, FIELD_CHARS + 1 - head.length);
|
|
25103
|
+
}
|
|
24377
25104
|
|
|
24378
25105
|
class ConversationTranscript {
|
|
24379
25106
|
turn;
|
|
24380
25107
|
write;
|
|
24381
25108
|
state = { pages: 0 };
|
|
24382
25109
|
workingSizes = new Map;
|
|
25110
|
+
fieldBytes = new Map;
|
|
24383
25111
|
workingBytes = 0;
|
|
24384
25112
|
sizes = new Map;
|
|
24385
25113
|
bytes = 4096;
|
|
@@ -24407,19 +25135,28 @@ class ConversationTranscript {
|
|
|
24407
25135
|
find(id) {
|
|
24408
25136
|
return this.pending.get(id);
|
|
24409
25137
|
}
|
|
24410
|
-
preview(item) {
|
|
25138
|
+
preview(item, known = {}) {
|
|
24411
25139
|
if (this.pending.has(item.id)) {
|
|
24412
|
-
const
|
|
25140
|
+
const cached = this.fieldBytes.get(item.id);
|
|
25141
|
+
const bytes = (field) => known[field]?.bytes ?? cached?.[field] ?? Buffer.byteLength(item[field]);
|
|
25142
|
+
const fields2 = {
|
|
25143
|
+
text: bytes("text"),
|
|
25144
|
+
args: bytes("args"),
|
|
25145
|
+
name: bytes("name")
|
|
25146
|
+
};
|
|
25147
|
+
this.fieldBytes.set(item.id, fields2);
|
|
25148
|
+
const size3 = fields2.text + fields2.args + fields2.name;
|
|
24413
25149
|
this.workingBytes += size3 - (this.workingSizes.get(item.id) ?? 0);
|
|
24414
25150
|
this.workingSizes.set(item.id, size3);
|
|
24415
25151
|
if (this.workingBytes > 2 * LIMITS.detailBytes)
|
|
24416
25152
|
throw new ContractError("capacity");
|
|
24417
25153
|
}
|
|
24418
|
-
const clip = (
|
|
24419
|
-
const
|
|
25154
|
+
const clip = (field) => {
|
|
25155
|
+
const value = item[field];
|
|
25156
|
+
const prefix = part(known[field]?.head ?? value, 0);
|
|
24420
25157
|
return prefix.length < value.length ? prefix + PREVIEW_MARK : value;
|
|
24421
25158
|
};
|
|
24422
|
-
const preview = { ...item, text: clip(
|
|
25159
|
+
const preview = { ...item, text: clip("text"), args: clip("args") };
|
|
24423
25160
|
const size2 = Buffer.byteLength(canonicalJson(preview, PREVIEW_BYTES));
|
|
24424
25161
|
this.bytes += size2 - (this.sizes.get(item.id) ?? 0);
|
|
24425
25162
|
this.sizes.set(item.id, size2);
|
|
@@ -24443,6 +25180,7 @@ class ConversationTranscript {
|
|
|
24443
25180
|
this.preview(item);
|
|
24444
25181
|
this.archive(item);
|
|
24445
25182
|
this.pending.delete(id);
|
|
25183
|
+
this.fieldBytes.delete(id);
|
|
24446
25184
|
this.workingBytes -= this.workingSizes.get(id) ?? 0;
|
|
24447
25185
|
this.workingSizes.delete(id);
|
|
24448
25186
|
}
|
|
@@ -24490,7 +25228,7 @@ var OMITTED_FLAG = {
|
|
|
24490
25228
|
};
|
|
24491
25229
|
function observedFile(path) {
|
|
24492
25230
|
try {
|
|
24493
|
-
const s =
|
|
25231
|
+
const s = lstatSync17(path);
|
|
24494
25232
|
return [s.dev, s.ino, s.size, s.mode, s.nlink, s.ctimeMs, s.mtimeMs].join(":");
|
|
24495
25233
|
} catch (error) {
|
|
24496
25234
|
if (error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
|
|
@@ -24531,17 +25269,18 @@ class ConversationCapture {
|
|
|
24531
25269
|
}
|
|
24532
25270
|
return item;
|
|
24533
25271
|
}
|
|
24534
|
-
update(item, field, value, maximum) {
|
|
25272
|
+
update(item, field, value, maximum, known = { bytes: Buffer.byteLength(value), head: value }) {
|
|
25273
|
+
const bytes = known.bytes;
|
|
24535
25274
|
if (this.transcript) {
|
|
24536
|
-
if (
|
|
25275
|
+
if (bytes > LIMITS.detailBytes)
|
|
24537
25276
|
throw new ContractError("capacity");
|
|
24538
25277
|
item[field] = value;
|
|
24539
|
-
this.transcript.preview(item);
|
|
25278
|
+
this.transcript.preview(item, { [field]: known });
|
|
24540
25279
|
return;
|
|
24541
25280
|
}
|
|
24542
25281
|
const before = item[field];
|
|
24543
25282
|
try {
|
|
24544
|
-
if (
|
|
25283
|
+
if (bytes > maximum)
|
|
24545
25284
|
throw new ContractError("capacity");
|
|
24546
25285
|
item[field] = value;
|
|
24547
25286
|
canonicalJson(this.turn, TURN_BYTES - 2048);
|
|
@@ -24561,14 +25300,19 @@ class ConversationCapture {
|
|
|
24561
25300
|
this.details.set(e.detailId, {
|
|
24562
25301
|
descriptor: e.descriptor,
|
|
24563
25302
|
sanitizer: new DetailSanitizer(e.descriptor.encoding, this.secrets()),
|
|
24564
|
-
text: ""
|
|
25303
|
+
text: "",
|
|
25304
|
+
bytes: 0,
|
|
25305
|
+
head: ""
|
|
24565
25306
|
});
|
|
24566
25307
|
} else if (e.kind === "detail-chunk" || e.kind === "detail-end") {
|
|
24567
25308
|
const d = this.details.get(e.detailId);
|
|
24568
25309
|
if (!d)
|
|
24569
25310
|
return;
|
|
24570
|
-
|
|
24571
|
-
|
|
25311
|
+
const added = e.kind === "detail-chunk" ? d.sanitizer.write(e.text) : d.sanitizer.end();
|
|
25312
|
+
d.text += added;
|
|
25313
|
+
d.bytes += Buffer.byteLength(added);
|
|
25314
|
+
d.head = previewHead(d.head, added);
|
|
25315
|
+
if (d.bytes > (this.transcript ? LIMITS.detailBytes : 256 * 1024))
|
|
24572
25316
|
throw new ContractError("capacity");
|
|
24573
25317
|
const o = d.descriptor.owner;
|
|
24574
25318
|
if (o.kind === "message") {
|
|
@@ -24576,20 +25320,26 @@ class ConversationCapture {
|
|
|
24576
25320
|
if (role2 === "assistant" && o.stage === "stream") {
|
|
24577
25321
|
this.messageText.set(o.blockId, {
|
|
24578
25322
|
messageId: o.messageId,
|
|
24579
|
-
text: d.text
|
|
25323
|
+
text: d.text,
|
|
25324
|
+
bytes: d.bytes,
|
|
25325
|
+
head: d.head
|
|
25326
|
+
});
|
|
25327
|
+
const blocks = [...this.messageText.values()].filter((block) => block.messageId === o.messageId);
|
|
25328
|
+
this.update(this.item(o.messageId, "assistant"), "text", blocks.length === 1 ? blocks[0].text : blocks.map((block) => block.text).join(""), 256 * 1024, {
|
|
25329
|
+
bytes: blocks.reduce((size2, block) => size2 + block.bytes, 0),
|
|
25330
|
+
head: blocks.reduce((head, block) => previewHead(head, block.head), "")
|
|
24580
25331
|
});
|
|
24581
|
-
this.update(this.item(o.messageId, "assistant"), "text", [...this.messageText.values()].filter((block) => block.messageId === o.messageId).map((block) => block.text).join(""), 256 * 1024);
|
|
24582
25332
|
}
|
|
24583
25333
|
} else if (o.kind === "message-final") {
|
|
24584
25334
|
const item = this.transcript?.find(o.messageId) ?? this.turn.items.find((i) => i.id === o.messageId);
|
|
24585
25335
|
if (item?.role === "assistant")
|
|
24586
|
-
this.update(item, "text", d.text, 256 * 1024);
|
|
25336
|
+
this.update(item, "text", d.text, 256 * 1024, d);
|
|
24587
25337
|
} else if (o.kind === "tool") {
|
|
24588
25338
|
const item = this.item(o.callId, "tool");
|
|
24589
25339
|
if (o.stage === "execution-arguments" || o.stage === "arguments-final")
|
|
24590
|
-
this.update(item, "args", d.text, 64 * 1024);
|
|
25340
|
+
this.update(item, "args", d.text, 64 * 1024, d);
|
|
24591
25341
|
if (o.stage === "result" || o.stage === "update-snapshot")
|
|
24592
|
-
this.update(item, "text", d.text, 256 * 1024);
|
|
25342
|
+
this.update(item, "text", d.text, 256 * 1024, d);
|
|
24593
25343
|
}
|
|
24594
25344
|
if (e.kind === "detail-end")
|
|
24595
25345
|
this.details.delete(e.detailId);
|
|
@@ -24604,7 +25354,7 @@ class ConversationCapture {
|
|
|
24604
25354
|
this.turn.notice = `worker-failure: ${e.code}`;
|
|
24605
25355
|
else if (e.kind === "omitted-block" && e.block.owner.kind !== "context" && e.block.owner.kind !== "handoff")
|
|
24606
25356
|
this.turn.omitted[OMITTED_FLAG[e.block.reason]] = true;
|
|
24607
|
-
if ([...this.details.values()].reduce((size2, detail) => size2 +
|
|
25357
|
+
if ([...this.details.values()].reduce((size2, detail) => size2 + detail.bytes + detail.sanitizer.retainedBytes, 0) > (this.transcript ? 2 * LIMITS.detailBytes : TURN_BYTES))
|
|
24608
25358
|
throw new ContractError("capacity");
|
|
24609
25359
|
if (!this.transcript)
|
|
24610
25360
|
canonicalJson(this.turn, TURN_BYTES);
|
|
@@ -24627,11 +25377,12 @@ class Conversations {
|
|
|
24627
25377
|
cwd;
|
|
24628
25378
|
active = new Map;
|
|
24629
25379
|
summaries = new Map;
|
|
25380
|
+
steps = new ConversationSteps((turnId, page) => this.page(turnId, page));
|
|
24630
25381
|
constructor(o) {
|
|
24631
25382
|
this.o = o;
|
|
24632
|
-
this.root =
|
|
24633
|
-
this.cwd =
|
|
24634
|
-
const sessions =
|
|
25383
|
+
this.root = join28(o.coordination.directory, "conversations-v1");
|
|
25384
|
+
this.cwd = realpathSync10(o.cwd);
|
|
25385
|
+
const sessions = join28(this.root, "sessions");
|
|
24635
25386
|
this.history = new ConversationHistory(sessions, () => {
|
|
24636
25387
|
this.o.owner.lease.assert();
|
|
24637
25388
|
return this.files.directory(sessions);
|
|
@@ -24645,13 +25396,13 @@ class Conversations {
|
|
|
24645
25396
|
return this.o.coordination.files;
|
|
24646
25397
|
}
|
|
24647
25398
|
path(id, name) {
|
|
24648
|
-
return
|
|
25399
|
+
return join28(this.root, "turns", decodeId(id), name);
|
|
24649
25400
|
}
|
|
24650
25401
|
identityPath(id) {
|
|
24651
|
-
return
|
|
25402
|
+
return join28(this.root, "identities", `${decodeId(id)}.json`);
|
|
24652
25403
|
}
|
|
24653
25404
|
metadataPath(id) {
|
|
24654
|
-
return
|
|
25405
|
+
return join28(this.root, "sessions", `${decodeId(id)}.json`);
|
|
24655
25406
|
}
|
|
24656
25407
|
read(path, max) {
|
|
24657
25408
|
const value = this.files.read(path, max);
|
|
@@ -24704,7 +25455,7 @@ class Conversations {
|
|
|
24704
25455
|
return meta;
|
|
24705
25456
|
}
|
|
24706
25457
|
slotPath(id, ordinal) {
|
|
24707
|
-
return
|
|
25458
|
+
return join28(this.root, "indices", decodeId(id), `${integer(ordinal, 0)}.json`);
|
|
24708
25459
|
}
|
|
24709
25460
|
turnAt(meta, ordinal) {
|
|
24710
25461
|
if (ordinal < 0 || ordinal >= meta.count)
|
|
@@ -24750,7 +25501,7 @@ class Conversations {
|
|
|
24750
25501
|
} : turn;
|
|
24751
25502
|
}
|
|
24752
25503
|
controlBound(id) {
|
|
24753
|
-
const path =
|
|
25504
|
+
const path = join28(this.root, "control-links", `${decodeId(id)}.json`);
|
|
24754
25505
|
const value = this.read(path, META_BYTES);
|
|
24755
25506
|
if (value === undefined)
|
|
24756
25507
|
return false;
|
|
@@ -24760,11 +25511,11 @@ class Conversations {
|
|
|
24760
25511
|
return true;
|
|
24761
25512
|
}
|
|
24762
25513
|
controlKey(id) {
|
|
24763
|
-
return observedFile(
|
|
25514
|
+
return observedFile(join28(this.root, "control-links", `${decodeId(id)}.json`));
|
|
24764
25515
|
}
|
|
24765
25516
|
bindControl(id) {
|
|
24766
25517
|
if (!this.controlBound(id))
|
|
24767
|
-
this.write(
|
|
25518
|
+
this.write(join28(this.root, "control-links", `${decodeId(id)}.json`), { schema: "cadence-conversation-control/1", conversationId: id }, META_BYTES);
|
|
24768
25519
|
}
|
|
24769
25520
|
hasTurn(id, turnId) {
|
|
24770
25521
|
const meta = this.metadata(id);
|
|
@@ -24793,10 +25544,13 @@ class Conversations {
|
|
|
24793
25544
|
].map(observedFile).join("|");
|
|
24794
25545
|
}
|
|
24795
25546
|
retainedSummary(id) {
|
|
25547
|
+
return this.retained(id).summary;
|
|
25548
|
+
}
|
|
25549
|
+
retained(id) {
|
|
24796
25550
|
const base = [this.metadataPath(id), this.identityPath(id)].map(observedFile).join("|");
|
|
24797
25551
|
const hit = this.summaries.get(id);
|
|
24798
25552
|
if (hit && !this.active.has(hit.summary.head) && hit.key === `${base}|${this.headKey(hit.summary.head, id, hit.count)}`)
|
|
24799
|
-
return { ...hit.summary };
|
|
25553
|
+
return { summary: { ...hit.summary }, count: hit.count };
|
|
24800
25554
|
const meta = this.metadata(id);
|
|
24801
25555
|
if (!meta)
|
|
24802
25556
|
throw new ContractError("corrupt-selected");
|
|
@@ -24813,7 +25567,7 @@ class Conversations {
|
|
|
24813
25567
|
count: meta.count
|
|
24814
25568
|
});
|
|
24815
25569
|
}
|
|
24816
|
-
return summary2;
|
|
25570
|
+
return { summary: summary2, count: meta.count };
|
|
24817
25571
|
}
|
|
24818
25572
|
liveSummary(live2) {
|
|
24819
25573
|
return {
|
|
@@ -24840,7 +25594,7 @@ class Conversations {
|
|
|
24840
25594
|
}
|
|
24841
25595
|
assertSettled() {
|
|
24842
25596
|
this.o.owner.lease.assert();
|
|
24843
|
-
const path =
|
|
25597
|
+
const path = join28(this.root, "sessions");
|
|
24844
25598
|
if (!this.files.directory(path))
|
|
24845
25599
|
return;
|
|
24846
25600
|
const dir = opendirSync10(path);
|
|
@@ -24855,68 +25609,74 @@ class Conversations {
|
|
|
24855
25609
|
dir.closeSync();
|
|
24856
25610
|
}
|
|
24857
25611
|
}
|
|
24858
|
-
|
|
25612
|
+
conversation(id) {
|
|
24859
25613
|
this.o.owner.lease.assert();
|
|
24860
|
-
|
|
24861
|
-
|
|
24862
|
-
const live2 = before === null ? [...this.active.values()].find((turn) => turn.conversationId === id) : undefined;
|
|
25614
|
+
if (observedFile(this.metadataPath(id)) === "absent") {
|
|
25615
|
+
const live2 = [...this.active.values()].find((turn) => turn.conversationId === id);
|
|
24863
25616
|
return {
|
|
24864
25617
|
kind: "conversation",
|
|
24865
25618
|
summary: live2 ? this.liveSummary(live2) : null,
|
|
24866
|
-
turns:
|
|
24867
|
-
{
|
|
24868
|
-
...structuredClone(live2.turn),
|
|
24869
|
-
notice: [
|
|
24870
|
-
"Current metadata unavailable; showing the active turn only. No state was reconstructed.",
|
|
24871
|
-
live2.turn.notice
|
|
24872
|
-
].filter(Boolean).join(`
|
|
24873
|
-
`)
|
|
24874
|
-
}
|
|
24875
|
-
] : [],
|
|
24876
|
-
before: null
|
|
25619
|
+
turns: 0
|
|
24877
25620
|
};
|
|
24878
25621
|
}
|
|
24879
|
-
|
|
24880
|
-
|
|
25622
|
+
const { summary: summary2, count: count2 } = this.retained(id);
|
|
25623
|
+
return { kind: "conversation", summary: summary2, turns: count2 };
|
|
25624
|
+
}
|
|
25625
|
+
turnSteps(id, ordinal, before) {
|
|
25626
|
+
this.o.owner.lease.assert();
|
|
25627
|
+
const meta = this.metadata(id);
|
|
25628
|
+
if (!meta)
|
|
24881
25629
|
throw new ContractError("stale");
|
|
24882
|
-
const
|
|
24883
|
-
|
|
24884
|
-
|
|
24885
|
-
|
|
24886
|
-
|
|
24887
|
-
|
|
24888
|
-
bytes += size2;
|
|
24889
|
-
turns.unshift(structuredClone(turn));
|
|
24890
|
-
--end;
|
|
24891
|
-
}
|
|
25630
|
+
const turn = this.turn(this.turnAt(meta, ordinal));
|
|
25631
|
+
const { steps, stable } = this.steps.list(turn);
|
|
25632
|
+
const end = before ?? steps.length;
|
|
25633
|
+
if (end > steps.length)
|
|
25634
|
+
throw new ContractError("stale");
|
|
25635
|
+
const start = Math.max(0, end - STEP_READ);
|
|
24892
25636
|
return {
|
|
24893
|
-
kind: "conversation",
|
|
24894
|
-
|
|
24895
|
-
|
|
24896
|
-
|
|
25637
|
+
kind: "conversation-turn",
|
|
25638
|
+
conversationId: id,
|
|
25639
|
+
ordinal,
|
|
25640
|
+
turn: {
|
|
25641
|
+
id: turn.id,
|
|
25642
|
+
epoch: turn.epoch,
|
|
25643
|
+
status: turn.status,
|
|
25644
|
+
notice: turn.notice,
|
|
25645
|
+
omitted: { ...turn.omitted },
|
|
25646
|
+
steps: steps.length,
|
|
25647
|
+
stable
|
|
25648
|
+
},
|
|
25649
|
+
start,
|
|
25650
|
+
steps: steps.slice(start, end).map((row) => row.step)
|
|
24897
25651
|
};
|
|
24898
25652
|
}
|
|
24899
|
-
|
|
25653
|
+
step(id, turnId, step, part2) {
|
|
24900
25654
|
this.o.owner.lease.assert();
|
|
24901
25655
|
if (!this.hasTurn(id, turnId))
|
|
24902
25656
|
throw new ContractError("invalid");
|
|
24903
|
-
const turn = this.turn(turnId);
|
|
24904
|
-
if (!turn.transcript || page >= turn.transcript.pages)
|
|
24905
|
-
throw new ContractError("stale");
|
|
24906
|
-
const record2 = closed(this.read(this.path(turnId, `transcript/${page}.json`), TRANSCRIPT_PAGE_BYTES), ["turnId", "page", "items"]);
|
|
24907
|
-
if (record2.turnId !== turnId || record2.page !== page)
|
|
24908
|
-
throw new ContractError("corrupt-selected");
|
|
24909
25657
|
return {
|
|
24910
|
-
kind: "conversation-
|
|
25658
|
+
kind: "conversation-step",
|
|
24911
25659
|
conversationId: id,
|
|
24912
|
-
|
|
24913
|
-
|
|
25660
|
+
turnId,
|
|
25661
|
+
step,
|
|
25662
|
+
part: part2,
|
|
25663
|
+
item: { ...this.steps.detail(this.turn(turnId), step, part2) }
|
|
24914
25664
|
};
|
|
24915
25665
|
}
|
|
25666
|
+
page(turnId, page) {
|
|
25667
|
+
const value = this.read(this.path(turnId, `transcript/${integer(page, 0)}.json`), TRANSCRIPT_PAGE_BYTES);
|
|
25668
|
+
if (value === undefined)
|
|
25669
|
+
throw new ContractError("corrupt-selected");
|
|
25670
|
+
const record2 = closed(value, ["turnId", "page", "items"]);
|
|
25671
|
+
const items = list(record2.items, 1, decodeChatItem);
|
|
25672
|
+
if (record2.turnId !== turnId || record2.page !== page || !items.length)
|
|
25673
|
+
throw new ContractError("corrupt-selected");
|
|
25674
|
+
return items[0];
|
|
25675
|
+
}
|
|
24916
25676
|
attempted(conversationId, commandId) {
|
|
24917
25677
|
this.o.owner.lease.assert();
|
|
24918
25678
|
const meta = this.metadata(conversationId);
|
|
24919
|
-
return this.active.has(commandId) || this.files.directory(
|
|
25679
|
+
return this.active.has(commandId) || this.files.directory(dirname19(this.path(commandId, "accepted.json"))) || !!meta && this.contains(meta, commandId) || !meta && !!this.files.read(this.identityPath(conversationId), META_BYTES);
|
|
24920
25680
|
}
|
|
24921
25681
|
command(input, reply) {
|
|
24922
25682
|
return {
|
|
@@ -24940,7 +25700,7 @@ class Conversations {
|
|
|
24940
25700
|
const receipt = this.receipt(input, !!control?.reply);
|
|
24941
25701
|
if (receipt)
|
|
24942
25702
|
return receipt;
|
|
24943
|
-
if (this.files.directory(
|
|
25703
|
+
if (this.files.directory(dirname19(this.path(input.commandId, "accepted.json"))))
|
|
24944
25704
|
throw new ContractError("corrupt-selected");
|
|
24945
25705
|
if (this.o.owner.state !== "idle")
|
|
24946
25706
|
throw new ContractError("quarantine");
|
|
@@ -24998,8 +25758,8 @@ class Conversations {
|
|
|
24998
25758
|
throw new ContractError("corrupt-selected");
|
|
24999
25759
|
const abort = new AbortController;
|
|
25000
25760
|
let begin;
|
|
25001
|
-
const ready2 = new Promise((
|
|
25002
|
-
begin =
|
|
25761
|
+
const ready2 = new Promise((resolve10) => {
|
|
25762
|
+
begin = resolve10;
|
|
25003
25763
|
});
|
|
25004
25764
|
const original = ready2.then(() => this.run(command, turn, previous, previousFile, next, abort, control));
|
|
25005
25765
|
const live2 = {
|
|
@@ -25041,7 +25801,7 @@ class Conversations {
|
|
|
25041
25801
|
}
|
|
25042
25802
|
origin(taskId2) {
|
|
25043
25803
|
this.o.owner.lease.assert();
|
|
25044
|
-
const value = this.read(
|
|
25804
|
+
const value = this.read(join28(this.root, "task-links", `${decodeId(taskId2)}.json`), META_BYTES);
|
|
25045
25805
|
if (value === undefined)
|
|
25046
25806
|
return { kind: "task-origin", taskId: taskId2, conversationId: null };
|
|
25047
25807
|
const row = closed(value, ["conversationId"]);
|
|
@@ -25309,10 +26069,10 @@ class ConversationWorkflow {
|
|
|
25309
26069
|
grants = new Map;
|
|
25310
26070
|
constructor(o) {
|
|
25311
26071
|
this.o = o;
|
|
25312
|
-
this.root =
|
|
26072
|
+
this.root = join29(o.coordination.directory, "conversation-workflows-v1");
|
|
25313
26073
|
}
|
|
25314
26074
|
path(id) {
|
|
25315
|
-
return
|
|
26075
|
+
return join29(this.root, decodeId(id), "state.json");
|
|
25316
26076
|
}
|
|
25317
26077
|
read(id) {
|
|
25318
26078
|
this.o.owner.lease.assert();
|
|
@@ -25321,7 +26081,7 @@ class ConversationWorkflow {
|
|
|
25321
26081
|
const bound = this.o.conversations.controlBound(id);
|
|
25322
26082
|
const file = this.o.coordination.files.read(path, BYTES);
|
|
25323
26083
|
if (!file)
|
|
25324
|
-
return bound || this.o.coordination.files.directory(
|
|
26084
|
+
return bound || this.o.coordination.files.directory(dirname20(path)) ? unusable2(id) : { state: fresh2(id) };
|
|
25325
26085
|
const record2 = closed(parseWireJson(file.bytes.toString("utf8"), BYTES), [
|
|
25326
26086
|
"schema",
|
|
25327
26087
|
"state"
|
|
@@ -25417,7 +26177,7 @@ class ConversationWorkflow {
|
|
|
25417
26177
|
return loaded.state;
|
|
25418
26178
|
}
|
|
25419
26179
|
runLink(runId) {
|
|
25420
|
-
return
|
|
26180
|
+
return join29(this.o.coordination.directory, "conversations-v1", "workflow-links", `${decodeId(runId)}.json`);
|
|
25421
26181
|
}
|
|
25422
26182
|
links(runIds) {
|
|
25423
26183
|
this.o.owner.lease.assert();
|
|
@@ -25499,14 +26259,14 @@ class ConversationWorkflow {
|
|
|
25499
26259
|
}, current2.file);
|
|
25500
26260
|
} catch {}
|
|
25501
26261
|
}
|
|
25502
|
-
preview(
|
|
26262
|
+
preview(run2, bytes, steps) {
|
|
25503
26263
|
const clip = (value, maximum) => diagnosticText(value, maximum, this.o.secrets());
|
|
25504
26264
|
return {
|
|
25505
|
-
...
|
|
25506
|
-
goal: clip(
|
|
25507
|
-
notice: clip(
|
|
25508
|
-
stepsOmitted: Math.max(0,
|
|
25509
|
-
steps:
|
|
26265
|
+
...run2,
|
|
26266
|
+
goal: clip(run2.goal, 320),
|
|
26267
|
+
notice: clip(run2.notice, 512),
|
|
26268
|
+
stepsOmitted: Math.max(0, run2.steps.length - steps),
|
|
26269
|
+
steps: run2.steps.slice(-steps).map((s) => {
|
|
25510
26270
|
const text2 = clip(s.text, bytes);
|
|
25511
26271
|
return { ...s, text: text2, truncated: s.truncated || text2 !== s.text };
|
|
25512
26272
|
})
|
|
@@ -25703,7 +26463,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25703
26463
|
approval(c, caller) {
|
|
25704
26464
|
if (!caller)
|
|
25705
26465
|
throw new ContractError("forbidden");
|
|
25706
|
-
const evidence =
|
|
26466
|
+
const evidence = join29(this.root, decodeId(c.conversationId), "approvals", `${decodeId(c.commandId)}.json`);
|
|
25707
26467
|
let effects = true;
|
|
25708
26468
|
try {
|
|
25709
26469
|
effects = this.o.owner.state !== "idle";
|
|
@@ -25745,7 +26505,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25745
26505
|
}
|
|
25746
26506
|
decide(c, caller, evidence, recording) {
|
|
25747
26507
|
const { state, file } = this.usable(c.conversationId);
|
|
25748
|
-
const view = this.o.conversations.
|
|
26508
|
+
const view = this.o.conversations.conversation(c.conversationId);
|
|
25749
26509
|
if (!state.enabled || c.revision !== state.revision || !state.proposal || state.proposal.id !== c.proposalId || state.proposal.digest !== c.digest || view.kind !== "conversation" || view.summary?.head !== state.head || ["queued", "running", "unknown"].includes(view.summary.status) || this.active.has(c.conversationId))
|
|
25750
26510
|
throw new ContractError("stale");
|
|
25751
26511
|
const epoch = this.o.owner.lease.registration.epoch;
|
|
@@ -25829,21 +26589,21 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25829
26589
|
// src/service/workflows.ts
|
|
25830
26590
|
import { randomUUID as randomUUID23 } from "node:crypto";
|
|
25831
26591
|
import {
|
|
25832
|
-
lstatSync as
|
|
26592
|
+
lstatSync as lstatSync21,
|
|
25833
26593
|
opendirSync as opendirSync13,
|
|
25834
|
-
realpathSync as
|
|
26594
|
+
realpathSync as realpathSync12,
|
|
25835
26595
|
rmdirSync,
|
|
25836
26596
|
statfsSync as statfsSync3,
|
|
25837
|
-
writeFileSync as
|
|
26597
|
+
writeFileSync as writeFileSync5
|
|
25838
26598
|
} from "node:fs";
|
|
25839
|
-
import { basename as basename3, dirname as
|
|
25840
|
-
import { fileURLToPath as
|
|
26599
|
+
import { basename as basename3, dirname as dirname22, join as join32, resolve as resolve11 } from "node:path";
|
|
26600
|
+
import { fileURLToPath as fileURLToPath6 } from "node:url";
|
|
25841
26601
|
|
|
25842
26602
|
// src/task-diff.ts
|
|
25843
|
-
import { spawnSync as
|
|
26603
|
+
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
25844
26604
|
import { createHash as createHash24 } from "node:crypto";
|
|
25845
|
-
import { lstatSync as
|
|
25846
|
-
import { join as
|
|
26605
|
+
import { lstatSync as lstatSync18, readdirSync as readdirSync3, readlinkSync } from "node:fs";
|
|
26606
|
+
import { join as join30, relative as relative8 } from "node:path";
|
|
25847
26607
|
var admin = (path) => path === ".cadence/assistant/tasks" || path.startsWith(".cadence/assistant/tasks/");
|
|
25848
26608
|
var GIT_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
25849
26609
|
function captureChanges(root, outputBytes = GIT_OUTPUT_BYTES) {
|
|
@@ -25862,7 +26622,7 @@ function captureChanges(root, outputBytes = GIT_OUTPUT_BYTES) {
|
|
|
25862
26622
|
if (Date.now() >= deadline)
|
|
25863
26623
|
outcome = "time budget exhausted";
|
|
25864
26624
|
else {
|
|
25865
|
-
const result =
|
|
26625
|
+
const result = spawnSync5("git", gitArguments(["-c", "core.quotepath=true", ...args]), {
|
|
25866
26626
|
cwd: root,
|
|
25867
26627
|
encoding: "buffer",
|
|
25868
26628
|
timeout: Math.max(1, deadline - Date.now()),
|
|
@@ -25886,8 +26646,8 @@ function captureChanges(root, outputBytes = GIT_OUTPUT_BYTES) {
|
|
|
25886
26646
|
return;
|
|
25887
26647
|
}
|
|
25888
26648
|
try {
|
|
25889
|
-
const path =
|
|
25890
|
-
const st =
|
|
26649
|
+
const path = join30(root, name);
|
|
26650
|
+
const st = lstatSync18(path);
|
|
25891
26651
|
add(name, `${st.mode}:${st.size}`);
|
|
25892
26652
|
if (st.isSymbolicLink()) {
|
|
25893
26653
|
add(name, readlinkSync(path));
|
|
@@ -25912,7 +26672,7 @@ function captureChanges(root, outputBytes = GIT_OUTPUT_BYTES) {
|
|
|
25912
26672
|
limits.push(`Unreadable/changing: ${name}`);
|
|
25913
26673
|
}
|
|
25914
26674
|
};
|
|
25915
|
-
const probe2 =
|
|
26675
|
+
const probe2 = spawnSync5("git", gitArguments(["rev-parse", "--is-inside-work-tree"]), {
|
|
25916
26676
|
cwd: root,
|
|
25917
26677
|
env: gitEnvironment(),
|
|
25918
26678
|
timeout: 3000,
|
|
@@ -26027,7 +26787,7 @@ ${data2.toString("utf8")}`);
|
|
|
26027
26787
|
return;
|
|
26028
26788
|
}
|
|
26029
26789
|
for (const name of names2) {
|
|
26030
|
-
const path =
|
|
26790
|
+
const path = join30(dir, name);
|
|
26031
26791
|
const rel = relative8(root, path).split("\\").join("/");
|
|
26032
26792
|
if (rel === ".git" || admin(rel))
|
|
26033
26793
|
continue;
|
|
@@ -26036,7 +26796,7 @@ ${data2.toString("utf8")}`);
|
|
|
26036
26796
|
break;
|
|
26037
26797
|
}
|
|
26038
26798
|
try {
|
|
26039
|
-
const st =
|
|
26799
|
+
const st = lstatSync18(path);
|
|
26040
26800
|
if (st.isDirectory())
|
|
26041
26801
|
walk(path, depth + 1);
|
|
26042
26802
|
else
|
|
@@ -26062,21 +26822,21 @@ ${data2.toString("utf8")}`);
|
|
|
26062
26822
|
}
|
|
26063
26823
|
|
|
26064
26824
|
// src/service/workflow-git.ts
|
|
26065
|
-
import { spawn as
|
|
26825
|
+
import { spawn as spawn4, spawnSync as spawnSync6 } from "node:child_process";
|
|
26066
26826
|
import { createHash as createHash25 } from "node:crypto";
|
|
26067
26827
|
import {
|
|
26068
26828
|
closeSync as closeSync9,
|
|
26069
|
-
constants as
|
|
26829
|
+
constants as constants8,
|
|
26070
26830
|
fstatSync as fstatSync6,
|
|
26071
|
-
lstatSync as
|
|
26831
|
+
lstatSync as lstatSync19,
|
|
26072
26832
|
openSync as openSync9,
|
|
26073
26833
|
readdirSync as readdirSync4,
|
|
26074
26834
|
readlinkSync as readlinkSync2,
|
|
26075
26835
|
readSync as readSync5,
|
|
26076
|
-
realpathSync as
|
|
26836
|
+
realpathSync as realpathSync11
|
|
26077
26837
|
} from "node:fs";
|
|
26078
26838
|
import { lstat, open as open2, readdir, readlink } from "node:fs/promises";
|
|
26079
|
-
import { dirname as
|
|
26839
|
+
import { dirname as dirname21, isAbsolute as isAbsolute10, join as join31, relative as relative9, resolve as resolve10, sep as sep7 } from "node:path";
|
|
26080
26840
|
var NAMES_BYTES = 64 * 1024 * 1024;
|
|
26081
26841
|
var SYNC_MS = 30000;
|
|
26082
26842
|
var READ_MS = 120000;
|
|
@@ -26092,7 +26852,7 @@ class WorkflowRefusal extends ContractError {
|
|
|
26092
26852
|
}
|
|
26093
26853
|
}
|
|
26094
26854
|
function gitRead(cwd, args) {
|
|
26095
|
-
const r =
|
|
26855
|
+
const r = spawnSync6("git", gitArguments(args), {
|
|
26096
26856
|
cwd,
|
|
26097
26857
|
env: gitEnvironment(),
|
|
26098
26858
|
encoding: "utf8",
|
|
@@ -26105,7 +26865,7 @@ function gitRead(cwd, args) {
|
|
|
26105
26865
|
return r.stdout;
|
|
26106
26866
|
}
|
|
26107
26867
|
function gitAnswer(cwd, args) {
|
|
26108
|
-
const r =
|
|
26868
|
+
const r = spawnSync6("git", gitArguments(args), {
|
|
26109
26869
|
cwd,
|
|
26110
26870
|
env: gitEnvironment(),
|
|
26111
26871
|
encoding: "utf8",
|
|
@@ -26119,7 +26879,7 @@ function gitAnswer(cwd, args) {
|
|
|
26119
26879
|
}
|
|
26120
26880
|
function gitRun(cwd, args, timeoutMs = READ_MS) {
|
|
26121
26881
|
return new Promise((done, fail2) => {
|
|
26122
|
-
const child =
|
|
26882
|
+
const child = spawn4("git", gitArguments(args), {
|
|
26123
26883
|
cwd,
|
|
26124
26884
|
env: gitEnvironment(),
|
|
26125
26885
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -26162,19 +26922,19 @@ async function gitHead(cwd) {
|
|
|
26162
26922
|
}
|
|
26163
26923
|
function contains2(root, path) {
|
|
26164
26924
|
const within = relative9(root, path);
|
|
26165
|
-
return !within || !within.startsWith(`..${
|
|
26925
|
+
return !within || !within.startsWith(`..${sep7}`) && within !== ".." && !isAbsolute10(within);
|
|
26166
26926
|
}
|
|
26167
26927
|
function nested(a, b) {
|
|
26168
26928
|
return contains2(a, b) || contains2(b, a);
|
|
26169
26929
|
}
|
|
26170
26930
|
function safeWorkflowFile(cwd, name) {
|
|
26171
26931
|
workflowPath(name);
|
|
26172
|
-
const p =
|
|
26932
|
+
const p = resolve10(cwd, name);
|
|
26173
26933
|
if (relative9(cwd, p).startsWith(".."))
|
|
26174
26934
|
throw new ContractError("forbidden");
|
|
26175
|
-
safeDirectory(
|
|
26935
|
+
safeDirectory(dirname21(p));
|
|
26176
26936
|
try {
|
|
26177
|
-
const st =
|
|
26937
|
+
const st = lstatSync19(p);
|
|
26178
26938
|
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1)
|
|
26179
26939
|
throw new ContractError("forbidden");
|
|
26180
26940
|
} catch (e) {
|
|
@@ -26185,12 +26945,12 @@ function safeWorkflowFile(cwd, name) {
|
|
|
26185
26945
|
}
|
|
26186
26946
|
function declaredTarget(cwd, name) {
|
|
26187
26947
|
workflowPath(name);
|
|
26188
|
-
const p =
|
|
26948
|
+
const p = resolve10(cwd, name);
|
|
26189
26949
|
if (relative9(cwd, p).startsWith(".."))
|
|
26190
26950
|
throw new ContractError("forbidden");
|
|
26191
|
-
safeDirectory(
|
|
26951
|
+
safeDirectory(dirname21(p));
|
|
26192
26952
|
try {
|
|
26193
|
-
const st =
|
|
26953
|
+
const st = lstatSync19(p);
|
|
26194
26954
|
if (st.isSymbolicLink() || !st.isFile() && !st.isDirectory() || st.isFile() && st.nlink !== 1)
|
|
26195
26955
|
throw new ContractError("forbidden");
|
|
26196
26956
|
} catch (e) {
|
|
@@ -26216,7 +26976,7 @@ function fileIdentity(p, st, hashBytes) {
|
|
|
26216
26976
|
return quick;
|
|
26217
26977
|
let fd;
|
|
26218
26978
|
try {
|
|
26219
|
-
fd = openSync9(p,
|
|
26979
|
+
fd = openSync9(p, constants8.O_RDONLY | (constants8.O_NOFOLLOW ?? 0));
|
|
26220
26980
|
} catch (e) {
|
|
26221
26981
|
if (absent2(e))
|
|
26222
26982
|
return null;
|
|
@@ -26241,7 +27001,7 @@ async function fileIdentityAsync(p, st, hashBytes) {
|
|
|
26241
27001
|
return quick;
|
|
26242
27002
|
let handle;
|
|
26243
27003
|
try {
|
|
26244
|
-
handle = await open2(p,
|
|
27004
|
+
handle = await open2(p, constants8.O_RDONLY | (constants8.O_NOFOLLOW ?? 0));
|
|
26245
27005
|
} catch (e) {
|
|
26246
27006
|
if (absent2(e))
|
|
26247
27007
|
return null;
|
|
@@ -26270,8 +27030,8 @@ function directoryIdentity(root, name) {
|
|
|
26270
27030
|
continue;
|
|
26271
27031
|
if (++count2 > DIRECTORY_ENTRIES)
|
|
26272
27032
|
throw tooLarge(name);
|
|
26273
|
-
const full =
|
|
26274
|
-
const st =
|
|
27033
|
+
const full = join31(directory, entry), relative10 = `${prefix}${entry}`;
|
|
27034
|
+
const st = lstatSync19(full);
|
|
26275
27035
|
if (st.isDirectory()) {
|
|
26276
27036
|
rows.push(`${relative10}/`);
|
|
26277
27037
|
visit(full, `${relative10}/`);
|
|
@@ -26292,7 +27052,7 @@ async function directoryIdentityAsync(root, name) {
|
|
|
26292
27052
|
continue;
|
|
26293
27053
|
if (++count2 > DIRECTORY_ENTRIES)
|
|
26294
27054
|
throw tooLarge(name);
|
|
26295
|
-
const full =
|
|
27055
|
+
const full = join31(directory, entry), relative10 = `${prefix}${entry}`;
|
|
26296
27056
|
const st = await lstat(full);
|
|
26297
27057
|
if (st.isDirectory()) {
|
|
26298
27058
|
rows.push(`${relative10}/`);
|
|
@@ -26306,12 +27066,12 @@ async function directoryIdentityAsync(root, name) {
|
|
|
26306
27066
|
`))}`;
|
|
26307
27067
|
}
|
|
26308
27068
|
function pathIdentity(cwd, name, walk = true) {
|
|
26309
|
-
const p =
|
|
27069
|
+
const p = resolve10(cwd, name);
|
|
26310
27070
|
if (relative9(cwd, p).startsWith(".."))
|
|
26311
27071
|
throw new ContractError("forbidden");
|
|
26312
27072
|
let st;
|
|
26313
27073
|
try {
|
|
26314
|
-
st =
|
|
27074
|
+
st = lstatSync19(p);
|
|
26315
27075
|
} catch (e) {
|
|
26316
27076
|
if (absent2(e))
|
|
26317
27077
|
return null;
|
|
@@ -26322,7 +27082,7 @@ function pathIdentity(cwd, name, walk = true) {
|
|
|
26322
27082
|
return fileIdentity(p, st, HASH_BYTES);
|
|
26323
27083
|
}
|
|
26324
27084
|
async function pathIdentityAsync(cwd, name) {
|
|
26325
|
-
const p =
|
|
27085
|
+
const p = resolve10(cwd, name);
|
|
26326
27086
|
if (relative9(cwd, p).startsWith(".."))
|
|
26327
27087
|
throw new ContractError("forbidden");
|
|
26328
27088
|
let st;
|
|
@@ -26526,20 +27286,20 @@ ${block}
|
|
|
26526
27286
|
async function assertWorktree(root, cwd) {
|
|
26527
27287
|
if (!safeDirectory(cwd))
|
|
26528
27288
|
throw new ContractError("stale");
|
|
26529
|
-
const real = (p) =>
|
|
27289
|
+
const real = (p) => realpathSync11(resolve10(p));
|
|
26530
27290
|
if (real((await gitText(cwd, ["rev-parse", "--show-toplevel"])).trim()) !== real(cwd))
|
|
26531
27291
|
throw new ContractError("stale");
|
|
26532
|
-
const common = async (p) => real(
|
|
27292
|
+
const common = async (p) => real(resolve10(p, (await gitText(p, ["rev-parse", "--git-common-dir"])).trim()));
|
|
26533
27293
|
if (await common(root) !== await common(cwd))
|
|
26534
27294
|
throw new ContractError("stale");
|
|
26535
|
-
const marker2 =
|
|
26536
|
-
if (!
|
|
27295
|
+
const marker2 = resolve10(cwd, ".git");
|
|
27296
|
+
if (!lstatSync19(marker2).isFile() || !(readBounded(marker2, 8192)?.toString("utf8") ?? "").startsWith("gitdir: "))
|
|
26537
27297
|
throw new ContractError("stale");
|
|
26538
27298
|
}
|
|
26539
27299
|
|
|
26540
27300
|
// src/service/workflow-history.ts
|
|
26541
27301
|
import { randomUUID as randomUUID22 } from "node:crypto";
|
|
26542
|
-
import { lstatSync as
|
|
27302
|
+
import { lstatSync as lstatSync20, opendirSync as opendirSync12 } from "node:fs";
|
|
26543
27303
|
class WorkflowHistoryScanner {
|
|
26544
27304
|
path;
|
|
26545
27305
|
directory;
|
|
@@ -26550,7 +27310,7 @@ class WorkflowHistoryScanner {
|
|
|
26550
27310
|
this.directory = directory;
|
|
26551
27311
|
}
|
|
26552
27312
|
incarnation() {
|
|
26553
|
-
const st =
|
|
27313
|
+
const st = lstatSync20(this.path, { bigint: true });
|
|
26554
27314
|
if (!st.isDirectory() || st.isSymbolicLink())
|
|
26555
27315
|
throw new ContractError("corrupt-selected");
|
|
26556
27316
|
return `${st.dev}:${st.ino}`;
|
|
@@ -26639,9 +27399,9 @@ class WorkflowHistoryScanner {
|
|
|
26639
27399
|
|
|
26640
27400
|
// src/service/workflows.ts
|
|
26641
27401
|
var OWNER_RUNTIME = Object.freeze({
|
|
26642
|
-
version: "2.0.
|
|
26643
|
-
build: "
|
|
26644
|
-
entry:
|
|
27402
|
+
version: "2.0.30",
|
|
27403
|
+
build: "b33664958cc26420c95cd53aff826f4f0d87cc3edee145ccb9467cad93ec29e2",
|
|
27404
|
+
entry: fileURLToPath6(import.meta.url),
|
|
26645
27405
|
pid: process.pid,
|
|
26646
27406
|
node: process.versions.node,
|
|
26647
27407
|
platform: process.platform,
|
|
@@ -26827,12 +27587,12 @@ class Workflows {
|
|
|
26827
27587
|
grants = new Map;
|
|
26828
27588
|
constructor(o) {
|
|
26829
27589
|
this.o = o;
|
|
26830
|
-
this.root =
|
|
26831
|
-
this.cwd =
|
|
26832
|
-
this.worktreeRoot =
|
|
26833
|
-
this.historyScanner = new WorkflowHistoryScanner(
|
|
27590
|
+
this.root = join32(o.coordination.directory, "workflows-v1");
|
|
27591
|
+
this.cwd = realpathSync12(o.coordination.store.root);
|
|
27592
|
+
this.worktreeRoot = join32(o.coordination.stateHome, "cadence-worktrees-v1", basename3(o.coordination.store.privateRoot));
|
|
27593
|
+
this.historyScanner = new WorkflowHistoryScanner(join32(this.root, "runs"), () => {
|
|
26834
27594
|
this.o.owner.lease.assert();
|
|
26835
|
-
return this.files.directory(
|
|
27595
|
+
return this.files.directory(join32(this.root, "runs"));
|
|
26836
27596
|
});
|
|
26837
27597
|
this.recover();
|
|
26838
27598
|
}
|
|
@@ -26840,7 +27600,7 @@ class Workflows {
|
|
|
26840
27600
|
return this.o.coordination.files;
|
|
26841
27601
|
}
|
|
26842
27602
|
path(id, name) {
|
|
26843
|
-
return
|
|
27603
|
+
return join32(this.root, "runs", decodeId(id), name);
|
|
26844
27604
|
}
|
|
26845
27605
|
write(path, value, expected, max = RECORD_BYTES2) {
|
|
26846
27606
|
this.o.owner.lease.assert();
|
|
@@ -26857,7 +27617,7 @@ class Workflows {
|
|
|
26857
27617
|
`), 2048, this.o.secrets());
|
|
26858
27618
|
}
|
|
26859
27619
|
recover() {
|
|
26860
|
-
const runs =
|
|
27620
|
+
const runs = join32(this.root, "runs");
|
|
26861
27621
|
if (!this.files.directory(runs))
|
|
26862
27622
|
return;
|
|
26863
27623
|
const ids = [];
|
|
@@ -26892,7 +27652,7 @@ class Workflows {
|
|
|
26892
27652
|
const accepted2 = this.retained(this.path(id, "accepted.json"));
|
|
26893
27653
|
if (!accepted2)
|
|
26894
27654
|
return;
|
|
26895
|
-
let
|
|
27655
|
+
let run3;
|
|
26896
27656
|
try {
|
|
26897
27657
|
const o = closed(parseWireJson(accepted2.bytes.toString("utf8"), RECORD_BYTES2), ["epoch", "request"]);
|
|
26898
27658
|
const request3 = decodeWorkflowRequest(o.request);
|
|
@@ -26901,7 +27661,7 @@ class Workflows {
|
|
|
26901
27661
|
try {
|
|
26902
27662
|
goal = this.proposal(request3.proposalId).plan.goal;
|
|
26903
27663
|
} catch {}
|
|
26904
|
-
|
|
27664
|
+
run3 = {
|
|
26905
27665
|
id,
|
|
26906
27666
|
epoch: decodeId(o.epoch),
|
|
26907
27667
|
goal,
|
|
@@ -26915,30 +27675,30 @@ class Workflows {
|
|
|
26915
27675
|
} catch {
|
|
26916
27676
|
return;
|
|
26917
27677
|
}
|
|
26918
|
-
this.write(path,
|
|
27678
|
+
this.write(path, run3);
|
|
26919
27679
|
return;
|
|
26920
27680
|
}
|
|
26921
|
-
let
|
|
27681
|
+
let run2;
|
|
26922
27682
|
try {
|
|
26923
|
-
|
|
27683
|
+
run2 = decodeWorkflowRun(parseWireJson(file.bytes.toString("utf8"), RECORD_BYTES2));
|
|
26924
27684
|
} catch {
|
|
26925
27685
|
return;
|
|
26926
27686
|
}
|
|
26927
|
-
if (
|
|
27687
|
+
if (run2.id !== id || !["queued", "running"].includes(run2.status) && run2.phase === "finished")
|
|
26928
27688
|
return;
|
|
26929
|
-
const integrating =
|
|
26930
|
-
|
|
26931
|
-
|
|
26932
|
-
for (const step of
|
|
27689
|
+
const integrating = run2.phase === "integrating";
|
|
27690
|
+
run2.status = "unknown";
|
|
27691
|
+
run2.phase = "finished";
|
|
27692
|
+
for (const step of run2.steps)
|
|
26933
27693
|
if (step.status === "running")
|
|
26934
27694
|
step.status = "unknown";
|
|
26935
|
-
|
|
26936
|
-
|
|
27695
|
+
run2.notice = diagnosticText([
|
|
27696
|
+
run2.notice,
|
|
26937
27697
|
RESTARTED,
|
|
26938
27698
|
integrating ? "Integration had started: inspect the target branch; a fast-forward may have completed." : ""
|
|
26939
27699
|
].filter(Boolean).join(`
|
|
26940
27700
|
`), 2048);
|
|
26941
|
-
this.write(path,
|
|
27701
|
+
this.write(path, run2, file);
|
|
26942
27702
|
}
|
|
26943
27703
|
current() {
|
|
26944
27704
|
this.o.owner.lease.assert();
|
|
@@ -26968,7 +27728,7 @@ class Workflows {
|
|
|
26968
27728
|
};
|
|
26969
27729
|
}
|
|
26970
27730
|
proposal(id) {
|
|
26971
|
-
const file = this.files.read(
|
|
27731
|
+
const file = this.files.read(join32(this.root, "proposals", `${decodeId(id)}.json`), RECORD_BYTES2);
|
|
26972
27732
|
if (!file)
|
|
26973
27733
|
throw new ContractError("unavailable");
|
|
26974
27734
|
return decodeWorkflowProposal(parseWireJson(file.bytes.toString("utf8"), RECORD_BYTES2));
|
|
@@ -26990,9 +27750,9 @@ class Workflows {
|
|
|
26990
27750
|
this.o.owner.lease.assert();
|
|
26991
27751
|
return {
|
|
26992
27752
|
kind: "workflow-active",
|
|
26993
|
-
runs: [...this.active.values()].map(({ run }) => ({
|
|
26994
|
-
...structuredClone(
|
|
26995
|
-
goal: clipUnits(
|
|
27753
|
+
runs: [...this.active.values()].map(({ run: run2 }) => ({
|
|
27754
|
+
...structuredClone(run2),
|
|
27755
|
+
goal: clipUnits(run2.goal, 320),
|
|
26996
27756
|
steps: []
|
|
26997
27757
|
}))
|
|
26998
27758
|
};
|
|
@@ -27007,13 +27767,13 @@ class Workflows {
|
|
|
27007
27767
|
const view = this.view(id);
|
|
27008
27768
|
if (view.kind !== "workflow" || view.response.type !== "run")
|
|
27009
27769
|
throw new ContractError("invalid");
|
|
27010
|
-
const
|
|
27770
|
+
const run2 = view.response.run;
|
|
27011
27771
|
return {
|
|
27012
27772
|
id,
|
|
27013
27773
|
run: {
|
|
27014
|
-
...
|
|
27015
|
-
goal: clipUnits(sanitizeText(
|
|
27016
|
-
notice: this.notice(
|
|
27774
|
+
...run2,
|
|
27775
|
+
goal: clipUnits(sanitizeText(run2.goal, this.o.secrets()), 320),
|
|
27776
|
+
notice: this.notice(run2.notice),
|
|
27017
27777
|
steps: []
|
|
27018
27778
|
}
|
|
27019
27779
|
};
|
|
@@ -27039,27 +27799,27 @@ class Workflows {
|
|
|
27039
27799
|
const file = live2 ? undefined : this.files.read(this.path(id, "state.json"), RECORD_BYTES2);
|
|
27040
27800
|
if (!live2 && !file)
|
|
27041
27801
|
throw new ContractError("unavailable");
|
|
27042
|
-
const
|
|
27043
|
-
if (
|
|
27802
|
+
const run2 = decodeWorkflowRun(live2 ? live2.run : parseWireJson(file ? file.bytes.toString("utf8") : "null", RECORD_BYTES2));
|
|
27803
|
+
if (run2.id !== id)
|
|
27044
27804
|
throw new ContractError("corrupt-selected");
|
|
27045
|
-
if (!live2 && ["running", "queued"].includes(
|
|
27046
|
-
|
|
27805
|
+
if (!live2 && ["running", "queued"].includes(run2.status))
|
|
27806
|
+
run2.status = "unknown";
|
|
27047
27807
|
return {
|
|
27048
27808
|
kind: "workflow",
|
|
27049
27809
|
commandId: null,
|
|
27050
|
-
response: { type: "run", run }
|
|
27810
|
+
response: { type: "run", run: run2 }
|
|
27051
27811
|
};
|
|
27052
27812
|
}
|
|
27053
27813
|
transcript(id, stepId) {
|
|
27054
27814
|
const view = this.view(id);
|
|
27055
27815
|
if (view.kind !== "workflow" || view.response.type !== "run")
|
|
27056
27816
|
throw new ContractError("unavailable");
|
|
27057
|
-
const
|
|
27817
|
+
const run2 = view.response.run, step = run2.steps.find((s) => s.id === stepId);
|
|
27058
27818
|
if (!step?.childRunId)
|
|
27059
27819
|
throw new ContractError("unavailable");
|
|
27060
27820
|
const file = this.files.read(this.path(id, `transcripts/${step.childRunId}.json`), RECORD_BYTES2);
|
|
27061
27821
|
const turn = file ? decodeChatTurn(parseWireJson(file.bytes.toString("utf8"), RECORD_BYTES2)) : null;
|
|
27062
|
-
if (turn && (turn.id !== step.childRunId || turn.epoch !==
|
|
27822
|
+
if (turn && (turn.id !== step.childRunId || turn.epoch !== run2.epoch))
|
|
27063
27823
|
throw new ContractError("corrupt-selected");
|
|
27064
27824
|
if (turn) {
|
|
27065
27825
|
const secrets = this.o.secrets();
|
|
@@ -27122,8 +27882,8 @@ class Workflows {
|
|
|
27122
27882
|
if (action === "prepare")
|
|
27123
27883
|
return [...this.grants.values()].some((g) => g.commandId === id && g.expiresAt >= Date.now());
|
|
27124
27884
|
if (action === "propose")
|
|
27125
|
-
return !!this.files.read(
|
|
27126
|
-
return this.files.directory(
|
|
27885
|
+
return !!this.files.read(join32(this.root, "proposals", `${id}.json`), RECORD_BYTES2);
|
|
27886
|
+
return this.files.directory(join32(this.root, "runs", id));
|
|
27127
27887
|
}
|
|
27128
27888
|
admit(commandId, epoch, r, effect) {
|
|
27129
27889
|
this.current();
|
|
@@ -27152,7 +27912,7 @@ class Workflows {
|
|
|
27152
27912
|
};
|
|
27153
27913
|
if (sanitizeData(p, secrets) !== canonicalJson(p))
|
|
27154
27914
|
throw new ContractError("forbidden");
|
|
27155
|
-
const target3 =
|
|
27915
|
+
const target3 = join32(this.root, "proposals", `${commandId}.json`);
|
|
27156
27916
|
const previous = this.files.read(target3, RECORD_BYTES2);
|
|
27157
27917
|
if (previous) {
|
|
27158
27918
|
if (previous.bytes.toString("utf8") !== canonicalJson(p))
|
|
@@ -27203,7 +27963,7 @@ class Workflows {
|
|
|
27203
27963
|
throw new ContractError("conflict");
|
|
27204
27964
|
return { ...this.view(commandId), commandId };
|
|
27205
27965
|
}
|
|
27206
|
-
if (this.files.directory(
|
|
27966
|
+
if (this.files.directory(dirname22(acceptedPath)))
|
|
27207
27967
|
throw new ContractError("quarantine");
|
|
27208
27968
|
let proposal;
|
|
27209
27969
|
let target2;
|
|
@@ -27231,7 +27991,7 @@ class Workflows {
|
|
|
27231
27991
|
this.capacity();
|
|
27232
27992
|
} else
|
|
27233
27993
|
throw new ContractError("invalid");
|
|
27234
|
-
const
|
|
27994
|
+
const run2 = {
|
|
27235
27995
|
id: commandId,
|
|
27236
27996
|
epoch,
|
|
27237
27997
|
goal: proposal?.plan.goal ?? (request3.action === "research" ? request3.goal : ""),
|
|
@@ -27253,11 +28013,11 @@ class Workflows {
|
|
|
27253
28013
|
};
|
|
27254
28014
|
const abort = new AbortController;
|
|
27255
28015
|
let begin;
|
|
27256
|
-
const ready2 = new Promise((
|
|
27257
|
-
begin =
|
|
28016
|
+
const ready2 = new Promise((resolve12) => {
|
|
28017
|
+
begin = resolve12;
|
|
27258
28018
|
});
|
|
27259
28019
|
const live2 = {
|
|
27260
|
-
run,
|
|
28020
|
+
run: run2,
|
|
27261
28021
|
abort,
|
|
27262
28022
|
original: ready2.then(() => this.execute(live2, request3)),
|
|
27263
28023
|
proposal,
|
|
@@ -27271,7 +28031,7 @@ class Workflows {
|
|
|
27271
28031
|
this.o.owner.trackConversation(live2.original, () => abort.abort());
|
|
27272
28032
|
try {
|
|
27273
28033
|
this.write(acceptedPath, { epoch, request: request3 });
|
|
27274
|
-
live2.file = this.write(this.path(commandId, "state.json"),
|
|
28034
|
+
live2.file = this.write(this.path(commandId, "state.json"), run2);
|
|
27275
28035
|
} catch (e) {
|
|
27276
28036
|
abort.abort();
|
|
27277
28037
|
begin();
|
|
@@ -27281,7 +28041,7 @@ class Workflows {
|
|
|
27281
28041
|
return {
|
|
27282
28042
|
kind: "workflow",
|
|
27283
28043
|
commandId,
|
|
27284
|
-
response: { type: "run", run: structuredClone(
|
|
28044
|
+
response: { type: "run", run: structuredClone(run2) }
|
|
27285
28045
|
};
|
|
27286
28046
|
}
|
|
27287
28047
|
capacity() {
|
|
@@ -27463,7 +28223,7 @@ Read applicable AGENTS.md, existing project conventions and relevant tests befor
|
|
|
27463
28223
|
const running = Promise.resolve().then(() => launchWorkflowWorker(prepared.prepared, cwd, live2.abort.signal, {
|
|
27464
28224
|
timeoutMs: this.o.timeoutMs ?? LIMITS.workerMs,
|
|
27465
28225
|
ownerSecrets: this.o.secrets(),
|
|
27466
|
-
roots: roots?.length ? roots.map((p) =>
|
|
28226
|
+
roots: roots?.length ? roots.map((p) => resolve11(cwd, p)) : [cwd],
|
|
27467
28227
|
onLifecycle: (event) => {
|
|
27468
28228
|
if (!step.timing)
|
|
27469
28229
|
return;
|
|
@@ -27558,7 +28318,7 @@ Read applicable AGENTS.md, existing project conventions and relevant tests befor
|
|
|
27558
28318
|
command: shell ? process.platform === "win32" ? environment2.COMSPEC ?? "cmd.exe" : "/bin/sh" : "git",
|
|
27559
28319
|
args: shell ? process.platform === "win32" ? ["/d", "/s", "/c", `"${operation.check.command}"`] : ["-c", operation.check.command] : gitArguments([
|
|
27560
28320
|
"-c",
|
|
27561
|
-
`core.hooksPath=${
|
|
28321
|
+
`core.hooksPath=${join32(this.root, "empty-hooks")}`,
|
|
27562
28322
|
"-c",
|
|
27563
28323
|
"commit.gpgsign=false",
|
|
27564
28324
|
"-c",
|
|
@@ -27795,9 +28555,9 @@ ${changed.map((p) => `- ${p}`).join(`
|
|
|
27795
28555
|
publish(live2, cwd, plan, names3, changed) {
|
|
27796
28556
|
for (const name of names3) {
|
|
27797
28557
|
const file = safeWorkflowFile(cwd, name);
|
|
27798
|
-
if (!safeDirectory(
|
|
28558
|
+
if (!safeDirectory(dirname22(file), true))
|
|
27799
28559
|
throw new ContractError("forbidden");
|
|
27800
|
-
|
|
28560
|
+
writeFileSync5(file, this.documentBody(live2, cwd, plan, name, changed), {
|
|
27801
28561
|
flag: name === ".cadence/project.md" ? "w" : "wx"
|
|
27802
28562
|
});
|
|
27803
28563
|
}
|
|
@@ -27805,17 +28565,17 @@ ${changed.map((p) => `- ${p}`).join(`
|
|
|
27805
28565
|
worktreeHome(id) {
|
|
27806
28566
|
if (nested(this.cwd, this.worktreeRoot))
|
|
27807
28567
|
throw new ContractError("unsupported-context");
|
|
27808
|
-
for (const directory of [
|
|
28568
|
+
for (const directory of [dirname22(this.worktreeRoot), this.worktreeRoot]) {
|
|
27809
28569
|
if (!safeDirectory(directory, true))
|
|
27810
28570
|
throw new ContractError("forbidden");
|
|
27811
|
-
const st =
|
|
28571
|
+
const st = lstatSync21(directory);
|
|
27812
28572
|
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
27813
28573
|
throw new ContractError("forbidden");
|
|
27814
28574
|
}
|
|
27815
|
-
const home =
|
|
28575
|
+
const home = join32(this.worktreeRoot, decodeId(id));
|
|
27816
28576
|
if (safeDirectory(home))
|
|
27817
28577
|
throw new ContractError("conflict");
|
|
27818
|
-
safeDirectory(
|
|
28578
|
+
safeDirectory(join32(home, "tasks"), true);
|
|
27819
28579
|
return home;
|
|
27820
28580
|
}
|
|
27821
28581
|
async merge(live2, candidate) {
|
|
@@ -27883,7 +28643,7 @@ ${result.output}`}`);
|
|
|
27883
28643
|
} finally {
|
|
27884
28644
|
this.closeStep(live2, step);
|
|
27885
28645
|
}
|
|
27886
|
-
for (const directory of [
|
|
28646
|
+
for (const directory of [join32(home, "tasks"), home])
|
|
27887
28647
|
try {
|
|
27888
28648
|
rmdirSync(directory);
|
|
27889
28649
|
} catch {}
|
|
@@ -27915,8 +28675,8 @@ Write only these exact files: ${task.writes.join(", ")} (a declared directory in
|
|
|
27915
28675
|
throw new ContractError("invalid");
|
|
27916
28676
|
const base = target2.head;
|
|
27917
28677
|
const home = this.worktreeHome(live2.run.id);
|
|
27918
|
-
const taskHome =
|
|
27919
|
-
const integration =
|
|
28678
|
+
const taskHome = join32(home, "tasks");
|
|
28679
|
+
const integration = join32(home, "integration");
|
|
27920
28680
|
await this.gitStep(live2, this.cwd, "worktree-integration", (git) => git(["worktree", "add", "--detach", integration, base]));
|
|
27921
28681
|
await assertWorktree(this.cwd, integration);
|
|
27922
28682
|
await this.setup(live2, integration, plan);
|
|
@@ -27933,7 +28693,7 @@ Write only these exact files: ${task.writes.join(", ")} (a declared directory in
|
|
|
27933
28693
|
live2.run.phase = "implementing";
|
|
27934
28694
|
this.save(live2);
|
|
27935
28695
|
const outcomes2 = await Promise.allSettled(batch.map(async (task) => {
|
|
27936
|
-
const cwd =
|
|
28696
|
+
const cwd = join32(taskHome, task.id);
|
|
27937
28697
|
await this.gitStep(live2, this.cwd, `worktree-${task.id}`, (git) => git(["worktree", "add", "--detach", cwd, base]));
|
|
27938
28698
|
const picks2 = workflowAncestors(plan.tasks, task.id).map((dep) => commits.get(dep.id)).filter((c) => !!c);
|
|
27939
28699
|
if (picks2.length)
|
|
@@ -27998,9 +28758,9 @@ Write only these exact files: ${task.writes.join(", ")} (a declared directory in
|
|
|
27998
28758
|
const file = safeWorkflowFile(this.cwd, name), before = documentBefore.get(name) ?? null;
|
|
27999
28759
|
if (before !== pathIdentity(this.cwd, name))
|
|
28000
28760
|
throw new WorkflowRefusal("conflict", `${name} changed during the run; the generated note was not written.`);
|
|
28001
|
-
if (!safeDirectory(
|
|
28761
|
+
if (!safeDirectory(dirname22(file), true))
|
|
28002
28762
|
throw new ContractError("unavailable");
|
|
28003
|
-
|
|
28763
|
+
writeFileSync5(file, this.documentBody(live2, this.cwd, plan, name, [...live2.delivered].sort(), true), { flag: before ? "w" : "wx" });
|
|
28004
28764
|
}
|
|
28005
28765
|
if (!await this.checks(live2, this.cwd, plan))
|
|
28006
28766
|
throw new Error("Local merge completed, but post-merge checks failed. No reset or rollback was attempted.");
|
|
@@ -28113,7 +28873,7 @@ class Workbench {
|
|
|
28113
28873
|
});
|
|
28114
28874
|
this.conversations.assertSettled();
|
|
28115
28875
|
this.files = new WorkbenchFiles(o.cwd, [o.coordination.files.namespace, o.agentDir], o.secrets);
|
|
28116
|
-
this.projectRoot =
|
|
28876
|
+
this.projectRoot = join33(o.coordination.files.namespace, "workbench-projects-v1");
|
|
28117
28877
|
}
|
|
28118
28878
|
closeReads() {
|
|
28119
28879
|
try {
|
|
@@ -28132,7 +28892,7 @@ class Workbench {
|
|
|
28132
28892
|
const active2 = this.workflows.activeRuns();
|
|
28133
28893
|
return {
|
|
28134
28894
|
...active2,
|
|
28135
|
-
links: this.cadence.links(active2.runs.map((
|
|
28895
|
+
links: this.cadence.links(active2.runs.map((run2) => run2.id))
|
|
28136
28896
|
};
|
|
28137
28897
|
}
|
|
28138
28898
|
case "workflow-history":
|
|
@@ -28158,10 +28918,12 @@ class Workbench {
|
|
|
28158
28918
|
}))
|
|
28159
28919
|
};
|
|
28160
28920
|
}
|
|
28161
|
-
case "conversation-transcript":
|
|
28162
|
-
return this.conversations.transcript(request3.id, request3.turnId, request3.page);
|
|
28163
28921
|
case "conversation":
|
|
28164
|
-
return this.conversations.
|
|
28922
|
+
return this.conversations.conversation(request3.id);
|
|
28923
|
+
case "conversation-turn":
|
|
28924
|
+
return this.conversations.turnSteps(request3.id, request3.ordinal, request3.before);
|
|
28925
|
+
case "conversation-step":
|
|
28926
|
+
return this.conversations.step(request3.id, request3.turnId, request3.step, request3.part);
|
|
28165
28927
|
case "files":
|
|
28166
28928
|
return this.files.list(request3.path);
|
|
28167
28929
|
case "file":
|
|
@@ -28217,7 +28979,7 @@ class Workbench {
|
|
|
28217
28979
|
for (let e = directory.readSync();e; e = directory.readSync()) {
|
|
28218
28980
|
if (!e.isFile() || !/^[a-f0-9]{64}\.json$/.test(e.name) || entries2.length > 128)
|
|
28219
28981
|
throw new ContractError("corrupt-selected");
|
|
28220
|
-
const saved = files.read(
|
|
28982
|
+
const saved = files.read(join33(this.projectRoot, e.name));
|
|
28221
28983
|
if (!saved)
|
|
28222
28984
|
throw new ContractError("corrupt-selected");
|
|
28223
28985
|
const row = closed(parseWireJson(saved.bytes.toString("utf8")), [
|
|
@@ -28286,7 +29048,7 @@ class Workbench {
|
|
|
28286
29048
|
if (this.opening.size >= 64)
|
|
28287
29049
|
throw new ContractError("capacity");
|
|
28288
29050
|
const path = this.files.directory(request3.path);
|
|
28289
|
-
const marker2 =
|
|
29051
|
+
const marker2 = join33(this.o.coordination.directory, "conversations-v1", "project-commands", `${request3.commandId}.json`);
|
|
28290
29052
|
const files = this.o.coordination.files;
|
|
28291
29053
|
if (files.read(marker2))
|
|
28292
29054
|
throw new ContractError("receipt-inconclusive");
|
|
@@ -28304,7 +29066,7 @@ class Workbench {
|
|
|
28304
29066
|
}
|
|
28305
29067
|
const opened = await openProjectDirectory(path, this.o.coordination.stateHome, this.o.agentDir, this.o.hostUrl);
|
|
28306
29068
|
for (const root of [this.o.cwd, opened.path]) {
|
|
28307
|
-
const entry =
|
|
29069
|
+
const entry = join33(this.projectRoot, `${hash2(root)}.json`);
|
|
28308
29070
|
const saved = files.read(entry);
|
|
28309
29071
|
if (!saved)
|
|
28310
29072
|
files.create(entry, canonicalJson({ path: root }));
|
|
@@ -28334,7 +29096,7 @@ async function predecessorExited() {
|
|
|
28334
29096
|
} catch {
|
|
28335
29097
|
return;
|
|
28336
29098
|
}
|
|
28337
|
-
await new Promise((
|
|
29099
|
+
await new Promise((resolve12) => setTimeout(resolve12, 100));
|
|
28338
29100
|
}
|
|
28339
29101
|
}
|
|
28340
29102
|
async function healBeforeStart(c) {
|
|
@@ -28372,8 +29134,8 @@ function legacy(store, budget, reader) {
|
|
|
28372
29134
|
decodeId(entry.name);
|
|
28373
29135
|
if (created.has(entry.name))
|
|
28374
29136
|
continue;
|
|
28375
|
-
const path =
|
|
28376
|
-
const size2 =
|
|
29137
|
+
const path = join34(store.tasks, entry.name, "task.json");
|
|
29138
|
+
const size2 = lstatSync22(path).size;
|
|
28377
29139
|
if (size2 > 128 * 1024)
|
|
28378
29140
|
throw new ContractError("corrupt-selected");
|
|
28379
29141
|
budget.reserve(size2);
|
|
@@ -28405,9 +29167,9 @@ var processLifetime = {
|
|
|
28405
29167
|
const restarts = process.uptime() > 600 ? 0 : previous;
|
|
28406
29168
|
if (!Number.isSafeInteger(restarts) || restarts >= SELF_RESTARTS)
|
|
28407
29169
|
return;
|
|
28408
|
-
const child =
|
|
29170
|
+
const child = spawn5(process.execPath, [
|
|
28409
29171
|
...process.execArgv,
|
|
28410
|
-
|
|
29172
|
+
fileURLToPath7(new URL("./service-host.mjs", import.meta.url))
|
|
28411
29173
|
], {
|
|
28412
29174
|
detached: true,
|
|
28413
29175
|
stdio: ["pipe", "ignore", "ignore"],
|
|
@@ -28426,7 +29188,7 @@ var processLifetime = {
|
|
|
28426
29188
|
}
|
|
28427
29189
|
};
|
|
28428
29190
|
async function createServiceHost(value, sourceBuild, lifetime) {
|
|
28429
|
-
const embedded = "
|
|
29191
|
+
const embedded = "d0edc9d93f682dd7c637bd28cdb756b823ad8eafeabebc63479bee1bac5b2b55";
|
|
28430
29192
|
if (embedded && sourceBuild)
|
|
28431
29193
|
throw new ContractError("unsupported-context");
|
|
28432
29194
|
const contractHash = decodeHash(embedded ?? sourceBuild?.contractHash);
|
|
@@ -28510,8 +29272,8 @@ async function createServiceHost(value, sourceBuild, lifetime) {
|
|
|
28510
29272
|
let timer;
|
|
28511
29273
|
await Promise.race([
|
|
28512
29274
|
owner?.idle(),
|
|
28513
|
-
new Promise((
|
|
28514
|
-
timer = setTimeout(
|
|
29275
|
+
new Promise((resolve12) => {
|
|
29276
|
+
timer = setTimeout(resolve12, FAULT_SETTLE_MS);
|
|
28515
29277
|
})
|
|
28516
29278
|
]);
|
|
28517
29279
|
clearTimeout(timer);
|
|
@@ -28702,6 +29464,7 @@ async function createServiceHost(value, sourceBuild, lifetime) {
|
|
|
28702
29464
|
return {
|
|
28703
29465
|
status: heldOwner.state === "idle" ? ready2 ? "alive" : "unavailable" : heldOwner.state,
|
|
28704
29466
|
epoch: native.epoch,
|
|
29467
|
+
work: heldOwner.acceptedPending,
|
|
28705
29468
|
actions: heldOwner.state === "idle" && !!workbench && ready2 && !!choice.snapshot().selected,
|
|
28706
29469
|
...operation === "diagnose" ? {
|
|
28707
29470
|
recording: heldWriter.quarantined ? "quarantined" : "available",
|