equipo: hosted limits count only while their owner is an owner
A limit row whose setting owner is no longer a current owner (removed, inactive or demoted) gives way to the defaults, checked when admission reads the limits inside its transaction; a roster lookup error for that owner denies. TestOwnerRaisesLimitRevoked; budget record raised for internal/equipo/chat.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+96
−3
.rastrillo/budgets.txt+1 −1docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+14 −0internal/equipo/chat/hosted_limits.go+11 −2internal/equipo/chat/hosted_limits_test.go+70 −0
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index acc5e8d1..dd8f1b4b 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -59,7 +59,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 46013 20214 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. Its review round 1 added the report-ingestion test for pack_incomplete; 45105/18911. Task 18: hosted_setting.go (SetHostedChecks, hostedChecks, HostedSettings; owner only, roster in the transaction, audited) and its tests; 45208/19070. Its review round 1: the audit atomicity test; 45208/19113. Task 19: hosted_jobs.go (the hosted_jobs and hosted_challenges tables, HostedJob and its lifecycle: newHostedJob, the listed moves only, ended and failed only with stop proof or never-launched evidence, settleHostedJob once, hostedJobForRunner; each move audited in its transaction) and its tests, TestMigrationAdditive included; 45658/19577. Its review round 1: the never-launched evidence contract (HostedNeverLaunched, launch_by and launching_since, hostedLaunchReturned) and tests for it, settlement of unproven terminal records and audit atomicity; 45728/19739. Task 20: hosted_limits.go (per-person hosted limits, S2-9: hostedAdmit held in the write by a per-person lock row, hostedHoldFor, Allocate, hostedSettle at 4x the baseline, SetHostedLimits owner only with the roster, revision and audit in the transaction) and its tests; 46013/20214. |
| +size internal/equipo/chat 46022 20284 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. Its review round 1 added the report-ingestion test for pack_incomplete; 45105/18911. Task 18: hosted_setting.go (SetHostedChecks, hostedChecks, HostedSettings; owner only, roster in the transaction, audited) and its tests; 45208/19070. Its review round 1: the audit atomicity test; 45208/19113. Task 19: hosted_jobs.go (the hosted_jobs and hosted_challenges tables, HostedJob and its lifecycle: newHostedJob, the listed moves only, ended and failed only with stop proof or never-launched evidence, settleHostedJob once, hostedJobForRunner; each move audited in its transaction) and its tests, TestMigrationAdditive included; 45658/19577. Its review round 1: the never-launched evidence contract (HostedNeverLaunched, launch_by and launching_since, hostedLaunchReturned) and tests for it, settlement of unproven terminal records and audit atomicity; 45728/19739. Task 20: hosted_limits.go (per-person hosted limits, S2-9: hostedAdmit held in the write by a per-person lock row, hostedHoldFor, Allocate, hostedSettle at 4x the baseline, SetHostedLimits owner only with the roster, revision and audit in the transaction) and its tests; 46013/20214. Its coordinator ruling: a limit counts only while its setting owner is a current owner (defaults otherwise, checked in the admission transaction) and TestOwnerRaisesLimitRevoked; 46022/20284. |
| 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 82b71be2..73fd720a 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -1523,3 +1523,17 @@ Mutations (exact-string replace in `internal/equipo/chat/hosted_limits.go` from |
| Not mutation-checked: the `hosted_admit_locks` write. On SQLite every transaction runs on rastrillo's single writer connection (`SetMaxOpenConns(1)`), so admissions are already serial, and `TestLimitsRace` passes with or without the row. The row keeps the counts and the insert atomic if the store ever gets more than one writer. |
| For Task 21: call `hostedAdmit`, then `newHostedJob` with `hostedHoldFor(size, lifetime, now)` and `At: now`, in the same transaction. On an error the step keeps waiting. Settle with `hostedSettle(attempt)` once the job has ended or failed with proof. |
| + |
| +### Task 20 coordinator ruling (a revoked owner's limit) |
| + |
| +Ruling: a limit row counts only while the owner who set it is still a current owner. Otherwise the defaults apply. This is checked when the limits are read inside the admission transaction (AGENTS.md: revoked owners keep no authority; the same rule as Task 18's setting). `hostedLimits` now calls `isOwner(row.Owner)` in the transaction. `ErrForbidden` (removed, inactive or demoted) gives `DefaultHostedLimits` at the row's revision, so the next owner's write still names the right revision. Any other lookup error is returned, and admission is denied. |
| + |
| +Red: `CGO_ENABLED=0 go test ./internal/equipo/chat -run 'TestOwnerRaisesLimitRevoked' -count=1`: exit 1, `hosted_limits_test.go:520: a removed owner's limit still counts: "admitted" <nil>`. |
| + |
| +`TestOwnerRaisesLimitRevoked`: a second owner raises the person's limit to three running, and two jobs are admitted. With that owner removed, a third is refused `running` (the defaults). The same holds with that owner demoted to member. With the setter's roster lookup failing, admission returns an error. Once they are an owner again, the limit is three at revision 1 and a third job is admitted. |
| + |
| +Green: `CGO_ENABLED=0 go test ./internal/equipo/chat -run 'TestLimits|TestHoldSpans|TestDelayedLaunch|TestSettleStays|TestNoProofKeepsHold|TestLimitCount|TestOwnerRaises' -count=1`: ok (10 tests). `make migration-check budget build vet`: exit 0 after raising the chat record to 46022/20284 with the reason. gofmt clean. |
| + |
| +Mutations (same method, `-run TestOwnerRaisesLimitRevoked`, restored and compared with `filecmp`: identical): |
| +- **271** a revoked owner's limit counts: `if err := t.isOwner(r.Owner); err != nil {` became `...; false && err != nil {`. Exit 1, `hosted_limits_test.go:520: a removed owner's limit still counts: "admitted" <nil>`. Restored. |
| +- **272** the setter's lookup error read as the defaults: `if errors.Is(err, ErrForbidden) {` became `if true || errors.Is(err, ErrForbidden) {`. Exit 1, `hosted_limits_test.go:529: admitted with the setter's roster lookup failing: "running"`. Restored. |
diff --git a/internal/equipo/chat/hosted_limits.go b/internal/equipo/chat/hosted_limits.go| index 547ffe11..7c192c56 100644 |
| --- a/internal/equipo/chat/hosted_limits.go |
| +++ b/internal/equipo/chat/hosted_limits.go |
| @@ -2,6 +2,7 @@ package chat |
| import ( |
| "context" |
| + "errors" |
| "fmt" |
| "time" |
| @@ -131,8 +132,10 @@ func hostedHoldFor(size isolatedjob.Size, lifetime time.Duration, now time.Time) |
| } |
| // hostedLimits is person's limits and the revision a write must name: the |
| -// defaults at revision 0 when no owner has set any. A read error is |
| -// returned, never read as the defaults. |
| +// defaults at revision 0 when no owner has set any. A row counts only |
| +// while the owner who set it is a current owner, checked in this |
| +// transaction; otherwise the defaults apply, at the row's revision. A |
| +// read or roster lookup error is returned, never read as the defaults. |
| func (t *txn) hostedLimits(person string) (HostedLimits, int64, error) { |
| var rows []hostedLimitRecord |
| if err := t.db.Where("person = ?", person).Limit(1).Find(&rows).Error; err != nil { |
| @@ -142,6 +145,12 @@ func (t *txn) hostedLimits(person string) (HostedLimits, int64, error) { |
| return DefaultHostedLimits, 0, nil |
| } |
| r := rows[0] |
| + if err := t.isOwner(r.Owner); err != nil { |
| + if errors.Is(err, ErrForbidden) { |
| + return DefaultHostedLimits, r.Revision, nil |
| + } |
| + return HostedLimits{}, 0, err |
| + } |
| return HostedLimits{Running: int(r.Running), DailySeconds: r.DailySeconds, MonthlyMicros: r.MonthlyMicros}, r.Revision, nil |
| } |
diff --git a/internal/equipo/chat/hosted_limits_test.go b/internal/equipo/chat/hosted_limits_test.go| index c9f8f9e5..a3513c64 100644 |
| --- a/internal/equipo/chat/hosted_limits_test.go |
| +++ b/internal/equipo/chat/hosted_limits_test.go |
| @@ -10,6 +10,7 @@ import ( |
| "amadan.net/oficina/equipo/internal/hostedwire" |
| "amadan.net/oficina/equipo/internal/isolatedjob" |
| + "amadan.net/oficina/equipo/internal/wire" |
| "gorm.io/gorm" |
| ) |
| @@ -473,3 +474,72 @@ func TestOwnerRaisesLimit(t *testing.T) { |
| t.Fatalf("a removed owner set limits: %v", err) |
| } |
| } |
| + |
| +// TestOwnerRaisesLimitRevoked: a limit counts only while the owner who |
| +// set it is a current owner, checked inside the admission transaction. A |
| +// removed or demoted owner's limit gives way to the defaults; a roster |
| +// lookup error for that owner denies. |
| +func TestOwnerRaisesLimitRevoked(t *testing.T) { |
| + r, _ := rosterRig(t) |
| + r.task("tkh", "Hosted", hkOwner) |
| + me := r.s.authority.Me |
| + ownerLookupFails := false |
| + r.s.authority.Me = func(tx *gorm.DB, address string) (wire.Me, error) { |
| + if ownerLookupFails && address == hkMember { |
| + return wire.Me{}, errors.New("roster unavailable") |
| + } |
| + return me(tx, address) |
| + } |
| + admit := func(step string) (string, error) { |
| + t.Helper() |
| + r.hostedStep(step) |
| + reason := "admitted" |
| + err := r.tx(func(t *txn) error { |
| + ok, why, err := t.hostedAdmit(hkOwner, isolatedjob.SizeLarge, HostedChecksLifetime, r.now) |
| + if err != nil || !ok { |
| + reason = why |
| + return err |
| + } |
| + hold, err := hostedHoldFor(isolatedjob.SizeLarge, HostedChecksLifetime, r.now) |
| + if err != nil { |
| + return err |
| + } |
| + _, err = t.newHostedJob(hostedJobSpec{Step: step, Size: isolatedjob.SizeLarge, Hold: hold, At: r.now}) |
| + return err |
| + }) |
| + return reason, err |
| + } |
| + r.must(r.s.SetHostedLimits(r.ctx, hkMember, hkOwner, HostedLimits{Running: 3, DailySeconds: 8 * 3600, MonthlyMicros: 50_000_000}, 0)) |
| + for _, step := range []string{"st-1", "st-2"} { |
| + if got, err := admit(step); got != "admitted" || err != nil { |
| + t.Fatalf("%s under the raised limit: %q %v", step, got, err) |
| + } |
| + } |
| + r.exec("UPDATE members SET active=0 WHERE address=?", hkMember) |
| + if got, err := admit("st-3"); got != "running" || err != nil { |
| + t.Fatalf("a removed owner's limit still counts: %q %v", got, err) |
| + } |
| + r.exec("UPDATE members SET active=1, role='member' WHERE address=?", hkMember) |
| + if got, err := admit("st-4"); got != "running" || err != nil { |
| + t.Fatalf("a demoted owner's limit still counts: %q %v", got, err) |
| + } |
| + r.exec("UPDATE members SET role='owner' WHERE address=?", hkMember) |
| + ownerLookupFails = true |
| + if got, err := admit("st-5"); err == nil { |
| + t.Fatalf("admitted with the setter's roster lookup failing: %q", got) |
| + } |
| + ownerLookupFails = false |
| + var l HostedLimits |
| + var rev int64 |
| + r.must(r.tx(func(t *txn) error { |
| + var err error |
| + l, rev, err = t.hostedLimits(hkOwner) |
| + return err |
| + })) |
| + if l.Running != 3 || rev != 1 { |
| + t.Fatalf("an owner again: %+v %d", l, rev) |
| + } |
| + if got, err := admit("st-6"); got != "admitted" || err != nil { |
| + t.Fatalf("the reinstated owner's limit: %q %v", got, err) |
| + } |
| +} |