@zoraxylia/dsh-codex-adapter 0.0.0-stage → 0.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.
- package/.claude-plugin/marketplace.json +15 -0
- package/.claude-plugin/plugin.json +8 -0
- package/.mcp.json +8 -0
- package/LICENSE +21 -0
- package/README.de.md +480 -0
- package/README.es.md +480 -0
- package/README.fr.md +480 -0
- package/README.hi.md +480 -0
- package/README.id.md +480 -0
- package/README.ja.md +478 -0
- package/README.ko.md +480 -0
- package/README.md +483 -2
- package/README.pt.md +480 -0
- package/README.ru.md +480 -0
- package/README.th.md +480 -0
- package/README.tr.md +480 -0
- package/README.vi.md +480 -0
- package/README.zh-TW.md +478 -0
- package/README.zh.md +482 -0
- package/agents/ds-flash.md +17 -0
- package/agents/ds-pro.md +17 -0
- package/agy/agents/ds-flash.md +18 -0
- package/agy/agents/ds-pro.md +18 -0
- package/agy/skills/dsh-config/SKILL.md +6 -0
- package/agy/skills/dsh-playbook/SKILL.md +65 -0
- package/agy/skills/dsh-status/SKILL.md +6 -0
- package/codex/agents/ds-flash.toml +23 -0
- package/codex/agents/ds-pro.toml +23 -0
- package/codex/prompts/dsh-config.md +3 -0
- package/codex/prompts/dsh-playbook.md +60 -0
- package/codex/prompts/dsh-status.md +1 -0
- package/commands/config.md +9 -0
- package/commands/off.md +5 -0
- package/commands/on.md +5 -0
- package/commands/playbook.md +64 -0
- package/commands/status.md +5 -0
- package/cordis.patch.yml +4 -0
- package/docs/images/dsh-crew-host.png +0 -0
- package/docs/images/dsh-crew-jobs.png +0 -0
- package/docs/images/dsh-crew-logo.png +0 -0
- package/docs/images/dsh-crew-overview.png +0 -0
- package/grok/agents/ds-flash.md +19 -0
- package/grok/agents/ds-pro.md +19 -0
- package/grok/commands/dsh-config.md +9 -0
- package/grok/commands/dsh-playbook.md +66 -0
- package/grok/commands/dsh-status.md +5 -0
- package/lib/client.js +2684 -0
- package/lib/server.mjs +15417 -0
- package/package.json +137 -4
- package/scripts/build-client.mjs +23 -0
- package/scripts/build-mcp.mjs +43 -0
- package/scripts/fake-agy.sh +23 -0
- package/scripts/release-local.mjs +87 -0
- package/scripts/smoke-bundle.mjs +166 -0
- package/scripts/smoke-cwd.mjs +41 -0
- package/scripts/smoke-pack.mjs +285 -0
- package/scripts/smoke.mjs +79 -0
- package/scripts/verify-cli-workers.mjs +133 -0
- package/scripts/verify-wpc10-client.mjs +237 -0
- package/scripts/verify-wpc14-proc-endpoints.mjs +356 -0
- package/scripts/verify-wpc9-guardrails.mjs +432 -0
- package/src/cli-workers.mjs +396 -0
- package/src/client/index.tsx +1133 -0
- package/src/client/jobs-view.ts +374 -0
- package/src/cwd-lock.mjs +91 -0
- package/src/hub/index.mjs +643 -0
- package/src/hub-client.mjs +72 -0
- package/src/i18n.mjs +19 -0
- package/src/install/cli.mjs +34 -0
- package/src/install/install.mjs +577 -0
- package/src/jobs.mjs +289 -0
- package/src/multimodal.mjs +744 -0
- package/src/origin-guard.mjs +133 -0
- package/src/paths.mjs +43 -0
- package/src/plugin-env.mjs +65 -0
- package/src/proc-kill.mjs +208 -0
- package/src/process-info.mjs +60 -0
- package/src/provider-probe.mjs +35 -0
- package/src/server.mjs +438 -0
- package/src/status-shard.mjs +95 -0
- package/src/tier-binding.mjs +63 -0
- package/src/vision-route.mjs +139 -0
- package/src/worker-profiles.mjs +95 -0
- package/src/worker-question.mjs +22 -0
- package/statusline/statusline.sh +14 -0
- package/statusline/worker-segment.sh +35 -0
- package/worker.cordis.yml +77 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// WPC9 guardrail verification: recursion guard + same-repo concurrency lock.
|
|
2
|
+
// House rule: fixture passing is not the same as really working — so every
|
|
3
|
+
// check here drives the REAL bundled MCP server (lib/server.mjs) over stdio,
|
|
4
|
+
// with the only stub being the external agy binary itself (a PATH shim that
|
|
5
|
+
// speaks agy's stream-json protocol). No real CLI tokens are spent and no
|
|
6
|
+
// live hub is contacted (DSH_CREW_HUB points at a dead port, or at an
|
|
7
|
+
// in-process mock hub for the hub-path tests). scripts/smoke.mjs is
|
|
8
|
+
// deliberately NOT run.
|
|
9
|
+
//
|
|
10
|
+
// Phases:
|
|
11
|
+
// A: adapter-level unit checks (origin-guard + cwd-lock + startJob refusal)
|
|
12
|
+
// B: end-to-end through the bundled server with a fake agy:
|
|
13
|
+
// B1 depth-exceeded refusal, B2 loop refusal, B3 dispatch + origin
|
|
14
|
+
// record + env propagation, B4 concurrent reject + holder info +
|
|
15
|
+
// status lock entry, B5 allow_concurrent_cwd, B6 release on completion,
|
|
16
|
+
// B7 release on cancel, B8 release on timeout
|
|
17
|
+
// C: hub path with an in-process mock hub (origin in spawn spec, cwd lock,
|
|
18
|
+
// stale-lock purge, origin re-attachment to hub views)
|
|
19
|
+
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
21
|
+
import { mkdtempSync, mkdirSync, writeFileSync, copyFileSync, chmodSync, rmSync, readFileSync, existsSync } from 'node:fs';
|
|
22
|
+
import { tmpdir } from 'node:os';
|
|
23
|
+
import { join, resolve, dirname } from 'node:path';
|
|
24
|
+
import { fileURLToPath } from 'node:url';
|
|
25
|
+
import http from 'node:http';
|
|
26
|
+
|
|
27
|
+
const REPO = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
28
|
+
const WORK = mkdtempSync(join(tmpdir(), 'wpc9-verify-'));
|
|
29
|
+
const FAKE_HOME = join(WORK, 'home');
|
|
30
|
+
const BIN = join(WORK, 'bin');
|
|
31
|
+
mkdirSync(FAKE_HOME, { recursive: true });
|
|
32
|
+
mkdirSync(BIN, { recursive: true });
|
|
33
|
+
const DEAD_HUB = 'http://127.0.0.1:9';
|
|
34
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
35
|
+
|
|
36
|
+
let failures = 0;
|
|
37
|
+
function check(cond, label, detail) {
|
|
38
|
+
console.log((cond ? 'PASS ' : 'FAIL ') + label);
|
|
39
|
+
if (detail !== undefined) console.log(' ' + String(detail).split(String.fromCharCode(10)).join(String.fromCharCode(10) + ' '));
|
|
40
|
+
if (!cond) failures++;
|
|
41
|
+
}
|
|
42
|
+
function sec(label) { console.log(String.fromCharCode(10) + '===== ' + label + ' ====='); }
|
|
43
|
+
|
|
44
|
+
// ---------- fake agy CLI (stream-json double, controlled via env/files) ----------
|
|
45
|
+
copyFileSync(join(REPO, 'scripts', 'fake-agy.sh'), join(BIN, 'agy'));
|
|
46
|
+
chmodSync(join(BIN, 'agy'), 0o755);
|
|
47
|
+
|
|
48
|
+
// ---------- MCP stdio client ----------
|
|
49
|
+
function makeClient(child) {
|
|
50
|
+
const pending = new Map();
|
|
51
|
+
let nextId = 1;
|
|
52
|
+
let buf = '';
|
|
53
|
+
let stderr = '';
|
|
54
|
+
child.stderr.on('data', (d) => { stderr += d.toString(); });
|
|
55
|
+
child.stdout.on('data', (d) => {
|
|
56
|
+
buf += d.toString();
|
|
57
|
+
let i;
|
|
58
|
+
while ((i = buf.indexOf(String.fromCharCode(10))) >= 0) {
|
|
59
|
+
const line = buf.slice(0, i).trim();
|
|
60
|
+
buf = buf.slice(i + 1);
|
|
61
|
+
if (!line) continue;
|
|
62
|
+
let msg; try { msg = JSON.parse(line); } catch { continue; }
|
|
63
|
+
if (msg.id !== undefined && pending.has(msg.id)) {
|
|
64
|
+
const p = pending.get(msg.id);
|
|
65
|
+
pending.delete(msg.id);
|
|
66
|
+
clearTimeout(p.timer);
|
|
67
|
+
if (msg.error) p.reject(new Error(msg.error.code + ': ' + msg.error.message));
|
|
68
|
+
else p.resolve(msg.result);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
function request(method, params) {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const id = nextId++;
|
|
75
|
+
const timer = setTimeout(() => reject(new Error('timeout waiting for ' + method + ' — server stderr: ' + stderr.slice(-800))), 40000);
|
|
76
|
+
pending.set(id, { resolve, reject, timer });
|
|
77
|
+
child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + String.fromCharCode(10));
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return { request, stderr: () => stderr };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function startServer(envExtra = {}) {
|
|
84
|
+
const child = spawn(process.execPath, [join(REPO, 'lib', 'server.mjs')], {
|
|
85
|
+
cwd: REPO,
|
|
86
|
+
env: { ...process.env, HOME: FAKE_HOME, DSH_CREW_HUB: DEAD_HUB, PATH: BIN + ':' + process.env.PATH, ...envExtra },
|
|
87
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
88
|
+
});
|
|
89
|
+
const client = makeClient(child);
|
|
90
|
+
await client.request('initialize', { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'wpc9-verify', version: '0.0.0' } });
|
|
91
|
+
child.stdin.write(JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' }) + String.fromCharCode(10));
|
|
92
|
+
return {
|
|
93
|
+
child, client,
|
|
94
|
+
call: async (name, args) => {
|
|
95
|
+
const result = await client.request('tools/call', { name, arguments: args });
|
|
96
|
+
const text = (result?.content ?? []).filter((c) => c.type === 'text').map((c) => c.text).join(String.fromCharCode(10));
|
|
97
|
+
let json = null;
|
|
98
|
+
try { json = JSON.parse(text); } catch {}
|
|
99
|
+
return { result, text, json, isError: !!result?.isError };
|
|
100
|
+
},
|
|
101
|
+
stop: () => { try { child.kill('SIGTERM'); } catch {} },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ================= PHASE A: adapter-level =================
|
|
106
|
+
sec('Phase A — adapter-level unit checks');
|
|
107
|
+
const originMod = await import('../src/origin-guard.mjs');
|
|
108
|
+
const lockMod = await import('../src/cwd-lock.mjs');
|
|
109
|
+
const { extendOrigin, readInheritedOrigin, DEFAULT_ORIGIN_DEPTH_LIMIT } = originMod;
|
|
110
|
+
const { acquireCwdLock, getCwdLocks, releaseCwdLockByJobId, normalizeCwd, CwdLockError } = lockMod;
|
|
111
|
+
|
|
112
|
+
const dirA = join(WORK, 'repoA'); mkdirSync(dirA, { recursive: true });
|
|
113
|
+
const dirB = join(WORK, 'repoB'); mkdirSync(dirB, { recursive: true });
|
|
114
|
+
const dirC = join(WORK, 'repoC'); mkdirSync(dirC, { recursive: true });
|
|
115
|
+
const dirD = join(WORK, 'repoD'); mkdirSync(dirD, { recursive: true });
|
|
116
|
+
const dirE = join(WORK, 'repoE'); mkdirSync(dirE, { recursive: true });
|
|
117
|
+
const dirF = join(WORK, 'repoF'); mkdirSync(dirF, { recursive: true });
|
|
118
|
+
const canonA = normalizeCwd(dirA);
|
|
119
|
+
|
|
120
|
+
check(DEFAULT_ORIGIN_DEPTH_LIMIT === 3, 'A0 depth limit default is 3', 'DEFAULT_ORIGIN_DEPTH_LIMIT=' + DEFAULT_ORIGIN_DEPTH_LIMIT);
|
|
121
|
+
|
|
122
|
+
// A1: depth-exceeded refusal
|
|
123
|
+
{
|
|
124
|
+
const inherited3 = { chain: [1, 2, 3].map((i) => ({ backend: 'standalone', cwd: join(WORK, 'hop' + i), source: 'claude-code' })), depth: 3 };
|
|
125
|
+
const r = extendOrigin({ inherited: inherited3, backend: 'agy', cwd: dirB, source: 'codex', depthLimit: 3 });
|
|
126
|
+
check(!r.ok && r.error.code === 'origin-depth-exceeded' && r.error.depth === 4 && r.error.limit === 3,
|
|
127
|
+
'A1 depth-exceeded refusal (chain 3 + 1 hop > limit 3)', r.error?.message);
|
|
128
|
+
const r2 = extendOrigin({ inherited: { chain: [inherited3.chain[0]], depth: 1 }, backend: 'agy', cwd: dirB, source: 'codex', depthLimit: 3 });
|
|
129
|
+
check(r2.ok && r2.origin.depth === 2, 'A1b depth 2 still allowed');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// A2: loop refusal
|
|
133
|
+
{
|
|
134
|
+
const inheritedLoop = { chain: [{ backend: 'agy', cwd: canonA, source: 'claude-code' }], depth: 1 };
|
|
135
|
+
const r = extendOrigin({ inherited: inheritedLoop, backend: 'agy', cwd: dirA, source: 'codex', depthLimit: 3 });
|
|
136
|
+
check(!r.ok && r.error.code === 'origin-loop', 'A2 loop refusal (same backend+cwd repeats)', r.error?.message);
|
|
137
|
+
const r2 = extendOrigin({ inherited: inheritedLoop, backend: 'grok', cwd: dirA, source: 'codex', depthLimit: 3 });
|
|
138
|
+
check(r2.ok && r2.origin.depth === 2, 'A2b same cwd, different backend is NOT a loop');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// A4: env round-trip + garbage
|
|
142
|
+
{
|
|
143
|
+
const chain = [{ backend: 'standalone', cwd: dirA, source: 'claude-code' }, { backend: 'agy', cwd: dirB, source: 'codex' }];
|
|
144
|
+
const parsed = readInheritedOrigin({ DSH_CREW_ORIGIN_CHAIN: JSON.stringify(chain) });
|
|
145
|
+
check(parsed.depth === 2 && parsed.chain.length === 2 && parsed.chain[1].backend === 'agy',
|
|
146
|
+
'A4 inherited chain parsed from env');
|
|
147
|
+
check(readInheritedOrigin({ DSH_CREW_ORIGIN_CHAIN: '{not json' }).depth === 0, 'A4b garbage env ignored');
|
|
148
|
+
check(readInheritedOrigin({}).depth === 0, 'A4c no env = empty chain');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// A5: lock conflict + holder info + release + bypass
|
|
152
|
+
{
|
|
153
|
+
const startedAt = new Date().toISOString();
|
|
154
|
+
const l1 = acquireCwdLock({ cwd: dirC, jobId: 'job-1', backend: 'standalone', mode: 'standalone', startedAt });
|
|
155
|
+
let err = null;
|
|
156
|
+
try { acquireCwdLock({ cwd: dirC, jobId: 'job-2', backend: 'agy', mode: 'cli', startedAt }); } catch (e) { err = e; }
|
|
157
|
+
check(err instanceof CwdLockError, 'A5 second acquire throws CwdLockError');
|
|
158
|
+
check(err?.message.includes('job-1') && err?.message.includes('standalone') && err?.message.includes(startedAt) && err?.message.includes('allow_concurrent_cwd'),
|
|
159
|
+
'A5 refusal names holder job id, backend, start time', err?.message);
|
|
160
|
+
l1.release();
|
|
161
|
+
const l2 = acquireCwdLock({ cwd: dirC, jobId: 'job-3', backend: 'grok', mode: 'cli', startedAt });
|
|
162
|
+
check(!!l2, 'A5 release allows re-acquire'); l2.release();
|
|
163
|
+
acquireCwdLock({ cwd: dirC, jobId: 'job-4', backend: 'x', mode: 'm', startedAt, allowConcurrent: true });
|
|
164
|
+
const l4 = acquireCwdLock({ cwd: dirC, jobId: 'job-5', backend: 'y', mode: 'm', startedAt });
|
|
165
|
+
check(!!l4, 'A5 allow_concurrent_cwd bypasses the lock'); l4.release();
|
|
166
|
+
check(getCwdLocks().length === 0, 'A5 lock table empty after releases');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A6: symlink/canonical alias shares one lock
|
|
170
|
+
{
|
|
171
|
+
const alias = join(tmpdir(), 'wpc9-alias-' + Date.now()); mkdirSync(alias, { recursive: true });
|
|
172
|
+
const startedAt = new Date().toISOString();
|
|
173
|
+
const l5 = acquireCwdLock({ cwd: alias, jobId: 'job-6', backend: 'x', mode: 'm', startedAt });
|
|
174
|
+
let aliased = false;
|
|
175
|
+
try { acquireCwdLock({ cwd: normalizeCwd(alias), jobId: 'job-7', backend: 'y', mode: 'm', startedAt }); } catch (e) { aliased = true; }
|
|
176
|
+
check(aliased, 'A6 canonical-path alias shares one lock (' + alias + ' vs ' + normalizeCwd(alias) + ')');
|
|
177
|
+
l5.release();
|
|
178
|
+
rmSync(alias, { recursive: true, force: true });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// A7: standalone startJob refuses under a held lock — before any SDK/key/runtime work
|
|
182
|
+
{
|
|
183
|
+
process.env.HOME = FAKE_HOME;
|
|
184
|
+
process.env.DEEPSEEK_API_KEY = 'wpc9-dummy-key'; // never read: refusal fires first
|
|
185
|
+
const jobsMod = await import('../src/jobs.mjs');
|
|
186
|
+
const startedAt = new Date().toISOString();
|
|
187
|
+
const lk = acquireCwdLock({ cwd: dirB, jobId: 'job-holder', backend: 'standalone', mode: 'standalone', startedAt });
|
|
188
|
+
let err = null;
|
|
189
|
+
try { await jobsMod.startJob({ task: 'must never run', cwd: dirB, source: 'verify' }); } catch (e) { err = e; }
|
|
190
|
+
const refused = err instanceof CwdLockError;
|
|
191
|
+
check(refused, 'A7 standalone startJob refused before any runtime launch', err?.message);
|
|
192
|
+
if (!refused) { console.error('A7: guard did not fire — a real runtime may have launched; aborting.'); process.exit(1); }
|
|
193
|
+
check(err?.message.includes('job-holder'), 'A7 standalone refusal names the holder');
|
|
194
|
+
check(jobsMod.listJobs().length === 0, 'A7 no phantom job registered after refusal');
|
|
195
|
+
lk.release();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ================= PHASE B: bundled server end-to-end (fake agy) =================
|
|
199
|
+
sec('Phase B — bundled MCP server end-to-end with fake agy');
|
|
200
|
+
|
|
201
|
+
// B1: depth-exceeded through the real tool, with a 3-hop inherited chain
|
|
202
|
+
{
|
|
203
|
+
const chain = [1, 2, 3].map((i) => ({ backend: i === 3 ? 'agy' : 'standalone', cwd: join(WORK, 'b1hop' + i), source: 'claude-code' }));
|
|
204
|
+
const capture = join(WORK, 'b1-capture');
|
|
205
|
+
const srv = await startServer({ DSH_CREW_ORIGIN_CHAIN: JSON.stringify(chain), DSH_CREW_DEPTH: '3', FAKE_AGY_CAPTURE: capture });
|
|
206
|
+
const r = await srv.call('dsh_run_worker', { task: 'say ok', worker: 'agy', cwd: dirB, timeout_seconds: 30 });
|
|
207
|
+
console.log(' B1 tool response: ' + r.text.slice(0, 900));
|
|
208
|
+
check(r.json?.error?.includes('origin guard') === true && r.json?.code === 'origin-depth-exceeded' && r.json?.depth === 4,
|
|
209
|
+
'B1 real server refuses over-deep dispatch with readable error');
|
|
210
|
+
check(!existsSync(capture), 'B1 refused BEFORE spawning the CLI (no capture file)');
|
|
211
|
+
const st = await srv.call('dsh_worker_status', {});
|
|
212
|
+
check(Array.isArray(st.json) && st.json.length === 0, 'B1 no job was created');
|
|
213
|
+
srv.stop();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// B2: loop through the real tool (agy@dirA already in the inherited chain)
|
|
217
|
+
{
|
|
218
|
+
const capture = join(WORK, 'b2-capture');
|
|
219
|
+
const srv = await startServer({
|
|
220
|
+
DSH_CREW_ORIGIN_CHAIN: JSON.stringify([{ backend: 'agy', cwd: canonA, source: 'claude-code' }]), DSH_CREW_DEPTH: '1',
|
|
221
|
+
FAKE_AGY_CAPTURE: capture,
|
|
222
|
+
});
|
|
223
|
+
const r = await srv.call('dsh_run_worker', { task: 'say ok', worker: 'agy', cwd: dirA, timeout_seconds: 30 });
|
|
224
|
+
console.log(' B2 tool response: ' + r.text.slice(0, 900));
|
|
225
|
+
check(r.json?.error?.includes('origin guard') === true && r.json?.code === 'origin-loop' && (r.json?.message ?? '').includes(canonA),
|
|
226
|
+
'B2 real server refuses the (backend,cwd) loop with readable error');
|
|
227
|
+
check(!existsSync(capture), 'B2 refused BEFORE spawning the CLI (no capture file)');
|
|
228
|
+
srv.stop();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// B3: clean dispatch — job record carries the origin; the child inherits it via env
|
|
232
|
+
{
|
|
233
|
+
const capture = join(WORK, 'b3-capture');
|
|
234
|
+
const srv = await startServer({ FAKE_AGY_CAPTURE: capture, FAKE_AGY_DELAY_MS: '150' });
|
|
235
|
+
const r = await srv.call('dsh_run_worker', { task: 'say ok', worker: 'agy', cwd: dirB, timeout_seconds: 60 });
|
|
236
|
+
console.log(' B3 tool response: ' + r.text.slice(0, 700));
|
|
237
|
+
const v = r.json;
|
|
238
|
+
check(v?.status === 'done' && v?.result === 'ok', 'B3 fake-agy dispatch completes (done, result "ok")');
|
|
239
|
+
check(v?.origin_depth === 1 && Array.isArray(v?.origin_chain) && v?.origin_chain.length === 1
|
|
240
|
+
&& v?.origin_chain[0]?.backend === 'agy' && v?.origin_chain[0]?.cwd === normalizeCwd(dirB),
|
|
241
|
+
'B3 job record carries origin_depth + origin_chain', JSON.stringify({ origin_depth: v?.origin_depth, origin_chain: v?.origin_chain }));
|
|
242
|
+
const capturedChain = existsSync(capture) ? readFileSync(capture, 'utf8') : '(no capture file)';
|
|
243
|
+
const capturedDepth = existsSync(capture + '.depth') ? readFileSync(capture + '.depth', 'utf8') : '(no depth file)';
|
|
244
|
+
check(capturedChain.includes('"agy"') && capturedChain.includes(normalizeCwd(dirB)) && capturedDepth === '1',
|
|
245
|
+
'B3 CLI child inherited the chain via DSH_CREW_ORIGIN_CHAIN/DSH_CREW_DEPTH env', 'chain=' + capturedChain + ' depth=' + capturedDepth);
|
|
246
|
+
const cfg = await srv.call('dsh_worker_config', {});
|
|
247
|
+
check(cfg.json?.origin?.depth === 0 && cfg.json?.origin?.depth_limit === 3 && cfg.json?.origin_depth_limit === 3,
|
|
248
|
+
'B3 dsh_worker_config shows inherited origin + default limit');
|
|
249
|
+
const cfg2 = await srv.call('dsh_worker_config', { origin_depth_limit: 2 });
|
|
250
|
+
check(cfg2.json?.origin_depth_limit === 2 && cfg2.json?.origin?.depth_limit === 2, 'B3 origin_depth_limit adjustable via dsh_worker_config');
|
|
251
|
+
const cfg3 = await srv.call('dsh_worker_config', { reset: true });
|
|
252
|
+
check(cfg3.json?.origin_depth_limit === 3, 'B3 reset restores the default limit');
|
|
253
|
+
srv.stop();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// B4-B7: concurrency lock — reject, bypass, release on completion, release on cancel
|
|
257
|
+
{
|
|
258
|
+
const delayFile = join(WORK, 'b4-delay'); writeFileSync(delayFile, '5000');
|
|
259
|
+
const modeFile = join(WORK, 'b4-mode'); writeFileSync(modeFile, 'NORMAL');
|
|
260
|
+
const srv = await startServer({ FAKE_AGY_DELAY_FILE: delayFile, FAKE_AGY_MODE_FILE: modeFile });
|
|
261
|
+
|
|
262
|
+
const spawned = await srv.call('dsh_spawn_worker', { task: 'long task', worker: 'agy', cwd: dirC });
|
|
263
|
+
const holderId = spawned.json?.id;
|
|
264
|
+
check(spawned.json?.status === 'running' && !!holderId, 'B4 spawn job 1 in ' + dirC + ' (id ' + holderId + ')');
|
|
265
|
+
|
|
266
|
+
const blocked = await srv.call('dsh_run_worker', { task: 'other task', worker: 'agy', cwd: dirC, timeout_seconds: 10 });
|
|
267
|
+
console.log(' B4 rejection response: ' + blocked.text.slice(0, 700));
|
|
268
|
+
check(blocked.json?.rejected_by === 'cwd-lock' && blocked.json?.holder?.jobId === holderId
|
|
269
|
+
&& blocked.json?.holder?.backend === 'agy' && typeof blocked.json?.holder?.startedAt === 'string',
|
|
270
|
+
'B4 second dispatch to the same cwd is rejected with holder info');
|
|
271
|
+
check((blocked.json?.error ?? '').includes(holderId) && (blocked.json?.error ?? '').includes('allow_concurrent_cwd'),
|
|
272
|
+
'B4 rejection error names holder job id + suggests allow_concurrent_cwd');
|
|
273
|
+
|
|
274
|
+
const st1 = await srv.call('dsh_worker_status', {});
|
|
275
|
+
const lockEntry = (st1.json ?? []).find((x) => x?.kind === 'cwd-lock');
|
|
276
|
+
check(!!lockEntry && lockEntry.holder?.jobId === holderId && lockEntry.cwd === normalizeCwd(dirC),
|
|
277
|
+
'B4 dsh_worker_status observes the lock (which cwd is held by whom)', JSON.stringify(lockEntry));
|
|
278
|
+
|
|
279
|
+
writeFileSync(delayFile, '200');
|
|
280
|
+
const allowed = await srv.call('dsh_run_worker', { task: 'read-only task', worker: 'agy', cwd: dirC, timeout_seconds: 60, allow_concurrent_cwd: true });
|
|
281
|
+
check(allowed.json?.status === 'done', 'B5 allow_concurrent_cwd: true lets the parallel dispatch through');
|
|
282
|
+
|
|
283
|
+
// B6: release on completion — wait for job 1, then a plain dispatch succeeds
|
|
284
|
+
const done1 = await srv.call('dsh_worker_result', { job_id: holderId, wait_seconds: 20 });
|
|
285
|
+
check(done1.json?.status === 'done' && !!done1.json?.endedAt, 'B6 job 1 completed (release-on-completion precondition)');
|
|
286
|
+
const again = await srv.call('dsh_run_worker', { task: 'after completion', worker: 'agy', cwd: dirC, timeout_seconds: 60 });
|
|
287
|
+
check(again.json?.status === 'done', 'B6 same-cwd dispatch succeeds after the holder completed (lock released)');
|
|
288
|
+
|
|
289
|
+
// B7: release on cancel
|
|
290
|
+
writeFileSync(modeFile, 'HANG');
|
|
291
|
+
const hanging = await srv.call('dsh_spawn_worker', { task: 'hang forever', worker: 'agy', cwd: dirD });
|
|
292
|
+
const hangId = hanging.json?.id;
|
|
293
|
+
check(hanging.json?.status === 'running' && !!hangId, 'B7 spawn hanging job in ' + dirD + ' (id ' + hangId + ')');
|
|
294
|
+
await sleep(400);
|
|
295
|
+
const cancelled = await srv.call('dsh_worker_cancel', { job_id: hangId });
|
|
296
|
+
check(cancelled.json?.status === 'cancelled', 'B7 cancel accepted (status cancelled)');
|
|
297
|
+
let settledView = null;
|
|
298
|
+
for (let i = 0; i < 30; i++) {
|
|
299
|
+
const r = await srv.call('dsh_worker_result', { job_id: hangId, wait_seconds: 1 });
|
|
300
|
+
if (r.json?.endedAt) { settledView = r.json; break; }
|
|
301
|
+
await sleep(200);
|
|
302
|
+
}
|
|
303
|
+
check(!!settledView && settledView.status === 'cancelled', 'B7 cancelled job fully settled (endedAt set)');
|
|
304
|
+
writeFileSync(modeFile, 'NORMAL');
|
|
305
|
+
const afterCancel = await srv.call('dsh_run_worker', { task: 'after cancel', worker: 'agy', cwd: dirD, timeout_seconds: 60 });
|
|
306
|
+
check(afterCancel.json?.status === 'done', 'B7 same-cwd dispatch succeeds after cancel (lock released)');
|
|
307
|
+
srv.stop();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// B8: release on timeout
|
|
311
|
+
{
|
|
312
|
+
const delayFile = join(WORK, 'b8-delay'); writeFileSync(delayFile, '20000');
|
|
313
|
+
const srv = await startServer({ FAKE_AGY_DELAY_FILE: delayFile });
|
|
314
|
+
const r = await srv.call('dsh_run_worker', { task: 'slow task', worker: 'agy', cwd: dirE, timeout_seconds: 2 });
|
|
315
|
+
console.log(' B8 first (timed-out) dispatch response: ' + r.text.slice(0, 400));
|
|
316
|
+
const id = r.json?.id;
|
|
317
|
+
check(!!id && (r.json?.status === 'running' || r.json?.status === 'failed'), 'B8 dispatch started then hit the 2s deadline (id ' + id + ')');
|
|
318
|
+
let settled = null;
|
|
319
|
+
for (let i = 0; i < 40; i++) {
|
|
320
|
+
const p = await srv.call('dsh_worker_result', { job_id: id, wait_seconds: 2 });
|
|
321
|
+
if (p.json?.endedAt) { settled = p.json; break; }
|
|
322
|
+
}
|
|
323
|
+
check(!!settled && settled.status === 'failed' && settled.stopReason === 'timeout', 'B8 timed-out job settled failed/timeout',
|
|
324
|
+
JSON.stringify({ status: settled?.status, stopReason: settled?.stopReason }));
|
|
325
|
+
writeFileSync(delayFile, '200');
|
|
326
|
+
const again = await srv.call('dsh_run_worker', { task: 'after timeout', worker: 'agy', cwd: dirE, timeout_seconds: 60 });
|
|
327
|
+
check(again.json?.status === 'done', 'B8 same-cwd dispatch succeeds after the timeout settled (lock released)');
|
|
328
|
+
srv.stop();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ================= PHASE C: hub path with a mock hub =================
|
|
332
|
+
sec('Phase C — hub path (in-process mock hub; the live hub is never contacted)');
|
|
333
|
+
|
|
334
|
+
async function startMockHub() {
|
|
335
|
+
const state = { jobs: new Map(), spawnBodies: [], nextId: 1 };
|
|
336
|
+
const server = http.createServer((req, res) => {
|
|
337
|
+
const send = (status, body) => {
|
|
338
|
+
const s = JSON.stringify(body);
|
|
339
|
+
res.writeHead(status, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(s) });
|
|
340
|
+
res.end(s);
|
|
341
|
+
};
|
|
342
|
+
const url = new URL(req.url, 'http://localhost');
|
|
343
|
+
const route = url.pathname.slice('/_dsh/dsh-crew'.length);
|
|
344
|
+
try {
|
|
345
|
+
if (route === '/ping') return send(200, { ok: true, service: 'dsh-crew-hub' });
|
|
346
|
+
if (route === '/jobs' && req.method === 'GET') return send(200, { ok: true, jobs: [...state.jobs.values()] });
|
|
347
|
+
if (route === '/jobs' && req.method === 'POST') {
|
|
348
|
+
let body = '';
|
|
349
|
+
req.on('data', (d) => { body += d; });
|
|
350
|
+
req.on('end', () => {
|
|
351
|
+
const spec = JSON.parse(body);
|
|
352
|
+
state.spawnBodies.push(spec);
|
|
353
|
+
const id = 'hub-' + (state.nextId++) + '-mock';
|
|
354
|
+
const job = {
|
|
355
|
+
id, tier: spec.tier, effort: spec.effort, status: 'running', source: spec.source,
|
|
356
|
+
task: String(spec.task ?? '').slice(0, 300), cwd: spec.cwd, mode: 'hub',
|
|
357
|
+
turn: 0, step: 0, tokens: { input: 0, output: 0, reasoning: 0 },
|
|
358
|
+
startedAt: new Date().toISOString(), endedAt: null, result: null, error: null, stopReason: null,
|
|
359
|
+
};
|
|
360
|
+
state.jobs.set(id, job);
|
|
361
|
+
send(200, { ok: true, job: { ...job } });
|
|
362
|
+
});
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const m = route.match(/^\/jobs\/([^\/]+)$/);
|
|
366
|
+
if (m && req.method === 'GET') {
|
|
367
|
+
const job = state.jobs.get(m[1]);
|
|
368
|
+
if (!job) return send(404, { ok: false, error: 'no such job' });
|
|
369
|
+
return send(200, { ok: true, job: { ...job, ...(job.status !== 'running' ? { result: job.result ?? '', stopReason: job.stopReason } : {}) } });
|
|
370
|
+
}
|
|
371
|
+
const c = route.match(/^\/jobs\/([^\/]+)\/cancel$/);
|
|
372
|
+
if (c && req.method === 'POST') {
|
|
373
|
+
const job = state.jobs.get(c[1]);
|
|
374
|
+
if (!job) return send(404, { ok: false, error: 'no such job' });
|
|
375
|
+
job.status = 'cancelled'; job.error = 'cancelled by request'; job.endedAt = new Date().toISOString();
|
|
376
|
+
return send(200, { ok: true, job: { ...job, result: '', stopReason: 'cancelled' } });
|
|
377
|
+
}
|
|
378
|
+
return send(404, { ok: false, error: 'unknown jobs endpoint' });
|
|
379
|
+
} catch (err) {
|
|
380
|
+
return send(400, { ok: false, error: err?.message ?? String(err) });
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
|
384
|
+
return { state, url: 'http://127.0.0.1:' + server.address().port, close: () => server.close() };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
{
|
|
388
|
+
const mock = await startMockHub();
|
|
389
|
+
const srv = await startServer({ DSH_CREW_HUB: mock.url });
|
|
390
|
+
|
|
391
|
+
const first = await srv.call('dsh_spawn_worker', { task: 'hub task', cwd: dirF });
|
|
392
|
+
const hubId = first.json?.id;
|
|
393
|
+
check(first.json?.status === 'running' && !!hubId, 'C1 hub spawn accepted (id ' + hubId + ')');
|
|
394
|
+
const body0 = mock.state.spawnBodies[0] ?? {};
|
|
395
|
+
check(body0.origin_chain?.length === 1 && body0.origin_chain?.[0]?.backend === 'hub' && body0.origin_chain?.[0]?.cwd === normalizeCwd(dirF)
|
|
396
|
+
&& body0.origin_depth === 1,
|
|
397
|
+
'C1 hub spawn spec carries origin_chain + origin_depth', JSON.stringify({ origin_chain: body0.origin_chain, origin_depth: body0.origin_depth }));
|
|
398
|
+
check(first.json?.origin_depth === 1 && first.json?.origin_chain?.length === 1,
|
|
399
|
+
'C1 hub job view re-attached the origin client-side', JSON.stringify({ origin_depth: first.json?.origin_depth, origin_chain: first.json?.origin_chain }));
|
|
400
|
+
|
|
401
|
+
const blocked = await srv.call('dsh_spawn_worker', { task: 'second hub task', cwd: dirF });
|
|
402
|
+
console.log(' C1 rejection response: ' + blocked.text.slice(0, 600));
|
|
403
|
+
check(blocked.json?.rejected_by === 'cwd-lock' && blocked.json?.holder?.jobId === hubId && blocked.json?.holder?.backend === 'hub',
|
|
404
|
+
'C1 second hub dispatch to the same cwd is rejected with holder info');
|
|
405
|
+
|
|
406
|
+
const resWhileRunning = await srv.call('dsh_worker_result', { job_id: hubId, wait_seconds: 0 });
|
|
407
|
+
check(resWhileRunning.json?.status === 'running' && resWhileRunning.json?.origin_depth === 1,
|
|
408
|
+
'C2 hub result view while running carries the origin');
|
|
409
|
+
|
|
410
|
+
mock.state.jobs.get(hubId).status = 'done';
|
|
411
|
+
mock.state.jobs.get(hubId).endedAt = new Date().toISOString();
|
|
412
|
+
const resDone = await srv.call('dsh_worker_result', { job_id: hubId, wait_seconds: 0 });
|
|
413
|
+
check(resDone.json?.status === 'done', 'C2 mock hub settles the job');
|
|
414
|
+
|
|
415
|
+
const st = await srv.call('dsh_worker_status', {});
|
|
416
|
+
const locks = (st.json ?? []).filter((x) => x?.kind === 'cwd-lock');
|
|
417
|
+
check(locks.length === 0, 'C2 dsh_worker_status purged the settled hub job lock');
|
|
418
|
+
|
|
419
|
+
const again = await srv.call('dsh_spawn_worker', { task: 'third hub task', cwd: dirF });
|
|
420
|
+
check(again.json?.status === 'running', 'C2 same-cwd hub dispatch succeeds after the holder settled');
|
|
421
|
+
|
|
422
|
+
srv.stop();
|
|
423
|
+
mock.close();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// ---------- summary ----------
|
|
427
|
+
sec('SUMMARY');
|
|
428
|
+
console.log('failures: ' + failures);
|
|
429
|
+
console.log('work dir: ' + WORK);
|
|
430
|
+
rmSync(WORK, { recursive: true, force: true });
|
|
431
|
+
if (failures > 0) { console.error('VERIFICATION FAILED (' + failures + ' failing checks)'); process.exit(1); }
|
|
432
|
+
console.log('ALL_WPC9_GUARDRAIL_CHECKS_PASSED');
|