feat(contract): declare contract 1, serve three operators, refuse four by name

Open Brainy's side of the API contract the accelerated engine published.

DECLARED: package.json carries "brainyContract": 1 and the engine states its
own via contractVersion() / BRAINY_CONTRACT_VERSION — two engines compare an
integer instead of probing prototypes, and a tool reads the package field
without importing the engine. Pinned so the two can never drift apart.

SERVED: hasAll, noneOf and excludes now work on the index path. The defect
underneath was worse than the reported divergence — the metadata index's
operator switch had NO DEFAULT CASE, so any operator without a case left the
field's match set at its initial [] and find() returned an empty page.
Documented, validator-accepted, matcher-implemented operators answering
silently wrong. hasAll intersects each element's posting set (an empty operand
is vacuously true of every row that has the field), noneOf complements their
union, excludes complements contains.

REFUSED BY NAME: startsWith, endsWith, matches and length raise
INVALID_QUERY naming the operator, the field and the reason. An equality/range
posting index cannot evaluate a substring, a pattern or an array length without
reading every row — which is the cost this path exists to avoid — so it refuses
rather than answering an empty page. Both engines now agree on all 25 tokens
and contract 1 has no remaining operator divergence. This is a visible change
for a consumer calling those four through find({ where }): an empty page
becomes a typed refusal.

EMITTED: scripts/emit-contract-manifest.mjs generates docs/api-contract.json
from the BUILT surface — prototype doors, exported error classes, the operator
sets read out of their single definitions, the field-addressing vocabulary, the
health verdicts. Nothing hand-maintained, so a diff between two manifests is a
diff between two engines. `--check` fails on a stale manifest, which makes the
announce-every-addition duty mechanical rather than remembered.

RATIFIED in docs/contract-1-ratification.md: the 41-of-57 required split with
the promise spelled out (a refusal is part of a door; deprecation is not
removal), the serving-withholding list confirmed exhaustive and identical, the
minor/major rule adopted with the announcement duty, the 30 storage seam
methods committed as supported surface until Stage 2, and a finding filed
against the spec — is / isNot / greaterEqual / lessEqual are listed there as
served aliases and have never existed in this engine, which throws
INVALID_QUERY on all four.
This commit is contained in:
David Snelling 2026-08-28 10:57:43 -07:00
parent 29a2e8c9e7
commit f758d7dc42
10 changed files with 2172 additions and 3 deletions

View file

@ -184,6 +184,7 @@ export {
// Export version utilities
export { getBrainyVersion } from './utils/version.js'
export { contractVersion, BRAINY_CONTRACT_VERSION } from './utils/version.js'
// Export plugin system
export type { BrainyPlugin, BrainyPluginContext, StorageAdapterFactory } from './plugin.js'

View file

@ -2,7 +2,7 @@
* 🧠 BRAINY EMBEDDED PATTERNS
*
* AUTO-GENERATED - DO NOT EDIT
* Generated: 2025-09-29T10:10:00-07:00
* Generated: 2026-08-27T09:18:45-07:00
* Patterns: 220
* Coverage: 94-98% of all queries
*

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

@ -2241,6 +2241,74 @@ export class MetadataIndexManager implements MetadataIndexProvider {
break
}
// ===== ARRAY SET OPERATORS =====
// An element-indexed array field makes all three exact on the
// index path. They were previously ABSENT from this switch, so
// `fieldResults` kept its initial `[]` and the whole find()
// returned an empty page — a documented, matcher-implemented
// operator answering silently wrong. Served here instead.
// hasAll: [a, b] — the field's array contains EVERY operand:
// the intersection of each element's posting set.
case 'hasAll': {
if (!Array.isArray(operand)) {
fieldResults = []
break
}
if (operand.length === 0) {
// Vacuously true of every row that HAS the field.
const anyBitmap = (this.columnStore && this.columnStore.hasField(field))
? await this.columnStore.rangeQuery(field)
: await this.getExistsBitmapLegacy(field)
fieldResults = this.idMapper.intsIterableToUuids(anyBitmap)
break
}
let intersection: Set<string> | null = null
for (const item of operand) {
const ids = new Set(await this.getIds(field, item))
if (intersection === null) {
intersection = ids
} else {
for (const id of [...intersection]) {
if (!ids.has(id)) intersection.delete(id)
}
}
if (intersection.size === 0) break
}
fieldResults = intersection ? [...intersection] : []
break
}
// noneOf: [a, b] — the field's value is NONE of the operands:
// the complement of their union.
case 'noneOf': {
if (!Array.isArray(operand)) {
fieldResults = []
break
}
const excludeInts: number[] = []
for (const value of operand) {
for (const uuid of await this.getIds(field, value)) {
const intId = this.idMapper.getInt(uuid)
if (intId !== undefined) excludeInts.push(intId)
}
}
fieldResults = this.complementIds(excludeInts)
break
}
// excludes: value — the field's array does NOT contain the value:
// the complement of `contains`.
case 'excludes': {
const excludeInts: number[] = []
for (const uuid of await this.getIds(field, operand)) {
const intId = this.idMapper.getInt(uuid)
if (intId !== undefined) excludeInts.push(intId)
}
fieldResults = this.complementIds(excludeInts)
break
}
// ===== MISSING OPERATOR =====
// missing: boolean - equivalent to exists: !boolean
case 'missing': {
@ -2257,6 +2325,27 @@ export class MetadataIndexManager implements MetadataIndexProvider {
}
break
}
// ===== EVERYTHING ELSE: REFUSED BY NAME, NEVER ANSWERED EMPTY ====
// An equality/range posting index cannot evaluate a substring, a
// pattern or an array length without reading every row, and this
// path exists precisely to avoid that. It used to fall out of the
// switch with `fieldResults` still `[]`, so `find({ where: { name:
// { startsWith: 'a' } } })` returned an empty page and looked like
// an answer. An accepted operator either works or refuses — the
// matcher's own support for these operators governs in-memory
// filtering, never an index-backed find().
default:
throw new BrainyError(
`Filter operator "${op}" on field "${rawField}" cannot be served by the ` +
`metadata index: an equality/range posting index cannot evaluate substrings, ` +
`patterns or array lengths without reading every row. It is REFUSED rather ` +
`than answered with an empty page. Filter on an indexable operator ` +
`(equals/eq, notEquals/ne, oneOf/in, noneOf, greaterThan/gt, ` +
`greaterThanOrEqual/gte, lessThan/lt, lessThanOrEqual/lte, between, contains, ` +
`excludes, hasAll, exists, missing) and narrow the rest in your own code.`,
'INVALID_QUERY'
)
}
// Intersect this operator's matches with the running set (AND semantics
// for multiple operators on the same field).

View file

@ -83,3 +83,27 @@ export function getAugmentationVersion(service: string): { augmentation: string;
version: getBrainyVersion()
}
}
/**
* The API-contract version this build implements a single integer that two
* engines can compare without probing prototypes.
*
* A MINOR release is ADDITIVE: doors and error codes may be added, never
* removed or narrowed, and the contract integer does not move. A MAJOR release
* is what a REQUIRED door's removal or a behavioural narrowing costs, and it
* bumps this integer. A consumer pinning `brainyContract` in a peer range is
* therefore pinning "what I may call", not "which build I run".
*
* Declared in package.json as `"brainyContract"` so a manifest, a tool, or a
* sibling package can read it without importing the engine, and returned here
* so a running process can state its own.
*/
export const BRAINY_CONTRACT_VERSION = 1 as const
/**
* @description The API-contract version this build implements.
* @returns The contract integer see {@link BRAINY_CONTRACT_VERSION}.
*/
export function contractVersion(): number {
return BRAINY_CONTRACT_VERSION
}