mirror of
https://github.com/runbear-io/beardrive.git
synced 2026-08-25 08:08:08 +02:00
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.
148 lines
3.8 KiB
Go
148 lines
3.8 KiB
Go
package store
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func openTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
s, err := Open(filepath.Join(t.TempDir(), "volume"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func TestReadSpool(t *testing.T) {
|
|
s := openTestStore(t)
|
|
|
|
// Nothing queued: no batch, no error.
|
|
if evs, err := s.PendingReads(); err != nil || len(evs) != 0 {
|
|
t.Fatalf("empty spool = %v, %v", evs, err)
|
|
}
|
|
|
|
// Repeat reads of one path dedupe to its latest event.
|
|
for i := 0; i < 3; i++ {
|
|
if err := s.LogRead("wiki/a.md", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := s.LogRead("b.md", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
evs, err := s.PendingReads()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(evs) != 2 || evs[0].Path != "wiki/a.md" || evs[1].Path != "b.md" {
|
|
t.Fatalf("batch = %+v, want deduped a.md + b.md", evs)
|
|
}
|
|
if evs[0].Time.IsZero() {
|
|
t.Fatal("events must carry their read time")
|
|
}
|
|
|
|
// The batch survives until cleared — a failed report just retries — and
|
|
// reads logged meanwhile land in a fresh spool behind it.
|
|
if err := s.LogRead("c.md", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
again, err := s.PendingReads()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(again) != 2 || again[0].Path != "wiki/a.md" {
|
|
t.Fatalf("retry batch = %+v, want the same uncleared batch", again)
|
|
}
|
|
if err := s.ClearPendingReads(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
next, err := s.PendingReads()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(next) != 1 || next[0].Path != "c.md" {
|
|
t.Fatalf("post-clear batch = %+v, want just c.md", next)
|
|
}
|
|
s.ClearPendingReads()
|
|
if evs, _ := s.PendingReads(); len(evs) != 0 {
|
|
t.Fatalf("drained spool still returned %+v", evs)
|
|
}
|
|
}
|
|
|
|
func TestReadSpoolSurvivesCorruptLines(t *testing.T) {
|
|
s := openTestStore(t)
|
|
s.LogRead("good.md", "")
|
|
f, err := os.OpenFile(s.readSpoolPath(), os.O_WRONLY|os.O_APPEND, 0o644)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.WriteString(`{"path": "torn`) // a torn write
|
|
f.Close()
|
|
s.LogRead("also-good.md", "")
|
|
evs, err := s.PendingReads()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The torn line joins the next event's line; both are dropped, but the
|
|
// batch itself survives.
|
|
if len(evs) == 0 || evs[0].Path != "good.md" {
|
|
t.Fatalf("batch = %+v, want good.md to survive the torn line", evs)
|
|
}
|
|
}
|
|
|
|
func TestReadSpoolCap(t *testing.T) {
|
|
s := openTestStore(t)
|
|
long := strings.Repeat("d", 1024)
|
|
for i := 0; i < 1100; i++ { // ~1.1 MB of events
|
|
if err := s.LogRead(long+"/"+string(rune('a'+i%26))+".md", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
fi, err := os.Stat(s.readSpoolPath())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fi.Size() > readSpoolMax+4096 {
|
|
t.Fatalf("spool grew past its cap: %d bytes", fi.Size())
|
|
}
|
|
}
|
|
|
|
// Two agent sessions on one device between syncs, both reading one path, are
|
|
// two reads by two sessions — the spool must not collapse them into one
|
|
// event carrying whichever session flushed last, which would credit one
|
|
// session's reads to another on the History run card.
|
|
func TestReadSpoolDedupesPerSession(t *testing.T) {
|
|
s := openTestStore(t)
|
|
for _, e := range []struct{ path, session string }{
|
|
{"wiki/a.md", "sess-1"},
|
|
{"wiki/a.md", "sess-2"},
|
|
{"wiki/a.md", "sess-1"}, // a repeat within one session still collapses
|
|
{"wiki/b.md", "sess-1"},
|
|
{"wiki/c.md", ""}, // no session (an older client / a platform that reports none)
|
|
} {
|
|
if err := s.LogRead(e.path, e.session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
evs, err := s.PendingReads()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]bool{}
|
|
for _, e := range evs {
|
|
got[e.Path+"|"+e.Session] = true
|
|
}
|
|
want := []string{"wiki/a.md|sess-1", "wiki/a.md|sess-2", "wiki/b.md|sess-1", "wiki/c.md|"}
|
|
if len(evs) != len(want) {
|
|
t.Fatalf("batch = %+v, want %d entries", evs, len(want))
|
|
}
|
|
for _, w := range want {
|
|
if !got[w] {
|
|
t.Errorf("batch is missing %q: %+v", w, evs)
|
|
}
|
|
}
|
|
}
|