From cab76319b83ea42f18c2d909d232a7673455602f Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 02:19:43 +0100 Subject: [PATCH] R1-1: a page hands a kept delete, so a deleted key never comes back alive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The page watch ignored delete markers, so a reader following pages of a bucket with history kept a deleted key's last value. A kept delete (a purge reads as one) is now an entry deleted: true with no value at its sequence, seen-again like any message (review round 1, finding 1, state.go:375). Builds: design 47 H2 §779, H-A-451 (1), H-A-382 (2) --- node-tools/internal/bus/state.go | 19 +++++++++++---- .../internal/runtime/state_history_test.go | 24 ++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/node-tools/internal/bus/state.go b/node-tools/internal/bus/state.go index 6fb9508..59d90e3 100644 --- a/node-tools/internal/bus/state.go +++ b/node-tools/internal/bus/state.go @@ -40,14 +40,18 @@ type StateIssued struct { // StateEntry is one key's current value. type StateEntry struct { - Key string `json:"key"` - Value json.RawMessage `json:"value"` + Key string `json:"key"` + // Value is none on a delete a page hands (Deleted). + Value json.RawMessage `json:"value,omitempty"` Revision uint64 `json:"revision"` // At is when the bus wrote this value (the entry's created time), as stdio.StateEntry.At reads it. At time.Time `json:"at,omitzero"` // SeenAgain is set in a page on a value the bus wrote after the read reached its bucket, so a reader knows it replaces // any value it holds for the key (design 47 H2 §779, H-A-382). SeenAgain bool `json:"seen-again,omitempty"` + // Deleted is set in a page on a key's kept delete (a purge reads as one), handed at its sequence with no value, so a + // reader following pages removes the key and a deleted key never comes back alive (design 47 H2 §779, H-A-451 (1)). + Deleted bool `json:"deleted,omitempty"` } // StateChange is one change a watch delivers: a key put, deleted, or expired (its own time to live, or the @@ -372,7 +376,8 @@ func (r *pageRead) bucket(kv jetstream.KeyValue, from pageCursor, reached bool) } from.mark = bs.StreamInfo().State.LastSeq } - opts := []jetstream.WatchOpt{jetstream.IncludeHistory(), jetstream.IgnoreDeletes()} + // Deletes are handed too: a page that dropped them would bring a deleted key with history back alive (H-A-451 (1)). + opts := []jetstream.WatchOpt{jetstream.IncludeHistory()} if from.after > 0 { opts = append(opts, jetstream.ResumeFromRevision(from.after+1)) } @@ -399,14 +404,18 @@ func (r *pageRead) bucket(kv jetstream.KeyValue, from pageCursor, reached bool) } e = got } - value := valueOf(e.Value()) + deleted := e.Operation() != jetstream.KeyValuePut + var value json.RawMessage + if !deleted { + value = valueOf(e.Value()) + } full := len(r.page.Entries) > 0 && (r.size+len(e.Key())+len(value) > r.maxBytes || PageClock().Sub(r.started) >= r.budget) if full { r.page.Next = from.String() return nil } r.page.Entries = append(r.page.Entries, StateEntry{Key: e.Key(), Value: value, Revision: e.Revision(), At: e.Created().UTC(), - SeenAgain: e.Revision() > from.mark}) + SeenAgain: e.Revision() > from.mark, Deleted: deleted}) r.size += len(e.Key()) + len(value) from.after = e.Revision() } diff --git a/node-tools/internal/runtime/state_history_test.go b/node-tools/internal/runtime/state_history_test.go index ab47047..1ba6f1e 100644 --- a/node-tools/internal/runtime/state_history_test.go +++ b/node-tools/internal/runtime/state_history_test.go @@ -172,8 +172,10 @@ func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModul } } type entry struct { - Key string `json:"key"` - SeenAgain bool `json:"seen-again"` + Key string `json:"key"` + Value json.RawMessage `json:"value"` + SeenAgain bool `json:"seen-again"` + Deleted bool `json:"deleted"` } type page struct { Entries []entry `json:"entries"` @@ -191,7 +193,10 @@ func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModul said := func(p page) string { var out []string for _, e := range p.Entries { - out = append(out, e.Key+map[bool]string{true: "+again"}[e.SeenAgain]) + out = append(out, e.Key+map[bool]string{true: "+again"}[e.SeenAgain]+map[bool]string{true: "+deleted"}[e.Deleted]) + if e.Deleted == (e.Value != nil) { + out = append(out, "(value "+string(e.Value)+")") + } } return strings.Join(out, ",") + map[bool]string{true: " (more)"}[p.Next != ""] } @@ -214,6 +219,19 @@ func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModul if got := said(read(owner, "issues", "", 1<<20)); got != "1,2,3,1" { t.Errorf("a fresh read is %s, want each kept message once, 1's older revision first: 1,2,3,1", got) } + // A kept delete is handed as an entry deleted, with no value, at its sequence, so a deleted key never comes back alive + // from a bucket with history (H-A-451 (1)). + for _, v := range []int{1, 2} { + if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "gone", "value": v}); err != nil { + t.Fatal(err) + } + } + if _, err := ask(owner, "delete", map[string]any{"state": "issues", "key": "gone"}); err != nil { + t.Fatal(err) + } + if got := said(read(owner, "issues", "", 1<<20)); got != "1,2,3,1,gone,gone,gone+deleted" { + t.Errorf("a read past a delete is %s, want its two values and then the delete: 1,2,3,1,gone,gone,gone+deleted", got) + } if _, err := ask(owner, "page", map[string]any{"state": "issues", "cursor": "2"}); err == nil || !strings.Contains(err.Error(), "not a cursor") { t.Errorf("a page after a key, not a cursor, answered %v; want it refused", err)