feat(extraction): EXP-06 — generic event anchors for As-of facts#7
Draft
moralespanitz wants to merge 1 commit intomainfrom
Draft
feat(extraction): EXP-06 — generic event anchors for As-of facts#7moralespanitz wants to merge 1 commit intomainfrom
moralespanitz wants to merge 1 commit intomainfrom
Conversation
When a fact starts with 'As of <date>, ...' and no DESCRIPTOR_RULE matches, emit a generic event.occurred anchor with the date and subject recovered from the prefix. Behind new flag genericEventAnchorEnabled (default false). Targets BEAM TR. Stage 7 dry-run on iter 7 v3 had TR 1/2 and EO 0/2; much of the variance was on facts that had clear temporal phrasing but didn't match LoCoMo-style descriptors. The fall-through anchor restores them at retrieval time. Risks: anchor inflation (new flag is off by default to bound this); subject collapse on User-only facts (subject extractor returns null in ambiguous cases rather than emitting a wrong subject). New config keys (defaults-off): - genericEventAnchorEnabled: false Behind feature flag. Defaults preserve current behavior.
3 tasks
moralespanitz
added a commit
that referenced
this pull request
May 6, 2026
…view #7) `positionInConversation` was set directly to `turn_id`. That looked correct for a single extraction, but the (user_id, memory_id) UNIQUE on `first_mention_events` means a re-run of `extractAndStore` for the same conversation silently keeps the FIRST inserted row — including its position. If the LLM's turn_id assignment drifted between runs (which it does in practice — non-deterministic decoding even at temperature=0 plus prompt-cache-state variation), readers would see position values that depend on which run happened to write first, breaking deterministic chronological ordering. Fix: `positionInConversation` is now the 0-based index in the FINAL turn-id-sorted output, NOT `turn_id` itself. Sort first, then enumerate. Re-runs produce identical (position, topic) tuples regardless of any turn_id drift, so the post-write read is stable. Updated `mapToEvents` in `src/services/first-mention-service.ts`: - Build candidates first (without position). - Sort by `turnId` ASC. - Assign `positionInConversation = index` during the final map. Tests: - `src/services/__tests__/first-mention-service.test.ts`: * existing happy-path / sort tests now assert position 0/1/... instead of position == turn_id. * new `produces stable positionInConversation across re-runs even when LLM turn_id drifts` test runs `extractAndStore` twice with drifted turn_ids and confirms both runs produce the same `[0, 1]` position sequence. - `src/db/__tests__/repository-first-mentions.test.ts` (new): integration test seeding a memory + running `store()` twice with drifted turn_ids — asserts only 2 rows survive, position sequence is `[0, 1]`, and the first-write turn_id (5) is what the read-back returns (ON CONFLICT DO NOTHING semantics).
moralespanitz
added a commit
that referenced
this pull request
May 6, 2026
…xtract (review #5) The two PR #18 read endpoints had no HTTP-level tests — only the underlying repository / service unit tests existed, which left the schema-validation middleware and route-level wiring uncovered. A schema rename or a route-handler regression could ship green. New file `src/routes/__tests__/event-chains-and-first-mentions.test.ts` mirrors the route-test pattern from `src/__tests__/route-validation.test.ts`: spin up an Express app on `app.listen(0, ...)`, wire `createMemoryRouter` against a real `MemoryService` backed by the test DB, drive endpoints via `fetch`. The MemoryService gets a real `FirstMentionRepository` plus a stubbed `chatFn` so the LLM call returns a deterministic JSON array. Coverage: GET /v1/memories/event-chains - 400 when `user_id` is missing - 400 when `entity_ids` is missing - 400 when `entity_ids` contains an invalid UUID - 400 when `entity_ids` exceeds the 100-entry cap (review #6) - 400 when `entity_ids` is present but holds only empty tokens - happy path: seed memory + entity + TLL row, hit the route, parse the response with `EventChainsResponseSchema` POST /v1/memories/first-mentions/extract - 400 when `user_id` is missing - 400 when `conversation_text` is empty - 400 when `conversation_text` exceeds MAX_CONVERSATION_LENGTH (100_000 chars) - 400 when `memory_ids_by_turn_id` is missing entirely - 400 when `source_site` is missing - happy path: stub LLM returns 2 events, route stores+returns them, response parsed with `FirstMentionsExtractResponseSchema`, `position_in_conversation` is the post-sorted [0, 1] sequence from review #7. 12/12 new tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a fact begins with
As of <date>, ...and noDESCRIPTOR_RULEmatches but a subject is recoverable, emit a genericevent.occurredanchor with the date and subject. Behind a new feature flaggenericEventAnchorEnabled(defaultfalse).This is EXP-06 from the Sprint 2 phase-2 implementation plan.
Why this exists
event-anchor-facts.tsships a list of LoCoMo-styleDESCRIPTOR_RULES(mentorship, internship, networking, Paris/Rome trips, etc.). Any BEAM fact with anAs of <date>,prefix that doesn't match one of those rules is emitted without an anchor and is invisible to the temporal-anchor retrieval path.The Stage 7 dry-run on iter 7 v3 measured TR 1/2 and EO 0/2; manual inspection of the failing facts showed clear temporal phrasing (
As of January 2026, user is using PostgreSQL,As of March 15 2025, user completed the API migration) that the rules silently dropped.The fall-through anchor restores those facts at retrieval time without any new LLM call.
The rule
In
inferDescriptors:DESCRIPTOR_RULESloop.descriptors.length === 0ANDoptions.genericEventAnchorEnabled === true, push{ label: 'event.occurred', subject, eventDateIso }and let the rest of the pipeline build the anchor fact.inferSubjecthelper, which already returnsnull(no anchor) when neither a person entity nor\buser\bis present in the fact text.The recorded-date prefix parser is widened to accept
Month Year(e.g.January 2026) in addition to the existingMonth Day Yearform. When only month-year is present, the synthesized event date is the first day of the month — sufficient for retrieval keying.Risks
As of <date>,fact becomes at least one anchor. The flag is off by default to bound this.User.inferSubjectfalls back toUserwhen no person entity is present. This weakens multi-event ordering (an EO concern, addressed by EXP-13). Subject extraction returnsnullrather than guessing for ambiguous inputs.descriptors.length === 0guard ensures we never double-emit on a single fact. Existing LoCoMo regression tests still pass with the flag on.event_boundary/boundary_probfields the LLM-judged extraction adds — anchors get their own retrieval boost.Test cases
src/services/__tests__/event-anchor-facts.test.ts— extended:As of January 2026, user is using PostgreSQL.(month-year prefix, flag on).As of March 15 2025, user completed the API migration.(full-date prefix, flag on).As of <date>prefix (flag on).mentorship.receivedstill fires for the existing LoCoMo fixture, and the generic fall-through does not also fire on the same source fact when the flag is on.[](no anchor) onAs of January 2026, the situation continues.rather than crashing or guessing.Random unstructured text without temporal prefix.) returns[]without throwing.All 12 tests pass (5 existing regression + 7 new). Related runtime-config tests for
consensusExtractFactswere updated to thread the new field through and all 18 of those continue to pass.Config override
To enable for a single ingest call without restarting the server:
{ "config_override": { "genericEventAnchorEnabled": true } }Or via env:
The field is also added to
INTERNAL_POLICY_CONFIG_FIELDSsoPUT /v1/memories/configaccepts it on dev/test deployments.Wiring
RuntimeConfig.genericEventAnchorEnabled: boolean(default false).IngestRuntimeConfigextended with the same field;MemoryServiceDeps.configalready pulls it through& IngestRuntimeConfig.ConsensusExtractionConfig.genericEventAnchorEnabled: boolean—buildExtractionOptionsforwards it intoextractFacts(...).ExtractionOptions.genericEventAnchorEnabled?: boolean(inobservation-date-extraction.ts, the existing pattern).extraction.ts:323forwards the flag intoenrichExtractedFacts(..., { genericEventAnchorEnabled }).enrichExtractedFactsandinferEventAnchorFactsaccept the new option; default-off preserves bit-identical output.quickExtractFactsaccepts an optionalEnrichmentOptions;memory-ingest.ts:performQuickIngestthreadsdeps.config.genericEventAnchorEnabledthrough so the quick path also benefits.Test plan
npx tsc --noEmit— exit 0npx vitest run src/services/__tests__/event-anchor-facts.test.ts— 12/12 passingnpx vitest run src/services/__tests__/consensus-extraction-runtime-config.test.tsobservation-date-extraction.test.tsquick-extraction-assistant.test.ts— 18/18 passingnpx vitest run src/services/__tests__/extraction.test.tsextraction-enrichment.test.tsextraction-cache.test.ts— 64/64 passingnpx vitest run src/services/__tests__/memory-ingest-runtime-config.test.tsingest-trace-branches.test.ts— 12/12 passing