# Self-healing coordinator

Implemented on `self-heal`, from the dispatched brief commit `cac8b11`
(main base `80c15d0`). Commit only; no push, merge or deployment.
New runtime copy is individually marked `COPY REVIEW PENDING` in
`internal/equipo/copy.go` and listed in the existing copy inventory.
No copy-review server was started. README prose is unchanged.

## Changes

The runner runs the pinned official CLI in the dedicated source home while
holding the existing directory lock, before staging any runtime login.
Claude 2.1.312 uses `auth status --json`; its startup performs and awaits
native refresh. Codex 0.165.0 uses app-server `account/read` with
`refreshToken:true`, after initialization. Neither path starts a model turn.
Equipo consumes account metadata only and does not extract refresh tokens.
The executable digests are checked before maintenance.

Saved-account setup and generic discovery also probe the locked source home.
An unconfined native CLI probe rejects a staged binding. This closes the setup
path as well as the dispatch path.

The source lock stays held throughout the attempt, so maintenance cannot
run alongside a turn. Runtime copies expire after 25 minutes, which covers
the existing 20-minute coordinator attempt limit. A production lease cannot
commit runtime credentials back to its source. Runtime network policy still
excludes the refresh hosts; Claude refresh uses platform.claude.com and Codex
uses auth.openai.com. Owner mode uses the same isolation and needs no new
certification. Certified mode continues to enforce its policy hashes.

Temporary maintenance failures remain retryable. A native missing-login result
or a recognized terminal refresh rejection produces `account.sign_in_again`.
That connection gets one conversation note containing its exact command,
including the dedicated-home environment, pinned executable and directory lock.
Repeated polls do not repeat that note. No credentials are included in reports.

Coordinator settings now show the current connection and the automatic-selection
reason. The binding picker and its Save/Reset controls are gone; the obsolete
binding form action is rejected. Recovery reset actions remain available for
uncertain work. On each new message and poll, selection prefers a currently
healthy report, then fewer active tasks, then the latest report. Only the
person's own enrolled runners qualify. An admitted attempt with pending
containment cannot be moved or cancelled by automatic selection.

Projects without an explicit budget inherit the team ceiling. Job creation,
plan admission, reservation headroom, usage reads and bug preparation agree
on this rule. Explicit project caps and aggregate team caps still apply.
Temporary connection and admission waits stay in the thinking bubble, name
the connection and retry on polling. Missing bindings are retried when a
runner becomes available. Routine fallback no longer adds system notes.

`equipo_status` accepts no caller-supplied scope. The normal claim, handle and
current-roster checks apply before it returns the person's runners and
connections, team budgets, coordinator usage and permitted projects. It excludes
credentials, home paths, machine keys and other people's connections. Stale
heartbeats are reported as unavailable. A deleted selected connection still
renders a waiting conversation; database failures remain errors.

## Verification

Behavioral failures observed before implementation:

- `CGO_ENABLED=0 go test ./internal/equipo/team -run TestSelfHeal -count=1`:
  failed with `job needs configured project`; passed after inheritance.
- `CGO_ENABLED=0 go test ./internal/runner/provider -run TestSourceRefresh -count=1`:
  failed because staging retained the old login and failed refresh still staged.
- `CGO_ENABLED=0 go test ./internal/coordinator -run TestEquipoStatusSchema -count=1`:
  rejected the new read-only tool before implementation.
- `CGO_ENABLED=0 go test ./internal/plan3test -run TestSelfHeal -count=1`:
  failed for the missing live wait bubble and the remaining picker.
- `CGO_ENABLED=0 go test ./internal/runner/provider -run TestSourceRefreshLease -count=1`:
  admitted an expired staged lease before the lifetime guard.
- `CGO_ENABLED=0 go test ./internal/runner/provider -run TestRefreshRevocationCodes -count=1`:
  failed to distinguish terminal refresh rejections before classification.
- `CGO_ENABLED=0 go test ./internal/runner/provider -run TestRefreshUsesOfficialCLIWithoutTurn -count=1`:
  the added staged-home case failed because an unconfined CLI accepted the copy.
- `CGO_ENABLED=0 go test ./internal/ownermodetest -run TestSelfHealRemovedConnection -count=1`:
  returned a not-found error for the waiting conversation before correction.

Each mutation below was applied alone, tested with `CGO_ENABLED=0 go test`
and `-count=1`, and restored immediately. All eleven tests failed at their
behavior assertion, not at compilation:

| Broken guard | Package and test |
| --- | --- |
| Skip source refresh | `./internal/runner/provider -run ^TestSourceRefreshBeforeStageUnderLock$` |
| Remove source flock | Same test |
| Ignore changed identity | `./internal/runner/provider -run ^TestSourceRefreshFailureDoesNotStage$` |
| Ignore staged lifetime | `./internal/runner/provider -run ^TestSourceRefreshLeaseExpiresAndCannotWriteBack$` |
| Permit runtime writeback | Same test |
| Send Codex refreshToken:false | `./internal/runner/provider -run ^TestRefreshUsesOfficialCLIWithoutTurn$` |
| Repeat sign-in note | `./internal/plan3test -run ^TestSelfHealRevokedLoginNotifiedOnce$` (subsequently moved to `./internal/ownermodetest`) |
| Prefer the busier connection | `./internal/plan3test -run ^TestSelfHealBusyConnectionFailsOver$` |
| Permit active-turn fallback | `./internal/plan3test -run ^TestSelfHealFailoverDoesNotMoveActiveTurn$`; caught cancellation of the active job |
| Remove the status runner-person filter | `./internal/ownermodetest -run ^TestSelfHealStatusIsScopedAndRevocationApplies$`; caught another person's runner label |
| Treat a stale heartbeat as available | `./internal/ownermodetest -run ^TestSelfHealStatusDoesNotCallStaleReportsAvailable$` |

Restored targeted tests cover source refresh and CLI failures, picker removal,
status scope and roster revocation, one-time login notices, busy failover,
active-attempt fencing, inherited budgets and project-card-to-work admission.
Existing tests were updated where they required the removed picker, repeated
setup notes, or rejection of an unset project budget. Their authority,
transaction rollback and accounting assertions remain.

## Live proof

Only the authorized paul@tito.io `claude-a` home was used. With the production
runner briefly stopped and the source directory exclusively locked, pinned
Claude `auth status --json` exited 0, reported the expected identity, and
changed the credential file's modification time. The proof inspected metadata
only; it did not read, print or copy credential contents. The runner was
restarted immediately. The brief reported this login as expired. The proof used
it unchanged; no credential was edited to manufacture an expiry. Codex refresh
is covered by fake CLI tests;
no live Codex account was used.

`CGO_ENABLED=0 make e2e-live` passed twice, in 91.8 seconds and 95.6 seconds.
Its fake CLI prerequisites passed too. The final browser run confirmed no
coordinator picker, proposed a project, clicked Set up, then received a second
admitted coordinator reply with `Projects:null` still in the budget record.
The coordinator called `equipo_status` successfully. Each passing run used two
coordinator turns. No specialist model work ran.

Other live harness attempts consumed five coordinator turns. One stopped on
a browser-helper error. Two encountered the setup card being replaced by a
late live update. One two-turn run created the project but its subsequent
reply became contained and uncertain. That run did not silently redispatch.
The final test waits for containment, opens the settled conversation, and
explicitly requires the reply through `post_message`. Metadata-only failure
diagnostics are now included in the harness. Total live coordinator turns:
nine, below the authorized twenty. The production runner was left active.

## Final gates and handoff

The first `CGO_ENABLED=0 make -k ci` completed all lanes. It failed the
reference inventory, size gate and tests that still expected the previous
setup notes, picker and coordinator inventory. It also caught an onboarding
probe error regression and a synthetic-policy test that now crossed the new
executable verification boundary. Targeted checks pass after correction.
The synthetic test now enters the shared policy builder with its synthetic
lease; production still verifies executable digests before source maintenance.
The two changed coordinator inventory hashes remain explicitly uncertified.

Build, vet, staticcheck, govulncheck, gitleaks, adapter checks, namespace checks,
migration checks and performance passed in that first full run. Local timing
notes reflect shared-machine load; no budget ceilings were raised. The size
gate passes after moving diagnostics and test helpers to their feature packages.
The reference inventory contains three new exact entries, including the
controller's authorization in the original brief.

Both permitted full reruns passed every gate with exit 0. Exactly three full
`CGO_ENABLED=0 make -k ci` runs were made. The final run tested 41 packages,
with zero failures, then passed adapter, namespace, migration and performance
checks. Cold boot had a 167 ms median against the 500 ms limit. All screen
performance checks passed.

The final local run reported timing advisories: runner 371.68 s against 330 s,
discovery tests 10.662 s against 10 s, provider 22.654 s against 15 s, Claude
49.786 s against 45 s, and Codex 41.921 s against 35 s. These are advisory
locally but become failures when CI or AMADAN_CI is set. No ceilings were raised.
The last two status mutations were made after the full gate, restored byte for
byte, and followed by passing targeted status and refresh tests.

Scratch logs and disposable test environments are removed. No review server
or test process remains. The pre-existing production runner is active, as
required. The controller can review and push this local commit; pending copy
still needs its release review. Nothing was pushed, merged or deployed.
