@ran-sh/dsh-crew 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +16 -16
- package/.claude-plugin/plugin.json +14 -14
- package/LICENSE +21 -21
- package/README.de.md +359 -359
- package/README.es.md +359 -359
- package/README.fr.md +359 -359
- package/README.hi.md +359 -359
- package/README.id.md +359 -359
- package/README.ja.md +359 -359
- package/README.ko.md +359 -359
- package/README.md +126 -126
- package/README.pt.md +359 -359
- package/README.ru.md +359 -359
- package/README.th.md +359 -359
- package/README.tr.md +359 -359
- package/README.vi.md +359 -359
- package/README.zh-TW.md +359 -359
- package/README.zh.md +116 -116
- package/agents/ds-flash.md +24 -24
- package/agents/ds-pro.md +24 -24
- package/agents/ds-reviewer.md +23 -23
- package/agents/ds-worker.md +23 -23
- package/bin/dsh-crew.mjs +16 -16
- package/codex/agents/ds-flash.toml +34 -34
- package/codex/agents/ds-pro.toml +34 -34
- package/codex/agents/ds-reviewer.toml +32 -32
- package/codex/agents/ds-worker.toml +32 -32
- package/codex/prompts/dsh-config.md +19 -19
- package/codex/prompts/dsh-status.md +5 -5
- package/commands/config.md +23 -23
- package/commands/off.md +5 -5
- package/commands/on.md +5 -5
- package/commands/status.md +9 -9
- package/cordis.patch.yml +4 -4
- package/docs/gpt-relay-extension.md +103 -103
- package/docs/installation.md +138 -138
- package/docs/job-contracts.md +103 -103
- package/docs/readiness-matrix.md +85 -85
- package/docs/ui-surfaces.md +107 -107
- package/official-web-bridge/cordis.patch.yml +4 -4
- package/official-web-bridge/entry.mjs +1 -1
- package/official-web-bridge/overlay-entry.mjs +59 -59
- package/official-web-bridge/package.json +25 -25
- package/package.json +3 -2
- package/scripts/build-client.mjs +49 -49
- package/scripts/live-crew-smoke.mjs +39 -39
- package/scripts/live-policy-matrix.mjs +177 -177
- package/scripts/policy-probe.mjs +101 -101
- package/scripts/remove-legacy-official-bridge.ps1 +89 -89
- package/scripts/setup.mjs +393 -393
- package/scripts/smoke-real.mjs +110 -110
- package/scripts/smoke.mjs +78 -78
- package/scripts/verify-crew-ui-polish.mjs +145 -145
- package/scripts/verify-history-ui.mjs +97 -97
- package/scripts/verify-installer-fix.mjs +26 -26
- package/scripts/verify-npm-install.mjs +311 -311
- package/scripts/verify-official-bridge-e2e.mjs +192 -192
- package/src/adaptive-routing.mjs +260 -260
- package/src/client/activation-summary.tsx +64 -64
- package/src/client/collapsible-sections.mjs +55 -55
- package/src/client/history-panel.tsx +108 -108
- package/src/client/host-readiness.mjs +71 -71
- package/src/client/index.tsx +1711 -1711
- package/src/client/model-callability-view.mjs +17 -17
- package/src/client/panel-chrome.tsx +40 -40
- package/src/client/quick-entry.tsx +10 -10
- package/src/client/quick-panel.tsx +275 -275
- package/src/client/readiness-envelope.mjs +34 -34
- package/src/client/surface-detection.mjs +43 -43
- package/src/config-readiness.mjs +226 -226
- package/src/credential-reference.mjs +38 -38
- package/src/delivery.mjs +205 -205
- package/src/dsh-cli-runtime.mjs +1021 -1021
- package/src/dsh-cohort.mjs +20 -20
- package/src/extension-contract.mjs +104 -104
- package/src/failure-classification.mjs +201 -201
- package/src/history/admission-gate.mjs +67 -67
- package/src/history/archive-store.mjs +272 -272
- package/src/history/cleanup-plan.mjs +89 -89
- package/src/history/http.mjs +32 -32
- package/src/history/operation.mjs +86 -86
- package/src/history/runner-detach.mjs +34 -34
- package/src/history/runner.mjs +52 -52
- package/src/history/runtime.mjs +36 -36
- package/src/history/service.mjs +177 -177
- package/src/history/state.mjs +27 -27
- package/src/hub/entry.mjs +104 -104
- package/src/hub/index.mjs +2694 -2694
- package/src/hub-client.mjs +154 -154
- package/src/hub-compatibility.mjs +42 -42
- package/src/i18n.mjs +19 -19
- package/src/information-flow.mjs +67 -67
- package/src/install/cli.mjs +28 -28
- package/src/install/install-legacy.mjs +711 -711
- package/src/install/install.mjs +483 -483
- package/src/install/npx-lifecycle.mjs +3456 -3456
- package/src/install/official-frontend-assets.mjs +78 -78
- package/src/install/official-web.mjs +95 -95
- package/src/install/payload-content.mjs +88 -88
- package/src/install/windows-startup.mjs +236 -236
- package/src/install/windows-supervisor-adapter.mjs +443 -443
- package/src/install/windows-supervisor-lifecycle.mjs +782 -782
- package/src/install/zcode.mjs +397 -397
- package/src/job-contracts.mjs +255 -255
- package/src/local-request-guard.mjs +60 -60
- package/src/mcp-runtime.mjs +340 -340
- package/src/model-callability-contract.mjs +79 -79
- package/src/model-catalog.mjs +180 -180
- package/src/model-routing.mjs +586 -586
- package/src/model-schedule.mjs +207 -207
- package/src/official-web-bridge.mjs +447 -447
- package/src/policy.mjs +235 -235
- package/src/provider-delete-adapters.mjs +1934 -1934
- package/src/provider-health.mjs +130 -130
- package/src/provider-inventory.mjs +182 -182
- package/src/provider-layer-migration-adapters.mjs +759 -759
- package/src/provider-layer-migration.mjs +198 -198
- package/src/provider-lifecycle-state.mjs +103 -103
- package/src/provider-lifecycle.mjs +252 -252
- package/src/provider-profile-store.mjs +390 -390
- package/src/provider-settings-store.mjs +633 -633
- package/src/provider-store-lock.mjs +67 -67
- package/src/readiness-matrix.mjs +181 -181
- package/src/removable-waiter.mjs +29 -29
- package/src/role-profiles.mjs +107 -107
- package/src/runtime-controls.mjs +84 -84
- package/src/runtime-identity-contract.mjs +34 -34
- package/src/runtime-identity.mjs +235 -235
- package/src/runtime-readiness-snapshot.mjs +285 -285
- package/src/server.mjs +581 -581
- package/src/session-origins.mjs +60 -60
- package/src/standalone-sdk.mjs +23 -23
- package/src/status-shard.mjs +63 -63
- package/src/structured-error-code.mjs +38 -38
- package/src/supervisor/restart-request.mjs +256 -256
- package/src/workflow-runtime.mjs +739 -739
- package/src/workflow.mjs +155 -155
- package/src/workspace-audit.mjs +231 -231
- package/src/workspace-context.mjs +146 -146
- package/src/workspace-isolation.mjs +463 -455
- package/src/workspace-readiness.mjs +32 -32
- package/statusline/statusline.sh +14 -14
- package/statusline/worker-segment.sh +35 -35
- package/windows/start-dsh-crew.cmd +57 -57
- package/windows/start-dsh-crew.ps1 +1302 -1302
- package/windows/supervisor-control.ps1 +467 -467
- package/worker.cordis.yml +67 -67
- package/zcode/agents/ds-reviewer.md +31 -31
- package/zcode/agents/ds-worker.md +31 -31
- package/zcode/commands/dsh-config.md +17 -17
- package/zcode/commands/dsh-status.md +5 -5
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
// Durable restart-request protocol between the Crew hub (3210) and the
|
|
2
|
-
// Windows Crew supervisor (launcher).
|
|
3
|
-
//
|
|
4
|
-
// The hub must NEVER spawn itself: self-spawn creates an ownership handoff
|
|
5
|
-
// window (port still held, PID reuse, no authority to judge the new
|
|
6
|
-
// process). Instead the hub writes a durable, self-describing restart
|
|
7
|
-
// request; the supervisor (the only process authority) polls, validates the
|
|
8
|
-
// request against its persisted ownership (PID + start time + live
|
|
9
|
-
// runtime_id), executes the restart, and writes a verified result.
|
|
10
|
-
//
|
|
11
|
-
// Directory layout (all under the Crew app root):
|
|
12
|
-
// supervisor/restart-requests/<request-id>.json
|
|
13
|
-
// supervisor/restart-results/<request-id>.json
|
|
14
|
-
// supervisor/heartbeat.json
|
|
15
|
-
// supervisor/ownership.json
|
|
16
|
-
|
|
17
|
-
import { randomUUID } from 'node:crypto';
|
|
18
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
19
|
-
import { dirname, join } from 'node:path';
|
|
20
|
-
|
|
21
|
-
export const RESTART_REQUEST_SCHEMA = 1;
|
|
22
|
-
export const RESTART_REQUEST_TTL_MS = 60_000;
|
|
23
|
-
|
|
24
|
-
export function supervisorStateRoot(appRoot) {
|
|
25
|
-
return join(appRoot, 'supervisor');
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function restartRequestsDir(appRoot) {
|
|
29
|
-
return join(supervisorStateRoot(appRoot), 'restart-requests');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function restartResultsDir(appRoot) {
|
|
33
|
-
return join(supervisorStateRoot(appRoot), 'restart-results');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function heartbeatFile(appRoot) {
|
|
37
|
-
return join(supervisorStateRoot(appRoot), 'heartbeat.json');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function ownershipFile(appRoot) {
|
|
41
|
-
return join(supervisorStateRoot(appRoot), 'ownership.json');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function writeFileAtomic(file, content) {
|
|
45
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
46
|
-
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
47
|
-
writeFileSync(temp, content);
|
|
48
|
-
try {
|
|
49
|
-
renameSync(temp, file);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
try { rmSync(temp, { force: true }); } catch {}
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Create a durable restart request. The runtime identity is taken from the
|
|
58
|
-
* CALLER's own live identity contract — never accepted from the HTTP caller —
|
|
59
|
-
* so a stale or forged request cannot claim a runtime_id it does not hold.
|
|
60
|
-
*/
|
|
61
|
-
export function createRestartRequest({
|
|
62
|
-
appRoot,
|
|
63
|
-
runtimeIdentity,
|
|
64
|
-
reason = null,
|
|
65
|
-
now = Date.now(),
|
|
66
|
-
ttlMs = RESTART_REQUEST_TTL_MS,
|
|
67
|
-
}) {
|
|
68
|
-
return createSupervisorRequest({
|
|
69
|
-
appRoot,
|
|
70
|
-
operation: 'restart',
|
|
71
|
-
runtimeIdentity,
|
|
72
|
-
reason,
|
|
73
|
-
now,
|
|
74
|
-
ttlMs,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Create a durable supervisor request for maintenance transactions
|
|
80
|
-
* (stop/start around a runtime-tree swap). The launcher-mediated npx flow
|
|
81
|
-
* uses these instead of spawning/killing the 3210 itself: the launcher is
|
|
82
|
-
* the only process authority. `lease` binds the stop and start phases of
|
|
83
|
-
* one transaction; `extra` carries the expected versions for verification.
|
|
84
|
-
*/
|
|
85
|
-
export function createSupervisorRequest({
|
|
86
|
-
appRoot,
|
|
87
|
-
operation,
|
|
88
|
-
runtimeIdentity,
|
|
89
|
-
reason = null,
|
|
90
|
-
lease = randomUUID(),
|
|
91
|
-
extra = null,
|
|
92
|
-
now = Date.now(),
|
|
93
|
-
ttlMs = RESTART_REQUEST_TTL_MS,
|
|
94
|
-
}) {
|
|
95
|
-
const request = {
|
|
96
|
-
schema_version: RESTART_REQUEST_SCHEMA,
|
|
97
|
-
request_id: randomUUID(),
|
|
98
|
-
operation,
|
|
99
|
-
execution_plane: runtimeIdentity?.execution_plane ?? 'hub-3210',
|
|
100
|
-
profile: runtimeIdentity?.profile ?? 'dsh-crew',
|
|
101
|
-
port: runtimeIdentity?.listen_port ?? 3210,
|
|
102
|
-
runtime_id: runtimeIdentity?.runtime_id ?? null,
|
|
103
|
-
lease,
|
|
104
|
-
requested_at: now,
|
|
105
|
-
expires_at: now + ttlMs,
|
|
106
|
-
reason: reason ?? null,
|
|
107
|
-
extra,
|
|
108
|
-
};
|
|
109
|
-
if (typeof request.runtime_id !== 'string' || request.runtime_id.length === 0) {
|
|
110
|
-
return { ok: false, code: 'RUNTIME_IDENTITY_INCOMPLETE', error: 'cannot request supervisor action without a live runtime_id' };
|
|
111
|
-
}
|
|
112
|
-
const dir = operation.startsWith('maintenance-') ? maintenanceRequestsDir(appRoot) : restartRequestsDir(appRoot);
|
|
113
|
-
mkdirSync(dir, { recursive: true });
|
|
114
|
-
const file = join(dir, `${request.request_id}.json`);
|
|
115
|
-
writeFileAtomic(file, JSON.stringify(request, null, 2) + '\n');
|
|
116
|
-
return { ok: true, request, file };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function maintenanceRequestsDir(appRoot) {
|
|
120
|
-
return join(supervisorStateRoot(appRoot), 'maintenance-requests');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function maintenanceResultsDir(appRoot) {
|
|
124
|
-
return join(supervisorStateRoot(appRoot), 'maintenance-results');
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function maintenanceSessionFile(appRoot) {
|
|
128
|
-
return join(supervisorStateRoot(appRoot), 'maintenance-session.json');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function readMaintenanceSession(appRoot) {
|
|
132
|
-
const file = maintenanceSessionFile(appRoot);
|
|
133
|
-
if (!existsSync(file)) return { ok: true, state: 'absent', session: null };
|
|
134
|
-
try {
|
|
135
|
-
const session = JSON.parse(readFileSync(file, 'utf8'));
|
|
136
|
-
const valid = session?.schema_version === 1
|
|
137
|
-
&& session.state === 'STOPPED'
|
|
138
|
-
&& typeof session.lease === 'string'
|
|
139
|
-
&& session.lease.length > 0
|
|
140
|
-
&& typeof session.runtime_id === 'string'
|
|
141
|
-
&& session.runtime_id.length > 0
|
|
142
|
-
&& typeof session.request_id === 'string'
|
|
143
|
-
&& session.request_id.length > 0;
|
|
144
|
-
return valid
|
|
145
|
-
? { ok: true, state: 'present', session }
|
|
146
|
-
: { ok: false, state: 'malformed', code: 'MAINTENANCE_SESSION_MALFORMED', session: null };
|
|
147
|
-
} catch (error) {
|
|
148
|
-
return { ok: false, state: 'malformed', code: 'MAINTENANCE_SESSION_MALFORMED', error: error?.message, session: null };
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function readRestartRequest(appRoot, requestId) {
|
|
153
|
-
try {
|
|
154
|
-
const parsed = JSON.parse(readFileSync(join(restartRequestsDir(appRoot), `${requestId}.json`), 'utf8'));
|
|
155
|
-
if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
|
|
156
|
-
} catch { /* absent or malformed */ }
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function listRestartRequests(appRoot) {
|
|
161
|
-
const dir = restartRequestsDir(appRoot);
|
|
162
|
-
if (!existsSync(dir)) return [];
|
|
163
|
-
return readdirSync(dir)
|
|
164
|
-
.filter((name) => name.endsWith('.json'))
|
|
165
|
-
.map((name) => {
|
|
166
|
-
try { return JSON.parse(readFileSync(join(dir, name), 'utf8')); } catch { return null; }
|
|
167
|
-
})
|
|
168
|
-
.filter(Boolean);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export function removeRestartRequest(appRoot, requestId) {
|
|
172
|
-
try { rmSync(join(restartRequestsDir(appRoot), `${requestId}.json`), { force: true }); } catch {}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export function writeRestartResult({ appRoot, request, state, detail = null, now = Date.now() }) {
|
|
176
|
-
const result = {
|
|
177
|
-
schema_version: RESTART_REQUEST_SCHEMA,
|
|
178
|
-
request_id: request.request_id,
|
|
179
|
-
operation: request.operation,
|
|
180
|
-
state,
|
|
181
|
-
runtime_id: request.runtime_id,
|
|
182
|
-
written_at: now,
|
|
183
|
-
detail,
|
|
184
|
-
};
|
|
185
|
-
const file = join(restartResultsDir(appRoot), `${request.request_id}.json`);
|
|
186
|
-
writeFileAtomic(file, JSON.stringify(result, null, 2) + '\n');
|
|
187
|
-
return result;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export function readRestartResult(appRoot, requestId) {
|
|
191
|
-
try {
|
|
192
|
-
const parsed = JSON.parse(readFileSync(join(restartResultsDir(appRoot), `${requestId}.json`), 'utf8'));
|
|
193
|
-
if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
|
|
194
|
-
} catch { /* absent */ }
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/** Observe a fresh heartbeat without granting it restart authority. */
|
|
199
|
-
export function readSupervisorHeartbeatRecord(appRoot, { now = Date.now(), staleAfterMs = 15_000 } = {}) {
|
|
200
|
-
try {
|
|
201
|
-
const parsed = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
|
|
202
|
-
if (parsed?.schema_version !== 1 || !Number.isInteger(parsed.pid) || parsed.pid < 1) return null;
|
|
203
|
-
if (typeof parsed.last_seen !== 'number' || now - parsed.last_seen > staleAfterMs || parsed.last_seen - now > 5_000) return null;
|
|
204
|
-
const hasOwnershipField = Object.hasOwn(parsed, 'ownership_ready');
|
|
205
|
-
const identityReady = typeof parsed.process_started_at_utc_ticks === 'string'
|
|
206
|
-
&& /^\d+$/u.test(parsed.process_started_at_utc_ticks)
|
|
207
|
-
&& parsed.process_started_at_utc_ticks !== '0'
|
|
208
|
-
&& typeof parsed.helper_hash === 'string'
|
|
209
|
-
&& /^[a-f0-9]{64}$/u.test(parsed.helper_hash)
|
|
210
|
-
&& typeof parsed.supervisor_instance_id === 'string'
|
|
211
|
-
&& parsed.supervisor_instance_id.trim().length > 0;
|
|
212
|
-
const state = !hasOwnershipField ? 'legacy-v1' : parsed.ownership_ready === true && identityReady ? 'ready' : 'starting';
|
|
213
|
-
return { state, record: { ...parsed, ownership_source: state } };
|
|
214
|
-
} catch { return null; }
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** Return only an ownership-proven supervisor heartbeat. */
|
|
218
|
-
export function readSupervisorHeartbeat(appRoot, options = {}) {
|
|
219
|
-
const observed = readSupervisorHeartbeatRecord(appRoot, options);
|
|
220
|
-
return observed?.state === 'ready' ? observed.record : null;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export function writeSupervisorHeartbeat({
|
|
224
|
-
appRoot,
|
|
225
|
-
pid,
|
|
226
|
-
now = Date.now(),
|
|
227
|
-
ownershipReady = true,
|
|
228
|
-
processStartedAtUtcTicks = null,
|
|
229
|
-
helperHash = null,
|
|
230
|
-
supervisorInstanceId = randomUUID(),
|
|
231
|
-
}) {
|
|
232
|
-
const record = {
|
|
233
|
-
schema_version: 1,
|
|
234
|
-
supervisor_instance_id: supervisorInstanceId,
|
|
235
|
-
pid,
|
|
236
|
-
process_started_at_utc_ticks: typeof processStartedAtUtcTicks === 'string' ? processStartedAtUtcTicks : null,
|
|
237
|
-
helper_hash: helperHash,
|
|
238
|
-
ownership_ready: ownershipReady === true,
|
|
239
|
-
last_seen: now,
|
|
240
|
-
protocol_version: 1,
|
|
241
|
-
};
|
|
242
|
-
// Merge onto the previous record so the instance id is stable per watcher
|
|
243
|
-
// run (the watcher rewrites this file every few seconds).
|
|
244
|
-
try {
|
|
245
|
-
const prev = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
|
|
246
|
-
const sameWatcher = prev?.schema_version === 1
|
|
247
|
-
&& prev.pid === record.pid
|
|
248
|
-
&& prev.process_started_at_utc_ticks === record.process_started_at_utc_ticks
|
|
249
|
-
&& prev.helper_hash === record.helper_hash
|
|
250
|
-
&& typeof prev.supervisor_instance_id === 'string'
|
|
251
|
-
&& prev.supervisor_instance_id.length > 0;
|
|
252
|
-
if (sameWatcher) record.supervisor_instance_id = prev.supervisor_instance_id;
|
|
253
|
-
} catch { /* first beat */ }
|
|
254
|
-
writeFileAtomic(heartbeatFile(appRoot), JSON.stringify(record, null, 2) + '\n');
|
|
255
|
-
return record;
|
|
256
|
-
}
|
|
1
|
+
// Durable restart-request protocol between the Crew hub (3210) and the
|
|
2
|
+
// Windows Crew supervisor (launcher).
|
|
3
|
+
//
|
|
4
|
+
// The hub must NEVER spawn itself: self-spawn creates an ownership handoff
|
|
5
|
+
// window (port still held, PID reuse, no authority to judge the new
|
|
6
|
+
// process). Instead the hub writes a durable, self-describing restart
|
|
7
|
+
// request; the supervisor (the only process authority) polls, validates the
|
|
8
|
+
// request against its persisted ownership (PID + start time + live
|
|
9
|
+
// runtime_id), executes the restart, and writes a verified result.
|
|
10
|
+
//
|
|
11
|
+
// Directory layout (all under the Crew app root):
|
|
12
|
+
// supervisor/restart-requests/<request-id>.json
|
|
13
|
+
// supervisor/restart-results/<request-id>.json
|
|
14
|
+
// supervisor/heartbeat.json
|
|
15
|
+
// supervisor/ownership.json
|
|
16
|
+
|
|
17
|
+
import { randomUUID } from 'node:crypto';
|
|
18
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
export const RESTART_REQUEST_SCHEMA = 1;
|
|
22
|
+
export const RESTART_REQUEST_TTL_MS = 60_000;
|
|
23
|
+
|
|
24
|
+
export function supervisorStateRoot(appRoot) {
|
|
25
|
+
return join(appRoot, 'supervisor');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function restartRequestsDir(appRoot) {
|
|
29
|
+
return join(supervisorStateRoot(appRoot), 'restart-requests');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function restartResultsDir(appRoot) {
|
|
33
|
+
return join(supervisorStateRoot(appRoot), 'restart-results');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function heartbeatFile(appRoot) {
|
|
37
|
+
return join(supervisorStateRoot(appRoot), 'heartbeat.json');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function ownershipFile(appRoot) {
|
|
41
|
+
return join(supervisorStateRoot(appRoot), 'ownership.json');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function writeFileAtomic(file, content) {
|
|
45
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
46
|
+
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
47
|
+
writeFileSync(temp, content);
|
|
48
|
+
try {
|
|
49
|
+
renameSync(temp, file);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
try { rmSync(temp, { force: true }); } catch {}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a durable restart request. The runtime identity is taken from the
|
|
58
|
+
* CALLER's own live identity contract — never accepted from the HTTP caller —
|
|
59
|
+
* so a stale or forged request cannot claim a runtime_id it does not hold.
|
|
60
|
+
*/
|
|
61
|
+
export function createRestartRequest({
|
|
62
|
+
appRoot,
|
|
63
|
+
runtimeIdentity,
|
|
64
|
+
reason = null,
|
|
65
|
+
now = Date.now(),
|
|
66
|
+
ttlMs = RESTART_REQUEST_TTL_MS,
|
|
67
|
+
}) {
|
|
68
|
+
return createSupervisorRequest({
|
|
69
|
+
appRoot,
|
|
70
|
+
operation: 'restart',
|
|
71
|
+
runtimeIdentity,
|
|
72
|
+
reason,
|
|
73
|
+
now,
|
|
74
|
+
ttlMs,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Create a durable supervisor request for maintenance transactions
|
|
80
|
+
* (stop/start around a runtime-tree swap). The launcher-mediated npx flow
|
|
81
|
+
* uses these instead of spawning/killing the 3210 itself: the launcher is
|
|
82
|
+
* the only process authority. `lease` binds the stop and start phases of
|
|
83
|
+
* one transaction; `extra` carries the expected versions for verification.
|
|
84
|
+
*/
|
|
85
|
+
export function createSupervisorRequest({
|
|
86
|
+
appRoot,
|
|
87
|
+
operation,
|
|
88
|
+
runtimeIdentity,
|
|
89
|
+
reason = null,
|
|
90
|
+
lease = randomUUID(),
|
|
91
|
+
extra = null,
|
|
92
|
+
now = Date.now(),
|
|
93
|
+
ttlMs = RESTART_REQUEST_TTL_MS,
|
|
94
|
+
}) {
|
|
95
|
+
const request = {
|
|
96
|
+
schema_version: RESTART_REQUEST_SCHEMA,
|
|
97
|
+
request_id: randomUUID(),
|
|
98
|
+
operation,
|
|
99
|
+
execution_plane: runtimeIdentity?.execution_plane ?? 'hub-3210',
|
|
100
|
+
profile: runtimeIdentity?.profile ?? 'dsh-crew',
|
|
101
|
+
port: runtimeIdentity?.listen_port ?? 3210,
|
|
102
|
+
runtime_id: runtimeIdentity?.runtime_id ?? null,
|
|
103
|
+
lease,
|
|
104
|
+
requested_at: now,
|
|
105
|
+
expires_at: now + ttlMs,
|
|
106
|
+
reason: reason ?? null,
|
|
107
|
+
extra,
|
|
108
|
+
};
|
|
109
|
+
if (typeof request.runtime_id !== 'string' || request.runtime_id.length === 0) {
|
|
110
|
+
return { ok: false, code: 'RUNTIME_IDENTITY_INCOMPLETE', error: 'cannot request supervisor action without a live runtime_id' };
|
|
111
|
+
}
|
|
112
|
+
const dir = operation.startsWith('maintenance-') ? maintenanceRequestsDir(appRoot) : restartRequestsDir(appRoot);
|
|
113
|
+
mkdirSync(dir, { recursive: true });
|
|
114
|
+
const file = join(dir, `${request.request_id}.json`);
|
|
115
|
+
writeFileAtomic(file, JSON.stringify(request, null, 2) + '\n');
|
|
116
|
+
return { ok: true, request, file };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function maintenanceRequestsDir(appRoot) {
|
|
120
|
+
return join(supervisorStateRoot(appRoot), 'maintenance-requests');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function maintenanceResultsDir(appRoot) {
|
|
124
|
+
return join(supervisorStateRoot(appRoot), 'maintenance-results');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function maintenanceSessionFile(appRoot) {
|
|
128
|
+
return join(supervisorStateRoot(appRoot), 'maintenance-session.json');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function readMaintenanceSession(appRoot) {
|
|
132
|
+
const file = maintenanceSessionFile(appRoot);
|
|
133
|
+
if (!existsSync(file)) return { ok: true, state: 'absent', session: null };
|
|
134
|
+
try {
|
|
135
|
+
const session = JSON.parse(readFileSync(file, 'utf8'));
|
|
136
|
+
const valid = session?.schema_version === 1
|
|
137
|
+
&& session.state === 'STOPPED'
|
|
138
|
+
&& typeof session.lease === 'string'
|
|
139
|
+
&& session.lease.length > 0
|
|
140
|
+
&& typeof session.runtime_id === 'string'
|
|
141
|
+
&& session.runtime_id.length > 0
|
|
142
|
+
&& typeof session.request_id === 'string'
|
|
143
|
+
&& session.request_id.length > 0;
|
|
144
|
+
return valid
|
|
145
|
+
? { ok: true, state: 'present', session }
|
|
146
|
+
: { ok: false, state: 'malformed', code: 'MAINTENANCE_SESSION_MALFORMED', session: null };
|
|
147
|
+
} catch (error) {
|
|
148
|
+
return { ok: false, state: 'malformed', code: 'MAINTENANCE_SESSION_MALFORMED', error: error?.message, session: null };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function readRestartRequest(appRoot, requestId) {
|
|
153
|
+
try {
|
|
154
|
+
const parsed = JSON.parse(readFileSync(join(restartRequestsDir(appRoot), `${requestId}.json`), 'utf8'));
|
|
155
|
+
if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
|
|
156
|
+
} catch { /* absent or malformed */ }
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function listRestartRequests(appRoot) {
|
|
161
|
+
const dir = restartRequestsDir(appRoot);
|
|
162
|
+
if (!existsSync(dir)) return [];
|
|
163
|
+
return readdirSync(dir)
|
|
164
|
+
.filter((name) => name.endsWith('.json'))
|
|
165
|
+
.map((name) => {
|
|
166
|
+
try { return JSON.parse(readFileSync(join(dir, name), 'utf8')); } catch { return null; }
|
|
167
|
+
})
|
|
168
|
+
.filter(Boolean);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function removeRestartRequest(appRoot, requestId) {
|
|
172
|
+
try { rmSync(join(restartRequestsDir(appRoot), `${requestId}.json`), { force: true }); } catch {}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function writeRestartResult({ appRoot, request, state, detail = null, now = Date.now() }) {
|
|
176
|
+
const result = {
|
|
177
|
+
schema_version: RESTART_REQUEST_SCHEMA,
|
|
178
|
+
request_id: request.request_id,
|
|
179
|
+
operation: request.operation,
|
|
180
|
+
state,
|
|
181
|
+
runtime_id: request.runtime_id,
|
|
182
|
+
written_at: now,
|
|
183
|
+
detail,
|
|
184
|
+
};
|
|
185
|
+
const file = join(restartResultsDir(appRoot), `${request.request_id}.json`);
|
|
186
|
+
writeFileAtomic(file, JSON.stringify(result, null, 2) + '\n');
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function readRestartResult(appRoot, requestId) {
|
|
191
|
+
try {
|
|
192
|
+
const parsed = JSON.parse(readFileSync(join(restartResultsDir(appRoot), `${requestId}.json`), 'utf8'));
|
|
193
|
+
if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
|
|
194
|
+
} catch { /* absent */ }
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Observe a fresh heartbeat without granting it restart authority. */
|
|
199
|
+
export function readSupervisorHeartbeatRecord(appRoot, { now = Date.now(), staleAfterMs = 15_000 } = {}) {
|
|
200
|
+
try {
|
|
201
|
+
const parsed = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
|
|
202
|
+
if (parsed?.schema_version !== 1 || !Number.isInteger(parsed.pid) || parsed.pid < 1) return null;
|
|
203
|
+
if (typeof parsed.last_seen !== 'number' || now - parsed.last_seen > staleAfterMs || parsed.last_seen - now > 5_000) return null;
|
|
204
|
+
const hasOwnershipField = Object.hasOwn(parsed, 'ownership_ready');
|
|
205
|
+
const identityReady = typeof parsed.process_started_at_utc_ticks === 'string'
|
|
206
|
+
&& /^\d+$/u.test(parsed.process_started_at_utc_ticks)
|
|
207
|
+
&& parsed.process_started_at_utc_ticks !== '0'
|
|
208
|
+
&& typeof parsed.helper_hash === 'string'
|
|
209
|
+
&& /^[a-f0-9]{64}$/u.test(parsed.helper_hash)
|
|
210
|
+
&& typeof parsed.supervisor_instance_id === 'string'
|
|
211
|
+
&& parsed.supervisor_instance_id.trim().length > 0;
|
|
212
|
+
const state = !hasOwnershipField ? 'legacy-v1' : parsed.ownership_ready === true && identityReady ? 'ready' : 'starting';
|
|
213
|
+
return { state, record: { ...parsed, ownership_source: state } };
|
|
214
|
+
} catch { return null; }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Return only an ownership-proven supervisor heartbeat. */
|
|
218
|
+
export function readSupervisorHeartbeat(appRoot, options = {}) {
|
|
219
|
+
const observed = readSupervisorHeartbeatRecord(appRoot, options);
|
|
220
|
+
return observed?.state === 'ready' ? observed.record : null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function writeSupervisorHeartbeat({
|
|
224
|
+
appRoot,
|
|
225
|
+
pid,
|
|
226
|
+
now = Date.now(),
|
|
227
|
+
ownershipReady = true,
|
|
228
|
+
processStartedAtUtcTicks = null,
|
|
229
|
+
helperHash = null,
|
|
230
|
+
supervisorInstanceId = randomUUID(),
|
|
231
|
+
}) {
|
|
232
|
+
const record = {
|
|
233
|
+
schema_version: 1,
|
|
234
|
+
supervisor_instance_id: supervisorInstanceId,
|
|
235
|
+
pid,
|
|
236
|
+
process_started_at_utc_ticks: typeof processStartedAtUtcTicks === 'string' ? processStartedAtUtcTicks : null,
|
|
237
|
+
helper_hash: helperHash,
|
|
238
|
+
ownership_ready: ownershipReady === true,
|
|
239
|
+
last_seen: now,
|
|
240
|
+
protocol_version: 1,
|
|
241
|
+
};
|
|
242
|
+
// Merge onto the previous record so the instance id is stable per watcher
|
|
243
|
+
// run (the watcher rewrites this file every few seconds).
|
|
244
|
+
try {
|
|
245
|
+
const prev = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
|
|
246
|
+
const sameWatcher = prev?.schema_version === 1
|
|
247
|
+
&& prev.pid === record.pid
|
|
248
|
+
&& prev.process_started_at_utc_ticks === record.process_started_at_utc_ticks
|
|
249
|
+
&& prev.helper_hash === record.helper_hash
|
|
250
|
+
&& typeof prev.supervisor_instance_id === 'string'
|
|
251
|
+
&& prev.supervisor_instance_id.length > 0;
|
|
252
|
+
if (sameWatcher) record.supervisor_instance_id = prev.supervisor_instance_id;
|
|
253
|
+
} catch { /* first beat */ }
|
|
254
|
+
writeFileAtomic(heartbeatFile(appRoot), JSON.stringify(record, null, 2) + '\n');
|
|
255
|
+
return record;
|
|
256
|
+
}
|