Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Design: Page-grain ingestion windows + version-filtered as_of (issue #656)
Status: implemented on the release/2.2 line (2.2.0): V62 page-grain
windows, version-filtered as_of FTS fused via the default path's RRF,
docs/temporal.md updated. Scope stayed Phase A only; Phase B remains
deferred (§7). Open question 1 resolved by the v2.1.0 cut (new features
ride 2.2). Open question 2 resolved for the materialized pair
(valid_from + valid_to): the symmetry with the V56 link windows was
worth the duplicated column. Open question 3 resolved for RRF merge
rather than bare fallback. One deliberate deviation from §3: the FTS leg
evaluates TTL expiry at T while the entity leg ignores it (see
docs/temporal.md "one asymmetry"); changing the entity leg's rule
would alter existing behaviour and is out of scope.
Follow-up (#776): V62 originally backfilled both windows in one
in-migration transaction, which on a large store (a 242 GB DB was
reported) ran for hours and grew the WAL to roughly the database size
before the server could accept traffic. V62 is now DDL-only (the two
columns + idx_pages_validity); the backfill moved to a chunked,
resumable, WAL-bounded boot-path step (ops::backfill_page_windows,
invoked from Store::open next to the entity-index backfill) that
reproduces the identical end state in bounded batches, checkpoints the
WAL (TRUNCATE) between each, logs progress, resumes rather than
restarts if interrupted, and no-ops on a store already backfilled. The
in-place reshape changed V62's checksum. migrations::run repairs only the
exact checksum recorded for the SQL shipped in v2.2.1 before running strict
validation; correctly-migrated stores still open, while every unknown checksum
divergence and every store ahead of the binary remains fatal (#1443).
1. The problem, and why entity-link windows are not enough
docs/temporal.md (bi-temporal-lite, 2.0 item 4) answers "what did we know
about X as of June" through validity windows on entity_page_links (V56,
repaired by V58). Issue #656 shows three gaps that live at page grain, not
link grain:
- Paraphrase blindness in audit mode.
as_ofis entity-only: the question must contain the exact retired entity and the page must carry the rightentities:/tags:frontmatter. "Which database were we on when incident X happened?" misses when phrased differently, because FTS/vector/graph are skipped inas_ofmode. - The page timeline is implicit, not queryable as a window. Page
versions form a chain (
supersedes+created_at), but no predicate expresses "versions alive at T" without re-deriving it per query. - V58-class risk has no page-grain equivalent. V58 repaired merge paths
that left link windows open, letting
as_ofresurrect retired knowledge. Whatever page-grain shape we choose must close transactionally on every retire path, not just the supersede path.
2. Vocabulary: this is ingestion time, not valid time
The issue title frames the feature as valid-time ("true in the world").
Phase A's valid_from/valid_to are ingestion time: the version's own
created_at and the superseding version's created_at. That is the same
dimension the entity-link windows already use, materialized at page grain. "Valid-time" stays reserved for the world-time split (Phase B, §7).
Pinned terms for this doc and the implementation PR:
| Term | Meaning | Where |
|---|---|---|
| ingestion window (page grain) | valid_from = the version's created_at; valid_to = the superseding version's created_at, else the retirement instant (§3), else NULL while the version is latest-live |
pages (new) |
| ingestion window (link grain) | existing valid_from / superseded_at on entity_page_links (V56/V58) |
unchanged |
| valid time / world time | when a fact was true in the world (Graphiti's valid_at/invalid_at) |
Phase B only, deferred |
Column names: valid_from / valid_to
valid_frommirrorsentity_page_links.valid_fromdeliberately: same dimension, same predicate shape, same tests. The "valid" here reads as "the version the store treated as current", never as world truth.- The window end is
valid_to, notsuperseded_at, becausepages.superseded_atalready exists with a different meaning: it is the V03 decay-tombstone eviction marker, written exactly whensupersedes IS NULL(sweep eviction, not supersession). Reusing that name would conflate "evicted by the forget sweep" with "replaced by a newer version".valid_toalso covers ends that have no superseding version at all (retirement without successor, below), where "superseded_at" would be wrong. valid_fromduplicatescreated_atby construction. That duplication is accepted (see §4): it keeps the window self-describing per row and theas_ofpredicate symmetric with the link-grain one.
3. The window end: supersede, retire, or open
A version's window closes at exactly one of:
- Supersede: a new version of the same path is written.
valid_to= the new version'screated_at, closed in the same transaction as theis_latestflip (mirrors the link-window close inupsert_page_in_tx). - Retire without successor: decay tombstone, purge-regenerate,
graveyard merge (the V58 class).
valid_to= the retirement instant (recorded explicitly on both grains), closed in the same transaction as the retire. New retire paths must close both grains; V58's audit is the checklist. - Open:
valid_to IS NULLwhile the version is the latest live one.
Deletion stays deletion: purged pages cascade away and the timeline does
not survive a purge, exactly as docs/temporal.md already states for
links. Expiry/TTL is orthogonal: the FTS leg applies not_expired at T;
the entity leg preserves its existing ignore-expiry behavior.
4. Materialized columns vs. a view/join
The window is derivable today: valid_from = p.created_at,
valid_to = (SELECT s.created_at FROM pages s WHERE s.supersedes = p.id)
over the already-indexed pages(supersedes), with a COALESCE(updated_at)
for successor-less retirements. No migration required. Weighed against
stored nullable columns + backfill:
View/join.
- Pro: zero migration (no refinery step, no backup gate, no backfill, no new V58-class risk); a window can never be left open by construction.
- Con: every
as_ofFTS query pays a self-join againstpages_ftscandidates on the read path, with an FTS5+join plan to defend in review; the retirement-instant rule moves into the view definition instead of going away (the V58 logic must still be specified somewhere); no covering index for the range predicate.
Materialized (ALTER TABLE pages ADD COLUMN valid_from/valid_to +
index + one-shot backfill).
- Pro: symmetric with the V56 link windows, with the same predicate shape
(
valid_from <= T AND (valid_to IS NULL OR valid_to > T)), same close-in-the-same-transaction discipline, same test shapes; a sargable range predicate with a covering index for theas_ofFTS join; each row's window is auditable without joining. - Con: it is a backup-gated store migration (§5), a bigger step than the issue suggests; it introduces a new leave-open risk, held down only by transactional closes plus tests with controls.
Recommendation: materialized. The deciding factors are predicate
symmetry across the two grains (one window semantic, two tables) and
keeping the as_of FTS join sargable. The migration cost is real but
bounded and precedented (V56 pattern + the §5 backup gate). If review
prefers zero-migration, the view is a legitimate fallback with identical
query semantics, and the rest of this doc is unchanged either way.
No FTS rebuild is needed in either case: the pages_fts_* triggers index
every version row already (including superseded ones), so version-filtered
FTS only changes a join predicate.
5. Migration: backup-gated, and stated plainly
Phase A is additive (nullable columns + index + backfill, no removed or renamed surface, no changed defaults), but it still has costs:
- Refinery migration, one step: add the nullable columns, backfill
(
valid_from = created_at;valid_tofrom the superseding version'screated_atviapages.supersedes,NULLfor latest; successor-less retired rows closed at the decay marker, else the earliest existing entity-link close, elseupdated_atas an approximation when no retirement timestamp survived), create the(valid_from, valid_to)index. Idempotent re-run migrates zero rows. - Backup gate. The migration refuses to run without a verified pre-migration safety archive, per the current store-migration policy (the #633 ordering fix: archive before the schema migration, abort when the archive cannot be written or verified). The issue understates this step; it is listed here so the implementation PR is sized correctly.
- Rollback. Restore the verified pre-migration snapshot from #633 before starting the older binary. Refinery rejects newer applied schema versions; an older binary cannot read the migrated store. Forward re-runs are no-ops.
6. as_of gains version-filtered FTS: the explicit reversal
docs/temporal.md deliberately runs as_of as a time-travel entity
lookup and skips FTS/vector/graph: "mixing current relevance with
historical validity answers neither honestly" (server.rs returns early
with streams_active: ["entity"], no raw-observation fallback).
Version-filtered FTS at T answers that objection. The maintainer asked for an explicit argument instead of a silent scope change, and this section is it:
- The original objection is about mixing present-tense relevance with past validity. Constraining the FTS corpus to versions whose ingestion window contains T makes the retrieved content historical. Ranking is not a historical snapshot: FTS5 BM25 still uses the current index's statistics, including later versions and other projects. Later writes can change ordering and the limited result set at a fixed T. Phase A retrieves historical versions with current-index relevance; it does not reproduce what search would have ranked then.
- Concretely,
as_ofruns two streams at T and merges via the existing RRF: the unchanged entity-window lookup plus FTS over page versions withvalid_from <= T AND (valid_to IS NULL OR valid_to > T)(and the usual not-expired predicate).explaingains theftsstream;streams_activereports both. - Still out of
as_of: vector (embeddings are present-tense artifacts of the current text; version-scoped vectors are a separate project), graph neighbours (latest-scoped by construction), and the raw-observation fallback (unchanged: off inas_of). - Default path unchanged: without
as_of, retrieval searches latest versions exactly as today. Theas_of+global/scopesrefusal and the invalid-instant error stay as they are.
This directly fixes gap 1 in §1: entity-less pages and paraphrased questions become answerable in audit mode, while the entity stream keeps its precision for entity-phrased questions.
7. Explicitly out of Phase A
- Phase B (world-time frontmatter
true_from/true_until+ confidence) stays deferred per the roadmap: dating facts by LLM without a confidence fallback is untrustworthy, and the zero-LLM path could never populate it.NULL = unknown, fall back to ingestion timeremains a future design, not this one. - Default-path rank changes. The issue proposes capping superseded /
validity-expired pages in non-
as_ofranking, but the default path only indexesis_latest = 1rows, so superseded versions never rank today. The "freshness never touches ranking" complaint is about stale-but-latest pages, which is a different problem. Tag-carried staleness already has an authority mechanism (superseded → 0.65,historical → 0.80inPageAuthority). Phase A makes no default-path rank change; any freshness signal is a separate proposal with eval numbers behind it (roadmap ground rule: baseline before change). - Typed
contradictsedges entering rank: natural follow-up once page windows exist, not this PR.
8. Acceptance for the implementation PR (Phase A only)
- Table-driven test
postgres → sqlite → postgres(+ revert):as_ofinside the middle window returns that version via the entity stream and via version-filtered FTS; default query unchanged. - Backfill idempotence; retire-without-successor closes both grains in the same transaction (V58 checklist paths); purge still destroys.
docs/temporal.mdupdated (the §6 reversal + the pinned vocabulary), CHANGELOG under[Unreleased](### Added,(#656)), migration note.- Full local gate per AGENTS.md; fast Linux CI per merge, full matrix on the release-candidate SHA before any tag, per the CI pacing rule.
9. Questions resolved during implementation
- The v2.1.0 cut landed after your "targeting 2.1" note: does the
implementation PR still ride the
release/2.1line (2.1.x additive) or wait for the next minor? - Materialized pair (
valid_from+valid_to) vs. minimal singlevalid_towithvalid_fromread ascreated_at: is the symmetry worth the duplicated column? - In
as_ofmode, should FTS and entity merge via RRF, or should FTS act strictly as fallback when the entity stream misses?