@riddledc/riddle-proof 0.7.226 → 0.8.1

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 (132) 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-QUAZAMOM.js → chunk-IWLQQ5S5.js} +12 -2
  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.d.cts +1 -0
  56. package/dist/cli.d.ts +1 -0
  57. package/dist/cli.js +8 -4139
  58. package/dist/codex-exec-agent.d.cts +1 -1
  59. package/dist/codex-exec-agent.d.ts +1 -1
  60. package/dist/codex-exec-agent.js +1 -0
  61. package/dist/diagnostics.js +1 -0
  62. package/dist/engine-harness-CMACHP6A.d.ts +96 -0
  63. package/dist/engine-harness-LBfqbFSe.d.cts +96 -0
  64. package/dist/engine-harness.d.cts +2 -79
  65. package/dist/engine-harness.d.ts +2 -79
  66. package/dist/engine-harness.js +3 -2
  67. package/dist/index.d.cts +2 -2
  68. package/dist/index.d.ts +2 -2
  69. package/dist/index.js +4 -3
  70. package/dist/local-agent.d.cts +1 -1
  71. package/dist/local-agent.d.ts +1 -1
  72. package/dist/local-agent.js +1 -0
  73. package/dist/openclaw.js +8 -107
  74. package/dist/playability.js +1 -0
  75. package/dist/profile/index.cjs +9403 -0
  76. package/dist/profile/index.d.cts +2 -0
  77. package/dist/profile/index.d.ts +2 -0
  78. package/dist/profile/index.js +53 -0
  79. package/dist/profile.js +1 -0
  80. package/dist/proof-run-core-CE0jx7wL.d.cts +359 -0
  81. package/dist/proof-run-core-CE0jx7wL.d.ts +359 -0
  82. package/dist/proof-run-core.d.cts +1 -322
  83. package/dist/proof-run-core.d.ts +1 -322
  84. package/dist/proof-run-core.js +2 -1
  85. package/dist/proof-run-engine-CSSc0mNn.d.ts +764 -0
  86. package/dist/proof-run-engine-HSRpUeBi.d.cts +764 -0
  87. package/dist/proof-run-engine.d.cts +2 -757
  88. package/dist/proof-run-engine.d.ts +2 -757
  89. package/dist/proof-run-engine.js +5 -1885
  90. package/dist/proof-session.js +1 -0
  91. package/dist/result.js +1 -0
  92. package/dist/riddle-client.js +1 -0
  93. package/dist/run-card.js +1 -0
  94. package/dist/runner-4LJ5z0D-.d.cts +29 -0
  95. package/dist/runner-BdQpOkZD.d.ts +29 -0
  96. package/dist/runner.d.cts +2 -22
  97. package/dist/runner.d.ts +2 -22
  98. package/dist/runner.js +3 -2
  99. package/dist/runtime/index.cjs +528 -0
  100. package/dist/runtime/index.d.cts +1 -0
  101. package/dist/runtime/index.d.ts +1 -0
  102. package/dist/runtime/index.js +37 -0
  103. package/dist/runtime/riddle-client.cjs +528 -0
  104. package/dist/runtime/riddle-client.d.cts +1 -0
  105. package/dist/runtime/riddle-client.d.ts +1 -0
  106. package/dist/runtime/riddle-client.js +37 -0
  107. package/dist/spec/checkpoint.cjs +807 -0
  108. package/dist/spec/checkpoint.d.cts +2 -0
  109. package/dist/spec/checkpoint.d.ts +2 -0
  110. package/dist/spec/checkpoint.js +34 -0
  111. package/dist/spec/index.cjs +1510 -0
  112. package/dist/spec/index.d.cts +5 -0
  113. package/dist/spec/index.d.ts +5 -0
  114. package/dist/spec/index.js +80 -0
  115. package/dist/spec/result.cjs +259 -0
  116. package/dist/spec/result.d.cts +2 -0
  117. package/dist/spec/result.d.ts +2 -0
  118. package/dist/spec/result.js +21 -0
  119. package/dist/spec/run-card.cjs +287 -0
  120. package/dist/spec/run-card.d.cts +2 -0
  121. package/dist/spec/run-card.d.ts +2 -0
  122. package/dist/spec/run-card.js +11 -0
  123. package/dist/spec/state.cjs +541 -0
  124. package/dist/spec/state.d.cts +2 -0
  125. package/dist/spec/state.d.ts +2 -0
  126. package/dist/spec/state.js +28 -0
  127. package/dist/spec/types.cjs +18 -0
  128. package/dist/spec/types.d.cts +1 -0
  129. package/dist/spec/types.d.ts +1 -0
  130. package/dist/spec/types.js +1 -0
  131. package/dist/state.js +1 -0
  132. package/package.json +95 -2
@@ -0,0 +1,2905 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/advanced/proof-run-engine.ts
31
+ var proof_run_engine_exports = {};
32
+ __export(proof_run_engine_exports, {
33
+ createRiddleProofEngine: () => createRiddleProofEngine,
34
+ executeWorkflow: () => executeWorkflow
35
+ });
36
+ module.exports = __toCommonJS(proof_run_engine_exports);
37
+
38
+ // src/proof-run-engine.ts
39
+ var import_node_child_process = require("child_process");
40
+ var import_node_fs2 = require("fs");
41
+ var import_node_path2 = __toESM(require("path"), 1);
42
+
43
+ // src/proof-run-core.ts
44
+ var import_node_fs = require("fs");
45
+ var import_node_crypto = require("crypto");
46
+ var import_node_path = __toESM(require("path"), 1);
47
+ var import_node_url = require("url");
48
+ var import_meta = {};
49
+ var WORKFLOW_STAGE_ORDER = ["setup", "recon", "author", "implement", "verify", "ship"];
50
+ function normalizedMode(value) {
51
+ return typeof value === "string" ? value.trim().toLowerCase() : "";
52
+ }
53
+ function previewModeFromWorkflowMode(value) {
54
+ const mode = normalizedMode(value);
55
+ return mode === "server" || mode === "static" ? mode : void 0;
56
+ }
57
+ function noImplementationModeInput(value) {
58
+ return value && typeof value === "object" ? value : {};
59
+ }
60
+ function noImplementationModeFor(params, state) {
61
+ const input = noImplementationModeInput(params);
62
+ const stateInput = noImplementationModeInput(state);
63
+ const mode = normalizedMode(input.mode ?? input.workflow_mode ?? stateInput.mode ?? stateInput.workflow_mode);
64
+ const implementationMode = normalizedMode(input.implementation_mode ?? stateInput.implementation_mode);
65
+ const requireDiff = input.require_diff ?? stateInput.require_diff;
66
+ const allowCodeChanges = input.allow_code_changes ?? stateInput.allow_code_changes;
67
+ return mode === "audit" || mode === "profile" || implementationMode === "none" || requireDiff === false || allowCodeChanges === false;
68
+ }
69
+ function canRunSetupWithoutRepo(params) {
70
+ return noImplementationModeFor(params) && Boolean((params.prod_url || "").trim());
71
+ }
72
+ var CHECKPOINT_CONTRACT_VERSION = "riddle-proof-run.checkpoint.v1";
73
+ function currentDistDir() {
74
+ const meta = typeof import_meta === "object" ? import_meta : {};
75
+ if (typeof meta.url === "string" && meta.url) {
76
+ return import_node_path.default.dirname((0, import_node_url.fileURLToPath)(meta.url));
77
+ }
78
+ if (typeof __dirname === "string") return __dirname;
79
+ return process.cwd();
80
+ }
81
+ var BUNDLED_RIDDLE_PROOF_DIR = import_node_path.default.resolve(
82
+ currentDistDir(),
83
+ "..",
84
+ "runtime"
85
+ );
86
+ var RIDDLE_PROOF_DIR_CANDIDATES = [
87
+ BUNDLED_RIDDLE_PROOF_DIR
88
+ ];
89
+ function createWorkflowStatePath() {
90
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:.TZ]/g, "").slice(0, 14);
91
+ return `/tmp/riddle-proof-state-${stamp}-${(0, import_node_crypto.randomUUID)().slice(0, 8)}.json`;
92
+ }
93
+ function argsPathForStatePath(statePath) {
94
+ const base = import_node_path.default.basename(statePath);
95
+ if (base.startsWith("riddle-proof-state")) {
96
+ return import_node_path.default.join(import_node_path.default.dirname(statePath), base.replace("riddle-proof-state", "riddle-proof-args"));
97
+ }
98
+ return import_node_path.default.join(import_node_path.default.dirname(statePath), `${base}.args.json`);
99
+ }
100
+ function isRiddleProofSkillDir(candidate) {
101
+ return (0, import_node_fs.existsSync)(workflowFile(candidate, "setup"));
102
+ }
103
+ function resolveRiddleProofDir(config = {}) {
104
+ if (config.riddleProofDir) return config.riddleProofDir;
105
+ const candidates = [
106
+ process.env.RIDDLE_PROOF_DIR,
107
+ ...RIDDLE_PROOF_DIR_CANDIDATES
108
+ ].filter((candidate) => Boolean(candidate));
109
+ return candidates.find(isRiddleProofSkillDir) || candidates.find((candidate) => (0, import_node_fs.existsSync)(candidate)) || RIDDLE_PROOF_DIR_CANDIDATES[0];
110
+ }
111
+ function resolveConfig(config = {}, params = {}) {
112
+ const configuredStatePath = config.statePath || "";
113
+ const shouldIsolateWorkflow = !params.state_path && !configuredStatePath && (params.action === "setup" || params.action === "run");
114
+ const statePath = params.state_path || (shouldIsolateWorkflow ? createWorkflowStatePath() : configuredStatePath || "/tmp/riddle-proof-state.json");
115
+ return {
116
+ riddleProofDir: resolveRiddleProofDir(config),
117
+ statePath,
118
+ argsPath: argsPathForStatePath(statePath),
119
+ defaultReviewer: config.defaultReviewer || "davisdiehl"
120
+ };
121
+ }
122
+ function ensureAction(action) {
123
+ if (["setup", "recon", "author", "implement", "verify", "ship", "status", "sync", "run"].includes(action)) {
124
+ return action;
125
+ }
126
+ throw new Error(`Unsupported action: ${action}`);
127
+ }
128
+ function workflowFile(riddleProofDir, action) {
129
+ return import_node_path.default.join(riddleProofDir, "pipelines", `riddle-proof-${action}.lobster`);
130
+ }
131
+ function asJsonString(value) {
132
+ if (value === void 0 || value === null || value === "") return "";
133
+ if (typeof value === "string") return value;
134
+ return JSON.stringify(value);
135
+ }
136
+ function buildSetupArgs(params, config) {
137
+ if (!params.repo && !canRunSetupWithoutRepo(params)) throw new Error("repo is required for setup/run");
138
+ if (!params.change_request) throw new Error("change_request is required for setup/run");
139
+ const commitMessage = (params.commit_message || params.change_request || "").trim();
140
+ const captureScript = (params.capture_script || "").trim();
141
+ const requestedReference = params.reference || (params.prod_url ? "both" : "before");
142
+ if (!commitMessage) throw new Error("commit_message is required for setup/run");
143
+ return {
144
+ repo: params.repo,
145
+ branch: params.branch || "",
146
+ change_request: params.change_request,
147
+ commit_message: commitMessage,
148
+ prod_url: params.prod_url || "",
149
+ capture_script: captureScript,
150
+ success_criteria: params.success_criteria || "",
151
+ assertions_json: params.assertions_json || asJsonString(params.assertions),
152
+ verification_mode: params.verification_mode || "proof",
153
+ resume_session: params.resume_session || "",
154
+ target_image_url: params.target_image_url || "",
155
+ target_image_hash: params.target_image_hash || "",
156
+ viewport_matrix_json: params.viewport_matrix_json || asJsonString(params.viewport_matrix),
157
+ deterministic_setup_json: params.deterministic_setup_json || asJsonString(params.deterministic_setup),
158
+ reference: requestedReference,
159
+ base_branch: params.base_branch || "main",
160
+ before_ref: params.before_ref || "",
161
+ allow_static_preview_fallback: params.allow_static_preview_fallback ? "true" : "",
162
+ context: params.context || "",
163
+ reviewer: params.reviewer || config.defaultReviewer,
164
+ mode: previewModeFromWorkflowMode(params.mode) || "",
165
+ implementation_mode: params.implementation_mode || "",
166
+ require_diff: params.require_diff,
167
+ allow_code_changes: params.allow_code_changes,
168
+ build_command: params.build_command || "npm run build",
169
+ build_output: params.build_output || "build",
170
+ server_image: params.server_image || "node:20-slim",
171
+ server_command: params.server_command || "npm start",
172
+ server_port: String(params.server_port || 3e3),
173
+ server_path: params.server_path || "",
174
+ server_path_source: params.server_path ? "user" : "",
175
+ use_auth: params.use_auth ? "true" : "",
176
+ auth_localStorage_json: params.auth_localStorage_json || "",
177
+ auth_cookies_json: params.auth_cookies_json || "",
178
+ auth_headers_json: params.auth_headers_json || "",
179
+ color_scheme: params.color_scheme || "",
180
+ wait_for_selector: params.wait_for_selector || "",
181
+ discord_channel: params.discord_channel || "",
182
+ discord_thread_id: params.discord_thread_id || "",
183
+ discord_message_id: params.discord_message_id || "",
184
+ discord_source_url: params.discord_source_url || "",
185
+ leave_draft: params.leave_draft ? "true" : ""
186
+ };
187
+ }
188
+ function readState(statePath) {
189
+ if (!(0, import_node_fs.existsSync)(statePath)) {
190
+ return null;
191
+ }
192
+ return JSON.parse((0, import_node_fs.readFileSync)(statePath, "utf-8"));
193
+ }
194
+ function writeState(statePath, state) {
195
+ (0, import_node_fs.writeFileSync)(statePath, JSON.stringify(state, null, 2));
196
+ }
197
+ function normalizeOptionalString(value) {
198
+ return typeof value === "string" ? value.trim() : void 0;
199
+ }
200
+ function knownEnvironmentIssuesFromNotes(notes) {
201
+ const text = notes.toLowerCase();
202
+ const issues = [];
203
+ if ((text.includes("erofs") || text.includes("read-only file system")) && text.includes("node_modules") && (text.includes(".vite-temp") || text.includes("vite.config"))) {
204
+ issues.push({
205
+ code: "vite_temp_config_erofs",
206
+ source: "implementation_notes",
207
+ severity: "environment",
208
+ summary: "Focused build verification hit the known Vite temp-config EROFS issue in shared node_modules."
209
+ });
210
+ }
211
+ return issues;
212
+ }
213
+ function guardProofEvidenceGlobalAssignments(script) {
214
+ return script.replace(
215
+ /^(\s*)(globalThis|window|self)\.__riddleProofEvidence\s*=\s*([^;\n]+);\s*$/gm,
216
+ (_match, indent, root, expression) => `${indent}try { if (typeof ${root} !== "undefined" && ${root}) ${root}.__riddleProofEvidence = ${expression.trim()}; } catch {}`
217
+ );
218
+ }
219
+ function normalizeCaptureScript(value) {
220
+ const script = normalizeOptionalString(value) || "";
221
+ return script ? guardProofEvidenceGlobalAssignments(script) : "";
222
+ }
223
+ function appendProofSummaryLine(state, line) {
224
+ const text = String(line || "").trim();
225
+ if (!text) return;
226
+ const existing = typeof state.proof_summary === "string" ? state.proof_summary.trim() : "";
227
+ if (existing.includes(text)) return;
228
+ state.proof_summary = existing ? `${existing}
229
+ ${text}` : text;
230
+ }
231
+ function hasAuthoredProofPlan(state = {}) {
232
+ return Boolean((state?.proof_plan || "").trim()) && Boolean((state?.capture_script || "").trim());
233
+ }
234
+ function syncAuthoringState(state = {}) {
235
+ const reconReady = ["ready_for_proof_plan", "completed"].includes(state?.recon_status || "");
236
+ const reconBlocked = ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "");
237
+ const reconExhausted = state?.recon_status === "exhausted";
238
+ if (reconBlocked) {
239
+ state.author_status = "needs_recon_judgment";
240
+ } else if (reconExhausted) {
241
+ state.author_status = "recon_exhausted";
242
+ } else if (!reconReady) {
243
+ state.author_status = state.author_status || "pending_recon";
244
+ } else if (state.author_status === "ready" || state.proof_plan_status === "ready") {
245
+ state.author_status = "ready";
246
+ } else {
247
+ state.author_status = "needs_authoring";
248
+ }
249
+ if (reconBlocked) {
250
+ state.proof_plan_status = "needs_recon_judgment";
251
+ } else if (reconExhausted) {
252
+ state.proof_plan_status = "recon_exhausted";
253
+ } else if (state.author_status === "ready" || state.proof_plan_status === "ready") {
254
+ state.proof_plan_status = "ready";
255
+ } else if (reconReady) {
256
+ state.proof_plan_status = "needs_authoring";
257
+ } else {
258
+ state.proof_plan_status = state.proof_plan_status || "pending_recon";
259
+ }
260
+ state.author_request = state.author_request || {};
261
+ state.author_summary = state.author_summary || "";
262
+ return state;
263
+ }
264
+ function ensureStageLoopState(state = {}) {
265
+ if (!state || typeof state !== "object") return state;
266
+ syncAuthoringState(state);
267
+ state.explicit_stage_gate = true;
268
+ state.stage_attempts = state.stage_attempts || {};
269
+ for (const stage of WORKFLOW_STAGE_ORDER) {
270
+ const current = state.stage_attempts[stage] || {};
271
+ state.stage_attempts[stage] = {
272
+ count: Number(current.count || 0),
273
+ last_status: current.last_status || null,
274
+ last_checkpoint: current.last_checkpoint || null,
275
+ last_summary: current.last_summary || null,
276
+ last_attempted_at: current.last_attempted_at || null,
277
+ history: Array.isArray(current.history) ? current.history : []
278
+ };
279
+ }
280
+ state.stage_decision_request = state.stage_decision_request || {};
281
+ state.active_checkpoint = state.active_checkpoint || null;
282
+ state.active_checkpoint_stage = state.active_checkpoint_stage || null;
283
+ state.last_requested_advance_stage = state.last_requested_advance_stage || null;
284
+ return state;
285
+ }
286
+ function clearStageDecisionRequest(state = {}) {
287
+ ensureStageLoopState(state);
288
+ state.stage_decision_request = {};
289
+ state.active_checkpoint = null;
290
+ state.active_checkpoint_stage = null;
291
+ return state;
292
+ }
293
+ function recordStageAttempt(state, stage, entry = {}) {
294
+ ensureStageLoopState(state);
295
+ const bucket = state.stage_attempts[stage];
296
+ const attemptNumber = Number(bucket.count || 0) + 1;
297
+ const item = {
298
+ attempt: attemptNumber,
299
+ stage,
300
+ status: entry.status || "completed",
301
+ checkpoint: entry.checkpoint || null,
302
+ summary: entry.summary || null,
303
+ requested_advance_stage: entry.requestedAdvanceStage || null,
304
+ halted_for_approval: Boolean(entry.haltedForApproval),
305
+ auto_approved: Boolean(entry.autoApproved),
306
+ retryable: Boolean(entry.retryable),
307
+ checkpoint_disposition: entry.checkpointDisposition || null,
308
+ error: entry.error || null,
309
+ details: entry.details || {},
310
+ attempted_at: (/* @__PURE__ */ new Date()).toISOString()
311
+ };
312
+ bucket.count = attemptNumber;
313
+ bucket.last_status = item.status;
314
+ bucket.last_checkpoint = item.checkpoint;
315
+ bucket.last_summary = item.summary;
316
+ bucket.last_attempted_at = item.attempted_at;
317
+ bucket.history = [...bucket.history, item].slice(-25);
318
+ state.stage_attempts[stage] = bucket;
319
+ state.last_requested_advance_stage = entry.requestedAdvanceStage || state.last_requested_advance_stage || null;
320
+ return item;
321
+ }
322
+ function setStageDecisionRequest(state, request) {
323
+ ensureStageLoopState(state);
324
+ const continueWithStage = request.continueWithStage || request.recommendedAdvanceStage || null;
325
+ state.active_checkpoint = request.checkpoint;
326
+ state.active_checkpoint_stage = request.stage;
327
+ state.stage_decision_request = {
328
+ stage: request.stage,
329
+ checkpoint: request.checkpoint,
330
+ summary: request.summary,
331
+ next_actions: request.nextActions || [],
332
+ advance_options: request.advanceOptions || [],
333
+ recommended_advance_stage: request.recommendedAdvanceStage || null,
334
+ continue_from_checkpoint: Boolean(continueWithStage),
335
+ continue_with_stage: continueWithStage,
336
+ blocking: Boolean(request.blocking),
337
+ details: request.details || {},
338
+ checkpoint_contract: request.checkpointContract || null,
339
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
340
+ };
341
+ return state.stage_decision_request;
342
+ }
343
+ function checkpointContinueStage(state) {
344
+ ensureStageLoopState(state);
345
+ const stage = state?.stage_decision_request?.continue_with_stage || state?.stage_decision_request?.recommended_advance_stage || null;
346
+ return WORKFLOW_STAGE_ORDER.includes(stage) ? stage : null;
347
+ }
348
+ function invalidateVerifyEvidence(state = {}) {
349
+ if (!state || typeof state !== "object") {
350
+ return { invalidated: false };
351
+ }
352
+ const hadAfterCdn = typeof state.after_cdn === "string" && state.after_cdn.trim().length > 0;
353
+ const hadVerifyResults = Boolean(state.verify_results && Object.keys(state.verify_results).length > 0);
354
+ const hadMergeRecommendation = typeof state.merge_recommendation === "string" && state.merge_recommendation.trim().length > 0;
355
+ const hadProofSummary = typeof state.proof_summary === "string" && state.proof_summary.trim().length > 0;
356
+ const hadVerifyStatus = typeof state.verify_status === "string" && state.verify_status.trim().length > 0;
357
+ const hadVerifySummary = typeof state.verify_summary === "string" && state.verify_summary.trim().length > 0;
358
+ const hadVerifyDecisionRequest = Boolean(state.verify_decision_request && Object.keys(state.verify_decision_request).length > 0);
359
+ const hadEvidenceNotes = Array.isArray(state.evidence_notes) && state.evidence_notes.length > 0;
360
+ const hadProofAssessment = Boolean(state.proof_assessment && Object.keys(state.proof_assessment).length > 0);
361
+ const hadProofAssessmentRequest = Boolean(state.proof_assessment_request && Object.keys(state.proof_assessment_request).length > 0);
362
+ const invalidated = hadAfterCdn || hadVerifyResults || hadMergeRecommendation || hadProofSummary || hadVerifyStatus || hadVerifySummary || hadVerifyDecisionRequest || hadEvidenceNotes || hadProofAssessment || hadProofAssessmentRequest;
363
+ if (invalidated) {
364
+ state.after_cdn = "";
365
+ state.verify_results = {};
366
+ state.verify_status = "";
367
+ state.verify_summary = "";
368
+ state.verify_decision_request = {};
369
+ state.merge_recommendation = null;
370
+ state.proof_summary = "";
371
+ state.evidence_notes = [];
372
+ state.proof_assessment = {};
373
+ state.proof_assessment_source = null;
374
+ state.proof_assessment_request = {};
375
+ }
376
+ return {
377
+ invalidated,
378
+ hadAfterCdn,
379
+ hadVerifyResults,
380
+ hadMergeRecommendation,
381
+ hadProofSummary,
382
+ hadVerifyStatus,
383
+ hadVerifySummary,
384
+ hadVerifyDecisionRequest,
385
+ hadEvidenceNotes,
386
+ hadProofAssessment,
387
+ hadProofAssessmentRequest
388
+ };
389
+ }
390
+ function normalizedReference(state = {}) {
391
+ const reference = String(state?.requested_reference || state?.reference || "before").trim();
392
+ return reference || "before";
393
+ }
394
+ function normalizedProofAssessment(state = {}) {
395
+ const proofAssessment = state?.proof_assessment || {};
396
+ const source = String(proofAssessment?.source || state?.proof_assessment_source || "").trim().toLowerCase();
397
+ const decision = String(proofAssessment?.decision || "").trim();
398
+ return {
399
+ decision: decision || null,
400
+ source: source || null
401
+ };
402
+ }
403
+ var VISUAL_FIRST_MODES = /* @__PURE__ */ new Set([
404
+ "visual",
405
+ "render",
406
+ "interaction",
407
+ "ui",
408
+ "layout",
409
+ "screenshot",
410
+ "canvas",
411
+ "animation"
412
+ ]);
413
+ function objectValue(value) {
414
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
415
+ }
416
+ function normalizedVerificationMode(state = {}) {
417
+ const bundle = objectValue(state?.evidence_bundle);
418
+ const bundleMode = String(bundle.verification_mode || "").trim().toLowerCase();
419
+ if (bundleMode) return bundleMode;
420
+ return String(state?.verification_mode || "proof").trim().toLowerCase() || "proof";
421
+ }
422
+ function visualDeltaRequiredForState(state = {}) {
423
+ if (noImplementationModeFor(state) || visualDeltaNotApplicableForNoImplementation(state)) return false;
424
+ const bundle = objectValue(state?.evidence_bundle);
425
+ const contract = objectValue(bundle.artifact_contract);
426
+ const required = objectValue(contract.required);
427
+ if (required.visual_delta === false) return false;
428
+ return required.visual_delta === true || VISUAL_FIRST_MODES.has(normalizedVerificationMode(state));
429
+ }
430
+ function visualDeltaForState(state = {}) {
431
+ const bundle = objectValue(state?.evidence_bundle);
432
+ const after = objectValue(bundle.after);
433
+ const afterDelta = objectValue(after.visual_delta);
434
+ if (Object.keys(afterDelta).length) return afterDelta;
435
+ const request = objectValue(state?.proof_assessment_request);
436
+ return objectValue(request.visual_delta);
437
+ }
438
+ function visualDeltaNotApplicableForNoImplementation(state = {}) {
439
+ const visualDelta = visualDeltaForState(state);
440
+ if (String(visualDelta.status || "").trim() !== "not_applicable") return false;
441
+ const reason = String(visualDelta.reason || "").toLowerCase();
442
+ return reason.includes("audit/no-diff") || reason.includes("does not require a before/after implementation delta");
443
+ }
444
+ function visualDeltaShipGateReason(state = {}) {
445
+ if (!visualDeltaRequiredForState(state)) return null;
446
+ const visualDelta = visualDeltaForState(state);
447
+ if (visualDelta.status === "measured" && visualDelta.passed === true) return null;
448
+ const status = String(visualDelta.status || "missing");
449
+ if (status === "unmeasured") {
450
+ const reason2 = String(visualDelta.reason || "").trim();
451
+ return reason2 ? `visual_delta.status=unmeasured blocks ready_to_ship for visual/UI proof: ${reason2}` : "visual_delta.status=unmeasured blocks ready_to_ship for visual/UI proof";
452
+ }
453
+ if (status === "measured" && visualDelta.passed === false) {
454
+ return "visual_delta.status=measured but visual_delta.passed=false blocks ready_to_ship for visual/UI proof";
455
+ }
456
+ const reason = String(visualDelta.reason || "").trim();
457
+ if (reason) return `visual_delta.status=${status} blocks ready_to_ship for visual/UI proof: ${reason}`;
458
+ return `visual_delta.status=${status} blocks ready_to_ship for visual/UI proof`;
459
+ }
460
+ function visualDeltaEvidenceIssueCode(state = {}, blocker = "") {
461
+ const visualDelta = visualDeltaForState(state || {});
462
+ const status = String(visualDelta.status || "").trim();
463
+ const reason = `${String(visualDelta.reason || "")}
464
+ ${blocker}`.toLowerCase();
465
+ if (status === "unmeasured") {
466
+ if (reason.includes("fetch") || reason.includes("allowlist") || reason.includes("registered domain") || reason.includes("high risk") || reason.includes("comparator")) {
467
+ return "comparator_fetch_blocked";
468
+ }
469
+ return "visual_delta_unmeasured";
470
+ }
471
+ if (status === "measured" && visualDelta.passed === false) return "semantic_proof_failed";
472
+ if (visualDeltaRequiredForState(state || {})) return "visual_delta_unmeasured";
473
+ return "semantic_proof_failed";
474
+ }
475
+ function requiredBaselineLabelsForState(state = {}) {
476
+ const reference = normalizedReference(state);
477
+ const labels = [];
478
+ if (reference === "before" || reference === "both") labels.push("before");
479
+ if (reference === "prod" || reference === "both") labels.push("prod");
480
+ return labels;
481
+ }
482
+ function validateShipGate(state = {}) {
483
+ const reference = normalizedReference(state);
484
+ const prodUrl = String(state?.prod_url || "").trim();
485
+ const beforeCdn = String(state?.before_cdn || "").trim();
486
+ const prodCdn = String(state?.prod_cdn || "").trim();
487
+ const afterCdn = String(state?.after_cdn || "").trim();
488
+ const verifyStatus = String(state?.verify_status || "").trim();
489
+ const proofAssessment = normalizedProofAssessment(state);
490
+ const verificationMode = normalizedVerificationMode(state);
491
+ const visualDelta = visualDeltaForState(state);
492
+ const visualDeltaRequired = visualDeltaRequiredForState(state);
493
+ const visualDeltaBlocker = visualDeltaShipGateReason(state);
494
+ const reasons = [];
495
+ if (!["before", "prod", "both"].includes(reference)) {
496
+ reasons.push(`reference must be before, prod, or both; got ${reference}`);
497
+ }
498
+ const requiredBaselines = requiredBaselineLabelsForState(state);
499
+ if (requiredBaselines.includes("before") && !beforeCdn) {
500
+ reasons.push("before_cdn is required before ship");
501
+ }
502
+ if (requiredBaselines.includes("prod")) {
503
+ if (!prodUrl) {
504
+ reasons.push(`prod_url is required when reference=${reference}`);
505
+ }
506
+ if (!prodCdn) {
507
+ reasons.push("prod_cdn is required before ship");
508
+ }
509
+ }
510
+ if (!afterCdn) {
511
+ reasons.push("after_cdn is required before ship");
512
+ }
513
+ if (verifyStatus !== "evidence_captured") {
514
+ reasons.push("verify_status must be evidence_captured before ship");
515
+ }
516
+ if (!["supervising_agent", "supervisor"].includes(proofAssessment.source || "")) {
517
+ reasons.push("proof_assessment.source must be supervising_agent before ship");
518
+ }
519
+ if (proofAssessment.decision !== "ready_to_ship") {
520
+ reasons.push("proof_assessment.decision must be ready_to_ship before ship");
521
+ }
522
+ if (visualDeltaBlocker) {
523
+ reasons.push(visualDeltaBlocker);
524
+ }
525
+ return {
526
+ ok: reasons.length === 0,
527
+ reasons,
528
+ required_baselines: requiredBaselines,
529
+ evidence: {
530
+ reference,
531
+ verification_mode: verificationMode || null,
532
+ prod_url: prodUrl || null,
533
+ before_cdn: beforeCdn || null,
534
+ prod_cdn: prodCdn || null,
535
+ after_cdn: afterCdn || null,
536
+ verify_status: verifyStatus || null,
537
+ proof_assessment_decision: proofAssessment.decision,
538
+ proof_assessment_source: proofAssessment.source,
539
+ visual_delta_required: visualDeltaRequired,
540
+ visual_delta_status: typeof visualDelta.status === "string" ? visualDelta.status : null,
541
+ visual_delta_passed: typeof visualDelta.passed === "boolean" ? visualDelta.passed : null
542
+ }
543
+ };
544
+ }
545
+ var CHECKPOINT_CONTRACT_SPECS = {
546
+ setup_review: {
547
+ purpose: "Inspect prepared workspace state before moving into recon."
548
+ },
549
+ recon_supervisor_judgment: {
550
+ purpose: "Supervising agent judges the latest recon attempt and either retries recon, promotes baselines for authoring, or escalates.",
551
+ accepted_inputs: [{
552
+ name: "recon_assessment_json",
553
+ type: "json",
554
+ required: true,
555
+ description: "JSON assessment with decision, summary, baseline_understanding, continue_with_stage, escalation_target, refined_inputs, and reasons."
556
+ }],
557
+ response_schema: {
558
+ decision: ["retry_recon", "ready_for_author", "recon_stuck"],
559
+ summary: "string",
560
+ baseline_understanding: {
561
+ reference: ["before", "prod", "both", "unknown"],
562
+ target_route: "string",
563
+ before_evidence_url: "string",
564
+ visible_before_state: "string",
565
+ relevant_elements: ["string"],
566
+ requested_change: "string",
567
+ proof_focus: "string",
568
+ stop_condition: "string",
569
+ quality_risks: ["string"]
570
+ },
571
+ continue_with_stage: ["recon", "author"],
572
+ escalation_target: ["agent", "human"],
573
+ refined_inputs: {
574
+ server_path: "string",
575
+ wait_for_selector: "string",
576
+ reference: ["before", "prod", "both"]
577
+ },
578
+ reasons: ["string"],
579
+ source: "supervising_agent"
580
+ },
581
+ required_state: ["recon_assessment_request", "recon_results.attempt_history"]
582
+ },
583
+ recon_review: {
584
+ purpose: "Recon baselines were promoted and the workflow is ready for proof authoring."
585
+ },
586
+ recon_human_escalation: {
587
+ purpose: "Recon is explicitly blocked for human direction after supervising-agent escalation."
588
+ },
589
+ author_supervisor_judgment: {
590
+ purpose: "Supervising agent authors the proof packet from recon observations.",
591
+ accepted_inputs: [{
592
+ name: "author_packet_json",
593
+ type: "json",
594
+ required: true,
595
+ description: "JSON proof packet with proof_plan, capture_script, optional refined_inputs, rationale, confidence, and summary."
596
+ }],
597
+ response_schema: {
598
+ proof_plan: "string",
599
+ capture_script: "string",
600
+ baseline_understanding_used: {
601
+ reference: ["before", "prod", "both", "unknown"],
602
+ target_route: "string",
603
+ before_evidence_url: "string",
604
+ visible_before_state: "string",
605
+ relevant_elements: ["string"],
606
+ requested_change: "string",
607
+ proof_focus: "string",
608
+ stop_condition: "string",
609
+ quality_risks: ["string"]
610
+ },
611
+ refined_inputs: {
612
+ server_path: "string",
613
+ wait_for_selector: "string",
614
+ reference: ["before", "prod", "both"]
615
+ },
616
+ rationale: ["string"],
617
+ confidence: "low | medium | high",
618
+ summary: "string"
619
+ },
620
+ required_state: ["author_request", "recon_results.baselines"]
621
+ },
622
+ author_review: {
623
+ purpose: "Proof packet is ready; choose whether to inspect, re-author, implement, or verify."
624
+ },
625
+ implement_changes_missing: {
626
+ purpose: "Implementation stage did not detect material code changes.",
627
+ accepted_inputs: [{
628
+ name: "make_code_changes",
629
+ type: "external_action",
630
+ required: true,
631
+ description: "Make the actual code changes in the after worktree, then resume the implement stage."
632
+ }]
633
+ },
634
+ implement_review: {
635
+ purpose: "Implementation changes were detected and stale verify evidence, if any, was invalidated."
636
+ },
637
+ implement_required: {
638
+ purpose: "Verify cannot run until implementation changes are recorded.",
639
+ accepted_inputs: [{
640
+ name: "make_code_changes",
641
+ type: "external_action",
642
+ required: true,
643
+ description: "Make the requested code change, then advance the run to implement."
644
+ }]
645
+ },
646
+ verify_capture_retry: {
647
+ purpose: "Verify capture was incomplete; the agent loop should revise capture authoring before shipping.",
648
+ accepted_inputs: [{
649
+ name: "author_packet_json",
650
+ type: "json",
651
+ description: "Optional revised proof packet if returning to author."
652
+ }],
653
+ required_state: ["verify_decision_request"]
654
+ },
655
+ verify_supervisor_judgment: {
656
+ purpose: "Supervising agent judges whether captured evidence proves the change is ready to ship.",
657
+ accepted_inputs: [{
658
+ name: "proof_assessment_json",
659
+ type: "json",
660
+ required: true,
661
+ description: "JSON assessment with decision, summary, recommended_stage, continue_with_stage, escalation_target, and reasons."
662
+ }],
663
+ response_schema: {
664
+ decision: ["ready_to_ship", "needs_richer_proof", "revise_capture"],
665
+ summary: "string",
666
+ recommended_stage: ["ship", "author", "implement", "recon", "verify"],
667
+ continue_with_stage: ["ship", "author", "implement", "recon", "verify"],
668
+ escalation_target: ["agent", "human"],
669
+ reasons: ["string"],
670
+ source: "supervising_agent"
671
+ },
672
+ required_state: ["before_cdn or prod_cdn", "after_cdn", "proof_assessment_request"],
673
+ include_ship_gate: true
674
+ },
675
+ verify_human_escalation: {
676
+ purpose: "Proof loop is explicitly escalated to the human after supervising-agent judgment."
677
+ },
678
+ verify_agent_retry: {
679
+ purpose: "Supervising agent judged the proof insufficient and kept the workflow inside the internal loop.",
680
+ accepted_inputs: [{
681
+ name: "author_packet_json",
682
+ type: "json",
683
+ description: "Optional revised proof packet if the checkpoint resumes to author."
684
+ }]
685
+ },
686
+ verify_ship_ready: {
687
+ purpose: "Proof assessment is ready_to_ship and the next continuation should enter ship.",
688
+ include_ship_gate: true
689
+ },
690
+ verify_required: {
691
+ purpose: "Ship is blocked until verify captures usable after evidence.",
692
+ include_ship_gate: true
693
+ },
694
+ verify_supervisor_judgment_required: {
695
+ purpose: "Ship is blocked until the supervising agent judges the current evidence ready_to_ship.",
696
+ accepted_inputs: [{
697
+ name: "proof_assessment_json",
698
+ type: "json",
699
+ required: true,
700
+ description: "JSON assessment that must use decision=ready_to_ship before ship can continue."
701
+ }],
702
+ include_ship_gate: true
703
+ },
704
+ ship_gate_blocked: {
705
+ purpose: "Ship is blocked because required baseline, after evidence, or supervising-agent proof approval is missing.",
706
+ include_ship_gate: true
707
+ },
708
+ ship_review: {
709
+ purpose: "Ship completed; inspect the PR, proof comment, and CI status.",
710
+ include_ship_gate: true
711
+ }
712
+ };
713
+ function buildCheckpointContract(state, request) {
714
+ const spec = CHECKPOINT_CONTRACT_SPECS[request.checkpoint] || {
715
+ purpose: request.summary
716
+ };
717
+ const continueWithStage = request.continueWithStage || request.recommendedAdvanceStage || null;
718
+ const payload = {
719
+ version: CHECKPOINT_CONTRACT_VERSION,
720
+ checkpoint: request.checkpoint,
721
+ stage: request.stage,
722
+ purpose: spec.purpose,
723
+ summary: request.summary,
724
+ blocking: Boolean(request.blocking),
725
+ next_actions: request.nextActions || [],
726
+ advance_options: request.advanceOptions || [],
727
+ accepted_inputs: spec.accepted_inputs || [],
728
+ response_schema: spec.response_schema || null,
729
+ required_state: spec.required_state || [],
730
+ resume: {
731
+ action: "run",
732
+ state_path: request.statePath || null,
733
+ continue_from_checkpoint: Boolean(continueWithStage),
734
+ continue_with_stage: continueWithStage
735
+ }
736
+ };
737
+ if (spec.include_ship_gate) {
738
+ payload.ship_gate = validateShipGate(state);
739
+ }
740
+ return payload;
741
+ }
742
+ function mergeStateFromParams(statePath, params) {
743
+ const state = readState(statePath);
744
+ if (!state) return null;
745
+ ensureStageLoopState(state);
746
+ const stringFields = [
747
+ "change_request",
748
+ "commit_message",
749
+ "prod_url",
750
+ "capture_script",
751
+ "success_criteria",
752
+ "assertions_json",
753
+ "verification_mode",
754
+ "resume_session",
755
+ "target_image_url",
756
+ "target_image_hash",
757
+ "viewport_matrix_json",
758
+ "deterministic_setup_json",
759
+ "base_branch",
760
+ "before_ref",
761
+ "context",
762
+ "reviewer",
763
+ "build_command",
764
+ "build_output",
765
+ "server_image",
766
+ "server_command",
767
+ "server_path",
768
+ "wait_for_selector",
769
+ "discord_channel",
770
+ "discord_thread_id",
771
+ "discord_message_id",
772
+ "discord_source_url",
773
+ "auth_localStorage_json",
774
+ "auth_cookies_json",
775
+ "auth_headers_json",
776
+ "proof_plan",
777
+ "implementation_notes",
778
+ "implementation_mode"
779
+ ];
780
+ for (const field of stringFields) {
781
+ if (params[field] !== void 0) {
782
+ state[field] = normalizeOptionalString(params[field]);
783
+ }
784
+ }
785
+ if (params.server_path !== void 0) {
786
+ state.server_path_source = "tool_param";
787
+ }
788
+ if (params.implementation_notes !== void 0) {
789
+ const issues = knownEnvironmentIssuesFromNotes(state.implementation_notes || "");
790
+ if (issues.length) state.implementation_environment_issues = issues;
791
+ }
792
+ if (params.reference !== void 0) state.reference = params.reference;
793
+ if (params.mode !== void 0) {
794
+ const previewMode = previewModeFromWorkflowMode(params.mode);
795
+ if (previewMode) {
796
+ state.mode = previewMode;
797
+ } else {
798
+ state.workflow_mode = normalizeOptionalString(params.mode);
799
+ }
800
+ }
801
+ if (params.implementation_mode !== void 0) state.implementation_mode = normalizeOptionalString(params.implementation_mode);
802
+ if (params.require_diff !== void 0) state.require_diff = params.require_diff;
803
+ if (params.allow_code_changes !== void 0) state.allow_code_changes = params.allow_code_changes;
804
+ if (params.allow_static_preview_fallback !== void 0) {
805
+ state.allow_static_preview_fallback = params.allow_static_preview_fallback;
806
+ }
807
+ if (params.server_port !== void 0) state.server_port = String(params.server_port);
808
+ if (params.color_scheme !== void 0) state.color_scheme = params.color_scheme || "";
809
+ if (params.use_auth !== void 0) state.use_auth = params.use_auth ? "true" : "";
810
+ if (params.leave_draft !== void 0) state.leave_draft = params.leave_draft ? "true" : "";
811
+ if (params.advance_stage !== void 0) state.last_requested_advance_stage = params.advance_stage;
812
+ if (params.recon_assessment_json !== void 0) {
813
+ const raw = normalizeOptionalString(params.recon_assessment_json) || "";
814
+ if (!raw) {
815
+ state.recon_assessment = {};
816
+ state.recon_assessment_source = null;
817
+ } else {
818
+ const parsed = JSON.parse(raw);
819
+ state.recon_assessment = {
820
+ ...parsed,
821
+ source: (parsed?.source || "supervising_agent").toString()
822
+ };
823
+ state.recon_assessment_source = state.recon_assessment.source;
824
+ if (parsed?.baseline_understanding && typeof parsed.baseline_understanding === "object") {
825
+ state.recon_baseline_understanding = parsed.baseline_understanding;
826
+ }
827
+ const refined = parsed?.refined_inputs || {};
828
+ if (typeof refined?.server_path === "string") {
829
+ state.server_path = normalizeOptionalString(refined.server_path) || "";
830
+ state.server_path_source = "supervising_agent";
831
+ }
832
+ if (typeof refined?.wait_for_selector === "string") state.wait_for_selector = normalizeOptionalString(refined.wait_for_selector) || "";
833
+ if (typeof refined?.reference === "string" && refined.reference.trim()) state.reference = refined.reference.trim();
834
+ }
835
+ }
836
+ if (params.author_packet_json !== void 0) {
837
+ const raw = normalizeOptionalString(params.author_packet_json) || "";
838
+ if (!raw) {
839
+ state.supervisor_author_packet = null;
840
+ } else {
841
+ const parsed = JSON.parse(raw);
842
+ if (typeof parsed?.capture_script === "string") {
843
+ parsed.capture_script = normalizeCaptureScript(parsed.capture_script);
844
+ }
845
+ state.supervisor_author_packet = parsed;
846
+ if (typeof parsed?.proof_plan === "string") state.proof_plan = normalizeOptionalString(parsed.proof_plan) || "";
847
+ if (typeof parsed?.capture_script === "string") state.capture_script = normalizeCaptureScript(parsed.capture_script);
848
+ if (parsed?.baseline_understanding_used && typeof parsed.baseline_understanding_used === "object") {
849
+ state.author_baseline_understanding_used = parsed.baseline_understanding_used;
850
+ }
851
+ const refined = parsed?.refined_inputs || {};
852
+ if (typeof refined?.server_path === "string") {
853
+ state.server_path = normalizeOptionalString(refined.server_path) || "";
854
+ state.server_path_source = "supervising_agent";
855
+ }
856
+ if (typeof refined?.wait_for_selector === "string") state.wait_for_selector = normalizeOptionalString(refined.wait_for_selector) || "";
857
+ if (typeof refined?.reference === "string" && refined.reference.trim()) state.reference = refined.reference.trim();
858
+ if (typeof parsed?.confidence === "string") state.supervisor_author_confidence = normalizeOptionalString(parsed.confidence) || null;
859
+ if (parsed?.rationale !== void 0) state.supervisor_author_rationale = parsed.rationale;
860
+ if (typeof parsed?.summary === "string") state.supervisor_author_summary = normalizeOptionalString(parsed.summary) || null;
861
+ invalidateVerifyEvidence(state);
862
+ }
863
+ }
864
+ if (params.proof_assessment_json !== void 0) {
865
+ const raw = normalizeOptionalString(params.proof_assessment_json) || "";
866
+ if (!raw) {
867
+ state.proof_assessment = {};
868
+ state.proof_assessment_source = null;
869
+ } else {
870
+ const parsed = JSON.parse(raw);
871
+ const assessment = {
872
+ ...parsed,
873
+ source: (parsed?.source || "supervising_agent").toString()
874
+ };
875
+ const readyBlocker = assessment?.decision === "ready_to_ship" ? visualDeltaShipGateReason({ ...state, proof_assessment: assessment, proof_assessment_source: assessment.source }) : null;
876
+ if (readyBlocker) {
877
+ assessment.blocked_decision = assessment.decision;
878
+ assessment.decision = "revise_capture";
879
+ assessment.recommended_stage = "verify";
880
+ assessment.continue_with_stage = "verify";
881
+ assessment.evidence_collection_incomplete = true;
882
+ assessment.recovery_stage = "verify";
883
+ assessment.recovery_reason = readyBlocker;
884
+ assessment.evidence_issue_code = visualDeltaEvidenceIssueCode(state, readyBlocker);
885
+ assessment.visual_delta = visualDeltaForState(state);
886
+ assessment.suggested_repair = "Keep the same Riddle Proof run in evidence/comparison recovery: repair or retry the visual comparator/fetch path, wait for artifact readiness if applicable, or produce a measured visual_delta artifact before proof review can mark ready_to_ship.";
887
+ const blockers = Array.isArray(assessment.blockers) ? assessment.blockers : [];
888
+ assessment.blockers = [...blockers, readyBlocker];
889
+ }
890
+ state.proof_assessment = assessment;
891
+ state.proof_assessment_source = state.proof_assessment.source;
892
+ if (typeof state.proof_assessment?.decision === "string") {
893
+ state.proof_decision = state.proof_assessment.decision;
894
+ }
895
+ if (typeof parsed?.summary === "string") {
896
+ state.proof_assessment_summary = normalizeOptionalString(parsed.summary) || null;
897
+ }
898
+ if (state.proof_assessment?.decision === "ready_to_ship") {
899
+ state.merge_recommendation = "ready-to-ship";
900
+ } else if (typeof state.proof_assessment?.decision === "string" && state.proof_assessment.decision.trim()) {
901
+ state.merge_recommendation = "do-not-merge";
902
+ }
903
+ appendProofSummaryLine(state, `Supervising proof assessment: ${state.proof_assessment.decision || "unknown"}`);
904
+ if (readyBlocker) {
905
+ appendProofSummaryLine(state, `Ready-to-ship assessment routed to evidence recovery: ${readyBlocker}`);
906
+ }
907
+ if (state.proof_assessment_summary) {
908
+ appendProofSummaryLine(state, `Assessment summary: ${state.proof_assessment_summary}`);
909
+ }
910
+ const reasons = Array.isArray(parsed?.reasons) ? parsed.reasons.filter((item) => typeof item === "string" && item.trim()).slice(0, 4) : [];
911
+ if (reasons.length) {
912
+ appendProofSummaryLine(state, `Assessment reasons: ${reasons.join("; ")}`);
913
+ }
914
+ }
915
+ }
916
+ if (params.assertions_json !== void 0) {
917
+ const raw = normalizeOptionalString(params.assertions_json) || "";
918
+ if (!raw) {
919
+ state.parsed_assertions = null;
920
+ } else {
921
+ state.parsed_assertions = JSON.parse(raw);
922
+ }
923
+ }
924
+ if ((params.proof_plan !== void 0 || params.capture_script !== void 0) && hasAuthoredProofPlan(state)) {
925
+ state.author_summary = state.author_summary || "Proof authoring inputs were updated from tool params.";
926
+ }
927
+ syncAuthoringState(state);
928
+ writeState(statePath, state);
929
+ return state;
930
+ }
931
+ function summarizeState(state) {
932
+ if (!state) {
933
+ return {
934
+ stage: "missing",
935
+ summary: "No riddle-proof state file exists yet.",
936
+ state: null
937
+ };
938
+ }
939
+ ensureStageLoopState(state);
940
+ const attemptCounts = Object.fromEntries(
941
+ WORKFLOW_STAGE_ORDER.map((stage) => [stage, Number(state.stage_attempts?.[stage]?.count || 0)])
942
+ );
943
+ const selected = {
944
+ workspace_ready: Boolean(state.workspace_ready),
945
+ repo: state.repo || null,
946
+ branch: state.branch || null,
947
+ mode: state.mode || null,
948
+ workflow_mode: state.workflow_mode || null,
949
+ implementation_mode: state.implementation_mode || null,
950
+ require_diff: state.require_diff ?? null,
951
+ allow_code_changes: state.allow_code_changes ?? null,
952
+ no_implementation_mode: noImplementationModeFor(state),
953
+ reference: state.reference || null,
954
+ before_ref: state.before_ref || null,
955
+ allow_static_preview_fallback: Boolean(state.allow_static_preview_fallback),
956
+ commit_message: state.commit_message || null,
957
+ author_status: state.author_status || null,
958
+ author_summary: state.author_summary || null,
959
+ author_request: state.author_request || null,
960
+ proof_plan_status: state.proof_plan_status || null,
961
+ proof_plan: state.proof_plan || null,
962
+ proof_plan_request: state.proof_plan_request || null,
963
+ proof_profile_applied: Boolean(state.proof_profile),
964
+ proof_profile: state.proof_profile || null,
965
+ recon_status: state.recon_status || null,
966
+ recon_assessment: state.recon_assessment || null,
967
+ recon_assessment_request: state.recon_assessment_request || null,
968
+ recon_assessment_source: state.recon_assessment_source || null,
969
+ recon_decision_request: state.recon_decision_request || null,
970
+ recon_attempts_used: Array.isArray(state.recon_results?.attempt_history) ? state.recon_results.attempt_history.length : 0,
971
+ recon_attempts_max: state.recon_results?.max_attempts || null,
972
+ recon_hypothesis: state.recon_hypothesis || null,
973
+ implementation_status: state.implementation_status || null,
974
+ implementation_summary: state.implementation_summary || null,
975
+ implementation_detection_summary: state.implementation_detection_summary || null,
976
+ implementation_detection: state.implementation_detection || null,
977
+ implementation_environment_issues: state.implementation_environment_issues || [],
978
+ verify_status: state.verify_status || null,
979
+ verify_summary: state.verify_summary || null,
980
+ verify_decision_request: state.verify_decision_request || null,
981
+ proof_assessment: state.proof_assessment || null,
982
+ proof_assessment_request: state.proof_assessment_request || null,
983
+ proof_assessment_source: state.proof_assessment_source || null,
984
+ merge_recommendation: state.merge_recommendation || null,
985
+ before_cdn: state.before_cdn || null,
986
+ after_cdn: state.after_cdn || null,
987
+ prod_cdn: state.prod_cdn || null,
988
+ pr_url: state.pr_url || null,
989
+ pr_branch: state.target_branch || state.branch || null,
990
+ pr_state: state.pr_state || null,
991
+ ship_commit: state.ship_commit || null,
992
+ ship_remote_head: state.ship_remote_head || null,
993
+ merge_commit: state.merge_commit || null,
994
+ merged_at: state.merged_at || null,
995
+ ship_push: state.ship_push || null,
996
+ ship_report: state.ship_report || null,
997
+ cleanup_report: state.cleanup_report || null,
998
+ proof_comment_url: state.proof_comment_url || null,
999
+ proof_assessment_comment_url: state.proof_assessment_comment_url || null,
1000
+ marked_ready: typeof state.marked_ready === "boolean" ? state.marked_ready : null,
1001
+ left_draft: typeof state.left_draft === "boolean" ? state.left_draft : null,
1002
+ ci_status: state.ci_status || null,
1003
+ reviewer: state.reviewer || null,
1004
+ active_checkpoint: state.active_checkpoint || null,
1005
+ active_checkpoint_stage: state.active_checkpoint_stage || null,
1006
+ continue_with_stage: checkpointContinueStage(state),
1007
+ stage_decision_request: state.stage_decision_request || {},
1008
+ stage_attempt_counts: attemptCounts,
1009
+ explicit_stage_gate: Boolean(state.explicit_stage_gate),
1010
+ last_requested_advance_stage: state.last_requested_advance_stage || null,
1011
+ recon_results: state.recon_results || null,
1012
+ verify_results: state.verify_results || null,
1013
+ proof_session: state.proof_session || null,
1014
+ parent_proof_session: state.parent_proof_session || null,
1015
+ proof_session_artifact_url: state.proof_session_artifact_url || null
1016
+ };
1017
+ const parts = [
1018
+ state.workspace_ready ? "workspace ready" : "workspace not ready",
1019
+ state.mode ? `mode=${state.mode}` : null,
1020
+ state.reference ? `reference=${state.reference}` : null,
1021
+ state.author_status ? `author=${state.author_status}` : null,
1022
+ state.proof_plan_status ? `proof=${state.proof_plan_status}` : null,
1023
+ state.recon_status ? `recon=${state.recon_status}` : null,
1024
+ state.implementation_status ? `implement=${state.implementation_status}` : null,
1025
+ state.verify_status ? `verify=${state.verify_status}` : null,
1026
+ state.active_checkpoint ? `checkpoint=${state.active_checkpoint}` : null,
1027
+ state.after_cdn ? "after evidence captured" : null,
1028
+ state.pr_url ? "PR linked" : null
1029
+ ].filter(Boolean);
1030
+ return {
1031
+ stage: state.stage || (state.after_cdn ? "verified" : state.workspace_ready ? "setup" : "unknown"),
1032
+ summary: parts.length ? parts.join(", ") : "State file present.",
1033
+ state: selected
1034
+ };
1035
+ }
1036
+
1037
+ // src/proof-run-engine.ts
1038
+ function snapshotFor(statePath) {
1039
+ return summarizeState(readState(statePath));
1040
+ }
1041
+ function authorReady(state) {
1042
+ return state?.author_status === "ready" || state?.proof_plan_status === "ready";
1043
+ }
1044
+ function implementationReady(state) {
1045
+ return ["changes_detected", "completed"].includes(state?.implementation_status || "");
1046
+ }
1047
+ function implementationRequired(params, state) {
1048
+ return !noImplementationModeFor(params, state);
1049
+ }
1050
+ function stageAfterAuthor(state, params) {
1051
+ return implementationReady(state) || !implementationRequired(params, state) ? "verify" : "implement";
1052
+ }
1053
+ function latestReconAttempt(state) {
1054
+ const history = Array.isArray(state?.recon_results?.attempt_history) ? state.recon_results.attempt_history : [];
1055
+ return history.length ? history[history.length - 1] : null;
1056
+ }
1057
+ function latestReconCapturedBaselines(state) {
1058
+ const latest = latestReconAttempt(state);
1059
+ if (latest?.captured_baselines && typeof latest.captured_baselines === "object") return latest.captured_baselines;
1060
+ if (latest?.baselines && typeof latest.baselines === "object") return latest.baselines;
1061
+ return state?.recon_results?.baselines || {};
1062
+ }
1063
+ function requiredReconBaselineLabels(state) {
1064
+ const labels = [];
1065
+ const reference = state?.requested_reference || state?.reference || "before";
1066
+ if (reference === "before" || reference === "both") labels.push("before");
1067
+ if ((reference === "prod" || reference === "both") && String(state?.prod_url || "").trim()) labels.push("prod");
1068
+ return labels;
1069
+ }
1070
+ function latestReconHasRequiredBaselines(state) {
1071
+ const baselines = latestReconCapturedBaselines(state);
1072
+ return requiredReconBaselineLabels(state).every((label) => Boolean((baselines?.[label]?.url || "").trim()));
1073
+ }
1074
+ function hasReconBaselineUnderstanding(state) {
1075
+ const understanding = state?.recon_assessment?.baseline_understanding || state?.recon_baseline_understanding || {};
1076
+ return Boolean(
1077
+ String(understanding?.visible_before_state || "").trim() && String(understanding?.requested_change || "").trim() && String(understanding?.proof_focus || "").trim() && String(understanding?.stop_condition || "").trim()
1078
+ );
1079
+ }
1080
+ function promoteLatestReconBaselines(state) {
1081
+ const baselines = latestReconCapturedBaselines(state);
1082
+ state.recon_results = state.recon_results || {};
1083
+ state.recon_results.baselines = baselines;
1084
+ state.recon_results.selected_attempt = latestReconAttempt(state) || {};
1085
+ state.before_cdn = (baselines?.before?.url || "").trim();
1086
+ state.prod_cdn = (baselines?.prod?.url || "").trim();
1087
+ return baselines;
1088
+ }
1089
+ function hasSupervisorReconAssessment(state) {
1090
+ const reconAssessment2 = state?.recon_assessment || {};
1091
+ const source = String(reconAssessment2?.source || state?.recon_assessment_source || "").trim().toLowerCase();
1092
+ if (!reconAssessment2?.decision) return false;
1093
+ return source === "supervising_agent" || source === "supervisor";
1094
+ }
1095
+ function reconAssessment(state) {
1096
+ const assessment = state?.recon_assessment || {};
1097
+ const decision = assessment?.decision || null;
1098
+ const continueWithStage = assessment?.continue_with_stage || assessment?.recommended_stage || (decision === "ready_for_author" ? "author" : "recon");
1099
+ return {
1100
+ decision,
1101
+ summary: assessment?.summary || state?.recon_assessment_request?.summary || state?.recon_summary || null,
1102
+ recommendedStage: assessment?.recommended_stage || continueWithStage || null,
1103
+ continueWithStage: continueWithStage || null,
1104
+ escalationTarget: assessment?.escalation_target || "agent",
1105
+ reasons: Array.isArray(assessment?.reasons) ? assessment.reasons : [],
1106
+ raw: assessment,
1107
+ source: String(assessment?.source || state?.recon_assessment_source || "").trim() || null
1108
+ };
1109
+ }
1110
+ function updateState(statePath, mutate) {
1111
+ const state = readState(statePath) || {};
1112
+ mutate(state);
1113
+ writeState(statePath, state);
1114
+ return state;
1115
+ }
1116
+ var RUNTIME_EVENT_LIMIT = 100;
1117
+ function nowIso() {
1118
+ return (/* @__PURE__ */ new Date()).toISOString();
1119
+ }
1120
+ function appendRuntimeEventToState(state, event) {
1121
+ const events = Array.isArray(state.runtime_events) ? state.runtime_events : [];
1122
+ state.runtime_events = [...events, event].slice(-RUNTIME_EVENT_LIMIT);
1123
+ state.runtime_updated_at = event.ts;
1124
+ }
1125
+ function beginRuntimeStep(statePath, action, step, workflowPath) {
1126
+ const timer = {
1127
+ startedAt: nowIso(),
1128
+ startedMs: Date.now()
1129
+ };
1130
+ updateState(statePath, (state) => {
1131
+ const current = {
1132
+ step,
1133
+ action,
1134
+ status: "running",
1135
+ started_at: timer.startedAt,
1136
+ workflow_file: import_node_path2.default.basename(workflowPath)
1137
+ };
1138
+ state.current_runtime_step = current;
1139
+ appendRuntimeEventToState(state, {
1140
+ ts: timer.startedAt,
1141
+ kind: "workflow.step.started",
1142
+ step,
1143
+ action,
1144
+ summary: `Started ${step} workflow step.`,
1145
+ details: {
1146
+ workflow_file: import_node_path2.default.basename(workflowPath)
1147
+ }
1148
+ });
1149
+ });
1150
+ return timer;
1151
+ }
1152
+ function finishRuntimeStep(statePath, action, result, timer) {
1153
+ const finishedAt = nowIso();
1154
+ const durationMs = Date.now() - timer.startedMs;
1155
+ const summary = result.haltedForApproval ? `${result.step} halted for approval.` : result.ok ? `Finished ${result.step} workflow step.` : `${result.step} workflow step failed.`;
1156
+ updateState(statePath, (state) => {
1157
+ const completed = {
1158
+ step: result.step,
1159
+ action,
1160
+ status: result.haltedForApproval ? "approval_required" : result.ok ? "completed" : "failed",
1161
+ started_at: timer.startedAt,
1162
+ finished_at: finishedAt,
1163
+ duration_ms: durationMs,
1164
+ ok: result.ok,
1165
+ halted_for_approval: result.haltedForApproval || false,
1166
+ auto_approved: result.autoApproved || false,
1167
+ error: result.error || null
1168
+ };
1169
+ state.current_runtime_step = null;
1170
+ state.last_runtime_step = completed;
1171
+ appendRuntimeEventToState(state, {
1172
+ ts: finishedAt,
1173
+ kind: "workflow.step.finished",
1174
+ step: result.step,
1175
+ action,
1176
+ summary,
1177
+ details: completed
1178
+ });
1179
+ });
1180
+ return {
1181
+ ...result,
1182
+ started_at: timer.startedAt,
1183
+ finished_at: finishedAt,
1184
+ duration_ms: durationMs
1185
+ };
1186
+ }
1187
+ function executedStep(res, extra = {}) {
1188
+ const output = {
1189
+ step: res.step,
1190
+ ok: res.ok,
1191
+ haltedForApproval: res.haltedForApproval || false,
1192
+ autoApproved: res.autoApproved || false,
1193
+ ...extra
1194
+ };
1195
+ if (typeof res.duration_ms === "number") output.duration_ms = res.duration_ms;
1196
+ return output;
1197
+ }
1198
+ function hasSupervisorProofAssessment(state) {
1199
+ const proofAssessment = state?.proof_assessment || {};
1200
+ const source = String(proofAssessment?.source || state?.proof_assessment_source || "").trim().toLowerCase();
1201
+ if (!proofAssessment?.decision) return false;
1202
+ return source === "supervising_agent" || source === "supervisor";
1203
+ }
1204
+ function verifyAssessment(state) {
1205
+ const proofAssessment = state?.proof_assessment || {};
1206
+ const verifyDecision = state?.verify_decision_request || {};
1207
+ if (hasSupervisorProofAssessment(state)) {
1208
+ return {
1209
+ decision: proofAssessment?.decision || null,
1210
+ summary: proofAssessment?.summary || verifyDecision?.summary || null,
1211
+ recommendedStage: proofAssessment?.continue_with_stage || proofAssessment?.recommended_stage || verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || null,
1212
+ continueWithStage: proofAssessment?.continue_with_stage || verifyDecision?.continue_with_stage || proofAssessment?.recommended_stage || verifyDecision?.recommended_stage || null,
1213
+ escalationTarget: proofAssessment?.escalation_target || "agent",
1214
+ reasons: Array.isArray(proofAssessment?.reasons) ? proofAssessment.reasons : [],
1215
+ raw: proofAssessment,
1216
+ source: "supervising_agent"
1217
+ };
1218
+ }
1219
+ if (state?.verify_status === "capture_incomplete") {
1220
+ return {
1221
+ decision: verifyDecision?.capture_quality?.decision || "revise_capture",
1222
+ summary: verifyDecision?.summary || "Verify needs another internal capture iteration before the evidence can be judged.",
1223
+ recommendedStage: verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || "author",
1224
+ continueWithStage: verifyDecision?.continue_with_stage || verifyDecision?.recommended_stage || "author",
1225
+ escalationTarget: "agent",
1226
+ reasons: Array.isArray(verifyDecision?.capture_quality?.reasons) ? verifyDecision.capture_quality.reasons : [],
1227
+ raw: verifyDecision?.capture_quality || verifyDecision,
1228
+ source: "workflow_capture"
1229
+ };
1230
+ }
1231
+ return {
1232
+ decision: null,
1233
+ summary: verifyDecision?.summary || "Verify captured evidence and is waiting for supervising-agent proof assessment.",
1234
+ recommendedStage: null,
1235
+ continueWithStage: null,
1236
+ escalationTarget: "agent",
1237
+ reasons: [],
1238
+ raw: proofAssessment,
1239
+ source: "awaiting_supervisor"
1240
+ };
1241
+ }
1242
+ function nonConvergenceSignals(state, assessment = verifyAssessment(state)) {
1243
+ const verifyAttempts = Number(state?.stage_attempts?.verify?.count || 0);
1244
+ const authorAttempts = Number(state?.stage_attempts?.author?.count || 0);
1245
+ const reconAttempts = Number(state?.stage_attempts?.recon?.count || 0);
1246
+ const continueStage = assessment.continueWithStage || assessment.recommendedStage || null;
1247
+ return {
1248
+ verifyAttempts,
1249
+ authorAttempts,
1250
+ reconAttempts,
1251
+ continueStage,
1252
+ warning: verifyAttempts >= 4 || continueStage === "author" && verifyAttempts >= 2 && authorAttempts >= 2 || continueStage === "recon" && verifyAttempts >= 2 && reconAttempts >= 2 || continueStage === "implement" && verifyAttempts >= 2
1253
+ };
1254
+ }
1255
+ function shouldEscalateVerifyToHuman(_state, assessment = verifyAssessment(_state)) {
1256
+ return assessment.escalationTarget === "human";
1257
+ }
1258
+ function recommendedAdvanceStage(state) {
1259
+ if (!state?.workspace_ready) return "setup";
1260
+ if (!state?.recon_results || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) return "recon";
1261
+ if (!authorReady(state)) return "author";
1262
+ if (!implementationReady(state) && !noImplementationModeFor(state)) return "implement";
1263
+ if (state?.verify_status === "capture_incomplete") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage || "author";
1264
+ if (state?.verify_status === "evidence_captured") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage;
1265
+ if (!(state?.after_cdn || "").trim()) return "verify";
1266
+ return null;
1267
+ }
1268
+ function normalizeStageRequest(state, requestedAdvanceStage) {
1269
+ if (requestedAdvanceStage) return requestedAdvanceStage;
1270
+ if (!state?.workspace_ready) return null;
1271
+ if (!state?.recon_results || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) return "recon";
1272
+ return null;
1273
+ }
1274
+ function stringValue(value) {
1275
+ return typeof value === "string" && value.trim() ? value.trim() : "";
1276
+ }
1277
+ function commandResult(command, args, cwd, timeout = 6e4) {
1278
+ try {
1279
+ return {
1280
+ ok: true,
1281
+ stdout: (0, import_node_child_process.execFileSync)(command, args, { cwd, encoding: "utf-8", timeout, stdio: ["ignore", "pipe", "pipe"] }),
1282
+ stderr: ""
1283
+ };
1284
+ } catch (error) {
1285
+ return {
1286
+ ok: false,
1287
+ stdout: String(error?.stdout || ""),
1288
+ stderr: String(error?.stderr || error?.message || "")
1289
+ };
1290
+ }
1291
+ }
1292
+ function repoDirForSync(state) {
1293
+ const candidates = [
1294
+ state?.repo_dir,
1295
+ state?.after_worktree,
1296
+ state?.before_worktree
1297
+ ].map(stringValue).filter(Boolean);
1298
+ return candidates.find((candidate) => (0, import_node_fs2.existsSync)(import_node_path2.default.join(candidate, ".git"))) || "";
1299
+ }
1300
+ function parseWorktreeList(output) {
1301
+ const entries = [];
1302
+ let current = {};
1303
+ for (const line of output.split(/\r?\n/)) {
1304
+ if (!line.trim()) {
1305
+ if (current.worktree) entries.push(current);
1306
+ current = {};
1307
+ continue;
1308
+ }
1309
+ const [key, ...rest] = line.split(" ");
1310
+ const value = rest.join(" ").trim();
1311
+ if (key === "worktree" || key === "HEAD" || key === "branch" || key === "detached") current[key] = value;
1312
+ }
1313
+ if (current.worktree) entries.push(current);
1314
+ return entries;
1315
+ }
1316
+ function gitStdout(cwd, args, timeout = 6e4) {
1317
+ const result = commandResult("git", args, cwd, timeout);
1318
+ return result.ok ? result.stdout.trim() : "";
1319
+ }
1320
+ function shortBranch(ref) {
1321
+ return ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
1322
+ }
1323
+ function safeInteger(value) {
1324
+ const parsed = Number.parseInt(value, 10);
1325
+ return Number.isFinite(parsed) ? parsed : null;
1326
+ }
1327
+ function baseCheckoutReport(repoDir, baseBranch, updateRequested, updateAllowed) {
1328
+ const remoteRef = `origin/${baseBranch}`;
1329
+ const report = {
1330
+ requested: updateRequested,
1331
+ repo_dir: repoDir,
1332
+ base_branch: baseBranch,
1333
+ remote_ref: remoteRef,
1334
+ updated: false
1335
+ };
1336
+ const listed = commandResult("git", ["worktree", "list", "--porcelain"], repoDir, 6e4);
1337
+ if (!listed.ok) {
1338
+ report.update_skipped = "worktree_list_failed";
1339
+ report.error = listed.stderr.slice(0, 300);
1340
+ return report;
1341
+ }
1342
+ const worktrees = parseWorktreeList(listed.stdout);
1343
+ const baseRef = `refs/heads/${baseBranch}`;
1344
+ const selected = worktrees.find((entry) => entry.branch === baseRef) || worktrees.find((entry) => import_node_path2.default.resolve(entry.worktree || "") === import_node_path2.default.resolve(repoDir) && shortBranch(entry.branch || "") === baseBranch);
1345
+ if (!selected?.worktree) {
1346
+ report.worktrees_seen = worktrees.map((entry) => ({
1347
+ path: entry.worktree || null,
1348
+ branch: entry.branch ? shortBranch(entry.branch) : null,
1349
+ detached: Boolean(entry.detached)
1350
+ }));
1351
+ report.update_skipped = "base_worktree_not_found";
1352
+ return report;
1353
+ }
1354
+ const baseDir = selected.worktree;
1355
+ const branch = shortBranch(selected.branch || "");
1356
+ const status = commandResult("git", ["status", "--porcelain"], baseDir, 6e4);
1357
+ const clean = status.ok && !status.stdout.trim();
1358
+ const localHead = gitStdout(baseDir, ["rev-parse", "HEAD"]);
1359
+ const remoteHead = gitStdout(baseDir, ["rev-parse", "--verify", remoteRef]);
1360
+ const counts = remoteHead ? gitStdout(baseDir, ["rev-list", "--left-right", "--count", `HEAD...${remoteRef}`]) : "";
1361
+ const [aheadRaw, behindRaw] = counts.split(/\s+/);
1362
+ Object.assign(report, {
1363
+ base_worktree: baseDir,
1364
+ branch: branch || null,
1365
+ clean,
1366
+ local_head: localHead || null,
1367
+ remote_head: remoteHead || null,
1368
+ ahead: safeInteger(aheadRaw || ""),
1369
+ behind: safeInteger(behindRaw || "")
1370
+ });
1371
+ if (!updateRequested) {
1372
+ report.update_skipped = "update_not_requested";
1373
+ return report;
1374
+ }
1375
+ if (!updateAllowed) {
1376
+ report.update_skipped = "fetch_failed";
1377
+ return report;
1378
+ }
1379
+ if (branch !== baseBranch) {
1380
+ report.update_skipped = "base_worktree_not_on_base_branch";
1381
+ return report;
1382
+ }
1383
+ if (!status.ok) {
1384
+ report.update_skipped = "status_failed";
1385
+ report.status_error = status.stderr.slice(0, 300);
1386
+ return report;
1387
+ }
1388
+ if (!clean) {
1389
+ report.update_skipped = "base_worktree_dirty";
1390
+ return report;
1391
+ }
1392
+ if (!remoteHead) {
1393
+ report.update_skipped = "remote_ref_missing";
1394
+ return report;
1395
+ }
1396
+ if (localHead && localHead === remoteHead) {
1397
+ report.update_skipped = "already_current";
1398
+ return report;
1399
+ }
1400
+ const merge = commandResult("git", ["merge", "--ff-only", remoteRef], baseDir, 12e4);
1401
+ if (!merge.ok) {
1402
+ report.update_skipped = "fast_forward_failed";
1403
+ report.update_error = merge.stderr.slice(0, 500);
1404
+ return report;
1405
+ }
1406
+ const updatedHead = gitStdout(baseDir, ["rev-parse", "HEAD"]);
1407
+ const updatedCounts = gitStdout(baseDir, ["rev-list", "--left-right", "--count", `HEAD...${remoteRef}`]);
1408
+ const [updatedAheadRaw, updatedBehindRaw] = updatedCounts.split(/\s+/);
1409
+ report.updated = true;
1410
+ report.local_head = updatedHead || report.local_head;
1411
+ report.ahead = safeInteger(updatedAheadRaw || "");
1412
+ report.behind = safeInteger(updatedBehindRaw || "");
1413
+ report.update_summary = merge.stdout.trim().slice(0, 500);
1414
+ return report;
1415
+ }
1416
+ function normalizeGhPrStatus(value) {
1417
+ const status = stringValue(value).toLowerCase();
1418
+ if (status === "merged") return "merged";
1419
+ if (status === "open") return "open";
1420
+ if (status === "closed") return "closed";
1421
+ return status || "unknown";
1422
+ }
1423
+ function prRefFromState(state) {
1424
+ return stringValue(state?.pr_number) || stringValue(state?.pr_url);
1425
+ }
1426
+ function prNumberFromUrl(url) {
1427
+ const match = url.match(/\/pull\/(\d+)(?:$|[?#])/);
1428
+ return match?.[1] || "";
1429
+ }
1430
+ function normalizePrState(raw, state, checkedAt = (/* @__PURE__ */ new Date()).toISOString()) {
1431
+ const mergeCommit = typeof raw?.mergeCommit === "object" && raw.mergeCommit ? stringValue(raw.mergeCommit.oid) : stringValue(raw?.mergeCommit);
1432
+ const url = stringValue(raw?.url) || stringValue(state?.pr_url);
1433
+ return {
1434
+ status: normalizeGhPrStatus(raw?.state),
1435
+ pr_url: url || null,
1436
+ pr_number: String(raw?.number || state?.pr_number || prNumberFromUrl(url) || ""),
1437
+ repo: stringValue(state?.repo) || null,
1438
+ head_branch: stringValue(raw?.headRefName) || stringValue(state?.target_branch) || stringValue(state?.branch) || null,
1439
+ base_branch: stringValue(raw?.baseRefName) || stringValue(state?.base_branch) || "main",
1440
+ merge_commit: mergeCommit || null,
1441
+ merged_at: stringValue(raw?.mergedAt) || null,
1442
+ closed_at: stringValue(raw?.closedAt) || null,
1443
+ checked_at: checkedAt,
1444
+ source: "gh"
1445
+ };
1446
+ }
1447
+ function cleanupMergedProofRun(state, repoDir, params, prState) {
1448
+ const cleanup = {
1449
+ requested: params.cleanup_merged_pr !== false,
1450
+ fetch_base: params.fetch_base !== false,
1451
+ update_base_checkout: params.update_base_checkout !== false,
1452
+ repo_dir: repoDir,
1453
+ worktrees_removed: [],
1454
+ worktree_remove_errors: [],
1455
+ branches_deleted: [],
1456
+ branch_delete_errors: [],
1457
+ pruned: false
1458
+ };
1459
+ const baseBranch = stringValue(prState.base_branch) || stringValue(state?.base_branch) || "main";
1460
+ let fetchedBase = params.fetch_base === false;
1461
+ if (params.fetch_base !== false && baseBranch) {
1462
+ const fetch = commandResult("git", ["fetch", "origin", baseBranch], repoDir, 12e4);
1463
+ cleanup.fetch = fetch.ok ? { ok: true, base_branch: baseBranch } : { ok: false, base_branch: baseBranch, error: fetch.stderr.slice(0, 300) };
1464
+ if (fetch.ok) {
1465
+ fetchedBase = true;
1466
+ state.base_synced_at = (/* @__PURE__ */ new Date()).toISOString();
1467
+ state.base_branch = baseBranch;
1468
+ }
1469
+ }
1470
+ cleanup.base_checkout = baseCheckoutReport(repoDir, baseBranch, params.update_base_checkout !== false, fetchedBase);
1471
+ if (params.cleanup_merged_pr === false) {
1472
+ cleanup.skipped = "cleanup_disabled";
1473
+ return cleanup;
1474
+ }
1475
+ const removed = [];
1476
+ const removeErrors = [];
1477
+ for (const candidate of [state?.before_worktree, state?.after_worktree].map(stringValue).filter(Boolean)) {
1478
+ if (!(0, import_node_fs2.existsSync)(candidate) || import_node_path2.default.resolve(candidate) === import_node_path2.default.resolve(repoDir)) continue;
1479
+ const remove = commandResult("git", ["worktree", "remove", "--force", candidate], repoDir, 12e4);
1480
+ if (remove.ok) {
1481
+ removed.push(candidate);
1482
+ } else {
1483
+ removeErrors.push({ path: candidate, error: remove.stderr.slice(0, 300) });
1484
+ }
1485
+ }
1486
+ cleanup.worktrees_removed = removed;
1487
+ cleanup.worktree_remove_errors = removeErrors;
1488
+ const afterBranch = stringValue(state?.after_worktree_branch);
1489
+ if (afterBranch.startsWith("riddle-proof/")) {
1490
+ const deleted = commandResult("git", ["branch", "-D", afterBranch], repoDir, 6e4);
1491
+ if (deleted.ok) {
1492
+ cleanup.branches_deleted = [afterBranch];
1493
+ } else {
1494
+ cleanup.branch_delete_errors = [{ branch: afterBranch, error: deleted.stderr.slice(0, 300) }];
1495
+ }
1496
+ }
1497
+ const prune = commandResult("git", ["worktree", "prune"], repoDir, 6e4);
1498
+ cleanup.pruned = prune.ok;
1499
+ if (!prune.ok) cleanup.prune_error = prune.stderr.slice(0, 300);
1500
+ return cleanup;
1501
+ }
1502
+ function syncPrLifecycle(statePath, params) {
1503
+ const state = readState(statePath);
1504
+ if (!state) {
1505
+ return {
1506
+ ok: false,
1507
+ action: "sync",
1508
+ state_path: statePath,
1509
+ checkpoint: "pr_sync_not_found",
1510
+ summary: "No readable Riddle Proof state exists at state_path.",
1511
+ state: null,
1512
+ nextAction: "Check the wrapper state_path or run riddle_proof_status first."
1513
+ };
1514
+ }
1515
+ const repoDir = repoDirForSync(state);
1516
+ const prRef = prRefFromState(state);
1517
+ if (!repoDir || !prRef) {
1518
+ const missingPr = !prRef;
1519
+ 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.";
1520
+ 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.";
1521
+ const prState2 = {
1522
+ status: missingPr ? "orphaned" : "unavailable",
1523
+ pr_url: state.pr_url || null,
1524
+ pr_number: String(state.pr_number || prNumberFromUrl(stringValue(state.pr_url)) || ""),
1525
+ repo: state.repo || null,
1526
+ head_branch: state.target_branch || state.branch || null,
1527
+ base_branch: state.base_branch || "main",
1528
+ checked_at: (/* @__PURE__ */ new Date()).toISOString(),
1529
+ source: repoDir ? "state" : "local_state",
1530
+ sync_recoverable: !missingPr,
1531
+ sync_blocker: missingPr ? "missing_pr_linkage" : "missing_local_repo",
1532
+ next_action: missingPr ? orphanNextAction : "State has a PR but no readable local git repo/worktree; restore repo access and rerun sync."
1533
+ };
1534
+ state.pr_state = prState2;
1535
+ if (missingPr) state.pr_sync_summary = orphanSummary;
1536
+ writeState(statePath, state);
1537
+ return {
1538
+ ok: false,
1539
+ action: "sync",
1540
+ state_path: statePath,
1541
+ checkpoint: missingPr ? "pr_sync_no_pr" : "pr_sync_unavailable",
1542
+ summary: missingPr ? orphanSummary : prState2.next_action,
1543
+ state: summarizeState(state).state,
1544
+ pr_state: prState2,
1545
+ nextAction: prState2.next_action
1546
+ };
1547
+ }
1548
+ const viewed = commandResult("gh", ["pr", "view", prRef, "--json", "state,mergedAt,closedAt,mergeCommit,headRefName,baseRefName,url,number"], repoDir, 6e4);
1549
+ if (!viewed.ok) {
1550
+ const prState2 = {
1551
+ status: "unavailable",
1552
+ pr_url: state.pr_url || null,
1553
+ pr_number: String(state.pr_number || prNumberFromUrl(stringValue(state.pr_url)) || ""),
1554
+ repo: state.repo || null,
1555
+ head_branch: state.target_branch || state.branch || null,
1556
+ base_branch: state.base_branch || "main",
1557
+ checked_at: (/* @__PURE__ */ new Date()).toISOString(),
1558
+ source: "gh",
1559
+ next_action: "GitHub PR state is unavailable; fix gh auth/repo access and rerun riddle_proof_sync."
1560
+ };
1561
+ state.pr_state = prState2;
1562
+ state.cleanup_report = { requested: params.cleanup_merged_pr !== false, skipped: "pr_state_unavailable", error: viewed.stderr.slice(0, 300) };
1563
+ writeState(statePath, state);
1564
+ return {
1565
+ ok: false,
1566
+ action: "sync",
1567
+ state_path: statePath,
1568
+ checkpoint: "pr_sync_unavailable",
1569
+ summary: prState2.next_action,
1570
+ state: summarizeState(state).state,
1571
+ pr_state: prState2,
1572
+ cleanup: state.cleanup_report,
1573
+ nextAction: prState2.next_action
1574
+ };
1575
+ }
1576
+ let rawPr;
1577
+ try {
1578
+ rawPr = JSON.parse(viewed.stdout);
1579
+ } catch {
1580
+ rawPr = {};
1581
+ }
1582
+ const prState = normalizePrState(rawPr, state);
1583
+ let cleanup = null;
1584
+ let checkpoint = "pr_sync_open";
1585
+ let ok = true;
1586
+ let summary = "PR is still open; no merge cleanup was performed.";
1587
+ let nextAction = "Wait for the PR to merge, then rerun riddle_proof_sync.";
1588
+ if (prState.status === "merged") {
1589
+ cleanup = cleanupMergedProofRun(state, repoDir, params, prState);
1590
+ prState.cleanup = cleanup;
1591
+ prState.next_action = "The PR is merged; sync recorded proof cleanup and the local base checkout refresh status.";
1592
+ state.finalized = true;
1593
+ state.merge_commit = prState.merge_commit || state.merge_commit || "";
1594
+ state.merged_at = prState.merged_at || state.merged_at || "";
1595
+ state.cleanup_report = cleanup;
1596
+ checkpoint = "pr_sync_merged";
1597
+ summary = "PR is merged and Riddle Proof state has been reconciled.";
1598
+ 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.";
1599
+ } else if (prState.status === "closed") {
1600
+ prState.next_action = "The PR is closed without a merge; inspect the PR before reusing or deleting the branch.";
1601
+ checkpoint = "pr_sync_closed";
1602
+ summary = "PR is closed without a merge; no merge cleanup was performed.";
1603
+ nextAction = prState.next_action;
1604
+ } else if (prState.status !== "open") {
1605
+ ok = false;
1606
+ prState.next_action = "PR state was not recognized; inspect gh pr view output and rerun sync.";
1607
+ checkpoint = "pr_sync_unavailable";
1608
+ summary = prState.next_action;
1609
+ nextAction = prState.next_action;
1610
+ }
1611
+ state.pr_state = prState;
1612
+ state.pr_url = prState.pr_url || state.pr_url;
1613
+ state.pr_number = prState.pr_number || state.pr_number;
1614
+ state.target_branch = prState.head_branch || state.target_branch || state.branch;
1615
+ state.branch = prState.head_branch || state.branch;
1616
+ state.base_branch = prState.base_branch || state.base_branch;
1617
+ writeState(statePath, state);
1618
+ const snapshot = summarizeState(state);
1619
+ return {
1620
+ ok,
1621
+ action: "sync",
1622
+ state_path: statePath,
1623
+ checkpoint,
1624
+ summary,
1625
+ state: snapshot.state,
1626
+ pr_state: prState,
1627
+ cleanup,
1628
+ nextAction
1629
+ };
1630
+ }
1631
+ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
1632
+ const config = resolvedConfig || resolveConfig(pluginConfig, params);
1633
+ const action = ensureAction(params.action);
1634
+ if (!(0, import_node_fs2.existsSync)(config.riddleProofDir)) {
1635
+ throw new Error(`riddle-proof runtime directory not found: ${config.riddleProofDir}`);
1636
+ }
1637
+ if (action === "status") {
1638
+ return {
1639
+ state_path: config.statePath,
1640
+ ...summarizeState(readState(config.statePath))
1641
+ };
1642
+ }
1643
+ if (action === "sync") {
1644
+ return syncPrLifecycle(config.statePath, params);
1645
+ }
1646
+ const stateKey = import_node_path2.default.basename(config.statePath).replace(/[^A-Za-z0-9_.-]/g, "-");
1647
+ const lobsterStateDir = import_node_path2.default.join(import_node_path2.default.dirname(config.statePath), "riddle-proof-lobster-state", stateKey);
1648
+ (0, import_node_fs2.mkdirSync)(lobsterStateDir, { recursive: true });
1649
+ const env = {
1650
+ ...process.env,
1651
+ RIDDLE_PROOF_DIR: config.riddleProofDir,
1652
+ RIDDLE_PROOF_STATE_FILE: config.statePath,
1653
+ RIDDLE_PROOF_ARGS_FILE: config.argsPath,
1654
+ LOBSTER_STATE_DIR: lobsterStateDir
1655
+ };
1656
+ const lobsterCommand = process.env.RIDDLE_PROOF_LOBSTER_COMMAND || "lobster";
1657
+ const lobsterPrefix = process.env.RIDDLE_PROOF_LOBSTER_SCRIPT ? [process.env.RIDDLE_PROOF_LOBSTER_SCRIPT] : [];
1658
+ const runOne = (step) => {
1659
+ const args = step === "setup" ? buildSetupArgs(params, config) : {};
1660
+ const stepWorkflowFile = workflowFile(config.riddleProofDir, step);
1661
+ const timer = beginRuntimeStep(config.statePath, action, step, stepWorkflowFile);
1662
+ let output;
1663
+ try {
1664
+ if (step === "setup") {
1665
+ (0, import_node_fs2.mkdirSync)(import_node_path2.default.dirname(config.argsPath), { recursive: true });
1666
+ (0, import_node_fs2.writeFileSync)(config.argsPath, JSON.stringify(args, null, 2));
1667
+ }
1668
+ output = JSON.parse(
1669
+ (0, import_node_child_process.execFileSync)(lobsterCommand, [...lobsterPrefix, "run", "--file", stepWorkflowFile, "--args-json", JSON.stringify(args)], {
1670
+ encoding: "utf-8",
1671
+ env
1672
+ })
1673
+ );
1674
+ } catch (error) {
1675
+ return finishRuntimeStep(config.statePath, action, {
1676
+ ok: false,
1677
+ step,
1678
+ error: error?.message || String(error),
1679
+ stdout: String(error?.stdout || ""),
1680
+ stderr: String(error?.stderr || "")
1681
+ }, timer);
1682
+ }
1683
+ if (output?.status === "needs_approval") {
1684
+ if (!params.auto_approve) {
1685
+ return finishRuntimeStep(config.statePath, action, {
1686
+ ok: false,
1687
+ haltedForApproval: true,
1688
+ step,
1689
+ approval: output.requiresApproval || null,
1690
+ raw: output
1691
+ }, timer);
1692
+ }
1693
+ const token = output?.requiresApproval?.resumeToken;
1694
+ if (!token) {
1695
+ return finishRuntimeStep(config.statePath, action, {
1696
+ ok: false,
1697
+ step,
1698
+ error: `${step} requested approval without a resume token.`,
1699
+ raw: output
1700
+ }, timer);
1701
+ }
1702
+ let resumed;
1703
+ try {
1704
+ resumed = JSON.parse(
1705
+ (0, import_node_child_process.execFileSync)(lobsterCommand, [...lobsterPrefix, "resume", "--token", token, "--approve", "yes"], {
1706
+ encoding: "utf-8",
1707
+ env
1708
+ })
1709
+ );
1710
+ } catch (error) {
1711
+ return finishRuntimeStep(config.statePath, action, {
1712
+ ok: false,
1713
+ step,
1714
+ autoApproved: true,
1715
+ error: error?.message || String(error),
1716
+ stdout: String(error?.stdout || ""),
1717
+ stderr: String(error?.stderr || "")
1718
+ }, timer);
1719
+ }
1720
+ return finishRuntimeStep(config.statePath, action, {
1721
+ ok: resumed?.ok !== false,
1722
+ step,
1723
+ autoApproved: true,
1724
+ raw: resumed
1725
+ }, timer);
1726
+ }
1727
+ return finishRuntimeStep(config.statePath, action, {
1728
+ ok: output?.ok !== false,
1729
+ step,
1730
+ raw: output
1731
+ }, timer);
1732
+ };
1733
+ let effectiveAdvanceStage = params.advance_stage || null;
1734
+ const recordAttempt = (stage, status, summary, extra = {}) => {
1735
+ updateState(config.statePath, (state) => {
1736
+ recordStageAttempt(state, stage, {
1737
+ status,
1738
+ summary,
1739
+ checkpoint: extra.checkpoint || null,
1740
+ requestedAdvanceStage: effectiveAdvanceStage || null,
1741
+ haltedForApproval: extra.haltedForApproval,
1742
+ autoApproved: extra.autoApproved,
1743
+ retryable: extra.retryable,
1744
+ checkpointDisposition: extra.checkpointDisposition || null,
1745
+ error: extra.error || null,
1746
+ details: extra.details || {}
1747
+ });
1748
+ });
1749
+ };
1750
+ const checkpoint = (stage, name, summary, extra = {}) => {
1751
+ const decision = updateState(config.statePath, (state) => {
1752
+ const checkpointContract = buildCheckpointContract(state, {
1753
+ statePath: config.statePath,
1754
+ stage,
1755
+ checkpoint: name,
1756
+ summary,
1757
+ nextActions: extra.nextActions,
1758
+ advanceOptions: extra.advanceOptions,
1759
+ recommendedAdvanceStage: extra.recommendedAdvanceStage,
1760
+ continueWithStage: extra.continueWithStage,
1761
+ blocking: extra.blocking
1762
+ });
1763
+ setStageDecisionRequest(state, {
1764
+ stage,
1765
+ checkpoint: name,
1766
+ summary,
1767
+ nextActions: extra.nextActions,
1768
+ advanceOptions: extra.advanceOptions,
1769
+ recommendedAdvanceStage: extra.recommendedAdvanceStage,
1770
+ continueWithStage: extra.continueWithStage,
1771
+ blocking: extra.blocking,
1772
+ details: extra.details,
1773
+ checkpointContract
1774
+ });
1775
+ }).stage_decision_request;
1776
+ const snapshot2 = snapshotFor(config.statePath);
1777
+ return {
1778
+ ok: extra.ok ?? true,
1779
+ action,
1780
+ state_path: config.statePath,
1781
+ stage: snapshot2.stage,
1782
+ checkpoint: name,
1783
+ summary,
1784
+ state: snapshot2.state,
1785
+ decisionRequest: decision,
1786
+ checkpointContract: decision?.checkpoint_contract || null,
1787
+ ...extra
1788
+ };
1789
+ };
1790
+ const primaryShipGateNextAction = (shipGate) => {
1791
+ const reasons = shipGate.reasons || [];
1792
+ if (reasons.some((reason) => reason.includes("proof_assessment"))) {
1793
+ 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";
1794
+ }
1795
+ if (reasons.some((reason) => reason.includes("visual_delta"))) {
1796
+ 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";
1797
+ }
1798
+ if (reasons.some((reason) => reason.includes("after_cdn") || reason.includes("verify_status"))) {
1799
+ return "rerun verify with stronger proof framing so after evidence is captured before shipping";
1800
+ }
1801
+ if (reasons.some((reason) => reason.includes("before_cdn") || reason.includes("prod_cdn") || reason.includes("prod_url"))) {
1802
+ return "return to recon and capture the missing required baseline before shipping";
1803
+ }
1804
+ return "inspect the ship gate details, repair the missing invariant, then resume the run";
1805
+ };
1806
+ const shipGateRecoveryStage = (shipGate) => {
1807
+ const reasons = shipGate.reasons || [];
1808
+ if (reasons.some((reason) => reason.includes("before_cdn") || reason.includes("prod_cdn") || reason.includes("prod_url"))) {
1809
+ return "recon";
1810
+ }
1811
+ if (reasons.some((reason) => reason.includes("implementation"))) {
1812
+ return "implement";
1813
+ }
1814
+ if (reasons.some((reason) => reason.includes("after_cdn") || reason.includes("verify_status") || reason.includes("visual_delta"))) {
1815
+ return "verify";
1816
+ }
1817
+ if (reasons.some((reason) => reason.includes("proof_assessment"))) {
1818
+ return "verify";
1819
+ }
1820
+ return "verify";
1821
+ };
1822
+ const shipGateBlocked = (state, executed, details = {}) => {
1823
+ const shipGate = validateShipGate(state);
1824
+ const nextAction = primaryShipGateNextAction(shipGate);
1825
+ const recoveryStage = shipGateRecoveryStage(shipGate);
1826
+ const advanceOptions = Array.from(/* @__PURE__ */ new Set([recoveryStage, "verify", "author", "implement", "recon", "ship"]));
1827
+ return checkpoint(
1828
+ "verify",
1829
+ "ship_gate_blocked",
1830
+ `Ship is blocked until the proof bundle satisfies the hard ship gate. Next action: ${nextAction}.`,
1831
+ {
1832
+ ok: false,
1833
+ nextActions: ["inspect_ship_gate", "advance_run_to_verify", "supply_proof_assessment_json", "return_to_recon_if_baseline_is_missing"],
1834
+ advanceOptions,
1835
+ recommendedAdvanceStage: recoveryStage,
1836
+ continueWithStage: recoveryStage,
1837
+ blocking: true,
1838
+ details: { ...details, shipGate, next_action: nextAction, recovery_stage: recoveryStage, executed },
1839
+ nextAction,
1840
+ recoveryStage,
1841
+ shipGate,
1842
+ verifyStatus: state?.verify_status || null,
1843
+ mergeRecommendation: state?.merge_recommendation || null,
1844
+ afterCdn: state?.after_cdn || null,
1845
+ proofAssessment: state?.proof_assessment || null,
1846
+ proofAssessmentRequest: state?.proof_assessment_request || null,
1847
+ executed
1848
+ }
1849
+ );
1850
+ };
1851
+ const failedRun = (stage, summary, res, extra = {}) => {
1852
+ recordAttempt(stage, res?.haltedForApproval ? "approval_required" : "failed", summary, {
1853
+ checkpoint: extra.checkpoint || null,
1854
+ haltedForApproval: res?.haltedForApproval || false,
1855
+ autoApproved: res?.autoApproved || false,
1856
+ error: res?.error || null,
1857
+ details: extra.details
1858
+ });
1859
+ const snapshot2 = snapshotFor(config.statePath);
1860
+ return {
1861
+ ok: false,
1862
+ action,
1863
+ state_path: config.statePath,
1864
+ stage: snapshot2.stage,
1865
+ summary,
1866
+ state: snapshot2.state,
1867
+ approval: res?.approval || null,
1868
+ error: res?.error || null,
1869
+ checkpoint: extra.checkpoint || null,
1870
+ ...extra
1871
+ };
1872
+ };
1873
+ if (action !== "setup") {
1874
+ mergeStateFromParams(config.statePath, params);
1875
+ }
1876
+ if (action === "run") {
1877
+ const executed = [];
1878
+ let state = readState(config.statePath);
1879
+ if (!state || !state.workspace_ready || params.advance_stage === "setup") {
1880
+ const setupRes = runOne("setup");
1881
+ executed.push(executedStep(setupRes));
1882
+ if (!setupRes.ok || setupRes.haltedForApproval) {
1883
+ return failedRun("setup", setupRes.haltedForApproval ? "setup halted for approval" : "setup failed", setupRes, {
1884
+ checkpoint: "setup_blocked"
1885
+ });
1886
+ }
1887
+ recordAttempt("setup", "completed", "Setup completed and state/worktrees are ready.", {
1888
+ checkpoint: params.advance_stage === "setup" ? "setup_review" : null,
1889
+ autoApproved: setupRes.autoApproved || false
1890
+ });
1891
+ mergeStateFromParams(config.statePath, params);
1892
+ state = readState(config.statePath);
1893
+ if (params.advance_stage === "setup") {
1894
+ return checkpoint(
1895
+ "setup",
1896
+ "setup_review",
1897
+ "Setup completed. Inspect the prepared workspace and explicitly advance to recon when ready.",
1898
+ {
1899
+ nextActions: ["inspect_setup_state", "advance_run_to_recon"],
1900
+ advanceOptions: ["recon", "setup"],
1901
+ recommendedAdvanceStage: "recon",
1902
+ details: { executed },
1903
+ executed
1904
+ }
1905
+ );
1906
+ }
1907
+ }
1908
+ state = readState(config.statePath);
1909
+ const continuedStage = params.continue_from_checkpoint ? checkpointContinueStage(state) : null;
1910
+ if (params.continue_from_checkpoint && !params.advance_stage && !continuedStage) {
1911
+ const recommended = recommendedAdvanceStage(state);
1912
+ return checkpoint(
1913
+ state?.active_checkpoint_stage || recommended || "recon",
1914
+ "continue_unavailable",
1915
+ "This run call asked to continue from a checkpoint, but the current state has no resumable checkpoint. Inspect status or set advance_stage explicitly.",
1916
+ {
1917
+ ok: false,
1918
+ nextActions: ["inspect_state", "set_advance_stage", "resume_run"],
1919
+ advanceOptions: ["recon", "author", "implement", "verify", "ship"],
1920
+ recommendedAdvanceStage: null,
1921
+ blocking: true,
1922
+ details: {
1923
+ executed,
1924
+ activeCheckpoint: state?.active_checkpoint || null,
1925
+ suggestedAdvanceStage: recommended || null
1926
+ },
1927
+ suggestedAdvanceStage: recommended || null,
1928
+ executed
1929
+ }
1930
+ );
1931
+ }
1932
+ effectiveAdvanceStage = params.advance_stage || continuedStage || null;
1933
+ if (effectiveAdvanceStage) {
1934
+ updateState(config.statePath, (state2) => {
1935
+ clearStageDecisionRequest(state2);
1936
+ state2.last_requested_advance_stage = effectiveAdvanceStage;
1937
+ });
1938
+ state = readState(config.statePath);
1939
+ }
1940
+ let requestedStage = normalizeStageRequest(state, effectiveAdvanceStage);
1941
+ const reconCheckpointActive = ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "") || state?.active_checkpoint === "recon_supervisor_judgment";
1942
+ if (requestedStage === "recon" && reconCheckpointActive) {
1943
+ const latestAttempt = latestReconAttempt(state);
1944
+ const latestCapturedBaselines = latestReconCapturedBaselines(state);
1945
+ const latestAssessment = reconAssessment(state);
1946
+ const reconAssessmentRequest = state?.recon_assessment_request || state?.recon_decision_request || null;
1947
+ const reconDetails = {
1948
+ executed,
1949
+ latestAttempt,
1950
+ latestCapturedBaselines,
1951
+ reconAssessmentRequest,
1952
+ reconAssessment: latestAssessment.raw
1953
+ };
1954
+ if (!hasSupervisorReconAssessment(state)) {
1955
+ return checkpoint(
1956
+ "recon",
1957
+ "recon_supervisor_judgment",
1958
+ "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.",
1959
+ {
1960
+ nextActions: ["inspect_recon_packet", "supply_recon_assessment_json", "continue_internal_loop_with_checkpoint"],
1961
+ advanceOptions: ["recon", "author"],
1962
+ recommendedAdvanceStage: "recon",
1963
+ continueWithStage: "recon",
1964
+ blocking: false,
1965
+ details: reconDetails,
1966
+ reconAssessmentRequest,
1967
+ reconDecisionRequest: state?.recon_decision_request || null,
1968
+ executed
1969
+ }
1970
+ );
1971
+ }
1972
+ if (latestAssessment.decision === "recon_stuck" && latestAssessment.escalationTarget === "human") {
1973
+ const summary = latestAssessment.summary || "The supervising agent concluded recon is genuinely stuck and should escalate to the human.";
1974
+ recordAttempt("recon", "escalated", summary, {
1975
+ checkpoint: "recon_human_escalation",
1976
+ details: reconDetails
1977
+ });
1978
+ return checkpoint(
1979
+ "recon",
1980
+ "recon_human_escalation",
1981
+ summary,
1982
+ {
1983
+ ok: false,
1984
+ nextActions: ["inspect_recon_history", "summarize_failed_baselines", "ask_human_for_direction"],
1985
+ advanceOptions: ["recon", "author"],
1986
+ recommendedAdvanceStage: null,
1987
+ continueWithStage: null,
1988
+ blocking: true,
1989
+ details: reconDetails,
1990
+ reconAssessment: latestAssessment.raw,
1991
+ reconAssessmentRequest,
1992
+ executed
1993
+ }
1994
+ );
1995
+ }
1996
+ if ((latestAssessment.decision === "ready_for_author" || latestAssessment.continueWithStage === "author") && latestReconHasRequiredBaselines(state) && hasReconBaselineUnderstanding(state)) {
1997
+ updateState(config.statePath, (currentState) => {
1998
+ promoteLatestReconBaselines(currentState);
1999
+ currentState.recon_status = "ready_for_proof_plan";
2000
+ currentState.recon_results = currentState.recon_results || {};
2001
+ currentState.recon_results.status = "ready_for_proof_plan";
2002
+ currentState.recon_assessment_request = {};
2003
+ currentState.recon_decision_request = {};
2004
+ if ((currentState.proof_plan || "").trim() && (currentState.capture_script || "").trim()) {
2005
+ currentState.author_status = "ready";
2006
+ currentState.proof_plan_status = "ready";
2007
+ } else if (!authorReady(currentState)) {
2008
+ currentState.author_status = "needs_authoring";
2009
+ currentState.proof_plan_status = "needs_authoring";
2010
+ }
2011
+ });
2012
+ state = readState(config.statePath);
2013
+ const approvedSummary = latestAssessment.summary || "The supervising agent approved the latest recon baseline and selected the route for proof authoring.";
2014
+ if (params.advance_stage === "recon") {
2015
+ recordAttempt("recon", "completed", approvedSummary, {
2016
+ checkpoint: "recon_review",
2017
+ details: {
2018
+ ...reconDetails,
2019
+ promotedBaselines: latestReconCapturedBaselines(state)
2020
+ }
2021
+ });
2022
+ return checkpoint(
2023
+ "recon",
2024
+ "recon_review",
2025
+ approvedSummary,
2026
+ {
2027
+ nextActions: ["inspect_recon_baseline", "continue_internal_loop_with_checkpoint", "advance_run_to_author"],
2028
+ advanceOptions: ["author", "recon", "implement"],
2029
+ recommendedAdvanceStage: "author",
2030
+ continueWithStage: "author",
2031
+ blocking: false,
2032
+ details: {
2033
+ ...reconDetails,
2034
+ promotedBaselines: latestReconCapturedBaselines(state)
2035
+ },
2036
+ reconAssessment: latestAssessment.raw,
2037
+ executed
2038
+ }
2039
+ );
2040
+ }
2041
+ recordAttempt("recon", "completed", approvedSummary, {
2042
+ checkpoint: "recon_auto_continue",
2043
+ details: {
2044
+ ...reconDetails,
2045
+ promotedBaselines: latestReconCapturedBaselines(state)
2046
+ }
2047
+ });
2048
+ effectiveAdvanceStage = "author";
2049
+ updateState(config.statePath, (currentState) => {
2050
+ currentState.last_requested_advance_stage = "author";
2051
+ });
2052
+ state = readState(config.statePath);
2053
+ requestedStage = normalizeStageRequest(state, effectiveAdvanceStage);
2054
+ } else if (latestAssessment.decision === "ready_for_author") {
2055
+ const missingUnderstanding = latestReconHasRequiredBaselines(state) && !hasReconBaselineUnderstanding(state);
2056
+ return checkpoint(
2057
+ "recon",
2058
+ "recon_supervisor_judgment",
2059
+ 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.",
2060
+ {
2061
+ ok: false,
2062
+ nextActions: ["inspect_recon_packet", "refine_recon_plan", "continue_internal_loop_with_checkpoint"],
2063
+ advanceOptions: ["recon", "author"],
2064
+ recommendedAdvanceStage: "recon",
2065
+ continueWithStage: "recon",
2066
+ blocking: false,
2067
+ details: reconDetails,
2068
+ reconAssessment: latestAssessment.raw,
2069
+ reconAssessmentRequest,
2070
+ executed
2071
+ }
2072
+ );
2073
+ } else {
2074
+ updateState(config.statePath, (currentState) => {
2075
+ currentState.recon_status = "";
2076
+ currentState.recon_assessment = {};
2077
+ currentState.recon_assessment_source = null;
2078
+ currentState.recon_assessment_request = {};
2079
+ currentState.recon_decision_request = {};
2080
+ currentState.before_cdn = "";
2081
+ currentState.prod_cdn = "";
2082
+ currentState.recon_results = currentState.recon_results || {};
2083
+ currentState.recon_results.baselines = {};
2084
+ currentState.recon_results.selected_attempt = {};
2085
+ currentState.recon_results.status = "retry_requested";
2086
+ });
2087
+ state = readState(config.statePath);
2088
+ }
2089
+ }
2090
+ if (!state?.recon_results || state?.stage === "setup" || state?.stage === "preflight" || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "") || requestedStage === "recon") {
2091
+ const reconRes = runOne("recon");
2092
+ executed.push(executedStep(reconRes));
2093
+ if (!reconRes.ok || reconRes.haltedForApproval) {
2094
+ return failedRun("recon", reconRes.haltedForApproval ? "recon halted for approval" : "recon failed", reconRes, {
2095
+ checkpoint: "recon_failed",
2096
+ details: { executed },
2097
+ executed
2098
+ });
2099
+ }
2100
+ state = readState(config.statePath);
2101
+ if (["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) {
2102
+ const reconAssessmentRequest = state?.recon_assessment_request || state?.recon_decision_request || null;
2103
+ 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.";
2104
+ const reconDetails = {
2105
+ executed,
2106
+ latestAttempt: latestReconAttempt(state),
2107
+ latestCapturedBaselines: latestReconCapturedBaselines(state),
2108
+ reconAssessmentRequest
2109
+ };
2110
+ recordAttempt("recon", "checkpoint", summary, {
2111
+ autoApproved: reconRes.autoApproved || false,
2112
+ checkpoint: "recon_supervisor_judgment",
2113
+ details: reconDetails
2114
+ });
2115
+ return checkpoint(
2116
+ "recon",
2117
+ "recon_supervisor_judgment",
2118
+ summary,
2119
+ {
2120
+ nextActions: ["inspect_recon_packet", "supply_recon_assessment_json", "continue_internal_loop_with_checkpoint"],
2121
+ advanceOptions: ["recon", "author"],
2122
+ recommendedAdvanceStage: "recon",
2123
+ continueWithStage: "recon",
2124
+ blocking: false,
2125
+ details: reconDetails,
2126
+ reconAssessmentRequest,
2127
+ reconDecisionRequest: state?.recon_decision_request || null,
2128
+ executed
2129
+ }
2130
+ );
2131
+ }
2132
+ recordAttempt("recon", "completed", "Recon completed and promoted an approved baseline context.", {
2133
+ autoApproved: reconRes.autoApproved || false,
2134
+ details: { executed }
2135
+ });
2136
+ }
2137
+ state = readState(config.statePath);
2138
+ if (!authorReady(state) || effectiveAdvanceStage === "author") {
2139
+ const authorRes = runOne("author");
2140
+ executed.push(executedStep(authorRes));
2141
+ if (!authorRes.ok || authorRes.haltedForApproval) {
2142
+ return failedRun("author", authorRes.haltedForApproval ? "author halted for approval" : "author failed", authorRes, {
2143
+ checkpoint: "author_failed",
2144
+ details: { executed },
2145
+ executed
2146
+ });
2147
+ }
2148
+ state = readState(config.statePath);
2149
+ if (!authorReady(state)) {
2150
+ recordAttempt("author", "checkpoint", "Author prepared a supervisor judgment request instead of delegating proof authoring to an internal model.", {
2151
+ autoApproved: authorRes.autoApproved || false,
2152
+ checkpoint: "author_supervisor_judgment",
2153
+ details: {
2154
+ executed,
2155
+ authorSummary: state?.author_summary || null,
2156
+ authorRequest: state?.author_request || null,
2157
+ serverPath: state?.server_path || null,
2158
+ waitForSelector: state?.wait_for_selector || null
2159
+ }
2160
+ });
2161
+ return checkpoint(
2162
+ "author",
2163
+ "author_supervisor_judgment",
2164
+ "Author distilled recon into a proof-authoring request. The supervising agent should supply the proof packet, then resume the workflow.",
2165
+ {
2166
+ nextActions: ["inspect_author_request", "supply_author_packet_json_or_proof_plan", "continue_internal_loop_with_checkpoint"],
2167
+ advanceOptions: ["author", "recon", "implement", "verify"],
2168
+ recommendedAdvanceStage: "author",
2169
+ continueWithStage: "author",
2170
+ details: {
2171
+ executed,
2172
+ authorSummary: state?.author_summary || null,
2173
+ authorRequest: state?.author_request || null,
2174
+ proofPlanDraft: state?.author_request?.fallback_defaults?.proof_plan || null,
2175
+ captureScriptDraft: state?.author_request?.fallback_defaults?.capture_script || null,
2176
+ serverPathDraft: state?.author_request?.fallback_defaults?.server_path || null,
2177
+ waitForSelectorDraft: state?.author_request?.fallback_defaults?.wait_for_selector || null
2178
+ },
2179
+ authorSummary: state?.author_summary || null,
2180
+ authorRequest: state?.author_request || null,
2181
+ proofPlanDraft: state?.author_request?.fallback_defaults?.proof_plan || null,
2182
+ captureScriptDraft: state?.author_request?.fallback_defaults?.capture_script || null,
2183
+ serverPathDraft: state?.author_request?.fallback_defaults?.server_path || null,
2184
+ waitForSelectorDraft: state?.author_request?.fallback_defaults?.wait_for_selector || null,
2185
+ executed
2186
+ }
2187
+ );
2188
+ }
2189
+ const noImplementationMode = !implementationRequired(params, state);
2190
+ const authorNextStage = stageAfterAuthor(state, params);
2191
+ const explicitAuthorDebug = params.advance_stage === "author";
2192
+ recordAttempt("author", "completed", "Author applied the supervising agent's proof packet to recon observations.", {
2193
+ autoApproved: authorRes.autoApproved || false,
2194
+ checkpoint: explicitAuthorDebug ? "author_review" : "author_auto_continue",
2195
+ details: {
2196
+ executed,
2197
+ authorSummary: state?.author_summary || null,
2198
+ authorModel: state?.author_model || null,
2199
+ authorRuntimeModelHint: state?.author_runtime_model_hint || null,
2200
+ serverPath: state?.server_path || null,
2201
+ waitForSelector: state?.wait_for_selector || null
2202
+ }
2203
+ });
2204
+ if (explicitAuthorDebug) {
2205
+ return checkpoint(
2206
+ "author",
2207
+ "author_review",
2208
+ 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.",
2209
+ {
2210
+ nextActions: authorNextStage === "verify" ? ["inspect_proof_packet", "advance_run_to_verify", "rerun_author"] : ["inspect_proof_packet", "advance_run_to_implement", "rerun_author"],
2211
+ advanceOptions: authorNextStage === "verify" ? ["author", "verify", "recon"] : ["author", "implement", "recon"],
2212
+ recommendedAdvanceStage: authorNextStage,
2213
+ continueWithStage: authorNextStage,
2214
+ details: {
2215
+ executed,
2216
+ authorSummary: state?.author_summary || null,
2217
+ authorModel: state?.author_model || null,
2218
+ authorRuntimeModelHint: state?.author_runtime_model_hint || null,
2219
+ proofPlan: state?.proof_plan || null,
2220
+ serverPath: state?.server_path || null,
2221
+ waitForSelector: state?.wait_for_selector || null
2222
+ },
2223
+ authorSummary: state?.author_summary || null,
2224
+ authorModel: state?.author_model || null,
2225
+ authorRuntimeModelHint: state?.author_runtime_model_hint || null,
2226
+ proofPlan: state?.proof_plan || null,
2227
+ serverPath: state?.server_path || null,
2228
+ waitForSelector: state?.wait_for_selector || null,
2229
+ executed
2230
+ }
2231
+ );
2232
+ }
2233
+ effectiveAdvanceStage = authorNextStage;
2234
+ updateState(config.statePath, (currentState) => {
2235
+ currentState.last_requested_advance_stage = authorNextStage;
2236
+ });
2237
+ state = readState(config.statePath);
2238
+ }
2239
+ if (!effectiveAdvanceStage) {
2240
+ const recommended = recommendedAdvanceStage(state);
2241
+ const noImplementationMode = !implementationRequired(params, state);
2242
+ return checkpoint(
2243
+ recommended || (noImplementationMode ? "verify" : "implement"),
2244
+ "awaiting_stage_advance",
2245
+ "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.",
2246
+ {
2247
+ nextActions: ["inspect_state", "set_advance_stage", "resume_run"],
2248
+ advanceOptions: noImplementationMode ? ["recon", "author", "verify", "ship"] : ["recon", "author", "implement", "verify", "ship"],
2249
+ recommendedAdvanceStage: recommended,
2250
+ details: { executed },
2251
+ executed
2252
+ }
2253
+ );
2254
+ }
2255
+ if (effectiveAdvanceStage === "implement") {
2256
+ if (!implementationRequired(params, state)) {
2257
+ recordAttempt("implement", "checkpoint", "Implementation stage was skipped because audit/no-diff mode disables code changes.", {
2258
+ checkpoint: "implement_disabled_for_audit",
2259
+ details: { executed }
2260
+ });
2261
+ return checkpoint(
2262
+ "verify",
2263
+ "implement_disabled_for_audit",
2264
+ "Audit/no-diff mode disables implementation. Continue to verify against the existing target; do not launch an implementation agent or require a git diff.",
2265
+ {
2266
+ nextActions: ["advance_run_to_verify", "inspect_author_packet", "rerun_recon_if_target_changed"],
2267
+ advanceOptions: ["verify", "author", "recon"],
2268
+ recommendedAdvanceStage: "verify",
2269
+ continueWithStage: "verify",
2270
+ blocking: false,
2271
+ details: { executed },
2272
+ executed
2273
+ }
2274
+ );
2275
+ }
2276
+ const implementRes = runOne("implement");
2277
+ executed.push(executedStep(implementRes));
2278
+ if (implementRes.haltedForApproval) {
2279
+ return failedRun("implement", "implement halted for approval", implementRes, {
2280
+ checkpoint: "implement_blocked",
2281
+ details: { executed },
2282
+ executed
2283
+ });
2284
+ }
2285
+ if (!implementRes.ok) {
2286
+ const implementError = `${implementRes.error || ""}
2287
+ ${implementRes.stdout || ""}
2288
+ ${implementRes.stderr || ""}`;
2289
+ if (implementError.includes("No implementation detected")) {
2290
+ const implementationState = readState(config.statePath) || {};
2291
+ const implementationSummary = stringValue(implementationState?.implementation_summary) || null;
2292
+ const implementationDetectionSummary = stringValue(implementationState?.implementation_detection_summary) || null;
2293
+ const implementationDetection = implementationState?.implementation_detection && typeof implementationState.implementation_detection === "object" && !Array.isArray(implementationState.implementation_detection) ? implementationState.implementation_detection : null;
2294
+ recordAttempt("implement", "checkpoint", "Implementation checkpoint found no material code changes yet.", {
2295
+ checkpoint: "implement_changes_missing",
2296
+ error: implementRes.error || null,
2297
+ retryable: true,
2298
+ checkpointDisposition: "retryable_implementation_gap",
2299
+ details: {
2300
+ executed,
2301
+ implementationSummary,
2302
+ implementationDetectionSummary,
2303
+ implementationDetection
2304
+ }
2305
+ });
2306
+ return checkpoint(
2307
+ "implement",
2308
+ "implement_changes_missing",
2309
+ "Proof plan is ready, but code changes are not recorded yet. Make the implementation changes on the after worktree, then resume run.",
2310
+ {
2311
+ nextActions: ["make_code_changes", "rerun_implement"],
2312
+ advanceOptions: ["implement", "author", "recon"],
2313
+ recommendedAdvanceStage: "implement",
2314
+ blocking: true,
2315
+ retryable: true,
2316
+ checkpointDisposition: "retryable_implementation_gap",
2317
+ details: {
2318
+ executed,
2319
+ implementationSummary,
2320
+ implementationDetectionSummary,
2321
+ implementationDetection
2322
+ },
2323
+ implementationSummary,
2324
+ implementationDetectionSummary,
2325
+ implementationDetection,
2326
+ executed
2327
+ }
2328
+ );
2329
+ }
2330
+ return failedRun("implement", "implement failed", implementRes, {
2331
+ checkpoint: "implement_failed",
2332
+ details: { executed },
2333
+ executed
2334
+ });
2335
+ }
2336
+ let invalidatedVerifyEvidence2 = false;
2337
+ updateState(config.statePath, (state2) => {
2338
+ invalidatedVerifyEvidence2 = invalidateVerifyEvidence(state2).invalidated;
2339
+ });
2340
+ recordAttempt("implement", "completed", "Implementation checkpoint recorded code changes on the after worktree.", {
2341
+ autoApproved: implementRes.autoApproved || false,
2342
+ checkpoint: "implement_review",
2343
+ details: { executed, invalidatedVerifyEvidence: invalidatedVerifyEvidence2 }
2344
+ });
2345
+ return checkpoint(
2346
+ "implement",
2347
+ "implement_review",
2348
+ 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.",
2349
+ {
2350
+ nextActions: ["inspect_branch_diff", "rerun_implement", "advance_run_to_verify"],
2351
+ advanceOptions: ["implement", "author", "verify", "recon"],
2352
+ recommendedAdvanceStage: "verify",
2353
+ details: {
2354
+ executed,
2355
+ implementationSummary: readState(config.statePath)?.implementation_summary || null,
2356
+ invalidatedVerifyEvidence: invalidatedVerifyEvidence2
2357
+ },
2358
+ implementationSummary: readState(config.statePath)?.implementation_summary || null,
2359
+ invalidatedVerifyEvidence: invalidatedVerifyEvidence2,
2360
+ executed
2361
+ }
2362
+ );
2363
+ }
2364
+ if (effectiveAdvanceStage === "verify") {
2365
+ state = readState(config.statePath);
2366
+ const needsImplementation = implementationRequired(params, state);
2367
+ if (needsImplementation && !implementationReady(state)) {
2368
+ return checkpoint(
2369
+ "implement",
2370
+ "implement_required",
2371
+ "Verify is blocked until implementation has been recorded. Run the implement stage after making code changes, then resume verify.",
2372
+ {
2373
+ ok: false,
2374
+ nextActions: ["make_code_changes", "advance_run_to_implement"],
2375
+ advanceOptions: ["implement", "author", "recon"],
2376
+ recommendedAdvanceStage: "implement",
2377
+ continueWithStage: "implement",
2378
+ blocking: true,
2379
+ details: { executed },
2380
+ executed
2381
+ }
2382
+ );
2383
+ }
2384
+ if (!needsImplementation && !implementationReady(state)) {
2385
+ recordAttempt("implement", "completed", "Implementation stage is not required for this audit/no-diff run.", {
2386
+ checkpoint: "implementation_not_required",
2387
+ details: { executed }
2388
+ });
2389
+ state = updateState(config.statePath, (currentState) => {
2390
+ currentState.implementation_status = "not_required";
2391
+ currentState.implementation_mode = currentState.implementation_mode || "none";
2392
+ if (currentState.require_diff === void 0) currentState.require_diff = false;
2393
+ if (currentState.allow_code_changes === void 0) currentState.allow_code_changes = false;
2394
+ });
2395
+ }
2396
+ const hasIncomingProofAssessment = typeof params.proof_assessment_json === "string" && params.proof_assessment_json.trim().length > 0;
2397
+ 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));
2398
+ let verifyRes = { ok: true, step: "verify", reusedEvidence: canReuseVerifyEvidence };
2399
+ if (!canReuseVerifyEvidence) {
2400
+ verifyRes = runOne("verify");
2401
+ executed.push(executedStep(verifyRes));
2402
+ if (!verifyRes.ok || verifyRes.haltedForApproval) {
2403
+ return failedRun("verify", verifyRes.haltedForApproval ? "verify halted for approval" : "verify failed", verifyRes, {
2404
+ checkpoint: "verify_failed",
2405
+ details: { executed },
2406
+ executed
2407
+ });
2408
+ }
2409
+ } else {
2410
+ executed.push(executedStep(verifyRes, { reusedEvidence: true }));
2411
+ }
2412
+ state = readState(config.statePath);
2413
+ const verifyStatus = state?.verify_status || ((state?.after_cdn || "").trim() ? "evidence_captured" : "capture_incomplete");
2414
+ const verifyDecisionRequest = state?.verify_decision_request || null;
2415
+ const verifySummary = state?.verify_summary || state?.proof_summary || null;
2416
+ const proofAssessment = verifyAssessment(state);
2417
+ const convergenceSignals = nonConvergenceSignals(state, proofAssessment);
2418
+ const rawVerifyRecommendedStage = proofAssessment.recommendedStage || null;
2419
+ const verifyRecommendedStage = !needsImplementation && rawVerifyRecommendedStage === "implement" ? "verify" : rawVerifyRecommendedStage;
2420
+ const rawVerifyContinueWithStage = shouldEscalateVerifyToHuman(state, proofAssessment) ? null : proofAssessment.continueWithStage || verifyRecommendedStage || null;
2421
+ const verifyContinueWithStage = !needsImplementation && rawVerifyContinueWithStage === "implement" ? "verify" : rawVerifyContinueWithStage;
2422
+ const verifyLoopAdvanceOptions = needsImplementation ? ["author", "verify", "implement", "recon"] : ["author", "verify", "recon"];
2423
+ const verifyReviewAdvanceOptions = needsImplementation ? ["verify", "author", "implement", "recon", "ship"] : ["verify", "author", "recon"];
2424
+ const verifyRetryAdvanceOptions = needsImplementation ? ["author", "implement", "ship", "verify", "recon"] : ["author", "verify", "recon"];
2425
+ const verifyDetails = {
2426
+ executed,
2427
+ verifyStatus,
2428
+ verifySummary,
2429
+ afterCdn: state?.after_cdn || null,
2430
+ mergeRecommendation: state?.merge_recommendation || null,
2431
+ verifyDecisionRequest,
2432
+ proofAssessment: proofAssessment.raw,
2433
+ proofAssessmentSource: proofAssessment.source || null,
2434
+ proofAssessmentRequest: state?.proof_assessment_request || null,
2435
+ verifyRecommendedStage,
2436
+ verifyContinueWithStage,
2437
+ convergenceSignals
2438
+ };
2439
+ if (verifyStatus !== "evidence_captured") {
2440
+ if ((verifyContinueWithStage || verifyRecommendedStage || "author") === "author") {
2441
+ updateState(config.statePath, (currentState) => {
2442
+ currentState.author_status = "needs_authoring";
2443
+ currentState.proof_plan_status = "needs_authoring";
2444
+ currentState.supervisor_author_packet = null;
2445
+ });
2446
+ state = readState(config.statePath);
2447
+ }
2448
+ const checkpointName = "verify_capture_retry";
2449
+ const summary = "Verify ran, but the proof packet still needs internal capture-plan work before it should ship.";
2450
+ recordAttempt("verify", "checkpoint", summary, {
2451
+ autoApproved: verifyRes.autoApproved || false,
2452
+ checkpoint: checkpointName,
2453
+ details: verifyDetails
2454
+ });
2455
+ return checkpoint(
2456
+ "verify",
2457
+ checkpointName,
2458
+ summary,
2459
+ {
2460
+ ok: true,
2461
+ nextActions: ["inspect_after_capture", "continue_internal_loop_with_checkpoint", "return_to_recon_if_baseline_is_wrong"],
2462
+ advanceOptions: verifyLoopAdvanceOptions,
2463
+ recommendedAdvanceStage: verifyRecommendedStage || "author",
2464
+ continueWithStage: verifyContinueWithStage || "author",
2465
+ blocking: false,
2466
+ details: verifyDetails,
2467
+ verifyStatus,
2468
+ verifySummary,
2469
+ afterCdn: state?.after_cdn || null,
2470
+ mergeRecommendation: state?.merge_recommendation || null,
2471
+ verifyDecisionRequest,
2472
+ proofAssessment: proofAssessment.raw,
2473
+ executed
2474
+ }
2475
+ );
2476
+ }
2477
+ if (!hasSupervisorProofAssessment(state)) {
2478
+ 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.";
2479
+ recordAttempt("verify", "checkpoint", summary, {
2480
+ autoApproved: verifyRes.autoApproved || false,
2481
+ checkpoint: "verify_supervisor_judgment",
2482
+ details: verifyDetails
2483
+ });
2484
+ return checkpoint(
2485
+ "verify",
2486
+ "verify_supervisor_judgment",
2487
+ summary,
2488
+ {
2489
+ nextActions: ["inspect_evidence", "author_proof_assessment_json", "continue_internal_loop_with_checkpoint"],
2490
+ advanceOptions: verifyReviewAdvanceOptions,
2491
+ recommendedAdvanceStage: "verify",
2492
+ continueWithStage: "verify",
2493
+ blocking: false,
2494
+ details: verifyDetails,
2495
+ verifyStatus,
2496
+ verifySummary,
2497
+ afterCdn: state?.after_cdn || null,
2498
+ mergeRecommendation: state?.merge_recommendation || null,
2499
+ verifyDecisionRequest,
2500
+ proofAssessmentRequest: state?.proof_assessment_request || null,
2501
+ executed
2502
+ }
2503
+ );
2504
+ }
2505
+ const shouldEscalate = shouldEscalateVerifyToHuman(state, proofAssessment);
2506
+ if (shouldEscalate) {
2507
+ const summary = "The supervising agent concluded the workflow hit a real wall and explicitly escalated the proof loop to the human.";
2508
+ recordAttempt("verify", "escalated", summary, {
2509
+ autoApproved: verifyRes.autoApproved || false,
2510
+ checkpoint: "verify_human_escalation",
2511
+ details: verifyDetails
2512
+ });
2513
+ return checkpoint(
2514
+ "verify",
2515
+ "verify_human_escalation",
2516
+ summary,
2517
+ {
2518
+ ok: false,
2519
+ nextActions: ["inspect_retry_history", "summarize_internal_loop", "ask_human_for_direction"],
2520
+ advanceOptions: verifyRetryAdvanceOptions,
2521
+ recommendedAdvanceStage: null,
2522
+ continueWithStage: null,
2523
+ blocking: true,
2524
+ details: verifyDetails,
2525
+ verifyStatus,
2526
+ verifySummary,
2527
+ afterCdn: state?.after_cdn || null,
2528
+ mergeRecommendation: state?.merge_recommendation || null,
2529
+ verifyDecisionRequest,
2530
+ proofAssessment: proofAssessment.raw,
2531
+ executed
2532
+ }
2533
+ );
2534
+ }
2535
+ const shouldAutoShip = verifyContinueWithStage === "ship" && needsImplementation && (params.ship_after_verify || params.continue_from_checkpoint || params.advance_stage !== "verify");
2536
+ if (shouldAutoShip) {
2537
+ const shipGate = validateShipGate(state);
2538
+ if (!shipGate.ok) {
2539
+ recordAttempt("verify", "checkpoint", "Verify cannot continue into ship because the hard ship gate is missing required evidence or approval.", {
2540
+ autoApproved: verifyRes.autoApproved || false,
2541
+ checkpoint: "ship_gate_blocked",
2542
+ details: { ...verifyDetails, shipGate }
2543
+ });
2544
+ return shipGateBlocked(state, executed, verifyDetails);
2545
+ }
2546
+ recordAttempt("verify", "checkpoint", "Verify captured a strong proof packet and is continuing directly into ship.", {
2547
+ autoApproved: verifyRes.autoApproved || false,
2548
+ checkpoint: "verify_then_ship",
2549
+ details: { ...verifyDetails, shipGate }
2550
+ });
2551
+ const shipRes = runOne("ship");
2552
+ executed.push(executedStep(shipRes));
2553
+ if (!shipRes.ok || shipRes.haltedForApproval) {
2554
+ 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";
2555
+ return failedRun("ship", shipRes.haltedForApproval ? "ship halted for approval" : "ship failed", shipRes, {
2556
+ checkpoint: "ship_failed",
2557
+ details: { executed, next_action: shipNextAction },
2558
+ nextAction: shipNextAction,
2559
+ executed
2560
+ });
2561
+ }
2562
+ recordAttempt("ship", "completed", "Ship updated the PR and posted proof artifacts after the supervising agent judged the proof strong enough.", {
2563
+ autoApproved: shipRes.autoApproved || false,
2564
+ checkpoint: "ship_review",
2565
+ details: { executed }
2566
+ });
2567
+ const snapshot2 = snapshotFor(config.statePath);
2568
+ 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.";
2569
+ const finalState = readState(config.statePath);
2570
+ const shipReport = finalState?.ship_report || snapshot2.state?.ship_report || null;
2571
+ return {
2572
+ ok: true,
2573
+ action,
2574
+ state_path: config.statePath,
2575
+ stage: snapshot2.stage,
2576
+ checkpoint: "ship_review",
2577
+ summary,
2578
+ state: snapshot2.state,
2579
+ shipReport,
2580
+ checkpointContract: buildCheckpointContract(readState(config.statePath), {
2581
+ statePath: config.statePath,
2582
+ stage: "ship",
2583
+ checkpoint: "ship_review",
2584
+ summary,
2585
+ nextActions: ["inspect_pr", "rerun_ship_if_needed"],
2586
+ advanceOptions: ["ship", "verify", "author", "implement"],
2587
+ recommendedAdvanceStage: "ship"
2588
+ }),
2589
+ executed
2590
+ };
2591
+ }
2592
+ if (proofAssessment.decision === "ready_to_ship") {
2593
+ if (!needsImplementation) {
2594
+ recordAttempt("verify", "completed", "Verify captured a proof packet for audit/no-diff mode; shipping remains disabled.", {
2595
+ autoApproved: verifyRes.autoApproved || false,
2596
+ checkpoint: "verify_audit_complete",
2597
+ details: verifyDetails
2598
+ });
2599
+ return checkpoint(
2600
+ "verify",
2601
+ "verify_audit_complete",
2602
+ "The supervising agent judged the audit proof sufficient. Audit/no-diff mode disables ship, PR creation, implementation agents, and git-diff requirements.",
2603
+ {
2604
+ nextActions: ["inspect_evidence", "report_audit_result", "rerun_verify_if_needed"],
2605
+ advanceOptions: ["verify", "author", "recon"],
2606
+ recommendedAdvanceStage: "verify",
2607
+ continueWithStage: null,
2608
+ blocking: false,
2609
+ details: verifyDetails,
2610
+ verifyStatus,
2611
+ verifySummary,
2612
+ afterCdn: state?.after_cdn || null,
2613
+ mergeRecommendation: state?.merge_recommendation || null,
2614
+ verifyDecisionRequest,
2615
+ proofAssessment: proofAssessment.raw,
2616
+ executed
2617
+ }
2618
+ );
2619
+ }
2620
+ const shipGate = validateShipGate(state);
2621
+ if (!shipGate.ok) {
2622
+ recordAttempt("verify", "checkpoint", "Verify cannot mark ship ready because the hard ship gate is missing required evidence or approval.", {
2623
+ autoApproved: verifyRes.autoApproved || false,
2624
+ checkpoint: "ship_gate_blocked",
2625
+ details: { ...verifyDetails, shipGate }
2626
+ });
2627
+ return shipGateBlocked(state, executed, verifyDetails);
2628
+ }
2629
+ recordAttempt("verify", "checkpoint", "Verify captured a strong proof packet and is ready to continue into ship.", {
2630
+ autoApproved: verifyRes.autoApproved || false,
2631
+ checkpoint: "verify_ship_ready",
2632
+ details: { ...verifyDetails, shipGate }
2633
+ });
2634
+ return checkpoint(
2635
+ "verify",
2636
+ "verify_ship_ready",
2637
+ "The supervising agent judged the proof strong enough to continue into ship.",
2638
+ {
2639
+ nextActions: ["inspect_evidence", "continue_internal_loop_with_checkpoint", "advance_run_to_ship_if_you_need_manual_control"],
2640
+ advanceOptions: ["ship", "verify", "author", "implement", "recon"],
2641
+ recommendedAdvanceStage: "ship",
2642
+ continueWithStage: "ship",
2643
+ blocking: false,
2644
+ details: { ...verifyDetails, shipGate },
2645
+ shipGate,
2646
+ verifyStatus,
2647
+ verifySummary,
2648
+ afterCdn: state?.after_cdn || null,
2649
+ mergeRecommendation: state?.merge_recommendation || null,
2650
+ verifyDecisionRequest,
2651
+ proofAssessment: proofAssessment.raw,
2652
+ executed
2653
+ }
2654
+ );
2655
+ }
2656
+ if (verifyContinueWithStage === "author") {
2657
+ updateState(config.statePath, (currentState) => {
2658
+ currentState.author_status = "needs_authoring";
2659
+ currentState.proof_plan_status = "needs_authoring";
2660
+ currentState.supervisor_author_packet = null;
2661
+ });
2662
+ state = readState(config.statePath);
2663
+ }
2664
+ 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.";
2665
+ recordAttempt("verify", "checkpoint", unresolvedSummary, {
2666
+ autoApproved: verifyRes.autoApproved || false,
2667
+ checkpoint: "verify_agent_retry",
2668
+ details: verifyDetails
2669
+ });
2670
+ return checkpoint(
2671
+ "verify",
2672
+ "verify_agent_retry",
2673
+ unresolvedSummary,
2674
+ {
2675
+ ok: true,
2676
+ 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"],
2677
+ advanceOptions: verifyRetryAdvanceOptions,
2678
+ recommendedAdvanceStage: verifyRecommendedStage,
2679
+ continueWithStage: verifyContinueWithStage,
2680
+ blocking: false,
2681
+ details: verifyDetails,
2682
+ verifyStatus,
2683
+ verifySummary,
2684
+ afterCdn: state?.after_cdn || null,
2685
+ mergeRecommendation: state?.merge_recommendation || null,
2686
+ verifyDecisionRequest,
2687
+ proofAssessment: proofAssessment.raw,
2688
+ executed
2689
+ }
2690
+ );
2691
+ }
2692
+ if (effectiveAdvanceStage === "ship") {
2693
+ state = readState(config.statePath);
2694
+ if (!implementationRequired(params, state)) {
2695
+ return checkpoint(
2696
+ "verify",
2697
+ "ship_disabled_for_audit",
2698
+ "Audit/no-diff mode disables ship and PR creation. Report the audit result from verify evidence instead.",
2699
+ {
2700
+ ok: false,
2701
+ nextActions: ["inspect_verify_state", "report_audit_result"],
2702
+ advanceOptions: ["verify", "author", "recon"],
2703
+ recommendedAdvanceStage: "verify",
2704
+ continueWithStage: "verify",
2705
+ blocking: true,
2706
+ details: { executed },
2707
+ executed
2708
+ }
2709
+ );
2710
+ }
2711
+ const shipAssessment = verifyAssessment(state);
2712
+ const shipGate = validateShipGate(state);
2713
+ if (state?.verify_status !== "evidence_captured") {
2714
+ return checkpoint(
2715
+ "verify",
2716
+ "verify_required",
2717
+ "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.",
2718
+ {
2719
+ ok: false,
2720
+ nextActions: ["advance_run_to_verify", "inspect_verify_state"],
2721
+ advanceOptions: ["verify", "author", "implement", "recon"],
2722
+ recommendedAdvanceStage: "verify",
2723
+ continueWithStage: "verify",
2724
+ blocking: true,
2725
+ details: {
2726
+ executed,
2727
+ shipGate,
2728
+ verifyStatus: state?.verify_status || null,
2729
+ mergeRecommendation: state?.merge_recommendation || null,
2730
+ afterCdn: state?.after_cdn || null
2731
+ },
2732
+ shipGate,
2733
+ verifyStatus: state?.verify_status || null,
2734
+ mergeRecommendation: state?.merge_recommendation || null,
2735
+ afterCdn: state?.after_cdn || null,
2736
+ executed
2737
+ }
2738
+ );
2739
+ }
2740
+ if (!hasSupervisorProofAssessment(state) || shipAssessment.decision !== "ready_to_ship") {
2741
+ return checkpoint(
2742
+ "verify",
2743
+ "verify_supervisor_judgment_required",
2744
+ "Ship is blocked until the supervising agent judges the current proof packet as ready_to_ship.",
2745
+ {
2746
+ ok: false,
2747
+ nextActions: ["inspect_evidence", "supply_proof_assessment_json", "continue_internal_loop_with_checkpoint"],
2748
+ advanceOptions: ["verify", "author", "implement", "recon", "ship"],
2749
+ recommendedAdvanceStage: "verify",
2750
+ continueWithStage: "verify",
2751
+ blocking: true,
2752
+ details: {
2753
+ executed,
2754
+ shipGate,
2755
+ verifyStatus: state?.verify_status || null,
2756
+ mergeRecommendation: state?.merge_recommendation || null,
2757
+ afterCdn: state?.after_cdn || null,
2758
+ proofAssessment: state?.proof_assessment || null,
2759
+ proofAssessmentRequest: state?.proof_assessment_request || null
2760
+ },
2761
+ shipGate,
2762
+ verifyStatus: state?.verify_status || null,
2763
+ mergeRecommendation: state?.merge_recommendation || null,
2764
+ afterCdn: state?.after_cdn || null,
2765
+ proofAssessment: state?.proof_assessment || null,
2766
+ proofAssessmentRequest: state?.proof_assessment_request || null,
2767
+ executed
2768
+ }
2769
+ );
2770
+ }
2771
+ if (!shipGate.ok) {
2772
+ return shipGateBlocked(state, executed, { shipAssessment: shipAssessment.raw });
2773
+ }
2774
+ const shipRes = runOne("ship");
2775
+ executed.push(executedStep(shipRes));
2776
+ if (!shipRes.ok || shipRes.haltedForApproval) {
2777
+ return failedRun("ship", shipRes.haltedForApproval ? "ship halted for approval" : "ship failed", shipRes, {
2778
+ checkpoint: "ship_failed",
2779
+ details: { executed },
2780
+ executed
2781
+ });
2782
+ }
2783
+ recordAttempt("ship", "completed", "Ship updated the PR and posted proof artifacts.", {
2784
+ autoApproved: shipRes.autoApproved || false,
2785
+ checkpoint: "ship_review",
2786
+ details: { executed }
2787
+ });
2788
+ return checkpoint(
2789
+ "ship",
2790
+ "ship_review",
2791
+ "Ship completed. Review the PR, proof comment, and cleanup results. Re-run ship if you need to refresh the PR after more changes.",
2792
+ {
2793
+ nextActions: ["inspect_pr", "rerun_ship_if_needed"],
2794
+ advanceOptions: ["ship", "verify", "author", "implement"],
2795
+ recommendedAdvanceStage: "ship",
2796
+ details: {
2797
+ executed,
2798
+ prUrl: readState(config.statePath)?.pr_url || null
2799
+ },
2800
+ prUrl: readState(config.statePath)?.pr_url || null,
2801
+ executed
2802
+ }
2803
+ );
2804
+ }
2805
+ }
2806
+ if (action === "ship") {
2807
+ const state = readState(config.statePath);
2808
+ const shipGate = validateShipGate(state);
2809
+ if (state?.verify_status !== "evidence_captured") {
2810
+ return checkpoint(
2811
+ "verify",
2812
+ "verify_required",
2813
+ "Ship is blocked until verify has captured a usable proof packet. Run verify, inspect the evidence, then ship only if the proof supports success.",
2814
+ {
2815
+ ok: false,
2816
+ nextActions: ["run_verify", "inspect_verify_state"],
2817
+ advanceOptions: ["verify", "author", "implement", "recon"],
2818
+ recommendedAdvanceStage: "verify",
2819
+ continueWithStage: "verify",
2820
+ blocking: true,
2821
+ details: { shipGate },
2822
+ shipGate,
2823
+ verifyStatus: state?.verify_status || null,
2824
+ mergeRecommendation: state?.merge_recommendation || null,
2825
+ afterCdn: state?.after_cdn || null
2826
+ }
2827
+ );
2828
+ }
2829
+ if (!hasSupervisorProofAssessment(state) || verifyAssessment(state).decision !== "ready_to_ship") {
2830
+ return checkpoint(
2831
+ "verify",
2832
+ "verify_supervisor_judgment_required",
2833
+ "Ship is blocked until the supervising agent judges the current proof packet as ready_to_ship.",
2834
+ {
2835
+ ok: false,
2836
+ nextActions: ["inspect_evidence", "supply_proof_assessment_json", "rerun_ship"],
2837
+ advanceOptions: ["verify", "author", "implement", "recon", "ship"],
2838
+ recommendedAdvanceStage: "verify",
2839
+ continueWithStage: "verify",
2840
+ blocking: true,
2841
+ details: { shipGate },
2842
+ shipGate,
2843
+ verifyStatus: state?.verify_status || null,
2844
+ mergeRecommendation: state?.merge_recommendation || null,
2845
+ afterCdn: state?.after_cdn || null,
2846
+ proofAssessment: state?.proof_assessment || null,
2847
+ proofAssessmentRequest: state?.proof_assessment_request || null
2848
+ }
2849
+ );
2850
+ }
2851
+ if (!shipGate.ok) {
2852
+ return shipGateBlocked(state, [], {});
2853
+ }
2854
+ }
2855
+ const single = runOne(action);
2856
+ if (!single.ok || single.haltedForApproval) {
2857
+ return failedRun(action, single.haltedForApproval ? `${action} halted for approval` : `${action} failed`, single, {
2858
+ checkpoint: `${action}_failed`
2859
+ });
2860
+ }
2861
+ let invalidatedVerifyEvidence = false;
2862
+ updateState(config.statePath, (state) => {
2863
+ if (action === "implement") {
2864
+ invalidatedVerifyEvidence = invalidateVerifyEvidence(state).invalidated;
2865
+ }
2866
+ clearStageDecisionRequest(state);
2867
+ });
2868
+ const singleSummary = action === "implement" && invalidatedVerifyEvidence ? "implement completed and invalidated prior verify evidence" : `${action} completed`;
2869
+ recordAttempt(action, "completed", singleSummary, {
2870
+ autoApproved: single.autoApproved || false,
2871
+ details: action === "implement" ? { invalidatedVerifyEvidence } : {}
2872
+ });
2873
+ const snapshot = snapshotFor(config.statePath);
2874
+ return {
2875
+ ok: true,
2876
+ action,
2877
+ state_path: config.statePath,
2878
+ stage: snapshot.stage,
2879
+ summary: singleSummary,
2880
+ state: snapshot.state,
2881
+ approval: null,
2882
+ autoApproved: single.autoApproved || false,
2883
+ error: null
2884
+ };
2885
+ }
2886
+ function createRiddleProofEngine(pluginConfig = {}) {
2887
+ return {
2888
+ execute(params) {
2889
+ const config = resolveConfig(pluginConfig, params);
2890
+ return executeWorkflow(params, pluginConfig, config);
2891
+ },
2892
+ status(statePath) {
2893
+ const config = resolveConfig(pluginConfig, { action: "status", state_path: statePath });
2894
+ return executeWorkflow({ action: "status", state_path: statePath }, pluginConfig, config);
2895
+ },
2896
+ resolveConfig(params = {}) {
2897
+ return resolveConfig(pluginConfig, params);
2898
+ }
2899
+ };
2900
+ }
2901
+ // Annotate the CommonJS export names for ESM import in node:
2902
+ 0 && (module.exports = {
2903
+ createRiddleProofEngine,
2904
+ executeWorkflow
2905
+ });