@ran-sh/dsh-crew 1.0.3 → 1.1.1
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/codex/AGENTS.md +101 -92
- package/docs/ui-surfaces.md +48 -21
- package/lib/client.js +68 -44
- package/official-web-bridge/lib/client.js +368 -4709
- package/official-web-bridge/package.json +1 -2
- package/package.json +130 -67
- package/scripts/build-client.mjs +30 -15
- package/scripts/remove-legacy-official-bridge.ps1 +89 -0
- package/scripts/setup.mjs +152 -140
- package/scripts/verify-npm-install.mjs +311 -310
- package/scripts/verify-official-bridge-e2e.mjs +12 -1
- package/src/client/host-readiness.mjs +62 -59
- package/src/client/index.tsx +35 -30
- package/src/client/quick-entry.tsx +10 -0
- package/src/client/quick-panel.tsx +295 -0
- package/src/client/surface-detection.mjs +43 -30
- package/src/dsh-cli-runtime.mjs +500 -40
- package/src/dsh-cohort.mjs +20 -0
- package/src/hub/index.mjs +1602 -1485
- package/src/install/npx-lifecycle.mjs +1568 -111
- package/src/install/official-web.mjs +36 -73
- package/src/jobs.mjs +20 -24
- package/src/official-web-bridge.mjs +329 -249
- package/src/provider-profile-store.mjs +12 -0
- package/src/provider-settings-store.mjs +347 -5
- package/src/runtime-identity.mjs +23 -1
- package/src/supervisor/restart-request.mjs +202 -0
- package/windows/start-dsh-crew.ps1 +792 -370
- package/worker.cordis.yml +32 -46
- package/zcode/AGENTS.md +35 -26
|
@@ -11,14 +11,28 @@ const PROVIDER_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
|
|
|
11
11
|
function sha256(value) { return createHash('sha256').update(value, 'utf8').digest('hex'); }
|
|
12
12
|
function indentOf(line) { return (line.match(/^\s*/u) ?? [''])[0].length; }
|
|
13
13
|
function nonBlank(line) { return line.trim() !== '' && !line.trimStart().startsWith('#'); }
|
|
14
|
+
function nextContentLine(lines, start, end = lines.length) {
|
|
15
|
+
for (let index = start; index < end; index += 1) if (nonBlank(lines[index])) return index;
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
function unquoteScalar(value) {
|
|
19
|
+
const input = typeof value === 'string' ? value.trim() : '';
|
|
20
|
+
if (!input) return null;
|
|
21
|
+
if (input.startsWith('"') && input.endsWith('"')) {
|
|
22
|
+
try { return JSON.parse(input); } catch { return null; }
|
|
23
|
+
}
|
|
24
|
+
if (input.startsWith("'") && input.endsWith("'")) return input.slice(1, -1).replace(/''/gu, "'");
|
|
25
|
+
return input;
|
|
26
|
+
}
|
|
14
27
|
function scalarField(lines, entry, field) {
|
|
15
|
-
const
|
|
28
|
+
const indent = Number.isInteger(entry?.fieldIndent) ? entry.fieldIndent : null;
|
|
29
|
+
const pattern = new RegExp(`^${indent === null ? '\\s+' : ` {${indent}}`}${field}:\\s*(.*?)\\s*$`);
|
|
16
30
|
for (const line of lines.slice(entry.start, entry.end)) {
|
|
17
31
|
const match = line.match(pattern);
|
|
18
32
|
if (!match) continue;
|
|
19
|
-
const value = match[1].trim();
|
|
33
|
+
const value = (entry?.style === 'flow' ? match[1].replace(/,\s*$/u, '') : match[1]).trim();
|
|
20
34
|
if (!value) return null;
|
|
21
|
-
return value
|
|
35
|
+
return unquoteScalar(value);
|
|
22
36
|
}
|
|
23
37
|
return null;
|
|
24
38
|
}
|
|
@@ -51,6 +65,39 @@ function parseProviderMap(source) {
|
|
|
51
65
|
}
|
|
52
66
|
const providersLine = lines.findIndex((line, index) => index > llmStart && index < blockEnd && /^ {2}providers:\s*(?:\{\s*\})?\s*$/u.test(line));
|
|
53
67
|
if (providersLine < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
68
|
+
const flowOpenLine = nextContentLine(lines, providersLine + 1, blockEnd);
|
|
69
|
+
if (flowOpenLine >= 0 && /^ {4}\{\s*$/u.test(lines[flowOpenLine])) {
|
|
70
|
+
let flowCloseLine = -1;
|
|
71
|
+
const entries = [];
|
|
72
|
+
let index = flowOpenLine + 1;
|
|
73
|
+
while (index < blockEnd) {
|
|
74
|
+
index = nextContentLine(lines, index, blockEnd);
|
|
75
|
+
if (index < 0) break;
|
|
76
|
+
if (/^ {4}\}\s*$/u.test(lines[index])) { flowCloseLine = index; break; }
|
|
77
|
+
const match = lines[index].match(/^ {6}([A-Za-z0-9][A-Za-z0-9._-]*):\s*$/u);
|
|
78
|
+
if (!match) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
79
|
+
const objectOpen = nextContentLine(lines, index + 1, blockEnd);
|
|
80
|
+
if (objectOpen < 0 || !/^ {8}\{\s*$/u.test(lines[objectOpen])) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
81
|
+
let objectClose = -1;
|
|
82
|
+
for (let cursor = objectOpen + 1; cursor < blockEnd; cursor += 1) {
|
|
83
|
+
if (/^ {8}\},?\s*$/u.test(lines[cursor])) { objectClose = cursor; break; }
|
|
84
|
+
}
|
|
85
|
+
if (objectClose < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
86
|
+
entries.push({ id: match[1], start: index, end: objectClose + 1, style: 'flow', fieldIndent: 10, objectOpen, objectClose });
|
|
87
|
+
index = objectClose + 1;
|
|
88
|
+
}
|
|
89
|
+
if (flowCloseLine < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
90
|
+
for (let entryIndex = 0; entryIndex < entries.length - 1; entryIndex += 1) {
|
|
91
|
+
if (!/^ {8}\},\s*$/u.test(lines[entries[entryIndex].objectClose])) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
92
|
+
}
|
|
93
|
+
if (entries.some((entry) => !validFlowEntryStructure(lines, entry))) {
|
|
94
|
+
return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true, style: 'flow', lines, llmStart, blockEnd, providersLine,
|
|
98
|
+
providersBlockEnd: flowCloseLine + 1, flowOpenLine, flowCloseLine, entries,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
54
101
|
let providersBlockEnd = blockEnd;
|
|
55
102
|
for (let index = providersLine + 1; index < blockEnd; index += 1) {
|
|
56
103
|
if (nonBlank(lines[index]) && indentOf(lines[index]) <= 2) { providersBlockEnd = index; break; }
|
|
@@ -74,10 +121,235 @@ function parseProviderMap(source) {
|
|
|
74
121
|
if (nonBlank(next) && indentOf(next) <= providerIndent) break;
|
|
75
122
|
end += 1;
|
|
76
123
|
}
|
|
77
|
-
entries.push({ id: match[1], start: index, end });
|
|
124
|
+
entries.push({ id: match[1], start: index, end, style: 'block', fieldIndent: 6 });
|
|
78
125
|
index = end - 1;
|
|
79
126
|
}
|
|
80
|
-
return { ok: true, lines, llmStart, blockEnd, providersLine, providersBlockEnd, entries };
|
|
127
|
+
return { ok: true, style: 'block', lines, llmStart, blockEnd, providersLine, providersBlockEnd, entries };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function splitFlowItems(source) {
|
|
131
|
+
const items = [];
|
|
132
|
+
let start = 0;
|
|
133
|
+
let braces = 0;
|
|
134
|
+
let brackets = 0;
|
|
135
|
+
let quote = null;
|
|
136
|
+
let escaped = false;
|
|
137
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
138
|
+
const char = source[index];
|
|
139
|
+
if (quote) {
|
|
140
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
141
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
142
|
+
if (char === quote) quote = null;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
146
|
+
if (char === '{') braces += 1;
|
|
147
|
+
else if (char === '}') braces -= 1;
|
|
148
|
+
else if (char === '[') brackets += 1;
|
|
149
|
+
else if (char === ']') brackets -= 1;
|
|
150
|
+
else if (char === ',' && braces === 0 && brackets === 0) {
|
|
151
|
+
items.push(source.slice(start, index).trim());
|
|
152
|
+
start = index + 1;
|
|
153
|
+
}
|
|
154
|
+
if (braces < 0 || brackets < 0) return null;
|
|
155
|
+
}
|
|
156
|
+
if (quote || braces !== 0 || brackets !== 0) return null;
|
|
157
|
+
const tail = source.slice(start).trim();
|
|
158
|
+
if (tail) items.push(tail);
|
|
159
|
+
return items;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseFlowObject(source, depth = 0) {
|
|
163
|
+
if (depth > 8) return null;
|
|
164
|
+
const input = source.trim().replace(/,\s*$/u, '');
|
|
165
|
+
if (!input.startsWith('{') || !input.endsWith('}')) return null;
|
|
166
|
+
const items = splitFlowItems(input.slice(1, -1));
|
|
167
|
+
if (!items) return null;
|
|
168
|
+
const fields = new Map();
|
|
169
|
+
for (const item of items) {
|
|
170
|
+
let splitAt = -1;
|
|
171
|
+
let quote = null;
|
|
172
|
+
let escaped = false;
|
|
173
|
+
for (let index = 0; index < item.length; index += 1) {
|
|
174
|
+
const char = item[index];
|
|
175
|
+
if (quote) {
|
|
176
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
177
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
178
|
+
if (char === quote) quote = null;
|
|
179
|
+
} else if (char === '"' || char === "'") quote = char;
|
|
180
|
+
else if (char === ':') { splitAt = index; break; }
|
|
181
|
+
}
|
|
182
|
+
if (splitAt < 1 || hasUnseparatedFlowMapping(item, splitAt)) return null;
|
|
183
|
+
const key = item.slice(0, splitAt).trim();
|
|
184
|
+
const value = item.slice(splitAt + 1).trim();
|
|
185
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/u.test(key) || !value || fields.has(key)) return null;
|
|
186
|
+
if (value.startsWith('{') && (!value.endsWith('}') || parseFlowObject(value, depth + 1) === null)) return null;
|
|
187
|
+
fields.set(key, value);
|
|
188
|
+
}
|
|
189
|
+
return fields;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function hasUnseparatedFlowMapping(item, firstColon) {
|
|
193
|
+
let braces = 0;
|
|
194
|
+
let brackets = 0;
|
|
195
|
+
let quote = null;
|
|
196
|
+
let escaped = false;
|
|
197
|
+
for (let index = firstColon + 1; index < item.length; index += 1) {
|
|
198
|
+
const char = item[index];
|
|
199
|
+
if (quote) {
|
|
200
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
201
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
202
|
+
if (char === quote) quote = null;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
206
|
+
if (char === '{') braces += 1;
|
|
207
|
+
else if (char === '}') braces -= 1;
|
|
208
|
+
else if (char === '[') brackets += 1;
|
|
209
|
+
else if (char === ']') brackets -= 1;
|
|
210
|
+
else if (char === ':' && braces === 0 && brackets === 0 && /\s/u.test(item[index + 1] ?? '')) return true;
|
|
211
|
+
if (braces < 0 || brackets < 0) return true;
|
|
212
|
+
}
|
|
213
|
+
return quote !== null || braces !== 0 || brackets !== 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function validFlowEntryStructure(lines, entry) {
|
|
217
|
+
const fields = [];
|
|
218
|
+
let cursor = entry.objectOpen + 1;
|
|
219
|
+
while (cursor < entry.objectClose) {
|
|
220
|
+
cursor = nextContentLine(lines, cursor, entry.objectClose);
|
|
221
|
+
if (cursor < 0) break;
|
|
222
|
+
const line = lines[cursor];
|
|
223
|
+
if (indentOf(line) !== entry.fieldIndent) return false;
|
|
224
|
+
const field = line.match(/^\s+([A-Za-z][A-Za-z0-9_-]*):\s*(.*?)\s*$/u);
|
|
225
|
+
if (!field) return false;
|
|
226
|
+
let terminator = cursor;
|
|
227
|
+
if (!field[2]) {
|
|
228
|
+
const listOpen = nextContentLine(lines, cursor + 1, entry.objectClose);
|
|
229
|
+
if (listOpen < 0 || indentOf(lines[listOpen]) !== entry.fieldIndent + 2 || !/^\s*\[\s*$/u.test(lines[listOpen])) return false;
|
|
230
|
+
const items = [];
|
|
231
|
+
let listClose = -1;
|
|
232
|
+
for (let nested = listOpen + 1; nested < entry.objectClose; nested += 1) {
|
|
233
|
+
if (!nonBlank(lines[nested])) continue;
|
|
234
|
+
if (indentOf(lines[nested]) === entry.fieldIndent + 2 && /^\s*\]\s*,?\s*$/u.test(lines[nested])) {
|
|
235
|
+
listClose = nested;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
if (indentOf(lines[nested]) !== entry.fieldIndent + 4 || !parseFlowObject(lines[nested])) return false;
|
|
239
|
+
items.push(nested);
|
|
240
|
+
}
|
|
241
|
+
if (listClose < 0) return false;
|
|
242
|
+
for (let index = 0; index < items.length - 1; index += 1) if (!/,\s*$/u.test(lines[items[index]])) return false;
|
|
243
|
+
terminator = listClose;
|
|
244
|
+
cursor = listClose + 1;
|
|
245
|
+
} else {
|
|
246
|
+
cursor += 1;
|
|
247
|
+
}
|
|
248
|
+
fields.push({ terminator });
|
|
249
|
+
}
|
|
250
|
+
for (let index = 0; index < fields.length - 1; index += 1) {
|
|
251
|
+
if (!/,\s*$/u.test(lines[fields[index].terminator])) return false;
|
|
252
|
+
}
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function parseFlowModel(source) {
|
|
257
|
+
const fields = parseFlowObject(source);
|
|
258
|
+
if (!fields) return null;
|
|
259
|
+
const allowed = new Set(['id', 'name', 'contextWindow', 'maxTokens', 'input', 'reasoningEfforts', 'compat']);
|
|
260
|
+
if ([...fields.keys()].some((key) => !allowed.has(key))) return null;
|
|
261
|
+
const id = unquoteScalar(fields.get('id'));
|
|
262
|
+
if (!id || id.length > 256 || /[\r\n]/u.test(id)) return null;
|
|
263
|
+
const model = { id };
|
|
264
|
+
if (fields.has('name')) {
|
|
265
|
+
const name = unquoteScalar(fields.get('name'));
|
|
266
|
+
if (!name || name.length > 256 || /[\r\n]/u.test(name)) return null;
|
|
267
|
+
model.name = name;
|
|
268
|
+
}
|
|
269
|
+
for (const [sourceKey, targetKey] of [['contextWindow', 'context_window'], ['maxTokens', 'max_tokens']]) {
|
|
270
|
+
if (!fields.has(sourceKey)) continue;
|
|
271
|
+
const value = Number(unquoteScalar(fields.get(sourceKey)));
|
|
272
|
+
if (!Number.isSafeInteger(value) || value <= 0) return null;
|
|
273
|
+
model[targetKey] = value;
|
|
274
|
+
}
|
|
275
|
+
if (fields.has('input')) {
|
|
276
|
+
const input = fields.get('input').trim();
|
|
277
|
+
if (!input.startsWith('[') || !input.endsWith(']')) return null;
|
|
278
|
+
const modalities = splitFlowItems(input.slice(1, -1));
|
|
279
|
+
if (!modalities) return null;
|
|
280
|
+
const normalized = modalities.map(unquoteScalar);
|
|
281
|
+
if (normalized.some((value) => !['text', 'image'].includes(value))) return null;
|
|
282
|
+
model.input = [...new Set(normalized)];
|
|
283
|
+
}
|
|
284
|
+
if (fields.has('reasoningEfforts')) {
|
|
285
|
+
const efforts = parseFlowObject(fields.get('reasoningEfforts'));
|
|
286
|
+
if (!efforts) return null;
|
|
287
|
+
const normalized = {};
|
|
288
|
+
for (const [key, raw] of efforts) {
|
|
289
|
+
const value = unquoteScalar(raw);
|
|
290
|
+
if (raw === 'null' || raw === '~') normalized[key] = null;
|
|
291
|
+
else if (!value || value.length > 256 || /[\r\n]/u.test(value)) return null;
|
|
292
|
+
else normalized[key] = value;
|
|
293
|
+
}
|
|
294
|
+
model.reasoning_efforts = normalized;
|
|
295
|
+
}
|
|
296
|
+
if (fields.has('compat')) {
|
|
297
|
+
if (fields.get('compat').trim() !== '{}') return null;
|
|
298
|
+
model.compat = {};
|
|
299
|
+
}
|
|
300
|
+
return model;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function readFlowMaterialization(parsed, entry, file) {
|
|
304
|
+
const directFields = new Set();
|
|
305
|
+
for (const line of parsed.lines.slice(entry.objectOpen + 1, entry.objectClose)) {
|
|
306
|
+
if (!nonBlank(line) || indentOf(line) !== entry.fieldIndent) continue;
|
|
307
|
+
const match = line.match(/^\s+([A-Za-z][A-Za-z0-9_-]*):/u);
|
|
308
|
+
if (!match || directFields.has(match[1])) return { ok: false, code: 'PROVIDER_MATERIALIZATION_UNSUPPORTED_FIELDS' };
|
|
309
|
+
directFields.add(match[1]);
|
|
310
|
+
}
|
|
311
|
+
const allowed = new Set(['displayName', 'apiKeyEnv', 'api', 'baseURL', 'models']);
|
|
312
|
+
if ([...directFields].some((field) => !allowed.has(field))) return { ok: false, code: 'PROVIDER_MATERIALIZATION_UNSUPPORTED_FIELDS' };
|
|
313
|
+
const rawDisplayName = scalarField(parsed.lines, entry, 'displayName');
|
|
314
|
+
const displayName = rawDisplayName && rawDisplayName.length <= 256 && !/[\r\n]/u.test(rawDisplayName) ? rawDisplayName : entry.id;
|
|
315
|
+
const rawCredentialRef = scalarField(parsed.lines, entry, 'apiKeyEnv');
|
|
316
|
+
const credential = classifyCredentialReference(rawCredentialRef, { kind: 'env' });
|
|
317
|
+
if (credential.redacted) return { ok: false, code: 'PROVIDER_CREDENTIAL_REFERENCE_UNSAFE' };
|
|
318
|
+
const api = scalarField(parsed.lines, entry, 'api');
|
|
319
|
+
if (api && (!/^[A-Za-z][A-Za-z0-9._-]{0,127}$/u.test(api) || /^(?:sk|pk|rk|token|secret)[_-]/iu.test(api))) return { ok: false, code: 'PROVIDER_API_SCHEMA_UNSUPPORTED' };
|
|
320
|
+
const rawBaseUrl = scalarField(parsed.lines, entry, 'baseURL');
|
|
321
|
+
const baseUrl = rawBaseUrl && rawBaseUrl.length <= 2048 && !/[\r\n]/u.test(rawBaseUrl) ? rawBaseUrl : null;
|
|
322
|
+
if (baseUrl) {
|
|
323
|
+
try {
|
|
324
|
+
const url = new URL(baseUrl);
|
|
325
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash || !url.hostname) return { ok: false, code: 'PROVIDER_BASE_URL_UNSAFE' };
|
|
326
|
+
} catch { return { ok: false, code: 'PROVIDER_BASE_URL_UNSAFE' }; }
|
|
327
|
+
}
|
|
328
|
+
const models = [];
|
|
329
|
+
if (directFields.has('models')) {
|
|
330
|
+
const modelsLine = parsed.lines.findIndex((line, index) => index > entry.objectOpen && index < entry.objectClose && /^ {10}models:\s*$/u.test(line));
|
|
331
|
+
const listOpen = nextContentLine(parsed.lines, modelsLine + 1, entry.objectClose);
|
|
332
|
+
if (modelsLine < 0 || listOpen < 0 || !/^ {12}\[\s*$/u.test(parsed.lines[listOpen])) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
333
|
+
let listClose = -1;
|
|
334
|
+
for (let index = listOpen + 1; index < entry.objectClose; index += 1) {
|
|
335
|
+
if (/^ {12}\]\s*,?\s*$/u.test(parsed.lines[index])) { listClose = index; break; }
|
|
336
|
+
if (!nonBlank(parsed.lines[index])) continue;
|
|
337
|
+
if (indentOf(parsed.lines[index]) !== 14) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
338
|
+
const model = parseFlowModel(parsed.lines[index]);
|
|
339
|
+
if (!model || models.some((candidate) => candidate.id === model.id)) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
340
|
+
models.push(model);
|
|
341
|
+
}
|
|
342
|
+
if (listClose < 0) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
ok: true,
|
|
346
|
+
provider: {
|
|
347
|
+
id: entry.id, display_name: displayName,
|
|
348
|
+
...(credential.value ? { credential_ref: credential.value } : {}),
|
|
349
|
+
...(api ? { api } : {}), ...(baseUrl ? { base_url: baseUrl } : {}),
|
|
350
|
+
models: models.slice(0, 256), source_file: file,
|
|
351
|
+
},
|
|
352
|
+
};
|
|
81
353
|
}
|
|
82
354
|
|
|
83
355
|
const SENSITIVE_CREDENTIAL_FIELD = /(?:api[_-]?key|access[_-]?token|refresh[_-]?token|(?:^|[_-])token(?:$|[_-])|secret|password|authorization|credential|private[_-]?key|client[_-]?secret|cookie|bearer|webhook)/iu;
|
|
@@ -142,6 +414,7 @@ export function readProviderSettingsMaterialization(source, { providerId, file =
|
|
|
142
414
|
if (!parsed.ok) return { ok: false, code: parsed.code };
|
|
143
415
|
const entry = parsed.entries.find((candidate) => candidate.id === providerId);
|
|
144
416
|
if (!entry) return { ok: false, code: 'PROVIDER_NOT_FOUND' };
|
|
417
|
+
if (parsed.style === 'flow') return readFlowMaterialization(parsed, entry, file);
|
|
145
418
|
const block = parsed.lines.slice(entry.start, entry.end).map((line) => ` ${line}`);
|
|
146
419
|
const synthetic = ['- id: llm-pi-ai', ' config:', ' providers:', ...block, ''].join('\n');
|
|
147
420
|
const result = readProviderMaterialization(synthetic, { providerId, file });
|
|
@@ -159,6 +432,47 @@ function yamlScalar(value, max = 2048) {
|
|
|
159
432
|
return JSON.stringify(value.trim());
|
|
160
433
|
}
|
|
161
434
|
|
|
435
|
+
function flowModelLine(model) {
|
|
436
|
+
const fields = [`id: ${yamlScalar(model.id, 256)}`];
|
|
437
|
+
const name = yamlScalar(model.name, 256);
|
|
438
|
+
if (name) fields.push(`name: ${name}`);
|
|
439
|
+
if (Number.isSafeInteger(model.context_window) && model.context_window > 0) fields.push(`contextWindow: ${model.context_window}`);
|
|
440
|
+
if (Number.isSafeInteger(model.max_tokens) && model.max_tokens > 0) fields.push(`maxTokens: ${model.max_tokens}`);
|
|
441
|
+
if (Array.isArray(model.input) && model.input.every((value) => value === 'text' || value === 'image')) fields.push(`input: [${[...new Set(model.input)].join(', ')}]`);
|
|
442
|
+
if (model.reasoning_efforts && typeof model.reasoning_efforts === 'object' && !Array.isArray(model.reasoning_efforts)) {
|
|
443
|
+
const efforts = [];
|
|
444
|
+
for (const [key, value] of Object.entries(model.reasoning_efforts)) {
|
|
445
|
+
if (/^[A-Za-z][A-Za-z0-9_-]*$/u.test(key) && (value === null || yamlScalar(value, 256))) efforts.push(`${key}: ${value === null ? 'null' : yamlScalar(value, 256)}`);
|
|
446
|
+
}
|
|
447
|
+
if (efforts.length > 0) fields.push(`reasoningEfforts: { ${efforts.join(', ')} }`);
|
|
448
|
+
}
|
|
449
|
+
if (model.compat && typeof model.compat === 'object' && !Array.isArray(model.compat) && Object.keys(model.compat).length === 0) fields.push('compat: {}');
|
|
450
|
+
return `{ ${fields.join(', ')} }`;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function flowProviderLines(provider) {
|
|
454
|
+
const displayName = yamlScalar(provider.display_name ?? provider.id, 256);
|
|
455
|
+
if (!displayName) return null;
|
|
456
|
+
const credential = provider.credential_ref ? classifyCredentialReference(provider.credential_ref, { kind: 'env' }).value : null;
|
|
457
|
+
if (provider.credential_ref && !credential) return null;
|
|
458
|
+
const api = yamlScalar(provider.api, 128);
|
|
459
|
+
const baseUrl = yamlScalar(provider.base_url, 2048);
|
|
460
|
+
const models = Array.isArray(provider.models)
|
|
461
|
+
? provider.models.filter((model) => model && typeof model.id === 'string' && yamlScalar(model.id, 256)).slice(0, 256)
|
|
462
|
+
: [];
|
|
463
|
+
const fields = [` displayName: ${displayName}`];
|
|
464
|
+
if (api) fields.push(` api: ${api}`);
|
|
465
|
+
if (baseUrl) fields.push(` baseURL: ${baseUrl}`);
|
|
466
|
+
if (credential) fields.push(` apiKeyEnv: ${yamlScalar(credential, 256)}`);
|
|
467
|
+
if (models.length === 0) {
|
|
468
|
+
return [` ${provider.id}:`, ' {', ...fields.map((line, index) => `${line}${index < fields.length - 1 ? ',' : ''}`), ' }'];
|
|
469
|
+
}
|
|
470
|
+
const lines = [` ${provider.id}:`, ' {', ...fields.map((line) => `${line},`), ' models:', ' ['];
|
|
471
|
+
for (let index = 0; index < models.length; index += 1) lines.push(` ${flowModelLine(models[index])}${index < models.length - 1 ? ',' : ''}`);
|
|
472
|
+
lines.push(' ]', ' }');
|
|
473
|
+
return lines;
|
|
474
|
+
}
|
|
475
|
+
|
|
162
476
|
/**
|
|
163
477
|
* Add one already-sanitized provider projection to the Harness user layer.
|
|
164
478
|
* The operation is pure and revision-bound; callers perform the atomic write.
|
|
@@ -172,6 +486,17 @@ export function addProviderSettings(source, { provider, expectedRevision } = {})
|
|
|
172
486
|
const parsed = parseProviderMap(source);
|
|
173
487
|
if (!parsed.ok) return { ok: false, code: parsed.code, revision: currentRevision };
|
|
174
488
|
if (parsed.entries.some((entry) => entry.id === provider.id)) return { ok: false, code: 'PROVIDER_SETTINGS_PROVIDER_EXISTS', revision: currentRevision };
|
|
489
|
+
if (parsed.style === 'flow') {
|
|
490
|
+
const providerLines = flowProviderLines(provider);
|
|
491
|
+
if (!providerLines) return { ok: false, code: 'PROVIDER_MATERIALIZATION_INVALID', revision: currentRevision };
|
|
492
|
+
const nextLines = [...parsed.lines];
|
|
493
|
+
const previous = parsed.entries.at(-1);
|
|
494
|
+
if (previous && !/,\s*$/u.test(nextLines[previous.objectClose])) nextLines[previous.objectClose] = `${nextLines[previous.objectClose]},`;
|
|
495
|
+
nextLines.splice(parsed.flowCloseLine, 0, ...providerLines);
|
|
496
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
497
|
+
const text = nextLines.join(newline);
|
|
498
|
+
return { ok: true, text, added: provider.id, revision: sha256(text) };
|
|
499
|
+
}
|
|
175
500
|
const displayName = yamlScalar(provider.display_name ?? provider.id, 256);
|
|
176
501
|
if (!displayName) return { ok: false, code: 'PROVIDER_MATERIALIZATION_INVALID', revision: currentRevision };
|
|
177
502
|
const lines = [` ${provider.id}:`, ` displayName: ${displayName}`];
|
|
@@ -256,6 +581,23 @@ export function mutateProviderSettings(source, {
|
|
|
256
581
|
}
|
|
257
582
|
const ranges = providerEntry ? [[providerEntry.start, providerEntry.end]] : [];
|
|
258
583
|
const finalProvider = providerEntry && parsed.entries.length === 1;
|
|
584
|
+
if (removeProvider && parsed.style === 'flow' && finalProvider) {
|
|
585
|
+
const lines = [...parsed.lines];
|
|
586
|
+
lines.splice(parsed.providersLine, parsed.flowCloseLine - parsed.providersLine + 1, ' providers: {}');
|
|
587
|
+
if (defaultParsed) {
|
|
588
|
+
const removedBeforeProvider = parsed.providersLine <= defaultParsed.providerLine ? parsed.flowCloseLine - parsed.providersLine : 0;
|
|
589
|
+
const removedBeforeModel = parsed.providersLine <= defaultParsed.modelLine ? parsed.flowCloseLine - parsed.providersLine : 0;
|
|
590
|
+
lines[defaultParsed.providerLine - removedBeforeProvider] = ` provider: ${replacementDefault.trim()}`;
|
|
591
|
+
lines[defaultParsed.modelLine - removedBeforeModel] = ` model: ${replacementModel.trim()}`;
|
|
592
|
+
}
|
|
593
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
594
|
+
const text = lines.join(newline);
|
|
595
|
+
return {
|
|
596
|
+
ok: true, text, removed: [providerId], remaining: [],
|
|
597
|
+
...(defaultParsed ? { previous_default: { provider: defaultParsed.provider, model: defaultParsed.model } } : {}),
|
|
598
|
+
revision: sha256(text),
|
|
599
|
+
};
|
|
600
|
+
}
|
|
259
601
|
const nextLines = parsed.lines.map((line, index) => {
|
|
260
602
|
if (ranges.some(([start, end]) => index >= start && index < end)) return null;
|
|
261
603
|
if (finalProvider && index === parsed.providersLine) return ' providers: {}';
|
package/src/runtime-identity.mjs
CHANGED
|
@@ -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.
|
|
29
|
+
export const RUNTIME_VERSION = '1.1.1';
|
|
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
|
+
}
|