@igorjpimenta/manifold 0.0.0-stage → 2026.11.22-beta.3

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 +1583 -2
  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 +49 -4
  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,118 @@
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
+ }
@@ -0,0 +1,74 @@
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
+ }
@@ -0,0 +1,35 @@
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
+ }
@@ -0,0 +1,257 @@
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
+ }
@@ -0,0 +1,194 @@
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
+ }