@deksden-com/dd-flow-cli 0.9.0-beta.9 → 0.9.0-beta.90
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/CHANGELOG.md +460 -0
- package/README.md +65 -0
- package/dist/build-info.json +8 -8
- package/dist/cli/command-inputs.js +304 -0
- package/dist/cli/help.js +89 -13
- package/dist/cli/hook-ingress.js +82 -0
- package/dist/cli/input-preparation.js +115 -0
- package/dist/cli/run-cli.js +1409 -382
- package/dist/cli.js +6 -2
- package/dist/harness-runtime/bin/dd-agy.mjs +37 -0
- package/dist/harness-runtime/bin/dd-codex.mjs +25 -0
- package/dist/harness-runtime/bin/dd-droid.mjs +33 -0
- package/dist/harness-runtime/bin/dd-grok.mjs +30 -0
- package/dist/harness-runtime/bin/dd-opencode.mjs +21 -0
- package/dist/harness-runtime/bin/dd-zcode.mjs +83 -0
- package/dist/harness-runtime/lib/daemon-operations.mjs +215 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.mjs +423 -0
- package/dist/harness-runtime/lib/dd-agy.mjs +59 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.mjs +146 -0
- package/dist/harness-runtime/lib/dd-codex.mjs +562 -0
- package/dist/harness-runtime/lib/dd-droid-daemon.mjs +125 -0
- package/dist/harness-runtime/lib/dd-droid.mjs +471 -0
- package/dist/harness-runtime/lib/dd-grok-daemon.mjs +274 -0
- package/dist/harness-runtime/lib/dd-grok.mjs +174 -0
- package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +198 -0
- package/dist/harness-runtime/lib/dd-opencode.mjs +98 -0
- package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +644 -0
- package/dist/harness-runtime/lib/dd-zcode.mjs +910 -0
- package/dist/harness-runtime/lib/delegation-instructions.d.mts +10 -0
- package/dist/harness-runtime/lib/delegation-instructions.mjs +130 -0
- package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
- package/dist/harness-runtime/lib/driver-recovery.mjs +129 -0
- package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
- package/dist/harness-runtime/lib/managed-daemon.mjs +260 -0
- package/dist/harness-runtime/lib/model-observations.mjs +77 -0
- package/dist/harness-runtime/lib/native-hook-command.d.mts +1 -0
- package/dist/harness-runtime/lib/native-hook-command.mjs +34 -0
- package/dist/harness-runtime/lib/observation-clock.mjs +36 -0
- package/dist/harness-runtime/lib/operation-context.mjs +5 -0
- package/dist/harness-runtime/lib/operation-errors.mjs +19 -0
- package/dist/harness-runtime/lib/process-json.mjs +70 -0
- package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
- package/dist/harness-runtime/lib/runner-events.mjs +200 -0
- package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
- package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
- package/dist/harness-runtime/lib/tool-observations.d.mts +17 -0
- package/dist/harness-runtime/lib/tool-observations.mjs +153 -0
- package/dist/schemas/agent-profile.schema.json +1 -1
- package/dist/schemas/code-review-result.schema.json +2 -2
- package/dist/schemas/code-work-batch.schema.json +4 -3
- package/dist/schemas/code-work-result.schema.json +4 -4
- package/dist/schemas/plan-review-result.schema.json +2 -2
- package/dist/schemas/run-control-receipt.schema.json +99 -0
- package/dist/schemas/run-control-request.schema.json +36 -0
- package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
- package/dist/services/canon.js +11 -8
- package/dist/services/cleanup.js +57 -13
- package/dist/services/cli-operation-classifier.js +22 -24
- package/dist/services/code-checks.js +543 -96
- package/dist/services/codex-hook-delivery.js +28 -0
- package/dist/services/command-context.js +80 -0
- package/dist/services/config.js +11 -8
- package/dist/services/continuation-outcome.js +20 -0
- package/dist/services/controller-fanout.js +132 -0
- package/dist/services/dashboard.js +103 -31
- package/dist/services/engines.js +121 -76
- package/dist/services/eval-snapshots.js +750 -73
- package/dist/services/execution-policy.js +170 -0
- package/dist/services/external-work-launch.js +110 -0
- package/dist/services/harness-adapter.js +147 -24
- package/dist/services/harness-config.js +2 -0
- package/dist/services/hooks.js +587 -299
- package/dist/services/lanes.js +61 -53
- package/dist/services/lifecycle-command.js +118 -7
- package/dist/services/lifecycle-invocations.js +1055 -0
- package/dist/services/managed-daemon-binding.js +38 -0
- package/dist/services/managed-processes.js +283 -27
- package/dist/services/merge-queue.js +179 -104
- package/dist/services/merge-server.js +73 -28
- package/dist/services/migrations.js +14 -9
- package/dist/services/native-daemon-history.js +49 -0
- package/dist/services/native-session-control.js +39 -0
- package/dist/services/plan-runtime.js +19 -8
- package/dist/services/plans.js +28 -25
- package/dist/services/projects.js +18 -5
- package/dist/services/prompts.js +23 -16
- package/dist/services/protocols.js +64 -20
- package/dist/services/recovery-observation-budget.js +61 -0
- package/dist/services/recovery-snapshot-database.js +107 -0
- package/dist/services/repair-intents.js +40 -0
- package/dist/services/run-control-receipt.js +56 -0
- package/dist/services/run-control-worker.js +363 -0
- package/dist/services/run-control.js +876 -0
- package/dist/services/run-controller-adapter.js +222 -0
- package/dist/services/run-controller-capture.js +134 -0
- package/dist/services/run-controller-process.js +189 -0
- package/dist/services/run-controller-recovery.js +222 -0
- package/dist/services/run-controller-state.js +36 -0
- package/dist/services/run-controller.js +789 -0
- package/dist/services/run-engine-bindings.js +1 -1
- package/dist/services/run-fork.js +138 -0
- package/dist/services/run-observations.js +112 -0
- package/dist/services/run-recovery-runtime.js +69 -0
- package/dist/services/run-recovery.js +343 -0
- package/dist/services/runs.js +368 -74
- package/dist/services/runtime-budget.js +434 -0
- package/dist/services/runtime-command.js +43 -0
- package/dist/services/runtime-scope-capture.js +50 -0
- package/dist/services/runtime-scope-control.js +450 -0
- package/dist/services/runtime-scope-resume.js +565 -0
- package/dist/services/runtime-scope-stop.js +99 -0
- package/dist/services/runtime-scope-worker.js +243 -0
- package/dist/services/runtime-service.js +97 -0
- package/dist/services/schema-validation.js +10 -8
- package/dist/services/sessions.js +35 -69
- package/dist/services/stage-blocker.js +17 -7
- package/dist/services/stage-context.js +44 -18
- package/dist/services/stage-lifecycle.js +99 -102
- package/dist/services/stage-pause.js +84 -55
- package/dist/services/stage-work-graph.js +35 -0
- package/dist/services/usage.js +37 -93
- package/dist/services/vnext-code-review.js +195 -88
- package/dist/services/vnext-code.js +285 -252
- package/dist/services/vnext-execution-profile.js +5 -3
- package/dist/services/vnext-fanout.js +160 -17
- package/dist/services/vnext-merge.js +291 -104
- package/dist/services/vnext-plan-review.js +139 -81
- package/dist/services/vnext-plan.js +138 -69
- package/dist/services/vnext-protocolize.js +129 -56
- package/dist/services/vnext-specify.js +88 -65
- package/dist/services/work-registry.js +716 -168
- package/dist/services/workspace-bootstrap.js +76 -0
- package/dist/services/worktrees.js +85 -35
- package/dist/shared/entity-references.js +8 -0
- package/dist/shared/errors.js +12 -0
- package/dist/storage/database.js +459 -32
- package/dist/storage/paths.js +17 -4
- package/dist/storage/work-references.js +23 -0
- package/dist/storage/writer-contract.js +77 -0
- package/dist/storage/writer-migration.js +102 -0
- package/package.json +24 -13
- package/tools/repair-paused-run-status.mjs +59 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { AppError } from "../shared/errors.js";
|
|
4
|
+
import { managedProcessRuntimeHome, managedProcessStatus } from "./managed-processes.js";
|
|
5
|
+
export function readNativeDaemonState(stateDir) {
|
|
6
|
+
if (!path.isAbsolute(stateDir))
|
|
7
|
+
throw new AppError("daemon_owner_mismatch", "Native state directory must be absolute", 1);
|
|
8
|
+
return JSON.parse(fs.readFileSync(path.join(stateDir, "daemon.json"), "utf8"));
|
|
9
|
+
}
|
|
10
|
+
/** A shared directory or logical owner is not a physical provider binding. */
|
|
11
|
+
export function managedProviderBelongsToDaemon(provider, daemon, state) {
|
|
12
|
+
const child = JSON.parse(provider.metadata_json), parent = JSON.parse(daemon.metadata_json);
|
|
13
|
+
return provider.kind === daemon.kind.replace(/-daemon$/, "-provider")
|
|
14
|
+
&& state.provider_process?.id === provider.id && state.provider_process.lease_token === provider.lease_token
|
|
15
|
+
&& Boolean(provider.pid && provider.pid_started_at && daemon.pid && daemon.pid_started_at)
|
|
16
|
+
&& state.provider_pid === provider.pid && child.owner_pid === daemon.pid && child.owner_pid_started_at === daemon.pid_started_at
|
|
17
|
+
&& provider.owner_id === daemon.owner_id && provider.project_id === daemon.project_id && provider.run_id === daemon.run_id && provider.work_id === daemon.work_id
|
|
18
|
+
&& child.state_dir === parent.state_dir && child.role === parent.role && child.dd_flow_home === parent.dd_flow_home && child.adapter_executable === parent.adapter_executable
|
|
19
|
+
&& child.dispatch_barrier === "run-generation@1" && child.budget?.scope_id === parent.budget?.scope_id;
|
|
20
|
+
}
|
|
21
|
+
/** Identity proof only. Liveness and clean shutdown are separate caller decisions. */
|
|
22
|
+
export function requireManagedDaemonBinding(context, input) {
|
|
23
|
+
const state = readNativeDaemonState(input.stateDir);
|
|
24
|
+
const owner = state.config?.runtime_owner;
|
|
25
|
+
const record = managedProcessStatus(context).find(record => record.id === state.resource_process?.id);
|
|
26
|
+
const metadata = record && JSON.parse(record.metadata_json);
|
|
27
|
+
if (!record || !owner || !record.kind.endsWith("-daemon")
|
|
28
|
+
|| record.lease_token !== state.resource_process?.lease_token
|
|
29
|
+
|| !record.pid || record.pid !== state.pid || !record.pid_started_at
|
|
30
|
+
|| record.owner_id !== owner.owner_id || input.ownerId !== undefined && record.owner_id !== input.ownerId
|
|
31
|
+
|| record.project_id !== input.projectId || owner.project_id !== input.projectId
|
|
32
|
+
|| record.run_id !== input.runId || owner.run_id !== input.runId
|
|
33
|
+
|| metadata?.state_dir !== input.stateDir || owner.state_dir !== input.stateDir
|
|
34
|
+
|| owner.dd_flow_home !== context.ddFlowHome || managedProcessRuntimeHome(record, context) !== context.ddFlowHome) {
|
|
35
|
+
throw new AppError("daemon_owner_mismatch", "Native daemon has no proven physical ownership in this RUN and runtime home", 1);
|
|
36
|
+
}
|
|
37
|
+
return { state, process: record };
|
|
38
|
+
}
|
|
@@ -1,28 +1,147 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
2
4
|
import { spawnSync } from "node:child_process";
|
|
3
5
|
import net from "node:net";
|
|
4
6
|
import { getResourceDatabase } from "../storage/database.js";
|
|
7
|
+
import { AppError } from "../shared/errors.js";
|
|
8
|
+
export function managedProcessBinding(record, home) {
|
|
9
|
+
return { resource_home: home, process_id: record.id, registration_sha256: crypto.createHash("sha256").update(JSON.stringify([home, record.id, record.kind, record.owner_id, record.lease_token, record.pid, record.pid_started_at, record.project_id, record.run_id, record.work_id, record.check_id, record.operation_id, record.metadata_json])).digest("hex") };
|
|
10
|
+
}
|
|
5
11
|
const defaultLeaseMs = 15 * 60_000;
|
|
6
12
|
export function resourceHome(context) {
|
|
7
13
|
return context.env?.DD_FLOW_RESOURCE_HOME ?? context.ddFlowHome ?? "/tmp/dd-flow-runtime";
|
|
8
14
|
}
|
|
9
|
-
|
|
15
|
+
/** Runtime IDs are home-local; a shared host registry needs this extra scope. */
|
|
16
|
+
export function managedProcessRuntimeHome(record, authority) {
|
|
17
|
+
const metadata = parseMetadata(record.metadata_json);
|
|
18
|
+
if (metadata.dd_flow_home !== undefined)
|
|
19
|
+
return typeof metadata.dd_flow_home === "string" && path.isAbsolute(metadata.dd_flow_home) ? metadata.dd_flow_home : null;
|
|
20
|
+
// Older native registrations can still prove their home from the retained
|
|
21
|
+
// exact daemon owner; PID or project/RUN labels alone never establish it.
|
|
22
|
+
if (typeof metadata.state_dir === "string" && path.isAbsolute(metadata.state_dir)) {
|
|
23
|
+
try {
|
|
24
|
+
const state = JSON.parse(fs.readFileSync(path.join(metadata.state_dir, "daemon.json"), "utf8"));
|
|
25
|
+
const owner = state.config?.runtime_owner;
|
|
26
|
+
if (owner?.owner_id === record.owner_id && owner.project_id === record.project_id && owner.run_id === record.run_id && owner.state_dir === metadata.state_dir && typeof owner.dd_flow_home === "string" && path.isAbsolute(owner.dd_flow_home))
|
|
27
|
+
return owner.dd_flow_home;
|
|
28
|
+
}
|
|
29
|
+
catch { /* No retained native scope proof. */ }
|
|
30
|
+
}
|
|
31
|
+
if (record.kind === "run-controller" && authority.db.get("SELECT 1 FROM sqlite_schema WHERE name = 'run_controllers'") && authority.db.get("SELECT 1 FROM run_controllers WHERE controller_id = ? AND process_id = ? AND process_lease_token = ? AND project_id = ? AND run_id = ?", [record.owner_id, record.id, record.lease_token, record.project_id, record.run_id]))
|
|
32
|
+
return authority.ddFlowHome;
|
|
33
|
+
if (record.kind === "run-control" && authority.db.get("SELECT 1 FROM sqlite_schema WHERE name = 'run_control_workers'") && authority.db.get("SELECT 1 FROM run_control_workers w JOIN run_controls c ON c.control_id = w.control_id WHERE w.control_id = ? AND w.process_id = ? AND w.process_lease_token = ? AND c.project_id = ? AND c.run_id = ?", [record.owner_id, record.id, record.lease_token, record.project_id, record.run_id]))
|
|
34
|
+
return authority.ddFlowHome;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
export function prepareManagedProcessRegistration(context, input) {
|
|
38
|
+
requireProcessText(input.kind, "kind");
|
|
39
|
+
requireProcessText(input.ownerId, "owner");
|
|
40
|
+
for (const field of ["id", "projectId", "runId", "workId", "checkId", "operationId", "stdoutPath", "stderrPath"]) {
|
|
41
|
+
if (input[field] !== undefined && input[field] !== null)
|
|
42
|
+
requireProcessText(input[field], field);
|
|
43
|
+
}
|
|
44
|
+
for (const field of ["pid", "ownerPid", "processGroupId"]) {
|
|
45
|
+
if (input[field] !== undefined)
|
|
46
|
+
requireProcessPid(input[field], field);
|
|
47
|
+
}
|
|
48
|
+
if (input.uniqueActiveOperation !== undefined && typeof input.uniqueActiveOperation !== "boolean")
|
|
49
|
+
throw new AppError("validation", "uniqueActiveOperation must be boolean", 2);
|
|
50
|
+
if (input.metadata !== undefined && (!input.metadata || typeof input.metadata !== "object" || Array.isArray(input.metadata)))
|
|
51
|
+
throw new AppError("validation", "metadata must be a JSON object", 2);
|
|
52
|
+
prepareManagedProcessLease(context, input.leaseMs);
|
|
53
|
+
const observer = input.kind === "eval-observer";
|
|
54
|
+
const observerBirth = observer && input.pid ? processStartedAt(input.pid) : null;
|
|
55
|
+
if (observer) {
|
|
56
|
+
if (!Number.isSafeInteger(input.pid) || !input.pid || input.pid < 1 || !observerBirth || input.ownerPid !== undefined && input.ownerPid !== input.pid || input.processGroupId !== undefined && input.processGroupId !== input.pid)
|
|
57
|
+
throw new AppError("process_ownership_unknown", "EVAL observer registration requires its live self-owned process identity", 1);
|
|
58
|
+
input = { ...input, ownerPid: input.pid, uniqueActiveOperation: true };
|
|
59
|
+
}
|
|
60
|
+
if (input.uniqueActiveOperation && !input.operationId)
|
|
61
|
+
throw new AppError("validation", "Unique managed operation requires operationId", 2);
|
|
62
|
+
let metadataJson;
|
|
63
|
+
try {
|
|
64
|
+
metadataJson = JSON.stringify({ ...input.metadata, dd_flow_home: context.ddFlowHome, ...(input.ownerPid ? { owner_pid: input.ownerPid, owner_pid_started_at: processStartedAt(input.ownerPid) } : {}) });
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
throw new AppError("validation", "metadata must be JSON-serializable", 2);
|
|
68
|
+
}
|
|
69
|
+
const reader = getResourceDatabase(resourceHome(context), "read_existing");
|
|
70
|
+
try {
|
|
71
|
+
if (input.id && reader.get("SELECT 1 FROM managed_processes WHERE id = ?", [input.id]))
|
|
72
|
+
throw new AppError("validation", "Managed process ID already exists; use its retained registration", 2, { process_id: input.id });
|
|
73
|
+
if (input.uniqueActiveOperation) {
|
|
74
|
+
const existing = reader.get("SELECT * FROM managed_processes WHERE operation_id = ? AND state IN ('starting','running','stopping','orphaned')", [input.operationId]);
|
|
75
|
+
if (existing)
|
|
76
|
+
throw new AppError("managed_operation_in_progress", "The operation already owns a managed process; observe it instead of spawning a duplicate", 1, { process_id: existing.id, operation_id: input.operationId });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
reader.close?.();
|
|
81
|
+
}
|
|
82
|
+
return { input, observer, observerBirth, metadataJson };
|
|
83
|
+
}
|
|
84
|
+
export function registerManagedProcess(context, rawInput) {
|
|
85
|
+
const { input, observer, observerBirth, metadataJson } = prepareManagedProcessRegistration(context, rawInput);
|
|
86
|
+
if (!input.uniqueActiveOperation)
|
|
87
|
+
return insertManagedProcess(context, input, metadataJson);
|
|
88
|
+
const db = registry(context);
|
|
89
|
+
db.beginWriteTransaction();
|
|
90
|
+
try {
|
|
91
|
+
const existing = db.get("SELECT * FROM managed_processes WHERE operation_id = ? AND state IN ('starting','running','stopping','orphaned')", [input.operationId]);
|
|
92
|
+
if (existing)
|
|
93
|
+
throw new AppError("managed_operation_in_progress", "The operation already owns a managed process; observe it instead of spawning a duplicate", 1, { process_id: existing.id, operation_id: input.operationId });
|
|
94
|
+
let result = insertManagedProcess(context, input, metadataJson);
|
|
95
|
+
if (observer) {
|
|
96
|
+
result = confirmManagedProcess(context, { id: result.id, leaseToken: result.lease_token, pid: input.pid, ...(input.processGroupId ? { processGroupId: input.processGroupId } : {}) });
|
|
97
|
+
if (result.pid_started_at !== observerBirth || !processIsAlive(result))
|
|
98
|
+
throw new AppError("process_ownership_unknown", "EVAL observer changed before registration committed", 1);
|
|
99
|
+
}
|
|
100
|
+
db.exec("COMMIT");
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
db.exec("ROLLBACK");
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function insertManagedProcess(context, input, metadataJson) {
|
|
10
109
|
const db = registry(context);
|
|
11
110
|
const now = context.now();
|
|
12
111
|
const id = input.id ?? `PROC-${crypto.randomUUID()}`;
|
|
13
112
|
const token = crypto.randomUUID();
|
|
14
113
|
db.run(`INSERT INTO managed_processes
|
|
15
114
|
(id, kind, pid, pid_started_at, owner_id, lease_token, lease_expires_at, project_id, run_id, work_id, check_id, operation_id, stdout_path, stderr_path, state, started_at, updated_at, finished_at, termination_reason, metadata_json)
|
|
16
|
-
VALUES (?, ?, NULL, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'starting', ?, ?, NULL, NULL, ?)`, [id, input.kind, input.ownerId, token, leaseExpiry(now, input.leaseMs), input.projectId ?? null, input.runId ?? null, input.workId ?? null, input.checkId ?? null, input.operationId ?? null, input.stdoutPath ?? null, input.stderrPath ?? null, now, now,
|
|
115
|
+
VALUES (?, ?, NULL, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'starting', ?, ?, NULL, NULL, ?)`, [id, input.kind, input.ownerId, token, leaseExpiry(now, input.leaseMs), input.projectId ?? null, input.runId ?? null, input.workId ?? null, input.checkId ?? null, input.operationId ?? null, input.stdoutPath ?? null, input.stderrPath ?? null, now, now, metadataJson]);
|
|
17
116
|
return requireProcess(db, id);
|
|
18
117
|
}
|
|
118
|
+
export function prepareManagedProcessConfirmation(context, input) {
|
|
119
|
+
requireProcessText(input.id, "id");
|
|
120
|
+
requireProcessText(input.leaseToken, "lease-token");
|
|
121
|
+
requireProcessPid(input.pid, "pid");
|
|
122
|
+
if (input.ownerPid !== undefined)
|
|
123
|
+
requireProcessPid(input.ownerPid, "owner-pid");
|
|
124
|
+
if (input.processGroupId !== undefined && input.processGroupId !== null)
|
|
125
|
+
requireProcessPid(input.processGroupId, "process-group-id");
|
|
126
|
+
prepareManagedProcessLease(context, input.leaseMs);
|
|
127
|
+
const current = readManagedProcess(context, input.id);
|
|
128
|
+
if (current.lease_token !== input.leaseToken)
|
|
129
|
+
throw new AppError("process_lease_lost", "Managed process lease does not match", 1);
|
|
130
|
+
if (current.state !== "starting")
|
|
131
|
+
throw new AppError("process_confirmation_rejected", "Only a starting managed process can be confirmed", 1, { process_id: current.id, state: current.state });
|
|
132
|
+
return current;
|
|
133
|
+
}
|
|
19
134
|
export function confirmManagedProcess(context, input) {
|
|
135
|
+
const current = prepareManagedProcessConfirmation(context, input);
|
|
20
136
|
const db = registry(context);
|
|
21
137
|
const now = context.now();
|
|
22
|
-
const current = requireProcess(db, input.id);
|
|
23
138
|
const metadata = parseMetadata(current.metadata_json);
|
|
24
139
|
if (input.processGroupId)
|
|
25
140
|
metadata.process_group_id = input.processGroupId;
|
|
141
|
+
if (input.ownerPid) {
|
|
142
|
+
metadata.owner_pid = input.ownerPid;
|
|
143
|
+
metadata.owner_pid_started_at = processStartedAt(input.ownerPid);
|
|
144
|
+
}
|
|
26
145
|
const result = db.run(`UPDATE managed_processes
|
|
27
146
|
SET pid = ?, pid_started_at = ?, state = 'running', lease_expires_at = ?, updated_at = ?, metadata_json = ?
|
|
28
147
|
WHERE id = ? AND lease_token = ? AND state = 'starting'`, [input.pid, processStartedAt(input.pid), leaseExpiry(now, input.leaseMs), now, JSON.stringify(metadata), input.id, input.leaseToken]);
|
|
@@ -30,17 +149,49 @@ export function confirmManagedProcess(context, input) {
|
|
|
30
149
|
throw new Error(`Managed process cannot be confirmed: ${input.id}`);
|
|
31
150
|
return requireProcess(db, input.id);
|
|
32
151
|
}
|
|
152
|
+
export function prepareManagedProcessHeartbeat(context, input) {
|
|
153
|
+
requireProcessText(input.id, "id");
|
|
154
|
+
requireProcessText(input.leaseToken, "lease-token");
|
|
155
|
+
prepareManagedProcessLease(context, input.leaseMs);
|
|
156
|
+
}
|
|
33
157
|
export function heartbeatManagedProcess(context, input) {
|
|
158
|
+
prepareManagedProcessHeartbeat(context, input);
|
|
159
|
+
const reader = getResourceDatabase(resourceHome(context), "read_existing");
|
|
160
|
+
try {
|
|
161
|
+
if (!reader.get("SELECT 1 FROM managed_processes WHERE id = ? AND lease_token = ? AND state IN ('starting','running','stopping')", [input.id, input.leaseToken]))
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
reader.close?.();
|
|
166
|
+
}
|
|
34
167
|
const db = registry(context);
|
|
35
168
|
return db.run("UPDATE managed_processes SET lease_expires_at = ?, updated_at = ? WHERE id = ? AND lease_token = ? AND state IN ('starting','running','stopping')", [leaseExpiry(context.now(), input.leaseMs), context.now(), input.id, input.leaseToken]).changes === 1;
|
|
36
169
|
}
|
|
170
|
+
export function prepareManagedProcessFinish(context, input) {
|
|
171
|
+
requireProcessText(input.id, "id");
|
|
172
|
+
requireProcessText(input.leaseToken, "lease-token");
|
|
173
|
+
if (input.state !== "stopped" && input.state !== "failed")
|
|
174
|
+
throw new AppError("validation", "--state must be stopped or failed", 2);
|
|
175
|
+
if (input.reason !== undefined && input.reason !== null && typeof input.reason !== "string")
|
|
176
|
+
throw new AppError("validation", "reason must be a string or null", 2);
|
|
177
|
+
return readManagedProcess(context, input.id);
|
|
178
|
+
}
|
|
37
179
|
export function finishManagedProcess(context, input) {
|
|
180
|
+
const existing = prepareManagedProcessFinish(context, input);
|
|
181
|
+
if (existing.lease_token === input.leaseToken && existing.state === input.state)
|
|
182
|
+
return true;
|
|
183
|
+
if (existing.lease_token !== input.leaseToken || !["starting", "running", "stopping", "orphaned"].includes(existing.state))
|
|
184
|
+
return false;
|
|
38
185
|
const db = registry(context);
|
|
39
186
|
const now = context.now();
|
|
40
187
|
const updated = db.run("UPDATE managed_processes SET state = ?, termination_reason = ?, finished_at = ?, updated_at = ?, lease_expires_at = ? WHERE id = ? AND lease_token = ? AND state IN ('starting','running','stopping','orphaned')", [input.state, input.reason ?? null, now, now, now, input.id, input.leaseToken]).changes === 1;
|
|
41
188
|
if (updated)
|
|
42
189
|
db.run("DELETE FROM managed_resources WHERE process_id = ?", [input.id]);
|
|
43
|
-
|
|
190
|
+
if (updated)
|
|
191
|
+
return true;
|
|
192
|
+
// Another client may have completed the same lease after our initial read.
|
|
193
|
+
const current = requireProcess(db, input.id);
|
|
194
|
+
return current.lease_token === input.leaseToken && current.state === input.state;
|
|
44
195
|
}
|
|
45
196
|
export function processIsAlive(record) {
|
|
46
197
|
if (!record.pid)
|
|
@@ -48,33 +199,49 @@ export function processIsAlive(record) {
|
|
|
48
199
|
try {
|
|
49
200
|
process.kill(record.pid, 0);
|
|
50
201
|
}
|
|
51
|
-
catch {
|
|
52
|
-
return
|
|
202
|
+
catch (error) {
|
|
203
|
+
return error.code !== "ESRCH";
|
|
53
204
|
}
|
|
54
|
-
|
|
205
|
+
const started = processStartedAt(record.pid);
|
|
206
|
+
return !record.pid_started_at || !started || record.pid_started_at === started;
|
|
55
207
|
}
|
|
56
|
-
function processTreeIsAlive(record) {
|
|
208
|
+
export function processTreeIsAlive(record) {
|
|
209
|
+
if (processIsAlive(record))
|
|
210
|
+
return true;
|
|
57
211
|
const group = parseMetadata(record.metadata_json).process_group_id;
|
|
58
212
|
if (process.platform !== "win32" && typeof group === "number") {
|
|
59
213
|
try {
|
|
60
214
|
process.kill(-group, 0);
|
|
61
215
|
return true;
|
|
62
216
|
}
|
|
63
|
-
catch {
|
|
64
|
-
return
|
|
217
|
+
catch (error) {
|
|
218
|
+
return error.code !== "ESRCH";
|
|
65
219
|
}
|
|
66
220
|
}
|
|
67
|
-
return
|
|
221
|
+
return false;
|
|
68
222
|
}
|
|
69
|
-
|
|
223
|
+
export function managedOwnerIsAlive(record) {
|
|
224
|
+
const metadata = parseMetadata(record.metadata_json);
|
|
225
|
+
if (typeof metadata.owner_pid !== "number")
|
|
226
|
+
return null;
|
|
227
|
+
return processIsAlive({ pid: metadata.owner_pid, pid_started_at: typeof metadata.owner_pid_started_at === "string" ? metadata.owner_pid_started_at : null });
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Expiry proves that observation stopped, not that the owner died. A live PID
|
|
231
|
+
* remains owned until its result is reconciled; only a dead process can be
|
|
232
|
+
* atomically claimed for cleanup.
|
|
233
|
+
*/
|
|
70
234
|
export function claimExpiredManagedProcesses(context, ownerId) {
|
|
71
235
|
const db = registry(context);
|
|
72
236
|
const now = context.now();
|
|
73
|
-
db.
|
|
237
|
+
db.beginWriteTransaction();
|
|
74
238
|
try {
|
|
75
239
|
const candidates = db.all("SELECT * FROM managed_processes WHERE state IN ('starting','running','stopping','orphaned') AND lease_expires_at < ? ORDER BY lease_expires_at, id", [now]);
|
|
76
240
|
const claimed = [];
|
|
77
241
|
for (const candidate of candidates) {
|
|
242
|
+
const ownerAlive = managedOwnerIsAlive(candidate);
|
|
243
|
+
if (ownerAlive === true || (ownerAlive === null && processTreeIsAlive(candidate)))
|
|
244
|
+
continue;
|
|
78
245
|
const token = crypto.randomUUID();
|
|
79
246
|
const result = db.run("UPDATE managed_processes SET owner_id = ?, lease_token = ?, state = 'orphaned', lease_expires_at = ?, updated_at = ? WHERE id = ? AND lease_token = ? AND state IN ('starting','running','stopping','orphaned') AND lease_expires_at < ?", [ownerId, token, leaseExpiry(now), now, candidate.id, candidate.lease_token, now]);
|
|
80
247
|
if (result.changes === 1)
|
|
@@ -88,11 +255,69 @@ export function claimExpiredManagedProcesses(context, ownerId) {
|
|
|
88
255
|
throw error;
|
|
89
256
|
}
|
|
90
257
|
}
|
|
258
|
+
/** Workspace gates are leases, not spawned processes. Children retain their own registrations. */
|
|
259
|
+
export function managedLeaseReleased(record) {
|
|
260
|
+
const metadata = parseMetadata(record.metadata_json);
|
|
261
|
+
return record.kind === "workspace-gate" && record.pid === null && record.pid_started_at === null
|
|
262
|
+
&& metadata.process_group_id == null && metadata.role === "check_gate"
|
|
263
|
+
&& ["stopped", "failed"].includes(record.state) && Boolean(record.finished_at);
|
|
264
|
+
}
|
|
91
265
|
export function managedProcessStatus(context) {
|
|
92
|
-
|
|
266
|
+
const db = getResourceDatabase(resourceHome(context), "read_existing");
|
|
267
|
+
try {
|
|
268
|
+
return db.all("SELECT * FROM managed_processes ORDER BY updated_at DESC, id");
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
db.close?.();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
export function prepareManagedProcessStopOperation(context, input) {
|
|
275
|
+
const graceMs = prepareManagedProcessStop(input);
|
|
276
|
+
const record = readManagedProcess(context, input.id);
|
|
277
|
+
assertStopOwnership(record, input.leaseToken);
|
|
278
|
+
return { record, graceMs };
|
|
279
|
+
}
|
|
280
|
+
function readManagedProcess(context, id) {
|
|
281
|
+
const db = getResourceDatabase(resourceHome(context), "read_existing");
|
|
282
|
+
try {
|
|
283
|
+
return requireProcess(db, id);
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
db.close?.();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function assertStopOwnership(record, leaseToken) {
|
|
290
|
+
if (record.lease_token !== leaseToken)
|
|
291
|
+
throw new AppError("process_lease_lost", "Managed process lease does not match", 1);
|
|
292
|
+
if (["stopped", "failed"].includes(record.state) && !processTreeIsAlive(record))
|
|
293
|
+
return;
|
|
294
|
+
if (!record.pid || !record.pid_started_at)
|
|
295
|
+
throw new AppError("process_identity_unknown", "Managed process has no confirmed physical identity; retain its pending launch", 1);
|
|
296
|
+
}
|
|
297
|
+
export async function stopManagedProcess(context, input) {
|
|
298
|
+
const { graceMs } = prepareManagedProcessStopOperation(context, input);
|
|
299
|
+
const db = registry(context), record = requireProcess(db, input.id);
|
|
300
|
+
assertStopOwnership(record, input.leaseToken);
|
|
301
|
+
if (["stopped", "failed"].includes(record.state) && !processTreeIsAlive(record))
|
|
302
|
+
return record;
|
|
303
|
+
db.run("UPDATE managed_processes SET state = 'stopping', updated_at = ? WHERE id = ? AND lease_token = ? AND state IN ('starting','running','orphaned')", [context.now(), record.id, record.lease_token]);
|
|
304
|
+
if (processTreeIsAlive(record)) {
|
|
305
|
+
terminateOwnedProcess(record, "SIGTERM");
|
|
306
|
+
await delay(graceMs);
|
|
307
|
+
if (input.force !== false && processTreeIsAlive(record)) {
|
|
308
|
+
terminateOwnedProcess(record, "SIGKILL");
|
|
309
|
+
await delay(250);
|
|
310
|
+
}
|
|
311
|
+
if (processTreeIsAlive(record))
|
|
312
|
+
throw Object.assign(new Error("Owned process tree could not be safely stopped; resources retained"), { code: "process_tree_not_settled", details: { process_id: record.id } });
|
|
313
|
+
}
|
|
314
|
+
if (!finishManagedProcess(context, { id: record.id, leaseToken: record.lease_token, state: "stopped", reason: input.reason ?? "requested_stop" }))
|
|
315
|
+
throw Object.assign(new Error("Managed process ownership changed before settlement"), { code: "process_lease_lost" });
|
|
316
|
+
return requireProcess(db, record.id);
|
|
93
317
|
}
|
|
94
318
|
/** Reconcile only records the system owns and has atomically claimed. */
|
|
95
319
|
export async function reconcileExpiredManagedProcesses(context, ownerId, graceMs = 1_000) {
|
|
320
|
+
prepareManagedProcessReconcile(ownerId, graceMs);
|
|
96
321
|
const claimed = claimExpiredManagedProcesses(context, ownerId);
|
|
97
322
|
const outcomes = [];
|
|
98
323
|
for (const processRecord of claimed) {
|
|
@@ -101,22 +326,46 @@ export async function reconcileExpiredManagedProcesses(context, ownerId, graceMs
|
|
|
101
326
|
outcomes.push({ id: processRecord.id, outcome: "already_stopped" });
|
|
102
327
|
continue;
|
|
103
328
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
terminateOwnedProcess(processRecord, "SIGKILL");
|
|
108
|
-
await delay(Math.min(graceMs, 250));
|
|
329
|
+
try {
|
|
330
|
+
await stopManagedProcess(context, { id: processRecord.id, leaseToken: processRecord.lease_token, graceMs, reason: "orphan_reconciled" });
|
|
331
|
+
outcomes.push({ id: processRecord.id, outcome: "stopped" });
|
|
109
332
|
}
|
|
110
|
-
|
|
333
|
+
catch (error) {
|
|
334
|
+
if (error.code !== "process_tree_not_settled")
|
|
335
|
+
throw error;
|
|
111
336
|
outcomes.push({ id: processRecord.id, outcome: "kill_failed" });
|
|
112
|
-
continue;
|
|
113
337
|
}
|
|
114
|
-
finishManagedProcess(context, { id: processRecord.id, leaseToken: processRecord.lease_token, state: "stopped", reason: "orphan_reconciled" });
|
|
115
|
-
outcomes.push({ id: processRecord.id, outcome: "stopped" });
|
|
116
338
|
}
|
|
117
339
|
return outcomes;
|
|
118
340
|
}
|
|
341
|
+
export function prepareManagedProcessStop(input) {
|
|
342
|
+
requireProcessText(input.id, "id");
|
|
343
|
+
requireProcessText(input.leaseToken, "lease-token");
|
|
344
|
+
if (input.reason !== undefined)
|
|
345
|
+
requireProcessText(input.reason, "reason");
|
|
346
|
+
if (input.force !== undefined && typeof input.force !== "boolean")
|
|
347
|
+
throw new AppError("validation", "force must be boolean", 2);
|
|
348
|
+
return prepareProcessGrace(input.graceMs);
|
|
349
|
+
}
|
|
350
|
+
export function prepareManagedProcessReconcile(ownerId, graceMs) {
|
|
351
|
+
requireProcessText(ownerId, "owner");
|
|
352
|
+
return prepareProcessGrace(graceMs);
|
|
353
|
+
}
|
|
354
|
+
function requireProcessText(value, parameter) {
|
|
355
|
+
if (typeof value !== "string" || !value.trim())
|
|
356
|
+
throw new AppError("validation", `--${parameter} must be nonempty text`, 2);
|
|
357
|
+
}
|
|
358
|
+
function requireProcessPid(value, parameter) {
|
|
359
|
+
if (!Number.isSafeInteger(value) || Number(value) <= 0)
|
|
360
|
+
throw new AppError("validation", `--${parameter} must be a positive integer`, 2);
|
|
361
|
+
}
|
|
362
|
+
function prepareProcessGrace(value = 1_000) {
|
|
363
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > 2 ** 31 - 1)
|
|
364
|
+
throw new AppError("validation", "--grace-ms must be a nonnegative integer within the supported timer range", 2);
|
|
365
|
+
return value;
|
|
366
|
+
}
|
|
119
367
|
export async function reservePorts(context, input) {
|
|
368
|
+
prepareManagedProcessLease(context, input.leaseMs);
|
|
120
369
|
const db = registry(context);
|
|
121
370
|
const claims = [];
|
|
122
371
|
const ports = {};
|
|
@@ -144,13 +393,20 @@ export async function reservePorts(context, input) {
|
|
|
144
393
|
}
|
|
145
394
|
function registry(context) { return getResourceDatabase(resourceHome(context)); }
|
|
146
395
|
function requireProcess(db, id) { const row = db.get("SELECT * FROM managed_processes WHERE id = ?", [id]); if (!row)
|
|
147
|
-
throw new
|
|
148
|
-
function
|
|
149
|
-
function
|
|
396
|
+
throw new AppError("not_found", `Managed process is missing: ${id}`, 1); return row; }
|
|
397
|
+
export function prepareManagedProcessLease(context, leaseMs) { return leaseExpiry(context.now(), leaseMs); }
|
|
398
|
+
function leaseExpiry(now, leaseMs = defaultLeaseMs) {
|
|
399
|
+
if (!Number.isSafeInteger(leaseMs) || leaseMs <= 0 || !Number.isFinite(new Date(Date.parse(now) + leaseMs).getTime()))
|
|
400
|
+
throw new AppError("validation", "--lease-ms must be a positive integer within the supported date range", 2);
|
|
401
|
+
return new Date(Date.parse(now) + leaseMs).toISOString();
|
|
402
|
+
}
|
|
403
|
+
export function processStartedAt(pid) { const result = spawnSync("ps", ["-o", "lstart=", "-p", String(pid)], { encoding: "utf8" }); const value = result.status === 0 ? result.stdout.trim() : ""; return value || null; }
|
|
150
404
|
function releasePortClaims(db, claims) { for (const claim of claims)
|
|
151
405
|
db.run("DELETE FROM managed_resources WHERE resource_kind = 'port' AND resource_key = ? AND lease_token = ?", [claim.key, claim.token]); }
|
|
152
406
|
function terminateOwnedProcess(record, signal) {
|
|
153
|
-
|
|
407
|
+
// Never signal a group merely because its former leader PID was once ours.
|
|
408
|
+
// PID reuse or a departed leader makes group ownership unprovable.
|
|
409
|
+
if (!record.pid || !record.pid_started_at || processStartedAt(record.pid) !== record.pid_started_at)
|
|
154
410
|
return;
|
|
155
411
|
const group = parseMetadata(record.metadata_json).process_group_id;
|
|
156
412
|
try {
|