/** * entity_relations repo (FEAT-481 SLICE-1, migration 027) — entity-to-entity * relation triples. Cloud-parity storage primitive: see * docs/specs/FEAT-481-design.md §1/§3/§4.1 for the verified cross-repo shape * this mirrors, and migrations/027-entity-relations.sql for the schema. * * This slice owns storage only — no extraction wiring, no MCP tool surface * (those are later slices). supersede()'s cascade lives in * memory-events.ts (mirrors the existing memory_entities unlink, next to * it); erase()'s cascade needs no code here at all — it's `ON DELETE * CASCADE` + `foreign_keys=ON` (src/storage/db.ts), same as memory_entities. */ import type { DB } from './db.js'; export interface EntityRelationInput { subjectEntityId: string; predicate: string; objectEntityId: string; evidenceMemoryId: string; sourceExcerpt: string; /** Model-reported confidence; defaults to 0.5 (matches the column default) when omitted. */ confidence?: number; /** Always populated by the caller — never left to default here (§1 of the design doc: "never left NULL"). */ validFrom: number; validTo?: number | null; } export interface EntityRelationRow { id: string; subject_entity_id: string; predicate: string; object_entity_id: string; evidence_memory_id: string; source_excerpt: string; confidence: number; valid_from: number; valid_to: number | null; } export interface EntityRelationInsertResult { /** The id that WOULD identify the row — only actually persisted when `inserted` is true. */ id: string; /** False when the atomic dedup guard found an existing currently-valid row for the tuple and skipped the write. */ inserted: boolean; } export interface EntityRelationQueryOpts { /** Defaults to the current wall-clock time (epoch ms) — "currently valid" is the same predicate with asOf bound to now. */ asOf?: number; /** Defaults to 50 (matches the get_entity_related MCP tool default, §4.1). */ limit?: number; } export type EntityRelationDirection = 'subject' | 'object' | 'both'; export declare class EntityRelationRepo { private db; constructor(db: DB); /** * Insert-with-dedup-guard (AC-2). The `INSERT...SELECT...WHERE NOT * EXISTS` MUST stay one atomic prepared statement — a single * `db.prepare(...).run(...)` call, no separate SELECT-then-INSERT round * trip. Splitting it into a `.get()` existence check followed by a * separate `.run()` insert reintroduces the exact TOCTOU race this guard * exists to close under SQLite's single-writer lock across concurrent * process (daemon/worker) writers — see design doc §3.4. * * "Currently-valid" for the dedup check is `valid_to IS NULL` on * entity_relations itself (not the memories-joined effective validity * §4.1 uses for reads) — two independent memories asserting the same * triple is corroborating evidence kept as distinct rows, not a * constraint violation; this guard only prevents re-asserting the exact * same still-open triple twice. */ insert(input: EntityRelationInput): EntityRelationInsertResult; /** Relations where `subjectEntityId` is the subject role — AC-3's JOIN-with-memories contract. */ getForSubject(subjectEntityId: string, opts?: EntityRelationQueryOpts): EntityRelationRow[]; /** Relations where `objectEntityId` is the object role — AC-3's JOIN-with-memories contract. */ getForObject(objectEntityId: string, opts?: EntityRelationQueryOpts): EntityRelationRow[]; /** * Relations touching `entityId` in either role by default (`direction: * 'both'`), or restricted to one role — the primitive behind the (later * slice's) `get_entity_related` MCP tool's `direction` parameter. */ getForEntity(entityId: string, opts?: EntityRelationQueryOpts & { direction?: EntityRelationDirection; }): EntityRelationRow[]; private queryByRole; }