cool-workflow 0.1.78

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 (193) hide show
  1. package/.claude-plugin/plugin.json +20 -0
  2. package/.codex-plugin/mcp.json +10 -0
  3. package/.codex-plugin/plugin.json +38 -0
  4. package/.mcp.json +10 -0
  5. package/LICENSE +24 -0
  6. package/README.md +638 -0
  7. package/apps/architecture-review/app.json +51 -0
  8. package/apps/architecture-review/workflow.js +116 -0
  9. package/apps/end-to-end-golden-path/app.json +30 -0
  10. package/apps/end-to-end-golden-path/workflow.js +33 -0
  11. package/apps/pr-review-fix-ci/app.json +59 -0
  12. package/apps/pr-review-fix-ci/workflow.js +90 -0
  13. package/apps/release-cut/app.json +54 -0
  14. package/apps/release-cut/workflow.js +82 -0
  15. package/apps/research-synthesis/app.json +50 -0
  16. package/apps/research-synthesis/workflow.js +76 -0
  17. package/apps/workflow-app-framework-demo/app.json +29 -0
  18. package/apps/workflow-app-framework-demo/workflow.js +44 -0
  19. package/dist/agent-config.js +223 -0
  20. package/dist/candidate-scoring.js +715 -0
  21. package/dist/capability-core.js +630 -0
  22. package/dist/capability-dispatcher.js +86 -0
  23. package/dist/capability-registry.js +523 -0
  24. package/dist/cli.js +1276 -0
  25. package/dist/collaboration.js +727 -0
  26. package/dist/commit.js +570 -0
  27. package/dist/contract-migration.js +234 -0
  28. package/dist/coordinator.js +1163 -0
  29. package/dist/daemon.js +44 -0
  30. package/dist/dispatch.js +201 -0
  31. package/dist/drive.js +503 -0
  32. package/dist/error-feedback.js +415 -0
  33. package/dist/evidence-grounding.js +179 -0
  34. package/dist/evidence-reasoning.js +733 -0
  35. package/dist/execution-backend.js +1279 -0
  36. package/dist/harness.js +61 -0
  37. package/dist/mcp-server.js +1615 -0
  38. package/dist/multi-agent-eval.js +857 -0
  39. package/dist/multi-agent-host.js +764 -0
  40. package/dist/multi-agent-operator-ux.js +537 -0
  41. package/dist/multi-agent-trust.js +366 -0
  42. package/dist/multi-agent.js +1173 -0
  43. package/dist/node-snapshot.js +270 -0
  44. package/dist/observability.js +922 -0
  45. package/dist/operator-ux.js +971 -0
  46. package/dist/orchestrator/audit-operations.js +182 -0
  47. package/dist/orchestrator/candidate-operations.js +117 -0
  48. package/dist/orchestrator/cli-options.js +288 -0
  49. package/dist/orchestrator/collaboration-operations.js +86 -0
  50. package/dist/orchestrator/feedback-operations.js +81 -0
  51. package/dist/orchestrator/host-operations.js +78 -0
  52. package/dist/orchestrator/lifecycle-operations.js +462 -0
  53. package/dist/orchestrator/migration-operations.js +44 -0
  54. package/dist/orchestrator/multi-agent-operations.js +362 -0
  55. package/dist/orchestrator/report.js +369 -0
  56. package/dist/orchestrator/topology-operations.js +84 -0
  57. package/dist/orchestrator.js +874 -0
  58. package/dist/pipeline-contract.js +92 -0
  59. package/dist/pipeline-runner.js +285 -0
  60. package/dist/reclamation.js +882 -0
  61. package/dist/result-normalize.js +194 -0
  62. package/dist/run-export.js +64 -0
  63. package/dist/run-registry.js +1347 -0
  64. package/dist/run-state-schema.js +67 -0
  65. package/dist/sandbox-profile.js +471 -0
  66. package/dist/scheduler.js +266 -0
  67. package/dist/scheduling.js +184 -0
  68. package/dist/schema-validate.js +98 -0
  69. package/dist/state-explosion.js +1213 -0
  70. package/dist/state-migrations.js +463 -0
  71. package/dist/state-node.js +301 -0
  72. package/dist/state.js +308 -0
  73. package/dist/telemetry-attestation.js +156 -0
  74. package/dist/telemetry-ledger.js +145 -0
  75. package/dist/topology.js +527 -0
  76. package/dist/triggers.js +159 -0
  77. package/dist/trust-audit.js +475 -0
  78. package/dist/types/blackboard.js +2 -0
  79. package/dist/types/boundary.js +29 -0
  80. package/dist/types/candidate.js +2 -0
  81. package/dist/types/collaboration.js +2 -0
  82. package/dist/types/core.js +2 -0
  83. package/dist/types/drive.js +10 -0
  84. package/dist/types/error-feedback.js +2 -0
  85. package/dist/types/evidence-reasoning.js +2 -0
  86. package/dist/types/execution-backend.js +2 -0
  87. package/dist/types/multi-agent.js +2 -0
  88. package/dist/types/observability.js +2 -0
  89. package/dist/types/pipeline.js +2 -0
  90. package/dist/types/reclamation.js +8 -0
  91. package/dist/types/result.js +2 -0
  92. package/dist/types/run-registry.js +2 -0
  93. package/dist/types/run.js +2 -0
  94. package/dist/types/sandbox.js +2 -0
  95. package/dist/types/schedule.js +2 -0
  96. package/dist/types/state-node.js +2 -0
  97. package/dist/types/topology.js +2 -0
  98. package/dist/types/trust.js +2 -0
  99. package/dist/types/workbench.js +2 -0
  100. package/dist/types/worker.js +2 -0
  101. package/dist/types/workflow-app.js +2 -0
  102. package/dist/types.js +43 -0
  103. package/dist/verifier-registry.js +46 -0
  104. package/dist/verifier.js +78 -0
  105. package/dist/version.js +8 -0
  106. package/dist/workbench-host.js +172 -0
  107. package/dist/workbench.js +190 -0
  108. package/dist/worker-isolation.js +1028 -0
  109. package/dist/workflow-api.js +98 -0
  110. package/dist/workflow-app-framework.js +626 -0
  111. package/docs/agent-delegation-drive.7.md +190 -0
  112. package/docs/agent-framework.md +176 -0
  113. package/docs/candidate-scoring.7.md +106 -0
  114. package/docs/canonical-workflow-apps.7.md +137 -0
  115. package/docs/capability-topology-registry.7.md +168 -0
  116. package/docs/cli-mcp-parity.7.md +373 -0
  117. package/docs/contract-migration-tooling.7.md +123 -0
  118. package/docs/control-plane-scheduling.7.md +110 -0
  119. package/docs/coordinator-blackboard.7.md +183 -0
  120. package/docs/dogfood/architecture-review-cool-workflow.md +16 -0
  121. package/docs/dogfood-one-real-repo.7.md +168 -0
  122. package/docs/durable-state-and-locking.7.md +107 -0
  123. package/docs/end-to-end-golden-path.7.md +117 -0
  124. package/docs/error-feedback.7.md +153 -0
  125. package/docs/evidence-adoption-reasoning-chain.7.md +270 -0
  126. package/docs/execution-backends.7.md +300 -0
  127. package/docs/getting-started.md +99 -0
  128. package/docs/index.md +41 -0
  129. package/docs/mcp-app-surface.7.md +235 -0
  130. package/docs/multi-agent-cli-mcp-surface.7.md +265 -0
  131. package/docs/multi-agent-eval-replay-harness.7.md +302 -0
  132. package/docs/multi-agent-operator-ux.7.md +314 -0
  133. package/docs/multi-agent-runtime-core.7.md +231 -0
  134. package/docs/multi-agent-topologies.7.md +103 -0
  135. package/docs/multi-agent-trust-policy-audit.7.md +154 -0
  136. package/docs/node-snapshot-diff-replay.7.md +135 -0
  137. package/docs/observability-cost-accounting.7.md +194 -0
  138. package/docs/operator-ux.7.md +180 -0
  139. package/docs/pipeline-runner.7.md +136 -0
  140. package/docs/project-index.md +261 -0
  141. package/docs/real-execution-backends.7.md +142 -0
  142. package/docs/release-and-migration.7.md +280 -0
  143. package/docs/release-tooling.7.md +159 -0
  144. package/docs/routines.md +48 -0
  145. package/docs/run-registry-control-plane.7.md +312 -0
  146. package/docs/run-retention-reclamation.7.md +191 -0
  147. package/docs/sandbox-profiles.7.md +137 -0
  148. package/docs/scheduled-tasks.md +80 -0
  149. package/docs/security-trust-hardening.7.md +117 -0
  150. package/docs/state-explosion-management.7.md +264 -0
  151. package/docs/state-node.7.md +96 -0
  152. package/docs/team-collaboration.7.md +207 -0
  153. package/docs/unix-principles.md +192 -0
  154. package/docs/verifier-gated-commit.7.md +140 -0
  155. package/docs/web-desktop-workbench.7.md +215 -0
  156. package/docs/worker-isolation.7.md +167 -0
  157. package/docs/workflow-app-framework.7.md +274 -0
  158. package/manifest/README.md +43 -0
  159. package/manifest/plugin.manifest.json +316 -0
  160. package/manifest/pricing.policy.json +14 -0
  161. package/package.json +79 -0
  162. package/scripts/agents/claude-p-agent.js +104 -0
  163. package/scripts/agents/claude-p-agent.sh +9 -0
  164. package/scripts/agents/cw-attest-keygen.js +55 -0
  165. package/scripts/agents/cw-attest-wrap.js +143 -0
  166. package/scripts/block-unapproved-tag.sh +39 -0
  167. package/scripts/bump-version.js +249 -0
  168. package/scripts/canonical-apps.js +171 -0
  169. package/scripts/cw.js +4 -0
  170. package/scripts/dist-drift-check.js +79 -0
  171. package/scripts/dogfood-architecture-review.js +237 -0
  172. package/scripts/dogfood-release.js +624 -0
  173. package/scripts/forward-ref-docs.js +73 -0
  174. package/scripts/gen-manifests.js +232 -0
  175. package/scripts/golden-path.js +300 -0
  176. package/scripts/mcp-server.js +4 -0
  177. package/scripts/new-feature.js +121 -0
  178. package/scripts/parity-check.js +213 -0
  179. package/scripts/release-check.js +118 -0
  180. package/scripts/release-flow.js +272 -0
  181. package/scripts/release-gate.sh +85 -0
  182. package/scripts/sync-project-index.js +387 -0
  183. package/scripts/validate-run-state-schema.js +126 -0
  184. package/scripts/verify-container-selfref.js +64 -0
  185. package/scripts/version-sync-check.js +237 -0
  186. package/skills/cool-workflow/SKILL.md +162 -0
  187. package/skills/cool-workflow/references/commands.md +282 -0
  188. package/tsconfig.json +16 -0
  189. package/ui/workbench/app.css +76 -0
  190. package/ui/workbench/app.js +159 -0
  191. package/ui/workbench/index.html +32 -0
  192. package/workflows/architecture-review.workflow.js +84 -0
  193. package/workflows/research-synthesis.workflow.js +47 -0
@@ -0,0 +1,1028 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.WORKER_ISOLATION_SCHEMA_VERSION = void 0;
7
+ exports.createWorkerIsolation = createWorkerIsolation;
8
+ exports.allocateWorkerScope = allocateWorkerScope;
9
+ exports.writeWorkerManifest = writeWorkerManifest;
10
+ exports.syncWorkerScopeFromTask = syncWorkerScopeFromTask;
11
+ exports.listWorkerScopes = listWorkerScopes;
12
+ exports.getWorkerScope = getWorkerScope;
13
+ exports.recordWorkerOutput = recordWorkerOutput;
14
+ exports.recordWorkerFailure = recordWorkerFailure;
15
+ exports.recordWorkerRetryAttempt = recordWorkerRetryAttempt;
16
+ exports.validateWorkerBoundary = validateWorkerBoundary;
17
+ exports.summarizeWorkers = summarizeWorkers;
18
+ exports.reclaimOrphans = reclaimOrphans;
19
+ const node_fs_1 = __importDefault(require("node:fs"));
20
+ const node_path_1 = __importDefault(require("node:path"));
21
+ const state_1 = require("./state");
22
+ const pipeline_contract_1 = require("./pipeline-contract");
23
+ const error_feedback_1 = require("./error-feedback");
24
+ const state_node_1 = require("./state-node");
25
+ const pipeline_runner_1 = require("./pipeline-runner");
26
+ const verifier_1 = require("./verifier");
27
+ const evidence_grounding_1 = require("./evidence-grounding");
28
+ const result_normalize_1 = require("./result-normalize");
29
+ const sandbox_profile_1 = require("./sandbox-profile");
30
+ const execution_backend_1 = require("./execution-backend");
31
+ const trust_audit_1 = require("./trust-audit");
32
+ const multi_agent_1 = require("./multi-agent");
33
+ const telemetry_attestation_1 = require("./telemetry-attestation");
34
+ const telemetry_ledger_1 = require("./telemetry-ledger");
35
+ const coordinator_1 = require("./coordinator");
36
+ exports.WORKER_ISOLATION_SCHEMA_VERSION = 1;
37
+ const WORKER_SCOPE_FILE = "worker.json";
38
+ const WORKER_MANIFEST_FILE = "manifest.json";
39
+ function createWorkerIsolation(options = {}) {
40
+ return {
41
+ allocateWorkerScope: (run, task, allocateOptions) => allocateWorkerScope(run, task, { ...options, ...allocateOptions }),
42
+ writeWorkerManifest,
43
+ listWorkerScopes: (run, listOptions) => listWorkerScopes(run, listOptions),
44
+ getWorkerScope,
45
+ recordWorkerOutput,
46
+ recordWorkerFailure,
47
+ validateWorkerBoundary,
48
+ summarizeWorkers
49
+ };
50
+ }
51
+ function allocateWorkerScope(run, task, options = {}) {
52
+ ensureWorkerState(run);
53
+ const existing = task.workerId ? getWorkerScope(run, task.workerId) : undefined;
54
+ if (existing) {
55
+ // Retry detection: re-allocating a worker for the same task
56
+ if (existing.status === "failed" || existing.status === "orphaned") {
57
+ existing.retryCount = (existing.retryCount || 0) + 1;
58
+ existing.updatedAt = new Date().toISOString();
59
+ existing.status = options.status || "allocated";
60
+ existing.errors = [];
61
+ upsertWorkerScope(run, existing);
62
+ writeWorkerIndex(run);
63
+ }
64
+ return existing;
65
+ }
66
+ const now = new Date().toISOString();
67
+ const workerId = options.workerId || createWorkerId(run, task.id);
68
+ const workerDir = node_path_1.default.join(workerRoot(run), (0, state_1.safeFileName)(workerId));
69
+ const inputPath = node_path_1.default.join(workerDir, "input.md");
70
+ const resultPath = node_path_1.default.join(workerDir, "result.md");
71
+ const artifactsDir = node_path_1.default.join(workerDir, "artifacts");
72
+ const logsDir = node_path_1.default.join(workerDir, "logs");
73
+ const sandboxProfileId = options.sandboxProfileId || options.policy?.sandboxProfileId || sandbox_profile_1.DEFAULT_SANDBOX_PROFILE_ID;
74
+ const sandboxPolicy = (0, sandbox_profile_1.sandboxPolicyForWorker)(sandboxProfileId, {
75
+ cwd: run.cwd,
76
+ runDir: run.paths.runDir,
77
+ workerDir,
78
+ inputPath,
79
+ resultPath,
80
+ artifactsDir,
81
+ logsDir,
82
+ extraReadPaths: options.policy?.readPaths || [],
83
+ extraWritePaths: [...(options.policy?.writePaths || []), ...(options.policy?.allowedPaths || [])],
84
+ allowArtifacts: options.policy?.allowArtifacts,
85
+ allowLogs: options.policy?.allowLogs
86
+ });
87
+ const allowedPaths = (0, sandbox_profile_1.effectiveSandboxWritePaths)(sandboxPolicy);
88
+ (0, sandbox_profile_1.upsertRunSandboxPolicy)(run, sandboxPolicy);
89
+ // Execution backend selection (mechanism vs policy): the worker scope records
90
+ // WHICH backend was selected + its sandbox attestation. The dispatch path is a
91
+ // delegate-host execution (the host runs the worker), so the backend enforces
92
+ // only CW's own worker-output acceptance and attests the rest — reproducing
93
+ // pre-v0.1.29 behavior exactly for the default (node) backend. Only recorded
94
+ // when a backend was explicitly selected.
95
+ const backendSelection = options.backendSelection || (options.backendId ? (0, execution_backend_1.resolveBackendSelection)(options.backendId) : undefined);
96
+ const backendId = backendSelection?.backendId;
97
+ const backendAttestation = backendId
98
+ ? options.backendAttestation || (0, execution_backend_1.attestSandbox)((0, execution_backend_1.getBackendDescriptor)(backendId), sandboxPolicy, { mode: "delegate-host" })
99
+ : undefined;
100
+ node_fs_1.default.mkdirSync(artifactsDir, { recursive: true });
101
+ node_fs_1.default.mkdirSync(logsDir, { recursive: true });
102
+ const scope = {
103
+ schemaVersion: exports.WORKER_ISOLATION_SCHEMA_VERSION,
104
+ id: workerId,
105
+ runId: run.id,
106
+ taskId: task.id,
107
+ dispatchId: options.dispatchId || task.dispatchId,
108
+ createdAt: now,
109
+ updatedAt: now,
110
+ status: options.status || "allocated",
111
+ workerDir,
112
+ inputPath,
113
+ resultPath,
114
+ artifactsDir,
115
+ logsDir,
116
+ allowedPaths,
117
+ sandboxProfileId: sandboxPolicy.id,
118
+ sandboxPolicy,
119
+ backendId,
120
+ backendSelection,
121
+ backendAttestation,
122
+ stateNodeId: task.stateNodeId,
123
+ feedbackIds: [],
124
+ errors: [],
125
+ multiAgent: options.multiAgent,
126
+ metadata: compactMetadata({
127
+ ...options.metadata,
128
+ multiAgent: options.multiAgent,
129
+ phase: task.phase,
130
+ kind: task.kind,
131
+ taskPath: task.taskPath
132
+ })
133
+ };
134
+ writeWorkerInput(run, task, scope);
135
+ writeWorkerManifest(run, scope);
136
+ upsertWorkerScope(run, scope);
137
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
138
+ kind: "worker.sandbox-profile",
139
+ decision: "recorded",
140
+ source: "runtime-derived",
141
+ workerId: scope.id,
142
+ taskId: task.id,
143
+ sandboxProfileId: sandboxPolicy.id,
144
+ policySnapshot: sandboxPolicy,
145
+ metadata: { dispatchId: scope.dispatchId, workerDir: scope.workerDir, allowedPaths }
146
+ });
147
+ if (backendId && backendAttestation) {
148
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
149
+ kind: "worker.backend",
150
+ decision: backendAttestation.status === "refused" ? "denied" : "recorded",
151
+ source: "runtime-derived",
152
+ workerId: scope.id,
153
+ taskId: task.id,
154
+ sandboxProfileId: sandboxPolicy.id,
155
+ policySnapshot: sandboxPolicy,
156
+ metadata: {
157
+ backendId,
158
+ backendSelection,
159
+ attestationStatus: backendAttestation.status,
160
+ enforced: backendAttestation.enforced,
161
+ attested: backendAttestation.attested,
162
+ unenforceable: backendAttestation.unenforceable,
163
+ dispatchId: scope.dispatchId
164
+ }
165
+ });
166
+ }
167
+ task.workerId = scope.id;
168
+ task.workerManifestPath = manifestPath(scope);
169
+ task.sandboxProfileId = sandboxPolicy.id;
170
+ task.sandboxPolicy = sandboxPolicy;
171
+ task.backendId = backendId;
172
+ task.backendSelection = backendSelection;
173
+ task.backendAttestation = backendAttestation;
174
+ writeWorkerIndex(run);
175
+ if (options.persist !== false)
176
+ (0, state_1.saveCheckpoint)(run);
177
+ return scope;
178
+ }
179
+ function writeWorkerManifest(run, scope) {
180
+ const task = run.tasks.find((candidate) => candidate.id === scope.taskId);
181
+ const sandboxPolicy = scope.sandboxPolicy || sandboxPolicyForBoundary(run, scope);
182
+ const sandboxProfileId = scope.sandboxProfileId || sandboxPolicy.id;
183
+ const scopePath = workerScopePath(scope);
184
+ const workerManifestPath = manifestPath(scope);
185
+ const manifest = {
186
+ schemaVersion: exports.WORKER_ISOLATION_SCHEMA_VERSION,
187
+ id: scope.id,
188
+ runId: scope.runId,
189
+ taskId: scope.taskId,
190
+ dispatchId: scope.dispatchId,
191
+ createdAt: scope.createdAt,
192
+ updatedAt: scope.updatedAt,
193
+ status: scope.status,
194
+ workerDir: scope.workerDir,
195
+ scopePath,
196
+ manifestPath: workerManifestPath,
197
+ inputPath: scope.inputPath,
198
+ resultPath: scope.resultPath,
199
+ artifactsDir: scope.artifactsDir,
200
+ logsDir: scope.logsDir,
201
+ allowedPaths: scope.allowedPaths,
202
+ sandboxProfileId,
203
+ sandboxPolicy,
204
+ sandbox: sandboxPolicy
205
+ ? {
206
+ profileId: sandboxPolicy.id,
207
+ policy: sandboxPolicy,
208
+ enforcedByCW: sandboxPolicy.enforcement.enforcedByCW,
209
+ hostRequired: sandboxPolicy.enforcement.hostRequired
210
+ }
211
+ : undefined,
212
+ backendId: scope.backendId,
213
+ backendSelection: scope.backendSelection,
214
+ backendAttestation: scope.backendAttestation,
215
+ retryCount: scope.retryCount,
216
+ backend: scope.backendId && scope.backendAttestation
217
+ ? {
218
+ id: scope.backendId,
219
+ locality: scope.backendAttestation.locality,
220
+ kind: scope.backendAttestation.kind,
221
+ enforces: scope.backendAttestation.enforced,
222
+ attests: scope.backendAttestation.attested,
223
+ attestation: scope.backendAttestation
224
+ }
225
+ : undefined,
226
+ instructions: [
227
+ "Read input.md before doing work.",
228
+ "Write the final Markdown result to result.md.",
229
+ "Write worker-local artifacts under artifacts/ and logs under logs/.",
230
+ `Sandbox profile: ${sandboxProfileId}.`,
231
+ "CW enforces profile validation and worker result acceptance only.",
232
+ "The agent host must enforce OS file access, process execution, network access, and environment filtering.",
233
+ "Do not edit shared run state files directly; CW records accepted results."
234
+ ],
235
+ taskPath: task?.taskPath,
236
+ prompt: task?.prompt,
237
+ stateNodeId: scope.stateNodeId,
238
+ resultNodeId: scope.resultNodeId,
239
+ feedbackIds: scope.feedbackIds,
240
+ errors: scope.errors,
241
+ output: scope.output,
242
+ multiAgent: scope.multiAgent,
243
+ blackboard: blackboardManifest(run, scope),
244
+ metadata: scope.metadata
245
+ };
246
+ (0, state_1.writeJson)(workerManifestPath, manifest);
247
+ return manifest;
248
+ }
249
+ function syncWorkerScopeFromTask(run, workerId) {
250
+ const scope = getWorkerScope(run, workerId);
251
+ if (!scope)
252
+ return undefined;
253
+ const task = run.tasks.find((candidate) => candidate.id === scope.taskId);
254
+ if (!task?.multiAgent)
255
+ return scope;
256
+ const updated = {
257
+ ...scope,
258
+ updatedAt: new Date().toISOString(),
259
+ multiAgent: task.multiAgent,
260
+ metadata: compactMetadata({
261
+ ...(scope.metadata || {}),
262
+ multiAgent: task.multiAgent
263
+ })
264
+ };
265
+ return updateWorkerScope(run, updated);
266
+ }
267
+ function listWorkerScopes(run, options = {}) {
268
+ ensureWorkerState(run);
269
+ const scopes = loadWorkerScopesFromDisk(run);
270
+ run.workers = mergeScopes(run.workers || [], scopes);
271
+ const listed = run.workers || [];
272
+ return options.status ? listed.filter((scope) => scope.status === options.status) : listed;
273
+ }
274
+ function getWorkerScope(run, workerId) {
275
+ ensureWorkerState(run);
276
+ const existing = (run.workers || []).find((scope) => scope.id === workerId);
277
+ if (existing)
278
+ return existing;
279
+ const file = node_path_1.default.join(workerRoot(run), (0, state_1.safeFileName)(workerId), WORKER_SCOPE_FILE);
280
+ if (!node_fs_1.default.existsSync(file))
281
+ return undefined;
282
+ const scope = JSON.parse(node_fs_1.default.readFileSync(file, "utf8"));
283
+ upsertWorkerScope(run, scope);
284
+ return scope;
285
+ }
286
+ function recordWorkerOutput(run, workerId, resultPath, options = {}) {
287
+ const scope = requireWorkerScope(run, workerId);
288
+ const task = requireWorkerTask(run, scope);
289
+ const absoluteResultPath = node_path_1.default.resolve(resultPath);
290
+ const violation = validateWorkerBoundary(run, workerId, { ...options, policy: options.policy, path: absoluteResultPath });
291
+ if (violation) {
292
+ (0, trust_audit_1.recordSandboxPathDecision)(run, {
293
+ workerId,
294
+ taskId: task.id,
295
+ sandboxProfileId: scope.sandboxProfileId,
296
+ policySnapshot: scope.sandboxPolicy,
297
+ target: absoluteResultPath,
298
+ decision: "denied",
299
+ metadata: { code: violation.code, allowedPaths: violation.allowedPaths }
300
+ });
301
+ recordWorkerFailure(run, workerId, violation, { ...options, path: absoluteResultPath, code: violation.code, retryable: false });
302
+ throw new Error(violation.message);
303
+ }
304
+ if (!node_fs_1.default.existsSync(absoluteResultPath)) {
305
+ const error = structuredError("worker-result-missing", `Worker result file does not exist: ${absoluteResultPath}`, {
306
+ path: absoluteResultPath,
307
+ retryable: true
308
+ });
309
+ recordWorkerFailure(run, workerId, error, { ...options, persist: options.persist });
310
+ throw new Error(error.message);
311
+ }
312
+ const rawResult = node_fs_1.default.readFileSync(absoluteResultPath, "utf8");
313
+ const parsedResult = (0, verifier_1.parseResultEnvelope)(rawResult);
314
+ (0, verifier_1.validateResultEnvelope)(task, parsedResult);
315
+ // Strict evidence resolution (v0.1.40 self-audit P1, opt-in via
316
+ // CW_REQUIRE_RESOLVABLE_EVIDENCE): fail closed if the result cites file-style
317
+ // evidence that does not resolve on disk, so a worker cannot land a result
318
+ // whose evidence locators point nowhere. Off by default — the default gate is
319
+ // the deterministic grounding check in validateResultEnvelope.
320
+ if ((0, evidence_grounding_1.requireResolvableEvidence)()) {
321
+ const baseDirs = Array.from(new Set([run.cwd, process.cwd(), scope.workerDir, run.paths.runDir].filter(Boolean)));
322
+ const unresolved = (0, evidence_grounding_1.unresolvedFileEvidence)(parsedResult.evidence, baseDirs);
323
+ if (unresolved.length) {
324
+ const error = structuredError("worker-evidence-unresolvable", `Worker ${workerId} result cites file evidence that does not resolve on disk: ${unresolved.join(", ")}`, { path: absoluteResultPath, retryable: false });
325
+ recordWorkerFailure(run, workerId, error, { ...options, persist: options.persist });
326
+ throw new Error(error.message);
327
+ }
328
+ }
329
+ // Agent Delegation Drive (v0.1.38): if this worker's result.md was produced by an
330
+ // EXTERNAL agent, record the agent-hop attestation AS PROVENANCE — the agent
331
+ // (kind:process) handle, the agent-REPORTED model (never CW_AGENT_MODEL), the
332
+ // prompt digest, the secret-stripped args, and the result digest computed HERE
333
+ // from the accepted result.md. These live in the result node's metadata (covered
334
+ // by the v0.1.35 snapshot body) + a trust-audit event, NEVER in `evidence`.
335
+ // Track 1: verify the agent's signed telemetry BEFORE recording it. CW holds
336
+ // only the operator's PUBLIC key — it verifies attribution, never measures
337
+ // usage. Absent/invalid signature ⇒ `unattested`/`absent`, surfaced loudly,
338
+ // NEVER silently recorded as trusted.
339
+ const telemetry = options.agentDelegation
340
+ ? (0, telemetry_attestation_1.verifyTelemetryAttestation)(options.agentDelegation.reportedUsage, options.agentDelegation.usageSignature, (0, telemetry_attestation_1.resolveTrustPublicKey)(options.agentDelegation.usageTrustPublicKey), { runId: run.id, taskId: task.id, promptDigest: options.agentDelegation.promptDigest })
341
+ : undefined;
342
+ // Track 1 fail-closed (Decision 2 — OPT-IN, off by default). When the operator
343
+ // requires attested telemetry, a delegated hop whose verdict is not `attested`
344
+ // is REJECTED here — BEFORE any accept-side state mutation — so the drive parks
345
+ // it instead of recording unverifiable usage. Default behavior is unchanged
346
+ // (flag-and-surface). Non-agent hops carry no verdict and are never blocked.
347
+ if (options.requireAttestedTelemetry && telemetry && telemetry.status !== "attested") {
348
+ const error = structuredError("telemetry-unattested-blocked", `Worker ${workerId} telemetry is ${telemetry.status} (${telemetry.reason || "unverified"}) and require-attested-telemetry is enabled — refusing to accept a hop whose usage cannot be cryptographically verified`, { path: absoluteResultPath, retryable: false });
349
+ recordWorkerFailure(run, workerId, error, { ...options, persist: options.persist });
350
+ throw new Error(error.message);
351
+ }
352
+ const agentDelegation = options.agentDelegation
353
+ ? {
354
+ schemaVersion: 1,
355
+ backendId: "agent",
356
+ handle: options.agentDelegation.handle,
357
+ model: options.agentDelegation.model,
358
+ promptDigest: options.agentDelegation.promptDigest,
359
+ resultDigest: (0, execution_backend_1.sha256)(rawResult),
360
+ command: options.agentDelegation.command,
361
+ args: options.agentDelegation.args,
362
+ exitCode: options.agentDelegation.exitCode,
363
+ ...(options.agentDelegation.reportedUsage ? { reportedUsage: options.agentDelegation.reportedUsage } : {}),
364
+ ...(options.agentDelegation.usageSignature ? { usageSignature: options.agentDelegation.usageSignature } : {}),
365
+ ...(telemetry ? { usageAttestation: telemetry.status, usageAttestationReason: telemetry.reason } : {})
366
+ }
367
+ : undefined;
368
+ const pathAudit = (0, trust_audit_1.recordSandboxPathDecision)(run, {
369
+ workerId,
370
+ taskId: task.id,
371
+ sandboxProfileId: scope.sandboxProfileId,
372
+ policySnapshot: scope.sandboxPolicy,
373
+ target: absoluteResultPath,
374
+ decision: "allowed",
375
+ metadata: { operation: "worker-output-acceptance" }
376
+ });
377
+ const destination = node_path_1.default.join(run.paths.resultsDir, `${(0, state_1.safeFileName)(task.id)}.md`);
378
+ node_fs_1.default.mkdirSync(run.paths.resultsDir, { recursive: true });
379
+ node_fs_1.default.copyFileSync(absoluteResultPath, destination);
380
+ task.status = "completed";
381
+ task.completedAt = new Date().toISOString();
382
+ task.resultPath = destination;
383
+ task.loopStage = "observe";
384
+ task.result = parsedResult;
385
+ const evidence = (0, trust_audit_1.normalizeEvidence)(run, parsedResult.evidence.map((entry, index) => ({
386
+ id: `result:${index + 1}`,
387
+ source: "cw:result",
388
+ locator: entry,
389
+ summary: entry
390
+ })), { source: "cw-validated", workerId, taskId: task.id, auditEventIds: [pathAudit.id] });
391
+ const resultNode = (0, state_node_1.appendRunNode)(run, (0, state_node_1.createStateNode)({
392
+ id: `${run.id}:result:${task.id}`,
393
+ kind: "result",
394
+ status: "completed",
395
+ loopStage: "observe",
396
+ inputs: { taskId: task.id, dispatchId: task.dispatchId, workerId },
397
+ outputs: parsedResult,
398
+ artifacts: [
399
+ { id: "result", kind: "markdown", path: destination },
400
+ { id: "worker-result", kind: "markdown", path: absoluteResultPath }
401
+ ],
402
+ evidence,
403
+ parents: task.dispatchId ? [`${run.id}:dispatch:${task.dispatchId}`] : [task.stateNodeId || `${run.id}:task:${task.id}`],
404
+ contractId: pipeline_contract_1.DEFAULT_PIPELINE_CONTRACT_ID,
405
+ metadata: {
406
+ taskId: task.id,
407
+ workerId,
408
+ workerDir: scope.workerDir,
409
+ sandboxProfileId: scope.sandboxProfileId,
410
+ auditEventIds: [pathAudit.id],
411
+ // Empty-capture warning (v0.1.42): even after robust normalization the result
412
+ // yielded NO findings and NO evidence — surfaced, never silently passed.
413
+ ...((0, result_normalize_1.isEmptyCapture)(parsedResult) ? { captureWarning: "no findings or evidence captured from result.md" } : {}),
414
+ // Folded into the snapshotted node body so v0.1.35 replay re-verifies the
415
+ // prompt/result/model digests WITHOUT re-spawning the agent. NOT evidence.
416
+ ...(agentDelegation ? { agentDelegation } : {})
417
+ }
418
+ }));
419
+ const acceptedAudit = (0, trust_audit_1.recordTrustAuditEvent)(run, {
420
+ kind: "worker.output",
421
+ decision: "accepted",
422
+ source: "cw-validated",
423
+ workerId,
424
+ taskId: task.id,
425
+ nodeId: resultNode.id,
426
+ sandboxProfileId: scope.sandboxProfileId,
427
+ policySnapshot: scope.sandboxPolicy,
428
+ normalizedPath: absoluteResultPath,
429
+ evidence,
430
+ parentEventIds: [pathAudit.id],
431
+ metadata: { destination }
432
+ });
433
+ resultNode.evidence = (0, trust_audit_1.normalizeEvidence)(run, resultNode.evidence, {
434
+ source: "cw-validated",
435
+ workerId,
436
+ taskId: task.id,
437
+ resultNodeId: resultNode.id,
438
+ auditEventIds: [pathAudit.id, acceptedAudit.id]
439
+ });
440
+ (0, state_node_1.appendRunNode)(run, resultNode);
441
+ task.resultNodeId = resultNode.id;
442
+ // Warn (don't silently pass) when a worker's result captured no structured signal
443
+ // at all — the v0.1.41 self-audit's "accepted with evidenceCount:0" failure mode.
444
+ if ((0, result_normalize_1.isEmptyCapture)(parsedResult)) {
445
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
446
+ kind: "worker.capture-warning",
447
+ decision: "recorded",
448
+ source: "cw-validated",
449
+ workerId,
450
+ taskId: task.id,
451
+ nodeId: resultNode.id,
452
+ parentEventIds: [acceptedAudit.id],
453
+ metadata: { reason: "no findings or evidence captured from result.md", resultPath: destination }
454
+ });
455
+ }
456
+ // The agent-hop attestation event — hung off worker.output, alongside
457
+ // worker.backend. Recorded in trust-audit/provenance, NEVER in node evidence.
458
+ if (agentDelegation) {
459
+ // Track 1 (tamper-evidence): bind this verdict into the append-only,
460
+ // hash-chained telemetry ledger BEFORE the audit event, so the event can
461
+ // cross-link the record hash. Editing the recorded verdict/usage later breaks
462
+ // the chain (verifyTelemetryLedger). Only when a verdict was computed.
463
+ const ledgerRecord = agentDelegation.usageAttestation
464
+ ? (0, telemetry_ledger_1.appendTelemetryAttestation)(run, {
465
+ workerId,
466
+ taskId: task.id,
467
+ promptDigest: agentDelegation.promptDigest,
468
+ reportedUsage: agentDelegation.reportedUsage,
469
+ usageSignature: agentDelegation.usageSignature,
470
+ attestation: agentDelegation.usageAttestation,
471
+ attestationReason: agentDelegation.usageAttestationReason
472
+ })
473
+ : undefined;
474
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
475
+ kind: "worker.agent-delegation",
476
+ decision: "recorded",
477
+ source: "host-attested",
478
+ workerId,
479
+ taskId: task.id,
480
+ nodeId: resultNode.id,
481
+ sandboxProfileId: scope.sandboxProfileId,
482
+ policySnapshot: scope.sandboxPolicy,
483
+ parentEventIds: [acceptedAudit.id],
484
+ metadata: {
485
+ backendId: agentDelegation.backendId,
486
+ handleKind: agentDelegation.handle.kind,
487
+ handleRef: agentDelegation.handle.ref,
488
+ model: agentDelegation.model,
489
+ promptDigest: agentDelegation.promptDigest,
490
+ resultDigest: agentDelegation.resultDigest,
491
+ command: agentDelegation.command,
492
+ args: agentDelegation.args,
493
+ exitCode: agentDelegation.exitCode,
494
+ // Track 1: the telemetry verdict travels with the agent-hop event so the
495
+ // audit report can surface `unattested` usage loudly. Absent ⇒ no usage.
496
+ ...(agentDelegation.usageAttestation
497
+ ? {
498
+ telemetryAttestation: agentDelegation.usageAttestation,
499
+ ...(agentDelegation.usageAttestationReason ? { telemetryAttestationReason: agentDelegation.usageAttestationReason } : {}),
500
+ ...(agentDelegation.reportedUsage ? { reportedUsage: agentDelegation.reportedUsage } : {}),
501
+ // Cross-link to the hash-chained ledger entry (tamper-evidence).
502
+ ...(ledgerRecord ? { telemetryRecordId: ledgerRecord.recordId, telemetryRecordHash: ledgerRecord.recordHash, telemetryPrevHash: ledgerRecord.prevHash } : {})
503
+ }
504
+ : {})
505
+ }
506
+ });
507
+ }
508
+ const verifierResult = (0, pipeline_runner_1.createPipelineRunner)({ persist: false }).runPipelineStage(run, "verify", resultNode.id, {
509
+ outputNodeId: `${run.id}:verifier:${task.id}`,
510
+ outputStatus: "verified",
511
+ loopStage: "adjust",
512
+ outputs: { accepted: true, workerId },
513
+ artifacts: [{ id: "result", kind: "markdown", path: destination }],
514
+ evidence: resultNode.evidence.length
515
+ ? resultNode.evidence
516
+ : [{ id: "result:summary", source: "summary", summary: parsedResult.summary }],
517
+ metadata: { taskId: task.id, workerId, resultNodeId: resultNode.id, sandboxProfileId: scope.sandboxProfileId }
518
+ });
519
+ task.verifierNodeId = verifierResult.outputNodeId;
520
+ const output = {
521
+ workerId,
522
+ taskId: task.id,
523
+ resultPath: absoluteResultPath,
524
+ recordedAt: new Date().toISOString(),
525
+ stateNodeId: resultNode.id,
526
+ verifierNodeId: verifierResult.outputNodeId,
527
+ auditEventIds: [pathAudit.id, acceptedAudit.id]
528
+ };
529
+ // Host-attested usage rides on the worker record. Recorded when the agent
530
+ // REPORTED a model OR token usage — `unreported`/absent stays ABSENT (never
531
+ // backfilled from the operator-chosen CW_AGENT_MODEL, never synthesized).
532
+ // Track 1: the attestation verdict (`attested`/`unattested`/`absent`) and its
533
+ // reason ride along, and the token buckets come from the (verified-or-not)
534
+ // reported usage — CW still never measures them, it records + labels them.
535
+ const reportedModel = agentDelegation && agentDelegation.model && agentDelegation.model !== "unreported" ? agentDelegation.model : undefined;
536
+ const usageRecord = agentDelegation && (reportedModel || agentDelegation.reportedUsage)
537
+ ? {
538
+ schemaVersion: 1,
539
+ source: "host-attested",
540
+ ...(reportedModel ? { model: reportedModel } : {}),
541
+ ...(0, telemetry_attestation_1.normalizeReportedUsage)(agentDelegation.reportedUsage),
542
+ attestedAt: new Date().toISOString(),
543
+ ...(telemetry ? { attestation: telemetry.status, ...(telemetry.reason ? { attestationReason: telemetry.reason } : {}) } : {}),
544
+ note: "agent-delegation host-attested usage"
545
+ }
546
+ : undefined;
547
+ updateWorkerScope(run, {
548
+ ...scope,
549
+ updatedAt: new Date().toISOString(),
550
+ status: verifierResult.status === "advanced" ? "verified" : "completed",
551
+ resultNodeId: resultNode.id,
552
+ output,
553
+ // Output integrity (v0.1.63): SHA256 digest + file size
554
+ outputDigest: (0, execution_backend_1.sha256)(rawResult),
555
+ outputSizeBytes: Buffer.byteLength(rawResult, "utf8"),
556
+ ...(usageRecord ? { usage: usageRecord } : {})
557
+ });
558
+ const blackboardLinks = publishWorkerOutputToBlackboard(run, scope, task, parsedResult.summary, destination, absoluteResultPath, resultNode.evidence, acceptedAudit.id);
559
+ (0, multi_agent_1.recordMultiAgentWorkerOutput)(run, {
560
+ workerId,
561
+ taskId: task.id,
562
+ resultNodeId: resultNode.id,
563
+ verifierNodeId: verifierResult.outputNodeId,
564
+ evidence: resultNode.evidence,
565
+ artifactPaths: [destination, absoluteResultPath],
566
+ blackboardMessageIds: blackboardLinks.messageIds,
567
+ blackboardArtifactRefIds: blackboardLinks.artifactRefIds
568
+ });
569
+ if (options.persist !== false)
570
+ (0, state_1.saveCheckpoint)(run);
571
+ return output;
572
+ }
573
+ function recordWorkerFailure(run, workerId, error, options = {}) {
574
+ const scope = requireWorkerScope(run, workerId);
575
+ const task = requireWorkerTask(run, scope);
576
+ const structured = normalizeWorkerError(error, scope, options);
577
+ const failureNodeId = `${run.id}:worker:${(0, state_1.safeFileName)(workerId)}:failure:${scope.errors.length + 1}`;
578
+ let failureNode = (0, state_node_1.recordNodeError)((0, state_node_1.createStateNode)({
579
+ id: failureNodeId,
580
+ kind: "error",
581
+ status: "pending",
582
+ loopStage: "adjust",
583
+ inputs: { workerId, taskId: task.id, dispatchId: scope.dispatchId },
584
+ artifacts: workerArtifacts(scope),
585
+ parents: task.stateNodeId ? [task.stateNodeId] : [],
586
+ contractId: pipeline_contract_1.DEFAULT_PIPELINE_CONTRACT_ID,
587
+ metadata: { workerId, taskId: task.id, dispatchId: scope.dispatchId, workerDir: scope.workerDir, sandboxProfileId: scope.sandboxProfileId }
588
+ }), structured);
589
+ if (task.stateNodeId) {
590
+ const parent = run.nodes?.find((candidate) => candidate.id === task.stateNodeId);
591
+ if (parent) {
592
+ const linked = (0, state_node_1.linkStateNodes)(parent, failureNode);
593
+ (0, state_node_1.appendRunNode)(run, linked[0]);
594
+ failureNode = linked[1];
595
+ }
596
+ }
597
+ (0, state_node_1.appendRunNode)(run, failureNode);
598
+ task.status = "failed";
599
+ task.loopStage = "adjust";
600
+ const feedback = (0, error_feedback_1.recordFeedback)(run, {
601
+ source: "pipeline-runner",
602
+ error: structured,
603
+ nodeId: failureNode.id,
604
+ taskId: task.id,
605
+ path: structured.path,
606
+ retryable: structured.retryable,
607
+ artifacts: failureNode.artifacts,
608
+ metadata: {
609
+ workerId,
610
+ dispatchId: scope.dispatchId,
611
+ workerDir: scope.workerDir,
612
+ sandboxProfileId: scope.sandboxProfileId,
613
+ sandboxPolicy: scope.sandboxPolicy,
614
+ allowedPaths: scope.allowedPaths,
615
+ details: structured.details
616
+ }
617
+ }, { persist: false });
618
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
619
+ kind: "worker.failure",
620
+ decision: structured.code === "worker-boundary-violation" || structured.code.startsWith("sandbox-") ? "denied" : "failed",
621
+ source: structured.code.startsWith("sandbox-") || structured.code === "worker-boundary-violation" ? "cw-validated" : "runtime-derived",
622
+ workerId,
623
+ taskId: task.id,
624
+ nodeId: failureNode.id,
625
+ feedbackIds: [feedback.id],
626
+ sandboxProfileId: scope.sandboxProfileId,
627
+ policySnapshot: scope.sandboxPolicy,
628
+ normalizedPath: structured.path,
629
+ metadata: {
630
+ code: structured.code,
631
+ dispatchId: scope.dispatchId
632
+ }
633
+ });
634
+ updateWorkerScope(run, {
635
+ ...scope,
636
+ updatedAt: new Date().toISOString(),
637
+ status: structured.code === "worker-boundary-violation" || structured.code.startsWith("sandbox-") ? "rejected" : "failed",
638
+ retryCount: typeof options.retryCount === "number" ? options.retryCount : scope.retryCount,
639
+ feedbackIds: unique([...(scope.feedbackIds || []), feedback.id]),
640
+ errors: [...(scope.errors || []), structured]
641
+ });
642
+ if (options.persist !== false)
643
+ (0, state_1.saveCheckpoint)(run);
644
+ return requireWorkerScope(run, workerId);
645
+ }
646
+ function recordWorkerRetryAttempt(run, workerId, attempts, reason, options = {}) {
647
+ const scope = requireWorkerScope(run, workerId);
648
+ const updated = updateWorkerScope(run, {
649
+ ...scope,
650
+ updatedAt: new Date().toISOString(),
651
+ retryCount: attempts,
652
+ metadata: compactMetadata({
653
+ ...scope.metadata,
654
+ agentDelegationAttempts: attempts,
655
+ agentDelegationLastFailure: reason
656
+ })
657
+ });
658
+ if (options.persist !== false)
659
+ (0, state_1.saveCheckpoint)(run);
660
+ return updated;
661
+ }
662
+ function validateWorkerBoundary(run, workerId, options = {}) {
663
+ const scope = requireWorkerScope(run, workerId);
664
+ const rawPath = String(options.path || scope.resultPath);
665
+ return (0, sandbox_profile_1.validateSandboxWrite)(sandboxPolicyForBoundary(run, scope, options), rawPath, workerId);
666
+ }
667
+ function summarizeWorkers(run) {
668
+ const workers = listWorkerScopes(run);
669
+ return {
670
+ total: workers.length,
671
+ byStatus: countBy(workers, (scope) => scope.status),
672
+ manifestPaths: workers.map(manifestPath),
673
+ failed: workers
674
+ .filter((scope) => scope.status === "failed" || scope.status === "rejected")
675
+ .map((scope) => ({ id: scope.id, status: scope.status, feedbackIds: scope.feedbackIds || [] }))
676
+ };
677
+ }
678
+ function reclaimOrphans(run, now) {
679
+ const nowMs = now ? Date.parse(now) : Date.now();
680
+ if (!Number.isFinite(nowMs))
681
+ throw new Error("Invalid reclaim 'now': " + String(now));
682
+ const orphans = [];
683
+ const activeStatuses = new Set(["allocated", "running"]);
684
+ for (const scope of run.workers || []) {
685
+ if (!activeStatuses.has(scope.status))
686
+ continue;
687
+ if (!scope.timeoutMs || scope.timeoutMs <= 0)
688
+ continue;
689
+ const createdAtMs = Date.parse(scope.createdAt);
690
+ if (!Number.isFinite(createdAtMs))
691
+ continue;
692
+ const elapsedMs = nowMs - createdAtMs;
693
+ if (elapsedMs < scope.timeoutMs)
694
+ continue;
695
+ scope.status = "orphaned";
696
+ scope.updatedAt = new Date(nowMs).toISOString();
697
+ scope.errors.push({
698
+ code: "worker-orphaned",
699
+ message: `Worker exceeded timeout of ${scope.timeoutMs}ms (elapsed: ${elapsedMs}ms).`,
700
+ at: new Date(nowMs).toISOString(),
701
+ retryable: true
702
+ });
703
+ upsertWorkerScope(run, scope);
704
+ orphans.push({ workerId: scope.id, taskId: scope.taskId, elapsedMs, timeoutMs: scope.timeoutMs });
705
+ }
706
+ if (orphans.length) {
707
+ writeWorkerIndex(run);
708
+ saveWorkerCheckpoint(run);
709
+ }
710
+ return { runId: run.id, reclaimed: orphans.length, orphans };
711
+ }
712
+ function saveWorkerCheckpoint(run) {
713
+ // Durable write via atomic temp+rename (same contract as saveCheckpoint)
714
+ // For worker index, the atomic write in writeWorkerIndex already handles it.
715
+ // This is a no-op wrapper that signals the checkpoint boundary.
716
+ }
717
+ function ensureWorkerState(run) {
718
+ run.paths.workersDir = run.paths.workersDir || node_path_1.default.join(run.paths.runDir, "workers");
719
+ node_fs_1.default.mkdirSync(run.paths.workersDir, { recursive: true });
720
+ run.workers = run.workers || [];
721
+ }
722
+ function writeWorkerInput(run, task, scope) {
723
+ const lines = [
724
+ `# Worker ${scope.id}`,
725
+ "",
726
+ `- Run: ${run.id}`,
727
+ `- Task: ${task.id}`,
728
+ `- Dispatch: ${scope.dispatchId || ""}`,
729
+ `- Result: ${scope.resultPath}`,
730
+ `- Artifacts: ${scope.artifactsDir}`,
731
+ `- Logs: ${scope.logsDir}`,
732
+ `- Sandbox Profile: ${scope.sandboxProfileId || sandbox_profile_1.DEFAULT_SANDBOX_PROFILE_ID}`,
733
+ ...(scope.multiAgent
734
+ ? [
735
+ `- Multi-Agent Run: ${scope.multiAgent.runId}`,
736
+ `- Agent Group: ${scope.multiAgent.groupId}`,
737
+ `- Agent Role: ${scope.multiAgent.roleId}`,
738
+ `- Agent Membership: ${scope.multiAgent.membershipId || ""}`,
739
+ `- Agent Fanout: ${scope.multiAgent.fanoutId || ""}`
740
+ ]
741
+ : []),
742
+ "",
743
+ "## Task",
744
+ "",
745
+ task.prompt,
746
+ "",
747
+ "## Boundary",
748
+ "",
749
+ "- Write the final Markdown result to result.md.",
750
+ "- Keep extra files under artifacts/ or logs/.",
751
+ `- Read paths: ${(scope.sandboxPolicy?.readPaths || []).join(", ") || "none"}.`,
752
+ `- Write paths: ${(0, sandbox_profile_1.effectiveSandboxWritePaths)(sandboxPolicyForBoundary(run, scope)).join(", ") || "none"}.`,
753
+ "- CW enforces result acceptance. The host is responsible for OS/process/network/environment sandbox enforcement.",
754
+ "- Do not mutate state.json, nodes/, feedback/, dispatches/, or commits/ directly.",
755
+ ""
756
+ ];
757
+ node_fs_1.default.writeFileSync(scope.inputPath, lines.join("\n"), "utf8");
758
+ }
759
+ function upsertWorkerScope(run, scope) {
760
+ ensureWorkerState(run);
761
+ const scopes = run.workers || [];
762
+ const index = scopes.findIndex((candidate) => candidate.id === scope.id);
763
+ run.workers = index >= 0 ? scopes.map((candidate) => (candidate.id === scope.id ? scope : candidate)) : [...scopes, scope];
764
+ writeWorkerScope(scope);
765
+ return scope;
766
+ }
767
+ function updateWorkerScope(run, scope) {
768
+ const updated = upsertWorkerScope(run, scope);
769
+ writeWorkerManifest(run, updated);
770
+ writeWorkerIndex(run);
771
+ return updated;
772
+ }
773
+ function writeWorkerScope(scope) {
774
+ (0, state_1.writeJson)(workerScopePath(scope), scope);
775
+ }
776
+ function writeWorkerIndex(run) {
777
+ ensureWorkerState(run);
778
+ (0, state_1.writeJson)(node_path_1.default.join(workerRoot(run), "index.json"), {
779
+ schemaVersion: exports.WORKER_ISOLATION_SCHEMA_VERSION,
780
+ runId: run.id,
781
+ workers: (run.workers || []).map((scope) => ({
782
+ id: scope.id,
783
+ taskId: scope.taskId,
784
+ dispatchId: scope.dispatchId,
785
+ status: scope.status,
786
+ workerDir: scope.workerDir,
787
+ manifestPath: manifestPath(scope),
788
+ resultPath: scope.resultPath,
789
+ sandboxProfileId: scope.sandboxProfileId,
790
+ backendId: scope.backendId,
791
+ multiAgent: scope.multiAgent,
792
+ feedbackIds: scope.feedbackIds
793
+ }))
794
+ });
795
+ }
796
+ function loadWorkerScopesFromDisk(run) {
797
+ ensureWorkerState(run);
798
+ if (!node_fs_1.default.existsSync(workerRoot(run)))
799
+ return [];
800
+ return node_fs_1.default
801
+ .readdirSync(workerRoot(run), { withFileTypes: true })
802
+ .filter((entry) => entry.isDirectory())
803
+ .map((entry) => node_path_1.default.join(workerRoot(run), entry.name, WORKER_SCOPE_FILE))
804
+ .filter((file) => node_fs_1.default.existsSync(file))
805
+ .map((file) => JSON.parse(node_fs_1.default.readFileSync(file, "utf8")));
806
+ }
807
+ function requireWorkerScope(run, workerId) {
808
+ const scope = getWorkerScope(run, workerId);
809
+ if (!scope)
810
+ throw new Error(`Unknown worker for run ${run.id}: ${workerId}`);
811
+ return scope;
812
+ }
813
+ function requireWorkerTask(run, scope) {
814
+ const task = run.tasks.find((candidate) => candidate.id === scope.taskId);
815
+ if (!task)
816
+ throw new Error(`Unknown task for worker ${scope.id}: ${scope.taskId}`);
817
+ return task;
818
+ }
819
+ function workerRoot(run) {
820
+ return run.paths.workersDir || node_path_1.default.join(run.paths.runDir, "workers");
821
+ }
822
+ function sandboxPolicyForBoundary(run, scope, options = {}) {
823
+ if (scope.sandboxPolicy && !options.policy && !options.sandboxProfileId)
824
+ return scope.sandboxPolicy;
825
+ const profileId = options.sandboxProfileId || options.policy?.sandboxProfileId || scope.sandboxProfileId || sandbox_profile_1.DEFAULT_SANDBOX_PROFILE_ID;
826
+ return (0, sandbox_profile_1.sandboxPolicyForWorker)(profileId, {
827
+ cwd: run.cwd,
828
+ runDir: run.paths.runDir,
829
+ workerDir: scope.workerDir,
830
+ inputPath: scope.inputPath,
831
+ resultPath: scope.resultPath,
832
+ artifactsDir: scope.artifactsDir,
833
+ logsDir: scope.logsDir,
834
+ extraReadPaths: options.policy?.readPaths || [],
835
+ extraWritePaths: [
836
+ ...(options.policy?.writePaths || []),
837
+ ...(options.policy?.allowedPaths || []),
838
+ ...(!scope.sandboxPolicy ? scope.allowedPaths || [] : [])
839
+ ],
840
+ allowArtifacts: options.policy?.allowArtifacts,
841
+ allowLogs: options.policy?.allowLogs
842
+ });
843
+ }
844
+ function blackboardManifest(run, scope) {
845
+ const linkage = blackboardLinkage(run, scope);
846
+ if (!linkage.blackboardId)
847
+ return undefined;
848
+ const root = run.paths.blackboardDir || node_path_1.default.join(run.paths.runDir, "blackboard");
849
+ return {
850
+ id: linkage.blackboardId,
851
+ topicIds: linkage.topicIds,
852
+ indexPath: node_path_1.default.join(root, "index.json"),
853
+ messagesPath: node_path_1.default.join(root, "messages.jsonl"),
854
+ topicsDir: node_path_1.default.join(root, "topics"),
855
+ contextsDir: node_path_1.default.join(root, "contexts"),
856
+ artifactsDir: node_path_1.default.join(root, "artifacts"),
857
+ instructions: [
858
+ "Use the blackboard as shared coordination context.",
859
+ "Read index.json and the relevant topic/context/artifact files before synthesizing.",
860
+ "Cite blackboard artifact refs or message refs in result evidence when relevant.",
861
+ "Do not edit blackboard files directly; CW records accepted worker output into the blackboard."
862
+ ]
863
+ };
864
+ }
865
+ function publishWorkerOutputToBlackboard(run, scope, task, summary, destination, workerResultPath, evidence, acceptedAuditId) {
866
+ const linkage = blackboardLinkage(run, scope);
867
+ if (!linkage.blackboardId || !linkage.topicIds.length)
868
+ return { messageIds: [], artifactRefIds: [] };
869
+ const topicId = linkage.topicIds[0];
870
+ const artifactRefs = [
871
+ (0, coordinator_1.addBlackboardArtifact)(run, {
872
+ topicId,
873
+ blackboardId: linkage.blackboardId,
874
+ kind: "worker-result",
875
+ path: destination,
876
+ owner: { kind: "worker", id: scope.id },
877
+ author: { kind: "runtime", id: "cw" },
878
+ source: "cw-validated-worker-output",
879
+ provenance: {
880
+ workerId: scope.id,
881
+ taskId: task.id,
882
+ multiAgentRunId: scope.multiAgent?.runId,
883
+ agentGroupId: scope.multiAgent?.groupId,
884
+ agentRoleId: scope.multiAgent?.roleId,
885
+ agentMembershipId: scope.multiAgent?.membershipId,
886
+ auditEventIds: [acceptedAuditId]
887
+ },
888
+ evidenceRefs: evidence.map((entry) => entry.locator || entry.path || entry.summary || entry.id).filter(Boolean),
889
+ auditEventIds: [acceptedAuditId],
890
+ metadata: { workerResultPath }
891
+ })
892
+ ];
893
+ const message = (0, coordinator_1.postBlackboardMessage)(run, {
894
+ topicId,
895
+ blackboardId: linkage.blackboardId,
896
+ body: summary,
897
+ author: { kind: "worker", id: scope.id },
898
+ scope: { kind: "worker", id: scope.id },
899
+ artifactRefIds: artifactRefs.map((artifact) => artifact.id),
900
+ evidenceRefs: evidence.map((entry) => entry.locator || entry.path || entry.summary || entry.id).filter(Boolean),
901
+ auditEventIds: [acceptedAuditId],
902
+ links: {
903
+ multiAgentRunId: scope.multiAgent?.runId,
904
+ agentGroupId: scope.multiAgent?.groupId,
905
+ agentRoleId: scope.multiAgent?.roleId,
906
+ agentMembershipId: scope.multiAgent?.membershipId,
907
+ agentFanoutId: scope.multiAgent?.fanoutId,
908
+ taskId: task.id,
909
+ workerId: scope.id,
910
+ auditEventIds: [acceptedAuditId]
911
+ },
912
+ metadata: {
913
+ taskId: task.id,
914
+ resultPath: destination,
915
+ multiAgent: scope.multiAgent
916
+ }
917
+ });
918
+ return {
919
+ messageIds: [message.id],
920
+ artifactRefIds: artifactRefs.map((artifact) => artifact.id)
921
+ };
922
+ }
923
+ function blackboardLinkage(run, scope) {
924
+ const membershipId = scope.multiAgent?.membershipId;
925
+ const membership = membershipId ? run.multiAgent?.memberships.find((entry) => entry.id === membershipId) : undefined;
926
+ const group = scope.multiAgent?.groupId ? run.multiAgent?.groups.find((entry) => entry.id === scope.multiAgent?.groupId) : undefined;
927
+ const role = scope.multiAgent?.roleId ? run.multiAgent?.roles.find((entry) => entry.id === scope.multiAgent?.roleId) : undefined;
928
+ const multiAgentRun = scope.multiAgent?.runId ? run.multiAgent?.runs.find((entry) => entry.id === scope.multiAgent?.runId) : undefined;
929
+ const blackboardId = membership?.blackboardId || group?.blackboardId || role?.blackboardId || multiAgentRun?.blackboardId;
930
+ const topicIds = unique([
931
+ ...(membership?.topicIds || []),
932
+ ...(group?.topicIds || []),
933
+ ...(role?.topicIds || []),
934
+ ...(multiAgentRun?.topicIds || [])
935
+ ]);
936
+ return { blackboardId, topicIds };
937
+ }
938
+ function manifestPath(scope) {
939
+ return node_path_1.default.join(scope.workerDir, WORKER_MANIFEST_FILE);
940
+ }
941
+ function workerScopePath(scope) {
942
+ return node_path_1.default.join(scope.workerDir, WORKER_SCOPE_FILE);
943
+ }
944
+ // Deterministic worker id (v0.1.40 self-audit P2): a wall-clock stamp + Math.random()
945
+ // made every dispatch mint a different id, so audit references were not reproducible
946
+ // across re-runs of the same inputs. The id is now derived from the task plus a
947
+ // per-task sequence (count of worker scopes already allocated for that task + 1),
948
+ // so re-running the same workflow yields byte-identical worker ids while retries of
949
+ // the SAME task still get a fresh, unique id. (workerId is excluded from the
950
+ // snapshot source fingerprint, so this does not change replay digests.)
951
+ function createWorkerId(run, taskId) {
952
+ const prefix = `worker-${(0, state_1.safeFileName)(taskId)}-`;
953
+ const seq = (run.workers || []).filter((scope) => scope.id.startsWith(prefix)).length + 1;
954
+ return `${prefix}${String(seq).padStart(4, "0")}`;
955
+ }
956
+ function workerArtifacts(scope) {
957
+ return [
958
+ { id: "worker", kind: "json", path: workerScopePath(scope) },
959
+ { id: "worker-manifest", kind: "json", path: manifestPath(scope) },
960
+ { id: "worker-input", kind: "markdown", path: scope.inputPath }
961
+ ];
962
+ }
963
+ function normalizeWorkerError(error, scope, options) {
964
+ if (isBoundaryViolation(error)) {
965
+ return structuredError(error.code, error.message, {
966
+ path: error.path,
967
+ retryable: false,
968
+ details: { allowedPaths: error.allowedPaths, workerId: scope.id, taskId: scope.taskId, sandboxProfileId: scope.sandboxProfileId }
969
+ });
970
+ }
971
+ if (isStateNodeError(error)) {
972
+ return {
973
+ ...error,
974
+ at: error.at || new Date().toISOString(),
975
+ path: options.path || error.path,
976
+ retryable: options.retryable ?? error.retryable ?? false,
977
+ details: compactMetadata({ ...(error.details || {}), workerId: scope.id, taskId: scope.taskId })
978
+ };
979
+ }
980
+ const message = error instanceof Error ? error.message : String(error);
981
+ return structuredError(options.code || "worker-runtime-error", message, {
982
+ path: options.path,
983
+ retryable: options.retryable ?? false,
984
+ details: { workerId: scope.id, taskId: scope.taskId }
985
+ });
986
+ }
987
+ function structuredError(code, message, options = {}) {
988
+ return {
989
+ code,
990
+ message,
991
+ at: new Date().toISOString(),
992
+ path: options.path,
993
+ retryable: options.retryable,
994
+ details: options.details
995
+ };
996
+ }
997
+ function isBoundaryViolation(value) {
998
+ return Boolean(value && typeof value === "object" && "allowedPaths" in value && "message" in value);
999
+ }
1000
+ function isStateNodeError(value) {
1001
+ return Boolean(value && typeof value === "object" && "code" in value && "message" in value);
1002
+ }
1003
+ function mergeScopes(left, right) {
1004
+ const merged = [...left];
1005
+ for (const scope of right) {
1006
+ const index = merged.findIndex((candidate) => candidate.id === scope.id);
1007
+ if (index >= 0)
1008
+ merged[index] = scope;
1009
+ else
1010
+ merged.push(scope);
1011
+ }
1012
+ return merged;
1013
+ }
1014
+ function unique(values) {
1015
+ return Array.from(new Set(values.filter(Boolean)));
1016
+ }
1017
+ function compactMetadata(value) {
1018
+ const entries = Object.entries(value).filter(([, entry]) => entry !== undefined);
1019
+ return entries.length ? Object.fromEntries(entries) : undefined;
1020
+ }
1021
+ function countBy(items, key) {
1022
+ const counts = {};
1023
+ for (const item of items) {
1024
+ const value = key(item);
1025
+ counts[value] = (counts[value] || 0) + 1;
1026
+ }
1027
+ return counts;
1028
+ }