Skip to content

fix(palace): report as-of rows without a retraction that came later - #486

Closed
dimetron wants to merge 1 commit into
mainfrom
fix/483-asof-mask
Closed

dimetron wants to merge 1 commit into
mainfrom
fix/483-asof-mask

Conversation

@dimetron

Copy link
Copy Markdown
Owner

Summary

An as-of view selected the right rows but handed them back carrying the
retraction that happened later. This fixes that, and adds the test that
should have caught it.

View.cond already excludes a statement retracted at or before the view's
transaction:

case viewAsOf:
    parts = append(parts, alias+"t_add <= ?", "("+alias+"t_ret IS NULL OR "+alias+"t_ret > ?)")

but the row kept its t_ret, ret_kind and retracted_at from the
transaction that retracted it afterwards. So Statement.Live() returned
false and the legacy Triple.Status said retracted for a fact that was
live at the instant asked about — the one question a bitemporal store exists
to answer. A caller reusing a historical row (for a report, a diff, or a
"what did I believe last Tuesday" answer) got the present state wearing a
past timestamp.

Reachable from the CLI, which is how I found it on main at 1023b37:

$ pi memory kg query alice --tx-as-of tx1
e1  alice  works_at  acme  —  retracted      # the fact was live at tx1

Fix

Mask the retraction for as-of rows only, matching the reference
implementation's read::row_to_triple:

t_ret: if mask_retraction { None } else { i(5).map(|t| TxId(t as u64)) },
// ...
let mask = matches!(spec.tx, TxSel::AsOf(_));
  • now needs nothing — everything it selects is live.
  • history must keep the retraction; that is its purpose.
  • as-of reports no retraction, so Live() and Status agree with the bound.

Every view-scoped read now goes through View.query (View.lookup for the
single-row reads in Explain) so a future read cannot forget the mask. Get
deliberately keeps queryStatements: it is documented to return a statement
whatever its state. The legacy Invalidate path resolves rows under a
history view and is unaffected.

Verification

  • TestKGAsOfMasksLaterRetraction covers About, Match, Explain and
    QueryWith, and pins the other two views: History still reports the
    retraction, Now reports nothing.

  • Guard seen failing. With the mask neutralised it fails on all four
    paths, naming the cause:

    AsOf(1): e1 reported retracted (t_ret=2) though the retraction was in tx2
    AsOf(1): retraction fields = 0x…/retracted/2026-01-01 00:00:02, want all nil
    AsOf.Match = [{… TRet:0x… RetKind:retracted …}], <nil>; want one live row
    Explain statement reported retracted under as-of: {… TRet:0x… …}
    --tx-as-of tx1 status = "retracted", want "active"
    

    Restored, it passes.

  • go test ./internal/palace/ → ok, -race → ok (19.2s), go vet clean,
    golangci-lint run ./internal/palace/... → 0 issues.

  • go test ./internal/cli/ -run TestMemoryKG → ok (the CLI path the bug
    showed up on).

  • Build on current main after rebase.

Not verified: the full internal/cli package. Running it whole on this host
was starved by concurrent builds (load average 17) and timed out; CI covers it.

Scope

One behavioural change, in a fix/ PR and not folded into phase 2: phase 2
adds more read paths, and every one of them would have inherited the same lie.

An as-of view selected the right rows but handed them back carrying the
later retraction. View.cond already excludes a statement retracted at or
before the view's transaction, yet the row kept its t_ret, ret_kind and
retracted_at from the transaction that retracted it afterwards. So
Statement.Live() returned false and the legacy Triple.Status said
"retracted" for a fact that was live at the instant asked about --
exactly the question a bitemporal store exists to answer. Reachable from
the CLI: `pi memory kg query alice --tx-as-of tx1` printed
status=retracted for a fact retracted later.

Mask the retraction for as-of rows only, matching Tiramemsu's
read::row_to_triple ((t_ret: if mask_retraction { None })). The now view
needs nothing (everything it selects is live) and the history view must
keep the retraction, which is its purpose.

Route every view-scoped read through View.query (and View.lookup for the
single-row Explains) so a new read cannot forget the mask. Get and the
legacy Invalidate path keep queryStatements: Get is documented to return
a statement whatever its state, and the legacy path resolves rows under
a history view.

TestKGAsOfMasksLaterRetraction covers About, Match, Explain and
QueryWith, and asserts History still reports the retraction while Now
reports nothing. With the mask disabled it fails on all four, naming the
retracted row and status=retracted.

Signed-off-by: pi <dimetron@me.com>
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.32%. Comparing base (04ebf5a) to head (f1d0947).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #486   +/-   ##
=======================================
  Coverage   91.31%   91.32%           
=======================================
  Files         403      403           
  Lines       52380    52393   +13     
=======================================
+ Hits        47831    47846   +15     
+ Misses       4543     4541    -2     
  Partials        6        6           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dimetron

Copy link
Copy Markdown
Owner Author

Superseded by #488 — closing rather than rebasing.

Why

This PR was branched from 04ebf5a, before PR #487 (phase 2) merged into main. Phase 2 landed the same fix independently:

  • View.mask + View.query in kg_view.go, wired into about, match, Conflicts and textSearch
  • Explain masks Explanation.Statement
  • a TestKGAsOfMasksLaterRetraction of its own in kg_core2_test.go

So the bulk of this PR is now duplicate, and it conflicted (CONFLICTING/DIRTY) on rebase. The View.lookup helper I added has no remaining caller, since phase 2 chose v.mask([]Statement{*st})[0] inline instead.

What phase 2 left behind

One field still reads the unmasked row. Explain masks the statement:

ex := &Explanation{Statement: v.mask([]Statement{*st})[0], View: v.String()}

but three lines of that block later derives the withdrawal from the raw st.TRet:

if st.TRet != nil {
    if ex.RetractedBy, err = loadTx(ctx, q, *st.TRet); err != nil {

palace-kg-explain renders both, so an as-of view prints a self-contradicting block — reproduced on main at 891ccc2:

- Status: live
- Withdrawn: tx2 at 2026-01-01 00:00:02

for a statement retracted at tx2 while the view asked about tx1. Phase 2's own test checks ex.Statement.TRet but not ex.RetractedBy, which is why it passes.

What moves to #488

Exactly that one line, plus the missing assertion on RetractedBy in the existing test — 27 lines including the test. #488 is branched from current main, so it is mergeable now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant