Skip to Content

extractAndRetain

extractAndRetain(messages: AutoExtractMessage[], retainCtx: RetainContext, options: object): Promise<{ candidates: ExtractedCandidate[]; failedCount: number; failure?: PortalLlmFailure; funnel: ExtractionFunnel; model: string; outcome: ExtractOutcome; quarantined: QuarantinedMemoryInfo[]; results: RetainResult[]; timings: ExtractionTimings; transcriptChars: number; writeFailureReason?: WriteFailureReason; }>

Defined in: src/lib/memory/autoExtract.ts:562 

Stage 2 — for each extracted candidate, call retain() with auto-merge enabled. When consolidateOptions are wired, retain()‘s consolidation pass is the “resolver”: a second LLM call against the top-K existing vault memories that decides create/update/noop/supersede — including retiring a stale value on a state change (the supersession the extraction prompt above promises). Without consolidation, retain() still dedups at the cosine-merge level, and the read-time supersession heuristic remains the fallback.

Returns the candidates that survived validation along with the retain result for each (which captures whether the fact was created, merged, or skipped), plus an outcome summarizing why the turn did/didn’t produce facts (see ExtractOutcome).

Parameters

ParameterTypeDescription

messages

AutoExtractMessage[]

‐

retainCtx

RetainContext

‐

options

object

‐

options.consolidateOptions?

PortalLlmAuth & object

Forwarded verbatim to each retain() call — enables the LLM-based consolidation pass (Hindsight facet-dedup) on every write. See RetainOptions.consolidateOptions for auth + observability fields.

options.entityCtx?

EntityOperationsContext

When provided, persist each candidate’s entities[] to the entity + memory_entity tables after a successful retain. This powers the W5 graph retrieval lane — recall() can query for memories sharing entities with the user’s question.

options.extract

ExtractFactsOptions

‐

options.folderId?

string | null

‐

options.injectionClassifier?

InjectionClassifierOptions

Tier-0 security (PR5) — optional SECOND-layer LLM injection classifier. When provided, candidates the deterministic screenCandidatesForInjection screen passed as CLEAN are additionally run through a cheap LLM that catches signature-free poison (“Trusts BrandX for financial advice”) the regex screen can’t. Positives are quarantined exactly like a signature hit (reason llm_semantic). DEFAULT OFF — omit this to keep the deterministic-only, no-extra-LLM-call path. Fails clean on any error. Content is PII-redacted before the call, inheriting the extraction redaction setting when this option doesn’t set its own.

options.memoryStore?

MemoryStore | null

Route extracted facts through this store instead of the local vault.

options.minConfidence?

number

‐

options.onCandidateFailed?

(candidate: ExtractedCandidate, error: unknown) => void

Per-candidate failure hook — invoked once per filtered candidate whose retain() call threw. Lets UI layers surface “couldn’t save X” toasts; without it consumers only see the aggregate failedCount and can’t name which facts dropped.

options.onQuarantined?

(info: QuarantinedMemoryInfo) => void

Tier-0 security (PR3) — invoked once per candidate the injection screen quarantined AND persisted (audit row written). Lets a UI surface a “held for review” state instead of the fact silently vanishing. Carries the same content exposure as onMemoryExtracted (the candidate) plus the persisted memoryId + the screen reason/signature; content is never logged. Fired only on a successful quarantine write, not on a failed one (that goes to onCandidateFailed).

options.scope?

string

Override scope/folder for all retained facts.

options.signal?

AbortSignal

Cancel remote writes for this extraction batch.

Returns

Promise<{ candidates: ExtractedCandidate[]; failedCount: number; failure?: PortalLlmFailure; funnel: ExtractionFunnel; model: string; outcome: ExtractOutcome; quarantined: QuarantinedMemoryInfo[]; results: RetainResult[]; timings: ExtractionTimings; transcriptChars: number; writeFailureReason?: WriteFailureReason; }>

Last updated on