@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.
- package/.migrations/ops/0000_futuristic_karma.sql +111 -0
- package/.migrations/ops/0001_low_maximus.sql +7 -0
- package/.migrations/ops/meta/0000_snapshot.json +763 -0
- package/.migrations/ops/meta/0001_snapshot.json +173 -0
- package/.migrations/ops/meta/_journal.json +20 -0
- package/.migrations/runtime/0000_real_boomer.sql +180 -0
- package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
- package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
- package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
- package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
- package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
- package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
- package/.migrations/runtime/0009_lying_shocker.sql +96 -0
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
- package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
- package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
- package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
- package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
- package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
- package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
- package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
- package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
- package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
- package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
- package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
- package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
- package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
- package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
- package/.migrations/runtime/meta/_journal.json +97 -0
- package/README.md +1584 -0
- package/RELEASING.md +89 -0
- package/agent/fleet.js +100 -0
- package/bin/cli.js +854 -0
- package/bin/manifold.js +4 -0
- package/boot/flow-revival.js +536 -0
- package/budget/index.js +495 -0
- package/budget/lib.js +59 -0
- package/dashboard/cost.js +528 -0
- package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
- package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
- package/dashboard/dist/index.html +13 -0
- package/dashboard/event-log.js +267 -0
- package/dashboard/index.html +12 -0
- package/dashboard/package-lock.json +2877 -0
- package/dashboard/package.json +37 -0
- package/dashboard/pipeline-graph.js +131 -0
- package/dashboard/postcss.config.js +6 -0
- package/dashboard/search.js +183 -0
- package/dashboard/server.js +1015 -0
- package/dashboard/src/App.tsx +42 -0
- package/dashboard/src/api/client.ts +118 -0
- package/dashboard/src/api/types.ts +510 -0
- package/dashboard/src/api/ws.tsx +112 -0
- package/dashboard/src/components/CancelButton.tsx +63 -0
- package/dashboard/src/components/ClickToCopy.tsx +68 -0
- package/dashboard/src/components/CopyButton.tsx +61 -0
- package/dashboard/src/components/DailyCostTrend.tsx +85 -0
- package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
- package/dashboard/src/components/Funnel.tsx +149 -0
- package/dashboard/src/components/GateForm.tsx +158 -0
- package/dashboard/src/components/GraphView.tsx +302 -0
- package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
- package/dashboard/src/components/Histogram.tsx +292 -0
- package/dashboard/src/components/Layout.tsx +28 -0
- package/dashboard/src/components/NeedsAttention.tsx +98 -0
- package/dashboard/src/components/NodeContextCard.tsx +132 -0
- package/dashboard/src/components/PathTrail.tsx +35 -0
- package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
- package/dashboard/src/components/RerunButton.tsx +46 -0
- package/dashboard/src/components/RunComparison.tsx +53 -0
- package/dashboard/src/components/Sparkline.tsx +94 -0
- package/dashboard/src/components/StatStrip.tsx +147 -0
- package/dashboard/src/components/StateTree.tsx +84 -0
- package/dashboard/src/components/StatusBadge.tsx +18 -0
- package/dashboard/src/components/SubflowPathPills.tsx +38 -0
- package/dashboard/src/components/SubflowSummary.tsx +24 -0
- package/dashboard/src/components/SubflowsCard.tsx +327 -0
- package/dashboard/src/components/TodayStrip.tsx +86 -0
- package/dashboard/src/components/TranscriptPane.tsx +99 -0
- package/dashboard/src/components/TranscriptRow.tsx +69 -0
- package/dashboard/src/components/ui/badge.tsx +38 -0
- package/dashboard/src/components/ui/button.tsx +28 -0
- package/dashboard/src/components/ui/card.tsx +42 -0
- package/dashboard/src/components/ui/skeleton.tsx +9 -0
- package/dashboard/src/components/ui/table.tsx +62 -0
- package/dashboard/src/index.css +26 -0
- package/dashboard/src/lib/anomaly.ts +42 -0
- package/dashboard/src/lib/format.ts +30 -0
- package/dashboard/src/lib/step-history.ts +13 -0
- package/dashboard/src/lib/subflow.ts +154 -0
- package/dashboard/src/lib/use-click-outside.ts +28 -0
- package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
- package/dashboard/src/lib/utils.ts +6 -0
- package/dashboard/src/main.tsx +10 -0
- package/dashboard/src/types/shims.d.ts +4 -0
- package/dashboard/src/views/Home.tsx +478 -0
- package/dashboard/src/views/PipelineDetail.tsx +1185 -0
- package/dashboard/src/views/PipelinesList.tsx +111 -0
- package/dashboard/src/views/RunDetail.tsx +248 -0
- package/dashboard/src/views/Search.tsx +196 -0
- package/dashboard/stats.js +668 -0
- package/dashboard/tailwind.config.ts +43 -0
- package/dashboard/tree-stats.js +12 -0
- package/dashboard/tsconfig.json +25 -0
- package/dashboard/views.js +231 -0
- package/dashboard/vite.config.ts +34 -0
- package/drizzle.config.js +12 -0
- package/drizzle.config.ops.js +12 -0
- package/elevation.js +116 -0
- package/engine/admission-promotion-resume.js +127 -0
- package/engine/agent-dispatch.js +64 -0
- package/engine/child-resume-trigger.js +315 -0
- package/engine/executor-budget.js +87 -0
- package/engine/executor-cancel.js +83 -0
- package/engine/executor-errors.js +122 -0
- package/engine/executor-helpers.js +254 -0
- package/engine/executor-nodes.js +292 -0
- package/engine/executor-persist.js +212 -0
- package/engine/executor-subflow-children.js +275 -0
- package/engine/executor.js +1013 -0
- package/engine/global-concurrency.js +33 -0
- package/engine/loader.js +528 -0
- package/engine/needs-resolver.js +229 -0
- package/engine/pipelines-watcher.js +133 -0
- package/engine/result-replay.js +34 -0
- package/engine/revival-messages.js +11 -0
- package/engine/spawn-child.js +55 -0
- package/engine/wait-for-flow-settlement.js +109 -0
- package/events/catalog.js +26 -0
- package/events/emit.js +44 -0
- package/events/kinds.js +118 -0
- package/events/serve-ws.js +74 -0
- package/events/stream.js +35 -0
- package/events/ws-dialog.js +257 -0
- package/events/ws-frame.js +194 -0
- package/events/ws-run-events.js +70 -0
- package/events.js +32 -0
- package/gates/gate-resolver.js +576 -0
- package/gates/paused-flows.js +55 -0
- package/graphql/columns.js +67 -0
- package/graphql/schema.js +333 -0
- package/index.js +49 -0
- package/mcp/http.js +159 -0
- package/mcp/identity.js +94 -0
- package/mcp/stdio.js +80 -0
- package/mcp/tools.js +245 -0
- package/mcp/wire-ops.js +178 -0
- package/package.json +51 -0
- package/routes/bearer-auth.js +57 -0
- package/routes/discuss-audio.js +87 -0
- package/routes/discuss-component.js +39 -0
- package/routes/discuss-draft.js +159 -0
- package/routes/discuss-helpers.js +20 -0
- package/routes/discuss.js +848 -0
- package/routes/failures.js +104 -0
- package/routes/graphql.js +61 -0
- package/routes/health.js +54 -0
- package/routes/operator-discuss.js +193 -0
- package/routes/operator-resume.js +32 -0
- package/routes/operator-retry.js +301 -0
- package/routes/operator.js +539 -0
- package/routes/pipelines.js +118 -0
- package/routes/runs.js +1129 -0
- package/scripts/ci/import-check.mjs +50 -0
- package/scripts/link-core-self.mjs +31 -0
- package/scripts/with-guard.sh +24 -0
- package/server/app.js +503 -0
- package/server/registry.js +53 -0
- package/server/serve.js +163 -0
- package/state/admission-queue-store.js +325 -0
- package/state/attribution-registries.js +102 -0
- package/state/batch-writer.js +162 -0
- package/state/db-guard.js +63 -0
- package/state/db-health.js +27 -0
- package/state/db.js +122 -0
- package/state/discuss-store.js +299 -0
- package/state/failure-store.js +427 -0
- package/state/migrate.js +104 -0
- package/state/node-cost.js +67 -0
- package/state/node-executions.js +775 -0
- package/state/node-health-store.js +124 -0
- package/state/ops-merge.js +93 -0
- package/state/paths.js +146 -0
- package/state/pipeline-dirs.js +99 -0
- package/state/pipeline-registry.js +449 -0
- package/state/run-store.js +697 -0
- package/state/schema.js +438 -0
- package/state/schema.ops.js +44 -0
- package/state/session-index.js +177 -0
- package/state/session-keys.js +39 -0
- package/state/state-store.js +311 -0
- package/state/subflow-state.js +126 -0
- package/state/tool-handoff.js +169 -0
- package/telemetry/budget-alert.js +353 -0
- package/telemetry/cli.js +331 -0
- package/telemetry/grammar.js +119 -0
- package/telemetry/http-budget.js +132 -0
- package/telemetry/http-cost.js +670 -0
- package/telemetry/http-helpers.js +134 -0
- package/telemetry/http-tree.js +349 -0
- package/telemetry/http.js +44 -0
- package/telemetry/index.js +307 -0
- package/telemetry/ingest.js +134 -0
- package/telemetry/labels.js +115 -0
- package/telemetry/pricer.js +77 -0
- package/telemetry/resolver.js +314 -0
- package/telemetry/store.js +147 -0
- package/utils/run-patch.js +43 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// import-check.mjs — no tracked source file may import through an absolute
|
|
3
|
+
// `file://` URL.
|
|
4
|
+
//
|
|
5
|
+
// A `file://` specifier is absolute by construction (no relative form), so an
|
|
6
|
+
// absolute one only resolves on the machine that wrote it — and in local test
|
|
7
|
+
// runs it resolves against the live working tree, where an unstaged file is
|
|
8
|
+
// indistinguishable from a committed one. CI checks out only the commit, so
|
|
9
|
+
// the check runs where the bug first becomes visible.
|
|
10
|
+
//
|
|
11
|
+
// Scans import/export/require specifier positions:
|
|
12
|
+
// import x from "file://…" | import "file://…" | import("file://…")
|
|
13
|
+
// export … from "file://…" | require("file://…")
|
|
14
|
+
// Comment-only lines are skipped so documentation can name the pattern.
|
|
15
|
+
|
|
16
|
+
import { execFileSync } from "node:child_process";
|
|
17
|
+
import { readFileSync } from "node:fs";
|
|
18
|
+
|
|
19
|
+
const SPECIFIER = /(?:\bfrom\s*|\bimport\s*\(?|\brequire\s*\()\s*[`"']file:\/\//;
|
|
20
|
+
const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*|#)/;
|
|
21
|
+
|
|
22
|
+
const files = execFileSync("git", ["ls-files", "-z", "*.js", "*.mjs", "*.cjs"], {
|
|
23
|
+
encoding: "utf8",
|
|
24
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
25
|
+
})
|
|
26
|
+
.split("\0")
|
|
27
|
+
.filter(Boolean);
|
|
28
|
+
|
|
29
|
+
const hits = [];
|
|
30
|
+
for (const rel of files) {
|
|
31
|
+
const lines = readFileSync(rel, "utf8").split("\n");
|
|
32
|
+
for (let i = 0; i < lines.length; i++) {
|
|
33
|
+
if (COMMENT_LINE.test(lines[i])) continue;
|
|
34
|
+
if (SPECIFIER.test(lines[i])) hits.push(`${rel}:${i + 1}: ${lines[i].trim()}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (hits.length) {
|
|
39
|
+
console.error(`import-check: ${hits.length} absolute file:// import specifier(s):`);
|
|
40
|
+
for (const h of hits) console.error(` ${h}`);
|
|
41
|
+
console.error(
|
|
42
|
+
"\nThese resolve only on the author's machine (and, locally, against the" +
|
|
43
|
+
" live working tree). Replace each with a relative import.",
|
|
44
|
+
);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log(
|
|
49
|
+
`import-check: ${files.length} tracked source files, 0 absolute file:// import specifiers`,
|
|
50
|
+
);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// link-core-self.mjs — make `@igorjpimenta/manifold/<path>` resolvable in a
|
|
2
|
+
// repo checkout.
|
|
3
|
+
//
|
|
4
|
+
// The adapter imports core through the published package name, which resolves
|
|
5
|
+
// from node_modules when installed. In the repo the core is the root package,
|
|
6
|
+
// and npm cannot link the root into its own node_modules as a workspace, so
|
|
7
|
+
// the link is made here. Installed consumers never run this: the dependency
|
|
8
|
+
// resolves from the registry.
|
|
9
|
+
//
|
|
10
|
+
// Idempotent; a non-writable node_modules (host install) is skipped silently.
|
|
11
|
+
|
|
12
|
+
import { existsSync, lstatSync, mkdirSync, symlinkSync, rmSync } from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
|
|
16
|
+
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
17
|
+
const scopeDir = path.join(root, "node_modules", "@igorjpimenta");
|
|
18
|
+
const link = path.join(scopeDir, "manifold");
|
|
19
|
+
|
|
20
|
+
// Installed consumers have no adapter package beside them; nothing to link.
|
|
21
|
+
if (!existsSync(path.join(root, "plugins", "openclaw", "manifold"))) process.exit(0);
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
if (existsSync(link) && !lstatSync(link).isSymbolicLink()) process.exit(0);
|
|
25
|
+
mkdirSync(scopeDir, { recursive: true });
|
|
26
|
+
if (existsSync(link)) rmSync(link, { force: true });
|
|
27
|
+
symlinkSync(root, link, "dir");
|
|
28
|
+
} catch {
|
|
29
|
+
// Read-only or absent node_modules: the dependency resolves normally when
|
|
30
|
+
// the package is installed for real.
|
|
31
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Wall-clock guard around the test suites: `with-guard.sh <cmd> [args...]`.
|
|
3
|
+
#
|
|
4
|
+
# Locally (macOS) the suites run under `taskpolicy -b` so a runaway test can't
|
|
5
|
+
# pin every core on the machine that also hosts the live gateway, plus a perl
|
|
6
|
+
# `alarm(N)` hard stop so a hung suite doesn't wedge the terminal forever.
|
|
7
|
+
#
|
|
8
|
+
# Neither taskpolicy nor a guaranteed perl exists on the Linux CI runner, so
|
|
9
|
+
# degrade: coreutils `timeout` keeps the hard stop (losing only the priority
|
|
10
|
+
# nice-ing, which is meaningless on a single-use runner), and a bare exec is
|
|
11
|
+
# the last resort. Exit status is the child's in every branch.
|
|
12
|
+
#
|
|
13
|
+
# Override the limit with TEST_GUARD_SECONDS.
|
|
14
|
+
set -e
|
|
15
|
+
|
|
16
|
+
LIMIT="${TEST_GUARD_SECONDS:-180}"
|
|
17
|
+
|
|
18
|
+
if command -v taskpolicy >/dev/null 2>&1 && command -v perl >/dev/null 2>&1; then
|
|
19
|
+
exec taskpolicy -b perl -e "alarm($LIMIT); exec @ARGV" -- "$@"
|
|
20
|
+
elif command -v timeout >/dev/null 2>&1; then
|
|
21
|
+
exec timeout "$LIMIT" "$@"
|
|
22
|
+
else
|
|
23
|
+
exec "$@"
|
|
24
|
+
fi
|
package/server/app.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// server/app.js — the manifold application, assembled outside any harness.
|
|
2
|
+
//
|
|
3
|
+
// createManifoldApp() builds every store, the executor, the pipelines watcher
|
|
4
|
+
// and revival, the HTTP routes and the dashboard, and returns them as an
|
|
5
|
+
// ordered service list. It takes one host object for the things only a
|
|
6
|
+
// harness can supply:
|
|
7
|
+
//
|
|
8
|
+
// logger structured logger ({info,warn,error})
|
|
9
|
+
// runTurn ({flowId,session,agentId,gateNodeId,modelOverride,itemId,
|
|
10
|
+
// text,systemPrompt}) => {text,runId,usage?}
|
|
11
|
+
// sessions { newDiscussSessionId(...) } — thread ids are minted by
|
|
12
|
+
// the harness, never by core
|
|
13
|
+
// invokeElevated (path, payload, opts) => {ok,status,body,error}; defaults
|
|
14
|
+
// to the local elevation loopback in elevation.js
|
|
15
|
+
// getBroadcast () => ((event, payload) => void) | null — the harness's
|
|
16
|
+
// WS fan-out; core emits canonically either way
|
|
17
|
+
// recordSpawn (entry) => void, optional lineage hook
|
|
18
|
+
//
|
|
19
|
+
// The app never touches a gateway API object, so the same code boots inside
|
|
20
|
+
// the openclaw plugin today and as a standalone `manifold serve` process.
|
|
21
|
+
|
|
22
|
+
import { createPipelinesLoader } from "../engine/loader.js";
|
|
23
|
+
import { createStateStore } from "../state/state-store.js";
|
|
24
|
+
import { createRunStore } from "../state/run-store.js";
|
|
25
|
+
import { getNodeExecutionsStore } from "../state/node-executions.js";
|
|
26
|
+
import { createAdmissionQueueStore } from "../state/admission-queue-store.js";
|
|
27
|
+
import { createExecutor } from "../engine/executor.js";
|
|
28
|
+
import { configureGlobalConcurrency, getGlobalConcurrencyLimit } from "../engine/global-concurrency.js";
|
|
29
|
+
import { createFailureStore } from "../state/failure-store.js";
|
|
30
|
+
import { createNodeHealthStore } from "../state/node-health-store.js";
|
|
31
|
+
import { createDiscussStore } from "../state/discuss-store.js";
|
|
32
|
+
import { createSessionIndex } from "../state/session-index.js";
|
|
33
|
+
import { createPipelineRegistryStore } from "../state/pipeline-registry.js";
|
|
34
|
+
import { checkDb } from "../state/db-health.js";
|
|
35
|
+
import { writeCorruptionAlert } from "../state/db-guard.js";
|
|
36
|
+
import { createGateResolver } from "../gates/gate-resolver.js";
|
|
37
|
+
import { createEventBus } from "../dashboard/event-log.js";
|
|
38
|
+
import { createDashboardServer } from "../dashboard/server.js";
|
|
39
|
+
import { costForAllPipelines } from "../dashboard/cost.js";
|
|
40
|
+
import { createPipelinesWatcher } from "../engine/pipelines-watcher.js";
|
|
41
|
+
import { flushAll as flushBatchWriter } from "../state/batch-writer.js";
|
|
42
|
+
import { invokeElevated as invokeElevatedLoopback } from "../elevation.js";
|
|
43
|
+
import { createDialogServer } from "../events/ws-dialog.js";
|
|
44
|
+
import { flowStateEmitter, gateEmitter } from "../events.js";
|
|
45
|
+
import { createEmitter } from "../events/emit.js";
|
|
46
|
+
import { createWsRunEventsListener } from "../events/ws-run-events.js";
|
|
47
|
+
import { createCatalogPublisher } from "../events/catalog.js";
|
|
48
|
+
import { registerDiscussHandlers } from "../routes/discuss.js";
|
|
49
|
+
import { registerRunRoutes } from "../routes/runs.js";
|
|
50
|
+
import { registerOperatorRoutes } from "../routes/operator.js";
|
|
51
|
+
import { resumeFlowFromGate } from "../routes/operator-resume.js";
|
|
52
|
+
import { registerFailureRoutes } from "../routes/failures.js";
|
|
53
|
+
import { registerGraphqlRoutes } from "../routes/graphql.js";
|
|
54
|
+
import { registerHealthRoutes } from "../routes/health.js";
|
|
55
|
+
import { registerPipelineRoutes } from "../routes/pipelines.js";
|
|
56
|
+
import { registerBudgetRoutes, openStore as openBudgetStore } from "../budget/index.js";
|
|
57
|
+
import { registerTelemetry } from "../telemetry/index.js";
|
|
58
|
+
import { reviveOrphanFlows, reconcileStuckAdmissions } from "../boot/flow-revival.js";
|
|
59
|
+
import { resolveStorePaths, ensureMigrated, stateFile } from "../state/paths.js";
|
|
60
|
+
import { setStateFile } from "../gates/paused-flows.js";
|
|
61
|
+
|
|
62
|
+
// reviveOrphanFlows only runs once, at startup — a stuck-promotion leak
|
|
63
|
+
// occurring hours into a long uptime would otherwise go undetected until the
|
|
64
|
+
// next restart. 5 minutes matches this codebase's other steady-state probe
|
|
65
|
+
// cadences (e.g. model-circuit-breaker's own recovery probe).
|
|
66
|
+
const STUCK_ADMISSION_RECONCILE_INTERVAL_MS = 5 * 60_000;
|
|
67
|
+
|
|
68
|
+
export const DEFAULT_OWNER_SESSION_KEY = "agent:main:explicit:manifold-owner";
|
|
69
|
+
export const DEFAULT_DASHBOARD_PORT = 7430;
|
|
70
|
+
export const DEFAULT_DASHBOARD_HOST = "127.0.0.1";
|
|
71
|
+
export const DEFAULT_DIALOG_PORT = 38295;
|
|
72
|
+
|
|
73
|
+
export function createManifoldApp({ host, config = {} } = {}) {
|
|
74
|
+
if (!host?.logger) throw new Error("createManifoldApp: host.logger is required");
|
|
75
|
+
if (typeof host.runTurn !== "function") throw new Error("createManifoldApp: host.runTurn is required");
|
|
76
|
+
const logger = host.logger;
|
|
77
|
+
const invokeElevated = host.invokeElevated ?? invokeElevatedLoopback;
|
|
78
|
+
const getBroadcast = host.getBroadcast ?? (() => null);
|
|
79
|
+
|
|
80
|
+
const storePaths = resolveStorePaths();
|
|
81
|
+
ensureMigrated({ dataDir: storePaths.dataDir, logger });
|
|
82
|
+
setStateFile(stateFile(storePaths.dataDir));
|
|
83
|
+
|
|
84
|
+
const pipelinesDir = config.pipelinesDir;
|
|
85
|
+
if (!pipelinesDir) throw new Error("createManifoldApp: config.pipelinesDir is required");
|
|
86
|
+
const ownerSessionKey = config.ownerSessionKey || DEFAULT_OWNER_SESSION_KEY;
|
|
87
|
+
const discussDefaultAgent = typeof config.discussDefaultAgent === "string" ? config.discussDefaultAgent : null;
|
|
88
|
+
const dashboardEnabled = config.dashboardEnabled !== false;
|
|
89
|
+
const dashboardPort = Number.isInteger(config.dashboardPort) ? config.dashboardPort : DEFAULT_DASHBOARD_PORT;
|
|
90
|
+
const dashboardHost = config.dashboardHost || DEFAULT_DASHBOARD_HOST;
|
|
91
|
+
|
|
92
|
+
configureGlobalConcurrency({ maxConcurrent: config.globalConcurrency });
|
|
93
|
+
|
|
94
|
+
const pipelinesRef = { value: new Map() };
|
|
95
|
+
const runtimeDbPath = storePaths.runtimeDb;
|
|
96
|
+
|
|
97
|
+
const runStore = createRunStore({ logger, dbPath: runtimeDbPath });
|
|
98
|
+
const nodeExecutionsStore = getNodeExecutionsStore(runtimeDbPath);
|
|
99
|
+
const admissionQueueStore = createAdmissionQueueStore({
|
|
100
|
+
dbPath: runtimeDbPath, getLimit: getGlobalConcurrencyLimit,
|
|
101
|
+
});
|
|
102
|
+
// resumeParentRef.resume is set to executor.resume once the executor
|
|
103
|
+
// exists below — store construction has to come before it, so the
|
|
104
|
+
// resume-trigger (state-store.js's finish/skip/fail/cancel) reads through
|
|
105
|
+
// this mutable box rather than a captured closure.
|
|
106
|
+
const resumeParentRef = { resume: null };
|
|
107
|
+
const emitCanonical = createEmitter({ getBroadcast, logger }).emit;
|
|
108
|
+
const failureStore = createFailureStore({ logger, dbPath: runtimeDbPath, nodeExecutionsStore, emit: emitCanonical });
|
|
109
|
+
const pipelineRegistryStore = createPipelineRegistryStore(runtimeDbPath);
|
|
110
|
+
const store = createStateStore({ logger }, ownerSessionKey, {
|
|
111
|
+
runStore, nodeExecutionsStore, resumeParentRef, pipelinesRef, failureStore, admissionQueueStore, logger,
|
|
112
|
+
});
|
|
113
|
+
// Memoized single load (engine/loader.js's createPipelinesLoader) — every
|
|
114
|
+
// caller (the loader service, flow-revival, and executor.js's per-flow
|
|
115
|
+
// fallback) awaits this SAME promise instead of each independently
|
|
116
|
+
// checking `pipelinesRef.value.size > 0` and potentially kicking off its
|
|
117
|
+
// own redundant loadPipelines() pass.
|
|
118
|
+
const ensurePipelinesLoaded = createPipelinesLoader(pipelinesDir, logger, pipelinesRef);
|
|
119
|
+
// createNodeHealthStore is async (opens sqlite + runs migrations) but app
|
|
120
|
+
// construction must be synchronous. Same lazy Promise-proxy pattern as
|
|
121
|
+
// sessionIndex below.
|
|
122
|
+
const _nodeHealthPromise = createNodeHealthStore({ logger, dbPath: runtimeDbPath });
|
|
123
|
+
const nodeHealthStore = {
|
|
124
|
+
isBlocked(p, n) { return _nodeHealthPromise.then(s => s.isBlocked(p, n)); },
|
|
125
|
+
markUnhealthy(p, n, info) { return _nodeHealthPromise.then(s => s.markUnhealthy(p, n, info)); },
|
|
126
|
+
reset(p, n, opts) { return _nodeHealthPromise.then(s => s.reset(p, n, opts)); },
|
|
127
|
+
list(p, opts) { return _nodeHealthPromise.then(s => s.list(p, opts)); },
|
|
128
|
+
close() { _nodeHealthPromise.then(s => s.close?.()).catch(() => {}); },
|
|
129
|
+
};
|
|
130
|
+
const discussStore = createDiscussStore({ logger, dbPath: runtimeDbPath });
|
|
131
|
+
const _sessionIndexPromise = createSessionIndex({ logger, dbPath: runtimeDbPath });
|
|
132
|
+
const sessionIndex = {
|
|
133
|
+
// Harness-owned session ids pass through verbatim — stored and read with
|
|
134
|
+
// no edge translation on either path.
|
|
135
|
+
record(data) { return _sessionIndexPromise.then((si) => si.record({ ...data })); },
|
|
136
|
+
recordThread(data) { return _sessionIndexPromise.then((si) => si.recordThread({ ...data })); },
|
|
137
|
+
findThread(flowId, gateNodeId, itemId) { return _sessionIndexPromise.then(si => si.findThread(flowId, gateNodeId, itemId)); },
|
|
138
|
+
hasSessionForNode(flowId, nodeId) { return _sessionIndexPromise.then(si => si.hasSessionForNode(flowId, nodeId)); },
|
|
139
|
+
flowIdForSessionKey(session) { return _sessionIndexPromise.then(si => si.flowIdForSessionKey(session)); },
|
|
140
|
+
close() { _sessionIndexPromise.then(si => si.close?.()).catch(() => {}); },
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
let dashboardBusRef = null;
|
|
144
|
+
const publishCatalogChanged = createCatalogPublisher({
|
|
145
|
+
emit: emitCanonical,
|
|
146
|
+
getDashboardBroadcast: () => (dashboardBusRef ? (e) => dashboardBusRef.broadcast(e) : null),
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const executor = createExecutor({
|
|
150
|
+
config: host.config ?? null,
|
|
151
|
+
pipelinesDir,
|
|
152
|
+
store,
|
|
153
|
+
pipelinesRef,
|
|
154
|
+
ensurePipelinesLoaded,
|
|
155
|
+
logger,
|
|
156
|
+
failureStore,
|
|
157
|
+
nodeHealthStore,
|
|
158
|
+
sessionIndex,
|
|
159
|
+
runStore,
|
|
160
|
+
nodeExecutionsStore,
|
|
161
|
+
admissionQueueStore,
|
|
162
|
+
// The harness owns the agent runtime: core receives a turn function,
|
|
163
|
+
// never the runtime object.
|
|
164
|
+
runTurn: host.runTurn,
|
|
165
|
+
emit: emitCanonical,
|
|
166
|
+
});
|
|
167
|
+
resumeParentRef.resume = executor.resume;
|
|
168
|
+
|
|
169
|
+
const services = [];
|
|
170
|
+
const service = (id, start, stop) => services.push({ id, start, stop });
|
|
171
|
+
|
|
172
|
+
service("manifold-loader", async () => {
|
|
173
|
+
logger.info(`manifold: loading pipelines from ${pipelinesDir}`);
|
|
174
|
+
const pipelines = await ensurePipelinesLoaded();
|
|
175
|
+
const ids = [...pipelines.keys()];
|
|
176
|
+
logger.info(`manifold: ${pipelines.size} pipeline(s) loaded${ids.length ? `: ${ids.join(", ")}` : ""}`);
|
|
177
|
+
// First reconcile has to happen here, not from the watcher below — the
|
|
178
|
+
// watcher's own first tick deliberately primes its fingerprint without
|
|
179
|
+
// reloading (this boot-time load already happened).
|
|
180
|
+
await pipelineRegistryStore.reconcilePipelineVersions(pipelinesRef.value, { logger });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const pipelinesWatcher = createPipelinesWatcher({ pipelinesDir, pipelinesRef, logger });
|
|
184
|
+
pipelinesWatcher.onReload((info) => { publishCatalogChanged(info).catch(() => {}); });
|
|
185
|
+
pipelinesWatcher.onReload(() => {
|
|
186
|
+
pipelineRegistryStore.reconcilePipelineVersions(pipelinesRef.value, { logger });
|
|
187
|
+
});
|
|
188
|
+
service("manifold-pipelines-watcher", () => pipelinesWatcher.start(), () => pipelinesWatcher.stop());
|
|
189
|
+
|
|
190
|
+
// batch-writer defers and coalesces fire-and-forget DB writes behind an
|
|
191
|
+
// in-memory queue flushed on its interval timer or a size threshold —
|
|
192
|
+
// nothing to do at boot, but stop() must flush buffered writes before the
|
|
193
|
+
// process exits.
|
|
194
|
+
service("manifold-batch-writer", async () => {}, () => flushBatchWriter());
|
|
195
|
+
|
|
196
|
+
let revivalTimer = null;
|
|
197
|
+
service("manifold-flow-revival", async (ctx) => {
|
|
198
|
+
const l = ctx?.logger ?? logger;
|
|
199
|
+
l.info("manifold revival: start");
|
|
200
|
+
// Not awaited: revival dispatches agents, and a caller's start() must
|
|
201
|
+
// return before those dispatches can complete.
|
|
202
|
+
runBootRevival(l);
|
|
203
|
+
revivalTimer = setInterval(() => {
|
|
204
|
+
reconcileStuckAdmissions({ admissionQueueStore, runStore, executor, logger: l })
|
|
205
|
+
.catch((err) => l.warn(`manifold revival: periodic stuck-admission reconcile failed: ${err?.message ?? err}`));
|
|
206
|
+
}, STUCK_ADMISSION_RECONCILE_INTERVAL_MS);
|
|
207
|
+
}, async () => {
|
|
208
|
+
if (revivalTimer) { clearInterval(revivalTimer); revivalTimer = null; }
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
async function runBootRevival(l) {
|
|
212
|
+
try {
|
|
213
|
+
// Guarantee pipelines are in memory before reviving orphan flows;
|
|
214
|
+
// without this a race between the loader and revival makes the orphan
|
|
215
|
+
// filter pass every flow and fail them with "pipeline not loaded".
|
|
216
|
+
await ensurePipelinesLoaded();
|
|
217
|
+
// Read orphan candidates from the SQL store, not store.list() — the
|
|
218
|
+
// in-process Map is empty at boot. includeChildren:true — a crashed
|
|
219
|
+
// childFlows child must be revived exactly like a crashed top-level
|
|
220
|
+
// flow.
|
|
221
|
+
const all = await runStore.list({ includeChildren: true });
|
|
222
|
+
await reviveOrphanFlows({
|
|
223
|
+
all,
|
|
224
|
+
pipelines: pipelinesRef.value,
|
|
225
|
+
store,
|
|
226
|
+
logger: l,
|
|
227
|
+
executor,
|
|
228
|
+
createCancelStore: (ownerKey) => createStateStore({ logger: l }, ownerKey, {
|
|
229
|
+
runStore, nodeExecutionsStore, resumeParentRef, pipelinesRef, failureStore, admissionQueueStore, logger: l,
|
|
230
|
+
}),
|
|
231
|
+
runStore,
|
|
232
|
+
nodeExecutionsStore,
|
|
233
|
+
sessionIndex,
|
|
234
|
+
admissionQueueStore,
|
|
235
|
+
// gateResolver is declared further below in this same closure —
|
|
236
|
+
// safe to reference here since this callback only runs once startup
|
|
237
|
+
// actually reaches this point.
|
|
238
|
+
maybeResolveFlow: gateResolver.maybeResolveFlow,
|
|
239
|
+
});
|
|
240
|
+
} catch (err) {
|
|
241
|
+
l.warn(`manifold revival: failed to enumerate orphans: ${err?.message ?? err}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const readStore = runStore;
|
|
246
|
+
// The route modules register their own services (budget/telemetry setup);
|
|
247
|
+
// they join the same ordered list the app starts and stops.
|
|
248
|
+
const routeHost = {
|
|
249
|
+
logger,
|
|
250
|
+
registerHttpRoute: host.registerHttpRoute,
|
|
251
|
+
registerService: (svc) => services.push(svc),
|
|
252
|
+
// Harness lifecycle hooks (telemetry subscribes to agent/session events).
|
|
253
|
+
// A host that has no such events accepts the registration and never
|
|
254
|
+
// fires it; the hook adapters in host.on decide what reaches core.
|
|
255
|
+
on: (event, handler) => host.on?.(event, handler),
|
|
256
|
+
// The gateway CLI lives with the harness; a standalone core is its own
|
|
257
|
+
// CLI (bin/manifold.js), so the registration is accepted and unused.
|
|
258
|
+
registerCli: (fn, meta) => host.registerCli?.(fn, meta),
|
|
259
|
+
config: host.config ?? {},
|
|
260
|
+
runtime: host.runtime ?? {},
|
|
261
|
+
};
|
|
262
|
+
const gateResolver = createGateResolver({
|
|
263
|
+
api: routeHost, store, executor, getBroadcast, readStore, runStore,
|
|
264
|
+
pipelineLookup: (pipelineId) => pipelinesRef.value.get(pipelineId),
|
|
265
|
+
admissionQueueStore,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
service("manifold-read-store", async () => {}, async () => { sessionIndex?.close?.(); });
|
|
269
|
+
|
|
270
|
+
const discussHandlers = registerDiscussHandlers({
|
|
271
|
+
api: routeHost,
|
|
272
|
+
store,
|
|
273
|
+
pipelinesRef,
|
|
274
|
+
emit: emitCanonical,
|
|
275
|
+
discussDefaultAgent,
|
|
276
|
+
invokeElevated,
|
|
277
|
+
discussStore,
|
|
278
|
+
runStore,
|
|
279
|
+
nodeExecutionsStore,
|
|
280
|
+
// Harness adapter: discuss thread ids come from here, never minted in core.
|
|
281
|
+
sessions: host.sessions,
|
|
282
|
+
sessionIndex,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
registerRunRoutes({
|
|
286
|
+
api: routeHost,
|
|
287
|
+
readStore,
|
|
288
|
+
costForAllPipelines,
|
|
289
|
+
runStore,
|
|
290
|
+
pipelinesRef,
|
|
291
|
+
pipelineRegistryStore,
|
|
292
|
+
invokeElevated,
|
|
293
|
+
discussStore,
|
|
294
|
+
failureStore,
|
|
295
|
+
sessionIndex,
|
|
296
|
+
handleDiscuss: discussHandlers.handleDiscuss,
|
|
297
|
+
handleDiscussHistory: discussHandlers.handleDiscussHistory,
|
|
298
|
+
handleDiscussApplyDraft: discussHandlers.handleDiscussApplyDraft,
|
|
299
|
+
handleDiscussAudio: discussHandlers.handleDiscussAudio,
|
|
300
|
+
handleDiscussAudioGet: discussHandlers.handleDiscussAudioGet,
|
|
301
|
+
handleDiscussAudioRetry: discussHandlers.handleDiscussAudioRetry,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
registerOperatorRoutes({
|
|
305
|
+
api: routeHost,
|
|
306
|
+
store,
|
|
307
|
+
readStore,
|
|
308
|
+
executor,
|
|
309
|
+
gateResolver,
|
|
310
|
+
failureStore,
|
|
311
|
+
pipelinesRef,
|
|
312
|
+
ensurePipelinesLoaded,
|
|
313
|
+
invokeElevated,
|
|
314
|
+
ownerSessionKey,
|
|
315
|
+
runStore,
|
|
316
|
+
admissionQueueStore,
|
|
317
|
+
resumeParentRef,
|
|
318
|
+
discussStore,
|
|
319
|
+
getBroadcast,
|
|
320
|
+
sessions: host.sessions,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
registerFailureRoutes({ api: routeHost, failureStore, invokeElevated });
|
|
324
|
+
registerGraphqlRoutes({ api: routeHost, failureStore, runStore });
|
|
325
|
+
registerHealthRoutes({ api: routeHost });
|
|
326
|
+
registerPipelineRoutes({ api: routeHost, pipelinesRef, readStore, ensurePipelinesLoaded });
|
|
327
|
+
|
|
328
|
+
registerBudgetRoutes(routeHost, {
|
|
329
|
+
getBroadcast,
|
|
330
|
+
// The app's store home, not the harness default the module carries.
|
|
331
|
+
openStore: (opts = {}) => openBudgetStore({ dbPath: storePaths.opsDb, runtimeDbPath, ...opts }),
|
|
332
|
+
// In-process resume for flow-bound budget approvals (no HTTP loopback).
|
|
333
|
+
onFlowResume: ({ flowId, nodeId, decision }) => resumeFlowFromGate({
|
|
334
|
+
store, readStore, admissionQueueStore, runStore, executor,
|
|
335
|
+
logger, flowId, nodeId, decision,
|
|
336
|
+
}),
|
|
337
|
+
});
|
|
338
|
+
registerTelemetry(routeHost, { getBroadcast, _dbPath: storePaths.opsDb });
|
|
339
|
+
|
|
340
|
+
let _dialogServer = null;
|
|
341
|
+
service("manifold-dialog", async () => {
|
|
342
|
+
_dialogServer = createDialogServer({
|
|
343
|
+
port: Number(process.env.MANIFOLD_DIALOG_PORT) || DEFAULT_DIALOG_PORT,
|
|
344
|
+
host: process.env.MANIFOLD_DIALOG_HOST ?? "127.0.0.1",
|
|
345
|
+
readFlow: (flowId) => readStore.get(flowId),
|
|
346
|
+
resolveTarget: discussHandlers.resolveDiscussTarget,
|
|
347
|
+
runTurn: async ({ flowId, session, agentId, gateNodeId, modelOverride, itemId, text, systemPrompt }) => {
|
|
348
|
+
const elev = await invokeElevated("/manifold/_elev/discuss", {
|
|
349
|
+
flowId, session, agentId, gateNodeId, modelOverride, text, itemId,
|
|
350
|
+
systemPrompt: typeof systemPrompt === "string" ? systemPrompt : null,
|
|
351
|
+
}, { timeoutMs: 360_000 });
|
|
352
|
+
let payload;
|
|
353
|
+
try { payload = JSON.parse(elev.body || "{}"); } catch { payload = {}; }
|
|
354
|
+
if (!(elev.ok && payload.ok)) {
|
|
355
|
+
return { ok: false, code: payload.code ?? "elev_failed", error: payload.error ?? payload.code ?? elev.error ?? `HTTP ${elev.status}` };
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
ok: true, reply: payload.reply ?? "", proposedDraft: payload.proposedDraft ?? null,
|
|
359
|
+
proposedMeta: payload.proposedMeta ?? null, durationMs: payload.durationMs ?? null,
|
|
360
|
+
};
|
|
361
|
+
},
|
|
362
|
+
discussStore,
|
|
363
|
+
sessionIndex,
|
|
364
|
+
recordLineage: host.recordSpawn,
|
|
365
|
+
runStore,
|
|
366
|
+
nodeExecutionsStore,
|
|
367
|
+
emit: emitCanonical,
|
|
368
|
+
logger,
|
|
369
|
+
});
|
|
370
|
+
await _dialogServer.start();
|
|
371
|
+
}, async () => {
|
|
372
|
+
try { await _dialogServer?.stop(); } catch {}
|
|
373
|
+
_dialogServer = null;
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
if (dashboardEnabled) {
|
|
377
|
+
const dashboardStore = readStore;
|
|
378
|
+
const dashboardBus = createEventBus({ store: dashboardStore, nodeExecutionsStore, logger });
|
|
379
|
+
dashboardBusRef = dashboardBus;
|
|
380
|
+
pipelinesWatcher.onReload((info) => { publishCatalogChanged(info).catch(() => {}); });
|
|
381
|
+
const dashboardServer = createDashboardServer({
|
|
382
|
+
writeStore: store,
|
|
383
|
+
readStore,
|
|
384
|
+
failureStore,
|
|
385
|
+
discussStore,
|
|
386
|
+
nodeExecutionsStore,
|
|
387
|
+
pipelinesRef,
|
|
388
|
+
pipelineRegistryStore,
|
|
389
|
+
ensurePipelinesLoaded,
|
|
390
|
+
executor,
|
|
391
|
+
eventBus: dashboardBus,
|
|
392
|
+
port: dashboardPort,
|
|
393
|
+
host: dashboardHost,
|
|
394
|
+
logger,
|
|
395
|
+
recordItemDecision: gateResolver.recordItemDecision,
|
|
396
|
+
});
|
|
397
|
+
service("manifold-dashboard", async () => {
|
|
398
|
+
dashboardBus.start();
|
|
399
|
+
try { await dashboardServer.start(); }
|
|
400
|
+
catch (err) {
|
|
401
|
+
logger.warn(`manifold dashboard: failed to start on ${dashboardHost}:${dashboardPort}: ${err?.message ?? err}`);
|
|
402
|
+
dashboardBus.stop();
|
|
403
|
+
}
|
|
404
|
+
}, async () => {
|
|
405
|
+
dashboardBus.stop();
|
|
406
|
+
await dashboardServer.stop();
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
let _wsRunEventsListener = null;
|
|
411
|
+
let _gateOpenedListener = null;
|
|
412
|
+
let _gateClosedListener = null;
|
|
413
|
+
service("manifold-ws-run-events", async () => {
|
|
414
|
+
_wsRunEventsListener = createWsRunEventsListener({
|
|
415
|
+
store,
|
|
416
|
+
nodeExecutionsStore,
|
|
417
|
+
pipelineLookup: (id) => pipelinesRef.value.get(id),
|
|
418
|
+
getBroadcast,
|
|
419
|
+
emit: emitCanonical,
|
|
420
|
+
});
|
|
421
|
+
flowStateEmitter.on("change", _wsRunEventsListener);
|
|
422
|
+
|
|
423
|
+
// Gate lifecycle goes out with its full payload (items, waitJson,
|
|
424
|
+
// stateJson) so a consumer can render the gate without a re-fetch;
|
|
425
|
+
// gate.closed tells it to drop the surface.
|
|
426
|
+
_gateOpenedListener = (payload) => {
|
|
427
|
+
try { getBroadcast()?.("manifold.gate.opened", payload); } catch {}
|
|
428
|
+
};
|
|
429
|
+
_gateClosedListener = (payload) => {
|
|
430
|
+
try { getBroadcast()?.("manifold.gate.closed", payload); } catch {}
|
|
431
|
+
};
|
|
432
|
+
gateEmitter.on("gate.opened", _gateOpenedListener);
|
|
433
|
+
gateEmitter.on("gate.closed", _gateClosedListener);
|
|
434
|
+
}, async () => {
|
|
435
|
+
if (_wsRunEventsListener) { flowStateEmitter.off("change", _wsRunEventsListener); _wsRunEventsListener = null; }
|
|
436
|
+
if (_gateOpenedListener) { gateEmitter.off("gate.opened", _gateOpenedListener); _gateOpenedListener = null; }
|
|
437
|
+
if (_gateClosedListener) { gateEmitter.off("gate.closed", _gateClosedListener); _gateClosedListener = null; }
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
let started = [];
|
|
441
|
+
async function start(ctx) {
|
|
442
|
+
started = [];
|
|
443
|
+
for (const s of services) {
|
|
444
|
+
await s.start?.(ctx);
|
|
445
|
+
started.push(s);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
async function stop(ctx) {
|
|
449
|
+
for (const s of started.slice().reverse()) {
|
|
450
|
+
try { await s.stop?.(ctx); } catch (err) { logger.warn(`manifold: stop ${s.id} failed: ${err?.message ?? err}`); }
|
|
451
|
+
}
|
|
452
|
+
started = [];
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// A blind manifold mints wrong gate decisions and drops pushes; refuse to
|
|
456
|
+
// boot on an unreadable primary instead. Checked after the stores have
|
|
457
|
+
// opened (a fresh home creates its files there), never as a missing-file
|
|
458
|
+
// false positive.
|
|
459
|
+
const dbCheck = checkDatabases(storePaths, { logger });
|
|
460
|
+
if (!dbCheck.ok) {
|
|
461
|
+
const err = new Error(`unreadable database ${dbCheck.dbPath}: ${dbCheck.error}`);
|
|
462
|
+
err.code = "db_unreadable";
|
|
463
|
+
throw err;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
services,
|
|
468
|
+
start,
|
|
469
|
+
stop,
|
|
470
|
+
executor,
|
|
471
|
+
store,
|
|
472
|
+
runStore,
|
|
473
|
+
readStore,
|
|
474
|
+
nodeExecutionsStore,
|
|
475
|
+
admissionQueueStore,
|
|
476
|
+
failureStore,
|
|
477
|
+
discussStore,
|
|
478
|
+
sessionIndex,
|
|
479
|
+
pipelineRegistryStore,
|
|
480
|
+
pipelinesRef,
|
|
481
|
+
ensurePipelinesLoaded,
|
|
482
|
+
gateResolver,
|
|
483
|
+
getDashboardBus: () => dashboardBusRef,
|
|
484
|
+
storePaths,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// checkDatabases — refuse to boot on an unreadable primary. A blind manifold
|
|
489
|
+
// mints wrong gate decisions and drops pushes; fail loudly and name the file
|
|
490
|
+
// instead. Synchronous probe: better-sqlite3 is already a dependency.
|
|
491
|
+
export function checkDatabases(storePaths, { logger = console } = {}) {
|
|
492
|
+
for (const dbPath of [storePaths.runtimeDb, storePaths.opsDb]) {
|
|
493
|
+
const check = checkDb(dbPath);
|
|
494
|
+
// Absent is not corrupt: a fresh home creates its files on first write.
|
|
495
|
+
if (!check.ok && /^missing:/.test(check.error ?? "")) continue;
|
|
496
|
+
if (!check.ok) {
|
|
497
|
+
logger.error(`manifold: refusing boot — unreadable database ${dbPath}: ${check.error}`);
|
|
498
|
+
writeCorruptionAlert({ dbPath, phase: "boot", error: check.error });
|
|
499
|
+
return { ok: false, dbPath, error: check.error };
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return { ok: true };
|
|
503
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// registry.js — the HTTP route registry the route modules already speak.
|
|
2
|
+
//
|
|
3
|
+
// Route modules call `api.registerHttpRoute({ path, auth, match, handler })`
|
|
4
|
+
// where handler(req, res) ends the response and returns true. The gateway
|
|
5
|
+
// supplies that method today; this registry supplies the same one for a
|
|
6
|
+
// standalone server, so `manifold serve` mounts the identical handlers.
|
|
7
|
+
//
|
|
8
|
+
// `auth` is ignored here: every handler guards itself with the shared
|
|
9
|
+
// MANIFOLD_BEARER (routes/bearer-auth.js), and the server binds loopback.
|
|
10
|
+
|
|
11
|
+
export function createRouteRegistry({ logger = console } = {}) {
|
|
12
|
+
const routes = [];
|
|
13
|
+
|
|
14
|
+
function registerHttpRoute(spec) {
|
|
15
|
+
if (!spec?.path || typeof spec.handler !== "function") {
|
|
16
|
+
throw new Error("registerHttpRoute: path and handler are required");
|
|
17
|
+
}
|
|
18
|
+
routes.push({ auth: "plugin", match: "exact", ...spec });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function find(pathname) {
|
|
22
|
+
for (const route of routes) {
|
|
23
|
+
if (route.match === "prefix" ? pathname.startsWith(route.path) : pathname === route.path) {
|
|
24
|
+
return route;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function handle(req, res) {
|
|
31
|
+
const url = new URL(req.url, `http://${req.headers.host ?? "127.0.0.1"}`);
|
|
32
|
+
const route = find(url.pathname);
|
|
33
|
+
if (!route) {
|
|
34
|
+
res.statusCode = 404;
|
|
35
|
+
res.setHeader("content-type", "application/json");
|
|
36
|
+
res.end(JSON.stringify({ error: "not found", path: url.pathname }));
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const handled = await route.handler(req, res);
|
|
41
|
+
if (handled !== true && !res.writableEnded) res.end();
|
|
42
|
+
} catch (err) {
|
|
43
|
+
logger?.warn?.(`manifold api: handler error on ${url.pathname}: ${err?.message ?? err}`);
|
|
44
|
+
if (!res.writableEnded) {
|
|
45
|
+
res.statusCode = 500;
|
|
46
|
+
res.setHeader("content-type", "application/json");
|
|
47
|
+
res.end(JSON.stringify({ error: "internal", message: err?.message ?? String(err) }));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { registerHttpRoute, routes, find, handle };
|
|
53
|
+
}
|