@facthouse/mcp 0.26.0
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/LICENSE +21 -0
- package/README.md +611 -0
- package/dist/cli/index.d.ts +30 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +785 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/init-backfill.d.ts +26 -0
- package/dist/cli/init-backfill.d.ts.map +1 -0
- package/dist/cli/init-backfill.js +56 -0
- package/dist/cli/init-backfill.js.map +1 -0
- package/dist/cli/init-knobs.d.ts +137 -0
- package/dist/cli/init-knobs.d.ts.map +1 -0
- package/dist/cli/init-knobs.js +400 -0
- package/dist/cli/init-knobs.js.map +1 -0
- package/dist/cli/init-wizard.d.ts +67 -0
- package/dist/cli/init-wizard.d.ts.map +1 -0
- package/dist/cli/init-wizard.js +319 -0
- package/dist/cli/init-wizard.js.map +1 -0
- package/dist/cli/init.d.ts +124 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +286 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/inspect-html.d.ts +15 -0
- package/dist/cli/inspect-html.d.ts.map +1 -0
- package/dist/cli/inspect-html.js +491 -0
- package/dist/cli/inspect-html.js.map +1 -0
- package/dist/cli/inspect-intelligence.d.ts +8 -0
- package/dist/cli/inspect-intelligence.d.ts.map +1 -0
- package/dist/cli/inspect-intelligence.js +103 -0
- package/dist/cli/inspect-intelligence.js.map +1 -0
- package/dist/cli/inspect-model.d.ts +42 -0
- package/dist/cli/inspect-model.d.ts.map +1 -0
- package/dist/cli/inspect-model.js +108 -0
- package/dist/cli/inspect-model.js.map +1 -0
- package/dist/cli/inspect-payload.d.ts +122 -0
- package/dist/cli/inspect-payload.d.ts.map +1 -0
- package/dist/cli/inspect-payload.js +338 -0
- package/dist/cli/inspect-payload.js.map +1 -0
- package/dist/cli/inspect-theme.d.ts +44 -0
- package/dist/cli/inspect-theme.d.ts.map +1 -0
- package/dist/cli/inspect-theme.js +118 -0
- package/dist/cli/inspect-theme.js.map +1 -0
- package/dist/cli/inspect.d.ts +24 -0
- package/dist/cli/inspect.d.ts.map +1 -0
- package/dist/cli/inspect.js +172 -0
- package/dist/cli/inspect.js.map +1 -0
- package/dist/cli/log-event.d.ts +55 -0
- package/dist/cli/log-event.d.ts.map +1 -0
- package/dist/cli/log-event.js +149 -0
- package/dist/cli/log-event.js.map +1 -0
- package/dist/cli/package-version.d.ts +2 -0
- package/dist/cli/package-version.d.ts.map +1 -0
- package/dist/cli/package-version.js +15 -0
- package/dist/cli/package-version.js.map +1 -0
- package/dist/cli/query.d.ts +53 -0
- package/dist/cli/query.d.ts.map +1 -0
- package/dist/cli/query.js +325 -0
- package/dist/cli/query.js.map +1 -0
- package/dist/cli/record.d.ts +55 -0
- package/dist/cli/record.d.ts.map +1 -0
- package/dist/cli/record.js +149 -0
- package/dist/cli/record.js.map +1 -0
- package/dist/cli/settings.d.ts +26 -0
- package/dist/cli/settings.d.ts.map +1 -0
- package/dist/cli/settings.js +161 -0
- package/dist/cli/settings.js.map +1 -0
- package/dist/cli/spend-dashboard.d.ts +53 -0
- package/dist/cli/spend-dashboard.d.ts.map +1 -0
- package/dist/cli/spend-dashboard.js +297 -0
- package/dist/cli/spend-dashboard.js.map +1 -0
- package/dist/cli/stats-html.d.ts +20 -0
- package/dist/cli/stats-html.d.ts.map +1 -0
- package/dist/cli/stats-html.js +879 -0
- package/dist/cli/stats-html.js.map +1 -0
- package/dist/cli/web.d.ts +60 -0
- package/dist/cli/web.d.ts.map +1 -0
- package/dist/cli/web.js +416 -0
- package/dist/cli/web.js.map +1 -0
- package/dist/config.d.ts +102 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +302 -0
- package/dist/config.js.map +1 -0
- package/dist/db/connection.d.ts +96 -0
- package/dist/db/connection.d.ts.map +1 -0
- package/dist/db/connection.js +200 -0
- package/dist/db/connection.js.map +1 -0
- package/dist/db/consolidation-lock.d.ts +16 -0
- package/dist/db/consolidation-lock.d.ts.map +1 -0
- package/dist/db/consolidation-lock.js +67 -0
- package/dist/db/consolidation-lock.js.map +1 -0
- package/dist/db/consolidations.d.ts +41 -0
- package/dist/db/consolidations.d.ts.map +1 -0
- package/dist/db/consolidations.js +165 -0
- package/dist/db/consolidations.js.map +1 -0
- package/dist/db/disk-budget.d.ts +44 -0
- package/dist/db/disk-budget.d.ts.map +1 -0
- package/dist/db/disk-budget.js +177 -0
- package/dist/db/disk-budget.js.map +1 -0
- package/dist/db/domains.d.ts +14 -0
- package/dist/db/domains.d.ts.map +1 -0
- package/dist/db/domains.js +37 -0
- package/dist/db/domains.js.map +1 -0
- package/dist/db/embeddings-hnsw.d.ts +30 -0
- package/dist/db/embeddings-hnsw.d.ts.map +1 -0
- package/dist/db/embeddings-hnsw.js +129 -0
- package/dist/db/embeddings-hnsw.js.map +1 -0
- package/dist/db/embeddings.d.ts +68 -0
- package/dist/db/embeddings.d.ts.map +1 -0
- package/dist/db/embeddings.js +139 -0
- package/dist/db/embeddings.js.map +1 -0
- package/dist/db/entities.d.ts +157 -0
- package/dist/db/entities.d.ts.map +1 -0
- package/dist/db/entities.js +489 -0
- package/dist/db/entities.js.map +1 -0
- package/dist/db/extract-watermarks.d.ts +55 -0
- package/dist/db/extract-watermarks.d.ts.map +1 -0
- package/dist/db/extract-watermarks.js +171 -0
- package/dist/db/extract-watermarks.js.map +1 -0
- package/dist/db/facts.d.ts +117 -0
- package/dist/db/facts.d.ts.map +1 -0
- package/dist/db/facts.js +326 -0
- package/dist/db/facts.js.map +1 -0
- package/dist/db/index.d.ts +26 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +23 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/inferences.d.ts +36 -0
- package/dist/db/inferences.d.ts.map +1 -0
- package/dist/db/inferences.js +212 -0
- package/dist/db/inferences.js.map +1 -0
- package/dist/db/intelligence-runs.d.ts +20 -0
- package/dist/db/intelligence-runs.d.ts.map +1 -0
- package/dist/db/intelligence-runs.js +60 -0
- package/dist/db/intelligence-runs.js.map +1 -0
- package/dist/db/pg-backend.d.ts +14 -0
- package/dist/db/pg-backend.d.ts.map +1 -0
- package/dist/db/pg-backend.js +53 -0
- package/dist/db/pg-backend.js.map +1 -0
- package/dist/db/postgres-schema.d.ts +13 -0
- package/dist/db/postgres-schema.d.ts.map +1 -0
- package/dist/db/postgres-schema.js +392 -0
- package/dist/db/postgres-schema.js.map +1 -0
- package/dist/db/postgres.d.ts +30 -0
- package/dist/db/postgres.d.ts.map +1 -0
- package/dist/db/postgres.js +154 -0
- package/dist/db/postgres.js.map +1 -0
- package/dist/db/prune.d.ts +67 -0
- package/dist/db/prune.d.ts.map +1 -0
- package/dist/db/prune.js +113 -0
- package/dist/db/prune.js.map +1 -0
- package/dist/db/scheduler-signal.d.ts +20 -0
- package/dist/db/scheduler-signal.d.ts.map +1 -0
- package/dist/db/scheduler-signal.js +32 -0
- package/dist/db/scheduler-signal.js.map +1 -0
- package/dist/db/schema-version.d.ts +7 -0
- package/dist/db/schema-version.d.ts.map +1 -0
- package/dist/db/schema-version.js +7 -0
- package/dist/db/schema-version.js.map +1 -0
- package/dist/db/schema.d.ts +12 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/db/schema.js +983 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/db/session-facts.d.ts +108 -0
- package/dist/db/session-facts.d.ts.map +1 -0
- package/dist/db/session-facts.js +244 -0
- package/dist/db/session-facts.js.map +1 -0
- package/dist/db/sessions.d.ts +85 -0
- package/dist/db/sessions.d.ts.map +1 -0
- package/dist/db/sessions.js +169 -0
- package/dist/db/sessions.js.map +1 -0
- package/dist/db/sources.d.ts +22 -0
- package/dist/db/sources.d.ts.map +1 -0
- package/dist/db/sources.js +43 -0
- package/dist/db/sources.js.map +1 -0
- package/dist/db/stats.d.ts +94 -0
- package/dist/db/stats.d.ts.map +1 -0
- package/dist/db/stats.js +79 -0
- package/dist/db/stats.js.map +1 -0
- package/dist/db/store.d.ts +19 -0
- package/dist/db/store.d.ts.map +1 -0
- package/dist/db/store.js +58 -0
- package/dist/db/store.js.map +1 -0
- package/dist/db/watermarks.d.ts +20 -0
- package/dist/db/watermarks.d.ts.map +1 -0
- package/dist/db/watermarks.js +30 -0
- package/dist/db/watermarks.js.map +1 -0
- package/dist/embedding/ollama.d.ts +31 -0
- package/dist/embedding/ollama.d.ts.map +1 -0
- package/dist/embedding/ollama.js +148 -0
- package/dist/embedding/ollama.js.map +1 -0
- package/dist/embedding/provider.d.ts +37 -0
- package/dist/embedding/provider.d.ts.map +1 -0
- package/dist/embedding/provider.js +71 -0
- package/dist/embedding/provider.js.map +1 -0
- package/dist/embedding/types.d.ts +61 -0
- package/dist/embedding/types.d.ts.map +1 -0
- package/dist/embedding/types.js +13 -0
- package/dist/embedding/types.js.map +1 -0
- package/dist/embedding/voyage.d.ts +31 -0
- package/dist/embedding/voyage.d.ts.map +1 -0
- package/dist/embedding/voyage.js +105 -0
- package/dist/embedding/voyage.js.map +1 -0
- package/dist/identity.d.ts +32 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +53 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +275 -0
- package/dist/index.js.map +1 -0
- package/dist/intelligence/backing.d.ts +19 -0
- package/dist/intelligence/backing.d.ts.map +1 -0
- package/dist/intelligence/backing.js +88 -0
- package/dist/intelligence/backing.js.map +1 -0
- package/dist/intelligence/cli.d.ts +90 -0
- package/dist/intelligence/cli.d.ts.map +1 -0
- package/dist/intelligence/cli.js +823 -0
- package/dist/intelligence/cli.js.map +1 -0
- package/dist/intelligence/consolidate.d.ts +84 -0
- package/dist/intelligence/consolidate.d.ts.map +1 -0
- package/dist/intelligence/consolidate.js +1234 -0
- package/dist/intelligence/consolidate.js.map +1 -0
- package/dist/intelligence/extract-prompt.d.ts +78 -0
- package/dist/intelligence/extract-prompt.d.ts.map +1 -0
- package/dist/intelligence/extract-prompt.js +146 -0
- package/dist/intelligence/extract-prompt.js.map +1 -0
- package/dist/intelligence/heuristic.d.ts +15 -0
- package/dist/intelligence/heuristic.d.ts.map +1 -0
- package/dist/intelligence/heuristic.js +224 -0
- package/dist/intelligence/heuristic.js.map +1 -0
- package/dist/intelligence/http.d.ts +77 -0
- package/dist/intelligence/http.d.ts.map +1 -0
- package/dist/intelligence/http.js +496 -0
- package/dist/intelligence/http.js.map +1 -0
- package/dist/intelligence/provider-type.d.ts +6 -0
- package/dist/intelligence/provider-type.d.ts.map +1 -0
- package/dist/intelligence/provider-type.js +18 -0
- package/dist/intelligence/provider-type.js.map +1 -0
- package/dist/intelligence/provider.d.ts +49 -0
- package/dist/intelligence/provider.d.ts.map +1 -0
- package/dist/intelligence/provider.js +52 -0
- package/dist/intelligence/provider.js.map +1 -0
- package/dist/intelligence/related-k.d.ts +15 -0
- package/dist/intelligence/related-k.d.ts.map +1 -0
- package/dist/intelligence/related-k.js +57 -0
- package/dist/intelligence/related-k.js.map +1 -0
- package/dist/intelligence/routing-view.d.ts +33 -0
- package/dist/intelligence/routing-view.d.ts.map +1 -0
- package/dist/intelligence/routing-view.js +54 -0
- package/dist/intelligence/routing-view.js.map +1 -0
- package/dist/intelligence/sampling.d.ts +20 -0
- package/dist/intelligence/sampling.d.ts.map +1 -0
- package/dist/intelligence/sampling.js +324 -0
- package/dist/intelligence/sampling.js.map +1 -0
- package/dist/intelligence/stage-router.d.ts +32 -0
- package/dist/intelligence/stage-router.d.ts.map +1 -0
- package/dist/intelligence/stage-router.js +251 -0
- package/dist/intelligence/stage-router.js.map +1 -0
- package/dist/intelligence/steps.d.ts +65 -0
- package/dist/intelligence/steps.d.ts.map +1 -0
- package/dist/intelligence/steps.js +86 -0
- package/dist/intelligence/steps.js.map +1 -0
- package/dist/intelligence/subject.d.ts +42 -0
- package/dist/intelligence/subject.d.ts.map +1 -0
- package/dist/intelligence/subject.js +44 -0
- package/dist/intelligence/subject.js.map +1 -0
- package/dist/intelligence/token-budget.d.ts +86 -0
- package/dist/intelligence/token-budget.d.ts.map +1 -0
- package/dist/intelligence/token-budget.js +312 -0
- package/dist/intelligence/token-budget.js.map +1 -0
- package/dist/intelligence/types.d.ts +179 -0
- package/dist/intelligence/types.d.ts.map +1 -0
- package/dist/intelligence/types.js +7 -0
- package/dist/intelligence/types.js.map +1 -0
- package/dist/intelligence/usage.d.ts +104 -0
- package/dist/intelligence/usage.d.ts.map +1 -0
- package/dist/intelligence/usage.js +233 -0
- package/dist/intelligence/usage.js.map +1 -0
- package/dist/ipc/scheduler-ipc.d.ts +52 -0
- package/dist/ipc/scheduler-ipc.d.ts.map +1 -0
- package/dist/ipc/scheduler-ipc.js +214 -0
- package/dist/ipc/scheduler-ipc.js.map +1 -0
- package/dist/paths.d.ts +29 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +47 -0
- package/dist/paths.js.map +1 -0
- package/dist/scheduler.d.ts +39 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +93 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/schemas/domains.d.ts +89 -0
- package/dist/schemas/domains.d.ts.map +1 -0
- package/dist/schemas/domains.js +160 -0
- package/dist/schemas/domains.js.map +1 -0
- package/dist/schemas/starter-vocabulary.d.ts +34 -0
- package/dist/schemas/starter-vocabulary.d.ts.map +1 -0
- package/dist/schemas/starter-vocabulary.js +91 -0
- package/dist/schemas/starter-vocabulary.js.map +1 -0
- package/dist/search/ann.d.ts +36 -0
- package/dist/search/ann.d.ts.map +1 -0
- package/dist/search/ann.js +59 -0
- package/dist/search/ann.js.map +1 -0
- package/dist/search/entity.d.ts +43 -0
- package/dist/search/entity.d.ts.map +1 -0
- package/dist/search/entity.js +147 -0
- package/dist/search/entity.js.map +1 -0
- package/dist/search/episodes.d.ts +28 -0
- package/dist/search/episodes.d.ts.map +1 -0
- package/dist/search/episodes.js +102 -0
- package/dist/search/episodes.js.map +1 -0
- package/dist/search/hnsw-graph.d.ts +35 -0
- package/dist/search/hnsw-graph.d.ts.map +1 -0
- package/dist/search/hnsw-graph.js +177 -0
- package/dist/search/hnsw-graph.js.map +1 -0
- package/dist/search/hnsw-ram.d.ts +29 -0
- package/dist/search/hnsw-ram.d.ts.map +1 -0
- package/dist/search/hnsw-ram.js +95 -0
- package/dist/search/hnsw-ram.js.map +1 -0
- package/dist/search/index.d.ts +119 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +420 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/interlocutor-rank.d.ts +10 -0
- package/dist/search/interlocutor-rank.d.ts.map +1 -0
- package/dist/search/interlocutor-rank.js +21 -0
- package/dist/search/interlocutor-rank.js.map +1 -0
- package/dist/search/key-facts.d.ts +34 -0
- package/dist/search/key-facts.d.ts.map +1 -0
- package/dist/search/key-facts.js +43 -0
- package/dist/search/key-facts.js.map +1 -0
- package/dist/search/profile.d.ts +31 -0
- package/dist/search/profile.d.ts.map +1 -0
- package/dist/search/profile.js +43 -0
- package/dist/search/profile.js.map +1 -0
- package/dist/search/vector.d.ts +90 -0
- package/dist/search/vector.d.ts.map +1 -0
- package/dist/search/vector.js +187 -0
- package/dist/search/vector.js.map +1 -0
- package/dist/sources/claude-code.d.ts +35 -0
- package/dist/sources/claude-code.d.ts.map +1 -0
- package/dist/sources/claude-code.js +401 -0
- package/dist/sources/claude-code.js.map +1 -0
- package/dist/sources/copy.d.ts +58 -0
- package/dist/sources/copy.d.ts.map +1 -0
- package/dist/sources/copy.js +104 -0
- package/dist/sources/copy.js.map +1 -0
- package/dist/sources/cursor.d.ts +44 -0
- package/dist/sources/cursor.d.ts.map +1 -0
- package/dist/sources/cursor.js +99 -0
- package/dist/sources/cursor.js.map +1 -0
- package/dist/sources/jsonl-copy.d.ts +58 -0
- package/dist/sources/jsonl-copy.d.ts.map +1 -0
- package/dist/sources/jsonl-copy.js +195 -0
- package/dist/sources/jsonl-copy.js.map +1 -0
- package/dist/sources/jsonl-ingest.d.ts +58 -0
- package/dist/sources/jsonl-ingest.d.ts.map +1 -0
- package/dist/sources/jsonl-ingest.js +195 -0
- package/dist/sources/jsonl-ingest.js.map +1 -0
- package/dist/sources/pull.d.ts +85 -0
- package/dist/sources/pull.d.ts.map +1 -0
- package/dist/sources/pull.js +145 -0
- package/dist/sources/pull.js.map +1 -0
- package/dist/sources/resolve.d.ts +38 -0
- package/dist/sources/resolve.d.ts.map +1 -0
- package/dist/sources/resolve.js +91 -0
- package/dist/sources/resolve.js.map +1 -0
- package/dist/tools/capture-fact-description.d.ts +18 -0
- package/dist/tools/capture-fact-description.d.ts.map +1 -0
- package/dist/tools/capture-fact-description.js +41 -0
- package/dist/tools/capture-fact-description.js.map +1 -0
- package/dist/tools/fact-manager.d.ts +69 -0
- package/dist/tools/fact-manager.d.ts.map +1 -0
- package/dist/tools/fact-manager.js +332 -0
- package/dist/tools/fact-manager.js.map +1 -0
- package/dist/tools/inference-descriptions.d.ts +8 -0
- package/dist/tools/inference-descriptions.d.ts.map +1 -0
- package/dist/tools/inference-descriptions.js +16 -0
- package/dist/tools/inference-descriptions.js.map +1 -0
- package/dist/tools/inferences.d.ts +11 -0
- package/dist/tools/inferences.d.ts.map +1 -0
- package/dist/tools/inferences.js +98 -0
- package/dist/tools/inferences.js.map +1 -0
- package/dist/tools/read-tools.d.ts +21 -0
- package/dist/tools/read-tools.d.ts.map +1 -0
- package/dist/tools/read-tools.js +276 -0
- package/dist/tools/read-tools.js.map +1 -0
- package/dist/tools/resources.d.ts +62 -0
- package/dist/tools/resources.d.ts.map +1 -0
- package/dist/tools/resources.js +199 -0
- package/dist/tools/resources.js.map +1 -0
- package/dist/tools/session-manager.d.ts +44 -0
- package/dist/tools/session-manager.d.ts.map +1 -0
- package/dist/tools/session-manager.js +227 -0
- package/dist/tools/session-manager.js.map +1 -0
- package/dist/types/config.d.ts +400 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +105 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/data.d.ts +357 -0
- package/dist/types/data.d.ts.map +1 -0
- package/dist/types/data.js +36 -0
- package/dist/types/data.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types.d.ts +115 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/warnings.d.ts.map +1 -0
- package/dist/warnings.js.map +1 -0
- package/package.json +62 -0
package/dist/config.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server configuration loader.
|
|
3
|
+
*
|
|
4
|
+
* Reads <dataDir>/config.json if present and deep-merges it over DEFAULT_CONFIG.
|
|
5
|
+
* Missing file → defaults silently. Malformed JSON → defaults with a stderr
|
|
6
|
+
* warning (non-fatal — we'd rather run with sane defaults than fail to boot).
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { envName, envValue } from "./identity.js";
|
|
11
|
+
import { DEFAULT_CONFIG } from "./types/config.js";
|
|
12
|
+
/** Filename expected in the data dir. */
|
|
13
|
+
export const CONFIG_FILENAME = "config.json";
|
|
14
|
+
/**
|
|
15
|
+
* Deep-merge overrides onto a base object. Overrides win for scalar/array
|
|
16
|
+
* fields; nested objects merge recursively. Arrays are replaced, not merged,
|
|
17
|
+
* because our config arrays (triggers, event_types, roles) are user-authoritative
|
|
18
|
+
* selections — a user-supplied array means "these exact entries, nothing else".
|
|
19
|
+
*/
|
|
20
|
+
export function mergeConfig(base, override) {
|
|
21
|
+
if (override === null || typeof override !== "object" || Array.isArray(override)) {
|
|
22
|
+
return override === undefined ? base : override;
|
|
23
|
+
}
|
|
24
|
+
if (typeof base !== "object" || base === null || Array.isArray(base)) {
|
|
25
|
+
return override;
|
|
26
|
+
}
|
|
27
|
+
const out = { ...base };
|
|
28
|
+
for (const [key, value] of Object.entries(override)) {
|
|
29
|
+
out[key] = mergeConfig(base[key], value);
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The complete default configuration — every knob the server understands, with
|
|
35
|
+
* its shipped default. `loadConfig` merges the user's config.json over this,
|
|
36
|
+
* and `facthouse init` writes it out verbatim so the knobs are discoverable
|
|
37
|
+
* (otherwise defaults are invisible and users can't find what's tunable).
|
|
38
|
+
*/
|
|
39
|
+
export function defaultServerConfig() {
|
|
40
|
+
return {
|
|
41
|
+
storage: { provider: "sqlite" },
|
|
42
|
+
temporal: { mode: "simple", bitemporal_since: null },
|
|
43
|
+
...DEFAULT_CONFIG,
|
|
44
|
+
// The core taxonomy ships in config rather than staying buried in code, so
|
|
45
|
+
// it is visible and editable: a user can add their own domains here and they
|
|
46
|
+
// exist from first run. Seeding also gives a classifier something to be
|
|
47
|
+
// consistent with — schema-congruence needs a schema to pre-exist, and an
|
|
48
|
+
// empty vocabulary offers nothing to match against.
|
|
49
|
+
// No domains. The engine ships no categories and no rules: it cannot know
|
|
50
|
+
// whether this store is about a life, a company or a research programme, and
|
|
51
|
+
// every vocabulary it could offer would be wrong for someone.
|
|
52
|
+
//
|
|
53
|
+
// Declare your own here to give the fallback classifier something to route
|
|
54
|
+
// on and to calibrate importance — each domain takes a description, regex
|
|
55
|
+
// `patterns`, and an `importance`. With an LLM provider (the default) you do
|
|
56
|
+
// not need to: it classifies from the content, and the routing prompt names
|
|
57
|
+
// whatever domains already exist so the vocabulary grows from use and stays
|
|
58
|
+
// consistent with itself.
|
|
59
|
+
domains: [],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Default transactional engine. sqlite is zero-config; postgres is opt-in.
|
|
64
|
+
* A request for anything else is refused — it must not silently open SQLite.
|
|
65
|
+
*/
|
|
66
|
+
export const SHIPPED_STORAGE_PROVIDER = "sqlite";
|
|
67
|
+
/** Engines this package will actually open. Unknown values still die. */
|
|
68
|
+
export const SUPPORTED_STORAGE_PROVIDERS = ["sqlite", "postgres"];
|
|
69
|
+
/** Environment variable holding the Postgres URL. Canonical name; compat still read. */
|
|
70
|
+
export const POSTGRES_URL_ENV = envName("POSTGRES_URL");
|
|
71
|
+
/**
|
|
72
|
+
* Which engine the store asked for. `FACTHOUSE_STORAGE` (or `FACTHOUSE_STORAGE`)
|
|
73
|
+
* wins over `storage.provider`. Missing or empty is sqlite.
|
|
74
|
+
*/
|
|
75
|
+
export function configuredStorageProvider(config, env = process.env) {
|
|
76
|
+
const fromEnv = envValue("STORAGE", env);
|
|
77
|
+
if (fromEnv)
|
|
78
|
+
return fromEnv.toLowerCase();
|
|
79
|
+
const storage = config.storage;
|
|
80
|
+
if (typeof storage === "string" && storage.trim() !== "") {
|
|
81
|
+
return storage.trim().toLowerCase();
|
|
82
|
+
}
|
|
83
|
+
if (storage !== null && typeof storage === "object" && !Array.isArray(storage)) {
|
|
84
|
+
const provider = storage.provider;
|
|
85
|
+
if (typeof provider === "string" && provider.trim() !== "") {
|
|
86
|
+
return provider.trim().toLowerCase();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return SHIPPED_STORAGE_PROVIDER;
|
|
90
|
+
}
|
|
91
|
+
export function isSupportedStorage(provider) {
|
|
92
|
+
return SUPPORTED_STORAGE_PROVIDERS.includes(provider);
|
|
93
|
+
}
|
|
94
|
+
/** Unknown engine. Callers must not open SQLite after this. */
|
|
95
|
+
export function unsupportedStorageMessage(provider) {
|
|
96
|
+
return (`Unknown storage provider "${provider}". The default engine is sqlite; ` +
|
|
97
|
+
`postgres is optional. SQLite was not opened.`);
|
|
98
|
+
}
|
|
99
|
+
export function assertSupportedStorage(provider) {
|
|
100
|
+
if (isSupportedStorage(provider))
|
|
101
|
+
return;
|
|
102
|
+
throw new Error(unsupportedStorageMessage(provider));
|
|
103
|
+
}
|
|
104
|
+
export function postgresUrl(env = process.env) {
|
|
105
|
+
return envValue("POSTGRES_URL", env);
|
|
106
|
+
}
|
|
107
|
+
export function postgresMissingUrlMessage() {
|
|
108
|
+
return (`Storage provider is postgres but ${POSTGRES_URL_ENV} is not set. ` +
|
|
109
|
+
`SQLite was not opened. Set the URL on the MCP entry (the same place as ` +
|
|
110
|
+
`${envName("DATA")}) or in the environment.`);
|
|
111
|
+
}
|
|
112
|
+
export function postgresInvalidUrlMessage() {
|
|
113
|
+
return (`${POSTGRES_URL_ENV} must be a postgres:// or postgresql:// URL. ` +
|
|
114
|
+
`SQLite was not opened.`);
|
|
115
|
+
}
|
|
116
|
+
export function postgresConnectFailedMessage(err) {
|
|
117
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
118
|
+
return `Could not connect to Postgres (${detail}). SQLite was not opened.`;
|
|
119
|
+
}
|
|
120
|
+
function isPostgresConnectionString(url) {
|
|
121
|
+
return /^(?:postgres|postgresql):\/\//i.test(url);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Required URL when the engine is postgres. Empty and the wrong scheme both
|
|
125
|
+
* die here — callers must not open SQLite afterwards.
|
|
126
|
+
*/
|
|
127
|
+
export function postgresUrlOrThrow(env = process.env) {
|
|
128
|
+
const url = postgresUrl(env);
|
|
129
|
+
if (!url)
|
|
130
|
+
throw new Error(postgresMissingUrlMessage());
|
|
131
|
+
if (!isPostgresConnectionString(url))
|
|
132
|
+
throw new Error(postgresInvalidUrlMessage());
|
|
133
|
+
return url;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Load config and refuse an unknown engine *before* a database file is
|
|
137
|
+
* created. Postgres is allowed; a missing URL is refused here so init does
|
|
138
|
+
* not prompt and then fail, and so MCP boot prints a clean message rather
|
|
139
|
+
* than a stack.
|
|
140
|
+
*/
|
|
141
|
+
export function loadShippedStoreConfig(dataDir, env = process.env) {
|
|
142
|
+
const config = loadConfig(dataDir);
|
|
143
|
+
const provider = configuredStorageProvider(config, env);
|
|
144
|
+
assertSupportedStorage(provider);
|
|
145
|
+
if (provider === "postgres")
|
|
146
|
+
postgresUrlOrThrow(env);
|
|
147
|
+
return config;
|
|
148
|
+
}
|
|
149
|
+
/** Fail-closed reader for a settings-style writer. Does not swallow. */
|
|
150
|
+
export class ConfigDocumentError extends Error {
|
|
151
|
+
code;
|
|
152
|
+
constructor(code, message) {
|
|
153
|
+
super(message);
|
|
154
|
+
this.name = "ConfigDocumentError";
|
|
155
|
+
this.code = code;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Raw on-disk `config.json`. Throws on missing, malformed, or non-object.
|
|
160
|
+
* Does not merge defaults. Does not mkdir. Must not be used as MCP boot.
|
|
161
|
+
*/
|
|
162
|
+
export function readConfigDocument(dataDir) {
|
|
163
|
+
const configPath = path.join(dataDir, CONFIG_FILENAME);
|
|
164
|
+
let raw;
|
|
165
|
+
try {
|
|
166
|
+
raw = readFileSync(configPath, "utf-8");
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
const code = err.code;
|
|
170
|
+
if (code === "ENOENT") {
|
|
171
|
+
throw new ConfigDocumentError("missing", `No ${CONFIG_FILENAME} at ${configPath}`);
|
|
172
|
+
}
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
let parsed;
|
|
176
|
+
try {
|
|
177
|
+
parsed = JSON.parse(raw);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
throw new ConfigDocumentError("malformed", `${CONFIG_FILENAME} is malformed`);
|
|
181
|
+
}
|
|
182
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
183
|
+
throw new ConfigDocumentError("not-object", `${CONFIG_FILENAME} must be a JSON object`);
|
|
184
|
+
}
|
|
185
|
+
return parsed;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Pretty-print `config.json`. The file must already exist. Caller decides
|
|
189
|
+
* whether the serialised text changed.
|
|
190
|
+
*/
|
|
191
|
+
export function writeConfigDocument(dataDir, doc) {
|
|
192
|
+
const configPath = path.join(dataDir, CONFIG_FILENAME);
|
|
193
|
+
if (!existsSync(configPath)) {
|
|
194
|
+
throw new ConfigDocumentError("missing", `No ${CONFIG_FILENAME} at ${configPath}`);
|
|
195
|
+
}
|
|
196
|
+
writeFileSync(configPath, JSON.stringify(doc, null, 2) + "\n", "utf-8");
|
|
197
|
+
}
|
|
198
|
+
export function loadConfig(dataDir) {
|
|
199
|
+
const base = defaultServerConfig();
|
|
200
|
+
const configPath = path.join(dataDir, CONFIG_FILENAME);
|
|
201
|
+
let raw;
|
|
202
|
+
try {
|
|
203
|
+
raw = readFileSync(configPath, "utf-8");
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
// Missing file is the common case — return defaults silently.
|
|
207
|
+
return base;
|
|
208
|
+
}
|
|
209
|
+
let parsed;
|
|
210
|
+
try {
|
|
211
|
+
parsed = JSON.parse(raw);
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
console.error(`[facthouse] Ignoring malformed ${CONFIG_FILENAME}: ${err.message}. Using defaults.`);
|
|
215
|
+
return base;
|
|
216
|
+
}
|
|
217
|
+
return mergeConfig(base, honourLegacyConfigKeys(parsed));
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Read-time compatibility for renamed keys. `intelligence.cli.graduate_model`
|
|
221
|
+
* became `integrate_model` when the pipeline vocabulary settled on copy /
|
|
222
|
+
* extract / integrate; a store written by an earlier release keeps working
|
|
223
|
+
* and is told once per process. `facthouse settings` rewrites the key.
|
|
224
|
+
*/
|
|
225
|
+
export function honourLegacyConfigKeys(parsed) {
|
|
226
|
+
if (!parsed || typeof parsed !== "object")
|
|
227
|
+
return parsed;
|
|
228
|
+
const doc = parsed;
|
|
229
|
+
const cli = doc.intelligence?.cli;
|
|
230
|
+
if (!cli || typeof cli !== "object")
|
|
231
|
+
return parsed;
|
|
232
|
+
if (cli.integrate_model === undefined && typeof cli.graduate_model === "string") {
|
|
233
|
+
cli.integrate_model = cli.graduate_model;
|
|
234
|
+
if (!legacyKeyNoticeShown) {
|
|
235
|
+
legacyKeyNoticeShown = true;
|
|
236
|
+
console.error(`[facthouse] ${CONFIG_FILENAME}: intelligence.cli.graduate_model is now ` +
|
|
237
|
+
`integrate_model. Still honoured; run facthouse settings to rewrite it.`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return parsed;
|
|
241
|
+
}
|
|
242
|
+
let legacyKeyNoticeShown = false;
|
|
243
|
+
/**
|
|
244
|
+
* Warning on an as-of-system-time read whose instant precedes the switch to
|
|
245
|
+
* bi-temporal recording. One string, used by the MCP tool and the CLI so they
|
|
246
|
+
* cannot disagree about what "incomplete" means.
|
|
247
|
+
*/
|
|
248
|
+
export const SYSTEM_TIME_INCOMPLETE_WARNING = "Results may be incomplete: this store started recording when the system " +
|
|
249
|
+
"retracted a belief after the requested instant, and earlier supersessions " +
|
|
250
|
+
"did not stamp it.";
|
|
251
|
+
/** Warning text when T precedes `bitemporal_since`, or the stamp is missing. */
|
|
252
|
+
export function systemTimeWarning(at, since) {
|
|
253
|
+
if (!since || at < since)
|
|
254
|
+
return SYSTEM_TIME_INCOMPLETE_WARNING;
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* When a store has just switched to bi-temporal mode, record `bitemporal_since`
|
|
259
|
+
* so as-of-system-time reads can warn about the unstamped era.
|
|
260
|
+
*
|
|
261
|
+
* Historical supersessions cannot be backfilled — simple mode never wrote
|
|
262
|
+
* `system_retired_at`. The stamp is the earliest instant at which that column
|
|
263
|
+
* is trustworthy. Idempotent: a store that already has a stamp is left alone.
|
|
264
|
+
*/
|
|
265
|
+
export function ensureBitemporalSince(dataDir, config) {
|
|
266
|
+
if (config.temporal.mode !== "bitemporal")
|
|
267
|
+
return config;
|
|
268
|
+
if (config.temporal.bitemporal_since)
|
|
269
|
+
return config;
|
|
270
|
+
const since = new Date().toISOString();
|
|
271
|
+
persistBitemporalSince(dataDir, since);
|
|
272
|
+
return {
|
|
273
|
+
...config,
|
|
274
|
+
temporal: { ...config.temporal, bitemporal_since: since },
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function persistBitemporalSince(dataDir, since) {
|
|
278
|
+
const configPath = path.join(dataDir, CONFIG_FILENAME);
|
|
279
|
+
let parsed = {};
|
|
280
|
+
try {
|
|
281
|
+
const raw = JSON.parse(readFileSync(configPath, "utf-8"));
|
|
282
|
+
if (raw !== null && typeof raw === "object" && !Array.isArray(raw)) {
|
|
283
|
+
parsed = raw;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
// Missing or unreadable — write a stub so the stamp survives a restart.
|
|
288
|
+
// loadConfig merges, so a temporal-only file still yields full defaults.
|
|
289
|
+
}
|
|
290
|
+
const existing = parsed.temporal !== null &&
|
|
291
|
+
typeof parsed.temporal === "object" &&
|
|
292
|
+
!Array.isArray(parsed.temporal)
|
|
293
|
+
? { ...parsed.temporal }
|
|
294
|
+
: {};
|
|
295
|
+
if (typeof existing.bitemporal_since === "string" && existing.bitemporal_since) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
existing.bitemporal_since = since;
|
|
299
|
+
parsed.temporal = existing;
|
|
300
|
+
writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n", "utf-8");
|
|
301
|
+
}
|
|
302
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,cAAc,EAAqB,MAAM,mBAAmB,CAAC;A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GAAI,MAAM,CAAC,QAAoC,EAAE;QACrD,CAAC,CAAC,EAAE,CAAC;IACT,IAAI,OAAO,QAAQ,CAAC,gBAAgB,KAAK,QAAQ,IAAI,QAAQ,CAAC,gBAAgB,EAAE,CAAC;QAC/E,OAAO;IACT,CAAC;IACD,QAAQ,CAAC,gBAAgB,GAAG,KAAK,CAAC;IAClC,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database handle. SQLite via Node's built-in `node:sqlite` is the default
|
|
3
|
+
* engine; the methods are async so the Postgres adapter can use `pg` without
|
|
4
|
+
* converting callers a second time.
|
|
5
|
+
*
|
|
6
|
+
* Uses the built-in module rather than a native addon by necessity, not
|
|
7
|
+
* preference: npm 12 made dependency lifecycle scripts opt-in, so
|
|
8
|
+
* better-sqlite3's `prebuild-install || node-gyp rebuild` no longer runs and no
|
|
9
|
+
* native binary is produced. The package then installs "successfully" and
|
|
10
|
+
* throws "Could not locate the bindings file" at runtime. An author cannot
|
|
11
|
+
* grant that approval for a user, and requiring everyone to pass
|
|
12
|
+
* `--allow-scripts` would break zero-config install. A built-in module has no
|
|
13
|
+
* install step to block.
|
|
14
|
+
*
|
|
15
|
+
* This module is the ONLY runtime coupling to the SQLite driver — everything
|
|
16
|
+
* else imports `Db` from here.
|
|
17
|
+
*/
|
|
18
|
+
import type { SQLInputValue } from "node:sqlite";
|
|
19
|
+
/**
|
|
20
|
+
* A value bindable to a `?` placeholder. Re-exported so callers building
|
|
21
|
+
* dynamic parameter lists don't have to import the driver directly.
|
|
22
|
+
*/
|
|
23
|
+
export type SqlParam = SQLInputValue;
|
|
24
|
+
/** Result of a mutating statement. `changes` is always a number. */
|
|
25
|
+
export interface RunResult {
|
|
26
|
+
changes: number;
|
|
27
|
+
lastInsertRowid: number | bigint;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A prepared statement. `prepare` itself is synchronous (it only holds SQL);
|
|
31
|
+
* execution is async so a network engine can sit behind the same shape.
|
|
32
|
+
*/
|
|
33
|
+
export interface Statement {
|
|
34
|
+
run(...params: SqlParam[]): Promise<RunResult>;
|
|
35
|
+
get(...params: SqlParam[]): Promise<unknown>;
|
|
36
|
+
all(...params: SqlParam[]): Promise<unknown[]>;
|
|
37
|
+
}
|
|
38
|
+
/** Which engine is behind this handle. */
|
|
39
|
+
export type Dialect = "sqlite" | "postgres";
|
|
40
|
+
/**
|
|
41
|
+
* The database handle type. Import this rather than the driver directly.
|
|
42
|
+
*
|
|
43
|
+
* `exec` / `prepare` / transactions are the portable surface. PRAGMA helpers
|
|
44
|
+
* stay SQLite-only and live beside this type.
|
|
45
|
+
*/
|
|
46
|
+
export interface Db {
|
|
47
|
+
readonly dialect: Dialect;
|
|
48
|
+
exec(sql: string): Promise<void>;
|
|
49
|
+
prepare(sql: string): Statement;
|
|
50
|
+
close(): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Run `fn` inside a transaction: commit on success, roll back on throw.
|
|
53
|
+
* Nested calls are savepoints.
|
|
54
|
+
*/
|
|
55
|
+
transaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Open or create a SQLite database at the given path.
|
|
59
|
+
* Pass ":memory:" for in-memory databases (tests).
|
|
60
|
+
*
|
|
61
|
+
* Opening stays synchronous: `DatabaseSync` construction and the boot
|
|
62
|
+
* PRAGMAs are local. Every statement after that is async.
|
|
63
|
+
*/
|
|
64
|
+
export declare function openDatabase(dbPath: string): Db;
|
|
65
|
+
/** Close the database connection. Safe to call multiple times. */
|
|
66
|
+
export declare function closeDatabase(db: Db): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Read a scalar pragma (e.g. `user_version`, `data_version`).
|
|
69
|
+
*
|
|
70
|
+
* Returns 0 when absent or non-numeric so callers get a number without
|
|
71
|
+
* repeating a type guard at every site.
|
|
72
|
+
*/
|
|
73
|
+
export declare function pragmaRead(db: Db, name: string): Promise<number>;
|
|
74
|
+
/**
|
|
75
|
+
* Write a pragma, e.g. `pragmaWrite(db, "user_version = 4")`.
|
|
76
|
+
*
|
|
77
|
+
* Some pragmas — notably `foreign_keys` — are silently ignored inside a
|
|
78
|
+
* transaction. Schema migrations toggle it around table rebuilds and must
|
|
79
|
+
* therefore stay outside one.
|
|
80
|
+
*/
|
|
81
|
+
export declare function pragmaWrite(db: Db, statement: string): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Run `fn` inside a transaction: commit on success, roll back on throw, and
|
|
84
|
+
* return whatever `fn` returns.
|
|
85
|
+
*
|
|
86
|
+
* Nested calls on the same async stack are SAVEPOINTs, because SQLite cannot
|
|
87
|
+
* BEGIN a transaction within a transaction. Concurrent callers on the same
|
|
88
|
+
* handle are queued instead — an await inside the first must not let the
|
|
89
|
+
* second BEGIN (or SAVEPOINT) into its work. Two MCP tools share one
|
|
90
|
+
* connection; this is what keeps them from interleaving.
|
|
91
|
+
*
|
|
92
|
+
* A failing inner block rolls back only its own work and rethrows, leaving the
|
|
93
|
+
* outer transaction to decide — the same semantics as before.
|
|
94
|
+
*/
|
|
95
|
+
export declare function withTransaction<T>(db: Db, fn: () => Promise<T>): Promise<T>;
|
|
96
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/db/connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,OAAO,KAAK,EAAE,aAAa,EAAiB,MAAM,aAAa,CAAC;AAEhE;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,CAAC;AAErC,oEAAoE;AACpE,MAAM,WAAW,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GAAG,OAAO,CAAC,IAAI,CAAC,CAE1E;AAwBD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,EAAE,EAAE,EAAE,EACN,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAEZ"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database handle. SQLite via Node's built-in `node:sqlite` is the default
|
|
3
|
+
* engine; the methods are async so the Postgres adapter can use `pg` without
|
|
4
|
+
* converting callers a second time.
|
|
5
|
+
*
|
|
6
|
+
* Uses the built-in module rather than a native addon by necessity, not
|
|
7
|
+
* preference: npm 12 made dependency lifecycle scripts opt-in, so
|
|
8
|
+
* better-sqlite3's `prebuild-install || node-gyp rebuild` no longer runs and no
|
|
9
|
+
* native binary is produced. The package then installs "successfully" and
|
|
10
|
+
* throws "Could not locate the bindings file" at runtime. An author cannot
|
|
11
|
+
* grant that approval for a user, and requiring everyone to pass
|
|
12
|
+
* `--allow-scripts` would break zero-config install. A built-in module has no
|
|
13
|
+
* install step to block.
|
|
14
|
+
*
|
|
15
|
+
* This module is the ONLY runtime coupling to the SQLite driver — everything
|
|
16
|
+
* else imports `Db` from here.
|
|
17
|
+
*/
|
|
18
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
19
|
+
import { DatabaseSync } from "node:sqlite";
|
|
20
|
+
class SqliteStatement {
|
|
21
|
+
stmt;
|
|
22
|
+
constructor(stmt) {
|
|
23
|
+
this.stmt = stmt;
|
|
24
|
+
}
|
|
25
|
+
async run(...params) {
|
|
26
|
+
const result = this.stmt.run(...params);
|
|
27
|
+
return {
|
|
28
|
+
changes: Number(result.changes),
|
|
29
|
+
lastInsertRowid: result.lastInsertRowid,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async get(...params) {
|
|
33
|
+
return this.stmt.get(...params);
|
|
34
|
+
}
|
|
35
|
+
async all(...params) {
|
|
36
|
+
return this.stmt.all(...params);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
class SqliteDb {
|
|
40
|
+
raw;
|
|
41
|
+
dialect = "sqlite";
|
|
42
|
+
constructor(raw) {
|
|
43
|
+
this.raw = raw;
|
|
44
|
+
}
|
|
45
|
+
async exec(sql) {
|
|
46
|
+
this.raw.exec(sql);
|
|
47
|
+
}
|
|
48
|
+
prepare(sql) {
|
|
49
|
+
return new SqliteStatement(this.raw.prepare(sql));
|
|
50
|
+
}
|
|
51
|
+
async close() {
|
|
52
|
+
try {
|
|
53
|
+
this.raw.close();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Already closed — ignore.
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async transaction(fn) {
|
|
60
|
+
return withSqliteTransaction(this, fn);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Open or create a SQLite database at the given path.
|
|
65
|
+
* Pass ":memory:" for in-memory databases (tests).
|
|
66
|
+
*
|
|
67
|
+
* Opening stays synchronous: `DatabaseSync` construction and the boot
|
|
68
|
+
* PRAGMAs are local. Every statement after that is async.
|
|
69
|
+
*/
|
|
70
|
+
export function openDatabase(dbPath) {
|
|
71
|
+
const raw = new DatabaseSync(dbPath);
|
|
72
|
+
raw.exec("PRAGMA journal_mode = WAL");
|
|
73
|
+
raw.exec("PRAGMA foreign_keys = ON");
|
|
74
|
+
raw.exec("PRAGMA busy_timeout = 5000");
|
|
75
|
+
return new SqliteDb(raw);
|
|
76
|
+
}
|
|
77
|
+
/** Close the database connection. Safe to call multiple times. */
|
|
78
|
+
export async function closeDatabase(db) {
|
|
79
|
+
await db.close();
|
|
80
|
+
}
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Pragmas (SQLite-only)
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
/**
|
|
85
|
+
* Read a scalar pragma (e.g. `user_version`, `data_version`).
|
|
86
|
+
*
|
|
87
|
+
* Returns 0 when absent or non-numeric so callers get a number without
|
|
88
|
+
* repeating a type guard at every site.
|
|
89
|
+
*/
|
|
90
|
+
export async function pragmaRead(db, name) {
|
|
91
|
+
const row = (await db.prepare(`PRAGMA ${name}`).get());
|
|
92
|
+
const value = row?.[name];
|
|
93
|
+
return typeof value === "number" ? value : 0;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Write a pragma, e.g. `pragmaWrite(db, "user_version = 4")`.
|
|
97
|
+
*
|
|
98
|
+
* Some pragmas — notably `foreign_keys` — are silently ignored inside a
|
|
99
|
+
* transaction. Schema migrations toggle it around table rebuilds and must
|
|
100
|
+
* therefore stay outside one.
|
|
101
|
+
*/
|
|
102
|
+
export async function pragmaWrite(db, statement) {
|
|
103
|
+
await db.exec(`PRAGMA ${statement}`);
|
|
104
|
+
}
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// Transactions
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
/**
|
|
109
|
+
* Per-connection transaction depth, keyed by handle so that multiple open
|
|
110
|
+
* databases (tests routinely open several) can't corrupt each other's nesting.
|
|
111
|
+
*/
|
|
112
|
+
const txDepth = new WeakMap();
|
|
113
|
+
/** Serialises top-level transactions per handle. Nested calls are savepoints. */
|
|
114
|
+
const txTail = new WeakMap();
|
|
115
|
+
/**
|
|
116
|
+
* True while running inside a transaction's `fn` on this async stack.
|
|
117
|
+
* Concurrent callers on the same handle also see `txDepth > 0`, so depth
|
|
118
|
+
* alone cannot tell nest from overlap — two MCP tools share one connection,
|
|
119
|
+
* and an await in the first would otherwise let the second BEGIN (or
|
|
120
|
+
* SAVEPOINT) inside the first's work.
|
|
121
|
+
*/
|
|
122
|
+
const txContext = new AsyncLocalStorage();
|
|
123
|
+
/**
|
|
124
|
+
* Run `fn` inside a transaction: commit on success, roll back on throw, and
|
|
125
|
+
* return whatever `fn` returns.
|
|
126
|
+
*
|
|
127
|
+
* Nested calls on the same async stack are SAVEPOINTs, because SQLite cannot
|
|
128
|
+
* BEGIN a transaction within a transaction. Concurrent callers on the same
|
|
129
|
+
* handle are queued instead — an await inside the first must not let the
|
|
130
|
+
* second BEGIN (or SAVEPOINT) into its work. Two MCP tools share one
|
|
131
|
+
* connection; this is what keeps them from interleaving.
|
|
132
|
+
*
|
|
133
|
+
* A failing inner block rolls back only its own work and rethrows, leaving the
|
|
134
|
+
* outer transaction to decide — the same semantics as before.
|
|
135
|
+
*/
|
|
136
|
+
export async function withTransaction(db, fn) {
|
|
137
|
+
return db.transaction(fn);
|
|
138
|
+
}
|
|
139
|
+
async function withSqliteTransaction(db, fn) {
|
|
140
|
+
if (txContext.getStore()) {
|
|
141
|
+
return runSqliteTransaction(db, fn);
|
|
142
|
+
}
|
|
143
|
+
const prev = txTail.get(db) ?? Promise.resolve();
|
|
144
|
+
const run = prev.then(() => txContext.run(true, () => runSqliteTransaction(db, fn)), () => txContext.run(true, () => runSqliteTransaction(db, fn)));
|
|
145
|
+
txTail.set(db, run.then(() => undefined, () => undefined));
|
|
146
|
+
return run;
|
|
147
|
+
}
|
|
148
|
+
async function runSqliteTransaction(db, fn) {
|
|
149
|
+
const depth = txDepth.get(db) ?? 0;
|
|
150
|
+
const nested = depth > 0;
|
|
151
|
+
// Name includes the depth so nested savepoints can't collide.
|
|
152
|
+
const savepoint = `om_sp_${depth}`;
|
|
153
|
+
// BEGIN IMMEDIATE, not BEGIN. A deferred transaction starts as a reader and
|
|
154
|
+
// upgrades at the first write — and if another connection wrote in the
|
|
155
|
+
// meantime, SQLite fails that upgrade with SQLITE_BUSY *immediately*, ignoring
|
|
156
|
+
// busy_timeout, because waiting could deadlock two transactions each holding a
|
|
157
|
+
// read lock. IMMEDIATE takes the write lock up front, where busy_timeout does
|
|
158
|
+
// apply, so a concurrent writer waits its turn instead of failing.
|
|
159
|
+
//
|
|
160
|
+
// This matters because two AI tools legitimately share one database. Under
|
|
161
|
+
// BEGIN, a capture could fail with "database is locked" purely because another
|
|
162
|
+
// client's server happened to be writing — returned to the assistant as an
|
|
163
|
+
// error result, not raised, so the fact was simply lost. Every caller of this
|
|
164
|
+
// helper writes, so there is no read-only path to penalise.
|
|
165
|
+
if (nested)
|
|
166
|
+
await db.exec(`SAVEPOINT ${savepoint}`);
|
|
167
|
+
else
|
|
168
|
+
await db.exec("BEGIN IMMEDIATE");
|
|
169
|
+
txDepth.set(db, depth + 1);
|
|
170
|
+
try {
|
|
171
|
+
const result = await fn();
|
|
172
|
+
if (nested)
|
|
173
|
+
await db.exec(`RELEASE ${savepoint}`);
|
|
174
|
+
else
|
|
175
|
+
await db.exec("COMMIT");
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
try {
|
|
180
|
+
if (nested) {
|
|
181
|
+
// Undo this block's work, then discard the savepoint so the outer
|
|
182
|
+
// transaction isn't left holding a stale entry on the stack.
|
|
183
|
+
await db.exec(`ROLLBACK TO ${savepoint}`);
|
|
184
|
+
await db.exec(`RELEASE ${savepoint}`);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
await db.exec("ROLLBACK");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// The transaction may already have been resolved (e.g. a statement
|
|
192
|
+
// aborted it). Never mask the original error with a rollback failure.
|
|
193
|
+
}
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
txDepth.set(db, depth);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/db/connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA8C3C,MAAM,eAAe;IAC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E;IAC/E,2EAA2E;IAC3E,8EAA8E;IAC9E,4DAA4D;IAC5D,IAAI,MAAM;QAAE,MAAM,EAAE,CAAC,IAAI,CAAC,aAAa,SAAS,EAAE,CAAC,CAAC;;QAC/C,MAAM,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IACtC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;IAE3B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,EAAE,EAAE,CAAC;QAC1B,IAAI,MAAM;YAAE,MAAM,EAAE,CAAC,IAAI,CAAC,WAAW,SAAS,EAAE,CAAC,CAAC;;YAC7C,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC7B,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,CAAC;YACH,IAAI,MAAM,EAAE,CAAC;gBACX,kEAAkE;gBAClE,6DAA6D;gBAC7D,MAAM,EAAE,CAAC,IAAI,CAAC,eAAe,SAAS,EAAE,CAAC,CAAC;gBAC1C,MAAM,EAAE,CAAC,IAAI,CAAC,WAAW,SAAS,EAAE,CAAC,CAAC;YACxC,CAAC;iBAAM,CAAC;gBACN,MAAM,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,mEAAmE;YACnE,sEAAsE;QACxE,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IACzB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisory lock for consolidation. Prevents concurrent consolidation runs.
|
|
3
|
+
* Single-row table enforced by CHECK(id = 1).
|
|
4
|
+
*/
|
|
5
|
+
import type { Db } from "./connection.js";
|
|
6
|
+
export interface LockState {
|
|
7
|
+
holder: string;
|
|
8
|
+
started_at: string;
|
|
9
|
+
}
|
|
10
|
+
/** Try to acquire the lock. Returns true if acquired. */
|
|
11
|
+
export declare function acquireLock(db: Db, holder: string): Promise<boolean>;
|
|
12
|
+
/** Release the lock. Returns true if released, false if holder didn't match. */
|
|
13
|
+
export declare function releaseLock(db: Db, holder: string): Promise<boolean>;
|
|
14
|
+
/** Check if a lock exists and return its state. */
|
|
15
|
+
export declare function getLockState(db: Db): Promise<LockState | null>;
|
|
16
|
+
//# sourceMappingURL=consolidation-lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consolidation-lock.d.ts","sourceRoot":"","sources":["../../src/db/consolidation-lock.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAM1C,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB;AAaD,yDAAyD;AACzD,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAkD1E;AAED,gFAAgF;AAChF,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAO1E;AAED,mDAAmD;AACnD,wBAAsB,YAAY,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAKpE"}
|