2026-07-13 14:43:29 -07:00
/ * *
* @module tests / unit / validate - invariants - delegation
2026-07-14 10:11:41 -07:00
* @description validateIndexConsistency ( ) was blind to native
2026-07-13 14:43:29 -07:00
* providers — it only saw the JS metadata index , so a native manifest ↔ segments ↔ count
* divergence read as "healthy" . It now feature - detects + aggregates each provider ' s
* validateInvariants ( ) , and repairIndex ( ) maps a failing invariant with heal : 'rebuild'
* to that provider ' s rebuild ( ) . "healthy-while-broken must be impossible."
* /
import { describe , it , expect , beforeEach } from 'vitest'
import { Brainy , NounType } from '../../src/index.js'
import type { ProviderInvariantReport } from '../../src/index.js'
const healthyReport = ( provider : string ) : ProviderInvariantReport = > ( {
provider ,
healthy : true ,
serving : true ,
invariants : [ { name : 'manifest-residency' , holds : true , detail : 'ok' , heal : 'none' } ] ,
checkedAt : 1 ,
durationMs : 1
} )
const brokenReport = ( provider : string ) : ProviderInvariantReport = > ( {
provider ,
healthy : false ,
serving : true ,
invariants : [
{ name : 'manifest-residency' , holds : true , detail : 'ok' , heal : 'none' } ,
{
name : 'posted-count-floor' ,
holds : false ,
detail : 'posted 2304 < canonical 2354' ,
expected : 2354 ,
actual : 2304 ,
heal : 'rebuild'
}
] ,
checkedAt : 1 ,
durationMs : 2
} )
describe ( 'validateIndexConsistency delegates to provider validateInvariants() (Pass 3)' , ( ) = > {
let brain : any
beforeEach ( async ( ) = > {
process . env . BRAINY_DETERMINISTIC_EMBEDDINGS = 'true'
brain = new Brainy ( { requireSubtype : false , storage : { type : 'memory' } , dimensions : 384 } )
await brain . init ( )
await brain . add ( { data : 'x' , type : NounType . Concept } )
await brain . flush ( )
} )
it ( 'a broken provider report makes the store unhealthy and names the failing invariant' , async ( ) = > {
brain . index . validateInvariants = async ( ) = > brokenReport ( 'vector' )
const v = await brain . validateIndexConsistency ( )
expect ( v . healthy ) . toBe ( false )
expect ( v . recommendation ) . toMatch ( /posted-count-floor/ )
expect ( v . recommendation ) . toMatch ( /posted 2304 < canonical 2354/ )
expect ( v . recommendation ) . toMatch ( /repairIndex\(\)/ )
expect ( v . providers ? . some ( ( p : ProviderInvariantReport ) = > p . provider === 'vector' && ! p . healthy ) ) . toBe ( true )
delete brain . index . validateInvariants
} )
it ( 'all-healthy provider reports do not flip the store unhealthy' , async ( ) = > {
brain . index . validateInvariants = async ( ) = > healthyReport ( 'vector' )
brain . graphIndex . validateInvariants = async ( ) = > healthyReport ( 'graph' )
const v = await brain . validateIndexConsistency ( )
expect ( v . healthy ) . toBe ( true )
expect ( v . providers ? . length ) . toBe ( 2 )
delete brain . index . validateInvariants
delete brain . graphIndex . validateInvariants
} )
it ( 'a validateInvariants() that THROWS is surfaced as unhealthy, never swallowed' , async ( ) = > {
brain . index . validateInvariants = async ( ) = > { throw new Error ( 'provider blew up' ) }
const v = await brain . validateIndexConsistency ( )
expect ( v . healthy ) . toBe ( false )
expect ( v . recommendation ) . toMatch ( /validate-invariants-threw/ )
delete brain . index . validateInvariants
fix(health): one contract for a throwing probe — heal is none, serving is not withheld; repair report gains missing/rebuilt/reason
A validateInvariants() that threw was re-synthesized by the host's catch as
heal:'rebuild' with serving:false — a rebuild lever one transient exception
away, while the native provider's own composer reports the same event as
heal:'none'. Two components disagreeing on what a thrown check means is how
a flaky probe becomes an outage. Both now agree: the report is named
('validate-invariants-threw'), loud (healthy:false, the error in detail),
unverified — and it never buys a rebuild and never withholds serving; the
provider's serving verdict is the provider's to compose, not inferred from a
probe that failed to run. The synthesized report also carries the provider's
name instead of 'unknown'.
RepairFamilyReport gains `missing: {count, sample}` (an exact count plus a
capped id sample — a verdict, not a dump), `rebuilt` (a full generational
rebuild ran, as opposed to an incremental heal) and `reason`, aligning the
receipt's shape with the provider-side health report.
Pinned in tests/unit/validate-invariants-delegation.test.ts.
2026-08-24 09:54:25 -07:00
} )
it ( 'ONE CONTRACT FOR A THROWING PROBE: heal is none (flakiness never buys a rebuild) and serving is not withheld' , async ( ) = > {
// The probe that fails to RUN must never be read as "the index is broken,
// rebuild it" — that synthesized heal:'rebuild' was the dark-rebuild lever
// one transient exception away, and the native composer already said
// 'none' for the same event. Both engines now agree: named, loud,
// unverified — and never a rebuild, never a withheld serve.
brain . index . validateInvariants = async ( ) = > { throw new Error ( 'transient: mmap window busy' ) }
const v = await brain . validateIndexConsistency ( )
const thrown = v . providers ? . find ( ( p : ProviderInvariantReport ) = >
p . invariants . some ( ( i ) = > i . name === 'validate-invariants-threw' )
)
expect ( thrown ) . toBeDefined ( )
expect ( thrown ! . healthy ) . toBe ( false )
expect ( thrown ! . serving ) . toBe ( true )
const inv = thrown ! . invariants . find ( ( i ) = > i . name === 'validate-invariants-threw' ) !
expect ( inv . holds ) . toBe ( false )
expect ( inv . heal ) . toBe ( 'none' )
expect ( inv . detail ) . toMatch ( /transient: mmap window busy/ )
// No provider report in the set recommends a rebuild for this event.
expect (
v . providers ! . flatMap ( ( p : ProviderInvariantReport ) = > p . invariants ) . some ( ( i ) = > i . heal === 'rebuild' )
) . toBe ( false )
delete brain . index . validateInvariants
2026-07-13 14:43:29 -07:00
} )
it ( 'providers without validateInvariants() are omitted (JS baseline unchanged)' , async ( ) = > {
const v = await brain . validateIndexConsistency ( )
expect ( v . providers ) . toBeUndefined ( )
expect ( typeof v . healthy ) . toBe ( 'boolean' )
} )
it ( 'repairIndex() rebuilds a provider whose failing invariant asks for it' , async ( ) = > {
let rebuilt = false
brain . index . validateInvariants = async ( ) = > ( rebuilt ? healthyReport ( 'vector' ) : brokenReport ( 'vector' ) )
const origRebuild = brain . index . rebuild . bind ( brain . index )
brain . index . rebuild = async ( . . . a : any [ ] ) = > { rebuilt = true ; return origRebuild ( . . . a ) }
await brain . repairIndex ( )
expect ( rebuilt ) . toBe ( true )
// After repair, the store validates healthy again.
const v = await brain . validateIndexConsistency ( )
expect ( v . providers ? . find ( ( p : ProviderInvariantReport ) = > p . provider === 'vector' ) ? . healthy ) . toBe ( true )
brain . index . rebuild = origRebuild
delete brain . index . validateInvariants
} )
} )