@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.
Files changed (87) hide show
  1. package/.claude-plugin/marketplace.json +15 -0
  2. package/.claude-plugin/plugin.json +8 -0
  3. package/.mcp.json +8 -0
  4. package/LICENSE +21 -0
  5. package/README.de.md +480 -0
  6. package/README.es.md +480 -0
  7. package/README.fr.md +480 -0
  8. package/README.hi.md +480 -0
  9. package/README.id.md +480 -0
  10. package/README.ja.md +478 -0
  11. package/README.ko.md +480 -0
  12. package/README.md +483 -2
  13. package/README.pt.md +480 -0
  14. package/README.ru.md +480 -0
  15. package/README.th.md +480 -0
  16. package/README.tr.md +480 -0
  17. package/README.vi.md +480 -0
  18. package/README.zh-TW.md +478 -0
  19. package/README.zh.md +482 -0
  20. package/agents/ds-flash.md +17 -0
  21. package/agents/ds-pro.md +17 -0
  22. package/agy/agents/ds-flash.md +18 -0
  23. package/agy/agents/ds-pro.md +18 -0
  24. package/agy/skills/dsh-config/SKILL.md +6 -0
  25. package/agy/skills/dsh-playbook/SKILL.md +65 -0
  26. package/agy/skills/dsh-status/SKILL.md +6 -0
  27. package/codex/agents/ds-flash.toml +23 -0
  28. package/codex/agents/ds-pro.toml +23 -0
  29. package/codex/prompts/dsh-config.md +3 -0
  30. package/codex/prompts/dsh-playbook.md +60 -0
  31. package/codex/prompts/dsh-status.md +1 -0
  32. package/commands/config.md +9 -0
  33. package/commands/off.md +5 -0
  34. package/commands/on.md +5 -0
  35. package/commands/playbook.md +64 -0
  36. package/commands/status.md +5 -0
  37. package/cordis.patch.yml +4 -0
  38. package/docs/images/dsh-crew-host.png +0 -0
  39. package/docs/images/dsh-crew-jobs.png +0 -0
  40. package/docs/images/dsh-crew-logo.png +0 -0
  41. package/docs/images/dsh-crew-overview.png +0 -0
  42. package/grok/agents/ds-flash.md +19 -0
  43. package/grok/agents/ds-pro.md +19 -0
  44. package/grok/commands/dsh-config.md +9 -0
  45. package/grok/commands/dsh-playbook.md +66 -0
  46. package/grok/commands/dsh-status.md +5 -0
  47. package/lib/client.js +2684 -0
  48. package/lib/server.mjs +15417 -0
  49. package/package.json +137 -4
  50. package/scripts/build-client.mjs +23 -0
  51. package/scripts/build-mcp.mjs +43 -0
  52. package/scripts/fake-agy.sh +23 -0
  53. package/scripts/release-local.mjs +87 -0
  54. package/scripts/smoke-bundle.mjs +166 -0
  55. package/scripts/smoke-cwd.mjs +41 -0
  56. package/scripts/smoke-pack.mjs +285 -0
  57. package/scripts/smoke.mjs +79 -0
  58. package/scripts/verify-cli-workers.mjs +133 -0
  59. package/scripts/verify-wpc10-client.mjs +237 -0
  60. package/scripts/verify-wpc14-proc-endpoints.mjs +356 -0
  61. package/scripts/verify-wpc9-guardrails.mjs +432 -0
  62. package/src/cli-workers.mjs +396 -0
  63. package/src/client/index.tsx +1133 -0
  64. package/src/client/jobs-view.ts +374 -0
  65. package/src/cwd-lock.mjs +91 -0
  66. package/src/hub/index.mjs +643 -0
  67. package/src/hub-client.mjs +72 -0
  68. package/src/i18n.mjs +19 -0
  69. package/src/install/cli.mjs +34 -0
  70. package/src/install/install.mjs +577 -0
  71. package/src/jobs.mjs +289 -0
  72. package/src/multimodal.mjs +744 -0
  73. package/src/origin-guard.mjs +133 -0
  74. package/src/paths.mjs +43 -0
  75. package/src/plugin-env.mjs +65 -0
  76. package/src/proc-kill.mjs +208 -0
  77. package/src/process-info.mjs +60 -0
  78. package/src/provider-probe.mjs +35 -0
  79. package/src/server.mjs +438 -0
  80. package/src/status-shard.mjs +95 -0
  81. package/src/tier-binding.mjs +63 -0
  82. package/src/vision-route.mjs +139 -0
  83. package/src/worker-profiles.mjs +95 -0
  84. package/src/worker-question.mjs +22 -0
  85. package/statusline/statusline.sh +14 -0
  86. package/statusline/worker-segment.sh +35 -0
  87. package/worker.cordis.yml +77 -0
@@ -0,0 +1,396 @@
1
+ // CLI worker adapter: runs a named worker profile (worker-profiles.mjs) as a
2
+ // child CLI process and folds it into the shared job registry (jobs.mjs), so
3
+ // dsh_worker_status / dsh_worker_result / dsh_worker_cancel work unchanged.
4
+ //
5
+ // Spawning rules (both backends):
6
+ // - detached: true — the CLI leads its own process group, so cancel/timeout
7
+ // can SIGTERM (then SIGKILL after a grace period) the whole tree and no
8
+ // orphan processes survive (e.g. agy's in-process language server).
9
+ // - env: full process.env passthrough (auth, proxy, GROK_HOME etc.).
10
+ // - cwd: the caller-provided workspace.
11
+ // - timeout: a wall timer here kills the group at the deadline; agy
12
+ // additionally gets --print-timeout = deadline + 15s as a self-exit
13
+ // backstop in case our timer can never fire.
14
+ //
15
+ // Streams per backend:
16
+ // agy — `agy --print <task> --output-format stream-json …` emits NDJSON:
17
+ // {"event":"init"} / {"event":"step_update"} (text_delta + usage while
18
+ // generating; the liveness signal) / {"event":"result"} (final response).
19
+ // grok — `grok -p <task> --output-format streaming-messages-json
20
+ // --include-partial-messages …` emits NDJSON: {"type":"system"} init,
21
+ // {"type":"stream_event","event":{Anthropic wire event}} deltas
22
+ // (liveness signal), {"type":"assistant"} whole message,
23
+ // {"type":"result"} final. Usage from "result" replaces the per-message
24
+ // accumulation to avoid double counting.
25
+ //
26
+ // Liveness ("在动" vs "卡死"): every stream line refreshes activity.lastEventAt.
27
+ // A staleness timer flips liveness to "stalled" when no event arrived for
28
+ // STALL_SECONDS while the process is still running (heuristic — a long tool
29
+ // run with a quiet CLI may false-positive, but the process is still alive and
30
+ // job.status stays "running"). dsh_worker_status surfaces liveness + activity.
31
+
32
+ import { spawn } from 'node:child_process';
33
+ import { resolve } from 'node:path';
34
+ import { resolveProfile } from './worker-profiles.mjs';
35
+ import { registerJob, publishStatus } from './jobs.mjs';
36
+ import { acquireCwdLock } from './cwd-lock.mjs';
37
+ import { ORIGIN_CHAIN_ENV, ORIGIN_DEPTH_ENV } from './origin-guard.mjs';
38
+
39
+ export const STALL_SECONDS = 120;
40
+ export const DEFAULT_TIMEOUT_MS = 1_800_000; // matches standalone spawn default
41
+ const SIGKILL_GRACE_MS = 2_000;
42
+ const PUBLISH_MIN_INTERVAL_MS = 2_000;
43
+ const RAW_LINE_CAP = 300; // bounded memory: keep the last N stream lines for debugging
44
+ const STDERR_TAIL_BYTES = 4_096;
45
+
46
+ let nextCliId = 1;
47
+
48
+ /** SIGTERM the process group, SIGKILL it after a grace period. Never throws. */
49
+ export function killGroup(child) {
50
+ try { process.kill(-child.pid, 'SIGTERM'); } catch {}
51
+ const t = setTimeout(() => {
52
+ try { process.kill(-child.pid, 'SIGKILL'); } catch {}
53
+ }, SIGKILL_GRACE_MS);
54
+ t.unref?.();
55
+ }
56
+
57
+ function usageToTokens(u) {
58
+ return {
59
+ input: (u?.input_tokens ?? 0) + (u?.cache_read_input_tokens ?? 0),
60
+ output: u?.output_tokens ?? 0,
61
+ reasoning: u?.thinking_tokens ?? 0,
62
+ };
63
+ }
64
+
65
+ /** Absolute usage for a backend whose "result" event reports session totals. */
66
+ function setTokensFromUsage(job, u) {
67
+ job.tokens = usageToTokens(u);
68
+ }
69
+
70
+ function addTokensFromUsage(job, u) {
71
+ const t = usageToTokens(u);
72
+ job.tokens.input += t.input;
73
+ job.tokens.output += t.output;
74
+ job.tokens.reasoning += t.reasoning;
75
+ }
76
+
77
+ function buildArgs(profile, task, timeoutMs, effortOverride) {
78
+ if (profile.backend === 'agy') {
79
+ const timeoutSec = Math.ceil(timeoutMs / 1000);
80
+ const args = [
81
+ '--print', task,
82
+ '--output-format', 'stream-json',
83
+ '--dangerously-skip-permissions',
84
+ '--mode', 'accept-edits',
85
+ '--print-timeout', `${timeoutSec + 15}s`, // self-exit backstop above our deadline
86
+ ];
87
+ if (profile.model) args.push('--model', profile.model);
88
+ if (effortOverride && profile.effortMap?.[effortOverride]) {
89
+ args.push('--effort', profile.effortMap[effortOverride]);
90
+ }
91
+ return args;
92
+ }
93
+ if (profile.backend === 'grok') {
94
+ const args = [
95
+ '-p', task,
96
+ '--output-format', 'streaming-messages-json',
97
+ '--include-partial-messages',
98
+ '--permission-mode', 'bypassPermissions',
99
+ ];
100
+ if (profile.model) args.push('--model', profile.model);
101
+ // grok-4.5 is not a reasoning model; no --reasoning-effort even when the
102
+ // caller passes effort (profile.effortMap === null).
103
+ return args;
104
+ }
105
+ throw new Error(`no CLI adapter for backend "${profile.backend}"`);
106
+ }
107
+
108
+ function makeLineHandler(profile, job) {
109
+ if (profile.backend === 'agy') return handleAgyLine;
110
+ if (profile.backend === 'grok') return handleGrokLine;
111
+ throw new Error(`no CLI adapter for backend "${profile.backend}"`);
112
+ }
113
+
114
+ function handleAgyLine(job, line, msg) {
115
+ const ev = msg.event;
116
+ if (ev === 'init') {
117
+ job.permissionMode = msg.init?.permission_mode ?? null;
118
+ job.toolCount = msg.init?.tools?.length ?? 0;
119
+ return;
120
+ }
121
+ if (ev === 'step_update') {
122
+ const s = msg.step_update ?? {};
123
+ job.turn = s.step_index ?? job.turn;
124
+ job.step = (s.step_index ?? 0) + 1;
125
+ if (s.step_type === 'agent_response' && typeof s.text_delta === 'string') {
126
+ job._text = (job._text ?? '') + s.text_delta;
127
+ }
128
+ if (s.usage) addTokensFromUsage(job, s.usage);
129
+ return;
130
+ }
131
+ if (ev === 'result') {
132
+ const r = msg.result ?? {};
133
+ job._agyStatus = r.status ?? null;
134
+ if (typeof r.response === 'string') job.result = r.response;
135
+ if (r.usage) setTokensFromUsage(job, r.usage);
136
+ if (r.status && r.status !== 'SUCCESS') job.error = `agy result status: ${r.status}`;
137
+ return;
138
+ }
139
+ if (ev === 'error') {
140
+ job.error = msg.error ?? JSON.stringify(msg);
141
+ return;
142
+ }
143
+ }
144
+
145
+ function handleGrokLine(job, line, msg) {
146
+ if (msg.type === 'system' && msg.subtype === 'init') {
147
+ job.permissionMode = msg.permissionMode ?? null;
148
+ job.modelUsed = msg.model ?? null;
149
+ job.toolCount = msg.tools?.length ?? 0;
150
+ return;
151
+ }
152
+ if (msg.type === 'stream_event') {
153
+ const e = msg.event ?? {};
154
+ switch (e.type) {
155
+ case 'message_start': job._msgText = ''; break;
156
+ case 'content_block_delta': {
157
+ const d = e.delta ?? {};
158
+ if (d.type === 'text_delta') job._msgText = (job._msgText ?? '') + d.text;
159
+ break;
160
+ }
161
+ case 'message_delta': {
162
+ if (e.usage) job._msgUsage = e.usage; // per-message; replaced by totals on result
163
+ if (e.delta?.stop_reason) job._stopReason = e.delta.stop_reason;
164
+ break;
165
+ }
166
+ }
167
+ return;
168
+ }
169
+ if (msg.type === 'assistant' && msg.message) {
170
+ const m = msg.message;
171
+ const text = (m.content ?? [])
172
+ .filter((c) => c?.type === 'text').map((c) => c.text).join('');
173
+ if (text) job._msgText = text; // last whole message = the final answer
174
+ if (m.usage) job._msgUsage = m.usage;
175
+ if (m.stop_reason) job._stopReason = m.stop_reason;
176
+ return;
177
+ }
178
+ if (msg.type === 'result') {
179
+ job._grokIsError = !!msg.is_error;
180
+ if (typeof msg.result === 'string') job.result = msg.result;
181
+ if (msg.usage) setTokensFromUsage(job, msg.usage); // totals win over message sums
182
+ if (msg.is_error) {
183
+ job.error = String(msg.errors?.[0] ?? `grok result error (${msg.subtype})`).slice(0, 400);
184
+ }
185
+ return;
186
+ }
187
+ if (msg.type === 'error') {
188
+ job.error = msg.error?.message ?? JSON.stringify(msg.error);
189
+ return;
190
+ }
191
+ }
192
+
193
+ function touchActivity(job) {
194
+ const now = Date.now();
195
+ job.activity.events += 1;
196
+ job.activity.lastEventAt = new Date(now).toISOString();
197
+ job.liveness = 'active';
198
+ clearTimeout(job._stallTimer);
199
+ job._stallTimer = setTimeout(() => {
200
+ if (job.status !== 'running') return;
201
+ job.liveness = 'stalled';
202
+ publishStatus();
203
+ }, STALL_SECONDS * 1000);
204
+ job._stallTimer.unref?.();
205
+ if (now - (job._lastPublishAt ?? 0) >= PUBLISH_MIN_INTERVAL_MS) {
206
+ job._lastPublishAt = now;
207
+ publishStatus();
208
+ }
209
+ }
210
+
211
+ function settle(job, { status, error, stopReason }) {
212
+ if (job._settled) return;
213
+ job._settled = true;
214
+ // Release the cwd lock on EVERY terminal path: exit (done/failed), spawn
215
+ // error, timeout kill, cancel — all settle through here (WPC9).
216
+ job._releaseCwdLock?.();
217
+ job._releaseCwdLock = null;
218
+ clearTimeout(job._timer);
219
+ clearTimeout(job._stallTimer);
220
+ if (job.status === 'cancelled') {
221
+ job.stopReason ??= 'cancelled';
222
+ } else {
223
+ job.status = status;
224
+ if (error !== undefined) job.error = job.error ?? error;
225
+ if (stopReason !== undefined) job.stopReason = job.stopReason ?? stopReason;
226
+ }
227
+ job.endedAt = new Date().toISOString();
228
+ publishStatus();
229
+ for (const w of job.waiters.splice(0)) w();
230
+ }
231
+
232
+ export async function startCliJob({ worker, task, cwd, timeoutMs = DEFAULT_TIMEOUT_MS, source = 'api', effort, origin = null, allowConcurrentCwd = false }) {
233
+ const profile = typeof worker === 'string' ? resolveProfile(worker) : worker;
234
+ const workspace = resolve(cwd ?? process.cwd());
235
+ const args = buildArgs(profile, task, timeoutMs, effort);
236
+ const id = `cli-${nextCliId++}-${Date.now().toString(36)}`;
237
+ const startedAt = new Date().toISOString();
238
+
239
+ // Cwd advisory lock BEFORE the child spawns: a conflicting dispatch is
240
+ // refused with the holder info (WPC9). The lock is released by settle(),
241
+ // which every terminal path passes through.
242
+ let releaseLock = null;
243
+ if (!allowConcurrentCwd) {
244
+ releaseLock = acquireCwdLock({ cwd: workspace, jobId: id, backend: profile.backend, mode: 'cli', startedAt }).release;
245
+ }
246
+
247
+ const job = {
248
+ id,
249
+ backend: profile.backend,
250
+ profile: profile.name,
251
+ mode: 'cli',
252
+ tier: profile.name, // display: profile name rides the tier column
253
+ model: profile.model ?? '(cli default)',
254
+ effort: (effort && profile.effortMap?.[effort]) ?? profile.effort ?? null,
255
+ task, source, cwd: workspace,
256
+ status: 'running', turn: 0, step: 0, currentTool: null, toolCalls: 0,
257
+ tokens: { input: 0, output: 0, reasoning: 0 },
258
+ startedAt, endedAt: null,
259
+ origin_depth: origin?.depth ?? null, origin_chain: origin?.chain ?? null,
260
+ result: null, error: null, stopReason: null,
261
+ activity: { events: 0, lastEventAt: null },
262
+ liveness: 'starting',
263
+ waiters: [],
264
+ permissionMode: null, modelUsed: null, toolCount: null,
265
+ _text: '', _msgText: '', _msgUsage: null, _stopReason: null,
266
+ _agyStatus: null, _grokIsError: false,
267
+ _rawLines: [], _stderrTail: '',
268
+ _timer: null, _stallTimer: null, _lastPublishAt: 0, _settled: false,
269
+ _releaseCwdLock: releaseLock,
270
+ };
271
+ try {
272
+ registerJob(job);
273
+ } catch (err) {
274
+ releaseLock?.();
275
+ throw err;
276
+ }
277
+
278
+ let child;
279
+ try {
280
+ child = spawn(profile.cli, args, {
281
+ cwd: workspace,
282
+ // Origin guard: the CLI worker inherits the dispatch chain, so a crew
283
+ // instance it starts itself continues the chain (WPC9).
284
+ env: {
285
+ ...process.env,
286
+ ...(origin ? { [ORIGIN_CHAIN_ENV]: JSON.stringify(origin.chain), [ORIGIN_DEPTH_ENV]: String(origin.depth) } : {}),
287
+ },
288
+ detached: true,
289
+ stdio: ['ignore', 'pipe', 'pipe'],
290
+ });
291
+ } catch (err) {
292
+ // spawn() throws synchronously only for malformed options; settle the job
293
+ // so it does not linger as 'running' with no process behind it.
294
+ settle(job, {
295
+ status: 'failed',
296
+ error: `could not spawn "${profile.cli}": ${err?.message ?? err}`,
297
+ stopReason: 'spawn-failed',
298
+ });
299
+ return job;
300
+ }
301
+ job.pid = child.pid;
302
+ // Positive process-group id (detached spawn ⇒ pgid === pid); kill(-pgid)
303
+ // signals the whole group. WPC14: the panel/proc-kill endpoint reads this
304
+ // from job views and shard mirrors to offer killing an orphaned worker.
305
+ job.pgid = child.pid;
306
+ // cancelJob() in jobs.mjs calls job.harness.close() — same interface as the
307
+ // standalone DSH harness.
308
+ job.harness = { child, close: async () => killGroup(child) };
309
+
310
+ job._timer = setTimeout(() => {
311
+ if (job.status !== 'running') return;
312
+ // Mark failed immediately so dsh_worker_status shows the timeout during
313
+ // the 0-2 s kill window; settle() finalizes endedAt/waiters on exit.
314
+ job.status = 'failed';
315
+ job.stopReason = 'timeout';
316
+ job.error = `timed out after ${Math.round(timeoutMs / 1000)}s (CLI process group killed)`;
317
+ job.liveness = 'stalled';
318
+ publishStatus();
319
+ killGroup(child);
320
+ job._timedOut = true;
321
+ }, timeoutMs);
322
+
323
+ const handleLine = makeLineHandler(profile, job);
324
+ let buf = '';
325
+ child.stdout.on('data', (chunk) => {
326
+ buf += chunk.toString();
327
+ let i;
328
+ while ((i = buf.indexOf('\n')) >= 0) {
329
+ const line = buf.slice(0, i).trim();
330
+ buf = buf.slice(i + 1);
331
+ if (!line) continue;
332
+ job._rawLines.push(line.slice(0, 500));
333
+ if (job._rawLines.length > RAW_LINE_CAP) job._rawLines.splice(0, job._rawLines.length - RAW_LINE_CAP);
334
+ touchActivity(job);
335
+ // Once failed/cancelled/timed-out, ignore what the dying process still
336
+ // writes: its final error events must not overwrite the timeout/cancel
337
+ // error we already recorded.
338
+ if (job.status !== 'running') continue;
339
+ try {
340
+ handleLine(job, line, JSON.parse(line));
341
+ } catch {
342
+ // Non-JSON stdout line (banner, stray output): keep as evidence, no parse.
343
+ }
344
+ }
345
+ });
346
+ child.stderr.on('data', (chunk) => {
347
+ job._stderrTail = (job._stderrTail + chunk.toString()).slice(-STDERR_TAIL_BYTES);
348
+ });
349
+
350
+ child.on('error', (err) => {
351
+ settle(job, {
352
+ status: 'failed',
353
+ error: `could not spawn "${profile.cli}": ${err?.message ?? err} — is it installed and on PATH?`,
354
+ stopReason: 'spawn-failed',
355
+ });
356
+ });
357
+
358
+ child.on('exit', (code, signal) => {
359
+ const timedOut = job._timedOut === true;
360
+ if (job.status === 'running') {
361
+ if (timedOut) {
362
+ settle(job, { status: 'failed', stopReason: 'timeout' });
363
+ } else if (job._agyStatus && job._agyStatus !== 'SUCCESS') {
364
+ settle(job, { status: 'failed', stopReason: job._agyStatus });
365
+ } else if (job._grokIsError) {
366
+ settle(job, { status: 'failed', stopReason: 'error' });
367
+ } else if (job.result != null && code === 0) {
368
+ job.stopReason = job._stopReason ?? 'completed';
369
+ settle(job, { status: 'done', stopReason: job.stopReason });
370
+ } else if (job.result != null && code !== 0) {
371
+ settle(job, {
372
+ status: 'failed',
373
+ stopReason: job._stopReason ?? `exit code ${code}`,
374
+ error: job._stderrTail.trim().slice(-400) || `CLI exited with code ${code}`,
375
+ });
376
+ } else if (code === 0) {
377
+ // Stream ended without a result event: fall back to accumulated text
378
+ // and the last per-message usage we saw.
379
+ job.result = job._text ?? job._msgText ?? '';
380
+ if (job._msgUsage) addTokensFromUsage(job, job._msgUsage);
381
+ job.stopReason = job._stopReason ?? 'completed';
382
+ settle(job, { status: 'done', stopReason: job.stopReason });
383
+ } else {
384
+ settle(job, {
385
+ status: 'failed',
386
+ stopReason: `exit code ${code}${signal ? ` (signal ${signal})` : ''}`,
387
+ error: job._stderrTail.trim().slice(-400) || `CLI exited with code ${code}`,
388
+ });
389
+ }
390
+ } else {
391
+ settle(job, { status: job.status }); // cancelled path: keep the cancellation
392
+ }
393
+ });
394
+
395
+ return job;
396
+ }