oficina / equipo Public

Astra round 1, findings 1 to 4: authority, never, dispatch, classes

1. saveTask under the principal authorizes against the stored task's
   creator, never the caller's copy, and refuses a changed creator.
2. The principal never writes a notebook (its own neither cleared nor
   replaced) and never saves a card, so withdrawMergeCards refuses too.
3. Housekeeping dispatches nothing in slice 1: hkSpend refuses outright;
   the reservation boundary comes with slice 2.
4. The executor checks the detector's class against the proposal before
   anything runs (act combines, link links, propose and ask carry out
   nothing yet); a mismatch is a refused row.

Tests that failed first: TestHousekeepingNever (forged creator, own
notes, a merge card withdrawn or resolved, dispatch within a
reservation, a housekeeping turn) and TestHousekeepingClassActions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 0e4f077f19871fb31d1c253ca461c52bcaea743f parent 3999a31
4 files changed, +116 −34
  • internal/equipo/chat/housekeeping.go +27 −23
  • internal/equipo/chat/housekeeping_store_test.go +79 −8
  • internal/equipo/chat/notebook.go +4 −3
  • internal/equipo/chat/projects.go +6 −0
diff --git a/internal/equipo/chat/housekeeping.go b/internal/equipo/chat/housekeeping.go
index c1aa0da1..9e174650 100644
--- a/internal/equipo/chat/housekeeping.go
+++ b/internal/equipo/chat/housekeeping.go
@@ -182,14 +182,17 @@ func (t *txn) hkTaskWrite(k *taskRecord) error {
if t.hk == nil {
return nil
}
- if err := t.hkOwns(k.Creator); err != nil {
- return err
- }
+ // Authority comes from the stored task, never the caller's copy.
var was taskRecord
if err := t.db.Where("id = ?", k.ID).First(&was).Error; err != nil {
return err
}
+ if err := t.hkOwns(was.Creator); err != nil {
+ return err
+ }
switch {
+ case k.Creator != was.Creator:
+ return fmt.Errorf("%w: a task's creator", ErrHousekeepingForbidden)
case k.Stage != was.Stage && (wire.FinalStage(k.Stage) || k.Stage == wire.StageMerging),
k.MergeEligible != was.MergeEligible, k.ReadySHA != was.ReadySHA, k.MergeAsked != was.MergeAsked,
k.MergedSHA != was.MergedSHA, k.CancelRequested != was.CancelRequested:
@@ -198,26 +201,12 @@ func (t *txn) hkTaskWrite(k *taskRecord) error {
return nil
}
-// hkSpend is the check before anything that spends under the principal: a
-// reservation held by the row, and the connection the choice selected
-// healthy, reported and below the stand-down line. Slice 1 dispatches
-// nothing, so in practice this always refuses.
-func (t *txn) hkSpend(person string) error {
- if t.hk == nil {
- return nil
- }
- if t.hk.Reserved.Zero() {
- return fmt.Errorf("%w: dispatch past the reservation", ErrHousekeepingForbidden)
- }
- h, err := t.selectedHeadroom(person)
- if err != nil {
- return err
- }
- if why := housekeeping.StandDown(h, t.hkNow()); why != "" {
- return fmt.Errorf("%w: connection %s", ErrHousekeepingForbidden, why)
- }
- return nil
-}
+// hkSpend is the check before anything that would spend under the
+// principal: a turn or a step dispatched. Slice 1 has no judgement turns
+// and no automatic retries, so it refuses outright; slice 2 replaces it
+// with the reservation boundary (a live claim for this person, a current
+// member, the selected connection's tier and headroom, no reuse).
+func (t *txn) hkSpend(string) error { return t.hkForbid("dispatch") }
// --- The ledger.
@@ -330,6 +319,12 @@ func (t *txn) hkRun(d housekeeping.Detector, f housekeeping.Finding) (bool, erro
r.State = wire.ActionClaimed
return false, t.hkSave(&r, !found)
}
+ // The class decides what a finding may become, before anything runs:
+ // a link or propose detector never combines.
+ if !hkClassActions[d.Class][f.Proposal.Action] {
+ r.State, r.Reason = wire.ActionRefused, wire.ReasonHousekeepingForbidden
+ return false, t.hkSave(&r, !found)
+ }
// Deterministic and free: claim and act in one transaction. The
// action runs in a savepoint so a refusal or an error takes back
// everything it wrote and leaves only the row.
@@ -382,6 +377,15 @@ func (t *txn) savepoint(fn func(*txn) error) error {
// errUnknownAction: a finding asked for an action the store does not know.
var errUnknownAction = errors.New("chat: housekeeping action unknown")
+// hkClassActions is what each class may carry out (spec §3): act
+// combines; link links; propose and ask carry out nothing in slice 1.
+var hkClassActions = map[string]map[string]bool{
+ housekeeping.ClassAct: {housekeeping.ActionCombine: true},
+ housekeeping.ClassLink: {housekeeping.ActionLink: true},
+ housekeeping.ClassPropose: {},
+ housekeeping.ClassAsk: {},
+}
+
// hkActions are the proposals the store knows how to carry out, each
// through the same store functions a person's request uses.
var hkActions = map[string]func(t *txn, r *hkRecord, f housekeeping.Finding) (hkResult, error){
diff --git a/internal/equipo/chat/housekeeping_store_test.go b/internal/equipo/chat/housekeeping_store_test.go
index e1c9576b..317c91b2 100644
--- a/internal/equipo/chat/housekeeping_store_test.go
+++ b/internal/equipo/chat/housekeeping_store_test.go
@@ -263,6 +263,9 @@ func TestHousekeepingNever(t *testing.T) {
}))
p := &Principal{Team: "TEAM1", Person: hkOwner, Detector: "test", FindingID: "hk_test"}
m := r.task("tkm", "Theirs", hkMember)
+ r.must(r.tx(func(t *txn) error {
+ return t.db.Create(&cardRecord{ID: "cmerge", Person: hkOwner, Kind: wire.CardMerge, State: wire.CardOpen, TaskID: k.ID, Options: "[]", Revision: 1, CreatedAt: r.now, UpdatedAt: r.now}).Error
+ }))
never := map[string]func(t *txn) error{
"delete a project": func(t *txn) error { _, err := t.removeProject(hkOwner, "p1"); return err },
"cancel a task": func(t *txn) error { _, err := t.cancelTask(hkOwner, k.ID); return err },
@@ -289,9 +292,29 @@ func TestHousekeepingNever(t *testing.T) {
k := r.load(k.ID)
return t.askMerge(&k, progress{}, false)
},
- "a setting": func(t *txn) error { _, err := t.setSetting("project_home", "x", hkOwner); return err },
- "another's notebook": func(t *txn) error { return t.saveNotebook(hkMember, nil) },
- "another's task": func(t *txn) error { return t.saveTask(&m) },
+ "a setting": func(t *txn) error { _, err := t.setSetting("project_home", "x", hkOwner); return err },
+ "another's notebook": func(t *txn) error { return t.saveNotebook(hkMember, nil) },
+ "another's task": func(t *txn) error { return t.saveTask(&m) },
+ // Astra round 1, finding 1: the caller's copy claims the
+ // principal's person; the stored task is another's.
+ "a forged creator": func(t *txn) error {
+ forged := m
+ forged.Creator, forged.DuplicateOf = hkOwner, "tkone"
+ return t.saveTask(&forged)
+ },
+ // Finding 2: clearing or replacing the person's own notes.
+ "clear own notes": func(t *txn) error { return t.saveNotebook(hkOwner, nil) },
+ "replace own notes": func(t *txn) error { return t.saveNotebook(hkOwner, []wire.NotebookEntry{{Text: "x"}}) },
+ // Finding 2: resolving or withdrawing an open card.
+ "withdraw a merge card": func(t *txn) error { return t.withdrawMergeCards(r.load(k.ID)) },
+ "resolve a card": func(t *txn) error {
+ var c cardRecord
+ if err := t.db.Where("id = ?", "cmerge").First(&c).Error; err != nil {
+ return err
+ }
+ c.State = wire.CardResolved
+ return t.saveCard(&c)
+ },
"dispatch, no reserve": func(t *txn) error { var s stepRecord; t.db.First(&s); return t.dispatchStep(r.load(k.ID), s) },
"a turn, no reserve": func(t *txn) error { return t.dispatch(&turnRecord{ID: "t1", Person: hkOwner}) },
}
@@ -307,6 +330,12 @@ func TestHousekeepingNever(t *testing.T) {
if k2 := r.load(k.ID); k2.Stage != wire.StageNoted || k2.MergeEligible != "" || k2.ReadySHA != "" {
t.Fatalf("task changed: %+v", k2)
}
+ if m2 := r.load(m.ID); m2.Creator != hkMember || m2.DuplicateOf != "" || m2.Revision != m.Revision {
+ t.Fatalf("the forged write reached another's task: %+v", m2)
+ }
+ if n := r.count("SELECT COUNT(*) FROM chat_cards WHERE id = 'cmerge' AND state = ?", wire.CardOpen); n != 1 {
+ t.Fatal("the merge card was withdrawn")
+ }
// Dispatch with a reservation: on a limited connection, refused; on
// the selected connection, healthy, reported and fresh, the gate opens.
@@ -323,9 +352,18 @@ func TestHousekeepingNever(t *testing.T) {
if err := dispatch(); !errors.Is(err, ErrHousekeepingForbidden) {
t.Fatalf("dispatch on a limited connection: %v", err)
}
+ // Finding 3: slice 1 has no judgement turns or retries, so even a
+ // reservation on a healthy, reported, fresh connection dispatches
+ // nothing.
r.connection("cn1", "local1", wire.ConnHealthy, 0.5, r.now)
- if err := dispatch(); err != nil {
- t.Fatalf("dispatch within a reservation on a healthy connection: %v", err)
+ if err := dispatch(); !errors.Is(err, ErrHousekeepingForbidden) {
+ t.Fatalf("dispatch within a reservation in slice 1: %v", err)
+ }
+ if err := r.tx(func(t *txn) error {
+ t.hk = reserved
+ return t.dispatch(&turnRecord{ID: "t2", Person: hkOwner, Kind: wire.TurnHousekeeping})
+ }); !errors.Is(err, ErrHousekeepingForbidden) {
+ t.Fatalf("a housekeeping turn in slice 1: %v", err)
}
// Through the ledger: a proposal whose action reaches a forbidden store
@@ -345,7 +383,8 @@ func TestHousekeepingNever(t *testing.T) {
},
} {
hkActions[name] = act
- t.Cleanup(func() { delete(hkActions, name) })
+ hkClassActions[housekeeping.ClassAct][name] = true
+ t.Cleanup(func() { delete(hkActions, name); delete(hkClassActions[housekeeping.ClassAct], name) })
f := housekeeping.Finding{Detector: "never", Subject: housekeeping.Subject{Kind: housekeeping.SubjectTask, ID: k.ID}, Owner: hkOwner, Key: "never:" + name, Proposal: housekeeping.Proposal{Action: name, Task: k.ID}}
d := housekeeping.Detector{ID: "never", Subject: housekeeping.SubjectTask, Class: housekeeping.ClassAct, Triggers: []string{housekeeping.TriggerSweep}, Judgement: housekeeping.JudgementNone}
if r.run(d, f) {
@@ -683,7 +722,8 @@ func TestHousekeepingLedger(t *testing.T) {
// A team subject's key raised by two people: one row.
team := housekeeping.Detector{ID: "team", Subject: housekeeping.SubjectProject, Class: housekeeping.ClassPropose, Triggers: []string{housekeeping.TriggerSweep}, Judgement: housekeeping.JudgementNone}
hkActions["note"] = func(*txn, *hkRecord, housekeeping.Finding) (hkResult, error) { return hkResult{Action: "note"}, nil }
- t.Cleanup(func() { delete(hkActions, "note") })
+ hkClassActions[housekeeping.ClassPropose]["note"] = true
+ t.Cleanup(func() { delete(hkActions, "note"); delete(hkClassActions[housekeeping.ClassPropose], "note") })
for range 2 {
r.run(team, housekeeping.Finding{Detector: "team", Subject: housekeeping.Subject{Kind: housekeeping.SubjectProject, ID: "p1"}, Key: "stale:p1", Proposal: housekeeping.Proposal{Action: "note"}})
}
@@ -716,7 +756,8 @@ func TestHousekeepingAutoDisable(t *testing.T) {
hkActions["explode"] = func(*txn, *hkRecord, housekeeping.Finding) (hkResult, error) {
return hkResult{}, errors.New("disk on fire")
}
- t.Cleanup(func() { delete(hkActions, "explode") })
+ hkClassActions[housekeeping.ClassAct]["explode"] = true
+ t.Cleanup(func() { delete(hkActions, "explode"); delete(hkClassActions[housekeeping.ClassAct], "explode") })
d := housekeeping.Detector{ID: housekeeping.DuplicateSameCreatorID, Subject: housekeeping.SubjectTask, Class: housekeeping.ClassAct, Triggers: []string{housekeeping.TriggerSweep}, Judgement: housekeeping.JudgementNone}
f := housekeeping.Finding{Subject: housekeeping.Subject{Kind: housekeeping.SubjectTask, ID: "tkone"}, Owner: hkOwner, Key: "boom:1", Proposal: housekeeping.Proposal{Action: "explode"}}
// The action fails inside a caller's transaction that then rolls back:
@@ -811,3 +852,33 @@ func TestHousekeepingPreempts(t *testing.T) {
t.Fatalf("order: %q", order)
}
}
+
+// TestHousekeepingClassActions (Astra round 1, finding 4): the executor
+// checks the detector's class against the proposal before anything runs.
+// A link or propose detector can never combine; an act detector can never
+// merely link through the combine's budget. Each mismatch is refused and
+// changes nothing.
+func TestHousekeepingClassActions(t *testing.T) {
+ r := newHKRig(t)
+ a := r.task("tka", "Swipe back from the edge doesn't work", hkOwner)
+ b := r.task("tkb", "Swipe back from the edge doesn't work again", hkOwner)
+ for _, c := range []struct {
+ class, action string
+ }{{housekeeping.ClassLink, housekeeping.ActionCombine}, {housekeeping.ClassPropose, housekeeping.ActionCombine}, {housekeeping.ClassAsk, housekeeping.ActionCombine}, {housekeeping.ClassAct, housekeeping.ActionLink}} {
+ d := housekeeping.Detector{ID: "x_" + c.class, Subject: housekeeping.SubjectTask, Class: c.class, Triggers: []string{housekeeping.TriggerSweep}, Judgement: housekeeping.JudgementNone}
+ f := combineFinding(b, a, hkOwner)
+ f.Detector, f.Proposal.Action = d.ID, c.action
+ if r.run(d, f) {
+ t.Errorf("%s detector ran %s", c.class, c.action)
+ }
+ if row, _ := r.row(hkOwner, d.ID, f.Key); row.State != wire.ActionRefused || row.Reason != wire.ReasonHousekeepingForbidden {
+ t.Errorf("%s with %s: %+v", c.class, c.action, row)
+ }
+ }
+ if k := r.load(b.ID); k.FoldedInto != "" || k.DuplicateOf != "" || k.Revision != b.Revision {
+ t.Fatalf("a mismatched proposal changed the task: %+v", k)
+ }
+ if n := r.count("SELECT COUNT(*) FROM chat_cards"); n != 0 {
+ t.Fatalf("a mismatched proposal left %d cards", n)
+ }
+}
diff --git a/internal/equipo/chat/notebook.go b/internal/equipo/chat/notebook.go
index 6993234e..faf74da5 100644
--- a/internal/equipo/chat/notebook.go
+++ b/internal/equipo/chat/notebook.go
@@ -44,9 +44,10 @@ func (t *txn) notebookReport(l Lease, v *wire.NotebookReport) error {
}
func (t *txn) saveNotebook(person string, entries []wire.NotebookEntry) error {
- // Housekeeping never touches another person's notebook.
- if t.hk != nil && person != t.hk.Person {
- return t.hkForbid("another person's notebook")
+ // Housekeeping never writes a notebook in slice 1: not another
+ // person's, and never clearing or replacing the person's own.
+ if err := t.hkForbid("notebook"); err != nil {
+ return err
}
if entries == nil {
entries = []wire.NotebookEntry{}
diff --git a/internal/equipo/chat/projects.go b/internal/equipo/chat/projects.go
index 65783460..287a05d4 100644
--- a/internal/equipo/chat/projects.go
+++ b/internal/equipo/chat/projects.go
@@ -268,6 +268,12 @@ func (t *txn) cardEvent(c cardRecord) error {
}
func (t *txn) saveCard(c *cardRecord) error {
+ // Housekeeping never answers, resolves or withdraws a card (a merge
+ // card through withdrawMergeCards included); its receipts are created,
+ // not saved.
+ if err := t.hkForbid("card"); err != nil {
+ return err
+ }
c.Revision++
c.UpdatedAt = t.now()
if err := t.db.Model(&cardRecord{}).Where("id = ?", c.ID).Select("state", "choice", "answer_message", "undo", "revision", "updated_at").Updates(c).Error; err != nil {