Reassign work safely when model services are unavailable
Traced the existing scheduler and retry paths, then fixed cases where recorded connection errors or a busy account lock could be treated as healthy. Added regression tests for unavailable connections, authorized fallback and recovery, without expanding spending permissions or changing execution fencing. Tests could not run because dependency-cache access was blocked and approval was unavailable. Documented the remaining work, including provider-wide outage detection, stale health evidence and checkpoint-preserving reassignment. Equipo task tkmngyab7pyj, Engineering step attempt 1.
4 files changed,
+142
−6
docs/engineering/connection-health-reassignment.md+48 −0internal/runner/thin/connection_health_test.go+83 −0internal/runner/thin/conns.go+10 −5internal/runner/thin/schedule.go+1 −1
CI failed — run details
diff --git a/docs/engineering/connection-health-reassignment.md b/docs/engineering/connection-health-reassignment.md| new file mode 100644 |
| index 00000000..50842c77 |
| --- /dev/null |
| +++ b/docs/engineering/connection-health-reassignment.md |
| @@ -0,0 +1,48 @@ |
| +# Connection health and reassignment trace |
| + |
| +This change is safety hardening, not complete provider-outage detection. |
| +A healthy runner hello today does not establish provider-wide uptime or |
| +contradict the reported Claude authentication incident earlier today. |
| + |
| +## Existing execution path |
| + |
| +- `thin.Conns` persists account health in `conn_state`. Authentication |
| + rejection is `sign_in_again`, quota is `limited`, and other refresh failures |
| + are `error`. `conn_validated` separately records successful real requests. |
| +- `Conns.Check` refreshes subscription credentials at startup and hourly. |
| + Token checks require an explicit Check again; a token's local CLI status |
| + cannot clear a rejection. No external status service is consulted. |
| +- `Engine.converse` retries auth/quota failures only with `TurnResult.Proven`. |
| + The durable `State.Retry` compares the previous attempt and state, records |
| + usage and advances the attempt before another run starts. |
| +- Engineering's existing loop requires `stepResult.terminated` before |
| + `Engineer.refused` advances the attempt. Each subsequent attempt goes through |
| + scheduler admission, including capability, budget and review-family rules. |
| + Generic failures do not automatically replay work. Engineering currently |
| + prepares a fresh tree on auth/quota fallback; this is not a general-purpose |
| + checkpoint-preserving outage migration mechanism. |
| +- Instance housekeeping uses selected connection headroom. Its `UpdatedAt` |
| + is a record-update timestamp, not proof of a recent provider request. |
| + Housekeeping claim recovery leaves dispatched claims awaiting runner sync. |
| + |
| +## Safety fixes in this change |
| + |
| +Metered connection choice and scheduler facts previously honored runtime |
| +quota/auth failures but silently ignored `error`, treating a previously |
| +validated key as healthy. Hello also omitted those runtime failures. All three |
| +now honor the same unavailable states, allowing the existing chooser to stand |
| +down or select an already-authorized alternative. No spending permission, |
| +capability or access rule changes. A busy credential-home lock no longer |
| +turns an unknown account into a healthy account: contention is not evidence |
| +that authentication works. |
| + |
| +## Remaining work before general outage reassignment |
| + |
| +Do not enable generic-error replay on the strength of these fixes. It still |
| +needs typed provider outage versus runner/network evidence, independently |
| +persisted observation and failure timestamps, stale/unknown views, bounded |
| +recovery probes, and corroboration before excluding an entire provider. |
| +Probing must not authorize new paid requests. Cross-agent reassignment must |
| +preserve checkpoints, reconcile side effects and fence the previous execution. |
| +Outage/recovery notices and tests for provider-wide incidents, stale evidence, |
| +checkpoint preservation and duplicate prevention are still required. |
diff --git a/internal/runner/thin/connection_health_test.go b/internal/runner/thin/connection_health_test.go| new file mode 100644 |
| index 00000000..44ebeedd |
| --- /dev/null |
| +++ b/internal/runner/thin/connection_health_test.go |
| @@ -0,0 +1,83 @@ |
| +package thin |
| + |
| +import ( |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/runner/connect" |
| + "amadan.net/oficina/equipo/internal/runner/provider" |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +func TestMeteredRuntimeFailureReachesEveryChooser(t *testing.T) { |
| + for _, state := range []string{wire.ConnError, wire.ConnSignInAgain, wire.ConnLimited} { |
| + t.Run(state, func(t *testing.T) { |
| + r := newSchedRig(t) |
| + r.tensorx(wire.Budget{}) |
| + if err := r.conns.load(r.ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.conns.mu.Lock() |
| + r.conns.save(piLocal, connState{State: state, ResetsAt: time.Now().Add(time.Hour)}) |
| + r.conns.mu.Unlock() |
| + if _, _, err := r.conns.ChooseFor(r.ctx, connPerson, connect.RoleReview, "", nil, true); !errors.Is(err, ErrNoConnection) { |
| + t.Fatalf("choice: %v", err) |
| + } |
| + facts, _, err := r.conns.scheduleFacts(r.ctx, r.state.db, connPerson, connect.RoleReview, time.Now()) |
| + if err != nil || len(facts) != 1 || facts[0].State != state { |
| + t.Fatalf("scheduler: %+v, %v", facts, err) |
| + } |
| + held := r.conns.heldMetered(time.Now()) |
| + if len(held) != 1 || held[0].State != state { |
| + t.Fatalf("hello: %+v", held) |
| + } |
| + // A real successful request recovers errors/auth, not an unexpired quota. |
| + if state != wire.ConnLimited { |
| + r.conns.Answered(piLocal) |
| + if _, _, err := r.conns.ChooseFor(r.ctx, connPerson, connect.RoleReview, "", nil, true); err != nil { |
| + t.Fatalf("recovery: %v", err) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestBusyAccountDoesNotProveHealth(t *testing.T) { |
| + r := newSchedRig(t, "claude-1") |
| + if err := r.conns.load(r.ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.conns.checked("claude-1", provider.ErrAccountBusy) |
| + if state := r.conns.live["claude-1"].State; state != "" { |
| + t.Fatalf("unknown became %q", state) |
| + } |
| + r.conns.SignInAgain("claude-1") |
| + r.conns.checked("claude-1", provider.ErrAccountBusy) |
| + if state := r.conns.live["claude-1"].State; state != wire.ConnSignInAgain { |
| + t.Fatalf("auth failure became %q", state) |
| + } |
| +} |
| + |
| +func TestAuthFailureUsesOnlyHealthyAuthorizedAlternative(t *testing.T) { |
| + r := newSchedRig(t, "claude-1", "codex-1") |
| + r.conns.Answered("claude-1") |
| + r.conns.Answered("codex-1") |
| + r.conns.SignInAgain("claude-1") |
| + b, _, err := r.conns.Choose(r.ctx, connPerson, connect.RoleReview, "claude-1", nil) |
| + if err != nil || b.ID != "codex-1" { |
| + t.Fatalf("fallback: %s, %v", b.ID, err) |
| + } |
| + r.conns.SignInAgain("codex-1") |
| + if _, _, err := r.conns.Choose(r.ctx, connPerson, connect.RoleReview, "", nil); !errors.Is(err, ErrNoConnection) { |
| + t.Fatalf("all unavailable: %v", err) |
| + } |
| + r.conns.Answered("claude-1") |
| + if _, _, err := r.conns.Choose(r.ctx, "someone-else", connect.RoleReview, "", nil); !errors.Is(err, ErrNoConnection) { |
| + t.Fatalf("cross-person choice: %v", err) |
| + } |
| + b, _, err = r.conns.Choose(r.ctx, connPerson, connect.RoleReview, "", nil) |
| + if err != nil || b.ID != "claude-1" { |
| + t.Fatalf("recovery: %s, %v", b.ID, err) |
| + } |
| +} |
diff --git a/internal/runner/thin/conns.go b/internal/runner/thin/conns.go| index e103f4dd..ebf0c48a 100644 |
| --- a/internal/runner/thin/conns.go |
| +++ b/internal/runner/thin/conns.go |
| @@ -237,7 +237,7 @@ func (c *Conns) meteredCandidate(ctx context.Context, b runner.AccountBinding) c |
| if m, ok, err := metered.Get(ctx, c.State.db, b.LocalID); err != nil || !ok || m.Refused { |
| cand.State = wire.ConnSignInAgain |
| } |
| - if cs := c.live[b.ID]; cs.State == wire.ConnLimited || cs.State == wire.ConnSignInAgain { |
| + if cs := c.live[b.ID]; connectionUnavailable(cs.State) { |
| cand.State, cand.ResetsAt = cs.State, cs.ResetsAt |
| } |
| return cand |
| @@ -541,6 +541,9 @@ func (c *Conns) heldMetered(now time.Time) []wire.HeldConnection { |
| continue |
| } |
| state := wire.ConnHealthy |
| + if cs := c.live[m.LocalID]; connectionUnavailable(cs.State) { |
| + state = cs.State |
| + } |
| if m.Refused { |
| state = wire.ConnSignInAgain |
| } |
| @@ -734,10 +737,6 @@ func (c *Conns) checked(id string, err error) { |
| switch { |
| case err == nil && (cs.State == wire.ConnSignInAgain || cs.State == wire.ConnError || cs.State == ""): |
| cs.State = wire.ConnHealthy |
| - case errors.Is(err, provider.ErrAccountBusy) && cs.State == "": |
| - // Another process holds the home's lock for its own refresh: the |
| - // account is in use, so it is signed in. |
| - cs.State = wire.ConnHealthy |
| case errors.Is(err, provider.ErrLoginRequired): |
| cs.State = wire.ConnSignInAgain |
| case err != nil && !errors.Is(err, provider.ErrAccountBusy) && cs.State != wire.ConnSignInAgain: |
| @@ -775,3 +774,9 @@ func (c *Conns) Forget(id string) { |
| _, _ = c.State.db.Exec("DELETE FROM conn_validated WHERE id=?", id) |
| _, _ = c.State.db.Exec("DELETE FROM conn_profile WHERE id=?", id) |
| } |
| + |
| +// connectionUnavailable is shared by choice, scheduler admission and hello. |
| +// A validated key is not evidence that a subsequent runtime failure recovered. |
| +func connectionUnavailable(state string) bool { |
| + return state == wire.ConnLimited || state == wire.ConnSignInAgain || state == wire.ConnError |
| +} |
diff --git a/internal/runner/thin/schedule.go b/internal/runner/thin/schedule.go| index 78852e64..315c561a 100644 |
| --- a/internal/runner/thin/schedule.go |
| +++ b/internal/runner/thin/schedule.go |
| @@ -209,7 +209,7 @@ func (c *Conns) scheduleFacts(ctx context.Context, q querier, person, role strin |
| sc.State = wire.ConnSignInAgain |
| } |
| var state, resets string |
| - if err := q.QueryRowContext(ctx, "SELECT state, resets_at FROM conn_state WHERE id=?", b.ID).Scan(&state, &resets); err == nil && (state == wire.ConnLimited || state == wire.ConnSignInAgain) { |
| + if err := q.QueryRowContext(ctx, "SELECT state, resets_at FROM conn_state WHERE id=?", b.ID).Scan(&state, &resets); err == nil && connectionUnavailable(state) { |
| sc.State = state |
| if t, err := time.Parse(time.RFC3339Nano, resets); err == nil { |
| sc.LimitedUntil = t |