@riddledc/riddle-proof 0.7.225 → 0.7.227

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 (139) hide show
  1. package/dist/adapters/codex-exec-agent.cjs +893 -0
  2. package/dist/adapters/codex-exec-agent.d.cts +3 -0
  3. package/dist/adapters/codex-exec-agent.d.ts +3 -0
  4. package/dist/adapters/codex-exec-agent.js +13 -0
  5. package/dist/adapters/codex.cjs +893 -0
  6. package/dist/adapters/codex.d.cts +3 -0
  7. package/dist/adapters/codex.d.ts +3 -0
  8. package/dist/adapters/codex.js +13 -0
  9. package/dist/adapters/local-agent.cjs +893 -0
  10. package/dist/adapters/local-agent.d.cts +3 -0
  11. package/dist/adapters/local-agent.d.ts +3 -0
  12. package/dist/adapters/local-agent.js +13 -0
  13. package/dist/adapters/openclaw.cjs +213 -0
  14. package/dist/adapters/openclaw.d.cts +2 -0
  15. package/dist/adapters/openclaw.d.ts +2 -0
  16. package/dist/adapters/openclaw.js +17 -0
  17. package/dist/advanced/engine-harness.cjs +5701 -0
  18. package/dist/advanced/engine-harness.d.cts +2 -0
  19. package/dist/advanced/engine-harness.d.ts +2 -0
  20. package/dist/advanced/engine-harness.js +16 -0
  21. package/dist/advanced/index.cjs +6242 -0
  22. package/dist/advanced/index.d.cts +5 -0
  23. package/dist/advanced/index.d.ts +5 -0
  24. package/dist/advanced/index.js +23 -0
  25. package/dist/advanced/proof-run-core.cjs +1085 -0
  26. package/dist/advanced/proof-run-core.d.cts +1 -0
  27. package/dist/advanced/proof-run-core.d.ts +1 -0
  28. package/dist/advanced/proof-run-core.js +59 -0
  29. package/dist/advanced/proof-run-engine.cjs +2905 -0
  30. package/dist/advanced/proof-run-engine.d.cts +2 -0
  31. package/dist/advanced/proof-run-engine.d.ts +2 -0
  32. package/dist/advanced/proof-run-engine.js +10 -0
  33. package/dist/advanced/runner.cjs +833 -0
  34. package/dist/advanced/runner.d.cts +2 -0
  35. package/dist/advanced/runner.d.ts +2 -0
  36. package/dist/advanced/runner.js +12 -0
  37. package/dist/app-contract/index.cjs +24 -0
  38. package/dist/app-contract/index.d.cts +1 -0
  39. package/dist/app-contract/index.d.ts +1 -0
  40. package/dist/app-contract/index.js +2 -0
  41. package/dist/basic-gameplay.js +1 -0
  42. package/dist/checkpoint.js +1 -0
  43. package/dist/chunk-BHL4JSGM.js +1900 -0
  44. package/dist/chunk-BTN76IGW.js +115 -0
  45. package/dist/{chunk-TGHTM66Z.js → chunk-COERZX63.js} +10 -2
  46. package/dist/{chunk-4DRRIJDX.js → chunk-IWLQQ5S5.js} +27 -4
  47. package/dist/chunk-MLKGABMK.js +9 -0
  48. package/dist/chunk-OFSECTSC.js +0 -0
  49. package/dist/{chunk-GBVEQQIM.js → chunk-TJ63IE65.js} +36 -1
  50. package/dist/chunk-VZD5LH7U.js +4138 -0
  51. package/dist/cli/index.cjs +18 -0
  52. package/dist/cli/index.d.cts +2 -0
  53. package/dist/cli/index.d.ts +2 -0
  54. package/dist/cli/index.js +12 -0
  55. package/dist/cli.cjs +19 -2
  56. package/dist/cli.d.cts +1 -0
  57. package/dist/cli.d.ts +1 -0
  58. package/dist/cli.js +8 -4139
  59. package/dist/codex-exec-agent.d.cts +1 -1
  60. package/dist/codex-exec-agent.d.ts +1 -1
  61. package/dist/codex-exec-agent.js +1 -0
  62. package/dist/diagnostics.js +1 -0
  63. package/dist/engine-harness-CMACHP6A.d.ts +96 -0
  64. package/dist/engine-harness-LBfqbFSe.d.cts +96 -0
  65. package/dist/engine-harness.cjs +19 -2
  66. package/dist/engine-harness.d.cts +2 -79
  67. package/dist/engine-harness.d.ts +2 -79
  68. package/dist/engine-harness.js +3 -2
  69. package/dist/index.cjs +19 -2
  70. package/dist/index.d.cts +2 -2
  71. package/dist/index.d.ts +2 -2
  72. package/dist/index.js +4 -3
  73. package/dist/local-agent.d.cts +1 -1
  74. package/dist/local-agent.d.ts +1 -1
  75. package/dist/local-agent.js +1 -0
  76. package/dist/openclaw.js +8 -107
  77. package/dist/playability.js +1 -0
  78. package/dist/profile/index.cjs +9403 -0
  79. package/dist/profile/index.d.cts +2 -0
  80. package/dist/profile/index.d.ts +2 -0
  81. package/dist/profile/index.js +53 -0
  82. package/dist/profile.js +1 -0
  83. package/dist/proof-run-core-CE0jx7wL.d.cts +359 -0
  84. package/dist/proof-run-core-CE0jx7wL.d.ts +359 -0
  85. package/dist/proof-run-core.d.cts +1 -322
  86. package/dist/proof-run-core.d.ts +1 -322
  87. package/dist/proof-run-core.js +2 -1
  88. package/dist/proof-run-engine-CSSc0mNn.d.ts +764 -0
  89. package/dist/proof-run-engine-HSRpUeBi.d.cts +764 -0
  90. package/dist/proof-run-engine.cjs +4 -0
  91. package/dist/proof-run-engine.d.cts +2 -757
  92. package/dist/proof-run-engine.d.ts +2 -757
  93. package/dist/proof-run-engine.js +5 -1881
  94. package/dist/proof-session.js +1 -0
  95. package/dist/result.js +1 -0
  96. package/dist/riddle-client.js +1 -0
  97. package/dist/run-card.js +1 -0
  98. package/dist/runner-4LJ5z0D-.d.cts +29 -0
  99. package/dist/runner-BdQpOkZD.d.ts +29 -0
  100. package/dist/runner.d.cts +2 -22
  101. package/dist/runner.d.ts +2 -22
  102. package/dist/runner.js +3 -2
  103. package/dist/runtime/index.cjs +528 -0
  104. package/dist/runtime/index.d.cts +1 -0
  105. package/dist/runtime/index.d.ts +1 -0
  106. package/dist/runtime/index.js +37 -0
  107. package/dist/runtime/riddle-client.cjs +528 -0
  108. package/dist/runtime/riddle-client.d.cts +1 -0
  109. package/dist/runtime/riddle-client.d.ts +1 -0
  110. package/dist/runtime/riddle-client.js +37 -0
  111. package/dist/spec/checkpoint.cjs +807 -0
  112. package/dist/spec/checkpoint.d.cts +2 -0
  113. package/dist/spec/checkpoint.d.ts +2 -0
  114. package/dist/spec/checkpoint.js +34 -0
  115. package/dist/spec/index.cjs +1510 -0
  116. package/dist/spec/index.d.cts +5 -0
  117. package/dist/spec/index.d.ts +5 -0
  118. package/dist/spec/index.js +80 -0
  119. package/dist/spec/result.cjs +259 -0
  120. package/dist/spec/result.d.cts +2 -0
  121. package/dist/spec/result.d.ts +2 -0
  122. package/dist/spec/result.js +21 -0
  123. package/dist/spec/run-card.cjs +287 -0
  124. package/dist/spec/run-card.d.cts +2 -0
  125. package/dist/spec/run-card.d.ts +2 -0
  126. package/dist/spec/run-card.js +11 -0
  127. package/dist/spec/state.cjs +541 -0
  128. package/dist/spec/state.d.cts +2 -0
  129. package/dist/spec/state.d.ts +2 -0
  130. package/dist/spec/state.js +28 -0
  131. package/dist/spec/types.cjs +18 -0
  132. package/dist/spec/types.d.cts +1 -0
  133. package/dist/spec/types.d.ts +1 -0
  134. package/dist/spec/types.js +1 -0
  135. package/dist/state.js +1 -0
  136. package/package.json +95 -2
  137. package/runtime/lib/verify.py +53 -3
  138. package/runtime/pipelines/riddle-proof-setup.lobster +45 -41
  139. package/runtime/tests/recon_verify_smoke.py +41 -0
@@ -1,1885 +1,9 @@
1
1
  import {
2
- buildCheckpointContract,
3
- buildSetupArgs,
4
- checkpointContinueStage,
5
- clearStageDecisionRequest,
6
- ensureAction,
7
- invalidateVerifyEvidence,
8
- mergeStateFromParams,
9
- noImplementationModeFor,
10
- readState,
11
- recordStageAttempt,
12
- resolveConfig,
13
- setStageDecisionRequest,
14
- summarizeState,
15
- validateShipGate,
16
- workflowFile,
17
- writeState
18
- } from "./chunk-GBVEQQIM.js";
19
-
20
- // src/proof-run-engine.ts
21
- import { execFileSync } from "child_process";
22
- import { existsSync, mkdirSync } from "fs";
23
- import path from "path";
24
- function snapshotFor(statePath) {
25
- return summarizeState(readState(statePath));
26
- }
27
- function authorReady(state) {
28
- return state?.author_status === "ready" || state?.proof_plan_status === "ready";
29
- }
30
- function implementationReady(state) {
31
- return ["changes_detected", "completed"].includes(state?.implementation_status || "");
32
- }
33
- function implementationRequired(params, state) {
34
- return !noImplementationModeFor(params, state);
35
- }
36
- function stageAfterAuthor(state, params) {
37
- return implementationReady(state) || !implementationRequired(params, state) ? "verify" : "implement";
38
- }
39
- function latestReconAttempt(state) {
40
- const history = Array.isArray(state?.recon_results?.attempt_history) ? state.recon_results.attempt_history : [];
41
- return history.length ? history[history.length - 1] : null;
42
- }
43
- function latestReconCapturedBaselines(state) {
44
- const latest = latestReconAttempt(state);
45
- if (latest?.captured_baselines && typeof latest.captured_baselines === "object") return latest.captured_baselines;
46
- if (latest?.baselines && typeof latest.baselines === "object") return latest.baselines;
47
- return state?.recon_results?.baselines || {};
48
- }
49
- function requiredReconBaselineLabels(state) {
50
- const labels = [];
51
- const reference = state?.requested_reference || state?.reference || "before";
52
- if (reference === "before" || reference === "both") labels.push("before");
53
- if ((reference === "prod" || reference === "both") && String(state?.prod_url || "").trim()) labels.push("prod");
54
- return labels;
55
- }
56
- function latestReconHasRequiredBaselines(state) {
57
- const baselines = latestReconCapturedBaselines(state);
58
- return requiredReconBaselineLabels(state).every((label) => Boolean((baselines?.[label]?.url || "").trim()));
59
- }
60
- function hasReconBaselineUnderstanding(state) {
61
- const understanding = state?.recon_assessment?.baseline_understanding || state?.recon_baseline_understanding || {};
62
- return Boolean(
63
- String(understanding?.visible_before_state || "").trim() && String(understanding?.requested_change || "").trim() && String(understanding?.proof_focus || "").trim() && String(understanding?.stop_condition || "").trim()
64
- );
65
- }
66
- function promoteLatestReconBaselines(state) {
67
- const baselines = latestReconCapturedBaselines(state);
68
- state.recon_results = state.recon_results || {};
69
- state.recon_results.baselines = baselines;
70
- state.recon_results.selected_attempt = latestReconAttempt(state) || {};
71
- state.before_cdn = (baselines?.before?.url || "").trim();
72
- state.prod_cdn = (baselines?.prod?.url || "").trim();
73
- return baselines;
74
- }
75
- function hasSupervisorReconAssessment(state) {
76
- const reconAssessment2 = state?.recon_assessment || {};
77
- const source = String(reconAssessment2?.source || state?.recon_assessment_source || "").trim().toLowerCase();
78
- if (!reconAssessment2?.decision) return false;
79
- return source === "supervising_agent" || source === "supervisor";
80
- }
81
- function reconAssessment(state) {
82
- const assessment = state?.recon_assessment || {};
83
- const decision = assessment?.decision || null;
84
- const continueWithStage = assessment?.continue_with_stage || assessment?.recommended_stage || (decision === "ready_for_author" ? "author" : "recon");
85
- return {
86
- decision,
87
- summary: assessment?.summary || state?.recon_assessment_request?.summary || state?.recon_summary || null,
88
- recommendedStage: assessment?.recommended_stage || continueWithStage || null,
89
- continueWithStage: continueWithStage || null,
90
- escalationTarget: assessment?.escalation_target || "agent",
91
- reasons: Array.isArray(assessment?.reasons) ? assessment.reasons : [],
92
- raw: assessment,
93
- source: String(assessment?.source || state?.recon_assessment_source || "").trim() || null
94
- };
95
- }
96
- function updateState(statePath, mutate) {
97
- const state = readState(statePath) || {};
98
- mutate(state);
99
- writeState(statePath, state);
100
- return state;
101
- }
102
- var RUNTIME_EVENT_LIMIT = 100;
103
- function nowIso() {
104
- return (/* @__PURE__ */ new Date()).toISOString();
105
- }
106
- function appendRuntimeEventToState(state, event) {
107
- const events = Array.isArray(state.runtime_events) ? state.runtime_events : [];
108
- state.runtime_events = [...events, event].slice(-RUNTIME_EVENT_LIMIT);
109
- state.runtime_updated_at = event.ts;
110
- }
111
- function beginRuntimeStep(statePath, action, step, workflowPath) {
112
- const timer = {
113
- startedAt: nowIso(),
114
- startedMs: Date.now()
115
- };
116
- updateState(statePath, (state) => {
117
- const current = {
118
- step,
119
- action,
120
- status: "running",
121
- started_at: timer.startedAt,
122
- workflow_file: path.basename(workflowPath)
123
- };
124
- state.current_runtime_step = current;
125
- appendRuntimeEventToState(state, {
126
- ts: timer.startedAt,
127
- kind: "workflow.step.started",
128
- step,
129
- action,
130
- summary: `Started ${step} workflow step.`,
131
- details: {
132
- workflow_file: path.basename(workflowPath)
133
- }
134
- });
135
- });
136
- return timer;
137
- }
138
- function finishRuntimeStep(statePath, action, result, timer) {
139
- const finishedAt = nowIso();
140
- const durationMs = Date.now() - timer.startedMs;
141
- const summary = result.haltedForApproval ? `${result.step} halted for approval.` : result.ok ? `Finished ${result.step} workflow step.` : `${result.step} workflow step failed.`;
142
- updateState(statePath, (state) => {
143
- const completed = {
144
- step: result.step,
145
- action,
146
- status: result.haltedForApproval ? "approval_required" : result.ok ? "completed" : "failed",
147
- started_at: timer.startedAt,
148
- finished_at: finishedAt,
149
- duration_ms: durationMs,
150
- ok: result.ok,
151
- halted_for_approval: result.haltedForApproval || false,
152
- auto_approved: result.autoApproved || false,
153
- error: result.error || null
154
- };
155
- state.current_runtime_step = null;
156
- state.last_runtime_step = completed;
157
- appendRuntimeEventToState(state, {
158
- ts: finishedAt,
159
- kind: "workflow.step.finished",
160
- step: result.step,
161
- action,
162
- summary,
163
- details: completed
164
- });
165
- });
166
- return {
167
- ...result,
168
- started_at: timer.startedAt,
169
- finished_at: finishedAt,
170
- duration_ms: durationMs
171
- };
172
- }
173
- function executedStep(res, extra = {}) {
174
- const output = {
175
- step: res.step,
176
- ok: res.ok,
177
- haltedForApproval: res.haltedForApproval || false,
178
- autoApproved: res.autoApproved || false,
179
- ...extra
180
- };
181
- if (typeof res.duration_ms === "number") output.duration_ms = res.duration_ms;
182
- return output;
183
- }
184
- function hasSupervisorProofAssessment(state) {
185
- const proofAssessment = state?.proof_assessment || {};
186
- const source = String(proofAssessment?.source || state?.proof_assessment_source || "").trim().toLowerCase();
187
- if (!proofAssessment?.decision) return false;
188
- return source === "supervising_agent" || source === "supervisor";
189
- }
190
- function verifyAssessment(state) {
191
- const proofAssessment = state?.proof_assessment || {};
192
- const verifyDecision = state?.verify_decision_request || {};
193
- if (hasSupervisorProofAssessment(state)) {
194
- return {
195
- decision: proofAssessment?.decision || null,
196
- summary: proofAssessment?.summary || verifyDecision?.summary || null,
197
- recommendedStage: proofAssessment?.continue_with_stage || proofAssessment?.recommended_stage || verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || null,
198
- continueWithStage: proofAssessment?.continue_with_stage || verifyDecision?.continue_with_stage || proofAssessment?.recommended_stage || verifyDecision?.recommended_stage || null,
199
- escalationTarget: proofAssessment?.escalation_target || "agent",
200
- reasons: Array.isArray(proofAssessment?.reasons) ? proofAssessment.reasons : [],
201
- raw: proofAssessment,
202
- source: "supervising_agent"
203
- };
204
- }
205
- if (state?.verify_status === "capture_incomplete") {
206
- return {
207
- decision: verifyDecision?.capture_quality?.decision || "revise_capture",
208
- summary: verifyDecision?.summary || "Verify needs another internal capture iteration before the evidence can be judged.",
209
- recommendedStage: verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || "author",
210
- continueWithStage: verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || "author",
211
- escalationTarget: "agent",
212
- reasons: Array.isArray(verifyDecision?.capture_quality?.reasons) ? verifyDecision.capture_quality.reasons : [],
213
- raw: verifyDecision?.capture_quality || verifyDecision,
214
- source: "workflow_capture"
215
- };
216
- }
217
- return {
218
- decision: null,
219
- summary: verifyDecision?.summary || "Verify captured evidence and is waiting for supervising-agent proof assessment.",
220
- recommendedStage: null,
221
- continueWithStage: null,
222
- escalationTarget: "agent",
223
- reasons: [],
224
- raw: proofAssessment,
225
- source: "awaiting_supervisor"
226
- };
227
- }
228
- function nonConvergenceSignals(state, assessment = verifyAssessment(state)) {
229
- const verifyAttempts = Number(state?.stage_attempts?.verify?.count || 0);
230
- const authorAttempts = Number(state?.stage_attempts?.author?.count || 0);
231
- const reconAttempts = Number(state?.stage_attempts?.recon?.count || 0);
232
- const continueStage = assessment.continueWithStage || assessment.recommendedStage || null;
233
- return {
234
- verifyAttempts,
235
- authorAttempts,
236
- reconAttempts,
237
- continueStage,
238
- warning: verifyAttempts >= 4 || continueStage === "author" && verifyAttempts >= 2 && authorAttempts >= 2 || continueStage === "recon" && verifyAttempts >= 2 && reconAttempts >= 2 || continueStage === "implement" && verifyAttempts >= 2
239
- };
240
- }
241
- function shouldEscalateVerifyToHuman(_state, assessment = verifyAssessment(_state)) {
242
- return assessment.escalationTarget === "human";
243
- }
244
- function recommendedAdvanceStage(state) {
245
- if (!state?.workspace_ready) return "setup";
246
- if (!state?.recon_results || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) return "recon";
247
- if (!authorReady(state)) return "author";
248
- if (!implementationReady(state) && !noImplementationModeFor(state)) return "implement";
249
- if (state?.verify_status === "capture_incomplete") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage || "author";
250
- if (state?.verify_status === "evidence_captured") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage;
251
- if (!(state?.after_cdn || "").trim()) return "verify";
252
- return null;
253
- }
254
- function normalizeStageRequest(state, requestedAdvanceStage) {
255
- if (requestedAdvanceStage) return requestedAdvanceStage;
256
- if (!state?.workspace_ready) return null;
257
- if (!state?.recon_results || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) return "recon";
258
- return null;
259
- }
260
- function stringValue(value) {
261
- return typeof value === "string" && value.trim() ? value.trim() : "";
262
- }
263
- function commandResult(command, args, cwd, timeout = 6e4) {
264
- try {
265
- return {
266
- ok: true,
267
- stdout: execFileSync(command, args, { cwd, encoding: "utf-8", timeout, stdio: ["ignore", "pipe", "pipe"] }),
268
- stderr: ""
269
- };
270
- } catch (error) {
271
- return {
272
- ok: false,
273
- stdout: String(error?.stdout || ""),
274
- stderr: String(error?.stderr || error?.message || "")
275
- };
276
- }
277
- }
278
- function repoDirForSync(state) {
279
- const candidates = [
280
- state?.repo_dir,
281
- state?.after_worktree,
282
- state?.before_worktree
283
- ].map(stringValue).filter(Boolean);
284
- return candidates.find((candidate) => existsSync(path.join(candidate, ".git"))) || "";
285
- }
286
- function parseWorktreeList(output) {
287
- const entries = [];
288
- let current = {};
289
- for (const line of output.split(/\r?\n/)) {
290
- if (!line.trim()) {
291
- if (current.worktree) entries.push(current);
292
- current = {};
293
- continue;
294
- }
295
- const [key, ...rest] = line.split(" ");
296
- const value = rest.join(" ").trim();
297
- if (key === "worktree" || key === "HEAD" || key === "branch" || key === "detached") current[key] = value;
298
- }
299
- if (current.worktree) entries.push(current);
300
- return entries;
301
- }
302
- function gitStdout(cwd, args, timeout = 6e4) {
303
- const result = commandResult("git", args, cwd, timeout);
304
- return result.ok ? result.stdout.trim() : "";
305
- }
306
- function shortBranch(ref) {
307
- return ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
308
- }
309
- function safeInteger(value) {
310
- const parsed = Number.parseInt(value, 10);
311
- return Number.isFinite(parsed) ? parsed : null;
312
- }
313
- function baseCheckoutReport(repoDir, baseBranch, updateRequested, updateAllowed) {
314
- const remoteRef = `origin/${baseBranch}`;
315
- const report = {
316
- requested: updateRequested,
317
- repo_dir: repoDir,
318
- base_branch: baseBranch,
319
- remote_ref: remoteRef,
320
- updated: false
321
- };
322
- const listed = commandResult("git", ["worktree", "list", "--porcelain"], repoDir, 6e4);
323
- if (!listed.ok) {
324
- report.update_skipped = "worktree_list_failed";
325
- report.error = listed.stderr.slice(0, 300);
326
- return report;
327
- }
328
- const worktrees = parseWorktreeList(listed.stdout);
329
- const baseRef = `refs/heads/${baseBranch}`;
330
- const selected = worktrees.find((entry) => entry.branch === baseRef) || worktrees.find((entry) => path.resolve(entry.worktree || "") === path.resolve(repoDir) && shortBranch(entry.branch || "") === baseBranch);
331
- if (!selected?.worktree) {
332
- report.worktrees_seen = worktrees.map((entry) => ({
333
- path: entry.worktree || null,
334
- branch: entry.branch ? shortBranch(entry.branch) : null,
335
- detached: Boolean(entry.detached)
336
- }));
337
- report.update_skipped = "base_worktree_not_found";
338
- return report;
339
- }
340
- const baseDir = selected.worktree;
341
- const branch = shortBranch(selected.branch || "");
342
- const status = commandResult("git", ["status", "--porcelain"], baseDir, 6e4);
343
- const clean = status.ok && !status.stdout.trim();
344
- const localHead = gitStdout(baseDir, ["rev-parse", "HEAD"]);
345
- const remoteHead = gitStdout(baseDir, ["rev-parse", "--verify", remoteRef]);
346
- const counts = remoteHead ? gitStdout(baseDir, ["rev-list", "--left-right", "--count", `HEAD...${remoteRef}`]) : "";
347
- const [aheadRaw, behindRaw] = counts.split(/\s+/);
348
- Object.assign(report, {
349
- base_worktree: baseDir,
350
- branch: branch || null,
351
- clean,
352
- local_head: localHead || null,
353
- remote_head: remoteHead || null,
354
- ahead: safeInteger(aheadRaw || ""),
355
- behind: safeInteger(behindRaw || "")
356
- });
357
- if (!updateRequested) {
358
- report.update_skipped = "update_not_requested";
359
- return report;
360
- }
361
- if (!updateAllowed) {
362
- report.update_skipped = "fetch_failed";
363
- return report;
364
- }
365
- if (branch !== baseBranch) {
366
- report.update_skipped = "base_worktree_not_on_base_branch";
367
- return report;
368
- }
369
- if (!status.ok) {
370
- report.update_skipped = "status_failed";
371
- report.status_error = status.stderr.slice(0, 300);
372
- return report;
373
- }
374
- if (!clean) {
375
- report.update_skipped = "base_worktree_dirty";
376
- return report;
377
- }
378
- if (!remoteHead) {
379
- report.update_skipped = "remote_ref_missing";
380
- return report;
381
- }
382
- if (localHead && localHead === remoteHead) {
383
- report.update_skipped = "already_current";
384
- return report;
385
- }
386
- const merge = commandResult("git", ["merge", "--ff-only", remoteRef], baseDir, 12e4);
387
- if (!merge.ok) {
388
- report.update_skipped = "fast_forward_failed";
389
- report.update_error = merge.stderr.slice(0, 500);
390
- return report;
391
- }
392
- const updatedHead = gitStdout(baseDir, ["rev-parse", "HEAD"]);
393
- const updatedCounts = gitStdout(baseDir, ["rev-list", "--left-right", "--count", `HEAD...${remoteRef}`]);
394
- const [updatedAheadRaw, updatedBehindRaw] = updatedCounts.split(/\s+/);
395
- report.updated = true;
396
- report.local_head = updatedHead || report.local_head;
397
- report.ahead = safeInteger(updatedAheadRaw || "");
398
- report.behind = safeInteger(updatedBehindRaw || "");
399
- report.update_summary = merge.stdout.trim().slice(0, 500);
400
- return report;
401
- }
402
- function normalizeGhPrStatus(value) {
403
- const status = stringValue(value).toLowerCase();
404
- if (status === "merged") return "merged";
405
- if (status === "open") return "open";
406
- if (status === "closed") return "closed";
407
- return status || "unknown";
408
- }
409
- function prRefFromState(state) {
410
- return stringValue(state?.pr_number) || stringValue(state?.pr_url);
411
- }
412
- function prNumberFromUrl(url) {
413
- const match = url.match(/\/pull\/(\d+)(?:$|[?#])/);
414
- return match?.[1] || "";
415
- }
416
- function normalizePrState(raw, state, checkedAt = (/* @__PURE__ */ new Date()).toISOString()) {
417
- const mergeCommit = typeof raw?.mergeCommit === "object" && raw.mergeCommit ? stringValue(raw.mergeCommit.oid) : stringValue(raw?.mergeCommit);
418
- const url = stringValue(raw?.url) || stringValue(state?.pr_url);
419
- return {
420
- status: normalizeGhPrStatus(raw?.state),
421
- pr_url: url || null,
422
- pr_number: String(raw?.number || state?.pr_number || prNumberFromUrl(url) || ""),
423
- repo: stringValue(state?.repo) || null,
424
- head_branch: stringValue(raw?.headRefName) || stringValue(state?.target_branch) || stringValue(state?.branch) || null,
425
- base_branch: stringValue(raw?.baseRefName) || stringValue(state?.base_branch) || "main",
426
- merge_commit: mergeCommit || null,
427
- merged_at: stringValue(raw?.mergedAt) || null,
428
- closed_at: stringValue(raw?.closedAt) || null,
429
- checked_at: checkedAt,
430
- source: "gh"
431
- };
432
- }
433
- function cleanupMergedProofRun(state, repoDir, params, prState) {
434
- const cleanup = {
435
- requested: params.cleanup_merged_pr !== false,
436
- fetch_base: params.fetch_base !== false,
437
- update_base_checkout: params.update_base_checkout !== false,
438
- repo_dir: repoDir,
439
- worktrees_removed: [],
440
- worktree_remove_errors: [],
441
- branches_deleted: [],
442
- branch_delete_errors: [],
443
- pruned: false
444
- };
445
- const baseBranch = stringValue(prState.base_branch) || stringValue(state?.base_branch) || "main";
446
- let fetchedBase = params.fetch_base === false;
447
- if (params.fetch_base !== false && baseBranch) {
448
- const fetch = commandResult("git", ["fetch", "origin", baseBranch], repoDir, 12e4);
449
- cleanup.fetch = fetch.ok ? { ok: true, base_branch: baseBranch } : { ok: false, base_branch: baseBranch, error: fetch.stderr.slice(0, 300) };
450
- if (fetch.ok) {
451
- fetchedBase = true;
452
- state.base_synced_at = (/* @__PURE__ */ new Date()).toISOString();
453
- state.base_branch = baseBranch;
454
- }
455
- }
456
- cleanup.base_checkout = baseCheckoutReport(repoDir, baseBranch, params.update_base_checkout !== false, fetchedBase);
457
- if (params.cleanup_merged_pr === false) {
458
- cleanup.skipped = "cleanup_disabled";
459
- return cleanup;
460
- }
461
- const removed = [];
462
- const removeErrors = [];
463
- for (const candidate of [state?.before_worktree, state?.after_worktree].map(stringValue).filter(Boolean)) {
464
- if (!existsSync(candidate) || path.resolve(candidate) === path.resolve(repoDir)) continue;
465
- const remove = commandResult("git", ["worktree", "remove", "--force", candidate], repoDir, 12e4);
466
- if (remove.ok) {
467
- removed.push(candidate);
468
- } else {
469
- removeErrors.push({ path: candidate, error: remove.stderr.slice(0, 300) });
470
- }
471
- }
472
- cleanup.worktrees_removed = removed;
473
- cleanup.worktree_remove_errors = removeErrors;
474
- const afterBranch = stringValue(state?.after_worktree_branch);
475
- if (afterBranch.startsWith("riddle-proof/")) {
476
- const deleted = commandResult("git", ["branch", "-D", afterBranch], repoDir, 6e4);
477
- if (deleted.ok) {
478
- cleanup.branches_deleted = [afterBranch];
479
- } else {
480
- cleanup.branch_delete_errors = [{ branch: afterBranch, error: deleted.stderr.slice(0, 300) }];
481
- }
482
- }
483
- const prune = commandResult("git", ["worktree", "prune"], repoDir, 6e4);
484
- cleanup.pruned = prune.ok;
485
- if (!prune.ok) cleanup.prune_error = prune.stderr.slice(0, 300);
486
- return cleanup;
487
- }
488
- function syncPrLifecycle(statePath, params) {
489
- const state = readState(statePath);
490
- if (!state) {
491
- return {
492
- ok: false,
493
- action: "sync",
494
- state_path: statePath,
495
- checkpoint: "pr_sync_not_found",
496
- summary: "No readable Riddle Proof state exists at state_path.",
497
- state: null,
498
- nextAction: "Check the wrapper state_path or run riddle_proof_status first."
499
- };
500
- }
501
- const repoDir = repoDirForSync(state);
502
- const prRef = prRefFromState(state);
503
- if (!repoDir || !prRef) {
504
- const missingPr = !prRef;
505
- const orphanSummary = "Riddle Proof state exists, but this run is not recoverable through PR sync because no PR URL or PR number was linked before it stopped.";
506
- const orphanNextAction = "Treat this as an orphaned proof run: update the base checkout directly if needed, then clean stale proof worktrees or riddle-proof/* branches outside normal PR sync.";
507
- const prState2 = {
508
- status: missingPr ? "orphaned" : "unavailable",
509
- pr_url: state.pr_url || null,
510
- pr_number: String(state.pr_number || prNumberFromUrl(stringValue(state.pr_url)) || ""),
511
- repo: state.repo || null,
512
- head_branch: state.target_branch || state.branch || null,
513
- base_branch: state.base_branch || "main",
514
- checked_at: (/* @__PURE__ */ new Date()).toISOString(),
515
- source: repoDir ? "state" : "local_state",
516
- sync_recoverable: !missingPr,
517
- sync_blocker: missingPr ? "missing_pr_linkage" : "missing_local_repo",
518
- next_action: missingPr ? orphanNextAction : "State has a PR but no readable local git repo/worktree; restore repo access and rerun sync."
519
- };
520
- state.pr_state = prState2;
521
- if (missingPr) state.pr_sync_summary = orphanSummary;
522
- writeState(statePath, state);
523
- return {
524
- ok: false,
525
- action: "sync",
526
- state_path: statePath,
527
- checkpoint: missingPr ? "pr_sync_no_pr" : "pr_sync_unavailable",
528
- summary: missingPr ? orphanSummary : prState2.next_action,
529
- state: summarizeState(state).state,
530
- pr_state: prState2,
531
- nextAction: prState2.next_action
532
- };
533
- }
534
- const viewed = commandResult("gh", ["pr", "view", prRef, "--json", "state,mergedAt,closedAt,mergeCommit,headRefName,baseRefName,url,number"], repoDir, 6e4);
535
- if (!viewed.ok) {
536
- const prState2 = {
537
- status: "unavailable",
538
- pr_url: state.pr_url || null,
539
- pr_number: String(state.pr_number || prNumberFromUrl(stringValue(state.pr_url)) || ""),
540
- repo: state.repo || null,
541
- head_branch: state.target_branch || state.branch || null,
542
- base_branch: state.base_branch || "main",
543
- checked_at: (/* @__PURE__ */ new Date()).toISOString(),
544
- source: "gh",
545
- next_action: "GitHub PR state is unavailable; fix gh auth/repo access and rerun riddle_proof_sync."
546
- };
547
- state.pr_state = prState2;
548
- state.cleanup_report = { requested: params.cleanup_merged_pr !== false, skipped: "pr_state_unavailable", error: viewed.stderr.slice(0, 300) };
549
- writeState(statePath, state);
550
- return {
551
- ok: false,
552
- action: "sync",
553
- state_path: statePath,
554
- checkpoint: "pr_sync_unavailable",
555
- summary: prState2.next_action,
556
- state: summarizeState(state).state,
557
- pr_state: prState2,
558
- cleanup: state.cleanup_report,
559
- nextAction: prState2.next_action
560
- };
561
- }
562
- let rawPr;
563
- try {
564
- rawPr = JSON.parse(viewed.stdout);
565
- } catch {
566
- rawPr = {};
567
- }
568
- const prState = normalizePrState(rawPr, state);
569
- let cleanup = null;
570
- let checkpoint = "pr_sync_open";
571
- let ok = true;
572
- let summary = "PR is still open; no merge cleanup was performed.";
573
- let nextAction = "Wait for the PR to merge, then rerun riddle_proof_sync.";
574
- if (prState.status === "merged") {
575
- cleanup = cleanupMergedProofRun(state, repoDir, params, prState);
576
- prState.cleanup = cleanup;
577
- prState.next_action = "The PR is merged; sync recorded proof cleanup and the local base checkout refresh status.";
578
- state.finalized = true;
579
- state.merge_commit = prState.merge_commit || state.merge_commit || "";
580
- state.merged_at = prState.merged_at || state.merged_at || "";
581
- state.cleanup_report = cleanup;
582
- checkpoint = "pr_sync_merged";
583
- summary = "PR is merged and Riddle Proof state has been reconciled.";
584
- nextAction = "Start the next proof run from the recorded base checkout; inspect cleanup.base_checkout only if it reports a skipped or failed fast-forward.";
585
- } else if (prState.status === "closed") {
586
- prState.next_action = "The PR is closed without a merge; inspect the PR before reusing or deleting the branch.";
587
- checkpoint = "pr_sync_closed";
588
- summary = "PR is closed without a merge; no merge cleanup was performed.";
589
- nextAction = prState.next_action;
590
- } else if (prState.status !== "open") {
591
- ok = false;
592
- prState.next_action = "PR state was not recognized; inspect gh pr view output and rerun sync.";
593
- checkpoint = "pr_sync_unavailable";
594
- summary = prState.next_action;
595
- nextAction = prState.next_action;
596
- }
597
- state.pr_state = prState;
598
- state.pr_url = prState.pr_url || state.pr_url;
599
- state.pr_number = prState.pr_number || state.pr_number;
600
- state.target_branch = prState.head_branch || state.target_branch || state.branch;
601
- state.branch = prState.head_branch || state.branch;
602
- state.base_branch = prState.base_branch || state.base_branch;
603
- writeState(statePath, state);
604
- const snapshot = summarizeState(state);
605
- return {
606
- ok,
607
- action: "sync",
608
- state_path: statePath,
609
- checkpoint,
610
- summary,
611
- state: snapshot.state,
612
- pr_state: prState,
613
- cleanup,
614
- nextAction
615
- };
616
- }
617
- async function executeWorkflow(params, pluginConfig, resolvedConfig) {
618
- const config = resolvedConfig || resolveConfig(pluginConfig, params);
619
- const action = ensureAction(params.action);
620
- if (!existsSync(config.riddleProofDir)) {
621
- throw new Error(`riddle-proof runtime directory not found: ${config.riddleProofDir}`);
622
- }
623
- if (action === "status") {
624
- return {
625
- state_path: config.statePath,
626
- ...summarizeState(readState(config.statePath))
627
- };
628
- }
629
- if (action === "sync") {
630
- return syncPrLifecycle(config.statePath, params);
631
- }
632
- const stateKey = path.basename(config.statePath).replace(/[^A-Za-z0-9_.-]/g, "-");
633
- const lobsterStateDir = path.join(path.dirname(config.statePath), "riddle-proof-lobster-state", stateKey);
634
- mkdirSync(lobsterStateDir, { recursive: true });
635
- const env = {
636
- ...process.env,
637
- RIDDLE_PROOF_DIR: config.riddleProofDir,
638
- RIDDLE_PROOF_STATE_FILE: config.statePath,
639
- RIDDLE_PROOF_ARGS_FILE: config.argsPath,
640
- LOBSTER_STATE_DIR: lobsterStateDir
641
- };
642
- const lobsterCommand = process.env.RIDDLE_PROOF_LOBSTER_COMMAND || "lobster";
643
- const lobsterPrefix = process.env.RIDDLE_PROOF_LOBSTER_SCRIPT ? [process.env.RIDDLE_PROOF_LOBSTER_SCRIPT] : [];
644
- const runOne = (step) => {
645
- const args = step === "setup" ? buildSetupArgs(params, config) : {};
646
- const stepWorkflowFile = workflowFile(config.riddleProofDir, step);
647
- const timer = beginRuntimeStep(config.statePath, action, step, stepWorkflowFile);
648
- let output;
649
- try {
650
- output = JSON.parse(
651
- execFileSync(lobsterCommand, [...lobsterPrefix, "run", "--file", stepWorkflowFile, "--args-json", JSON.stringify(args)], {
652
- encoding: "utf-8",
653
- env
654
- })
655
- );
656
- } catch (error) {
657
- return finishRuntimeStep(config.statePath, action, {
658
- ok: false,
659
- step,
660
- error: error?.message || String(error),
661
- stdout: String(error?.stdout || ""),
662
- stderr: String(error?.stderr || "")
663
- }, timer);
664
- }
665
- if (output?.status === "needs_approval") {
666
- if (!params.auto_approve) {
667
- return finishRuntimeStep(config.statePath, action, {
668
- ok: false,
669
- haltedForApproval: true,
670
- step,
671
- approval: output.requiresApproval || null,
672
- raw: output
673
- }, timer);
674
- }
675
- const token = output?.requiresApproval?.resumeToken;
676
- if (!token) {
677
- return finishRuntimeStep(config.statePath, action, {
678
- ok: false,
679
- step,
680
- error: `${step} requested approval without a resume token.`,
681
- raw: output
682
- }, timer);
683
- }
684
- let resumed;
685
- try {
686
- resumed = JSON.parse(
687
- execFileSync(lobsterCommand, [...lobsterPrefix, "resume", "--token", token, "--approve", "yes"], {
688
- encoding: "utf-8",
689
- env
690
- })
691
- );
692
- } catch (error) {
693
- return finishRuntimeStep(config.statePath, action, {
694
- ok: false,
695
- step,
696
- autoApproved: true,
697
- error: error?.message || String(error),
698
- stdout: String(error?.stdout || ""),
699
- stderr: String(error?.stderr || "")
700
- }, timer);
701
- }
702
- return finishRuntimeStep(config.statePath, action, {
703
- ok: resumed?.ok !== false,
704
- step,
705
- autoApproved: true,
706
- raw: resumed
707
- }, timer);
708
- }
709
- return finishRuntimeStep(config.statePath, action, {
710
- ok: output?.ok !== false,
711
- step,
712
- raw: output
713
- }, timer);
714
- };
715
- let effectiveAdvanceStage = params.advance_stage || null;
716
- const recordAttempt = (stage, status, summary, extra = {}) => {
717
- updateState(config.statePath, (state) => {
718
- recordStageAttempt(state, stage, {
719
- status,
720
- summary,
721
- checkpoint: extra.checkpoint || null,
722
- requestedAdvanceStage: effectiveAdvanceStage || null,
723
- haltedForApproval: extra.haltedForApproval,
724
- autoApproved: extra.autoApproved,
725
- retryable: extra.retryable,
726
- checkpointDisposition: extra.checkpointDisposition || null,
727
- error: extra.error || null,
728
- details: extra.details || {}
729
- });
730
- });
731
- };
732
- const checkpoint = (stage, name, summary, extra = {}) => {
733
- const decision = updateState(config.statePath, (state) => {
734
- const checkpointContract = buildCheckpointContract(state, {
735
- statePath: config.statePath,
736
- stage,
737
- checkpoint: name,
738
- summary,
739
- nextActions: extra.nextActions,
740
- advanceOptions: extra.advanceOptions,
741
- recommendedAdvanceStage: extra.recommendedAdvanceStage,
742
- continueWithStage: extra.continueWithStage,
743
- blocking: extra.blocking
744
- });
745
- setStageDecisionRequest(state, {
746
- stage,
747
- checkpoint: name,
748
- summary,
749
- nextActions: extra.nextActions,
750
- advanceOptions: extra.advanceOptions,
751
- recommendedAdvanceStage: extra.recommendedAdvanceStage,
752
- continueWithStage: extra.continueWithStage,
753
- blocking: extra.blocking,
754
- details: extra.details,
755
- checkpointContract
756
- });
757
- }).stage_decision_request;
758
- const snapshot2 = snapshotFor(config.statePath);
759
- return {
760
- ok: extra.ok ?? true,
761
- action,
762
- state_path: config.statePath,
763
- stage: snapshot2.stage,
764
- checkpoint: name,
765
- summary,
766
- state: snapshot2.state,
767
- decisionRequest: decision,
768
- checkpointContract: decision?.checkpoint_contract || null,
769
- ...extra
770
- };
771
- };
772
- const primaryShipGateNextAction = (shipGate) => {
773
- const reasons = shipGate.reasons || [];
774
- if (reasons.some((reason) => reason.includes("proof_assessment"))) {
775
- return "resume with riddle_proof_review using decision=ready_to_ship only after screenshots, semantic evidence, and required comparison metrics prove the request; otherwise choose needs_implementation, revise_capture, or needs_richer_proof for the specific missing stage";
776
- }
777
- if (reasons.some((reason) => reason.includes("visual_delta"))) {
778
- return "keep the run in verify/evidence recovery until a measured before/after visual_delta exists; choose revise_capture rather than ready_to_ship or generic needs_richer_proof for this visual proof";
779
- }
780
- if (reasons.some((reason) => reason.includes("after_cdn") || reason.includes("verify_status"))) {
781
- return "rerun verify with stronger proof framing so after evidence is captured before shipping";
782
- }
783
- if (reasons.some((reason) => reason.includes("before_cdn") || reason.includes("prod_cdn") || reason.includes("prod_url"))) {
784
- return "return to recon and capture the missing required baseline before shipping";
785
- }
786
- return "inspect the ship gate details, repair the missing invariant, then resume the run";
787
- };
788
- const shipGateRecoveryStage = (shipGate) => {
789
- const reasons = shipGate.reasons || [];
790
- if (reasons.some((reason) => reason.includes("before_cdn") || reason.includes("prod_cdn") || reason.includes("prod_url"))) {
791
- return "recon";
792
- }
793
- if (reasons.some((reason) => reason.includes("implementation"))) {
794
- return "implement";
795
- }
796
- if (reasons.some((reason) => reason.includes("after_cdn") || reason.includes("verify_status") || reason.includes("visual_delta"))) {
797
- return "verify";
798
- }
799
- if (reasons.some((reason) => reason.includes("proof_assessment"))) {
800
- return "verify";
801
- }
802
- return "verify";
803
- };
804
- const shipGateBlocked = (state, executed, details = {}) => {
805
- const shipGate = validateShipGate(state);
806
- const nextAction = primaryShipGateNextAction(shipGate);
807
- const recoveryStage = shipGateRecoveryStage(shipGate);
808
- const advanceOptions = Array.from(/* @__PURE__ */ new Set([recoveryStage, "verify", "author", "implement", "recon", "ship"]));
809
- return checkpoint(
810
- "verify",
811
- "ship_gate_blocked",
812
- `Ship is blocked until the proof bundle satisfies the hard ship gate. Next action: ${nextAction}.`,
813
- {
814
- ok: false,
815
- nextActions: ["inspect_ship_gate", "advance_run_to_verify", "supply_proof_assessment_json", "return_to_recon_if_baseline_is_missing"],
816
- advanceOptions,
817
- recommendedAdvanceStage: recoveryStage,
818
- continueWithStage: recoveryStage,
819
- blocking: true,
820
- details: { ...details, shipGate, next_action: nextAction, recovery_stage: recoveryStage, executed },
821
- nextAction,
822
- recoveryStage,
823
- shipGate,
824
- verifyStatus: state?.verify_status || null,
825
- mergeRecommendation: state?.merge_recommendation || null,
826
- afterCdn: state?.after_cdn || null,
827
- proofAssessment: state?.proof_assessment || null,
828
- proofAssessmentRequest: state?.proof_assessment_request || null,
829
- executed
830
- }
831
- );
832
- };
833
- const failedRun = (stage, summary, res, extra = {}) => {
834
- recordAttempt(stage, res?.haltedForApproval ? "approval_required" : "failed", summary, {
835
- checkpoint: extra.checkpoint || null,
836
- haltedForApproval: res?.haltedForApproval || false,
837
- autoApproved: res?.autoApproved || false,
838
- error: res?.error || null,
839
- details: extra.details
840
- });
841
- const snapshot2 = snapshotFor(config.statePath);
842
- return {
843
- ok: false,
844
- action,
845
- state_path: config.statePath,
846
- stage: snapshot2.stage,
847
- summary,
848
- state: snapshot2.state,
849
- approval: res?.approval || null,
850
- error: res?.error || null,
851
- checkpoint: extra.checkpoint || null,
852
- ...extra
853
- };
854
- };
855
- if (action !== "setup") {
856
- mergeStateFromParams(config.statePath, params);
857
- }
858
- if (action === "run") {
859
- const executed = [];
860
- let state = readState(config.statePath);
861
- if (!state || !state.workspace_ready || params.advance_stage === "setup") {
862
- const setupRes = runOne("setup");
863
- executed.push(executedStep(setupRes));
864
- if (!setupRes.ok || setupRes.haltedForApproval) {
865
- return failedRun("setup", setupRes.haltedForApproval ? "setup halted for approval" : "setup failed", setupRes, {
866
- checkpoint: "setup_blocked"
867
- });
868
- }
869
- recordAttempt("setup", "completed", "Setup completed and state/worktrees are ready.", {
870
- checkpoint: params.advance_stage === "setup" ? "setup_review" : null,
871
- autoApproved: setupRes.autoApproved || false
872
- });
873
- mergeStateFromParams(config.statePath, params);
874
- state = readState(config.statePath);
875
- if (params.advance_stage === "setup") {
876
- return checkpoint(
877
- "setup",
878
- "setup_review",
879
- "Setup completed. Inspect the prepared workspace and explicitly advance to recon when ready.",
880
- {
881
- nextActions: ["inspect_setup_state", "advance_run_to_recon"],
882
- advanceOptions: ["recon", "setup"],
883
- recommendedAdvanceStage: "recon",
884
- details: { executed },
885
- executed
886
- }
887
- );
888
- }
889
- }
890
- state = readState(config.statePath);
891
- const continuedStage = params.continue_from_checkpoint ? checkpointContinueStage(state) : null;
892
- if (params.continue_from_checkpoint && !params.advance_stage && !continuedStage) {
893
- const recommended = recommendedAdvanceStage(state);
894
- return checkpoint(
895
- state?.active_checkpoint_stage || recommended || "recon",
896
- "continue_unavailable",
897
- "This run call asked to continue from a checkpoint, but the current state has no resumable checkpoint. Inspect status or set advance_stage explicitly.",
898
- {
899
- ok: false,
900
- nextActions: ["inspect_state", "set_advance_stage", "resume_run"],
901
- advanceOptions: ["recon", "author", "implement", "verify", "ship"],
902
- recommendedAdvanceStage: null,
903
- blocking: true,
904
- details: {
905
- executed,
906
- activeCheckpoint: state?.active_checkpoint || null,
907
- suggestedAdvanceStage: recommended || null
908
- },
909
- suggestedAdvanceStage: recommended || null,
910
- executed
911
- }
912
- );
913
- }
914
- effectiveAdvanceStage = params.advance_stage || continuedStage || null;
915
- if (effectiveAdvanceStage) {
916
- updateState(config.statePath, (state2) => {
917
- clearStageDecisionRequest(state2);
918
- state2.last_requested_advance_stage = effectiveAdvanceStage;
919
- });
920
- state = readState(config.statePath);
921
- }
922
- let requestedStage = normalizeStageRequest(state, effectiveAdvanceStage);
923
- const reconCheckpointActive = ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "") || state?.active_checkpoint === "recon_supervisor_judgment";
924
- if (requestedStage === "recon" && reconCheckpointActive) {
925
- const latestAttempt = latestReconAttempt(state);
926
- const latestCapturedBaselines = latestReconCapturedBaselines(state);
927
- const latestAssessment = reconAssessment(state);
928
- const reconAssessmentRequest = state?.recon_assessment_request || state?.recon_decision_request || null;
929
- const reconDetails = {
930
- executed,
931
- latestAttempt,
932
- latestCapturedBaselines,
933
- reconAssessmentRequest,
934
- reconAssessment: latestAssessment.raw
935
- };
936
- if (!hasSupervisorReconAssessment(state)) {
937
- return checkpoint(
938
- "recon",
939
- "recon_supervisor_judgment",
940
- "Recon gathered route hints, candidate paths, baseline captures, and observations. The supervising agent should now judge whether the latest baseline is trustworthy, whether recon should retry/reframe, and whether recon is done.",
941
- {
942
- nextActions: ["inspect_recon_packet", "supply_recon_assessment_json", "continue_internal_loop_with_checkpoint"],
943
- advanceOptions: ["recon", "author"],
944
- recommendedAdvanceStage: "recon",
945
- continueWithStage: "recon",
946
- blocking: false,
947
- details: reconDetails,
948
- reconAssessmentRequest,
949
- reconDecisionRequest: state?.recon_decision_request || null,
950
- executed
951
- }
952
- );
953
- }
954
- if (latestAssessment.decision === "recon_stuck" && latestAssessment.escalationTarget === "human") {
955
- const summary = latestAssessment.summary || "The supervising agent concluded recon is genuinely stuck and should escalate to the human.";
956
- recordAttempt("recon", "escalated", summary, {
957
- checkpoint: "recon_human_escalation",
958
- details: reconDetails
959
- });
960
- return checkpoint(
961
- "recon",
962
- "recon_human_escalation",
963
- summary,
964
- {
965
- ok: false,
966
- nextActions: ["inspect_recon_history", "summarize_failed_baselines", "ask_human_for_direction"],
967
- advanceOptions: ["recon", "author"],
968
- recommendedAdvanceStage: null,
969
- continueWithStage: null,
970
- blocking: true,
971
- details: reconDetails,
972
- reconAssessment: latestAssessment.raw,
973
- reconAssessmentRequest,
974
- executed
975
- }
976
- );
977
- }
978
- if ((latestAssessment.decision === "ready_for_author" || latestAssessment.continueWithStage === "author") && latestReconHasRequiredBaselines(state) && hasReconBaselineUnderstanding(state)) {
979
- updateState(config.statePath, (currentState) => {
980
- promoteLatestReconBaselines(currentState);
981
- currentState.recon_status = "ready_for_proof_plan";
982
- currentState.recon_results = currentState.recon_results || {};
983
- currentState.recon_results.status = "ready_for_proof_plan";
984
- currentState.recon_assessment_request = {};
985
- currentState.recon_decision_request = {};
986
- if ((currentState.proof_plan || "").trim() && (currentState.capture_script || "").trim()) {
987
- currentState.author_status = "ready";
988
- currentState.proof_plan_status = "ready";
989
- } else if (!authorReady(currentState)) {
990
- currentState.author_status = "needs_authoring";
991
- currentState.proof_plan_status = "needs_authoring";
992
- }
993
- });
994
- state = readState(config.statePath);
995
- const approvedSummary = latestAssessment.summary || "The supervising agent approved the latest recon baseline and selected the route for proof authoring.";
996
- if (params.advance_stage === "recon") {
997
- recordAttempt("recon", "completed", approvedSummary, {
998
- checkpoint: "recon_review",
999
- details: {
1000
- ...reconDetails,
1001
- promotedBaselines: latestReconCapturedBaselines(state)
1002
- }
1003
- });
1004
- return checkpoint(
1005
- "recon",
1006
- "recon_review",
1007
- approvedSummary,
1008
- {
1009
- nextActions: ["inspect_recon_baseline", "continue_internal_loop_with_checkpoint", "advance_run_to_author"],
1010
- advanceOptions: ["author", "recon", "implement"],
1011
- recommendedAdvanceStage: "author",
1012
- continueWithStage: "author",
1013
- blocking: false,
1014
- details: {
1015
- ...reconDetails,
1016
- promotedBaselines: latestReconCapturedBaselines(state)
1017
- },
1018
- reconAssessment: latestAssessment.raw,
1019
- executed
1020
- }
1021
- );
1022
- }
1023
- recordAttempt("recon", "completed", approvedSummary, {
1024
- checkpoint: "recon_auto_continue",
1025
- details: {
1026
- ...reconDetails,
1027
- promotedBaselines: latestReconCapturedBaselines(state)
1028
- }
1029
- });
1030
- effectiveAdvanceStage = "author";
1031
- updateState(config.statePath, (currentState) => {
1032
- currentState.last_requested_advance_stage = "author";
1033
- });
1034
- state = readState(config.statePath);
1035
- requestedStage = normalizeStageRequest(state, effectiveAdvanceStage);
1036
- } else if (latestAssessment.decision === "ready_for_author") {
1037
- const missingUnderstanding = latestReconHasRequiredBaselines(state) && !hasReconBaselineUnderstanding(state);
1038
- return checkpoint(
1039
- "recon",
1040
- "recon_supervisor_judgment",
1041
- missingUnderstanding ? "The supervising agent tried to approve recon, but did not provide a concrete baseline_understanding. The before evidence must be understood before proof authoring or code edits begin." : "The supervising agent tried to approve recon, but the latest attempt is still missing one or more required baseline screenshots. Retry recon with a better plan or declare the loop genuinely stuck.",
1042
- {
1043
- ok: false,
1044
- nextActions: ["inspect_recon_packet", "refine_recon_plan", "continue_internal_loop_with_checkpoint"],
1045
- advanceOptions: ["recon", "author"],
1046
- recommendedAdvanceStage: "recon",
1047
- continueWithStage: "recon",
1048
- blocking: false,
1049
- details: reconDetails,
1050
- reconAssessment: latestAssessment.raw,
1051
- reconAssessmentRequest,
1052
- executed
1053
- }
1054
- );
1055
- } else {
1056
- updateState(config.statePath, (currentState) => {
1057
- currentState.recon_status = "";
1058
- currentState.recon_assessment = {};
1059
- currentState.recon_assessment_source = null;
1060
- currentState.recon_assessment_request = {};
1061
- currentState.recon_decision_request = {};
1062
- currentState.before_cdn = "";
1063
- currentState.prod_cdn = "";
1064
- currentState.recon_results = currentState.recon_results || {};
1065
- currentState.recon_results.baselines = {};
1066
- currentState.recon_results.selected_attempt = {};
1067
- currentState.recon_results.status = "retry_requested";
1068
- });
1069
- state = readState(config.statePath);
1070
- }
1071
- }
1072
- if (!state?.recon_results || state?.stage === "setup" || state?.stage === "preflight" || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "") || requestedStage === "recon") {
1073
- const reconRes = runOne("recon");
1074
- executed.push(executedStep(reconRes));
1075
- if (!reconRes.ok || reconRes.haltedForApproval) {
1076
- return failedRun("recon", reconRes.haltedForApproval ? "recon halted for approval" : "recon failed", reconRes, {
1077
- checkpoint: "recon_failed",
1078
- details: { executed },
1079
- executed
1080
- });
1081
- }
1082
- state = readState(config.statePath);
1083
- if (["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) {
1084
- const reconAssessmentRequest = state?.recon_assessment_request || state?.recon_decision_request || null;
1085
- const summary = "Recon gathered route hints, candidate paths, baseline captures, and observations. The supervising agent should now judge whether the latest baseline is trustworthy, whether recon should retry/reframe, and whether recon is done.";
1086
- const reconDetails = {
1087
- executed,
1088
- latestAttempt: latestReconAttempt(state),
1089
- latestCapturedBaselines: latestReconCapturedBaselines(state),
1090
- reconAssessmentRequest
1091
- };
1092
- recordAttempt("recon", "checkpoint", summary, {
1093
- autoApproved: reconRes.autoApproved || false,
1094
- checkpoint: "recon_supervisor_judgment",
1095
- details: reconDetails
1096
- });
1097
- return checkpoint(
1098
- "recon",
1099
- "recon_supervisor_judgment",
1100
- summary,
1101
- {
1102
- nextActions: ["inspect_recon_packet", "supply_recon_assessment_json", "continue_internal_loop_with_checkpoint"],
1103
- advanceOptions: ["recon", "author"],
1104
- recommendedAdvanceStage: "recon",
1105
- continueWithStage: "recon",
1106
- blocking: false,
1107
- details: reconDetails,
1108
- reconAssessmentRequest,
1109
- reconDecisionRequest: state?.recon_decision_request || null,
1110
- executed
1111
- }
1112
- );
1113
- }
1114
- recordAttempt("recon", "completed", "Recon completed and promoted an approved baseline context.", {
1115
- autoApproved: reconRes.autoApproved || false,
1116
- details: { executed }
1117
- });
1118
- }
1119
- state = readState(config.statePath);
1120
- if (!authorReady(state) || effectiveAdvanceStage === "author") {
1121
- const authorRes = runOne("author");
1122
- executed.push(executedStep(authorRes));
1123
- if (!authorRes.ok || authorRes.haltedForApproval) {
1124
- return failedRun("author", authorRes.haltedForApproval ? "author halted for approval" : "author failed", authorRes, {
1125
- checkpoint: "author_failed",
1126
- details: { executed },
1127
- executed
1128
- });
1129
- }
1130
- state = readState(config.statePath);
1131
- if (!authorReady(state)) {
1132
- recordAttempt("author", "checkpoint", "Author prepared a supervisor judgment request instead of delegating proof authoring to an internal model.", {
1133
- autoApproved: authorRes.autoApproved || false,
1134
- checkpoint: "author_supervisor_judgment",
1135
- details: {
1136
- executed,
1137
- authorSummary: state?.author_summary || null,
1138
- authorRequest: state?.author_request || null,
1139
- serverPath: state?.server_path || null,
1140
- waitForSelector: state?.wait_for_selector || null
1141
- }
1142
- });
1143
- return checkpoint(
1144
- "author",
1145
- "author_supervisor_judgment",
1146
- "Author distilled recon into a proof-authoring request. The supervising agent should supply the proof packet, then resume the workflow.",
1147
- {
1148
- nextActions: ["inspect_author_request", "supply_author_packet_json_or_proof_plan", "continue_internal_loop_with_checkpoint"],
1149
- advanceOptions: ["author", "recon", "implement", "verify"],
1150
- recommendedAdvanceStage: "author",
1151
- continueWithStage: "author",
1152
- details: {
1153
- executed,
1154
- authorSummary: state?.author_summary || null,
1155
- authorRequest: state?.author_request || null,
1156
- proofPlanDraft: state?.author_request?.fallback_defaults?.proof_plan || null,
1157
- captureScriptDraft: state?.author_request?.fallback_defaults?.capture_script || null,
1158
- serverPathDraft: state?.author_request?.fallback_defaults?.server_path || null,
1159
- waitForSelectorDraft: state?.author_request?.fallback_defaults?.wait_for_selector || null
1160
- },
1161
- authorSummary: state?.author_summary || null,
1162
- authorRequest: state?.author_request || null,
1163
- proofPlanDraft: state?.author_request?.fallback_defaults?.proof_plan || null,
1164
- captureScriptDraft: state?.author_request?.fallback_defaults?.capture_script || null,
1165
- serverPathDraft: state?.author_request?.fallback_defaults?.server_path || null,
1166
- waitForSelectorDraft: state?.author_request?.fallback_defaults?.wait_for_selector || null,
1167
- executed
1168
- }
1169
- );
1170
- }
1171
- const noImplementationMode = !implementationRequired(params, state);
1172
- const authorNextStage = stageAfterAuthor(state, params);
1173
- const explicitAuthorDebug = params.advance_stage === "author";
1174
- recordAttempt("author", "completed", "Author applied the supervising agent's proof packet to recon observations.", {
1175
- autoApproved: authorRes.autoApproved || false,
1176
- checkpoint: explicitAuthorDebug ? "author_review" : "author_auto_continue",
1177
- details: {
1178
- executed,
1179
- authorSummary: state?.author_summary || null,
1180
- authorModel: state?.author_model || null,
1181
- authorRuntimeModelHint: state?.author_runtime_model_hint || null,
1182
- serverPath: state?.server_path || null,
1183
- waitForSelector: state?.wait_for_selector || null
1184
- }
1185
- });
1186
- if (explicitAuthorDebug) {
1187
- return checkpoint(
1188
- "author",
1189
- "author_review",
1190
- authorNextStage === "verify" ? noImplementationMode ? "Author applied the supervising agent's proof packet. Audit/no-diff mode disables implementation, so you can continue straight into verify." : "Author applied the supervising agent's proof packet. Because implementation is already recorded, you can continue straight into verify." : "Author applied the supervising agent's proof packet. Inspect it if needed, then continue into implement.",
1191
- {
1192
- nextActions: authorNextStage === "verify" ? ["inspect_proof_packet", "advance_run_to_verify", "rerun_author"] : ["inspect_proof_packet", "advance_run_to_implement", "rerun_author"],
1193
- advanceOptions: authorNextStage === "verify" ? ["author", "verify", "recon"] : ["author", "implement", "recon"],
1194
- recommendedAdvanceStage: authorNextStage,
1195
- continueWithStage: authorNextStage,
1196
- details: {
1197
- executed,
1198
- authorSummary: state?.author_summary || null,
1199
- authorModel: state?.author_model || null,
1200
- authorRuntimeModelHint: state?.author_runtime_model_hint || null,
1201
- proofPlan: state?.proof_plan || null,
1202
- serverPath: state?.server_path || null,
1203
- waitForSelector: state?.wait_for_selector || null
1204
- },
1205
- authorSummary: state?.author_summary || null,
1206
- authorModel: state?.author_model || null,
1207
- authorRuntimeModelHint: state?.author_runtime_model_hint || null,
1208
- proofPlan: state?.proof_plan || null,
1209
- serverPath: state?.server_path || null,
1210
- waitForSelector: state?.wait_for_selector || null,
1211
- executed
1212
- }
1213
- );
1214
- }
1215
- effectiveAdvanceStage = authorNextStage;
1216
- updateState(config.statePath, (currentState) => {
1217
- currentState.last_requested_advance_stage = authorNextStage;
1218
- });
1219
- state = readState(config.statePath);
1220
- }
1221
- if (!effectiveAdvanceStage) {
1222
- const recommended = recommendedAdvanceStage(state);
1223
- const noImplementationMode = !implementationRequired(params, state);
1224
- return checkpoint(
1225
- recommended || (noImplementationMode ? "verify" : "implement"),
1226
- "awaiting_stage_advance",
1227
- "Proof authoring is ready. The wrapper will not guess the next stage from here, explicitly choose whether to revisit recon/author, validate implementation, capture verify evidence, or ship.",
1228
- {
1229
- nextActions: ["inspect_state", "set_advance_stage", "resume_run"],
1230
- advanceOptions: noImplementationMode ? ["recon", "author", "verify", "ship"] : ["recon", "author", "implement", "verify", "ship"],
1231
- recommendedAdvanceStage: recommended,
1232
- details: { executed },
1233
- executed
1234
- }
1235
- );
1236
- }
1237
- if (effectiveAdvanceStage === "implement") {
1238
- if (!implementationRequired(params, state)) {
1239
- recordAttempt("implement", "checkpoint", "Implementation stage was skipped because audit/no-diff mode disables code changes.", {
1240
- checkpoint: "implement_disabled_for_audit",
1241
- details: { executed }
1242
- });
1243
- return checkpoint(
1244
- "verify",
1245
- "implement_disabled_for_audit",
1246
- "Audit/no-diff mode disables implementation. Continue to verify against the existing target; do not launch an implementation agent or require a git diff.",
1247
- {
1248
- nextActions: ["advance_run_to_verify", "inspect_author_packet", "rerun_recon_if_target_changed"],
1249
- advanceOptions: ["verify", "author", "recon"],
1250
- recommendedAdvanceStage: "verify",
1251
- continueWithStage: "verify",
1252
- blocking: false,
1253
- details: { executed },
1254
- executed
1255
- }
1256
- );
1257
- }
1258
- const implementRes = runOne("implement");
1259
- executed.push(executedStep(implementRes));
1260
- if (implementRes.haltedForApproval) {
1261
- return failedRun("implement", "implement halted for approval", implementRes, {
1262
- checkpoint: "implement_blocked",
1263
- details: { executed },
1264
- executed
1265
- });
1266
- }
1267
- if (!implementRes.ok) {
1268
- const implementError = `${implementRes.error || ""}
1269
- ${implementRes.stdout || ""}
1270
- ${implementRes.stderr || ""}`;
1271
- if (implementError.includes("No implementation detected")) {
1272
- const implementationState = readState(config.statePath) || {};
1273
- const implementationSummary = stringValue(implementationState?.implementation_summary) || null;
1274
- const implementationDetectionSummary = stringValue(implementationState?.implementation_detection_summary) || null;
1275
- const implementationDetection = implementationState?.implementation_detection && typeof implementationState.implementation_detection === "object" && !Array.isArray(implementationState.implementation_detection) ? implementationState.implementation_detection : null;
1276
- recordAttempt("implement", "checkpoint", "Implementation checkpoint found no material code changes yet.", {
1277
- checkpoint: "implement_changes_missing",
1278
- error: implementRes.error || null,
1279
- retryable: true,
1280
- checkpointDisposition: "retryable_implementation_gap",
1281
- details: {
1282
- executed,
1283
- implementationSummary,
1284
- implementationDetectionSummary,
1285
- implementationDetection
1286
- }
1287
- });
1288
- return checkpoint(
1289
- "implement",
1290
- "implement_changes_missing",
1291
- "Proof plan is ready, but code changes are not recorded yet. Make the implementation changes on the after worktree, then resume run.",
1292
- {
1293
- nextActions: ["make_code_changes", "rerun_implement"],
1294
- advanceOptions: ["implement", "author", "recon"],
1295
- recommendedAdvanceStage: "implement",
1296
- blocking: true,
1297
- retryable: true,
1298
- checkpointDisposition: "retryable_implementation_gap",
1299
- details: {
1300
- executed,
1301
- implementationSummary,
1302
- implementationDetectionSummary,
1303
- implementationDetection
1304
- },
1305
- implementationSummary,
1306
- implementationDetectionSummary,
1307
- implementationDetection,
1308
- executed
1309
- }
1310
- );
1311
- }
1312
- return failedRun("implement", "implement failed", implementRes, {
1313
- checkpoint: "implement_failed",
1314
- details: { executed },
1315
- executed
1316
- });
1317
- }
1318
- let invalidatedVerifyEvidence2 = false;
1319
- updateState(config.statePath, (state2) => {
1320
- invalidatedVerifyEvidence2 = invalidateVerifyEvidence(state2).invalidated;
1321
- });
1322
- recordAttempt("implement", "completed", "Implementation checkpoint recorded code changes on the after worktree.", {
1323
- autoApproved: implementRes.autoApproved || false,
1324
- checkpoint: "implement_review",
1325
- details: { executed, invalidatedVerifyEvidence: invalidatedVerifyEvidence2 }
1326
- });
1327
- return checkpoint(
1328
- "implement",
1329
- "implement_review",
1330
- invalidatedVerifyEvidence2 ? "Implementation changes were detected and prior verify evidence was invalidated. Inspect the branch diff or notes, then explicitly choose whether to iterate implementation again or advance to verify." : "Implementation changes were detected. Inspect the branch diff or notes, then explicitly choose whether to iterate implementation again or advance to verify.",
1331
- {
1332
- nextActions: ["inspect_branch_diff", "rerun_implement", "advance_run_to_verify"],
1333
- advanceOptions: ["implement", "author", "verify", "recon"],
1334
- recommendedAdvanceStage: "verify",
1335
- details: {
1336
- executed,
1337
- implementationSummary: readState(config.statePath)?.implementation_summary || null,
1338
- invalidatedVerifyEvidence: invalidatedVerifyEvidence2
1339
- },
1340
- implementationSummary: readState(config.statePath)?.implementation_summary || null,
1341
- invalidatedVerifyEvidence: invalidatedVerifyEvidence2,
1342
- executed
1343
- }
1344
- );
1345
- }
1346
- if (effectiveAdvanceStage === "verify") {
1347
- state = readState(config.statePath);
1348
- const needsImplementation = implementationRequired(params, state);
1349
- if (needsImplementation && !implementationReady(state)) {
1350
- return checkpoint(
1351
- "implement",
1352
- "implement_required",
1353
- "Verify is blocked until implementation has been recorded. Run the implement stage after making code changes, then resume verify.",
1354
- {
1355
- ok: false,
1356
- nextActions: ["make_code_changes", "advance_run_to_implement"],
1357
- advanceOptions: ["implement", "author", "recon"],
1358
- recommendedAdvanceStage: "implement",
1359
- continueWithStage: "implement",
1360
- blocking: true,
1361
- details: { executed },
1362
- executed
1363
- }
1364
- );
1365
- }
1366
- if (!needsImplementation && !implementationReady(state)) {
1367
- recordAttempt("implement", "completed", "Implementation stage is not required for this audit/no-diff run.", {
1368
- checkpoint: "implementation_not_required",
1369
- details: { executed }
1370
- });
1371
- state = updateState(config.statePath, (currentState) => {
1372
- currentState.implementation_status = "not_required";
1373
- currentState.implementation_mode = currentState.implementation_mode || "none";
1374
- if (currentState.require_diff === void 0) currentState.require_diff = false;
1375
- if (currentState.allow_code_changes === void 0) currentState.allow_code_changes = false;
1376
- });
1377
- }
1378
- const hasIncomingProofAssessment = typeof params.proof_assessment_json === "string" && params.proof_assessment_json.trim().length > 0;
1379
- const canReuseVerifyEvidence = (params.advance_stage !== "verify" || hasIncomingProofAssessment) && state?.verify_status === "evidence_captured" && Boolean((state?.after_cdn || "").trim()) && (state?.active_checkpoint === "verify_supervisor_judgment" || hasSupervisorProofAssessment(state));
1380
- let verifyRes = { ok: true, step: "verify", reusedEvidence: canReuseVerifyEvidence };
1381
- if (!canReuseVerifyEvidence) {
1382
- verifyRes = runOne("verify");
1383
- executed.push(executedStep(verifyRes));
1384
- if (!verifyRes.ok || verifyRes.haltedForApproval) {
1385
- return failedRun("verify", verifyRes.haltedForApproval ? "verify halted for approval" : "verify failed", verifyRes, {
1386
- checkpoint: "verify_failed",
1387
- details: { executed },
1388
- executed
1389
- });
1390
- }
1391
- } else {
1392
- executed.push(executedStep(verifyRes, { reusedEvidence: true }));
1393
- }
1394
- state = readState(config.statePath);
1395
- const verifyStatus = state?.verify_status || ((state?.after_cdn || "").trim() ? "evidence_captured" : "capture_incomplete");
1396
- const verifyDecisionRequest = state?.verify_decision_request || null;
1397
- const verifySummary = state?.verify_summary || state?.proof_summary || null;
1398
- const proofAssessment = verifyAssessment(state);
1399
- const convergenceSignals = nonConvergenceSignals(state, proofAssessment);
1400
- const rawVerifyRecommendedStage = proofAssessment.recommendedStage || null;
1401
- const verifyRecommendedStage = !needsImplementation && rawVerifyRecommendedStage === "implement" ? "verify" : rawVerifyRecommendedStage;
1402
- const rawVerifyContinueWithStage = shouldEscalateVerifyToHuman(state, proofAssessment) ? null : proofAssessment.continueWithStage || verifyRecommendedStage || null;
1403
- const verifyContinueWithStage = !needsImplementation && rawVerifyContinueWithStage === "implement" ? "verify" : rawVerifyContinueWithStage;
1404
- const verifyLoopAdvanceOptions = needsImplementation ? ["author", "verify", "implement", "recon"] : ["author", "verify", "recon"];
1405
- const verifyReviewAdvanceOptions = needsImplementation ? ["verify", "author", "implement", "recon", "ship"] : ["verify", "author", "recon"];
1406
- const verifyRetryAdvanceOptions = needsImplementation ? ["author", "implement", "ship", "verify", "recon"] : ["author", "verify", "recon"];
1407
- const verifyDetails = {
1408
- executed,
1409
- verifyStatus,
1410
- verifySummary,
1411
- afterCdn: state?.after_cdn || null,
1412
- mergeRecommendation: state?.merge_recommendation || null,
1413
- verifyDecisionRequest,
1414
- proofAssessment: proofAssessment.raw,
1415
- proofAssessmentSource: proofAssessment.source || null,
1416
- proofAssessmentRequest: state?.proof_assessment_request || null,
1417
- verifyRecommendedStage,
1418
- verifyContinueWithStage,
1419
- convergenceSignals
1420
- };
1421
- if (verifyStatus !== "evidence_captured") {
1422
- if ((verifyContinueWithStage || verifyRecommendedStage || "author") === "author") {
1423
- updateState(config.statePath, (currentState) => {
1424
- currentState.author_status = "needs_authoring";
1425
- currentState.proof_plan_status = "needs_authoring";
1426
- currentState.supervisor_author_packet = null;
1427
- });
1428
- state = readState(config.statePath);
1429
- }
1430
- const checkpointName = "verify_capture_retry";
1431
- const summary = "Verify ran, but the proof packet still needs internal capture-plan work before it should ship.";
1432
- recordAttempt("verify", "checkpoint", summary, {
1433
- autoApproved: verifyRes.autoApproved || false,
1434
- checkpoint: checkpointName,
1435
- details: verifyDetails
1436
- });
1437
- return checkpoint(
1438
- "verify",
1439
- checkpointName,
1440
- summary,
1441
- {
1442
- ok: true,
1443
- nextActions: ["inspect_after_capture", "continue_internal_loop_with_checkpoint", "return_to_recon_if_baseline_is_wrong"],
1444
- advanceOptions: verifyLoopAdvanceOptions,
1445
- recommendedAdvanceStage: verifyRecommendedStage || "author",
1446
- continueWithStage: verifyContinueWithStage || "author",
1447
- blocking: false,
1448
- details: verifyDetails,
1449
- verifyStatus,
1450
- verifySummary,
1451
- afterCdn: state?.after_cdn || null,
1452
- mergeRecommendation: state?.merge_recommendation || null,
1453
- verifyDecisionRequest,
1454
- proofAssessment: proofAssessment.raw,
1455
- executed
1456
- }
1457
- );
1458
- }
1459
- if (!hasSupervisorProofAssessment(state)) {
1460
- const summary = "Verify captured usable evidence. The supervising agent should now assess whether the proof supports ship or more internal iteration, then resume the workflow with proof_assessment_json.";
1461
- recordAttempt("verify", "checkpoint", summary, {
1462
- autoApproved: verifyRes.autoApproved || false,
1463
- checkpoint: "verify_supervisor_judgment",
1464
- details: verifyDetails
1465
- });
1466
- return checkpoint(
1467
- "verify",
1468
- "verify_supervisor_judgment",
1469
- summary,
1470
- {
1471
- nextActions: ["inspect_evidence", "author_proof_assessment_json", "continue_internal_loop_with_checkpoint"],
1472
- advanceOptions: verifyReviewAdvanceOptions,
1473
- recommendedAdvanceStage: "verify",
1474
- continueWithStage: "verify",
1475
- blocking: false,
1476
- details: verifyDetails,
1477
- verifyStatus,
1478
- verifySummary,
1479
- afterCdn: state?.after_cdn || null,
1480
- mergeRecommendation: state?.merge_recommendation || null,
1481
- verifyDecisionRequest,
1482
- proofAssessmentRequest: state?.proof_assessment_request || null,
1483
- executed
1484
- }
1485
- );
1486
- }
1487
- const shouldEscalate = shouldEscalateVerifyToHuman(state, proofAssessment);
1488
- if (shouldEscalate) {
1489
- const summary = "The supervising agent concluded the workflow hit a real wall and explicitly escalated the proof loop to the human.";
1490
- recordAttempt("verify", "escalated", summary, {
1491
- autoApproved: verifyRes.autoApproved || false,
1492
- checkpoint: "verify_human_escalation",
1493
- details: verifyDetails
1494
- });
1495
- return checkpoint(
1496
- "verify",
1497
- "verify_human_escalation",
1498
- summary,
1499
- {
1500
- ok: false,
1501
- nextActions: ["inspect_retry_history", "summarize_internal_loop", "ask_human_for_direction"],
1502
- advanceOptions: verifyRetryAdvanceOptions,
1503
- recommendedAdvanceStage: null,
1504
- continueWithStage: null,
1505
- blocking: true,
1506
- details: verifyDetails,
1507
- verifyStatus,
1508
- verifySummary,
1509
- afterCdn: state?.after_cdn || null,
1510
- mergeRecommendation: state?.merge_recommendation || null,
1511
- verifyDecisionRequest,
1512
- proofAssessment: proofAssessment.raw,
1513
- executed
1514
- }
1515
- );
1516
- }
1517
- const shouldAutoShip = verifyContinueWithStage === "ship" && needsImplementation && (params.ship_after_verify || params.continue_from_checkpoint || params.advance_stage !== "verify");
1518
- if (shouldAutoShip) {
1519
- const shipGate = validateShipGate(state);
1520
- if (!shipGate.ok) {
1521
- recordAttempt("verify", "checkpoint", "Verify cannot continue into ship because the hard ship gate is missing required evidence or approval.", {
1522
- autoApproved: verifyRes.autoApproved || false,
1523
- checkpoint: "ship_gate_blocked",
1524
- details: { ...verifyDetails, shipGate }
1525
- });
1526
- return shipGateBlocked(state, executed, verifyDetails);
1527
- }
1528
- recordAttempt("verify", "checkpoint", "Verify captured a strong proof packet and is continuing directly into ship.", {
1529
- autoApproved: verifyRes.autoApproved || false,
1530
- checkpoint: "verify_then_ship",
1531
- details: { ...verifyDetails, shipGate }
1532
- });
1533
- const shipRes = runOne("ship");
1534
- executed.push(executedStep(shipRes));
1535
- if (!shipRes.ok || shipRes.haltedForApproval) {
1536
- const shipNextAction = shipRes?.error && String(shipRes.error).includes("temporary proof branch") ? "product bug: ship resolved a temporary proof branch; resolve the PR head branch before retrying ship" : "inspect the ship error, confirm the PR head branch and verified commit, then retry ship";
1537
- return failedRun("ship", shipRes.haltedForApproval ? "ship halted for approval" : "ship failed", shipRes, {
1538
- checkpoint: "ship_failed",
1539
- details: { executed, next_action: shipNextAction },
1540
- nextAction: shipNextAction,
1541
- executed
1542
- });
1543
- }
1544
- recordAttempt("ship", "completed", "Ship updated the PR and posted proof artifacts after the supervising agent judged the proof strong enough.", {
1545
- autoApproved: shipRes.autoApproved || false,
1546
- checkpoint: "ship_review",
1547
- details: { executed }
1548
- });
1549
- const snapshot2 = snapshotFor(config.statePath);
1550
- const summary = "The supervising agent judged the proof strong enough, so the workflow shipped automatically and left the PR as the main human review surface.";
1551
- const finalState = readState(config.statePath);
1552
- const shipReport = finalState?.ship_report || snapshot2.state?.ship_report || null;
1553
- return {
1554
- ok: true,
1555
- action,
1556
- state_path: config.statePath,
1557
- stage: snapshot2.stage,
1558
- checkpoint: "ship_review",
1559
- summary,
1560
- state: snapshot2.state,
1561
- shipReport,
1562
- checkpointContract: buildCheckpointContract(readState(config.statePath), {
1563
- statePath: config.statePath,
1564
- stage: "ship",
1565
- checkpoint: "ship_review",
1566
- summary,
1567
- nextActions: ["inspect_pr", "rerun_ship_if_needed"],
1568
- advanceOptions: ["ship", "verify", "author", "implement"],
1569
- recommendedAdvanceStage: "ship"
1570
- }),
1571
- executed
1572
- };
1573
- }
1574
- if (proofAssessment.decision === "ready_to_ship") {
1575
- if (!needsImplementation) {
1576
- recordAttempt("verify", "completed", "Verify captured a proof packet for audit/no-diff mode; shipping remains disabled.", {
1577
- autoApproved: verifyRes.autoApproved || false,
1578
- checkpoint: "verify_audit_complete",
1579
- details: verifyDetails
1580
- });
1581
- return checkpoint(
1582
- "verify",
1583
- "verify_audit_complete",
1584
- "The supervising agent judged the audit proof sufficient. Audit/no-diff mode disables ship, PR creation, implementation agents, and git-diff requirements.",
1585
- {
1586
- nextActions: ["inspect_evidence", "report_audit_result", "rerun_verify_if_needed"],
1587
- advanceOptions: ["verify", "author", "recon"],
1588
- recommendedAdvanceStage: "verify",
1589
- continueWithStage: null,
1590
- blocking: false,
1591
- details: verifyDetails,
1592
- verifyStatus,
1593
- verifySummary,
1594
- afterCdn: state?.after_cdn || null,
1595
- mergeRecommendation: state?.merge_recommendation || null,
1596
- verifyDecisionRequest,
1597
- proofAssessment: proofAssessment.raw,
1598
- executed
1599
- }
1600
- );
1601
- }
1602
- const shipGate = validateShipGate(state);
1603
- if (!shipGate.ok) {
1604
- recordAttempt("verify", "checkpoint", "Verify cannot mark ship ready because the hard ship gate is missing required evidence or approval.", {
1605
- autoApproved: verifyRes.autoApproved || false,
1606
- checkpoint: "ship_gate_blocked",
1607
- details: { ...verifyDetails, shipGate }
1608
- });
1609
- return shipGateBlocked(state, executed, verifyDetails);
1610
- }
1611
- recordAttempt("verify", "checkpoint", "Verify captured a strong proof packet and is ready to continue into ship.", {
1612
- autoApproved: verifyRes.autoApproved || false,
1613
- checkpoint: "verify_ship_ready",
1614
- details: { ...verifyDetails, shipGate }
1615
- });
1616
- return checkpoint(
1617
- "verify",
1618
- "verify_ship_ready",
1619
- "The supervising agent judged the proof strong enough to continue into ship.",
1620
- {
1621
- nextActions: ["inspect_evidence", "continue_internal_loop_with_checkpoint", "advance_run_to_ship_if_you_need_manual_control"],
1622
- advanceOptions: ["ship", "verify", "author", "implement", "recon"],
1623
- recommendedAdvanceStage: "ship",
1624
- continueWithStage: "ship",
1625
- blocking: false,
1626
- details: { ...verifyDetails, shipGate },
1627
- shipGate,
1628
- verifyStatus,
1629
- verifySummary,
1630
- afterCdn: state?.after_cdn || null,
1631
- mergeRecommendation: state?.merge_recommendation || null,
1632
- verifyDecisionRequest,
1633
- proofAssessment: proofAssessment.raw,
1634
- executed
1635
- }
1636
- );
1637
- }
1638
- if (verifyContinueWithStage === "author") {
1639
- updateState(config.statePath, (currentState) => {
1640
- currentState.author_status = "needs_authoring";
1641
- currentState.proof_plan_status = "needs_authoring";
1642
- currentState.supervisor_author_packet = null;
1643
- });
1644
- state = readState(config.statePath);
1645
- }
1646
- const unresolvedSummary = convergenceSignals.warning ? "The supervising agent kept the workflow in the internal loop, but retry history suggests it may not be converging yet. Keep iterating internally or explicitly escalate with escalation_target=human when you conclude it is genuinely stuck." : "The supervising agent judged that the workflow should keep iterating internally before it ships.";
1647
- recordAttempt("verify", "checkpoint", unresolvedSummary, {
1648
- autoApproved: verifyRes.autoApproved || false,
1649
- checkpoint: "verify_agent_retry",
1650
- details: verifyDetails
1651
- });
1652
- return checkpoint(
1653
- "verify",
1654
- "verify_agent_retry",
1655
- unresolvedSummary,
1656
- {
1657
- ok: true,
1658
- nextActions: convergenceSignals.warning ? ["inspect_retry_history", "decide_whether_to_keep_iterating_or_escalate", "continue_internal_loop_with_checkpoint"] : needsImplementation ? ["inspect_proof_assessment", "continue_internal_loop_with_checkpoint", "return_to_implement_if_fix_failed"] : ["inspect_proof_assessment", "continue_internal_loop_with_checkpoint", "rerun_author_if_proof_contract_is_wrong"],
1659
- advanceOptions: verifyRetryAdvanceOptions,
1660
- recommendedAdvanceStage: verifyRecommendedStage,
1661
- continueWithStage: verifyContinueWithStage,
1662
- blocking: false,
1663
- details: verifyDetails,
1664
- verifyStatus,
1665
- verifySummary,
1666
- afterCdn: state?.after_cdn || null,
1667
- mergeRecommendation: state?.merge_recommendation || null,
1668
- verifyDecisionRequest,
1669
- proofAssessment: proofAssessment.raw,
1670
- executed
1671
- }
1672
- );
1673
- }
1674
- if (effectiveAdvanceStage === "ship") {
1675
- state = readState(config.statePath);
1676
- if (!implementationRequired(params, state)) {
1677
- return checkpoint(
1678
- "verify",
1679
- "ship_disabled_for_audit",
1680
- "Audit/no-diff mode disables ship and PR creation. Report the audit result from verify evidence instead.",
1681
- {
1682
- ok: false,
1683
- nextActions: ["inspect_verify_state", "report_audit_result"],
1684
- advanceOptions: ["verify", "author", "recon"],
1685
- recommendedAdvanceStage: "verify",
1686
- continueWithStage: "verify",
1687
- blocking: true,
1688
- details: { executed },
1689
- executed
1690
- }
1691
- );
1692
- }
1693
- const shipAssessment = verifyAssessment(state);
1694
- const shipGate = validateShipGate(state);
1695
- if (state?.verify_status !== "evidence_captured") {
1696
- return checkpoint(
1697
- "verify",
1698
- "verify_required",
1699
- "Ship is blocked until verify has captured a usable proof packet. Run verify, inspect the evidence, then explicitly advance to ship only if the proof supports success.",
1700
- {
1701
- ok: false,
1702
- nextActions: ["advance_run_to_verify", "inspect_verify_state"],
1703
- advanceOptions: ["verify", "author", "implement", "recon"],
1704
- recommendedAdvanceStage: "verify",
1705
- continueWithStage: "verify",
1706
- blocking: true,
1707
- details: {
1708
- executed,
1709
- shipGate,
1710
- verifyStatus: state?.verify_status || null,
1711
- mergeRecommendation: state?.merge_recommendation || null,
1712
- afterCdn: state?.after_cdn || null
1713
- },
1714
- shipGate,
1715
- verifyStatus: state?.verify_status || null,
1716
- mergeRecommendation: state?.merge_recommendation || null,
1717
- afterCdn: state?.after_cdn || null,
1718
- executed
1719
- }
1720
- );
1721
- }
1722
- if (!hasSupervisorProofAssessment(state) || shipAssessment.decision !== "ready_to_ship") {
1723
- return checkpoint(
1724
- "verify",
1725
- "verify_supervisor_judgment_required",
1726
- "Ship is blocked until the supervising agent judges the current proof packet as ready_to_ship.",
1727
- {
1728
- ok: false,
1729
- nextActions: ["inspect_evidence", "supply_proof_assessment_json", "continue_internal_loop_with_checkpoint"],
1730
- advanceOptions: ["verify", "author", "implement", "recon", "ship"],
1731
- recommendedAdvanceStage: "verify",
1732
- continueWithStage: "verify",
1733
- blocking: true,
1734
- details: {
1735
- executed,
1736
- shipGate,
1737
- verifyStatus: state?.verify_status || null,
1738
- mergeRecommendation: state?.merge_recommendation || null,
1739
- afterCdn: state?.after_cdn || null,
1740
- proofAssessment: state?.proof_assessment || null,
1741
- proofAssessmentRequest: state?.proof_assessment_request || null
1742
- },
1743
- shipGate,
1744
- verifyStatus: state?.verify_status || null,
1745
- mergeRecommendation: state?.merge_recommendation || null,
1746
- afterCdn: state?.after_cdn || null,
1747
- proofAssessment: state?.proof_assessment || null,
1748
- proofAssessmentRequest: state?.proof_assessment_request || null,
1749
- executed
1750
- }
1751
- );
1752
- }
1753
- if (!shipGate.ok) {
1754
- return shipGateBlocked(state, executed, { shipAssessment: shipAssessment.raw });
1755
- }
1756
- const shipRes = runOne("ship");
1757
- executed.push(executedStep(shipRes));
1758
- if (!shipRes.ok || shipRes.haltedForApproval) {
1759
- return failedRun("ship", shipRes.haltedForApproval ? "ship halted for approval" : "ship failed", shipRes, {
1760
- checkpoint: "ship_failed",
1761
- details: { executed },
1762
- executed
1763
- });
1764
- }
1765
- recordAttempt("ship", "completed", "Ship updated the PR and posted proof artifacts.", {
1766
- autoApproved: shipRes.autoApproved || false,
1767
- checkpoint: "ship_review",
1768
- details: { executed }
1769
- });
1770
- return checkpoint(
1771
- "ship",
1772
- "ship_review",
1773
- "Ship completed. Review the PR, proof comment, and cleanup results. Re-run ship if you need to refresh the PR after more changes.",
1774
- {
1775
- nextActions: ["inspect_pr", "rerun_ship_if_needed"],
1776
- advanceOptions: ["ship", "verify", "author", "implement"],
1777
- recommendedAdvanceStage: "ship",
1778
- details: {
1779
- executed,
1780
- prUrl: readState(config.statePath)?.pr_url || null
1781
- },
1782
- prUrl: readState(config.statePath)?.pr_url || null,
1783
- executed
1784
- }
1785
- );
1786
- }
1787
- }
1788
- if (action === "ship") {
1789
- const state = readState(config.statePath);
1790
- const shipGate = validateShipGate(state);
1791
- if (state?.verify_status !== "evidence_captured") {
1792
- return checkpoint(
1793
- "verify",
1794
- "verify_required",
1795
- "Ship is blocked until verify has captured a usable proof packet. Run verify, inspect the evidence, then ship only if the proof supports success.",
1796
- {
1797
- ok: false,
1798
- nextActions: ["run_verify", "inspect_verify_state"],
1799
- advanceOptions: ["verify", "author", "implement", "recon"],
1800
- recommendedAdvanceStage: "verify",
1801
- continueWithStage: "verify",
1802
- blocking: true,
1803
- details: { shipGate },
1804
- shipGate,
1805
- verifyStatus: state?.verify_status || null,
1806
- mergeRecommendation: state?.merge_recommendation || null,
1807
- afterCdn: state?.after_cdn || null
1808
- }
1809
- );
1810
- }
1811
- if (!hasSupervisorProofAssessment(state) || verifyAssessment(state).decision !== "ready_to_ship") {
1812
- return checkpoint(
1813
- "verify",
1814
- "verify_supervisor_judgment_required",
1815
- "Ship is blocked until the supervising agent judges the current proof packet as ready_to_ship.",
1816
- {
1817
- ok: false,
1818
- nextActions: ["inspect_evidence", "supply_proof_assessment_json", "rerun_ship"],
1819
- advanceOptions: ["verify", "author", "implement", "recon", "ship"],
1820
- recommendedAdvanceStage: "verify",
1821
- continueWithStage: "verify",
1822
- blocking: true,
1823
- details: { shipGate },
1824
- shipGate,
1825
- verifyStatus: state?.verify_status || null,
1826
- mergeRecommendation: state?.merge_recommendation || null,
1827
- afterCdn: state?.after_cdn || null,
1828
- proofAssessment: state?.proof_assessment || null,
1829
- proofAssessmentRequest: state?.proof_assessment_request || null
1830
- }
1831
- );
1832
- }
1833
- if (!shipGate.ok) {
1834
- return shipGateBlocked(state, [], {});
1835
- }
1836
- }
1837
- const single = runOne(action);
1838
- if (!single.ok || single.haltedForApproval) {
1839
- return failedRun(action, single.haltedForApproval ? `${action} halted for approval` : `${action} failed`, single, {
1840
- checkpoint: `${action}_failed`
1841
- });
1842
- }
1843
- let invalidatedVerifyEvidence = false;
1844
- updateState(config.statePath, (state) => {
1845
- if (action === "implement") {
1846
- invalidatedVerifyEvidence = invalidateVerifyEvidence(state).invalidated;
1847
- }
1848
- clearStageDecisionRequest(state);
1849
- });
1850
- const singleSummary = action === "implement" && invalidatedVerifyEvidence ? "implement completed and invalidated prior verify evidence" : `${action} completed`;
1851
- recordAttempt(action, "completed", singleSummary, {
1852
- autoApproved: single.autoApproved || false,
1853
- details: action === "implement" ? { invalidatedVerifyEvidence } : {}
1854
- });
1855
- const snapshot = snapshotFor(config.statePath);
1856
- return {
1857
- ok: true,
1858
- action,
1859
- state_path: config.statePath,
1860
- stage: snapshot.stage,
1861
- summary: singleSummary,
1862
- state: snapshot.state,
1863
- approval: null,
1864
- autoApproved: single.autoApproved || false,
1865
- error: null
1866
- };
1867
- }
1868
- function createRiddleProofEngine(pluginConfig = {}) {
1869
- return {
1870
- execute(params) {
1871
- const config = resolveConfig(pluginConfig, params);
1872
- return executeWorkflow(params, pluginConfig, config);
1873
- },
1874
- status(statePath) {
1875
- const config = resolveConfig(pluginConfig, { action: "status", state_path: statePath });
1876
- return executeWorkflow({ action: "status", state_path: statePath }, pluginConfig, config);
1877
- },
1878
- resolveConfig(params = {}) {
1879
- return resolveConfig(pluginConfig, params);
1880
- }
1881
- };
1882
- }
2
+ createRiddleProofEngine,
3
+ executeWorkflow
4
+ } from "./chunk-BHL4JSGM.js";
5
+ import "./chunk-TJ63IE65.js";
6
+ import "./chunk-MLKGABMK.js";
1883
7
  export {
1884
8
  createRiddleProofEngine,
1885
9
  executeWorkflow