# Runner start: heartbeat report encoding

The runner exits because its first heartbeat contains account reports encoded
with Go field names (`LocalID`, `Routing`, `AccountID`). The heartbeat handler
uses the strict protocol decoder, which requires `local_id`, `routing` and
`account_id`. The exact failing check is `runnerproto.DecodeLimit` on
`q.Reports` in `Handler.runnerConnections`, before `ReportConnections` runs.
The instance returns HTTP 422 with `form.invalid`; the client unwraps that
refusal as `provider.ErrContract` and previously discarded its location.

This is a code defect, not a local configuration error. No edit to
`runner.env`, bindings, installed releases or the service was made. The
existing `runner.env.before-enable-execution` backup remains in place.
No credential files or account homes were inspected by investigation commands.
The explicitly authorized foreground runner uses its own normal credential and
account discovery paths. No account login or model task was requested.

## Reproduction and change

The service was inactive before and after the investigation. The initial
literal empty-environment run exited 1 with `runner.policy at EQUIPO_CODEX_PATH`:
without `HOME`, the protected home paths are relative. Systemd supplies `HOME`
to the actual user service. Adding only that standard variable reproduced the
journal's `runner.contract at -`, exit 1:

```sh
timeout --signal=TERM --kill-after=5s 40s env -i HOME=/home/paulca \
  /bin/bash --noprofile --norc -c '
    set -a
    . /home/paulca/.local/share/equipo-runner/runner.env
    set +a
    exec /home/paulca/.local/share/equipo-runner/releases/9f616f3/equipo-runner run
  '
```

A disposable Go overlay added safe stage/status diagnostics. With the same
environment, it returned `runner.contract at http.heartbeat.status_422.form.invalid`.
No request body, response prose, token or account-home content was logged.

`HTTPInstance.Heartbeat` now uses `runnerproto.Marshal` for its nested reports.
The outer protocol encoder preserves `json.RawMessage`, so it cannot repair
ordinary JSON inside that field. Persisted report JSON remains unchanged.
HTTP contract failures now identify status, envelope decoding, version or
result decoding. Refusals retain their typed status/reason and `errors.Is` /
`errors.As` behavior. Unknown remote reasons become the existing fixed
`form.invalid` code. The original failure now prints:

```text
Runner error runner.contract at http.response.status_422.form.invalid.
```

No app copy was added or changed. The existing diagnostic format is reused;
new diagnostic locations are protocol check identifiers. No copy-review server
was started, as instructed.

## Verification

The new tests ran in the runner package before implementation:

```sh
CGO_ENABLED=0 go test ./internal/runner \
  -run '^TestRunnerHeartbeatWireReports$|^TestRunnerHTTPContractDiagnostics$' -count=1
```

Exit 1: the two-account heartbeat failed against the real protocol HTTP handler,
and all six diagnostic cases failed. The heartbeat test also covers empty and
nil account lists. The diagnostic test checks safe refusal codes, redirect
rejection, invalid JSON, wrong protocol version and invalid result shape.

After implementation, this targeted command passed. The new tests now live in
`internal/runner/transporttest`, using the public client against loopback HTTP
servers and the real heartbeat handler:

```sh
CGO_ENABLED=0 go test ./internal/runner/transporttest ./internal/runner ./internal/equipotest \
  -run '^TestRunnerHeartbeatWireReports$|^TestRunnerHTTPContractDiagnostics$|^TestRunnerHTTPClient|^TestDirectBugRuntimeRenewHeartbeat$|^TestDirectClaimRevalidates$|^TestRunnerHeartbeatAndValidatedReceipt$|^TestRunnerAppCSRF$' \
  -count=1
```

Seven independent mutations used disposable overlays in
`/home/paulca/.cache/tmp/equipo-runner-start`. For each name below:

```sh
CGO_ENABLED=0 go test -overlay=/home/paulca/.cache/tmp/equipo-runner-start/NAME.json \
  ./internal/runner/transporttest \
  -run '^TestRunnerHeartbeatWireReports$|^TestRunnerHTTPContractDiagnostics$' -count=1
```

| NAME | Deliberately broken behavior | Result |
| --- | --- | --- |
| wire | Restore ordinary JSON for reports | Exit 1, two-account heartbeat rejected |
| refusal | Remove refusal location | Exit 1, refusal diagnostic assertions |
| status | Remove unexpected-status location | Exit 1, redirect diagnostic assertion |
| envelope | Remove envelope location | Exit 1, malformed-envelope assertion |
| version | Remove version location | Exit 1, wrong-version assertion |
| result | Remove result location | Exit 1, wrong-result assertion |
| redaction | Use the raw remote error code | Exit 1, untrusted reason exposed |

The unchanged working sources passed the targeted command again after the
mutations. No mutation was applied to the working source files.

A second temporary overlay only printed successful heartbeat/report counts
and poll/assignment counts. Built with `CGO_ENABLED=0 go build -overlay=...`
and run with the same environment under a 35-second timeout, it recorded:

```text
probe: heartbeat accepted, reports=2
probe: poll accepted, assignments=0
probe: heartbeat accepted, reports=2
```

SIGTERM stopped the foreground process at the deadline; `timeout` returned 124.
Two `runner.poll_retry` lines appeared as cancellation interrupted polling.
The runner reached the job loop and reported both configured accounts. This
proves report acceptance, not that both accounts passed provider preflight.
No authenticated browser session was used to inspect the app.

One full `make -k ci` completed with exit 2. Its only failing target was
`budget`: the initial test file took `internal/runner` from 8798 to 8902 test
lines against its existing 8800 ceiling. The tests were moved into
`internal/runner/transporttest`, using the public client and loopback HTTP
servers. `make budget` then passed with all 40 directories within budget.
No budget was raised. The final tests and all seven mutations passed in their
new package. There was no second full CI run; the original full gate remains
recorded as failed, with its size failure corrected by the focused rerun.

The full test step ran 30 packages, none cached, with zero failures. Build,
vet, formatting, staticcheck, govulncheck, gitleaks, reference inventory,
adapter checks, namespace checks, migrations and performance all passed.
Cold boot median was 250 ms against the 500 ms limit. After moving the tests,
`make budget vet staticcheck fmt-check runner-dead-references` passed again.

Local timing overruns were advisory; these timings would fail under CI timing
enforcement. No time allowance was changed:

| Package | Measured seconds | Budget seconds |
| --- | ---: | ---: |
| directlive | 518.394 | 360 |
| equipotest | 336.270 | 300 |
| runner | 539.301 | 330 |
| runner/forge | 15.751 | 15 |
| runner/provider | 23.431 | 15 |
| runner/provider/claude | 51.572 | 45 |
| runner/provider/codex | 43.410 | 35 |

## Controller: install the reviewed runner, then restart

Restarting the installed `9f616f3` runner alone will repeat the failure. After
review and human merge, use a clean checkout of the chosen version on docs-t1.
Build a versioned runner release with the existing certified executable paths:

```bash
set -euo pipefail
umask 077
set -a
. "$HOME/.local/share/equipo-runner/runner.env"
set +a
make release-runner
EQUIPO_START_VERSION=$(git rev-parse --short HEAD)
EQUIPO_START_ROOT="$HOME/.local/share/equipo-runner"
EQUIPO_START_RELEASE="$EQUIPO_START_ROOT/releases/$EQUIPO_START_VERSION"
EQUIPO_START_SOURCE="$PWD/releases/equipo-runner-linux-amd64-$EQUIPO_START_VERSION"
mkdir "$EQUIPO_START_RELEASE"
install -m 700 "$EQUIPO_START_SOURCE/equipo-runner" "$EQUIPO_START_RELEASE/"
install -m 700 "$EQUIPO_START_SOURCE/equipo-tool-proxy" "$EQUIPO_START_RELEASE/"
install -m 600 "$EQUIPO_START_SOURCE/install-manifest.json" "$EQUIPO_START_RELEASE/"
"$EQUIPO_START_RELEASE/equipo-runner" verify-install-manifest \
  "$EQUIPO_START_RELEASE/install-manifest.json" >/dev/null
export EQUIPO_START_ROOT EQUIPO_START_RELEASE EQUIPO_START_VERSION
python3 - <<'PY'
import os, pathlib
root = pathlib.Path(os.environ['EQUIPO_START_ROOT'])
service = root / 'equipo-runner.service'
old = service.read_bytes()
before = b'ExecStart=' + bytes(str(root / 'releases/9f616f3/equipo-runner'), 'utf-8') + b' run'
after = b'ExecStart=' + bytes(os.environ['EQUIPO_START_RELEASE'] + '/equipo-runner', 'utf-8') + b' run'
assert old.count(before) == 1, 'Service changed; reconcile its current release first'
backup = root / ('equipo-runner.service.before-' + os.environ['EQUIPO_START_VERSION'])
with backup.open('xb') as f:
    f.write(old)
service.write_bytes(old.replace(before, after))
service.chmod(0o600)
PY
systemctl --user daemon-reload
systemctl --user restart equipo-runner.service
systemctl --user is-active equipo-runner.service
systemctl --user status equipo-runner.service --no-pager
journalctl --user -u equipo-runner.service --since '2 minutes ago' --no-pager
```

The existing tool-proxy path can stay in `runner.env`: that binary is unchanged
and the release gate verifies its certified hash. The new runner uses
`os.Executable()`, so the old `EQUIPO_RUNNER_EXECUTABLE` value is ignored and
needs no edit. No re-enrolment is needed. These are controller commands; this
session did not install a release, start the unit, merge or deploy.

Sign in at
[https://equipo.bds.oncarlos.com/connections](https://equipo.bds.oncarlos.com/connections).
Refresh after startup and confirm cards for `claude-a` and `claude-b`, both
showing Claude `2.1.312`. Neither should show the preflight-unavailable or
sign-in-again message when ready. Connection availability expires after 90
seconds without a report, so refresh again after that interval with the unit
still active to confirm continuing heartbeats. The enrolled-machine page at
[https://equipo.bds.oncarlos.com/runners/enrol](https://equipo.bds.oncarlos.com/runners/enrol)
should list `docs-t1`, but that page has no online badge and enrolment alone
does not prove liveness. Use the active unit plus fresh, available connection
cards. If a card remains unavailable, report its state before requesting work;
accepted heartbeat reports can correctly describe an unavailable account.

Task scratch, temporary overlay binaries and logs were removed. The foreground
runner and all task test servers exited; the systemd unit remains inactive.
The change is committed on `runner-start` for push to `origin/runner-start`.
Human review, versioned installation and restart remain with the controller.
