diff --git a/.claude/skills/architecture.md b/.claude/skills/architecture.md
index de046b17..4de3c287 100644
--- a/.claude/skills/architecture.md
+++ b/.claude/skills/architecture.md
@@ -2,7 +2,7 @@
## What Is Brainy
-@soulcraft/brainy (v7.17.0) is a Universal Knowledge Protocol -- a Triple Intelligence database combining vector search, graph traversal, and metadata filtering in a single library. Published to npm as a public MIT-licensed package.
+@soulcraftlabs/brainy (v7.17.0) is a Universal Knowledge Protocol -- a Triple Intelligence database combining vector search, graph traversal, and metadata filtering in a single library. Published to npm as a public MIT-licensed package.
## Core Architecture
diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml
index 5e93cd96..da5887f6 100644
--- a/.forgejo/workflows/ci.yml
+++ b/.forgejo/workflows/ci.yml
@@ -5,6 +5,10 @@ name: CI
# sequential, so tag-triggered matrix jobs (~22 min) would queue AHEAD of the
# tag's publish-source run and starve every release (observed on 8.10.3 and
# 9.0.0: the publish sat behind the tag's own redundant CI).
+concurrency:
+ group: ci-${{ github.ref }}
+ cancel-in-progress: true
+
on:
push:
branches: ['**']
diff --git a/.forgejo/workflows/publish-source.yml b/.forgejo/workflows/publish-source.yml
index a08875ae..6bd42b2a 100644
--- a/.forgejo/workflows/publish-source.yml
+++ b/.forgejo/workflows/publish-source.yml
@@ -12,6 +12,11 @@ on:
push:
tags:
- 'v*'
+ workflow_dispatch:
+ inputs:
+ ref_reason:
+ description: 'why this manual run (e.g. tag event dropped)'
+ required: false
jobs:
publish:
@@ -32,7 +37,7 @@ jobs:
run: |
set -eo pipefail
- SOURCE_NPM_REG="https://source.soulcraft.com/api/packages/soulcraft/npm/"
+ SOURCE_NPM_REG="https://source.soulcraft.com/api/packages/soulcraftlabs/npm/"
VERSION="$(node -p "require('./package.json').version")"
# The dist-tag follows the version: a prerelease (any hyphen —
# 10.4.0-rc.1) publishes under 'rc' and must NEVER move 'latest' —
@@ -43,13 +48,13 @@ jobs:
case "$VERSION" in
*-*) NPM_TAG="rc" ;;
esac
- echo "Publishing @soulcraft/brainy@${VERSION} to The Source registry (dist-tag: ${NPM_TAG})..."
+ echo "Publishing @soulcraftlabs/brainy@${VERSION} to The Source registry (dist-tag: ${NPM_TAG})..."
TMPRC="$(mktemp)"
chmod 600 "$TMPRC"
{
- echo "@soulcraft:registry=${SOURCE_NPM_REG}"
- echo "//source.soulcraft.com/api/packages/soulcraft/npm/:_authToken=${FORGE_NPM_TOKEN}"
+ echo "@soulcraftlabs:registry=${SOURCE_NPM_REG}"
+ echo "//source.soulcraft.com/api/packages/soulcraftlabs/npm/:_authToken=${FORGE_NPM_TOKEN}"
} > "$TMPRC"
# The release script bumps package.json's version before it tags, so
@@ -64,7 +69,7 @@ jobs:
# exit code: a benign duplicate publish (a prior run, or a mirror, already
# landed this exact version) reports failure even though the registry
# already holds the right content.
- LANDED_VERSION="$(npm view "@soulcraft/brainy@${VERSION}" version --userconfig "$TMPRC" 2>/dev/null || echo "")"
+ LANDED_VERSION="$(npm view "@soulcraftlabs/brainy@${VERSION}" version --userconfig "$TMPRC" 2>/dev/null || echo "")"
rm -f "$TMPRC"
if [ "$LANDED_VERSION" != "$VERSION" ]; then
@@ -73,7 +78,7 @@ jobs:
fi
if [ "$PUBLISH_OK" = true ]; then
- echo "Published and verified @soulcraft/brainy@${VERSION} on The Source registry."
+ echo "Published and verified @soulcraftlabs/brainy@${VERSION} on The Source registry."
else
- echo "::warning::npm publish reported failure, but readback confirms @soulcraft/brainy@${VERSION} is already live on The Source (a prior run or mirror landed it) — treating this run as successful, since the registry content is correct. Any OTHER failure mode would have failed the readback check above instead."
+ echo "::warning::npm publish reported failure, but readback confirms @soulcraftlabs/brainy@${VERSION} is already live on The Source (a prior run or mirror landed it) — treating this run as successful, since the registry content is correct. Any OTHER failure mode would have failed the readback check above instead."
fi
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd64d88f..a54d609e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,101 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
-### [10.4.0-rc.1](https://source.soulcraft.com/soulcraft/brainy/compare/v10.3.1...v10.4.0-rc.1) (2026-08-24)
+### [10.4.4](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.3...v10.4.4) (2026-08-28)
+
+- fix(vfs): the old-root sweep narrates only when it has something to say (d49148e1)
+- fix(tests): the health-gate pin follows the verdict, and the VFS suite uses its own store (42e2da25)
+- Merge branch 'next/open-lazy-open-and-counts' (5ebd3b40)
+- docs: the contract manifest stands alone; public docs describe this engine only (a8c724a2)
+- docs(releases): 10.4.4 consumer notes — correctness and observability, with the performance line stated exactly (61a46927)
+- docs: measurements in public history carry numbers, not provenance (02c61636)
+- feat(open): name the two steps that hold the vfs-bootstrap phase (2cf38010)
+- fix(storage): a dead flush watch falls back to the 500ms poll, not the 30s sweep (5c22f950)
+- fix(storage): the flush watcher cannot arm twice in its async window (16d2e1a9)
+- perf(idle): the flush-request watch is event-driven; the heartbeat is observability (fb1da1c5)
+- perf(open): answer "are there any entities?" with one directory read (417ddb51)
+- perf(generations): discover generations by directory name, not by walking the log (9dd39921)
+- fix(flush): clear() and repairIndex() set the dirty witness themselves (e4c27fbc)
+- feat(open): the open names the STEP that cost the time, not just the phase (5a091cca)
+- perf(vfs): the old-root sweep runs once per store, not once per open (4a67aa0f)
+- chore: keep the generated neural stamps at main's values (c1f09723)
+- feat(contract): declare contract 1, serve three operators, refuse four by name (48802ba3)
+- fix(open): a provider rebuilding itself is a third state, not a CRITICAL (50676c02)
+- feat(open): open never waits for a provider that is rebuilding itself (131daa08)
+- perf(flush): an idle brain does no work — no periodic flush without a write (f5a6cb3f)
+- feat(repair): repairIndex narrates every phase and its receipt carries the walls (3fffd9c6)
+- fix(storage): a suspect count ledger heals itself, and counts.json is written atomically (f4e2d34b)
+- feat(open): the open narrates itself, on a channel production cannot clamp (afe08a1f)
+- fix(storage): a clean close is recorded, and the writer lock is always given up (e652162c)
+- docs: repository links point at soulcraftlabs/open-brainy — the soulcraft/brainy path becomes the native engine's repo tonight (38c3397b)
+
+
+### [10.4.3](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.2...v10.4.3) (2026-08-27)
+
+- Merge branch 'next/open-brainy-rename' (a58372f0)
+- chore: rename to @soulcraftlabs/brainy for Open Brainy on The Source (a99b1e83)
+- docs(releases): 10.4.3 — Open Brainy's first release under the new name, same engine as 10.4.2; The Source is the one registry (9f248b24)
+
+
+### [10.4.2](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.2-rc.1...v10.4.2) (2026-08-27)
+
+- docs(releases): 10.4.1 and 10.4.2 consumer notes; 10.4.2 is the last MIT release under this name, Open Brainy continues at @soulcraftlabs/brainy (a082e0ef)
+
+
+### [10.4.2-rc.1](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.1...v10.4.2-rc.1) (2026-08-27)
+
+- Merge branch 'next/zero-norm-unvector-door' (9b84ef5b)
+- fix(vectors): a zero-norm vector is not a vector, canonical side included, plus the sanctioned unvector door (0de76659)
+- fix(hnsw): skip unvectored rows on rebuild; refuse empty vectors in the index (8fc553b1)
+- fix(storage): derive the canonical count ledger from identity records, stamp the derivation rule, and mark legacy-derived ledgers suspect at load (fd6b4ce4)
+- Merge branch 'next/enumeration-identity-rekey' (204d74c1)
+- fix(storage): enumeration re-keys on the identity record, not the vector leg (f8d8ce16)
+- fix(init): rethrow plugin activation failures with the original error as cause so the originating frame survives to the caller (2496e09a)
+- Merge branch 'next/vfs-root-zero-norm' (4c7b0fab)
+- fix(vfs): the VFS root never persists a zero-norm vector (c6cc0de9)
+- build: derive generated-file stamps from git commit time, not wall clock (8a5c1245)
+- Merge remote-tracking branch 'origin/release/10.4.1' (aad9e2ee)
+- docs(concepts): the serving law — a failure is graded by whether an answer could be wrong, never by the cost of the fix; reads refuse per family (2914e0eb)
+- chore(release): 10.4.1-rc.1 (7870dc40)
+
+
+### [10.4.1](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.0...v10.4.1) (2026-08-26)
+
+- fix(reads): the read gate is per-family; a write carrying unchanged data never re-embeds (c039411e)
+- docs(guide): the docs pipeline publishes through the ingest API — the separate deploy step is retired (21e506e8)
+
+
+### [10.4.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.0-rc.4...v10.4.0) (2026-08-26)
+
+- docs(releases): the 10.4.0 entry catches up to the late trains — repair routing, the vector ledger and open-gate leg, the loud config guard, the JSON-safe crossing (834149ed)
+
+
+### [10.4.0-rc.4](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.0-rc.3...v10.4.0-rc.4) (2026-08-25)
+
+- feat(vector): the vectored-noun scalar joins the count ledger; the open gate closes the vector leg (9730835b)
+
+
+### [10.4.0-rc.3](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.0-rc.2...v10.4.0-rc.3) (2026-08-25)
+
+- fix(update-seam): the metadata crossing never carries BigInt endpoint ints (f4780c8e)
+- Merge branch 'worktree-agent-ad3aff0dffd17a6eb' (f14da34b)
+- fix(add): empty string is real data, not a missing field (258e9042)
+- feat(vfs): implement readdir's recursive option — typed since 7.30, never read (fc516da6)
+- feat(open-path): init never gates on the embedding model; open goes concurrent; slow opens narrate (96624f40)
+
+
+### [10.4.0-rc.2](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.4.0-rc.1...v10.4.0-rc.2) (2026-08-25)
+
+- test(readiness): the report helper's clock freezes — two independently-built reports compared across a millisecond tick made the plant lane red (39b916a3)
+- feat(repair): a heal:'repair' verdict routes to the provider's own incremental repair() (553e0d97)
+- fix(storage): an unknown nested storage config can never silently land on the shared default root (ddd5e719)
+- docs(release): the 10.4.0 entry, the index-health concept doc, and the API surfaces — written from the tree, not the plan (8cced871)
+- fix(plugins): the silent-degrade doors close — a broken accelerator install can never read as absent (b9ba50fb)
+- feat(recovery): the catchup verdict is consumed; verb rows go live; the metadata rebuild goes online (18f172e0)
+- feat(health): the gate reads the named report — reads refuse loudly, never rebuild; open serves before it returns; the ceremony door (f8f64780)
+
+
+### [10.4.0-rc.1](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.3.1...v10.4.0-rc.1) (2026-08-24)
- ci(publish): the home dist-tag follows the version — a prerelease publishes under 'rc' and never moves 'latest' (a1376e4a)
- chore(release): --source-only — a home-only prerelease mode (The Source, never the storefront) (dcbad176)
@@ -15,13 +109,13 @@ All notable changes to this project will be documented in this file. See [standa
- ci(gate): the machine-health preflight and the truncation verdict guard (1e046aa1)
-### [10.3.1](https://source.soulcraft.com/soulcraft/brainy/compare/v10.3.0...v10.3.1) (2026-08-18)
+### [10.3.1](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.3.0...v10.3.1) (2026-08-18)
- docs(releases): the 10.3.1 consumer entry — the fold that behaves (900cc895)
- fix(recovery): the fold streams and narrates; the checkpoint chain arms at the flip (ed7d1db9)
-### [10.3.0](https://source.soulcraft.com/soulcraft/brainy/compare/v10.2.0...v10.3.0) (2026-08-18)
+### [10.3.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.2.0...v10.3.0) (2026-08-18)
- docs(releases): the 10.3.0 consumer entry — the trust-and-provenance release (97d75649)
- fix(locks): the fence keys ownership on pid+hostname — a same-process re-open never fences its predecessor (0991cf28)
@@ -30,14 +124,14 @@ All notable changes to this project will be documented in this file. See [standa
- feat(log): system commits carry their origin; the attested per-id reconcile door (9ac9e706)
-### [10.2.0](https://source.soulcraft.com/soulcraft/brainy/compare/v10.1.0...v10.2.0) (2026-08-17)
+### [10.2.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.1.0...v10.2.0) (2026-08-17)
- docs(releases): the 10.2.0 consumer entry — adoption completes in one call (97538e1f)
- ci: the correctness plant runs integration + conformance on every push — a release never waits on a second machine (b17fdc8e)
- fix(adoption): the baseline backfill runs to completion — one call adopts a pre-log baseline of any size (a5a18838)
-### [10.1.0](https://source.soulcraft.com/soulcraft/brainy/compare/v10.0.0...v10.1.0) (2026-08-13)
+### [10.1.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v10.0.0...v10.1.0) (2026-08-13)
- docs(releases): the 10.1.0 consumer entry — bounded recovery, restore founding, the two write-path cures (7d3c8696)
- fix(restore): a restore is an unclean event — the swap runs quiesced and the snapshot's durability stamps never survive it (9ca80667)
@@ -46,7 +140,7 @@ All notable changes to this project will be documented in this file. See [standa
- feat(query): the sparse-store cut — where on a never-carried field serves operator truth, never a refusal (7b67db4d)
-### [10.0.0](https://source.soulcraft.com/soulcraft/brainy/compare/v9.0.0...v10.0.0) (2026-08-12)
+### [10.0.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v9.0.0...v10.0.0) (2026-08-12)
- fix(adoption): the baseline backfill cures hydration-law drift — existing brains reach the crash-safe default with zero operator steps (25f0dd96)
- fix(adoption): the reserved-root mint exemption — int 0 is legitimate for exactly one id (2abe8b38)
@@ -78,7 +172,7 @@ All notable changes to this project will be documented in this file. See [standa
- test: version-coupling pins go major-agnostic — the 8.x literals broke at the 9.0.0 bump while the coupling law itself behaved correctly (8a6807e8)
-### [9.0.0](https://source.soulcraft.com/soulcraft/brainy/compare/v8.11.0...v9.0.0) (2026-08-04)
+### [9.0.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v8.11.0...v9.0.0) (2026-08-04)
- docs: 9.0 namespace-migration guide — the simple story + the mechanical sweep checklist, published for humans and tooling alike (61ab9db2)
- fix(release): storefront leg republishes CI's exact forge artifact — byte-identity by construction, verified by cross-registry shasum before the ceremony reports success (d89df2ed)
@@ -113,7 +207,7 @@ All notable changes to this project will be documented in this file. See [standa
- feat: scanFacts liveness contract — first batch or loud failure within a documented bound (f8e6da2b)
-### [8.11.0](https://source.soulcraft.com/soulcraft/brainy/compare/v8.10.1...v8.11.0) (2026-07-27)
+### [8.11.0](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v8.10.1...v8.11.0) (2026-07-27)
- docs: the last two archived-host links point home (91ef1c8b)
- feat: includeHidden — export carries every visibility tier for migration-grade canon completeness (63c1eeb9)
@@ -122,19 +216,19 @@ All notable changes to this project will be documented in this file. See [standa
- ci: run the pipeline on the forge (999d0ebb)
-### [8.10.3](https://source.soulcraft.com/soulcraft/brainy/compare/v8.10.2...v8.10.3) (2026-08-03)
+### [8.10.3](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v8.10.2...v8.10.3) (2026-08-03)
- docs: dedupe the 8.10.2 release-notes entry the cherry doubled onto the branch (8c956608)
- fix: user metadata named 'level' is a real field everywhere — the engine-internal node layer no longer shadows it in sort/filter/aggregation, and the indexing views stop stamping a phantom 0 into its column; index epoch 2 rebuilds existing brains at first open (958a0859)
-### [8.10.2](https://source.soulcraft.com/soulcraft/brainy/compare/v8.10.1...v8.10.2) (2026-07-29)
+### [8.10.2](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v8.10.1...v8.10.2) (2026-07-29)
- docs: 8.10.2 consumer release notes — update() write granularity, PathResolver idle-log fix, graph-lsm key recognition (a0123b5b)
- fix: metadata-only update() never rewrites the noun record — the unconditional whole-vector save turned per-entity stat touches into full rewrites+fsync, amplifying read-heavy sweeps into disk saturation on a production deployment (5b65eb82)
-### [8.10.1](https://source.soulcraft.com/soulcraft/brainy/compare/v8.10.0...v8.10.1) (2026-07-24)
+### [8.10.1](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v8.10.0...v8.10.1) (2026-07-24)
- refactor: remove the orphaned transaction-result type left behind by the dead-path removal (edf123a5)
- fix: warm() metadata surface routes through the active provider (warm hook added to the metadata contract); add maintenanceDebt() observability surface (5b2cbf74)
diff --git a/CLAUDE.md b/CLAUDE.md
index c7336a18..56df0b72 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -12,13 +12,13 @@ Handoff file: `/home/dpsifr/.strategy/PLATFORM-HANDOFF.md`
**Brainy's current open actions:** None. MIT open-source — no platform-specific actions.
-**Current version:** run `npm view @soulcraft/brainy version` (never trust a hardcoded number here — this line went stale for months); consumer-facing changes tracked in `RELEASES.md`
+**Current version:** run `npm view @soulcraftlabs/brainy version --registry https://source.soulcraft.com/api/packages/soulcraftlabs/npm/` (never trust a hardcoded number here — this line went stale for months); consumer-facing changes tracked in `RELEASES.md`
---
## Project Overview
-Brainy is a Universal Knowledge Protocol -- a Triple Intelligence database that combines vector similarity search, graph traversal, and metadata filtering into a single TypeScript library. Published as `@soulcraft/brainy` on npm under the MIT license.
+Brainy is a Universal Knowledge Protocol -- a Triple Intelligence database that combines vector similarity search, graph traversal, and metadata filtering into a single TypeScript library. Published as `@soulcraftlabs/brainy` on The Source (source.soulcraft.com registry) under the MIT license.
## Getting Started
@@ -91,7 +91,7 @@ test: add/update tests (patch version bump)
## Docs Pipeline — soulcraft.com/docs
-Docs in `docs/**/*.md` are published with the npm package (included in `files`) and synced to soulcraft.com/docs on every portal deploy. Frontmatter controls what appears publicly.
+Docs in `docs/**/*.md` are published with the npm package (included in `files`) and go live on soulcraft.com/docs via the docs ingest API: the release script's `scripts/push-docs.js` step POSTs every public doc to `https://soulcraft.com/api/docs/ingest` (auth: `DOCS_INGEST_SECRET` in the environment). No separate deploy step is involved (the old deploy-to-publish flow was retired in a platform change, 2026-08). Frontmatter controls what appears publicly.
### Docs check triggers
@@ -161,9 +161,9 @@ npm run release:major # Breaking changes (rare, manual decision)
The script: verifies clean git state, builds, tests, bumps version, updates CHANGELOG.md, commits, tags, pushes, publishes to npm, and creates a GitHub release.
After a successful release, remind the user:
-> "Published. Deploy portal to pick up the new docs → go to the portal project and deploy."
+> "Published. Docs are live on soulcraft.com/docs (pushed via the ingest API during the release) — spot-check a changed page with curl."
-Do NOT deploy portal from here. Portal is always deployed separately from within the portal project.
+There is no separate deploy step anymore. If the docs push failed (the script warns loudly), re-run `node scripts/push-docs.js` with `DOCS_INGEST_SECRET` set.
## Closed-Source Product Names — HARD RULE
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index d277091d..54d4f784 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -6,7 +6,7 @@ may find elsewhere in the repo's history.
## Where the project lives
-The source of truth is a self-hosted forge: **source.soulcraft.com/soulcraft/brainy**.
+The source of truth is a self-hosted forge: **source.soulcraft.com/soulcraftlabs/open-brainy**.
It's anonymously readable and cloneable — no account needed to browse, clone,
or build.
@@ -31,7 +31,7 @@ fine) to talk through the approach saves everyone rework.
## Development setup
```bash
-git clone https://source.soulcraft.com/soulcraft/brainy.git
+git clone https://source.soulcraft.com/soulcraftlabs/open-brainy.git
cd brainy
npm install
npm run build
@@ -57,6 +57,17 @@ see `package.json` for `test:integration`, `test:coverage`, and friends.
description states a number, cite the benchmark that produced it (see
[docs/performance-envelopes.md](docs/performance-envelopes.md) for the
pattern). Don't state an estimate as if it were measured.
+- **Measurements carry numbers, not provenance.** Public commit messages and
+ docs give the SHAPE a number was taken at and never where it was taken: no
+ hostnames, no store or deployment identities, no operational anecdotes about
+ someone's running system. "A 14,056-noun / 72,679-verb production-shaped
+ store, measured solo under an exclusive lock" tells a reader everything the
+ number depends on; the machine it ran on and whose data it was tell them
+ nothing except where somebody's infrastructure lives.
+- **Documents that answer or reference a confidential specification never enter
+ this repository, even summarized.** The public docs describe THIS engine and
+ the published contract, and nothing else — a summary of a private document is
+ still that document's contents.
## License
diff --git a/README.md b/README.md
index ca558340..762c9ec3 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
-
+
Brainy
@@ -11,9 +11,9 @@
-
-
-
+
+
+
@@ -30,6 +30,8 @@
---
+**Open Brainy** is the MIT engine — the open API, client library, types, and protocol; an openly specified canonical on-disk format; and this TypeScript reference engine, scoped as a single-node engine for stores up to roughly one million rows. `@soulcraft/brainy` 10.4.2 was the last release under the old package name — the name passes to the native engine, **Brainy**, at 11.0.0: the same API over the same open format at production scale, and it requires a license.
+
Built because we were tired of stitching a vector store to a graph database to a document store — and spending weeks on plumbing before writing a line of business logic. Brainy indexes every fact **three ways at once** and lets one call query them together:
| You write | Brainy indexes it as | You query it with |
@@ -45,12 +47,14 @@ It runs **inside your process** — no server, no Docker, nothing to operate —
## Quick start
```bash
-bun add @soulcraft/brainy # Bun ≥ 1.1 — recommended
-npm install @soulcraft/brainy # Node.js ≥ 22
+bun add @soulcraftlabs/brainy # Bun ≥ 1.1 — recommended
+npm install @soulcraftlabs/brainy # Node.js ≥ 22
```
+> **Registry**: add `@soulcraftlabs:registry=https://source.soulcraft.com/api/packages/soulcraftlabs/npm/` to your `.npmrc` (anonymous read).
+
```javascript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy() // in-memory; one line swaps to disk
await brain.init()
diff --git a/RELEASES.md b/RELEASES.md
index cc0272c3..c875cb26 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -1,7 +1,14 @@
# @soulcraft/brainy — Release Notes for Consumers
+Machine-readable release notes are published at
+https://source.soulcraft.com/soulcraftlabs/releases/raw/branch/main/open-brainy.json
+(this engine) and
+https://source.soulcraft.com/soulcraftlabs/releases/raw/branch/main/brainy.json
+(the product engine) — read by HQ's `/hq/releases` door, and the source of
+truth ahead of this file.
+
This file is the **quick reference for downstream sessions** tracking Brainy changes.
-Full auto-generated changelog: `CHANGELOG.md` · Releases: https://source.soulcraft.com/soulcraft/brainy/releases
+Full auto-generated changelog: `CHANGELOG.md` · Releases: https://source.soulcraft.com/soulcraftlabs/open-brainy/releases
**How to use:** Brainy is the underlying data engine for downstream applications. Read this when:
- Upgrading `@soulcraft/brainy` in your application
@@ -31,6 +38,330 @@ is sometimes cited as a 7.x removal — those methods never existed on 7.x; the
---
+## v10.4.4 — 2026-08-28
+
+**A correctness and observability release.** The headline is not speed: it is that a
+restart now tells you the truth about itself, a store stops lying about how much it
+holds, and the engine stops doing work nobody asked for. There is a performance
+improvement and it is modest; it is stated exactly below rather than rounded up.
+
+### The dark restart — fixed at the root
+
+A service could stop cleanly, exit 0, having awaited `close()` on every store it held,
+and its next boot would announce `Overwriting stale writer lock … appears dead` for
+every one of them. Nothing had crashed. Two deployments hit this; the same defect also
+made those boots pay a crash-recovery fold they did not owe.
+
+The cause was not the lock. `close()` released it correctly — when it got there. A
+failure part-way through close skipped both the release AND the clean-shutdown marker,
+and "the recorded pid is gone" reads identically for an orderly restart and a crash.
+
+- `close()` is now two parts and the second is unconditional: the flush-request watcher,
+ the **writer lock**, the VFS timers and the terminal `closed` flag are released whether
+ the durable steps succeeded or not. The original failure is narrated with what it costs
+ the next open, then rethrown.
+- Releasing the lock writes a **clean-close record** naming the lock generation it gave
+ up. The next open reads that record instead of guessing: recorded → nothing to recover;
+ absent → it says so, and names the recovery it is about to run. This also ends two
+ long-standing false alarms — a recycled pid locking a store out of its own reopen, and
+ `Re-acquiring writer lock … this is a bug` after a perfectly clean close.
+- The signal path stopped failing in a batch. One store's failing flush used to strand
+ every remaining store's lock and markers — at exit code 0. Now: per-store isolation, the
+ generation store's close (the marker) is part of shutdown, the lock goes in a `finally`,
+ and the handler no longer calls `process.exit()` when the host application has its own
+ signal handler, a race that truncated the host's own shutdown mid-flight.
+
+### The count ledger stops lying, and `counts.json` is written atomically
+
+The all-tier scalars are the denominator a coverage check subtracts against. A ledger
+derived under the old rule — one entity per id DIRECTORY — counted ghost and scar
+containers as rows, and was only FLAGGED suspect: it went on serving wrong numbers for
+the life of the store. Two copies of one archive could disagree, and a downstream index
+heal reported remaining work that did not exist.
+
+- Such a ledger now derives itself honestly **in the background** after the open, counting
+ identity records, and persists the correction stamped. Nothing waits for it, because no
+ read is served from a denominator.
+- A derivation that raced a write refuses to stamp its number: one retry on a quiet store,
+ then the ledger stays SUSPECT and names `repairIndex()` as the door that recounts under
+ a barrier.
+- `counts.json` is written temp+rename. A truncating write left a window in which a
+ concurrent reader saw the file EMPTY — and an unparseable ledger sends the next open
+ down the full-rescan path, so the cheapest file in the store was buying the most
+ expensive recovery.
+
+### An open and a repair narrate themselves — on a channel a log level cannot silence
+
+A store could open for three minutes and print nothing at all. The phase timings existed;
+they were written to a channel that every production-looking environment clamps away.
+
+- Narration moved to an always-visible channel. An open now heartbeats the phase it is in,
+ names each phase as it ends with what it was paying for, and names the expensive STEP
+ inside a phase. `repairIndex()` does the same and its receipt carries a per-family
+ `durationMs` — a repair that ran for half an hour with no output could only be watched
+ through `top`.
+- A brain nobody has written to now does nothing: a flush over a clean store is a no-op
+ and says nothing, the graph index's auto-flush asks before it acts, and the
+ cross-process flush-request watch is **event-driven** (`fs.watch`) instead of polling a
+ directory every 500 ms per store forever, with a slow safety sweep behind it and a
+ narrated fall back to polling where a filesystem cannot be watched.
+- A provider that is REBUILDING ITSELF is no longer confused with a broken one. `init()`
+ does not wait for it, every other family serves, and that family's doors refuse **by
+ name, carrying the provider's own progress**, saying plainly that they open by
+ themselves and no action is needed. Health narration dedupes by content, so an unchanged
+ verdict is silent however a provider's generation counter moves.
+
+### For operators — one behaviour change
+
+**Four `where` operators that previously returned an empty page now raise
+`INVALID_QUERY`:** `startsWith`, `endsWith`, `matches` and `length`. An equality/range
+posting index cannot evaluate a substring, a pattern or an array length without reading
+every row, and it now refuses by name instead of answering with an empty result that
+looks like an answer.
+
+**Three that previously returned an empty page are now SERVED:** `hasAll`, `noneOf` and
+`excludes`. All 25 accepted operator tokens now agree between this engine and its
+accelerated counterpart.
+
+### Performance — stated exactly
+
+Measured on a 14,056-noun / 72,679-verb production-shaped store, both builds solo under
+an exclusive lock:
+
+- **Warm reopen after a clean close: 85.7 s → 77.0 s (−10.2%).** The whole of that gain is
+ one fix — generation discovery reads directory NAMES instead of recursively walking the
+ entire generation log (−9.2 s, and it scales with history rather than row count). The
+ VFS phase is **unchanged**.
+- **Cold open: −31.4 s** (518.1 s → 486.7 s), of which the count-ledger derivation moving
+ off the critical path accounts for storage-init dropping 5,941 ms → 25 ms.
+- **A dominant ~38 s remains, diagnosed and NOT fixed.** It is not the VFS — the VFS's own
+ init is under 2 s of that phase. It is the log-authority adoption and/or the
+ pending-embed log recovery, both now instrumented so the next measurement names the
+ culprit outright.
+
+Continuing work, named so nobody has to rediscover it: that ~38 s term; making the
+generation store's committed-range set lazy; the hydration path that substitutes
+`Date.now()` for an unreadable stored timestamp (inventing data); and a VFS path-prefix
+filter built with a `$startsWith` spelling no operator set accepts, so
+`searchFiles({ path })` throws today.
+
+---
+
+## v10.4.3 — 2026-08-27 (Open Brainy's first release)
+
+**`@soulcraftlabs/brainy` 10.4.3 is the same engine as `@soulcraft/brainy` 10.4.2, byte for
+byte — only the name, the registry, and the pointers changed.** Install:
+
+```bash
+npm install @soulcraftlabs/brainy
+```
+
+with the registry line in your `.npmrc` (anonymous read):
+
+```
+@soulcraftlabs:registry=https://source.soulcraft.com/api/packages/soulcraftlabs/npm/
+```
+
+- **The Source is the one registry.** Open Brainy publishes to source.soulcraft.com only; the
+ npmjs republish step is retired from the release rail. Existing npmjs versions of
+ `@soulcraft/brainy` stay as they are and receive no new versions.
+- **The repository moved** to `soulcraftlabs/open-brainy` on The Source; the old path redirects.
+- **No engine change.** Everything in the 10.4.2 notes applies unchanged; adoption is one
+ install-line change (`@soulcraft/brainy` → `@soulcraftlabs/brainy`), which downstream
+ applications make together with their native-engine bump.
+
+## v10.4.2 — 2026-08-27 (a zero-norm vector is not a vector)
+
+**This is the last release of the MIT engine under the `@soulcraft/brainy` name.**
+The MIT package continues as **Open Brainy** — `@soulcraftlabs/brainy`: the open API,
+client library, types and protocol, an openly specified canonical format, and the TypeScript
+reference engine, scoped honestly as a single-node engine for stores up to roughly one
+million rows. The `@soulcraft/brainy` name passes to the native engine, **Brainy**, at a
+major version bump; that engine implements the same API over the same open format at
+production scale, requires a license, and refuses loudly without one. Nothing changes
+for existing installs until that major ships; the move is announced with it.
+
+Six fixes, one law: a vector with no magnitude carries no information, so it must
+never reach a vector index — in any engine — and the canonical store must say so.
+
+- **The permanently-unvectored row.** `add({ ..., vector: [] })` (and the same item
+ shape in `addMany` / `transact`) is now the sanctioned "no vector" row: persisted
+ with an empty vector leg, never embedded, never indexed, counted as unvectored in
+ the canonical ledger. Metadata-only rows — telemetry tallies, counters, plumbing —
+ no longer need a placeholder vector and never enter the vector leg. `vector: []`
+ together with `deferEmbedding: true` is refused with a typed error (a supplied
+ vector has nothing to defer). Previously `vector: []` threw a dimension error.
+- **The unvector door.** `update({ id, vector: [] })` (and its `transact()` twin) is
+ the sanctioned way to strip a vector from an existing row: canonical vector → `[]`,
+ removal from the vector index, the vectored ledger decremented exactly once — and
+ idempotent, so a resumed cleanup pass may simply re-issue. It never re-embeds, and
+ it clears a pending deferred-embed marker durably so the background worker cannot
+ re-vector the row later. Note that a rebuild never sheds vectors (it re-derives the
+ index from canonical rows); shedding historical vectors needs this door.
+- **Zero-norm vectors are normalized at the write.** An explicit all-zero vector on
+ any write path is persisted as unvectored (`[]`) with one warning naming the row;
+ the vector-index operations keep their own refusal as a second line. The engine's
+ own VFS root, which used to persist a deliberate all-zero placeholder (harmless
+ under cosine distance, a false attractor under a downstream engine's
+ squared-euclidean serving — a production incident this week), is now created
+ unvectored, and an existing store's legacy root is migrated on open by a single
+ fixed-path read before the health gate runs — never a walk.
+- **Enumeration keys on the identity record.** `getNouns()` / `getVerbs()` and the
+ cursor walks behind them enumerate by the metadata record, the same key the
+ canonical ledger counts by — previously the walk keyed on the vector file, so a
+ row holding metadata but no vector was counted yet never yielded (a permanent
+ "missing" phantom in coverage math), while an orphaned vector-only directory
+ could be yielded as a phantom id. The recovery fold also never deletes an existing
+ vector when it replays a metadata-only after-image (preserve-if-absent). One
+ documented gap remains: a verb's endpoints live only in its vector leg, so a
+ metadata-only verb is counted and loudly skipped, never fabricated — the fix is a
+ canonical-format change and lands with the open format.
+- **The ledger's one-time derivation counts identity records.** Stores upgraded from
+ pre-ledger versions derived their ALL-visibility scalars once by counting id
+ directories, which included ghost and scar containers left by an old partial-delete
+ defect — an inflated denominator whose coverage row could never reach exact. The
+ derivation now counts only directories holding a metadata record, `counts.json`
+ carries a derivation-rule stamp, and a ledger derived under the old rule is marked
+ `suspect` at open (one O(1) field read, one warning) so the online `repairIndex()`
+ path clears it with a real recount.
+- **The vector index refuses what it cannot hold.** `rebuild()` skips unvectored and
+ zero-norm rows (one summary line), re-pins the vector dimension from the first real
+ vector after a restart (previously a restart left the pin unset, so a wrong-length
+ insert became the new pin instead of being rejected), and `addItem` / `updateItem`
+ throw a typed `EmptyVectorIndexError` on a length-0 vector instead of ever storing
+ a vector-less node.
+- **Smaller:** a failing plugin activation now rethrows with the original error as
+ `cause` (the originating file and line survive to the caller's log); build
+ generators stamp from the repository history of their inputs instead of wall clock,
+ so two builds of the same tree are byte-identical.
+
+Adoption: one restart, paired with its native-engine release. The first open of an
+existing store runs the legacy-root migration (one narrated line) and, on stores that
+upgraded from pre-ledger versions, marks the ledger suspect until the next sanctioned
+recount — no rebuild in either case.
+
+## v10.4.1 — 2026-08-26 (reads refuse per family; an unchanged write never re-embeds)
+
+Two production defects from the same week, fixed together as a patch to 10.4.0.
+
+- **The read gate is per family.** A read now refuses only when the index family it
+ actually consults is unhealthy: a metadata filter is served while the vector leg is
+ rebuilding; a semantic query is refused only by the vector family; a graph
+ traversal only by the graph family. Previously any unhealthy family refused every
+ read on the brain — under a long vector rebuild, a production deployment's
+ metadata-only reads were refused for the duration, and the retries became a write
+ pump of their own.
+- **Unchanged data never re-embeds.** `update()` compares the incoming `data`
+ structurally with the stored record; an update carrying identical data (a common
+ shape for periodic upserts) no longer embeds again and no longer churns the vector
+ leg. Previously every such update re-embedded and re-inserted, which under load
+ saturated the vector index with near-identical vectors.
+
+Adoption: one restart, paired with its native-engine release.
+
+## v10.4.0 — 2026-08-25 (the health report has a name)
+
+Three related cures, one root cause: an index deciding whether it could be trusted
+by sampling itself instead of by exact accounting. This release replaces every
+sampled self-probe with ledger-derived truth, and a read against an unhealthy index
+now refuses loudly instead of guessing.
+
+- **The canonical count ledger.** Storage now tracks two scalars per family
+ (nouns/verbs) on the write path: the user-facing `counted` total — unchanged,
+ still what `getNounCount()` / `getVerbCount()` return — and a new ALL-visibility
+ `all` total covering every tier, the real denominator a derived index's own
+ coverage math needs. The unfiltered storage-level `totalCount` returned by
+ `getNouns()` / `getVerbs()` is now this unclamped ALL scalar; previously it could
+ only ever move up (`Math.max(scalar, scanned)`), so an inflated counter could
+ never self-correct. A delete that cannot prove the record it removed actually
+ existed (no canonical read, no prior image available) no longer decrements on
+ faith — it marks the ledger `suspect` (narrated once per session) instead of
+ silently drifting, and the next `repairIndex()` clears the flag with a real
+ recount.
+- **One contract for a throwing health probe.** A provider's `validateInvariants()`
+ is documented to never throw — but if one does anyway (a bug, a transient fault),
+ it is now read the same way everywhere: `heal: 'none'`, the error named in the
+ report, never synthesized into a rebuild trigger and never swallowed into "looks
+ fine." A flaky check can no longer buy itself a rebuild. `repairIndex()`'s
+ per-family receipt also gains `missing` (an exact count plus a capped id sample),
+ `rebuilt` (a full rebuild ran, vs. an incremental heal), and `reason`.
+- **The named health report; reads refuse instead of rebuilding.** Any index
+ provider may now expose a synchronous, O(1) `healthReport()` — composed from the
+ provider's own exact ledgers, never a sample — and this is the one signal
+ Brainy's read gate trusts. The first-query lazy-build path is gone: `brain.init()`
+ now runs every needed rebuild to completion before it returns, always, regardless
+ of dataset size. A read that lands on a provider whose health report says it
+ isn't serving throws a typed error instead of triggering a rebuild mid-query —
+ `GraphIndexNotReadyError`, `MetadataIndexNotReadyError`, or
+ `VectorIndexNotReadyError` (all exported from `@soulcraft/brainy`), naming the
+ reasons. `repairIndex({ rebuild: ['metadata' | 'graph' | 'vector'] | 'all' })` is
+ the new explicit operator door: it rebuilds the named family unconditionally, no
+ health check consulted — reach for it when you have independent reason to
+ distrust a family regardless of what it self-reports. Bare `repairIndex()` is
+ unchanged in spirit: report-driven, heals only what its own checks say needs it.
+- New concept doc: [Index Health](docs/concepts/index-health.md) walks the whole
+ story from a consumer's side — degraded-but-serving vs. not-ready, what
+ `repairIndex()` checks and heals per family, what `suspect` counts mean.
+
+**Nothing to change to adopt this.** No API removed, no signature narrowed —
+`repairIndex()` gains an optional options bag and its return value gains fields,
+both additive. The honest notes: if your code ever relied on a `find()` against a
+cold/not-yet-built index quietly triggering a rebuild and returning results a beat
+later, that behavior is gone — it now throws one of the three typed
+`*NotReadyError` classes instead (catch them if you need to distinguish "not ready
+yet" from "no results"). And `disableAutoRebuild: true` no longer defers index
+construction to the first query — a needed rebuild always runs at `open()` now;
+the flag has no effect on timing. Full manual control still lives in
+`repairIndex({ rebuild: [...] })`.
+
+- **Crash-reopen catchup.** After an unclean shutdown, the metadata index now
+ folds the exact fact window it missed — `find()` serves every acked write on
+ reopen, closing the gap where canonical reads and counts recovered a
+ crash-window write but the index kept serving its pre-crash state until the
+ next full rebuild. Related root-cause fixed alongside: `close()` never
+ stamped the index watermarks (only `flush()` did), so a close without a
+ prior flush caused a needless full rescan verdict on the next open.
+- **Relation rows are live in the metadata index.** Previously verb rows
+ entered the metadata index only during a rebuild — so a rebuilt store's
+ relation postings went stale from the first `relate()` after it. Relations
+ are now posted and retracted on the live write path (relate / unrelate /
+ updateRelation / remove's cascade, and their `transact()` forms), in the
+ same commit as the graph leg.
+- **The metadata rebuild is online.** `rebuild()` for the metadata family no
+ longer clears and rebuilds in place (reads went empty for the duration): it
+ builds a complete replacement beside the serving index, mirrors concurrent
+ writes to both, swaps atomically, and persists once after the swap. Reads
+ never observe a partial index. `repairIndex({ rebuild: ['metadata'] })` uses
+ it automatically.
+- **Incremental heal is routed.** A provider invariant that asks for the
+ incremental heal (`heal: 'repair'`) now routes to the provider's own
+ `repair()` when it exposes one — re-posting exactly what its ledger names,
+ never a store-sized rebuild — and the post-heal re-read of the report decides
+ success; a repair that doesn't converge is recorded with the escalation named.
+- **The vector family joins the count ledger.** `getCanonicalCounts()` gains
+ `vectors: { all }` — the count of canonical entities holding a real vector
+ (deferred-embed entities count when their vector lands). And the open gate
+ closes the vector leg: a store whose canonical rows hold vectors but whose
+ derived vector index is empty now builds at `open()` (or refuses with the
+ typed error) instead of silently serving empty vector-search results.
+- **An unknown storage config shape fails loudly.** A nested `config` object
+ carrying a path-shaped key (a shape that was never supported) used to fall
+ through silently to the default shared directory — every instance writing one
+ store while callers believed each had its own. It now throws, naming the
+ canonical `path` key.
+- **Relation index rows are JSON-safe.** Internal endpoint identifiers can no
+ longer ride the metadata-index crossing (a native provider serializes it);
+ they stay on the graph operations where they belong.
+- **A broken accelerator install can never read as "not installed."** The
+ auto-detection free pass now requires the resolution error to name the
+ accelerator package itself, exactly — a missing platform-binary sibling
+ package, an inner file path, or a dependency failure is a broken install and
+ `init()` throws loudly. And a plugin that declines activation is narrated on
+ the always-on log channel, so `silent: true` can no longer hide a fallback
+ to the default engines.
+
+---
+
## v10.3.1 — 2026-08-18 (the fold that behaves)
Three recovery cures from one production first-boot incident (a brain's first
diff --git a/SECURITY.md b/SECURITY.md
index 1f3c4732..91d40d49 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -30,7 +30,7 @@ commit to backporting fixes to unsupported lines.
## Scope
-This policy covers the `@soulcraft/brainy` package itself — the code in
+This policy covers the `@soulcraftlabs/brainy` package itself — the code in
this repository. If you're evaluating a deployment that also uses
`@soulcraft/cor`, report issues in that package the same way, to the same
address; we'll route internally.
diff --git a/bin/brainy-ts.js b/bin/brainy-ts.js
index 4e9aedb8..90a35e98 100644
--- a/bin/brainy-ts.js
+++ b/bin/brainy-ts.js
@@ -3,7 +3,7 @@
/**
* Modern TypeScript CLI Runner
*
- * This is the entry point after npm install @soulcraft/brainy
+ * This is the entry point after npm install @soulcraftlabs/brainy
* It runs the compiled TypeScript CLI code
*/
diff --git a/bun.lock b/bun.lock
index c31b3865..1e3e66e2 100644
--- a/bun.lock
+++ b/bun.lock
@@ -3,7 +3,7 @@
"configVersion": 0,
"workspaces": {
"": {
- "name": "@soulcraft/brainy",
+ "name": "@soulcraftlabs/brainy",
"dependencies": {
"@aws-sdk/client-s3": "^3.540.0",
"@azure/identity": "^4.0.0",
diff --git a/docs/DEVELOPER_LEARNING_PATH.md b/docs/DEVELOPER_LEARNING_PATH.md
index 4134ae63..b2d22fb1 100644
--- a/docs/DEVELOPER_LEARNING_PATH.md
+++ b/docs/DEVELOPER_LEARNING_PATH.md
@@ -25,13 +25,13 @@
### Prerequisites
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
### Your First Neural Database
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
// Step 1: Create and initialize Brainy
const brain = new Brainy({
@@ -143,7 +143,7 @@ Once you're comfortable with basic operations, move to **Level 2** to learn abou
### Building a Knowledge Graph
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy({ storage: { type: 'memory' } })
await brain.init()
@@ -314,7 +314,7 @@ Ready for AI-powered search and clustering? Move to **Level 3**.
### Triple Intelligence in Action
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy({ storage: { type: 'memory' } })
await brain.init()
@@ -529,7 +529,7 @@ Want to treat files as intelligent entities? Learn the **Virtual Filesystem** in
### Files as Intelligent Entities
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy({ storage: { type: 'memory' } })
await brain.init()
@@ -832,7 +832,7 @@ Ready for production deployment? Level 5 covers **planet-scale architecture**.
### Production-Ready Deployment
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
// 1. PRODUCTION STORAGE - Filesystem with off-site snapshots
console.log('Initializing production storage...\n')
diff --git a/docs/FIND_SYSTEM.md b/docs/FIND_SYSTEM.md
index 1cc38ce9..77fbbd79 100644
--- a/docs/FIND_SYSTEM.md
+++ b/docs/FIND_SYSTEM.md
@@ -1217,7 +1217,7 @@ where: {
await brain.find({ type: 'Document' })
// ✅ Correct: Use NounType enum
-import { NounType } from '@soulcraft/brainy'
+import { NounType } from '@soulcraftlabs/brainy'
await brain.find({ type: NounType.Document })
// ❌ Error: Operator not recognized
diff --git a/docs/MIGRATION-V3-TO-V4.md b/docs/MIGRATION-V3-TO-V4.md
index 29c409ac..680b6928 100644
--- a/docs/MIGRATION-V3-TO-V4.md
+++ b/docs/MIGRATION-V3-TO-V4.md
@@ -153,13 +153,13 @@ brainy-data/
### Step 1: Update Brainy Package
```bash
-npm install @soulcraft/brainy@latest
+npm install @soulcraftlabs/brainy@latest
```
**Check your version:**
```bash
-npm list @soulcraft/brainy
-# Should show: @soulcraft/brainy@4.0.0
+npm list @soulcraftlabs/brainy
+# Should show: @soulcraftlabs/brainy@4.0.0
```
### Step 2: No Code Changes Required! ✅
@@ -374,7 +374,7 @@ If you encounter issues, you can rollback:
```bash
# Reinstall v3
-npm install @soulcraft/brainy@^3.50.0
+npm install @soulcraftlabs/brainy@^3.50.0
# Restart application
```
@@ -389,7 +389,7 @@ rm -rf ./data
cp -r ./data-backup ./data
# Reinstall v3
-npm install @soulcraft/brainy@^3.50.0
+npm install @soulcraftlabs/brainy@^3.50.0
```
## Common Migration Scenarios
@@ -539,7 +539,7 @@ console.log('Storage type:', status.type)
**Migration Checklist:**
- ✅ Backup data
-- ✅ Update npm package (`npm install @soulcraft/brainy@latest`)
+- ✅ Update npm package (`npm install @soulcraftlabs/brainy@latest`)
- ✅ Restart application (automatic migration)
- ✅ Verify data integrity
- ✅ Enable lifecycle policies
diff --git a/docs/PERFORMANCE.md b/docs/PERFORMANCE.md
index 248a2c70..b543e84a 100644
--- a/docs/PERFORMANCE.md
+++ b/docs/PERFORMANCE.md
@@ -323,58 +323,24 @@ Only the graph adjacency index carries a committed scale assertion:
- ✅ **Single-Node by Design**: One process owns one `path`; scale out at the service layer
- ✅ **Zero Stubs**: Every line of code is production-ready
-## Lazy Loading Performance
+## Index Build at Open (10.4+)
-Brainy supports two initialization modes for optimal performance across different use cases:
+As of 10.4, `brain.init()` runs every needed index rebuild to completion before
+it returns — always, regardless of dataset size. There is no lazy,
+first-query rebuild path: a brain either finishes opening healthy, or `init()`
+fails loudly. `disableAutoRebuild` no longer defers index construction to a
+first query; it has no effect on *when* a rebuild runs. Manual control over
+rebuilds is `repairIndex({ rebuild: [...] })`. See
+[Index Health](concepts/index-health.md) for the full read-gate contract
+(providers self-report readiness via `healthReport()`; a read against a
+not-serving provider throws a typed `*NotReadyError` rather than rebuilding
+mid-query).
-### Mode 1: Auto-Rebuild (Default)
-
-```javascript
-const brain = new Brainy()
-await brain.init() // Rebuilds indexes during init (~500ms-3s for 10K entities)
-```
-
-**Performance:**
-- Init time: 500ms-3s (depends on dataset size)
-- First query: Instant (indexes already loaded)
-- Use case: Traditional applications, long-running servers
-
-### Mode 2: Lazy Loading
-
-```javascript
-const brain = new Brainy({ disableAutoRebuild: true })
-await brain.init() // Returns instantly (0-10ms)
-
-const results = await brain.find({ limit: 10 }) // First query triggers rebuild (~50-200ms)
-const more = await brain.find({ limit: 100 }) // Subsequent queries instant (0ms check)
-```
-
-**Performance:**
-- Init time: 0-10ms (instant)
-- First query: 50-200ms (includes index rebuild for 1K-10K entities)
-- Subsequent queries: 0ms check (instant)
-- Concurrent queries: Wait for same rebuild (mutex prevents duplicates)
-
-**Concurrency Safety:**
-```javascript
-// 100 concurrent queries immediately after init
-await brain.init()
-
-const promises = Array.from({ length: 100 }, () =>
- brain.find({ limit: 10 })
-)
-
-const results = await Promise.all(promises)
-// ✅ Only 1 rebuild triggered (mutex)
-// ✅ All 100 queries return correct results
-// ✅ Total time: ~60ms (not 6000ms!)
-```
-
-**Use Cases for Lazy Loading:**
-- **Serverless/Edge**: Minimize cold start time (0-10ms init)
-- **Development**: Faster restarts during development
-- **Large datasets**: Defer index loading until needed
-- **Read-heavy workloads**: Writes don't wait for index rebuild
+
## Zero Configuration Required
@@ -384,10 +350,6 @@ Brainy is designed to be **smart enough to tune itself dynamically**. No configu
// That's it. Brainy handles everything.
const brain = new Brainy()
await brain.init()
-
-// Or with lazy loading for serverless
-const brain = new Brainy({ disableAutoRebuild: true })
-await brain.init() // Instant (0-10ms)
```
### Automatic Self-Tuning
@@ -395,7 +357,6 @@ await brain.init() // Instant (0-10ms)
- **Metadata Index**: Auto-builds sorted indices for range queries on first use
- **Graph Index**: Auto-flushes every 30 seconds
- **Default Tuning**: Research-based vector index defaults
-- **Lazy Loading**: Indices built only when needed
- **Cache Management**: LRU caches with TTL
### Intelligent Defaults
diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md
index d9a4d3e7..238d2252 100644
--- a/docs/PLUGINS.md
+++ b/docs/PLUGINS.md
@@ -10,7 +10,7 @@ next:
- guides/storage-adapters
---
-# Plugin Development Guide
+# Plugin System
Brainy has a plugin system that allows third-party packages to replace internal subsystems with custom implementations. This is how `@soulcraft/cor` provides optional native acceleration, and it's the same system available to any developer.
@@ -46,7 +46,7 @@ If no plugin provides a given key, brainy uses its built-in JavaScript implement
### 1. Implement the `BrainyPlugin` interface
```typescript
-import type { BrainyPlugin, BrainyPluginContext } from '@soulcraft/brainy/plugin'
+import type { BrainyPlugin, BrainyPluginContext } from '@soulcraftlabs/brainy/plugin'
const myPlugin: BrainyPlugin = {
name: 'my-brainy-plugin', // Must be unique (typically your npm package name)
@@ -90,7 +90,7 @@ await brain.init()
**Programmatic registration:** For plugins not installed as npm packages, use `brain.use()`:
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import myPlugin from './my-plugin.js'
const brain = new Brainy()
@@ -200,15 +200,30 @@ members so a warm reopen never pays a redundant rebuild-from-canonical:
- **`init?(): Promise`** — eager cold-load. Brainy awaits it once during
`brain.init()`, after the metadata provider's `init()` (the id-mapper hydrates first)
and **before the rebuild gate**.
-- **`isReady?(): boolean`** — honest durability signal. `true` ⇔ the persisted index is
- loaded (or cheaply demand-loadable) and consistent with what was last persisted. When
- exposed, the rebuild gate defers to this signal **instead of** the `size() === 0` /
- `totalEntries === 0` heuristics — a disk-native index may report 0 resident entries
- while fully durable. Never return `true` if the durable state failed to load: the
- signal is honest in both directions, and a not-ready provider gets its rebuild even
- when `size() > 0`.
+- **`healthReport?(): HealthReport`** — the PREFERRED signal (10.4+). A named,
+ synchronous, O(1) verdict derived from the provider's own exact ledgers — never a
+ sample, never I/O, must never throw for a well-formed provider. Brainy's read gate
+ (`assessProviderHealth()`) reads this INSTEAD of `isReady()` / size heuristics when
+ present: `serving: false` refuses the read with a typed `*NotReadyError` rather than
+ triggering a rebuild — a read never starts a store walk. `healthy` marks every
+ *verified* invariant holding; a family named in `unledgered` counts as neither
+ healthy nor broken. See `HealthReport` / `LedgerInvariantResult` /
+ `InvariantSource` in `src/plugin.ts`, and
+ [Index Health](concepts/index-health.md) for the consumer-facing story.
+- **`isReady?(): boolean`** — honest durability signal, the fallback when
+ `healthReport()` is absent. `true` ⇔ the persisted index is loaded (or cheaply
+ demand-loadable) and consistent with what was last persisted. When exposed, the
+ gate defers to this signal **instead of** the `size() === 0` / `totalEntries === 0`
+ heuristics — a disk-native index may report 0 resident entries while fully durable.
+ Never return `true` if the durable state failed to load: the signal is honest in
+ both directions, and a not-ready provider gets its rebuild even when `size() > 0`.
- **`isMigrating?(): boolean`** — while `true`, the provider owns its index (background
migration); brainy skips its rebuild entirely.
+- **`validateInvariants?(): Promise`** — the async DEEP
+ diagnostic (full scans allowed), distinct from the bounded, sync `healthReport()`.
+ Must never throw — a failure is `healthy: false` data, not an exception; a provider
+ that throws anyway is read as a loud, unverified failure (never as "healthy") by
+ every caller, never silently retried into a rebuild.
Providers that implement none of these keep the size/count heuristics — correct for
engines whose `rebuild()` *is* their load path (like brainy's built-in JS vector index).
@@ -257,10 +272,10 @@ When provided by an optional native acceleration plugin (such as `@soulcraft/cor
#### `cache`
**Type:** `UnifiedCache`
-Replaces the global `UnifiedCache` singleton used for VFS path resolution, semantic caching, and vector index caching. Must implement the `UnifiedCache` interface (available from `@soulcraft/brainy/internals`).
+Replaces the global `UnifiedCache` singleton used for VFS path resolution, semantic caching, and vector index caching. Must implement the `UnifiedCache` interface (available from `@soulcraftlabs/brainy/internals`).
```typescript
-import type { UnifiedCache } from '@soulcraft/brainy/internals'
+import type { UnifiedCache } from '@soulcraftlabs/brainy/internals'
context.registerProvider('cache', myNativeCache)
```
@@ -310,8 +325,8 @@ Plugins can register custom storage backends that users reference by name.
### Implementing a Storage Adapter
```typescript
-import type { StorageAdapterFactory } from '@soulcraft/brainy/plugin'
-import type { StorageAdapter } from '@soulcraft/brainy'
+import type { StorageAdapterFactory } from '@soulcraftlabs/brainy/plugin'
+import type { StorageAdapter } from '@soulcraftlabs/brainy'
class MyStorageAdapter implements StorageAdapter {
async init(): Promise { /* ... */ }
@@ -345,9 +360,9 @@ Brainy provides three entry points for plugin developers:
| Import Path | Contents | Stability |
|-------------|----------|-----------|
-| `@soulcraft/brainy` | Public API, types, StorageAdapter | Stable (semver) |
-| `@soulcraft/brainy/plugin` | BrainyPlugin, BrainyPluginContext, StorageAdapterFactory | Stable (semver) |
-| `@soulcraft/brainy/internals` | UnifiedCache, EntityIdMapper, logger utilities | Internal (may change between minor versions) |
+| `@soulcraftlabs/brainy` | Public API, types, StorageAdapter | Stable (semver) |
+| `@soulcraftlabs/brainy/plugin` | BrainyPlugin, BrainyPluginContext, StorageAdapterFactory | Stable (semver) |
+| `@soulcraftlabs/brainy/internals` | UnifiedCache, EntityIdMapper, logger utilities | Internal (may change between minor versions) |
## Diagnostics
@@ -425,7 +440,7 @@ A minimal but useful plugin that provides SIMD-accelerated distance calculations
```typescript
// simd-distance-plugin/src/plugin.ts
-import type { BrainyPlugin, BrainyPluginContext } from '@soulcraft/brainy/plugin'
+import type { BrainyPlugin, BrainyPluginContext } from '@soulcraftlabs/brainy/plugin'
// Hypothetical native module
import { simdCosineDistance } from './native.js'
@@ -455,7 +470,7 @@ export default simdDistancePlugin
"main": "./dist/plugin.js",
"types": "./dist/plugin.d.ts",
"peerDependencies": {
- "@soulcraft/brainy": ">=7.0.0"
+ "@soulcraftlabs/brainy": ">=7.0.0"
}
}
```
@@ -463,7 +478,7 @@ export default simdDistancePlugin
Usage:
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy({ plugins: ['brainy-simd-distance'] })
await brain.init()
diff --git a/docs/PRODUCTION_SERVICE_ARCHITECTURE.md b/docs/PRODUCTION_SERVICE_ARCHITECTURE.md
index 4568cd31..ad4a4a40 100644
--- a/docs/PRODUCTION_SERVICE_ARCHITECTURE.md
+++ b/docs/PRODUCTION_SERVICE_ARCHITECTURE.md
@@ -54,7 +54,7 @@ After 40 API calls:
```typescript
// server.ts
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// SINGLETON INSTANCE
let brainInstance: Brainy | null = null
@@ -174,7 +174,7 @@ process.on('SIGTERM', async () => {
```typescript
// server.ts - Clean Bun implementation
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brain: Brainy | null = null
diff --git a/docs/README.md b/docs/README.md
index 3290001f..ddb37d20 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -5,7 +5,7 @@
## Quick Start
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
diff --git a/docs/RELEASE-GUIDE.md b/docs/RELEASE-GUIDE.md
index 94c6a6cb..4b120bd8 100644
--- a/docs/RELEASE-GUIDE.md
+++ b/docs/RELEASE-GUIDE.md
@@ -99,7 +99,7 @@ Examples:
```bash
# 1. Deprecate wrong version on npm
-npm deprecate @soulcraft/brainy@X.X.X "Incorrect version - use Y.Y.Y"
+npm deprecate @soulcraftlabs/brainy@X.X.X "Incorrect version - use Y.Y.Y"
# 2. Fix version in package.json
# 3. Republish correct version
diff --git a/docs/SCALING.md b/docs/SCALING.md
index e9ae1136..054d2096 100644
--- a/docs/SCALING.md
+++ b/docs/SCALING.md
@@ -13,7 +13,7 @@
### In-Memory
```typescript
-import Brainy from '@soulcraft/brainy'
+import Brainy from '@soulcraftlabs/brainy'
const brain = new Brainy({ storage: { type: 'memory' } })
```
@@ -43,7 +43,7 @@ The native vector provider (via the optional `@soulcraft/cor` package) extends t
Numbers below are **measured** by `tests/benchmarks/find-composition-scale.js` (a single
Node 22 process, in-memory storage, 384-dim vectors, `balanced` recall). They are the
-open-core (pure-TypeScript) path — what you get from `@soulcraft/brainy` with no native
+open-core (pure-TypeScript) path — what you get from `@soulcraftlabs/brainy` with no native
provider installed. Run it yourself: `node --max-old-space-size=8192 tests/benchmarks/find-composition-scale.js 100000`.
`find()` query latency, p50 / p95 (200 queries each):
diff --git a/docs/api-contract.json b/docs/api-contract.json
new file mode 100644
index 00000000..aafd838a
--- /dev/null
+++ b/docs/api-contract.json
@@ -0,0 +1,1544 @@
+{
+ "contractVersion": 1,
+ "engine": "@soulcraftlabs/brainy",
+ "compatibility": {
+ "minor": "additive — a new optional door, a new served operator, a new error class; every existing implementation still conforms",
+ "major": "breaking — a door removed, an answer narrowed, an ordering law changed, an optional door promoted to required, or an operator moved from served to refused"
+ },
+ "doors": [
+ {
+ "name": "adaptiveHistoryBudgetBytes",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "add",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "addMany",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "adoptLogAuthority",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "adoptLogAuthorityInner",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "aggViewFromEntity",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "anyProviderMigrating",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "applyFusionScoring",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "applyGraphConstraints",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "armIdleFlushTimer",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "asOf",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "assertGenerationStoreReady",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "assertWritable",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "audit",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "auditGraph",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "autoAdoptLegacyVfsBlobsIfNeeded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "autoAlpha",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "autoCompactHistory",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "awaitMigrationLock",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "awaitPendingEmbeds",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "backfillAggregateIfNeeded",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "batchGet",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "brainWideStrictRequiresSubtype",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "bridgeLegacyPendingEmbedSidecars",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "buildAtGenerationVectors",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "buildGraphView",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "buildMetadataFilter",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "buildMigrationUpdate",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "buildRelationMigrationUpdate",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "cacheVerbInt",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "canServeVectorAtGeneration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "checkHealth",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "checkMigrations",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "clear",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "clearPendingEmbed",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "close",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "closeDurableSteps",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "cluster",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "collectProviderInvariants",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "compactHistory",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "consumeMetadataWatermarkVerdict",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "convertMetadataToEntity",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "convertNounToEntity",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "counts",
+ "kind": "accessor"
+ },
+ {
+ "name": "createIndex",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "createMigrationBackupIfNeeded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "createPinnedDb",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "createResult",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "dbFinalizationRegistry",
+ "kind": "accessor"
+ },
+ {
+ "name": "dbHost",
+ "kind": "accessor"
+ },
+ {
+ "name": "defineAggregate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "detectIdKind",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "diagnostics",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "diff",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "embed",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "embedBatch",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "emitCommitted",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "enforceSubtypeOnAdd",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "enforceSubtypeOnRelate",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "enforceTrackedFieldValues",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "enhanceNLPResult",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "enqueuePendingEmbed",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "ensureAggregationIndex",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "ensureIndexesLoaded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "ensureInitialized",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "entityForAggFromRawRecord",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "entityFromGenerationRecord",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "entityIntsToUuids",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "entityViewFromRawRecord",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "excludedVisibilityTiers",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "executeGraphSearch",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "executeProximitySearch",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "executeTextSearch",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "executeVectorSearch",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "explain",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "export",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "extract",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "extractConcepts",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "extractEntities",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "factSegmentPaths",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "fieldCountsAggregateName",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "fillSubtypes",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "filterIdsBelted",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "find",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "findAggregate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "findDuplicates",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "findMatchingWords",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "flush",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "formatInfo",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "formatSubtypeError",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "generation",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "generationDigest",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "get",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "getActivePlugins",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getAvailableFields",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getBackgroundDeduplicator",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getFieldsForType",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "getFieldStatistics",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getFieldsWithCardinality",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getFieldValues",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "getIndexStats",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getIndexStatus",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getMemoryStats",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getNeighborUuids",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "getNounCount",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getOptimalQueryPlan",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "getStats",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "getStorageType",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getSubtypeRule",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "getTripleIntelligence",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "getTypedNeighbors",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "getVerbCount",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "graph",
+ "kind": "accessor"
+ },
+ {
+ "name": "graphAccelerationProvider",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "graphCommunities",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphCommunitiesFallback",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphCommunitiesNative",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "graphEntityInt",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphExport",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphExportFallback",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphExportNative",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "graphPath",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "graphPathFallback",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "graphPathNative",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "graphRank",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphRankFallback",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "graphRankNative",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "graphSubgraph",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "graphSubgraphFallback",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "graphSubgraphFromQuery",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "graphSubgraphNative",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "groupByLabel",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "hasStorageMethod",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "hasVectorOrTextCriteria",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "health",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "highlight",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "highlightSemanticPhase",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "history",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "historyStats",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "hub",
+ "kind": "accessor"
+ },
+ {
+ "name": "hydrateIdMapperForGraphRebuild",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "hydrateNativeSubgraph",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "import",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "importPluginPackage",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "incidentEdges",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "indexStats",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "init",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "insights",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "isEmbeddingReady",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "isInfrastructureWrite",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "isInitialized",
+ "kind": "accessor"
+ },
+ {
+ "name": "isReadOnly",
+ "kind": "accessor"
+ },
+ {
+ "name": "kickBackgroundFlush",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "kickEmbedWorker",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "legacyLayoutMigrationPhase",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "loadAnalyticsGraph",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "loadPlugins",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "logAuthority",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "maintenanceDebt",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "materializeAtGeneration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "metadataIndexRetractionOp",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "migrate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "migrateField",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "migrateInternal",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "migrateLegacyZeroNormVfsRootIfNeeded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "migrationSnapshot",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "neededFamiliesMigrating",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "neighbors",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "newId",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "nlp",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "normalizeConfig",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "noteWriteForPersistence",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "now",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "onChange",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "pagination",
+ "kind": "accessor"
+ },
+ {
+ "name": "parseMigrationPath",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "parseNaturalQuery",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "pathExists",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "pendingEmbedCount",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "performInit",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "persistPinnedGeneration",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "persistSingleOp",
+ "kind": "method",
+ "arity": 6
+ },
+ {
+ "name": "pickMetadataProbe",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "pickVectorProbe",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "pinGeneration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "planGetEntity",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "planTransact",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "planTxAdd",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "planTxRelate",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "planTxRemove",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "planTxUnrelate",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "planTxUpdate",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "projectionGauges",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "providerForFamily",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "providerIsMigrating",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "providerMigrationStatus",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "queryAggregate",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "queryIndexFamilies",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "readPath",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "ready",
+ "kind": "accessor"
+ },
+ {
+ "name": "rebuildIndexesIfNeeded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "rebuildMetadataIndexOnline",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "reconcileLogDivergence",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "reconstructPath",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "recordStateAt",
+ "kind": "method",
+ "arity": 3
+ },
+ {
+ "name": "recoverPendingEmbedsFromLog",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "registerShutdownHooks",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "relate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "related",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "relateMany",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "relationFromGenerationRecord",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "relationshipSubtypesOf",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "releaseGeneration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "remove",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "removeAggregate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "removeMany",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "removeMigrationBackupSafe",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "repackHistory",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "repairIndex",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "requestFlush",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "requireProviders",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "requireSubtype",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "resolveAsOfGeneration",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "resolveDiffEndpoint",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "resolveHiddenIds",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "resolveHNSWPersistMode",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "resolveRawGeneration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "resolveRetentionPolicy",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "resolveVerbEndpointInts",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "resolveVerbIntsToIds",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "restore",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "rrfFusion",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "runAggregationBackfillWalk",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "runAggregationCatchUp",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "runEmbedWorker",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "runOracle",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "runRepairIndexPhases",
+ "kind": "method",
+ "arity": 5
+ },
+ {
+ "name": "scanFacts",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "seedIdsToInts",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "selectorToSeedIds",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "setRetentionBudget",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "setupEmbedder",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "setupIndex",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "setupStorage",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "similar",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "similarity",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "splitForHighlighting",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "stampBrainFormat",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "stampBrainFormatIfNeeded",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "stampEntityTree",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "stampProjectionWatermarks",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "stats",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "storageAdapter",
+ "kind": "accessor"
+ },
+ {
+ "name": "stream",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "streaming",
+ "kind": "accessor"
+ },
+ {
+ "name": "subtypesOf",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "trackField",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "transact",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "transactionLog",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "unrelate",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "unvectorNounForRootMigration",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "update",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "updateMany",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "updateRelation",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "upsertMergeParams",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "use",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "usesDefaultWasmEmbedder",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "validateIndexConsistency",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "vectorSearchAtGeneration",
+ "kind": "method",
+ "arity": 4
+ },
+ {
+ "name": "verbsToRelations",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "verbToRelationLike",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "verifyEntityTreeStamp",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "verifyGraphAdjacencyLive",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "verifyLogAuthority",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "verifyMetadataLive",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "verifyVectorLive",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "versionedIndexProviders",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "vfs",
+ "kind": "accessor"
+ },
+ {
+ "name": "waitForIndexed",
+ "kind": "method",
+ "arity": 2
+ },
+ {
+ "name": "warm",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "warmupEmbeddings",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "warnIfReadsDegraded",
+ "kind": "method",
+ "arity": 1
+ },
+ {
+ "name": "wireConnectionsCodec",
+ "kind": "method",
+ "arity": 0
+ },
+ {
+ "name": "wireGraphIdResolver",
+ "kind": "method",
+ "arity": 0
+ }
+ ],
+ "errors": [
+ "BrainyError",
+ "DerivedArtifactMissingError",
+ "GraphIndexNotReadyError",
+ "MetadataIndexNotReadyError",
+ "MigrationInProgressError",
+ "ProtectedArtifactError",
+ "VectorIndexNotReadyError"
+ ],
+ "operators": {
+ "accepted": [
+ "between",
+ "contains",
+ "endsWith",
+ "eq",
+ "equals",
+ "excludes",
+ "exists",
+ "greaterThan",
+ "greaterThanOrEqual",
+ "gt",
+ "gte",
+ "hasAll",
+ "in",
+ "length",
+ "lessThan",
+ "lessThanOrEqual",
+ "lt",
+ "lte",
+ "matches",
+ "missing",
+ "ne",
+ "noneOf",
+ "notEquals",
+ "oneOf",
+ "startsWith"
+ ],
+ "servedOnIndexPath": [
+ "between",
+ "contains",
+ "eq",
+ "equals",
+ "excludes",
+ "exists",
+ "greaterThan",
+ "greaterThanOrEqual",
+ "gt",
+ "gte",
+ "hasAll",
+ "in",
+ "lessThan",
+ "lessThanOrEqual",
+ "lt",
+ "lte",
+ "missing",
+ "ne",
+ "noneOf",
+ "notEquals",
+ "oneOf"
+ ],
+ "refusedByIndexPath": [
+ "endsWith",
+ "length",
+ "matches",
+ "startsWith"
+ ],
+ "combinators": [
+ "allOf",
+ "anyOf",
+ "not"
+ ]
+ },
+ "fieldAddressing": {
+ "systemKeyPrefix": "system.",
+ "systemEntityScalars": [
+ "confidence",
+ "createdAt",
+ "createdBy",
+ "id",
+ "service",
+ "subtype",
+ "type",
+ "updatedAt",
+ "visibility",
+ "weight"
+ ],
+ "systemRelationScalars": [
+ "confidence",
+ "createdAt",
+ "createdBy",
+ "service",
+ "sourceId",
+ "subtype",
+ "targetId",
+ "updatedAt",
+ "verb",
+ "visibility",
+ "weight"
+ ],
+ "plumbingFields": [
+ "_rev",
+ "connections",
+ "data",
+ "level",
+ "vector"
+ ]
+ },
+ "health": {
+ "verdicts": [
+ "pass",
+ "warn",
+ "fail"
+ ],
+ "healKinds": [
+ "none",
+ "repair",
+ "rebuild"
+ ],
+ "servingWithholdingInvariants": [
+ "index-initialized",
+ "durable-state-present",
+ "manifest-residency",
+ "replay-clean",
+ "strand-latch"
+ ]
+ }
+}
diff --git a/docs/api/README.md b/docs/api/README.md
index 4ca84364..ba49ff48 100644
--- a/docs/api/README.md
+++ b/docs/api/README.md
@@ -24,7 +24,7 @@ next:
## Quick Start
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy() // Zero config!
await brain.init() // VFS auto-initialized!
@@ -1010,7 +1010,7 @@ await db.release() // unpin + free cached materialization
### Db API errors
-All exported from `@soulcraft/brainy`:
+All exported from `@soulcraftlabs/brainy`:
| Error | Thrown by | Meaning |
|---|---|---|
@@ -1451,6 +1451,34 @@ const count = await brain.getVerbCount()
---
+### The canonical count ledger (`StorageAdapter.getCanonicalCounts()`)
+
+An OPTIONAL method on the `StorageAdapter` interface (implemented by both
+built-in adapters), not a method on `Brainy` itself — relevant if you're
+writing a custom storage adapter or composing a provider's own
+`healthReport()`. O(1), no I/O. Per family (`nouns`/`verbs`):
+
+```typescript
+interface CanonicalCounts {
+ nouns: { counted: number; all: number }
+ verbs: { counted: number; all: number }
+ suspect: boolean
+}
+```
+
+- `counted` mirrors `getNounCount()` / `getVerbCount()` (public + internal tiers).
+- `all` is the ALL-visibility scalar — every tier, including system/internal
+ records — the denominator a derived index's own coverage math is measured
+ against.
+- `suspect` is `true` when an unprovable delete has left `all` unverified since
+ the last recount; `brain.repairIndex()` clears it with a real canonical walk.
+
+Adapters without the ledger omit the method; treat absence as "no
+denominator," never as zero. See
+**[Index Health](../concepts/index-health.md)** for the full story.
+
+---
+
### Subtype & facet APIs
Full guide: **[Subtypes & Facets](../guides/subtypes-and-facets.md)**.
@@ -1831,6 +1859,104 @@ const semanticOnly = await brain.getStats({ excludeVFS: true })
---
+### `repairIndex(options?)` → `Promise`
+
+The ceremony door for index repair. Bare `repairIndex()` is report-driven: it
+prunes orphaned containers, recomputes count rollups, reconciles VFS
+containment, and rebuilds only a derived-index family whose own health check
+asks for it. Pass `options.rebuild` to force one or more families to rebuild
+UNCONDITIONALLY — no health check is consulted — when an operator has
+independent reason to reconcile a family regardless of what it self-reports.
+
+```typescript
+// Report-driven: only heals what actually needs it
+const report = await brain.repairIndex()
+console.log(report.healedTotal, report.families)
+
+// Explicit: force the graph adjacency to rebuild from canonical, unconditionally
+await brain.repairIndex({ rebuild: ['graph'] })
+
+// Explicit: force all three derived indexes to rebuild
+await brain.repairIndex({ rebuild: 'all' })
+```
+
+**`RepairReport`:**
+- `families: RepairFamilyReport[]` — one row per family checked
+- `healedTotal: number` — items healed across every family
+- `durationMs: number`
+
+**`RepairFamilyReport`** (one row):
+- `family: string` — e.g. `'orphaned-containers'`, `'count-rollups'`,
+ `'vfs-containment'`, `'metadata-corruption'`, `'provider:metadata'`,
+ `'provider:graph'`, `'provider:vector'`
+- `checked: boolean` — was this family actually examined (`false` ⇒ see `skipped`)
+- `healed: number` — items re-posted/corrected in place (the incremental heal count)
+- `missing?: { count: number; sample: string[] }` — exact count plus a capped id
+ sample when the check can name what diverged (never the full list)
+- `rebuilt?: boolean` — a full generational rebuild ran (vs. an incremental heal)
+- `detail?: string` / `reason?: string` — narration
+- `skipped?: string` — why the family wasn't checked
+
+Full walkthrough — what each family checks, degraded-but-serving vs. not-ready,
+and what `suspect` counts mean — in
+**[Index Health](../concepts/index-health.md)**.
+
+---
+
+### Index readiness: typed errors, `healthReport()`, `disableAutoRebuild`
+
+Every derived-index provider (vector, graph, metadata) may expose a named,
+synchronous, O(1) `healthReport()` composed from its own exact ledgers — the
+signal Brainy's read gate trusts over sampling or size heuristics. `init()`
+brings every provider to serving before it returns; there is no first-query
+lazy-rebuild path. A read that reaches a provider whose health report says it
+isn't serving throws instead of rebuilding mid-query:
+
+| Error | Thrown by | Meaning |
+|---|---|---|
+| `GraphIndexNotReadyError` | `find({ connected })`, `neighbors()`, `related()` | Graph adjacency isn't serving |
+| `MetadataIndexNotReadyError` | `find({ where })` | Metadata/field index isn't serving |
+| `VectorIndexNotReadyError` | `find({ query })`, `similar()` | Vector index isn't serving |
+
+All three are exported from `@soulcraftlabs/brainy`. Catch them to distinguish
+"index not ready" from a genuine empty result:
+
+```typescript
+import { MetadataIndexNotReadyError } from '@soulcraftlabs/brainy'
+
+try {
+ const rows = await brain.find({ where: { status: 'active' } })
+} catch (err) {
+ if (err instanceof MetadataIndexNotReadyError) {
+ // reconcile: await brain.repairIndex(), then retry
+ } else {
+ throw err
+ }
+}
+```
+
+**`disableAutoRebuild`** no longer defers index construction to the first
+query. A needed rebuild always runs at `open()`, regardless of this flag or
+dataset size; the flag has no effect on *when* a rebuild runs. Full manual
+control lives in `repairIndex({ rebuild: [...] })`, above.
+
+### `validateIndexConsistency()` → `Promise<...>`
+
+The deep, async diagnostic counterpart to `healthReport()` — safe to run on a
+live brain, but does more work (a provider's `validateInvariants()` may run a
+full scan, not just read a ledger). Aggregates the JS metadata index's own
+consistency check with every derived-index provider's invariant report.
+
+```typescript
+const validation = await brain.validateIndexConsistency()
+if (!validation.healthy) {
+ console.log(validation.recommendation) // what to run, e.g. repairIndex()
+ console.log(validation.providers) // each provider's own invariant report, when exposed
+}
+```
+
+---
+
## Lifecycle
### Initialization
@@ -2082,7 +2208,7 @@ For the full taxonomy with all 169 types and their descriptions, see:
- **📖 Documentation:** [Full Documentation](../)
- **🐛 Issues:** [GitHub Issues](https://github.com/soulcraftlabs/brainy/issues)
- **💬 Discussions:** [GitHub Discussions](https://github.com/soulcraftlabs/brainy/discussions)
-- **📦 NPM:** [@soulcraft/brainy](https://www.npmjs.com/package/@soulcraft/brainy)
+- **📦 NPM:** [@soulcraftlabs/brainy](https://www.npmjs.com/package/@soulcraftlabs/brainy)
- **⭐ GitHub:** [Star us](https://github.com/soulcraftlabs/brainy)
---
diff --git a/docs/architecture/data-storage-architecture.md b/docs/architecture/data-storage-architecture.md
index 48064757..83b9e23a 100644
--- a/docs/architecture/data-storage-architecture.md
+++ b/docs/architecture/data-storage-architecture.md
@@ -268,7 +268,7 @@ locks/_flush_responses/ # writer answers with .ack
| **Counts/statistics** | Per-type and per-subtype maps | `_system/{type,subtype,verb-subtype}-statistics.json.gz`, `counts.json` | Recomputable by scanning entities (`brainy inspect repair`) |
A pluggable index provider (the 8.0 plugin contract in
-`@soulcraft/brainy/plugin`) may replace any of the JS implementations; the
+`@soulcraftlabs/brainy/plugin`) may replace any of the JS implementations; the
persisted formats above are contract-bound so JS and native implementations
can interleave on the same directory.
diff --git a/docs/architecture/finite-type-system.md b/docs/architecture/finite-type-system.md
index 76492ee5..48a8b1fe 100644
--- a/docs/architecture/finite-type-system.md
+++ b/docs/architecture/finite-type-system.md
@@ -126,7 +126,7 @@ class TypeAwareMetadataIndex {
**The Design**: Specify types clearly in your API calls:
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
// Add entity with explicit type
await brain.add({
@@ -231,7 +231,7 @@ class OrgEnrichmentAugmentation {
**Brainy's Approach**: Extract **typed** concepts:
```typescript
-import { NaturalLanguageProcessor } from '@soulcraft/brainy'
+import { NaturalLanguageProcessor } from '@soulcraftlabs/brainy'
const nlp = new NaturalLanguageProcessor()
const concepts = await nlp.extractConcepts("Alice works at Google in San Francisco")
@@ -382,7 +382,7 @@ import {
getVerbTypes,
BrainyTypes,
suggestType
-} from '@soulcraft/brainy'
+} from '@soulcraftlabs/brainy'
// Get all available noun types
const nounTypes = getNounTypes()
diff --git a/docs/architecture/index-architecture.md b/docs/architecture/index-architecture.md
index 8b3dc540..6a754b56 100644
--- a/docs/architecture/index-architecture.md
+++ b/docs/architecture/index-architecture.md
@@ -723,6 +723,14 @@ async stats(): Promise {
### 5. Index Rebuilding (Lazy Loading Support)
+> **Stale as of 10.4 — "Mode 2: Lazy Loading on First Query" below is
+> RETIRED.** `disableAutoRebuild` no longer defers index construction to a
+> first query; `brain.init()` now runs every needed rebuild to completion
+> before it returns, unconditionally, and a read against a not-serving
+> provider throws a typed `*NotReadyError` instead of rebuilding mid-query.
+> See `docs/concepts/index-health.md` for the current contract. Left below
+> as historical background on the rebuild mechanics.
+
**Two modes of index loading:**
#### Mode 1: Auto-Rebuild on init() (default)
diff --git a/docs/architecture/initialization-and-rebuild.md b/docs/architecture/initialization-and-rebuild.md
index a1645744..e19bdd9f 100644
--- a/docs/architecture/initialization-and-rebuild.md
+++ b/docs/architecture/initialization-and-rebuild.md
@@ -1,5 +1,15 @@
# Initialization and Rebuild Processes
+> **Stale as of 10.4 — "Mode 2: Lazy Loading on First Query" below is RETIRED.**
+> `disableAutoRebuild` no longer defers index construction to a first query;
+> `brain.init()` now runs every needed rebuild to completion before it
+> returns, unconditionally. A read against a not-serving provider throws a
+> typed `*NotReadyError` instead of rebuilding mid-query. See
+> `docs/concepts/index-health.md` for the current contract; this document's
+> line-number references to `src/brainy.ts` also predate the file's current
+> size and are unreliable. Left as historical background on the rebuild
+> mechanics, not as a current API description.
+
This document explains how Brainy's four indexes (MetadataIndex, vector index, GraphAdjacencyIndex, DeletedItemsIndex) initialize and rebuild from persisted storage.
## Core Principle: All Indexes Are Disk-Based
diff --git a/docs/architecture/multiprocess-storage-mixin.md b/docs/architecture/multiprocess-storage-mixin.md
index 46f98398..1593bf8f 100644
--- a/docs/architecture/multiprocess-storage-mixin.md
+++ b/docs/architecture/multiprocess-storage-mixin.md
@@ -127,7 +127,7 @@ For reference, a clean migration path:
`isMultiProcessSafe` type-guard. Keep `hasStorageMethod` for
build/install artifact protection.
5. Document the new contract in `concepts/storage-adapters.md`.
-6. Major-version-bump the `@soulcraft/brainy` peerDep range expected by
+6. Major-version-bump the `@soulcraftlabs/brainy` peerDep range expected by
plugins.
Estimated work: ~half a day of code, ~2 hours of doc/example updates,
diff --git a/docs/architecture/noun-verb-taxonomy.md b/docs/architecture/noun-verb-taxonomy.md
index 286464be..3dac6892 100644
--- a/docs/architecture/noun-verb-taxonomy.md
+++ b/docs/architecture/noun-verb-taxonomy.md
@@ -20,7 +20,7 @@ next:
Every example on this page is written against the real Brainy 8.0 API. The setup is always the same:
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -40,7 +40,7 @@ Brainy's **Noun-Verb Taxonomy** achieves broad coverage of human knowledge throu
- **Multi-hop Graph Traversals = Relationship Complexity**
- **Result: Model data across virtually any industry**
-Every piece of information can be represented as entities (nouns) connected by relationships (verbs) carrying properties (metadata). The standardized type system from `@soulcraft/brainy` (`NounType`, `VerbType`) gives those nouns and verbs a stable, shared name.
+Every piece of information can be represented as entities (nouns) connected by relationships (verbs) carrying properties (metadata). The standardized type system from `@soulcraftlabs/brainy` (`NounType`, `VerbType`) gives those nouns and verbs a stable, shared name.
## The Power of Standardization: Universal Interoperability
diff --git a/docs/architecture/zero-config.md b/docs/architecture/zero-config.md
index d42d6784..a35e6416 100644
--- a/docs/architecture/zero-config.md
+++ b/docs/architecture/zero-config.md
@@ -35,7 +35,7 @@ constructor and `init()`.
## Instant Start
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// That's it. No config needed.
const brain = new Brainy()
diff --git a/docs/concepts/field-addressing.md b/docs/concepts/field-addressing.md
index c459021b..d24dd66b 100644
--- a/docs/concepts/field-addressing.md
+++ b/docs/concepts/field-addressing.md
@@ -167,7 +167,7 @@ await brain.find({ orderBy: 'createdAt' })
`UnresolvableFieldError` is exported from the package root:
```typescript
-import { UnresolvableFieldError } from '@soulcraft/brainy'
+import { UnresolvableFieldError } from '@soulcraftlabs/brainy'
try {
await brain.find({ orderBy: 'createdAt' })
diff --git a/docs/concepts/index-health.md b/docs/concepts/index-health.md
new file mode 100644
index 00000000..923267df
--- /dev/null
+++ b/docs/concepts/index-health.md
@@ -0,0 +1,217 @@
+---
+title: Index Health
+slug: concepts/index-health
+public: true
+category: concepts
+template: concept
+order: 8
+description: How Brainy knows whether a derived index can be trusted — exact accounting instead of sampling, the named health report, degraded-but-serving vs. not-ready, and what repairIndex() checks, heals, and rebuilds.
+next:
+ - concepts/generation-fact-log
+ - guides/inspection
+---
+
+# Index Health
+
+Brainy keeps one **canonical** copy of every entity and relationship, and three
+**derived** indexes built from it — vector, metadata, and graph — so `find()` can
+answer semantically, by filter, and by traversal without re-deriving the answer from
+scratch on every query. A derived index is a cache with a serving structure: it can
+be present but stale, present but only partially loaded, or fully out of sync with
+canonical after a crash. This page is about how Brainy decides whether to trust one,
+what it does when it can't, and how you reconcile the two.
+
+## Exact accounting instead of sampling
+
+Older health checks worked by inference: does `size()` return something greater
+than zero, does a spot-check on one known item come back correct. Both are proxies.
+A cold index can report a nonzero count while its actual serving structure never
+loaded, and a spot-check only proves the one item it happened to ask about.
+
+Every derived-index provider may now expose a named, synchronous, O(1)
+`healthReport()` — composed from the provider's own **exact ledgers** (real counters
+it already maintains on the write path), never a sample or a walk. This is the one
+signal Brainy's read gate consults. A provider that doesn't yet expose one falls
+back to an honest `isReady()` boolean, and finally to a size heuristic for engines
+with neither — but wherever a `healthReport()` exists, it wins.
+
+Underneath, storage itself keeps an analogous **canonical count ledger**: a
+`counted` scalar (the user-facing total — what `getNounCount()` / `getVerbCount()`
+return) and an `all` scalar (every tier, including internal records a derived
+index's own coverage math needs to compare against). This is the real denominator
+a provider's `healthReport()` measures itself by, rather than a total that can only
+ever ratchet upward. See [What `suspect` counts mean](#what-suspect-counts-mean)
+below for the one case that ledger can't stay exact through on its own.
+
+## The named report
+
+A `HealthReport` carries, per provider (`'vector'` / `'graph'` / `'metadata'`):
+
+- **`healthy`** — `true` iff every *verified* invariant holds. An invariant whose
+ family has no ledger yet is `unledgered`, never counted either way — unknown,
+ not passing.
+- **`serving`** — can this provider answer a query right now. A failing invariant
+ graded `heal: 'repair'` or `heal: 'none'` still leaves `serving: true` — this is
+ **degraded-but-serving**: something is off (say, a stale rollup on an
+ `employee` record's relationship count) but reads keep working. Only a failure
+ graded `heal: 'rebuild'` flips `serving` to `false` — **not-ready** — because the
+ provider itself is telling you its serving structure cannot answer correctly.
+- **`invariants`** — each checked condition, with its provenance
+ (`source: 'ledger'` — an exact count; `'deep'` — a full scan, diagnostic-only;
+ `'unledgered'` — not yet tracked) and, for a failing one, an exact `missing`
+ count plus a capped sample of the affected ids — a verdict, never a dump.
+- **`generation`** — bumps on every ledger mutation and rebuild, so a caller can
+ cache a verdict per generation instead of re-deriving it.
+
+The distinction that matters day to day: `healthy: false` can be entirely benign —
+a maintenance window, a divergence `repairIndex()` will clean up on its own
+schedule. `serving: false` is not benign. It means this provider is refusing to
+answer, on its own word, right now.
+
+**How a failure gets its grade — the serving law.** A provider grades `heal` by
+one question only: *could an answer be wrong?* — never *how expensive is the
+fix?* A missing-postings shortfall, however large, is `heal: 'repair'` (re-post
+exactly what the ledger names, reads serving throughout); it can never withhold
+serving just because healing it takes work. `serving` is withheld only by a
+small, named set of rebuild-graded conditions — the index not initialized, its
+durable state absent, a manifest naming files that are not resident, a replay
+that did not complete cleanly — the states in which an answer could genuinely be
+wrong. And a read is only ever refused by the family it actually consults: a
+metadata filter is answered by the metadata index alone, vector search by the
+vector index, traversal by the graph index — one family's refusal never blocks
+another family's reads.
+
+## Reads refuse — they never rebuild
+
+A query that reaches a not-serving provider does not trigger a rebuild from inside
+the read. Brainy retired that path deliberately: a rebuild kicked off by an ordinary
+`find({ where: { status: 'active' } })` call is a dark, unpredictable cost hiding
+behind a request that looks like a cheap read. Instead, the read throws a typed,
+catchable error naming the reason:
+
+| Error | Thrown when | Meaning |
+|---|---|---|
+| `GraphIndexNotReadyError` | `find({ connected })`, `neighbors()`, `related()` | The graph adjacency index isn't serving — traversal would otherwise return `[]` indistinguishable from "no relationships" |
+| `MetadataIndexNotReadyError` | `find({ where })` | The metadata/field index isn't serving — a filtered read would otherwise return `[]` indistinguishable from "no matches" |
+| `VectorIndexNotReadyError` | `find({ query })`, `similar()` | The vector index isn't serving — a semantic search would otherwise return `[]` indistinguishable from "nothing similar" |
+
+All three are exported from `@soulcraftlabs/brainy`. Catch them where your application
+needs to distinguish "this index isn't ready yet" from "there's genuinely nothing
+here" — a health dashboard, a retry policy, an operator alert. The fix is always
+the same: reconcile the index, either by reopening the brain (which brings every
+provider to serving before `init()` returns — see the next section) or by calling
+`repairIndex()` explicitly.
+
+```typescript
+try {
+ const active = await brain.find({ where: { status: 'active' } })
+} catch (err) {
+ if (err instanceof MetadataIndexNotReadyError) {
+ // not a "no results" — the index itself refused; alert or retry after repair
+ } else {
+ throw err
+ }
+}
+```
+
+### Rebuilds happen at open, not on first query
+
+`brain.init()` runs every needed rebuild to completion **before it returns**,
+unconditionally, regardless of dataset size. There is no lazy, first-query
+rebuild path anymore — a brain either finishes opening healthy, or it fails
+open loudly. `disableAutoRebuild: true` no longer defers index construction to
+the first query: it has no effect on *when* a needed rebuild runs. Full manual
+control over rebuilds is `repairIndex({ rebuild: [...] })` (below), not this flag.
+
+## `repairIndex()` — checking and healing
+
+Bare `repairIndex()` is **report-driven**: it only heals what its own checks say
+actually needs it, and it always returns a full per-family receipt.
+
+```typescript
+const report = await brain.repairIndex()
+report.healedTotal // total items healed across every family
+report.durationMs
+report.families // one row per family checked
+```
+
+Each `RepairFamilyReport` row names what happened:
+
+- **`checked`** — was this family actually examined (`false` means skipped —
+ see `skipped` for why).
+- **`healed`** — items re-posted or corrected in place.
+- **`missing`** — when the check can name what diverged: an exact `count` plus a
+ capped `sample` of ids.
+- **`rebuilt`** — a full generational rebuild ran (as opposed to an incremental
+ heal).
+- **`detail`** / **`reason`** / **`skipped`** — the receipt's narration; a row is
+ always either checked or explains why it wasn't. Nothing is silent.
+
+On every call, bare `repairIndex()`:
+
+1. Prunes orphaned canonical containers left by a partial delete.
+2. Recomputes the count rollups from one canonical walk (unconditional — this is
+ also what clears a `suspect` ledger; see below).
+3. Reconciles VFS containment edges, if the VFS is initialized.
+4. Runs the metadata index's own corruption detection pass.
+5. Consults each of the three derived-index providers' own health check and
+ rebuilds only a family whose failing invariant actually asks for it
+ (`heal: 'rebuild'`) — never a provider that reports `healthy` or a lesser
+ grade.
+
+### The explicit rebuild door
+
+`options.rebuild` skips the health check and rebuilds one or more families
+**unconditionally** — the operator override for when you have independent reason
+to distrust a family regardless of what it self-reports (a suspicious deploy, a
+storage-layer incident, a support ticket that doesn't match what the health report
+says):
+
+```typescript
+// Force the graph adjacency to rebuild from canonical, no invariant consulted
+await brain.repairIndex({ rebuild: ['graph'] })
+
+// Force all three derived indexes
+await brain.repairIndex({ rebuild: 'all' })
+```
+
+A family named this way is recorded with `rebuilt: true` and
+`reason: 'explicit rebuild requested'`, and is skipped by the normal
+health-driven pass in the same call — it was already rebuilt unconditionally.
+
+Reach for the explicit door when you need certainty regardless of self-report;
+reach for bare `repairIndex()` for routine maintenance and after any incident
+where you're not sure which family (if any) needs it.
+
+## What `suspect` counts mean
+
+Storage's canonical count ledger increments the ALL-visibility total on every new
+record and decrements it on every *proven* delete — one where the record was read,
+or the caller supplied its prior image. A delete that cannot prove what it removed
+existed doesn't guess: it flags the ledger `suspect` (an operator-visible
+`console.warn`, narrated once per session, not once per delete) rather than risk
+decrementing a total that was never incremented for that record in the first
+place. This is intentionally rare — it's a defensive fallback for callers on an
+unusual removal path, not a per-delete cost.
+
+`suspect` is not directly exposed on any `Brainy` method today — it lives on the
+`StorageAdapter`'s optional `getCanonicalCounts()`, primarily consulted by
+`repairIndex()`'s recount step and by custom storage adapters composing their own
+`healthReport()`. What matters for an application: a `suspect` ledger is not
+incorrect, just *unverified since the last recount* — and `repairIndex()`'s
+unconditional count-rollup step (step 2, above) recomputes the ALL scalars from a
+real canonical walk on every call, clearing the flag with proof either way.
+
+## Practical guidance
+
+- **On a normal restart**, do nothing — `init()` brings every provider to
+ serving before it returns, or fails loudly.
+- **On a `*NotReadyError`** from a live read, reconcile with `repairIndex()`
+ (report-driven is almost always sufficient) and retry.
+- **After an incident** where you distrust a specific family regardless of what
+ it reports healthy — a storage-layer fault, a suspicious restore — use the
+ explicit door: `repairIndex({ rebuild: ['metadata' | 'graph' | 'vector'] })`.
+- **To audit before trusting a report**, `brain.auditGraph()` walks every stored
+ relationship and proves (or disproves) that reads return canonical truth,
+ independent of what any provider self-reports — see
+ [Inspecting a Live Brainy](../guides/inspection.md).
diff --git a/docs/concepts/storage-adapters.md b/docs/concepts/storage-adapters.md
index af6d068f..82aa01e8 100644
--- a/docs/concepts/storage-adapters.md
+++ b/docs/concepts/storage-adapters.md
@@ -61,7 +61,7 @@ The only required override is the capability flag. Returning `true` from
to call `acquireWriterLock()` at init.
```typescript
-import { FileSystemStorage } from '@soulcraft/brainy'
+import { FileSystemStorage } from '@soulcraftlabs/brainy'
export class MmapFileSystemStorage extends FileSystemStorage {
public supportsMultiProcessLocking(): boolean {
@@ -79,7 +79,7 @@ If your storage is **not filesystem-backed** (a custom
network backend), extend `BaseStorage` directly:
```typescript
-import { BaseStorage } from '@soulcraft/brainy'
+import { BaseStorage } from '@soulcraftlabs/brainy'
export class MyCloudStorage extends BaseStorage {
// BaseStorage's default no-op implementations of the multi-process
@@ -101,7 +101,7 @@ The defensive check at every new-storage-method call site (`brainy.ts`,
`hasStorageMethod(name)`) does **not** exist to handle "plugin bundles a
stale BaseStorage." Plugins ship a dist that preserves the dynamic ESM
import (verify in your plugin's `dist/`: `import { FileSystemStorage } from
-'@soulcraft/brainy'` is not rewritten to a vendored copy). The prototype
+'@soulcraftlabs/brainy'` is not rewritten to a vendored copy). The prototype
chain at runtime resolves to whatever Brainy version your consumer has
installed.
@@ -109,8 +109,8 @@ installed.
the prototype chain at the consumer-app level:
- **Stale `node_modules`** — a lingering install from before the consumer
- upgraded Brainy. The package.json says `@soulcraft/brainy@7.22.0` but
- `node_modules/@soulcraft/brainy` is still 7.20.x.
+ upgraded Brainy. The package.json says `@soulcraftlabs/brainy@7.22.0` but
+ `node_modules/@soulcraftlabs/brainy` is still 7.20.x.
- **Lockfile drift** — `bun.lockb` / `package-lock.json` pins a brainy
version older than the package.json range, and `bun install` honors the
lockfile.
@@ -131,7 +131,7 @@ and the warning names the adapter class plus a remediation hint:
methods on its prototype chain. Writer locking and the flush-request RPC are
disabled for this directory. Likely fix: clean install (`rm -rf node_modules
bun.lockb && bun install`) or rebuild your container image to refresh
-`@soulcraft/brainy` to ≥7.21. See docs/concepts/storage-adapters.md.
+`@soulcraftlabs/brainy` to ≥7.21. See docs/concepts/storage-adapters.md.
```
## Authoring a new storage adapter — minimum checklist
@@ -168,7 +168,7 @@ bun.lockb && bun install`) or rebuild your container image to refresh
install time — fix install, not your plugin.
6. **Pin your peer dep generously.** `"peerDependencies": {
- "@soulcraft/brainy": "^7.21.0" }` accepts any compatible 7.x. Don't pin
+ "@soulcraftlabs/brainy": "^7.21.0" }` accepts any compatible 7.x. Don't pin
to an exact patch unless you're tracking a known regression.
## Future direction
@@ -185,5 +185,5 @@ follow-up; consumers don't need to anticipate the change.
heartbeat semantics, what the lock protects.
- [`guides/inspection`](../guides/inspection.md) — `brainy inspect` and the
read-only mode.
-- `node_modules/@soulcraft/brainy/dist/storage/baseStorage.d.ts` — the
+- `node_modules/@soulcraftlabs/brainy/dist/storage/baseStorage.d.ts` — the
authoritative type signatures for every method this page references.
diff --git a/docs/guides/aggregation.md b/docs/guides/aggregation.md
index 11d86ec8..616c8fc4 100644
--- a/docs/guides/aggregation.md
+++ b/docs/guides/aggregation.md
@@ -22,7 +22,7 @@ they share a single scan.
## Quick Start
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
diff --git a/docs/guides/framework-integration.md b/docs/guides/framework-integration.md
index 984466c5..8f85da00 100644
--- a/docs/guides/framework-integration.md
+++ b/docs/guides/framework-integration.md
@@ -8,7 +8,7 @@ Brainy is **framework-friendly** - designed to drop into the server side of any
Brainy embeds an HNSW vector index, a graph engine, and a filesystem-backed persistence layer. These belong on the server:
-- **Zero configuration**: Just `import { Brainy } from '@soulcraft/brainy'`
+- **Zero configuration**: Just `import { Brainy } from '@soulcraftlabs/brainy'`
- **Auto storage detection**: `new Brainy()` auto-selects filesystem persistence on Node
- **Cleaner code**: No browser polyfills, no conditional client/server imports
- **Better DX**: One instance shared across your server routes
@@ -18,13 +18,13 @@ Brainy embeds an HNSW vector index, a graph engine, and a filesystem-backed pers
### Install Brainy
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
### Basic Integration
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// Run on the server (API route, server component, backend service)
// new Brainy() auto-detects filesystem persistence on Node
@@ -105,7 +105,7 @@ On the server, create one Brainy instance and reuse it across requests. This mod
```javascript
// lib/brain.server.js
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brainPromise
@@ -163,7 +163,7 @@ On the server, create one Brainy instance and reuse it across requests:
```javascript
// server/brain.js (server-only module)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brainPromise
@@ -248,7 +248,7 @@ The matching backend endpoint uses Brainy directly (Node/Bun):
```typescript
// server: api/search
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy() // auto-detects filesystem persistence on Node
await brain.init()
@@ -266,7 +266,7 @@ In Next.js, Brainy lives in server code only: API routes, server components, or
```javascript
// lib/brain.server.js (imported only by server code)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brainPromise
@@ -318,7 +318,7 @@ Brainy runs in a server-only module (`*.server.js`); the component fetches resul
```javascript
// src/lib/server/brain.js (server-only — note the .server suffix)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brainPromise
@@ -432,7 +432,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
ssr: {
- external: ['@soulcraft/brainy']
+ external: ['@soulcraftlabs/brainy']
}
})
```
@@ -440,7 +440,7 @@ export default defineConfig({
```javascript
// rollup.config.js (server bundle)
export default {
- external: ['@soulcraft/brainy', 'node:fs', 'node:path', 'node:crypto']
+ external: ['@soulcraftlabs/brainy', 'node:fs', 'node:path', 'node:crypto']
}
```
@@ -466,7 +466,7 @@ export async function load({ url }) {
```javascript
// For build-time usage (runs in Node during the build)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
export async function generateStaticProps() {
const brain = new Brainy({
@@ -513,7 +513,7 @@ export async function generateStaticProps() {
### Issue: Large client bundle size
**Cause**: A client module is pulling in Brainy.
-**Solution**: Move the `import { Brainy } from '@soulcraft/brainy'` into a server-only module so it never reaches the browser bundle.
+**Solution**: Move the `import { Brainy } from '@soulcraftlabs/brainy'` into a server-only module so it never reaches the browser bundle.
### Issue: SSR hydration mismatch
**Solution**: Run the search on the server (loader / server action / API route) and pass the results down as props, so server and client render the same markup.
diff --git a/docs/guides/import-anything.md b/docs/guides/import-anything.md
index b1bb15ef..ffabe55c 100644
--- a/docs/guides/import-anything.md
+++ b/docs/guides/import-anything.md
@@ -9,7 +9,7 @@ Brainy's import is **ONE magical method** that understands EVERYTHING:
## The Ultimate Simplicity
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
diff --git a/docs/guides/import-progress-examples.md b/docs/guides/import-progress-examples.md
index 66f50713..18c3cb9a 100644
--- a/docs/guides/import-progress-examples.md
+++ b/docs/guides/import-progress-examples.md
@@ -13,7 +13,7 @@ Brainy provides real-time progress tracking for **all 7 supported file formats**
### Basic Progress Tracking
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import * as fs from 'fs'
const brain = await Brainy.create()
diff --git a/docs/guides/import-quick-reference.md b/docs/guides/import-quick-reference.md
index 7837d49e..3bc26dae 100644
--- a/docs/guides/import-quick-reference.md
+++ b/docs/guides/import-quick-reference.md
@@ -7,7 +7,7 @@
## Basic Import
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -187,7 +187,7 @@ await brain.import(file, {
## Complete Example
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import * as fs from 'fs'
async function importCatalog() {
diff --git a/docs/guides/inspection.md b/docs/guides/inspection.md
index 240e81ae..8560b543 100644
--- a/docs/guides/inspection.md
+++ b/docs/guides/inspection.md
@@ -108,7 +108,7 @@ check fails — useful for piping into monitoring or CI.
## Programmatic inspection
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const reader = await Brainy.openReadOnly({
storage: { type: 'filesystem', path: '/data/brain' }
diff --git a/docs/guides/installation.md b/docs/guides/installation.md
index 0a36f632..20d40ea2 100644
--- a/docs/guides/installation.md
+++ b/docs/guides/installation.md
@@ -21,21 +21,21 @@ next:
## Install
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
Or with your preferred package manager:
```bash
-bun add @soulcraft/brainy
-yarn add @soulcraft/brainy
-pnpm add @soulcraft/brainy
+bun add @soulcraftlabs/brainy
+yarn add @soulcraftlabs/brainy
+pnpm add @soulcraftlabs/brainy
```
## Verify
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -52,7 +52,7 @@ npm install @soulcraft/cor
```
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy({ plugins: ['@soulcraft/cor'] })
await brain.init() // native providers registered during init
@@ -71,7 +71,7 @@ remains available on npm if you need it.
Brainy ships with full TypeScript types. No `@types/` package needed:
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
diff --git a/docs/guides/migration-3.36.0.md b/docs/guides/migration-3.36.0.md
index 8b1f239e..5f00534a 100644
--- a/docs/guides/migration-3.36.0.md
+++ b/docs/guides/migration-3.36.0.md
@@ -66,7 +66,7 @@ const results = await brain.search("query")
**New diagnostics for capacity planning and performance tuning.**
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -112,7 +112,7 @@ Recommendations: ${stats.recommendations.join(', ')}
### Step 1: Update Package
```bash
-npm install @soulcraft/brainy@latest
+npm install @soulcraftlabs/brainy@latest
```
### Step 2: Restart Your Application
@@ -134,7 +134,7 @@ npm run start
### Check Adaptive Sizing is Working
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -218,7 +218,7 @@ For debugging or compatibility testing:
If you need to rollback to v3.35.0:
```bash
-npm install @soulcraft/brainy@3.35.0
+npm install @soulcraftlabs/brainy@3.35.0
```
**Note:** We don't anticipate any issues, but rollback is straightforward if needed.
@@ -367,7 +367,7 @@ if (stats.fairness.fairnessViolation) {
## Next Steps
-1. ✅ **Upgrade:** `npm install @soulcraft/brainy@latest`
+1. ✅ **Upgrade:** `npm install @soulcraftlabs/brainy@latest`
2. 📊 **Monitor:** Use `getCacheStats()` to verify performance improvements
3. 🎯 **Tune:** Adjust based on recommendations (if needed)
4. 📖 **Read:** [Operations Guide](../operations/capacity-planning.md) for capacity planning
diff --git a/docs/guides/model-loading.md b/docs/guides/model-loading.md
index e5b7b1d6..cc1b2b6a 100644
--- a/docs/guides/model-loading.md
+++ b/docs/guides/model-loading.md
@@ -37,7 +37,7 @@ This single WASM file contains everything needed for sentence embeddings.
```bash
# Bun as a runtime — supported and recommended
-bun add @soulcraft/brainy
+bun add @soulcraftlabs/brainy
bun run server.ts
```
diff --git a/docs/guides/namespace-migration.md b/docs/guides/namespace-migration.md
index fad3c766..f7d2c7f7 100644
--- a/docs/guides/namespace-migration.md
+++ b/docs/guides/namespace-migration.md
@@ -80,7 +80,7 @@ If you read raw stored records (fact-log scanners, export tooling), use
the exported shape-aware splitters — they handle both record eras:
```typescript
-import { splitNounMetadataRecord } from '@soulcraft/brainy'
+import { splitNounMetadataRecord } from '@soulcraftlabs/brainy'
const { reserved, custom } = splitNounMetadataRecord(rawRecord)
// reserved = engine fields · custom = the user's bag, ANY names
```
@@ -88,7 +88,7 @@ const { reserved, custom } = splitNounMetadataRecord(rawRecord)
Feature detection (never version-sniff):
```typescript
-import * as brainy from '@soulcraft/brainy'
+import * as brainy from '@soulcraftlabs/brainy'
const lawActive = 'FIELD_ADDRESSING_CAPABILITY' in brainy // 'field-addressing/v1'
```
diff --git a/docs/guides/nextjs-integration.md b/docs/guides/nextjs-integration.md
index ab55e51f..25d6062d 100644
--- a/docs/guides/nextjs-integration.md
+++ b/docs/guides/nextjs-integration.md
@@ -9,7 +9,7 @@ Complete guide to integrating Brainy with Next.js applications, covering App Rou
```bash
npx create-next-app@latest my-brainy-app
cd my-brainy-app
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
### Basic Setup
@@ -18,7 +18,7 @@ npm install @soulcraft/brainy
// app/components/BrainyProvider.jsx
'use client'
import { createContext, useContext, useEffect, useState } from 'react'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const BrainyContext = createContext()
@@ -271,7 +271,7 @@ export default function SearchPage() {
```javascript
// app/api/search/route.js (App Router)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brain = null
@@ -332,7 +332,7 @@ export async function GET() {
```javascript
// pages/api/search.js (Pages Router)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brain = null
@@ -374,7 +374,7 @@ export default async function handler(req, res) {
```javascript
// app/api/data/route.js
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brain = null
@@ -418,7 +418,7 @@ export async function POST(request) {
```jsx
// app/actions/brainy.js
'use server'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brain = null
@@ -630,7 +630,7 @@ CMD ["npm", "start"]
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
- serverComponentsExternalPackages: ['@soulcraft/brainy']
+ serverComponentsExternalPackages: ['@soulcraftlabs/brainy']
},
webpack: (config, { isServer }) => {
if (!isServer) {
@@ -797,7 +797,7 @@ export function rateLimit(req, limit = 100, window = 60000) {
// app/contexts/BrainyContext.jsx
'use client'
import { createContext, useContext, useReducer, useEffect } from 'react'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const BrainyContext = createContext()
@@ -873,7 +873,7 @@ import { BrainyProvider } from '../app/components/BrainyProvider'
import { Search } from '../app/components/Search'
// Mock Brainy
-jest.mock('@soulcraft/brainy', () => ({
+jest.mock('@soulcraftlabs/brainy', () => ({
Brainy: jest.fn().mockImplementation(() => ({
init: jest.fn().mockResolvedValue(undefined),
find: jest.fn().mockResolvedValue([
diff --git a/docs/guides/optimistic-concurrency.md b/docs/guides/optimistic-concurrency.md
index 268bc5fa..2984998b 100644
--- a/docs/guides/optimistic-concurrency.md
+++ b/docs/guides/optimistic-concurrency.md
@@ -32,7 +32,7 @@ Brainy 7.31.0 adds a per-entity revision counter so multiple writers can coordin
Every distributed-job scheduler eventually wants this exact loop:
```ts
-import { Brainy, RevisionConflictError } from '@soulcraft/brainy'
+import { Brainy, RevisionConflictError } from '@soulcraftlabs/brainy'
const LOCK_ID = '...uuid for this job slot...'
@@ -137,7 +137,7 @@ await brain.addIfMissing({ // ← not a real API
It's race-prone as a plain read-then-write: two concurrent imports both see "not found," both insert, you get duplicates. Without a unique-index primitive (which Brainy doesn't have today), close the race with whole-store CAS — read at a pinned generation, then commit only if nothing moved:
```ts
-import { GenerationConflictError } from '@soulcraft/brainy'
+import { GenerationConflictError } from '@soulcraftlabs/brainy'
async function addIfMissingByEmail(email: string, data: string) {
for (let attempt = 0; attempt < 5; attempt++) {
diff --git a/docs/guides/quick-start.md b/docs/guides/quick-start.md
index 097c55fe..d9a4e896 100644
--- a/docs/guides/quick-start.md
+++ b/docs/guides/quick-start.md
@@ -18,13 +18,13 @@ Get Brainy running in under a minute.
## 1. Install
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
## 2. Initialize
```typescript
-import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
+import { Brainy, NounType, VerbType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -67,7 +67,7 @@ await brain.relate({
## 5. Query with Triple Intelligence
```typescript
-import type { Result } from '@soulcraft/brainy'
+import type { Result } from '@soulcraftlabs/brainy'
// All three search paradigms in one call
const results: Result[] = await brain.find({
diff --git a/docs/guides/standard-import-progress.md b/docs/guides/standard-import-progress.md
index 9f2e2e5b..27dabe75 100644
--- a/docs/guides/standard-import-progress.md
+++ b/docs/guides/standard-import-progress.md
@@ -11,7 +11,7 @@
### One Interface for Everything
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = await Brainy.create()
@@ -78,7 +78,7 @@ interface ImportProgress {
```typescript
import { useState } from 'react'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
function UniversalImportProgress({ file }: { file: File }) {
const [progress, setProgress] = useState({
@@ -177,7 +177,7 @@ function UniversalImportProgress({ file }: { file: File }) {
```typescript
import ora from 'ora'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
async function importWithProgress(filePath: string) {
const spinner = ora('Starting import...').start()
diff --git a/docs/guides/storage-adapters.md b/docs/guides/storage-adapters.md
index 06ec9f3a..a4224bc8 100644
--- a/docs/guides/storage-adapters.md
+++ b/docs/guides/storage-adapters.md
@@ -28,7 +28,7 @@ on-disk layout (memory's "disk" is a JS Map).
## Quick start
```ts
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// Filesystem (recommended for any persistent workload):
const brain = new Brainy({
@@ -134,7 +134,7 @@ config; the `type` is optional.
If you want to skip the factory:
```ts
-import { FileSystemStorage, MemoryStorage } from '@soulcraft/brainy'
+import { FileSystemStorage, MemoryStorage } from '@soulcraftlabs/brainy'
const fsStorage = new FileSystemStorage('./brainy-data')
const memStorage = new MemoryStorage()
diff --git a/docs/guides/subtypes-and-facets.md b/docs/guides/subtypes-and-facets.md
index ff5de320..74311528 100644
--- a/docs/guides/subtypes-and-facets.md
+++ b/docs/guides/subtypes-and-facets.md
@@ -34,7 +34,7 @@ Three layers solve this:
### Write
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -240,7 +240,7 @@ await brain.migrateField({
A realistic adoption sequence for a brain that started without these primitives:
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
const brain = new Brainy({ storage: { type: 'filesystem', path: './brain-data' } })
await brain.init()
diff --git a/docs/guides/upgrading-7-to-8.md b/docs/guides/upgrading-7-to-8.md
index a3c64fb9..53aa2a5c 100644
--- a/docs/guides/upgrading-7-to-8.md
+++ b/docs/guides/upgrading-7-to-8.md
@@ -25,7 +25,7 @@ content — and how 8.0 recovers it for you.
## TL;DR
-- **Just upgrade to `@soulcraft/brainy@8.0.12` (or later) and open the store.**
+- **Just upgrade to `@soulcraftlabs/brainy@8.0.12` (or later) and open the store.**
If a previous upgrade left VFS content stranded, 8.0.12 **heals it on open**,
with no operator action.
- Want to force or script it? Call **`await brain.vfs.adoptOrphanedBlobs()`**.
@@ -90,7 +90,7 @@ So the operator action for a stranded store is simply: **upgrade to 8.0.12 and
open it.**
```ts
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// Opening the store is all that is required — recovery runs during init().
const brain = new Brainy({ storage: { type: 'filesystem', path: '/data/my-store' } })
@@ -182,5 +182,5 @@ and opening each store is sufficient.
The recovery is copy-only, so no rollback of the recovery itself is ever needed.
If you need to roll back the **whole** 7→8 upgrade, restore the directory from
your pre-upgrade backup (retained automatically while recovery is incomplete, or
-your own snapshot) and pin `@soulcraft/brainy@7.x`. 8.0 does not keep the old
+your own snapshot) and pin `@soulcraftlabs/brainy@7.x`. 8.0 does not keep the old
branch layout in place, so a directory-level restore is the rollback path.
diff --git a/docs/guides/vue-integration.md b/docs/guides/vue-integration.md
index 7f7c6a06..34d18ebf 100644
--- a/docs/guides/vue-integration.md
+++ b/docs/guides/vue-integration.md
@@ -12,7 +12,7 @@ Complete guide to integrating Brainy with Vue.js applications, covering Vue 3, N
npm create vue@latest my-brainy-app
cd my-brainy-app
npm install
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
### Basic Setup
@@ -574,7 +574,7 @@ Nuxt's server engine (Nitro) is the natural home for Brainy: it runs on Node/Bun
```javascript
// server/utils/brain.js (server-only — Nitro never bundles this into the client)
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
let brainPromise
@@ -1201,7 +1201,7 @@ import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
ssr: {
- external: ['@soulcraft/brainy']
+ external: ['@soulcraftlabs/brainy']
}
})
```
diff --git a/docs/neural-extraction.md b/docs/neural-extraction.md
index 989b1b60..cfb6d764 100644
--- a/docs/neural-extraction.md
+++ b/docs/neural-extraction.md
@@ -24,7 +24,7 @@ Brainy's neural extraction system uses a **4-signal ensemble architecture** to c
### Method 1: Brain Instance (Recommended)
```typescript
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -62,9 +62,9 @@ const people = await brain.extractEntities('...', {
import {
SmartExtractor,
SmartRelationshipExtractor
-} from '@soulcraft/brainy'
+} from '@soulcraftlabs/brainy'
// Or use subpath imports:
-import { SmartExtractor } from '@soulcraft/brainy/neural/SmartExtractor'
+import { SmartExtractor } from '@soulcraftlabs/brainy/neural/SmartExtractor'
const brain = new Brainy()
await brain.init()
@@ -176,7 +176,7 @@ const withVectors = await brain.extractEntities(text, {
**Direct entity type classifier.** Use when you have pre-detected candidates or need custom configuration.
```typescript
-import { SmartExtractor, FormatContext } from '@soulcraft/brainy'
+import { SmartExtractor, FormatContext } from '@soulcraftlabs/brainy'
const extractor = new SmartExtractor(brain, {
minConfidence: 0.7, // Threshold
@@ -229,7 +229,7 @@ interface ExtractionResult {
**Relationship type classifier.** Determines verb/relationship types between entities.
```typescript
-import { SmartRelationshipExtractor } from '@soulcraft/brainy'
+import { SmartRelationshipExtractor } from '@soulcraftlabs/brainy'
const relExtractor = new SmartRelationshipExtractor(brain, {
minConfidence: 0.6,
@@ -286,7 +286,7 @@ const rel = await relExtractor.infer(
**Full extraction orchestrator.** Handles candidate detection, classification, and deduplication.
```typescript
-import { NeuralEntityExtractor } from '@soulcraft/brainy'
+import { NeuralEntityExtractor } from '@soulcraftlabs/brainy'
const extractor = new NeuralEntityExtractor(brain)
@@ -607,7 +607,7 @@ const locations = entities.filter(e => e.type === NounType.Location)
### Example 2: Excel Data Classification
```typescript
-import { SmartExtractor } from '@soulcraft/brainy'
+import { SmartExtractor } from '@soulcraftlabs/brainy'
const extractor = new SmartExtractor(brain)
@@ -629,7 +629,7 @@ for (let i = 0; i < cells.length; i++) {
### Example 3: Relationship Extraction
```typescript
-import { SmartRelationshipExtractor } from '@soulcraft/brainy'
+import { SmartRelationshipExtractor } from '@soulcraftlabs/brainy'
const relExtractor = new SmartRelationshipExtractor(brain)
diff --git a/docs/transactions.md b/docs/transactions.md
index fce7d10e..cbea39c0 100644
--- a/docs/transactions.md
+++ b/docs/transactions.md
@@ -204,8 +204,8 @@ await brain.add({ data: { name: 'Entity' }, type: NounType.Thing })
### Basic Add Operation
```typescript
-import { Brainy } from '@soulcraft/brainy'
-import { NounType } from '@soulcraft/brainy/types'
+import { Brainy } from '@soulcraftlabs/brainy'
+import { NounType } from '@soulcraftlabs/brainy/types'
const brain = new Brainy()
await brain.init()
@@ -428,7 +428,7 @@ await brain.relate({ ... }) // a crash here leaves the entity unlinked
```typescript
import { describe, it, expect } from 'vitest'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
describe('Transaction Tests', () => {
it('should rollback on failure', async () => {
diff --git a/docs/universal-display-augmentation.md b/docs/universal-display-augmentation.md
index da42874c..464b91fb 100644
--- a/docs/universal-display-augmentation.md
+++ b/docs/universal-display-augmentation.md
@@ -23,7 +23,7 @@ The Universal Display Augmentation is a powerful AI-powered system that automati
### Basic Usage
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brainy = new Brainy()
await brainy.init()
diff --git a/docs/vfs/PROJECTION_STRATEGY_API.md b/docs/vfs/PROJECTION_STRATEGY_API.md
index 380862e1..f1319d5b 100644
--- a/docs/vfs/PROJECTION_STRATEGY_API.md
+++ b/docs/vfs/PROJECTION_STRATEGY_API.md
@@ -71,9 +71,9 @@ Let's build a projection that organizes files by priority (high, medium, low):
### Step 1: Create the Strategy Class
```typescript
-import { BaseProjectionStrategy } from '@soulcraft/brainy/vfs/semantic'
-import { Brainy } from '@soulcraft/brainy'
-import { VirtualFileSystem, VFSEntity } from '@soulcraft/brainy/vfs'
+import { BaseProjectionStrategy } from '@soulcraftlabs/brainy/vfs/semantic'
+import { Brainy } from '@soulcraftlabs/brainy'
+import { VirtualFileSystem, VFSEntity } from '@soulcraftlabs/brainy/vfs'
export class PriorityProjection extends BaseProjectionStrategy {
readonly name = 'priority'
@@ -141,7 +141,7 @@ export class PriorityProjection extends BaseProjectionStrategy {
### Step 2: Register the Strategy
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import { PriorityProjection } from './PriorityProjection'
const brain = new Brainy()
@@ -537,7 +537,7 @@ Use the projection's resolve cache:
```typescript
import { describe, it, expect, beforeAll } from 'vitest'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import { PriorityProjection } from './PriorityProjection'
describe('PriorityProjection', () => {
@@ -714,7 +714,7 @@ async resolve(brain, vfs, value: string) {
3. Use appropriate limits: Don't fetch more than needed
### Type errors
-1. Import correct types: `import { Brainy, VirtualFileSystem } from '@soulcraft/brainy'`
+1. Import correct types: `import { Brainy, VirtualFileSystem } from '@soulcraftlabs/brainy'`
2. Use `as VFSEntity` when mapping results
3. Check BaseProjectionStrategy import
diff --git a/docs/vfs/QUICK_START.md b/docs/vfs/QUICK_START.md
index 8b0efce6..4a1f83dc 100644
--- a/docs/vfs/QUICK_START.md
+++ b/docs/vfs/QUICK_START.md
@@ -14,11 +14,11 @@ A file explorer that:
## ⚡ Step 1: Basic Setup (1 minute)
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// ✅ CORRECT: Use filesystem storage for production
const brain = new Brainy({
@@ -115,7 +115,7 @@ Here's a complete React component using the correct patterns:
```tsx
import React, { useState, useEffect } from 'react'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
export function FileExplorer() {
const [brain, setBrain] = useState(null)
@@ -288,8 +288,8 @@ Your file explorer is now working! Here's what to explore next:
### "Module not found" errors
```bash
# Make sure you're using the right import
-npm ls @soulcraft/brainy # Check version
-npm install @soulcraft/brainy@latest # Update if needed
+npm ls @soulcraftlabs/brainy # Check version
+npm install @soulcraftlabs/brainy@latest # Update if needed
```
### "VFS not initialized" errors
diff --git a/docs/vfs/README.md b/docs/vfs/README.md
index b95f0d7b..a94910c9 100644
--- a/docs/vfs/README.md
+++ b/docs/vfs/README.md
@@ -24,7 +24,7 @@ Brainy VFS is a revolutionary virtual filesystem that runs on top of Brainy's ne
## Quick Start
```javascript
-import { VirtualFileSystem } from '@soulcraft/brainy/vfs'
+import { VirtualFileSystem } from '@soulcraftlabs/brainy/vfs'
// Initialize the VFS
const vfs = new VirtualFileSystem({
@@ -381,7 +381,7 @@ Brainy VFS fully leverages Brainy's revolutionary Triple Intelligence system:
## Installation
```bash
-npm install @soulcraft/brainy
+npm install @soulcraftlabs/brainy
```
## Requirements
diff --git a/docs/vfs/ROADMAP.md b/docs/vfs/ROADMAP.md
index 93c5b901..c8d15cd2 100644
--- a/docs/vfs/ROADMAP.md
+++ b/docs/vfs/ROADMAP.md
@@ -135,7 +135,7 @@ Mount VFS as a native filesystem on Linux/Mac/Windows.
```typescript
// Planned (research phase)
-import { mountVFS } from '@soulcraft/brainy/vfs/fuse'
+import { mountVFS } from '@soulcraftlabs/brainy/vfs/fuse'
await mountVFS(vfs, {
mountPoint: '/mnt/brainy',
@@ -160,7 +160,7 @@ These features would benefit from community contributions. If you're interested
### Express.js Static Middleware
```typescript
// Wanted: Community contribution
-import { createStaticMiddleware } from '@soulcraft/brainy/vfs/express'
+import { createStaticMiddleware } from '@soulcraftlabs/brainy/vfs/express'
app.use('/files', createStaticMiddleware(vfs, {
index: ['index.html', 'index.md'],
@@ -172,7 +172,7 @@ app.use('/files', createStaticMiddleware(vfs, {
### VSCode Extension
```typescript
// Wanted: Community contribution
-import { VFSProvider } from '@soulcraft/brainy/vfs/vscode'
+import { VFSProvider } from '@soulcraftlabs/brainy/vfs/vscode'
const provider = new VFSProvider(vfs)
vscode.workspace.registerFileSystemProvider('brainy', provider)
diff --git a/docs/vfs/SEMANTIC_VFS.md b/docs/vfs/SEMANTIC_VFS.md
index 9298c822..f34ee9ae 100644
--- a/docs/vfs/SEMANTIC_VFS.md
+++ b/docs/vfs/SEMANTIC_VFS.md
@@ -327,7 +327,7 @@ console.log(id1 === id2 && id2 === id3) // true
Create your own semantic dimensions:
```typescript
-import { BaseProjectionStrategy } from '@soulcraft/brainy/vfs/semantic'
+import { BaseProjectionStrategy } from '@soulcraftlabs/brainy/vfs/semantic'
class PriorityProjection extends BaseProjectionStrategy {
readonly name = 'priority'
diff --git a/docs/vfs/VFS_API_GUIDE.md b/docs/vfs/VFS_API_GUIDE.md
index e0c6a94c..5dcaaeb8 100644
--- a/docs/vfs/VFS_API_GUIDE.md
+++ b/docs/vfs/VFS_API_GUIDE.md
@@ -7,7 +7,7 @@ Brainy's Virtual Filesystem (VFS) provides a POSIX-like filesystem interface tha
## Quick Start
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// Initialize Brainy
const brain = new Brainy({
@@ -598,7 +598,7 @@ const user = await store.findById('users', 'user123')
VFS uses standard POSIX-style errors:
```typescript
-import { VFSError, VFSErrorCode } from '@soulcraft/brainy'
+import { VFSError, VFSErrorCode } from '@soulcraftlabs/brainy'
try {
await vfs.readFile('/nonexistent.txt')
diff --git a/docs/vfs/VFS_CORE.md b/docs/vfs/VFS_CORE.md
index 1eeaf9f8..c1d502c0 100644
--- a/docs/vfs/VFS_CORE.md
+++ b/docs/vfs/VFS_CORE.md
@@ -280,7 +280,7 @@ GitBridge provides Git import/export capabilities:
#### GitBridge Usage
```javascript
// Import and instantiate GitBridge
-import { GitBridge } from '@soulcraft/brainy'
+import { GitBridge } from '@soulcraftlabs/brainy'
const gitBridge = new GitBridge(vfs, brain)
// Export VFS to Git repository structure
@@ -452,7 +452,7 @@ This ordering prevents race conditions where file writes might fail because pare
## Complete Example
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
async function vfsExample() {
// Initialize
diff --git a/docs/vfs/VFS_GRAPH_TYPES.md b/docs/vfs/VFS_GRAPH_TYPES.md
index 3c1f30f0..478bef7f 100644
--- a/docs/vfs/VFS_GRAPH_TYPES.md
+++ b/docs/vfs/VFS_GRAPH_TYPES.md
@@ -196,5 +196,5 @@ await brain.relate({
Always import and use the type enums:
```javascript
-import { NounType, VerbType } from '@soulcraft/brainy'
+import { NounType, VerbType } from '@soulcraftlabs/brainy'
```
\ No newline at end of file
diff --git a/docs/vfs/VFS_INITIALIZATION.md b/docs/vfs/VFS_INITIALIZATION.md
index 97e6b0bf..fd12fc71 100644
--- a/docs/vfs/VFS_INITIALIZATION.md
+++ b/docs/vfs/VFS_INITIALIZATION.md
@@ -5,7 +5,7 @@
The Brainy VFS is automatically initialized during `brain.init()`. No separate initialization needed!
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
// Create and initialize Brainy
const brain = new Brainy({
@@ -71,7 +71,7 @@ VFS stores files as entities and relationships in the same graph as everything e
## Complete Example
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
async function useVFS() {
// Initialize Brainy
@@ -100,7 +100,7 @@ useVFS().catch(console.error)
## TypeScript Usage
```typescript
-import { Brainy, VirtualFileSystem } from '@soulcraft/brainy'
+import { Brainy, VirtualFileSystem } from '@soulcraftlabs/brainy'
class FileManager {
private brain: Brainy
diff --git a/docs/vfs/building-file-explorers.md b/docs/vfs/building-file-explorers.md
index 6bb31871..7514c12e 100644
--- a/docs/vfs/building-file-explorers.md
+++ b/docs/vfs/building-file-explorers.md
@@ -37,7 +37,7 @@ Brainy VFS provides safe, tree-aware methods that prevent these issues:
### Method 1: Use `getDirectChildren()` (Recommended)
```typescript
-import { Brainy, VirtualFileSystem } from '@soulcraft/brainy'
+import { Brainy, VirtualFileSystem } from '@soulcraftlabs/brainy'
const brain = new Brainy()
await brain.init()
@@ -97,7 +97,7 @@ Here's a complete example using React:
```tsx
import React, { useState, useEffect } from 'react'
-import { VirtualFileSystem } from '@soulcraft/brainy'
+import { VirtualFileSystem } from '@soulcraftlabs/brainy'
interface FileNode {
name: string
@@ -177,7 +177,7 @@ function TreeView({ node, onToggle, expanded }) {
If you must build trees manually from flat lists, use the `VFSTreeUtils`:
```typescript
-import { VFSTreeUtils } from '@soulcraft/brainy/vfs'
+import { VFSTreeUtils } from '@soulcraftlabs/brainy/vfs'
// Get all entities somehow
const allEntities = await vfs.getDescendants('/root')
diff --git a/examples/bluesky-distributed-setup.js b/examples/bluesky-distributed-setup.js
index 9e83cf25..e3b33506 100644
--- a/examples/bluesky-distributed-setup.js
+++ b/examples/bluesky-distributed-setup.js
@@ -7,7 +7,7 @@
* the Bluesky firehose with Brainy's distributed architecture
*/
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
import { WebSocket } from 'ws'
// =====================================================
diff --git a/examples/monitor-cache-performance.ts b/examples/monitor-cache-performance.ts
index 9d50d476..87c965a2 100644
--- a/examples/monitor-cache-performance.ts
+++ b/examples/monitor-cache-performance.ts
@@ -14,7 +14,7 @@
* ts-node examples/monitor-cache-performance.ts
*/
-import { Brainy, NounType } from '@soulcraft/brainy'
+import { Brainy, NounType } from '@soulcraftlabs/brainy'
// ANSI color codes for pretty output
const colors = {
diff --git a/integrations/README.md b/integrations/README.md
index aa3d795b..de156623 100644
--- a/integrations/README.md
+++ b/integrations/README.md
@@ -5,7 +5,7 @@ Connect Brainy to spreadsheets, BI tools, and external systems with zero configu
## Quick Start
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy({ integrations: true })
await brain.init()
@@ -178,7 +178,7 @@ Webhooks include `X-Brainy-Signature` header with HMAC-SHA256 signature.
### Minimal (in-memory):
```typescript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy({ integrations: true })
await brain.init()
@@ -194,7 +194,7 @@ console.log(brain.hub.getInstructions())
```typescript
import express from 'express'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const app = express()
const brain = new Brainy({
@@ -232,7 +232,7 @@ app.listen(3000, () => {
```typescript
import { Hono } from 'hono'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const app = new Hono()
diff --git a/integrations/google-sheets/README.md b/integrations/google-sheets/README.md
index b2b0af3a..8309a30a 100644
--- a/integrations/google-sheets/README.md
+++ b/integrations/google-sheets/README.md
@@ -99,7 +99,7 @@ Add the `BRAINY_URL` script property in Apps Script settings.
The simplest way to enable all integrations:
```javascript
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const brain = new Brainy({ integrations: true })
await brain.init()
@@ -112,7 +112,7 @@ With Express:
```javascript
import express from 'express'
-import { Brainy } from '@soulcraft/brainy'
+import { Brainy } from '@soulcraftlabs/brainy'
const app = express()
const brain = new Brainy({ integrations: true })
diff --git a/package-lock.json b/package-lock.json
index 8cee0038..c4f66561 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
- "name": "@soulcraft/brainy",
- "version": "10.4.0-rc.1",
+ "name": "@soulcraftlabs/brainy",
+ "version": "10.4.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
- "name": "@soulcraft/brainy",
- "version": "10.4.0-rc.1",
+ "name": "@soulcraftlabs/brainy",
+ "version": "10.4.4",
"license": "MIT",
"dependencies": {
"@msgpack/msgpack": "^3.1.2",
diff --git a/package.json b/package.json
index ef803c11..06ce0253 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,7 @@
{
- "name": "@soulcraft/brainy",
- "version": "10.4.0-rc.1",
+ "name": "@soulcraftlabs/brainy",
+ "version": "10.4.4",
+ "brainyContract": 1,
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. Stage 3 CANONICAL: 42 nouns × 127 verbs covering 96-97% of all human knowledge.",
"main": "dist/index.js",
"module": "dist/index.js",
@@ -126,15 +127,16 @@
"license": "MIT",
"private": false,
"publishConfig": {
- "access": "public"
+ "access": "public",
+ "registry": "https://source.soulcraft.com/api/packages/soulcraftlabs/npm/"
},
- "homepage": "https://source.soulcraft.com/soulcraft/brainy",
+ "homepage": "https://source.soulcraft.com/soulcraftlabs/open-brainy",
"bugs": {
- "url": "https://source.soulcraft.com/soulcraft/brainy/issues"
+ "url": "https://source.soulcraft.com/soulcraftlabs/open-brainy/issues"
},
"repository": {
"type": "git",
- "url": "git+https://source.soulcraft.com/soulcraft/brainy.git"
+ "url": "git+https://source.soulcraft.com/soulcraftlabs/open-brainy.git"
},
"files": [
"dist/**/*.js",
diff --git a/scripts/buildEmbeddedPatterns.ts b/scripts/buildEmbeddedPatterns.ts
index 73e51224..c046df45 100644
--- a/scripts/buildEmbeddedPatterns.ts
+++ b/scripts/buildEmbeddedPatterns.ts
@@ -10,6 +10,7 @@ import { TransformerEmbedding } from '../src/utils/embedding.js'
import * as fs from 'fs/promises'
import * as path from 'path'
import { fileURLToPath } from 'url'
+import { resolveDeterministicStamp } from './lib/deterministicStamp.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -97,13 +98,22 @@ async function buildEmbeddedPatterns() {
// Convert to base64 for embedding in TypeScript
const uint8 = new Uint8Array(buffer)
const base64 = Buffer.from(uint8).toString('base64')
-
+
+ // Deterministic stamp: derived from the git commit time of this
+ // generator's inputs, never from wall-clock time — two builds of the
+ // same source tree must produce byte-identical output.
+ const outputPath = path.join(__dirname, '..', 'src', 'neural', 'embeddedPatterns.ts')
+ const generatedStamp = resolveDeterministicStamp(
+ [path.join(__dirname, 'buildEmbeddedPatterns.ts'), libraryPath],
+ outputPath
+ )
+
// Generate TypeScript file with everything embedded
const tsContent = `/**
* 🧠 BRAINY EMBEDDED PATTERNS
*
* AUTO-GENERATED - DO NOT EDIT
- * Generated: ${new Date().toISOString()}
+ * Generated: ${generatedStamp}
* Patterns: ${libraryData.patterns.length}
* Coverage: 94-98% of all queries
*
@@ -197,7 +207,6 @@ prodLog.info(\`🧠 Brainy Pattern Library loaded: \${EMBEDDED_PATTERNS.length}
`
// Write the TypeScript file
- const outputPath = path.join(__dirname, '..', 'src', 'neural', 'embeddedPatterns.ts')
await fs.writeFile(outputPath, tsContent)
// Report statistics
diff --git a/scripts/buildTypeEmbeddings.ts b/scripts/buildTypeEmbeddings.ts
index 61bcf238..688d6ac1 100644
--- a/scripts/buildTypeEmbeddings.ts
+++ b/scripts/buildTypeEmbeddings.ts
@@ -11,6 +11,7 @@ import * as fs from 'fs/promises'
import * as path from 'path'
import { fileURLToPath } from 'url'
import { NounType, VerbType } from '../src/types/graphTypes.js'
+import { resolveDeterministicStamp } from './lib/deterministicStamp.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -373,12 +374,24 @@ async function buildTypeEmbeddings() {
const uint8 = new Uint8Array(buffer)
const base64 = Buffer.from(uint8).toString('base64')
+ // Deterministic stamp: derived from the git commit time of this
+ // generator's inputs, never from wall-clock time — two builds of the
+ // same source tree must produce byte-identical output.
+ const outputPath = path.join(__dirname, '..', 'src', 'neural', 'embeddedTypeEmbeddings.ts')
+ const generatedStamp = resolveDeterministicStamp(
+ [
+ path.join(__dirname, 'buildTypeEmbeddings.ts'),
+ path.join(__dirname, '..', 'src', 'types', 'graphTypes.ts')
+ ],
+ outputPath
+ )
+
// Generate TypeScript file
const tsContent = `/**
* 🧠 BRAINY EMBEDDED TYPE EMBEDDINGS
*
* AUTO-GENERATED - DO NOT EDIT
- * Generated: ${new Date().toISOString()}
+ * Generated: ${generatedStamp}
* Noun Types: ${nounTypes.length}
* Verb Types: ${verbTypes.length}
*
@@ -395,7 +408,7 @@ export const TYPE_METADATA = {
verbTypes: ${verbTypes.length},
totalTypes: ${totalTypes},
embeddingDimensions: ${embeddingDim},
- generatedAt: "${new Date().toISOString()}",
+ generatedAt: "${generatedStamp}",
sizeBytes: {
embeddings: ${buffer.byteLength},
base64: ${base64.length}
@@ -494,7 +507,6 @@ prodLog.info(\`🧠 Brainy Type Embeddings loaded: \${TYPE_METADATA.nounTypes} n
`
// Write the TypeScript file
- const outputPath = path.join(__dirname, '..', 'src', 'neural', 'embeddedTypeEmbeddings.ts')
await fs.writeFile(outputPath, tsContent)
// Report statistics
diff --git a/scripts/emit-contract-manifest.mjs b/scripts/emit-contract-manifest.mjs
new file mode 100644
index 00000000..be73d4ca
--- /dev/null
+++ b/scripts/emit-contract-manifest.mjs
@@ -0,0 +1,128 @@
+#!/usr/bin/env node
+/**
+ * Emit this build's API-contract manifest to docs/api-contract.json.
+ *
+ * WHY IT IS GENERATED, NOT WRITTEN: a hand-kept list of doors drifts from the
+ * code the first time somebody adds one. This reads the surface the build
+ * actually exposes — the prototype's own methods and accessors, the exported
+ * error classes, the `where` operator sets, the field-addressing vocabulary,
+ * the health verdicts — so a diff between two engines' manifests is a diff
+ * between two engines, never between two authors.
+ *
+ * Requirement marking (required / optional per door) is NOT derivable from the
+ * surface — it is a commitment, recorded with the contract's owner rather than
+ * here. This manifest carries the surface; the promise lives with the contract.
+ *
+ * Usage: node scripts/emit-contract-manifest.mjs [--check]
+ * --check exits non-zero when the committed manifest is stale.
+ */
+
+import { writeFileSync, readFileSync, existsSync } from 'node:fs'
+import { join, dirname } from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
+const OUT = join(ROOT, 'docs', 'api-contract.json')
+
+const { Brainy } = await import(join(ROOT, 'dist', 'brainy.js'))
+const errorsModule = await import(join(ROOT, 'dist', 'errors', 'brainyError.js'))
+const versionModule = await import(join(ROOT, 'dist', 'utils', 'version.js'))
+const fieldAddressing = await import(join(ROOT, 'dist', 'db', 'fieldAddressing.js'))
+
+/** Every own method and accessor on the class's prototype, minus the private ones. */
+function surfaceOf(ctor) {
+ const doors = []
+ for (const name of Object.getOwnPropertyNames(ctor.prototype)) {
+ if (name === 'constructor' || name.startsWith('_')) continue
+ const descriptor = Object.getOwnPropertyDescriptor(ctor.prototype, name)
+ if (!descriptor) continue
+ if (typeof descriptor.value === 'function') {
+ doors.push({ name, kind: 'method', arity: descriptor.value.length })
+ } else if (descriptor.get) {
+ doors.push({ name, kind: 'accessor' })
+ }
+ }
+ return doors.sort((a, b) => a.name.localeCompare(b.name))
+}
+
+const errors = Object.entries(errorsModule)
+ .filter(([name, value]) => typeof value === 'function' && /Error$/.test(name))
+ .map(([name]) => name)
+ .sort()
+
+// The operator sets, read from the engine's own refusal message so the
+// manifest can never disagree with the validator.
+const filterSource = readFileSync(join(ROOT, 'src', 'utils', 'metadataFilter.ts'), 'utf-8')
+const acceptedMatch = filterSource.match(/const VALUE_OPERATORS = new Set\(\[([\s\S]*?)\]\)/)
+if (!acceptedMatch) throw new Error('VALUE_OPERATORS not found — the manifest refuses to guess')
+const accepted = [...acceptedMatch[1].matchAll(/'([^']+)'/g)].map((m) => m[1]).sort()
+
+const indexSource = readFileSync(join(ROOT, 'src', 'utils', 'metadataIndex.ts'), 'utf-8')
+const refusedByIndex = ['endsWith', 'length', 'matches', 'startsWith'].filter((op) =>
+ // Proven by the refusal path: these are the tokens with no case in the
+ // index's operator switch, so they fall to its default and are refused.
+ !new RegExp(`case '${op}':`).test(indexSource)
+)
+const servedOnIndex = accepted.filter((op) => !refusedByIndex.includes(op))
+
+const manifest = {
+ contractVersion: versionModule.contractVersion(),
+ engine: '@soulcraftlabs/brainy',
+ compatibility: {
+ minor:
+ 'additive — a new optional door, a new served operator, a new error class; every existing implementation still conforms',
+ major:
+ 'breaking — a door removed, an answer narrowed, an ordering law changed, an optional door promoted to required, or an operator moved from served to refused'
+ },
+ doors: surfaceOf(Brainy),
+ errors,
+ operators: {
+ accepted,
+ servedOnIndexPath: servedOnIndex,
+ refusedByIndexPath: refusedByIndex,
+ combinators: ['allOf', 'anyOf', 'not']
+ },
+ fieldAddressing: {
+ systemKeyPrefix: 'system.',
+ systemEntityScalars: [...(fieldAddressing.SYSTEM_ENTITY_SCALARS ?? [])].sort(),
+ systemRelationScalars: [...(fieldAddressing.SYSTEM_RELATION_SCALARS ?? [])].sort(),
+ plumbingFields: [...(fieldAddressing.PLUMBING_FIELDS ?? [])].sort()
+ },
+ health: {
+ verdicts: ['pass', 'warn', 'fail'],
+ healKinds: ['none', 'repair', 'rebuild'],
+ servingWithholdingInvariants: [
+ 'index-initialized',
+ 'durable-state-present',
+ 'manifest-residency',
+ 'replay-clean',
+ 'strand-latch'
+ ]
+ }
+}
+
+const rendered = `${JSON.stringify(manifest, null, 2)}\n`
+
+if (process.argv.includes('--check')) {
+ if (!existsSync(OUT)) {
+ console.error(`docs/api-contract.json is missing — run: node scripts/emit-contract-manifest.mjs`)
+ process.exit(1)
+ }
+ if (readFileSync(OUT, 'utf-8') !== rendered) {
+ console.error(
+ `docs/api-contract.json is STALE — the public surface changed. Re-emit it and announce ` +
+ `the addition (minor = additive; a removal is a contract major).`
+ )
+ process.exit(1)
+ }
+ console.log(`docs/api-contract.json is current (${manifest.doors.length} doors, contract ${manifest.contractVersion}).`)
+ process.exit(0)
+}
+
+writeFileSync(OUT, rendered)
+console.log(
+ `Wrote docs/api-contract.json — contract ${manifest.contractVersion}, ` +
+ `${manifest.doors.length} doors, ${manifest.errors.length} error classes, ` +
+ `${manifest.operators.accepted.length} operators ` +
+ `(${manifest.operators.refusedByIndexPath.length} refused by the index path).`
+)
diff --git a/scripts/lib/deterministicStamp.ts b/scripts/lib/deterministicStamp.ts
new file mode 100644
index 00000000..c2a66604
--- /dev/null
+++ b/scripts/lib/deterministicStamp.ts
@@ -0,0 +1,118 @@
+/**
+ * Deterministic generation-stamp resolution for Brainy's build-time code
+ * generators.
+ *
+ * Two builds of the same source tree must produce byte-identical output.
+ * A wall-clock stamp (`new Date()`) breaks that guarantee, so every
+ * generator that writes a "Generated:" header or a `generatedAt` field
+ * into its output must resolve the stamp through this module instead.
+ *
+ * Resolution order:
+ * 1. The newest git commit timestamp among the generator's input files
+ * (the generator script itself always counts as an input).
+ * 2. If git metadata is unavailable (for example, building from a
+ * published npm tarball with no `.git` directory), the stamp already
+ * recorded in the previously generated output file.
+ * 3. If neither is available, the fixed epoch string
+ * `1970-01-01T00:00:00.000Z`.
+ *
+ * Every fallback logs a line to stderr — deterministic degradation is
+ * loud, never a silent divergence.
+ */
+
+import { execFileSync } from 'child_process'
+import * as fs from 'fs'
+
+const EPOCH_STAMP = '1970-01-01T00:00:00.000Z'
+const STAMP_PATTERN = /\*\s*Generated:\s*(\S+)/
+
+/**
+ * Resolve the deterministic stamp for a generator run.
+ *
+ * @param inputPaths Absolute paths to every file whose content determines
+ * the generator's output, including the generator script itself.
+ * @param previousOutputPath Absolute path to the previously generated
+ * file, used for the existing-stamp fallback when git is unavailable.
+ * @returns An ISO-8601 timestamp string that is deterministic for a given
+ * source tree.
+ */
+export function resolveDeterministicStamp(
+ inputPaths: string[],
+ previousOutputPath: string
+): string {
+ const gitStamp = newestGitCommitTimestamp(inputPaths)
+ if (gitStamp) {
+ return gitStamp
+ }
+
+ const existingStamp = readExistingStamp(previousOutputPath)
+ if (existingStamp) {
+ process.stderr.write(
+ `[deterministic-stamp] no git commit history found for generator inputs; ` +
+ `reusing existing stamp from ${previousOutputPath}: ${existingStamp}\n`
+ )
+ return existingStamp
+ }
+
+ process.stderr.write(
+ `[deterministic-stamp] no git commit history and no previous output at ` +
+ `${previousOutputPath}; falling back to fixed epoch stamp ${EPOCH_STAMP}\n`
+ )
+ return EPOCH_STAMP
+}
+
+/**
+ * Find the newest git commit timestamp among the given input paths.
+ * Returns null if git is unavailable, the tree is not a git repository,
+ * or none of the inputs have any commit history yet.
+ */
+function newestGitCommitTimestamp(inputPaths: string[]): string | null {
+ let newest: string | null = null
+
+ for (const inputPath of inputPaths) {
+ if (!fs.existsSync(inputPath)) {
+ continue
+ }
+
+ let out: string
+ try {
+ out = execFileSync(
+ 'git',
+ ['log', '-1', '--format=%cI', '--', inputPath],
+ { stdio: ['ignore', 'pipe', 'ignore'] }
+ )
+ .toString()
+ .trim()
+ } catch {
+ // git missing, not a repository, or no permissions — handled by the
+ // caller's fallback chain.
+ continue
+ }
+
+ if (!out) {
+ // Path exists but has no commit history yet (e.g. newly created,
+ // uncommitted file).
+ continue
+ }
+
+ if (!newest || new Date(out).getTime() > new Date(newest).getTime()) {
+ newest = out
+ }
+ }
+
+ return newest
+}
+
+/**
+ * Parse the `* Generated: ` header out of a previously
+ * generated file, if one exists.
+ */
+function readExistingStamp(outputPath: string): string | null {
+ if (!fs.existsSync(outputPath)) {
+ return null
+ }
+
+ const content = fs.readFileSync(outputPath, 'utf-8')
+ const match = content.match(STAMP_PATTERN)
+ return match ? match[1] : null
+}
diff --git a/scripts/release.sh b/scripts/release.sh
index 5d03e66b..142fa06f 100755
--- a/scripts/release.sh
+++ b/scripts/release.sh
@@ -15,11 +15,11 @@ NC='\033[0m' # No Color
RELEASE_TYPE="${1:-patch}" # patch, minor, or major
SKIP_TESTS=false
DRY_RUN=false
-# --source-only: the HOME leg only — tag, CI's publish to The Source, and the
-# release page; NO storefront (npmjs) publish, NO pair verification, NO docs
-# push. The pair-gate shape: a prerelease the fleet's other engine devDeps
-# from our own registry while the pair is proven, never a public artifact.
-# Refused for a non-prerelease version — a public floor is always a pair.
+# --source-only is now a no-op: The Source is the one registry, so every
+# release already ships Source-only — tag, CI's publish to The Source, the
+# release page, and the docs push, with no separate storefront leg to skip.
+# The flag is still accepted (for backward-compatible invocations) and just
+# prints a notice; it no longer changes behavior.
SOURCE_ONLY=false
for arg in "$@"; do
@@ -109,7 +109,7 @@ else
;;
*)
echo -e "${RED}❌ Invalid release type: ${RELEASE_TYPE}${NC}"
- echo "Usage: ./scripts/release.sh [patch|minor|major|] [--dry-run] [--source-only (prereleases only)]"
+ echo "Usage: ./scripts/release.sh [patch|minor|major|] [--dry-run] [--source-only (no-op; The Source is the one registry)]"
exit 1
;;
esac
@@ -129,11 +129,7 @@ if [ "$PRERELEASE" = true ]; then
echo -e "${YELLOW}⚠️ Prerelease → npm dist-tag '${NPM_TAG}', GitHub prerelease${NC}"
fi
if [ "$SOURCE_ONLY" = true ]; then
- if [ "$PRERELEASE" != true ]; then
- echo -e "${RED}❌ --source-only is for prereleases only: a non-prerelease version is a public floor and always ships as the byte-identical pair.${NC}"
- exit 1
- fi
- echo -e "${YELLOW}⚠️ --source-only → The Source (home) ONLY: no npmjs publish, no pair verification, no docs push${NC}"
+ echo -e "${YELLOW}⚠️ The Source is the one registry; --source-only is implied${NC}"
fi
echo ""
@@ -158,7 +154,8 @@ else
fi
# Create new changelog entry
-CHANGELOG_ENTRY="### [${NEW_VERSION}](https://source.soulcraft.com/soulcraft/brainy/compare/v${CURRENT_VERSION}...v${NEW_VERSION}) ($(date +%Y-%m-%d))
+RELEASE_DATE=$(date +%Y-%m-%d)
+CHANGELOG_ENTRY="### [${NEW_VERSION}](https://source.soulcraft.com/soulcraftlabs/open-brainy/compare/v${CURRENT_VERSION}...v${NEW_VERSION}) (${RELEASE_DATE})
${COMMITS}
"
@@ -178,6 +175,19 @@ if [ -f "CHANGELOG.md" ]; then
fi
echo -e "${GREEN}✅ CHANGELOG updated${NC}\n"
+# Step 6b: Update the releases wall entry — mechanical, derived from the
+# CHANGELOG entry just composed. The fleet's HQ page reads open-brainy.json
+# from the one shared releases repo, soulcraftlabs/releases on The Source —
+# this used to be hand-written after every release (David: never again —
+# make it a step of the rail, landed in the one shared home; this repo no
+# longer hosts its own copy). This step clones/fetches that repo into a
+# local cache, prepends the entry, and pushes it directly — a real
+# cross-repo push, refusing loudly (never skipping) on any
+# clone/validation/commit/push failure.
+echo -e "${BLUE}5️⃣▸ Updating the releases wall...${NC}"
+node scripts/wall-entry.mjs --product open-brainy --version "${NEW_VERSION}" --date "${RELEASE_DATE}" --from-changelog CHANGELOG.md
+echo -e "${GREEN}✅ Releases wall updated${NC}\n"
+
# Step 7: Create release commit
echo -e "${BLUE}6️⃣ Creating release commit...${NC}"
git add package.json package-lock.json CHANGELOG.md
@@ -209,9 +219,9 @@ echo -e "${GREEN}✅ Pushed to origin${NC}\n"
# .forgejo/workflows/publish-source.yml, which builds and publishes on The
# Source's own runner (datacenter-side: seconds, not the laptop's WAN timing
# out on an 87MB tarball PUT). The laptop holds no home-registry publish
-# credential anymore; it only waits for CI's result before trusting the
-# home/npmjs pair enough to publish the storefront leg.
-SOURCE_NPM_REG="https://source.soulcraft.com/api/packages/soulcraft/npm/"
+# credential anymore; it only waits for CI's result before continuing on to
+# the release page and the docs push.
+SOURCE_NPM_REG="https://source.soulcraft.com/api/packages/soulcraftlabs/npm/"
SOURCE_POLL_INTERVAL_S=15
SOURCE_POLL_MAX_ATTEMPTS=200 # 200 × 15s = 50 minutes — the runner is sequential and a busy day's ci.yml
# backlog has twice exceeded the old 20-minute window (8.10.3, 9.0.0);
@@ -219,7 +229,7 @@ SOURCE_POLL_MAX_ATTEMPTS=200 # 200 × 15s = 50 minutes — the runner is sequen
echo -e "${BLUE}9️⃣ Waiting for CI to publish v${NEW_VERSION} to The Source registry (home)...${NC}"
SOURCE_LANDED=false
for ((attempt = 1; attempt <= SOURCE_POLL_MAX_ATTEMPTS; attempt++)); do
- LANDED_VERSION=$(npm view "@soulcraft/brainy@${NEW_VERSION}" version "--@soulcraft:registry=${SOURCE_NPM_REG}" 2>/dev/null || echo "")
+ LANDED_VERSION=$(npm view "@soulcraftlabs/brainy@${NEW_VERSION}" version "--@soulcraftlabs:registry=${SOURCE_NPM_REG}" 2>/dev/null || echo "")
if [ "$LANDED_VERSION" = "$NEW_VERSION" ]; then
SOURCE_LANDED=true
break
@@ -232,62 +242,16 @@ if [ "$SOURCE_LANDED" = true ]; then
echo -e "${GREEN}✅ CI published v${NEW_VERSION} to The Source${NC}\n"
else
echo -e "${RED}❌ CI's home publish did not land — check the workflow run on The Source; the pair must not diverge.${NC}"
- echo -e "${RED} v${NEW_VERSION} was tagged and pushed, but @soulcraft/brainy@${NEW_VERSION} never became visible on the${NC}"
- echo -e "${RED} Source registry after ${SOURCE_POLL_MAX_ATTEMPTS} attempts, ${SOURCE_POLL_INTERVAL_S}s apart. Aborting before npmjs.${NC}"
+ echo -e "${RED} v${NEW_VERSION} was tagged and pushed, but @soulcraftlabs/brainy@${NEW_VERSION} never became visible on the${NC}"
+ echo -e "${RED} Source registry after ${SOURCE_POLL_MAX_ATTEMPTS} attempts, ${SOURCE_POLL_INTERVAL_S}s apart. Aborting.${NC}"
exit 1
fi
-if [ "$SOURCE_ONLY" = true ]; then
- echo -e "${YELLOW}9️⃣½ Storefront (npmjs) leg SKIPPED — --source-only: v${NEW_VERSION} lives on The Source under dist-tag '${NPM_TAG}' only${NC}\n"
-else
- echo -e "${BLUE}9️⃣½ Publishing to npmjs (storefront, dist-tag: ${NPM_TAG})...${NC}"
- # BYTE-IDENTITY LAW: the storefront republishes CI's EXACT artifact — download
- # the tarball The Source serves and publish that file, never a fresh local pack
- # (a local rebuild can differ byte-wise, and the fleet verifies the pair by
- # shasum across registries).
- STOREFRONT_TMP="$(mktemp -d)"
- (cd "$STOREFRONT_TMP" && npm pack "@soulcraft/brainy@${NEW_VERSION}" "--@soulcraft:registry=${SOURCE_NPM_REG}" >/dev/null)
- SOURCE_TARBALL="$(ls "$STOREFRONT_TMP"/soulcraft-brainy-*.tgz)"
- echo -e "${BLUE} home artifact: $(sha256sum "$SOURCE_TARBALL" | cut -d' ' -f1)${NC}"
- npm publish "$SOURCE_TARBALL" --tag "$NPM_TAG" "--@soulcraft:registry=https://registry.npmjs.org/"
- rm -rf "$STOREFRONT_TMP"
- # Brainy is the only PUBLIC @soulcraft package — verify visibility after every publish.
- npm access get status @soulcraft/brainy "--@soulcraft:registry=https://registry.npmjs.org/" || true
- # Verify the pair is byte-identical by registry-reported shasum — divergence
- # here means the storefront leg must be treated as failed, loudly. RETRIED
- # with raw curl: npmjs metadata propagates with a lag measured in minutes,
- # and a one-shot npm-view probe fired a false DIVERGENCE on 10.0.0 while a
- # raw curl of the registry document already confirmed byte-identity. The
- # probe now reads the registry JSON directly (no npm cache in the path) and
- # gives propagation up to 5 minutes before calling the pair divergent.
- NPMJS_VERIFY_ATTEMPTS=20
- NPMJS_VERIFY_INTERVAL_S=15 # 20 × 15s = 5 minutes of propagation grace
- SOURCE_SHA=$(npm view "@soulcraft/brainy@${NEW_VERSION}" dist.shasum "--@soulcraft:registry=${SOURCE_NPM_REG}" 2>/dev/null || echo "source-unavailable")
- PAIR_IDENTICAL=false
- for ((attempt = 1; attempt <= NPMJS_VERIFY_ATTEMPTS; attempt++)); do
- NPMJS_SHA=$(curl -fsSL "https://registry.npmjs.org/@soulcraft%2Fbrainy" 2>/dev/null \
- | node -e "let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{try{const v=JSON.parse(d).versions[process.argv[1]];console.log(v?v.dist.shasum:'')}catch{console.log('')}})" "${NEW_VERSION}" \
- || echo "")
- if [ -n "$NPMJS_SHA" ] && [ "$SOURCE_SHA" = "$NPMJS_SHA" ]; then
- PAIR_IDENTICAL=true
- break
- fi
- echo -e "${YELLOW} … npmjs metadata not settled (attempt ${attempt}/${NPMJS_VERIFY_ATTEMPTS}: '${NPMJS_SHA:-absent}' vs '${SOURCE_SHA}'); retrying in ${NPMJS_VERIFY_INTERVAL_S}s${NC}"
- sleep "$NPMJS_VERIFY_INTERVAL_S"
- done
- if [ "$PAIR_IDENTICAL" = true ]; then
- echo -e "${GREEN}✅ Published to npmjs — byte-identical pair (shasum ${NPMJS_SHA})${NC}\n"
- else
- echo -e "${RED}❌ REGISTRY DIVERGENCE: The Source shasum ${SOURCE_SHA} != npmjs shasum ${NPMJS_SHA} after ${NPMJS_VERIFY_ATTEMPTS} attempts — investigate before announcing${NC}\n"
- exit 1
- fi
-fi
-
# Step 11: Release object on The Source (presentational — the tag, CHANGELOG,
# and RELEASES.md are the record; this just gives The Source's UI a release page).
echo -e "${BLUE}🔟 Creating release page on The Source...${NC}"
if [ -n "${FORGEJO_RELEASE_TOKEN:-}" ]; then
- if curl -sf -X POST "https://source.soulcraft.com/api/v1/repos/soulcraft/brainy/releases" \
+ if curl -sf -X POST "https://source.soulcraft.com/api/v1/repos/soulcraftlabs/open-brainy/releases" \
-H "Authorization: token ${FORGEJO_RELEASE_TOKEN}" -H "Content-Type: application/json" \
-d "{\"tag_name\":\"v${NEW_VERSION}\",\"name\":\"v${NEW_VERSION}\",\"prerelease\":${PRERELEASE}}" >/dev/null; then
echo -e "${GREEN}✅ Release page created on The Source${NC}\n"
@@ -298,29 +262,15 @@ else
echo -e "${RED}⚠️ FORGEJO_RELEASE_TOKEN unset — no release page created; tag + CHANGELOG remain the record${NC}\n"
fi
-# Step 12: Push public docs to the soulcraft.com docs ingest door
-# (VENUE-DOCS-RELEASE-PUSH). Skips with a loud warning when
-# DOCS_INGEST_SECRET is unset; fails loudly (without undoing the publish —
-# that already happened) when a push errors, so the docs site never
-# silently trails npm.
-if [ "$SOURCE_ONLY" = true ]; then
- echo -e "${YELLOW}1️⃣2️⃣ Docs push SKIPPED — --source-only (a home-only prerelease publishes no public docs)${NC}\n"
-else
- echo -e "${BLUE}1️⃣2️⃣ Pushing public docs to soulcraft.com/docs...${NC}"
- if node scripts/push-docs.js; then
- echo -e "${GREEN}✅ Docs push step done${NC}\n"
- else
- echo -e "${RED}❌ Docs push FAILED — soulcraft.com/docs trails npm until re-run or interim sync${NC}\n"
- fi
-fi
+# Step 12 RETIRED (2026-08-31, CORTEX-SITE-BRAINY-RENAME round 12, David-ruled):
+# soulcraft.com/docs carries the paid product's documentation only. This
+# engine's documentation home is THIS repository — README and docs/ — and the
+# site serves 301s for the slugs this rail used to push. The push script stays
+# in the tree for history; the rail no longer calls it.
+echo -e "${BLUE}Docs step: this engine documents itself in its own repo (site push retired 2026-08-31)${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}🎉 Release ${NEW_VERSION} complete!${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
-if [ "$SOURCE_ONLY" = true ]; then
- echo -e "📦 npmjs: ${YELLOW}not published (--source-only)${NC}"
-else
- echo -e "📦 npm: ${BLUE}https://www.npmjs.com/package/@soulcraft/brainy/v/${NEW_VERSION}${NC}"
-fi
-echo -e "🏠 The Source: ${BLUE}https://source.soulcraft.com/soulcraft/brainy/releases/tag/v${NEW_VERSION}${NC}"
+echo -e "🏠 The Source: ${BLUE}https://source.soulcraft.com/soulcraftlabs/open-brainy/releases/tag/v${NEW_VERSION}${NC}"
diff --git a/scripts/wall-entry.mjs b/scripts/wall-entry.mjs
new file mode 100644
index 00000000..d4ec7ba5
--- /dev/null
+++ b/scripts/wall-entry.mjs
@@ -0,0 +1,504 @@
+#!/usr/bin/env node
+/**
+ * @module scripts/wall-entry
+ * @description The releases-wall entry, made mechanical. The fleet's HQ page
+ * reads one public JSON per product from the ONE releases repo on The Source
+ * (soulcraftlabs/releases, files .json at its root — shape
+ * {product, entries:[{version, date, headline, items, url, thumb?}]}), at
+ * https://source.soulcraft.com/soulcraftlabs/releases/raw/branch/main/.json.
+ * Those entries were hand-written after every release, then briefly written
+ * into this repo's own releases/.json; this script is the one door
+ * that composes an entry and lands it in the shared repo, so it is never
+ * hand-written and never forked across repos again.
+ *
+ * Two modes:
+ *
+ * 1. Generate + publish (default):
+ * node wall-entry.mjs --product --version --date \
+ * --from-changelog
+ * Derives an entry from the CHANGELOG.md entry for (headline = the
+ * entry's first bullet, items = every bullet, trimmed of its trailing
+ * commit hash), then:
+ * - clones (or, if a cached clone already exists, fetches and resets)
+ * the releases repo into a local cache directory,
+ * - prepends the entry to /.json, newest first — replacing
+ * any existing entry for the same version so a re-run is idempotent,
+ * - validates the file's shape before and after,
+ * - commits the change as "chore(wall):
" and pushes main.
+ * A failure at any step (clone, validation, commit, push, a
+ * non-fast-forward remote) exits non-zero naming the cure. Nothing is
+ * ever skipped — the wall either lands correctly or the release fails.
+ *
+ * 2. Dry run:
+ * node wall-entry.mjs --dry-run --product --version \
+ * --date --from-changelog
+ * Derives the entry exactly as above and prints it, along with the file
+ * it would be written to, but touches no clone and no remote — usable
+ * from a fresh checkout with no cache and no network.
+ *
+ * 3. Validate only (--check):
+ * node wall-entry.mjs --check --file
+ * Validates an arbitrary wall file's exact key set (top-level and
+ * per-entry), field types, and strict-descending semver ordering with
+ * no duplicates. Read-only; never writes. Exit 0 = clean, exit 1 =
+ * named violations printed to stderr.
+ *
+ * The remote and the local cache directory are each overridable
+ * (--remote / --cache-dir, or WALL_ENTRY_RELEASES_REMOTE /
+ * WALL_ENTRY_RELEASES_CACHE_DIR) so tests can point at a throwaway local
+ * bare repo and a throwaway cache directory — never the real remote or the
+ * real developer cache.
+ *
+ * No dependencies beyond the system `git` binary — CHANGELOG parsing,
+ * semver comparison, and JSON shape checking are all hand-rolled below.
+ */
+
+import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs'
+import { execFileSync } from 'node:child_process'
+import { homedir } from 'node:os'
+import { dirname, join } from 'node:path'
+
+const DEFAULT_REMOTE = 'git@source.soulcraft.com:soulcraftlabs/releases.git'
+
+/** @returns {string} */
+function defaultCacheDir() {
+ const base = process.env.XDG_CACHE_HOME || join(homedir(), '.cache')
+ return join(base, 'soulcraft-releases')
+}
+
+// Required on every entry; "thumb" is optional (may be absent, or present as
+// string | null) — matching the HQ contract's {..., thumb?}.
+const ENTRY_REQUIRED_KEYS = ['version', 'date', 'headline', 'items', 'url']
+const ENTRY_OPTIONAL_KEYS = ['thumb']
+const ENTRY_ALLOWED_KEYS = [...ENTRY_REQUIRED_KEYS, ...ENTRY_OPTIONAL_KEYS]
+const FILE_KEYS = ['product', 'entries']
+
+// The public permalink pattern, by product. Every entry MUST carry an https
+// permalink: HQ's parser rejects a wall whose entries carry url: null (the
+// whole feed became unreadable on 2026-09-02). A product whose forge repo is
+// private links its PUBLIC package page on The Source instead of a release
+// page that would 404 for HQ's readers.
+const RELEASE_URL_PATTERNS = {
+ 'open-brainy': (version) => `https://source.soulcraft.com/soulcraftlabs/open-brainy/releases/tag/v${version}`,
+ 'brainy': (version) => `https://source.soulcraft.com/soulcraft/-/packages/npm/@soulcraft%2Fbrainy/${version}`,
+}
+
+/**
+ * Parse argv into a flag map. `--flag value` sets a string; `--flag` alone
+ * (end of argv, or followed by another `--flag`) sets boolean true.
+ * @param {string[]} argv
+ * @returns {Record}
+ */
+function parseArgs(argv) {
+ /** @type {Record} */
+ const args = {}
+ for (let i = 0; i < argv.length; i++) {
+ const a = argv[i]
+ if (!a.startsWith('--')) continue
+ const key = a.slice(2)
+ const next = argv[i + 1]
+ if (next === undefined || next.startsWith('--')) {
+ args[key] = true
+ } else {
+ args[key] = next
+ i++
+ }
+ }
+ return args
+}
+
+/**
+ * Print a loud, named error and exit 1. Every refusal in this script goes
+ * through here so the failure mode is always the same shape: "wall-entry: ".
+ * @param {string} message
+ * @returns {never}
+ */
+function fail(message) {
+ console.error(`wall-entry: ${message}`)
+ process.exit(1)
+}
+
+/**
+ * @param {string} version
+ * @returns {{major: number, minor: number, patch: number, pre: string | null} | null}
+ */
+function parseSemver(version) {
+ const m = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/.exec(version)
+ if (!m) return null
+ return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]), pre: m[4] ?? null }
+}
+
+/**
+ * @param {string} a
+ * @param {string} b
+ * @returns {number} positive if a > b, negative if a < b, 0 if equal.
+ */
+function compareSemver(a, b) {
+ const pa = parseSemver(a)
+ const pb = parseSemver(b)
+ if (!pa || !pb) throw new Error(`cannot compare non-semver versions "${a}" vs "${b}"`)
+ if (pa.major !== pb.major) return pa.major - pb.major
+ if (pa.minor !== pb.minor) return pa.minor - pb.minor
+ if (pa.patch !== pb.patch) return pa.patch - pb.patch
+ if (pa.pre === pb.pre) return 0
+ if (pa.pre === null) return 1 // a release outranks any prerelease of the same core version
+ if (pb.pre === null) return -1
+ return pa.pre < pb.pre ? -1 : pa.pre > pb.pre ? 1 : 0
+}
+
+/**
+ * Validate a wall file's full shape: top-level keys ("product", "entries" —
+ * no more, no less), per-entry keys and field types ("thumb" optional), and
+ * strict-descending semver ordering with no duplicates. Collects every
+ * violation instead of failing on the first, so a caller reports the whole
+ * picture in one pass.
+ * @param {unknown} data
+ * @returns {string[]} Violation messages; empty means the file is clean.
+ */
+function validateShape(data) {
+ /** @type {string[]} */
+ const errors = []
+
+ if (typeof data !== 'object' || data === null || Array.isArray(data)) {
+ return ['top level: expected a JSON object']
+ }
+ const obj = /** @type {Record} */ (data)
+
+ const topKeys = Object.keys(obj)
+ const missingTop = FILE_KEYS.filter((k) => !(k in obj))
+ const extraTop = topKeys.filter((k) => !FILE_KEYS.includes(k))
+ if (missingTop.length) errors.push(`top level: missing key(s) ${missingTop.join(', ')}`)
+ if (extraTop.length) errors.push(`top level: unexpected key(s) ${extraTop.join(', ')}`)
+
+ if (typeof obj.product !== 'string' || obj.product.trim() === '') {
+ errors.push('top level: "product" must be a non-empty string')
+ }
+ if (!Array.isArray(obj.entries)) {
+ errors.push('top level: "entries" must be an array')
+ return errors // nothing further to check without an array
+ }
+
+ const entries = /** @type {unknown[]} */ (obj.entries)
+ entries.forEach((rawEntry, i) => {
+ const label = `entries[${i}]`
+ if (typeof rawEntry !== 'object' || rawEntry === null || Array.isArray(rawEntry)) {
+ errors.push(`${label}: expected an object`)
+ return
+ }
+ const entry = /** @type {Record} */ (rawEntry)
+ const keys = Object.keys(entry)
+ const missing = ENTRY_REQUIRED_KEYS.filter((k) => !(k in entry))
+ const extra = keys.filter((k) => !ENTRY_ALLOWED_KEYS.includes(k))
+ if (missing.length) errors.push(`${label}: missing key(s) ${missing.join(', ')}`)
+ if (extra.length) errors.push(`${label}: unexpected key(s) ${extra.join(', ')}`)
+
+ if (typeof entry.version !== 'string' || !parseSemver(entry.version)) {
+ errors.push(`${label}: "version" must be a semver string (got ${JSON.stringify(entry.version)})`)
+ }
+ if (typeof entry.date !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(entry.date) || Number.isNaN(Date.parse(entry.date))) {
+ errors.push(`${label}: "date" must be a YYYY-MM-DD string (got ${JSON.stringify(entry.date)})`)
+ }
+ if (typeof entry.headline !== 'string' || entry.headline.trim() === '') {
+ errors.push(`${label}: "headline" must be a non-empty string`)
+ }
+ if (!Array.isArray(entry.items) || entry.items.length === 0 || entry.items.some((it) => typeof it !== 'string' || it.trim() === '')) {
+ errors.push(`${label}: "items" must be a non-empty array of non-empty strings`)
+ }
+ if (typeof entry.url !== 'string' || !/^https:\/\/\S+$/.test(entry.url)) {
+ errors.push(`${label}: "url" must be an https permalink — never null; HQ's parser rejects the whole feed`)
+ }
+ if ('thumb' in entry && !(entry.thumb === null || typeof entry.thumb === 'string')) {
+ errors.push(`${label}: "thumb" must be a string or null when present`)
+ }
+ })
+
+ // Ordering: newest first, strictly descending, no duplicate versions —
+ // checked only over entries whose version parsed (a bad version is
+ // already reported above; comparing it too would just be noise).
+ const versioned = entries
+ .map((e, i) => ({ i, version: /** @type {any} */ (e)?.version }))
+ .filter((e) => typeof e.version === 'string' && parseSemver(e.version))
+ for (let i = 0; i < versioned.length - 1; i++) {
+ const a = versioned[i]
+ const b = versioned[i + 1]
+ const cmp = compareSemver(a.version, b.version)
+ if (cmp === 0) {
+ errors.push(`entries[${a.i}] and entries[${b.i}]: duplicate version ${a.version}`)
+ } else if (cmp < 0) {
+ errors.push(`entries[${a.i}] (${a.version}) sits above entries[${b.i}] (${b.version}) — not newest-first`)
+ }
+ }
+
+ return errors
+}
+
+/**
+ * Extract one version's entry body from a standard-version-style CHANGELOG.md
+ * (headings `### [version](url) (date)`, followed by `- bullet (hash)` lines
+ * until the next heading or EOF).
+ * @param {string} changelog
+ * @param {string} version
+ * @returns {string[]} Bullet lines, trimmed of their leading "- " and
+ * trailing " (hash)".
+ */
+function extractChangelogBullets(changelog, version) {
+ const lines = changelog.split('\n')
+ const headingRe = /^### \[([^\]]+)\]\(.*\)\s*\(\d{4}-\d{2}-\d{2}\)\s*$/
+ let start = -1
+ for (let i = 0; i < lines.length; i++) {
+ const m = headingRe.exec(lines[i])
+ if (m && m[1] === version) {
+ start = i + 1
+ break
+ }
+ }
+ if (start === -1) {
+ fail(
+ `version ${version} has no CHANGELOG entry yet — run this after the CHANGELOG step composes "### [${version}]", not before`,
+ )
+ }
+ /** @type {string[]} */
+ const bullets = []
+ for (let i = start; i < lines.length; i++) {
+ if (headingRe.test(lines[i])) break // next entry starts
+ const bulletMatch = /^- (.+?)(?:\s\(([0-9a-f]{6,40})\))?$/.exec(lines[i].trim())
+ if (lines[i].trim().startsWith('- ') && bulletMatch) {
+ const text = bulletMatch[1].trim()
+ if (text) bullets.push(text)
+ }
+ }
+ if (bullets.length === 0) {
+ fail(`version ${version}'s CHANGELOG entry has no bullets to derive a headline/items from`)
+ }
+ return bullets
+}
+
+/**
+ * Derive a wall entry from a CHANGELOG.md.
+ * @param {{product: string, version: string, date: string, changelogPath: string, url?: string, thumb?: string | null}} opts
+ * @returns {{version: string, date: string, headline: string, items: string[], url: string, thumb: string | null}}
+ */
+function deriveEntry({ product, version, date, changelogPath, url, thumb }) {
+ if (!parseSemver(version)) fail(`--version "${version}" is not a semver string`)
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || Number.isNaN(Date.parse(date))) {
+ fail(`--date "${date}" is not a YYYY-MM-DD date`)
+ }
+ if (!existsSync(changelogPath)) fail(`--from-changelog "${changelogPath}" does not exist`)
+
+ const changelog = readFileSync(changelogPath, 'utf8')
+ const items = extractChangelogBullets(changelog, version)
+ const headline = items[0]
+
+ const pattern = RELEASE_URL_PATTERNS[product]
+ if (url === undefined && pattern === undefined) {
+ throw new Error(`wall-entry: no permalink pattern for product "${product}" — add one to RELEASE_URL_PATTERNS or pass --url; entries never carry url: null`)
+ }
+ const resolvedUrl = url !== undefined ? url : pattern(version)
+ const resolvedThumb = thumb !== undefined ? thumb : null
+
+ return { version, date, headline, items, url: resolvedUrl, thumb: resolvedThumb }
+}
+
+/**
+ * Load and shape-validate a wall file.
+ * @param {string} filePath
+ * @returns {Record}
+ */
+function loadWallFile(filePath) {
+ if (!existsSync(filePath)) fail(`"${filePath}" does not exist`)
+ /** @type {unknown} */
+ let data
+ try {
+ data = JSON.parse(readFileSync(filePath, 'utf8'))
+ } catch (err) {
+ fail(`"${filePath}" is not valid JSON: ${/** @type {Error} */ (err).message}`)
+ }
+ const errors = validateShape(data)
+ if (errors.length) {
+ fail(`"${filePath}" fails shape validation —\n ${errors.join('\n ')}`)
+ }
+ return /** @type {Record} */ (data)
+}
+
+/**
+ * Run a git command, throwing an Error whose message is git's own stderr
+ * (trimmed) on failure — every caller wraps this to name the cure.
+ * @param {string[]} args
+ * @param {string} cwd
+ * @returns {string} stdout, trimmed.
+ */
+function git(args, cwd) {
+ try {
+ return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim()
+ } catch (err) {
+ const stderr = /** @type {any} */ (err).stderr
+ const message = (typeof stderr === 'string' && stderr.trim()) || /** @type {Error} */ (err).message
+ throw new Error(message)
+ }
+}
+
+/**
+ * Ensure a clean, up-to-date local clone of the releases repo at
+ * `cacheDir`, checked out on `main` — cloning fresh if `cacheDir` has no
+ * `.git`, otherwise fetching and hard-resetting onto `origin/main` (so a
+ * stray local commit or edit left by a previous failed run can never leak
+ * into the next one).
+ * @param {string} remote
+ * @param {string} cacheDir
+ */
+function ensureReleasesClone(remote, cacheDir) {
+ if (existsSync(join(cacheDir, '.git'))) {
+ try {
+ git(['remote', 'set-url', 'origin', remote], cacheDir)
+ git(['fetch', '--prune', 'origin'], cacheDir)
+ git(['checkout', 'main'], cacheDir)
+ git(['reset', '--hard', 'origin/main'], cacheDir)
+ git(['clean', '-fd'], cacheDir)
+ } catch (err) {
+ fail(
+ `cannot refresh the cached releases checkout at "${cacheDir}" from "${remote}" — ${/** @type {Error} */ (err).message}\n` +
+ ` cure: delete "${cacheDir}" and re-run so it re-clones from scratch, or confirm SSH access with "ssh -T git@source.soulcraft.com"`,
+ )
+ }
+ return
+ }
+
+ mkdirSync(dirname(cacheDir), { recursive: true })
+ try {
+ git(['clone', remote, cacheDir], dirname(cacheDir))
+ } catch (err) {
+ fail(
+ `cannot clone "${remote}" — ${/** @type {Error} */ (err).message}\n` +
+ ` cure: confirm SSH access with "ssh -T git@source.soulcraft.com" and that the soulcraftlabs/releases repo exists yet`,
+ )
+ }
+ try {
+ git(['checkout', 'main'], cacheDir)
+ } catch (err) {
+ fail(
+ `cloned "${remote}" into "${cacheDir}" but could not check out "main" — ${/** @type {Error} */ (err).message}\n` +
+ ` cure: confirm the releases repo's default branch is named "main"`,
+ )
+ }
+}
+
+/**
+ * Prepend `entry` to the wall at `/.json`, replacing any
+ * existing entry for the same version (idempotent re-runs), validating
+ * before and after, committing, and pushing — or refusing loudly, naming
+ * the cure, at whichever step fails.
+ * @param {{version: string, date: string, headline: string, items: string[], url: string, thumb: string | null}} entry
+ * @param {string} product
+ * @param {string} remote
+ * @param {string} cacheDir
+ */
+function publishEntry(entry, product, remote, cacheDir) {
+ ensureReleasesClone(remote, cacheDir)
+
+ const filePath = join(cacheDir, `${product}.json`)
+ if (!existsSync(filePath)) {
+ fail(
+ `"${filePath}" does not exist in the releases repo — cure: seed "${product}.json" at the repo root first (it must exist before any release rail can prepend to it)`,
+ )
+ }
+ const wall = loadWallFile(filePath)
+
+ if (wall.product !== product) {
+ fail(`"${filePath}" has product "${wall.product}", but --product "${product}" was given — refusing a cross-product write`)
+ }
+
+ const replacing = wall.entries.some((e) => e.version === entry.version)
+ wall.entries = [entry, ...wall.entries.filter((e) => e.version !== entry.version)]
+
+ const postErrors = validateShape(wall)
+ if (postErrors.length) {
+ fail(`the entry for ${entry.version} would leave "${filePath}" invalid —\n ${postErrors.join('\n ')}`)
+ }
+
+ writeFileSync(filePath, JSON.stringify(wall, null, 2) + '\n', 'utf8')
+
+ const status = git(['status', '--porcelain', '--', `${product}.json`], cacheDir)
+ if (status === '') {
+ console.log(`wall-entry: "${product}.json" already carries an identical entry for ${entry.version} — nothing to commit or push`)
+ return
+ }
+
+ try {
+ git(['add', `${product}.json`], cacheDir)
+ git(['commit', '-m', `chore(wall): ${product} ${entry.version}`], cacheDir)
+ } catch (err) {
+ fail(`cannot commit the wall entry in "${cacheDir}" — ${/** @type {Error} */ (err).message}\n cure: inspect "${cacheDir}" by hand and re-run once its git state is clean`)
+ }
+
+ try {
+ git(['push', 'origin', 'main'], cacheDir)
+ } catch (err) {
+ fail(
+ `push to "${remote}" failed (likely a non-fast-forward — another release landed on main first) — ${/** @type {Error} */ (err).message}\n` +
+ ` cure: re-run this release step; it re-fetches and resets onto the latest origin/main before retrying`,
+ )
+ }
+
+ const sha = git(['rev-parse', 'HEAD'], cacheDir)
+ console.log(
+ `wall-entry: ${replacing ? 'replaced' : 'wrote'} v${entry.version} in "${product}.json" (${wall.entries.length} entries, newest first) — pushed ${sha} to ${remote} main`,
+ )
+}
+
+function main() {
+ const args = parseArgs(process.argv.slice(2))
+
+ if (args.check) {
+ const filePath = /** @type {string | undefined} */ (args.file)
+ if (!filePath) fail('--check needs --file ')
+ const wall = loadWallFile(/** @type {string} */ (filePath))
+ console.log(`wall-entry --check: "${filePath}" OK — product "${wall.product}", ${wall.entries.length} entries, newest-first, no duplicates`)
+ process.exit(0)
+ }
+
+ // Generate mode (default, also covers --dry-run): --product, --version,
+ // --date, --from-changelog required.
+ const product = /** @type {string | undefined} */ (args.product)
+ const version = /** @type {string | undefined} */ (args.version)
+ const date = /** @type {string | undefined} */ (args.date)
+ const fromChangelog = /** @type {string | undefined} */ (args['from-changelog'])
+
+ const missing = []
+ if (!product) missing.push('--product')
+ if (!version) missing.push('--version')
+ if (!date) missing.push('--date')
+ if (!fromChangelog) missing.push('--from-changelog')
+ if (missing.length) {
+ fail(
+ `missing required flag(s): ${missing.join(', ')}\n` +
+ 'Usage:\n' +
+ ' wall-entry.mjs --product --version --date --from-changelog [--dry-run]\n' +
+ ' wall-entry.mjs --check --file ',
+ )
+ }
+
+ const urlArg = args.url === true ? undefined : /** @type {string | undefined} */ (args.url)
+ const thumbArg = args.thumb === true ? undefined : /** @type {string | undefined} */ (args.thumb)
+
+ const entry = deriveEntry({
+ product: /** @type {string} */ (product),
+ version: /** @type {string} */ (version),
+ date: /** @type {string} */ (date),
+ changelogPath: /** @type {string} */ (fromChangelog),
+ url: urlArg,
+ thumb: thumbArg,
+ })
+
+ const remote = /** @type {string} */ (args.remote ?? process.env.WALL_ENTRY_RELEASES_REMOTE ?? DEFAULT_REMOTE)
+ const cacheDir = /** @type {string} */ (args['cache-dir'] ?? process.env.WALL_ENTRY_RELEASES_CACHE_DIR ?? defaultCacheDir())
+
+ if (args['dry-run']) {
+ console.log(`wall-entry --dry-run: would write to "${join(cacheDir, `${product}.json`)}" in ${remote} (main), pushed as "chore(wall): ${product} ${version}"`)
+ console.log(JSON.stringify(entry, null, 2))
+ process.exit(0)
+ }
+
+ publishEntry(entry, /** @type {string} */ (product), remote, cacheDir)
+}
+
+main()
diff --git a/src/brainy.ts b/src/brainy.ts
index db332e18..da04577e 100644
--- a/src/brainy.ts
+++ b/src/brainy.ts
@@ -25,6 +25,7 @@ import {
} from './storage/brainFormat.js'
import type { BrainFormat } from './storage/brainFormat.js'
import { StorageAdapter, Vector, DistanceFunction, EmbeddingFunction, GraphVerb, STANDARD_ENTITY_FIELDS } from './coreTypes.js'
+import { isZeroNormVector } from './utils/distance.js'
import type { HNSWNoun, HNSWNounWithMetadata, HNSWVerbWithMetadata, EntityVisibility } from './coreTypes.js'
import {
defaultEmbeddingFunction,
@@ -197,7 +198,12 @@ import {
import { isDeterministicEmbedMode } from './embeddings/deterministicEmbedMode.js'
import { GenerationConflictError, StoreInconsistentError } from './db/errors.js'
import { BrainyError, GraphIndexNotReadyError, MetadataIndexNotReadyError, MigrationInProgressError, VectorIndexNotReadyError } from './errors/brainyError.js'
-import { assessIndexReadiness } from './utils/indexReadiness.js'
+import {
+ assessIndexReadiness,
+ assessProviderHealth,
+ assessProviderRebuild,
+ describeRebuildProgress
+} from './utils/indexReadiness.js'
import { reconstructNounWrapper } from './db/factLog.js'
import { asBrainyFieldRefusal } from './db/fieldAddressing.js'
import {
@@ -395,6 +401,15 @@ interface PlannedTransact {
* marker outlives its write.
*/
markerRecords: FactMarkerRecord[]
+ /**
+ * Ids the batch's `{ op: 'update' }` unvector door (`vector: []`) needs to
+ * decrement on the vectored-noun ledger — consumed by `transact()` with a
+ * proper `await this.storage.noteVectorUnlanded?.(id)` per id, AFTER
+ * `commitTransaction` resolves (never for a rejected batch). Kept separate
+ * from `postCommit` (`Array<() => void>`, called synchronously, fire-and-
+ * forget) because the ledger hook is async and must be awaited.
+ */
+ vectorUnlands: string[]
}
/**
@@ -634,8 +649,6 @@ export class Brainy implements BrainyInterface {
/** One-shot guard so the degraded-reads warning fires once per degraded window
* (reset when the degraded state clears). See {@link warnIfReadsDegraded}. */
private _degradedReadWarned = false
- /** One-shot guard so the metadata cold-open consistency probe runs once per brain. */
- private _metadataConsistencyProbed = false
/** Graph-adjacency cold-load consistency: verified-live this session (one-shot). */
private _graphAdjacencyVerified = false
/** Re-entrancy guard: a verify (rebuild → reads) is in flight. */
@@ -732,6 +745,24 @@ export class Brainy implements BrainyInterface {
// Write acks NEVER await it; a failed background flush is LOUD and re-armed.
private _persistDirtyWrites = 0
private _persistLastFlushAt = Date.now()
+ /**
+ * Whether a write has been committed since the last flush that ran. THE
+ * ENGINE DOES NO PERIODIC WORK WITHOUT A CAUSE: a brain nobody has written
+ * to has nothing to make durable, and a flush over it must cost nothing and
+ * say nothing. Before this, a flush called every provider, stamped the
+ * watermarks, persisted the generation counter and re-stamped the entity
+ * tree whether or not anything had changed — roughly 28 writes for a store
+ * that had not moved.
+ *
+ * WHAT THIS DOES NOT EXPLAIN, stated so nobody reads it as solved: a
+ * production process holding 21 brains printed "All indexes flushed to disk
+ * in 216-601ms" per brain every ~35s and idled at 1.26 cores with no writes
+ * for ten minutes. This engine's cadence is WRITE-DRIVEN — every trigger
+ * runs through noteWriteForPersistence, which only a committed write calls —
+ * so something was calling flush() on those brains, and this gate makes such
+ * a call free rather than accounting for it. The caller is still unidentified.
+ */
+ private _dirtySinceLastFlush = false
private _persistIdleTimer: ReturnType | null = null
private _persistBackgroundFlight: Promise | null = null
@@ -744,6 +775,15 @@ export class Brainy implements BrainyInterface {
private _pendingEmbedIds = new Set()
private _embedWorkerFlight: Promise | null = null
+ // OPEN-PATH FIX: the background embedding-engine warm kicked off (never
+ // awaited) by `performInit()` when `eagerEmbeddings` resolves true. Stored
+ // for observability only — `embed()`/`embeddingManager.embed()` already
+ // await the engine's OWN singleton init promise internally, so nothing
+ // needs to explicitly await this field for correctness. Never rejects on
+ // its own: a `.catch` narrates the failure and swallows it so a failed
+ // warm never surfaces as an unhandled rejection.
+ private _embeddingWarmPromise: Promise | null = null
+
/** The stored log-authority switch, read once at open (default: tree). */
private _logAuthority: LogAuthorityRecord = { authority: 'tree' }
// A failed walk latches its error: retries within the cooldown rethrow it
@@ -804,11 +844,31 @@ export class Brainy implements BrainyInterface {
// applies only to instances that were never closed.
private closed = false
- // Lazy rebuild state (Production-scale lazy loading)
- // Prevents race conditions when multiple queries trigger rebuild simultaneously
- private lazyRebuildInProgress = false
+ // Index-build-at-open state. `lazyRebuildCompleted` predates the health-gate
+ // law (it named a first-QUERY lazy rebuild) and stays for `getIndexStatus()`
+ // API compatibility, but its truth changed: a needed rebuild now runs
+ // unconditionally at open() (see `rebuildIndexesIfNeeded`), never deferred to
+ // a read, so this simply flips true once that open-time step has run.
+ // `lazyRebuildInProgress` / `lazyRebuildPromise` (the first-query rebuild's
+ // concurrency guard) are retired with the lazy-build path they served —
+ // `ensureIndexesLoaded()` is a read-time CHECK now, never a build.
private lazyRebuildCompleted = false
- private lazyRebuildPromise: Promise | null = null
+
+ // Read-gate narration dedup: a degraded-but-serving or not-ready health
+ // report narrates via prodLog.warn ONCE per (provider, report.generation) —
+ // never once per read. Keyed on the provider instance itself.
+ /**
+ * The last health narration emitted per provider, keyed by its CONTENT.
+ *
+ * This used to dedupe on the provider's `generation` counter, which bumps on
+ * every ledger mutation and every rebuild boundary — so a provider that
+ * bumps its generation on routine work re-emitted the same unchanged health
+ * line on every read that consulted it, and a provider that never bumped
+ * could suppress a line whose reasons had genuinely changed. The dedupe key
+ * is now what the line SAYS: an unchanged verdict is silent however the
+ * generation moves, and a changed verdict is always heard.
+ */
+ private _lastNarratedHealth = new Map()
constructor(config?: BrainyConfig) {
// The reserved-field write policy died with the field-addressing law:
@@ -926,14 +986,14 @@ export class Brainy implements BrainyInterface {
* extends FileSystemStorage`) inherit new methods Brainy adds to
* `FileSystemStorage` / `BaseStorage` automatically — `typeof` walks the
* prototype chain, so there's no in-package version skew to worry about as
- * long as the plugin's own dist resolves `@soulcraft/brainy` dynamically
+ * long as the plugin's own dist resolves `@soulcraftlabs/brainy` dynamically
* (which Cortex 2.2.x onward does — see
* `node_modules/@soulcraft/cor/dist/storage/mmapFileSystemStorage.js`).
*
* This helper exists for the **build/install** failure modes the import
* resolution can't catch:
* - Stale `node_modules` left over from a prior `bun install` against
- * `@soulcraft/brainy ≤7.20.x`.
+ * `@soulcraftlabs/brainy ≤7.20.x`.
* - Lockfile drift pinning brainy below the version that introduced the
* method.
* - Docker layer caches that reuse a `node_modules` from an earlier image.
@@ -1072,6 +1132,86 @@ export class Brainy implements BrainyInterface {
configureLogger({ level: LogLevel.DEBUG }) // Enable verbose logging
}
+ // OPEN-PATH NARRATION: phase timing across the five named stretches of
+ // init — storage init / generation-store open+fold / index init+gate /
+ // VFS bootstrap / embedding-warm-started. Each `markPhase()` call records
+ // elapsed ms SINCE THE PREVIOUS checkpoint, so the buckets always sum to
+ // the pre-integration/warmOnOpen total.
+ //
+ // THE LAW THIS ENFORCES: an open is never silent for more than
+ // OPEN_HEARTBEAT_MS. A production service opening a 16 GB store logged
+ // NOTHING for three minutes and then began work — the operator could not
+ // tell a slow open from a hung one, and restarted into the same wall.
+ // Two mechanisms, both on the always-visible narration channel (the old
+ // breakdown used `prodLog.warn`, which production clamps away — that is
+ // why the three minutes were silent):
+ // - a heartbeat that names the phase currently running and its elapsed
+ // wall, every OPEN_HEARTBEAT_MS, for as long as the open lasts;
+ // - one line per phase AS IT ENDS, naming its wall and its cause, for
+ // any phase over OPEN_PHASE_NARRATE_MS.
+ // The heartbeat is unref'd and cleared in the `finally` below, so it can
+ // neither hold the process open nor outlive a failed init. It cannot fire
+ // inside a phase that blocks the event loop synchronously; such a phase
+ // must narrate its own progress (the generation-log fold does).
+ const OPEN_HEARTBEAT_MS = 5_000
+ const OPEN_PHASE_NARRATE_MS = 2_000
+ /** Phase order + what each one is paying for, quoted in its narration. */
+ const OPEN_PHASES: ReadonlyArray<{ name: string; cause: string }> = [
+ { name: 'storage-init', cause: 'opening the store and loading its count ledger' },
+ {
+ name: 'generation-store-open-fold',
+ cause: 'opening the generation store: crash-recovery replay/fold, derived-family registration, format handshake'
+ },
+ { name: 'index-init-gate', cause: 'constructing the derived indexes and gating them for serving' },
+ { name: 'vfs-bootstrap', cause: 'bootstrapping the virtual filesystem' },
+ { name: 'embedding-warm-started', cause: 'starting the background embedding warm' }
+ ]
+ const initStart = Date.now()
+ let lastPhaseCheckpoint = initStart
+ let currentPhaseIndex = 0
+ const phaseTimingsMs: Record = {}
+ const openHeartbeat: ReturnType = setInterval(() => {
+ const phase = OPEN_PHASES[currentPhaseIndex]
+ if (!phase) return
+ prodLog.narrate(
+ `[Brainy] open: still in phase ${currentPhaseIndex + 1}/${OPEN_PHASES.length} ` +
+ `"${phase.name}" after ${Math.round((Date.now() - lastPhaseCheckpoint) / 1000)}s ` +
+ `(${Math.round((Date.now() - initStart) / 1000)}s into the open) — ${phase.cause}`
+ )
+ }, OPEN_HEARTBEAT_MS)
+ if (typeof openHeartbeat.unref === 'function') openHeartbeat.unref()
+ /**
+ * Narrate one STEP inside a phase when it turns out to be expensive.
+ * A phase that costs a minute and names only itself tells an operator
+ * where to look but not what to look at; this names the step. Silent
+ * under OPEN_PHASE_NARRATE_MS, so a fast open says nothing extra.
+ */
+ const step = async (name: string, cause: string, run: () => Promise): Promise => {
+ const startedAt = Date.now()
+ try {
+ return await run()
+ } finally {
+ const elapsed = Date.now() - startedAt
+ if (elapsed >= OPEN_PHASE_NARRATE_MS) {
+ prodLog.narrate(`[Brainy] open: step "${name}" took ${elapsed}ms — ${cause}`)
+ }
+ }
+ }
+ const markPhase = (name: string): void => {
+ const now = Date.now()
+ const elapsed = now - lastPhaseCheckpoint
+ phaseTimingsMs[name] = elapsed
+ lastPhaseCheckpoint = now
+ const finished = OPEN_PHASES[currentPhaseIndex]
+ if (elapsed >= OPEN_PHASE_NARRATE_MS && finished && finished.name === name) {
+ prodLog.narrate(
+ `[Brainy] open: phase ${currentPhaseIndex + 1}/${OPEN_PHASES.length} ` +
+ `"${name}" finished in ${elapsed}ms — ${finished.cause}`
+ )
+ }
+ currentPhaseIndex++
+ }
+
try {
// Auto-detect and activate plugins BEFORE storage setup
// so plugin-provided storage factories (e.g., filesystem override from cor) are available
@@ -1132,7 +1272,7 @@ export class Brainy implements BrainyInterface {
`and the flush-request RPC are disabled for this directory. ` +
`Likely fix: clean install (\`rm -rf node_modules bun.lockb && ` +
`bun install\`) or rebuild your container image to refresh ` +
- `\`@soulcraft/brainy\` to ≥7.21. See docs/concepts/storage-adapters.md.`
+ `\`@soulcraftlabs/brainy\` to ≥7.21. See docs/concepts/storage-adapters.md.`
)
} else {
console.warn(
@@ -1143,6 +1283,12 @@ export class Brainy implements BrainyInterface {
}
}
+ // PHASE 1 of 5 — "storage init": plugin/legacy-layout bootstrap,
+ // storage adapter construction+init, the OS-limit check, and the
+ // writer-lock claim, all folded into one bucket (everything above this
+ // line since performInit started).
+ markPhase('storage-init')
+
// 8.0 generational MVCC: open the record layer BEFORE any index is
// created or loaded. Crash recovery may rewrite canonical entity files
// (restoring before-images of an uncommitted transaction), and every
@@ -1151,9 +1297,12 @@ export class Brainy implements BrainyInterface {
// instances skip recovery (readers never write; the next writer
// repairs).
this.generationStore = new GenerationStore(this.storage)
- const generationOpenResult = await this.generationStore.open({
- readOnly: this.config.mode === 'reader'
- })
+ const generationOpenResult = await step(
+ 'generation-store.open',
+ 'reading the generation manifest and committed ranges, opening the fact log and the ' +
+ 'packed segment tier, and folding any crash-recovery replay',
+ () => this.generationStore.open({ readOnly: this.config.mode === 'reader' })
+ )
// The generation fact log is CANONICAL state, not a derived index — no
// sweeper, GC, or blob-lifecycle path may ever delete under it. Declare
@@ -1191,7 +1340,11 @@ export class Brainy implements BrainyInterface {
// rollup invariants against the log head + live counters. Loud on
// genuine incoherence (repairIndex heals), silent on absent/coherent,
// benign-behind refreshes at the next flush. Never blocks open.
- await this.verifyEntityTreeStamp()
+ await step(
+ 'verify-entity-tree-stamp',
+ 'comparing the entity tree\'s stamped generation and rollups against the store',
+ () => this.verifyEntityTreeStamp()
+ )
// 8.0 ⇄ native-provider version handshake: load the on-disk brain-format
// marker (`_system/brain-format.json`) into an in-memory field NOW —
@@ -1203,7 +1356,11 @@ export class Brainy implements BrainyInterface {
// them from the canonical records and then re-stamps the marker AFTER the
// rebuild verifies (non-destructive: a crash mid-rebuild leaves the old /
// absent marker, so the next open idempotently re-rebuilds).
- this._brainFormat = await readBrainFormat(this.storage)
+ this._brainFormat = await step(
+ 'read-brain-format',
+ 'reading the on-disk format marker that decides whether the derived indexes are stale',
+ () => readBrainFormat(this.storage)
+ )
this._indexEpochStale =
this._brainFormat === null || this._brainFormat.indexEpoch !== EXPECTED_INDEX_EPOCH
@@ -1214,9 +1371,19 @@ export class Brainy implements BrainyInterface {
// upgrade verifies + stamps; retained on failure. No-op for a reader, for
// non-filesystem storage, or for a brain with no persisted data.
if (this._indexEpochStale && this.config.migrationBackup && !this.isReadOnly) {
- await this.createMigrationBackupIfNeeded()
+ await step(
+ 'pre-upgrade-backup',
+ 'snapshotting the brain directory before a one-time format rebuild (migrationBackup)',
+ () => this.createMigrationBackupIfNeeded()
+ )
}
+ // PHASE 2 of 5 — "generation-store open+fold": GenerationStore
+ // construction+open (crash-recovery replay/rollback fold), the
+ // derived-family registration, the fact-scan seam, the entity-tree
+ // stamp check, the brain-format handshake, and the pre-upgrade backup.
+ markPhase('generation-store-open-fold')
+
// Provider: embeddings (reassign embedder if plugin provides one)
const embeddingProvider = this.pluginRegistry.getProvider('embeddings')
if (embeddingProvider) {
@@ -1371,13 +1538,44 @@ export class Brainy implements BrainyInterface {
`[Brainy] Rebuilding indexes after crash recovery rolled back ` +
`${generationOpenResult.rolledBackGenerations} uncommitted transaction(s)`
)
+ // SELF-REBUILD DEFERENCE, same law as the open gate: a provider that
+ // is already rebuilding itself from canonical is doing exactly this
+ // work. Kicking a second rebuild on top of it is redundant at best.
+ // Safe by ordering: the crash-recovery fold ran in the generation
+ // store's open, BEFORE any provider was constructed, so a provider
+ // rebuilding now is reading the repaired canonical records.
+ const kick = async (leg: string, provider: { rebuild: () => Promise }) => {
+ const rebuilding = assessProviderRebuild(provider)
+ if (rebuilding) {
+ prodLog.narrate(
+ `[Brainy] crash-recovery rebuild: the ${leg} provider is already ` +
+ `${describeRebuildProgress(rebuilding)} from canonical — not kicking a second one.`
+ )
+ return
+ }
+ await provider.rebuild()
+ }
await Promise.all([
- this.metadataIndex.rebuild(),
- this.index.rebuild(),
- this.graphIndex.rebuild()
+ kick('metadata', this.metadataIndex),
+ kick('vector', this.index as unknown as { rebuild: () => Promise }),
+ kick('graph', this.graphIndex)
])
}
+ // METADATA WATERMARK CATCHUP: the JS metadata index computed its
+ // three-way watermark verdict inside metadataIndex.init() above,
+ // against the generation store's now-FINAL committed generation (the
+ // crash-recovery fold above — the durable-at-ack replay of acked
+ // writes whose canonical bytes hadn't reached disk — has already run,
+ // and any rolled-back-transaction rebuild just above already brought
+ // every index current, so the verdict is consumed here whether or not
+ // that rebuild ran). Consumed BEFORE the rebuild gate below and BEFORE
+ // this open serves any read — the cure for the class of bug where
+ // canonical get()/counts recover a crash-window write but find()
+ // keeps serving the metadata index's pre-crash state (the index
+ // flushes only periodically, not per-commit).
+ await this.consumeMetadataWatermarkVerdict(generationOpenResult.rolledBackGenerations > 0)
+
// 8.0 versioned-provider replay-gap check: a provider whose persisted
// index generation is behind the storage layer's committed generation
// replays the gap itself (post-commit applier contract) — surface the
@@ -1444,12 +1642,38 @@ export class Brainy implements BrainyInterface {
}).backfillBlobHistoryRefCountsIfNeeded()
}
- // Rebuild indexes if needed for existing data
- await this.rebuildIndexesIfNeeded()
+ // LEG C (zero-norm/unvector-door law): migrate a legacy zero-norm VFS
+ // root BEFORE the vector-leg open gate below ever compares the
+ // canonical vectored-noun count against the vector index's size — see
+ // migrateLegacyZeroNormVfsRootIfNeeded's JSDoc for why this is a safe
+ // O(1) exception to "nothing at open may scale with brain size", and
+ // why it must run here rather than waiting on VirtualFileSystem's own
+ // (VFS-instance-gated) lazy migration.
+ await this.migrateLegacyZeroNormVfsRootIfNeeded()
+
+ // Rebuild indexes if needed for existing data. Runs to completion before
+ // init() returns — there is no more first-query lazy path, so the flag
+ // below (kept for getIndexStatus() API compatibility) simply flips true
+ // once this open-time step has run.
+ await step(
+ 'rebuild-indexes-if-needed',
+ 'the derived-index gate: each family\'s readiness verdict, and any build it asks for',
+ () => this.rebuildIndexesIfNeeded()
+ )
+ this.lazyRebuildCompleted = true
// Check for pending data migrations
await this.checkMigrations()
+ // PHASE 3 of 5 — "index init+gate": provider wiring (embeddings,
+ // cache, roaring, msgpack, sort:topK, distance), HNSW/metadata/graph
+ // index construction, the eager cold-load, id-resolver + connections-
+ // codec wiring, crash-recovery index rebuild, the replay-gap check,
+ // legacy VFS blob adoption, blob-history backfill, the legacy
+ // zero-norm VFS root migration, and the rebuildIndexesIfNeeded() gate
+ // + migration check.
+ markPhase('index-init-gate')
+
// Register shutdown hooks for graceful count flushing (once globally)
if (!Brainy.shutdownHooksRegisteredGlobally) {
this.registerShutdownHooks()
@@ -1508,7 +1732,11 @@ export class Brainy implements BrainyInterface {
// Initialize VFS: Ensure VFS is ready when accessed as property
// This eliminates need for separate vfs.init() calls - zero additional complexity
this._vfs = new VirtualFileSystem(this)
- await this._vfs.init()
+ await step(
+ 'vfs.init',
+ 'creating or adopting the VFS root and wiring the path resolver',
+ () => this._vfs!.init()
+ )
this._vfsInitialized = true // Mark VFS as fully initialized
// 8.0 MVCC: infrastructure bootstrap (VFS root, etc.) is now the
@@ -1532,7 +1760,11 @@ export class Brainy implements BrainyInterface {
const storedArtifact = await this.storage
.readRawObject(LOG_AUTHORITY_PATH)
.catch(() => null)
- const authority = await readLogAuthority(this.storage)
+ const authority = await step(
+ 'read-log-authority',
+ 'reading the stored storage-authority artifact',
+ () => readLogAuthority(this.storage)
+ )
this._logAuthority = authority
if (authority.authority === 'log') {
this.generationStore.setLogDurability('at-ack')
@@ -1543,7 +1775,12 @@ export class Brainy implements BrainyInterface {
this.generationStore.getFactLog() !== null
) {
try {
- await this.adoptLogAuthority()
+ await step(
+ 'adopt-log-authority',
+ 'the adoption oracle: verifying the log against canonical before flipping this ' +
+ 'brain to durable-at-ack, and backfilling any curable divergence',
+ () => this.adoptLogAuthority()
+ )
prodLog.info(
'[Brainy] storage authority adopted at open: generation log ' +
'(fleet default; oracle green; durable-at-ack enabled)'
@@ -1583,8 +1820,16 @@ export class Brainy implements BrainyInterface {
// this is where it lands.
if (!this.isReadOnly) {
try {
- await this.bridgeLegacyPendingEmbedSidecars()
- await this.recoverPendingEmbedsFromLog()
+ await step(
+ 'bridge-pending-embed-sidecars',
+ 'migrating any pre-log deferred-embed marker files into the generation log',
+ () => this.bridgeLegacyPendingEmbedSidecars()
+ )
+ await step(
+ 'recover-pending-embeds',
+ 'folding the generation log\'s deferred-embed markers back into the pending set',
+ () => this.recoverPendingEmbedsFromLog()
+ )
if (this._pendingEmbedIds.size > 0) {
prodLog.info(
`[Brainy] ${this._pendingEmbedIds.size} deferred embed(s) pending from a previous ` +
@@ -1601,15 +1846,40 @@ export class Brainy implements BrainyInterface {
}
}
- // Eager embedding initialization.
+ // PHASE 4 of 5 — "VFS bootstrap": shutdown-hook registration, blob
+ // storage init, the provider-summary log, flipping `initialized`,
+ // the migration-lock wait, VFS construction+init, flipping generation
+ // stamping active, the log-authority adopt/oracle check, and
+ // pending-embed crash recovery.
+ markPhase('vfs-bootstrap')
+
+ // Eager embedding initialization — BACKGROUND WARM (open-path fix).
//
- // Adaptive default (8.0): the WASM embedding engine eagerly initializes
+ // Adaptive default (8.0): the WASM embedding engine eagerly WARMS
// during init() WHENEVER it is the active embedder — i.e. no native
// 'embeddings' provider has taken over — and the instance is a writer
// (not reader-mode) outside of unit tests. The WASM module (≈93MB with
- // the embedded model) takes 90-140s to compile on throttled CPUs; paying
- // that during boot rather than on the first embed()-driven call is the
- // right default for the overwhelmingly common single-process server.
+ // the embedded model) takes 90-140s to compile on throttled CPUs.
+ //
+ // Historically this AWAITED `embeddingManager.init()` INLINE, so every
+ // writer's open() blocked on the compile — N concurrent opens all
+ // queued on the ONE process-global singleton (an ~80x contention
+ // multiplier measured in a production restart storm: 90,017ms busy vs
+ // 1,117ms quiet). The engine only needs to be ready before the FIRST
+ // REAL embed() call, not before init() returns, so this now only
+ // STARTS the warm and moves on — init() never waits for it.
+ //
+ // No double-await needed for correctness: `this.embed()` (~line 15420)
+ // delegates to `this.embedder`, which for the default engine is
+ // `embeddingManager.getEmbeddingFunction()` → `embeddingManager.embed()`
+ // (src/embeddings/EmbeddingManager.ts). That method calls `await
+ // this.init()` FIRST, and `init()` itself serializes every concurrent
+ // caller onto ONE shared `globalInitPromise` — so the first real
+ // embed() automatically waits for whichever finishes first: this
+ // background warm (if still running) or a fresh init() (if the warm
+ // hasn't reached this code yet, e.g. `eagerEmbeddings: false`).
+ // Verified by reading both call sites; `_embeddingWarmPromise` below
+ // is stored for observability only, never re-awaited by embed().
//
// Skipped automatically when:
// - a native 'embeddings' provider is registered (it owns embeddings;
@@ -1617,8 +1887,8 @@ export class Brainy implements BrainyInterface {
// - reader-mode (readers don't embed — they query existing vectors),
// - unit-test mode (tests must stay fast and use the mock embedder).
//
- // `eagerEmbeddings: false` is the explicit override to force lazy init
- // (first-embed) even when this instance is the active embedder.
+ // `eagerEmbeddings: false` keeps meaning "no warm at all" — fully lazy,
+ // the first embed() call pays the full cost inline, same as before.
const isUnitTestMode = isDeterministicEmbedMode()
const eager = this.config.eagerEmbeddings ?? true
if (
@@ -1627,9 +1897,45 @@ export class Brainy implements BrainyInterface {
this.config.mode !== 'reader' &&
!isUnitTestMode
) {
- console.log('Eager embedding initialization enabled...')
- await embeddingManager.init()
- console.log('Embedding engine ready')
+ const warmStart = Date.now()
+ console.log('Background embedding-engine warm started (init() does not wait for it)...')
+ this._embeddingWarmPromise = embeddingManager
+ .init()
+ .then(() => {
+ prodLog.info(
+ `[Brainy] background embedding-engine warm complete in ${Date.now() - warmStart}ms`
+ )
+ })
+ .catch((err) => {
+ // Loud, never silent: a warm that fails to compile must be
+ // heard NOW, not discovered as a mystery latency spike on
+ // whichever request happens to trigger the first real embed().
+ // That first embed() call still retries init() itself (the
+ // singleton promise contract above) and surfaces its own typed
+ // error to its caller — this is the immediate, background echo.
+ prodLog.warn(
+ `[Brainy] background embedding-engine warm FAILED: ` +
+ `${(err as Error).message} — the first embed() call will retry ` +
+ `initialization and surface the error there`
+ )
+ })
+ }
+
+ // PHASE 5 of 5 — "embedding-warm-started": just the synchronous cost
+ // of kicking off the background warm above (the warm's own compile
+ // time is NOT included — that's the whole point of backgrounding it).
+ markPhase('embedding-warm-started')
+ {
+ const totalOpenMs = Date.now() - initStart
+ if (totalOpenMs > 2000) {
+ const phaseList = Object.entries(phaseTimingsMs)
+ .map(([name, ms]) => `${name}=${ms}ms`)
+ .join(', ')
+ prodLog.narrate(
+ `[Brainy] slow open: ${totalOpenMs}ms total (${phaseList}) — see the ` +
+ `phase breakdown above to find which one to investigate first`
+ )
+ }
}
// Integration Hub initialization
@@ -1682,7 +1988,15 @@ export class Brainy implements BrainyInterface {
if (error instanceof Error && (error as Error & { code?: string }).code === 'BRAINY_WRITER_LOCKED') {
throw error
}
- throw new Error(`Failed to initialize Brainy: ${error}`)
+ // Wrap with the original as `cause` so the originating frame (a plugin's
+ // own file:line, e.g. a provider boot failure) survives to the caller's
+ // log — a plain string interpolation discards both stack and cause.
+ const message = error instanceof Error ? error.message : String(error)
+ throw new Error(`Failed to initialize Brainy: ${message}`, { cause: error })
+ } finally {
+ // The open is over — succeeded or failed. Stop the heartbeat here so a
+ // failed init never leaves a timer narrating a phase nobody is running.
+ clearInterval(openHeartbeat)
}
}
@@ -1700,76 +2014,112 @@ export class Brainy implements BrainyInterface {
* NOTE: Registers globally (once for all instances) to avoid MaxListenersExceededWarning
*/
private registerShutdownHooks(): void {
+ /**
+ * The signal-path shutdown. THREE LAWS, each written by a production
+ * shutdown that looked clean and wasn't:
+ *
+ * 1. PER-INSTANCE ISOLATION. This used to be one `try` around a loop over
+ * every open brain: the first instance whose flush rejected aborted the
+ * loop, so every remaining brain kept its writer lock and its unwritten
+ * markers — and the process still exited 0. A pool of brains failed in
+ * a batch, not one at a time.
+ * 2. THE MARKER IS PART OF SHUTDOWN. Flushing the indexes without closing
+ * the generation store leaves the clean-shutdown marker unwritten, so
+ * the NEXT open reads the store as crashed and folds the whole
+ * generation log — measured in tens of seconds on a real store, paid on
+ * every restart, after a shutdown the operator saw exit 0.
+ * 3. THE LOCK IS ALWAYS GIVEN UP. In a `finally`, per instance: a process
+ * on its way out holds nothing.
+ */
const flushOnShutdown = async () => {
console.log('Shutdown signal received - flushing pending data...')
- try {
- let flushedCount = 0
- for (const instance of Brainy.instances) {
- if (instance.initialized) {
- // Flush all buffered data, then close to release resources (timers, handles)
- await Promise.all([
- (async () => {
- if (instance.storage && typeof instance.storage.flushCounts === 'function') {
- await instance.storage.flushCounts()
- }
- })(),
- (async () => {
- if (instance.metadataIndex && typeof instance.metadataIndex.flush === 'function') {
- await instance.metadataIndex.flush()
- }
- })(),
- (async () => {
- if (instance.graphIndex && typeof instance.graphIndex.flush === 'function') {
- await instance.graphIndex.flush()
- }
- })(),
- (async () => {
- if (instance.index && typeof instance.index.flush === 'function') {
- await instance.index.flush()
- }
- })()
- ])
- // Close components to stop timers that would prevent clean process exit
- await Promise.all([
- (async () => {
- if (instance.graphIndex && typeof instance.graphIndex.close === 'function') {
- await instance.graphIndex.close()
- }
- })(),
- (async () => {
- const index = instance.index as JsHnswVectorIndex & VectorIndexOptionalHooks
- if (index && typeof index.close === 'function') {
- await index.close()
- }
- })(),
- (async () => {
- const metadataIndex = instance.metadataIndex as MetadataIndexManager & MetadataIndexOptionalHooks
- if (metadataIndex && typeof metadataIndex.close === 'function') {
- await metadataIndex.close()
- }
- })(),
- // Release the writer lock so a successor process can take over.
- // No-op for readers and for backends without locking.
- (async () => {
- if (instance.storage && typeof instance.storage.releaseWriterLock === 'function') {
- await instance.storage.releaseWriterLock()
- }
- })(),
- // Stop the flush-request watcher to release its interval timer.
- (async () => {
- if (instance.storage && typeof instance.storage.stopFlushRequestWatcher === 'function') {
- instance.storage.stopFlushRequestWatcher()
- }
- })(),
- ])
- flushedCount++
+ let flushedCount = 0
+ let failedCount = 0
+ // Snapshot: close() splices Brainy.instances while we iterate.
+ for (const instance of [...Brainy.instances]) {
+ if (!instance.initialized) continue
+ try {
+ // Flush all buffered data (parallel across components, this brain only).
+ await Promise.all([
+ (async () => {
+ if (instance.storage && typeof instance.storage.flushCounts === 'function') {
+ await instance.storage.flushCounts()
+ }
+ })(),
+ (async () => {
+ if (instance.metadataIndex && typeof instance.metadataIndex.flush === 'function') {
+ await instance.metadataIndex.flush()
+ }
+ })(),
+ (async () => {
+ if (instance.graphIndex && typeof instance.graphIndex.flush === 'function') {
+ await instance.graphIndex.flush()
+ }
+ })(),
+ (async () => {
+ if (instance.index && typeof instance.index.flush === 'function') {
+ await instance.index.flush()
+ }
+ })()
+ ])
+
+ // Close the generation store: persists the counter, advances the
+ // fold checkpoint, and stamps the clean-shutdown marker LAST — the
+ // one step that decides whether the next open adopts or folds. Law 2.
+ if (instance.generationStore && !instance.isReadOnly) {
+ await instance.generationStore.close()
+ }
+
+ // Close components to stop timers that would prevent clean process exit
+ await Promise.all([
+ (async () => {
+ if (instance.graphIndex && typeof instance.graphIndex.close === 'function') {
+ await instance.graphIndex.close()
+ }
+ })(),
+ (async () => {
+ const index = instance.index as JsHnswVectorIndex & VectorIndexOptionalHooks
+ if (index && typeof index.close === 'function') {
+ await index.close()
+ }
+ })(),
+ (async () => {
+ const metadataIndex = instance.metadataIndex as MetadataIndexManager & MetadataIndexOptionalHooks
+ if (metadataIndex && typeof metadataIndex.close === 'function') {
+ await metadataIndex.close()
+ }
+ })()
+ ])
+ flushedCount++
+ } catch (error) {
+ failedCount++
+ console.error('Failed to flush one Brainy instance on shutdown:', error)
+ } finally {
+ // Law 3 — the lock and the watcher go regardless.
+ try {
+ if (instance.storage && typeof instance.storage.stopFlushRequestWatcher === 'function') {
+ instance.storage.stopFlushRequestWatcher()
+ }
+ } catch (error) {
+ console.error('Failed to stop the flush-request watcher on shutdown:', error)
+ }
+ try {
+ if (instance.storage && typeof instance.storage.releaseWriterLock === 'function') {
+ await instance.storage.releaseWriterLock()
+ }
+ } catch (error) {
+ console.error('Failed to release the writer lock on shutdown:', error)
}
}
- if (flushedCount > 0) {
- console.log(`Flushed successfully (${flushedCount} instance${flushedCount > 1 ? 's' : ''})`)
- }
- } catch (error) {
- console.error('Failed to flush on shutdown:', error)
+ }
+ if (flushedCount > 0) {
+ console.log(`Flushed successfully (${flushedCount} instance${flushedCount > 1 ? 's' : ''})`)
+ }
+ if (failedCount > 0) {
+ console.error(
+ `${failedCount} Brainy instance${failedCount > 1 ? 's' : ''} did not complete shutdown — ` +
+ `their writer locks were released, but their next open will run crash recovery.`
+ )
}
}
@@ -1777,13 +2127,32 @@ export class Brainy implements BrainyInterface {
// kept as statics so the last live instance's close() can deregister them
// — the signal handles they hold are ref'd and would otherwise keep the
// process alive forever after every brain is closed.
+ /**
+ * Exit the process ONLY when Brainy is the sole handler for this signal.
+ *
+ * Registering a signal listener suppresses Node's default terminate
+ * behaviour, so a library that attaches one must either exit or be sure
+ * someone else will. Brainy attaching one AND exiting was the wrong half
+ * of that choice for every host application with its own graceful
+ * shutdown: both handlers run concurrently, and whichever finishes first
+ * wins — a library flush finishing before an application's close()
+ * terminated that close mid-flight, at exit code 0, with locks and
+ * markers unwritten. When the host has its own handler (listener count
+ * above our own), the host owns the exit; Brainy only makes its data
+ * durable and steps aside.
+ */
+ const exitIfSoleShutdownOwner = (signal: 'SIGTERM' | 'SIGINT'): void => {
+ if (process.listenerCount(signal) <= 1) {
+ process.exit(0)
+ }
+ }
Brainy.sigtermListener = async () => {
await flushOnShutdown()
- process.exit(0)
+ exitIfSoleShutdownOwner('SIGTERM')
}
Brainy.sigintListener = async () => {
await flushOnShutdown()
- process.exit(0)
+ exitIfSoleShutdownOwner('SIGINT')
}
Brainy.beforeExitListener = async () => {
// Self-deregister FIRST: Node re-emits 'beforeExit' after every event-
@@ -2250,6 +2619,17 @@ export class Brainy implements BrainyInterface {
[{ type: 'embed.landed', id, vector: newVector }],
'system:embed-landing'
)
+ // Vectored-noun ledger: the landing commit above carries a vector
+ // write with NO accompanying metadata operation, so the
+ // saveNounMetadata(..., hasVector) seam never fires for it — the
+ // narrow storage hook is the only seam left. `oldVector.length===0`
+ // (already known for free from the pre-embed read above) proves this
+ // is a GENUINE first landing, not a re-embed of an already-vectored
+ // row (e.g. a deferred update() on a row that already had a real
+ // vector) — the latter must never double-count.
+ if (oldVector.length === 0) {
+ await this.storage.noteVectorLanded?.(id)
+ }
this.clearPendingEmbed(id)
} catch (err) {
prodLog.warn(
@@ -2396,6 +2776,12 @@ export class Brainy implements BrainyInterface {
* engine's own cadence (callers never call flush() in hot paths).
*/
private noteWriteForPersistence(): void {
+ // THE DIRTY WITNESS. Set on every committed write — both commit paths
+ // (single-op and transaction) end here, and the deferred-embed worker
+ // lands its vectors through the single-op path — BEFORE the policy check,
+ // so a `'manual'` consumer's explicit flush() is never skipped either.
+ // Cleared by a flush that actually runs; see flush().
+ this._dirtySinceLastFlush = true
const cfg = this.config.persistence
if (this.isReadOnly || cfg?.policy === 'manual') return
this._persistDirtyWrites++
@@ -2764,13 +3150,43 @@ export class Brainy implements BrainyInterface {
// vector shape, is structurally impossible). The background worker
// embeds + inserts.
const deferringEmbed = params.deferEmbedding === true && !params.vector
- const vector = deferringEmbed
+ let vector = deferringEmbed
? []
: params.vector || (await this.embed(params.data))
+ // THE ZERO-NORM LAW (canonical write side): a zero-norm vector is not a
+ // vector — it never crosses an engine boundary (the engine pair's seam
+ // law). This engine's own cosine distance treats an all-zero vector
+ // safely (a zero-norm operand always scores MAXIMUM distance — see
+ // isZeroNormVector's JSDoc), but a downstream engine serving squared-
+ // euclidean distance cannot tell it apart from a legitimate origin
+ // point — a false attractor that silently darkened 150+ rows in a
+ // production deployment. The index belt (AddToVectorIndexOperation)
+ // already refuses to INDEX a zero-norm vector, but until now the
+ // CANONICAL write still persisted it and the vectored-noun ledger
+ // counted it — so a near-empty store whose only vectored row was
+ // zero-norm read "canonical vectored > 0, index size 0" and threw a
+ // not-ready error at open. Normalize HERE, before the dimension pin,
+ // the vectored-ledger flag (`SaveNounMetadataOperation`'s `hasVector`),
+ // and the index ops below ever see it, so it persists as the sanctioned
+ // "unvectored" `[]` shape instead — the canonical write still succeeds.
+ if (!deferringEmbed && vector.length > 0 && isZeroNormVector(vector)) {
+ prodLog.warn(
+ `[Brainy] add(): entity ${id} was given an explicit all-zero vector — ` +
+ `a zero-norm vector is not a vector; persisted unvectored ([]) instead.`
+ )
+ vector = []
+ }
+
// Ensure dimensions are set (a deferred-embed stub carries no dimension
// information — the worker's real vector goes through the same guard).
- if (!deferringEmbed) {
+ // Gated on `vector.length > 0`, not `!deferringEmbed`: ANY insert whose
+ // vector is the "unvectored" empty-array shape carries no dimension
+ // information, deferred or not — an explicit `vector: []` (e.g. the VFS
+ // root's zero-norm fix, see VirtualFileSystem.doInitializeRoot()) must
+ // never pin `this.dimensions` to 0, which would poison every subsequent
+ // real embed's dimension check for the life of the store.
+ if (!deferringEmbed && vector.length > 0) {
if (!this.dimensions) {
this.dimensions = vector.length
} else if (vector.length !== this.dimensions) {
@@ -2868,8 +3284,11 @@ export class Brainy implements BrainyInterface {
const runInsert: TransactionFunction = async (tx) => {
// Operation 1: Save metadata FIRST (TypeAwareStorage caching)
// isNew=true: skip pre-read for rollback (entity doesn't exist yet)
+ // hasVector: the vectored-noun ledger counts this insert iff its
+ // vector is real/non-empty (never true for a deferred embed, whose
+ // stub `vector` is `[]` — it counts later, at landing).
tx.addOperation(
- new SaveNounMetadataOperation(this.storage, id, storageMetadata, true)
+ new SaveNounMetadataOperation(this.storage, id, storageMetadata, true, vector.length > 0)
)
// Operation 2: Save vector data
@@ -2883,10 +3302,15 @@ export class Brainy implements BrainyInterface {
}, true)
)
- // Operation 3: Add to HNSW index (after entity saved). A deferred
- // embed has nothing to index yet — the worker's atomic update
- // inserts the real vector.
- if (!deferringEmbed) {
+ // Operation 3: Add to HNSW index (after entity saved). Gated on
+ // `vector.length > 0`, not `!deferringEmbed`: a deferred embed has
+ // nothing to index yet (the worker's atomic update inserts the real
+ // vector later), and an explicit `vector: []` insert (the VFS root's
+ // zero-norm fix — permanently unvectored plumbing, never embedded)
+ // is exactly the same "nothing to index yet" shape. The zero-norm
+ // BELT (a real all-zero vector, non-empty) is enforced inside
+ // AddToVectorIndexOperation itself — see its JSDoc.
+ if (vector.length > 0) {
tx.addOperation(
new AddToVectorIndexOperation(this.index, id, vector, this.indexWriteGeneration)
)
@@ -3444,25 +3868,70 @@ export class Brainy implements BrainyInterface {
// new `data`); otherwise new `data` re-embeds; otherwise the existing
// vector is kept. Any vector change re-indexes HNSW below.
let vector = existing.vector
+ // 'data' is a real new value whenever it's not null/undefined — an
+ // empty string ('') is legitimate content (e.g. truncating a file to
+ // empty via overwrite), matching validateUpdateParams's absent-vs-empty
+ // distinction. Using `Boolean(params.data)` here would treat '' as "no
+ // new data", silently skipping BOTH the deferred marker and the eager
+ // re-embed below — a stale vector left behind with no path to ever
+ // correct itself (a quiet loss, not the deferred-but-eventually-
+ // correct flicker the deferEmbedding contract promises).
+ const rawHasNewData = params.data !== undefined && params.data !== null
+ // NO RE-EMBED ON UNCHANGED DATA: a write carrying the row's CURRENT data
+ // is not a data change — no re-embed, no deferred landing, no vector
+ // rewrite. A host heartbeat re-writing an unchanged row every few
+ // seconds fed a live index-row loop on a production store (each
+ // "change" landed a vector); the amplifier dies here regardless of how
+ // often the host writes.
+ const dataUnchanged = rawHasNewData && Brainy.sameEntityData(params.data, existing.data)
+ const hasNewData = rawHasNewData && !dataUnchanged
+
+ // THE ZERO-NORM LAW (canonical write side) — see add()'s matching
+ // comment: an explicit REAL all-zero vector is not a vector. Normalize
+ // to the sanctioned "unvectored" `[]` shape BEFORE the dimension
+ // check, the unvector-door decision below, and the index ops ever see
+ // it — a local copy; `params.vector` itself is never mutated.
+ let explicitVector = params.vector
+ if (explicitVector && explicitVector.length > 0 && isZeroNormVector(explicitVector)) {
+ prodLog.warn(
+ `[Brainy] update(): entity ${params.id} was given an explicit all-zero vector — ` +
+ `a zero-norm vector is not a vector; persisted unvectored ([]) instead.`
+ )
+ explicitVector = []
+ }
+
+ // THE SANCTIONED UNVECTOR DOOR: `explicitVector` at length 0 (an
+ // explicit `vector: []`, or a real all-zero vector just normalized
+ // above) is an instruction to remove the vector NOW — never "please
+ // embed". `validateUpdateParams` already refuses combining it with
+ // `deferEmbedding: true` (an empty array is truthy, so that guard
+ // fires unconditionally on any explicit `vector`). Idempotent on an
+ // already-unvectored row: the ledger decrement near the end of this
+ // method is gated on the PRIOR vector actually having been real.
+ const isExplicitUnvector = explicitVector !== undefined && explicitVector.length === 0
+
// MT5 deferred re-embedding: the OLD vector keeps serving semantic
// search — stale-but-present, never absent (the flicker law) — until
// the background worker embeds the new data and swaps it atomically.
const deferringEmbed =
- params.deferEmbedding === true && Boolean(params.data) && !params.vector
- if (params.vector) {
- if (this.dimensions && params.vector.length !== this.dimensions) {
+ params.deferEmbedding === true && hasNewData && !explicitVector
+ if (explicitVector) {
+ // A length-0 explicit vector (the unvector door) carries no
+ // dimension information — exempt from the check, mirroring add()'s
+ // own `vector.length > 0` gate on the dimension pin.
+ if (explicitVector.length > 0 && this.dimensions && explicitVector.length !== this.dimensions) {
throw new Error(
- `Vector dimension mismatch: expected ${this.dimensions}, got ${params.vector.length}`
+ `Vector dimension mismatch: expected ${this.dimensions}, got ${explicitVector.length}`
)
}
- vector = params.vector
- } else if (params.data && !deferringEmbed) {
+ vector = explicitVector
+ } else if (hasNewData && !deferringEmbed) {
vector = await this.embed(params.data)
}
// A deferred data change does NOT reindex now (the vector is unchanged;
// the worker's atomic swap carries the real reindex later).
const needsReindexing = Boolean(
- (params.data && !deferringEmbed) || params.type || params.vector
+ (hasNewData && !deferringEmbed) || params.type || explicitVector
)
// Always update the noun with new metadata
@@ -3556,6 +4025,22 @@ export class Brainy implements BrainyInterface {
? [this.enqueuePendingEmbed(params.id)]
: undefined
+ // Leg D — the unvector door clears a PENDING deferred-embed marker:
+ // without this, the worker would later embed this row's current data
+ // and silently re-vector it, defeating the caller's explicit "remove
+ // the vector now" instruction. The clear rides THIS SAME commit fact
+ // (an `embed.landed` record with an empty vector — the recovery fold
+ // disarms a pending marker on ANY `embed.landed` for the id,
+ // regardless of the vector it carries), so a crash between the write
+ // and the in-memory clear below still recovers disarmed. Mutually
+ // exclusive with `embedMarkers` above: `deferringEmbed` requires an
+ // ABSENT `explicitVector`, so the two branches never both apply.
+ const clearsPendingEmbed = isExplicitUnvector && this._pendingEmbedIds.has(params.id)
+ const commitRecords: FactMarkerRecord[] | undefined =
+ embedMarkers ?? (clearsPendingEmbed
+ ? [{ type: 'embed.landed', id: params.id, vector: [] }]
+ : undefined)
+
// Execute atomically with transaction system, generation-stamped as one
// immutable Model-B generation (before-image = the entity's prior state).
await this.persistSingleOp({ nouns: [params.id] }, async (tx) => {
@@ -3644,7 +4129,33 @@ export class Brainy implements BrainyInterface {
}
}
]
- : undefined, embedMarkers)
+ : undefined, commitRecords)
+
+ // Leg D continued — the in-memory pending-embed clear runs only AFTER
+ // the commit above actually succeeded (an aborted update must not
+ // disarm a marker whose durable `embed.landed` twin was never
+ // written).
+ if (clearsPendingEmbed) {
+ this.clearPendingEmbed(params.id)
+ prodLog.warn(
+ `[Brainy] update(): entity ${params.id} had a pending deferred embed — ` +
+ `the unvector door cleared it ('vector: []' is an explicit instruction, ` +
+ `never "please embed").`
+ )
+ }
+
+ // Leg D — vectored-ledger decrement for the sanctioned unvector door.
+ // update()'s own metadata write goes through UpdateNounMetadataOperation
+ // (isNew=false), so the saveNounMetadata(..., hasVector) seam never
+ // fires here — noteVectorUnlanded is the ONLY seam, the same
+ // sanctioned hook unvectorNounForRootMigration() uses. Gated on the
+ // PRIOR vector having actually been real (non-empty, non-zero-norm):
+ // an already-unvectored row's second call is a true no-op — no
+ // decrement, matching the ledger-exactness law (never double-count,
+ // never drift negative).
+ if (isExplicitUnvector && existing.vector.length > 0 && !isZeroNormVector(existing.vector)) {
+ await this.storage.noteVectorUnlanded?.(params.id)
+ }
// Aggregation hook (outside transaction — derived data). `existing` is
// the full get() view — every reserved field top-level — and must be
@@ -3661,6 +4172,117 @@ export class Brainy implements BrainyInterface {
if (deferringEmbed) this.kickEmbedWorker()
}
+ /**
+ * @description Build the metadata-index retraction operation for one id
+ * (noun or verb) — the null-metadata-safe closure shared by every removal
+ * leg that reaches the metadata index with a possibly-missed pre-read:
+ * `remove()`'s own noun leg, its verb-cascade retractions, `unrelate()`,
+ * and their `transact()`/`planTx*` mirrors (both callers add the returned
+ * operation to their own batch — `tx.addOperation()` for a single-op
+ * transaction, `plan.operations.push()` for a planned `transact()` batch).
+ * THE NULL-METADATA SKIP IS CLOSED (a posting-leak class):
+ * - metadata present → the ordinary, provider-agnostic
+ * `RemoveFromMetadataIndexOperation` (exact per-field retraction).
+ * - metadata absent (a torn pre-read, or the row was already gone) →
+ * a provider exposing `removeEntityById` (the id-keyed contract) gets
+ * exact per-entity retraction via its reverse record; the JS index
+ * gets `removeFromIndex(id)` — safe id-keyed cleanup (deleted bitmap +
+ * id mapper; field statistics reconcile at the next rebuild/repairIndex),
+ * narrated; a native provider WITHOUT the contract is never called
+ * metadata-omitted (that path walks its value space) — the skip is
+ * tracked in the degraded set instead, narrated, so `repairIndex()`
+ * reconciles it (and this method returns `null` — no operation to add).
+ * Silence is the only thing outlawed.
+ * @param id - The noun/verb id being retracted.
+ * @param metadata - The pre-read metadata/entity structure, or falsy when
+ * the read missed.
+ * @param context - Narration prefix identifying the caller/id, e.g.
+ * `remove(${id})` or `remove(${entityId}) cascade unrelate ${verbId}`.
+ * @returns The operation to add to the caller's batch, or `null` when
+ * nothing could be done (already narrated + tracked as degraded).
+ */
+ /**
+ * @description A JSON-safe view of a record bound for the metadata-index
+ * crossing. The seam's metadata is JSON-safe BY CONTRACT (a native provider
+ * serializes it; u64 ints as Number corrupt above 2^53) — but
+ * {@link resolveVerbEndpointInts} MIRRORS the resolved endpoint ints onto
+ * the verb object itself as BigInt (`verb.sourceInt`/`targetInt`), so a
+ * verb object reused as index metadata carried BigInts into
+ * JSON.stringify, which throws, aborting the whole transaction (found by
+ * the first joint pair gate). Endpoint ints ride their OWN op params on the
+ * graph legs — the metadata crossing drops every BigInt-valued top-level
+ * key instead of guessing at a lossy numeric encoding.
+ * @param metadata - The candidate index-metadata record.
+ * @returns The same object when already JSON-safe, else a shallow copy
+ * without the BigInt-valued keys.
+ */
+ private static jsonSafeIndexMetadata(metadata: unknown): unknown {
+ if (metadata === null || typeof metadata !== 'object') return metadata
+ const rec = metadata as Record
+ let hasBigint = false
+ for (const k in rec) {
+ if (typeof rec[k] === 'bigint') { hasBigint = true; break }
+ }
+ if (!hasBigint) return metadata
+ const out: Record = {}
+ for (const k in rec) {
+ if (typeof rec[k] !== 'bigint') out[k] = rec[k]
+ }
+ return out
+ }
+
+ private metadataIndexRetractionOp(
+ id: string,
+ metadata: unknown,
+ context: string
+ ): Operation | null {
+ if (metadata) {
+ return new RemoveFromMetadataIndexOperation(
+ this.metadataIndex, id, Brainy.jsonSafeIndexMetadata(metadata), this.indexWriteGeneration
+ )
+ }
+ const prov = this.metadataIndex as unknown as {
+ removeEntityById?: (id: string) => Promise
+ removeFromIndex?: (id: string, metadata?: unknown, generation?: bigint) => Promise
+ }
+ if (typeof prov.removeEntityById === 'function') {
+ const g = this.indexWriteGeneration
+ return {
+ name: 'RemoveEntityByIdTombstone',
+ execute: async () => {
+ await prov.removeEntityById!(id)
+ return async () => {
+ // Undo of an id-keyed tombstone on an absent row: nothing to
+ // restore (the row had no readable metadata to re-post).
+ void g
+ }
+ }
+ }
+ } else if (this.metadataIndex instanceof MetadataIndexManager) {
+ const gv = this.indexWriteGeneration
+ prodLog.warn(
+ `[Brainy] ${context}: no metadata at delete — id-keyed index cleanup ran ` +
+ `(deleted bitmap + id mapper); field statistics reconcile at the next rebuild/repairIndex.`
+ )
+ return {
+ name: 'IdKeyedIndexCleanup',
+ execute: async () => {
+ await prov.removeFromIndex!(id, undefined, typeof gv === 'function' ? gv() : gv)
+ return async () => {}
+ }
+ }
+ } else {
+ this._indexDegradedIds.add(id)
+ prodLog.warn(
+ `[Brainy] ${context}: no metadata at delete and this provider has no id-keyed ` +
+ `removal — its postings for this id are NOT tombstoned yet (tracked as degraded; ` +
+ `repairIndex() reconciles). Never calling a metadata-omitted native removal: that ` +
+ `path walks the store's value space.`
+ )
+ return null
+ }
+ }
+
/**
* Remove an entity and all its relationships
*
@@ -3725,61 +4347,11 @@ export class Brainy implements BrainyInterface {
)
}
- // Operation 2: Remove from metadata index. THE NULL-METADATA SKIP IS
- // CLOSED (a posting-leak class, confirmed at this site): when the
- // pre-read missed, the leg no longer silently skips —
- // - a provider exposing removeEntityById (the id-keyed contract)
- // gets it: exact per-entity retraction via its reverse record;
- // - the JS index gets removeFromIndex(id) — safe id-keyed cleanup
- // (deleted bitmap + id mapper; field stats reconcile at rebuild);
- // - a NATIVE provider WITHOUT the contract is never called
- // metadata-omitted (that path walks its value space) — the skip
- // happens, but NARRATED and tracked in the degraded set so
- // repairIndex reconciles it. Silence is the only thing outlawed.
- if (metadata) {
- tx.addOperation(
- new RemoveFromMetadataIndexOperation(this.metadataIndex, id, metadata, this.indexWriteGeneration)
- )
- } else {
- const prov = this.metadataIndex as unknown as {
- removeEntityById?: (id: string) => Promise
- removeFromIndex?: (id: string, metadata?: unknown, generation?: bigint) => Promise
- }
- if (typeof prov.removeEntityById === 'function') {
- const g = this.indexWriteGeneration
- tx.addOperation({
- name: 'RemoveEntityByIdTombstone',
- execute: async () => {
- await prov.removeEntityById!(id)
- return async () => {
- // Undo of an id-keyed tombstone on an absent row: nothing
- // to restore (the row had no readable metadata to re-post).
- void g
- }
- }
- })
- } else if (this.metadataIndex instanceof MetadataIndexManager) {
- const gv = this.indexWriteGeneration
- tx.addOperation({
- name: 'IdKeyedIndexCleanup',
- execute: async () => {
- await prov.removeFromIndex!(id, undefined, typeof gv === 'function' ? gv() : gv)
- return async () => {}
- }
- })
- prodLog.warn(
- `[Brainy] remove(${id}): no metadata at delete — id-keyed index cleanup ran ` +
- `(deleted bitmap + id mapper); field statistics reconcile at the next rebuild/repairIndex.`
- )
- } else {
- this._indexDegradedIds.add(id)
- prodLog.warn(
- `[Brainy] remove(${id}): no metadata at delete and this provider has no id-keyed ` +
- `removal — its postings for this id are NOT tombstoned yet (tracked as degraded; ` +
- `repairIndex() reconciles). Never calling a metadata-omitted native removal: that ` +
- `path walks the store's value space.`
- )
- }
+ // Operation 2: Remove from metadata index (null-metadata-safe — see
+ // metadataIndexRetractionOp's JSDoc for the full closure).
+ {
+ const retractionOp = this.metadataIndexRetractionOp(id, metadata, `remove(${id})`)
+ if (retractionOp) tx.addOperation(retractionOp)
}
// Operation 3: Delete noun (full removal). The pre-read metadata rides
@@ -3797,6 +4369,21 @@ export class Brainy implements BrainyInterface {
tx.addOperation(
new RemoveFromGraphIndexOperation(this.graphIndex, verb, { sourceInt, targetInt }, this.graphWriteGeneration)
)
+ // Retract the cascaded relation's metadata-index row too — the
+ // live mirror of what a rebuild would derive for this (now-gone)
+ // edge (mirrors the noun leg above). The whole hydrated verb
+ // (system fields top-level + the custom bag under `metadata`,
+ // same shape `extractIndexableFields` reads for any entity-record
+ // frame) is the before-image — every entry in `allVerbs` was
+ // already successfully hydrated by the reads above, so this is
+ // never metadata-omitted in practice, but the closure stays
+ // defensive rather than assuming.
+ {
+ const cascadeRetractionOp = this.metadataIndexRetractionOp(
+ verb.id, verb, `remove(${id}) cascade unrelate ${verb.id}`
+ )
+ if (cascadeRetractionOp) tx.addOperation(cascadeRetractionOp)
+ }
// Delete verb metadata
tx.addOperation(
new DeleteVerbMetadataOperation(this.storage, verb.id)
@@ -3976,8 +4563,9 @@ export class Brainy implements BrainyInterface {
// index read funnels through this helper, so the gate here makes
// serve-while-not-ready UNREPRESENTABLE — a production store once acked
// writes while every non-find() read served empty from a not-ready
- // provider for 15 minutes. Fast path after the latch is one boolean.
- await this.ensureIndexesLoaded()
+ // provider for 15 minutes. A CHECK only — it never builds; throws a typed
+ // NotReady error if a provider's health report says it isn't serving.
+ this.ensureIndexesLoaded(['graph'])
const entityInt = this.graphEntityInt(uuid)
if (entityInt === undefined) return []
const neighborInts = await this.graphIndex.getNeighbors(entityInt, options)
@@ -4005,72 +4593,72 @@ export class Brainy implements BrainyInterface {
/**
* @description Verify that the graph adjacency is actually LIVE before a graph read trusts
* its result. A native graph index can load its relationship COUNT (manifest) on a cold open
- * of a LARGE brain (≥10k nouns, which skips the eager index rebuild) but NOT its
- * source→target adjacency, so `getNeighbors()` returns `[]` for EVERY source even though
- * edges are persisted — and `find({ connected })` / `neighbors()` / `related()` would serve
- * that `[]` as if it were truth.
+ * but NOT its source→target adjacency, so `getNeighbors()` returns `[]` for EVERY source even
+ * though edges are persisted — and `find({ connected })` / `neighbors()` / `related()` would
+ * serve that `[]` as if it were truth.
*
- * Two detection strategies, in order of honesty:
- * - **Preferred (8.0 contract):** the provider exposes a sync `isReady()` that is true ONLY
- * when the edges are loaded. `false` → hydrate the id-mapper (a native int adjacency
- * resolves endpoints through it), rebuild from storage, and re-check `isReady()`; if it is
- * still `false`, throw {@link GraphIndexNotReadyError} rather than returning `[]`.
- * - **Fallback (providers without `isReady()`):** a GLOBAL known-edge sample (a real
+ * NEVER REBUILDS, NEVER WALKS THE STORE — a read-path rebuild is exactly the dark-rebuild
+ * failure mode this contract retires (open() alone owns building; see
+ * {@link rebuildIndexesIfNeeded}). Two detection strategies, in order of honesty:
+ * - **Preferred:** {@link assessProviderHealth} — the provider's named `healthReport()` when
+ * exposed, else its sync `isReady()`. Not serving → THROW {@link GraphIndexNotReadyError}
+ * naming the reasons, immediately — no rebuild attempt.
+ * - **Fallback (providers with neither signal):** a READ-ONLY GLOBAL known-edge sample (a real
* persisted verb's `sourceId`, which by definition HAS an outgoing edge) — NOT any queried
* anchor, because brainy cannot cheaply tell "adjacency unloaded" from "this node is
- * genuinely edgeless" per-anchor. If that known-edge source resolves to no neighbors, the
- * adjacency did not load: rebuild and re-probe; if even that fails, throw.
+ * genuinely edgeless" per-anchor. If that known-edge source resolves to no neighbors, THROW —
+ * the probe refuses loudly; it does not self-heal.
*
- * @returns `'live'` when the adjacency is already trustworthy (or there is genuinely nothing
- * to verify), or `'rebuilt'` when a cold-unloaded adjacency was just healed from storage —
- * in which case callers that observed an empty result must RE-RUN their collection.
- * @throws {GraphIndexNotReadyError} when the index claims edges but cannot serve a known
- * persisted edge (or stays not-ready) even after a rebuild.
+ * @returns `'live'` when the adjacency is already trustworthy (or there is genuinely nothing to
+ * verify).
+ * @throws {GraphIndexNotReadyError} when the index is not serving, or claims edges but cannot
+ * serve a known persisted edge.
*/
- private async verifyGraphAdjacencyLive(): Promise<'live' | 'rebuilt'> {
+ private async verifyGraphAdjacencyLive(): Promise<'live'> {
if (this._graphAdjacencyVerified) return 'live'
// Coordinated migration LOCK (#18): while the graph provider owns a locked
- // rebuild-from-canonical, brainy must NOT fire its own graphIndex.rebuild()
- // on a read — that would race the provider's in-place rebuild. The data-plane
- // lock (awaitMigrationLock in ensureInitialized) already makes callers wait,
- // so this is normally unreachable mid-migration; the guard is defensive. It
+ // rebuild-from-canonical, brainy must NOT judge it here — the provider owns
+ // its index until it verifies-and-swaps. The data-plane lock
+ // (awaitMigrationLock in ensureInitialized) already makes callers wait, so
+ // this is normally unreachable mid-migration; the guard is defensive. It
// deliberately does NOT set `_graphAdjacencyVerified`, so the real verify runs
// once the migration clears.
if (this.providerIsMigrating(this.graphIndex)) return 'live'
- // Re-entrancy: rebuild() can trigger reads (neighbors/related) that call back into this
- // guard. While a verify is in flight, short-circuit so we cannot recurse into rebuild().
+ // Re-entrancy: a fallback probe below calls getNeighbors(), which does not
+ // re-enter this guard, but the short-circuit is kept defensively cheap.
if (this._graphAdjacencyVerifying) return 'live'
this._graphAdjacencyVerifying = true
try {
- const gi = this.graphIndex as GraphAdjacencyIndex & { isReady?: () => boolean }
-
- // ── Strategy 1: honest isReady() signal (cortex >= 2.7.8 / 3.0) ──────────
- if (typeof gi.isReady === 'function') {
- if (gi.isReady()) {
+ // ── Strategy 1: the health-report/isReady() authority — never rebuilds ──
+ const assessment = assessProviderHealth(this.graphIndex)
+ if (assessment.via === 'health-report' || assessment.via === 'is-ready') {
+ if (assessment.readiness === 'ready') {
this._graphAdjacencyVerified = true
return 'live'
}
- // Not ready: the edges did not load on open. Hydrate the id-mapper, then rebuild.
- if (!this.config.silent) {
- console.warn(
- `[Brainy] Graph adjacency reports not-ready (isReady() === false) — the persisted ` +
- `adjacency did not load on open. Rebuilding from storage…`
+ // A provider that is REBUILDING ITSELF gets a refusal that says so,
+ // with its own progress: open deliberately did not wait for it (see
+ // rebuildIndexesIfNeeded), so this door is temporarily closed and will
+ // open on its own. Anything else is a broken index needing a repair.
+ const rebuilding = assessProviderRebuild(this.graphIndex)
+ if (rebuilding) {
+ throw new GraphIndexNotReadyError(
+ `Graph adjacency index is ${describeRebuildProgress(rebuilding)} and is not serving ` +
+ `yet. find({ connected }), neighbors() and related() refuse rather than serve an ` +
+ `empty result. The brain is open and every other family is serving; this door opens ` +
+ `by itself when the provider reports serving — no action is needed.`
)
}
- await this.hydrateIdMapperForGraphRebuild()
- await this.graphIndex.rebuild()
- if (gi.isReady()) {
- this._graphAdjacencyVerified = true
- return 'rebuilt'
- }
throw new GraphIndexNotReadyError(
- `Graph adjacency index reports not-ready even after a rebuild — the persisted ` +
- `adjacency could not be loaded. find({ connected }), neighbors() and related() ` +
- `cannot be served reliably for this brain.`
+ `Graph adjacency index is not serving (via ${assessment.via}): ` +
+ `${assessment.reasons.join('; ') || 'not ready'}. find({ connected }), neighbors() and ` +
+ `related() refuse rather than serve an empty result — rebuild via ` +
+ `repairIndex({ rebuild: ['graph'] }) or reopen the brain.`
)
}
- // ── Strategy 2: known-edge-sample probe (providers without isReady()) ────
+ // ── Strategy 2: known-edge-sample probe (providers with neither signal) ─
+ // READ-ONLY — refuses loudly on failure; never calls rebuild().
const claimed = await this.graphIndex.size()
if (!claimed || claimed <= 0) return 'live' // no edges claimed — nothing to verify
@@ -4086,10 +4674,9 @@ export class Brainy implements BrainyInterface {
// the sample is not one of this brain's own edges — e.g. a shared on-disk store reused
// across instances surfaces a foreign verb whose UUID this brain's resident mapper never
// interned. We cannot prove a cold-unloaded adjacency from such a sample, so treat it as
- // INCONCLUSIVE: mark verified and return 'live' rather than rebuilding/throwing. (The honest
- // cold-load signal for native providers is isReady(), checked above; the JS baseline keeps
- // its mapper resident, so its OWN edges always resolve — the targeted 7.x failure mode,
- // "mapper loaded but adjacency empty", still resolves the source and is detected below.)
+ // INCONCLUSIVE: mark verified and return 'live' rather than throwing. (The honest cold-load
+ // signal for native providers is Strategy 1, checked above; the JS baseline keeps its mapper
+ // resident, so its OWN edges always resolve.)
const sourceInt = this.graphEntityInt(verb.sourceId)
if (sourceInt === undefined) {
this._graphAdjacencyVerified = true
@@ -4097,37 +4684,23 @@ export class Brainy implements BrainyInterface {
}
// Ask the adjacency for ONE neighbor of the (mapped) known-edge source.
- const probeKnownSource = async (): Promise =>
- (await this.graphIndex.getNeighbors(sourceInt, { limit: 1 })).length > 0
-
- if (await probeKnownSource()) {
+ const hasNeighbor = (await this.graphIndex.getNeighbors(sourceInt, { limit: 1 })).length > 0
+ if (hasNeighbor) {
this._graphAdjacencyVerified = true
return 'live' // adjacency is live — the common case
}
// INCONSISTENT: the index reports edges but a KNOWN-mapped persisted edge's source has none →
- // the adjacency did not load on open. Hydrate the mapper and rebuild from storage.
- if (!this.config.silent) {
- console.warn(
- `[Brainy] Graph adjacency reports ${claimed} relationship(s) but a persisted edge ` +
- `resolves to none — the persisted adjacency did not load on open. Rebuilding from storage…`
- )
- }
- await this.hydrateIdMapperForGraphRebuild()
- await this.graphIndex.rebuild()
-
- if (await probeKnownSource()) {
- this._graphAdjacencyVerified = true
- return 'rebuilt'
- }
+ // the adjacency did not load. Refuse loudly — never rebuild from a read.
throw new GraphIndexNotReadyError(
- `Graph adjacency index reports ${claimed} relationship(s) but returns no edges even ` +
- `after a rebuild — the persisted adjacency could not be loaded. find({ connected }), ` +
- `neighbors() and related() cannot be served reliably for this brain.`
+ `Graph adjacency index reports ${claimed} relationship(s) but a persisted edge's source ` +
+ `resolves to none — the persisted adjacency did not load. find({ connected }), ` +
+ `neighbors() and related() refuse rather than serve an empty result — rebuild via ` +
+ `repairIndex({ rebuild: ['graph'] }) or reopen the brain.`
)
} catch (err) {
if (err instanceof GraphIndexNotReadyError) throw err
- // A transient probe/rebuild failure must not break the actual query NOR be
+ // A transient probe failure must not break the actual query NOR be
// masked as "no data". Allow a re-check on the next graph read and fall through.
this._graphAdjacencyVerified = false
if (!this.config.silent) {
@@ -4144,27 +4717,60 @@ export class Brainy implements BrainyInterface {
* On a cold open a native metadata provider can report data yet not serve its
* `where` postings, so `find({ where })` silently returns `[]` — the exact
* failure a downstream deployment reported (cold reads blanking filtered pages
- * after every restart). This one-shot guard, run on the first FILTERED `find()`,
- * closes that: it takes a KNOWN persisted entity + one of its plain field values
- * and asks the index to resolve it. If the index returns the known id the field
- * postings are live (the common case, and the ONLY cost on a warm brain — one
- * O(1) probe). If it does not, the postings did not load: brainy rebuilds the
- * index from the canonical records and re-probes; if it STILL cannot serve the
- * known value it throws a loud {@link MetadataIndexNotReadyError} rather than
- * let a silent `[]` stand. Inconclusive cases (empty store, no plain field to
- * probe, a shared store surfacing a foreign entity) are treated as live — never
- * a false rebuild. A migrating provider is skipped (it owns its locked rebuild).
- * @returns `'live'` when the index serves, `'rebuilt'` when a rebuild restored it.
+ * after every restart).
+ *
+ * NEVER REBUILDS, NEVER WALKS THE STORE — a read-path rebuild is exactly the
+ * dark-rebuild failure mode this contract retires (open() alone owns
+ * building; see {@link rebuildIndexesIfNeeded}). Two detection strategies:
+ * - **Preferred:** {@link assessProviderHealth} — the provider's named
+ * `healthReport()` when exposed, else its sync `isReady()`. Not serving →
+ * THROW {@link MetadataIndexNotReadyError} naming the reasons, immediately.
+ * - **Fallback (providers with neither signal):** a READ-ONLY known-value
+ * probe, run on the first FILTERED `find()`: take a KNOWN persisted entity
+ * + one of its plain field values and ask the index to resolve it. If the
+ * index does not return the known id, THROW — the probe refuses loudly;
+ * it does not self-heal. Inconclusive cases (empty store, no plain field
+ * to probe, a shared store surfacing a foreign entity) are treated as
+ * live — never a false throw. A migrating provider is skipped (it owns
+ * its locked rebuild).
+ * @returns `'live'` when the index serves.
*/
- private async verifyMetadataLive(): Promise<'live' | 'rebuilt'> {
+ private async verifyMetadataLive(): Promise<'live'> {
if (this._metadataVerified) return 'live'
// Migration LOCK (#18): a migrating provider owns its in-place rebuild — do
// not race it. Defensive; the data-plane lock already gates callers upstream.
if (this.providerIsMigrating(this.metadataIndex)) return 'live'
- // Re-entrancy: rebuild() can trigger reads that call back into this guard.
+ // Re-entrancy: the fallback probe below calls filterIdsBelted(), which
+ // re-enters ensureIndexesLoaded() (a cheap CHECK) but not this guard.
if (this._metadataVerifying) return 'live'
this._metadataVerifying = true
try {
+ // ── Strategy 1: the health-report/isReady() authority — never rebuilds ──
+ const assessment = assessProviderHealth(this.metadataIndex)
+ if (assessment.via === 'health-report' || assessment.via === 'is-ready') {
+ if (assessment.readiness === 'ready') {
+ this._metadataVerified = true
+ return 'live'
+ }
+ const rebuilding = assessProviderRebuild(this.metadataIndex)
+ if (rebuilding) {
+ throw new MetadataIndexNotReadyError(
+ `Metadata field index is ${describeRebuildProgress(rebuilding)} and is not serving ` +
+ `yet. find({ where }) and other filtered reads refuse rather than serve an empty ` +
+ `result. The brain is open and every other family is serving; this door opens by ` +
+ `itself when the provider reports serving — no action is needed.`
+ )
+ }
+ throw new MetadataIndexNotReadyError(
+ `Metadata field index is not serving (via ${assessment.via}): ` +
+ `${assessment.reasons.join('; ') || 'not ready'}. find({ where }) and other filtered ` +
+ `reads refuse rather than serve an empty result — rebuild via ` +
+ `repairIndex({ rebuild: ['metadata'] }) or reopen the brain.`
+ )
+ }
+
+ // ── Strategy 2: known-value probe (providers with neither signal) ──────
+ // READ-ONLY — refuses loudly on failure; never calls rebuild().
// A KNOWN persisted entity + one plain field to probe. Sample a few so a
// system-only entity (e.g. the VFS root) doesn't make every open inconclusive.
const sample = await this.storage.getNouns({ pagination: { limit: 5, offset: 0 } })
@@ -4186,7 +4792,7 @@ export class Brainy implements BrainyInterface {
return ids.includes(p.id)
} catch {
// FIELD_NOT_INDEXED for a field a persisted entity actually holds is
- // itself the cold/broken signal — treat as not-serving (→ rebuild).
+ // itself the cold/broken signal — treat as not-serving.
return false
}
}
@@ -4196,26 +4802,15 @@ export class Brainy implements BrainyInterface {
return 'live' // field postings are live — the common case
}
- if (!this.config.silent) {
- console.warn(
- `[Brainy] Metadata field index returns no match for a known persisted value of ` +
- `'${p.field}' — the field postings did not load on open. Rebuilding from storage…`
- )
- }
- await this.metadataIndex.rebuild()
-
- if (await probeServes()) {
- this._metadataVerified = true
- return 'rebuilt'
- }
throw new MetadataIndexNotReadyError(
- `Metadata field index cannot serve a known persisted value of '${p.field}' even after ` +
- `a rebuild — find({ where }) and other filtered reads cannot be served reliably for ` +
- `this brain (a silent empty result would misrepresent existing data).`
+ `Metadata field index cannot serve a known persisted value of '${p.field}' — the field ` +
+ `postings did not load. find({ where }) and other filtered reads refuse rather than ` +
+ `serve an empty result — rebuild via repairIndex({ rebuild: ['metadata'] }) or reopen ` +
+ `the brain.`
)
} catch (err) {
if (err instanceof MetadataIndexNotReadyError) throw err
- // A transient probe/rebuild failure must not break the query NOR mask as
+ // A transient probe failure must not break the query NOR mask as
// "no data". Allow a re-check on the next filtered read and fall through.
this._metadataVerified = false
if (!this.config.silent) {
@@ -4261,57 +4856,61 @@ export class Brainy implements BrainyInterface {
* report a non-zero `size()` (its persisted COUNT loaded) yet not have loaded
* its serving structure (the mmap/DiskANN graph) — so a pure semantic
* `find({ query })` silently returns `[]`. A pure semantic query has
- * `hasFilterCriteria === false`, so the metadata guard never fires; this guard
- * closes that gap. Run one-shot on the first vector/proximity search:
- * - **Preferred (honest signal):** the provider exposes `isReady()`. `false`
- * → rebuild from storage, re-check; if still `false`, throw
- * {@link VectorIndexNotReadyError} rather than serving `[]`.
- * - **Fallback (no `isReady()`):** a KNOWN persisted vector (sampled +
- * hydrated) is searched against the index; if it does not self-match, the
- * serving structure did not load — rebuild + re-probe, else throw.
+ * `hasFilterCriteria === false`, so the metadata guard never fires; this
+ * guard closes that gap. Run one-shot on the first vector/proximity search.
+ *
+ * NEVER REBUILDS, NEVER WALKS THE STORE — a read-path rebuild is exactly the
+ * dark-rebuild failure mode this contract retires (open() alone owns
+ * building; see {@link rebuildIndexesIfNeeded}). Two detection strategies:
+ * - **Preferred:** {@link assessProviderHealth} — the provider's named
+ * `healthReport()` when exposed, else its sync `isReady()`. Not serving →
+ * THROW {@link VectorIndexNotReadyError} naming the reasons, immediately.
+ * - **Fallback (providers with neither signal):** a READ-ONLY KNOWN
+ * persisted vector (sampled + hydrated) is searched against the index; if
+ * it does not self-match, THROW — the probe refuses loudly; it does not
+ * self-heal.
* Inconclusive cases (empty store, no probeable vector, `size()===0` — where
- * the JS baseline's cold load is `ensureIndexesLoaded`'s job) are treated as
- * live: never a false rebuild. A migrating provider is skipped (it owns its
- * locked rebuild).
- * @returns `'live'` when the index serves, `'rebuilt'` when a rebuild restored it.
+ * the JS baseline is built at open) are treated as live: never a false
+ * throw. A migrating provider is skipped (it owns its locked rebuild).
+ * @returns `'live'` when the index serves.
*/
- private async verifyVectorLive(): Promise<'live' | 'rebuilt'> {
+ private async verifyVectorLive(): Promise<'live'> {
if (this._vectorVerified) return 'live'
// Migration LOCK (#18): a migrating provider owns its in-place rebuild.
if (this.providerIsMigrating(this.index)) return 'live'
- // Re-entrancy: rebuild() can trigger reads that call back into this guard.
+ // Re-entrancy: the fallback probe below calls index.search(), which does
+ // not re-enter this guard, but the short-circuit is kept defensively cheap.
if (this._vectorVerifying) return 'live'
this._vectorVerifying = true
try {
- // ── Strategy 1: honest isReady() signal (native provider) ──────────────
- const readiness = assessIndexReadiness(this.index)
- if (readiness !== 'unknown') {
- if (readiness === 'ready') {
+ // ── Strategy 1: the health-report/isReady() authority — never rebuilds ──
+ const assessment = assessProviderHealth(this.index)
+ if (assessment.via === 'health-report' || assessment.via === 'is-ready') {
+ if (assessment.readiness === 'ready') {
this._vectorVerified = true
return 'live'
}
- // Not ready: the serving structure did not load on open. Rebuild.
- if (!this.config.silent) {
- console.warn(
- `[Brainy] Vector index reports not-ready (isReady() === false) — the persisted ` +
- `vector index did not load on open. Rebuilding from storage…`
+ const rebuilding = assessProviderRebuild(this.index)
+ if (rebuilding) {
+ throw new VectorIndexNotReadyError(
+ `Vector index is ${describeRebuildProgress(rebuilding)} and is not serving yet. ` +
+ `Semantic find({ query }) and proximity search refuse rather than serve an empty ` +
+ `result. The brain is open and every other family is serving; this door opens by ` +
+ `itself when the provider reports serving — no action is needed.`
)
}
- await this.index.rebuild()
- if (assessIndexReadiness(this.index) === 'ready') {
- this._vectorVerified = true
- return 'rebuilt'
- }
throw new VectorIndexNotReadyError(
- `Vector index reports not-ready even after a rebuild — semantic find({ query }) and ` +
- `proximity search cannot be served reliably for this brain (a silent empty result ` +
- `would misrepresent existing data).`
+ `Vector index is not serving (via ${assessment.via}): ` +
+ `${assessment.reasons.join('; ') || 'not ready'}. Semantic find({ query }) and ` +
+ `proximity search refuse rather than serve an empty result — rebuild via ` +
+ `repairIndex({ rebuild: ['vector'] }) or reopen the brain.`
)
}
- // ── Strategy 2: known-vector probe (providers without isReady()) ───────
+ // ── Strategy 2: known-vector probe (providers with neither signal) ─────
+ // READ-ONLY — refuses loudly on failure; never calls rebuild().
const claimed = this.index.size()
- if (!claimed || claimed <= 0) return 'live' // JS cold path is ensureIndexesLoaded's job
+ if (!claimed || claimed <= 0) return 'live' // JS cold path is built at open
const probe = await this.pickVectorProbe()
if (!probe) {
@@ -4321,44 +4920,30 @@ export class Brainy implements BrainyInterface {
}
const p = probe
- const probeServes = async (): Promise => {
- // The failure mode we guard is the SILENT EMPTY result: a cold index that
- // loaded its COUNT but not its serving structure returns `[]` for a
- // known-present vector, while a warm index returns at least one hit. We
- // check for a NON-EMPTY result, NOT an exact self-match — HNSW is
- // approximate and `get()` may return a re-hydrated/normalized vector, so
- // demanding the exact self as top-1 would false-positive on a perfectly
- // healthy index (and wrongly rebuild → throw).
- const hits = await this.index.search(p.vector, 1)
- return hits.length > 0
- }
+ // The failure mode we guard is the SILENT EMPTY result: a cold index that
+ // loaded its COUNT but not its serving structure returns `[]` for a
+ // known-present vector, while a warm index returns at least one hit. We
+ // check for a NON-EMPTY result, NOT an exact self-match — HNSW is
+ // approximate and `get()` may return a re-hydrated/normalized vector, so
+ // demanding the exact self as top-1 would false-positive on a perfectly
+ // healthy index (and wrongly throw).
+ const hits = await this.index.search(p.vector, 1)
void p.id // probe keyed on the vector; id retained for diagnostics only
- if (await probeServes()) {
+ if (hits.length > 0) {
this._vectorVerified = true
return 'live' // serving structure is live — the common case
}
- if (!this.config.silent) {
- console.warn(
- `[Brainy] Vector index reports ${claimed} vector(s) but a known persisted vector ` +
- `returns no results — the serving structure did not load on open. Rebuilding…`
- )
- }
- await this.index.rebuild()
-
- if (await probeServes()) {
- this._vectorVerified = true
- return 'rebuilt'
- }
throw new VectorIndexNotReadyError(
`Vector index reports ${claimed} vector(s) but a known persisted vector returns no ` +
- `results even after a rebuild — semantic find({ query }) cannot be served reliably ` +
- `for this brain (a silent empty result would misrepresent existing data).`
+ `results — the serving structure did not load. Semantic find({ query }) refuses rather ` +
+ `than serve an empty result — rebuild via repairIndex({ rebuild: ['vector'] }) or ` +
+ `reopen the brain.`
)
} catch (err) {
if (err instanceof VectorIndexNotReadyError) throw err
- // A transient probe/rebuild failure must not break the query NOR mask as
+ // A transient probe failure must not break the query NOR mask as
// "no data". Allow a re-check on the next vector read and fall through.
this._vectorVerified = false
if (!this.config.silent) {
@@ -4663,6 +5248,16 @@ export class Brainy implements BrainyInterface {
)
)
+ // Operation 3b: Add the verb's metadata-index row, in the SAME
+ // commit as the graph leg — the live mirror of what rebuild()'s
+ // verb walk already derives (ADR-007 A4: one mechanism, never a
+ // second hand-rolled shape). `verbMetadata` is the exact raw stored
+ // record `SaveVerbMetadataOperation` above just persisted — the same
+ // shape `storage.getVerbMetadata()`/rebuild() read back.
+ tx.addOperation(
+ new AddToMetadataIndexOperation(this.metadataIndex, id, verbMetadata, this.indexWriteGeneration)
+ )
+
// Create bidirectional if requested
if (params.bidirectional && reverseId) {
const reverseVerb: GraphVerb = {
@@ -4700,6 +5295,13 @@ export class Brainy implements BrainyInterface {
(verbInt) => this.cacheVerbInt(verbInt, reverseId)
)
)
+
+ // Operation 6b: Add the reverse edge's metadata-index row (same
+ // stored shape as the primary edge — SaveVerbMetadataOperation
+ // above persists the same `verbMetadata` object for both).
+ tx.addOperation(
+ new AddToMetadataIndexOperation(this.metadataIndex, reverseId, verbMetadata, this.indexWriteGeneration)
+ )
}
},
undefined,
@@ -4782,6 +5384,15 @@ export class Brainy implements BrainyInterface {
)
}
+ // Operation 1b: Retract the verb's metadata-index row — the live
+ // mirror of remove()'s cascade leg (null-metadata-safe; see
+ // metadataIndexRetractionOp's JSDoc). Nothing to retract when the
+ // pre-read found no verb (already gone / never existed).
+ if (verb) {
+ const retractionOp = this.metadataIndexRetractionOp(id, verb, `unrelate(${id})`)
+ if (retractionOp) tx.addOperation(retractionOp)
+ }
+
// Operation 2: Delete verb metadata (which also deletes vector)
tx.addOperation(
new DeleteVerbMetadataOperation(this.storage, id)
@@ -4925,6 +5536,23 @@ export class Brainy implements BrainyInterface {
new UpdateVerbMetadataOperation(this.storage, params.id, updatedMetadata)
)
+ // Re-post the verb's metadata-index row — remove the old shape, add
+ // the new one, same commit (the plain pair; there is no update-op
+ // capability for the metadata leg yet — see the GRAPH leg's
+ // typeChanged branch just below for the capability this ISN'T:
+ // that's the graph adjacency's own remove+add, keyed on the verb
+ // TYPE changing; the metadata row updates on EVERY updateRelation()
+ // call, since metadata/subtype/weight/etc. can all change without a
+ // type change). `existing` is the pre-update hydrated verb (already
+ // read above); `updatedMetadata` is the raw stored record just
+ // persisted — the same shape relate()/rebuild() use to add.
+ tx.addOperation(
+ new RemoveFromMetadataIndexOperation(this.metadataIndex, params.id, Brainy.jsonSafeIndexMetadata(existing), this.indexWriteGeneration)
+ )
+ tx.addOperation(
+ new AddToMetadataIndexOperation(this.metadataIndex, params.id, updatedMetadata, this.indexWriteGeneration)
+ )
+
// If the verb type changed, re-index in graph adjacency so traversal-by-type
// stays consistent. The id is preserved across the swap.
if (typeChanged && reindexInts) {
@@ -6559,14 +7187,10 @@ export class Brainy implements BrainyInterface {
// loader and cold-read probes below already defer to a migrating provider.
await this.ensureInitialized({ needs: [] })
- // Ensure indexes are loaded (lazy loading when disableAutoRebuild: true)
- // This is a production-safe, concurrency-controlled lazy load
- await this.ensureIndexesLoaded()
-
- // One-shot cold-open self-heal: an O(1) probe of the metadata index (when the
- // provider offers one) repairs an already-poisoned index on first read — the
- // metadata counterpart of the graph cold-load guard. No-op for the JS index.
- await this.ensureMetadataConsistencyProbed()
+ // READ-SURFACE READINESS GATE (see filterIdsBelted): a CHECK only — it
+ // never builds. open() already brought every provider to serving before
+ // init() returned; this throws a typed NotReady error if one isn't.
+ this.ensureIndexesLoaded(['metadata'])
// Loudly flag a degraded derived index (failed init rebuild, or an
// adopt-forward degraded commit) so a partial result is never mistaken for
@@ -6578,6 +7202,13 @@ export class Brainy implements BrainyInterface {
let params: FindParams =
typeof query === 'string' ? await this.parseNaturalQuery(query) : query
+ // The vector and graph legs gate only the finds that consult them.
+ const consultsVector = Boolean(
+ (params.query && params.query.trim() !== '') || params.vector || params.near
+ )
+ if (consultsVector) this.ensureIndexesLoaded(['vector'])
+ if (params.connected) this.ensureIndexesLoaded(['graph'])
+
// Id normalization (8.0): resolve the graph-traversal anchor id(s) so a
// caller may constrain by natural key. Each maps to the canonical UUID
// add() stored; real UUIDs pass through. Done once here so every downstream
@@ -7926,6 +8557,11 @@ export class Brainy implements BrainyInterface {
*/
async clear(): Promise {
await this.ensureInitialized()
+ // A clear mutates durable state without going through a commit path, so
+ // it must set the dirty witness itself — otherwise a `clear()` followed by
+ // `flush()` would find the brain "clean" and skip the entity-tree stamp,
+ // leaving a stamp that describes the population this call just removed.
+ this._dirtySinceLastFlush = true
// Clear storage
await this.storage.clear()
@@ -8911,6 +9547,15 @@ export class Brainy implements BrainyInterface {
hook()
}
+ // Leg D — vectored-ledger decrements for this batch's unvector-door
+ // updates (see planTxUpdate's matching comment), applied after the
+ // commit point and properly awaited (unlike `postCommit`'s synchronous
+ // fire-and-forget hooks) — each is the same sanctioned hook
+ // unvectorNounForRootMigration() uses.
+ for (const id of plan.vectorUnlands) {
+ await this.storage.noteVectorUnlanded?.(id)
+ }
+
// Change feed: the batch's events share its single committed generation.
// A rejected batch throws at commitTransaction and never reaches here.
this.emitCommitted(plan.changeEvents, undefined, generation, timestamp)
@@ -9993,6 +10638,18 @@ export class Brainy implements BrainyInterface {
for (const id of nounIds) {
const noun = await snapshotStorage.getNoun(id)
if (noun && Array.isArray(noun.vector) && noun.vector.length > 0) {
+ // THE ZERO-NORM LAW: a direct provider-write seam (this materializer
+ // inserts one-by-one, bypassing AddToVectorIndexOperation's own
+ // belt) — apply the same refusal here rather than handing a false
+ // attractor to the ephemeral reader's index.
+ if (isZeroNormVector(noun.vector)) {
+ prodLog.warn(
+ `[Brainy] materializeAtGeneration: refusing to index a zero-norm vector for ` +
+ `entity ${noun.id} — a zero-norm vector is not a vector and never crosses an ` +
+ `engine boundary (the materialized record is unaffected)`
+ )
+ continue
+ }
await reader.index.addItem({ id: noun.id, vector: noun.vector })
}
}
@@ -10142,7 +10799,8 @@ export class Brainy implements BrainyInterface {
casUpdates: [],
createdNouns: new Set(),
changeEvents: [],
- markerRecords: []
+ markerRecords: [],
+ vectorUnlands: []
}
for (const op of ops) {
@@ -10264,10 +10922,26 @@ export class Brainy implements BrainyInterface {
// marker-less committed row would be a silently missing vector, which is
// the disallowed direction). The background worker embeds + inserts.
const deferringEmbed = params.deferEmbedding === true && !params.vector
- const vector = deferringEmbed
+ let vector = deferringEmbed
? []
: params.vector || (await this.embed(params.data))
- if (!deferringEmbed) {
+
+ // THE ZERO-NORM LAW — see the single-add() insert path's matching
+ // comment (a zero-norm vector is not a vector; never crosses an engine
+ // boundary). Normalized here BEFORE the dimension pin and the
+ // vectored-ledger `hasVector` flag below ever see it.
+ if (!deferringEmbed && vector.length > 0 && isZeroNormVector(vector)) {
+ prodLog.warn(
+ `[Brainy] transact add: entity ${id} was given an explicit all-zero vector — ` +
+ `a zero-norm vector is not a vector; persisted unvectored ([]) instead.`
+ )
+ vector = []
+ }
+
+ // Gated on `vector.length > 0` — see the single-add() insert path's
+ // matching comment: an explicit `vector: []` carries no dimension
+ // information either, deferred or not.
+ if (!deferringEmbed && vector.length > 0) {
if (!this.dimensions) {
this.dimensions = vector.length
} else if (vector.length !== this.dimensions) {
@@ -10345,11 +11019,16 @@ export class Brainy implements BrainyInterface {
plan.postCommit.push(() => this.kickEmbedWorker())
}
plan.operations.push(
- new SaveNounMetadataOperation(this.storage, id, storageMetadata, isNew),
+ // hasVector: see the single-add() insert path's comment — never true
+ // for a deferred embed (stub vector `[]`; counted later at landing).
+ new SaveNounMetadataOperation(this.storage, id, storageMetadata, isNew, vector.length > 0),
new SaveNounOperation(this.storage, { id, vector, connections: new Map(), level: 0 }, isNew),
- ...(deferringEmbed
- ? []
- : [new AddToVectorIndexOperation(this.index, id, vector, this.indexWriteGeneration)]),
+ // Gated on `vector.length > 0` — see the single-add() insert path's
+ // matching comment: an explicit `vector: []` has nothing to index
+ // either, deferred or not.
+ ...(vector.length > 0
+ ? [new AddToVectorIndexOperation(this.index, id, vector, this.indexWriteGeneration)]
+ : []),
new AddToMetadataIndexOperation(this.metadataIndex, id, entityForIndexing, this.indexWriteGeneration)
)
plan.touchedNouns.push(id)
@@ -10420,18 +11099,71 @@ export class Brainy implements BrainyInterface {
// Resolve the updated vector — mirror of update(): an explicit `vector`
// always wins, new `data` re-embeds, otherwise the existing vector is
// kept. Any vector change re-indexes HNSW below.
+ // 'data' is present whenever it's not null/undefined — '' is real
+ // content (see the identical hasNewData in update()); a plain truthy
+ // check would silently skip re-embedding an emptied value and leave a
+ // stale vector with no path to ever correct itself.
+ const rawHasNewData = params.data !== undefined && params.data !== null
+ // No re-embed on unchanged data — the transact() mirror of update()'s rule.
+ const dataUnchanged = rawHasNewData && Brainy.sameEntityData(params.data, existing.data)
+ const hasNewData = rawHasNewData && !dataUnchanged
let vector = existing.vector
- if (params.vector) {
- if (this.dimensions && params.vector.length !== this.dimensions) {
+
+ // THE ZERO-NORM LAW + THE SANCTIONED UNVECTOR DOOR — transact() mirror
+ // of update()'s matching block: an explicit REAL all-zero vector
+ // normalizes to `[]` (never crosses an engine boundary), and an
+ // explicit `vector: []` (post-normalization) is the sanctioned unvector
+ // instruction, exempt from the dimension check. `validateUpdateParams`
+ // already refuses combining it with `deferEmbedding: true`.
+ let explicitVector = params.vector
+ if (explicitVector && explicitVector.length > 0 && isZeroNormVector(explicitVector)) {
+ prodLog.warn(
+ `[Brainy] transact update: entity ${params.id} was given an explicit all-zero ` +
+ `vector — a zero-norm vector is not a vector; persisted unvectored ([]) instead.`
+ )
+ explicitVector = []
+ }
+ const isExplicitUnvector = explicitVector !== undefined && explicitVector.length === 0
+
+ if (explicitVector) {
+ if (explicitVector.length > 0 && this.dimensions && explicitVector.length !== this.dimensions) {
throw new Error(
- `Vector dimension mismatch: expected ${this.dimensions}, got ${params.vector.length}`
+ `Vector dimension mismatch: expected ${this.dimensions}, got ${explicitVector.length}`
)
}
- vector = params.vector
- } else if (params.data) {
+ vector = explicitVector
+ } else if (hasNewData) {
vector = await this.embed(params.data)
}
- const needsReindexing = Boolean(params.data || params.type || params.vector)
+ const needsReindexing = Boolean(hasNewData || params.type || explicitVector)
+
+ // Leg D — the unvector door clears a PENDING deferred-embed marker (see
+ // update()'s matching comment for the full rationale): the durable
+ // clear (an `embed.landed` record, empty vector) rides the batch's ONE
+ // commit fact via `plan.markerRecords`; the in-memory clear is deferred
+ // to `plan.postCommit` so an aborted batch never disarms a marker whose
+ // durable twin was never written.
+ const clearsPendingEmbed = isExplicitUnvector && this._pendingEmbedIds.has(params.id)
+ if (clearsPendingEmbed) {
+ plan.markerRecords.push({ type: 'embed.landed', id: params.id, vector: [] })
+ plan.postCommit.push(() => {
+ this.clearPendingEmbed(params.id)
+ prodLog.warn(
+ `[Brainy] transact update: entity ${params.id} had a pending deferred embed — ` +
+ `the unvector door cleared it ('vector: []' is an explicit instruction, ` +
+ `never "please embed").`
+ )
+ })
+ }
+
+ // Leg D — vectored-ledger decrement for the sanctioned unvector door,
+ // deferred to `plan.vectorUnlands` (consumed with a proper `await` in
+ // `transact()`, AFTER the commit succeeds — see its matching comment).
+ // Gated on the PRIOR vector having actually been real (non-empty,
+ // non-zero-norm): idempotent on an already-unvectored row.
+ if (isExplicitUnvector && existing.vector.length > 0 && !isZeroNormVector(existing.vector)) {
+ plan.vectorUnlands.push(params.id)
+ }
const newMetadata =
params.merge !== false
@@ -10629,6 +11361,15 @@ export class Brainy implements BrainyInterface {
new RemoveFromGraphIndexOperation(this.graphIndex, verb, () => this.resolveVerbEndpointInts(verb), this.graphWriteGeneration),
new DeleteVerbMetadataOperation(this.storage, verb.id)
)
+ // Retract the cascaded relation's metadata-index row too — the
+ // transact() mirror of remove()'s single-op cascade leg
+ // (null-metadata-safe; see metadataIndexRetractionOp's JSDoc).
+ {
+ const cascadeRetractionOp = this.metadataIndexRetractionOp(
+ verb.id, verb, `transact remove(${id}) cascade unrelate ${verb.id}`
+ )
+ if (cascadeRetractionOp) plan.operations.push(cascadeRetractionOp)
+ }
plan.touchedVerbs.push(verb.id)
state.verbs.delete(verb.id)
state.removedVerbs.add(verb.id)
@@ -10795,7 +11536,10 @@ export class Brainy implements BrainyInterface {
// id mapper to assign an int for an entity that did not exist yet.
new AddToGraphIndexOperation(this.graphIndex, verb, () => this.resolveVerbEndpointInts(verb), this.graphWriteGeneration, (verbInt) =>
this.cacheVerbInt(verbInt, id)
- )
+ ),
+ // The transact() mirror of relate()'s metadata-index leg — same
+ // commit as the graph leg, same raw stored shape.
+ new AddToMetadataIndexOperation(this.metadataIndex, id, verbMetadata, this.indexWriteGeneration)
)
plan.touchedVerbs.push(id)
state.verbs.set(id, verb)
@@ -10837,7 +11581,8 @@ export class Brainy implements BrainyInterface {
new SaveVerbMetadataOperation(this.storage, reverseId, verbMetadata),
new AddToGraphIndexOperation(this.graphIndex, reverseVerb, () => this.resolveVerbEndpointInts(reverseVerb), this.graphWriteGeneration, (verbInt) =>
this.cacheVerbInt(verbInt, reverseId)
- )
+ ),
+ new AddToMetadataIndexOperation(this.metadataIndex, reverseId, verbMetadata, this.indexWriteGeneration)
)
plan.touchedVerbs.push(reverseId)
state.verbs.set(reverseId, reverseVerb)
@@ -10882,6 +11627,12 @@ export class Brainy implements BrainyInterface {
// may have been created earlier in this same batch (forward refs).
new RemoveFromGraphIndexOperation(this.graphIndex, verb, () => this.resolveVerbEndpointInts(verb), this.graphWriteGeneration)
)
+ // The transact() mirror of unrelate()'s metadata-index leg
+ // (null-metadata-safe; see metadataIndexRetractionOp's JSDoc — a
+ // present `verb` here is never metadata-omitted, but the closure
+ // stays defensive rather than assuming).
+ const retractionOp = this.metadataIndexRetractionOp(id, verb, `transact unrelate(${id})`)
+ if (retractionOp) plan.operations.push(retractionOp)
}
plan.operations.push(new DeleteVerbMetadataOperation(this.storage, id))
plan.touchedVerbs.push(id)
@@ -11590,6 +12341,31 @@ export class Brainy implements BrainyInterface {
}
}
+ /**
+ * @description Stamp every projection's watermark with the store's
+ * current committed generation — the door BOTH {@link flush} and {@link
+ * close} open right before persisting, so EITHER path leaves a stamped,
+ * `'adopt'`-verdicting artifact on disk (stamp-after-data still holds
+ * inside each owner: this only hands the generation over — the owner's
+ * OWN flush is what durably writes the stamp, LAST). Before this method
+ * existed, `close()` had its own separate flush fan-out that never
+ * stamped, so a `close()` without a preceding explicit `flush()` left
+ * every projection unstamped — a real, closed store that legitimately
+ * verdicts `'rescan'` on its very next open (not a bug in the verdict,
+ * a gap in `close()`'s persistence completeness that this closes).
+ * No `committedGeneration` capability, or a replacement provider that
+ * doesn't carry the stamp method (a native pair swaps these managers) =
+ * no stamp = the owner's verdict machinery treats the artifact as
+ * legacy — never a flush/close crash either way.
+ */
+ private stampProjectionWatermarks(): void {
+ const wmGen = this.storage?.committedGeneration?.() ?? null
+ if (wmGen === null) return
+ ;(this.metadataIndex as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
+ ;(this.index as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
+ ;(this.graphIndex as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
+ }
+
/**
* Flush all indexes and caches to persistent storage
* CRITICAL FIX: Ensures data survives server restarts
@@ -11620,6 +12396,27 @@ export class Brainy implements BrainyInterface {
return
}
+ // A CLEAN BRAIN FLUSHES NOTHING, AND SAYS NOTHING. No write has been
+ // committed since the last flush, so every step below would re-persist
+ // state identical to what is already on disk — provider flushes, the
+ // watermark stamps, the generation counter, the entity-tree stamp — and
+ // print two lines announcing it. The witness is set by every committed
+ // write (see noteWriteForPersistence) and cleared here; a write landing
+ // DURING this flush sets it again, so it is never lost — the next flush
+ // does that write's work. This makes an unexplained flush FREE; it does
+ // not explain one (see _dirtySinceLastFlush).
+ if (!this._dirtySinceLastFlush) {
+ return
+ }
+ this._dirtySinceLastFlush = false
+ // An explicit flush IS a flush: tell the cadence so, or the very next
+ // write sees "30s since the last flush" (the cadence only counted its
+ // own) and kicks a background flush that has nothing left to do, and the
+ // idle timer fires two seconds later over writes this flush already
+ // persisted.
+ this._persistLastFlushAt = Date.now()
+ this._persistDirtyWrites = 0
+
console.log('Flushing Brainy indexes and caches to disk...')
const startTime = Date.now()
@@ -11629,22 +12426,8 @@ export class Brainy implements BrainyInterface {
await this.generationStore.flushPendingSingleOps()
// Flush all components in parallel for performance
- // Watermark stamps ride every flush fan-out: stamp each projection with
- // the committed generation BEFORE its flush persists (stamp-after-data
- // holds inside each owner — the stamp is its LAST write; here we only
- // hand the generation over). No committedGeneration capability = no
- // stamp = the owner's verdict machinery treats the artifact as legacy.
- {
- const wmGen = this.storage?.committedGeneration?.() ?? null
- if (wmGen !== null) {
- // ALL THREE optional-chained: a replacement provider (the native
- // pair swaps these managers) may not carry the stamp method — a
- // missing stamp is a verdict-side rescan, never a flush crash.
- ;(this.metadataIndex as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
- ;(this.index as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
- ;(this.graphIndex as { stampWatermark?: (g: number) => void }).stampWatermark?.(wmGen)
- }
- }
+ // Watermark stamps ride every flush fan-out — see stampProjectionWatermarks().
+ this.stampProjectionWatermarks()
await Promise.all([
// 1. Flush storage adapter counts (entity/verb counts by type)
(async () => {
@@ -11714,6 +12497,18 @@ export class Brainy implements BrainyInterface {
* healed by `repairIndex()`, whose unconditional recount rebuilds the
* rollups from a canonical walk and re-stamps. Best-effort: a stamp-write
* fault warns loudly but never fails the flush that carried real data.
+ *
+ * THE SOURCE IS `committedGeneration()`, NEVER `generation()`. The latter is
+ * the ALLOCATED counter — a number a write in flight has claimed and may
+ * never commit. Stamping it made the stamp's generation label a claim about
+ * counts it was not taken at, and every crash inside a write window then
+ * produced a spurious verdict at the next open: either `sourceGeneration N
+ * is ahead of the log head N-1` (the allocated generation died with the
+ * process) or `rollup invariant 'nounCount': stamped X, observed Y` (the
+ * recovery fold folded facts the stamp's counts predate). MEASURED on the
+ * crash-consistency lane before this line changed: 4 of 11 SIGKILL cycles on
+ * a coherent store raised one of those two verdicts, each of them naming
+ * `repairIndex()` — a whole-store recount — as the cure for nothing.
*/
private async stampEntityTree(): Promise {
if (this.isReadOnly) return
@@ -11724,7 +12519,7 @@ export class Brainy implements BrainyInterface {
])
await writeFamilyStamp(this.storage, ENTITY_TREE_STAMP_PATH, {
family: 'entity-tree',
- sourceGeneration: this.generationStore.generation(),
+ sourceGeneration: this.generationStore.committedGeneration(),
members: { mode: 'rollup', invariants: { nounCount, verbCount } }
})
} catch (error) {
@@ -11737,16 +12532,24 @@ export class Brainy implements BrainyInterface {
/**
* @description Open-time coherence check for the entity tree's family stamp:
- * compare `sourceGeneration` against the log head and the stamped rollup
- * invariants against the live counters. Verdicts:
+ * compare `sourceGeneration` against the store's COMMITTED generation and
+ * the stamped rollup invariants against the live counters. Verdicts:
* - `coherent` / `absent` (legacy store; first flush stamps) → silent.
* - `behind` → benign for the tree (it is written BY the commit; only the
* stamp is stale — a crash landed between commit and flush). Refreshed at
* the next flush.
+ * - `torn` → a TORN GENERATION-LOG TAIL, handled by
+ * {@link demoteTornEntityTreeStamp}: terminal, never a wait.
* - `incoherent` → LOUD: the tree or its counters diverged from what was
* stamped — `repairIndex()` recounts from canonical and re-stamps.
* Never blocks open; a fault reading the stamp is surfaced as unverifiable,
* never conflated with absence.
+ *
+ * THE COMPARISON IS AGAINST `committedGeneration()`, matching what
+ * {@link stampEntityTree} writes and what every other open-time watermark in
+ * this class already reasons about (the fact-scan capability, the metadata /
+ * graph / HNSW watermark verdicts). Comparing against the allocated counter
+ * was the one place that disagreed, and disagreeing was the whole defect.
*/
private async verifyEntityTreeStamp(): Promise {
let stamp: FamilyStamp | null
@@ -11763,11 +12566,16 @@ export class Brainy implements BrainyInterface {
this.storage.getNounCount(),
this.storage.getVerbCount()
])
- const verdict = verifyFamilyStamp(stamp, this.generationStore.generation(), {
+ const verdict = verifyFamilyStamp(stamp, this.generationStore.committedGeneration(), {
nounCount,
verbCount
})
- if (verdict.state === 'incoherent') {
+ if (verdict.state === 'torn') {
+ await this.demoteTornEntityTreeStamp(stamp as FamilyStamp, verdict.stampSource, verdict.head, {
+ nounCount,
+ verbCount
+ })
+ } else if (verdict.state === 'incoherent') {
prodLog.warn(
`[Brainy] entity-tree stamp INCOHERENT at open: ${verdict.failures.join('; ')}. ` +
`The canonical tree or its counters diverged from the stamped state — run ` +
@@ -11781,6 +12589,92 @@ export class Brainy implements BrainyInterface {
}
}
+ /**
+ * @description THE TERMINAL VERDICT for a torn generation-log tail.
+ *
+ * A stamp whose `sourceGeneration` sits ABOVE the store's committed
+ * watermark witnesses a generation that is not in the log: the stamp's fsync
+ * outlived the tail's. By the time this runs, log-authority recovery has
+ * already folded every intact fact above the manifest and advanced the
+ * watermark to cover them — so if the stamp is STILL ahead, the generation
+ * it names is not merely late, it is GONE. There is nothing to wait for.
+ *
+ * That is the whole point of this method. A field report of this class
+ * (single-process store, abrupt termination mid-fold) described a reopen
+ * that narrated the tear and then held 100% CPU with zero log growth for
+ * eight minutes before an operator wiped the directory. A recovery that
+ * cannot say what it is waiting for has no business spinning; the honest
+ * answer here is a verdict, taken now, at O(1) cost.
+ *
+ * WHAT THE VERDICT DOES — the stamped surface is UNUSABLE, so it is
+ * discarded rather than believed: the stamped counts describe a generation
+ * that never became durable, and comparing them against live counters can
+ * only produce noise. The tree itself is not in question (it IS canonical —
+ * every commit writes it, and the fold re-applied every after-image the log
+ * still holds), so the demotion is a re-derivation of this family's verified
+ * surface at the generation the store can actually show:
+ *
+ * - WRITER open → re-stamp at `committedGeneration()` from the live
+ * counters — exactly what the next flush would write, taken now so the
+ * tear cannot re-narrate on every subsequent open. Both count sets are
+ * logged so an operator can see whether anything really moved.
+ * - READER open → a reader cannot re-stamp. Narrate the same terminal
+ * verdict with the named cure and carry on serving; a read-only inspector
+ * is never locked out of a store, and never left waiting either.
+ *
+ * BOUNDEDNESS: straight-line code. No loop, no retry, no await on any
+ * external progress signal — the two counter reads and one stamp write are
+ * the entire cost, and none of them scales with the store.
+ */
+ private async demoteTornEntityTreeStamp(
+ stamp: FamilyStamp,
+ stampSource: number,
+ head: number,
+ observed: { nounCount: number; verbCount: number }
+ ): Promise {
+ const stamped = stamp.members.mode === 'rollup' ? stamp.members.invariants : {}
+ const detail =
+ `[Brainy] TORN GENERATION-LOG TAIL at open: ${ENTITY_TREE_STAMP_PATH} witnesses source ` +
+ `generation ${stampSource} (stamped ${stamp.committedAt}), but the store's committed ` +
+ `generation is ${head} after crash recovery — the stamp's fsync outlived the log tail's, ` +
+ `and generation ${stampSource} is not in the log to arrive. Stamped rollups ` +
+ `${JSON.stringify(stamped)}; observed ${JSON.stringify(observed)}.`
+
+ if (this.isReadOnly) {
+ prodLog.warn(
+ `${detail} This open is READ-ONLY, so the stamp cannot be re-derived: the entity-tree ` +
+ `family stays UNVERIFIED for this session (reads are unaffected — the canonical tree ` +
+ `is the truth this stamp only describes). Cure: open the store with a writer, or run ` +
+ `brain.repairIndex() there, to recount from canonical and re-stamp.`
+ )
+ return
+ }
+
+ const startedAt = Date.now()
+ try {
+ await writeFamilyStamp(this.storage, ENTITY_TREE_STAMP_PATH, {
+ family: 'entity-tree',
+ sourceGeneration: head,
+ members: {
+ mode: 'rollup',
+ invariants: { nounCount: observed.nounCount, verbCount: observed.verbCount }
+ }
+ })
+ prodLog.warn(
+ `${detail} DEMOTED: the unusable stamp was re-derived at committed generation ${head} ` +
+ `from the live counters in ${Date.now() - startedAt}ms — terminal, not a wait. If the ` +
+ `observed counts above look wrong for your data, run brain.repairIndex() to recount ` +
+ `from canonical.`
+ )
+ } catch (error) {
+ prodLog.warn(
+ `${detail} The demotion's re-stamp FAILED (${(error as Error).message}) — the tear will ` +
+ `narrate again at the next open, which is the honest outcome; the store still serves ` +
+ `from canonical. Cure: run brain.repairIndex() to recount from canonical and re-stamp.`
+ )
+ }
+ }
+
/**
* Ask the writer process serving this data directory to flush its in-memory
* indexes to disk, so a read-only inspector can observe fresh state.
@@ -11851,10 +12745,10 @@ export class Brainy implements BrainyInterface {
}
/**
- * Get index loading status (Diagnostic for lazy loading)
+ * Get index loading status (diagnostic)
*
- * Returns detailed information about index population and lazy loading state.
- * Useful for debugging empty query results or performance troubleshooting.
+ * Returns detailed information about index population state. Useful for
+ * debugging empty query results or performance troubleshooting.
*
* @example
* ```typescript
@@ -11863,7 +12757,7 @@ export class Brainy implements BrainyInterface {
* console.log(`Metadata Index: ${status.metadataIndex.entries} entries`)
* console.log(`Graph Index: ${status.graphIndex.relationships} relationships`)
* console.log(`Pending embeds: ${status.projections.semantic.pendingEmbeds}`)
- * console.log(`Lazy rebuild completed: ${status.lazyRebuildCompleted}`)
+ * console.log(`Index build completed at open: ${status.lazyRebuildCompleted}`)
* ```
*/
@@ -11882,8 +12776,9 @@ export class Brainy implements BrainyInterface {
// index read funnels through this helper, so the gate here makes
// serve-while-not-ready UNREPRESENTABLE — a production store once acked
// writes while every non-find() read served empty from a not-ready
- // provider for 15 minutes. Fast path after the latch is one boolean.
- await this.ensureIndexesLoaded()
+ // provider for 15 minutes. A CHECK only — it never builds; throws a typed
+ // NotReady error if a provider's health report says it isn't serving.
+ this.ensureIndexesLoaded(['metadata'])
try {
return await this.metadataIndex.getIdsForFilter(filter, opts)
} catch (err) {
@@ -11895,6 +12790,10 @@ export class Brainy implements BrainyInterface {
async getIndexStatus(): Promise<{
initialized: boolean
+ /** `true` once open()'s index-build-if-needed step has run. Named for API
+ * compatibility with the retired first-query lazy-build path; a needed
+ * rebuild now always runs at open, never deferred to a read, so this is
+ * simply `initialized`'s index-build counterpart. */
lazyRebuildCompleted: boolean
/** Deferred embeds not yet landed (MT5) — the eventual-vector-index backlog. */
pendingEmbeds: number
@@ -12100,21 +12999,49 @@ export class Brainy implements BrainyInterface {
const metadataStats = await this.metadataIndex.getStats()
const graphSize = await this.graphIndex.size()
- // 1. Index size parity. HNSW must hold at least one node per indexed entity.
- if (hnswSize === metadataStats.totalEntries) {
+ // 1. Index size parity. HNSW must hold one node per VECTORED noun — the
+ // vectored-noun ledger (`getCanonicalCounts().vectors.all`), NOT the raw
+ // metadata-entry count: every store's VFS root is PERMANENTLY unvectored
+ // (`vector: []` by design — a zero-norm/empty vector never crosses into
+ // the index, see AddToVectorIndexOperation/JsHnswVectorIndex.rebuild()'s
+ // matching belts), and a not-yet-landed deferred embed is unvectored
+ // too. Comparing against total entries counted the always-unvectored
+ // root as a permanent 1-node "drift" on every VFS-having store — a false
+ // warn on an otherwise perfectly healthy handoff. `vectors.all` is
+ // already the documented coverage denominator for exactly this
+ // comparison (see `CanonicalCounts.vectors`'s JSDoc). Falls back to the
+ // metadata-entry count when the ledger is unavailable or suspect (a
+ // storage adapter without the optional hook, or an unrecounted store) —
+ // never worse than the prior behavior in that case.
+ const vectorLedgerForParity = await this.storage.getCanonicalCounts?.()
+ const vectorParityTarget =
+ vectorLedgerForParity && !vectorLedgerForParity.suspect
+ ? vectorLedgerForParity.vectors.all
+ : metadataStats.totalEntries
+ if (hnswSize === vectorParityTarget) {
checks.push({
name: 'index-parity',
status: 'pass',
- message: `HNSW (${hnswSize}) and metadata index (${metadataStats.totalEntries}) agree.`,
- details: { hnswSize, metadataEntries: metadataStats.totalEntries, graphRelationships: graphSize }
+ message: `HNSW (${hnswSize}) and the vectored-noun ledger (${vectorParityTarget}) agree.`,
+ details: {
+ hnswSize,
+ vectoredNouns: vectorParityTarget,
+ metadataEntries: metadataStats.totalEntries,
+ graphRelationships: graphSize
+ }
})
} else {
- const drift = Math.abs(hnswSize - metadataStats.totalEntries)
+ const drift = Math.abs(hnswSize - vectorParityTarget)
checks.push({
name: 'index-parity',
- status: drift > Math.max(10, metadataStats.totalEntries * 0.01) ? 'fail' : 'warn',
- message: `HNSW (${hnswSize}) and metadata (${metadataStats.totalEntries}) differ by ${drift}. Run a rebuild if the gap is unexpected.`,
- details: { hnswSize, metadataEntries: metadataStats.totalEntries, drift }
+ status: drift > Math.max(10, vectorParityTarget * 0.01) ? 'fail' : 'warn',
+ message: `HNSW (${hnswSize}) and the vectored-noun ledger (${vectorParityTarget}) differ by ${drift}. Run a rebuild if the gap is unexpected.`,
+ details: {
+ hnswSize,
+ vectoredNouns: vectorParityTarget,
+ metadataEntries: metadataStats.totalEntries,
+ drift
+ }
})
}
@@ -14373,8 +15300,9 @@ export class Brainy implements BrainyInterface {
// index read funnels through this helper, so the gate here makes
// serve-while-not-ready UNREPRESENTABLE — a production store once acked
// writes while every non-find() read served empty from a not-ready
- // provider for 15 minutes. Fast path after the latch is one boolean.
- await this.ensureIndexesLoaded()
+ // provider for 15 minutes. A CHECK only — it never builds; throws a typed
+ // NotReady error if a provider's health report says it isn't serving.
+ this.ensureIndexesLoaded(['graph'])
// 8.0 BigInt boundary: unmapped node → no relations.
const nodeInt = this.graphEntityInt(nodeId)
if (nodeInt === undefined) return []
@@ -14983,16 +15911,13 @@ export class Brainy implements BrainyInterface {
// Cold-load guard: an empty connected set is suspicious. The native adjacency can report
// size()>0 (or isReady()===false) on a cold open yet have loaded NO source→target edges — so
- // traversal silently returns []. Re-verify against the honest isReady() signal (or, for older
- // providers, a GLOBAL known-edge sample — NOT the queried anchor, which may be genuinely
- // edgeless). If the adjacency was dead and a rebuild healed it, re-collect; if it stays dead,
- // verifyGraphAdjacencyLive() throws GraphIndexNotReadyError. A genuinely edgeless anchor
- // verifies 'live' and the empty result stands — no spurious rebuild/throw.
+ // traversal would silently return [] as if it were truth. Re-verify against the health-report/
+ // isReady() authority (or, for older providers, a READ-ONLY GLOBAL known-edge sample — NOT the
+ // queried anchor, which may be genuinely edgeless): a dead adjacency throws
+ // GraphIndexNotReadyError here rather than serving the empty set as fact — verifyGraphAdjacencyLive
+ // never rebuilds, so a genuinely edgeless anchor simply verifies 'live' and the empty result stands.
if (connectedIds.size === 0) {
- const verdict = await this.verifyGraphAdjacencyLive()
- if (verdict === 'rebuilt') {
- await populate()
- }
+ await this.verifyGraphAdjacencyLive()
}
// Filter existing results to only connected entities
@@ -15718,6 +16643,160 @@ export class Brainy implements BrainyInterface {
return embeddingManager.isInitialized()
}
+ /**
+ * Whether the process-global WASM embedding engine (all-MiniLM-L6-v2,
+ * fixed 384-dim output, ≈93MB with the bundled model, 90-140s cold compile
+ * on throttled CPUs) is this instance's active embedder — `false` when a
+ * plugin has replaced it via the `'embeddings'` provider key. A native
+ * provider has no such cold-start cost and may use a different output
+ * dimension, so it is never worth avoiding.
+ *
+ * Used by init-path bootstrap writes (the VFS root — see
+ * `VirtualFileSystem.doInitializeRoot()`) to decide whether embedding a
+ * value during `init()` risks paying the WASM engine's cold compile.
+ *
+ * @returns true when the default WASM engine is active (no native
+ * `'embeddings'` provider registered).
+ */
+ usesDefaultWasmEmbedder(): boolean {
+ return !this.pluginRegistry.hasProvider('embeddings')
+ }
+
+ /**
+ * @description LEG C of the zero-norm/unvector-door law — migrate a
+ * legacy zero-norm VFS root BEFORE the vector-leg open gate
+ * ({@link rebuildIndexesIfNeeded}'s `vectorCoverageGap` check) ever
+ * compares the canonical vectored-noun count against the vector index's
+ * size. A pre-fix store may have persisted the VFS root (the fixed
+ * all-zeros UUID) with a REAL all-zero placeholder vector — lawful inside
+ * brainy (`cosineDistance` treats a zero-norm operand as MAXIMUM distance,
+ * see {@link isZeroNormVector}'s JSDoc) but never indexed (the index belt
+ * refuses to insert a zero-norm vector) and never meant to cross an
+ * engine boundary. Left unmigrated, the canonical ledger still counts it
+ * as vectored while the vector index correctly holds nothing for it — a
+ * near-empty store whose ONLY vectored row is this zero-norm root reads
+ * "canonical vectored 1, index size 0" and throws
+ * `VectorIndexNotReadyError` at open, going DARK instead of serving.
+ *
+ * THE LIFECYCLE LAW: nothing at open may scale with brain size. This step
+ * is safe under that law BECAUSE the VFS root lives at a FIXED,
+ * well-known id (`00000000-0000-0000-0000-000000000000` — mirrors
+ * `VirtualFileSystem.VFS_ROOT_ID`; kept as a literal here, the same
+ * convention as the other reserved-root literals in this file and in
+ * `db/factLog.ts`/`db/portableGraph.ts` — `brainy.ts` cannot import
+ * `VirtualFileSystem.ts`, which itself imports `Brainy`) — this is ONE
+ * direct canonical read by id (`storage.getNoun`, the same O(1)
+ * fixed-path lookup {@link unvectorNounForRootMigration} itself uses
+ * internally), NEVER a listing or a walk over `entities/nouns/**`. An
+ * absent root (a store that has never used the VFS) is a no-op, no error.
+ *
+ * Runs UNCONDITIONALLY at every open, independent of whether a
+ * `VirtualFileSystem` is ever constructed this session — the vector-leg
+ * gate this fixes runs during Brainy's OWN init, before any
+ * `VirtualFileSystem` instance exists to run its own lazy migration at
+ * `doInitializeRoot()` (kept in place as the second line of defense for a
+ * VFS actually opened this session — belt AND suspenders, never either
+ * alone).
+ */
+ private async migrateLegacyZeroNormVfsRootIfNeeded(): Promise {
+ const VFS_ROOT_ID = '00000000-0000-0000-0000-000000000000'
+ // TORN-TOLERANT: a torn root record is a recovery-walk healer's job
+ // (see tests/integration/recovery-walk-tolerance.test.ts — an init-time
+ // walk that meets a torn record narrates+counts, via the adapter's own
+ // loud floor at the read site, and heals PAST it; the open itself must
+ // still succeed), not this O(1) migration check's. Skip this open's
+ // migration attempt rather than aborting init(): this leg is a
+ // defensive EXTRA (the index belt + VirtualFileSystem's own
+ // doInitializeRoot() migration still stand as the other lines of
+ // defense), and it retries harmlessly at a later open once the root
+ // heals.
+ let root: HNSWNounWithMetadata | null
+ try {
+ root = await this.storage.getNoun(VFS_ROOT_ID)
+ } catch (err) {
+ if ((err as { code?: string }).code !== 'TORN_RECORD') throw err
+ prodLog.warn(
+ `[Brainy] open(): the VFS root's record is TORN — skipping the zero-norm root ` +
+ `migration check this open (the recovery walk is the healer; this migration ` +
+ `retries harmlessly once the root heals).`
+ )
+ return
+ }
+ if (!root || !Array.isArray(root.vector) || root.vector.length === 0) return
+ if (!isZeroNormVector(root.vector)) return
+ const migrated = await this.unvectorNounForRootMigration(VFS_ROOT_ID)
+ if (migrated) {
+ prodLog.warn(
+ `[Brainy] open(): migrated the VFS root's legacy all-zero placeholder vector to ` +
+ `the unvectored shape (zero-norm vectors never cross an engine boundary) — run ` +
+ `before the vector-leg open gate compares canonical-vectored-count against the ` +
+ `vector index, so a near-empty store never reads a false coverage gap.`
+ )
+ }
+ }
+
+ /**
+ * SANCTIONED, ONE-TIME MIGRATION HOOK — rewrite a canonical noun's
+ * persisted vector from a real (non-empty) vector to the "unvectored"
+ * empty-array shape: the vector record is rewritten to `[]`, the row is
+ * removed from the vector index (if present), and the vectored-noun
+ * ledger (`getCanonicalCounts().vectors.all`) is decremented through the
+ * sanctioned {@link StorageAdapter.noteVectorUnlanded} hook — so the
+ * coverage ledger never silently drifts.
+ *
+ * Exists SOLELY for the VFS root zero-norm migration, called from two
+ * sites that detect the same legacy shape (a persisted root whose vector
+ * is the legacy all-zero placeholder): {@link migrateLegacyZeroNormVfsRootIfNeeded}
+ * (this brain's own init sequence, BEFORE the vector-leg open gate — Leg
+ * C of the zero-norm/unvector-door law) and
+ * `VirtualFileSystem.doInitializeRoot()` (the second line of defense, for
+ * a VFS actually constructed this session). This is NOT the general-
+ * purpose unvector API — ordinary application data uses the sanctioned
+ * unvector DOOR instead (`update({ id, vector: [] })` / the same op inside
+ * `transact()`), which decrements the ledger and clears any pending
+ * deferred-embed marker inline; it does not call this method. Never call
+ * this outside a VFS root migration.
+ *
+ * Idempotent: a noun already unvectored (`vector.length === 0`) or absent
+ * is a no-op — safe to call on every `init()`.
+ *
+ * @param id - The canonical noun id to migrate.
+ * @returns `true` if a migration write happened, `false` if the noun was
+ * already unvectored (or absent) — a no-op.
+ */
+ async unvectorNounForRootMigration(id: string): Promise {
+ const noun = await this.storage.getNoun(id)
+ if (!noun || !Array.isArray(noun.vector) || noun.vector.length === 0) return false
+
+ await this.persistSingleOp({ nouns: [id] }, async (tx) => {
+ // Rewrite the vector leg to the unvectored shape. Placeholder adjacency
+ // (mirrors update()'s own SaveNounOperation staging) — the op preserves
+ // stored graph state when `connections.size === 0`.
+ tx.addOperation(
+ new SaveNounOperation(this.storage, {
+ id,
+ vector: [],
+ connections: new Map(),
+ level: 0
+ })
+ )
+ // Remove from the vector index — safe even if the row was never
+ // actually indexed (RemoveFromVectorIndexOperation's removeItem is a
+ // no-op when the id is absent).
+ tx.addOperation(
+ new RemoveFromVectorIndexOperation(this.index, id, noun.vector, this.indexWriteGeneration)
+ )
+ })
+
+ // Vectored-noun ledger: this migration carries a vector write with no
+ // accompanying metadata operation (metadata is untouched), so the
+ // saveNounMetadata(..., hasVector) seam never fires for it — mirrors the
+ // deferred-embed LANDING path's use of the narrow storage hook, in
+ // reverse.
+ await this.storage.noteVectorUnlanded?.(id)
+ return true
+ }
+
/**
* Setup embedder
*/
@@ -15798,8 +16877,19 @@ export class Brainy implements BrainyInterface {
if (legacyEntityPaths.length === 0) {
// Already flat (root entities, no head-branch entities) → stamp the marker
// so future opens short-circuit. A genuinely empty/fresh dir gets no marker.
- const rootEntities = await probe.listRawObjects('entities')
- if (rootEntities.length > 0) {
+ // "Are there any entities?" is answered by ONE directory read, not by a
+ // recursive listing of every file in the tree: this runs on the open path
+ // of every store that does not yet carry the marker (a restore, a store
+ // built by an older release), and on a large store that listing walks the
+ // whole canonical tree to learn a boolean.
+ const oneLevel = (
+ probe as unknown as { listRawPrefixes?: (prefix: string) => Promise }
+ ).listRawPrefixes
+ const hasRootEntities =
+ typeof oneLevel === 'function'
+ ? (await oneLevel.call(probe, 'entities')).length > 0
+ : (await probe.listRawObjects('entities')).length > 0
+ if (hasRootEntities) {
await probe.writeRawObject('_system/migration-layout.json', {
layout: 'flat-v8',
version: 8,
@@ -16228,111 +17318,112 @@ export class Brainy implements BrainyInterface {
}
/**
- * Ensure indexes are loaded (Production-scale lazy loading)
+ * @description THE READ GATE. Every read choke point (getNeighborUuids,
+ * find, filterIdsBelted, getTypedNeighbors) calls this before touching a
+ * derived index. It is a CHECK, never a build: it asks each of the three
+ * providers (vector, metadata, graph) for its named health verdict via
+ * {@link assessProviderHealth} — the provider's own sync, O(1)
+ * `healthReport()` when exposed, else the `isReady()` / size-heuristic
+ * fallback — and either lets the read proceed or throws the matching typed
+ * `*NotReadyError` naming the provider and its failing reasons. It NEVER
+ * triggers a rebuild and NEVER walks the store: a needed rebuild is
+ * entirely open()'s job (see {@link rebuildIndexesIfNeeded}), which runs to
+ * completion before `init()` returns — so by the time any read reaches
+ * this gate, a healthy provider is already built. A migrating provider is
+ * deferred to exactly as before (it owns its own in-place rebuild).
*
- * Called by query methods (find, search, get, etc.) when disableAutoRebuild is true.
- * Handles concurrent queries safely - multiple calls wait for same rebuild.
- *
- * Performance:
- * - First query: Triggers rebuild (~50-200ms for 1K-10K entities)
- * - Concurrent queries: Wait for same rebuild (no duplicate work)
- * - Subsequent queries: Instant (0ms check, indexes already loaded)
- *
- * Production scale:
- * - 1K entities: ~50ms
- * - 10K entities: ~200ms
- * - 100K entities: ~2s (streaming pagination)
- * - 1M+ entities: Uses chunked lazy loading (per-type on demand)
+ * A report with something worth telling an operator (a failing invariant,
+ * whether serving or not, or a named `unledgered` family) narrates via
+ * `prodLog.warn` ONCE per (provider, `report.generation`) — never once per
+ * read — before any throw decision is made.
*/
- private async ensureIndexesLoaded(): Promise {
- // Fast path: If rebuild already completed, return immediately (0ms)
- if (this.lazyRebuildCompleted) {
- return
+ /**
+ * @description Whether two entity `data` payloads are the same content —
+ * the "no re-embed on unchanged data" comparison. Primitives compare by
+ * value; objects compare structurally with key order normalized.
+ * @param a - The incoming data.
+ * @param b - The stored data.
+ * @returns `true` when the content is identical.
+ */
+ private static sameEntityData(a: unknown, b: unknown): boolean {
+ if (a === b) return true
+ if (a === null || b === null || typeof a !== typeof b) return false
+ if (typeof a !== 'object') return false
+ const stable = (v: unknown): string =>
+ JSON.stringify(v, (_k, val) =>
+ val && typeof val === 'object' && !Array.isArray(val)
+ ? Object.keys(val as Record).sort().reduce((o, k) => {
+ ;(o as Record)[k] = (val as Record)[k]
+ return o
+ }, {} as Record)
+ : val
+ )
+ try { return stable(a) === stable(b) } catch { return false }
+ }
+
+ private ensureIndexesLoaded(
+ families: ReadonlyArray<'vector' | 'metadata' | 'graph'> = ['vector', 'metadata', 'graph']
+ ): void {
+ // PER-FAMILY SCOPE. This gate used to refuse on ANY provider's not-ready
+ // verdict at every read choke point — so a pure metadata find({where})
+ // was refused because the VECTOR leg was not serving; a production
+ // deployment's badge reads returned 500s for exactly that reason on the
+ // pair's first adoption. A read may only be refused by the family it
+ // actually consults: metadata reads by the metadata leg (+ graph for a
+ // `connected` filter), vector search by the vector leg, traversal by the
+ // graph leg. Callers name what they need.
+ const all: ReadonlyArray BrainyError]> = [
+ ['vector', this.index, VectorIndexNotReadyError],
+ ['metadata', this.metadataIndex, MetadataIndexNotReadyError],
+ ['graph', this.graphIndex, GraphIndexNotReadyError]
+ ]
+ const providers = all.filter(([name]) => families.includes(name))
+
+ for (const [name, provider, ErrorClass] of providers) {
+ // Migration LOCK (#18) deference: a migrating provider owns its own
+ // in-place rebuild — brainy must not judge (or race) it here.
+ if (this.providerIsMigrating(provider)) continue
+
+ const assessment = assessProviderHealth(provider)
+
+ if (assessment.reasons.length > 0 && assessment.report != null) {
+ const generation = assessment.report.generation
+ // Dedupe by CONTENT, not by the provider's generation counter — see
+ // _lastNarratedHealth. The generation is still REPORTED (an operator
+ // wants to know which generation produced the verdict); it just no
+ // longer decides whether the line is worth saying.
+ const line =
+ `[Brainy] ${assessment.report.provider} health (generation ${generation}): ` +
+ assessment.reasons.join('; ')
+ const key = `${assessment.report.provider}\u0000${assessment.reasons.join('; ')}`
+ if (this._lastNarratedHealth.get(provider) !== key) {
+ this._lastNarratedHealth.set(provider, key)
+ prodLog.warn(line)
+ }
+ }
+
+ if (assessment.readiness === 'not-ready') {
+ // A provider REBUILDING ITSELF gets a refusal that says so, with its
+ // own progress: open deliberately did not wait for it, this door is
+ // temporarily closed, and it opens by itself. Distinct from a broken
+ // index, which needs an operator.
+ const rebuilding = assessProviderRebuild(provider)
+ if (rebuilding) {
+ throw new ErrorClass(
+ `${name} index is ${describeRebuildProgress(rebuilding)} and is not serving yet. ` +
+ `Reads of this family refuse rather than serve an empty result. The brain is open ` +
+ `and every other family is serving; this door opens by itself when the provider ` +
+ `reports serving — no action is needed.`
+ )
+ }
+ throw new ErrorClass(
+ `${name} index is not serving (via ${assessment.via}): ` +
+ `${assessment.reasons.join('; ') || 'not ready'}. Reads refuse rather than serve an ` +
+ `empty result — open() builds the derived indexes; a read never does. Rebuild via ` +
+ `repairIndex({ rebuild: ['${name}'] }) or reopen the brain.`
+ )
+ }
}
-
- // If indexes already populated AND honestly serving, mark complete and skip.
- // Honest gate: when a provider exposes isReady(), that REPLACES the size()>0
- // proxy (a native index can report a non-zero size while its serving structure
- // is not loaded — the silent-empty cold-load class). A not-ready provider falls
- // through so the rebuild path can load it; verifyVectorLive() is the query-time
- // backstop either way. Providers without isReady() keep the size() heuristic
- // (the JS index's size()>0 genuinely means loaded).
- //
- // ALL THREE providers vote (fleet-adoption find, SELF-ENGINE-PAIR-STANDARD):
- // this gate used to assess ONLY the vector index, so a not-ready native
- // METADATA provider (its strand report) never blocked the completion latch
- // — under disableAutoRebuild the promised lazy first-query rebuild never
- // fired and every find() silently returned [] on a populated store. A
- // not-ready report from ANY provider now falls through to the rebuild.
- const vectorReadiness = assessIndexReadiness(this.index)
- const metadataReadiness = assessIndexReadiness(this.metadataIndex)
- const graphReadiness = assessIndexReadiness(this.graphIndex)
- const anyProviderNotReady =
- vectorReadiness === 'not-ready' ||
- metadataReadiness === 'not-ready' ||
- graphReadiness === 'not-ready'
- if (
- !anyProviderNotReady &&
- (vectorReadiness === 'ready' || (vectorReadiness === 'unknown' && this.index.size() > 0))
- ) {
- this.lazyRebuildCompleted = true
- return
- }
-
- // Migration LOCK (#18) deference: while the vector provider runs its one-time
- // 7.x → 8.0 rebuild-from-canonical, a first query must NOT trigger brainy's
- // force-rebuild — the provider owns that index. Normally unreachable here: the
- // data-plane lock (awaitMigrationLock) makes the caller wait upstream, so a
- // query only reaches this point once the migration has cleared. Defensive
- // (no `lazyRebuildCompleted` latch) so the check re-runs: once the provider
- // clears the lock, `index.size() > 0` above ends the lazy path normally.
- if (this.providerIsMigrating(this.index)) {
- return
- }
-
- // Concurrency control: If rebuild is in progress, wait for it
- if (this.lazyRebuildInProgress && this.lazyRebuildPromise) {
- await this.lazyRebuildPromise
- return
- }
-
- // Check if lazy rebuild is needed
- // Only needed if: disableAutoRebuild=true AND indexes are empty AND storage has data
- if (!this.config.disableAutoRebuild) {
- // Auto-rebuild is enabled, indexes should already be loaded
- return
- }
-
- // Check if storage has data (fast check with limit=1)
- const entities = await this.storage.getNouns({ pagination: { limit: 1 } })
- const hasData = (entities.totalCount && entities.totalCount > 0) || entities.items.length > 0
-
- if (!hasData) {
- // Storage is empty, no rebuild needed
- this.lazyRebuildCompleted = true
- return
- }
-
- // Start lazy rebuild (with mutex to prevent concurrent rebuilds).
- // ALWAYS narrated (prodLog, never the silent-suppressible console): a
- // read that triggers an index build must be visible to the operator —
- // fifteen silent minutes of a production blackout taught this line.
- prodLog.warn(
- `[Brainy] first read on this instance is building the derived indexes ` +
- `(deferred at open by disableAutoRebuild) — reads WAIT and then serve; ` +
- `nothing serves empty. Bounded by store size; progress under [MetadataIndex]/[GraphIndex].`
- )
- this.lazyRebuildInProgress = true
- this.lazyRebuildPromise = this.rebuildIndexesIfNeeded(true)
- .then(() => {
- this.lazyRebuildCompleted = true
- })
- .finally(() => {
- this.lazyRebuildInProgress = false
- this.lazyRebuildPromise = null
- })
-
- await this.lazyRebuildPromise
}
/**
@@ -16392,7 +17483,186 @@ export class Brainy implements BrainyInterface {
}
/**
- * Rebuild indexes from persisted data if needed (LAZY LOADING)
+ * @description Consume the JS metadata index's watermark verdict (see
+ * {@link MetadataIndexManager.watermarkVerdict}) at open — the coordinator
+ * half of the catchup wiring; {@link MetadataIndexManager.applyWatermarkCatchup}
+ * is the mechanism half. Feature-detected to the JS manager only: a native
+ * metadata-index provider consumes the same verdict door in its own train
+ * (this method never touches the native-provider wrapper contract).
+ *
+ * Ordering: called from `performInit()` immediately after
+ * `metadataIndex.init()` has computed the verdict against the generation
+ * store's now-FINAL committed generation, and BEFORE `rebuildIndexesIfNeeded()`
+ * (the open-time rebuild gate) or any read serves — so a caller can never
+ * observe the pre-catchup state.
+ *
+ * @param alreadyRebuilt - `true` when crash recovery just rebuilt every
+ * index from canonical (rolled-back uncommitted transactions) — the
+ * verdict's prescribed action is redundant with what already ran (a
+ * fresh canonical walk supersedes any catchup fold or rescan), so it is
+ * skipped, narrated, rather than duplicating the work.
+ */
+ private async consumeMetadataWatermarkVerdict(alreadyRebuilt: boolean): Promise {
+ if (!(this.metadataIndex instanceof MetadataIndexManager)) return
+ const verdict = this.metadataIndex.watermarkVerdict()
+ if (verdict === null || verdict === 'adopt') return
+
+ if (alreadyRebuilt) {
+ prodLog.info(
+ `[Brainy] metadata index watermark verdict '${verdict}' at open — skipped: crash ` +
+ `recovery already rebuilt every index from canonical this open.`
+ )
+ return
+ }
+
+ const window = this.metadataIndex.watermarkGap()
+ // A genuine first boot (no persisted artifact at all) verdicts 'rescan'
+ // too — same as a real unverifiable artifact — but it is routine, not
+ // alarming: narrate it at info level instead of warn (mirrors the
+ // manager's own internal distinction in loadWatermarkVerdict()).
+ const firstBoot = verdict === 'rescan' && !this.metadataIndex.watermarkArtifactPresent()
+ const preNarrate = firstBoot ? prodLog.info.bind(prodLog) : prodLog.warn.bind(prodLog)
+ preNarrate(
+ verdict === 'catchup' && window
+ ? `[Brainy] metadata index watermark verdict: CATCHUP — folding generations ` +
+ `(${window.from}, ${window.to}] from the fact log before this open serves reads.`
+ : firstBoot
+ ? `[Brainy] metadata index watermark verdict: rescan (no persisted artifact — first ` +
+ `boot; the rebuild below is a trivial no-op walk).`
+ : `[Brainy] metadata index watermark verdict: RESCAN — the persisted artifact is ` +
+ `unverifiable (unstamped, or ahead of the store's committed generation); ` +
+ `forcing a full rebuild from canonical at open.`
+ )
+
+ const scan = window
+ ? this.scanFacts({ fromGeneration: window.from + 1, toGeneration: window.to })
+ : null
+ const result = await this.metadataIndex.applyWatermarkCatchup(scan)
+
+ if (result.action === 'rescan') {
+ const postNarrate = firstBoot ? prodLog.debug.bind(prodLog) : prodLog.warn.bind(prodLog)
+ postNarrate(
+ `[Brainy] metadata index catchup demoted to a full rebuild` +
+ `${result.reason ? ` — ${result.reason}` : ''}.`
+ )
+ } else if (result.action === 'caught-up') {
+ prodLog.warn(
+ `[Brainy] metadata index catchup complete: ${result.factsApplied} fact(s) folded ` +
+ `(${result.nounsApplied} noun op(s), ${result.verbsApplied} verb op(s)) — index now ` +
+ `reflects generation ${result.window?.to}.`
+ )
+ }
+ }
+
+ /**
+ * @description B3 Deliverable 3 — THE ONLINE METADATA REBUILD.
+ * `repairIndex()`'s ceremony door for the `'metadata'` family routes here
+ * instead of calling `MetadataIndexManager.rebuild()` directly: build a
+ * FRESH replacement manager BESIDE the live one (same storage, same
+ * idMapper — identity is shared, never a second mapper), walk canonical
+ * into it while every live write during the build ALSO mirrors there
+ * (`MetadataIndexManager.beginShadow`), fold the generation window the
+ * walk may have read stale, then atomically swap this brain's reference —
+ * `this.metadataIndex` points at the OLD manager for the ENTIRE build, so
+ * every read in progress (and every read that starts before the swap
+ * line executes) keeps serving its full, unbuilt-adjacent population;
+ * nothing ever observes a half-built index.
+ *
+ * PERSISTENCE CHOICE (named per the B3 brief): the JS manager's persisted
+ * keys (field-index chunks, column-store segments, the watermark stamp,
+ * the id-mapper record) are GLOBAL per storage — not namespaced per
+ * manager instance — so two managers cannot safely persist independently
+ * mid-build (a segment-number race, a stamp race, an id-mapper reload
+ * that would discard the live manager's not-yet-flushed assignments —
+ * see `MetadataIndexManager.initForShadowBuild`'s JSDoc for the id-mapper
+ * hazard specifically). This build therefore PERSISTS ONLY AT SWAP: the
+ * shadow builds entirely in memory (`rebuild({ inMemoryOnly: true })` +
+ * a fact-log fold — neither touches storage) and flushes exactly once,
+ * after the swap, as the sole owner of the shared keys.
+ *
+ * FALLBACK: a store with no fact log (or a non-JS/native metadata
+ * provider — its own train owns its online-rebuild strategy) cannot
+ * safely bound "what landed during the walk"; this method falls back to
+ * the ORIGINAL blocking clear-then-walk `rebuild()`, narrated.
+ */
+ private async rebuildMetadataIndexOnline(): Promise {
+ if (!(this.metadataIndex instanceof MetadataIndexManager)) {
+ // A registered provider (e.g. a native accelerator) may replace
+ // `this.metadataIndex` with a non-MetadataIndexManager object at
+ // runtime even though the field's declared type is the JS class —
+ // the cast mirrors the same reach-in used elsewhere in this file
+ // (e.g. checkHealth()'s `metadataProvider` locals) for exactly this.
+ const provider = this.metadataIndex as unknown as MetadataIndexProvider
+ await provider.rebuild()
+ return
+ }
+
+ const committedAtStart = this.storage.committedGeneration?.() ?? null
+ const factLogAvailable = committedAtStart !== null && this.scanFacts() !== null
+ if (!factLogAvailable) {
+ prodLog.warn(
+ `[Brainy] repairIndex(): metadata rebuild — no fact log on this store, build-beside ` +
+ `is unavailable; falling back to the blocking rebuild (reads may serve a ` +
+ `partially-built index for its duration).`
+ )
+ await this.metadataIndex.rebuild()
+ return
+ }
+
+ prodLog.warn(
+ `[Brainy] repairIndex(): metadata rebuild — building a fresh replacement index BESIDE ` +
+ `the live one (reads keep serving the current index throughout); swapping in ` +
+ `atomically once it is caught up.`
+ )
+ const startedAt = Date.now()
+ const oldManager = this.metadataIndex
+ const shadow = new MetadataIndexManager(this.storage, {}, {
+ entityIdMapper: oldManager.getIdMapper()
+ })
+
+ oldManager.beginShadow(shadow)
+ let committedAtSwap: number
+ try {
+ await shadow.buildBeside(committedAtStart!)
+ // Capture the true final generation right before the swap — a
+ // synchronous read, no `await` between here and the reference
+ // assignment below, so nothing can land ungoverned in the gap: the
+ // shadow has been live-mirroring every write since beginShadow()
+ // above, and this generation is the floor a FUTURE open's watermark
+ // verdict will trust once stamped.
+ committedAtSwap = this.storage.committedGeneration?.() ?? committedAtStart!
+ } catch (err) {
+ oldManager.endShadow()
+ prodLog.error(
+ `[Brainy] repairIndex(): online metadata rebuild FAILED during the walk/fold — the ` +
+ `live index is UNCHANGED (never swapped); reads keep serving the current ` +
+ `(pre-rebuild) metadata index. Error: ${(err as Error).message}`
+ )
+ throw err
+ }
+
+ oldManager.endShadow()
+ this.metadataIndex = shadow
+
+ // NOW persist — the shadow is the SOLE owner of the shared storage keys
+ // (nothing references `oldManager` any more; it never flushes again).
+ shadow.stampWatermark(committedAtSwap)
+ await shadow.flush()
+
+ prodLog.warn(
+ `[Brainy] repairIndex(): online metadata rebuild complete in ${Date.now() - startedAt}ms — ` +
+ `swapped in a fresh index reflecting generation ${committedAtSwap}, zero read downtime.`
+ )
+ }
+
+ /**
+ * @description Rebuild indexes from persisted data if needed — THE OPEN-TIME
+ * BUILD. Called once per open (init calls it; `repairIndex()`'s
+ * write-quarantine lift calls it forced). Runs to completion BEFORE `init()`
+ * returns: a needed rebuild is NEVER deferred to a read (there is no more
+ * first-query lazy path — see {@link ensureIndexesLoaded}, which is a
+ * read-time CHECK only). `disableAutoRebuild` no longer defers index
+ * construction to the first query; see its JSDoc in `brainy.types.ts`.
*
* FIXES FOR CRITICAL BUGS:
* - Bug #1: GraphAdjacencyIndex rebuild never called ✅ FIXED
@@ -16402,34 +17672,24 @@ export class Brainy implements BrainyInterface {
*
* Production-grade rebuild with:
* - Handles BILLIONS of entities via streaming pagination
- * - Smart threshold-based decisions (auto-rebuild < 1000 items)
- * - Lazy loading on first query (when disableAutoRebuild: true)
+ * - A provider's named {@link HealthReport} (when it exposes one) decides
+ * per-leg need; `isReady()` / a size heuristic decides otherwise — no
+ * dataset-size threshold gates whether the rebuild runs at open.
* - Progress reporting for large datasets
* - Parallel index rebuilds for performance
* - Robust error recovery (continues on partial failures)
- * - Concurrency-safe (multiple queries wait for same rebuild)
*
- * @param force - Force rebuild even if disableAutoRebuild is true (for lazy loading)
+ * @param force - Force the rebuild path to run even when no leg reports a need (used by `repairIndex()`'s write-quarantine lift).
*/
private async rebuildIndexesIfNeeded(force = false): Promise {
try {
- // Check if auto-rebuild is explicitly disabled (ONLY during init, not for lazy loading)
- // force=true means this is a lazy rebuild triggered by first query
- if (this.config.disableAutoRebuild === true && !force) {
- if (!this.config.silent) {
- console.log('⚡ Auto-rebuild explicitly disabled via config')
- console.log('💡 Indexes will build automatically on first query (lazy loading)')
- }
- return
- }
-
// No instant fast-path here: the honest per-leg readiness checks below
- // are all O(1) (one bounded storage sample + each provider's size()/
- // isReady()), and this method runs exactly once per open (init calls it;
- // the lazy path passes force=true). The removed shortcut keyed off
- // `this.index.size() > 0`, a dishonest proxy — it skipped the metadata
- // and graph checks whenever the vector happened to be warm, and it never
- // fired on a real cold process (the JS vector size is 0 until it loads).
+ // are all O(1) (one bounded storage sample + each provider's health
+ // report / size()/isReady()), and this method runs exactly once per
+ // open. The removed shortcut keyed off `this.index.size() > 0`, a
+ // dishonest proxy — it skipped the metadata and graph checks whenever
+ // the vector happened to be warm, and it never fired on a real cold
+ // process (the JS vector size is 0 until it loads).
// BUG #2 FIX: Don't trust counts - check actual storage instead
// Counts can be lost/corrupted in container restarts
@@ -16448,30 +17708,23 @@ export class Brainy implements BrainyInterface {
return
}
- // Intelligent decision: Auto-rebuild based on dataset size
- // Production scale: Handles billions via streaming pagination
- const AUTO_REBUILD_THRESHOLD = 10000 // Auto-rebuild if < 10K items (increased from 1K)
-
// Check if indexes need rebuilding
const metadataStats = await this.metadataIndex.getStats()
const hnswIndexSize = this.index.size()
- // Readiness contract: when a provider exposes isReady(), that honest
- // signal REPLACES the size/count heuristic below — an mmap/disk-native
- // index legitimately reports 0 resident entries while fully durable on
- // disk, and rebuilding it from canonical re-reads every entity file on
- // every boot (the 48-seconds-per-restart class a production deployment
- // hit). The signal is honest in BOTH directions: a provider whose
- // durable state failed to load returns false and gets its rebuild even
- // when size() > 0 (the silent-empty cold-load failure). Providers
- // without isReady() keep the exact prior empty-heuristics.
- const providerReady = (leg: unknown): boolean | undefined => {
- const candidate = leg as { isReady?: () => boolean }
- return typeof candidate.isReady === 'function' ? candidate.isReady() : undefined
+ // Readiness contract: a provider's named {@link HealthReport} (when
+ // exposed) is the authority — `serving === false` needs the rebuild,
+ // full stop. Absent a health report, fall back to `isReady()` (an
+ // mmap/disk-native index legitimately reports 0 resident entries while
+ // fully durable on disk, so rebuilding it from canonical on every boot
+ // would be the 48-seconds-per-restart class a production deployment
+ // hit); absent BOTH, keep the per-leg empty-heuristic passed in.
+ const legNeedsRebuild = (provider: unknown, emptyFallback: boolean): boolean => {
+ const assessment = assessProviderHealth(provider)
+ if (assessment.via === 'health-report') return assessment.readiness !== 'ready'
+ if (assessment.via === 'is-ready') return assessment.readiness === 'not-ready'
+ return emptyFallback
}
- const metadataReady = providerReady(this.metadataIndex)
- const vectorReady = providerReady(this.index)
- const graphReady = providerReady(this.graphIndex)
// Epoch-drift trigger: a format-version change makes EVERY derived index
// suspect even when each is non-empty, so it forces a rebuild of all
@@ -16485,15 +17738,43 @@ export class Brainy implements BrainyInterface {
// by awaitMigrationLock meanwhile (nothing serves from a half-built index).
// Gated per-index, so a non-migrating sibling still rebuilds when it needs
// to; a migrating provider is skipped even under epoch-drift or size()===0.
- const metadataMigrating = this.providerIsMigrating(this.metadataIndex)
- const vectorMigrating = this.providerIsMigrating(this.index)
- const graphMigrating = this.providerIsMigrating(this.graphIndex)
+ // SELF-REBUILD DEFERENCE (the sibling of the migration lock, and the
+ // reason a production open took 641 seconds): a provider that reports
+ // `rebuildInProgress()` is ALREADY rebuilding its own index. Brainy must
+ // neither start a second rebuild nor WAIT for the provider's — init()
+ // returns, every other family serves, and that family's own doors refuse
+ // by name (carrying this progress) until the provider reports serving.
+ // A provider without the hook behaves exactly as before.
+ const metadataRebuilding = assessProviderRebuild(this.metadataIndex)
+ const vectorRebuilding = assessProviderRebuild(this.index)
+ const graphRebuilding = assessProviderRebuild(this.graphIndex)
+ for (const [leg, progress] of [
+ ['metadata', metadataRebuilding],
+ ['vector', vectorRebuilding],
+ ['graph', graphRebuilding]
+ ] as const) {
+ if (progress) {
+ prodLog.narrate(
+ `[Brainy] open(): the ${leg} provider is ${describeRebuildProgress(progress)} — ` +
+ `open does NOT wait for it. The brain opens now, every other family serves, and ` +
+ `${leg} reads refuse by name until the provider reports itself serving.`
+ )
+ }
+ }
+
+ const metadataMigrating =
+ this.providerIsMigrating(this.metadataIndex) || metadataRebuilding !== null
+ const vectorMigrating = this.providerIsMigrating(this.index) || vectorRebuilding !== null
+ const graphMigrating = this.providerIsMigrating(this.graphIndex) || graphRebuilding !== null
+ // The epoch stamp certifies EVERY derived index, so it must not advance
+ // while any family is still being built — by a migration lock or by the
+ // provider itself.
const anyMigrating = metadataMigrating || vectorMigrating || graphMigrating
// Per-leg decision, in precedence order: a migrating provider owns its
- // index (skip) → epoch drift forces a rebuild → an exposed isReady()
- // decides → otherwise a per-leg fallback. The fallbacks differ by leg
- // because "empty" means different things:
+ // index (skip) → epoch drift forces a rebuild → the health-report/
+ // isReady() authority decides → otherwise a per-leg fallback. The
+ // fallbacks differ by leg because "empty" means different things:
// - METADATA: past the empty-store early-return, entities exist, so the
// id-mapper SHOULD have loaded entries — totalEntries===0 is a real
// load-failure signal, so rebuild (self-heal from canonical).
@@ -16504,62 +17785,132 @@ export class Brainy implements BrainyInterface {
// against canonical) inside storage.getGraphIndex() BEFORE this gate,
// so it is already authoritative here; re-deriving would be spurious
// (a full O(E) verb scan on every open of an edgeless brain). It
- // therefore rebuilds only on epoch drift or a native !isReady().
- // (verifyGraphAdjacencyLive is the query-time backstop.)
+ // therefore rebuilds only on epoch drift or a native !isReady()/
+ // not-serving report. (verifyGraphAdjacencyLive is the query-time
+ // backstop — it refuses loudly, it never rebuilds.)
const shouldRebuildMetadata =
!metadataMigrating &&
- (epochStale ||
- (metadataReady !== undefined ? !metadataReady : metadataStats.totalEntries === 0))
- const shouldRebuildVector =
+ (epochStale || legNeedsRebuild(this.metadataIndex, metadataStats.totalEntries === 0))
+
+ // VECTOR LEG — the two-engine gate's last red: a migrated 7.x-era store
+ // can hold canonical vectored nouns with NO derived vector index built.
+ // `legNeedsRebuild`'s size-heuristic fallback (below) only fires off
+ // `hnswIndexSize === 0`, and its health-report branch trusts a
+ // provider's own `serving` verdict verbatim — but a provider's health
+ // report can legitimately say `serving: true` while vector coverage is
+ // honestly UNLEDGERED on ITS side too (an unledgered invariant never
+ // flips serving), so neither signal alone can tell "genuinely empty"
+ // apart from "never built". The canonical vectored-noun ledger
+ // (`getCanonicalCounts().vectors.all` — Deliverable 1) is the
+ // denominator that CAN tell them apart, and is compared here:
+ // - a CONFIDENT (non-suspect) ledger `> 0` while the reported node
+ // count is 0 is a proven coverage gap — force the build regardless
+ // of what a health report claims;
+ // - a CONFIDENT ledger `=== 0` while the node count is 0 proves there
+ // is nothing to load (e.g. every noun's embed is still deferred) —
+ // skip the size-heuristic fallback's blunt "always rebuild when
+ // empty" trigger, which otherwise wastes a full canonical walk for
+ // zero benefit on every cold open of such a store;
+ // - an unavailable/suspect ledger changes nothing — loud errors never
+ // quiet losses, so a doubtful ledger must never suppress a rebuild
+ // the old heuristic would have run.
+ // The bare `isReady()` boolean (no report, no `unledgered` concept) is
+ // NOT overridden — that signal is what fixed the 48-seconds-per-restart
+ // regression pinned in tests/unit/cold-open-rebuild-gate.test.ts (a
+ // disk-native provider legitimately reporting 0 resident while durable
+ // on disk), and re-deriving it from a denominator the provider itself
+ // has no way to consult would reopen exactly that regression.
+ const vectorAssessment = assessProviderHealth(this.index)
+ const vectorLedger = await this.storage.getCanonicalCounts?.()
+ const vectorLedgerAll = vectorLedger?.vectors.all
+ const vectorLedgerConfident = vectorLedger !== undefined && !vectorLedger.suspect
+ const vectorHasCoverageProof = vectorLedgerConfident && (vectorLedgerAll as number) > 0
+ const vectorConfirmedEmpty = vectorLedgerConfident && vectorLedgerAll === 0
+
+ let vectorNeedsRebuild: boolean
+ if (vectorAssessment.via === 'is-ready') {
+ // Bare isReady() stays authoritative and UNMODIFIED — see above.
+ vectorNeedsRebuild = vectorAssessment.readiness === 'not-ready'
+ } else if (vectorAssessment.via === 'health-report') {
+ vectorNeedsRebuild =
+ vectorAssessment.readiness !== 'ready' ||
+ (hnswIndexSize === 0 && vectorHasCoverageProof)
+ } else {
+ // size-heuristic / no provider (the built-in JS engine's own posture).
+ vectorNeedsRebuild = hnswIndexSize === 0 && !vectorConfirmedEmpty
+ }
+
+ const shouldRebuildVector = !vectorMigrating && (epochStale || vectorNeedsRebuild)
+
+ // Narration (and the FAIL-TYPED backstop below) are scoped EXACTLY to
+ // the defect this gate closes: a provider whose OWN health report
+ // claims `serving: true` — an affirmative "I am ready" a caller would
+ // otherwise trust outright — while the canonical ledger proves vector
+ // coverage is missing. This is deliberately NARROWER than "any branch
+ // where the ledger contributed to the decision":
+ // - the bare isReady() branch is untouched, as above (never in scope);
+ // - the health-report branch's OWN `readiness !== 'ready'` case is
+ // already an ordinary, PRE-EXISTING rebuild trigger (the provider
+ // admits not-ready) — not a ledger override, so not a "gap";
+ // - the size-heuristic/no-provider branch's rebuild-when-empty is the
+ // SAME blunt trigger the code always had (`hnswIndexSize === 0`)
+ // — the ledger only ever SUPPRESSES a rebuild there (the confirmed-
+ // empty case), it never forces one the old heuristic wouldn't
+ // already have run. Marking that branch a "gap" too made the
+ // FAIL-TYPED backstop fire on ordinary white-box tests that stub
+ // rebuild() as a no-op and pin `size()` at 0 to drive OTHER
+ // assertions (e.g. migration-deference's isMigrating() coverage) —
+ // those are not silent-empty defects, so they must open exactly as
+ // before (tests/unit/brainy/migration-deference.test.ts).
+ const vectorCoverageGap =
!vectorMigrating &&
- (epochStale || (vectorReady !== undefined ? !vectorReady : hnswIndexSize === 0))
+ vectorAssessment.via === 'health-report' &&
+ vectorAssessment.readiness === 'ready' &&
+ hnswIndexSize === 0 &&
+ vectorHasCoverageProof
+ if (vectorCoverageGap) {
+ prodLog.warn(
+ `[Brainy] open(): vector index reports ${hnswIndexSize} node(s) but the canonical ` +
+ `ledger holds ${vectorLedgerAll} vectored noun(s) — the derived vector index is ` +
+ `missing or unbuilt on this store. Forcing the vector rebuild rather than serving ` +
+ `silent-empty search results.`
+ )
+ }
+
const shouldRebuildGraph =
!graphMigrating &&
- (epochStale || (graphReady !== undefined ? !graphReady : false))
+ (epochStale || legNeedsRebuild(this.graphIndex, false))
const needsRebuild = shouldRebuildMetadata || shouldRebuildVector || shouldRebuildGraph
if (!needsRebuild && !force) {
- // All indexes report current — durably loaded (isReady/size), or owned
- // by a background migration. No rebuild needed.
+ // All indexes report current — durably loaded (health-report/isReady/
+ // size), or owned by a background migration. No rebuild needed.
return
}
- // Determine rebuild strategy
- const isLazyRebuild = force && this.config.disableAutoRebuild === true
- const isSmallDataset = totalCount < AUTO_REBUILD_THRESHOLD
- const shouldRebuild = isLazyRebuild || isSmallDataset || this.config.disableAutoRebuild === false
+ // Name exactly which legs rebuild — "all indexes" was a lie whenever
+ // the durable legs were skipped (e.g. only the JS vector index loads
+ // here on a warm reopen), and it misread as a whole-brain rebuild in
+ // consumer boot logs.
+ const rebuildingLegs = [
+ shouldRebuildMetadata && 'metadata',
+ shouldRebuildVector && 'vector',
+ shouldRebuildGraph && 'graph'
+ ]
+ .filter(Boolean)
+ .join(' + ')
- if (!shouldRebuild) {
- // Large dataset with auto-rebuild disabled: Wait for lazy loading
- if (!this.config.silent) {
- console.log(`⚡ Large dataset (${totalCount.toLocaleString()} items) - using lazy loading for optimal startup`)
- console.log('💡 Indexes will build automatically on first query')
- }
- return
- }
-
- // REBUILD: Either small dataset, forced rebuild, or explicit enable
- const rebuildReason = isLazyRebuild
- ? '🔄 Lazy loading triggered by first query'
- : isSmallDataset
- ? `🔄 Small dataset (${totalCount.toLocaleString()} items)`
- : '🔄 Auto-rebuild explicitly enabled'
-
- if (!this.config.silent) {
- // Name exactly which legs rebuild — "all indexes" was a lie whenever
- // the durable legs were skipped (e.g. only the JS vector index loads
- // here on a warm reopen), and it misread as a whole-brain rebuild in
- // consumer boot logs.
- const rebuildingLegs = [
- shouldRebuildMetadata && 'metadata',
- shouldRebuildVector && 'vector',
- shouldRebuildGraph && 'graph'
- ]
- .filter(Boolean)
- .join(' + ')
- console.log(`${rebuildReason} - loading/rebuilding ${rebuildingLegs || 'no'} index(es) from persisted data...`)
- }
+ // ALWAYS narrated (prodLog, never the silent-suppressible console): there
+ // is no more first-query lazy path — a rebuild that runs here BLOCKS
+ // open() regardless of dataset size or `disableAutoRebuild`, so an
+ // operator must see it in the boot log, not discover it as an
+ // unexplained slow open.
+ prodLog.warn(
+ `[Brainy] open() is building/rebuilding the ${rebuildingLegs || 'no'} index(es) from ` +
+ `${totalCount.toLocaleString()} stored entities — open blocks until the derived ` +
+ `indexes serve; reads never build.`
+ )
// Before the graph rebuild, hydrate the entity id-mapper from the persisted
// snapshot. A native int-keyed adjacency resolves every verb endpoint through
@@ -16578,21 +17929,49 @@ export class Brainy implements BrainyInterface {
// provider running its own background migration is skipped here (it owns
// its index until it verifies-and-swaps).
const rebuildStartTime = Date.now()
+ // The vector leg's build door, by contract with the native provider: a
+ // provider exposing fillFromCanonical() gets THAT call — idempotent, the
+ // provider's own init runs it first so this is the backstop — never a
+ // full rebuild() for a coverage gap. A PARTIAL shortfall deliberately
+ // triggers nothing here: that is repair()'s operator door. The JS index
+ // has no fill door and keeps its rebuild.
+ const vectorBuild = (): Promise