@igorjpimenta/manifold 0.0.0-bootstrap.1 → 0.0.0-stage

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 (210) hide show
  1. package/README.md +2 -1583
  2. package/package.json +4 -49
  3. package/.migrations/ops/0000_futuristic_karma.sql +0 -111
  4. package/.migrations/ops/0001_low_maximus.sql +0 -7
  5. package/.migrations/ops/meta/0000_snapshot.json +0 -763
  6. package/.migrations/ops/meta/0001_snapshot.json +0 -173
  7. package/.migrations/ops/meta/_journal.json +0 -20
  8. package/.migrations/runtime/0000_real_boomer.sql +0 -180
  9. package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
  10. package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
  11. package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
  12. package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
  13. package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
  14. package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
  15. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
  16. package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
  17. package/.migrations/runtime/0009_lying_shocker.sql +0 -96
  18. package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
  19. package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
  20. package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
  21. package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
  22. package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
  23. package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
  24. package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
  25. package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
  26. package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
  27. package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
  28. package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
  29. package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
  30. package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
  31. package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
  32. package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
  33. package/.migrations/runtime/meta/_journal.json +0 -97
  34. package/RELEASING.md +0 -89
  35. package/agent/fleet.js +0 -100
  36. package/bin/cli.js +0 -854
  37. package/bin/manifold.js +0 -4
  38. package/boot/flow-revival.js +0 -536
  39. package/budget/index.js +0 -495
  40. package/budget/lib.js +0 -59
  41. package/dashboard/cost.js +0 -528
  42. package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
  43. package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
  44. package/dashboard/dist/index.html +0 -13
  45. package/dashboard/event-log.js +0 -267
  46. package/dashboard/index.html +0 -12
  47. package/dashboard/package-lock.json +0 -2877
  48. package/dashboard/package.json +0 -37
  49. package/dashboard/pipeline-graph.js +0 -131
  50. package/dashboard/postcss.config.js +0 -6
  51. package/dashboard/search.js +0 -183
  52. package/dashboard/server.js +0 -1015
  53. package/dashboard/src/App.tsx +0 -42
  54. package/dashboard/src/api/client.ts +0 -118
  55. package/dashboard/src/api/types.ts +0 -510
  56. package/dashboard/src/api/ws.tsx +0 -112
  57. package/dashboard/src/components/CancelButton.tsx +0 -63
  58. package/dashboard/src/components/ClickToCopy.tsx +0 -68
  59. package/dashboard/src/components/CopyButton.tsx +0 -61
  60. package/dashboard/src/components/DailyCostTrend.tsx +0 -85
  61. package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
  62. package/dashboard/src/components/Funnel.tsx +0 -149
  63. package/dashboard/src/components/GateForm.tsx +0 -158
  64. package/dashboard/src/components/GraphView.tsx +0 -302
  65. package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
  66. package/dashboard/src/components/Histogram.tsx +0 -292
  67. package/dashboard/src/components/Layout.tsx +0 -28
  68. package/dashboard/src/components/NeedsAttention.tsx +0 -98
  69. package/dashboard/src/components/NodeContextCard.tsx +0 -132
  70. package/dashboard/src/components/PathTrail.tsx +0 -35
  71. package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
  72. package/dashboard/src/components/RerunButton.tsx +0 -46
  73. package/dashboard/src/components/RunComparison.tsx +0 -53
  74. package/dashboard/src/components/Sparkline.tsx +0 -94
  75. package/dashboard/src/components/StatStrip.tsx +0 -147
  76. package/dashboard/src/components/StateTree.tsx +0 -84
  77. package/dashboard/src/components/StatusBadge.tsx +0 -18
  78. package/dashboard/src/components/SubflowPathPills.tsx +0 -38
  79. package/dashboard/src/components/SubflowSummary.tsx +0 -24
  80. package/dashboard/src/components/SubflowsCard.tsx +0 -327
  81. package/dashboard/src/components/TodayStrip.tsx +0 -86
  82. package/dashboard/src/components/TranscriptPane.tsx +0 -99
  83. package/dashboard/src/components/TranscriptRow.tsx +0 -69
  84. package/dashboard/src/components/ui/badge.tsx +0 -38
  85. package/dashboard/src/components/ui/button.tsx +0 -28
  86. package/dashboard/src/components/ui/card.tsx +0 -42
  87. package/dashboard/src/components/ui/skeleton.tsx +0 -9
  88. package/dashboard/src/components/ui/table.tsx +0 -62
  89. package/dashboard/src/index.css +0 -26
  90. package/dashboard/src/lib/anomaly.ts +0 -42
  91. package/dashboard/src/lib/format.ts +0 -30
  92. package/dashboard/src/lib/step-history.ts +0 -13
  93. package/dashboard/src/lib/subflow.ts +0 -154
  94. package/dashboard/src/lib/use-click-outside.ts +0 -28
  95. package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
  96. package/dashboard/src/lib/utils.ts +0 -6
  97. package/dashboard/src/main.tsx +0 -10
  98. package/dashboard/src/types/shims.d.ts +0 -4
  99. package/dashboard/src/views/Home.tsx +0 -478
  100. package/dashboard/src/views/PipelineDetail.tsx +0 -1185
  101. package/dashboard/src/views/PipelinesList.tsx +0 -111
  102. package/dashboard/src/views/RunDetail.tsx +0 -248
  103. package/dashboard/src/views/Search.tsx +0 -196
  104. package/dashboard/stats.js +0 -668
  105. package/dashboard/tailwind.config.ts +0 -43
  106. package/dashboard/tree-stats.js +0 -12
  107. package/dashboard/tsconfig.json +0 -25
  108. package/dashboard/views.js +0 -231
  109. package/dashboard/vite.config.ts +0 -34
  110. package/drizzle.config.js +0 -12
  111. package/drizzle.config.ops.js +0 -12
  112. package/elevation.js +0 -116
  113. package/engine/admission-promotion-resume.js +0 -127
  114. package/engine/agent-dispatch.js +0 -64
  115. package/engine/child-resume-trigger.js +0 -315
  116. package/engine/executor-budget.js +0 -87
  117. package/engine/executor-cancel.js +0 -83
  118. package/engine/executor-errors.js +0 -122
  119. package/engine/executor-helpers.js +0 -254
  120. package/engine/executor-nodes.js +0 -292
  121. package/engine/executor-persist.js +0 -212
  122. package/engine/executor-subflow-children.js +0 -275
  123. package/engine/executor.js +0 -1013
  124. package/engine/global-concurrency.js +0 -33
  125. package/engine/loader.js +0 -528
  126. package/engine/needs-resolver.js +0 -229
  127. package/engine/pipelines-watcher.js +0 -133
  128. package/engine/result-replay.js +0 -34
  129. package/engine/revival-messages.js +0 -11
  130. package/engine/spawn-child.js +0 -55
  131. package/engine/wait-for-flow-settlement.js +0 -109
  132. package/events/catalog.js +0 -26
  133. package/events/emit.js +0 -44
  134. package/events/kinds.js +0 -118
  135. package/events/serve-ws.js +0 -74
  136. package/events/stream.js +0 -35
  137. package/events/ws-dialog.js +0 -257
  138. package/events/ws-frame.js +0 -194
  139. package/events/ws-run-events.js +0 -70
  140. package/events.js +0 -32
  141. package/gates/gate-resolver.js +0 -576
  142. package/gates/paused-flows.js +0 -55
  143. package/graphql/columns.js +0 -67
  144. package/graphql/schema.js +0 -333
  145. package/index.js +0 -49
  146. package/mcp/http.js +0 -159
  147. package/mcp/identity.js +0 -94
  148. package/mcp/stdio.js +0 -80
  149. package/mcp/tools.js +0 -245
  150. package/mcp/wire-ops.js +0 -178
  151. package/routes/bearer-auth.js +0 -57
  152. package/routes/discuss-audio.js +0 -87
  153. package/routes/discuss-component.js +0 -39
  154. package/routes/discuss-draft.js +0 -159
  155. package/routes/discuss-helpers.js +0 -20
  156. package/routes/discuss.js +0 -848
  157. package/routes/failures.js +0 -104
  158. package/routes/graphql.js +0 -61
  159. package/routes/health.js +0 -54
  160. package/routes/operator-discuss.js +0 -193
  161. package/routes/operator-resume.js +0 -32
  162. package/routes/operator-retry.js +0 -301
  163. package/routes/operator.js +0 -539
  164. package/routes/pipelines.js +0 -118
  165. package/routes/runs.js +0 -1129
  166. package/scripts/ci/import-check.mjs +0 -50
  167. package/scripts/link-core-self.mjs +0 -31
  168. package/scripts/with-guard.sh +0 -24
  169. package/server/app.js +0 -503
  170. package/server/registry.js +0 -53
  171. package/server/serve.js +0 -163
  172. package/state/admission-queue-store.js +0 -325
  173. package/state/attribution-registries.js +0 -102
  174. package/state/batch-writer.js +0 -162
  175. package/state/db-guard.js +0 -63
  176. package/state/db-health.js +0 -27
  177. package/state/db.js +0 -122
  178. package/state/discuss-store.js +0 -299
  179. package/state/failure-store.js +0 -427
  180. package/state/migrate.js +0 -104
  181. package/state/node-cost.js +0 -67
  182. package/state/node-executions.js +0 -775
  183. package/state/node-health-store.js +0 -124
  184. package/state/ops-merge.js +0 -93
  185. package/state/paths.js +0 -146
  186. package/state/pipeline-dirs.js +0 -99
  187. package/state/pipeline-registry.js +0 -449
  188. package/state/run-store.js +0 -697
  189. package/state/schema.js +0 -438
  190. package/state/schema.ops.js +0 -44
  191. package/state/session-index.js +0 -177
  192. package/state/session-keys.js +0 -39
  193. package/state/state-store.js +0 -311
  194. package/state/subflow-state.js +0 -126
  195. package/state/tool-handoff.js +0 -169
  196. package/telemetry/budget-alert.js +0 -353
  197. package/telemetry/cli.js +0 -331
  198. package/telemetry/grammar.js +0 -119
  199. package/telemetry/http-budget.js +0 -132
  200. package/telemetry/http-cost.js +0 -670
  201. package/telemetry/http-helpers.js +0 -134
  202. package/telemetry/http-tree.js +0 -349
  203. package/telemetry/http.js +0 -44
  204. package/telemetry/index.js +0 -307
  205. package/telemetry/ingest.js +0 -134
  206. package/telemetry/labels.js +0 -115
  207. package/telemetry/pricer.js +0 -77
  208. package/telemetry/resolver.js +0 -314
  209. package/telemetry/store.js +0 -147
  210. package/utils/run-patch.js +0 -43
@@ -1,576 +0,0 @@
1
- import { getNodeExecutionsStore } from "../state/node-executions.js";
2
- import { gateEmitter } from "../events.js";
3
- import { createEmitter } from "../events/emit.js";
4
- import { assembleState } from "../engine/executor-helpers.js";
5
- import { admitAndSetRunning } from "../engine/child-resume-trigger.js";
6
-
7
- // gate-resolver.js — channel-agnostic resolution for gate items.
8
- //
9
- // One write site for every consumer that records a decision on a gate
10
- // item: the dashboard's POST /approve (source="dashboard"), the CLI's
11
- // `manifold approve` (source="cli"), and a harness/app bridge over the
12
- // gateway REST surface (source="app"). All of them call
13
- // `recordItemDecision`, which:
14
- //
15
- // 1. validates the flow (status, kind, item membership)
16
- // 2. idempotently merges {decision, source, decidedAt, ...} into
17
- // state.approvalPending[itemId]
18
- // 3. triggers maybeResolveFlow → applyResolutionsAndResume once all items
19
- // have a decision (or a sendFailed marker). That last step calls
20
- // setRunning + executor.resume so the flow actually advances.
21
- //
22
- // "human-gate" appears in this module only as the wait-kind discriminator the
23
- // executor stamps; nothing here talks to a specific delivery channel. The
24
- // gateway broadcasts manifold.gate.opened/closed over WS with the payload
25
- // below, and each consumer (app bridge, dashboard, CLI) decides how to
26
- // surface it. Channel-specific delivery + cleanup lives in that consumer,
27
- // which calls this factory the same way the dashboard and CLI do.
28
- //
29
- // Per-process design: the factory closes over a store + executor instance,
30
- // so the gateway and the CLI each spin up their own resolver (different
31
- // stores/executors, same logic).
32
- //
33
- // Optional per-item apply hook: a human-gate node can declare
34
- // `apply(ctx, {itemId, decision, editedText, reason})` on itself.
35
- // When `pipelineLookup` is supplied, applyResolutionsAndResume calls it
36
- // once per decided item immediately after that item's decision durably
37
- // commits. No return value is folded back into state, node authors
38
- // persist their own results.
39
- //
40
- // `ctx` (apply's first arg) is the flow-level assembled state, matching
41
- // PRIME_FILL's existing apply(state, item) signature.
42
- //
43
- // Consumers allowed to resolve a gate. Mostly human-driven (app approval,
44
- // dashboard buttons, CLI), plus
45
- // `pipeline` for automated self-resolutions — used by audit / sweep
46
- // nodes inside other pipelines that need to cancel stale gates whose
47
- // underlying state changed out from under the operator (e.g. poll-
48
- // inbox's EVICT_GATES cancelling reply/touch gates after the listing
49
- // got a reply or was won).
50
- const VALID_SOURCES = ["app", "dashboard", "cli", "pipeline"];
51
-
52
- export function createGateResolver({ api, store, executor, getBroadcast, readStore, runStore, pipelineLookup, nodeExecutionsStore, admissionQueueStore }) {
53
- const logger = api.logger;
54
- // Canonical-kind fan-out for this resolver's WS emission (events/emit.js):
55
- // validated envelope + `plugin.`-prefixed wire names, never throws
56
- // on transport faults — the same contract the inline broadcasts had.
57
- const { emit } = createEmitter({ getBroadcast, logger });
58
- // Injectable for tests (defaults to the real singleton in production) —
59
- // same DI pattern as engine/executor.js.
60
- const nodeExecutions = nodeExecutionsStore ?? getNodeExecutionsStore();
61
-
62
- // Revision-checked state_json update for flows that are in the DB but not in
63
- // the in-process Map (post-restart blocked flows). The executor won't resume
64
- // the flow until it's revived (gateway restart), but the decision IS persisted
65
- // in runtime.sqlite and will be picked up on revival.
66
- function writeSqlDirect(flowId, expectedRevision, stateJson) {
67
- if (!runStore) return { applied: false, code: "no_run_store" };
68
- return runStore.updateStateJsonCAS(flowId, expectedRevision, stateJson);
69
- }
70
-
71
- // Durable, operator-visible record of an apply() failure — called from
72
- // the catch block below, in addition to (not instead of) the warn log.
73
- // Best-effort: this already runs on a failure path, so it takes a single
74
- // attempt rather than a full CAS retry loop — if this write itself
75
- // fails, the warn log above is still the fallback trail. Written to
76
- // flow_node_executions so it surfaces through the same errorEntries path
77
- // as every other failure — branch-mode (subNodeId set) keys the row to
78
- // that branch; root-level (subNodeId null) writes a flow-level row.
79
- async function persistApplyFailureMarker({ flowId, nodeId, subNodeId, itemId, message }) {
80
- const errMsg = `apply() failed: ${message}`;
81
- const now = Date.now();
82
- try {
83
- const pipelineId = (await store.get(flowId))?.stateJson?.pipelineId ?? null;
84
- nodeExecutions?.write({
85
- flowId, pipelineId, nodeId: subNodeId ?? nodeId,
86
- branchKey: subNodeId ? itemId : null,
87
- enteredAt: now, exitedAt: now, error: errMsg,
88
- });
89
- } catch (err) {
90
- logger.warn(`manifold [${flowId.slice(0, 8)}] gate-resolver: persistApplyFailureMarker itself failed: ${err?.message ?? err}`);
91
- }
92
- }
93
-
94
- // Revision-aware state update for a blocked/running flow. Re-reads
95
- // the flow up to N times to handle concurrent updates (e.g., poller
96
- // delivering while a callback resolves). Routes by status:
97
- // blocked → setWaiting preserves the existing waitJson and keeps
98
- // status pinned at "blocked" (the canonical resting state for an
99
- // open gate — despite the function's name, it never writes
100
- // "waiting"; see index.js's _wsRunEventsListener for why).
101
- // running → setRunning. Used by the eager-delivery + during-running
102
- // decision path: the executor's loop workers are still
103
- // advancing branches, so we MUST NOT flip status to blocked or we'd
104
- // yank the rug out from under them and race them onto stale state.
105
- // setRunning is a no-op on status itself; it just persists the new
106
- // stateJson (and incidentally re-nulls waitJson, which was already null
107
- // while running anyway).
108
- async function updateApprovalState(flowId, updater) {
109
- for (let attempt = 0; attempt < 5; attempt++) {
110
- const flow = (await store.get(flowId)) ?? (await readStore?.get(flowId));
111
- if (!flow) return { applied: false, code: "flow_not_found" };
112
- const next = updater(flow.stateJson ?? {});
113
- let result;
114
- if (flow.status === "running") {
115
- result = await store.setRunning({
116
- flowId,
117
- expectedRevision: flow.revision,
118
- lastStep: flow.lastStep,
119
- stateJson: next,
120
- });
121
- } else {
122
- result = await store.setWaiting({
123
- flowId,
124
- expectedRevision: flow.revision,
125
- lastStep: flow.lastStep,
126
- stateJson: next,
127
- waitJson: flow.waitJson,
128
- statusReason: flow.statusReason,
129
- });
130
- }
131
- if (result.applied) return result;
132
- // Flow exists in SQL (readStore) but not in the in-process Map —
133
- // store.setWaiting/setRunning return "not_found" in this case. Write
134
- // the decision directly to SQL so it persists for when the flow is
135
- // revived. maybeResolveFlow is a no-op for Map-absent flows (it
136
- // short-circuits on store.get returning null), so the flow stays
137
- // blocked until a restart/revival picks up the persisted decision.
138
- if (result.code === "not_found" && !(await store.get(flowId))) {
139
- logger.info(`manifold gate-resolver: ${flowId.slice(0, 8)} not in Map; writing decision via sql-direct`);
140
- return writeSqlDirect(flowId, flow.revision, next);
141
- }
142
- if (result.code !== "revision_conflict") return result;
143
- // tight backoff before retry
144
- await new Promise((r) => setTimeout(r, 25 + attempt * 25));
145
- }
146
- return { applied: false, code: "retry_exhausted" };
147
- }
148
-
149
- // Resolve EACH decided item as soon as its decision lands — don't wait
150
- // for the whole gate batch. Items with no decision yet stay in the
151
- // waiting set; their branch sits at the gate. Items that have a
152
- // decision (or a sendFailed marker from a TG delivery failure) get
153
- // applied + appended to approvalResults, and the executor re-enters
154
- // the loop node to advance just those branches. Multiple
155
- // partial passes can fire as decisions trickle in.
156
- async function maybeResolveFlow(flowId, awaitCompletion = false) {
157
- const flow = await store.get(flowId);
158
- if (!flow) return;
159
- // Gate pauses are always persisted as "blocked" — "waiting" is never
160
- // actually written anywhere (confirmed repo-wide), so it's not checked.
161
- if (flow.status !== "blocked") return;
162
- if (flow.waitJson?.kind !== "human-gate" && flow.waitJson?.kind !== "gate" && flow.waitJson?.kind !== "verify-required") return;
163
-
164
- const items = flow.waitJson?.items ?? [];
165
- const approvalPending = flow.stateJson?.approvalPending ?? {};
166
- // Idempotency: an item already reflected in approvalResults[nodeId] for
167
- // THIS SPECIFIC decision was processed by an earlier partial-resume
168
- // pass. The executor may re-emit it in the next waitJson (top-level
169
- // gate nodes whose pendingSelector returns all items, not just
170
- // undecided ones), and we'd re-fire forever. Skip it here — the next
171
- // node already consumed the decision.
172
- //
173
- // Compared by decidedAt, not just itemId presence: a branch item
174
- // (itemId === branchKey) can be legitimately decided more than once if
175
- // its branch revisits the SAME human-gate sub-node for a new round
176
- // (GAP_GATE's round 2 after round 1 already resolved) — a blanket
177
- // itemId-presence check would treat round 2's freshly-recorded
178
- // approvalPending entry (newer decidedAt) as already processed forever,
179
- // since approvalResults is append-only and round 1's older entry for
180
- // the same itemId never goes away. Keep the LATEST entry per itemId
181
- // (array is append-only, so last write wins) and only skip when the
182
- // pending decision IS that already-recorded one.
183
- const nodeId = flow.waitJson?.nodeId;
184
- const resultsForNode = (nodeId && Array.isArray(flow.stateJson?.approvalResults?.[nodeId]))
185
- ? flow.stateJson.approvalResults[nodeId] : [];
186
- const lastProcessedByItem = new Map();
187
- for (const r of resultsForNode) {
188
- if (r?.itemId) lastProcessedByItem.set(r.itemId, r);
189
- }
190
- const decided = [];
191
- for (const { itemId } of items) {
192
- const entry = approvalPending[itemId];
193
- if (!entry) continue;
194
- const alreadyProcessedEntry = lastProcessedByItem.get(itemId);
195
- if (alreadyProcessedEntry && alreadyProcessedEntry.decidedAt === entry.decidedAt) continue;
196
- // Skip entries recorded for a different gate. approvalPending is a
197
- // flat map keyed by itemId; when a later gate reuses the same itemIds
198
- // (e.g. VERIFY_SUBMIT after PROPOSE), stale decisions would otherwise
199
- // auto-resolve the new gate. entry.gateNodeId is null for entries written
200
- // before this guard was added — treat those as unscoped (always eligible).
201
- if (entry.gateNodeId != null && entry.gateNodeId !== nodeId) continue;
202
- if (entry.decision) {
203
- decided.push({ itemId, decision: entry.decision, editedText: entry.editedText, reason: entry.reason, decidedAt: entry.decidedAt });
204
- } else if (entry.sendFailed) {
205
- decided.push({ itemId, decision: "reject", reason: `send failed: ${entry.sendError ?? "n/a"}`, decidedAt: entry.decidedAt ?? Date.now() });
206
- }
207
- }
208
- if (decided.length === 0) return;
209
- await applyResolutionsAndResume(flowId, decided, 0, awaitCompletion);
210
- }
211
-
212
- // A decision is recorded in two separate CAS writes — (1)
213
- // recordItemDecision's updateApprovalState durably persists
214
- // approvalPending[itemId], already committed by the time this function
215
- // runs; (2) this function's own setRunning below moves it into
216
- // approvedIds/rejectedIds and resumes the flow. If (2)'s retry loop
217
- // exhausts under sustained revision contention (e.g. several items in
218
- // one gate batch each independently triggering a maybeResolveFlow pass
219
- // that races the others' CAS writes), the decision sits recorded-but-
220
- // unapplied until the next gateway restart — flow-revival.js's
221
- // stuck-gate-decision reconciliation pass is a boot-time backstop, not a
222
- // live one. ROUND_BACKOFF_MS/MAX_ROUNDS give the SAME exhausted call up
223
- // to 2 more full 5-attempt passes, spaced out to let concurrent writers
224
- // finish, before truly giving up — closing the gap live instead of only
225
- // at the next restart.
226
- const ROUND_BACKOFF_MS = 500;
227
- const MAX_ROUNDS = 3;
228
-
229
- // `awaitCompletion` mirrors routes/operator-retry.js's retryFailureBranch
230
- // param of the same name and exists for the same reason: this function's
231
- // default behavior schedules the actual executor.resume via setImmediate
232
- // (and, on sustained revision contention, a further round via setTimeout)
233
- // and returns before either fires — correct for the long-lived HTTP
234
- // callers that use this (dashboard, companion apps), but fatal for the CLI's
235
- // one-shot `manifold approve` process, which needs the resume (and any
236
- // scheduled retry round) to have genuinely finished before it exits.
237
- async function applyResolutionsAndResume(flowId, resolutions, retryRound = 0, awaitCompletion = false) {
238
- // Revision-aware: re-read on conflict. Appends new decisions to
239
- // approvalResults[nodeId] so the executor sees the full decision history
240
- // on each re-entry. approvalPending is
241
- // left untouched: its entries double as clients' "already-delivered"
242
- // guard, so dropping them would cause re-delivery
243
- // on the next setWaiting pass for any
244
- // gate whose pendingSelector doesn't filter on approvedIds/rejectedIds.
245
- // recordItemDecision separately enforces per-item idempotency via the
246
- // entry.decision check, so leaving decided entries in place is safe.
247
- for (let attempt = 0; attempt < 5; attempt++) {
248
- const flow = await store.get(flowId);
249
- if (!flow) return;
250
- if (flow.status !== "blocked") return;
251
- const state = flow.stateJson ?? {};
252
- const approvedIds = [...(state.approvedIds ?? [])];
253
- const rejectedIds = [...(state.rejectedIds ?? [])];
254
- const editedTexts = { ...(state.editedTexts ?? {}) };
255
- const newResultsThisPass = [];
256
- for (const r of resolutions) {
257
- if (!r) continue;
258
- if (r.decision === "approve") {
259
- if (!approvedIds.includes(r.itemId)) approvedIds.push(r.itemId);
260
- if (typeof r.editedText === "string") editedTexts[r.itemId] = r.editedText;
261
- } else {
262
- if (!rejectedIds.includes(r.itemId)) rejectedIds.push(r.itemId);
263
- }
264
- newResultsThisPass.push(r);
265
- }
266
- const nodeId = flow.waitJson?.nodeId ?? "gate";
267
- const prevResults = Array.isArray(state.approvalResults?.[nodeId]) ? state.approvalResults[nodeId] : [];
268
- const newState = {
269
- ...state,
270
- approvedIds,
271
- rejectedIds,
272
- editedTexts,
273
- approvalResults: { ...(state.approvalResults ?? {}), [nodeId]: [...prevResults, ...newResultsThisPass] },
274
- };
275
- const items = flow.waitJson?.items ?? [];
276
- const undecidedCount = items.reduce((n, it) => {
277
- const e = state.approvalPending?.[it.itemId];
278
- return n + (e?.decision || e?.sendFailed ? 0 : 1);
279
- }, 0);
280
- // Re-requests this flow's own admission (kind:"flow-start" for a
281
- // root-level gate, kind:"child-dispatch" for a loop-branch gate) —
282
- // it was released the moment the flow blocked on this gate
283
- // (state-store.js's setWaiting hook), so resuming past it must
284
- // re-claim a slot, not assume one is still held. If the pool is
285
- // full, the flow is left at status:"queued" instead of running —
286
- // the decision itself still commits below (approvedIds/rejectedIds/
287
- // apply hooks), only the actual resume is deferred to
288
- // admission-queue-store's own release()/promotion cycle.
289
- const result = await admitAndSetRunning({
290
- flowId,
291
- expectedRevision: flow.revision,
292
- lastStep: flow.lastStep,
293
- stateJson: newState,
294
- parentInvocationId: flow.parentInvocationId,
295
- store,
296
- admissionQueueStore,
297
- runStore,
298
- resume: executor.resume,
299
- logger,
300
- api,
301
- });
302
- if (result.applied) {
303
- logger.info(`manifold [${flowId.slice(0, 8)}] gate-resolver: resolved ${newResultsThisPass.length} (${approvedIds.length} approve, ${rejectedIds.length} reject total)${undecidedCount > 0 ? `; ${undecidedCount} still awaiting` : ""}${result.queued ? "; queued — waiting for a shared admission slot to resume" : "; resuming"}`);
304
- gateEmitter.emit("gate.closed", { flowId, nodeId });
305
- // Resume patches for this write come from the WS run listener's
306
- // per-revision patch — emit no second shape here; clients decode one.
307
- // Human-gate apply hook: fires per item, immediately after THAT
308
- // item's decision lands — the "write the moment this one decides,
309
- // don't wait for siblings" semantics, for both a root-level gate
310
- // (own pendingSelector, no loop/branch involved) and a loop
311
- // branch's own human-gate sub-node (e.g. PROPOSE's GATE/GAP_GATE).
312
- // Fired ONLY here, after the CAS write above has committed — the
313
- // retry loop above re-runs its in-memory computation on a
314
- // revision_conflict and must stay pure; firing apply() inside it
315
- // would double-run any real side effect (e.g. a file write) on
316
- // every retry attempt. No return value is folded back into
317
- // state — if a node needs to persist something, it does so
318
- // itself inside apply (mirrors WRITE, which owns its own file
319
- // I/O rather than expecting the engine to round-trip a result).
320
- const pipelineNode = pipelineLookup?.(state.pipelineId)?.nodes?.[nodeId];
321
- if (pipelineNode) {
322
- // newState only ever carries ENTRY_FIELDS + pipelineId/item/
323
- // itemIndex/parentItem (executor-persist.js's toPersistableState
324
- // allowlist) — any kind:"code" node's own output key (e.g.
325
- // state.DRAFT, state.tbds) is stripped before it ever reaches
326
- // flow.stateJson and is only reconstructed via assembleState()'s
327
- // replay of flow_node_invocations.result_json. Every other live
328
- // read site (executor.js's dispatch loop) goes through
329
- // assembleState() for exactly this reason; apply() needs the
330
- // same so it can see prior nodes' real output (e.g.
331
- // state.DRAFT.draftPath), not just entry/bookkeeping fields.
332
- // Computed once per pass (same for every item in
333
- // newResultsThisPass), and only when there's actually an apply()
334
- // to feed — skips a needless nodeExecutionsStore round-trip for
335
- // every gate that doesn't define one.
336
- const hasApply = typeof pipelineNode.apply === "function";
337
- const assembledState = hasApply
338
- ? await assembleState(flowId, { runStore, nodeExecutionsStore: nodeExecutions, baseState: newState })
339
- : null;
340
- for (const r of newResultsThisPass) {
341
- // apply lives directly on the waited-on node.
342
- let gateNode = hasApply ? pipelineNode : null;
343
- // Dead in practice: a childFlows child's gate is resolved against
344
- // the CHILD's own flow_runs row, so the flow-level gate node is
345
- // always the right one and no per-item sub-node hop is needed.
346
- // Kept in the signature because persistApplyFailureMarker still
347
- // takes it and keys its row on it.
348
- let subNodeId = null;
349
- let applyCtx = assembledState;
350
- if (typeof gateNode?.apply !== "function") continue;
351
- // The matching waitJson item — carries requestPayload.layout,
352
- // where a gate-UI field edit (persistRevisedMeta) actually
353
- // lands. `r` only ever carries the decision + optional body
354
- // editedText, never the edited metadata fields, so apply()
355
- // needs this to see what the operator actually changed.
356
- const matchedItem = items.find((it) => it?.itemId === r.itemId);
357
- try {
358
- await gateNode.apply(applyCtx, r, matchedItem);
359
- } catch (err) {
360
- const errMessage = err?.message ?? String(err);
361
- logger.warn(`manifold [${flowId.slice(0, 8)}] gate-resolver: apply(${nodeId}, ${r.itemId}) threw: ${errMessage}`);
362
- // Beyond the warn log: persist a durable, operator-visible
363
- // marker. Without this, the gate decision still commits (the
364
- // item is approved/rejected either way) but if apply()'s own
365
- // side effect failed — a file-lock timeout, a disk error —
366
- // the operator has no way to know their decision didn't
367
- // durably land anywhere; they'd see "approved" and assume
368
- // done.
369
- await persistApplyFailureMarker({ flowId, nodeId, subNodeId, itemId: r.itemId, message: errMessage });
370
- }
371
- }
372
- }
373
- // Queued for admission — the decision durably committed above
374
- // (approvedIds/rejectedIds/apply hooks all already ran), but the
375
- // flow itself isn't running yet. Nothing left to do here:
376
- // admission-queue-store's own release()/promotion cycle resumes it
377
- // once a slot frees, same as any other queued flow.
378
- if (result.queued) return;
379
- const resumeAndReconsume = async () => {
380
- try {
381
- await executor.resume(flowId);
382
- // Race: decisions can land while the flow is `running` (between
383
- // setRunning above and the executor's next setWaiting). Those
384
- // writes go to approvalPending but maybeResolveFlow short-circuits
385
- // on non-blocked status. After resume() returns, the flow has
386
- // either transitioned back to blocked (more items to process) or
387
- // terminated; in the blocked case, re-fire to consume any
388
- // decisions that arrived mid-flight.
389
- await maybeResolveFlow(flowId, awaitCompletion);
390
- } catch (err) {
391
- logger.warn(`manifold gate-resolver: resume [${flowId.slice(0, 8)}] error: ${err?.message ?? err}`);
392
- }
393
- };
394
- if (awaitCompletion) await resumeAndReconsume();
395
- else setImmediate(resumeAndReconsume);
396
- return;
397
- }
398
- if (result.code !== "revision_conflict") {
399
- logger.warn(`manifold gate-resolver: setRunning failed for ${flowId.slice(0, 8)}: ${result.code}`);
400
- return;
401
- }
402
- await new Promise((r) => setTimeout(r, 25 + attempt * 25));
403
- }
404
- if (retryRound < MAX_ROUNDS - 1) {
405
- const delayMs = ROUND_BACKOFF_MS * (retryRound + 1);
406
- logger.warn(
407
- `manifold gate-resolver: setRunning failed for ${flowId.slice(0, 8)}: retry_exhausted ` +
408
- `(round ${retryRound + 1}/${MAX_ROUNDS}); scheduling another round in ${delayMs}ms`
409
- );
410
- const runNextRound = () => applyResolutionsAndResume(flowId, resolutions, retryRound + 1, awaitCompletion).catch((err) => {
411
- logger.warn(`manifold gate-resolver: scheduled retry round for ${flowId.slice(0, 8)} threw: ${err?.message ?? err}`);
412
- });
413
- if (awaitCompletion) {
414
- await new Promise((r) => setTimeout(r, delayMs));
415
- await runNextRound();
416
- } else {
417
- setTimeout(runNextRound, delayMs);
418
- }
419
- return;
420
- }
421
- // All live rounds exhausted — the decision is still durably recorded in
422
- // approvalPending (written before this function ever ran), so nothing
423
- // is lost. boot/flow-revival.js's stuck-gate-decision reconciliation
424
- // pass will pick this up and apply it on the next restart; this is the
425
- // loud, discoverable trail for why that backstop had to fire, not a
426
- // starting point for further live retries (avoids retrying forever
427
- // against a genuinely wedged flow).
428
- logger.warn(
429
- `manifold gate-resolver: setRunning failed for ${flowId.slice(0, 8)}: retry_exhausted after ${MAX_ROUNDS} rounds — ` +
430
- `decision remains durably recorded in approvalPending; will self-heal via flow-revival.js's stuck-gate-decision reconciliation pass on next restart`
431
- );
432
- }
433
-
434
- // Shared write path for every channel resolving a gate item.
435
- // See module header for the full caller list.
436
- //
437
- // Returns { ok, status, code? }. Status:
438
- // "already-decided" — idempotent; no write performed
439
- // "applied" — decision recorded; consolidator triggered
440
- // ok=false — code carries the failure reason
441
- async function recordItemDecision({ flowId, itemId, decision, source, editedText, reason, awaitCompletion = false }) {
442
- if (!flowId || typeof flowId !== "string") {
443
- return { ok: false, code: "missing_flow_id" };
444
- }
445
- if (!itemId || typeof itemId !== "string") {
446
- return { ok: false, code: "missing_item_id" };
447
- }
448
- if (!["approve", "reject", "edit"].includes(decision)) {
449
- return { ok: false, code: "invalid_decision" };
450
- }
451
- if (decision === "edit" && typeof editedText !== "string") {
452
- return { ok: false, code: "edit_requires_text" };
453
- }
454
- // Defense in depth: detect payloads that echo the gate's own
455
- // description back as `editedText` — a client edit intent can ship the
456
- // whole dialog body (including its "Approve to send / Reject to drop /
457
- // To edit, run in a terminal: openclaw manifold respond ..." footer)
458
- // instead of the operator's typed input. The check looks for any of
459
- // the description's telltale strings; one match is enough since these
460
- // never appear in genuine reply text.
461
- if (decision === "edit" && typeof editedText === "string") {
462
- const tellTale = [
463
- "Approve to send",
464
- "Reject to drop",
465
- "openclaw manifold respond",
466
- "─── Approve to send",
467
- ];
468
- if (tellTale.some((s) => editedText.includes(s))) {
469
- logger.warn(
470
- `manifold [${flowId.slice(0, 8)}] gate-resolver: ${itemId} edit payload looks like the gate description itself (source=${source}); rejecting to avoid shipping it as a reply`
471
- );
472
- return { ok: false, code: "edit_text_looks_like_description" };
473
- }
474
- }
475
- // "edit" is caller-facing sugar for "approve, with modified text" — an
476
- // edit IS an approval, there's no distinct third resolution state.
477
- // Normalize here (after the telltale check above, which still needs to
478
- // see the literal "edit" value) so storage/idempotency-logging/
479
- // applyResolutionsAndResume only ever see approve|reject; editedText
480
- // still carries through regardless of which spelling the caller used.
481
- if (decision === "edit") decision = "approve";
482
- if (!source || !VALID_SOURCES.includes(source)) {
483
- return { ok: false, code: "invalid_source" };
484
- }
485
-
486
- const flow = (await store.get(flowId)) ?? (await readStore?.get(flowId));
487
- if (!flow) return { ok: false, code: "flow_not_found" };
488
- if (flow.status !== "blocked" && flow.status !== "running") {
489
- return { ok: false, code: "flow_not_awaiting" };
490
- }
491
- // Item-membership validation:
492
- // - blocked: validate against flow.waitJson.items (canonical batch list).
493
- // - running: never mid-batch — childFlows gates always surface via
494
- // waitJson when blocked; validItem=false.
495
- // Idempotency first — if a decision was already recorded for this
496
- // itemId (any source, any earlier call), short-circuit ok:true.
497
- // This MUST run before the item-membership validation below: after
498
- // the executor consumes a decision, the resolved itemId drops out
499
- // of waitJson.items, so re-calling resolve for the same itemId
500
- // (audit retry, double-tap, channel race) would otherwise 409 with
501
- // item_not_in_wait even though the desired end state is already
502
- // in place. Idempotent retries should be no-ops, not failures.
503
- const existing = flow.stateJson?.approvalPending?.[itemId];
504
- if (existing?.decision) {
505
- // A branch item's identity (itemId === branchKey) is shared across
506
- // every visit to a human-gate sub-node — a branch only ever awaits
507
- // one thing at a time, so GAP_GATE's round 2 reuses the exact same
508
- // itemId as round 1. A blanket "already decided" here would
509
- // silently swallow round 2's answer forever: nothing else in this file
510
- // clears approvalPending[itemId], by design — client dismiss-signal
511
- // checks depend on it surviving past consumption.
512
- // This guard runs BEFORE the item-membership validation below, so it
513
- // cannot use membership to tell the two cases apart: a re-presented
514
- // gate does carry a fresh waitJson.items entry, but nothing has
515
- // consulted that by here. An existing approvalPending entry is
516
- // therefore always treated as a genuine duplicate.
517
- logger.info(
518
- `manifold [${flowId.slice(0, 8)}] gate-resolver: ${itemId} already decided (${existing.decision} via ${existing.source ?? "?"}); ignoring duplicate from ${source}`
519
- );
520
- return { ok: true, status: "already-decided" };
521
- }
522
-
523
- let validItem = false;
524
- if (flow.status === "blocked") {
525
- if (flow.waitJson?.kind !== "human-gate" && flow.waitJson?.kind !== "gate" && flow.waitJson?.kind !== "verify-required") {
526
- return { ok: false, code: "flow_not_gate" };
527
- }
528
- const items = Array.isArray(flow.waitJson?.items) ? flow.waitJson.items : [];
529
- validItem = items.some((it) => it?.itemId === itemId);
530
- } else {
531
- // status === "running": never mid-batch — childFlows gates always
532
- // surface via waitJson when blocked.
533
- validItem = false;
534
- }
535
- if (!validItem) {
536
- return { ok: false, code: "item_not_in_wait" };
537
- }
538
-
539
- // Capture the gate that owns this decision so maybeResolveFlow can
540
- // skip entries that were recorded for an earlier gate with the same
541
- // itemId (e.g. PROPOSE items leaking into VERIFY_SUBMIT).
542
- const gateNodeId = flow.waitJson?.nodeId ?? null;
543
-
544
- const writeResult = await updateApprovalState(flowId, (s) => ({
545
- ...s,
546
- approvalPending: {
547
- ...(s.approvalPending ?? {}),
548
- [itemId]: {
549
- ...(s.approvalPending?.[itemId] ?? {}),
550
- decision,
551
- source,
552
- decidedAt: Date.now(),
553
- gateNodeId,
554
- ...(typeof editedText === "string" ? { editedText } : {}),
555
- ...(typeof reason === "string" ? { reason } : {}),
556
- },
557
- },
558
- }));
559
- if (!writeResult.applied) {
560
- logger.warn(`manifold gate-resolver: persist decision failed for ${flowId.slice(0, 8)}:${itemId}: ${writeResult.code}`);
561
- return { ok: false, code: writeResult.code ?? "persist_failed" };
562
- }
563
- logger.info(`manifold [${flowId.slice(0, 8)}] gate-resolver: ${itemId} → ${decision} (source=${source})`);
564
-
565
- await maybeResolveFlow(flowId, awaitCompletion);
566
- // Canonical gate.resolved: wire name is the kind (`plugin.gate.resolved`).
567
- try { await emit("gate.resolved", { flowId, itemId, decision, resolvedAt: Date.now() }); } catch {}
568
- return { ok: true, status: "applied" };
569
- }
570
-
571
- return {
572
- recordItemDecision,
573
- maybeResolveFlow,
574
- updateApprovalState,
575
- };
576
- }
@@ -1,55 +0,0 @@
1
- // paused-flows.js — persistent set of flow IDs whose before_dispatch claim
2
- // is temporarily disabled, letting the operator talk to the main agent
3
- // without the priming hook intercepting every message.
4
- //
5
- // State lives under the core-owned data dir so it survives gateway
6
- // restarts and is visible to both the hook (gateway process) and the CLI
7
- // (separate process).
8
-
9
- import fs from "node:fs";
10
- import path from "node:path";
11
- import { resolveStorePaths, stateFile } from "../state/paths.js";
12
-
13
- function defaultStateFile() {
14
- return stateFile(resolveStorePaths().dataDir);
15
- }
16
- // Adapter-owned override: the openclaw adapter calls setStateFile once
17
- // with its resolved store paths.
18
- let _stateFileOverride = null;
19
- export function setStateFile(file) {
20
- _stateFileOverride = file ?? null;
21
- }
22
- function stateFilePath() {
23
- return _stateFileOverride ?? defaultStateFile();
24
- }
25
-
26
- export function listPaused() {
27
- try {
28
- const raw = fs.readFileSync(stateFilePath(), "utf8");
29
- const parsed = JSON.parse(raw);
30
- return new Set(Array.isArray(parsed) ? parsed : []);
31
- } catch {
32
- return new Set();
33
- }
34
- }
35
-
36
- export function isPaused(flowId) {
37
- return listPaused().has(flowId);
38
- }
39
-
40
- export function pause(flowId) {
41
- const set = listPaused();
42
- set.add(flowId);
43
- write(set);
44
- }
45
-
46
- export function resume(flowId) {
47
- const set = listPaused();
48
- set.delete(flowId);
49
- write(set);
50
- }
51
-
52
- function write(set) {
53
- fs.mkdirSync(path.dirname(stateFilePath()), { recursive: true });
54
- fs.writeFileSync(stateFilePath(), JSON.stringify([...set]), "utf8");
55
- }