Files
Snow Lee (Sungwon)andGitHub 5f1ac98dae feat(hub): see what each agent session read, not just what it changed (BEA-98) (#135)
History showed what an agent run CHANGED. What it read lived in a daily
aggregate with no session dimension, so the two could not be joined and
nobody could answer "when my agent answered, what did it look at — and was
it the fresh version or archive/retired-spec.md?".

The join is one string carried through four places: hook -> spool -> hub ->
run card. A run card now marks each change the run also read, lists the
files it read and never touched, and says on screen why a read can be
missing.

The three landmines the issue asks be named here:

1. Op.Note is USER-SETTABLE (`bdrive sync --note`), so joining reads to
   writes on the note string would let any member with write access forge a
   note that collides with a teammate's run card and hang their reads off
   it. Fixed by adding journal.Op.Session — set only by `bdrive sync
   --hook`, never by --note — and joining on that. The note stays settable
   and stays untrusted; the join simply never reads it. Op.Session is
   additive JSONL and, like Mtime, is never an input to Less or Replay, so
   replay determinism is untouched and older ops carry "".

   The read half has the same hole one step further on: POST /reads takes
   the session id from the CLIENT, so a member could report reads under a
   teammate's session and paint files onto their card. Every session row is
   therefore pinned to the ownsDevice-validated device, and the query
   requires ?session= AND ?device= together — a forged row can only be found
   under the forger's own device, which MayActAs guarantees is never
   somebody else's.

2. BUCKET CARDINALITY. Putting the session in the read_stats key would take
   a 2k-file project from ~2k to ~100k rows/day, into a table ReadLedger
   loads whole at boot and full-scans on every heat request, hub-wide — so
   it would slow the Dashboard for projects that never ran an agent. This
   is the escape hatch the spec itself names, taken up front: session rows
   live in their own read_sessions repo, outside ReadLedger.byKey. No
   read_stats PK migration, no change to the resident-row count, ?by=device
   byte-identical. They get their own retention (session_retention_days,
   default 30) which DELETES rather than folds — no heat total was ever
   derived from them.

3. READS ARE RECORDED ONLY FOR PATHS IN THE CURRENT REPLAY, so a session
   that read a file it then deleted shows a change with no read. That is by
   design, and the run card says so in its footer rather than leaving it to
   read as a bug.

Privacy ruling, written into internal/webapp/reads.go before anything
serves it: a session id appears only in History responses on the op that
carries it, and as a ?session= filter INPUT. It is never enumerated — no
listing endpoint, no session column in /heat output, nothing new in
?by=device.

Also: PendingReads now dedupes on (path, session), not path alone. Two
agent sessions on one device between syncs used to collapse into one event
carrying whichever session flushed last — one session's reads silently
credited to another.

Tests: journal round-trip + Less-ignores-Session; the forge test (`sync
--note "claude-code session <someone-else's>"` leaves Session empty); a
multi-device syncer test carrying the session through convergence; spool
per-session dedup; hub round-trip, cross-device forge, query contract and
non-enumeration; db_conformance on file, sqlite AND postgres; runs.ts
grouping incl. legacy fallback; a Playwright spec on the seeded run card.
2026-08-11 04:18:53 +09:00

59 lines
1.8 KiB
Go

package journal
import (
"strings"
"testing"
)
// Op.Session holds the same standing as Mtime: additive on the wire, and
// invisible to the ordering. A journal written by this code still parses in
// the old shape, and an op written before the field existed reads back as "".
func TestSessionIsAdditive(t *testing.T) {
with := op(1, "a", 1, KindPut, "x.txt", "blob1")
with.Session = "8f21e4"
without := op(2, "a", 2, KindPut, "y.txt", "blob2")
data, err := Marshal([]Op{with, without})
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
if !strings.Contains(lines[0], `"session":"8f21e4"`) {
t.Fatalf("op lost its session: %s", lines[0])
}
if strings.Contains(lines[1], "session") {
t.Fatalf("op without a session should emit no session key: %s", lines[1])
}
got, err := Parse(data)
if err != nil {
t.Fatal(err)
}
if got[0].Session != "8f21e4" {
t.Fatalf("Session = %q, want 8f21e4", got[0].Session)
}
if got[1].Session != "" {
t.Fatalf("Session should be empty, got %q", got[1].Session)
}
// A line written by an older device carries no session key at all.
legacy, err := Parse([]byte(`{"seq":1,"lamport":1,"device":"a","kind":"put","path":"z.txt"}`))
if err != nil {
t.Fatal(err)
}
if legacy[0].Session != "" {
t.Fatalf("legacy op invented a session: %q", legacy[0].Session)
}
}
// Replay determinism is the invariant this field must not touch: two ops
// differing ONLY in Session compare equal under Less in both directions, so
// no peer's ordering can depend on it.
func TestSessionDoesNotOrder(t *testing.T) {
a := op(1, "dev", 1, KindPut, "x.txt", "blob1")
b := a
b.Session = "8f21e4"
if Less(a, b) || Less(b, a) {
t.Fatalf("Session must not be an input to Less: Less(a,b)=%v Less(b,a)=%v", Less(a, b), Less(b, a))
}
}