runner: hosted checks pack carries the base and the candidate
Task 15. TransferHeader.Valid accepts hosted_pack (inbound, base then candidate, full SHAs); deps stays refused. inboundHeader selects by attempt and kind. A hosted worker (Worker.Hosted) indexes the pack into an empty store of the attempt's own and needs both commits; a missing one ends the step cannot_run, pack_incomplete. The instance opens the hosted pack's transfer under its own ID (openHostedPack, for Task 21's placement). Budget records raised for wire, thin and chat.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 files changed,
+431
−27
.rastrillo/budgets.txt+3 −3docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+20 −0internal/equipo/chat/hosted_pack_test.go+101 −0internal/equipo/chat/tasks.go+12 −0internal/runner/thin/hosted_pack_test.go+234 −0internal/runner/thin/relay.go+6 −4internal/runner/thin/worker.go+3 −0internal/runner/thin/worker_checks.go+32 −13internal/runner/thin/worker_test.go+4 −3internal/wire/hosted.go+4 −0internal/wire/mac_engineering.go+8 −2internal/wire/runners.go+4 −2
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 4da5293c..5bd4c329 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -15,7 +15,7 @@ time internal/equipo/team 60s Plan 3 loaded CI measured 45.985s; niced serial re |
| time internal/equipotest 300s Final fix package rerun measured 187.035s (review 172.4s); 300s gives 60 percent headroom for HTTP, sandbox, authority and final regression coverage. |
| size internal/equipo 24735 - Preview, retry, merges, kept work, check-db, notifications, places, GitHub routes, installer, abilities, lookups, Mac waits, slice F codes, D1, probe details, Design's copy, /api/team; measured 24735. |
| -size internal/wire 9848 - Protocols 31 to 35 (facts, diffs, evidence, CI units, files, places), abilities, 36 lookups, 37 Mac Engineering, 38 Design, unproven and probe detail codes, 39 hosted runners (hosted.go: placement, hosted_pack and deps transfer limits, enrolment types); measured 9848. |
| +size internal/wire 9860 - Protocols 31 to 35 (facts, diffs, evidence, CI units, files, places), abilities, 36 lookups, 37 Mac Engineering, 38 Design, unproven and probe detail codes, 39 hosted runners (hosted.go: placement, hosted_pack and deps transfer limits, enrolment types); measured 9848. Task 15 opened TransferHeader.Valid to hosted_pack (inbound, base then candidate) and added ReasonPackIncomplete; 9860. |
| size internal/equipotest - 12693 GitHub connect value search, lifecycle and sweep-log contracts; Task 21 migration fixtures, Rastrillo asset contract, renderer archive link; measured 0/12693 source/test lines. |
| time internal/runner/checks 90s CI worker measured 44.553s (44.6s rounded); |
| time internal/runner 330s Plan 3 CI repair measured 292.120s for the full package (controller CI 472.184s). |
| @@ -39,7 +39,7 @@ time internal/runner/provider 15s Plan 3 CI repair measured 10.346s for the full |
| perf GET /releases/* skip Verified runner release file downloads, not browser GET screens. |
| perf GET /install.sh skip Mac runners: the one-line bootstrap a terminal pipes to sh, not a browser screen. |
| time internal/runner/containment 15s Plan 3 loaded CI measured 10.817s; |
| -size internal/runner/thin 35839 35453 Protocols 31-35, merge credential, publication guards, kept work, scans, 36 lookups; Mac pin, Engineering, asks, F probes, D1, P2, held reports; 38 Design step, renderer; hosted slice 2 Task 11 moved the base-gate checks to checkrun (source down) and pinned the primary and Mac behaviour with tests (test up), and its round 1 fixes added the worker step-engine wiring test; Task 12 added Link.TokenSource and Link.HTTPClient (a hosted runner's bearer is read at each dial and its dial goes through the proxy) and their two tests; 35831/35453. Task 14 review round 1 added Link.ReconnectWait (the wait between connections, a fake clock's in the hosted renewal tests, which drive the real Link through certificate rotation and bearer renewal); 35839/35453. |
| +size internal/runner/thin 35863 35688 Protocols 31-35, merge credential, publication guards, kept work, scans, 36 lookups; Mac pin, Engineering, asks, F probes, D1, P2, held reports; 38 Design step, renderer; hosted slice 2 Task 11 moved the base-gate checks to checkrun (source down) and pinned the primary and Mac behaviour with tests (test up), and its round 1 fixes added the worker step-engine wiring test; Task 12 added Link.TokenSource and Link.HTTPClient (a hosted runner's bearer is read at each dial and its dial goes through the proxy) and their two tests; 35831/35453. Task 14 review round 1 added Link.ReconnectWait (the wait between connections, a fake clock's in the hosted renewal tests, which drive the real Link through certificate rotation and bearer renewal); 35839/35453. Task 15: the hosted pack (inboundHeader selects by attempt and kind, Worker.Hosted indexes the base and the candidate into an empty store, pack_incomplete) and hosted_pack_test.go; 35863/35688. |
| time internal/thintest 240s Thin contract and page tests, the Assistant's and the models' included, share one harness. Measured 202s on a shared 16 CPU box (2026-11-13); CI runs two lanes. |
| time internal/runner/mac 45s Real processes killed and proven gone: groups, sessions, the runner-user sweep, turns, restarts; probes end to end. Measured 29.3s locally. |
| time internal/runner/thin 45s Thin v1 slice 4 review: real git repositories for the merge and squash checks, a stalled fetch, and a 16 MiB candidate streamed through the review diff and the checks pack (about 5s). |
| @@ -58,7 +58,7 @@ perf GET /api/v1/assistant/snapshot skip Assistant person API: bearer-only JSON |
| perf GET /api/v1/assistant/messages skip Assistant person API: bearer-only JSON for Base's chat, not a browser screen. |
| perf GET /api/v1/assistant/events skip Assistant event stream (Server-Sent Events), bearer-only and long-lived; not a screen. |
| perf GET /api/v1/assistant/abilities skip Assistant person API: bearer-only JSON for Base's Settings line, not a browser screen. |
| -size internal/equipo/chat 45093 18782 File facts/diffs, previews, merges, unfinished work, CI, receipts, Assistant files, places, abilities, lookups, go-aheads, Mac work, D1, Design specialists, grants, captures; 45093/18782. |
| +size internal/equipo/chat 45105 18883 File facts/diffs, previews, merges, unfinished work, CI, receipts, Assistant files, places, abilities, lookups, go-aheads, Mac work, D1, Design specialists, grants, captures; 45093/18782. Hosted slice 2 Task 15: openHostedPack (the instance's send_pack for a hosted pack) and its test; 45105/18883. |
| perf GET /api/v1/questions/{id} skip Thin v1 person API: bearer-only JSON for devices; the browser reads questions on /tasks/{id}. |
| perf GET /api/v1/steps/{id}/log skip Thin v1 person API: bearer-only JSON for devices; the browser shows the log tail on /tasks/{id}. |
| perf GET /api/v1/connections/preference skip Connections slice 1 person API: bearer-only JSON for devices; the browser shows the preference on /conversation/connections. |
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md| index 8a38b24e..768eb076 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -1131,3 +1131,23 @@ Mutation checks (exact unique string replaced by a script, `-count=1 -timeout 12 |
| 157. No retry (a failure fails at once). `-run TestRenewalRetries`: FAIL `renew_test.go:712: certificate renewals answered [502], want ...`, `renew_test.go:725: bearer renewals asked at [30m0s], want [30m0s 30m5s 30m15s]`. Restored. |
| 158. Renew past the deadline (the `renewAt` deadline check → `if false`). `-run TestNoRenewalPastDeadline`: FAIL `renew_test.go:749: 2 certificate and 1 bearer renewals, want 1 and 0`. Restored. |
| 159. Keep working after the final failure (work contexts not cancelled). `-run TestRenewalFailureStopsWork`: FAIL `renew_test.go:640: the work context still runs after the failed renewal` (both). Restored. |
| + |
| +## Task 15 |
| + |
| +The hosted pack carries the base and the candidate. `TransferHeader.Valid` accepts `hosted_pack` only inbound, with a base and a candidate that differ, no old base, full SHAs (so `Commits` is `[BaseSHA, CandidateSHA]`); `deps` stays refused until Task 23. The primary's `packCommits` needed no new code: it packs `Commits()` (trees and blobs, `--no-walk`, not thin). `inboundHeader` takes the kind and selects by attempt and kind (`IFNULL(json_extract(header, '$.kind'), '')`). `Worker.Hosted` (Task 17 sets it) makes the checks path await a `hosted_pack`, refuse a dispatch whose header is not valid (`placement_refused`), index into a fresh store under the attempt's step directory (`objects.git`, removed with it, never the shared store), and require every commit of the header; a missing one (`errCommitMissing`) ends the step done, `cannot_run`, `pack_incomplete` (`wire.ReasonPackIncomplete`). A personal worker is unchanged (`candidate_unavailable`). The instance's `openHostedPack(k, s, primary, base)` (chat/tasks.go) validates the header and opens the transfer (an instance-made `tr_` ID, one per transfer) with its `send_pack`; Task 21's placement calls it. `SendPackCommand` gains no field: the header's kind is the protocol-gated field (Task 1's `downgradeHostedRunners`). |
| + |
| +`pack_incomplete` is not yet in `wire.StepReasons`: `TestStepReasonsHaveWords` would need its words, which Task 25 writes. Until then an instance records it as `unknown`; no hosted step runs before Task 21. |
| + |
| +Red (the tests against API stubs: `Hosted` unused, `inboundHeader`'s kind ignored, `Valid` unchanged, `openHostedPack` returning `ErrConflict`): `go test ./internal/runner/thin ./internal/equipo/chat -run 'TestHostedPack|TestEmptyStore|TestMissingBase|TestInboundSelects' -count=1` FAIL `hosted_pack_test.go:76: hosted header valid false, commits [...], want [base candidate]`; `hosted_pack_test.go:148: hosted checks {... State:failed Reason:candidate_unavailable ...}`; `hosted_pack_test.go:182: no base: {... Reason:candidate_unavailable ...}` (and no candidate); `hosted_pack_test.go:210: the deps chunk answered for the hosted pack: true <nil>`; chat `hosted_pack_test.go:64: chat: idempotency conflict`. |
| + |
| +Green: the brief's command: ok (thin 4.4 s, chat 0.15 s). `go test ./internal/wire -count=1`: ok. Targeted thin `-run 'TestWorker|TestPack|TestRelay|TestPublish|TestPrepareBase|TestHosted|TestEmptyStore|TestMissingBase|TestInbound|TestCheck'`: ok. Chat `-run 'TestTransfer|TestHostedPack|TestChecksNeedAMac|TestUnsandboxedChoice|TestAccessWorker'`: ok. Thintest `-run 'TestContractTransfer|TestOutboundPackOversize|TestContractPublication|TestContractMacEngineeringPlacedOnWorker|TestContractWorkerEngineeringReports'`: ok. `make budget build vet`: exit 0 (records raised: wire 9860, thin 35863/35688, chat 45105/18883, each with its reason). staticcheck on the three packages, gofmt, `git diff --check`: clean. |
| + |
| +Mutation checks (exact unique string replaced by a script, `-count=1 -timeout 120s`, restored from a copy and checked with `cmp`); each compiled and failed on an assertion. |
| + |
| +160. Select the inbound transfer by attempt alone (the kind condition and argument removed from `inboundHeader`). `-run TestInboundSelectsByKind`: FAIL `hosted_pack_test.go:219: the deps chunk answered for the hosted pack: true <nil>`. Restored. |
| +161. The hosted worker indexes into the shared store (`store` not replaced). `-run 'TestEmptyStoreGetsBothCommits|TestMissingBaseRefused'`: FAIL `hosted_pack_test.go:151: the attempt's store held [], want the base and the candidate`; `hosted_pack_test.go:182: no base: {... State:done ... Checks:passed ...}` (and no candidate: the earlier attempt's objects in the shared store passed). Restored. |
| +162. Require only the candidate (`w.index(..., r.CandidateSHA)`). `-run TestMissingBaseRefused`: FAIL `hosted_pack_test.go:182: no base: {... Checks:passed ...} gate [[... ./.amadan/ci]]`. Restored. |
| +163. A missing commit ends `candidate_unavailable` (the `pack_incomplete` branch → `if false`). Same test: FAIL `hosted_pack_test.go:182: no base: {... State:failed Reason:candidate_unavailable ...}` (and no candidate). Restored. |
| +164. No header guard on the worker (`if !want.Valid()` → `if false`). Same test: FAIL `hosted_pack_test.go:192: hosted checks without a base: {... Checks:passed ...}`. Restored. |
| +165. `Valid` accepts a base equal to the candidate. `-run TestHostedPackHasBaseAndCandidate`: FAIL `hosted_pack_test.go:110: the base is the candidate: fork/exec /nonexistent/git: no such file or directory, want refused before git runs`. Restored. |
| +166. The instance opens a hosted pack without checking the header (`if !h.Valid()` → `if false` in `openHostedPack`). `-run TestHostedPackSendPackFromInstance`: FAIL `hosted_pack_test.go:94: hosted pack on base "": <nil>, want ErrInvalid`. Restored. |
diff --git a/internal/equipo/chat/hosted_pack_test.go b/internal/equipo/chat/hosted_pack_test.go| new file mode 100644 |
| index 00000000..60438102 |
| --- /dev/null |
| +++ b/internal/equipo/chat/hosted_pack_test.go |
| @@ -0,0 +1,101 @@ |
| +package chat |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "io" |
| + "log/slog" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "gorm.io/gorm" |
| +) |
| + |
| +// Hosted slice 2, Task 15: the instance asks the primary for a hosted |
| +// checks step's pack, the base and the candidate, under a transfer ID it |
| +// makes; the send_pack never reaches a primary below |
| +// ProtocolHostedRunners. Placement (Task 21) calls openHostedPack. |
| +func TestHostedPackSendPackFromInstance(t *testing.T) { |
| + src, err := hkTemplatePath() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, err := os.ReadFile(src) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + path := filepath.Join(t.TempDir(), "hosted.db") |
| + if err := os.WriteFile(path, raw, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { _ = d.Close() }) |
| + s := NewStore(d, Authority{Copy: func(key string) string { return key }, PrimaryRunner: func(*gorm.DB, string) (string, error) { return "R1", nil }}, nil) |
| + const person = "owner@example.com" |
| + now := time.Now().UTC() |
| + base, candidate := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| + k := taskRecord{ID: "tk1", ProjectID: "p1", Creator: person, Stage: wire.StageInProgress, Revision: 1, CreatedAt: now, UpdatedAt: now} |
| + st := stepRecord{ID: "s1", TaskID: "tk1", Idx: 1, Specialist: wire.SpecialistChecks, Person: person, State: wire.StepQueued, Attempt: 1, RunnerID: "R2", CandidateSHA: candidate} |
| + for _, row := range []any{&projectRecord{ID: "p1", Repo: "amadan.net/oficina/sample"}, &k, &st} { |
| + if err := d.G.Create(row).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var pack, deps transferRecord |
| + if err := s.tx(context.Background(), func(tx *txn) error { |
| + var err error |
| + if pack, err = tx.openHostedPack(k, st, "R1", base); err != nil { |
| + return err |
| + } |
| + // Task 23's bundle, for the same attempt, under its own ID. |
| + deps, err = tx.openTransfer(k, st, "R1", "R2", wire.TransferHeader{Direction: wire.TransferInbound, Kind: wire.TransferKindDeps, BaseSHA: base, CandidateSHA: candidate}) |
| + return err |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !strings.HasPrefix(pack.ID, "tr_") || pack.ID == deps.ID || pack.FromRunner != "R1" || pack.ToRunner != "R2" { |
| + t.Fatalf("hosted pack transfer %+v beside deps %s", pack, deps.ID) |
| + } |
| + want := wire.TransferHeader{ID: pack.ID, Direction: wire.TransferInbound, Repo: "amadan.net/oficina/sample", TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: base, CandidateSHA: candidate, Kind: wire.TransferKindHostedPack} |
| + if got := transferHeader(pack); got != want || !got.Valid() || !slices.Equal(got.Commits(), []string{base, candidate}) { |
| + t.Fatalf("hosted pack header %+v, want %+v", got, want) |
| + } |
| + var cmds []commandRecord |
| + if err := d.G.Where("kind = ? AND runner_id = ?", wire.FrameSendPack, "R1").Order("seq").Find(&cmds).Error; err != nil || len(cmds) != 2 { |
| + t.Fatalf("send_pack commands to the primary %d %v", len(cmds), err) |
| + } |
| + var sent wire.SendPackCommand |
| + if err := json.Unmarshal([]byte(cmds[0].Body), &sent); err != nil || sent.Transfer != want { |
| + t.Fatalf("send_pack %+v %v", sent.Transfer, err) |
| + } |
| + frame, err := wire.NewFrame("sp", wire.FrameSendPack, sent) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := wire.Encode(frame, wire.ProtocolHostedRunners-1); !errors.Is(err, wire.ErrProtocol) { |
| + t.Fatalf("a hosted send_pack below ProtocolHostedRunners: %v", err) |
| + } |
| + // No base, or a base that is the candidate: no transfer, no command. |
| + for _, b := range []string{"", candidate} { |
| + if err := s.tx(context.Background(), func(tx *txn) error { |
| + _, err := tx.openHostedPack(k, st, "R1", b) |
| + return err |
| + }); !errors.Is(err, ErrInvalid) { |
| + t.Fatalf("hosted pack on base %q: %v, want ErrInvalid", b, err) |
| + } |
| + } |
| + var n int64 |
| + if err := d.G.Model(&transferRecord{}).Count(&n).Error; err != nil || n != 2 { |
| + t.Fatalf("transfers %d %v, want 2", n, err) |
| + } |
| +} |
diff --git a/internal/equipo/chat/tasks.go b/internal/equipo/chat/tasks.go| index 0d89f2fa..02671396 100644 |
| --- a/internal/equipo/chat/tasks.go |
| +++ b/internal/equipo/chat/tasks.go |
| @@ -1133,6 +1133,18 @@ func (t *txn) startTask(r turnRecord, id, brief, maker string) (wire.ToolResult, |
| return wire.ToolResult{Code: "started", Text: string(raw)}, nil |
| } |
| +// openHostedPack asks the primary for a hosted checks attempt's pack, the |
| +// base and the candidate (wire.TransferKindHostedPack), under a transfer |
| +// ID of its own. Placement on a hosted VM needs ProtocolHostedRunners on |
| +// both ends; Encode refuses the send_pack below it. |
| +func (t *txn) openHostedPack(k taskRecord, s stepRecord, primary, base string) (transferRecord, error) { |
| + h := wire.TransferHeader{Direction: wire.TransferInbound, Kind: wire.TransferKindHostedPack, BaseSHA: base, CandidateSHA: s.CandidateSHA} |
| + if !h.Valid() { |
| + return transferRecord{}, fmt.Errorf("%w: hosted pack for %s", ErrInvalid, s.ID) |
| + } |
| + return t.openTransfer(k, s, primary, s.RunnerID, h) |
| +} |
| + |
| // dispatchStep queues the dispatch command for the step's attempt on the |
| // runner it is placed on, with the task's findings. The caller saves s. |
| func (t *txn) dispatchStep(k *taskRecord, s *stepRecord) error { |
diff --git a/internal/runner/thin/hosted_pack_test.go b/internal/runner/thin/hosted_pack_test.go| new file mode 100644 |
| index 00000000..1b9e89de |
| --- /dev/null |
| +++ b/internal/runner/thin/hosted_pack_test.go |
| @@ -0,0 +1,234 @@ |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "encoding/base64" |
| + "encoding/json" |
| + "errors" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "slices" |
| + "sort" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + rungate "amadan.net/oficina/equipo/internal/runner/gate" |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Hosted slice 2, Task 15: a hosted checks step's pack carries the base |
| +// and the candidate (wire.TransferKindHostedPack). The primary packs both |
| +// commits' trees and blobs and no history; the hosted worker indexes it |
| +// into an empty store and needs both commits before checkout. Each |
| +// transfer of an attempt (the pack, and Task 23's deps bundle) has its |
| +// own instance-made ID, and the worker selects by attempt and kind. |
| + |
| +// hostedHeader is the hosted pack's header for the rig's attempt. |
| +func (x *workerRig) hostedHeader(id string) wire.TransferHeader { |
| + h := x.header() |
| + h.ID, h.Kind = id, wire.TransferKindHostedPack |
| + return h |
| +} |
| + |
| +// outboxPack reassembles the blobs the primary queued for transfer h, |
| +// each under exactly h. |
| +func outboxPack(t *testing.T, s *State, h wire.TransferHeader) []byte { |
| + t.Helper() |
| + frames, _, err := s.Unacked(context.Background(), 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var pack []byte |
| + for _, f := range frames { |
| + if f.Type != wire.FrameBlob { |
| + continue |
| + } |
| + var b wire.Blob |
| + if err := json.Unmarshal(f.Data, &b); err != nil { |
| + t.Fatal(err) |
| + } |
| + if b.Transfer != h { |
| + t.Fatalf("a blob under %+v, want %+v", b.Transfer, h) |
| + } |
| + data, err := base64.StdEncoding.DecodeString(b.Data) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pack = append(pack, data...) |
| + } |
| + if len(pack) == 0 { |
| + t.Fatal("the primary queued no blobs") |
| + } |
| + return pack |
| +} |
| + |
| +// The instance's send_pack for a hosted pack reaches the primary, which |
| +// uploads, under the instance's transfer ID and header, a pack of exactly |
| +// the base and the candidate with their trees: no parent of either. A |
| +// header a hosted pack never has is refused before git runs. |
| +func TestHostedPackHasBaseAndCandidate(t *testing.T) { |
| + r, c0, c1, c2 := packRig(t) |
| + ctx := context.Background() |
| + h := wire.TransferHeader{ID: "tr_hosted", Direction: wire.TransferInbound, Kind: wire.TransferKindHostedPack, Repo: sampleRepo, TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: c1, CandidateSHA: c2} |
| + if got := h.Commits(); !h.Valid() || !slices.Equal(got, []string{c1, c2}) { |
| + t.Fatalf("hosted header valid %v, commits %v, want [base candidate]", h.Valid(), got) |
| + } |
| + if err := r.e.O.State.AcceptUpload(ctx, "sp-1", wire.SendPackCommand{Transfer: h, Have: []string{}}); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.relay.Once(ctx) |
| + inv := packInventory(t, outboxPack(t, r.e.O.State, h)) |
| + want := []string{c1, c2} |
| + sort.Strings(want) |
| + if !slices.Equal(inv["commit"], want) || slices.Contains(inv["commit"], c0) { |
| + t.Fatalf("commits in the hosted pack %v, want exactly the base and the candidate %v", inv["commit"], want) |
| + } |
| + var got []string |
| + for _, kind := range []string{"commit", "tree", "blob", "tag"} { |
| + got = append(got, inv[kind]...) |
| + } |
| + sort.Strings(got) |
| + if all := objectsOf(t, r.e.Clones.Path(sampleRepo), c1, c2); !slices.Equal(got, all) { |
| + t.Fatalf("objects in the hosted pack %v, want both trees exactly %v", got, all) |
| + } |
| + bad := map[string]func(*wire.TransferHeader){ |
| + "no base": func(h *wire.TransferHeader) { h.BaseSHA = "" }, |
| + "no candidate": func(h *wire.TransferHeader) { h.CandidateSHA = "" }, |
| + "the base is the candidate": func(h *wire.TransferHeader) { h.BaseSHA = c2 }, |
| + "an old base": func(h *wire.TransferHeader) { h.OldBaseSHA = c0 }, |
| + "outbound": func(h *wire.TransferHeader) { h.Direction = wire.TransferOutbound }, |
| + "a short base": func(h *wire.TransferHeader) { h.BaseSHA = c1[:12] }, |
| + "a deps transfer": func(h *wire.TransferHeader) { h.Kind = wire.TransferKindDeps }, |
| + } |
| + r.e.O.Runner.GitPath, r.e.Clones.Git = "/nonexistent/git", "/nonexistent/git" |
| + for name, change := range bad { |
| + b := h |
| + change(&b) |
| + if _, err := r.e.packCommits(ctx, b); !errors.Is(err, errPackRefused) { |
| + t.Errorf("%s: %v, want refused before git runs", name, err) |
| + } |
| + } |
| +} |
| + |
| +// storeGate is a stub gate that, while the gate runs, reads the hosted |
| +// attempt's object store beside the tree: which commits it holds and how |
| +// many objects, loose and packed. |
| +type storeGate struct { |
| + stubGate |
| + commits []string |
| + held []string |
| + count string |
| +} |
| + |
| +func (g *storeGate) Run(ctx context.Context, marker, dir string, argv []string, limit time.Duration, record func(string) error) rungate.Result { |
| + store := filepath.Join(filepath.Dir(dir), "objects.git") |
| + for _, c := range g.commits { |
| + if out, err := exec.Command("git", "--git-dir="+store, "cat-file", "-t", c).Output(); err == nil && strings.TrimSpace(string(out)) == "commit" { |
| + g.held = append(g.held, c) |
| + } |
| + } |
| + out, _ := exec.Command("git", "--git-dir="+store, "count-objects", "-v").Output() |
| + g.count = "\n" + string(out) |
| + return g.stubGate.Run(ctx, marker, dir, argv, limit, record) |
| +} |
| + |
| +// A hosted worker indexes the pack into an empty store of the attempt's |
| +// own (never the shared store a personal worker keeps), finds both |
| +// commits there, and checks the candidate. |
| +func TestEmptyStoreGetsBothCommits(t *testing.T) { |
| + x := newWorkerRig(t) |
| + x.w.Hosted = true |
| + g := &storeGate{commits: []string{x.base, x.commit}} |
| + x.w.Gate = g |
| + x.chunk(t, "p1", x.hostedHeader("tr_pack"), onePack(t, x.seed, x.base, x.commit), 0, 1, "") |
| + rep := x.run() |
| + if rep.State != wire.StepDone || rep.Checks != wire.ChecksPassed || !strings.Contains(rep.Output, "let back = true") { |
| + t.Fatalf("hosted checks %+v", rep) |
| + } |
| + if !slices.Equal(g.held, []string{x.base, x.commit}) { |
| + t.Fatalf("the attempt's store held %v, want the base and the candidate", g.held) |
| + } |
| + objects := len(objectsOf(t, filepath.Join(x.seed, ".git"), x.base, x.commit)) |
| + if !strings.Contains(g.count, "\ncount: 0\n") || !strings.Contains(g.count, "\nin-pack: "+itoa(int64(objects))+"\n") || !strings.Contains(g.count, "\npacks: 1\n") { |
| + t.Fatalf("the attempt's store is not just the pack's %d objects:\n%s", objects, g.count) |
| + } |
| + if _, err := os.Stat(x.w.objects(x.r.Repo)); !os.IsNotExist(err) { |
| + t.Fatalf("the hosted worker used the shared store: %v", err) |
| + } |
| +} |
| + |
| +// A hosted pack without the base, or without the candidate, ends the |
| +// step cannot_run, pack_incomplete, before the gate runs, even when an |
| +// earlier attempt left the missing commit in the worker's shared store. |
| +// On the primary, a base the clone does not have is refused. |
| +func TestMissingBaseRefused(t *testing.T) { |
| + for name, only := range map[string]func(*workerRig) []byte{ |
| + "no base": func(x *workerRig) []byte { return x.pack }, |
| + "no candidate": func(x *workerRig) []byte { return x.other }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + x := newWorkerRig(t) |
| + x.w.Hosted = true |
| + ctx := context.Background() |
| + both := onePack(t, x.seed, x.base, x.commit) |
| + if err := x.w.index(ctx, x.w.objects(x.r.Repo), both, x.base, x.commit); err != nil { |
| + t.Fatal(err) |
| + } |
| + x.chunk(t, "p1", x.hostedHeader("tr_pack"), only(x), 0, 1, "") |
| + rep := x.run() |
| + if rep.State != wire.StepDone || rep.Checks != wire.ChecksCannotRun || rep.Reason != wire.ReasonPackIncomplete || !rep.Termination || len(x.gate.ran) != 0 { |
| + t.Fatalf("%s: %+v gate %v", name, rep, x.gate.ran) |
| + } |
| + }) |
| + } |
| + t.Run("a dispatch without a base", func(t *testing.T) { |
| + x := newWorkerRig(t) |
| + x.w.Hosted = true |
| + x.r.BaseSHA = "" |
| + x.chunk(t, "p1", x.hostedHeader("tr_pack"), x.pack, 0, 1, "") |
| + if rep := x.run(); rep.State != wire.StepFailed || rep.Reason != "placement_refused" || len(x.gate.ran) != 0 { |
| + t.Fatalf("hosted checks without a base: %+v gate %v", rep, x.gate.ran) |
| + } |
| + }) |
| + t.Run("primary", func(t *testing.T) { |
| + r, _, _, c2 := packRig(t) |
| + h := wire.TransferHeader{ID: "tr_hosted", Direction: wire.TransferInbound, Kind: wire.TransferKindHostedPack, Repo: sampleRepo, TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: strings.Repeat("e", 40), CandidateSHA: c2} |
| + if _, err := r.e.packCommits(context.Background(), h); !errors.Is(err, errPackRefused) { |
| + t.Fatalf("a base the clone does not have: %v", err) |
| + } |
| + }) |
| +} |
| + |
| +// A deps transfer and a hosted pack for one attempt are never confused: |
| +// each kind finds its own transfer, and the worker checks from the pack |
| +// whichever arrived first. |
| +func TestInboundSelectsByKind(t *testing.T) { |
| + x := newWorkerRig(t) |
| + x.w.Hosted = true |
| + ctx := context.Background() |
| + deps := x.hostedHeader("tr_deps") |
| + deps.Kind = wire.TransferKindDeps |
| + pack := x.hostedHeader("tr_pack") |
| + if _, ok, err := x.w.State.inboundHeader(ctx, x.r, wire.TransferKindHostedPack); ok || err != nil { |
| + t.Fatalf("a header before any chunk: %v %v", ok, err) |
| + } |
| + x.chunk(t, "d1", deps, []byte("a dependency bundle"), 0, 1, "") |
| + if _, ok, err := x.w.State.inboundHeader(ctx, x.r, wire.TransferKindHostedPack); ok || err != nil { |
| + t.Fatalf("the deps chunk answered for the hosted pack: %v %v", ok, err) |
| + } |
| + x.chunk(t, "p1", pack, onePack(t, x.seed, x.base, x.commit), 0, 1, "") |
| + for kind, want := range map[string]wire.TransferHeader{wire.TransferKindHostedPack: pack, wire.TransferKindDeps: deps} { |
| + h, ok, err := x.w.State.inboundHeader(ctx, x.r, kind) |
| + if err != nil || !ok || h != want { |
| + t.Fatalf("%s: %+v %v %v, want %+v", kind, h, ok, err, want) |
| + } |
| + } |
| + if _, ok, err := x.w.State.inboundHeader(ctx, x.r, ""); ok || err != nil { |
| + t.Fatalf("a checks transfer found among hosted ones: %v %v", ok, err) |
| + } |
| + if rep := x.run(); rep.State != wire.StepDone || rep.Checks != wire.ChecksPassed { |
| + t.Fatalf("hosted checks beside a deps transfer: %+v", rep) |
| + } |
| +} |
diff --git a/internal/runner/thin/relay.go b/internal/runner/thin/relay.go| index 84dc8b9d..e36ec882 100644 |
| --- a/internal/runner/thin/relay.go |
| +++ b/internal/runner/thin/relay.go |
| @@ -195,10 +195,11 @@ func (s *State) pack(ctx context.Context, want wire.TransferHeader) ([]byte, err |
| } |
| // inboundHeader is the transfer header a worker's step expects, from the |
| -// chunks that name its attempt. |
| -func (s *State) inboundHeader(ctx context.Context, r stepRow) (wire.TransferHeader, bool, error) { |
| +// chunks that name its attempt and kind: a hosted attempt's pack and its |
| +// deps bundle are two transfers, never confused. |
| +func (s *State) inboundHeader(ctx context.Context, r stepRow, kind string) (wire.TransferHeader, bool, error) { |
| var header string |
| - err := s.db.QueryRowContext(ctx, "SELECT header FROM chunks WHERE step_id=? AND attempt=? AND direction=? ORDER BY idx LIMIT 1", r.StepID, r.Attempt, wire.TransferInbound).Scan(&header) |
| + err := s.db.QueryRowContext(ctx, "SELECT header FROM chunks WHERE step_id=? AND attempt=? AND direction=? AND IFNULL(json_extract(header, '$.kind'), '')=? ORDER BY idx LIMIT 1", r.StepID, r.Attempt, wire.TransferInbound, kind).Scan(&header) |
| if errors.Is(err, sql.ErrNoRows) { |
| return wire.TransferHeader{}, false, nil |
| } |
| @@ -494,7 +495,8 @@ func (e *Engineer) prepareBase(ctx context.Context, c wire.PrepareBaseCommand) ( |
| // packCommits is the pack of exactly the commits the header names |
| // (TransferHeader.Commits), each with its tree and blobs, and no history: |
| // for a checks transfer the candidate alone, for an Engineering transfer |
| -// the base and, for a rebase round, the old base and the candidate. A |
| +// the base and, for a rebase round, the old base and the candidate, for |
| +// a hosted pack the base and the candidate (a fresh VM has neither). A |
| // header that is not valid, a repository that is not a hub repository, |
| // or a name that is not a full SHA is refused before git runs; a commit |
| // the fresh clone does not have is refused before the pack is built. |
diff --git a/internal/runner/thin/worker.go b/internal/runner/thin/worker.go| index 42317189..c7f402bc 100644 |
| --- a/internal/runner/thin/worker.go |
| +++ b/internal/runner/thin/worker.go |
| @@ -60,6 +60,9 @@ type Worker struct { |
| // Eng is what this worker needs to take Engineering (nil: it takes |
| // none; worker_engineering.go). |
| Eng *WorkerEngineering |
| + // Hosted: a hosted VM's worker (slice 2), whose checks take a |
| + // hosted_pack of the base and the candidate into an empty store. |
| + Hosted bool |
| // mine, in tests, stands in for Engine.Conns.Mine (the person's |
| // connections, read from the bindings file). |
| mine func(person string) []runner.AccountBinding |
diff --git a/internal/runner/thin/worker_checks.go b/internal/runner/thin/worker_checks.go| index 1eef4aad..a0f942fa 100644 |
| --- a/internal/runner/thin/worker_checks.go |
| +++ b/internal/runner/thin/worker_checks.go |
| @@ -58,7 +58,17 @@ func (w *Worker) checks(ctx context.Context, r stepRow, progress func(wire.StepR |
| return end(wire.StepCancelled, leaseReason(err)) |
| } |
| defer func() { _ = w.State.dropStepChunks(context.WithoutCancel(ctx), r.StepID, r.Attempt) }() |
| - pack, err := w.awaitPack(ctx, r, wire.TransferHeader{Direction: wire.TransferInbound, Repo: r.Repo, TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, BaseSHA: r.BaseSHA, CandidateSHA: r.CandidateSHA}) |
| + want := wire.TransferHeader{Direction: wire.TransferInbound, Repo: r.Repo, TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, BaseSHA: r.BaseSHA, CandidateSHA: r.CandidateSHA} |
| + store, dir := w.objects(r.Repo), filepath.Join(w.stepsDir(), stepKey(r)) |
| + defer os.RemoveAll(dir) |
| + if w.Hosted { |
| + // The base and the candidate, into an empty store of its own. |
| + want.Kind, store = wire.TransferKindHostedPack, filepath.Join(dir, "objects.git") |
| + if !want.Valid() { |
| + return end(wire.StepFailed, "placement_refused") |
| + } |
| + } |
| + pack, err := w.awaitPack(ctx, r, want) |
| switch { |
| case ctx.Err() != nil: |
| return end(wire.StepCancelled, "cancelled") |
| @@ -66,14 +76,15 @@ func (w *Worker) checks(ctx context.Context, r stepRow, progress func(wire.StepR |
| w.Engine.log("worker pack", err) |
| return end(wire.StepFailed, "candidate_unavailable") |
| } |
| - store := w.objects(r.Repo) |
| - if err := w.index(ctx, store, pack, r.CandidateSHA); err != nil { |
| + if err := w.index(ctx, store, pack, want.Commits()...); err != nil { |
| w.Engine.log("worker index", err) |
| + if w.Hosted && errors.Is(err, errCommitMissing) { |
| + rep.Checks = wire.ChecksCannotRun |
| + return end(wire.StepDone, wire.ReasonPackIncomplete) |
| + } |
| return end(wire.StepFailed, "candidate_unavailable") |
| } |
| rep.Have = []string{r.CandidateSHA} |
| - dir := filepath.Join(w.stepsDir(), stepKey(r)) |
| - defer os.RemoveAll(dir) |
| tree := workspace.TaskTree{Dir: filepath.Join(dir, "tree"), GitDir: store, Git: w.Engine.O.Runner.GitPath, Branch: r.Branch, Base: r.BaseSHA} |
| if err := w.checkout(ctx, tree, r.CandidateSHA); err != nil { |
| w.Engine.log("worker tree", err) |
| @@ -146,7 +157,7 @@ func (w *Worker) awaitPack(ctx context.Context, r stepRow, want wire.TransferHea |
| } |
| deadline := time.Now().Add(wait) |
| for { |
| - h, ok, err := w.State.inboundHeader(ctx, r) |
| + h, ok, err := w.State.inboundHeader(ctx, r, want.Kind) |
| if err != nil { |
| return nil, err |
| } |
| @@ -170,25 +181,33 @@ func (w *Worker) awaitPack(ctx context.Context, r stepRow, want wire.TransferHea |
| } |
| } |
| +// errCommitMissing: the indexed pack does not yield a commit it must. |
| +var errCommitMissing = errors.New("the pack does not hold the commit") |
| + |
| // index adds the pack to the repository's store and requires that it |
| -// yields exactly the candidate, a commit whose whole tree is present. |
| -func (w *Worker) index(ctx context.Context, store string, pack []byte, sha string) error { |
| +// yields each of shas (the candidate; a hosted pack's base too), a commit |
| +// whose whole tree is present. |
| +func (w *Worker) index(ctx context.Context, store string, pack []byte, shas ...string) error { |
| if _, err := os.Stat(store); err != nil { |
| if err := os.MkdirAll(store, 0700); err != nil { |
| return err |
| } |
| - if _, err := w.git(ctx, nil, "init", "-q", "--bare", "--template=", "--object-format="+map[bool]string{true: "sha256", false: "sha1"}[len(sha) == 64], store); err != nil { |
| + if _, err := w.git(ctx, nil, "init", "-q", "--bare", "--template=", "--object-format="+map[bool]string{true: "sha256", false: "sha1"}[len(shas[0]) == 64], store); err != nil { |
| return err |
| } |
| } |
| if _, err := w.git(ctx, strings.NewReader(string(pack)), "--git-dir="+store, "index-pack", "--stdin"); err != nil { |
| return err |
| } |
| - if kind, err := w.git(ctx, nil, "--git-dir="+store, "cat-file", "-t", "--end-of-options", sha); err != nil || kind != "commit" { |
| - return errors.New("the pack does not hold the candidate") |
| + for _, sha := range shas { |
| + if kind, err := w.git(ctx, nil, "--git-dir="+store, "cat-file", "-t", "--end-of-options", sha); err != nil || kind != "commit" { |
| + return errCommitMissing |
| + } |
| + if _, err := w.git(ctx, nil, "--git-dir="+store, "rev-list", "--objects", "--no-walk", "--end-of-options", sha); err != nil { |
| + return errors.Join(errCommitMissing, err) |
| + } |
| } |
| - _, err := w.git(ctx, nil, "--git-dir="+store, "rev-list", "--objects", "--no-walk", "--end-of-options", sha) |
| - return err |
| + return nil |
| } |
| // checkout writes the candidate's tree into a fresh directory. |
diff --git a/internal/runner/thin/worker_test.go b/internal/runner/thin/worker_test.go| index 51422cae..bb82913a 100644 |
| --- a/internal/runner/thin/worker_test.go |
| +++ b/internal/runner/thin/worker_test.go |
| @@ -40,11 +40,12 @@ type workerRig struct { |
| r stepRow |
| base, commit string |
| pack, other []byte |
| + seed string |
| } |
| -func onePack(t *testing.T, dir, sha string) []byte { |
| +func onePack(t *testing.T, dir string, shas ...string) []byte { |
| t.Helper() |
| - objects, err := exec.Command("git", "-C", dir, "rev-list", "--objects", "--no-walk", sha).Output() |
| + objects, err := exec.Command("git", append([]string{"-C", dir, "rev-list", "--objects", "--no-walk"}, shas...)...).Output() |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -82,7 +83,7 @@ func newWorkerRig(t *testing.T) *workerRig { |
| e.O.Runner.GitPath, e.O.Runner.StateDir, e.O.State = "/usr/bin/git", state, s |
| g := &stubGate{} |
| r := stepRow{DispatchCommand: wire.DispatchCommand{Person: "p@x", TaskID: "tk1", StepID: "s2", Attempt: 1, Specialist: wire.SpecialistChecks, Repo: "amadan.net/woodstar/woodstar", Branch: "equipo/tk1-swipe", BaseSHA: base, CandidateSHA: commit, Needs: []string{wire.TagMacOS, wire.TagXcode}}} |
| - return &workerRig{w: &Worker{State: s, Engine: e, Gate: g, PackWait: 300 * time.Millisecond}, gate: g, r: r, base: base, commit: commit, pack: onePack(t, seed, commit), other: onePack(t, seed, base)} |
| + return &workerRig{w: &Worker{State: s, Engine: e, Gate: g, PackWait: 300 * time.Millisecond}, gate: g, r: r, base: base, commit: commit, pack: onePack(t, seed, commit), other: onePack(t, seed, base), seed: seed} |
| } |
| func (x *workerRig) header() wire.TransferHeader { |
diff --git a/internal/wire/hosted.go b/internal/wire/hosted.go| index 2cc78a67..4b320130 100644 |
| --- a/internal/wire/hosted.go |
| +++ b/internal/wire/hosted.go |
| @@ -27,6 +27,10 @@ const ( |
| TransferKindDeps = "deps" |
| ) |
| +// ReasonPackIncomplete: a hosted pack indexed without its base or its |
| +// candidate; the checks end cannot_run. |
| +const ReasonPackIncomplete = "pack_incomplete" |
| + |
| // DepsTransferMax is a deps transfer's own byte limit. Every other kind |
| // keeps MaxTransferBytes. |
| const DepsTransferMax = 256 << 20 |
diff --git a/internal/wire/mac_engineering.go b/internal/wire/mac_engineering.go| index 71599b51..393d4f17 100644 |
| --- a/internal/wire/mac_engineering.go |
| +++ b/internal/wire/mac_engineering.go |
| @@ -123,7 +123,8 @@ func ValidCommitSHA(s string) bool { return commitSHA.MatchString(s) } |
| // order, de-duplicated and bounded by MaxTransferCommits: the candidate |
| // for a checks transfer or an outbound commit; for an Engineering |
| // transfer the base, then (a rebase round) the old base and the |
| -// candidate. Empty fields are left out. |
| +// candidate; for a hosted pack the base and the candidate. Empty fields |
| +// are left out. |
| func (h TransferHeader) Commits() []string { |
| named := []string{h.CandidateSHA} |
| if h.Kind != "" { |
| @@ -142,7 +143,7 @@ func (h TransferHeader) Commits() []string { |
| // transfer has a candidate (and its base, when known) and no old base. |
| // An Engineering transfer is inbound and has a base, and either nothing |
| // else (a first step or a fix round) or both an old base and a candidate |
| -// (a rebase round). |
| +// (a rebase round). A deps transfer is not valid yet (Task 23). |
| func (h TransferHeader) Valid() bool { |
| switch h.Kind { |
| case "": |
| @@ -153,6 +154,11 @@ func (h TransferHeader) Valid() bool { |
| if h.Direction != TransferInbound || h.BaseSHA == "" || (h.OldBaseSHA == "") != (h.CandidateSHA == "") { |
| return false |
| } |
| + case TransferKindHostedPack: |
| + // Inbound, exactly two commits: the base, then the candidate. |
| + if h.Direction != TransferInbound || h.OldBaseSHA != "" || h.BaseSHA == "" || h.CandidateSHA == "" || h.BaseSHA == h.CandidateSHA { |
| + return false |
| + } |
| default: |
| return false |
| } |
diff --git a/internal/wire/runners.go b/internal/wire/runners.go| index 206d92a5..4e4a7fb3 100644 |
| --- a/internal/wire/runners.go |
| +++ b/internal/wire/runners.go |
| @@ -654,8 +654,10 @@ type Blob struct { |
| Data string `json:"data"` |
| } |
| -// SendPackCommand asks the primary for the pack of CandidateSHA, which |
| -// it uploads as the transfer's blobs (Have: commits the receiver holds). |
| +// SendPackCommand asks the primary for the pack of the header's Commits, |
| +// which it uploads as the transfer's blobs (Have: commits the receiver |
| +// holds). A hosted_pack (ProtocolHostedRunners) asks for the base and the |
| +// candidate; Encode refuses it below that protocol. |
| type SendPackCommand struct { |
| Transfer TransferHeader `json:"transfer"` |
| Have []string `json:"have"` |