feat(plugin): an optional planFindPage door — an index that can plan a find answers it in one call
Some checks failed
CI / Node 22 (push) Successful in 12m34s
CI / Node 24 (push) Successful in 12m21s
CI / Integration + conformance (Node 22) (push) Failing after 17m4s
CI / Bun (latest) (push) Successful in 12m19s

The provider's read doors each serve one stage, so a find that consults three of
them crosses into the index three times and marshals a result set at every
crossing: a filter matching a hundred thousand rows builds a hundred thousand id
strings to return a page of twenty-five. An index able to decide the stage order
itself can answer the page in one call and build ids only for the page.

planFindPage is optional and additive, in the shape filterIdsWithin and
getIdSetForFilter already set. The hook sits above the branch selection, because
the branches are what decide stage order per call site and an index that plans
has to be asked before that choice is made. Absent — as it is on this engine's
own index — every find is served by the stage doors exactly as before, which is
what keeps this engine the ordering oracle for any index that implements one.

The contract the door must keep, written where an implementer will read it:
identical rows in identical order to what the stage doors would produce; the
graph-first law (neighbours are the candidate universe, the filter runs over
those ids, orderBy sorts the whole set, the page is cut last); null returned
BEFORE any work rather than instead of an answer; and emptyAt naming the stage
that produced an empty page, so the serving law is applied to the right index —
an empty graph answer is re-verified against the adjacency before it is
believed, and a filter-empty is not.

Pinned in tests/integration/find-planner-door.test.ts: absent changes nothing;
present it is asked first with normalized params, the hidden ids and the graph
provider; its page is used and hydrated in its order; a declining door leaves
the result identical to the no-door path; and the two emptyAt branches verify
the adjacency, or correctly do not.
This commit is contained in:
David Snelling 2026-09-01 13:11:16 -07:00
parent d5147ed608
commit 4d5f823f47
4 changed files with 226 additions and 2 deletions

View file

@ -7344,6 +7344,47 @@ export class Brainy<T = any> implements BrainyInterface<T> {
await this.verifyMetadataLive()
}
// PLANNED FIND (optional provider door, `MetadataIndexProvider.planFindPage`).
//
// The stage doors below each serve one stage, so a find that consults
// three of them crosses into the index three times and marshals a result
// set at every crossing — a filter matching a hundred thousand rows
// builds a hundred thousand id strings to return a page of twenty-five.
// An index that can decide the stage order itself answers the page in one
// call and materializes ids only for the page.
//
// The hook sits ABOVE the branch selection because the branches are what
// decide stage order per call site; an index that plans has to be asked
// before that choice is made, not inside one of its arms.
//
// Optional and additive: a provider without the door, and any shape the
// door hands back, take exactly the path they always took. `null` is a
// routing decision the door must make BEFORE doing any work — never a
// partial answer. Every guard above still ran (readiness, the migration
// gate, the where-clause validation, the metadata cold-read guard), and
// the serving law is applied here on the way out: an empty answer is
// re-verified against the index that produced it before it is believed.
const planningIndex = this.metadataIndex as unknown as MetadataIndexProvider
if (typeof planningIndex.planFindPage === 'function') {
const planned = await planningIndex.planFindPage(params, [...hiddenIds], this.graphIndex)
if (planned !== null && planned !== undefined) {
if (planned.ids.length === 0) {
// A cold adjacency can report a size yet hold no edges, so an empty
// graph answer is not truth until the adjacency verifies live. A
// genuinely edgeless anchor verifies and the empty result stands.
if (planned.emptyAt === 'graph') await this.verifyGraphAdjacencyLive()
return []
}
const plannedEntities = await this.batchGet(planned.ids)
const plannedResults: Result<T>[] = []
for (const id of planned.ids) {
const entity = plannedEntities.get(id)
if (entity) plannedResults.push(this.createResult(id, 1.0, entity))
}
return plannedResults
}
}
// Handle metadata-only queries (no vector search needed)
if (!hasVectorSearchCriteria && !hasGraphCriteria && hasFilterCriteria) {
// Build filter for metadata index

View file

@ -2,7 +2,7 @@
* 🧠 BRAINY EMBEDDED TYPE EMBEDDINGS
*
* AUTO-GENERATED - DO NOT EDIT
* Generated: 2026-06-29T10:04:19-07:00
* Generated: 2026-08-27T09:18:45-07:00
* Noun Types: 42
* Verb Types: 127
*
@ -19,7 +19,7 @@ export const TYPE_METADATA = {
verbTypes: 127,
totalTypes: 169,
embeddingDimensions: 384,
generatedAt: "2026-06-29T10:04:19-07:00",
generatedAt: "2026-08-27T09:18:45-07:00",
sizeBytes: {
embeddings: 259584,
base64: 346112

View file

@ -424,6 +424,52 @@ export interface MetadataIndexProvider {
* @param ids - The candidate ids (canonical). The answer is a subsequence.
*/
filterIdsWithin?(filter: any, ids: readonly string[]): Promise<string[]>
/**
* @description OPTIONAL: plan and execute a WHOLE `find()` the graph
* traversal, the metadata filter, the ordering and the page and answer the
* page's ids, or `null` for a shape this index does not plan.
*
* The doors above each serve one stage, so a `find()` that consults three of
* them crosses into the index three times and marshals a result set at every
* crossing. An index that can decide the stage ORDER itself does the whole
* thing in one call and materializes ids only for the page a filter
* matching a hundred thousand rows then builds twenty-five id strings instead
* of a hundred thousand.
*
* The contract this door must keep, because Brainy cannot check it:
*
* - **The same answer.** Identical rows, in identical order, to what the
* stage doors would have produced for the same params. This door changes
* which code runs, never what the answer is.
* - **The law of the stages** (`find({ connected })` is graph-first): the
* neighbour set is the candidate universe, the filter is evaluated over
* those ids only, `orderBy` sorts the whole candidate set, and the page is
* cut LAST.
* - **`null` before work, not instead of an answer.** A shape the index does
* not plan must be handed back BEFORE any evaluation, so Brainy serves it
* through the stage doors exactly as it always has. Returning `null` after
* partial work, or an empty page for a shape it could not evaluate, is a
* silent wrong answer.
* - **`emptyAt` names the stage** that produced an empty page `'graph'`,
* `'filter'`, `'visibility'` or `'none'` so Brainy can apply its serving
* law to the right index. An empty answer from an index that is not
* serving must refuse loudly, and Brainy can only re-verify what it is told.
*
* Absent every `find()` is served by the stage doors, which is Brainy's
* own behaviour and the ordering oracle for any implementation of this one.
* @param params - The find params, already normalized by `find()`
* (natural-language parsed, `connected` anchors resolved to canonical ids,
* an empty `where` dropped).
* @param hiddenIds - Ids this read must not return; apply BEFORE paging so
* `limit` stays exact.
* @param graphIndex - The active graph provider, for a `connected` plan.
* @returns The page's ids plus the stage that emptied it, or `null`.
*/
planFindPage?(
params: any,
hiddenIds: readonly string[],
graphIndex: unknown
): Promise<{ ids: string[]; emptyAt: 'graph' | 'filter' | 'visibility' | 'none' } | null>
getIdsForTextQuery(query: string): Promise<Array<{ id: string; matchCount: number }>>
getSortedIdsForFilter(filter: any, orderBy: string, order?: 'asc' | 'desc', topK?: number): Promise<string[]>
getFilterValues(field: string): Promise<string[]>

View file

@ -0,0 +1,137 @@
/**
* @module tests/integration/find-planner-door
* @description The optional `MetadataIndexProvider.planFindPage` door.
*
* The stage doors each serve one stage, so a `find()` that consults three of
* them crosses into the index three times and marshals a result set at every
* crossing a filter matching a hundred thousand rows builds a hundred
* thousand id strings to return a page of twenty-five. An index that can decide
* the stage order itself answers the page in one call.
*
* These pins hold the three properties that make such a door safe to add:
*
* 1. **Absent, nothing changes.** The reference index has no planner, and every
* find is served by the stage doors exactly as before. That is also what
* makes this engine the ordering oracle for any index that implements one.
* 2. **Present, it is asked first and its answer is used** above the branch
* selection, with the params already normalized, the hidden ids passed, and
* the graph provider handed over.
* 3. **`null` is routing, not an answer.** A door that declines a shape leaves
* it to the path that always served it, and the result is unchanged.
*
* Plus the serving law: an empty page stamped `emptyAt: 'graph'` is re-verified
* against the adjacency before it is believed, so a not-serving graph refuses
* loudly instead of answering `[]` as truth.
*/
import { describe, it, expect, beforeAll, vi } from 'vitest'
import { Brainy } from '../../src/brainy'
import { NounType, VerbType } from '../../src/types/graphTypes'
import { generateTestVector } from '../helpers/test-factory'
describe('find(): the optional planner door', () => {
let brain: Brainy<any>
const anchor = 'planner-anchor'
let neighbourId = ''
beforeAll(async () => {
brain = new Brainy({ requireSubtype: false, storage: { type: 'memory' } })
await brain.init()
await brain.add({
id: anchor,
data: 'anchor',
type: NounType.Person,
metadata: { kind: 'anchor' },
vector: generateTestVector()
})
for (let i = 0; i < 12; i++) {
const id = await brain.add({
id: `row-${i}`,
data: `row ${i}`,
type: NounType.Person,
metadata: { kind: 'note', rank: i },
vector: generateTestVector()
})
if (i === 0) neighbourId = id
await brain.relate({ from: anchor, to: id, type: VerbType.Knows })
}
})
/** Install a planner door for one call, then remove it. */
const withDoor = async <T>(
door: (...a: any[]) => Promise<any>,
body: () => Promise<T>
): Promise<T> => {
const index = (brain as any).metadataIndex
index.planFindPage = door
try {
return await body()
} finally {
delete index.planFindPage
}
}
it('is absent on the reference index — every find is served by the stage doors', async () => {
expect((brain as any).metadataIndex.planFindPage).toBeUndefined()
const results = await brain.find({ where: { kind: 'note' }, limit: 5 })
expect(results).toHaveLength(5)
})
it('is asked before the branches, with normalized params and the graph provider', async () => {
const door = vi.fn(async () => null)
await withDoor(door, async () => {
await brain.find({ where: { kind: 'note' }, limit: 5 })
})
expect(door).toHaveBeenCalledTimes(1)
const [params, hidden, graph] = door.mock.calls[0] as any[]
expect(params.where).toEqual({ kind: 'note' })
expect(Array.isArray(hidden)).toBe(true)
expect(graph).toBe((brain as any).graphIndex)
})
it('uses the page it answers, hydrated and in the door\'s order', async () => {
const results = await withDoor(
async () => ({ ids: [neighbourId], emptyAt: 'none' as const }),
async () => brain.find({ where: { kind: 'note' }, limit: 5 })
)
expect(results).toHaveLength(1)
expect(results[0].entity.id).toBe(neighbourId)
})
it('a declining door changes nothing — the shape is served as it always was', async () => {
const withoutDoor = await brain.find({ where: { kind: 'note' }, orderBy: 'rank', limit: 4 })
const declined = await withDoor(
async () => null,
async () => brain.find({ where: { kind: 'note' }, orderBy: 'rank', limit: 4 })
)
expect(declined.map((r) => r.entity.id)).toEqual(withoutDoor.map((r) => r.entity.id))
})
it('re-verifies the adjacency before believing an empty graph answer', async () => {
const verify = vi.spyOn(brain as any, 'verifyGraphAdjacencyLive')
try {
const results = await withDoor(
async () => ({ ids: [], emptyAt: 'graph' as const }),
async () => brain.find({ connected: { from: anchor }, where: { kind: 'note' }, limit: 5 })
)
expect(results).toEqual([])
expect(verify).toHaveBeenCalled()
} finally {
verify.mockRestore()
}
})
it('does not re-verify the adjacency for an empty the FILTER produced', async () => {
const verify = vi.spyOn(brain as any, 'verifyGraphAdjacencyLive')
verify.mockClear()
try {
const results = await withDoor(
async () => ({ ids: [], emptyAt: 'filter' as const }),
async () => brain.find({ where: { kind: 'note' }, limit: 5 })
)
expect(results).toEqual([])
expect(verify).not.toHaveBeenCalled()
} finally {
verify.mockRestore()
}
})
})