@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
package/events/kinds.js DELETED
@@ -1,118 +0,0 @@
1
- // events/kinds.js — canonical Phase-1 event-kind registry.
2
- //
3
- // Core emits bare kinds (dots only, no `plugin.` prefix); the gateway
4
- // boundary adds the prefix. There are no legacy names: a breaking change
5
- // mints a new kind. Every payload contract here is the minimum a consumer
6
- // can rely on — producers may attach more fields, never fewer.
7
-
8
- // Every kind with a Phase-1 producer. Frozen: adding a kind is a
9
- // deliberate registry change, never an inline string at a call site.
10
- // Evolution rule (CloudEvents primer §versioning): additive payload
11
- // changes keep the kind; a breaking change mints a NEW kind and dual-
12
- // produces old and new until consumers cut over. There is deliberately no
13
- // integer version field — an unread version versions nothing; the kind
14
- // name is the version.
15
- export const WIRE_KINDS = Object.freeze([
16
- "run.created",
17
- "run.started",
18
- "run.updated",
19
- "run.ended",
20
- "gate.opened",
21
- "gate.resolved",
22
- "session.transcript.completed",
23
- "session.transcript.failed",
24
- "session.transcript.updated",
25
- "discuss.component.proposed",
26
- "discuss.pending.updated",
27
- "pipeline.catalog.updated",
28
- "pipeline.stats.updated",
29
- "budget.queue.updated",
30
- "budget.lock.changed",
31
- "budget.cap.changed",
32
- "cost.record.added",
33
- "failure.recorded",
34
- "approval.requested",
35
- ]);
36
-
37
- // Named but producer-less: emitting one is a typo or a Phase-2 leak.
38
- export const RESERVED_KINDS = Object.freeze([
39
- "gate.dissolved",
40
- ]);
41
-
42
- // Minimum required payload fields per kind. Session kinds additionally
43
- // require a valid `scope` (checked in assertPayload, not expressible here).
44
- const REQUIRED_FIELDS = {
45
- "run.created": ["flowId"],
46
- "run.started": ["flowId"],
47
- "run.updated": ["flowId"],
48
- "run.ended": ["flowId", "status"],
49
- "gate.opened": ["flowId"],
50
- "gate.resolved": ["flowId", "itemId", "decision"],
51
- "session.transcript.completed": ["scope", "text"],
52
- "session.transcript.failed": ["scope", "error"],
53
- "session.transcript.updated": ["scope"],
54
- "discuss.component.proposed": ["flowId", "itemId", "component"],
55
- "discuss.pending.updated": ["flowId", "itemId"],
56
- "pipeline.catalog.updated": ["count", "ids"],
57
- "pipeline.stats.updated": ["id"],
58
- "budget.queue.updated": ["decision", "count"],
59
- "budget.lock.changed": ["locked", "day"],
60
- "budget.cap.changed": [],
61
- "cost.record.added": ["costTotal"],
62
- "failure.recorded": ["failureId", "flowId"],
63
- "approval.requested": ["id"],
64
- };
65
-
66
- export const SESSION_SCOPES = Object.freeze(["node", "discuss"]);
67
-
68
- // Gateway WS carries every wire kind except run.created (dashboard-only;
69
- // the WS sink skips it).
70
-
71
- // Dashboard-only kinds (served over the dashboard bus/SSE, never gateway
72
- // WS) — read by the catalog publisher, nothing else should.
73
- const DASHBOARD_ONLY = Object.freeze(["run.created"]);
74
-
75
- export function isWireKind(kind) {
76
- return WIRE_KINDS.includes(kind);
77
- }
78
-
79
- export function isReservedKind(kind) {
80
- return RESERVED_KINDS.includes(kind);
81
- }
82
-
83
- // Throws on unknown/reserved kinds and missing fields. Returns the
84
- // payload untouched — validation only, no normalization.
85
- export function assertPayload(kind, payload) {
86
- if (isReservedKind(kind)) {
87
- throw new Error(`event kind reserved (no Phase-1 producer): ${kind}`);
88
- }
89
- const required = REQUIRED_FIELDS[kind];
90
- if (!required) {
91
- throw new Error(`unknown event kind: ${kind}`);
92
- }
93
- if (payload === null || typeof payload !== "object") {
94
- throw new Error(`event payload must be an object: ${kind}`);
95
- }
96
- for (const field of required) {
97
- if (payload[field] === undefined || payload[field] === null) {
98
- throw new Error(`event ${kind} missing required field: ${field}`);
99
- }
100
- }
101
- if (kind.startsWith("session.transcript.") && !SESSION_SCOPES.includes(payload.scope)) {
102
- throw new Error(`event ${kind} requires scope ${SESSION_SCOPES.join("|")}`);
103
- }
104
- if (kind === "session.transcript.completed" && payload.scope === "node" && payload.nodeId == null) {
105
- throw new Error(`event ${kind} scope=node requires nodeId`);
106
- }
107
- if (kind === "session.transcript.completed" && payload.scope === "discuss" && payload.turnId == null) {
108
- throw new Error(`event ${kind} scope=discuss requires turnId`);
109
- }
110
- return payload;
111
- }
112
-
113
- // True for kinds served over the dashboard bus/SSE only — the gateway WS
114
- // sink skips them. Unknown kinds throw — never route an unmapped name.
115
- export function isDashboardOnly(kind) {
116
- if (!isWireKind(kind)) throw new Error(`unknown event kind: ${kind}`);
117
- return DASHBOARD_ONLY.includes(kind);
118
- }
@@ -1,74 +0,0 @@
1
- // serve-ws.js — dumb event pipe over WebSocket. Tails the in-process push
2
- // stream (events/stream.js) and broadcasts every appended entry to all
3
- // connected clients, verbatim. No interpretation, no filtering, no notify
4
- // logic: a gate opening is just another entry, and what it means is the
5
- // subscriber's decision. Framing and client hygiene live in
6
- // events/ws-frame.js (shared with the dashboard's live endpoint).
7
- //
8
- // Wire protocol (JSON text frames, server → client only):
9
- // {"type":"welcome","lastEventId":N} on connect (history length)
10
- // {"type":"history","events":[...]} backlog, oldest-first, capped
11
- // {"type":"event","event":{...entry...}} one per appended entry, in order
12
- // Clients track their own seen state: reconnecting replays history, so a
13
- // missed frame costs a re-read, never a lost event. No auth in-process —
14
- // bind localhost, or put a bearer-checked reverse proxy in front.
15
-
16
- import http from "node:http";
17
- import { attachWsEndpoint, encodeWsFrame } from "./ws-frame.js";
18
-
19
- export const DEFAULT_WS_PORT = 38294;
20
- export const HISTORY_LIMIT = 200;
21
-
22
- export function startWsServer({ stream, port = null, host = "127.0.0.1", historyLimit = null, logger } = {}) {
23
- if (!stream || typeof stream.subscribe !== "function" || typeof stream.history !== "function") {
24
- throw new Error("manifold: serve-ws requires an events/stream.js stream (subscribe + history)");
25
- }
26
- const listenPort = port ?? DEFAULT_WS_PORT;
27
- const histLimit = historyLimit ?? HISTORY_LIMIT;
28
-
29
- const server = http.createServer((req, res) => {
30
- res.writeHead(404, { "content-type": "text/plain" });
31
- res.end("manifold events: websocket only — connect with an Upgrade request\n");
32
- });
33
-
34
- // Every upgrade target is the event door — path routing is the reverse
35
- // proxy's job for this transport.
36
- const door = attachWsEndpoint(server, {
37
- match: () => true,
38
- onConnect: (socket) => {
39
- logger?.info?.(`manifold: ws subscriber connected (${door.clientCount()} total)`);
40
- const history = stream.history({ limit: histLimit });
41
- socket.write(encodeWsFrame(JSON.stringify({ type: "welcome", lastEventId: history.length })));
42
- socket.write(encodeWsFrame(JSON.stringify({ type: "history", events: history })));
43
- },
44
- logger,
45
- });
46
-
47
- const unsubscribe = stream.subscribe((entry) => {
48
- try {
49
- door.broadcast({ type: "event", event: entry });
50
- } catch (err) {
51
- logger?.warn?.(`manifold: ws broadcast failed: ${err?.message ?? err}`);
52
- }
53
- });
54
-
55
- return new Promise((resolve, reject) => {
56
- server.once("error", (err) => {
57
- try { unsubscribe(); } catch {}
58
- reject(err);
59
- });
60
- server.listen(listenPort, host, () => {
61
- resolve({
62
- port: server.address().port,
63
- host,
64
- clients: () => door.clientCount(),
65
- close: () =>
66
- new Promise((res) => {
67
- try { unsubscribe(); } catch {}
68
- door.close();
69
- server.close(() => res());
70
- }),
71
- });
72
- });
73
- });
74
- }
package/events/stream.js DELETED
@@ -1,35 +0,0 @@
1
- // events/stream.js — in-process push-event stream for subscribers that live
2
- // in this process (adapter wake routes, dashboard relays). Append-only
3
- // history plus fan-out subscribe; delivery faults never throw into the
4
- // producer — a dead subscriber must not fail the dispatch that produced
5
- // the event.
6
- export function createEventStream({ maxHistory = 200, logger = null } = {}) {
7
- const history = [];
8
- const subscribers = new Set();
9
-
10
- function append(event) {
11
- if (!event || typeof event !== "object") return null;
12
- const entry = { ...event, at: event.at ?? Date.now() };
13
- history.push(entry);
14
- while (history.length > maxHistory) history.shift();
15
- for (const fn of [...subscribers]) {
16
- try { fn(entry); } catch (err) {
17
- try { logger?.warn?.(`events/stream: subscriber failed: ${err?.message ?? err}`); } catch {}
18
- }
19
- }
20
- return entry;
21
- }
22
-
23
- function subscribe(fn) {
24
- if (typeof fn !== "function") throw new Error("events/stream: subscribe requires a function");
25
- subscribers.add(fn);
26
- return () => subscribers.delete(fn);
27
- }
28
-
29
- function getHistory({ event = null, limit = maxHistory } = {}) {
30
- const rows = event ? history.filter((e) => e.event === event) : [...history];
31
- return rows.slice(-Math.max(0, limit));
32
- }
33
-
34
- return { append, subscribe, history: getHistory };
35
- }
@@ -1,257 +0,0 @@
1
- // events/ws-dialog.js — message transport into an open discuss session,
2
- // over the harness's own WebSocket connection.
3
- //
4
- // The parrot channel's HTTP route and the gateway discuss route both
5
- // implemented this same turn lifecycle (validate → 202+turnId → persist
6
- // the operator message → stamp the session → run the turn → persist the
7
- // reply → emit the transcript). This door is that lifecycle on a socket:
8
- // one connection carries outbound turns and inbound replies, so any
9
- // harness client needs no channel plugin and no HTTP round-trips.
10
- //
11
- // Wire protocol, client → server (JSON text frames):
12
- // {"type":"message","id":<client-echo?>,flowId,itemId?,text}
13
- // Wire protocol, server → client:
14
- // {"type":"ack","id"?,turnId}
15
- // {"type":"reply","id"?,turnId,text,proposedDraft?,proposedMeta?,durationMs?}
16
- // {"type":"error","id"?,turnId?,code,error?}
17
- // Connect with ?bearer=<MANIFOLD_BEARER> (timing-safe compare, fail-closed
18
- // when the env is unset — same posture as routes/bearer-auth.js, but from
19
- // the query string because WebSocket clients cannot set headers).
20
- // Bearer secrets travel in URLs: prefer wss:// and note proxy access logs.
21
- //
22
- // The turn itself runs through injected runTurn (the adapter closes over
23
- // invokeElevated → /manifold/_elev/discuss, the same elevation the HTTP
24
- // route uses). No pending files: the old discuss-turns markers were
25
- // write-only, and discussStore's turn_status is the live record.
26
- import http from "node:http";
27
- import crypto from "node:crypto";
28
- import { randomUUID } from "node:crypto";
29
- import { attachWsEndpoint, encodeWsFrame } from "./ws-frame.js";
30
- import { buildDiscussPrompt } from "../routes/discuss.js";
31
-
32
- const MAX_MESSAGE_BYTES = 1024 * 1024;
33
- export const DEFAULT_DIALOG_PORT = 38295;
34
-
35
- function tokensEqual(a, b) {
36
- const ab = Buffer.from(a, "utf8");
37
- const bb = Buffer.from(b, "utf8");
38
- if (ab.length !== bb.length) {
39
- crypto.timingSafeEqual(ab, ab); // burn the cycles to keep timing flat
40
- return false;
41
- }
42
- return crypto.timingSafeEqual(ab, bb);
43
- }
44
-
45
- let warnedNoSecret = false;
46
-
47
- function checkBearer(bearer, logger) {
48
- if (typeof bearer !== "string" || bearer === "") return { ok: false, reason: "no-bearer" };
49
- const secret = process.env.MANIFOLD_BEARER ?? "";
50
- if (!secret) {
51
- if (!warnedNoSecret) {
52
- warnedNoSecret = true;
53
- try { logger?.warn?.("manifold: dialog door MANIFOLD_BEARER unset — every connect fails closed"); } catch {}
54
- }
55
- return { ok: false, reason: "no-secret" };
56
- }
57
- if (!tokensEqual(bearer, secret)) return { ok: false, reason: "no-match" };
58
- return { ok: true };
59
- }
60
-
61
- function send(socket, obj) {
62
- try {
63
- if (!socket.destroyed && socket.writable) socket.write(encodeWsFrame(JSON.stringify(obj)));
64
- } catch { /* client gone; cleanup on close */ }
65
- }
66
-
67
- export function createDialogServer({
68
- port = null,
69
- host = "127.0.0.1",
70
- path = "/api/dialog",
71
- readFlow,
72
- resolveTarget,
73
- runTurn,
74
- discussStore,
75
- sessionIndex,
76
- recordLineage = null,
77
- runStore = null,
78
- nodeExecutionsStore = null,
79
- emit,
80
- logger = null,
81
- } = {}) {
82
- if (typeof readFlow !== "function") throw new Error("ws-dialog: readFlow(flowId) is required");
83
- if (typeof resolveTarget !== "function") throw new Error("ws-dialog: resolveTarget(flow, itemId) is required");
84
- if (typeof runTurn !== "function") throw new Error("ws-dialog: runTurn(turn) is required");
85
- if (!discussStore) throw new Error("ws-dialog: discussStore is required");
86
- if (!sessionIndex?.recordThread) throw new Error("ws-dialog: sessionIndex.recordThread is required");
87
- const listenPort = port ?? DEFAULT_DIALOG_PORT;
88
- let server = null;
89
- let door = null;
90
- let running = false;
91
-
92
- async function handleMessage(socket, raw) {
93
- let msg = null;
94
- try {
95
- if (typeof raw !== "string" || raw.length > MAX_MESSAGE_BYTES) throw new Error("too_large");
96
- msg = JSON.parse(raw);
97
- } catch {
98
- send(socket, { type: "error", code: "invalid_frame", error: "message must be JSON under 1MB" });
99
- return;
100
- }
101
- if (!msg || msg.type !== "message") {
102
- send(socket, { type: "error", id: msg?.id ?? null, code: "unsupported_type", error: "only {type:\"message\", flowId, text} is accepted" });
103
- return;
104
- }
105
- const flowId = typeof msg.flowId === "string" ? msg.flowId.trim() : "";
106
- const itemId = typeof msg.itemId === "string" ? msg.itemId : null;
107
- const text = typeof msg.text === "string" ? msg.text.trim() : "";
108
- if (!flowId || !text) {
109
- send(socket, { type: "error", id: msg.id ?? null, code: "invalid_body", error: "flowId and text are required" });
110
- return;
111
- }
112
- let flow = null;
113
- try {
114
- flow = await readFlow(flowId);
115
- } catch (err) {
116
- logger?.warn?.(`manifold dialog: flow read failed: ${err?.message ?? err}`);
117
- }
118
- if (!flow) {
119
- send(socket, { type: "error", id: msg.id ?? null, code: "flow_not_found", error: `flow not found: ${flowId}` });
120
- return;
121
- }
122
- const target = await resolveTarget(flow, itemId);
123
- if (!target?.ok) {
124
- send(socket, { type: "error", id: msg.id ?? null, code: target?.code ?? "no_target", error: target?.error ?? "no discuss target" });
125
- return;
126
- }
127
- const systemPrompt = await buildDiscussPrompt({ flow, target, runStore, nodeExecutionsStore, logger });
128
- const turnId = randomUUID();
129
- const flowIdShort = flow.flowId.slice(0, 8);
130
- send(socket, { type: "ack", id: msg.id ?? null, turnId });
131
-
132
- try {
133
- await discussStore.appendMessage(flow.flowId, target.itemId, { turnId, role: "operator", text, ts: Date.now() });
134
- await discussStore.setTurnStatus(flow.flowId, target.itemId, "running", turnId);
135
- } catch (err) {
136
- logger?.warn?.(`manifold dialog: discuss-store write (operator msg): ${err?.message ?? err}`);
137
- }
138
- if (target.needsStamp) {
139
- try {
140
- await sessionIndex.recordThread({
141
- flowId: flow.flowId, gateNodeId: target.gateNodeId ?? "",
142
- itemId: target.itemId, agentId: target.agent ?? null, session: target.session,
143
- });
144
- await recordLineage?.({
145
- childSession: target.session, flowId: flow.flowId, nodeId: target.nodeId?.toLowerCase?.() ?? null,
146
- itemKey: target.itemId, pipelineId: flow.stateJson?.pipelineId ?? null, agentId: target.agent ?? null,
147
- kind: "flow-gate-discuss", source: "spawn_hook",
148
- });
149
- } catch (err) {
150
- logger?.warn?.(`manifold dialog: stamp failed: ${err?.message ?? err}`);
151
- }
152
- }
153
-
154
- const startedMs = Date.now();
155
- let result;
156
- try {
157
- result = await runTurn({
158
- flowId: flow.flowId, session: target.session, agentId: target.agent,
159
- gateNodeId: target.gateNodeId,
160
- modelOverride: target.modelOverride ?? null, itemId: target.itemId, text,
161
- systemPrompt: typeof systemPrompt === "string" ? systemPrompt : null,
162
- });
163
- } catch (err) {
164
- result = { ok: false, code: "dispatch_failed", error: err?.message ?? String(err) };
165
- }
166
- const durationMs = Date.now() - startedMs;
167
- if (result?.ok) {
168
- try {
169
- await discussStore.appendMessage(flow.flowId, target.itemId, {
170
- turnId, role: "agent", text: result.reply ?? "",
171
- proposedDraft: result.proposedDraft ?? null, proposedMeta: result.proposedMeta ?? null, ts: Date.now(),
172
- });
173
- await discussStore.clearPending(flow.flowId, target.itemId);
174
- await discussStore.setTurnStatus(flow.flowId, target.itemId, "idle", null);
175
- } catch (err) {
176
- logger?.warn?.(`manifold dialog: discuss-store write (agent reply): ${err?.message ?? err}`);
177
- }
178
- try {
179
- await emit?.("session.transcript.completed", {
180
- scope: "discuss", turnId, flowId: flow.flowId, itemId: target.itemId,
181
- text: result.reply ?? "", durationMs, emittedAtMs: Date.now(),
182
- });
183
- } catch {}
184
- send(socket, {
185
- type: "reply", id: msg.id ?? null, turnId, text: result.reply ?? "",
186
- ...(result.proposedDraft != null ? { proposedDraft: result.proposedDraft } : {}),
187
- ...(result.proposedMeta != null ? { proposedMeta: result.proposedMeta } : {}),
188
- durationMs,
189
- });
190
- } else {
191
- const errText = result?.error ?? result?.code ?? "turn failed";
192
- try {
193
- await discussStore.stampMessageError(flow.flowId, target.itemId, turnId, errText);
194
- await discussStore.setTurnStatus(flow.flowId, target.itemId, "error", null);
195
- } catch {}
196
- try {
197
- await emit?.("session.transcript.failed", {
198
- scope: "discuss", turnId, flowId: flow.flowId, itemId: target.itemId, error: errText,
199
- });
200
- } catch {}
201
- send(socket, { type: "error", id: msg.id ?? null, turnId, code: result?.code ?? "turn_failed", error: errText });
202
- }
203
- logger?.info?.(`manifold dialog [${flowIdShort}/${turnId.slice(0, 8)}]: turn ${result?.ok ? "completed" : "failed"} in ${durationMs}ms`);
204
- }
205
-
206
- return {
207
- async start() {
208
- if (running) return;
209
- server = http.createServer((req, res) => {
210
- res.writeHead(404, { "content-type": "text/plain" });
211
- res.end("manifold dialog: websocket only — connect with an Upgrade request\n");
212
- });
213
- door = attachWsEndpoint(server, {
214
- match: (pathname) => pathname === path,
215
- onConnect: (socket, req) => {
216
- let bearer = "";
217
- try {
218
- bearer = new URL(req.url ?? "/", "http://localhost").searchParams.get("bearer") ?? "";
219
- } catch { /* keep "" */ }
220
- const authz = checkBearer(bearer, logger);
221
- if (!authz.ok) {
222
- send(socket, { type: "error", code: "unauthorized", error: authz.reason });
223
- socket.destroy();
224
- return null;
225
- }
226
- return null;
227
- },
228
- onMessage: (socket, text) => {
229
- handleMessage(socket, text).catch((err) => {
230
- logger?.warn?.(`manifold dialog: turn failed: ${err?.message ?? err}`);
231
- send(socket, { type: "error", code: "turn_failed", error: err?.message ?? String(err) });
232
- });
233
- },
234
- logger,
235
- });
236
- await new Promise((resolve, reject) => {
237
- server.once("error", (err) => { server = null; reject(err); });
238
- server.listen(listenPort, host, () => resolve());
239
- });
240
- running = true;
241
- logger?.info?.(`manifold dialog: listening on ws://${host}:${server.address().port}${path} (bearer-guarded)`);
242
- },
243
- async stop() {
244
- running = false;
245
- try { door?.close(); } catch {}
246
- door = null;
247
- if (server) {
248
- await new Promise((res) => server.close(() => res()));
249
- server = null;
250
- }
251
- },
252
- // Test seam: drive one turn without a socket.
253
- _handleMessage: handleMessage,
254
- clients: () => door?.clientCount?.() ?? 0,
255
- url() { return `ws://${host}:${server ? server.address().port : listenPort}${path}`; },
256
- };
257
- }
@@ -1,194 +0,0 @@
1
- // events/ws-frame.js — shared server-side WebSocket framing for manifold's
2
- // loopback event servers (events/serve-ws.js, dashboard/server.js).
3
- //
4
- // One door, two rooms: the handshake accept, server→client text frames, and
5
- // client-frame hygiene (ping→pong, close→destroy, everything else consumed
6
- // and ignored) live here so both servers behave identically. No stream,
7
- // store, or routing coupling — attachWsEndpoint only moves bytes for
8
- // sockets whose upgrade URL matches, and hands each socket to onConnect
9
- // for the server's own hello frames.
10
- import crypto from "node:crypto";
11
-
12
- const WS_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
13
-
14
- export function wsAcceptKey(key) {
15
- return crypto.createHash("sha1").update(key + WS_GUID).digest("base64");
16
- }
17
-
18
- // Server → client frames are never masked (RFC 6455 §5.3).
19
- export function encodeWsFrame(text) {
20
- const payload = Buffer.from(text, "utf8");
21
- const len = payload.length;
22
- let header;
23
- if (len < 126) {
24
- header = Buffer.from([0x81, len]);
25
- } else if (len < 65536) {
26
- header = Buffer.alloc(4);
27
- header[0] = 0x81;
28
- header[1] = 126;
29
- header.writeUInt16BE(len, 2);
30
- } else {
31
- header = Buffer.alloc(10);
32
- header[0] = 0x81;
33
- header[1] = 127;
34
- header.writeBigUInt64BE(BigInt(len), 2);
35
- }
36
- return Buffer.concat([header, payload]);
37
- }
38
-
39
- // Attach a server→client event door to an existing http server. `match` is
40
- // the server's own path predicate (dashboard strips its /graph prefix
41
- // before matching; serve-ws accepts every upgrade target).
42
- //
43
- // onConnect(socket, req) runs after the 101 response: send hello frames
44
- // there and optionally return a tag stored for broadcast filtering.
45
- // broadcast(obj, visibleTo) encodes once and writes to every open socket
46
- // whose tag passes — untagged sockets always receive.
47
- export function attachWsEndpoint(server, { match, onConnect = null, onMessage = null, logger = null } = {}) {
48
- if (typeof match !== "function") throw new Error("ws-frame: attachWsEndpoint requires a match(pathname) predicate");
49
- const clients = new Set();
50
- const tags = new Map();
51
-
52
- function drop(socket) {
53
- clients.delete(socket);
54
- tags.delete(socket);
55
- }
56
-
57
- function broadcast(obj, visibleTo = null) {
58
- const frame = encodeWsFrame(JSON.stringify(obj));
59
- for (const sock of [...clients]) {
60
- if (sock.destroyed || !sock.writable) {
61
- drop(sock);
62
- continue;
63
- }
64
- try {
65
- if (visibleTo && !visibleTo(tags.get(sock) ?? null)) continue;
66
- sock.write(frame);
67
- } catch {
68
- drop(sock);
69
- }
70
- }
71
- }
72
-
73
- // Client frames are only ever interesting as close/ping — unless the
74
- // endpoint takes messages, in which case complete text messages are
75
- // delivered to onMessage and everything else is consumed and ignored.
76
- // Mask bit respected when skipping payloads.
77
- function wireSocket(socket) {
78
- let buf = Buffer.alloc(0);
79
- let textParts = null;
80
- socket.on("data", (chunk) => {
81
- buf = Buffer.concat([buf, chunk]);
82
- for (;;) {
83
- if (buf.length < 2) return;
84
- const fin = (buf[0] & 0x80) !== 0;
85
- const opcode = buf[0] & 0x0f;
86
- let len = buf[1] & 0x7f;
87
- let off = 2;
88
- if (len === 126) {
89
- if (buf.length < 4) return;
90
- len = buf.readUInt16BE(2);
91
- off = 4;
92
- } else if (len === 127) {
93
- if (buf.length < 10) return;
94
- if (buf.readUInt32BE(2) !== 0) {
95
- socket.destroy();
96
- return;
97
- }
98
- len = buf.readUInt32BE(6);
99
- off = 10;
100
- }
101
- let mask = null;
102
- if (buf[1] & 0x80) {
103
- if (buf.length < off + 4) return;
104
- mask = buf.slice(off, off + 4);
105
- off += 4;
106
- }
107
- if (buf.length < off + len) return;
108
- let payload = buf.slice(off, off + len);
109
- buf = buf.slice(off + len);
110
- if (mask) {
111
- const unmasked = Buffer.allocUnsafe(payload.length);
112
- for (let i = 0; i < payload.length; i++) unmasked[i] = payload[i] ^ mask[i % 4];
113
- payload = unmasked;
114
- }
115
- if (opcode === 0x8) {
116
- socket.destroy();
117
- return;
118
- }
119
- if (opcode === 0x9) {
120
- socket.write(Buffer.from([0x8a, 0x00]));
121
- continue;
122
- }
123
- // Text start (0x1) opens a message, continuations (0x0) extend it;
124
- // anything else (binary, reserved) is consumed and ignored. Without
125
- // an onMessage taker there is nothing to deliver to.
126
- if (onMessage && (opcode === 0x1 || (opcode === 0x0 && textParts !== null))) {
127
- if (opcode === 0x1) textParts = [];
128
- textParts.push(payload.toString("utf8"));
129
- if (fin) {
130
- const text = textParts.join("");
131
- textParts = null;
132
- try {
133
- onMessage(socket, text);
134
- } catch (err) {
135
- logger?.warn?.(`ws-frame: onMessage failed: ${err?.message ?? err}`);
136
- }
137
- }
138
- continue;
139
- }
140
- textParts = null;
141
- }
142
- });
143
- socket.on("close", () => drop(socket));
144
- socket.on("error", () => drop(socket));
145
- }
146
-
147
- function onUpgrade(req, socket) {
148
- let pathname = "/";
149
- try {
150
- pathname = new URL(req.url ?? "/", "http://localhost").pathname;
151
- } catch { /* keep "/" */ }
152
- const key = req.headers["sec-websocket-key"];
153
- if (!match(pathname) || (req.headers.upgrade || "").toLowerCase() !== "websocket" || !key) {
154
- socket.destroy();
155
- return;
156
- }
157
- socket.write(
158
- "HTTP/1.1 101 Switching Protocols\r\n" +
159
- "Upgrade: websocket\r\n" +
160
- "Connection: Upgrade\r\n" +
161
- `Sec-WebSocket-Accept: ${wsAcceptKey(key)}\r\n\r\n`
162
- );
163
- clients.add(socket);
164
- wireSocket(socket);
165
- try {
166
- const tag = onConnect?.(socket, req);
167
- if (tag && typeof tag.then === "function") {
168
- tag.then(
169
- (t) => { if (t !== undefined) tags.set(socket, t); },
170
- (err) => logger?.warn?.(`ws-frame: onConnect failed: ${err?.message ?? err}`),
171
- );
172
- } else if (tag !== undefined) {
173
- tags.set(socket, tag);
174
- }
175
- } catch (err) {
176
- logger?.warn?.(`ws-frame: onConnect failed: ${err?.message ?? err}`);
177
- }
178
- }
179
-
180
- server.on("upgrade", onUpgrade);
181
-
182
- return {
183
- broadcast,
184
- clientCount: () => clients.size,
185
- close() {
186
- server.off("upgrade", onUpgrade);
187
- for (const sock of [...clients]) {
188
- try { sock.destroy(); } catch {}
189
- }
190
- clients.clear();
191
- tags.clear();
192
- },
193
- };
194
- }