@igorjpimenta/manifold 0.0.0-bootstrap.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 (210) hide show
  1. package/.migrations/ops/0000_futuristic_karma.sql +111 -0
  2. package/.migrations/ops/0001_low_maximus.sql +7 -0
  3. package/.migrations/ops/meta/0000_snapshot.json +763 -0
  4. package/.migrations/ops/meta/0001_snapshot.json +173 -0
  5. package/.migrations/ops/meta/_journal.json +20 -0
  6. package/.migrations/runtime/0000_real_boomer.sql +180 -0
  7. package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
  8. package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
  9. package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
  10. package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
  11. package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
  12. package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
  13. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
  14. package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
  15. package/.migrations/runtime/0009_lying_shocker.sql +96 -0
  16. package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
  17. package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
  18. package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
  19. package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
  20. package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
  21. package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
  22. package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
  23. package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
  24. package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
  25. package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
  26. package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
  27. package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
  28. package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
  29. package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
  30. package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
  31. package/.migrations/runtime/meta/_journal.json +97 -0
  32. package/README.md +1584 -0
  33. package/RELEASING.md +89 -0
  34. package/agent/fleet.js +100 -0
  35. package/bin/cli.js +854 -0
  36. package/bin/manifold.js +4 -0
  37. package/boot/flow-revival.js +536 -0
  38. package/budget/index.js +495 -0
  39. package/budget/lib.js +59 -0
  40. package/dashboard/cost.js +528 -0
  41. package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
  42. package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
  43. package/dashboard/dist/index.html +13 -0
  44. package/dashboard/event-log.js +267 -0
  45. package/dashboard/index.html +12 -0
  46. package/dashboard/package-lock.json +2877 -0
  47. package/dashboard/package.json +37 -0
  48. package/dashboard/pipeline-graph.js +131 -0
  49. package/dashboard/postcss.config.js +6 -0
  50. package/dashboard/search.js +183 -0
  51. package/dashboard/server.js +1015 -0
  52. package/dashboard/src/App.tsx +42 -0
  53. package/dashboard/src/api/client.ts +118 -0
  54. package/dashboard/src/api/types.ts +510 -0
  55. package/dashboard/src/api/ws.tsx +112 -0
  56. package/dashboard/src/components/CancelButton.tsx +63 -0
  57. package/dashboard/src/components/ClickToCopy.tsx +68 -0
  58. package/dashboard/src/components/CopyButton.tsx +61 -0
  59. package/dashboard/src/components/DailyCostTrend.tsx +85 -0
  60. package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
  61. package/dashboard/src/components/Funnel.tsx +149 -0
  62. package/dashboard/src/components/GateForm.tsx +158 -0
  63. package/dashboard/src/components/GraphView.tsx +302 -0
  64. package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
  65. package/dashboard/src/components/Histogram.tsx +292 -0
  66. package/dashboard/src/components/Layout.tsx +28 -0
  67. package/dashboard/src/components/NeedsAttention.tsx +98 -0
  68. package/dashboard/src/components/NodeContextCard.tsx +132 -0
  69. package/dashboard/src/components/PathTrail.tsx +35 -0
  70. package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
  71. package/dashboard/src/components/RerunButton.tsx +46 -0
  72. package/dashboard/src/components/RunComparison.tsx +53 -0
  73. package/dashboard/src/components/Sparkline.tsx +94 -0
  74. package/dashboard/src/components/StatStrip.tsx +147 -0
  75. package/dashboard/src/components/StateTree.tsx +84 -0
  76. package/dashboard/src/components/StatusBadge.tsx +18 -0
  77. package/dashboard/src/components/SubflowPathPills.tsx +38 -0
  78. package/dashboard/src/components/SubflowSummary.tsx +24 -0
  79. package/dashboard/src/components/SubflowsCard.tsx +327 -0
  80. package/dashboard/src/components/TodayStrip.tsx +86 -0
  81. package/dashboard/src/components/TranscriptPane.tsx +99 -0
  82. package/dashboard/src/components/TranscriptRow.tsx +69 -0
  83. package/dashboard/src/components/ui/badge.tsx +38 -0
  84. package/dashboard/src/components/ui/button.tsx +28 -0
  85. package/dashboard/src/components/ui/card.tsx +42 -0
  86. package/dashboard/src/components/ui/skeleton.tsx +9 -0
  87. package/dashboard/src/components/ui/table.tsx +62 -0
  88. package/dashboard/src/index.css +26 -0
  89. package/dashboard/src/lib/anomaly.ts +42 -0
  90. package/dashboard/src/lib/format.ts +30 -0
  91. package/dashboard/src/lib/step-history.ts +13 -0
  92. package/dashboard/src/lib/subflow.ts +154 -0
  93. package/dashboard/src/lib/use-click-outside.ts +28 -0
  94. package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
  95. package/dashboard/src/lib/utils.ts +6 -0
  96. package/dashboard/src/main.tsx +10 -0
  97. package/dashboard/src/types/shims.d.ts +4 -0
  98. package/dashboard/src/views/Home.tsx +478 -0
  99. package/dashboard/src/views/PipelineDetail.tsx +1185 -0
  100. package/dashboard/src/views/PipelinesList.tsx +111 -0
  101. package/dashboard/src/views/RunDetail.tsx +248 -0
  102. package/dashboard/src/views/Search.tsx +196 -0
  103. package/dashboard/stats.js +668 -0
  104. package/dashboard/tailwind.config.ts +43 -0
  105. package/dashboard/tree-stats.js +12 -0
  106. package/dashboard/tsconfig.json +25 -0
  107. package/dashboard/views.js +231 -0
  108. package/dashboard/vite.config.ts +34 -0
  109. package/drizzle.config.js +12 -0
  110. package/drizzle.config.ops.js +12 -0
  111. package/elevation.js +116 -0
  112. package/engine/admission-promotion-resume.js +127 -0
  113. package/engine/agent-dispatch.js +64 -0
  114. package/engine/child-resume-trigger.js +315 -0
  115. package/engine/executor-budget.js +87 -0
  116. package/engine/executor-cancel.js +83 -0
  117. package/engine/executor-errors.js +122 -0
  118. package/engine/executor-helpers.js +254 -0
  119. package/engine/executor-nodes.js +292 -0
  120. package/engine/executor-persist.js +212 -0
  121. package/engine/executor-subflow-children.js +275 -0
  122. package/engine/executor.js +1013 -0
  123. package/engine/global-concurrency.js +33 -0
  124. package/engine/loader.js +528 -0
  125. package/engine/needs-resolver.js +229 -0
  126. package/engine/pipelines-watcher.js +133 -0
  127. package/engine/result-replay.js +34 -0
  128. package/engine/revival-messages.js +11 -0
  129. package/engine/spawn-child.js +55 -0
  130. package/engine/wait-for-flow-settlement.js +109 -0
  131. package/events/catalog.js +26 -0
  132. package/events/emit.js +44 -0
  133. package/events/kinds.js +118 -0
  134. package/events/serve-ws.js +74 -0
  135. package/events/stream.js +35 -0
  136. package/events/ws-dialog.js +257 -0
  137. package/events/ws-frame.js +194 -0
  138. package/events/ws-run-events.js +70 -0
  139. package/events.js +32 -0
  140. package/gates/gate-resolver.js +576 -0
  141. package/gates/paused-flows.js +55 -0
  142. package/graphql/columns.js +67 -0
  143. package/graphql/schema.js +333 -0
  144. package/index.js +49 -0
  145. package/mcp/http.js +159 -0
  146. package/mcp/identity.js +94 -0
  147. package/mcp/stdio.js +80 -0
  148. package/mcp/tools.js +245 -0
  149. package/mcp/wire-ops.js +178 -0
  150. package/package.json +51 -0
  151. package/routes/bearer-auth.js +57 -0
  152. package/routes/discuss-audio.js +87 -0
  153. package/routes/discuss-component.js +39 -0
  154. package/routes/discuss-draft.js +159 -0
  155. package/routes/discuss-helpers.js +20 -0
  156. package/routes/discuss.js +848 -0
  157. package/routes/failures.js +104 -0
  158. package/routes/graphql.js +61 -0
  159. package/routes/health.js +54 -0
  160. package/routes/operator-discuss.js +193 -0
  161. package/routes/operator-resume.js +32 -0
  162. package/routes/operator-retry.js +301 -0
  163. package/routes/operator.js +539 -0
  164. package/routes/pipelines.js +118 -0
  165. package/routes/runs.js +1129 -0
  166. package/scripts/ci/import-check.mjs +50 -0
  167. package/scripts/link-core-self.mjs +31 -0
  168. package/scripts/with-guard.sh +24 -0
  169. package/server/app.js +503 -0
  170. package/server/registry.js +53 -0
  171. package/server/serve.js +163 -0
  172. package/state/admission-queue-store.js +325 -0
  173. package/state/attribution-registries.js +102 -0
  174. package/state/batch-writer.js +162 -0
  175. package/state/db-guard.js +63 -0
  176. package/state/db-health.js +27 -0
  177. package/state/db.js +122 -0
  178. package/state/discuss-store.js +299 -0
  179. package/state/failure-store.js +427 -0
  180. package/state/migrate.js +104 -0
  181. package/state/node-cost.js +67 -0
  182. package/state/node-executions.js +775 -0
  183. package/state/node-health-store.js +124 -0
  184. package/state/ops-merge.js +93 -0
  185. package/state/paths.js +146 -0
  186. package/state/pipeline-dirs.js +99 -0
  187. package/state/pipeline-registry.js +449 -0
  188. package/state/run-store.js +697 -0
  189. package/state/schema.js +438 -0
  190. package/state/schema.ops.js +44 -0
  191. package/state/session-index.js +177 -0
  192. package/state/session-keys.js +39 -0
  193. package/state/state-store.js +311 -0
  194. package/state/subflow-state.js +126 -0
  195. package/state/tool-handoff.js +169 -0
  196. package/telemetry/budget-alert.js +353 -0
  197. package/telemetry/cli.js +331 -0
  198. package/telemetry/grammar.js +119 -0
  199. package/telemetry/http-budget.js +132 -0
  200. package/telemetry/http-cost.js +670 -0
  201. package/telemetry/http-helpers.js +134 -0
  202. package/telemetry/http-tree.js +349 -0
  203. package/telemetry/http.js +44 -0
  204. package/telemetry/index.js +307 -0
  205. package/telemetry/ingest.js +134 -0
  206. package/telemetry/labels.js +115 -0
  207. package/telemetry/pricer.js +77 -0
  208. package/telemetry/resolver.js +314 -0
  209. package/telemetry/store.js +147 -0
  210. package/utils/run-patch.js +43 -0
@@ -0,0 +1,438 @@
1
+ import { sqliteTable, text, integer, real, index, uniqueIndex, primaryKey, foreignKey } from 'drizzle-orm/sqlite-core';
2
+ import { sql, desc } from 'drizzle-orm';
3
+
4
+ export const harnesses = sqliteTable('harnesses', {
5
+ id: text('id').primaryKey(),
6
+ createdAt: integer('created_at').notNull(),
7
+ updatedAt: integer('updated_at').notNull(),
8
+ });
9
+
10
+ export const pipelines = sqliteTable('pipelines', {
11
+ id: text('id').primaryKey(),
12
+ name: text('name').notNull(),
13
+ harness: text('harness').references(() => harnesses.id, { onDelete: 'set null' }),
14
+ createdAt: integer('created_at').notNull(),
15
+ updatedAt: integer('updated_at').notNull(),
16
+ deletedAt: integer('deleted_at'),
17
+ }, (table) => [
18
+ index('idx_pipelines_harness_name').on(table.harness, table.name),
19
+ ]);
20
+
21
+ // Session keys are assigned by the harness, outside this database, so a
22
+ // key can be referenced (parent/root lineage, cost attribution, dispatch
23
+ // session) before any table of ours has seen it. This registry is the
24
+ // target that makes those pointers FKs; writers insert OR IGNORE the keys
25
+ // they are about to reference, and state/session-keys.js backfills the
26
+ // union on migration. Rows are never pruned — a key is an identity, not a
27
+ // live record.
28
+ export const sessions = sqliteTable('sessions', {
29
+ sessionKey: text('session_key').primaryKey(),
30
+ firstSeenAt: integer('first_seen_at').notNull(),
31
+ }, (table) => [
32
+ index('idx_sessions_first_seen').on(table.firstSeenAt),
33
+ ]);
34
+
35
+ // Attribution keys (item, node name, turn run) are written by engine and
36
+ // telemetry frames, not owned by a row of ours, so these three registries
37
+ // are their materialized key set. Every writer registers what it is about
38
+ // to attribute (state/attribution-registries.js), and migration backfills
39
+ // the union, so a key is never dangling. Rows are append-only: a pruned
40
+ // flow must not erase the attribution that names it.
41
+ export const flowItems = sqliteTable('flow_items', {
42
+ flowId: text('flow_id').notNull(),
43
+ itemKey: text('item_key').notNull(),
44
+ firstSeenAt: integer('first_seen_at').notNull(),
45
+ }, (table) => [
46
+ primaryKey({ columns: [table.flowId, table.itemKey] }),
47
+ index('idx_flow_items_key').on(table.itemKey),
48
+ ]);
49
+
50
+ export const turnRuns = sqliteTable('turn_runs', {
51
+ runId: text('run_id').primaryKey(),
52
+ firstSeenAt: integer('first_seen_at').notNull(),
53
+ });
54
+
55
+ export const pipelineNodes = sqliteTable('pipeline_nodes', {
56
+ pipelineId: text('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'cascade' }),
57
+ nodeName: text('node_name').notNull(),
58
+ firstSeenAt: integer('first_seen_at').notNull(),
59
+ }, (table) => [
60
+ primaryKey({ columns: [table.pipelineId, table.nodeName] }),
61
+ index('idx_pipeline_nodes_name').on(table.nodeName),
62
+ ]);
63
+
64
+ export const flowRuns = sqliteTable('flow_runs', {
65
+ flowId: text('flow_id').primaryKey(),
66
+ ownerKey: text('owner_key').notNull().default(''),
67
+ revision: integer('revision').notNull().default(0),
68
+ status: text('status').notNull(),
69
+ lastStep: text('last_step'),
70
+ statusReason: text('status_reason'),
71
+ stateJson: text('state_json').notNull().default('{}'),
72
+ waitJson: text('wait_json'),
73
+ cancelRequestedAt: integer('cancel_requested_at'),
74
+ createdAt: integer('created_at').notNull(),
75
+ updatedAt: integer('updated_at').notNull(),
76
+ endedAt: integer('ended_at'),
77
+ parentInvocationId: text('parent_invocation_id').references(() => flowNodeInvocations.id, { onDelete: 'set null' }),
78
+ pipelineId: text('pipeline_id').references(() => pipelines.id, { onDelete: 'set null' }),
79
+ }, (table) => [
80
+ index('idx_pfr_status').on(table.status).where(sql`${table.endedAt} IS NULL`),
81
+ index('idx_pfr_updated').on(desc(table.updatedAt)),
82
+ index('idx_pfr_created').on(desc(table.createdAt)),
83
+ uniqueIndex('idx_pfr_parent_invocation').on(table.parentInvocationId).where(sql`${table.parentInvocationId} IS NOT NULL`),
84
+ index('idx_flow_runs_pipeline_id').on(table.pipelineId),
85
+ ]);
86
+
87
+ export const flowNodeInvocations = sqliteTable('flow_node_invocations', {
88
+ id: text('id').primaryKey(),
89
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
90
+ pipelineId: text('pipeline_id'),
91
+ nodeId: text('node_id').notNull(),
92
+ subflowKey: text('subflow_key'),
93
+ visitSeq: integer('visit_seq').notNull().default(1),
94
+ invokedAt: integer('invoked_at').notNull(),
95
+ // Option B, Phase B0 — this
96
+ // table's first payload column. resultWrittenAt is the "is this real"
97
+ // signal a bare nullable resultJson can't provide on its own (not
98
+ // dispatched vs. dispatched-not-complete vs. complete-with-null-result).
99
+ // resultTooLargeBytes carries the real size of a result that was capped
100
+ // instead of stored (see writeResult() in
101
+ // state/node-executions.js). Every write today sets it null; a non-null
102
+ // value marks a row whose result was never stored.
103
+ resultJson: text('result_json'),
104
+ resultWrittenAt: integer('result_written_at'),
105
+ resultTooLargeBytes: integer('result_too_large_bytes'),
106
+ }, (table) => [
107
+ // NOTE: idx_invocations_identity (UNIQUE on flow_id, node_id,
108
+ // COALESCE(subflow_key, ''), visit_seq) is intentionally absent here —
109
+ // drizzle-kit cannot serialize sql fragments inside an index ON clause,
110
+ // so it lives verbatim at the end of .migrations/runtime/0000 (same
111
+ // anchor-edit category as the IF NOT EXISTS pass above it).
112
+ index('idx_invocations_flow').on(table.flowId),
113
+ index('idx_invocations_node_subflow').on(table.nodeId, table.subflowKey),
114
+ index('idx_invocations_pipe_node').on(table.pipelineId, table.nodeId),
115
+ ]);
116
+
117
+ // error/error_type intentionally NOT here — error content lives entirely
118
+ // into failure_events, which gets a permanent row on every single
119
+ // throw regardless of what a cascade edge decides to do next. This table
120
+ // is purely an execution-window/identity ledger: which node, which
121
+ // attempt, when it ran, why kind of dispatch (fresh/retry/revival/
122
+ // recheck) — never what went wrong.
123
+ export const flowNodeExecutions = sqliteTable('flow_node_executions', {
124
+ id: text('id').primaryKey(),
125
+ invocationId: text('invocation_id').notNull().references(() => flowNodeInvocations.id, { onDelete: 'cascade' }),
126
+ enteredAt: integer('entered_at').notNull(),
127
+ exitedAt: integer('exited_at').notNull(),
128
+ kind: text('kind').notNull().default('legacy'),
129
+ }, (table) => [
130
+ index('idx_fne_invocation').on(table.invocationId),
131
+ index('idx_fne_exited').on(table.exitedAt),
132
+ ]);
133
+
134
+ // flow_branches (branch storage) is still a real table in runtime.sqlite —
135
+ // read directly via raw SQL by state/node-executions.js, never through
136
+ // Drizzle — and deliberately has no schema export here: nothing imports
137
+ // one.
138
+
139
+ // Deliberately thin — pipeline_id/branch_item_id/failure_node/recovery_hint/
140
+ // resolved status are reconstructed at read time via invocation_id/
141
+ // execution_id joining to flow_node_invocations/flow_node_executions
142
+ // (see failure-store.js's deriveFailureContext), not stored here.
143
+ export const failureEvents = sqliteTable('failure_events', {
144
+ id: text('id').primaryKey(),
145
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
146
+ failedAt: integer('failed_at').notNull(),
147
+ failureSummary: text('failure_summary').notNull(),
148
+ invocationId: text('invocation_id').references(() => flowNodeInvocations.id, { onDelete: 'set null' }),
149
+ executionId: text('execution_id').references(() => flowNodeExecutions.id, { onDelete: 'set null' }),
150
+ // Error content lives here, not in flow_node_executions, so it is not
151
+ // derivable via a join. NodeFailure vocabulary: never_attempted|
152
+ // confirmed_absent|inconclusive|infra_unavailable|blocked — plus
153
+ // "subflow-mode-not-satisfied" for a subflow node whose exit mode wasn't
154
+ // met (not a thrown NodeFailure at all, a policy verdict — see
155
+ // failure-store.js's classifyRecoveryHint) — or null for an ordinary
156
+ // engine-caught throw with no further classification.
157
+ errorType: text('error_type'),
158
+ }, (table) => [
159
+ index('idx_failures_flow').on(table.flowId),
160
+ index('idx_failures_failed_at').on(desc(table.failedAt)),
161
+ index('idx_failures_invocation').on(table.invocationId),
162
+ uniqueIndex('idx_failures_execution').on(table.executionId).where(sql`${table.executionId} IS NOT NULL`),
163
+ ]);
164
+
165
+ export const discussState = sqliteTable('discuss_state', {
166
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
167
+ itemId: text('item_id').notNull(),
168
+ messagesJson: text('messages_json').notNull().default('[]'),
169
+ turnStatus: text('turn_status').notNull().default('idle'),
170
+ activeTurnId: text('active_turn_id'),
171
+ pendingJson: text('pending_json'),
172
+ updatedAt: integer('updated_at').notNull(),
173
+ componentsJson: text('components_json').notNull().default('[]'),
174
+ }, (table) => [
175
+ index('idx_discuss_updated').on(table.updatedAt),
176
+ // One row per discuss thread. Without this constraint two concurrent
177
+ // appendMessage calls each insert their own row and the thread's
178
+ // messages split across them.
179
+ uniqueIndex('idx_discuss_state_unique').on(table.flowId, table.itemId),
180
+ ]);
181
+
182
+ export const flowSessions = sqliteTable('flow_sessions', {
183
+ id: text('id').primaryKey(),
184
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
185
+ session: text('session_key').notNull().references(() => sessions.sessionKey),
186
+ nodeId: text('node_id'),
187
+ branchItemId: text('branch_item_id'),
188
+ spawnedAt: integer('spawned_at').notNull(),
189
+ }, (table) => [
190
+ foreignKey({ columns: [table.flowId, table.branchItemId], foreignColumns: [flowItems.flowId, flowItems.itemKey] }),
191
+ index('idx_flow_sessions_flow_id').on(table.flowId),
192
+ uniqueIndex('idx_flow_sessions_uniq').on(table.flowId, table.session),
193
+ ]);
194
+
195
+ // Gate discuss thread identity: one row per (flow, gate, item), keyed by
196
+ // the coordinates — never by parsing the session id. Replaces the
197
+ // per-thread JSON sidecars: the roster is the map, the harness owns the id.
198
+ export const discussThreads = sqliteTable('discuss_threads', {
199
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
200
+ gateNodeId: text('gate_node_id').notNull(),
201
+ itemId: text('item_id').notNull(),
202
+ agentId: text('agent_id'),
203
+ session: text('session_key').notNull().references(() => sessions.sessionKey),
204
+ updatedAt: integer('updated_at').notNull(),
205
+ }, (table) => [
206
+ uniqueIndex('idx_discuss_threads_unique').on(table.flowId, table.gateNodeId, table.itemId),
207
+ ]);
208
+
209
+ export const nodeHealth = sqliteTable('node_health', {
210
+ id: text('id').primaryKey(),
211
+ pipelineId: text('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'restrict' }),
212
+ nodeId: text('node_id').notNull(),
213
+ status: text('status').notNull(),
214
+ reason: text('reason').notNull(),
215
+ blockedByFlowId: text('blocked_by_flow_id').references(() => flowRuns.flowId, { onDelete: 'set null' }),
216
+ resolvedVia: text('resolved_via'),
217
+ createdAt: integer('created_at').notNull(),
218
+ updatedAt: integer('updated_at').notNull(),
219
+ endedAt: integer('ended_at'),
220
+ }, (table) => [
221
+ foreignKey({ columns: [table.pipelineId, table.nodeId], foreignColumns: [pipelineNodes.pipelineId, pipelineNodes.nodeName] }),
222
+ uniqueIndex('idx_node_health_one_active').on(table.pipelineId, table.nodeId).where(sql`${table.status} = 'blocked'`),
223
+ index('idx_node_health_pipeline').on(table.pipelineId, table.status),
224
+ ]);
225
+
226
+ // Point lookup of one gate node's own record — keyed (flow_id, node_id) so
227
+ // sibling gates never share a key that can go stale. Written/read via raw
228
+ // SQL (like flow_branches below); the def exists so snapshots cover it.
229
+ export const gateRecords = sqliteTable('gate_records', {
230
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
231
+ nodeId: text('node_id').notNull(),
232
+ recordJson: text('record_json').notNull(),
233
+ updatedAt: integer('updated_at').notNull(),
234
+ }, (table) => [
235
+ primaryKey({ columns: [table.flowId, table.nodeId] }),
236
+ ]);
237
+
238
+ export const pipelineVersions = sqliteTable('pipeline_versions', {
239
+ id: text('id').primaryKey(),
240
+ pipelineId: text('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'restrict' }),
241
+ kind: text('kind'),
242
+ allowConcurrent: text('allow_concurrent'),
243
+ isSynthetic: integer('is_synthetic').notNull(),
244
+ parentPipelineId: text('parent_pipeline_id').references(() => pipelines.id, { onDelete: 'set null' }),
245
+ sourcePath: text('source_path'),
246
+ contentHash: text('content_hash').notNull(),
247
+ registeredAt: integer('registered_at').notNull(),
248
+ retiredAt: integer('retired_at'),
249
+ }, (table) => [
250
+ index('idx_pv_lookup').on(table.pipelineId, table.registeredAt),
251
+ uniqueIndex('idx_pv_active').on(table.pipelineId).where(sql`${table.retiredAt} IS NULL`),
252
+ ]);
253
+
254
+ export const pipelineNodeVersions = sqliteTable('pipeline_node_versions', {
255
+ id: text('id').primaryKey(),
256
+ pipelineVersionId: text('pipeline_version_id').notNull().references(() => pipelineVersions.id, { onDelete: 'cascade' }),
257
+ pipelineId: text('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'restrict' }),
258
+ nodeName: text('node_name').notNull(),
259
+ nodeId: text('node_id'),
260
+ kind: text('kind').notNull(),
261
+ agent: text('agent'),
262
+ mode: text('mode'),
263
+ contentHash: text('content_hash').notNull(),
264
+ registeredAt: integer('registered_at').notNull(),
265
+ retiredAt: integer('retired_at'),
266
+ }, (table) => [
267
+ index('idx_pnv_lookup').on(table.pipelineId, table.nodeName, table.registeredAt),
268
+ uniqueIndex('idx_pnv_active').on(table.pipelineId, table.nodeName).where(sql`${table.retiredAt} IS NULL`),
269
+ ]);
270
+
271
+ export const flowAdmissionQueue = sqliteTable('flow_admission_queue', {
272
+ id: text('id').primaryKey(),
273
+ kind: text('kind').notNull(),
274
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
275
+ nodeId: text('node_id'),
276
+ status: text('status').notNull(),
277
+ createdAt: integer('created_at').notNull(),
278
+ admittedAt: integer('admitted_at'),
279
+ }, (table) => [
280
+ index('idx_admission_status').on(table.status),
281
+ index('idx_admission_flow').on(table.flowId),
282
+ index('idx_admission_status_created').on(table.status, table.createdAt),
283
+ ]);
284
+
285
+ export const flowEvents = sqliteTable('flow_events', {
286
+ id: text('id').primaryKey(),
287
+ flowId: text('flow_id').notNull().references(() => flowRuns.flowId, { onDelete: 'cascade' }),
288
+ eventType: text('event_type').notNull(),
289
+ prevStep: text('prev_step'),
290
+ currStep: text('curr_step'),
291
+ prevStatus: text('prev_status'),
292
+ currStatus: text('curr_status'),
293
+ waitKind: text('wait_kind'),
294
+ occurredAt: integer('occurred_at').notNull(),
295
+ }, (table) => [
296
+ index('idx_flow_events_flow_id').on(table.flowId),
297
+ index('idx_flow_events_occurred_at').on(table.occurredAt),
298
+ ]);
299
+ // Model-routing circuit-breaker state. Separate from node_health because the
300
+ // two tiers disagree on expiry: a quarantined model carries its own deadline
301
+ // in until_ms, while a blocked node has no deadline at all and waits for an
302
+ // operator. See model-circuit-breaker/store.js.
303
+ export const modelHealth = sqliteTable('model_health', {
304
+ id: text('id').primaryKey(),
305
+ ref: text('ref').notNull(),
306
+ status: text('status').notNull(),
307
+ reason: text('reason').notNull(),
308
+ downSince: integer('down_since').notNull(),
309
+ untilMs: integer('until_ms'),
310
+ probing: integer('probing').notNull().default(0),
311
+ strikeLevel: integer('strike_level').notNull().default(0),
312
+ strikeEndedAt: integer('strike_ended_at'),
313
+ createdAt: integer('created_at').notNull(),
314
+ updatedAt: integer('updated_at').notNull(),
315
+ });
316
+
317
+
318
+ // Per-harness pipeline directory overrides, one row per harness. Written by
319
+ // deliberate admin acts (`manifold pipelines register`, operator tooling);
320
+ // read once at boot to resolve the pipelines dir. No expiry: removing an
321
+ // override is a delete, not a flag.
322
+ export const pipelineDirs = sqliteTable('pipeline_dirs', {
323
+ harness: text('harness').primaryKey().references(() => harnesses.id, { onDelete: 'cascade' }),
324
+ path: text('path').notNull(),
325
+ createdAt: integer('created_at').notNull(),
326
+ });
327
+
328
+ // Structured tool results delivered by agents over MCP
329
+ // (manifold_deliver_result). Keyed by the harness-stamped session the
330
+ // agent and the waiting dispatch both know — no invocationId resolution.
331
+ // Latest write wins; the dispatch consumes the row for its own turn.
332
+ export const toolResults = sqliteTable('tool_results', {
333
+ session: text('session_key').primaryKey().references(() => sessions.sessionKey),
334
+ runId: text('run_id').references(() => flowRuns.flowId, { onDelete: 'set null' }),
335
+ agentId: text('agent_id'),
336
+ kind: text('kind'),
337
+ payloadJson: text('payload_json').notNull(),
338
+ updatedAt: integer('updated_at').notNull(),
339
+ });
340
+
341
+ // Lineage and cost telemetry, moved out of ops.sqlite: flow_id and
342
+ // pipeline_id carry FKs to runtime parents, and SQLite cannot constrain a
343
+ // column against a table in another file. The ops-side definitions stay in
344
+ // schema.ops.js as the frozen shape of the old file.
345
+ export const queueItems = sqliteTable('queue_items', {
346
+ id: text('id').primaryKey(),
347
+ label: text('label').notNull(),
348
+ agent: text('agent').notNull(),
349
+ agentEmoji: text('agent_emoji').notNull().default('🤖'),
350
+ trigger: text('trigger').notNull().default('manual'),
351
+ costLow: real('cost_low').notNull().default(0),
352
+ costHigh: real('cost_high').notNull().default(0),
353
+ brief: text('brief'),
354
+ modelChain: text('model_chain'),
355
+ pipeline: text('pipeline'),
356
+ node: text('node'),
357
+ tokenEstimate: integer('token_estimate'),
358
+ status: text('status').notNull().default('pending'),
359
+ createdAt: integer('created_at').notNull(),
360
+ expiresAt: integer('expires_at'),
361
+ decidedAt: integer('decided_at'),
362
+ decision: text('decision'),
363
+ pipelineId: text('pipeline_id').references(() => pipelines.id, { onDelete: 'set null' }),
364
+ pipelineArgs: text('pipeline_args'),
365
+ flowId: text('flow_id').references(() => flowRuns.flowId, { onDelete: 'set null' }),
366
+ enqueuerSession: text('enqueuer_session_key').references(() => sessions.sessionKey, { onDelete: 'set null' }),
367
+ kind: text('kind'),
368
+ }, (table) => [
369
+ foreignKey({ columns: [table.pipelineId, table.node], foreignColumns: [pipelineNodes.pipelineId, pipelineNodes.nodeName] }),
370
+ index('idx_status').on(table.status),
371
+ index('idx_created_at').on(desc(table.createdAt)),
372
+ index('idx_flow_node').on(table.flowId, table.node),
373
+ ]);
374
+
375
+ export const sessionLineage = sqliteTable('session_lineage', {
376
+ childSession: text('child_session_key').primaryKey().references(() => sessions.sessionKey),
377
+ parentSessionKey: text('parent_session_key').references(() => sessions.sessionKey, { onDelete: 'set null' }),
378
+ rootSessionKey: text('root_session_key').notNull().references(() => sessions.sessionKey),
379
+ kind: text('kind').notNull(),
380
+ rootKind: text('root_kind').notNull(),
381
+ rootLabel: text('root_label'),
382
+ agentId: text('agent_id'),
383
+ pipelineId: text('pipeline_id').references(() => pipelines.id, { onDelete: 'set null' }),
384
+ flowId: text('flow_id').references(() => flowRuns.flowId, { onDelete: 'set null' }),
385
+ nodeId: text('node_id'),
386
+ itemKey: text('item_key'),
387
+ firstSeenAt: integer('first_seen_at').notNull(),
388
+ source: text('source').notNull(),
389
+ runId: text('run_id').references(() => turnRuns.runId, { onDelete: 'set null' }),
390
+ }, (table) => [
391
+ foreignKey({ columns: [table.pipelineId, table.nodeId], foreignColumns: [pipelineNodes.pipelineId, pipelineNodes.nodeName] }),
392
+ foreignKey({ columns: [table.flowId, table.itemKey], foreignColumns: [flowItems.flowId, flowItems.itemKey] }),
393
+ index('idx_lineage_root').on(table.rootSessionKey),
394
+ index('idx_lineage_parent').on(table.parentSessionKey),
395
+ index('idx_lineage_pipeline').on(table.pipelineId),
396
+ index('idx_lineage_flow').on(table.flowId),
397
+ index('idx_lineage_root_kind').on(table.rootKind),
398
+ index('idx_lineage_run').on(table.runId),
399
+ ]);
400
+
401
+ export const costByCall = sqliteTable('cost_by_call', {
402
+ sessionId: text('session_id').notNull(),
403
+ msgId: text('msg_id').notNull(),
404
+ ts: integer('ts').notNull(),
405
+ provider: text('provider'),
406
+ model: text('model'),
407
+ inputTokens: integer('input_tokens'),
408
+ outputTokens: integer('output_tokens'),
409
+ cacheReadTokens: integer('cache_read_tokens'),
410
+ cacheWriteTokens: integer('cache_write_tokens'),
411
+ totalTokens: integer('total_tokens'),
412
+ costInput: real('cost_input'),
413
+ costOutput: real('cost_output'),
414
+ costCacheRead: real('cost_cache_read'),
415
+ costCacheWrite: real('cost_cache_write'),
416
+ costTotal: real('cost_total').notNull(),
417
+ session: text('session_key').references(() => sessions.sessionKey, { onDelete: 'set null' }),
418
+ agentId: text('agent_id'),
419
+ rootSessionKey: text('root_session_key').references(() => sessions.sessionKey, { onDelete: 'set null' }),
420
+ rootKind: text('root_kind'),
421
+ rootLabel: text('root_label'),
422
+ pipelineId: text('pipeline_id').references(() => pipelines.id, { onDelete: 'set null' }),
423
+ flowId: text('flow_id').references(() => flowRuns.flowId, { onDelete: 'set null' }),
424
+ nodeId: text('node_id'),
425
+ itemKey: text('item_key'),
426
+ }, (table) => [
427
+ primaryKey({ columns: [table.sessionId, table.msgId] }),
428
+ foreignKey({ columns: [table.pipelineId, table.nodeId], foreignColumns: [pipelineNodes.pipelineId, pipelineNodes.nodeName] }),
429
+ foreignKey({ columns: [table.flowId, table.itemKey], foreignColumns: [flowItems.flowId, flowItems.itemKey] }),
430
+ index('idx_cost_ts').on(table.ts),
431
+ index('idx_cost_root_key').on(table.rootSessionKey),
432
+ index('idx_cost_root_kind_ts').on(table.rootKind, table.ts),
433
+ index('idx_cost_pipeline_ts').on(table.pipelineId, table.ts),
434
+ index('idx_cost_flow_node').on(table.flowId, table.nodeId),
435
+ index('idx_cost_model_ts').on(table.model, table.ts),
436
+ index('idx_cost_session').on(table.sessionId),
437
+ ]);
438
+
@@ -0,0 +1,44 @@
1
+ // state/schema.ops.js — ops.sqlite table definitions (drizzle source of truth).
2
+ //
3
+ // Companion to state/schema.js (runtime.sqlite). Same contract: drizzle-kit
4
+ // generates .migrations/ops snapshots from this file; state/migrate.js
5
+ // applies them at open. Raw-SQL readers stay as they are — defs exist so
6
+ // snapshots cover every table, not to force ORM queries.
7
+ // queue_items / session_lineage / cost_by_call are not defined here: they
8
+ // live in state/schema.js (runtime.sqlite) so their flow_id and pipeline_id
9
+ // can carry FKs. Older ops files still contain the copies they were moved
10
+ // from; state/drop-moved-ops.js removes them once the runtime side holds
11
+ // every row (see that file for the ordering and the row-count guard).
12
+ import { sqliteTable, text, integer, real, index, uniqueIndex, primaryKey } from 'drizzle-orm/sqlite-core';
13
+ import { desc, sql } from 'drizzle-orm';
14
+
15
+ export const gateState = sqliteTable('gate_state', {
16
+ id: text('id').primaryKey().default('singleton'),
17
+ locked: integer('locked').notNull().default(0),
18
+ day: text('day'),
19
+ });
20
+
21
+ export const capVersions = sqliteTable('cap_versions', {
22
+ id: text('id').primaryKey(),
23
+ capUsd: real('cap_usd').notNull(),
24
+ lockThreshold: real('lock_threshold').notNull(),
25
+ registeredAt: integer('registered_at').notNull(),
26
+ retiredAt: integer('retired_at'),
27
+ }, (table) => [
28
+ index('idx_cv_lookup').on(table.registeredAt),
29
+ uniqueIndex('idx_cv_active').on(sql`(1)`).where(sql`${table.retiredAt} IS NULL`),
30
+ ]);
31
+
32
+ // Durable log of threshold crossings — the .alerted-thresholds.json debounce
33
+ // file only ever answers "did we already fire threshold X today" and is
34
+ // overwritten every local day, so it can't answer "when did we last
35
+ // cross 80%". This table is the queryable history that backs that question.
36
+ export const budgetAlertEvents = sqliteTable('budget_alert_events', {
37
+ id: text('id').primaryKey(),
38
+ threshold: real('threshold').notNull(),
39
+ totalUsd: real('total_usd').notNull(),
40
+ capUsd: real('cap_usd').notNull(),
41
+ firedAt: integer('fired_at').notNull(),
42
+ }, (table) => [
43
+ index('idx_bae_fired_at').on(table.firedAt),
44
+ ]);
@@ -0,0 +1,177 @@
1
+ // session-index.js — durable flowId→session index. Sessions are the
2
+ // ids the dispatch used — stored and read verbatim, with no assumed
3
+ // shape.
4
+ //
5
+ // Storage: the store home's runtime.sqlite (resolveStorePaths().runtimeDb).
6
+ //
7
+ // INSERT OR IGNORE via UNIQUE(flow_id, session_key) — recording the same
8
+ // session twice (e.g. on error-retry with the same session) is a no-op.
9
+
10
+ import { mkdirSync } from "node:fs";
11
+ import { eq, and } from "drizzle-orm";
12
+ import { getSharedDb, withSharedWrite, resolveStorePaths } from "./db.js";
13
+ import { flowSessions, discussThreads } from "./schema.js";
14
+ import { randomUUID } from "node:crypto";
15
+ import path from "node:path";
16
+ import os from "node:os";
17
+ import { migrateDatabase, RUNTIME_MIGRATIONS_FOLDER } from "./migrate.js";
18
+ import { ensureSessionKeys } from "./session-keys.js";
19
+ import { ensureFlowItems } from "./attribution-registries.js";
20
+
21
+ function defaultDbPath() {
22
+ return resolveStorePaths().runtimeDb;
23
+ }
24
+
25
+ export async function createSessionIndex({ dbPath = defaultDbPath(), logger } = {}) {
26
+ mkdirSync(path.dirname(dbPath), { recursive: true });
27
+ let db, client;
28
+ try {
29
+ await migrateDatabase({ dbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
30
+ ({ db, client } = await getSharedDb(dbPath));
31
+ } catch (err) {
32
+ logger?.warn?.(`manifold session-index: failed to open DB: ${err?.message ?? err}`);
33
+ // Return a no-op shim so callers don't need to null-check.
34
+ return { async record() {}, async hasSessionForNode() { return false; }, async flowIdForSessionKey() { return null; }, async listForFlow() { return []; }, async recordThread() {}, async findThread() { return null; }, close() {} };
35
+ }
36
+
37
+ // `agent` is accepted and ignored (kept for call-site compatibility).
38
+ async function record({ flowId, session, nodeId = null, branchItemId = null, agent = null } = {}) {
39
+ void agent;
40
+ if (!flowId || !session) return;
41
+ try {
42
+ await withSharedWrite(dbPath, async () => {
43
+ await ensureSessionKeys(client, [session]);
44
+ await ensureFlowItems(client, [[flowId, branchItemId]]);
45
+ return db.insert(flowSessions).values({
46
+ id: randomUUID(), flowId, session,
47
+ nodeId: nodeId ?? null, branchItemId: branchItemId ?? null,
48
+ spawnedAt: Date.now(),
49
+ }).onConflictDoNothing();
50
+ });
51
+ } catch (err) {
52
+ logger?.warn?.(`manifold session-index: record failed: ${err?.message ?? err}`);
53
+ }
54
+ }
55
+
56
+ // Was an agent dispatch ever actually ATTEMPTED for this (flowId, nodeId)?
57
+ // record() (above) is called at the top of dispatchAgentWithRetry, always
58
+ // before the turn is run. This answers "did dispatch
59
+ // start" independent of whether it finished, blocked, or is still in
60
+ // flight. Used by boot/flow-revival.js to distinguish a flow that crashed
61
+ // mid-turn (revive via continuation prompt) from one that was merely queued
62
+ // waiting for a slot and never actually started (revive via a fresh
63
+ // dispatch instead).
64
+ async function hasSessionForNode(flowId, nodeId) {
65
+ if (!flowId || !nodeId) return false;
66
+ try {
67
+ const rows = await db.select({ x: flowSessions.id }).from(flowSessions)
68
+ .where(and(eq(flowSessions.flowId, flowId), eq(flowSessions.nodeId, nodeId)))
69
+ .limit(1);
70
+ return rows.length > 0;
71
+ } catch (err) {
72
+ logger?.warn?.(`manifold session-index: hasSessionForNode failed: ${err?.message ?? err}`);
73
+ return false;
74
+ }
75
+ }
76
+
77
+ // Reverse of the above: given a session alone (no flowId in hand —
78
+ // e.g. a session observed on the event queue with no flow attached),
79
+ // find which flow it belongs to.
80
+ async function flowIdForSessionKey(session) {
81
+ if (!session) return null;
82
+ try {
83
+ const rows = await db.select({ x: flowSessions.flowId }).from(flowSessions)
84
+ .where(eq(flowSessions.session, session))
85
+ .limit(1);
86
+ return rows[0]?.x ?? null;
87
+ } catch (err) {
88
+ logger?.warn?.(`manifold session-index: flowIdForSessionKey failed: ${err?.message ?? err}`);
89
+ return null;
90
+ }
91
+ }
92
+
93
+ // Every session ever recorded for one flow, in spawn order.
94
+ // Transcript listing joins these rows to per-node results (dispatches)
95
+ // and discuss threads (branchItemId = itemId) so attribution comes from
96
+ // stored columns, never from parsing the session itself.
97
+ async function listForFlow(flowId) {
98
+ if (!flowId) return [];
99
+ try {
100
+ const rows = await db.select({
101
+ session: flowSessions.session,
102
+ nodeId: flowSessions.nodeId,
103
+ branchItemId: flowSessions.branchItemId,
104
+ spawnedAt: flowSessions.spawnedAt,
105
+ }).from(flowSessions)
106
+ .where(eq(flowSessions.flowId, flowId))
107
+ .orderBy(flowSessions.spawnedAt);
108
+ return rows.map((r) => ({
109
+ session: r.session,
110
+ nodeId: r.nodeId ?? null,
111
+ branchItemId: r.branchItemId ?? null,
112
+ spawnedAt: Number(r.spawnedAt),
113
+ }));
114
+ } catch (err) {
115
+ logger?.warn?.(`manifold session-index: listForFlow failed: ${err?.message ?? err}`);
116
+ return [];
117
+ }
118
+ }
119
+
120
+ // Gate discuss thread identity: one row per (flow, gate, item), upserted
121
+ // on first turn. The roster is the thread map — the harness owns the id.
122
+ // Latest write wins on conflict.
123
+ async function recordThread({ flowId, gateNodeId, itemId, agentId = null, session } = {}) {
124
+ if (!flowId || !gateNodeId || !itemId || !session) return;
125
+ try {
126
+ await withSharedWrite(dbPath, async () => {
127
+ await ensureSessionKeys(client, [session]);
128
+ return db.insert(discussThreads).values({
129
+ flowId, gateNodeId, itemId,
130
+ agentId: agentId ?? null, session,
131
+ updatedAt: Date.now(),
132
+ }).onConflictDoUpdate({
133
+ target: [discussThreads.flowId, discussThreads.gateNodeId, discussThreads.itemId],
134
+ set: {
135
+ agentId: agentId ?? null, session,
136
+ updatedAt: Date.now(),
137
+ },
138
+ });
139
+ });
140
+ } catch (err) {
141
+ logger?.warn?.(`manifold session-index: recordThread failed: ${err?.message ?? err}`);
142
+ }
143
+ }
144
+
145
+ // Thread stamp for (flow, gate, item), or null when no turn ran yet.
146
+ async function findThread(flowId, gateNodeId, itemId) {
147
+ if (!flowId || !gateNodeId || !itemId) return null;
148
+ try {
149
+ const rows = await db.select({
150
+ session: discussThreads.session,
151
+ agentId: discussThreads.agentId,
152
+ }).from(discussThreads)
153
+ .where(and(
154
+ eq(discussThreads.flowId, flowId),
155
+ eq(discussThreads.gateNodeId, gateNodeId),
156
+ eq(discussThreads.itemId, itemId),
157
+ ))
158
+ .limit(1);
159
+ if (!rows.length) return null;
160
+ return { session: rows[0].session, agentId: rows[0].agentId ?? null };
161
+ } catch (err) {
162
+ logger?.warn?.(`manifold session-index: findThread failed: ${err?.message ?? err}`);
163
+ return null;
164
+ }
165
+ }
166
+
167
+ return {
168
+ record,
169
+ hasSessionForNode,
170
+ flowIdForSessionKey,
171
+ listForFlow,
172
+ recordThread,
173
+ findThread,
174
+ close() { try { client.close(); } catch { /* ignore */ } },
175
+ _db: client,
176
+ };
177
+ }