oficina / sheets Public

Add atomic initial tab content and complete bounded range snapshots

codex agent, operated by paul@keymail.dev pushed by paul@keymail.dev ba4f50caf782a0852f2d24d174f415f4f0f5d26b parent 3f299d5
6 files changed, +262 −0
  • docs/superpowers/specs/2026-10-25-agent-access.md +14 −0
  • internal/sheets/store/handle_blank_cap_test.go +21 −0
  • internal/sheets/store/handle_range.go +50 −0
  • internal/sheets/store/initial_export_test.go +10 −0
  • internal/sheets/store/initial_tab.go +40 −0
  • internal/sheets/store/initial_tab_test.go +127 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/specs/2026-10-25-agent-access.md b/docs/superpowers/specs/2026-10-25-agent-access.md
index 323fc9e..22d00d6 100644
--- a/docs/superpowers/specs/2026-10-25-agent-access.md
+++ b/docs/superpowers/specs/2026-10-25-agent-access.md
@@ -109,3 +109,17 @@ operations through the suite HTTP/MCP gateway. New gates must fail against
material adverse mutations. Run complete CI, highest-capability branch review,
and deployed canary verification before merge. Source/spec approval does not
claim deployment.
+
+## Store foundation review (2026-10-25)
+
+GPT-6 Astra approved the first store slice after two calculation-completeness
+findings were fixed. ReadRange holds one handle lock for raw/calculated values
+and revision, refuses a tab with durable dirty markers, and refuses nonempty
+inputs with missing caches. Capped browser recalculation and unrecoverable
+corrupt cache states therefore produce explicit failure rather than old or
+empty values at a new revision. InitializeTab never registers its transaction
+handle and shares the caller's atomic commit/rollback. Focused race tests passed.
+Five effective mutations failed: cache transaction handle, remove range lock,
+remove initial-tab emptiness guard, ignore dirty markers, ignore missing cache.
+This is store-slice approval; app adapter, full CI, whole-branch review and
+deployed canary remain required.
diff --git a/internal/sheets/store/handle_blank_cap_test.go b/internal/sheets/store/handle_blank_cap_test.go
index fa46609..733f155 100644
--- a/internal/sheets/store/handle_blank_cap_test.go
+++ b/internal/sheets/store/handle_blank_cap_test.go
@@ -118,6 +118,24 @@ func TestBlankFeederOverCapDependentsAreRecoverable(t *testing.T) {
t.Fatalf("seed dependent cells: %v", err)
}
+ // A corrupt cache with equal version stamps cannot be repaired when its
+ // fanout exceeds the per-pass cap. It must not masquerade as an empty cell.
+ feeder := db.Model(&store.Cell{}).Where("tab_id=? AND row_id=? AND col_id=?", localTab, feederRow, colA)
+ if err := feeder.Update("cached_sealed", []byte("broken-cache")).Error; err != nil {
+ t.Fatal(err)
+ }
+ broken, err := store.Open(db, s, localTab)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if snapshot, err := broken.ReadRange(0, 0, 1, 1); err == nil {
+ t.Fatalf("range hid missing cache as empty: %+v", snapshot)
+ }
+ broken.Release()
+ if err := feeder.Update("cached_sealed", feederCachedSealed).Error; err != nil {
+ t.Fatal(err)
+ }
+
h, err := store.Open(db, s, localTab)
if err != nil {
t.Fatalf("store.Open: %v", err)
@@ -144,6 +162,9 @@ func TestBlankFeederOverCapDependentsAreRecoverable(t *testing.T) {
if !rep.Capped {
t.Fatal("blank unexpectedly recalculated")
}
+ if snapshot, err := h.ReadRange(1, 0, 1, 1); err == nil {
+ t.Fatalf("range served stale calculation at new revision: %+v", snapshot)
+ }
var watermark uint64
if err := db.Raw("SELECT content_version FROM tabs WHERE id = ?", localTab).Scan(&watermark).Error; err != nil {
t.Fatal(err)
diff --git a/internal/sheets/store/handle_range.go b/internal/sheets/store/handle_range.go
new file mode 100644
index 0000000..05057ad
--- /dev/null
+++ b/internal/sheets/store/handle_range.go
@@ -0,0 +1,50 @@
+package store
+
+import (
+ "errors"
+ "math"
+
+ "amadan.net/oficina/sheets/internal/sheets/calc"
+)
+
+// RangeCell is one coordinate and its raw/calculated content at one watermark.
+type RangeCell struct {
+ Row, Col int
+ Raw string
+ Value calc.Value
+}
+type RangeSnapshot struct {
+ Version uint64
+ Cells []RangeCell
+}
+
+// ReadRange takes one lock for inputs, calculated values and next-stamp version.
+// It never grows axes or evaluates a formula. App authority is checked upstream.
+func (h *Handle) ReadRange(row, col, height, width int) (RangeSnapshot, error) {
+ if row < 0 || col < 0 || height < 1 || width < 1 || height > MaxCellBatch || width > MaxCellBatch || height > MaxCellBatch/width || row > math.MaxInt-height || col > math.MaxInt-width {
+ return RangeSnapshot{}, errors.New("store: invalid range")
+ }
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ // A capped browser edit leaves durable dirty markers and old graph caches.
+ // Refuse the tab until recovery completes; never label those values current.
+ var stale int
+ if err := h.db.Raw("SELECT 1 FROM cells WHERE tab_id=? AND cache_version < input_version LIMIT 1", h.tabID).Scan(&stale).Error; err != nil {
+ return RangeSnapshot{}, err
+ }
+ if stale != 0 {
+ return RangeSnapshot{}, errors.New("store: calculation incomplete")
+ }
+ out := RangeSnapshot{Version: h.nextVersion, Cells: make([]RangeCell, 0, height*width)}
+ for r := row; r < row+height; r++ {
+ for c := col; c < col+width; c++ {
+ key := calc.CellKey{RowID: uint64(r) + 1, ColID: uint64(c) + 1}
+ value, cached := h.graph.CachedValue(key)
+ if !cached && h.rawText[key] != "" {
+ return RangeSnapshot{}, errors.New("store: calculation cache unavailable")
+ }
+ out.Cells = append(out.Cells, RangeCell{Row: r, Col: c, Raw: h.rawText[key], Value: value})
+ }
+ }
+ return out, nil
+}
diff --git a/internal/sheets/store/initial_export_test.go b/internal/sheets/store/initial_export_test.go
new file mode 100644
index 0000000..0a40697
--- /dev/null
+++ b/internal/sheets/store/initial_export_test.go
@@ -0,0 +1,10 @@
+package store
+
+import "gorm.io/gorm"
+
+func InitialHandleCachedForTest(db *gorm.DB, tabID uint64) bool {
+ registryMu.Lock()
+ defer registryMu.Unlock()
+ _, ok := registry[handleKey{db: db, tabID: tabID}]
+ return ok
+}
diff --git a/internal/sheets/store/initial_tab.go b/internal/sheets/store/initial_tab.go
new file mode 100644
index 0000000..d53dcd7
--- /dev/null
+++ b/internal/sheets/store/initial_tab.go
@@ -0,0 +1,40 @@
+package store
+
+import (
+ "errors"
+
+ "amadan.net/oficina/sheets/internal/sheets/seal"
+ "gorm.io/gorm"
+)
+
+// InitializeTab writes initial cells inside the caller's creation transaction.
+// Its handle is never registered or returned: it holds tx and must be discarded
+// on either commit or rollback. The caller opens a canonical handle after commit.
+func InitializeTab(tx *gorm.DB, sealer *seal.Sealer, tabID uint64, writes []CellWrite, actor uint64) error {
+ if tx == nil || tx.Statement == nil {
+ return errors.New("store: creation transaction required")
+ }
+ if _, ok := tx.Statement.ConnPool.(gorm.TxCommitter); !ok {
+ return errors.New("store: creation transaction required")
+ }
+ var tab Tab
+ if err := tx.Select("id", "content_version").First(&tab, tabID).Error; err != nil {
+ return err
+ }
+ var count int64
+ if err := tx.Model(&Cell{}).Where("tab_id=?", tabID).Count(&count).Error; err != nil {
+ return err
+ }
+ if tab.ContentVersion != 0 || count != 0 {
+ return errors.New("store: initial tab is not empty")
+ }
+ h, err := buildHandle(tx, sealer, tabID)
+ if err != nil {
+ return err
+ }
+ if len(writes) == 0 {
+ return nil
+ }
+ _, err = h.SetBatch(writes, actor)
+ return err
+}
diff --git a/internal/sheets/store/initial_tab_test.go b/internal/sheets/store/initial_tab_test.go
new file mode 100644
index 0000000..92d8a60
--- /dev/null
+++ b/internal/sheets/store/initial_tab_test.go
@@ -0,0 +1,127 @@
+package store_test
+
+import (
+ "errors"
+ "math"
+ "strconv"
+ "sync"
+ "testing"
+
+ "amadan.net/oficina/sheets/internal/sheets/store"
+ "gorm.io/gorm"
+)
+
+func TestInitialTabSharesOuterTransactionAndNeverCachesIt(t *testing.T) {
+ db, sealer, _ := seeded(t)
+ writes := []store.CellWrite{{Row: 0, Col: 0, Raw: "21"}, {Row: 1, Col: 0, Raw: "=A1*2"}}
+ if err := store.InitializeTab(db, sealer, 99, writes, actor); err == nil {
+ t.Fatal("nontransaction initialization accepted")
+ }
+ refused := errors.New("parent rollback")
+ create := func(rollback bool) error {
+ return db.Transaction(func(tx *gorm.DB) error {
+ if err := tx.Create(&store.Tab{ID: 99, SheetID: 1}).Error; err != nil {
+ return err
+ }
+ if err := store.InitializeTab(tx, sealer, 99, writes, actor); err != nil {
+ return err
+ }
+ if store.InitialHandleCachedForTest(tx, 99) {
+ t.Fatal("transaction handle entered live cache")
+ }
+ if rollback {
+ return refused
+ }
+ if err := store.InitializeTab(tx, sealer, 99, writes, actor); err == nil {
+ t.Fatal("initial values reapplied")
+ }
+ return nil
+ })
+ }
+ if err := create(true); !errors.Is(err, refused) {
+ t.Fatal(err)
+ }
+ for _, model := range []any{&store.Tab{}, &store.Cell{}, &store.Row{}, &store.Col{}} {
+ var count int64
+ q := db.Model(model)
+ if _, ok := model.(*store.Tab); ok {
+ q = q.Where("id=99")
+ } else {
+ q = q.Where("tab_id=99")
+ }
+ if err := q.Count(&count).Error; err != nil || count != 0 {
+ t.Fatal("outer rollback retained rows", count, err)
+ }
+ }
+ if err := create(false); err != nil {
+ t.Fatal(err)
+ }
+ h, err := store.Open(db, sealer, 99)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer h.Release()
+ snapshot, err := h.ReadRange(0, 0, 2, 1)
+ if err != nil || snapshot.Cells[0].Value.Num != 21 || snapshot.Cells[1].Value.Num != 42 || snapshot.Version != h.Version() {
+ t.Fatal(snapshot, err)
+ }
+ if _, err = h.SetBatch([]store.CellWrite{{Row: 0, Col: 0, Raw: "8"}}, actor); err != nil {
+ t.Fatal("canonical handle retained committed transaction", err)
+ }
+ snapshot, err = h.ReadRange(0, 0, 2, 1)
+ if err != nil || snapshot.Cells[1].Value.Num != 16 {
+ t.Fatal(snapshot, err)
+ }
+}
+
+func TestRangeSnapshotIsBoundedAndCoherentDuringBatchWrites(t *testing.T) {
+ h := openSeeded(t)
+ if _, err := h.SetBatch([]store.CellWrite{{Row: 0, Col: 0, Raw: "0"}, {Row: 1, Col: 0, Raw: "0"}}, actor); err != nil {
+ t.Fatal(err)
+ }
+ for _, box := range [][4]int{{-1, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 501, 1}, {0, 0, 25, 25}, {math.MaxInt, 0, 1, 1}} {
+ if _, err := h.ReadRange(box[0], box[1], box[2], box[3]); err == nil {
+ t.Fatal("unbounded range accepted", box)
+ }
+ }
+ done := make(chan struct{})
+ var wg sync.WaitGroup
+ errs := make(chan error, 1)
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ defer close(done)
+ for i := 1; i <= 20; i++ {
+ value := strconv.Itoa(i)
+ if _, err := h.SetBatch([]store.CellWrite{{Row: 0, Col: 0, Raw: value}, {Row: 1, Col: 0, Raw: value}}, actor); err != nil {
+ errs <- err
+ return
+ }
+ }
+ }()
+ var last uint64
+readLoop:
+ for {
+ snapshot, err := h.ReadRange(0, 0, 2, 1)
+ if err != nil || snapshot.Cells[0].Raw != snapshot.Cells[1].Raw || snapshot.Cells[0].Value.Num != snapshot.Cells[1].Value.Num || snapshot.Version < last {
+ t.Error("mixed range snapshot", snapshot, err)
+ break
+ }
+ last = snapshot.Version
+ select {
+ case <-done:
+ break readLoop
+ default:
+ }
+ }
+ wg.Wait()
+ select {
+ case err := <-errs:
+ t.Fatal(err)
+ default:
+ }
+ snapshot, err := h.ReadRange(0, 0, 2, 1)
+ if err != nil || snapshot.Cells[0].Value.Num != 20 || snapshot.Version != h.Version() {
+ t.Fatal(snapshot, err)
+ }
+}