oficina / equipo Public

Abilities: build off holds a person's jobs at the next step

A job whose person can't have building done is held (ability_held_at,
set once, the job card redrawn): its thread's queued turns wait in
dispatchNext, a send and a retry; a step's start is refused ability_off
in Authorize, where the start would be recorded, while a started step
keeps its grant; Coordinator tools that start or change work in its
thread are refused with the instance's text, and project_created (bound
to an earlier authorisation) still runs. team_request, team_reply and
team_nudge check build in their own transaction. Answering a held job's
card from Equipo's page is 403 with "Building is turned off for you, so
this can't start. You can still drop it." (approved); drop still works.
Building back on resumes nothing by itself.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 96d9e0431e59a19dd4ba4913b5575d704f1c4709 parent 87d221b
10 files changed, +316 −2
  • internal/equipo/chat/ability_hold.go +103 −0
  • internal/equipo/chat/handoff.go +6 −0
  • internal/equipo/chat/projects.go +10 −0
  • internal/equipo/chat/retry.go +7 −1
  • internal/equipo/chat/runners.go +10 −0
  • internal/equipo/chat/store.go +9 −1
  • internal/equipo/chat/tools.go +5 −0
  • internal/equipo/chat_page.go +11 −0
  • internal/thintest/assistant_ops_contract_test.go +2 −0
  • internal/thintest/build_off_contract_test.go +153 −0
diff --git a/internal/equipo/chat/ability_hold.go b/internal/equipo/chat/ability_hold.go
new file mode 100644
index 00000000..55336285
--- /dev/null
+++ b/internal/equipo/chat/ability_hold.go
@@ -0,0 +1,103 @@
+package chat
+
+import (
+ "slices"
+
+ "amadan.net/oficina/equipo/internal/equipo/assist"
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Jobs when building is off (spec 2026-11-26-assistant-abilities §6.3):
+// a job whose person can't have building done is held at its next step.
+// The hold is stored on the hand-off (ability_held_at), so neither the
+// switch coming back nor a runner reconnecting resumes it: only the
+// person's own Carry on now does (resumeAbilityHold).
+
+// buildOffCoordinatorText is the Coordinator's refusal for a tool that
+// would start or change work in a held job (model-facing, reviewed as
+// code).
+const buildOffCoordinatorText = "Refused: the person this work is for can't have new work done right now. Stop here; don't start or change anything."
+
+// buildOffTools start or change work in a job's thread: refused while the
+// job is held. Reads, status, notes and cancelling still run, and so does
+// project_created, which records a repository an earlier create_project
+// already authorised (projectCreated checks that binding).
+var buildOffTools = append(slices.Clone(workTools), wire.ToolCreateProject, wire.ToolAddProject, wire.ToolPublishSite)
+
+// buildAllowed reports whether h's person may have building done now.
+func (t *txn) buildAllowed(h handoffRecord) (bool, error) {
+ sw, err := t.switches(h.Person)
+ if err != nil {
+ return false, err
+ }
+ return sw.Can(wire.AbilityBuild), nil
+}
+
+// abilityHeld reports whether h's work is held, holding it first when
+// its person can't have building done: what every shared dispatch path
+// and a step's start check before letting any of its work go.
+func (t *txn) abilityHeld(h handoffRecord) (bool, error) {
+ if h.AbilityHeldAt != nil {
+ return true, nil
+ }
+ ok, err := t.buildAllowed(h)
+ if err != nil || ok {
+ return false, err
+ }
+ return true, t.holdForAbility(h)
+}
+
+// holdForAbility records the hold on h, once, and redraws its job card.
+func (t *txn) holdForAbility(h handoffRecord) error {
+ if h.AbilityHeldAt != nil {
+ return nil
+ }
+ now := t.now()
+ res := t.db.Model(&handoffRecord{}).Where("id = ? AND ability_held_at IS NULL", h.ID).Update("ability_held_at", now)
+ if res.Error != nil || res.RowsAffected == 0 {
+ return res.Error
+ }
+ h.AbilityHeldAt = &now
+ return t.projectJob(h)
+}
+
+// laneHeld reports whether lane is a held job's thread: its queued turns
+// wait.
+func (t *txn) laneHeld(lane string) (bool, error) {
+ id, ok := wire.JobOf(lane)
+ if !ok {
+ return false, nil
+ }
+ var h handoffRecord
+ if err := t.db.Where("id = ?", id).Limit(1).Find(&h).Error; err != nil || h.ID == "" {
+ return false, err
+ }
+ return t.abilityHeld(h)
+}
+
+// taskHeld reports whether task is followed by a held job.
+func (t *txn) taskHeld(task string) (bool, error) {
+ if task == "" {
+ return false, nil
+ }
+ h, ok, err := t.handoffOf("", task)
+ if err != nil || !ok {
+ return false, err
+ }
+ return t.abilityHeld(h)
+}
+
+// requireBuild refuses a team operation that starts or moves work on for
+// a person who can't have building done: ability_off, with the instance's
+// text naming it and who to ask.
+func (t *txn) requireBuild(person string) error {
+ sw, err := t.switches(person)
+ if err != nil {
+ return err
+ }
+ if sw.Can(wire.AbilityBuild) {
+ return nil
+ }
+ res := t.s.abilityRefusal(sw, wire.AbilityBuild)
+ return assist.Failure{Code: res.Code, Message: res.Text}
+}
diff --git a/internal/equipo/chat/handoff.go b/internal/equipo/chat/handoff.go
index 08a89094..94f8bba9 100644
--- a/internal/equipo/chat/handoff.go
+++ b/internal/equipo/chat/handoff.go
@@ -243,6 +243,9 @@ func (s *Store) teamRequest(ctx context.Context, call toolCallRecord, input json
if err != nil {
return err
}
+ if err := t.requireBuild(r.Person); err != nil {
+ return err
+ }
left, err := t.turnFiles(r)
if err != nil {
return err
@@ -1575,6 +1578,9 @@ func (s *Store) teamReply(ctx context.Context, call toolCallRecord, input json.R
if err != nil {
return err
}
+ if err := t.requireBuild(r.Person); err != nil {
+ return err
+ }
var done handoffActionRecord
if err := t.db.Where("ledger_key = ? AND person = ?", key, r.Person).Limit(1).Find(&done).Error; err != nil || done.LedgerKey != "" {
out = json.RawMessage(done.Result)
diff --git a/internal/equipo/chat/projects.go b/internal/equipo/chat/projects.go
index 637c4e1b..07d11d1c 100644
--- a/internal/equipo/chat/projects.go
+++ b/internal/equipo/chat/projects.go
@@ -715,6 +715,16 @@ func (s *Store) AnswerDevice(ctx context.Context, person, credential, id string,
} else if err != nil {
return err
}
+ // A card in a held job's thread that would start or change
+ // work can't be answered; dropping the job still can.
+ if in.Choice != "drop" {
+ if held, err := t.laneHeld(c.Lane); err != nil || held {
+ if held {
+ return ErrAbilityOff
+ }
+ return err
+ }
+ }
if c.Kind == wire.CardHome {
card, cur, e := t.answerHome(person, c, in)
out.Card, current = card, cur
diff --git a/internal/equipo/chat/retry.go b/internal/equipo/chat/retry.go
index 6b4741c6..c7ad0f1b 100644
--- a/internal/equipo/chat/retry.go
+++ b/internal/equipo/chat/retry.go
@@ -100,7 +100,13 @@ func (s *Store) Retry(ctx context.Context, person, id string) (out wire.Turn, er
if err != nil {
return err
}
- if busy == 0 {
+ held, err := t.laneHeld(r.Lane)
+ if err != nil {
+ return err
+ }
+ if held {
+ // A held job's turn waits for the person's Carry on now.
+ } else if busy == 0 {
err = t.dispatch(&r)
} else {
err = t.yieldTurns(person)
diff --git a/internal/equipo/chat/runners.go b/internal/equipo/chat/runners.go
index ac2c8289..8e75bc44 100644
--- a/internal/equipo/chat/runners.go
+++ b/internal/equipo/chat/runners.go
@@ -655,6 +655,16 @@ func (s *Store) Authorize(ctx context.Context, l Lease, stepID string, attempt i
if st.StartedAt != nil {
return nil
}
+ // The start of a step of a held job, or one whose person can't
+ // have building done: refused here, where the start would be
+ // recorded, so it never starts; a step already started keeps its
+ // grant and finishes (abilities §6.3).
+ if held, err := t.taskHeld(st.TaskID); err != nil || held {
+ if held {
+ refused = wire.CodeAbilityOff
+ }
+ return err
+ }
now := t.now()
return t.db.Model(&stepRecord{}).Where("id = ? AND attempt = ?", st.ID, st.Attempt).Update("started_at", now).Error
})
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go
index 2dc0486d..68329d7e 100644
--- a/internal/equipo/chat/store.go
+++ b/internal/equipo/chat/store.go
@@ -718,6 +718,10 @@ func (t *txn) dispatchNext(person, lane string) error {
if err != nil || busy != 0 {
return err
}
+ // A held job's turns wait for the person's Carry on now.
+ if held, err := t.laneHeld(lane); err != nil || held {
+ return err
+ }
var next turnRecord
err = t.db.Where("person = ? AND coalesce(lane, '') = ? AND dispatched = ? AND state = ?", person, lane, false, wire.TurnQueued).Order(personFirst).First(&next).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -865,7 +869,11 @@ func (t *txn) sendSelectedOrigin(lane, person, key, text string, files []string,
if err != nil {
return out, false, err
}
- if busy == 0 {
+ held, err := t.laneHeld(lane)
+ if err != nil {
+ return out, false, err
+ }
+ if busy == 0 && !held {
if err = t.dispatch(&turn); err != nil {
return out, false, err
}
diff --git a/internal/equipo/chat/tools.go b/internal/equipo/chat/tools.go
index 46a863b4..eef6d8f2 100644
--- a/internal/equipo/chat/tools.go
+++ b/internal/equipo/chat/tools.go
@@ -565,6 +565,11 @@ func (t *txn) runTool(l Lease, v *wire.ToolCall) (wire.ToolResult, error) {
if fenced, err := t.handoffFenced(r, v.Name); err != nil || fenced {
return refused("request_cancelled", toolRequestCancelled), err
}
+ if slices.Contains(buildOffTools, v.Name) {
+ if held, err := t.laneHeld(r.Lane); err != nil || held {
+ return refused(wire.CodeAbilityOff, buildOffCoordinatorText), err
+ }
+ }
if err := t.toolActivity(r, v); err != nil {
return wire.ToolResult{}, err
}
diff --git a/internal/equipo/chat_page.go b/internal/equipo/chat_page.go
index c45936d6..a588a9a3 100644
--- a/internal/equipo/chat_page.go
+++ b/internal/equipo/chat_page.go
@@ -791,6 +791,17 @@ func (a *application) chatActionTo(w http.ResponseWriter, r *http.Request, back
return
}
if err := act(ctx, address); err != nil {
+ if errors.Is(err, chat.ErrAbilityOff) {
+ // Building is off for the person: this job's work can't start.
+ data, perr := a.page(r, "jobs")
+ if perr != nil {
+ a.failure(w, r, perr)
+ return
+ }
+ data.Title, data.EmptyKey = "jobs.title", "chat.card.ability_off"
+ renderStatus(w, http.StatusForbidden, "empty", data)
+ return
+ }
if errors.Is(err, chat.ErrCardConflict) {
if strings.Contains(r.Header.Get("Accept"), "application/json") {
runnerJSON(w, http.StatusConflict, struct{}{})
diff --git a/internal/thintest/assistant_ops_contract_test.go b/internal/thintest/assistant_ops_contract_test.go
index e59d9fe7..11032552 100644
--- a/internal/thintest/assistant_ops_contract_test.go
+++ b/internal/thintest/assistant_ops_contract_test.go
@@ -91,6 +91,8 @@ type opsRig struct {
base *FakeBase
a *AssistantClient
protocol int
+ // boardRev counts the boards setBoard has given (build_off tests).
+ boardRev int64
}
func newOpsRig(t *testing.T, providers ...assist.Provider) *opsRig {
diff --git a/internal/thintest/build_off_contract_test.go b/internal/thintest/build_off_contract_test.go
new file mode 100644
index 00000000..703e5683
--- /dev/null
+++ b/internal/thintest/build_off_contract_test.go
@@ -0,0 +1,153 @@
+package thintest
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/url"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// setBoard has the owner's Base answer the board with off turned off (at
+// a new revision) and gives Equipo the hint, as Base does on a change.
+func (o *opsRig) setBoard(off ...string) {
+ o.t.Helper()
+ o.boardRev++
+ o.base.SetBoard(o.base.Token, o.boardRev+1, "Paul", off...)
+ status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "")
+ if status == http.StatusAccepted {
+ // A hint within 5 s of the last fetch waits its turn.
+ time.Sleep(5100 * time.Millisecond)
+ status, _, body = raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "")
+ }
+ var out wire.AbilitiesRefreshed
+ if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != o.boardRev+1 {
+ o.t.Fatalf("hint: %d %s", status, body)
+ }
+}
+
+func refusedOff(t *testing.T, what string, res wire.ToolResult) {
+ t.Helper()
+ if !res.IsError || res.Code != wire.CodeAbilityOff {
+ t.Errorf("%s with building off: %+v", what, res)
+ }
+}
+
+// With building off, team_request, team_reply and team_nudge are refused
+// ability_off, and team_status and team_cancel still run.
+func TestTeamOpsRefusedBuildOff(t *testing.T) {
+ o := newTeamRig(t)
+ o.ask("Fix the sign in page.")
+ request, _ := o.handoff("r1", "Fix the sign in page.", "")
+ o.reply(o.turn)
+ o.setBoard(wire.AbilityBuild)
+ o.ask("Fix the shop too.")
+ refusedOff(t, "team_request", o.use("r2", "team_request", map[string]any{"words": "Fix the shop too."}))
+ refusedOff(t, "team_reply", o.use("rp", "team_reply", map[string]any{"request": request, "answer": "The web one."}))
+ refusedOff(t, "team_nudge", o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Fix the shop too."}))
+ if res := o.use("st", "team_status", map[string]any{}); res.IsError {
+ t.Errorf("team_status: %+v", res)
+ }
+ if res := o.use("cn", "team_cancel", map[string]any{"request": request}); res.IsError && res.Code == wire.CodeAbilityOff {
+ t.Errorf("team_cancel: %+v", res)
+ }
+ if n := o.rows("SELECT count(*) FROM chat_handoffs"); n != 1 {
+ t.Errorf("%d hand-offs", n)
+ }
+}
+
+// Building turned off while a job's thread turn runs: starting or changing
+// work is refused, reads and status still run, and the job is held. A
+// message posted to its thread afterwards waits: the thread's turns
+// aren't dispatched. Answering one of its cards from Equipo's page that
+// would start work is refused.
+func TestJobLaneTurnsHeld(t *testing.T) {
+ o := newTeamRig(t)
+ owner := o.inst.Cookie()
+ o.ask("Make me a website.")
+ request, cmd := o.handoff("j1", "Make me a website.", "")
+ lane := wire.JobLane(request)
+ o.in(cmd.TurnID, func() {
+ if res := o.call("ja", wire.ToolAskPerson, map[string]any{"question": "Which website?", "options": []string{"A new one", "One I have"}}); res.Code != "asked" {
+ t.Fatalf("ask %+v", res)
+ }
+ })
+ o.setBoard(wire.AbilityBuild)
+ o.in(cmd.TurnID, func() {
+ refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"}))
+ refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."}))
+ if res := o.call("st", wire.ToolStatus, map[string]any{}); res.IsError {
+ t.Errorf("status: %+v", res)
+ }
+ })
+ if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 {
+ t.Fatal("the job isn't held")
+ }
+ o.reply(cmd.TurnID)
+ card, rev := o.threadQuestion(request)
+ if status, body := o.post(owner, "/conversation/cards/"+card+"/answer", url.Values{"choice": {"A new one"}, "revision": {itoa(int(rev))}}); status != http.StatusForbidden {
+ t.Errorf("answered from Equipo's page: %d %.200s", status, body)
+ }
+ if status, _ := o.post(owner, "/jobs/"+request+"/messages", url.Values{"body": {"A new one."}, "request_id": {"held-1"}}); status != http.StatusSeeOther {
+ t.Fatalf("thread message: %d", status)
+ }
+ if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 {
+ t.Errorf("%d turns waiting in the held job's thread", n)
+ }
+ // Building on again, or a runner reconnecting, resumes nothing.
+ o.setBoard()
+ if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 {
+ t.Errorf("building back on dispatched the held job's turn")
+ }
+}
+
+// A step of a job dispatched before building went off isn't granted its
+// start; a step already started keeps its grant (and finishes). Building
+// back on doesn't release it: the job is held.
+func TestBuildOffRefusesStart(t *testing.T) {
+ o := newTeamRig(t)
+ o.ask("Fix the README.")
+ request, cmd := o.handoff("b1", "Fix the README.", "")
+ var first, second *wire.DispatchCommand
+ o.in(cmd.TurnID, func() {
+ _, first = o.started("b1", "Fix teh")
+ _, second = o.started("b2", "Fix more")
+ })
+ if refused := o.lease(o.runner, "l-first", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" {
+ t.Fatalf("first step's start refused %q", refused)
+ }
+ o.setBoard(wire.AbilityBuild)
+ if refused := o.lease(o.runner, "l-first-again", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" {
+ t.Errorf("a started step lost its grant: %q", refused)
+ }
+ if refused := o.lease(o.runner, "l-second", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused != wire.CodeAbilityOff {
+ t.Errorf("a step not started yet: %q", refused)
+ }
+ if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 {
+ t.Error("the job isn't held")
+ }
+ o.setBoard()
+ if refused := o.lease(o.runner, "l-second-again", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused != wire.CodeAbilityOff {
+ t.Errorf("building back on released the held step: %q", refused)
+ }
+}
+
+// project_created isn't refused for building being off: it records a
+// repository an earlier create_project already authorised, bound to that
+// authorisation (projectCreated refuses one bound to none), so nothing is
+// left stranded. Everything that would start work is refused.
+func TestProjectCreatedAfterSwitchOff(t *testing.T) {
+ o := newTeamRig(t)
+ o.ask("Make me a website.")
+ _, cmd := o.handoff("pc", "Make me a website.", "")
+ o.setBoard(wire.AbilityBuild)
+ o.in(cmd.TurnID, func() {
+ if res := o.call("other/created", wire.ToolProjectCreated, map[string]any{"repo": "amadan.net/oficina/other"}); res.Code != "invalid" {
+ t.Errorf("unbound project_created: %+v", res)
+ }
+ refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"}))
+ refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"repo": "amadan.net/oficina/site", "title": "Site", "brief": "Build it."}))
+ })
+}