Spend: protocol 3 carries feedback and turns; migration renumbered 0059
projects-ux takes 0058 (chat_project_stars) and merges first; it is not on origin/main yet, so the usage columns move to 0059. An upgrade test drops them from a database with usage rows and checks the migration brings them back null while the task page still sums the rows. Mixed versions: protocol 3 adds DispatchCommand.Feedback and Usage.Turns. Encode drops both for a protocol 1 or 2 peer (downgrade3) and passes a frame without them through untouched; the runner offers protocol 3 and encodes what it sends at the spoken protocol. An older runner therefore never sees feedback and runs a fix round fresh, as before; an older instance never sees turns. Tests: TestEncodeProtocol2DropsProtocol3Fields (each frame both ways); TestContractFixRoundFeedbackByProtocol (protocols 1 and 2 get no feedback, 3 does); TestLinkSpeaksProtocol2 (a new runner sends no turns to a protocol 2 instance). Making downgrade3 a no-op fails all three.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+193
−20
.rastrillo/budgets.txt+1 −1internal/equipo/migrations/{0058_usage_round_turns.sql => 0059_usage_round_turns.sql}+0 −0internal/runner/thin/protocol_test.go+20 −6internal/thintest/review_contract_test.go+43 −4internal/thintest/spend_pages_test.go+40 −0internal/wire/frames.go+1 −0internal/wire/runners.go+44 −9internal/wire/runners_test.go+43 −0internal/wire/tasks.go+1 −0
CI passed — run details
Intent
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 8bbfb3f6..29f875d2 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -62,4 +62,4 @@ perf GET /conversation/attachments/{id}/preview skip Composer attachments: an im |
| perf GET /api/v1/runners skip Mac runners person API: bearer-only JSON for devices; the browser shows runners in the Team section. |
| time cmd/equipo-runner 60s Mac runners slice B: TestReleaseRunnerHasNoFakeWorker builds the release runner exactly as make release-runner does and the thin package under the testbuild tag (measured 11.9s locally with a warm cache). |
| size internal/headless - 8300 Claude's sign-in by a link adds its end-to-end, contained-launch and real runner kill tests (signin_test.go, signin_contained_test.go, about 400 lines); measured 8090. |
| -size internal/thintest - 10100 Spend 1 adds the task page's spend contract (spend_pages_test.go, about 60 lines); measured 10025. Claude's sign-in by a link adds the code contract (signin_contract_test.go: the card, the code route's refusals, the wrong-code card, the kind it becomes and the canary sweep, about 220 lines); measured 9896. The Connections Add page and the Settings rail add their page contracts and the rail's browser drive (connections_add_pages_test.go, settings_rail_pages_test.go, about 230 lines); measured 9676. Claude setup tokens add the token and Check again contracts; measured 9444 with the composer and Mac lines merged. The instant thinking line adds its browser drive (instant_browser_test.go, about 230 lines); measured 9064 with the composer and Mac lines merged. Mac runners add the role, scoping, migration, placement, withdrawal, transfer and publication contracts (about 900 test lines) beside the thin v1 contracts; one package keeps one instance harness. |
| +size internal/thintest - 10200 Spend 1 adds the task page's spend contract and the 0059 upgrade (spend_pages_test.go, about 100 lines) and the fix round feedback by protocol (about 40); measured 10104. Claude's sign-in by a link adds the code contract (signin_contract_test.go: the card, the code route's refusals, the wrong-code card, the kind it becomes and the canary sweep, about 220 lines); measured 9896. The Connections Add page and the Settings rail add their page contracts and the rail's browser drive (connections_add_pages_test.go, settings_rail_pages_test.go, about 230 lines); measured 9676. Claude setup tokens add the token and Check again contracts; measured 9444 with the composer and Mac lines merged. The instant thinking line adds its browser drive (instant_browser_test.go, about 230 lines); measured 9064 with the composer and Mac lines merged. Mac runners add the role, scoping, migration, placement, withdrawal, transfer and publication contracts (about 900 test lines) beside the thin v1 contracts; one package keeps one instance harness. |
diff --git a/internal/equipo/migrations/0058_usage_round_turns.sql b/internal/equipo/migrations/0059_usage_round_turns.sql| similarity index 100% |
| rename from internal/equipo/migrations/0058_usage_round_turns.sql |
| rename to internal/equipo/migrations/0059_usage_round_turns.sql |
diff --git a/internal/runner/thin/protocol_test.go b/internal/runner/thin/protocol_test.go| index c233794c..a6e835a8 100644 |
| --- a/internal/runner/thin/protocol_test.go |
| +++ b/internal/runner/thin/protocol_test.go |
| @@ -48,7 +48,7 @@ func olderInstance(t *testing.T, c *websocket.Conn) (refused bool, hello []byte) |
| return false, raw |
| } |
| -// A new runner against an older instance: its protocol 2 hello is refused |
| +// A new runner against an older instance: its current hello is refused |
| // once, it says hello again at protocol 1 (exactly the slice 4 hello), logs |
| // that the instance is older, and runs at protocol 1. |
| func TestLinkFallsBackToProtocol1(t *testing.T) { |
| @@ -57,8 +57,8 @@ func TestLinkFallsBackToProtocol1(t *testing.T) { |
| link, _, _, _ := startLink(t, inst.url, &stubCoordinator{}, func(*State) {}) |
| link.Logger = slog.New(slog.NewTextHandler(logs, nil)) |
| first := <-inst.conns |
| - if refused, raw := olderInstance(t, first); !refused || !strings.Contains(string(raw), `"protocol":2`) { |
| - t.Fatalf("first hello was not protocol 2: %s", raw) |
| + if refused, raw := olderInstance(t, first); !refused || !strings.Contains(string(raw), `"protocol":3`) { |
| + t.Fatalf("first hello was not the current protocol: %s", raw) |
| } |
| second := <-inst.conns |
| if refused, raw := olderInstance(t, second); refused { |
| @@ -111,8 +111,10 @@ func TestWorkerRefusesAnOlderInstance(t *testing.T) { |
| } |
| } |
| -// Against a protocol 2 instance the runner speaks protocol 2 and learns |
| -// its role. |
| +// The runner offers the current protocol; against a protocol 2 instance |
| +// it speaks protocol 2 and learns its role, and what it sends then has |
| +// no protocol 3 field (a usage row's turns), which that instance's |
| +// strict decoder would refuse. |
| func TestLinkSpeaksProtocol2(t *testing.T) { |
| inst := newFakeInstance(t) |
| link, _, _, _ := startLink(t, inst.url, &stubCoordinator{}) |
| @@ -121,7 +123,7 @@ func TestLinkSpeaksProtocol2(t *testing.T) { |
| var f wire.Frame |
| _ = json.Unmarshal(raw, &f) |
| _, body, _ := wire.ParseFrame(raw) |
| - if body.(*wire.Hello).Protocol != wire.Protocol2 { |
| + if body.(*wire.Hello).Protocol != wire.ProtocolCurrent { |
| t.Fatalf("hello %s", raw) |
| } |
| write(t, c, wiretest.Fixture("frames/welcome-worker.json")) |
| @@ -129,6 +131,18 @@ func TestLinkSpeaksProtocol2(t *testing.T) { |
| if p, role := link.Spoken(); p != wire.Protocol2 || role != wire.RoleWorker { |
| t.Fatalf("spoken %d %q", p, role) |
| } |
| + n := int64(3) |
| + state := link.State |
| + if err := state.tx(context.Background(), func(tx *sql.Tx) error { |
| + return state.enqueue(tx, wire.FrameStep, "", wire.StepReport{TaskID: "tk1", StepID: "s1", Attempt: 1, State: wire.StepDone, Findings: []wire.Finding{}, Termination: true, Usage: []wire.Usage{{Provider: "claude", Connection: "claude-1", Model: "m", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, Turns: &n}}}) |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + link.Notify() |
| + _, raw = read(t, c, wire.FrameStep) |
| + if strings.Contains(string(raw), `"turns"`) || !strings.Contains(string(raw), `"connection":"claude-1"`) { |
| + t.Fatalf("protocol 3 field sent at protocol 2: %s", raw) |
| + } |
| } |
| // A runner the instance made a worker stays one, whatever its OS: the |
diff --git a/internal/thintest/review_contract_test.go b/internal/thintest/review_contract_test.go| index 94e7029a..f3e37c62 100644 |
| --- a/internal/thintest/review_contract_test.go |
| +++ b/internal/thintest/review_contract_test.go |
| @@ -1,6 +1,7 @@ |
| package thintest |
| import ( |
| + "fmt" |
| "slices" |
| "strings" |
| "testing" |
| @@ -93,10 +94,10 @@ func TestContractReviewAndChecks(t *testing.T) { |
| if fix.StepID == eng.StepID || !strings.Contains(fix.Brief, "The second typo is still there. (README.md:7)") || !strings.Contains(fix.Brief, "Fix round 1 of 2") { |
| t.Fatalf("fix round %+v", fix) |
| } |
| - // The feedback also comes apart, the brief's own fix-round part, for |
| - // a runner that resumes Engineering's session; the first round had none. |
| - if !strings.HasPrefix(fix.Feedback, "Fix round 1 of 2") || !strings.HasSuffix(fix.Brief, fix.Feedback) || strings.Contains(fix.Feedback, "Fix the README typo") || eng.Feedback != "" || rv1.Feedback != "" { |
| - t.Fatalf("fix round feedback %q (brief %q)", fix.Feedback, fix.Brief) |
| + // This runner speaks protocol 1: the fix round's feedback, a protocol 3 |
| + // field, never reaches it (TestContractFixRoundFeedbackByProtocol). |
| + if fix.Feedback != "" || eng.Feedback != "" || rv1.Feedback != "" { |
| + t.Fatalf("fix round feedback at protocol 1 %q", fix.Feedback) |
| } |
| k, _ := r.client.Task(ctx, id) |
| if k.Stage != wire.StageInProgress || k.CandidateSHA != cand || k.Verdict != wire.VerdictChanges || k.Checks != wire.ChecksPassed || k.FixRounds != 1 || k.NeedsYou != "" { |
| @@ -406,3 +407,41 @@ func TestContractCannotRunReasonsAreDelivered(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// A fix round's feedback goes only to a runner that speaks protocol 3: |
| +// an older runner (protocol 1 or 2, whose strict decoder knows no such |
| +// field) gets the same dispatch as before and runs the round fresh. |
| +func TestContractFixRoundFeedbackByProtocol(t *testing.T) { |
| + for _, proto := range []int{wire.Protocol1, wire.Protocol2, wire.Protocol3} { |
| + t.Run(fmt.Sprint(proto), func(t *testing.T) { |
| + r := newToolRig(t) |
| + if proto > wire.Protocol1 { |
| + gen := r.runner.Welcome.Generation |
| + r.runner.Close() |
| + h := primaryHello(gen) |
| + h.Protocol = proto |
| + p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(p.Close) |
| + if p.Welcome.Protocol != proto { |
| + t.Fatalf("welcome %+v", p.Welcome) |
| + } |
| + r.runner = p |
| + } |
| + base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| + r.pushed("f", "Fix the README typo", base, cand) |
| + c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| + r.checks("f-c", c, wire.ChecksFailed, exit(2), "", "README.md:5: teh\n") |
| + fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) |
| + raw := string(r.runner.Received[len(r.runner.Received)-1]) |
| + if !strings.Contains(fix.Brief, "Fix round 1 of 2") { |
| + t.Fatalf("brief %q", fix.Brief) |
| + } |
| + if got := strings.Contains(raw, `"feedback"`); got != (proto >= wire.Protocol3) || got && (!strings.HasPrefix(fix.Feedback, "Fix round 1 of 2") || !strings.HasSuffix(fix.Brief, fix.Feedback) || strings.Contains(fix.Feedback, "Fix the README typo")) { |
| + t.Fatalf("protocol %d: feedback sent %v: %s", proto, got, raw) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/thintest/spend_pages_test.go b/internal/thintest/spend_pages_test.go| index f4af20f0..358cb3d5 100644 |
| --- a/internal/thintest/spend_pages_test.go |
| +++ b/internal/thintest/spend_pages_test.go |
| @@ -1,10 +1,14 @@ |
| package thintest |
| import ( |
| + "database/sql" |
| + "io" |
| + "log/slog" |
| "strings" |
| "testing" |
| "amadan.net/oficina/equipo/internal/wire" |
| + "amadan.net/rastrillo/rastrillo/db" |
| ) |
| func tokens(n int64) *int64 { return &n } |
| @@ -55,3 +59,39 @@ func TestPagesTaskSpend(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Migration 0059 over an instance whose usage rows predate it: the rows |
| +// stay, their round and turns are null (unknown, never zero), and the |
| +// task page still sums them. |
| +func TestContractMigrationUsageRoundTurns(t *testing.T) { |
| + r := newToolRig(t) |
| + owner := r.inst.Cookie() |
| + base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| + id := taskID(t, r.call("u1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "teh panes"})) |
| + r.call("u2", wire.ToolStartTask, map[string]string{"task": id, "brief": "Fix it."}) |
| + _, body := r.expect(wire.FrameDispatch) |
| + d := body.(*wire.DispatchCommand) |
| + r.step("u-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) |
| + used := []wire.Usage{{Provider: "codex", Connection: "codex-a", Model: "gpt-6-astra", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: tokens(9000), CachedInputTokens: tokens(4000), OutputTokens: tokens(1000), Turns: tokens(2)}} |
| + r.step("u-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true, Usage: used}) |
| + r.runner.Close() |
| + r.inst.Stop() |
| + d0, err := db.Open(r.inst.Path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, q := range []string{"ALTER TABLE chat_usage DROP COLUMN round", "ALTER TABLE chat_usage DROP COLUMN turns", "DELETE FROM rastrillo_migrations WHERE id LIKE 'equipo/0059_%'"} { |
| + if _, err := d0.Writer().Exec(q); err != nil { |
| + t.Fatalf("%s: %v", q, err) |
| + } |
| + } |
| + d0.Close() |
| + r.inst.Restart(t) |
| + var round, turns sql.NullInt64 |
| + if err := r.inst.DB.Writer().QueryRow("SELECT round, turns FROM chat_usage WHERE step_id = ?", d.StepID).Scan(&round, &turns); err != nil || round.Valid || turns.Valid { |
| + t.Fatalf("migrated row: round %v turns %v %v", round, turns, err) |
| + } |
| + if page := r.get(owner, "/tasks/"+id, 200); !strings.Contains(page, `<p equipo-quiet data-spend>Used 10k tokens (4k cached)</p>`) { |
| + t.Fatalf("task page after the upgrade:\n%s", page) |
| + } |
| +} |
diff --git a/internal/wire/frames.go b/internal/wire/frames.go| index 2f07b266..2c53b088 100644 |
| --- a/internal/wire/frames.go |
| +++ b/internal/wire/frames.go |
| @@ -408,6 +408,7 @@ type Usage struct { |
| // Turns is how many model requests the attempt made, as the harness |
| // counted them (Claude's num_turns, Codex's usage notices); null when |
| // it did not say. A multi-model attempt carries it on one row only. |
| + // Protocol 3: never sent to an older instance (Encode). |
| Turns *int64 `json:"turns,omitempty"` |
| } |
diff --git a/internal/wire/runners.go b/internal/wire/runners.go| index f3fcea12..cd59d1d3 100644 |
| --- a/internal/wire/runners.go |
| +++ b/internal/wire/runners.go |
| @@ -13,13 +13,15 @@ import ( |
| // Runner stream protocols. 1 is the slice 4 wire: a hello without |
| // protocol. 2 adds roles, capabilities, placement, the attempt-specific |
| // start authorization, pack transfers and publication (Mac runners spec |
| -// §3.3). The instance speaks the lower of the two on each connection and |
| -// encodes every frame for it with Encode; nothing a protocol 1 runner |
| -// does not know ever reaches it. |
| +// §3.3). 3 adds a fix round's Feedback (DispatchCommand) and a usage |
| +// row's Turns. Each end speaks the lower of the two on each connection |
| +// and encodes every frame for it with Encode; nothing an older peer does |
| +// not know ever reaches it. |
| const ( |
| Protocol1 = 1 |
| Protocol2 = 2 |
| - ProtocolCurrent = Protocol2 |
| + Protocol3 = 3 |
| + ProtocolCurrent = Protocol3 |
| ) |
| // Runner roles. A person's first runner is the primary: the coordinator, |
| @@ -343,11 +345,12 @@ func Speaks(protocol int, kind string) bool { return protocol >= Protocol2 || !p |
| // ErrProtocol means a frame cannot be spoken at the connection's protocol. |
| var ErrProtocol = errors.New("wire: frame not in this protocol") |
| -// Encode returns f as the given protocol speaks it. At protocol 1 it is |
| -// exactly the slice 4 frame and field set: protocol 2 fields are dropped |
| -// and a protocol 2 frame type is refused (ErrProtocol). |
| +// Encode returns f as the given protocol speaks it. Below protocol 3 the |
| +// protocol 3 fields are dropped; at protocol 1 it is exactly the slice 4 |
| +// frame and field set: protocol 2 fields are dropped too and a protocol |
| +// 2 frame type is refused (ErrProtocol). |
| func Encode(f Frame, protocol int) (Frame, error) { |
| - if protocol >= Protocol2 { |
| + if protocol >= ProtocolCurrent { |
| return f, nil |
| } |
| if !Speaks(protocol, f.Type) { |
| @@ -357,7 +360,11 @@ func Encode(f Frame, protocol int) (Frame, error) { |
| if err != nil { |
| return Frame{}, err |
| } |
| - if !downgrade(body) { |
| + changed := downgrade3(body) |
| + if protocol < Protocol2 { |
| + changed = downgrade(body) || changed |
| + } |
| + if !changed { |
| return f, nil |
| } |
| raw, err := json.Marshal(body) |
| @@ -368,6 +375,34 @@ func Encode(f Frame, protocol int) (Frame, error) { |
| return f, nil |
| } |
| +// downgrade3 clears a body's protocol 3 fields (omitempty, so absent |
| +// once zero). It reports whether any was set. |
| +func downgrade3(body any) bool { |
| + turns := func(rows []Usage) bool { |
| + set := false |
| + for i := range rows { |
| + set = set || rows[i].Turns != nil |
| + rows[i].Turns = nil |
| + } |
| + return set |
| + } |
| + switch v := body.(type) { |
| + case *DispatchCommand: |
| + set := v.Feedback != "" |
| + v.Feedback = "" |
| + return set |
| + case *StepReport: |
| + return turns(v.Usage) |
| + case *AnswerReport: |
| + return turns(v.Usage) |
| + case *Reply: |
| + return turns(v.Usage) |
| + case *UsageReport: |
| + return turns(v.Rows) |
| + } |
| + return false |
| +} |
| + |
| // downgrade clears a body's protocol 2 fields (all omitempty, so they |
| // are absent once zero). It reports whether the body has any. |
| func downgrade(body any) bool { |
diff --git a/internal/wire/runners_test.go b/internal/wire/runners_test.go| index d95cd782..78e7145f 100644 |
| --- a/internal/wire/runners_test.go |
| +++ b/internal/wire/runners_test.go |
| @@ -157,3 +157,46 @@ func TestCovers(t *testing.T) { |
| t.Fatal("covers") |
| } |
| } |
| + |
| +// Protocol 3 adds a fix round's feedback (instance to runner) and the |
| +// turns of a usage row (runner to instance). Encoded for a protocol 2 or |
| +// 1 peer, whose strict decoder knows neither, they are gone and nothing |
| +// else changes; a frame without them is passed through as it is. |
| +func TestEncodeProtocol2DropsProtocol3Fields(t *testing.T) { |
| + n := int64(4) |
| + row := func(turns *int64) []wire.Usage { |
| + return []wire.Usage{{Provider: "claude", Connection: "c1", Model: "m", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, Turns: turns}} |
| + } |
| + for _, c := range []struct { |
| + kind string |
| + new, prev any |
| + }{ |
| + {wire.FrameDispatch, wire.DispatchCommand{TaskID: "tk", Round: 1, Findings: []wire.Finding{}, Feedback: "Fix round 1 of 2."}, wire.DispatchCommand{TaskID: "tk", Round: 1, Findings: []wire.Finding{}}}, |
| + {wire.FrameStep, wire.StepReport{TaskID: "tk", Findings: []wire.Finding{}, Usage: row(&n)}, wire.StepReport{TaskID: "tk", Findings: []wire.Finding{}, Usage: row(nil)}}, |
| + {wire.FrameAnswer, wire.AnswerReport{TaskID: "tk", Usage: row(&n)}, wire.AnswerReport{TaskID: "tk", Usage: row(nil)}}, |
| + {wire.FrameReply, wire.Reply{TurnID: "t", Usage: row(&n)}, wire.Reply{TurnID: "t", Usage: row(nil)}}, |
| + {wire.FrameUsage, wire.UsageReport{StepID: "s", Rows: row(&n)}, wire.UsageReport{StepID: "s", Rows: row(nil)}}, |
| + } { |
| + f, err := wire.NewFrame("f1", c.kind, c.new) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want, _ := wire.NewFrame("f1", c.kind, c.prev) |
| + if same, err := wire.Encode(f, wire.Protocol3); err != nil || !bytes.Equal(same.Data, f.Data) { |
| + t.Fatalf("%s changed at protocol 3: %v", c.kind, err) |
| + } |
| + for _, p := range []int{wire.Protocol2, wire.Protocol1} { |
| + old, err := wire.Encode(f, p) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !bytes.Equal(old.Data, want.Data) { |
| + t.Fatalf("%s at protocol %d:\n got %s\nwant %s", c.kind, p, old.Data, want.Data) |
| + } |
| + } |
| + // Without the new fields, a protocol 2 frame is untouched. |
| + if same, err := wire.Encode(want, wire.Protocol2); err != nil || !bytes.Equal(same.Data, want.Data) { |
| + t.Fatalf("%s without protocol 3 fields changed at protocol 2: %v", c.kind, err) |
| + } |
| + } |
| +} |
diff --git a/internal/wire/tasks.go b/internal/wire/tasks.go| index 56ad3b58..abfe3be3 100644 |
| --- a/internal/wire/tasks.go |
| +++ b/internal/wire/tasks.go |
| @@ -610,6 +610,7 @@ type DispatchCommand struct { |
| // about the commit this round starts from, the fix-round part of |
| // Brief. A runner holding the session that made that commit sends it |
| // as that session's next message instead of starting afresh. |
| + // Protocol 3: an older runner never gets it, and runs the round fresh. |
| Feedback string `json:"feedback,omitempty"` |
| // Rebase (Engineering): put the change of CandidateSHA, made on |
| // FromBase, on top of the default branch's tip (BaseSHA is empty: the |