@ran-sh/dsh-crew 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/.claude-plugin/marketplace.json +16 -16
  2. package/.claude-plugin/plugin.json +14 -14
  3. package/LICENSE +21 -21
  4. package/README.de.md +359 -359
  5. package/README.es.md +359 -359
  6. package/README.fr.md +359 -359
  7. package/README.hi.md +359 -359
  8. package/README.id.md +359 -359
  9. package/README.ja.md +359 -359
  10. package/README.ko.md +359 -359
  11. package/README.md +126 -126
  12. package/README.pt.md +359 -359
  13. package/README.ru.md +359 -359
  14. package/README.th.md +359 -359
  15. package/README.tr.md +359 -359
  16. package/README.vi.md +359 -359
  17. package/README.zh-TW.md +359 -359
  18. package/README.zh.md +116 -116
  19. package/agents/ds-flash.md +24 -24
  20. package/agents/ds-pro.md +24 -24
  21. package/agents/ds-reviewer.md +23 -23
  22. package/agents/ds-worker.md +23 -23
  23. package/bin/dsh-crew.mjs +16 -16
  24. package/codex/agents/ds-flash.toml +34 -34
  25. package/codex/agents/ds-pro.toml +34 -34
  26. package/codex/agents/ds-reviewer.toml +32 -32
  27. package/codex/agents/ds-worker.toml +32 -32
  28. package/codex/prompts/dsh-config.md +19 -19
  29. package/codex/prompts/dsh-status.md +5 -5
  30. package/commands/config.md +23 -23
  31. package/commands/off.md +5 -5
  32. package/commands/on.md +5 -5
  33. package/commands/status.md +9 -9
  34. package/cordis.patch.yml +4 -4
  35. package/docs/gpt-relay-extension.md +103 -103
  36. package/docs/installation.md +138 -138
  37. package/docs/job-contracts.md +103 -103
  38. package/docs/readiness-matrix.md +85 -85
  39. package/docs/ui-surfaces.md +107 -107
  40. package/official-web-bridge/cordis.patch.yml +4 -4
  41. package/official-web-bridge/entry.mjs +1 -1
  42. package/official-web-bridge/overlay-entry.mjs +59 -59
  43. package/official-web-bridge/package.json +25 -25
  44. package/package.json +3 -2
  45. package/scripts/build-client.mjs +49 -49
  46. package/scripts/live-crew-smoke.mjs +39 -39
  47. package/scripts/live-policy-matrix.mjs +177 -177
  48. package/scripts/policy-probe.mjs +101 -101
  49. package/scripts/remove-legacy-official-bridge.ps1 +89 -89
  50. package/scripts/setup.mjs +393 -393
  51. package/scripts/smoke-real.mjs +110 -110
  52. package/scripts/smoke.mjs +78 -78
  53. package/scripts/verify-crew-ui-polish.mjs +145 -145
  54. package/scripts/verify-history-ui.mjs +97 -97
  55. package/scripts/verify-installer-fix.mjs +26 -26
  56. package/scripts/verify-npm-install.mjs +311 -311
  57. package/scripts/verify-official-bridge-e2e.mjs +192 -192
  58. package/src/adaptive-routing.mjs +260 -260
  59. package/src/client/activation-summary.tsx +64 -64
  60. package/src/client/collapsible-sections.mjs +55 -55
  61. package/src/client/history-panel.tsx +108 -108
  62. package/src/client/host-readiness.mjs +71 -71
  63. package/src/client/index.tsx +1711 -1711
  64. package/src/client/model-callability-view.mjs +17 -17
  65. package/src/client/panel-chrome.tsx +40 -40
  66. package/src/client/quick-entry.tsx +10 -10
  67. package/src/client/quick-panel.tsx +275 -275
  68. package/src/client/readiness-envelope.mjs +34 -34
  69. package/src/client/surface-detection.mjs +43 -43
  70. package/src/config-readiness.mjs +226 -226
  71. package/src/credential-reference.mjs +38 -38
  72. package/src/delivery.mjs +205 -205
  73. package/src/dsh-cli-runtime.mjs +1021 -1021
  74. package/src/dsh-cohort.mjs +20 -20
  75. package/src/extension-contract.mjs +104 -104
  76. package/src/failure-classification.mjs +201 -201
  77. package/src/history/admission-gate.mjs +67 -67
  78. package/src/history/archive-store.mjs +272 -272
  79. package/src/history/cleanup-plan.mjs +89 -89
  80. package/src/history/http.mjs +32 -32
  81. package/src/history/operation.mjs +86 -86
  82. package/src/history/runner-detach.mjs +34 -34
  83. package/src/history/runner.mjs +52 -52
  84. package/src/history/runtime.mjs +36 -36
  85. package/src/history/service.mjs +177 -177
  86. package/src/history/state.mjs +27 -27
  87. package/src/hub/entry.mjs +104 -104
  88. package/src/hub/index.mjs +2694 -2694
  89. package/src/hub-client.mjs +154 -154
  90. package/src/hub-compatibility.mjs +42 -42
  91. package/src/i18n.mjs +19 -19
  92. package/src/information-flow.mjs +67 -67
  93. package/src/install/cli.mjs +28 -28
  94. package/src/install/install-legacy.mjs +711 -711
  95. package/src/install/install.mjs +483 -483
  96. package/src/install/npx-lifecycle.mjs +3456 -3456
  97. package/src/install/official-frontend-assets.mjs +78 -78
  98. package/src/install/official-web.mjs +95 -95
  99. package/src/install/payload-content.mjs +88 -88
  100. package/src/install/windows-startup.mjs +236 -236
  101. package/src/install/windows-supervisor-adapter.mjs +443 -443
  102. package/src/install/windows-supervisor-lifecycle.mjs +782 -782
  103. package/src/install/zcode.mjs +397 -397
  104. package/src/job-contracts.mjs +255 -255
  105. package/src/local-request-guard.mjs +60 -60
  106. package/src/mcp-runtime.mjs +340 -340
  107. package/src/model-callability-contract.mjs +79 -79
  108. package/src/model-catalog.mjs +180 -180
  109. package/src/model-routing.mjs +586 -586
  110. package/src/model-schedule.mjs +207 -207
  111. package/src/official-web-bridge.mjs +447 -447
  112. package/src/policy.mjs +235 -235
  113. package/src/provider-delete-adapters.mjs +1934 -1934
  114. package/src/provider-health.mjs +130 -130
  115. package/src/provider-inventory.mjs +182 -182
  116. package/src/provider-layer-migration-adapters.mjs +759 -759
  117. package/src/provider-layer-migration.mjs +198 -198
  118. package/src/provider-lifecycle-state.mjs +103 -103
  119. package/src/provider-lifecycle.mjs +252 -252
  120. package/src/provider-profile-store.mjs +390 -390
  121. package/src/provider-settings-store.mjs +633 -633
  122. package/src/provider-store-lock.mjs +67 -67
  123. package/src/readiness-matrix.mjs +181 -181
  124. package/src/removable-waiter.mjs +29 -29
  125. package/src/role-profiles.mjs +107 -107
  126. package/src/runtime-controls.mjs +84 -84
  127. package/src/runtime-identity-contract.mjs +34 -34
  128. package/src/runtime-identity.mjs +235 -235
  129. package/src/runtime-readiness-snapshot.mjs +285 -285
  130. package/src/server.mjs +581 -581
  131. package/src/session-origins.mjs +60 -60
  132. package/src/standalone-sdk.mjs +23 -23
  133. package/src/status-shard.mjs +63 -63
  134. package/src/structured-error-code.mjs +38 -38
  135. package/src/supervisor/restart-request.mjs +256 -256
  136. package/src/workflow-runtime.mjs +739 -739
  137. package/src/workflow.mjs +155 -155
  138. package/src/workspace-audit.mjs +231 -231
  139. package/src/workspace-context.mjs +146 -146
  140. package/src/workspace-isolation.mjs +463 -455
  141. package/src/workspace-readiness.mjs +32 -32
  142. package/statusline/statusline.sh +14 -14
  143. package/statusline/worker-segment.sh +35 -35
  144. package/windows/start-dsh-crew.cmd +57 -57
  145. package/windows/start-dsh-crew.ps1 +1302 -1302
  146. package/windows/supervisor-control.ps1 +467 -467
  147. package/worker.cordis.yml +67 -67
  148. package/zcode/agents/ds-reviewer.md +31 -31
  149. package/zcode/agents/ds-worker.md +31 -31
  150. package/zcode/commands/dsh-config.md +17 -17
  151. package/zcode/commands/dsh-status.md +5 -5
@@ -1,739 +1,739 @@
1
- // Workflow runtime: the single business flow for every worker dispatch, shared
2
- // by blocking (dsh_run_worker) and async (dsh_spawn_worker) and by Hub and
3
- // Standalone execution — the only difference between those transports is which
4
- // adapter executes an attempt and whether the caller awaits.
5
- //
6
- // A Workflow Job is one logical delegation; an Attempt is one actual DSH
7
- // worker/reviewer session within it. The runtime owns the state machine
8
- // (phase transitions guarded by workflow.canTransition), the attempt loop
9
- // (verify -> escalate on evidence -> reviewer pass -> finalize), candidate
10
- // capture, review mutation detection, concurrency limiting and cancellation.
11
- //
12
- // All I/O is injected through `adapters`, so this module stays testable without
13
- // DSH, a hub, git or a real filesystem. Policy decisions import the pure
14
- // modules (policy.mjs / workflow.mjs) directly; Hub/Standalone specifics never
15
- // appear here.
16
-
17
- import { resolveModelPolicy, shouldAutoReview, getRoleState } from './policy.mjs';
18
- import { raceWaiters } from './removable-waiter.mjs';
19
- import { applyWorkspaceEvidence, buildOutcome, decideNextStep, JOB_PHASES, canTransition } from './workflow.mjs';
20
- import { parseDeliveryReport } from './delivery.mjs';
21
- import { classifyFailure } from './failure-classification.mjs';
22
- import { createCanonicalJobEvent } from './job-contracts.mjs';
23
-
24
- export const WORKFLOW_ERROR_CODES = {
25
- ISOLATION_UNAVAILABLE: 'ISOLATION_UNAVAILABLE',
26
- NOT_GIT_REPOSITORY: 'NOT_GIT_REPOSITORY',
27
- WORKTREE_CREATE_FAILED: 'WORKTREE_CREATE_FAILED',
28
- CANDIDATE_CAPTURE_FAILED: 'CANDIDATE_CAPTURE_FAILED',
29
- WORKFLOW_CANCELLED: 'WORKFLOW_CANCELLED',
30
- ATTEMPT_INFRA_FAILURE: 'ATTEMPT_INFRA_FAILURE',
31
- REVIEWER_MUTATED_CANDIDATE: 'REVIEWER_MUTATED_CANDIDATE',
32
- };
33
-
34
- const TERMINAL = new Set([JOB_PHASES.COMPLETED, JOB_PHASES.FAILED, JOB_PHASES.CANCELLED, JOB_PHASES.INTERRUPTED]);
35
-
36
- function lines(value) {
37
- if (typeof value !== 'string' || value.trim() === '') return [];
38
- return value.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
39
- }
40
-
41
- export function normalizeReviewVerdict(value) {
42
- const s = String(value ?? '').trim().toLowerCase();
43
- if (/^(approve|approved|pass)\b/.test(s) || s === 'pass' || s === 'approve') return 'approve';
44
- if (/request.*chang|chang.*request|reject|needs changes/i.test(s)) return 'request_changes';
45
- return 'inconclusive';
46
- }
47
-
48
- export function normalizeReview({ attemptResult, beforeCandidate, afterCandidate } = {}) {
49
- const parsed = parseDeliveryReport(attemptResult?.result ?? '');
50
- const mutation = mutationDetected(beforeCandidate, afterCandidate);
51
- const reportedVerdict = normalizeReviewVerdict(parsed.sections?.Verdict ?? attemptResult?.result ?? '');
52
- return {
53
- verdict: mutation ? 'request_changes' : reportedVerdict,
54
- reported_verdict: reportedVerdict,
55
- findings: lines(parsed.sections?.['Review Findings']),
56
- evidence: lines(parsed.sections?.Evidence),
57
- risks: lines(parsed.sections?.Risks),
58
- delivery_complete: parsed.complete ?? false,
59
- model: attemptResult?.model ?? null,
60
- provider: attemptResult?.provider ?? null,
61
- selection_trace: attemptResult?.selection_trace ?? null,
62
- status: attemptResult?.status ?? 'failed',
63
- ...(mutation ? { mutated_candidate: true, invalidated: true } : {}),
64
- };
65
- }
66
-
67
- function mutationDetected(before, after) {
68
- if (!before) return false;
69
- if (before.fingerprint == null) return false;
70
- if (!after) return true;
71
- return after.fingerprint != null && before.fingerprint !== after.fingerprint;
72
- }
73
-
74
- function sumUsage(attempts) {
75
- const tokens = { input: 0, output: 0, reasoning: 0 };
76
- for (const a of attempts) {
77
- const u = a.usage;
78
- if (!u) continue;
79
- tokens.input += u.input ?? 0;
80
- tokens.output += u.output ?? 0;
81
- tokens.reasoning += u.reasoning ?? 0;
82
- }
83
- return tokens;
84
- }
85
-
86
- export function normalizeMaxParallel(value, fallback = 3) {
87
- const fallbackValue = Number.isInteger(Number(fallback))
88
- ? Math.max(1, Math.min(16, Number(fallback)))
89
- : 3;
90
- const parsed = Number(value);
91
- if (!Number.isInteger(parsed)) return fallbackValue;
92
- return Math.max(1, Math.min(16, parsed));
93
- }
94
-
95
- export function createWorkflowRuntime(adapters, {
96
- maxParallel = 3,
97
- idFactory = () => `wf-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`,
98
- clock = () => Date.now(),
99
- logger = null,
100
- } = {}) {
101
- const jobs = new Map();
102
- const queue = [];
103
- let active = 0;
104
- let maxParallelLimit = normalizeMaxParallel(maxParallel);
105
- let nestedIdFactory = null;
106
-
107
- function nextWorkflowId() {
108
- if (nestedIdFactory) return nestedIdFactory();
109
- const generated = idFactory();
110
- // A few embedders provide a factory-builder for deterministic tests.
111
- // Normalize that legacy shape once while keeping the public id a string.
112
- if (typeof generated === 'function') {
113
- nestedIdFactory = generated;
114
- return nestedIdFactory();
115
- }
116
- return generated;
117
- }
118
-
119
- function log(msg) { logger?.debug?.(`workflow: ${msg}`); }
120
-
121
- function runtimeState() {
122
- return {
123
- max_parallel: maxParallelLimit,
124
- active,
125
- queued: queue.length,
126
- };
127
- }
128
-
129
- function syncLiveMaxParallel() {
130
- if (typeof adapters.getRuntimeControls !== 'function') return false;
131
- const controls = adapters.getRuntimeControls() ?? {};
132
- if (controls.max_parallel === undefined) return false;
133
- const next = normalizeMaxParallel(controls.max_parallel, maxParallelLimit);
134
- if (next === maxParallelLimit) return false;
135
- maxParallelLimit = next;
136
- return true;
137
- }
138
-
139
- function transition(job, to, reason) {
140
- if (job.phase === to) return;
141
- if (!canTransition(job.phase, to)) throw new Error(`illegal workflow transition ${job.phase} -> ${to}`);
142
- job.phase = to;
143
- job.events.push({ at: clock(), phase: to, type: 'phase', reason: reason ?? to });
144
- log(`${job.id} ${to}`);
145
- }
146
-
147
- function recordCanonical(job, type, data = {}, attempt = null, at = clock()) {
148
- job.canonical_event_sequence += 1;
149
- job.canonical_events.push(createCanonicalJobEvent({
150
- jobId: job.id,
151
- type,
152
- sequence: job.canonical_event_sequence,
153
- at,
154
- role: job.role,
155
- attempt,
156
- data,
157
- }));
158
- }
159
-
160
- function createJob(spec) {
161
- const now = clock();
162
- const job = {
163
- id: nextWorkflowId(),
164
- client_job_id: spec.client_job_id ?? null,
165
- role: spec.role ?? 'worker',
166
- delivery: spec.delivery === 'review' ? 'review' : 'coding',
167
- model_class_hint: spec.model_class_hint === 'pro' ? 'pro' : spec.model_class_hint === 'flash' ? 'flash' : null,
168
- original_task: spec.task,
169
- source: spec.source ?? 'api',
170
- requested_cwd: spec.cwd,
171
- requested_isolation: spec.requested_isolation ?? null,
172
- workspace_branch: spec.workspace_branch ?? null,
173
- timeout_seconds: spec.timeout_seconds ?? null,
174
- profile_id: spec.profile_id ?? null,
175
- allow_fallback: spec.allow_fallback !== false,
176
- allow_no_changes: spec.allow_no_changes === true,
177
- routing: spec.routing ?? 'auto',
178
- review_strictness: spec.review_strictness ?? null,
179
- workspace_context: spec.workspace_context ? { ...spec.workspace_context } : null,
180
- effort: spec.effort ?? 'max',
181
- phase: JOB_PHASES.CREATED,
182
- status: 'running',
183
- cancelling: false,
184
- execution_cwd: spec.cwd,
185
- isolation: 'shared',
186
- base_revision: null,
187
- primary_workspace_dirty: false,
188
- attempts: [],
189
- current_attempt_id: null,
190
- candidate: null,
191
- decision: null,
192
- review: null,
193
- outcome: null,
194
- error: null,
195
- cleanup_warning: null,
196
- candidate_capture_failed: false,
197
- retain_workspace: false,
198
- workspace_retained: false,
199
- events: [{ at: now, phase: JOB_PHASES.CREATED, type: 'created' }],
200
- canonical_events: [],
201
- canonical_event_sequence: 0,
202
- createdAt: now,
203
- startedAt: now,
204
- endedAt: null,
205
- workspaceHandle: null,
206
- workspaceReleasePromise: null,
207
- cancelPromise: null,
208
- waiters: [],
209
- };
210
- recordCanonical(job, 'job.created', { client_job_id: job.client_job_id }, null, now);
211
- return job;
212
- }
213
-
214
- function releaseSlot() {
215
- active = Math.max(0, active - 1);
216
- drain();
217
- }
218
-
219
- function handleKickoffFailure(job, err) {
220
- failJob(job, err);
221
- releaseSlot();
222
- }
223
-
224
- function drain({ sync = true } = {}) {
225
- if (sync) syncLiveMaxParallel();
226
- while (active < maxParallelLimit && queue.length > 0) {
227
- const job = queue.shift();
228
- active += 1;
229
- kickoff(job).catch((err) => handleKickoffFailure(job, err));
230
- }
231
- }
232
-
233
- function setMaxParallel(value) {
234
- maxParallelLimit = normalizeMaxParallel(value, maxParallelLimit);
235
- // Raising the limit admits queued work immediately. Lowering it never
236
- // cancels already-running work: drain simply waits until active drops below
237
- // the new limit.
238
- drain({ sync: false });
239
- return runtimeState();
240
- }
241
-
242
- function refreshRuntimeControls() {
243
- syncLiveMaxParallel();
244
- drain({ sync: false });
245
- return runtimeState();
246
- }
247
-
248
- function setTerminal(job) {
249
- if (job.status !== 'running') return;
250
- if (job.phase === JOB_PHASES.COMPLETED) job.status = 'done';
251
- else if (job.phase === JOB_PHASES.CANCELLED) job.status = 'cancelled';
252
- else job.status = 'failed';
253
- job.endedAt = clock();
254
- job.current_attempt_id = null;
255
- for (const w of job.waiters.splice(0)) w();
256
- }
257
-
258
- function failJob(job, err) {
259
- if (job.status !== 'running') return;
260
- job.error = err?.message ?? String(err);
261
- job.error_code = err?.code ?? err?.policyCode ?? job.error_code ?? null;
262
- job.phase = JOB_PHASES.FAILED;
263
- recordCanonical(job, 'job.failed', {
264
- error_code: job.error_code,
265
- });
266
- setTerminal(job);
267
- }
268
-
269
- async function releaseWorkspace(job) {
270
- if (job.retain_workspace) {
271
- job.workspace_retained = true;
272
- return;
273
- }
274
- if (!job.workspaceHandle) return;
275
- if (job.workspaceReleasePromise) return job.workspaceReleasePromise;
276
- const handle = job.workspaceHandle;
277
- job.workspaceReleasePromise = (async () => {
278
- try {
279
- const r = await adapters.releaseWorkspace(handle);
280
- if (!r || r.ok !== true) {
281
- // A failed cleanup leaves the isolated workspace in place; say so
282
- // truthfully instead of claiming a clean release.
283
- job.cleanup_warning = r?.error ?? 'worktree cleanup failed';
284
- job.workspace_retained = true;
285
- }
286
- } catch (err) {
287
- job.cleanup_warning = err?.message ?? String(err);
288
- job.workspace_retained = true;
289
- } finally {
290
- job.workspaceHandle = null;
291
- job.workspaceReleasePromise = null;
292
- }
293
- })();
294
- return job.workspaceReleasePromise;
295
- }
296
-
297
- async function runWorkflow(job) {
298
- const isReviewJob = job.role === 'reviewer' || job.delivery === 'review';
299
- let config = {};
300
-
301
- try {
302
- config = adapters.getConfig?.() ?? {};
303
- job.review_gate = ['required', 'optional', 'off'].includes(config?.review?.gate) ? config.review.gate : 'required';
304
- const alloc = await adapters.allocateWorkspace?.(job);
305
- if (alloc && alloc.ok === false) {
306
- failJob(job, Object.assign(new Error(alloc.error ?? alloc.reason), { code: alloc.reason }));
307
- return;
308
- }
309
- if (alloc && alloc.ok) {
310
- job.workspaceHandle = alloc.handle ?? null;
311
- job.execution_cwd = alloc.execution_cwd ?? job.requested_cwd;
312
- job.isolation = alloc.isolation ?? 'worktree';
313
- job.base_revision = alloc.base_revision ?? null;
314
- job.primary_workspace_dirty = alloc.primary_workspace_dirty === true;
315
- }
316
- recordCanonical(job, 'job.started', {
317
- isolation: job.isolation,
318
- base_revision: job.base_revision,
319
- });
320
- transition(job, JOB_PHASES.RUNNING, 'start');
321
-
322
- if (isReviewJob) {
323
- transition(job, JOB_PHASES.REVIEWING, 'explicit reviewer');
324
- const before = alloc?.ok && alloc.isolation === 'worktree' ? await safeCapture(adapters, job.execution_cwd, job.base_revision) : null;
325
- // Explicit reviewers inspect the caller's stated task/workspace
326
- // directly. The bounded worker-candidate capsule belongs only to the
327
- // automatic post-worker review path; constructing it with no worker
328
- // view falsely tells an explicit reviewer that nothing was delivered.
329
- const reviewTask = job.original_task;
330
- const review = await runReviewerAttempt(job, reviewTask, config, before);
331
- if (job.cancelling) { await cancelWorkflow(job); return; }
332
- job.review = review;
333
- const reviewApproved = review.status === 'done'
334
- && review.delivery_complete === true
335
- && review.verdict === 'approve';
336
- if (!reviewApproved) {
337
- const code = review.verdict === 'request_changes'
338
- ? 'REVIEW_CHANGES_REQUESTED'
339
- : 'REVIEW_INCONCLUSIVE';
340
- failJob(job, Object.assign(
341
- new Error(`explicit review blocked acceptance: verdict=${review.verdict}, reviewer status=${review.status}, review delivery_complete=${review.delivery_complete === true}`),
342
- { code },
343
- ));
344
- return;
345
- }
346
- if (job.review) transition(job, JOB_PHASES.READY, 'review complete');
347
- finalize(job);
348
- return;
349
- }
350
-
351
- let attempt = 0;
352
- let escalationReason = null;
353
- for (;;) {
354
- if (job.cancelling) { await cancelWorkflow(job); return; }
355
- if (attempt > 0) transition(job, JOB_PHASES.RUNNING, `escalated attempt ${attempt}`);
356
- const resolvedPolicy = resolveModelPolicy(config, 'worker', { attempt });
357
- const fallbackPolicy = job.allow_fallback === false
358
- ? { ...resolvedPolicy, escalation: { ...resolvedPolicy.escalation, enabled: false } }
359
- : resolvedPolicy;
360
- const policy = job.routing === 'stable'
361
- ? { ...fallbackPolicy, adaptive: { ...fallbackPolicy.adaptive, enabled: false } }
362
- : fallbackPolicy;
363
- const attemptId = attemptIdFor(adapters, job.id, attempt === 0 ? '' : String(attempt));
364
- job.current_attempt_id = attemptId;
365
- job.events.push({ at: clock(), phase: job.phase, type: 'attempt/start', attempt, escalation_reason: escalationReason });
366
- recordCanonical(job, 'worker.started', {
367
- attempt_id: attemptId,
368
- escalation_reason: escalationReason,
369
- }, attempt);
370
- const ar = await adapters.executeAttempt({
371
- id: attemptId,
372
- workflowId: job.id,
373
- role: 'worker',
374
- attempt,
375
- task: job.original_task,
376
- cwd: job.execution_cwd,
377
- effort: job.effort,
378
- timeout_seconds: job.timeout_seconds,
379
- policy,
380
- source: job.source,
381
- model_class_hint: job.model_class_hint,
382
- escalation_reason: escalationReason,
383
- onAttemptStarted: (actualId) => {
384
- if (typeof actualId === 'string' && actualId) job.current_attempt_id = actualId;
385
- },
386
- });
387
- job.current_attempt_id = null;
388
- if (job.cancelling) { await cancelWorkflow(job); return; }
389
- const attemptView = attemptRecord(ar, attempt);
390
- job.attempts.push(attemptView);
391
- recordCanonical(job, 'model.selected', {
392
- provider: attemptView.provider,
393
- model: attemptView.model,
394
- source: attemptView.selection_source,
395
- fallback_reason: attemptView.selection_trace?.fallback_reason ?? null,
396
- }, attempt);
397
- recordCanonical(job, 'worker.completed', {
398
- attempt_id: attemptView.id,
399
- status: attemptView.status,
400
- stop_reason: attemptView.stopReason,
401
- error_code: attemptView.error_code,
402
- }, attempt);
403
- if (ar.infra === true) {
404
- failJob(job, Object.assign(new Error(ar.error ?? 'infrastructure failure'), { code: WORKFLOW_ERROR_CODES.ATTEMPT_INFRA_FAILURE }));
405
- return;
406
- }
407
- let outcome = ar.outcome ?? buildOutcome({
408
- result: ar.result ?? '',
409
- stopReason: ar.stopReason,
410
- executionStatus: ar.status === 'done' ? 'completed' : 'failed',
411
- });
412
- transition(job, JOB_PHASES.VERIFYING, `attempt ${attempt} complete`);
413
-
414
- let workspaceEvidenceAvailable = false;
415
- let workspaceHasChanges = false;
416
-
417
- // Capture the latest candidate after every attempt. Capture failure is
418
- // not allowed to delete the only recoverable state: retain the worktree
419
- // and preserve the worker business result, while surfacing the warning.
420
- if (alloc?.ok && alloc.isolation === 'worktree') {
421
- const candidate = await safeCapture(adapters, job.execution_cwd, job.base_revision);
422
- if (candidate === null) {
423
- job.candidate_capture_failed = true;
424
- job.retain_workspace = true;
425
- job.candidate = null;
426
- job.events.push({ at: clock(), phase: job.phase, type: 'candidate/failed', attempt, message: 'candidate capture failed; worktree retained for recovery' });
427
- } else {
428
- job.candidate = candidate;
429
- attemptView.candidate_fingerprint = candidate.fingerprint ?? null;
430
- workspaceEvidenceAvailable = true;
431
- workspaceHasChanges = Array.isArray(candidate.changed_files) && candidate.changed_files.length > 0;
432
- if (candidate.complete === false || candidate.replayable === false) {
433
- job.retain_workspace = true;
434
- job.events.push({ at: clock(), phase: job.phase, type: 'candidate/incomplete', attempt, message: 'candidate is not fully replayable; worktree retained' });
435
- }
436
- }
437
- }
438
- outcome = applyWorkspaceEvidence(outcome, {
439
- evidenceAvailable: workspaceEvidenceAvailable,
440
- hasChanges: workspaceHasChanges,
441
- allowNoChanges: job.allow_no_changes,
442
- });
443
- job.outcome = outcome;
444
-
445
- const reviewRequested = !isReviewJob && shouldAutoReview(config);
446
- const reviewerAuto = getRoleState(config, 'reviewer') !== 'disabled';
447
- const decision = decideNextStep({ outcome, policy, attempt, reviewRequested, reviewerAuto });
448
- job.decision = { step: decision.step, phase: decision.phase, reason: decision.reason };
449
- job.events.push({ at: clock(), phase: job.phase, type: 'attempt/complete', attempt, decision: decision.step });
450
-
451
- if (job.cancelling) { await cancelWorkflow(job); return; }
452
- if (decision.step === 'fail') {
453
- const finalAttemptCode = ar.status === 'failed' ? attemptView.error_code : null;
454
- failJob(job, Object.assign(new Error(`workflow failed: ${decision.reason}`), { code: finalAttemptCode }));
455
- return;
456
- }
457
- if (decision.step === 'escalate') {
458
- recordCanonical(job, 'model.fallback', {
459
- reason: decision.reason,
460
- from_attempt: attempt,
461
- to_attempt: attempt + 1,
462
- }, attempt);
463
- transition(job, JOB_PHASES.ESCALATING, decision.reason);
464
- escalationReason = decision.reason;
465
- attempt += 1;
466
- continue;
467
- }
468
- if (decision.step === 'review') {
469
- transition(job, JOB_PHASES.REVIEWING, 'automatic review');
470
- const before = job.candidate;
471
- const reviewTask = adapters.buildReviewTask(job.original_task, { outcome, candidate: job.candidate ?? null }, { strictness: job.review_strictness ?? 'standard' });
472
- const review = await runReviewerAttempt(job, reviewTask, config, before, job.execution_cwd, job.base_revision);
473
- if (job.cancelling) { await cancelWorkflow(job); return; }
474
- job.review = review;
475
- // The review verdict is an acceptance gate, not a footnote: only a
476
- // reviewer that ran to completion, delivered its own auditable
477
- // contract, and approved may finalize as COMPLETED. Anything else
478
- // fails closed through the existing FAILED machinery so the MCP
479
- // surface reports a failed workflow instead of a false "done".
480
- const reviewApproved = review.status === 'done'
481
- && review.delivery_complete === true
482
- && review.verdict === 'approve';
483
- if (!reviewApproved && job.review_gate === 'required') {
484
- const code = review.verdict === 'request_changes'
485
- ? 'REVIEW_CHANGES_REQUESTED'
486
- : 'REVIEW_INCONCLUSIVE';
487
- failJob(job, Object.assign(
488
- new Error(`automatic review blocked acceptance: verdict=${review.verdict}, reviewer status=${review.status}, review delivery_complete=${review.delivery_complete === true}`),
489
- { code },
490
- ));
491
- return;
492
- }
493
- transition(job, JOB_PHASES.READY, decision.reason);
494
- finalize(job);
495
- return;
496
- }
497
- transition(job, JOB_PHASES.READY, decision.reason);
498
- finalize(job);
499
- return;
500
- }
501
- } catch (err) {
502
- if (job.cancelling) { await cancelWorkflow(job); return; }
503
- failJob(job, err);
504
- } finally {
505
- await releaseWorkspace(job);
506
- releaseSlot();
507
- }
508
- }
509
-
510
- async function safeCapture(adapters, cwd, baseRevision) {
511
- if (typeof adapters.captureCandidate !== 'function') return null;
512
- try {
513
- const c = await adapters.captureCandidate({ cwd, baseRevision });
514
- return c && c.ok ? c : null;
515
- } catch { return null; }
516
- }
517
-
518
- async function runReviewerAttempt(job, task, config, beforeCandidate, cwd = job.execution_cwd, baseRevision = job.base_revision) {
519
- const requireFingerprint = (candidate) => {
520
- if (job.isolation !== 'worktree') return;
521
- if (candidate?.ok === true && typeof candidate.fingerprint === 'string' && candidate.fingerprint.trim()) return;
522
- job.candidate_capture_failed = true;
523
- job.retain_workspace = true;
524
- throw Object.assign(new Error('review workspace evidence unavailable; worktree retained'), {
525
- code: 'REVIEW_EVIDENCE_UNAVAILABLE',
526
- });
527
- };
528
- requireFingerprint(beforeCandidate);
529
- const policy = resolveModelPolicy(config, 'reviewer', { attempt: 0 });
530
- const attemptId = attemptIdFor(adapters, job.id, 'review');
531
- job.current_attempt_id = attemptId;
532
- job.events.push({ at: clock(), phase: job.phase, type: 'review/start' });
533
- recordCanonical(job, 'review.started', { attempt_id: attemptId }, 0);
534
- const ar = await adapters.executeAttempt({
535
- id: attemptId,
536
- workflowId: job.id,
537
- role: 'reviewer',
538
- attempt: 0,
539
- task,
540
- cwd,
541
- effort: job.effort,
542
- policy,
543
- source: job.source,
544
- model_class_hint: 'pro',
545
- escalation_reason: null,
546
- onAttemptStarted: (actualId) => {
547
- if (typeof actualId === 'string' && actualId) job.current_attempt_id = actualId;
548
- },
549
- });
550
- job.current_attempt_id = null;
551
- const attemptView = { ...attemptRecord(ar, 0), phase: 'review' };
552
- job.attempts.push(attemptView);
553
- recordCanonical(job, 'model.selected', {
554
- provider: attemptView.provider,
555
- model: attemptView.model,
556
- source: attemptView.selection_source,
557
- fallback_reason: attemptView.selection_trace?.fallback_reason ?? null,
558
- }, 0);
559
- if (ar.status === 'failed' && attemptView.error_code) job.error_code = attemptView.error_code;
560
- const afterCandidate = job.isolation === 'worktree' ? await safeCapture(adapters, cwd, baseRevision) : null;
561
- requireFingerprint(afterCandidate);
562
- const review = normalizeReview({ attemptResult: ar, beforeCandidate, afterCandidate });
563
- recordCanonical(job, 'review.completed', {
564
- attempt_id: attemptView.id,
565
- status: review.status,
566
- verdict: review.verdict,
567
- error_code: attemptView.error_code,
568
- }, 0);
569
- return review;
570
- }
571
-
572
- function finalize(job) {
573
- transition(job, JOB_PHASES.COMPLETED, job.review ? 'reviewed' : 'verified');
574
- recordCanonical(job, 'job.completed', {
575
- reviewed: !!job.review,
576
- task_status: job.outcome?.task_status ?? null,
577
- });
578
- setTerminal(job);
579
- }
580
-
581
- function attemptRecord(ar, attempt) {
582
- return {
583
- id: ar.id,
584
- role: ar.role ?? 'worker',
585
- attempt,
586
- provider: ar.provider ?? null,
587
- model: ar.model ?? null,
588
- execution_context: ar.execution_context ?? null,
589
- selection_source: ar.selection_source ?? null,
590
- selection_trace: ar.selection_trace ?? null,
591
- status: ar.status ?? 'failed',
592
- result: ar.result ?? null,
593
- stopReason: ar.stopReason ?? null,
594
- outcome: ar.outcome ?? null,
595
- usage: ar.usage ?? null,
596
- error: ar.error ?? null,
597
- error_code: ar.error_code ?? ar.code ?? null,
598
- timed_out: ar.timed_out === true,
599
- };
600
- }
601
-
602
- function attemptIdFor(adapters, workflowId, suffix = '') {
603
- return typeof adapters.attemptId === 'function'
604
- ? adapters.attemptId(workflowId, suffix)
605
- : `${workflowId}-a${suffix || '0'}`;
606
- }
607
-
608
- function cancelWorkflow(job) {
609
- if (job.cancelPromise) return job.cancelPromise;
610
- if (job.status !== 'running') return;
611
- job.cancelPromise = (async () => {
612
- job.cancelling = true;
613
- job.events.push({ at: clock(), phase: job.phase, type: 'cancel' });
614
- const activeAttempt = job.current_attempt_id;
615
- if (activeAttempt) {
616
- try { await adapters.cancelAttempt?.(activeAttempt); } catch {}
617
- }
618
- await releaseWorkspace(job);
619
- job.phase = JOB_PHASES.CANCELLED;
620
- job.status = 'cancelled';
621
- recordCanonical(job, 'job.cancelled', { active_attempt_id: activeAttempt ?? null });
622
- job.endedAt = clock();
623
- job.current_attempt_id = null;
624
- for (const w of job.waiters.splice(0)) w();
625
- })();
626
- return job.cancelPromise;
627
- }
628
-
629
- function workflowView(job, { withResult = false } = {}) {
630
- const failure = classifyFailure({
631
- phase: job.phase,
632
- status: job.status,
633
- errorCode: job.error_code,
634
- outcome: job.outcome,
635
- decision: job.decision,
636
- review: job.review,
637
- childAttempts: job.attempts,
638
- });
639
- const v = {
640
- id: job.id,
641
- client_job_id: job.client_job_id,
642
- role: job.role,
643
- phase: job.phase,
644
- status: job.status,
645
- attempt: job.attempts.length,
646
- current_model: job.attempts[job.attempts.length - 1]?.model ?? null,
647
- execution_context: job.attempts[job.attempts.length - 1]?.execution_context ?? null,
648
- model_class_hint: job.model_class_hint,
649
- source: job.source,
650
- profile_id: job.profile_id,
651
- allow_no_changes: job.allow_no_changes,
652
- routing: job.routing,
653
- workspace_context: job.workspace_context ? { ...job.workspace_context } : null,
654
- workspace_branch: job.workspace_branch,
655
- timeout_seconds: job.timeout_seconds,
656
- requested_cwd: job.requested_cwd,
657
- execution_cwd: job.execution_cwd,
658
- isolation: job.isolation,
659
- base_revision: job.base_revision,
660
- primary_workspace_dirty: job.primary_workspace_dirty,
661
- startedAt: job.createdAt,
662
- updatedAt: job.endedAt ?? clock(),
663
- tokens: sumUsage(job.attempts),
664
- error: job.error ?? null,
665
- error_code: job.error_code ?? null,
666
- failure,
667
- cleanup_warning: job.cleanup_warning ?? null,
668
- candidate_capture_failed: job.candidate_capture_failed === true,
669
- workspace_retained: job.workspace_retained === true,
670
- decision: job.decision,
671
- candidate_available: !!job.candidate,
672
- review_status: job.review ? job.review.status ?? null : null,
673
- review_gate: job.review_gate ?? 'required',
674
- event_cursor: job.canonical_events.at(-1)?.sequence ?? 0,
675
- };
676
- if (withResult) {
677
- v.child_attempts = job.attempts.map((a) => ({
678
- id: a.id, role: a.role, attempt: a.attempt, provider: a.provider, model: a.model,
679
- execution_context: a.execution_context ?? null,
680
- selection_source: a.selection_source, selection_trace: a.selection_trace ?? null,
681
- status: a.status, stopReason: a.stopReason,
682
- outcome_task: a.outcome?.task_status ?? null,
683
- error: a.error ?? null, error_code: a.error_code ?? null, timed_out: a.timed_out === true,
684
- candidate_fingerprint: a.candidate_fingerprint ?? null,
685
- tokens: a.usage ?? null,
686
- }));
687
- v.outcome = job.outcome;
688
- v.candidate = job.candidate;
689
- v.review = job.review;
690
- v.events = job.events;
691
- v.canonical_events = job.canonical_events.map((event) => ({ ...event, data: { ...event.data } }));
692
- }
693
- return v;
694
- }
695
-
696
- function start(spec) {
697
- syncLiveMaxParallel();
698
- const job = createJob(spec);
699
- jobs.set(job.id, job);
700
- if (active < maxParallelLimit) {
701
- active += 1;
702
- kickoff(job).catch((err) => handleKickoffFailure(job, err));
703
- } else {
704
- transition(job, JOB_PHASES.QUEUED, 'concurrency limit');
705
- queue.push(job);
706
- }
707
- return job;
708
- }
709
-
710
- async function kickoff(job) { await runWorkflow(job); }
711
-
712
- async function wait(id, timeoutMs) {
713
- const job = jobs.get(id);
714
- if (!job) return undefined;
715
- if (job.status !== 'running') return job;
716
- await raceWaiters(job.waiters, { timeoutMs });
717
- return job;
718
- }
719
-
720
- function get(id, opts = {}) {
721
- const job = jobs.get(id);
722
- return job ? workflowView(job, opts) : undefined;
723
- }
724
-
725
- function list() { return [...jobs.values()].map((j) => workflowView(j)); }
726
-
727
- async function cancel(id) {
728
- const job = jobs.get(id);
729
- if (!job) return undefined;
730
- const qi = queue.indexOf(job);
731
- if (qi !== -1) queue.splice(qi, 1);
732
- await cancelWorkflow(job);
733
- return workflowView(job);
734
- }
735
-
736
- return { start, wait, get, list, cancel, runtimeState, setMaxParallel, refreshRuntimeControls };
737
- }
738
-
739
- export { JOB_PHASES, TERMINAL };
1
+ // Workflow runtime: the single business flow for every worker dispatch, shared
2
+ // by blocking (dsh_run_worker) and async (dsh_spawn_worker) and by Hub and
3
+ // Standalone execution — the only difference between those transports is which
4
+ // adapter executes an attempt and whether the caller awaits.
5
+ //
6
+ // A Workflow Job is one logical delegation; an Attempt is one actual DSH
7
+ // worker/reviewer session within it. The runtime owns the state machine
8
+ // (phase transitions guarded by workflow.canTransition), the attempt loop
9
+ // (verify -> escalate on evidence -> reviewer pass -> finalize), candidate
10
+ // capture, review mutation detection, concurrency limiting and cancellation.
11
+ //
12
+ // All I/O is injected through `adapters`, so this module stays testable without
13
+ // DSH, a hub, git or a real filesystem. Policy decisions import the pure
14
+ // modules (policy.mjs / workflow.mjs) directly; Hub/Standalone specifics never
15
+ // appear here.
16
+
17
+ import { resolveModelPolicy, shouldAutoReview, getRoleState } from './policy.mjs';
18
+ import { raceWaiters } from './removable-waiter.mjs';
19
+ import { applyWorkspaceEvidence, buildOutcome, decideNextStep, JOB_PHASES, canTransition } from './workflow.mjs';
20
+ import { parseDeliveryReport } from './delivery.mjs';
21
+ import { classifyFailure } from './failure-classification.mjs';
22
+ import { createCanonicalJobEvent } from './job-contracts.mjs';
23
+
24
+ export const WORKFLOW_ERROR_CODES = {
25
+ ISOLATION_UNAVAILABLE: 'ISOLATION_UNAVAILABLE',
26
+ NOT_GIT_REPOSITORY: 'NOT_GIT_REPOSITORY',
27
+ WORKTREE_CREATE_FAILED: 'WORKTREE_CREATE_FAILED',
28
+ CANDIDATE_CAPTURE_FAILED: 'CANDIDATE_CAPTURE_FAILED',
29
+ WORKFLOW_CANCELLED: 'WORKFLOW_CANCELLED',
30
+ ATTEMPT_INFRA_FAILURE: 'ATTEMPT_INFRA_FAILURE',
31
+ REVIEWER_MUTATED_CANDIDATE: 'REVIEWER_MUTATED_CANDIDATE',
32
+ };
33
+
34
+ const TERMINAL = new Set([JOB_PHASES.COMPLETED, JOB_PHASES.FAILED, JOB_PHASES.CANCELLED, JOB_PHASES.INTERRUPTED]);
35
+
36
+ function lines(value) {
37
+ if (typeof value !== 'string' || value.trim() === '') return [];
38
+ return value.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
39
+ }
40
+
41
+ export function normalizeReviewVerdict(value) {
42
+ const s = String(value ?? '').trim().toLowerCase();
43
+ if (/^(approve|approved|pass)\b/.test(s) || s === 'pass' || s === 'approve') return 'approve';
44
+ if (/request.*chang|chang.*request|reject|needs changes/i.test(s)) return 'request_changes';
45
+ return 'inconclusive';
46
+ }
47
+
48
+ export function normalizeReview({ attemptResult, beforeCandidate, afterCandidate } = {}) {
49
+ const parsed = parseDeliveryReport(attemptResult?.result ?? '');
50
+ const mutation = mutationDetected(beforeCandidate, afterCandidate);
51
+ const reportedVerdict = normalizeReviewVerdict(parsed.sections?.Verdict ?? attemptResult?.result ?? '');
52
+ return {
53
+ verdict: mutation ? 'request_changes' : reportedVerdict,
54
+ reported_verdict: reportedVerdict,
55
+ findings: lines(parsed.sections?.['Review Findings']),
56
+ evidence: lines(parsed.sections?.Evidence),
57
+ risks: lines(parsed.sections?.Risks),
58
+ delivery_complete: parsed.complete ?? false,
59
+ model: attemptResult?.model ?? null,
60
+ provider: attemptResult?.provider ?? null,
61
+ selection_trace: attemptResult?.selection_trace ?? null,
62
+ status: attemptResult?.status ?? 'failed',
63
+ ...(mutation ? { mutated_candidate: true, invalidated: true } : {}),
64
+ };
65
+ }
66
+
67
+ function mutationDetected(before, after) {
68
+ if (!before) return false;
69
+ if (before.fingerprint == null) return false;
70
+ if (!after) return true;
71
+ return after.fingerprint != null && before.fingerprint !== after.fingerprint;
72
+ }
73
+
74
+ function sumUsage(attempts) {
75
+ const tokens = { input: 0, output: 0, reasoning: 0 };
76
+ for (const a of attempts) {
77
+ const u = a.usage;
78
+ if (!u) continue;
79
+ tokens.input += u.input ?? 0;
80
+ tokens.output += u.output ?? 0;
81
+ tokens.reasoning += u.reasoning ?? 0;
82
+ }
83
+ return tokens;
84
+ }
85
+
86
+ export function normalizeMaxParallel(value, fallback = 3) {
87
+ const fallbackValue = Number.isInteger(Number(fallback))
88
+ ? Math.max(1, Math.min(16, Number(fallback)))
89
+ : 3;
90
+ const parsed = Number(value);
91
+ if (!Number.isInteger(parsed)) return fallbackValue;
92
+ return Math.max(1, Math.min(16, parsed));
93
+ }
94
+
95
+ export function createWorkflowRuntime(adapters, {
96
+ maxParallel = 3,
97
+ idFactory = () => `wf-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`,
98
+ clock = () => Date.now(),
99
+ logger = null,
100
+ } = {}) {
101
+ const jobs = new Map();
102
+ const queue = [];
103
+ let active = 0;
104
+ let maxParallelLimit = normalizeMaxParallel(maxParallel);
105
+ let nestedIdFactory = null;
106
+
107
+ function nextWorkflowId() {
108
+ if (nestedIdFactory) return nestedIdFactory();
109
+ const generated = idFactory();
110
+ // A few embedders provide a factory-builder for deterministic tests.
111
+ // Normalize that legacy shape once while keeping the public id a string.
112
+ if (typeof generated === 'function') {
113
+ nestedIdFactory = generated;
114
+ return nestedIdFactory();
115
+ }
116
+ return generated;
117
+ }
118
+
119
+ function log(msg) { logger?.debug?.(`workflow: ${msg}`); }
120
+
121
+ function runtimeState() {
122
+ return {
123
+ max_parallel: maxParallelLimit,
124
+ active,
125
+ queued: queue.length,
126
+ };
127
+ }
128
+
129
+ function syncLiveMaxParallel() {
130
+ if (typeof adapters.getRuntimeControls !== 'function') return false;
131
+ const controls = adapters.getRuntimeControls() ?? {};
132
+ if (controls.max_parallel === undefined) return false;
133
+ const next = normalizeMaxParallel(controls.max_parallel, maxParallelLimit);
134
+ if (next === maxParallelLimit) return false;
135
+ maxParallelLimit = next;
136
+ return true;
137
+ }
138
+
139
+ function transition(job, to, reason) {
140
+ if (job.phase === to) return;
141
+ if (!canTransition(job.phase, to)) throw new Error(`illegal workflow transition ${job.phase} -> ${to}`);
142
+ job.phase = to;
143
+ job.events.push({ at: clock(), phase: to, type: 'phase', reason: reason ?? to });
144
+ log(`${job.id} ${to}`);
145
+ }
146
+
147
+ function recordCanonical(job, type, data = {}, attempt = null, at = clock()) {
148
+ job.canonical_event_sequence += 1;
149
+ job.canonical_events.push(createCanonicalJobEvent({
150
+ jobId: job.id,
151
+ type,
152
+ sequence: job.canonical_event_sequence,
153
+ at,
154
+ role: job.role,
155
+ attempt,
156
+ data,
157
+ }));
158
+ }
159
+
160
+ function createJob(spec) {
161
+ const now = clock();
162
+ const job = {
163
+ id: nextWorkflowId(),
164
+ client_job_id: spec.client_job_id ?? null,
165
+ role: spec.role ?? 'worker',
166
+ delivery: spec.delivery === 'review' ? 'review' : 'coding',
167
+ model_class_hint: spec.model_class_hint === 'pro' ? 'pro' : spec.model_class_hint === 'flash' ? 'flash' : null,
168
+ original_task: spec.task,
169
+ source: spec.source ?? 'api',
170
+ requested_cwd: spec.cwd,
171
+ requested_isolation: spec.requested_isolation ?? null,
172
+ workspace_branch: spec.workspace_branch ?? null,
173
+ timeout_seconds: spec.timeout_seconds ?? null,
174
+ profile_id: spec.profile_id ?? null,
175
+ allow_fallback: spec.allow_fallback !== false,
176
+ allow_no_changes: spec.allow_no_changes === true,
177
+ routing: spec.routing ?? 'auto',
178
+ review_strictness: spec.review_strictness ?? null,
179
+ workspace_context: spec.workspace_context ? { ...spec.workspace_context } : null,
180
+ effort: spec.effort ?? 'max',
181
+ phase: JOB_PHASES.CREATED,
182
+ status: 'running',
183
+ cancelling: false,
184
+ execution_cwd: spec.cwd,
185
+ isolation: 'shared',
186
+ base_revision: null,
187
+ primary_workspace_dirty: false,
188
+ attempts: [],
189
+ current_attempt_id: null,
190
+ candidate: null,
191
+ decision: null,
192
+ review: null,
193
+ outcome: null,
194
+ error: null,
195
+ cleanup_warning: null,
196
+ candidate_capture_failed: false,
197
+ retain_workspace: false,
198
+ workspace_retained: false,
199
+ events: [{ at: now, phase: JOB_PHASES.CREATED, type: 'created' }],
200
+ canonical_events: [],
201
+ canonical_event_sequence: 0,
202
+ createdAt: now,
203
+ startedAt: now,
204
+ endedAt: null,
205
+ workspaceHandle: null,
206
+ workspaceReleasePromise: null,
207
+ cancelPromise: null,
208
+ waiters: [],
209
+ };
210
+ recordCanonical(job, 'job.created', { client_job_id: job.client_job_id }, null, now);
211
+ return job;
212
+ }
213
+
214
+ function releaseSlot() {
215
+ active = Math.max(0, active - 1);
216
+ drain();
217
+ }
218
+
219
+ function handleKickoffFailure(job, err) {
220
+ failJob(job, err);
221
+ releaseSlot();
222
+ }
223
+
224
+ function drain({ sync = true } = {}) {
225
+ if (sync) syncLiveMaxParallel();
226
+ while (active < maxParallelLimit && queue.length > 0) {
227
+ const job = queue.shift();
228
+ active += 1;
229
+ kickoff(job).catch((err) => handleKickoffFailure(job, err));
230
+ }
231
+ }
232
+
233
+ function setMaxParallel(value) {
234
+ maxParallelLimit = normalizeMaxParallel(value, maxParallelLimit);
235
+ // Raising the limit admits queued work immediately. Lowering it never
236
+ // cancels already-running work: drain simply waits until active drops below
237
+ // the new limit.
238
+ drain({ sync: false });
239
+ return runtimeState();
240
+ }
241
+
242
+ function refreshRuntimeControls() {
243
+ syncLiveMaxParallel();
244
+ drain({ sync: false });
245
+ return runtimeState();
246
+ }
247
+
248
+ function setTerminal(job) {
249
+ if (job.status !== 'running') return;
250
+ if (job.phase === JOB_PHASES.COMPLETED) job.status = 'done';
251
+ else if (job.phase === JOB_PHASES.CANCELLED) job.status = 'cancelled';
252
+ else job.status = 'failed';
253
+ job.endedAt = clock();
254
+ job.current_attempt_id = null;
255
+ for (const w of job.waiters.splice(0)) w();
256
+ }
257
+
258
+ function failJob(job, err) {
259
+ if (job.status !== 'running') return;
260
+ job.error = err?.message ?? String(err);
261
+ job.error_code = err?.code ?? err?.policyCode ?? job.error_code ?? null;
262
+ job.phase = JOB_PHASES.FAILED;
263
+ recordCanonical(job, 'job.failed', {
264
+ error_code: job.error_code,
265
+ });
266
+ setTerminal(job);
267
+ }
268
+
269
+ async function releaseWorkspace(job) {
270
+ if (job.retain_workspace) {
271
+ job.workspace_retained = true;
272
+ return;
273
+ }
274
+ if (!job.workspaceHandle) return;
275
+ if (job.workspaceReleasePromise) return job.workspaceReleasePromise;
276
+ const handle = job.workspaceHandle;
277
+ job.workspaceReleasePromise = (async () => {
278
+ try {
279
+ const r = await adapters.releaseWorkspace(handle);
280
+ if (!r || r.ok !== true) {
281
+ // A failed cleanup leaves the isolated workspace in place; say so
282
+ // truthfully instead of claiming a clean release.
283
+ job.cleanup_warning = r?.error ?? 'worktree cleanup failed';
284
+ job.workspace_retained = true;
285
+ }
286
+ } catch (err) {
287
+ job.cleanup_warning = err?.message ?? String(err);
288
+ job.workspace_retained = true;
289
+ } finally {
290
+ job.workspaceHandle = null;
291
+ job.workspaceReleasePromise = null;
292
+ }
293
+ })();
294
+ return job.workspaceReleasePromise;
295
+ }
296
+
297
+ async function runWorkflow(job) {
298
+ const isReviewJob = job.role === 'reviewer' || job.delivery === 'review';
299
+ let config = {};
300
+
301
+ try {
302
+ config = adapters.getConfig?.() ?? {};
303
+ job.review_gate = ['required', 'optional', 'off'].includes(config?.review?.gate) ? config.review.gate : 'required';
304
+ const alloc = await adapters.allocateWorkspace?.(job);
305
+ if (alloc && alloc.ok === false) {
306
+ failJob(job, Object.assign(new Error(alloc.error ?? alloc.reason), { code: alloc.reason }));
307
+ return;
308
+ }
309
+ if (alloc && alloc.ok) {
310
+ job.workspaceHandle = alloc.handle ?? null;
311
+ job.execution_cwd = alloc.execution_cwd ?? job.requested_cwd;
312
+ job.isolation = alloc.isolation ?? 'worktree';
313
+ job.base_revision = alloc.base_revision ?? null;
314
+ job.primary_workspace_dirty = alloc.primary_workspace_dirty === true;
315
+ }
316
+ recordCanonical(job, 'job.started', {
317
+ isolation: job.isolation,
318
+ base_revision: job.base_revision,
319
+ });
320
+ transition(job, JOB_PHASES.RUNNING, 'start');
321
+
322
+ if (isReviewJob) {
323
+ transition(job, JOB_PHASES.REVIEWING, 'explicit reviewer');
324
+ const before = alloc?.ok && alloc.isolation === 'worktree' ? await safeCapture(adapters, job.execution_cwd, job.base_revision) : null;
325
+ // Explicit reviewers inspect the caller's stated task/workspace
326
+ // directly. The bounded worker-candidate capsule belongs only to the
327
+ // automatic post-worker review path; constructing it with no worker
328
+ // view falsely tells an explicit reviewer that nothing was delivered.
329
+ const reviewTask = job.original_task;
330
+ const review = await runReviewerAttempt(job, reviewTask, config, before);
331
+ if (job.cancelling) { await cancelWorkflow(job); return; }
332
+ job.review = review;
333
+ const reviewApproved = review.status === 'done'
334
+ && review.delivery_complete === true
335
+ && review.verdict === 'approve';
336
+ if (!reviewApproved) {
337
+ const code = review.verdict === 'request_changes'
338
+ ? 'REVIEW_CHANGES_REQUESTED'
339
+ : 'REVIEW_INCONCLUSIVE';
340
+ failJob(job, Object.assign(
341
+ new Error(`explicit review blocked acceptance: verdict=${review.verdict}, reviewer status=${review.status}, review delivery_complete=${review.delivery_complete === true}`),
342
+ { code },
343
+ ));
344
+ return;
345
+ }
346
+ if (job.review) transition(job, JOB_PHASES.READY, 'review complete');
347
+ finalize(job);
348
+ return;
349
+ }
350
+
351
+ let attempt = 0;
352
+ let escalationReason = null;
353
+ for (;;) {
354
+ if (job.cancelling) { await cancelWorkflow(job); return; }
355
+ if (attempt > 0) transition(job, JOB_PHASES.RUNNING, `escalated attempt ${attempt}`);
356
+ const resolvedPolicy = resolveModelPolicy(config, 'worker', { attempt });
357
+ const fallbackPolicy = job.allow_fallback === false
358
+ ? { ...resolvedPolicy, escalation: { ...resolvedPolicy.escalation, enabled: false } }
359
+ : resolvedPolicy;
360
+ const policy = job.routing === 'stable'
361
+ ? { ...fallbackPolicy, adaptive: { ...fallbackPolicy.adaptive, enabled: false } }
362
+ : fallbackPolicy;
363
+ const attemptId = attemptIdFor(adapters, job.id, attempt === 0 ? '' : String(attempt));
364
+ job.current_attempt_id = attemptId;
365
+ job.events.push({ at: clock(), phase: job.phase, type: 'attempt/start', attempt, escalation_reason: escalationReason });
366
+ recordCanonical(job, 'worker.started', {
367
+ attempt_id: attemptId,
368
+ escalation_reason: escalationReason,
369
+ }, attempt);
370
+ const ar = await adapters.executeAttempt({
371
+ id: attemptId,
372
+ workflowId: job.id,
373
+ role: 'worker',
374
+ attempt,
375
+ task: job.original_task,
376
+ cwd: job.execution_cwd,
377
+ effort: job.effort,
378
+ timeout_seconds: job.timeout_seconds,
379
+ policy,
380
+ source: job.source,
381
+ model_class_hint: job.model_class_hint,
382
+ escalation_reason: escalationReason,
383
+ onAttemptStarted: (actualId) => {
384
+ if (typeof actualId === 'string' && actualId) job.current_attempt_id = actualId;
385
+ },
386
+ });
387
+ job.current_attempt_id = null;
388
+ if (job.cancelling) { await cancelWorkflow(job); return; }
389
+ const attemptView = attemptRecord(ar, attempt);
390
+ job.attempts.push(attemptView);
391
+ recordCanonical(job, 'model.selected', {
392
+ provider: attemptView.provider,
393
+ model: attemptView.model,
394
+ source: attemptView.selection_source,
395
+ fallback_reason: attemptView.selection_trace?.fallback_reason ?? null,
396
+ }, attempt);
397
+ recordCanonical(job, 'worker.completed', {
398
+ attempt_id: attemptView.id,
399
+ status: attemptView.status,
400
+ stop_reason: attemptView.stopReason,
401
+ error_code: attemptView.error_code,
402
+ }, attempt);
403
+ if (ar.infra === true) {
404
+ failJob(job, Object.assign(new Error(ar.error ?? 'infrastructure failure'), { code: WORKFLOW_ERROR_CODES.ATTEMPT_INFRA_FAILURE }));
405
+ return;
406
+ }
407
+ let outcome = ar.outcome ?? buildOutcome({
408
+ result: ar.result ?? '',
409
+ stopReason: ar.stopReason,
410
+ executionStatus: ar.status === 'done' ? 'completed' : 'failed',
411
+ });
412
+ transition(job, JOB_PHASES.VERIFYING, `attempt ${attempt} complete`);
413
+
414
+ let workspaceEvidenceAvailable = false;
415
+ let workspaceHasChanges = false;
416
+
417
+ // Capture the latest candidate after every attempt. Capture failure is
418
+ // not allowed to delete the only recoverable state: retain the worktree
419
+ // and preserve the worker business result, while surfacing the warning.
420
+ if (alloc?.ok && alloc.isolation === 'worktree') {
421
+ const candidate = await safeCapture(adapters, job.execution_cwd, job.base_revision);
422
+ if (candidate === null) {
423
+ job.candidate_capture_failed = true;
424
+ job.retain_workspace = true;
425
+ job.candidate = null;
426
+ job.events.push({ at: clock(), phase: job.phase, type: 'candidate/failed', attempt, message: 'candidate capture failed; worktree retained for recovery' });
427
+ } else {
428
+ job.candidate = candidate;
429
+ attemptView.candidate_fingerprint = candidate.fingerprint ?? null;
430
+ workspaceEvidenceAvailable = true;
431
+ workspaceHasChanges = Array.isArray(candidate.changed_files) && candidate.changed_files.length > 0;
432
+ if (candidate.complete === false || candidate.replayable === false) {
433
+ job.retain_workspace = true;
434
+ job.events.push({ at: clock(), phase: job.phase, type: 'candidate/incomplete', attempt, message: 'candidate is not fully replayable; worktree retained' });
435
+ }
436
+ }
437
+ }
438
+ outcome = applyWorkspaceEvidence(outcome, {
439
+ evidenceAvailable: workspaceEvidenceAvailable,
440
+ hasChanges: workspaceHasChanges,
441
+ allowNoChanges: job.allow_no_changes,
442
+ });
443
+ job.outcome = outcome;
444
+
445
+ const reviewRequested = !isReviewJob && shouldAutoReview(config);
446
+ const reviewerAuto = getRoleState(config, 'reviewer') !== 'disabled';
447
+ const decision = decideNextStep({ outcome, policy, attempt, reviewRequested, reviewerAuto });
448
+ job.decision = { step: decision.step, phase: decision.phase, reason: decision.reason };
449
+ job.events.push({ at: clock(), phase: job.phase, type: 'attempt/complete', attempt, decision: decision.step });
450
+
451
+ if (job.cancelling) { await cancelWorkflow(job); return; }
452
+ if (decision.step === 'fail') {
453
+ const finalAttemptCode = ar.status === 'failed' ? attemptView.error_code : null;
454
+ failJob(job, Object.assign(new Error(`workflow failed: ${decision.reason}`), { code: finalAttemptCode }));
455
+ return;
456
+ }
457
+ if (decision.step === 'escalate') {
458
+ recordCanonical(job, 'model.fallback', {
459
+ reason: decision.reason,
460
+ from_attempt: attempt,
461
+ to_attempt: attempt + 1,
462
+ }, attempt);
463
+ transition(job, JOB_PHASES.ESCALATING, decision.reason);
464
+ escalationReason = decision.reason;
465
+ attempt += 1;
466
+ continue;
467
+ }
468
+ if (decision.step === 'review') {
469
+ transition(job, JOB_PHASES.REVIEWING, 'automatic review');
470
+ const before = job.candidate;
471
+ const reviewTask = adapters.buildReviewTask(job.original_task, { outcome, candidate: job.candidate ?? null }, { strictness: job.review_strictness ?? 'standard' });
472
+ const review = await runReviewerAttempt(job, reviewTask, config, before, job.execution_cwd, job.base_revision);
473
+ if (job.cancelling) { await cancelWorkflow(job); return; }
474
+ job.review = review;
475
+ // The review verdict is an acceptance gate, not a footnote: only a
476
+ // reviewer that ran to completion, delivered its own auditable
477
+ // contract, and approved may finalize as COMPLETED. Anything else
478
+ // fails closed through the existing FAILED machinery so the MCP
479
+ // surface reports a failed workflow instead of a false "done".
480
+ const reviewApproved = review.status === 'done'
481
+ && review.delivery_complete === true
482
+ && review.verdict === 'approve';
483
+ if (!reviewApproved && job.review_gate === 'required') {
484
+ const code = review.verdict === 'request_changes'
485
+ ? 'REVIEW_CHANGES_REQUESTED'
486
+ : 'REVIEW_INCONCLUSIVE';
487
+ failJob(job, Object.assign(
488
+ new Error(`automatic review blocked acceptance: verdict=${review.verdict}, reviewer status=${review.status}, review delivery_complete=${review.delivery_complete === true}`),
489
+ { code },
490
+ ));
491
+ return;
492
+ }
493
+ transition(job, JOB_PHASES.READY, decision.reason);
494
+ finalize(job);
495
+ return;
496
+ }
497
+ transition(job, JOB_PHASES.READY, decision.reason);
498
+ finalize(job);
499
+ return;
500
+ }
501
+ } catch (err) {
502
+ if (job.cancelling) { await cancelWorkflow(job); return; }
503
+ failJob(job, err);
504
+ } finally {
505
+ await releaseWorkspace(job);
506
+ releaseSlot();
507
+ }
508
+ }
509
+
510
+ async function safeCapture(adapters, cwd, baseRevision) {
511
+ if (typeof adapters.captureCandidate !== 'function') return null;
512
+ try {
513
+ const c = await adapters.captureCandidate({ cwd, baseRevision });
514
+ return c && c.ok ? c : null;
515
+ } catch { return null; }
516
+ }
517
+
518
+ async function runReviewerAttempt(job, task, config, beforeCandidate, cwd = job.execution_cwd, baseRevision = job.base_revision) {
519
+ const requireFingerprint = (candidate) => {
520
+ if (job.isolation !== 'worktree') return;
521
+ if (candidate?.ok === true && typeof candidate.fingerprint === 'string' && candidate.fingerprint.trim()) return;
522
+ job.candidate_capture_failed = true;
523
+ job.retain_workspace = true;
524
+ throw Object.assign(new Error('review workspace evidence unavailable; worktree retained'), {
525
+ code: 'REVIEW_EVIDENCE_UNAVAILABLE',
526
+ });
527
+ };
528
+ requireFingerprint(beforeCandidate);
529
+ const policy = resolveModelPolicy(config, 'reviewer', { attempt: 0 });
530
+ const attemptId = attemptIdFor(adapters, job.id, 'review');
531
+ job.current_attempt_id = attemptId;
532
+ job.events.push({ at: clock(), phase: job.phase, type: 'review/start' });
533
+ recordCanonical(job, 'review.started', { attempt_id: attemptId }, 0);
534
+ const ar = await adapters.executeAttempt({
535
+ id: attemptId,
536
+ workflowId: job.id,
537
+ role: 'reviewer',
538
+ attempt: 0,
539
+ task,
540
+ cwd,
541
+ effort: job.effort,
542
+ policy,
543
+ source: job.source,
544
+ model_class_hint: 'pro',
545
+ escalation_reason: null,
546
+ onAttemptStarted: (actualId) => {
547
+ if (typeof actualId === 'string' && actualId) job.current_attempt_id = actualId;
548
+ },
549
+ });
550
+ job.current_attempt_id = null;
551
+ const attemptView = { ...attemptRecord(ar, 0), phase: 'review' };
552
+ job.attempts.push(attemptView);
553
+ recordCanonical(job, 'model.selected', {
554
+ provider: attemptView.provider,
555
+ model: attemptView.model,
556
+ source: attemptView.selection_source,
557
+ fallback_reason: attemptView.selection_trace?.fallback_reason ?? null,
558
+ }, 0);
559
+ if (ar.status === 'failed' && attemptView.error_code) job.error_code = attemptView.error_code;
560
+ const afterCandidate = job.isolation === 'worktree' ? await safeCapture(adapters, cwd, baseRevision) : null;
561
+ requireFingerprint(afterCandidate);
562
+ const review = normalizeReview({ attemptResult: ar, beforeCandidate, afterCandidate });
563
+ recordCanonical(job, 'review.completed', {
564
+ attempt_id: attemptView.id,
565
+ status: review.status,
566
+ verdict: review.verdict,
567
+ error_code: attemptView.error_code,
568
+ }, 0);
569
+ return review;
570
+ }
571
+
572
+ function finalize(job) {
573
+ transition(job, JOB_PHASES.COMPLETED, job.review ? 'reviewed' : 'verified');
574
+ recordCanonical(job, 'job.completed', {
575
+ reviewed: !!job.review,
576
+ task_status: job.outcome?.task_status ?? null,
577
+ });
578
+ setTerminal(job);
579
+ }
580
+
581
+ function attemptRecord(ar, attempt) {
582
+ return {
583
+ id: ar.id,
584
+ role: ar.role ?? 'worker',
585
+ attempt,
586
+ provider: ar.provider ?? null,
587
+ model: ar.model ?? null,
588
+ execution_context: ar.execution_context ?? null,
589
+ selection_source: ar.selection_source ?? null,
590
+ selection_trace: ar.selection_trace ?? null,
591
+ status: ar.status ?? 'failed',
592
+ result: ar.result ?? null,
593
+ stopReason: ar.stopReason ?? null,
594
+ outcome: ar.outcome ?? null,
595
+ usage: ar.usage ?? null,
596
+ error: ar.error ?? null,
597
+ error_code: ar.error_code ?? ar.code ?? null,
598
+ timed_out: ar.timed_out === true,
599
+ };
600
+ }
601
+
602
+ function attemptIdFor(adapters, workflowId, suffix = '') {
603
+ return typeof adapters.attemptId === 'function'
604
+ ? adapters.attemptId(workflowId, suffix)
605
+ : `${workflowId}-a${suffix || '0'}`;
606
+ }
607
+
608
+ function cancelWorkflow(job) {
609
+ if (job.cancelPromise) return job.cancelPromise;
610
+ if (job.status !== 'running') return;
611
+ job.cancelPromise = (async () => {
612
+ job.cancelling = true;
613
+ job.events.push({ at: clock(), phase: job.phase, type: 'cancel' });
614
+ const activeAttempt = job.current_attempt_id;
615
+ if (activeAttempt) {
616
+ try { await adapters.cancelAttempt?.(activeAttempt); } catch {}
617
+ }
618
+ await releaseWorkspace(job);
619
+ job.phase = JOB_PHASES.CANCELLED;
620
+ job.status = 'cancelled';
621
+ recordCanonical(job, 'job.cancelled', { active_attempt_id: activeAttempt ?? null });
622
+ job.endedAt = clock();
623
+ job.current_attempt_id = null;
624
+ for (const w of job.waiters.splice(0)) w();
625
+ })();
626
+ return job.cancelPromise;
627
+ }
628
+
629
+ function workflowView(job, { withResult = false } = {}) {
630
+ const failure = classifyFailure({
631
+ phase: job.phase,
632
+ status: job.status,
633
+ errorCode: job.error_code,
634
+ outcome: job.outcome,
635
+ decision: job.decision,
636
+ review: job.review,
637
+ childAttempts: job.attempts,
638
+ });
639
+ const v = {
640
+ id: job.id,
641
+ client_job_id: job.client_job_id,
642
+ role: job.role,
643
+ phase: job.phase,
644
+ status: job.status,
645
+ attempt: job.attempts.length,
646
+ current_model: job.attempts[job.attempts.length - 1]?.model ?? null,
647
+ execution_context: job.attempts[job.attempts.length - 1]?.execution_context ?? null,
648
+ model_class_hint: job.model_class_hint,
649
+ source: job.source,
650
+ profile_id: job.profile_id,
651
+ allow_no_changes: job.allow_no_changes,
652
+ routing: job.routing,
653
+ workspace_context: job.workspace_context ? { ...job.workspace_context } : null,
654
+ workspace_branch: job.workspace_branch,
655
+ timeout_seconds: job.timeout_seconds,
656
+ requested_cwd: job.requested_cwd,
657
+ execution_cwd: job.execution_cwd,
658
+ isolation: job.isolation,
659
+ base_revision: job.base_revision,
660
+ primary_workspace_dirty: job.primary_workspace_dirty,
661
+ startedAt: job.createdAt,
662
+ updatedAt: job.endedAt ?? clock(),
663
+ tokens: sumUsage(job.attempts),
664
+ error: job.error ?? null,
665
+ error_code: job.error_code ?? null,
666
+ failure,
667
+ cleanup_warning: job.cleanup_warning ?? null,
668
+ candidate_capture_failed: job.candidate_capture_failed === true,
669
+ workspace_retained: job.workspace_retained === true,
670
+ decision: job.decision,
671
+ candidate_available: !!job.candidate,
672
+ review_status: job.review ? job.review.status ?? null : null,
673
+ review_gate: job.review_gate ?? 'required',
674
+ event_cursor: job.canonical_events.at(-1)?.sequence ?? 0,
675
+ };
676
+ if (withResult) {
677
+ v.child_attempts = job.attempts.map((a) => ({
678
+ id: a.id, role: a.role, attempt: a.attempt, provider: a.provider, model: a.model,
679
+ execution_context: a.execution_context ?? null,
680
+ selection_source: a.selection_source, selection_trace: a.selection_trace ?? null,
681
+ status: a.status, stopReason: a.stopReason,
682
+ outcome_task: a.outcome?.task_status ?? null,
683
+ error: a.error ?? null, error_code: a.error_code ?? null, timed_out: a.timed_out === true,
684
+ candidate_fingerprint: a.candidate_fingerprint ?? null,
685
+ tokens: a.usage ?? null,
686
+ }));
687
+ v.outcome = job.outcome;
688
+ v.candidate = job.candidate;
689
+ v.review = job.review;
690
+ v.events = job.events;
691
+ v.canonical_events = job.canonical_events.map((event) => ({ ...event, data: { ...event.data } }));
692
+ }
693
+ return v;
694
+ }
695
+
696
+ function start(spec) {
697
+ syncLiveMaxParallel();
698
+ const job = createJob(spec);
699
+ jobs.set(job.id, job);
700
+ if (active < maxParallelLimit) {
701
+ active += 1;
702
+ kickoff(job).catch((err) => handleKickoffFailure(job, err));
703
+ } else {
704
+ transition(job, JOB_PHASES.QUEUED, 'concurrency limit');
705
+ queue.push(job);
706
+ }
707
+ return job;
708
+ }
709
+
710
+ async function kickoff(job) { await runWorkflow(job); }
711
+
712
+ async function wait(id, timeoutMs) {
713
+ const job = jobs.get(id);
714
+ if (!job) return undefined;
715
+ if (job.status !== 'running') return job;
716
+ await raceWaiters(job.waiters, { timeoutMs });
717
+ return job;
718
+ }
719
+
720
+ function get(id, opts = {}) {
721
+ const job = jobs.get(id);
722
+ return job ? workflowView(job, opts) : undefined;
723
+ }
724
+
725
+ function list() { return [...jobs.values()].map((j) => workflowView(j)); }
726
+
727
+ async function cancel(id) {
728
+ const job = jobs.get(id);
729
+ if (!job) return undefined;
730
+ const qi = queue.indexOf(job);
731
+ if (qi !== -1) queue.splice(qi, 1);
732
+ await cancelWorkflow(job);
733
+ return workflowView(job);
734
+ }
735
+
736
+ return { start, wait, get, list, cancel, runtimeState, setMaxParallel, refreshRuntimeControls };
737
+ }
738
+
739
+ export { JOB_PHASES, TERMINAL };