@graphorin/memory 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +83 -0
- package/README.md +24 -7
- package/dist/conflict/index.js +0 -1
- package/dist/conflict/pipeline.js +0 -1
- package/dist/conflict/pipeline.js.map +1 -1
- package/dist/conflict/stages/stage3-heuristic-regex.js +0 -1
- package/dist/conflict/stages/stage3-heuristic-regex.js.map +1 -1
- package/dist/conflict/types.d.ts +1 -1
- package/dist/conflict/types.js.map +1 -1
- package/dist/consolidator/index.js +2 -0
- package/dist/consolidator/index.js.map +1 -1
- package/dist/consolidator/lock.js +7 -1
- package/dist/consolidator/lock.js.map +1 -1
- package/dist/consolidator/phases/deep.js +19 -12
- package/dist/consolidator/phases/deep.js.map +1 -1
- package/dist/consolidator/phases/induce.d.ts.map +1 -1
- package/dist/consolidator/phases/induce.js +2 -3
- package/dist/consolidator/phases/induce.js.map +1 -1
- package/dist/consolidator/phases/reflect.js +35 -8
- package/dist/consolidator/phases/reflect.js.map +1 -1
- package/dist/consolidator/phases/standard.js +199 -28
- package/dist/consolidator/phases/standard.js.map +1 -1
- package/dist/consolidator/reconcile.js +16 -15
- package/dist/consolidator/reconcile.js.map +1 -1
- package/dist/consolidator/runtime.d.ts.map +1 -1
- package/dist/consolidator/runtime.js +129 -47
- package/dist/consolidator/runtime.js.map +1 -1
- package/dist/consolidator/scheduler.d.ts +17 -3
- package/dist/consolidator/scheduler.d.ts.map +1 -1
- package/dist/consolidator/scheduler.js.map +1 -1
- package/dist/consolidator/types.d.ts +30 -1
- package/dist/consolidator/types.d.ts.map +1 -1
- package/dist/consolidator/types.js +10 -0
- package/dist/consolidator/types.js.map +1 -1
- package/dist/context-engine/compaction/clear-tool-results.d.ts +1 -1
- package/dist/context-engine/compaction/clear-tool-results.js +1 -1
- package/dist/context-engine/compaction/clear-tool-results.js.map +1 -1
- package/dist/context-engine/compaction/compactor.d.ts +1 -0
- package/dist/context-engine/compaction/compactor.d.ts.map +1 -1
- package/dist/context-engine/compaction/compactor.js +32 -8
- package/dist/context-engine/compaction/compactor.js.map +1 -1
- package/dist/context-engine/compaction/hooks/types.d.ts +1 -1
- package/dist/context-engine/compaction/index.d.ts +1 -0
- package/dist/context-engine/compaction/templates/marker.d.ts +33 -0
- package/dist/context-engine/compaction/templates/marker.d.ts.map +1 -0
- package/dist/context-engine/compaction/templates/marker.js +34 -0
- package/dist/context-engine/compaction/templates/marker.js.map +1 -0
- package/dist/context-engine/compaction/templates/summary-9-section.d.ts +1 -1
- package/dist/context-engine/compaction/templates/summary-9-section.d.ts.map +1 -1
- package/dist/context-engine/compaction/templates/summary-9-section.js +4 -3
- package/dist/context-engine/compaction/templates/summary-9-section.js.map +1 -1
- package/dist/context-engine/engine.d.ts +7 -176
- package/dist/context-engine/engine.d.ts.map +1 -1
- package/dist/context-engine/engine.js +0 -2
- package/dist/context-engine/engine.js.map +1 -1
- package/dist/context-engine/index.d.ts +8 -49
- package/dist/context-engine/io-types.d.ts +144 -0
- package/dist/context-engine/io-types.d.ts.map +1 -0
- package/dist/context-engine/locale-packs/types.d.ts +1 -1
- package/dist/context-engine/locale-packs/types.js.map +1 -1
- package/dist/context-engine/privacy-filter.d.ts +1 -2
- package/dist/context-engine/privacy-filter.d.ts.map +1 -1
- package/dist/context-engine/privacy-filter.js +1 -2
- package/dist/context-engine/privacy-filter.js.map +1 -1
- package/dist/errors/index.d.ts +2 -2
- package/dist/errors/index.js +2 -2
- package/dist/errors/index.js.map +1 -1
- package/dist/facade.d.ts +43 -52
- package/dist/facade.d.ts.map +1 -1
- package/dist/facade.js +5 -3
- package/dist/facade.js.map +1 -1
- package/dist/graph/entity-resolver.js +1 -1
- package/dist/graph/entity-resolver.js.map +1 -1
- package/dist/index.d.ts +17 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -7
- package/dist/index.js.map +1 -1
- package/dist/internal/envelope.js +57 -0
- package/dist/internal/envelope.js.map +1 -0
- package/dist/internal/injection-heuristics.js +0 -0
- package/dist/internal/injection-heuristics.js.map +1 -1
- package/dist/internal/llm-json.js +40 -0
- package/dist/internal/llm-json.js.map +1 -0
- package/dist/internal/storage-adapter.d.ts +23 -10
- package/dist/internal/storage-adapter.d.ts.map +1 -1
- package/dist/memory-interface.d.ts +131 -0
- package/dist/memory-interface.d.ts.map +1 -0
- package/dist/package.js +6 -0
- package/dist/package.js.map +1 -0
- package/dist/search/iterative.d.ts.map +1 -1
- package/dist/search/iterative.js +2 -3
- package/dist/search/iterative.js.map +1 -1
- package/dist/search/query-transform.d.ts.map +1 -1
- package/dist/search/query-transform.js +2 -3
- package/dist/search/query-transform.js.map +1 -1
- package/dist/tiers/episodic-memory.js +3 -3
- package/dist/tiers/episodic-memory.js.map +1 -1
- package/dist/tiers/index.d.ts +2 -2
- package/dist/tiers/insight-memory.js +1 -1
- package/dist/tiers/insight-memory.js.map +1 -1
- package/dist/tiers/procedural-memory.d.ts +1 -1
- package/dist/tiers/procedural-memory.js +2 -2
- package/dist/tiers/procedural-memory.js.map +1 -1
- package/dist/tiers/semantic-memory.d.ts +58 -4
- package/dist/tiers/semantic-memory.d.ts.map +1 -1
- package/dist/tiers/semantic-memory.js +103 -20
- package/dist/tiers/semantic-memory.js.map +1 -1
- package/dist/tools/block-tools.d.ts +19 -57
- package/dist/tools/block-tools.d.ts.map +1 -1
- package/dist/tools/block-tools.js.map +1 -1
- package/dist/tools/fact-tools.d.ts +53 -214
- package/dist/tools/fact-tools.d.ts.map +1 -1
- package/dist/tools/fact-tools.js +3 -2
- package/dist/tools/fact-tools.js.map +1 -1
- package/dist/tools/recall-tools.d.ts +34 -175
- package/dist/tools/recall-tools.d.ts.map +1 -1
- package/dist/tools/recall-tools.js +5 -4
- package/dist/tools/recall-tools.js.map +1 -1
- package/dist/tools/runbook-tools.d.ts +15 -58
- package/dist/tools/runbook-tools.d.ts.map +1 -1
- package/dist/tools/runbook-tools.js.map +1 -1
- package/dist/tools/types.d.ts +1 -1
- package/package.json +18 -17
- package/src/conflict/index.ts +29 -0
- package/src/conflict/locale-packs/en.ts +142 -0
- package/src/conflict/locale-packs/index.ts +17 -0
- package/src/conflict/locale-packs/types.ts +138 -0
- package/src/conflict/pipeline.ts +371 -0
- package/src/conflict/stages/helpers.ts +65 -0
- package/src/conflict/stages/stage1-exact-dedup.ts +40 -0
- package/src/conflict/stages/stage2-embedding-three-zone.ts +54 -0
- package/src/conflict/stages/stage3-heuristic-regex.ts +51 -0
- package/src/conflict/stages/stage4-subject-predicate.ts +46 -0
- package/src/conflict/stages/stage5-defer-to-deep.ts +34 -0
- package/src/conflict/types.ts +263 -0
- package/src/consolidator/budget.ts +312 -0
- package/src/consolidator/decay.ts +246 -0
- package/src/consolidator/dlq.ts +61 -0
- package/src/consolidator/errors.ts +87 -0
- package/src/consolidator/idempotency.ts +27 -0
- package/src/consolidator/index.ts +269 -0
- package/src/consolidator/lock.ts +141 -0
- package/src/consolidator/noise-filter.ts +235 -0
- package/src/consolidator/phases/deep.ts +308 -0
- package/src/consolidator/phases/induce.ts +423 -0
- package/src/consolidator/phases/learned-context.ts +233 -0
- package/src/consolidator/phases/light.ts +166 -0
- package/src/consolidator/phases/reflect.ts +504 -0
- package/src/consolidator/phases/standard.ts +1002 -0
- package/src/consolidator/reconcile.ts +273 -0
- package/src/consolidator/runtime.ts +1049 -0
- package/src/consolidator/scheduler.ts +182 -0
- package/src/consolidator/triggers.ts +134 -0
- package/src/consolidator/types.ts +622 -0
- package/src/context-engine/annotations.ts +161 -0
- package/src/context-engine/auto-recall.ts +92 -0
- package/src/context-engine/compaction/clear-tool-results.ts +232 -0
- package/src/context-engine/compaction/compactor.ts +403 -0
- package/src/context-engine/compaction/hooks/reanchor-persona-block.ts +55 -0
- package/src/context-engine/compaction/hooks/reanchor-pinned-facts.ts +66 -0
- package/src/context-engine/compaction/hooks/reanchor-project-rules.ts +57 -0
- package/src/context-engine/compaction/hooks/reanchor-recent-results.ts +116 -0
- package/src/context-engine/compaction/hooks/types.ts +68 -0
- package/src/context-engine/compaction/index.ts +60 -0
- package/src/context-engine/compaction/templates/marker.ts +33 -0
- package/src/context-engine/compaction/templates/summary-9-section.ts +223 -0
- package/src/context-engine/compaction/thresholds.ts +107 -0
- package/src/context-engine/compaction/types.ts +255 -0
- package/src/context-engine/engine.ts +970 -0
- package/src/context-engine/index.ts +118 -0
- package/src/context-engine/io-types.ts +162 -0
- package/src/context-engine/locale-packs/en.ts +107 -0
- package/src/context-engine/locale-packs/index.ts +19 -0
- package/src/context-engine/locale-packs/resolver.ts +197 -0
- package/src/context-engine/locale-packs/types.ts +177 -0
- package/src/context-engine/metadata.ts +114 -0
- package/src/context-engine/preambles/inbound-en.ts +19 -0
- package/src/context-engine/privacy-filter.ts +190 -0
- package/src/context-engine/templates/base-en.ts +19 -0
- package/src/context-engine/templates/composer.ts +91 -0
- package/src/context-engine/token-budget.ts +319 -0
- package/src/context-engine/token-counter.ts +157 -0
- package/src/errors/index.ts +273 -0
- package/src/facade.ts +750 -0
- package/src/graph/entity-resolver.ts +364 -0
- package/src/graph/index.ts +24 -0
- package/src/index.ts +259 -0
- package/src/internal/contextualize.ts +173 -0
- package/src/internal/embedder-binding.ts +53 -0
- package/src/internal/envelope.ts +70 -0
- package/src/internal/id.ts +23 -0
- package/src/internal/injection-heuristics.ts +0 -0
- package/src/internal/llm-json.ts +38 -0
- package/src/internal/spans.ts +35 -0
- package/src/internal/storage-adapter.ts +963 -0
- package/src/memory-interface.ts +158 -0
- package/src/migration/embedder-migration.ts +237 -0
- package/src/migration/index.ts +13 -0
- package/src/search/explain.ts +123 -0
- package/src/search/fit-weights.ts +109 -0
- package/src/search/graph-ppr.ts +38 -0
- package/src/search/index.ts +55 -0
- package/src/search/iterative.ts +658 -0
- package/src/search/query-transform.ts +285 -0
- package/src/search/rrf.ts +225 -0
- package/src/search/trust.ts +55 -0
- package/src/search/types.ts +50 -0
- package/src/tiers/episodic-memory.ts +463 -0
- package/src/tiers/index.ts +50 -0
- package/src/tiers/insight-memory.ts +208 -0
- package/src/tiers/procedural-memory.ts +482 -0
- package/src/tiers/semantic-memory.ts +1729 -0
- package/src/tiers/session-memory.ts +271 -0
- package/src/tiers/shared-memory.ts +62 -0
- package/src/tiers/working-memory.ts +368 -0
- package/src/tools/block-tools.ts +172 -0
- package/src/tools/fact-tools.ts +473 -0
- package/src/tools/index.ts +109 -0
- package/src/tools/recall-tools.ts +303 -0
- package/src/tools/runbook-tools.ts +107 -0
- package/src/tools/taint.ts +39 -0
- package/src/tools/types.ts +37 -0
- package/dist/conflict/locale-packs/index.js +0 -4
- package/dist/context-engine/compaction/index.js +0 -10
- package/dist/context-engine/index.d.ts.map +0 -1
- package/dist/context-engine/index.js +0 -25
- package/dist/context-engine/locale-packs/index.js +0 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity-resolver.js","names":["bestId: string | null","#store","#embedder","#embedderId","#provider","#mergeThreshold","#adjudicateThreshold","#llmAdjudication","#embed","#create","#adjudicate"],"sources":["../../src/graph/entity-resolver.ts"],"sourcesContent":["/**\n * Entity resolution for the lightweight in-SQLite relation graph (P2-1).\n *\n * Raw `subject` / `object` strings on facts fragment (\"Anna\", \"Anna S.\",\n * \"my sister\" become unrelated rows), which kills multi-hop recall. This\n * module folds them into **canonical entities** so the one-hop CTE can\n * traverse relationships. Resolution is layered, cheapest first:\n *\n * 1. **lexical** - exact match on the folded {@link normalizeEntityName}.\n * 2. **embedding** - cosine over candidate name vectors; `≥ mergeThreshold`\n * reuses the match.\n * 3. **adjudication** - a middle similarity band (`[adjudicate, merge)`)\n * is *ambiguous*; an opt-in LLM call (provider + `llmAdjudication`)\n * decides. **Offline / by default the ambiguous band mints a new\n * entity** - the resolver never auto-merges on weak evidence, because\n * a wrong merge fuses two distinct people (the stated P2-1 risk).\n *\n * The pure policy ({@link resolveEntityDecision}) is provider-agnostic and\n * does no I/O; {@link EntityResolver} wires it to an injected store +\n * embedder + optional provider. With no embedder it degrades to\n * lexical-only - still useful, and still fully offline.\n *\n * @packageDocumentation\n */\n\nimport type {\n EmbedderProvider,\n Fact,\n Provider,\n ProviderRequest,\n SessionScope,\n} from '@graphorin/core';\nimport type { EntityWithEmbedding, GraphMemoryStoreExt } from '../internal/storage-adapter.js';\n\n/** Cosine `≥` this auto-reuses an existing entity (embedding match). */\nexport const DEFAULT_MERGE_THRESHOLD = 0.92;\n/** Cosine in `[this, merge)` is *ambiguous* - adjudicate or mint new. */\nexport const DEFAULT_ADJUDICATE_THRESHOLD = 0.82;\n\n/**\n * Outcome of the pure resolution policy. `match` reuses an existing\n * entity; `ambiguous` flags a middle-similarity candidate for the caller\n * to adjudicate (or conservatively reject); `new` mints a fresh entity.\n *\n * @stable\n */\nexport type EntityResolveDecision =\n | {\n readonly kind: 'match';\n readonly entityId: string;\n readonly similarity: number;\n readonly via: 'lexical' | 'embedding';\n }\n | { readonly kind: 'ambiguous'; readonly entityId: string; readonly similarity: number }\n | { readonly kind: 'new' };\n\n/** Minimal candidate shape the pure policy compares against. */\nexport interface ResolutionCandidate {\n readonly id: string;\n readonly normalizedName: string;\n readonly vector: Float32Array | null;\n /**\n * MST-11: the embedder that produced `vector`. When both this and the\n * query's `vectorEmbedderId` are known and differ, the candidate is skipped\n * for embedding comparison - vectors from different models live in different\n * spaces, so their cosine is meaningless. Absent on either side ⇒ compared\n * (byte-identical to the prior behaviour).\n */\n readonly embedderId?: string | null;\n}\n\n/** Inputs to {@link resolveEntityDecision} (all provided by the caller). */\nexport interface ResolveDecisionInput {\n readonly normalizedName: string;\n readonly vector?: Float32Array | null;\n /** MST-11: the embedder that produced `vector` (gates cross-embedder cosine). */\n readonly vectorEmbedderId?: string | null;\n readonly candidates: ReadonlyArray<ResolutionCandidate>;\n readonly mergeThreshold: number;\n readonly adjudicateThreshold: number;\n}\n\n/**\n * Fold an entity surface form into a canonical lexical key: Unicode\n * NFKC, lowercased, internal whitespace collapsed, surrounding\n * punctuation stripped. `\" Anna S. \"` → `\"anna s\"`. Returns `''` for a\n * name with no letters/digits (the resolver treats that as \"no entity\").\n *\n * @stable\n */\nexport function normalizeEntityName(name: string): string {\n return name\n .normalize('NFKC')\n .toLowerCase()\n .replace(/\\s+/g, ' ')\n .trim()\n .replace(/^[^\\p{L}\\p{N}]+|[^\\p{L}\\p{N}]+$/gu, '');\n}\n\n/**\n * Cosine similarity of two embeddings in `[-1, 1]`. Compares over the\n * shorter length and returns `0` when either vector is empty / zero-norm.\n *\n * @stable\n */\nexport function cosineSimilarity(a: Float32Array, b: Float32Array): number {\n const len = Math.min(a.length, b.length);\n let dot = 0;\n let na = 0;\n let nb = 0;\n for (let i = 0; i < len; i++) {\n const ai = a[i] ?? 0;\n const bi = b[i] ?? 0;\n dot += ai * bi;\n na += ai * ai;\n nb += bi * bi;\n }\n if (na === 0 || nb === 0) return 0;\n return dot / Math.sqrt(na * nb);\n}\n\n/**\n * Pure resolution policy: lexical exact match → embedding cosine →\n * ambiguous band → new. No I/O; deterministic. The caller decides what\n * to do with `ambiguous` (LLM adjudicate, or conservatively mint new).\n *\n * @stable\n */\nexport function resolveEntityDecision(input: ResolveDecisionInput): EntityResolveDecision {\n // 1. Exact lexical match is the cheapest + strongest signal.\n for (const c of input.candidates) {\n if (c.normalizedName === input.normalizedName) {\n return { kind: 'match', entityId: c.id, similarity: 1, via: 'lexical' };\n }\n }\n // 2. Embedding similarity (only when both sides have a vector).\n const v = input.vector;\n if (v === undefined || v === null || v.length === 0) return { kind: 'new' };\n let bestId: string | null = null;\n let bestSim = Number.NEGATIVE_INFINITY;\n for (const c of input.candidates) {\n if (c.vector === null || c.vector.length === 0) continue;\n // MST-11: never compare vectors across embedders - different models live\n // in different vector spaces, so their cosine is meaningless. Skip only\n // when both embedder ids are known and differ.\n if (\n input.vectorEmbedderId != null &&\n c.embedderId != null &&\n c.embedderId !== input.vectorEmbedderId\n ) {\n continue;\n }\n const sim = cosineSimilarity(v, c.vector);\n if (sim > bestSim) {\n bestSim = sim;\n bestId = c.id;\n }\n }\n if (bestId === null) return { kind: 'new' };\n if (bestSim >= input.mergeThreshold) {\n return { kind: 'match', entityId: bestId, similarity: bestSim, via: 'embedding' };\n }\n if (bestSim >= input.adjudicateThreshold) {\n return { kind: 'ambiguous', entityId: bestId, similarity: bestSim };\n }\n return { kind: 'new' };\n}\n\n/** Tunable thresholds + LLM-adjudication switch for {@link EntityResolver}. */\nexport interface EntityResolutionConfig {\n /** Cosine `≥` this auto-reuses a match. Default {@link DEFAULT_MERGE_THRESHOLD}. */\n readonly mergeThreshold?: number;\n /** Cosine in `[this, merge)` is ambiguous. Default {@link DEFAULT_ADJUDICATE_THRESHOLD}. */\n readonly adjudicateThreshold?: number;\n /**\n * Resolve the ambiguous band with one provider call. Requires a\n * `provider`. Default `false` ⇒ ambiguous mints a new entity (no\n * network call; never auto-merges on weak evidence).\n */\n readonly llmAdjudication?: boolean;\n}\n\n/** Construction deps for {@link EntityResolver}. */\nexport interface EntityResolverDeps {\n readonly store: GraphMemoryStoreExt;\n readonly embedder?: EmbedderProvider | null;\n readonly embedderId?: () => string | null;\n readonly provider?: Provider | null;\n readonly config?: EntityResolutionConfig;\n}\n\nconst ADJUDICATION_SYSTEM_PROMPT =\n 'You decide whether two short names refer to the SAME real-world entity ' +\n '(person, place, org, thing). Reply with a single word: \"yes\" or \"no\". ' +\n 'Be conservative - answer \"no\" unless they are clearly the same entity.';\n\n/** Build the (pure) adjudication request. Exposed for testing. */\nexport function buildAdjudicationRequest(\n nameA: string,\n nameB: string,\n options: { readonly signal?: AbortSignal } = {},\n): ProviderRequest {\n return {\n messages: [{ role: 'user', content: `Name A: ${nameA}\\nName B: ${nameB}\\nSame entity?` }],\n systemMessage: ADJUDICATION_SYSTEM_PROMPT,\n temperature: 0,\n maxTokens: 4,\n ...(options.signal !== undefined ? { signal: options.signal } : {}),\n };\n}\n\n/** Parse a yes/no adjudication reply. Conservative: only a clear yes is `true`. */\nexport function parseAdjudication(text: string): boolean {\n return /^\\s*(yes|true|same)\\b/i.test(text);\n}\n\n/**\n * Resolves a fact's subject / object strings to canonical entity ids and\n * links them, applying {@link resolveEntityDecision} backed by an\n * injected store + embedder (+ optional provider for adjudication).\n * Constructed only when entity resolution is opted in\n * (`createMemory({ graph: { entityResolution: true } })`); otherwise the\n * write path skips it and behaviour is unchanged + offline.\n *\n * @stable\n */\nexport class EntityResolver {\n readonly #store: GraphMemoryStoreExt;\n readonly #embedder: EmbedderProvider | null;\n readonly #embedderId: () => string | null;\n readonly #provider: Provider | null;\n readonly #mergeThreshold: number;\n readonly #adjudicateThreshold: number;\n readonly #llmAdjudication: boolean;\n\n constructor(deps: EntityResolverDeps) {\n this.#store = deps.store;\n this.#embedder = deps.embedder ?? null;\n this.#embedderId = deps.embedderId ?? (() => null);\n this.#provider = deps.provider ?? null;\n this.#mergeThreshold = deps.config?.mergeThreshold ?? DEFAULT_MERGE_THRESHOLD;\n this.#adjudicateThreshold = deps.config?.adjudicateThreshold ?? DEFAULT_ADJUDICATE_THRESHOLD;\n this.#llmAdjudication = deps.config?.llmAdjudication ?? false;\n }\n\n /**\n * Resolve a single name to a canonical entity id (find-or-create),\n * deduping via lexical + embedding similarity. Returns `null` for a\n * name that normalizes to empty (no entity).\n */\n async resolve(\n scope: SessionScope,\n rawName: string,\n opts: { readonly signal?: AbortSignal } = {},\n ): Promise<string | null> {\n const normalizedName = normalizeEntityName(rawName);\n if (normalizedName.length === 0) return null;\n // 1. Exact lexical match - the cheapest, strongest signal. Resolve it via\n // an uncapped indexed lookup so an alias of an arbitrarily-old entity\n // dedups without scanning (and deserializing) the bounded candidate\n // window, and short-circuits before any embedding call (CS-11). Stores\n // that don't implement it fall through to the capped lexical scan below.\n const exact = await this.#store.findEntityByNormalizedName?.(scope, normalizedName);\n if (exact != null) return exact.id;\n const vector = await this.#embed(rawName, opts.signal);\n // 2. Without a query vector, embedding dedup is impossible - skip the\n // BLOB-deserializing candidate scan entirely and mint a new entity.\n if (vector === null || vector.length === 0) {\n return this.#create(scope, rawName, normalizedName, vector);\n }\n const candidates = await this.#store.listEntities(scope);\n const decision = resolveEntityDecision({\n normalizedName,\n vector,\n vectorEmbedderId: this.#embedderId(),\n candidates,\n mergeThreshold: this.#mergeThreshold,\n adjudicateThreshold: this.#adjudicateThreshold,\n });\n if (decision.kind === 'match') return decision.entityId;\n if (decision.kind === 'ambiguous') {\n if (this.#llmAdjudication && this.#provider !== null) {\n const same = await this.#adjudicate(rawName, decision.entityId, candidates, opts.signal);\n if (same) return decision.entityId;\n }\n // Conservative default: weak evidence ⇒ a distinct new entity.\n }\n return this.#create(scope, rawName, normalizedName, vector);\n }\n\n /**\n * Resolve + link a fact's subject and object (the predicate is a\n * relation label, never an entity). Idempotent on re-link.\n */\n async linkFact(\n scope: SessionScope,\n fact: Fact,\n opts: { readonly signal?: AbortSignal } = {},\n ): Promise<void> {\n if (fact.subject !== undefined) {\n const id = await this.resolve(scope, fact.subject, opts);\n if (id !== null) await this.#store.linkFactEntity(fact.id, id, 'subject');\n }\n if (fact.object !== undefined) {\n const id = await this.resolve(scope, fact.object, opts);\n if (id !== null) await this.#store.linkFactEntity(fact.id, id, 'object');\n }\n }\n\n async #embed(name: string, signal?: AbortSignal): Promise<Float32Array | null> {\n const embedder = this.#embedder;\n if (embedder === null || this.#embedderId() === null) return null;\n void signal;\n try {\n const [vector] = await embedder.embed([name]);\n return vector ?? null;\n } catch {\n return null;\n }\n }\n\n async #create(\n scope: SessionScope,\n name: string,\n normalizedName: string,\n vector: Float32Array | null,\n ): Promise<string> {\n const embedderId = this.#embedderId();\n return this.#store.upsertEntity(scope, {\n name,\n normalizedName,\n ...(vector !== null ? { vector } : {}),\n ...(vector !== null && embedderId !== null ? { embedderId } : {}),\n });\n }\n\n async #adjudicate(\n name: string,\n candidateId: string,\n candidates: ReadonlyArray<EntityWithEmbedding>,\n signal?: AbortSignal,\n ): Promise<boolean> {\n const provider = this.#provider;\n if (provider === null) return false;\n const candidate = candidates.find((c) => c.id === candidateId);\n if (candidate === undefined) return false;\n try {\n const request = buildAdjudicationRequest(name, candidate.name, {\n ...(signal !== undefined ? { signal } : {}),\n });\n const response = await provider.generate(request);\n return parseAdjudication(response.text ?? '');\n } catch {\n return false;\n }\n }\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"entity-resolver.js","names":["bestId: string | null","#store","#embedder","#embedderId","#provider","#mergeThreshold","#adjudicateThreshold","#llmAdjudication","#embed","#create","#adjudicate"],"sources":["../../src/graph/entity-resolver.ts"],"sourcesContent":["/**\n * Entity resolution for the lightweight in-SQLite relation graph (P2-1).\n *\n * Raw `subject` / `object` strings on facts fragment (\"Anna\", \"Anna S.\",\n * \"my sister\" become unrelated rows), which kills multi-hop recall. This\n * module folds them into **canonical entities** so the one-hop CTE can\n * traverse relationships. Resolution is layered, cheapest first:\n *\n * 1. **lexical** - exact match on the folded {@link normalizeEntityName}.\n * 2. **embedding** - cosine over candidate name vectors; `≥ mergeThreshold`\n * reuses the match.\n * 3. **adjudication** - a middle similarity band (`[adjudicate, merge)`)\n * is *ambiguous*; an opt-in LLM call (provider + `llmAdjudication`)\n * decides. **Offline / by default the ambiguous band mints a new\n * entity** - the resolver never auto-merges on weak evidence, because\n * a wrong merge fuses two distinct people (the stated P2-1 risk).\n *\n * The pure policy ({@link resolveEntityDecision}) is provider-agnostic and\n * does no I/O; {@link EntityResolver} wires it to an injected store +\n * embedder + optional provider. With no embedder it degrades to\n * lexical-only - still useful, and still fully offline.\n *\n * @packageDocumentation\n */\n\nimport type {\n EmbedderProvider,\n Fact,\n Provider,\n ProviderRequest,\n SessionScope,\n} from '@graphorin/core';\nimport type { EntityWithEmbedding, GraphMemoryStoreExt } from '../internal/storage-adapter.js';\n\n/** Cosine `≥` this auto-reuses an existing entity (embedding match). */\nexport const DEFAULT_MERGE_THRESHOLD = 0.92;\n/** Cosine in `[this, merge)` is *ambiguous* - adjudicate or mint new. */\nexport const DEFAULT_ADJUDICATE_THRESHOLD = 0.82;\n\n/**\n * Outcome of the pure resolution policy. `match` reuses an existing\n * entity; `ambiguous` flags a middle-similarity candidate for the caller\n * to adjudicate (or conservatively reject); `new` mints a fresh entity.\n *\n * @stable\n */\nexport type EntityResolveDecision =\n | {\n readonly kind: 'match';\n readonly entityId: string;\n readonly similarity: number;\n readonly via: 'lexical' | 'embedding';\n }\n | { readonly kind: 'ambiguous'; readonly entityId: string; readonly similarity: number }\n | { readonly kind: 'new' };\n\n/** Minimal candidate shape the pure policy compares against. */\nexport interface ResolutionCandidate {\n readonly id: string;\n readonly normalizedName: string;\n readonly vector: Float32Array | null;\n /**\n * MST-11: the embedder that produced `vector`. When both this and the\n * query's `vectorEmbedderId` are known and differ, the candidate is skipped\n * for embedding comparison - vectors from different models live in different\n * spaces, so their cosine is meaningless. Absent on either side ⇒ compared\n * (byte-identical to the prior behaviour).\n */\n readonly embedderId?: string | null;\n}\n\n/** Inputs to {@link resolveEntityDecision} (all provided by the caller). */\nexport interface ResolveDecisionInput {\n readonly normalizedName: string;\n readonly vector?: Float32Array | null;\n /** MST-11: the embedder that produced `vector` (gates cross-embedder cosine). */\n readonly vectorEmbedderId?: string | null;\n readonly candidates: ReadonlyArray<ResolutionCandidate>;\n readonly mergeThreshold: number;\n readonly adjudicateThreshold: number;\n}\n\n/**\n * Fold an entity surface form into a canonical lexical key: Unicode\n * NFKC, lowercased, internal whitespace collapsed, surrounding\n * punctuation stripped. `\" Anna S. \"` → `\"anna s\"`. Returns `''` for a\n * name with no letters/digits (the resolver treats that as \"no entity\").\n *\n * @stable\n */\nexport function normalizeEntityName(name: string): string {\n return name\n .normalize('NFKC')\n .toLowerCase()\n .replace(/\\s+/g, ' ')\n .trim()\n .replace(/^[^\\p{L}\\p{N}]+|[^\\p{L}\\p{N}]+$/gu, '');\n}\n\n/**\n * Cosine similarity of two embeddings in `[-1, 1]`. Compares over the\n * shorter length and returns `0` when either vector is empty / zero-norm.\n *\n * @stable\n */\nexport function cosineSimilarity(a: Float32Array, b: Float32Array): number {\n const len = Math.min(a.length, b.length);\n let dot = 0;\n let na = 0;\n let nb = 0;\n for (let i = 0; i < len; i++) {\n const ai = a[i] ?? 0;\n const bi = b[i] ?? 0;\n dot += ai * bi;\n na += ai * ai;\n nb += bi * bi;\n }\n if (na === 0 || nb === 0) return 0;\n return dot / Math.sqrt(na * nb);\n}\n\n/**\n * Pure resolution policy: lexical exact match → embedding cosine →\n * ambiguous band → new. No I/O; deterministic. The caller decides what\n * to do with `ambiguous` (LLM adjudicate, or conservatively mint new).\n *\n * @stable\n */\nexport function resolveEntityDecision(input: ResolveDecisionInput): EntityResolveDecision {\n // 1. Exact lexical match is the cheapest + strongest signal.\n for (const c of input.candidates) {\n if (c.normalizedName === input.normalizedName) {\n return { kind: 'match', entityId: c.id, similarity: 1, via: 'lexical' };\n }\n }\n // 2. Embedding similarity (only when both sides have a vector).\n const v = input.vector;\n if (v === undefined || v === null || v.length === 0) return { kind: 'new' };\n let bestId: string | null = null;\n let bestSim = Number.NEGATIVE_INFINITY;\n for (const c of input.candidates) {\n if (c.vector === null || c.vector.length === 0) continue;\n // MST-11: never compare vectors across embedders - different models live\n // in different vector spaces, so their cosine is meaningless. Skip only\n // when both embedder ids are known and differ.\n if (\n input.vectorEmbedderId != null &&\n c.embedderId != null &&\n c.embedderId !== input.vectorEmbedderId\n ) {\n continue;\n }\n const sim = cosineSimilarity(v, c.vector);\n if (sim > bestSim) {\n bestSim = sim;\n bestId = c.id;\n }\n }\n if (bestId === null) return { kind: 'new' };\n if (bestSim >= input.mergeThreshold) {\n return { kind: 'match', entityId: bestId, similarity: bestSim, via: 'embedding' };\n }\n if (bestSim >= input.adjudicateThreshold) {\n return { kind: 'ambiguous', entityId: bestId, similarity: bestSim };\n }\n return { kind: 'new' };\n}\n\n/** Tunable thresholds + LLM-adjudication switch for {@link EntityResolver}. */\nexport interface EntityResolutionConfig {\n /** Cosine `≥` this auto-reuses a match. Default {@link DEFAULT_MERGE_THRESHOLD}. */\n readonly mergeThreshold?: number;\n /** Cosine in `[this, merge)` is ambiguous. Default {@link DEFAULT_ADJUDICATE_THRESHOLD}. */\n readonly adjudicateThreshold?: number;\n /**\n * Resolve the ambiguous band with one provider call. Requires a\n * `provider`. Default `false` ⇒ ambiguous mints a new entity (no\n * network call; never auto-merges on weak evidence).\n */\n readonly llmAdjudication?: boolean;\n}\n\n/** Construction deps for {@link EntityResolver}. */\nexport interface EntityResolverDeps {\n readonly store: GraphMemoryStoreExt;\n readonly embedder?: EmbedderProvider | null;\n readonly embedderId?: () => string | null;\n readonly provider?: Provider | null;\n readonly config?: EntityResolutionConfig;\n}\n\nconst ADJUDICATION_SYSTEM_PROMPT =\n 'You decide whether two short names refer to the SAME real-world entity ' +\n '(person, place, org, thing). Reply with a single word: \"yes\" or \"no\". ' +\n 'Be conservative - answer \"no\" unless they are clearly the same entity.';\n\n/** Build the (pure) adjudication request. Exposed for testing. */\nexport function buildAdjudicationRequest(\n nameA: string,\n nameB: string,\n options: { readonly signal?: AbortSignal } = {},\n): ProviderRequest {\n return {\n messages: [{ role: 'user', content: `Name A: ${nameA}\\nName B: ${nameB}\\nSame entity?` }],\n systemMessage: ADJUDICATION_SYSTEM_PROMPT,\n temperature: 0,\n maxTokens: 4,\n ...(options.signal !== undefined ? { signal: options.signal } : {}),\n };\n}\n\n/** Parse a yes/no adjudication reply. Conservative: only a clear yes is `true`. */\nexport function parseAdjudication(text: string): boolean {\n return /^\\s*(yes|true|same)\\b/i.test(text);\n}\n\n/**\n * Resolves a fact's subject / object strings to canonical entity ids and\n * links them, applying {@link resolveEntityDecision} backed by an\n * injected store + embedder (+ optional provider for adjudication).\n * Constructed only when entity resolution is opted in\n * (`createMemory({ graph: { entityResolution: true } })`); otherwise the\n * write path skips it and behaviour is unchanged + offline.\n *\n * @stable\n */\nexport class EntityResolver {\n readonly #store: GraphMemoryStoreExt;\n readonly #embedder: EmbedderProvider | null;\n readonly #embedderId: () => string | null;\n readonly #provider: Provider | null;\n readonly #mergeThreshold: number;\n readonly #adjudicateThreshold: number;\n readonly #llmAdjudication: boolean;\n\n constructor(deps: EntityResolverDeps) {\n this.#store = deps.store;\n this.#embedder = deps.embedder ?? null;\n this.#embedderId = deps.embedderId ?? (() => null);\n this.#provider = deps.provider ?? null;\n this.#mergeThreshold = deps.config?.mergeThreshold ?? DEFAULT_MERGE_THRESHOLD;\n this.#adjudicateThreshold = deps.config?.adjudicateThreshold ?? DEFAULT_ADJUDICATE_THRESHOLD;\n this.#llmAdjudication = deps.config?.llmAdjudication ?? false;\n }\n\n /**\n * Resolve a single name to a canonical entity id (find-or-create),\n * deduping via lexical + embedding similarity. Returns `null` for a\n * name that normalizes to empty (no entity).\n */\n async resolve(\n scope: SessionScope,\n rawName: string,\n opts: { readonly signal?: AbortSignal } = {},\n ): Promise<string | null> {\n const normalizedName = normalizeEntityName(rawName);\n if (normalizedName.length === 0) return null;\n // 1. Exact lexical match - the cheapest, strongest signal. Resolve it via\n // an uncapped indexed lookup so an alias of an arbitrarily-old entity\n // dedups without scanning (and deserializing) the bounded candidate\n // window, and short-circuits before any embedding call (CS-11). Stores\n // that don't implement it fall through to the capped lexical scan below.\n const exact = await this.#store.findEntityByNormalizedName?.(scope, normalizedName);\n if (exact != null) return exact.id;\n const vector = await this.#embed(rawName, opts.signal);\n // 2. Without a query vector, embedding dedup is impossible - skip the\n // BLOB-deserializing candidate scan entirely and mint a new entity.\n if (vector === null || vector.length === 0) {\n return this.#create(scope, rawName, normalizedName, vector);\n }\n const candidates = await this.#store.listEntities(scope);\n const decision = resolveEntityDecision({\n normalizedName,\n vector,\n vectorEmbedderId: this.#embedderId(),\n candidates,\n mergeThreshold: this.#mergeThreshold,\n adjudicateThreshold: this.#adjudicateThreshold,\n });\n if (decision.kind === 'match') return decision.entityId;\n if (decision.kind === 'ambiguous') {\n if (this.#llmAdjudication && this.#provider !== null) {\n const same = await this.#adjudicate(rawName, decision.entityId, candidates, opts.signal);\n if (same) return decision.entityId;\n }\n // Conservative default: weak evidence ⇒ a distinct new entity.\n }\n return this.#create(scope, rawName, normalizedName, vector);\n }\n\n /**\n * Resolve + link a fact's subject and object (the predicate is a\n * relation label, never an entity). Idempotent on re-link.\n */\n async linkFact(\n scope: SessionScope,\n fact: Fact,\n opts: { readonly signal?: AbortSignal } = {},\n ): Promise<void> {\n if (fact.subject !== undefined) {\n const id = await this.resolve(scope, fact.subject, opts);\n if (id !== null) await this.#store.linkFactEntity(fact.id, id, 'subject');\n }\n if (fact.object !== undefined) {\n const id = await this.resolve(scope, fact.object, opts);\n if (id !== null) await this.#store.linkFactEntity(fact.id, id, 'object');\n }\n }\n\n async #embed(name: string, signal?: AbortSignal): Promise<Float32Array | null> {\n const embedder = this.#embedder;\n if (embedder === null || this.#embedderId() === null) return null;\n try {\n // W-089: forward the caller's AbortSignal so a cancelled write\n // really interrupts a remote embedding. An abort lands in the\n // catch: resolve degrades to mint-new, which is the correct\n // cancellation semantics for a best-effort enrichment. No\n // `taskType` on purpose - entity names are compared name-to-name,\n // so both sides must use the same (document) embedding role.\n const [vector] = await embedder.embed([name], {\n ...(signal !== undefined ? { signal } : {}),\n });\n return vector ?? null;\n } catch {\n return null;\n }\n }\n\n async #create(\n scope: SessionScope,\n name: string,\n normalizedName: string,\n vector: Float32Array | null,\n ): Promise<string> {\n const embedderId = this.#embedderId();\n return this.#store.upsertEntity(scope, {\n name,\n normalizedName,\n ...(vector !== null ? { vector } : {}),\n ...(vector !== null && embedderId !== null ? { embedderId } : {}),\n });\n }\n\n async #adjudicate(\n name: string,\n candidateId: string,\n candidates: ReadonlyArray<EntityWithEmbedding>,\n signal?: AbortSignal,\n ): Promise<boolean> {\n const provider = this.#provider;\n if (provider === null) return false;\n const candidate = candidates.find((c) => c.id === candidateId);\n if (candidate === undefined) return false;\n try {\n const request = buildAdjudicationRequest(name, candidate.name, {\n ...(signal !== undefined ? { signal } : {}),\n });\n const response = await provider.generate(request);\n return parseAdjudication(response.text ?? '');\n } catch {\n return false;\n }\n }\n}\n"],"mappings":"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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LocaleMatch, LocalePack, LocalePatternEntry, LocaleSupersedeKind, defineLocalePack, evaluateMarkers } from "./conflict/locale-packs/types.js";
|
|
2
|
-
import { ConflictAuditDecision, ConflictAuditInputLike, ConflictAuditStage, ConflictMemoryStoreExt, ConsolidatorMemoryStoreExt, ConsolidatorRunFinish, ConsolidatorRunInput, ConsolidatorStatePatch, ConsolidatorStateRow, DecayMemoryStoreExt, DlqBatchInput, DlqBatchRow, EmbeddedWriteOptions, EmbeddingMetaRegistryLike, EpisodicMemoryStoreExt, InsightListOptions as InsightListOptions$1, InsightMemoryStoreExt, InsightSearchStoreOptions, MemoryStoreAdapter, PendingConflictInputLike, PendingConflictRowLike, SemanticMemoryStoreExt, SessionMemoryStoreExt, SessionMessageRecord } from "./internal/storage-adapter.js";
|
|
2
|
+
import { ConflictAuditDecision, ConflictAuditInputLike, ConflictAuditStage, ConflictMemoryStoreExt, ConsolidatorMemoryStoreExt, ConsolidatorRunFinish, ConsolidatorRunInput, ConsolidatorStatePatch, ConsolidatorStateRow, DecayMemoryStoreExt, DlqBatchInput, DlqBatchRow, EmbeddedWriteOptions, EmbeddingMetaRegistryLike, EpisodicMemoryStoreExt, GraphMemoryStoreExt, InsightListOptions as InsightListOptions$1, InsightMemoryStoreExt, InsightSearchStoreOptions, MemoryStoreAdapter, PendingConflictInputLike, PendingConflictRowLike, ProceduralMemoryStoreExt, SemanticMemoryStoreExt, SessionMemoryStoreExt, SessionMessageRecord } from "./internal/storage-adapter.js";
|
|
3
3
|
import { ConflictDecision, ConflictPipeline, ConflictPipelineDeps, ConflictPipelineOptions, ConflictStage, ConflictThresholds, DEFAULT_CONFLICT_THRESHOLDS, PipelineStage, ReconcileAction, ReconcileDecision, StageContext, StageOutcome, reconcileToConflictDecision } from "./conflict/types.js";
|
|
4
4
|
import { _resetBypassWarningForTesting, createConflictPipeline, runConflictPipeline } from "./conflict/pipeline.js";
|
|
5
5
|
import "./conflict/index.js";
|
|
@@ -10,7 +10,7 @@ import { DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_MERGE_THRESHOLD, EntityResolution
|
|
|
10
10
|
import { DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_MAX_ITERATIONS, DifficultyAssessment, DifficultyGateOptions, IterativeRetrievalDeps, IterativeRetrievalOptions, IterativeRetrievalResult, MAX_ITERATIONS_CEILING, RETRIEVAL_GRADE_SYSTEM_PROMPT, RetrievalGrade, RetrievalGradeOptions, RetrievalGrader, assessQueryDifficulty, buildGradeRequest, createProviderRetrievalGrader, parseGrade, runIterativeRetrieval } from "./search/iterative.js";
|
|
11
11
|
import { DEFAULT_MAX_QUERY_VARIANTS, HYDE_SYSTEM_PROMPT, QUERY_EXPANSION_SYSTEM_PROMPT, QueryTransformOptions, QueryTransformer, buildExpansionRequest, buildHydeRequest, createProviderQueryTransformer, parseHypothetical, parseQueryVariants } from "./search/query-transform.js";
|
|
12
12
|
import { ReRankOptions, ReRanker } from "./search/types.js";
|
|
13
|
-
import { FactInput, FactRememberOptions, FactSearchOptions, FusionStrategy, FusionWeights, IterativeRecallResult, IterativeSearchOptions, RememberOutcome, SemanticMemory } from "./tiers/semantic-memory.js";
|
|
13
|
+
import { FactInput, FactRememberOptions, FactSearchOptions, FusionStrategy, FusionWeights, IterativeRecallResult, IterativeSearchOptions, RememberOutcome, SemanticMemory, SemanticSearchDefaults } from "./tiers/semantic-memory.js";
|
|
14
14
|
import { BlockDefinition, BlockSpec, WorkingMemory, defineBlock } from "./tiers/working-memory.js";
|
|
15
15
|
import { CONSOLIDATOR_TIER_DEFAULTS, ConsolidatorBudgetSnapshot, ConsolidatorCeilings, ConsolidatorConfig, ConsolidatorLastRuns, ConsolidatorPhase, ConsolidatorStatus, ConsolidatorTier, ConsolidatorTriggerReason, ConsolidatorTriggerSpec, CreateConsolidatorOptions, OnBudgetExceed, PhaseListener, PhaseOutcome } from "./consolidator/types.js";
|
|
16
16
|
import { ParsedTrigger, parseTriggerSpec, reasonFromTrigger } from "./consolidator/triggers.js";
|
|
@@ -27,32 +27,35 @@ import { ContextTokenCounter, HEURISTIC_TOKEN_COUNTER, adaptTokenCounter, countM
|
|
|
27
27
|
import { CLEARED_TOOL_RESULT_MARKER, ClearToolResultsOptions, ClearToolResultsOutcome, clearOldToolResults } from "./context-engine/compaction/clear-tool-results.js";
|
|
28
28
|
import { CompactionConfig, CompactionContext, CompactionResult, CompactionSource, CompactionStrategy, CompactionSummarizer, CompactionTriggerConfig, PostCompactionHook, PostCompactionHookContext } from "./context-engine/compaction/types.js";
|
|
29
29
|
import { DEFAULT_PRESERVE_RECENT_TURNS, ExecuteCompactionInput, executeCompaction } from "./context-engine/compaction/compactor.js";
|
|
30
|
+
import { PartitionResult, PrivacyDecision, PrivacyDecisionReason, PrivacyFilterContext, decide, effectiveAcceptsSensitivity, partition } from "./context-engine/privacy-filter.js";
|
|
31
|
+
import { MemoryBaseMode, SkillMetadataCard, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills } from "./context-engine/templates/composer.js";
|
|
32
|
+
import { AllocationResult, DEFAULT_LAYER_PRIORITY, LayerAllocation, LayerCandidate, LayerId, OverflowMode, allocate, truncateToTokens } from "./context-engine/token-budget.js";
|
|
33
|
+
import { AnnotatedPart, AssembleInput, AssembledPrompt, CompileOptions, CompileScope, MemoryContextBlocks, ResolvedContextEngineConfig } from "./context-engine/io-types.js";
|
|
34
|
+
import { InsightListOptions, InsightMemory, InsightSearchOptions, capInsightsBelowFacts } from "./tiers/insight-memory.js";
|
|
35
|
+
import { InduceOptions, ProceduralMemory, RuleActivationContext, RuleInput } from "./tiers/procedural-memory.js";
|
|
36
|
+
import { SessionCompactionPolicy, SessionCompactionResult, SessionMemory } from "./tiers/session-memory.js";
|
|
37
|
+
import { SharedMemory } from "./tiers/shared-memory.js";
|
|
38
|
+
import { ContextEngine, Memory } from "./memory-interface.js";
|
|
30
39
|
import { NamedPostCompactionHook } from "./context-engine/compaction/hooks/types.js";
|
|
31
40
|
import { reanchorPersonaBlock } from "./context-engine/compaction/hooks/reanchor-persona-block.js";
|
|
32
41
|
import { reanchorPinnedFacts } from "./context-engine/compaction/hooks/reanchor-pinned-facts.js";
|
|
33
42
|
import { reanchorProjectRules } from "./context-engine/compaction/hooks/reanchor-project-rules.js";
|
|
34
43
|
import { ReanchorRecentResultsOptions, reanchorRecentResults } from "./context-engine/compaction/hooks/reanchor-recent-results.js";
|
|
44
|
+
import { COMPACTION_SUMMARY_CLOSE, COMPACTION_SUMMARY_MARKER, COMPACTION_SUMMARY_OPEN, COMPACTION_SUMMARY_TAG } from "./context-engine/compaction/templates/marker.js";
|
|
35
45
|
import { CompactionMetadataPayload, RenderedTemplate, SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, buildSummarizerPrompt, renderFinalSummary } from "./context-engine/compaction/templates/summary-9-section.js";
|
|
36
46
|
import { AutoCompactionDefault, DEFAULT_RESERVED_FOR_COMPACTION, DEFAULT_RESERVED_FOR_RESPONSE, DEFAULT_THRESHOLD_RATIO, resolveAutoCompactionDefault, resolveTriggerThreshold } from "./context-engine/compaction/thresholds.js";
|
|
37
|
-
import {
|
|
38
|
-
import { MemoryBaseMode, SkillMetadataCard, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills } from "./context-engine/templates/composer.js";
|
|
39
|
-
import { AllocationResult, DEFAULT_LAYER_PRIORITY, LayerAllocation, LayerCandidate, LayerId, OverflowMode, allocate, truncateToTokens } from "./context-engine/token-budget.js";
|
|
40
|
-
import { AnnotatedPart, AssembleInput, AssembledPrompt, AutoRecallConfig, ContextEngine, ContextEngineConfig, LayerConfig, PrivacyConfig, ResolvedContextEngineConfig, createContextEngine } from "./context-engine/engine.js";
|
|
47
|
+
import { AutoRecallConfig, ContextEngineConfig, LayerConfig, PrivacyConfig, createContextEngine } from "./context-engine/engine.js";
|
|
41
48
|
import { LocaleResolverLogger, _getLocaleFallbackWarningsForTesting, _resetLocaleFallbackWarningsForTesting, resolveLocalePack } from "./context-engine/locale-packs/resolver.js";
|
|
42
49
|
import { MemoryMetadataDeps, gatherMemoryMetadata, renderMetadataBlock } from "./context-engine/metadata.js";
|
|
43
50
|
import { INBOUND_SANITIZATION_PREAMBLE_EN } from "./context-engine/preambles/inbound-en.js";
|
|
44
51
|
import { BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL } from "./context-engine/templates/base-en.js";
|
|
45
|
-
import
|
|
46
|
-
import { InsightListOptions, InsightMemory, InsightSearchOptions, capInsightsBelowFacts } from "./tiers/insight-memory.js";
|
|
47
|
-
import { InduceOptions, ProceduralMemory, RuleActivationContext, RuleInput } from "./tiers/procedural-memory.js";
|
|
48
|
-
import { SessionCompactionPolicy, SessionCompactionResult, SessionMemory } from "./tiers/session-memory.js";
|
|
49
|
-
import { SharedMemory } from "./tiers/shared-memory.js";
|
|
52
|
+
import "./context-engine/index.js";
|
|
50
53
|
import { MemoryToolDeps, ScopeResolver } from "./tools/types.js";
|
|
51
54
|
import { createBlockAppendTool, createBlockReplaceTool, createBlockRethinkTool } from "./tools/block-tools.js";
|
|
52
55
|
import { createFactForgetTool, createFactHistoryTool, createFactRememberTool, createFactSearchTool, createFactSupersedeTool, createFactValidateTool } from "./tools/fact-tools.js";
|
|
53
56
|
import { createConversationSearchTool, createDeepRecallTool, createRecallEpisodesTool } from "./tools/recall-tools.js";
|
|
54
57
|
import { BuildMemoryToolsOptions, buildMemoryTools } from "./tools/index.js";
|
|
55
|
-
import { CreateMemoryOptions,
|
|
58
|
+
import { CreateMemoryOptions, _resetConsolidatorConfigWarningForTesting, createMemory } from "./facade.js";
|
|
56
59
|
import "./graph/index.js";
|
|
57
60
|
import { EmbedderMigrationStrategy, MigrateEmbedderOptions, MigrationProgress, migrateEmbedder } from "./migration/embedder-migration.js";
|
|
58
61
|
import "./migration/index.js";
|
|
@@ -100,8 +103,7 @@ import "./tiers/index.js";
|
|
|
100
103
|
*
|
|
101
104
|
* @packageDocumentation
|
|
102
105
|
*/
|
|
103
|
-
|
|
104
|
-
declare const VERSION = "0.6.0";
|
|
106
|
+
declare const VERSION: string;
|
|
105
107
|
//#endregion
|
|
106
|
-
export { type AllocationResult, type AnnotatedPart, type AssembleInput, type AssembledPrompt, type AutoCompactionDefault, type AutoRecallConfig, type AutoRecallStrategy, type AutoRecallStrategyContext, type AutoRecallTriggerResult, type AutoRecallTriggers, BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL, type BaseTemplateFragments, BlockDefinition, BlockSpec, BudgetExceededError, type BuildMemoryToolsOptions, CLEARED_TOOL_RESULT_MARKER, CONSOLIDATOR_TIER_DEFAULTS, CONTENT_ORIGIN_ATTR, type ClearToolResultsOptions, type ClearToolResultsOutcome, type CompactionConfig, type CompactionContext, type CompactionMetadataPayload, type CompactionResult, type CompactionSource, type CompactionStrategy, type CompactionSummarizer, type CompactionSummaryTemplate, type CompactionTriggerConfig, type CompileOptions, type CompileScope, type ConflictAuditDecision, type ConflictAuditInputLike, type ConflictAuditStage, ConflictDecision, type ConflictMemoryStoreExt, ConflictPipeline, ConflictPipelineDeps, ConflictPipelineOptions, ConflictStage, ConflictThresholds, type Consolidator, type ConsolidatorBudgetSnapshot, type ConsolidatorCatchupPolicy, type ConsolidatorCeilings, type ConsolidatorConfig, type ConsolidatorLastRuns, type ConsolidatorMemoryStoreExt, type ConsolidatorPhase, type ConsolidatorRunFinish, type ConsolidatorRunInput, type ConsolidatorStatePatch, type ConsolidatorStateRow, type ConsolidatorStatus, type ConsolidatorTier, type ConsolidatorTriggerReason, type ConsolidatorTriggerSpec, type ContentAnnotation, type ContentOrigin, type ContextEngine, type ContextEngineConfig, type ContextLocalePack, type ContextTokenCounter, type ContextualRetrievalMode, type CreateConsolidatorOptions, type CreateMemoryOptions, CustomTierMisconfiguredError, DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_CONFLICT_THRESHOLDS, DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_INDUCTION_MAX_TOKENS, DEFAULT_LAYER_PRIORITY, DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_QUERY_VARIANTS, DEFAULT_MERGE_THRESHOLD, DEFAULT_PPR_DAMPING, DEFAULT_PRESERVE_RECENT_TURNS, DEFAULT_RESERVED_FOR_COMPACTION, DEFAULT_RESERVED_FOR_RESPONSE, DEFAULT_THRESHOLD_RATIO, type DecayMemoryStoreExt, DifficultyAssessment, DifficultyGateOptions, type DlqBatchInput, type DlqBatchRow, type EmbeddedWriteOptions, EmbedderMigrationAbortedError, EmbedderMigrationLockedError, EmbedderMigrationStateError, EmbedderMigrationStrategy, EmbedderRegistrationError, type EmbeddingMetaRegistryLike, EntityResolutionConfig, EntityResolveDecision, EntityResolver, EntityResolverDeps, EpisodeInput, EpisodeRetrievalWeights, EpisodeSearchOptions, EpisodicMemory, type EpisodicMemoryStoreExt, type ExecuteCompactionInput, FactInput, FactRememberOptions, FactSearchOptions, FitFusionCase, FitFusionOptions, FitFusionResult, FusionStrategy, FusionWeights, GraphorinMemoryError, HEURISTIC_TOKEN_COUNTER, HYDE_SYSTEM_PROMPT, INBOUND_SANITIZATION_PREAMBLE_EN, INBOUND_TRUST_ATTR, INDUCTION_SYSTEM_PROMPT, type InboundSanitizationPreamble, type InboundTrust, InduceOptions, type InducedProcedure, InsightListOptions, InsightMemory, type InsightMemoryStoreExt, InsightSearchOptions, type InsightSearchStoreOptions, type InsightListOptions$1 as InsightStoreListOptions, IterativeRecallResult, IterativeRetrievalDeps, IterativeRetrievalOptions, IterativeRetrievalResult, IterativeSearchOptions, type LayerAllocation, type LayerCandidate, type LayerConfig, type LayerId, LocaleMatch, LocalePack, LocalePatternEntry, type LocaleResolverLogger, LocaleSupersedeKind, MAX_ITERATIONS_CEILING, MAX_PROCEDURE_STEPS, MAX_TRAJECTORY_STEPS_SHOWN, type Memory, type MemoryBaseMode, type MemoryContextBlocks, type MemoryMetadataDeps, type MemoryStoreAdapter, MemoryToolDeniedError, type MemoryToolDeps, MigrateEmbedderOptions, MigrationProgress, NON_INBOUND_ORIGINS, type NamedPostCompactionHook, type OnBudgetExceed, type OverflowMode, type ParsedTrigger, type PartialContextLocalePack, type PartitionResult, type PendingConflictInputLike, type PendingConflictRowLike, type PhaseListener, type PhaseOutcome, PipelineStage, type PostCompactionHook, type PostCompactionHookContext, type PrivacyConfig, type PrivacyDecision, type PrivacyDecisionReason, type PrivacyFilterContext, ProceduralMemory, ProcedureInductionNotConfiguredError, ProviderNotConfiguredError, QUERY_EXPANSION_SYSTEM_PROMPT, QuarantinePromotionRefusedError, QueryTransformOptions, QueryTransformer, RETRIEVAL_GRADE_SYSTEM_PROMPT, RRFReranker, RRF_DEFAULT_K, ReRankOptions, ReRanker, type ReanchorRecentResultsOptions, RecallExplanation, RecalledMemoryExplanation, ReconcileAction, ReconcileDecision, type RegisterTriggersOptions, type RegisterTriggersResult, RememberOutcome, type RenderedTemplate, ResolutionCandidate, ResolveDecisionInput, type ResolvedContextEngineConfig, RetrievalGrade, RetrievalGradeOptions, RetrievalGrader, RuleActivationContext, RuleInput, SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, type SchedulerLike, type ScopeResolver, SemanticMemory, type SemanticMemoryStoreExt, SessionCompactionPolicy, SessionCompactionResult, SessionMemory, type SessionMemoryStoreExt, type SessionMessageRecord, SharedMemory, type SkillMetadataCard, StageContext, StageOutcome, type Trajectory, type TrajectoryStep, type TriggerDeclarationLike, VERSION, type VerificationResult, WeightedRRFReranker, type WorkflowInducer, type WorkflowInductionOptions, WorkingBlockOverflowError, WorkingBlockReadOnlyError, WorkingBlockReplaceMismatchError, WorkingMemory, _getLocaleFallbackWarningsForTesting, _resetBypassWarningForTesting, _resetConsolidatorConfigWarningForTesting, _resetLocaleFallbackWarningsForTesting, adaptTokenCounter, allocate as allocateTokenBudget, annotate, assessQueryDifficulty, buildAdjudicationRequest, buildExpansionRequest, buildGradeRequest, buildHydeRequest, buildInductionRequest, buildMemoryTools, buildSummarizerPrompt, capInsightsBelowFacts, checkSuccessCriteria, clearOldToolResults, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills, cosineSimilarity, countMessageTokens, createBlockAppendTool, createBlockReplaceTool, createBlockRethinkTool, createConflictPipeline, createConsolidator, createConsolidatorPlaceholder, createContextEngine, createConversationSearchTool, createDeepRecallTool, createFactForgetTool, createFactHistoryTool, createFactRememberTool, createFactSearchTool, createFactSupersedeTool, createFactValidateTool, createMemory, createProviderQueryTransformer, createProviderRetrievalGrader, createProviderWorkflowInducer, createRecallEpisodesTool, defaultLocaleHeuristicStrategy, defineAutoRecallStrategy, defineBlock, defineContextLocalePack, defineLocalePack, effectiveAcceptsSensitivity, enLocalePack, evaluateMarkers, executeCompaction, explainRecall, fitFusionWeights, formatRecallExplanation, fuseRrf, fuseWeighted, gatherMemoryMetadata, isForeignProvenance, migrateEmbedder, ndcgAtK, normalizeEntityName, normalizeInducedProcedure, parseAdjudication, parseGrade, parseHypothetical, parseInducedProcedure, parseQueryVariants, parseTriggerSpec, partition as partitionBySensitivity, pprActivation, decide as privacyDecide, reanchorPersonaBlock, reanchorPinnedFacts, reanchorProjectRules, reanchorRecentResults, reasonFromTrigger, reconcileToConflictDecision, registerConsolidatorTriggers, renderFinalSummary, renderMessageText, renderMetadataBlock, resolveAutoCompactionDefault, resolveEntityDecision, resolveLocalePack, resolveTriggerThreshold, runConflictPipeline, runIterativeRetrieval, runWorkflowInduction, shouldFireInboundPreamble, toSpanAttributes, trajectoryFromRunState, truncateToTokens, trustDiscount };
|
|
108
|
+
export { type AllocationResult, type AnnotatedPart, type AssembleInput, type AssembledPrompt, type AutoCompactionDefault, type AutoRecallConfig, type AutoRecallStrategy, type AutoRecallStrategyContext, type AutoRecallTriggerResult, type AutoRecallTriggers, BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL, type BaseTemplateFragments, BlockDefinition, BlockSpec, BudgetExceededError, type BuildMemoryToolsOptions, CLEARED_TOOL_RESULT_MARKER, COMPACTION_SUMMARY_CLOSE, COMPACTION_SUMMARY_MARKER, COMPACTION_SUMMARY_OPEN, COMPACTION_SUMMARY_TAG, CONSOLIDATOR_TIER_DEFAULTS, CONTENT_ORIGIN_ATTR, type ClearToolResultsOptions, type ClearToolResultsOutcome, type CompactionConfig, type CompactionContext, type CompactionMetadataPayload, type CompactionResult, type CompactionSource, type CompactionStrategy, type CompactionSummarizer, type CompactionSummaryTemplate, type CompactionTriggerConfig, type CompileOptions, type CompileScope, type ConflictAuditDecision, type ConflictAuditInputLike, type ConflictAuditStage, ConflictDecision, type ConflictMemoryStoreExt, ConflictPipeline, ConflictPipelineDeps, ConflictPipelineOptions, ConflictStage, ConflictThresholds, type Consolidator, type ConsolidatorBudgetSnapshot, type ConsolidatorCatchupPolicy, type ConsolidatorCeilings, type ConsolidatorConfig, type ConsolidatorLastRuns, type ConsolidatorMemoryStoreExt, type ConsolidatorPhase, type ConsolidatorRunFinish, type ConsolidatorRunInput, type ConsolidatorStatePatch, type ConsolidatorStateRow, type ConsolidatorStatus, type ConsolidatorTier, type ConsolidatorTriggerReason, type ConsolidatorTriggerSpec, type ContentAnnotation, type ContentOrigin, type ContextEngine, type ContextEngineConfig, type ContextLocalePack, type ContextTokenCounter, type ContextualRetrievalMode, type CreateConsolidatorOptions, type CreateMemoryOptions, CustomTierMisconfiguredError, DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_CONFLICT_THRESHOLDS, DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_INDUCTION_MAX_TOKENS, DEFAULT_LAYER_PRIORITY, DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_QUERY_VARIANTS, DEFAULT_MERGE_THRESHOLD, DEFAULT_PPR_DAMPING, DEFAULT_PRESERVE_RECENT_TURNS, DEFAULT_RESERVED_FOR_COMPACTION, DEFAULT_RESERVED_FOR_RESPONSE, DEFAULT_THRESHOLD_RATIO, type DecayMemoryStoreExt, DifficultyAssessment, DifficultyGateOptions, type DlqBatchInput, type DlqBatchRow, type EmbeddedWriteOptions, EmbedderMigrationAbortedError, EmbedderMigrationLockedError, EmbedderMigrationStateError, EmbedderMigrationStrategy, EmbedderRegistrationError, type EmbeddingMetaRegistryLike, EntityResolutionConfig, EntityResolveDecision, EntityResolver, EntityResolverDeps, EpisodeInput, EpisodeRetrievalWeights, EpisodeSearchOptions, EpisodicMemory, type EpisodicMemoryStoreExt, type ExecuteCompactionInput, FactInput, FactRememberOptions, FactSearchOptions, FitFusionCase, FitFusionOptions, FitFusionResult, FusionStrategy, FusionWeights, type GraphMemoryStoreExt, GraphorinMemoryError, HEURISTIC_TOKEN_COUNTER, HYDE_SYSTEM_PROMPT, INBOUND_SANITIZATION_PREAMBLE_EN, INBOUND_TRUST_ATTR, INDUCTION_SYSTEM_PROMPT, type InboundSanitizationPreamble, type InboundTrust, InduceOptions, type InducedProcedure, InsightListOptions, InsightMemory, type InsightMemoryStoreExt, InsightSearchOptions, type InsightSearchStoreOptions, type InsightListOptions$1 as InsightStoreListOptions, IterativeRecallResult, IterativeRetrievalDeps, IterativeRetrievalOptions, IterativeRetrievalResult, IterativeSearchOptions, type LayerAllocation, type LayerCandidate, type LayerConfig, type LayerId, LocaleMatch, LocalePack, LocalePatternEntry, type LocaleResolverLogger, LocaleSupersedeKind, MAX_ITERATIONS_CEILING, MAX_PROCEDURE_STEPS, MAX_TRAJECTORY_STEPS_SHOWN, type Memory, type MemoryBaseMode, type MemoryContextBlocks, type MemoryMetadataDeps, type MemoryStoreAdapter, MemoryToolDeniedError, type MemoryToolDeps, MigrateEmbedderOptions, MigrationProgress, NON_INBOUND_ORIGINS, type NamedPostCompactionHook, type OnBudgetExceed, type OverflowMode, type ParsedTrigger, type PartialContextLocalePack, type PartitionResult, type PendingConflictInputLike, type PendingConflictRowLike, type PhaseListener, type PhaseOutcome, PipelineStage, type PostCompactionHook, type PostCompactionHookContext, type PrivacyConfig, type PrivacyDecision, type PrivacyDecisionReason, type PrivacyFilterContext, ProceduralMemory, type ProceduralMemoryStoreExt, ProcedureInductionNotConfiguredError, ProviderNotConfiguredError, QUERY_EXPANSION_SYSTEM_PROMPT, QuarantinePromotionRefusedError, QueryTransformOptions, QueryTransformer, RETRIEVAL_GRADE_SYSTEM_PROMPT, RRFReranker, RRF_DEFAULT_K, ReRankOptions, ReRanker, type ReanchorRecentResultsOptions, RecallExplanation, RecalledMemoryExplanation, ReconcileAction, ReconcileDecision, type RegisterTriggersOptions, type RegisterTriggersResult, RememberOutcome, type RenderedTemplate, ResolutionCandidate, ResolveDecisionInput, type ResolvedContextEngineConfig, RetrievalGrade, RetrievalGradeOptions, RetrievalGrader, RuleActivationContext, RuleInput, SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, type SchedulerLike, type ScopeResolver, SemanticMemory, type SemanticMemoryStoreExt, SemanticSearchDefaults, SessionCompactionPolicy, SessionCompactionResult, SessionMemory, type SessionMemoryStoreExt, type SessionMessageRecord, SharedMemory, type SkillMetadataCard, StageContext, StageOutcome, type Trajectory, type TrajectoryStep, type TriggerDeclarationLike, VERSION, type VerificationResult, WeightedRRFReranker, type WorkflowInducer, type WorkflowInductionOptions, WorkingBlockOverflowError, WorkingBlockReadOnlyError, WorkingBlockReplaceMismatchError, WorkingMemory, _getLocaleFallbackWarningsForTesting, _resetBypassWarningForTesting, _resetConsolidatorConfigWarningForTesting, _resetLocaleFallbackWarningsForTesting, adaptTokenCounter, allocate as allocateTokenBudget, annotate, assessQueryDifficulty, buildAdjudicationRequest, buildExpansionRequest, buildGradeRequest, buildHydeRequest, buildInductionRequest, buildMemoryTools, buildSummarizerPrompt, capInsightsBelowFacts, checkSuccessCriteria, clearOldToolResults, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills, cosineSimilarity, countMessageTokens, createBlockAppendTool, createBlockReplaceTool, createBlockRethinkTool, createConflictPipeline, createConsolidator, createConsolidatorPlaceholder, createContextEngine, createConversationSearchTool, createDeepRecallTool, createFactForgetTool, createFactHistoryTool, createFactRememberTool, createFactSearchTool, createFactSupersedeTool, createFactValidateTool, createMemory, createProviderQueryTransformer, createProviderRetrievalGrader, createProviderWorkflowInducer, createRecallEpisodesTool, defaultLocaleHeuristicStrategy, defineAutoRecallStrategy, defineBlock, defineContextLocalePack, defineLocalePack, effectiveAcceptsSensitivity, enLocalePack, evaluateMarkers, executeCompaction, explainRecall, fitFusionWeights, formatRecallExplanation, fuseRrf, fuseWeighted, gatherMemoryMetadata, isForeignProvenance, migrateEmbedder, ndcgAtK, normalizeEntityName, normalizeInducedProcedure, parseAdjudication, parseGrade, parseHypothetical, parseInducedProcedure, parseQueryVariants, parseTriggerSpec, partition as partitionBySensitivity, pprActivation, decide as privacyDecide, reanchorPersonaBlock, reanchorPinnedFacts, reanchorProjectRules, reanchorRecentResults, reasonFromTrigger, reconcileToConflictDecision, registerConsolidatorTriggers, renderFinalSummary, renderMessageText, renderMetadataBlock, resolveAutoCompactionDefault, resolveEntityDecision, resolveLocalePack, resolveTriggerThreshold, runConflictPipeline, runIterativeRetrieval, runWorkflowInduction, shouldFireInboundPreamble, toSpanAttributes, trajectoryFromRunState, truncateToTokens, trustDiscount };
|
|
107
109
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAuCa"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { version } from "./package.js";
|
|
1
2
|
import { defineLocalePack, evaluateMarkers } from "./conflict/locale-packs/types.js";
|
|
2
3
|
import { DEFAULT_CONFLICT_THRESHOLDS, reconcileToConflictDecision } from "./conflict/types.js";
|
|
3
4
|
import { _resetBypassWarningForTesting, createConflictPipeline, runConflictPipeline } from "./conflict/pipeline.js";
|
|
4
|
-
import "./conflict/index.js";
|
|
5
5
|
import { EmbedderMigrationAbortedError, EmbedderMigrationLockedError, EmbedderMigrationStateError, EmbedderRegistrationError, GraphorinMemoryError, MemoryToolDeniedError, ProcedureInductionNotConfiguredError, QuarantinePromotionRefusedError, WorkingBlockOverflowError, WorkingBlockReadOnlyError, WorkingBlockReplaceMismatchError } from "./errors/index.js";
|
|
6
6
|
import { BudgetExceededError, CustomTierMisconfiguredError, ProviderNotConfiguredError } from "./consolidator/errors.js";
|
|
7
7
|
import { DEFAULT_INDUCTION_MAX_TOKENS, INDUCTION_SYSTEM_PROMPT, MAX_PROCEDURE_STEPS, MAX_TRAJECTORY_STEPS_SHOWN, buildInductionRequest, checkSuccessCriteria, createProviderWorkflowInducer, normalizeInducedProcedure, parseInducedProcedure, runWorkflowInduction, trajectoryFromRunState } from "./consolidator/phases/induce.js";
|
|
@@ -15,6 +15,7 @@ import { CONTENT_ORIGIN_ATTR, INBOUND_TRUST_ATTR, NON_INBOUND_ORIGINS, annotate,
|
|
|
15
15
|
import { defaultLocaleHeuristicStrategy, defineAutoRecallStrategy } from "./context-engine/auto-recall.js";
|
|
16
16
|
import { HEURISTIC_TOKEN_COUNTER, adaptTokenCounter, countMessageTokens, renderMessageText } from "./context-engine/token-counter.js";
|
|
17
17
|
import { CLEARED_TOOL_RESULT_MARKER, clearOldToolResults } from "./context-engine/compaction/clear-tool-results.js";
|
|
18
|
+
import { COMPACTION_SUMMARY_CLOSE, COMPACTION_SUMMARY_MARKER, COMPACTION_SUMMARY_OPEN, COMPACTION_SUMMARY_TAG } from "./context-engine/compaction/templates/marker.js";
|
|
18
19
|
import { SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, buildSummarizerPrompt, renderFinalSummary } from "./context-engine/compaction/templates/summary-9-section.js";
|
|
19
20
|
import { DEFAULT_PRESERVE_RECENT_TURNS, executeCompaction } from "./context-engine/compaction/compactor.js";
|
|
20
21
|
import { reanchorPersonaBlock } from "./context-engine/compaction/hooks/reanchor-persona-block.js";
|
|
@@ -32,7 +33,6 @@ import { DEFAULT_LAYER_PRIORITY, allocate, truncateToTokens } from "./context-en
|
|
|
32
33
|
import { createContextEngine } from "./context-engine/engine.js";
|
|
33
34
|
import { INBOUND_SANITIZATION_PREAMBLE_EN } from "./context-engine/preambles/inbound-en.js";
|
|
34
35
|
import { BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL } from "./context-engine/templates/base-en.js";
|
|
35
|
-
import "./context-engine/index.js";
|
|
36
36
|
import { DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_MERGE_THRESHOLD, EntityResolver, buildAdjudicationRequest, cosineSimilarity, normalizeEntityName, parseAdjudication, resolveEntityDecision } from "./graph/entity-resolver.js";
|
|
37
37
|
import { DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_MAX_ITERATIONS, MAX_ITERATIONS_CEILING, RETRIEVAL_GRADE_SYSTEM_PROMPT, assessQueryDifficulty, buildGradeRequest, createProviderRetrievalGrader, parseGrade, runIterativeRetrieval } from "./search/iterative.js";
|
|
38
38
|
import { DEFAULT_MAX_QUERY_VARIANTS, HYDE_SYSTEM_PROMPT, QUERY_EXPANSION_SYSTEM_PROMPT, buildExpansionRequest, buildHydeRequest, createProviderQueryTransformer, parseHypothetical, parseQueryVariants } from "./search/query-transform.js";
|
|
@@ -53,8 +53,6 @@ import { buildMemoryTools } from "./tools/index.js";
|
|
|
53
53
|
import { _resetConsolidatorConfigWarningForTesting, createMemory } from "./facade.js";
|
|
54
54
|
import { migrateEmbedder } from "./migration/embedder-migration.js";
|
|
55
55
|
import { fitFusionWeights, ndcgAtK } from "./search/fit-weights.js";
|
|
56
|
-
import "./search/index.js";
|
|
57
|
-
import "./tiers/index.js";
|
|
58
56
|
|
|
59
57
|
//#region src/index.ts
|
|
60
58
|
/**
|
|
@@ -92,9 +90,9 @@ import "./tiers/index.js";
|
|
|
92
90
|
*
|
|
93
91
|
* @packageDocumentation
|
|
94
92
|
*/
|
|
95
|
-
/** Canonical version constant
|
|
96
|
-
const VERSION =
|
|
93
|
+
/** Canonical version constant, derived from `package.json` at build time. */
|
|
94
|
+
const VERSION = version;
|
|
97
95
|
|
|
98
96
|
//#endregion
|
|
99
|
-
export { BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL, BudgetExceededError, CLEARED_TOOL_RESULT_MARKER, CONSOLIDATOR_TIER_DEFAULTS, CONTENT_ORIGIN_ATTR, CustomTierMisconfiguredError, DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_CONFLICT_THRESHOLDS, DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_INDUCTION_MAX_TOKENS, DEFAULT_LAYER_PRIORITY, DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_QUERY_VARIANTS, DEFAULT_MERGE_THRESHOLD, DEFAULT_PPR_DAMPING, DEFAULT_PRESERVE_RECENT_TURNS, DEFAULT_RESERVED_FOR_COMPACTION, DEFAULT_RESERVED_FOR_RESPONSE, DEFAULT_THRESHOLD_RATIO, EmbedderMigrationAbortedError, EmbedderMigrationLockedError, EmbedderMigrationStateError, EmbedderRegistrationError, EntityResolver, EpisodicMemory, GraphorinMemoryError, HEURISTIC_TOKEN_COUNTER, HYDE_SYSTEM_PROMPT, INBOUND_SANITIZATION_PREAMBLE_EN, INBOUND_TRUST_ATTR, INDUCTION_SYSTEM_PROMPT, InsightMemory, MAX_ITERATIONS_CEILING, MAX_PROCEDURE_STEPS, MAX_TRAJECTORY_STEPS_SHOWN, MemoryToolDeniedError, NON_INBOUND_ORIGINS, ProceduralMemory, ProcedureInductionNotConfiguredError, ProviderNotConfiguredError, QUERY_EXPANSION_SYSTEM_PROMPT, QuarantinePromotionRefusedError, RETRIEVAL_GRADE_SYSTEM_PROMPT, RRFReranker, RRF_DEFAULT_K, SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, SemanticMemory, SessionMemory, SharedMemory, VERSION, WeightedRRFReranker, WorkingBlockOverflowError, WorkingBlockReadOnlyError, WorkingBlockReplaceMismatchError, WorkingMemory, _getLocaleFallbackWarningsForTesting, _resetBypassWarningForTesting, _resetConsolidatorConfigWarningForTesting, _resetLocaleFallbackWarningsForTesting, adaptTokenCounter, allocate as allocateTokenBudget, annotate, assessQueryDifficulty, buildAdjudicationRequest, buildExpansionRequest, buildGradeRequest, buildHydeRequest, buildInductionRequest, buildMemoryTools, buildSummarizerPrompt, capInsightsBelowFacts, checkSuccessCriteria, clearOldToolResults, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills, cosineSimilarity, countMessageTokens, createBlockAppendTool, createBlockReplaceTool, createBlockRethinkTool, createConflictPipeline, createConsolidator, createConsolidatorPlaceholder, createContextEngine, createConversationSearchTool, createDeepRecallTool, createFactForgetTool, createFactHistoryTool, createFactRememberTool, createFactSearchTool, createFactSupersedeTool, createFactValidateTool, createMemory, createProviderQueryTransformer, createProviderRetrievalGrader, createProviderWorkflowInducer, createRecallEpisodesTool, defaultLocaleHeuristicStrategy, defineAutoRecallStrategy, defineBlock, defineContextLocalePack, defineLocalePack, effectiveAcceptsSensitivity, enLocalePack, evaluateMarkers, executeCompaction, explainRecall, fitFusionWeights, formatRecallExplanation, fuseRrf, fuseWeighted, gatherMemoryMetadata, isForeignProvenance, migrateEmbedder, ndcgAtK, normalizeEntityName, normalizeInducedProcedure, parseAdjudication, parseGrade, parseHypothetical, parseInducedProcedure, parseQueryVariants, parseTriggerSpec, partition as partitionBySensitivity, pprActivation, decide as privacyDecide, reanchorPersonaBlock, reanchorPinnedFacts, reanchorProjectRules, reanchorRecentResults, reasonFromTrigger, reconcileToConflictDecision, registerConsolidatorTriggers, renderFinalSummary, renderMessageText, renderMetadataBlock, resolveAutoCompactionDefault, resolveEntityDecision, resolveLocalePack, resolveTriggerThreshold, runConflictPipeline, runIterativeRetrieval, runWorkflowInduction, shouldFireInboundPreamble, toSpanAttributes, trajectoryFromRunState, truncateToTokens, trustDiscount };
|
|
97
|
+
export { BASE_TEMPLATE_EN_FULL, BASE_TEMPLATE_EN_MINIMAL, BudgetExceededError, CLEARED_TOOL_RESULT_MARKER, COMPACTION_SUMMARY_CLOSE, COMPACTION_SUMMARY_MARKER, COMPACTION_SUMMARY_OPEN, COMPACTION_SUMMARY_TAG, CONSOLIDATOR_TIER_DEFAULTS, CONTENT_ORIGIN_ATTR, CustomTierMisconfiguredError, DEFAULT_ADJUDICATE_THRESHOLD, DEFAULT_CONFLICT_THRESHOLDS, DEFAULT_DIFFICULTY_THRESHOLD, DEFAULT_INDUCTION_MAX_TOKENS, DEFAULT_LAYER_PRIORITY, DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_QUERY_VARIANTS, DEFAULT_MERGE_THRESHOLD, DEFAULT_PPR_DAMPING, DEFAULT_PRESERVE_RECENT_TURNS, DEFAULT_RESERVED_FOR_COMPACTION, DEFAULT_RESERVED_FOR_RESPONSE, DEFAULT_THRESHOLD_RATIO, EmbedderMigrationAbortedError, EmbedderMigrationLockedError, EmbedderMigrationStateError, EmbedderRegistrationError, EntityResolver, EpisodicMemory, GraphorinMemoryError, HEURISTIC_TOKEN_COUNTER, HYDE_SYSTEM_PROMPT, INBOUND_SANITIZATION_PREAMBLE_EN, INBOUND_TRUST_ATTR, INDUCTION_SYSTEM_PROMPT, InsightMemory, MAX_ITERATIONS_CEILING, MAX_PROCEDURE_STEPS, MAX_TRAJECTORY_STEPS_SHOWN, MemoryToolDeniedError, NON_INBOUND_ORIGINS, ProceduralMemory, ProcedureInductionNotConfiguredError, ProviderNotConfiguredError, QUERY_EXPANSION_SYSTEM_PROMPT, QuarantinePromotionRefusedError, RETRIEVAL_GRADE_SYSTEM_PROMPT, RRFReranker, RRF_DEFAULT_K, SUMMARY_TEMPLATE_NAME, SUMMARY_TEMPLATE_VERSION, SemanticMemory, SessionMemory, SharedMemory, VERSION, WeightedRRFReranker, WorkingBlockOverflowError, WorkingBlockReadOnlyError, WorkingBlockReplaceMismatchError, WorkingMemory, _getLocaleFallbackWarningsForTesting, _resetBypassWarningForTesting, _resetConsolidatorConfigWarningForTesting, _resetLocaleFallbackWarningsForTesting, adaptTokenCounter, allocate as allocateTokenBudget, annotate, assessQueryDifficulty, buildAdjudicationRequest, buildExpansionRequest, buildGradeRequest, buildHydeRequest, buildInductionRequest, buildMemoryTools, buildSummarizerPrompt, capInsightsBelowFacts, checkSuccessCriteria, clearOldToolResults, composeInboundPreamble, composeLayer1, composeLayer2, composeLayer4Skills, cosineSimilarity, countMessageTokens, createBlockAppendTool, createBlockReplaceTool, createBlockRethinkTool, createConflictPipeline, createConsolidator, createConsolidatorPlaceholder, createContextEngine, createConversationSearchTool, createDeepRecallTool, createFactForgetTool, createFactHistoryTool, createFactRememberTool, createFactSearchTool, createFactSupersedeTool, createFactValidateTool, createMemory, createProviderQueryTransformer, createProviderRetrievalGrader, createProviderWorkflowInducer, createRecallEpisodesTool, defaultLocaleHeuristicStrategy, defineAutoRecallStrategy, defineBlock, defineContextLocalePack, defineLocalePack, effectiveAcceptsSensitivity, enLocalePack, evaluateMarkers, executeCompaction, explainRecall, fitFusionWeights, formatRecallExplanation, fuseRrf, fuseWeighted, gatherMemoryMetadata, isForeignProvenance, migrateEmbedder, ndcgAtK, normalizeEntityName, normalizeInducedProcedure, parseAdjudication, parseGrade, parseHypothetical, parseInducedProcedure, parseQueryVariants, parseTriggerSpec, partition as partitionBySensitivity, pprActivation, decide as privacyDecide, reanchorPersonaBlock, reanchorPinnedFacts, reanchorProjectRules, reanchorRecentResults, reasonFromTrigger, reconcileToConflictDecision, registerConsolidatorTriggers, renderFinalSummary, renderMessageText, renderMetadataBlock, resolveAutoCompactionDefault, resolveEntityDecision, resolveLocalePack, resolveTriggerThreshold, runConflictPipeline, runIterativeRetrieval, runWorkflowInduction, shouldFireInboundPreamble, toSpanAttributes, trajectoryFromRunState, truncateToTokens, trustDiscount };
|
|
100
98
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@graphorin/memory` - seven-tier memory system for the Graphorin\n * framework (MST-13: working / session / episodic / semantic /\n * procedural / shared + the read-only insight tier from P1-1).\n *\n * Surface overview:\n *\n * - The {@link createMemory} facade that wires every tier sub-module\n * + the memory tools + the search reranker + the context-engine +\n * the consolidator.\n * - Tier sub-modules under `./tiers`: {@link WorkingMemory},\n * {@link SessionMemory}, {@link EpisodicMemory}, {@link SemanticMemory},\n * {@link ProceduralMemory}, {@link SharedMemory}, plus the read-only\n * {@link InsightMemory}.\n * - Eleven memory tools under `./tools` (plus the gated twelfth,\n * `deep_recall`, registered only when iterative retrieval is\n * configured): `block_append`, `block_replace`, `block_rethink`,\n * `fact_remember`, `fact_search`, `fact_supersede`, `fact_forget`,\n * `recall_episodes`, `conversation_search`, `fact_history` (P0-2),\n * `fact_validate` (P1-4).\n * - The hybrid search composition under `./search`, including the\n * built-in {@link RRFReranker} (k=60 default) and the\n * {@link ReRanker} contract.\n * - The embedder migration runner under `./migration`\n * ({@link migrateEmbedder} with three coexistence policies).\n * - Typed errors under `./errors`.\n *\n * Forward-looking surfaces (for ergonomic typing today):\n *\n * - {@link MemoryContextBlocks} + {@link CompileOptions} from `./context-engine`.\n * - {@link Consolidator}, {@link ConsolidatorTriggerSpec},\n * {@link ConsolidatorTier} from `./consolidator`.\n *\n * @packageDocumentation\n */\n\n/** Canonical version constant
|
|
1
|
+
{"version":3,"file":"index.js","names":["VERSION: string","pkg.version"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@graphorin/memory` - seven-tier memory system for the Graphorin\n * framework (MST-13: working / session / episodic / semantic /\n * procedural / shared + the read-only insight tier from P1-1).\n *\n * Surface overview:\n *\n * - The {@link createMemory} facade that wires every tier sub-module\n * + the memory tools + the search reranker + the context-engine +\n * the consolidator.\n * - Tier sub-modules under `./tiers`: {@link WorkingMemory},\n * {@link SessionMemory}, {@link EpisodicMemory}, {@link SemanticMemory},\n * {@link ProceduralMemory}, {@link SharedMemory}, plus the read-only\n * {@link InsightMemory}.\n * - Eleven memory tools under `./tools` (plus the gated twelfth,\n * `deep_recall`, registered only when iterative retrieval is\n * configured): `block_append`, `block_replace`, `block_rethink`,\n * `fact_remember`, `fact_search`, `fact_supersede`, `fact_forget`,\n * `recall_episodes`, `conversation_search`, `fact_history` (P0-2),\n * `fact_validate` (P1-4).\n * - The hybrid search composition under `./search`, including the\n * built-in {@link RRFReranker} (k=60 default) and the\n * {@link ReRanker} contract.\n * - The embedder migration runner under `./migration`\n * ({@link migrateEmbedder} with three coexistence policies).\n * - Typed errors under `./errors`.\n *\n * Forward-looking surfaces (for ergonomic typing today):\n *\n * - {@link MemoryContextBlocks} + {@link CompileOptions} from `./context-engine`.\n * - {@link Consolidator}, {@link ConsolidatorTriggerSpec},\n * {@link ConsolidatorTier} from `./consolidator`.\n *\n * @packageDocumentation\n */\n\n/** Canonical version constant, derived from `package.json` at build time. */\nimport pkg from '../package.json' with { type: 'json' };\n\nexport const VERSION: string = pkg.version;\n\nexport * from './conflict/index.js';\nexport {\n BudgetExceededError,\n buildInductionRequest,\n CONSOLIDATOR_TIER_DEFAULTS,\n type Consolidator,\n type ConsolidatorBudgetSnapshot,\n type ConsolidatorCatchupPolicy,\n type ConsolidatorCeilings,\n type ConsolidatorConfig,\n type ConsolidatorLastRuns,\n type ConsolidatorPhase,\n type ConsolidatorStatus,\n type ConsolidatorTier,\n type ConsolidatorTriggerReason,\n type ConsolidatorTriggerSpec,\n type CreateConsolidatorOptions,\n CustomTierMisconfiguredError,\n checkSuccessCriteria,\n createConsolidator,\n createConsolidatorPlaceholder,\n createProviderWorkflowInducer,\n DEFAULT_INDUCTION_MAX_TOKENS,\n INDUCTION_SYSTEM_PROMPT,\n type InducedProcedure,\n MAX_PROCEDURE_STEPS,\n MAX_TRAJECTORY_STEPS_SHOWN,\n normalizeInducedProcedure,\n type OnBudgetExceed,\n type ParsedTrigger,\n type PhaseListener,\n type PhaseOutcome,\n ProviderNotConfiguredError,\n parseInducedProcedure,\n parseTriggerSpec,\n type RegisterTriggersOptions,\n type RegisterTriggersResult,\n reasonFromTrigger,\n registerConsolidatorTriggers,\n runWorkflowInduction,\n type SchedulerLike,\n type Trajectory,\n type TrajectoryStep,\n type TriggerDeclarationLike,\n trajectoryFromRunState,\n type VerificationResult,\n type WorkflowInducer,\n type WorkflowInductionOptions,\n} from './consolidator/index.js';\nexport {\n _getLocaleFallbackWarningsForTesting,\n _resetLocaleFallbackWarningsForTesting,\n type AllocationResult,\n type AnnotatedPart,\n type AssembledPrompt,\n type AssembleInput,\n type AutoCompactionDefault,\n type AutoRecallConfig,\n type AutoRecallStrategy,\n type AutoRecallStrategyContext,\n type AutoRecallTriggerResult,\n type AutoRecallTriggers,\n adaptTokenCounter,\n allocateTokenBudget,\n annotate,\n BASE_TEMPLATE_EN_FULL,\n BASE_TEMPLATE_EN_MINIMAL,\n type BaseTemplateFragments,\n buildSummarizerPrompt,\n CLEARED_TOOL_RESULT_MARKER,\n type ClearToolResultsOptions,\n type ClearToolResultsOutcome,\n COMPACTION_SUMMARY_CLOSE,\n COMPACTION_SUMMARY_MARKER,\n COMPACTION_SUMMARY_OPEN,\n COMPACTION_SUMMARY_TAG,\n CONTENT_ORIGIN_ATTR,\n type CompactionConfig,\n type CompactionContext,\n type CompactionMetadataPayload,\n type CompactionResult,\n type CompactionSource,\n type CompactionStrategy,\n type CompactionSummarizer,\n type CompactionSummaryTemplate,\n type CompactionTriggerConfig,\n type CompileOptions,\n type CompileScope,\n type ContentAnnotation,\n type ContentOrigin,\n type ContextEngine,\n type ContextEngineConfig,\n type ContextLocalePack,\n type ContextTokenCounter,\n clearOldToolResults,\n composeInboundPreamble,\n composeLayer1,\n composeLayer2,\n composeLayer4Skills,\n countMessageTokens,\n createContextEngine,\n DEFAULT_LAYER_PRIORITY,\n DEFAULT_PRESERVE_RECENT_TURNS,\n DEFAULT_RESERVED_FOR_COMPACTION,\n DEFAULT_RESERVED_FOR_RESPONSE,\n DEFAULT_THRESHOLD_RATIO,\n defaultLocaleHeuristicStrategy,\n defineAutoRecallStrategy,\n defineContextLocalePack,\n type ExecuteCompactionInput,\n effectiveAcceptsSensitivity,\n enLocalePack,\n executeCompaction,\n gatherMemoryMetadata,\n HEURISTIC_TOKEN_COUNTER,\n INBOUND_SANITIZATION_PREAMBLE_EN,\n INBOUND_TRUST_ATTR,\n type InboundSanitizationPreamble,\n type InboundTrust,\n type LayerAllocation,\n type LayerCandidate,\n type LayerConfig,\n type LayerId,\n type LocaleResolverLogger,\n type MemoryBaseMode,\n type MemoryContextBlocks,\n type MemoryMetadataDeps,\n type NamedPostCompactionHook,\n NON_INBOUND_ORIGINS,\n type OverflowMode,\n type PartialContextLocalePack,\n type PartitionResult,\n type PostCompactionHook,\n type PostCompactionHookContext,\n type PrivacyConfig,\n type PrivacyDecision,\n type PrivacyDecisionReason,\n type PrivacyFilterContext,\n partitionBySensitivity,\n privacyDecide,\n type ReanchorRecentResultsOptions,\n type RenderedTemplate,\n type ResolvedContextEngineConfig,\n reanchorPersonaBlock,\n reanchorPinnedFacts,\n reanchorProjectRules,\n reanchorRecentResults,\n renderFinalSummary,\n renderMessageText,\n renderMetadataBlock,\n resolveAutoCompactionDefault,\n resolveLocalePack,\n resolveTriggerThreshold,\n type SkillMetadataCard,\n SUMMARY_TEMPLATE_NAME,\n SUMMARY_TEMPLATE_VERSION,\n shouldFireInboundPreamble,\n toSpanAttributes,\n truncateToTokens,\n} from './context-engine/index.js';\nexport * from './errors/index.js';\nexport {\n _resetConsolidatorConfigWarningForTesting,\n type CreateMemoryOptions,\n createMemory,\n type Memory,\n} from './facade.js';\nexport * from './graph/index.js';\nexport type { ContextualRetrievalMode } from './internal/contextualize.js';\nexport type {\n ConflictAuditDecision,\n ConflictAuditInputLike,\n ConflictAuditStage,\n ConflictMemoryStoreExt,\n ConsolidatorMemoryStoreExt,\n ConsolidatorRunFinish,\n ConsolidatorRunInput,\n ConsolidatorStatePatch,\n ConsolidatorStateRow,\n DecayMemoryStoreExt,\n DlqBatchInput,\n DlqBatchRow,\n EmbeddedWriteOptions,\n EmbeddingMetaRegistryLike,\n EpisodicMemoryStoreExt,\n GraphMemoryStoreExt,\n InsightListOptions as InsightStoreListOptions,\n InsightMemoryStoreExt,\n InsightSearchStoreOptions,\n MemoryStoreAdapter,\n PendingConflictInputLike,\n PendingConflictRowLike,\n ProceduralMemoryStoreExt,\n SemanticMemoryStoreExt,\n SessionMemoryStoreExt,\n SessionMessageRecord,\n} from './internal/storage-adapter.js';\nexport * from './migration/index.js';\nexport * from './search/index.js';\nexport * from './tiers/index.js';\nexport {\n type BuildMemoryToolsOptions,\n buildMemoryTools,\n createBlockAppendTool,\n createBlockReplaceTool,\n createBlockRethinkTool,\n createConversationSearchTool,\n createDeepRecallTool,\n createFactForgetTool,\n createFactHistoryTool,\n createFactRememberTool,\n createFactSearchTool,\n createFactSupersedeTool,\n createFactValidateTool,\n createRecallEpisodesTool,\n type MemoryToolDeps,\n type ScopeResolver,\n} from './tools/index.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAaA,UAAkBC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//#region src/internal/envelope.ts
|
|
2
|
+
/**
|
|
3
|
+
* Untrusted-content envelope helpers shared across the memory package.
|
|
4
|
+
*
|
|
5
|
+
* Extracted from the CE-15 compaction-summary neutralization
|
|
6
|
+
* (`wrapSummaryAsDerived` in `context-engine/compaction/compactor.ts`)
|
|
7
|
+
* so every prompt that interpolates untrusted memory text (compaction
|
|
8
|
+
* summaries, reconcile neighbours, deep-phase judge candidates,
|
|
9
|
+
* reflection evidence) delimits it with ONE substitution scheme. The
|
|
10
|
+
* scheme is the same visible bracket-substitution `@graphorin/tools`
|
|
11
|
+
* applies in `wrapEnvelope` (W-030): the closing marker
|
|
12
|
+
* `<<</untrusted_content>>>` becomes `[[/untrusted_content]]` and the
|
|
13
|
+
* opening prefix `<<<untrusted_content` becomes `[[untrusted_content`,
|
|
14
|
+
* tolerant to case and whitespace. Zero-width insertion is not used -
|
|
15
|
+
* models read straight through zero-width splits (the reason
|
|
16
|
+
* `normalizeForMatching` strips them before injection scans).
|
|
17
|
+
*
|
|
18
|
+
* The helper is deliberately local: `@graphorin/memory` does not
|
|
19
|
+
* depend on `@graphorin/tools`, and the substitution scheme is a
|
|
20
|
+
* cross-package SPEC pinned by tests on literal-marker inputs.
|
|
21
|
+
*
|
|
22
|
+
* @packageDocumentation
|
|
23
|
+
*/
|
|
24
|
+
/** Opening delimiter prefix of the untrusted-content envelope. */
|
|
25
|
+
const UNTRUSTED_CONTENT_OPEN_PREFIX = "<<<untrusted_content";
|
|
26
|
+
/** Closing delimiter of the untrusted-content envelope. */
|
|
27
|
+
const UNTRUSTED_CONTENT_CLOSE = "<<</untrusted_content>>>";
|
|
28
|
+
const EMBEDDED_CLOSING_RE = /<<<\s*\/\s*untrusted_content(\s*>>>)?/gi;
|
|
29
|
+
const EMBEDDED_OPENING_RE = /<<<\s*untrusted_content/gi;
|
|
30
|
+
/**
|
|
31
|
+
* Replace envelope markers embedded in `body` with the visible `[[` /
|
|
32
|
+
* `]]` bracket-substitution so the body cannot prematurely close (or
|
|
33
|
+
* spoof a nested opening of) the envelope it is being wrapped into.
|
|
34
|
+
* Marker-free bodies are returned bytes-equal.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
function neutralizeEnvelopeMarkers(body) {
|
|
39
|
+
if (!body.includes("<<<")) return body;
|
|
40
|
+
return body.replace(EMBEDDED_CLOSING_RE, (_match, terminator) => terminator === void 0 ? "[[/untrusted_content" : "[[/untrusted_content]]").replace(EMBEDDED_OPENING_RE, "[[untrusted_content");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Wrap `body` in the untrusted-content envelope with the supplied
|
|
44
|
+
* attributes, neutralizing embedded markers first. Attribute insertion
|
|
45
|
+
* order is preserved, so
|
|
46
|
+
* `wrapUntrusted(body, { trust: 'derived', tool: 'compaction-summarizer' })`
|
|
47
|
+
* reproduces the historical CE-15 output byte-for-byte.
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
function wrapUntrusted(body, attrs) {
|
|
52
|
+
return `${UNTRUSTED_CONTENT_OPEN_PREFIX}${attrs === void 0 ? "" : Object.entries(attrs).map(([key, value]) => ` ${key}="${value.replace(/"/g, """)}"`).join("")}>>>\n${neutralizeEnvelopeMarkers(body)}\n${UNTRUSTED_CONTENT_CLOSE}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { UNTRUSTED_CONTENT_OPEN_PREFIX, wrapUntrusted };
|
|
57
|
+
//# sourceMappingURL=envelope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.js","names":[],"sources":["../../src/internal/envelope.ts"],"sourcesContent":["/**\n * Untrusted-content envelope helpers shared across the memory package.\n *\n * Extracted from the CE-15 compaction-summary neutralization\n * (`wrapSummaryAsDerived` in `context-engine/compaction/compactor.ts`)\n * so every prompt that interpolates untrusted memory text (compaction\n * summaries, reconcile neighbours, deep-phase judge candidates,\n * reflection evidence) delimits it with ONE substitution scheme. The\n * scheme is the same visible bracket-substitution `@graphorin/tools`\n * applies in `wrapEnvelope` (W-030): the closing marker\n * `<<</untrusted_content>>>` becomes `[[/untrusted_content]]` and the\n * opening prefix `<<<untrusted_content` becomes `[[untrusted_content`,\n * tolerant to case and whitespace. Zero-width insertion is not used -\n * models read straight through zero-width splits (the reason\n * `normalizeForMatching` strips them before injection scans).\n *\n * The helper is deliberately local: `@graphorin/memory` does not\n * depend on `@graphorin/tools`, and the substitution scheme is a\n * cross-package SPEC pinned by tests on literal-marker inputs.\n *\n * @packageDocumentation\n */\n\n/** Opening delimiter prefix of the untrusted-content envelope. */\nexport const UNTRUSTED_CONTENT_OPEN_PREFIX = '<<<untrusted_content';\n\n/** Closing delimiter of the untrusted-content envelope. */\nexport const UNTRUSTED_CONTENT_CLOSE = '<<</untrusted_content>>>';\n\n// Tolerant embedded-marker matching, mirroring the tools package: the\n// closing form optionally consumes its `>>>` terminator so the full\n// marker collapses to `[[/untrusted_content]]`.\nconst EMBEDDED_CLOSING_RE = /<<<\\s*\\/\\s*untrusted_content(\\s*>>>)?/gi;\nconst EMBEDDED_OPENING_RE = /<<<\\s*untrusted_content/gi;\n\n/**\n * Replace envelope markers embedded in `body` with the visible `[[` /\n * `]]` bracket-substitution so the body cannot prematurely close (or\n * spoof a nested opening of) the envelope it is being wrapped into.\n * Marker-free bodies are returned bytes-equal.\n *\n * @internal\n */\nexport function neutralizeEnvelopeMarkers(body: string): string {\n if (!body.includes('<<<')) return body;\n return body\n .replace(EMBEDDED_CLOSING_RE, (_match, terminator) =>\n terminator === undefined ? '[[/untrusted_content' : '[[/untrusted_content]]',\n )\n .replace(EMBEDDED_OPENING_RE, '[[untrusted_content');\n}\n\n/**\n * Wrap `body` in the untrusted-content envelope with the supplied\n * attributes, neutralizing embedded markers first. Attribute insertion\n * order is preserved, so\n * `wrapUntrusted(body, { trust: 'derived', tool: 'compaction-summarizer' })`\n * reproduces the historical CE-15 output byte-for-byte.\n *\n * @internal\n */\nexport function wrapUntrusted(body: string, attrs?: Readonly<Record<string, string>>): string {\n const attrStr =\n attrs === undefined\n ? ''\n : Object.entries(attrs)\n .map(([key, value]) => ` ${key}=\"${value.replace(/\"/g, '"')}\"`)\n .join('');\n return `${UNTRUSTED_CONTENT_OPEN_PREFIX}${attrStr}>>>\\n${neutralizeEnvelopeMarkers(body)}\\n${UNTRUSTED_CONTENT_CLOSE}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,gCAAgC;;AAG7C,MAAa,0BAA0B;AAKvC,MAAM,sBAAsB;AAC5B,MAAM,sBAAsB;;;;;;;;;AAU5B,SAAgB,0BAA0B,MAAsB;AAC9D,KAAI,CAAC,KAAK,SAAS,MAAM,CAAE,QAAO;AAClC,QAAO,KACJ,QAAQ,sBAAsB,QAAQ,eACrC,eAAe,SAAY,yBAAyB,yBACrD,CACA,QAAQ,qBAAqB,sBAAsB;;;;;;;;;;;AAYxD,SAAgB,cAAc,MAAc,OAAkD;AAO5F,QAAO,GAAG,gCALR,UAAU,SACN,KACA,OAAO,QAAQ,MAAM,CAClB,KAAK,CAAC,KAAK,WAAW,IAAI,IAAI,IAAI,MAAM,QAAQ,MAAM,SAAS,CAAC,GAAG,CACnE,KAAK,GAAG,CACiC,OAAO,0BAA0B,KAAK,CAAC,IAAI"}
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injection-heuristics.js","names":["DEFAULT_INJECTION_RULES: ReadonlyArray<InjectionRule>","markers: string[]"],"sources":["../../src/internal/injection-heuristics.ts"],"sourcesContent":["/**\n * Offline heuristics (no provider calls) that flag obvious memory-injection\n * markers in candidate memory text (P1-4). Long-lived memory is the prime\n * target for poisoning: an injected instruction planted today fires weeks\n * later when semantically recalled, defeating session-scoped prompt-injection\n * defenses (MINJA arXiv:2601.05504; MemoryGraft arXiv:2512.16962). Flagged\n * candidates are quarantined at write time so they never enter\n * action-driving recall until a human validates them.\n *\n * The detector is deliberately *high-precision*: every rule targets\n * injection-characteristic structure (imperative override of prior\n * instructions, role-markup smuggling, secrecy/exfiltration directives), not\n * lone common verbs, so benign first-party facts (\"I always run in the\n * mornings\", \"lives in Lisbon\") are not quarantined. It is locale-aware via\n * pluggable {@link InjectionRule} lists and performs no network or model\n * calls - pure regex over the candidate string.\n *\n * @packageDocumentation\n */\n\nimport { normalizeForMatching } from '@graphorin/security/guardrails';\n\n/**\n * A single named injection-detection rule. The `label` is a stable,\n * audit-friendly identifier surfaced in {@link InjectionScan.markers};\n * the `pattern` MUST NOT carry the `g` flag (stateful `RegExp.test`).\n *\n * @stable\n */\nexport interface InjectionRule {\n readonly label: string;\n readonly pattern: RegExp;\n}\n\n/**\n * Result of scanning a candidate string. `flagged` is `true` when at\n * least one rule matched; `markers` lists the matched rule labels\n * (deduped, in rule order) for the audit trail.\n *\n * @stable\n */\nexport interface InjectionScan {\n readonly flagged: boolean;\n readonly markers: ReadonlyArray<string>;\n}\n\n/**\n * Options for {@link detectMemoryInjection}.\n *\n * @stable\n */\nexport interface InjectionHeuristicOptions {\n /**\n * Replace the default English rule set wholesale (advanced - for a\n * fully custom locale). When omitted, {@link DEFAULT_INJECTION_RULES}\n * is used.\n */\n readonly rules?: ReadonlyArray<InjectionRule>;\n /**\n * Additional locale-specific rules merged *after* the active rule set.\n * The common way to make the detector locale-aware without discarding\n * the English defaults.\n */\n readonly extraRules?: ReadonlyArray<InjectionRule>;\n}\n\n/**\n * Default English injection-marker rules. High-precision: each requires\n * injection-characteristic structure so benign facts do not trip them.\n *\n * @stable\n */\nexport const DEFAULT_INJECTION_RULES: ReadonlyArray<InjectionRule> = Object.freeze([\n {\n label: 'ignore-previous-instructions',\n pattern:\n /\\bignore\\s+(?:all\\s+|any\\s+)?(?:previous|prior|earlier|the\\s+above|preceding)\\s+(?:instructions?|prompts?|messages?|commands?|directions?|context|rules?)\\b/i,\n },\n {\n label: 'disregard-instructions',\n pattern:\n /\\bdisregard\\s+(?:all\\s+|any\\s+|the\\s+)?(?:previous|prior|earlier|above|preceding|prior\\s+)?(?:instructions?|prompts?|rules?|guidelines?|guardrails?)\\b/i,\n },\n {\n label: 'override-directive',\n pattern: /\\boverride\\s+(?:the\\s+)?(?:system|safety|security|previous|prior|default)\\b/i,\n },\n {\n label: 'system-prompt-exfil',\n pattern:\n /\\b(?:reveal|show|print|repeat|leak|expose|dump)\\b[^.?!\\n]{0,40}\\b(?:system\\s+prompt|system\\s+message|initial\\s+instructions|hidden\\s+(?:instructions|prompt))\\b/i,\n },\n {\n label: 'role-markup-injection',\n pattern: /<\\/?(?:system|im_start|im_end|tool_call|function_call)\\b|\\[\\/?(?:INST|SYS)\\]/i,\n },\n {\n label: 'secrecy-directive',\n pattern:\n /\\b(?:do\\s+not|don['’]?t|never)\\s+(?:tell|inform|notify|alert|mention\\s+(?:this\\s+)?to|reveal\\s+(?:this\\s+)?to)\\s+(?:the\\s+)?(?:user|human|operator|owner)\\b|\\bwithout\\s+(?:telling|informing|notifying|alerting)\\s+(?:the\\s+)?(?:user|human|operator)\\b/i,\n },\n {\n label: 'exfiltration-directive',\n pattern:\n /\\b(?:send|forward|email|transmit|upload|post|exfiltrate|leak)\\b[^.?!\\n]{0,60}\\b(?:password|secret|api[\\s_-]?keys?|credentials?|private\\s+keys?|access\\s+tokens?|auth\\s+tokens?)\\b/i,\n },\n {\n label: 'persistent-override',\n pattern:\n /\\bfrom\\s+now\\s+on\\b[^.?!\\n]{0,40}\\b(?:always|never|you\\s+must|ignore|disregard|do\\s+not)\\b/i,\n },\n]);\n\n/**\n * Scan candidate memory text for obvious injection markers. Pure,\n * offline, allocation-light. Returns the matched rule labels so callers\n * can record *why* a candidate was quarantined.\n *\n * @stable\n */\nexport function detectMemoryInjection(\n text: string,\n options: InjectionHeuristicOptions = {},\n): InjectionScan {\n const rules = options.rules ?? DEFAULT_INJECTION_RULES;\n // C6: also scan the NFKC/zero-width-stripped fold so cheap\n // character-injection (zero-width splits, fullwidth homoglyphs) does\n // not slip a poisoned memory past the quarantine gate.\n const normalized = normalizeForMatching(text);\n const scanBoth = (pattern: RegExp): boolean =>\n pattern.test(text) || (normalized !== text && pattern.test(normalized));\n const markers: string[] = [];\n for (const rule of rules) {\n if (scanBoth(rule.pattern)) markers.push(rule.label);\n }\n if (options.extraRules !== undefined) {\n for (const rule of options.extraRules) {\n if (!markers.includes(rule.label) && scanBoth(rule.pattern)) markers.push(rule.label);\n }\n }\n return { flagged: markers.length > 0, markers: Object.freeze(markers) };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAaA,0BAAwD,OAAO,OAAO;CACjF;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SAAS;EACV;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SAAS;EACV;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACF,CAAC;;;;;;;;AASF,SAAgB,sBACd,MACA,UAAqC,EAAE,EACxB;CACf,MAAM,QAAQ,QAAQ,SAAS;CAI/B,MAAM,aAAa,qBAAqB,KAAK;CAC7C,MAAM,YAAY,YAChB,QAAQ,KAAK,KAAK,IAAK,eAAe,QAAQ,QAAQ,KAAK,WAAW;CACxE,MAAMC,UAAoB,EAAE;AAC5B,MAAK,MAAM,QAAQ,MACjB,KAAI,SAAS,KAAK,QAAQ,CAAE,SAAQ,KAAK,KAAK,MAAM;AAEtD,KAAI,QAAQ,eAAe,QACzB;OAAK,MAAM,QAAQ,QAAQ,WACzB,KAAI,CAAC,QAAQ,SAAS,KAAK,MAAM,IAAI,SAAS,KAAK,QAAQ,CAAE,SAAQ,KAAK,KAAK,MAAM;;AAGzF,QAAO;EAAE,SAAS,QAAQ,SAAS;EAAG,SAAS,OAAO,OAAO,QAAQ;EAAE"}
|
|
1
|
+
{"version":3,"file":"injection-heuristics.js","names":["DEFAULT_INJECTION_RULES: ReadonlyArray<InjectionRule>","markers: string[]"],"sources":["../../src/internal/injection-heuristics.ts"],"sourcesContent":["/**\n * Offline heuristics (no provider calls) that flag obvious memory-injection\n * markers in candidate memory text (P1-4). Long-lived memory is the prime\n * target for poisoning: an injected instruction planted today fires weeks\n * later when semantically recalled, defeating session-scoped prompt-injection\n * defenses (MINJA arXiv:2601.05504; MemoryGraft arXiv:2512.16962). Flagged\n * candidates are quarantined at write time so they never enter\n * action-driving recall until a human validates them.\n *\n * The detector is deliberately *high-precision*: every rule targets\n * injection-characteristic structure (imperative override of prior\n * instructions, role-markup smuggling, secrecy/exfiltration directives), not\n * lone common verbs, so benign first-party facts (\"I always run in the\n * mornings\", \"lives in Lisbon\") are not quarantined. It is locale-aware via\n * pluggable {@link InjectionRule} lists and performs no network or model\n * calls - pure regex over the candidate string.\n *\n * @packageDocumentation\n */\n\nimport { normalizeForMatching } from '@graphorin/security/guardrails';\n\n/**\n * A single named injection-detection rule. The `label` is a stable,\n * audit-friendly identifier surfaced in {@link InjectionScan.markers};\n * the `pattern` MUST NOT carry the `g` flag (stateful `RegExp.test`).\n *\n * @stable\n */\nexport interface InjectionRule {\n readonly label: string;\n readonly pattern: RegExp;\n}\n\n/**\n * Result of scanning a candidate string. `flagged` is `true` when at\n * least one rule matched; `markers` lists the matched rule labels\n * (deduped, in rule order) for the audit trail.\n *\n * @stable\n */\nexport interface InjectionScan {\n readonly flagged: boolean;\n readonly markers: ReadonlyArray<string>;\n}\n\n/**\n * Options for {@link detectMemoryInjection}.\n *\n * @stable\n */\nexport interface InjectionHeuristicOptions {\n /**\n * Replace the default English rule set wholesale (advanced - for a\n * fully custom locale). When omitted, {@link DEFAULT_INJECTION_RULES}\n * is used.\n */\n readonly rules?: ReadonlyArray<InjectionRule>;\n /**\n * Additional locale-specific rules merged *after* the active rule set.\n * The common way to make the detector locale-aware without discarding\n * the English defaults.\n */\n readonly extraRules?: ReadonlyArray<InjectionRule>;\n}\n\n/**\n * Default English injection-marker rules. High-precision: each requires\n * injection-characteristic structure so benign facts do not trip them.\n *\n * @stable\n */\nexport const DEFAULT_INJECTION_RULES: ReadonlyArray<InjectionRule> = Object.freeze([\n {\n label: 'ignore-previous-instructions',\n pattern:\n /\\bignore\\s+(?:all\\s+|any\\s+)?(?:previous|prior|earlier|the\\s+above|preceding)\\s+(?:instructions?|prompts?|messages?|commands?|directions?|context|rules?)\\b/i,\n },\n {\n label: 'disregard-instructions',\n pattern:\n /\\bdisregard\\s+(?:all\\s+|any\\s+|the\\s+)?(?:previous|prior|earlier|above|preceding|prior\\s+)?(?:instructions?|prompts?|rules?|guidelines?|guardrails?)\\b/i,\n },\n {\n label: 'override-directive',\n pattern: /\\boverride\\s+(?:the\\s+)?(?:system|safety|security|previous|prior|default)\\b/i,\n },\n {\n label: 'system-prompt-exfil',\n pattern:\n /\\b(?:reveal|show|print|repeat|leak|expose|dump)\\b[^.?!\\n]{0,40}\\b(?:system\\s+prompt|system\\s+message|initial\\s+instructions|hidden\\s+(?:instructions|prompt))\\b/i,\n },\n {\n label: 'role-markup-injection',\n pattern: /<\\/?(?:system|im_start|im_end|tool_call|function_call)\\b|\\[\\/?(?:INST|SYS)\\]/i,\n },\n {\n label: 'secrecy-directive',\n pattern:\n /\\b(?:do\\s+not|don['’]?t|never)\\s+(?:tell|inform|notify|alert|mention\\s+(?:this\\s+)?to|reveal\\s+(?:this\\s+)?to)\\s+(?:the\\s+)?(?:user|human|operator|owner)\\b|\\bwithout\\s+(?:telling|informing|notifying|alerting)\\s+(?:the\\s+)?(?:user|human|operator)\\b/i,\n },\n {\n label: 'exfiltration-directive',\n pattern:\n /\\b(?:send|forward|email|transmit|upload|post|exfiltrate|leak)\\b[^.?!\\n]{0,60}\\b(?:password|secret|api[\\s_-]?keys?|credentials?|private\\s+keys?|access\\s+tokens?|auth\\s+tokens?)\\b/i,\n },\n {\n label: 'persistent-override',\n pattern:\n /\\bfrom\\s+now\\s+on\\b[^.?!\\n]{0,40}\\b(?:always|never|you\\s+must|ignore|disregard|do\\s+not)\\b/i,\n },\n]);\n\n/**\n * Scan candidate memory text for obvious injection markers. Pure,\n * offline, allocation-light. Returns the matched rule labels so callers\n * can record *why* a candidate was quarantined.\n *\n * @stable\n */\nexport function detectMemoryInjection(\n text: string,\n options: InjectionHeuristicOptions = {},\n): InjectionScan {\n const rules = options.rules ?? DEFAULT_INJECTION_RULES;\n // C6: also scan the NFKC/zero-width-stripped fold so cheap\n // character-injection (zero-width splits, fullwidth homoglyphs) does\n // not slip a poisoned memory past the quarantine gate.\n const normalized = normalizeForMatching(text);\n const scanBoth = (pattern: RegExp): boolean =>\n pattern.test(text) || (normalized !== text && pattern.test(normalized));\n const markers: string[] = [];\n for (const rule of rules) {\n if (scanBoth(rule.pattern)) markers.push(rule.label);\n }\n if (options.extraRules !== undefined) {\n for (const rule of options.extraRules) {\n if (!markers.includes(rule.label) && scanBoth(rule.pattern)) markers.push(rule.label);\n }\n }\n return { flagged: markers.length > 0, markers: Object.freeze(markers) };\n}\n\n/** Replacement token for {@link stripMemoryInjectionMarkers}. */\nconst INJECTION_STRIP_MASK = '[REDACTED:injection-marker]';\n\n// Per-rule global clones for the strip pass. The catalogued patterns\n// deliberately carry no `g` flag (stateful `RegExp.test`); a strip\n// needs replace-all semantics, so clone lazily and cache by source.\nconst globalRuleCache = new Map<string, RegExp>();\n\nfunction globalPattern(pattern: RegExp): RegExp {\n const key = `${pattern.source}\u0000${pattern.flags}`;\n let cached = globalRuleCache.get(key);\n if (cached === undefined) {\n cached = new RegExp(\n pattern.source,\n pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`,\n );\n globalRuleCache.set(key, cached);\n }\n cached.lastIndex = 0;\n return cached;\n}\n\n/**\n * Read-time strip of the same high-precision injection markers\n * {@link detectMemoryInjection} flags at write time. Used when\n * ALREADY-STORED memory text (which may predate the write-time\n * quarantine gate, or carry `user` provenance that the gate does not\n * screen) is interpolated into a background prompt (reconcile / judge\n * / reflection). Matches are replaced with a visible\n * `[REDACTED:injection-marker]` token; marker-free text is returned\n * bytes-equal. Deliberately local and allocation-light - this runs on\n * the consolidation hot path, so the heavyweight observability\n * catalogue is NOT pulled in.\n *\n * @internal\n */\nexport function stripMemoryInjectionMarkers(\n text: string,\n options: InjectionHeuristicOptions = {},\n): string {\n const rules = options.rules ?? DEFAULT_INJECTION_RULES;\n let out = text;\n for (const rule of rules) {\n out = out.replace(globalPattern(rule.pattern), INJECTION_STRIP_MASK);\n }\n if (options.extraRules !== undefined) {\n for (const rule of options.extraRules) {\n out = out.replace(globalPattern(rule.pattern), INJECTION_STRIP_MASK);\n }\n }\n return out;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAaA,0BAAwD,OAAO,OAAO;CACjF;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SAAS;EACV;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SAAS;EACV;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACD;EACE,OAAO;EACP,SACE;EACH;CACF,CAAC;;;;;;;;AASF,SAAgB,sBACd,MACA,UAAqC,EAAE,EACxB;CACf,MAAM,QAAQ,QAAQ,SAAS;CAI/B,MAAM,aAAa,qBAAqB,KAAK;CAC7C,MAAM,YAAY,YAChB,QAAQ,KAAK,KAAK,IAAK,eAAe,QAAQ,QAAQ,KAAK,WAAW;CACxE,MAAMC,UAAoB,EAAE;AAC5B,MAAK,MAAM,QAAQ,MACjB,KAAI,SAAS,KAAK,QAAQ,CAAE,SAAQ,KAAK,KAAK,MAAM;AAEtD,KAAI,QAAQ,eAAe,QACzB;OAAK,MAAM,QAAQ,QAAQ,WACzB,KAAI,CAAC,QAAQ,SAAS,KAAK,MAAM,IAAI,SAAS,KAAK,QAAQ,CAAE,SAAQ,KAAK,KAAK,MAAM;;AAGzF,QAAO;EAAE,SAAS,QAAQ,SAAS;EAAG,SAAS,OAAO,OAAO,QAAQ;EAAE;;;AAIzE,MAAM,uBAAuB;AAK7B,MAAM,kCAAkB,IAAI,KAAqB;AAEjD,SAAS,cAAc,SAAyB;CAC9C,MAAM,MAAM,GAAG,QAAQ,OAAO,GAAG,QAAQ;CACzC,IAAI,SAAS,gBAAgB,IAAI,IAAI;AACrC,KAAI,WAAW,QAAW;AACxB,WAAS,IAAI,OACX,QAAQ,QACR,QAAQ,MAAM,SAAS,IAAI,GAAG,QAAQ,QAAQ,GAAG,QAAQ,MAAM,GAChE;AACD,kBAAgB,IAAI,KAAK,OAAO;;AAElC,QAAO,YAAY;AACnB,QAAO;;;;;;;;;;;;;;;;AAiBT,SAAgB,4BACd,MACA,UAAqC,EAAE,EAC/B;CACR,MAAM,QAAQ,QAAQ,SAAS;CAC/B,IAAI,MAAM;AACV,MAAK,MAAM,QAAQ,MACjB,OAAM,IAAI,QAAQ,cAAc,KAAK,QAAQ,EAAE,qBAAqB;AAEtE,KAAI,QAAQ,eAAe,OACzB,MAAK,MAAM,QAAQ,QAAQ,WACzB,OAAM,IAAI,QAAQ,cAAc,KAAK,QAAQ,EAAE,qBAAqB;AAGxE,QAAO"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/internal/llm-json.ts
|
|
2
|
+
/**
|
|
3
|
+
* Shared tolerant-parsing primitives for LLM text completions that are
|
|
4
|
+
* expected to carry JSON. Every consolidator phase and search transformer
|
|
5
|
+
* that parses a model response uses these two helpers; they used to be
|
|
6
|
+
* copy-pasted per module (7 copies of {@link stripFence}, 3 of
|
|
7
|
+
* {@link sliceJsonObject}) until the CodeQL ReDoS fix (06b7552) had to be
|
|
8
|
+
* applied to all of them at once - the canonical demonstration of why the
|
|
9
|
+
* single definition lives here now.
|
|
10
|
+
*
|
|
11
|
+
* Package-local (`internal/`): not part of the public `@graphorin/memory`
|
|
12
|
+
* API surface.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Strip a single surrounding Markdown code fence, if present. The
|
|
18
|
+
* info-string is matched with `[^\n]*` (never across lines) so a hostile
|
|
19
|
+
* megabyte-long first line cannot trigger polynomial backtracking; an
|
|
20
|
+
* unterminated fence leaves the text untouched and the caller's JSON
|
|
21
|
+
* parsing fails soft instead.
|
|
22
|
+
*/
|
|
23
|
+
function stripFence(text) {
|
|
24
|
+
return /^```[^\n]*\n([\s\S]*?)\n```/u.exec(text.trim())?.[1] ?? text;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Best-effort recovery slice for completions that wrap a JSON object in
|
|
28
|
+
* prose: everything from the first `{` to the last `}`. Returns `null`
|
|
29
|
+
* when no plausible object span exists.
|
|
30
|
+
*/
|
|
31
|
+
function sliceJsonObject(text) {
|
|
32
|
+
const start = text.indexOf("{");
|
|
33
|
+
const end = text.lastIndexOf("}");
|
|
34
|
+
if (start < 0 || end < start) return null;
|
|
35
|
+
return text.slice(start, end + 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { sliceJsonObject, stripFence };
|
|
40
|
+
//# sourceMappingURL=llm-json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-json.js","names":[],"sources":["../../src/internal/llm-json.ts"],"sourcesContent":["/**\n * Shared tolerant-parsing primitives for LLM text completions that are\n * expected to carry JSON. Every consolidator phase and search transformer\n * that parses a model response uses these two helpers; they used to be\n * copy-pasted per module (7 copies of {@link stripFence}, 3 of\n * {@link sliceJsonObject}) until the CodeQL ReDoS fix (06b7552) had to be\n * applied to all of them at once - the canonical demonstration of why the\n * single definition lives here now.\n *\n * Package-local (`internal/`): not part of the public `@graphorin/memory`\n * API surface.\n *\n * @packageDocumentation\n */\n\n/**\n * Strip a single surrounding Markdown code fence, if present. The\n * info-string is matched with `[^\\n]*` (never across lines) so a hostile\n * megabyte-long first line cannot trigger polynomial backtracking; an\n * unterminated fence leaves the text untouched and the caller's JSON\n * parsing fails soft instead.\n */\nexport function stripFence(text: string): string {\n const match = /^```[^\\n]*\\n([\\s\\S]*?)\\n```/u.exec(text.trim());\n return match?.[1] ?? text;\n}\n\n/**\n * Best-effort recovery slice for completions that wrap a JSON object in\n * prose: everything from the first `{` to the last `}`. Returns `null`\n * when no plausible object span exists.\n */\nexport function sliceJsonObject(text: string): string | null {\n const start = text.indexOf('{');\n const end = text.lastIndexOf('}');\n if (start < 0 || end < start) return null;\n return text.slice(start, end + 1);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,WAAW,MAAsB;AAE/C,QADc,+BAA+B,KAAK,KAAK,MAAM,CAAC,GAC/C,MAAM;;;;;;;AAQvB,SAAgB,gBAAgB,MAA6B;CAC3D,MAAM,QAAQ,KAAK,QAAQ,IAAI;CAC/B,MAAM,MAAM,KAAK,YAAY,IAAI;AACjC,KAAI,QAAQ,KAAK,MAAM,MAAO,QAAO;AACrC,QAAO,KAAK,MAAM,OAAO,MAAM,EAAE"}
|
|
@@ -37,8 +37,11 @@ interface EpisodicMemoryStoreExt extends EpisodicMemoryStore {
|
|
|
37
37
|
searchVector?(scope: SessionScope$1, embedding: Float32Array, embedderId: string, topK: number, /** Point-in-time filter (`started_at <= asOf`, ISO-8601). P0-2. */
|
|
38
38
|
asOf?: string, /** Include quarantined episodes (validation/inspector path). P1-4. */
|
|
39
39
|
includeQuarantined?: boolean): Promise<ReadonlyArray<MemoryHit<Episode$1>>>;
|
|
40
|
-
/**
|
|
41
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Mark an episode archived. Soft-archive - the row stays for replay.
|
|
42
|
+
* W-154: with `scope`, adapters no-op unless the row belongs to it.
|
|
43
|
+
*/
|
|
44
|
+
archive?(id: string, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
42
45
|
/**
|
|
43
46
|
* Most-recent episodes by end time (newest first), with no FTS / vector
|
|
44
47
|
* query - recency, not relevance (MCON-1). Powers `EpisodicMemory.recent()`
|
|
@@ -53,7 +56,7 @@ interface EpisodicMemoryStoreExt extends EpisodicMemoryStore {
|
|
|
53
56
|
* (auto-formed) episode into default recall or re-quarantine an active one,
|
|
54
57
|
* with a `memory_history` audit row. Powers {@link EpisodicMemory.validate}.
|
|
55
58
|
*/
|
|
56
|
-
setStatus?(id: string, status: MemoryStatus, reason?: string): Promise<void>;
|
|
59
|
+
setStatus?(id: string, status: MemoryStatus, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
57
60
|
/**
|
|
58
61
|
* Count the recall-eligible episodes for the scope (CE-5) - a `COUNT(*)`,
|
|
59
62
|
* never materialising rows. Powers honest `metadata()` counts.
|
|
@@ -100,7 +103,7 @@ interface SemanticMemoryStoreExt extends SemanticMemoryStore {
|
|
|
100
103
|
* Powers {@link SemanticMemory.validate}; the default
|
|
101
104
|
* `@graphorin/store-sqlite` adapter implements it.
|
|
102
105
|
*/
|
|
103
|
-
setStatus?(factId: string, status: MemoryStatus, reason?: string): Promise<void>;
|
|
106
|
+
setStatus?(factId: string, status: MemoryStatus, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
104
107
|
/**
|
|
105
108
|
* Count the recall-eligible facts for the scope (CE-5) - a `COUNT(*)` with
|
|
106
109
|
* the default recall filters (live, non-archived, non-quarantined), never
|
|
@@ -112,7 +115,17 @@ interface SemanticMemoryStoreExt extends SemanticMemoryStore {
|
|
|
112
115
|
* but the row itself + every per-embedder vec0 entry is removed.
|
|
113
116
|
* Distinct from {@link SemanticMemoryStore.forget} (soft-delete).
|
|
114
117
|
*/
|
|
115
|
-
purge?(id: string, reason?: string): Promise<void>;
|
|
118
|
+
purge?(id: string, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* W-019: record a PENDING supersede link - set `newId.supersedes =
|
|
121
|
+
* oldId` WITHOUT closing the old fact's validity interval. Used when
|
|
122
|
+
* a supersede's successor lands quarantined: the old fact must stay
|
|
123
|
+
* in default recall until the successor is validated, at which point
|
|
124
|
+
* {@link SemanticMemory.validate} completes the closure via
|
|
125
|
+
* {@link SemanticMemoryStore.supersede}. No schema change - the
|
|
126
|
+
* `supersedes` column already exists.
|
|
127
|
+
*/
|
|
128
|
+
linkPendingSupersede?(newId: string, oldId: string): Promise<void>;
|
|
116
129
|
/**
|
|
117
130
|
* Walk the bi-temporal supersede chain that `factId` belongs to and
|
|
118
131
|
* return every fact in it, oldest → newest (by `validFrom`),
|
|
@@ -518,7 +531,7 @@ interface DecayMemoryStoreExt {
|
|
|
518
531
|
* Soft-archive a fact (sets `archived = 1`). The audit row in
|
|
519
532
|
* `memory_history` records the archive event.
|
|
520
533
|
*/
|
|
521
|
-
archiveFact(id: string, reason?: string): Promise<void>;
|
|
534
|
+
archiveFact(id: string, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
522
535
|
/**
|
|
523
536
|
* Record a retrieval access for the given facts (MRET-7): stamp
|
|
524
537
|
* `lastAccessedAt` and reinforce `strength` (implementation-capped).
|
|
@@ -526,7 +539,7 @@ interface DecayMemoryStoreExt {
|
|
|
526
539
|
* MUST treat failures as non-fatal (the read path never breaks on a
|
|
527
540
|
* bookkeeping write).
|
|
528
541
|
*/
|
|
529
|
-
markAccessed?(ids: ReadonlyArray<string>, accessedAt?: number): Promise<void>;
|
|
542
|
+
markAccessed?(ids: ReadonlyArray<string>, accessedAt?: number, scope?: SessionScope$1): Promise<void>;
|
|
530
543
|
/**
|
|
531
544
|
* Narrow decay-column read for exactly the given fact ids (MRET-8) -
|
|
532
545
|
* powers per-search decay re-ranking without the old O(scope)
|
|
@@ -581,7 +594,7 @@ interface InsightMemoryStoreExt {
|
|
|
581
594
|
* (reflection) insight or re-quarantine an active one, with a
|
|
582
595
|
* `memory_history` audit row. Powers {@link InsightMemory.validate}.
|
|
583
596
|
*/
|
|
584
|
-
setStatus?(id: string, status: MemoryStatus, reason?: string): Promise<void>;
|
|
597
|
+
setStatus?(id: string, status: MemoryStatus, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
585
598
|
/**
|
|
586
599
|
* Adjust an insight's ExpeL salience by `delta`, clamped at 0. The
|
|
587
600
|
* floor is the value at which {@link prune} removes it.
|
|
@@ -606,7 +619,7 @@ interface ProceduralMemoryStoreExt extends ProceduralMemoryStore {
|
|
|
606
619
|
* procedure into `activate()` or re-quarantine an active one, with a
|
|
607
620
|
* `memory_history` audit row. Powers {@link ProceduralMemory.validate}.
|
|
608
621
|
*/
|
|
609
|
-
setStatus?(id: string, status: MemoryStatus, reason?: string): Promise<void>;
|
|
622
|
+
setStatus?(id: string, status: MemoryStatus, reason?: string, scope?: SessionScope$1): Promise<void>;
|
|
610
623
|
/**
|
|
611
624
|
* Lexical runbook search over rule text (D3, migration 028) - content
|
|
612
625
|
* recall for "find the procedure for this task", as opposed to
|
|
@@ -787,5 +800,5 @@ interface MemoryStoreAdapter extends Omit<MemoryStore, 'session' | 'episodic' |
|
|
|
787
800
|
readonly graph?: GraphMemoryStoreExt;
|
|
788
801
|
}
|
|
789
802
|
//#endregion
|
|
790
|
-
export { ConflictAuditDecision, ConflictAuditInputLike, ConflictAuditStage, ConflictMemoryStoreExt, ConsolidatorMemoryStoreExt, ConsolidatorRunFinish, ConsolidatorRunInput, ConsolidatorStatePatch, ConsolidatorStateRow, DecayMemoryStoreExt, DlqBatchInput, DlqBatchRow, EmbeddedWriteOptions, EmbeddingMetaRegistryLike, EpisodicMemoryStoreExt, GraphMemoryStoreExt, InsightListOptions, InsightMemoryStoreExt, InsightSearchStoreOptions, MemoryStoreAdapter, PendingConflictInputLike, PendingConflictRowLike, SemanticMemoryStoreExt, SessionMemoryStoreExt, SessionMessageRecord };
|
|
803
|
+
export { ConflictAuditDecision, ConflictAuditInputLike, ConflictAuditStage, ConflictMemoryStoreExt, ConsolidatorMemoryStoreExt, ConsolidatorRunFinish, ConsolidatorRunInput, ConsolidatorStatePatch, ConsolidatorStateRow, DecayMemoryStoreExt, DlqBatchInput, DlqBatchRow, EmbeddedWriteOptions, EmbeddingMetaRegistryLike, EpisodicMemoryStoreExt, GraphMemoryStoreExt, InsightListOptions, InsightMemoryStoreExt, InsightSearchStoreOptions, MemoryStoreAdapter, PendingConflictInputLike, PendingConflictRowLike, ProceduralMemoryStoreExt, SemanticMemoryStoreExt, SessionMemoryStoreExt, SessionMessageRecord };
|
|
791
804
|
//# sourceMappingURL=storage-adapter.d.ts.map
|