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,715 @@
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.CANDIDATE_SCHEMA_VERSION = void 0;
7
+ exports.createCandidateScoring = createCandidateScoring;
8
+ exports.registerCandidate = registerCandidate;
9
+ exports.listCandidates = listCandidates;
10
+ exports.getCandidate = getCandidate;
11
+ exports.scoreCandidate = scoreCandidate;
12
+ exports.rankCandidates = rankCandidates;
13
+ exports.selectCandidate = selectCandidate;
14
+ exports.rejectCandidate = rejectCandidate;
15
+ exports.summarizeCandidates = summarizeCandidates;
16
+ const node_fs_1 = __importDefault(require("node:fs"));
17
+ const node_path_1 = __importDefault(require("node:path"));
18
+ const error_feedback_1 = require("./error-feedback");
19
+ const state_1 = require("./state");
20
+ const state_node_1 = require("./state-node");
21
+ const trust_audit_1 = require("./trust-audit");
22
+ const collaboration_1 = require("./collaboration");
23
+ exports.CANDIDATE_SCHEMA_VERSION = 1;
24
+ function createCandidateScoring(options = {}) {
25
+ return {
26
+ registerCandidate: (run, input) => registerCandidate(run, input, options),
27
+ listCandidates: (run, listOptions) => listCandidates(run, listOptions),
28
+ getCandidate,
29
+ scoreCandidate: (run, candidateId, input) => scoreCandidate(run, candidateId, input, options),
30
+ rankCandidates,
31
+ selectCandidate: (run, candidateId, selectOptions) => selectCandidate(run, candidateId, selectOptions, options),
32
+ rejectCandidate: (run, candidateId, reason) => rejectCandidate(run, candidateId, reason, options),
33
+ summarizeCandidates
34
+ };
35
+ }
36
+ function registerCandidate(run, input, options = {}) {
37
+ ensureCandidateState(run);
38
+ const existing = input.id ? getCandidate(run, input.id) : undefined;
39
+ if (existing)
40
+ return existing;
41
+ const now = new Date().toISOString();
42
+ const id = input.id || createCandidateId(input.kind || "manual", input.workerId || input.taskId || input.resultNodeId);
43
+ const candidate = {
44
+ schemaVersion: exports.CANDIDATE_SCHEMA_VERSION,
45
+ id,
46
+ runId: run.id,
47
+ kind: input.kind || inferCandidateKind(input),
48
+ status: "registered",
49
+ createdAt: now,
50
+ updatedAt: now,
51
+ workerId: input.workerId,
52
+ taskId: input.taskId,
53
+ resultNodeId: input.resultNodeId,
54
+ verifierNodeId: input.verifierNodeId,
55
+ resultPath: input.resultPath,
56
+ artifacts: input.artifacts || artifactsFromInput(input),
57
+ evidence: (0, trust_audit_1.normalizeEvidence)(run, input.evidence || evidenceFromInput(run, input), {
58
+ source: input.workerId ? "cw-validated" : "operator-recorded",
59
+ workerId: input.workerId,
60
+ taskId: input.taskId,
61
+ resultNodeId: input.resultNodeId,
62
+ verifierNodeId: input.verifierNodeId,
63
+ candidateId: id
64
+ }),
65
+ scores: [],
66
+ feedbackIds: [],
67
+ metadata: compactMetadata(input.metadata || {})
68
+ };
69
+ upsertCandidate(run, candidate);
70
+ (0, trust_audit_1.recordTrustAuditEvent)(run, {
71
+ kind: "candidate.register",
72
+ decision: "recorded",
73
+ source: input.workerId ? "cw-validated" : "operator-recorded",
74
+ workerId: input.workerId,
75
+ taskId: input.taskId,
76
+ nodeId: input.resultNodeId,
77
+ candidateId: candidate.id,
78
+ evidence: candidate.evidence,
79
+ metadata: { kind: candidate.kind, verifierNodeId: candidate.verifierNodeId }
80
+ });
81
+ appendCandidateNode(run, candidate, "registered");
82
+ if (shouldPersist(options))
83
+ (0, state_1.saveCheckpoint)(run);
84
+ return candidate;
85
+ }
86
+ function listCandidates(run, options = {}) {
87
+ ensureCandidateState(run);
88
+ const loaded = loadCandidatesFromDisk(run);
89
+ run.candidates = mergeCandidates(run.candidates || [], loaded);
90
+ return (run.candidates || []).filter((candidate) => {
91
+ if (options.status && candidate.status !== options.status)
92
+ return false;
93
+ if (options.kind && candidate.kind !== options.kind)
94
+ return false;
95
+ return true;
96
+ });
97
+ }
98
+ function getCandidate(run, candidateId) {
99
+ ensureCandidateState(run);
100
+ const existing = (run.candidates || []).find((candidate) => candidate.id === candidateId);
101
+ if (existing)
102
+ return existing;
103
+ const file = candidateFile(run, candidateId);
104
+ if (!node_fs_1.default.existsSync(file))
105
+ return undefined;
106
+ const candidate = JSON.parse(node_fs_1.default.readFileSync(file, "utf8"));
107
+ upsertCandidate(run, candidate);
108
+ return candidate;
109
+ }
110
+ function scoreCandidate(run, candidateId, input, options = {}) {
111
+ const candidate = requireCandidate(run, candidateId);
112
+ const scoreId = input.id || createScoreId(candidateId);
113
+ const evidence = (0, trust_audit_1.normalizeEvidence)(run, input.evidence || [], {
114
+ source: "operator-recorded",
115
+ candidateId,
116
+ scoreId
117
+ });
118
+ const policy = mergePolicy(options.policy);
119
+ if (policy.requireEvidence && !evidence.length) {
120
+ const feedback = recordCandidateFailure(run, candidate, "candidate-score-missing-evidence", {
121
+ message: `Candidate ${candidateId} score requires evidence`,
122
+ retryable: true
123
+ });
124
+ updateCandidate(run, {
125
+ ...candidate,
126
+ updatedAt: new Date().toISOString(),
127
+ status: "failed",
128
+ feedbackIds: unique([...(candidate.feedbackIds || []), feedback.id])
129
+ });
130
+ throw new Error(`Candidate ${candidateId} score requires evidence`);
131
+ }
132
+ const total = sumCriteria(input.criteria);
133
+ const maxTotal = input.maxTotal ?? Math.max(total, 1);
134
+ const normalized = maxTotal > 0 ? clamp(total / maxTotal, 0, 1) : 0;
135
+ const score = {
136
+ schemaVersion: exports.CANDIDATE_SCHEMA_VERSION,
137
+ id: scoreId,
138
+ candidateId,
139
+ runId: run.id,
140
+ createdAt: new Date().toISOString(),
141
+ scorer: input.scorer || "operator",
142
+ criteria: input.criteria,
143
+ total,
144
+ maxTotal,
145
+ normalized,
146
+ verdict: input.verdict || verdictFor(normalized, policy),
147
+ evidence,
148
+ artifacts: input.artifacts || [],
149
+ notes: input.notes,
150
+ metadata: compactMetadata(input.metadata || {})
151
+ };
152
+ writeScore(run, candidateId, score);
153
+ const updated = updateCandidate(run, {
154
+ ...candidate,
155
+ updatedAt: new Date().toISOString(),
156
+ status: score.verdict === "fail" ? "failed" : "scored",
157
+ scores: unique([...(candidate.scores || []), score.id]),
158
+ evidence: mergeById(candidate.evidence, evidence),
159
+ artifacts: mergeById(candidate.artifacts, score.artifacts)
160
+ });
161
+ const scoreAudit = (0, trust_audit_1.recordTrustAuditEvent)(run, {
162
+ kind: "candidate.score",
163
+ decision: score.verdict === "fail" ? "rejected" : "accepted",
164
+ source: "operator-recorded",
165
+ candidateId,
166
+ scoreId: score.id,
167
+ workerId: candidate.workerId,
168
+ taskId: candidate.taskId,
169
+ nodeId: candidate.verifierNodeId || candidate.resultNodeId,
170
+ evidence: score.evidence,
171
+ metadata: { criteria: score.criteria, normalized: score.normalized, verdict: score.verdict }
172
+ });
173
+ score.evidence = (0, trust_audit_1.normalizeEvidence)(run, score.evidence, {
174
+ source: "operator-recorded",
175
+ candidateId,
176
+ scoreId: score.id,
177
+ auditEventIds: [scoreAudit.id]
178
+ });
179
+ writeScore(run, candidateId, score);
180
+ appendCandidateNode(run, updated, "scored", score);
181
+ writeCandidateIndex(run);
182
+ if (shouldPersist(options))
183
+ (0, state_1.saveCheckpoint)(run);
184
+ return score;
185
+ }
186
+ function rankCandidates(run, options = {}) {
187
+ const policy = mergePolicy(options.policy);
188
+ const rankable = listCandidates(run).filter((candidate) => options.includeRejected || candidate.status !== "rejected");
189
+ const rows = rankable.map((candidate) => {
190
+ const scores = readScores(run, candidate.id);
191
+ const best = bestScore(scores);
192
+ return {
193
+ candidate,
194
+ best,
195
+ normalized: best?.normalized ?? 0
196
+ };
197
+ });
198
+ rows.sort((left, right) => compareRows(left, right, policy));
199
+ const candidates = rows.map((row, index) => ({
200
+ candidateId: row.candidate.id,
201
+ status: row.candidate.status,
202
+ scoreCount: row.candidate.scores.length,
203
+ bestScoreId: row.best?.id,
204
+ normalized: row.normalized,
205
+ verdict: row.best?.verdict,
206
+ rank: index + 1
207
+ }));
208
+ const ranking = {
209
+ schemaVersion: exports.CANDIDATE_SCHEMA_VERSION,
210
+ runId: run.id,
211
+ createdAt: new Date().toISOString(),
212
+ policy,
213
+ candidates,
214
+ ties: detectTies(candidates)
215
+ };
216
+ (0, state_1.writeJson)(rankingPath(run), ranking);
217
+ return ranking;
218
+ }
219
+ function selectCandidate(run, candidateId, options = {}, scoringOptions = {}) {
220
+ const candidate = requireCandidate(run, candidateId);
221
+ const policy = mergePolicy(scoringOptions.policy);
222
+ const ranking = rankCandidates(run, { policy });
223
+ const ranked = ranking.candidates.find((entry) => entry.candidateId === candidateId);
224
+ const verifierNode = candidate.verifierNodeId
225
+ ? run.nodes?.find((node) => node.id === candidate.verifierNodeId)
226
+ : undefined;
227
+ const bestScore = options.scoreId
228
+ ? readScores(run, candidateId).find((score) => score.id === options.scoreId)
229
+ : readScores(run, candidateId).find((score) => score.id === ranked?.bestScoreId);
230
+ const failures = [];
231
+ if (candidate.status === "rejected" || candidate.status === "failed") {
232
+ failures.push(error("candidate-not-selectable", `Candidate ${candidateId} is ${candidate.status}`));
233
+ }
234
+ if (policy.requireVerifierGate && !options.allowUnverified) {
235
+ if (!verifierNode || verifierNode.status !== "verified") {
236
+ failures.push(error("candidate-selection-missing-verifier", `Candidate ${candidateId} requires a verified verifier node`));
237
+ }
238
+ else if (!verifierNode.evidence.length) {
239
+ failures.push(error("candidate-selection-missing-evidence", `Candidate ${candidateId} verifier node has no evidence`));
240
+ }
241
+ else if (emptyCaptureWarning(run, verifierNode)) {
242
+ // HARD no-false-green gate (v0.1.43) — kept in SYNC with the commit gate
243
+ // (commit.ts emptyCaptureWarning): a verifier node whose backing result was
244
+ // an empty-capture must not be selectable, so selection + commit agree.
245
+ failures.push(error("candidate-selection-empty-capture", `Candidate ${candidateId} verifier node has no real evidence (empty-capture result)`));
246
+ }
247
+ }
248
+ if (policy.minNormalized !== undefined && (bestScore?.normalized ?? 0) < policy.minNormalized) {
249
+ failures.push(error("candidate-selection-score-below-threshold", `Candidate ${candidateId} score is below threshold`, {
250
+ details: { normalized: bestScore?.normalized ?? 0, minNormalized: policy.minNormalized }
251
+ }));
252
+ }
253
+ // REVIEW GATE on selection — POLICY layered on the verifier gate above, never
254
+ // replacing it. Empty unless a review policy applies to "selection"; fails
255
+ // closed when required approvals from authorized roles are missing.
256
+ for (const reviewError of (0, collaboration_1.reviewGateErrors)(run, {
257
+ targetKind: "selection",
258
+ candidateId,
259
+ selfActorIds: (0, collaboration_1.selfActorIdsForCandidate)(run, candidateId)
260
+ })) {
261
+ failures.push(reviewError);
262
+ }
263
+ if (failures.length) {
264
+ const feedbackIds = failures.map((failure) => recordCandidateFailure(run, candidate, failure.code, {
265
+ message: failure.message,
266
+ retryable: false,
267
+ details: failure.details
268
+ }).id);
269
+ updateCandidate(run, {
270
+ ...candidate,
271
+ updatedAt: new Date().toISOString(),
272
+ status: "failed",
273
+ feedbackIds: unique([...(candidate.feedbackIds || []), ...feedbackIds])
274
+ });
275
+ if (shouldPersist(scoringOptions))
276
+ (0, state_1.saveCheckpoint)(run);
277
+ throw new Error(failures.map((failure) => failure.message).join("; "));
278
+ }
279
+ const now = new Date().toISOString();
280
+ const selection = {
281
+ schemaVersion: exports.CANDIDATE_SCHEMA_VERSION,
282
+ id: createSelectionId(candidateId),
283
+ runId: run.id,
284
+ candidateId,
285
+ selectedAt: now,
286
+ selectedBy: options.selectedBy || "operator",
287
+ verifierNodeId: candidate.verifierNodeId,
288
+ scoreId: bestScore?.id,
289
+ rankingPath: options.rankingPath || rankingPath(run),
290
+ reason: options.reason || "selected candidate",
291
+ evidence: (0, trust_audit_1.normalizeEvidence)(run, mergeEvidence(candidate.evidence, verifierNode?.evidence || []), {
292
+ source: "cw-validated",
293
+ workerId: candidate.workerId,
294
+ taskId: candidate.taskId,
295
+ resultNodeId: candidate.resultNodeId,
296
+ verifierNodeId: candidate.verifierNodeId,
297
+ candidateId,
298
+ scoreId: bestScore?.id
299
+ }),
300
+ artifacts: candidate.artifacts,
301
+ feedbackIds: [],
302
+ acceptanceRationale: (0, trust_audit_1.buildAcceptanceRationale)({
303
+ selectedCandidateId: candidateId,
304
+ scoreId: bestScore?.id,
305
+ scoreCriteria: bestScore?.criteria,
306
+ verifierNodeId: candidate.verifierNodeId,
307
+ evidenceCount: mergeEvidence(candidate.evidence, verifierNode?.evidence || []).length,
308
+ sandboxProfileId: sandboxProfileForCandidate(run, candidate),
309
+ workerId: candidate.workerId,
310
+ commitGateResult: "passed"
311
+ }),
312
+ metadata: compactMetadata({
313
+ ...(options.metadata || {}),
314
+ rank: ranked?.rank,
315
+ normalized: bestScore?.normalized
316
+ })
317
+ };
318
+ const selectionAudit = (0, trust_audit_1.recordTrustAuditEvent)(run, {
319
+ kind: "candidate.selection",
320
+ decision: "accepted",
321
+ source: "cw-validated",
322
+ workerId: candidate.workerId,
323
+ taskId: candidate.taskId,
324
+ nodeId: candidate.verifierNodeId,
325
+ candidateId,
326
+ scoreId: bestScore?.id,
327
+ selectionId: selection.id,
328
+ sandboxProfileId: selection.acceptanceRationale?.sandboxProfileId,
329
+ evidence: selection.evidence,
330
+ metadata: selection.acceptanceRationale
331
+ });
332
+ selection.evidence = (0, trust_audit_1.normalizeEvidence)(run, selection.evidence, {
333
+ source: "cw-validated",
334
+ workerId: candidate.workerId,
335
+ taskId: candidate.taskId,
336
+ resultNodeId: candidate.resultNodeId,
337
+ verifierNodeId: candidate.verifierNodeId,
338
+ candidateId,
339
+ scoreId: bestScore?.id,
340
+ selectionId: selection.id,
341
+ auditEventIds: [selectionAudit.id]
342
+ });
343
+ selection.acceptanceRationale = (0, trust_audit_1.buildAcceptanceRationale)({
344
+ ...selection.acceptanceRationale,
345
+ auditEventIds: [selectionAudit.id]
346
+ });
347
+ run.candidateSelections = [...(run.candidateSelections || []), selection];
348
+ writeSelection(run, selection);
349
+ const updated = updateCandidate(run, {
350
+ ...candidate,
351
+ updatedAt: now,
352
+ status: verifierNode?.status === "verified" ? "verified" : "selected",
353
+ selectedAt: now,
354
+ evidence: selection.evidence
355
+ });
356
+ appendSelectionNode(run, updated, selection);
357
+ writeCandidateIndex(run);
358
+ if (shouldPersist(scoringOptions))
359
+ (0, state_1.saveCheckpoint)(run);
360
+ return selection;
361
+ }
362
+ function rejectCandidate(run, candidateId, reason, options = {}) {
363
+ const candidate = requireCandidate(run, candidateId);
364
+ const feedback = recordCandidateFailure(run, candidate, "candidate-rejected", {
365
+ message: reason || `Candidate ${candidateId} rejected`,
366
+ retryable: false
367
+ });
368
+ const updated = updateCandidate(run, {
369
+ ...candidate,
370
+ updatedAt: new Date().toISOString(),
371
+ status: "rejected",
372
+ rejectedAt: new Date().toISOString(),
373
+ feedbackIds: unique([...(candidate.feedbackIds || []), feedback.id])
374
+ });
375
+ appendCandidateNode(run, updated, "rejected");
376
+ if (shouldPersist(options))
377
+ (0, state_1.saveCheckpoint)(run);
378
+ return updated;
379
+ }
380
+ function summarizeCandidates(run) {
381
+ const candidates = listCandidates(run);
382
+ return {
383
+ total: candidates.length,
384
+ byStatus: countBy(candidates, (candidate) => candidate.status),
385
+ byKind: countBy(candidates, (candidate) => candidate.kind),
386
+ indexPath: indexPath(run),
387
+ rankingPath: rankingPath(run),
388
+ selections: (run.candidateSelections || []).length
389
+ };
390
+ }
391
+ function ensureCandidateState(run) {
392
+ run.paths.candidatesDir = run.paths.candidatesDir || node_path_1.default.join(run.paths.runDir, "candidates");
393
+ node_fs_1.default.mkdirSync(run.paths.candidatesDir, { recursive: true });
394
+ run.candidates = run.candidates || [];
395
+ run.candidateSelections = run.candidateSelections || [];
396
+ }
397
+ function upsertCandidate(run, candidate) {
398
+ ensureCandidateState(run);
399
+ const candidates = run.candidates || [];
400
+ const index = candidates.findIndex((entry) => entry.id === candidate.id);
401
+ run.candidates = index >= 0 ? candidates.map((entry) => (entry.id === candidate.id ? candidate : entry)) : [...candidates, candidate];
402
+ writeCandidate(run, candidate);
403
+ writeCandidateIndex(run);
404
+ return candidate;
405
+ }
406
+ function updateCandidate(run, candidate) {
407
+ return upsertCandidate(run, candidate);
408
+ }
409
+ function requireCandidate(run, candidateId) {
410
+ const candidate = getCandidate(run, candidateId);
411
+ if (!candidate)
412
+ throw new Error(`Unknown candidate for run ${run.id}: ${candidateId}`);
413
+ return candidate;
414
+ }
415
+ function appendCandidateNode(run, candidate, stage, score) {
416
+ const parents = [candidate.resultNodeId, candidate.verifierNodeId].filter(Boolean);
417
+ const node = (0, state_node_1.appendRunNode)(run, (0, state_node_1.createStateNode)({
418
+ id: `${run.id}:candidate:${(0, state_1.safeFileName)(candidate.id)}:${stage}`,
419
+ kind: "candidate",
420
+ status: candidate.status === "failed" ? "failed" : candidate.status === "verified" ? "verified" : "completed",
421
+ loopStage: stage === "registered" ? "observe" : "adjust",
422
+ inputs: { candidateId: candidate.id, workerId: candidate.workerId, taskId: candidate.taskId },
423
+ outputs: compactMetadata({
424
+ status: candidate.status,
425
+ scoreId: score?.id,
426
+ normalized: score?.normalized,
427
+ verdict: score?.verdict
428
+ }) || {},
429
+ artifacts: candidateArtifacts(run, candidate),
430
+ evidence: candidate.evidence,
431
+ parents,
432
+ metadata: { candidateId: candidate.id, stage, kind: candidate.kind }
433
+ }));
434
+ for (const parentId of parents) {
435
+ const parent = run.nodes?.find((candidateNode) => candidateNode.id === parentId);
436
+ if (!parent)
437
+ continue;
438
+ const linked = (0, state_node_1.linkStateNodes)(parent, node);
439
+ (0, state_node_1.appendRunNode)(run, linked[0]);
440
+ (0, state_node_1.appendRunNode)(run, linked[1]);
441
+ }
442
+ }
443
+ function appendSelectionNode(run, candidate, selection) {
444
+ const parentIds = [candidate.verifierNodeId, `${run.id}:candidate:${(0, state_1.safeFileName)(candidate.id)}:scored`].filter(Boolean);
445
+ const node = (0, state_node_1.appendRunNode)(run, (0, state_node_1.createStateNode)({
446
+ id: `${run.id}:candidate:${(0, state_1.safeFileName)(candidate.id)}:selection:${(0, state_1.safeFileName)(selection.id)}`,
447
+ kind: "candidate",
448
+ status: candidate.status === "verified" ? "verified" : "completed",
449
+ loopStage: "adjust",
450
+ inputs: { candidateId: candidate.id, selectionId: selection.id },
451
+ outputs: selection,
452
+ artifacts: selection.artifacts,
453
+ evidence: selection.evidence,
454
+ parents: parentIds,
455
+ metadata: { candidateId: candidate.id, selectionId: selection.id, selected: true }
456
+ }));
457
+ for (const parentId of parentIds) {
458
+ const parent = run.nodes?.find((candidateNode) => candidateNode.id === parentId);
459
+ if (!parent)
460
+ continue;
461
+ const linked = (0, state_node_1.linkStateNodes)(parent, node);
462
+ (0, state_node_1.appendRunNode)(run, linked[0]);
463
+ (0, state_node_1.appendRunNode)(run, linked[1]);
464
+ }
465
+ }
466
+ function recordCandidateFailure(run, candidate, code, options) {
467
+ return (0, error_feedback_1.recordFeedback)(run, {
468
+ source: "verifier",
469
+ error: {
470
+ code,
471
+ message: options.message,
472
+ at: new Date().toISOString(),
473
+ retryable: options.retryable,
474
+ details: compactMetadata({
475
+ ...(options.details || {}),
476
+ candidateId: candidate.id,
477
+ workerId: candidate.workerId,
478
+ taskId: candidate.taskId
479
+ })
480
+ },
481
+ taskId: candidate.taskId,
482
+ retryable: options.retryable,
483
+ evidence: candidate.evidence,
484
+ artifacts: candidateArtifacts(run, candidate),
485
+ metadata: { candidateId: candidate.id, workerId: candidate.workerId, resultNodeId: candidate.resultNodeId }
486
+ }, { persist: false });
487
+ }
488
+ function writeCandidate(run, candidate) {
489
+ (0, state_1.writeJson)(candidateFile(run, candidate.id), candidate);
490
+ }
491
+ function writeScore(run, candidateId, score) {
492
+ (0, state_1.writeJson)(node_path_1.default.join(candidateDir(run, candidateId), "scores", `${(0, state_1.safeFileName)(score.id)}.json`), score);
493
+ }
494
+ function writeSelection(run, selection) {
495
+ (0, state_1.writeJson)(node_path_1.default.join(candidateRoot(run), "selections", `${(0, state_1.safeFileName)(selection.id)}.json`), selection);
496
+ }
497
+ function writeCandidateIndex(run) {
498
+ ensureCandidateState(run);
499
+ (0, state_1.writeJson)(indexPath(run), {
500
+ schemaVersion: exports.CANDIDATE_SCHEMA_VERSION,
501
+ runId: run.id,
502
+ candidates: (run.candidates || []).map((candidate) => ({
503
+ id: candidate.id,
504
+ kind: candidate.kind,
505
+ status: candidate.status,
506
+ workerId: candidate.workerId,
507
+ taskId: candidate.taskId,
508
+ resultNodeId: candidate.resultNodeId,
509
+ verifierNodeId: candidate.verifierNodeId,
510
+ resultPath: candidate.resultPath,
511
+ scores: candidate.scores,
512
+ feedbackIds: candidate.feedbackIds
513
+ })),
514
+ selections: run.candidateSelections || []
515
+ });
516
+ }
517
+ function loadCandidatesFromDisk(run) {
518
+ ensureCandidateState(run);
519
+ return node_fs_1.default
520
+ .readdirSync(candidateRoot(run), { withFileTypes: true })
521
+ .filter((entry) => entry.isDirectory() && entry.name !== "selections")
522
+ .map((entry) => node_path_1.default.join(candidateRoot(run), entry.name, "candidate.json"))
523
+ .filter((file) => node_fs_1.default.existsSync(file))
524
+ .map((file) => JSON.parse(node_fs_1.default.readFileSync(file, "utf8")));
525
+ }
526
+ function readScores(run, candidateId) {
527
+ const dir = node_path_1.default.join(candidateDir(run, candidateId), "scores");
528
+ if (!node_fs_1.default.existsSync(dir))
529
+ return [];
530
+ return node_fs_1.default
531
+ .readdirSync(dir)
532
+ .filter((file) => file.endsWith(".json"))
533
+ .sort()
534
+ .map((file) => JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, file), "utf8")));
535
+ }
536
+ function candidateArtifacts(run, candidate) {
537
+ return [
538
+ { id: "candidate", kind: "json", path: candidateFile(run, candidate.id) },
539
+ ...candidate.artifacts
540
+ ];
541
+ }
542
+ function artifactsFromInput(input) {
543
+ const artifacts = [];
544
+ if (input.resultPath)
545
+ artifacts.push({ id: "result", kind: "markdown", path: node_path_1.default.resolve(input.resultPath) });
546
+ return artifacts;
547
+ }
548
+ function evidenceFromInput(run, input) {
549
+ const resultNode = input.resultNodeId ? run.nodes?.find((node) => node.id === input.resultNodeId) : undefined;
550
+ const verifierNode = input.verifierNodeId ? run.nodes?.find((node) => node.id === input.verifierNodeId) : undefined;
551
+ return mergeById(resultNode?.evidence || [], verifierNode?.evidence || []);
552
+ }
553
+ function inferCandidateKind(input) {
554
+ if (input.workerId)
555
+ return "worker-output";
556
+ if (input.resultNodeId || input.resultPath)
557
+ return "result";
558
+ return "manual";
559
+ }
560
+ function bestScore(scores) {
561
+ return [...scores].sort((left, right) => right.normalized - left.normalized || left.createdAt.localeCompare(right.createdAt))[0];
562
+ }
563
+ function compareRows(left, right, policy) {
564
+ const byScore = right.normalized - left.normalized;
565
+ if (byScore !== 0)
566
+ return byScore;
567
+ if (policy.tieBreaker === "candidateId")
568
+ return left.candidate.id.localeCompare(right.candidate.id);
569
+ const byCreated = left.candidate.createdAt.localeCompare(right.candidate.createdAt);
570
+ return byCreated || left.candidate.id.localeCompare(right.candidate.id);
571
+ }
572
+ function detectTies(candidates) {
573
+ const groups = new Map();
574
+ for (const candidate of candidates) {
575
+ const key = String(candidate.normalized);
576
+ groups.set(key, [...(groups.get(key) || []), candidate.candidateId]);
577
+ }
578
+ return Array.from(groups.values()).filter((group) => group.length > 1);
579
+ }
580
+ function mergePolicy(policy = {}) {
581
+ return {
582
+ id: policy.id || "cw.candidate.default",
583
+ title: policy.title || "Default Candidate Scoring",
584
+ criteria: policy.criteria || [],
585
+ requireEvidence: policy.requireEvidence ?? true,
586
+ requireVerifierGate: policy.requireVerifierGate ?? true,
587
+ minNormalized: policy.minNormalized,
588
+ tieBreaker: policy.tieBreaker || "createdAt"
589
+ };
590
+ }
591
+ function verdictFor(normalized, policy) {
592
+ if (policy.minNormalized !== undefined && normalized < policy.minNormalized)
593
+ return "fail";
594
+ if (normalized >= 0.7)
595
+ return "pass";
596
+ if (normalized >= 0.4)
597
+ return "warn";
598
+ return "fail";
599
+ }
600
+ function sumCriteria(criteria) {
601
+ return Object.values(criteria).reduce((total, value) => total + Number(value || 0), 0);
602
+ }
603
+ function candidateRoot(run) {
604
+ ensureCandidateState(run);
605
+ return run.paths.candidatesDir || node_path_1.default.join(run.paths.runDir, "candidates");
606
+ }
607
+ function candidateDir(run, candidateId) {
608
+ return node_path_1.default.join(candidateRoot(run), (0, state_1.safeFileName)(candidateId));
609
+ }
610
+ function candidateFile(run, candidateId) {
611
+ return node_path_1.default.join(candidateDir(run, candidateId), "candidate.json");
612
+ }
613
+ function indexPath(run) {
614
+ return node_path_1.default.join(candidateRoot(run), "index.json");
615
+ }
616
+ function rankingPath(run) {
617
+ return node_path_1.default.join(candidateRoot(run), "ranking.json");
618
+ }
619
+ function createCandidateId(kind, seed) {
620
+ const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
621
+ const suffix = Math.random().toString(36).slice(2, 8);
622
+ return `candidate-${(0, state_1.safeFileName)(kind)}-${seed ? `${(0, state_1.safeFileName)(seed)}-` : ""}${stamp}-${suffix}`;
623
+ }
624
+ function createScoreId(candidateId) {
625
+ const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
626
+ return `score-${(0, state_1.safeFileName)(candidateId)}-${stamp}-${Math.random().toString(36).slice(2, 8)}`;
627
+ }
628
+ function createSelectionId(candidateId) {
629
+ const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
630
+ return `selection-${(0, state_1.safeFileName)(candidateId)}-${stamp}-${Math.random().toString(36).slice(2, 8)}`;
631
+ }
632
+ function shouldPersist(options) {
633
+ return options.persist !== false;
634
+ }
635
+ function error(code, message, options = {}) {
636
+ return {
637
+ code,
638
+ message,
639
+ at: new Date().toISOString(),
640
+ retryable: false,
641
+ details: options.details
642
+ };
643
+ }
644
+ /** HARD no-false-green gate (v0.1.43) — kept in SYNC with commit.ts. Traces a
645
+ * verifier node back to its source result node and returns the empty-capture
646
+ * marker (set at ingest via isEmptyCapture) when present. Reads ONLY persisted
647
+ * state, so selection replays deterministically. */
648
+ function emptyCaptureWarning(run, verifierNode) {
649
+ const resultNodeId = (typeof verifierNode.inputs?.inputNodeId === "string" ? verifierNode.inputs.inputNodeId : undefined) ||
650
+ verifierNode.parents[0];
651
+ const resultNode = resultNodeId ? run.nodes?.find((node) => node.id === resultNodeId) : undefined;
652
+ const warning = resultNode?.metadata?.captureWarning;
653
+ return typeof warning === "string" && warning ? warning : undefined;
654
+ }
655
+ function mergeCandidates(left, right) {
656
+ const merged = [...left];
657
+ for (const candidate of right) {
658
+ const index = merged.findIndex((entry) => entry.id === candidate.id);
659
+ if (index >= 0)
660
+ merged[index] = candidate;
661
+ else
662
+ merged.push(candidate);
663
+ }
664
+ return merged;
665
+ }
666
+ function mergeById(left, right) {
667
+ const merged = [...left];
668
+ for (const item of right) {
669
+ const index = merged.findIndex((entry) => entry.id === item.id);
670
+ if (index >= 0)
671
+ merged[index] = item;
672
+ else
673
+ merged.push(item);
674
+ }
675
+ return merged;
676
+ }
677
+ function mergeEvidence(left, right) {
678
+ const merged = [...left];
679
+ for (const item of right) {
680
+ const index = merged.findIndex((entry) => entry.id === item.id &&
681
+ entry.source === item.source &&
682
+ entry.path === item.path &&
683
+ entry.locator === item.locator);
684
+ if (index >= 0)
685
+ merged[index] = item;
686
+ else
687
+ merged.push(item);
688
+ }
689
+ return merged;
690
+ }
691
+ function sandboxProfileForCandidate(run, candidate) {
692
+ const worker = candidate.workerId ? (run.workers || []).find((entry) => entry.id === candidate.workerId) : undefined;
693
+ if (worker?.sandboxProfileId)
694
+ return worker.sandboxProfileId;
695
+ const task = candidate.taskId ? (run.tasks || []).find((entry) => entry.id === candidate.taskId) : undefined;
696
+ return task?.sandboxProfileId;
697
+ }
698
+ function unique(values) {
699
+ return Array.from(new Set(values.filter(Boolean)));
700
+ }
701
+ function countBy(items, key) {
702
+ const counts = {};
703
+ for (const item of items) {
704
+ const value = key(item);
705
+ counts[value] = (counts[value] || 0) + 1;
706
+ }
707
+ return counts;
708
+ }
709
+ function clamp(value, min, max) {
710
+ return Math.max(min, Math.min(max, value));
711
+ }
712
+ function compactMetadata(value) {
713
+ const entries = Object.entries(value).filter(([, entry]) => entry !== undefined);
714
+ return entries.length ? Object.fromEntries(entries) : undefined;
715
+ }