@ran-sh/dsh-crew 1.0.3 → 1.1.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.
@@ -5,10 +5,18 @@
5
5
  // - HUB_PROTOCOL_VERSION changes only when Hub <-> MCP wire semantics become
6
6
  // incompatible.
7
7
  //
8
+ // The Harness/DSH cohort version (e.g. 0.1.2-rc.1) is a THIRD, independent
9
+ // domain: it identifies the installed @deepseek-ai/dsh package generation,
10
+ // never the Crew release. Verifiers must compare the cohort against
11
+ // dsh_version, never against runtime_version.
12
+ //
8
13
  // Keep this module dependency-light so Hub, MCP and tests all use the exact
9
14
  // same compatibility rules.
10
15
 
11
16
  import { randomUUID } from 'node:crypto';
17
+ import { existsSync, readFileSync } from 'node:fs';
18
+ import { homedir } from 'node:os';
19
+ import { join } from 'node:path';
12
20
 
13
21
  // Production Crew execution is intentionally bound to the isolated 3210 Hub.
14
22
  // These fields are public provenance only; no credential or session secret is
@@ -18,7 +26,7 @@ export const PRODUCTION_PROFILE = 'dsh-crew';
18
26
  export const PRODUCTION_LISTEN_PORT = 3210;
19
27
  const RUNTIME_ID = randomUUID();
20
28
 
21
- export const RUNTIME_VERSION = '1.0.3';
29
+ export const RUNTIME_VERSION = '1.1.0';
22
30
  export const HUB_PROTOCOL_VERSION = 1;
23
31
 
24
32
  export const HUB_CAPABILITIES = Object.freeze([
@@ -80,6 +88,7 @@ export function getHubRuntimeIdentity() {
80
88
  surface: 'native-crew-harness',
81
89
  ui_role: 'runtime',
82
90
  runtime_version: RUNTIME_VERSION,
91
+ dsh_version: readCrewDshVersion(),
83
92
  protocol_version: HUB_PROTOCOL_VERSION,
84
93
  execution_plane: PRODUCTION_EXECUTION_PLANE,
85
94
  profile: PRODUCTION_PROFILE,
@@ -89,6 +98,19 @@ export function getHubRuntimeIdentity() {
89
98
  };
90
99
  }
91
100
 
101
+ // Installed Harness cohort version (the @deepseek-ai/dsh package under the
102
+ // Crew-owned DSH home). Null when unreadable: callers treat null as
103
+ // "unknown cohort", never as a match. This is the ONLY field cohort
104
+ // verifiers may compare against TARGET_DSH_VERSION.
105
+ export function readCrewDshVersion({ home = homedir() } = {}) {
106
+ try {
107
+ const file = join(home, '.config', 'dsh-crew', 'harness', 'runtime', 'node_modules', '@deepseek-ai', 'dsh', 'package.json');
108
+ if (!existsSync(file)) return null;
109
+ const parsed = JSON.parse(readFileSync(file, 'utf8'));
110
+ return typeof parsed?.version === 'string' && parsed.version.length > 0 ? parsed.version : null;
111
+ } catch { return null; }
112
+ }
113
+
92
114
  /**
93
115
  * Evaluate an already-received Hub ping body.
94
116
  *
@@ -0,0 +1,202 @@
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 readRestartRequest(appRoot, requestId) {
128
+ try {
129
+ const parsed = JSON.parse(readFileSync(join(restartRequestsDir(appRoot), `${requestId}.json`), 'utf8'));
130
+ if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
131
+ } catch { /* absent or malformed */ }
132
+ return null;
133
+ }
134
+
135
+ export function listRestartRequests(appRoot) {
136
+ const dir = restartRequestsDir(appRoot);
137
+ if (!existsSync(dir)) return [];
138
+ return readdirSync(dir)
139
+ .filter((name) => name.endsWith('.json'))
140
+ .map((name) => {
141
+ try { return JSON.parse(readFileSync(join(dir, name), 'utf8')); } catch { return null; }
142
+ })
143
+ .filter(Boolean);
144
+ }
145
+
146
+ export function removeRestartRequest(appRoot, requestId) {
147
+ try { rmSync(join(restartRequestsDir(appRoot), `${requestId}.json`), { force: true }); } catch {}
148
+ }
149
+
150
+ export function writeRestartResult({ appRoot, request, state, detail = null, now = Date.now() }) {
151
+ const result = {
152
+ schema_version: RESTART_REQUEST_SCHEMA,
153
+ request_id: request.request_id,
154
+ operation: request.operation,
155
+ state,
156
+ runtime_id: request.runtime_id,
157
+ written_at: now,
158
+ detail,
159
+ };
160
+ const file = join(restartResultsDir(appRoot), `${request.request_id}.json`);
161
+ writeFileAtomic(file, JSON.stringify(result, null, 2) + '\n');
162
+ return result;
163
+ }
164
+
165
+ export function readRestartResult(appRoot, requestId) {
166
+ try {
167
+ const parsed = JSON.parse(readFileSync(join(restartResultsDir(appRoot), `${requestId}.json`), 'utf8'));
168
+ if (parsed?.schema_version === RESTART_REQUEST_SCHEMA && parsed.request_id === requestId) return parsed;
169
+ } catch { /* absent */ }
170
+ return null;
171
+ }
172
+
173
+ /**
174
+ * Read the supervisor heartbeat. Returns null when the supervisor is not
175
+ * actively heartbeating (absent, stale beyond tolerance, or malformed).
176
+ */
177
+ export function readSupervisorHeartbeat(appRoot, { now = Date.now(), staleAfterMs = 15_000 } = {}) {
178
+ try {
179
+ const parsed = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
180
+ if (parsed?.schema_version !== 1 || !Number.isInteger(parsed.pid) || parsed.pid < 1) return null;
181
+ if (typeof parsed.last_seen !== 'number' || now - parsed.last_seen > staleAfterMs) return null;
182
+ return parsed;
183
+ } catch { return null; }
184
+ }
185
+
186
+ export function writeSupervisorHeartbeat({ appRoot, pid, now = Date.now() }) {
187
+ const record = {
188
+ schema_version: 1,
189
+ supervisor_instance_id: randomUUID(),
190
+ pid,
191
+ last_seen: now,
192
+ protocol_version: 1,
193
+ };
194
+ // Merge onto the previous record so the instance id is stable per watcher
195
+ // run (the watcher rewrites this file every few seconds).
196
+ try {
197
+ const prev = JSON.parse(readFileSync(heartbeatFile(appRoot), 'utf8'));
198
+ if (prev?.schema_version === 1) record.supervisor_instance_id = prev.supervisor_instance_id;
199
+ } catch { /* first beat */ }
200
+ writeFileAtomic(heartbeatFile(appRoot), JSON.stringify(record, null, 2) + '\n');
201
+ return record;
202
+ }