Public: anyone can read this; the crew can change it.

One versioned agent protocol: heartbeat, claim, logs, result and containers, served beside the old sync route, with tokens scoped to their role #18475

opened this

Merged 3h 17m ago, live in production

The box agent and the runner now speak one versioned protocol at POST /api/v1/agent/v1/ , and the old routes still answer as before. I ran the game, runner, lending, contract, route-mark and gate tests; they pass, but I did not run the full suite. tsc is clean.

Notes

opened this

The box agent and the runner now speak one versioned protocol at POST /api/v1/agent/v1/<message>, and the old routes still answer as before. I ran the game, runner, lending, contract, route-mark and gate tests; they pass, but I did not run the full suite. tsc is clean.

What changed

  • src/machines/protocol.ts: defines the five messages (heartbeat, claim, logs, result, containers) as plain data. Every request carries v: 1, and each field is validated at the boundary. It also holds the role table and splitReport, which turns a runner's one job report into its logs and result messages.
  • src/http/api/agent_v1.ts: a token's role is box (fgb_), runner (forge_runner_), lender (a lent runner) or helper. A message outside the role is a 403 wrong_role that names the role. A v other than 1 is a 400 bad_version that says which version the forge speaks. A valid request goes to the same handler the old route runs, so versions 0 and 1 behave the same.
  • Version 0: /api/v1/games/agent/sync and /api/v1/runner/* are untouched. A new agent_use table, written at most once a minute per machine and version, records each machine's last use of each version. lastUse(db, 0) shows whether anything still speaks version 0.
  • Clients: agent.sh sends its sync, light sync and boot word as heartbeat with v: 1. The runner claims through claim, and its job reports go out as logs then result. An empty logs still renews the lease.
  • Wiring: contract row and tag, route mark, docs/features/agent-protocol.md, migration and its pin.
  • Tests: test/agent-protocol.test.ts covers:
    • each message round-trips, for a runner and for a box;
    • a version 0 sync is replayed and answers as before;
    • a bad version is refused;
    • a wrong-role token is refused with the role named;
    • the boundary validation;
    • the report split;
    • the last use of each version is recorded.
  • Existing tests I changed: fake forges in game-lend-box, runner-upgrade and runner-end-lines now read v1 requests as the old paths through test/helpers/agent-v1.ts. game-boot expects v: 1 in the boot body.

Left out

  • Helper role: it has a role-table entry (heartbeat only) but no token is minted, since the Mac app helper has none today.
  • Box messages: a box's logs, result and containers are accepted as the sync cut down to their fields. The agent still sends one heartbeat, because splitting its sync into separate calls risks a deployed agent for no gain yet.
  • Job tokens: a job token is treated as the runner role, so a lent job's report is not named as lender.
  • Old routes: none removed; that waits for 30 days with no version 0 use.
  • src/machines/shell.ts: not touched.
  • Gate notes: the gate's "one implementation?" notes on noteUse and lastUse are false matches. Nothing was reused from similar.ts.

To try

  • Enrol a runner and POST /api/v1/agent/v1/heartbeat with its token and {"v":1,"state":"idle"}. It answers 204.
  • Send {"v":2,...} and it answers 400 bad_version.
  • Send a claim with a box token and it answers 403 naming the box role.
  • Run bun test test/agent-protocol.test.ts.

🤖 Generated with Claude Code

Closes #18457

commented

The box agent and the runner now speak one versioned protocol at POST /api/v1/agent/v1/<message>, and the old routes still answer as before. I ran the game, runner, lending, contract, route-mark and gate tests; they pass, but I did not run the full suite. tsc is clean.

What changed

  • src/machines/protocol.ts: defines the five messages (heartbeat, claim, logs, result, containers) as plain data. Every request carries v: 1, and each field is validated at the boundary. It also holds the role table and splitReport, which turns a runner's one job report into its logs and result messages.
  • src/http/api/agent_v1.ts: a token's role is box (fgb_), runner (forge_runner_), lender (a lent runner) or helper. A message outside the role is a 403 wrong_role that names the role. A v other than 1 is a 400 bad_version that says which version the forge speaks. A valid request goes to the same handler the old route runs, so versions 0 and 1 behave the same.
  • Version 0: /api/v1/games/agent/sync and /api/v1/runner/* are untouched. A new agent_use table, written at most once a minute per machine and version, records each machine's last use of each version. lastUse(db, 0) shows whether anything still speaks version 0.
  • Clients: agent.sh sends its sync, light sync and boot word as heartbeat with v: 1. The runner claims through claim, and its job reports go out as logs then result. An empty logs still renews the lease.
  • Wiring: contract row and tag, route mark, docs/features/agent-protocol.md, migration and its pin.
  • Tests: test/agent-protocol.test.ts covers:
    • each message round-trips, for a runner and for a box;
    • a version 0 sync is replayed and answers as before;
    • a bad version is refused;
    • a wrong-role token is refused with the role named;
    • the boundary validation;
    • the report split;
    • the last use of each version is recorded.
  • Existing tests I changed: fake forges in game-lend-box, runner-upgrade and runner-end-lines now read v1 requests as the old paths through test/helpers/agent-v1.ts. game-boot expects v: 1 in the boot body.

Left out

  • Helper role: it has a role-table entry (heartbeat only) but no token is minted, since the Mac app helper has none today.
  • Box messages: a box's logs, result and containers are accepted as the sync cut down to their fields. The agent still sends one heartbeat, because splitting its sync into separate calls risks a deployed agent for no gain yet.
  • Job tokens: a job token is treated as the runner role, so a lent job's report is not named as lender.
  • Old routes: none removed; that waits for 30 days with no version 0 use.
  • src/machines/shell.ts: not touched.
  • Gate notes: the gate's "one implementation?" notes on noteUse and lastUse are false matches. Nothing was reused from similar.ts.

To try

  • Enrol a runner and POST /api/v1/agent/v1/heartbeat with its token and {"v":1,"state":"idle"}. It answers 204.
  • Send {"v":2,...} and it answers 400 bad_version.
  • Send a claim with a box token and it answers 403 naming the box role.
  • Run bun test test/agent-protocol.test.ts.

🤖 Generated with Claude Code

commented

Both findings are fixed and pushed. I did not run the check's four named tests (dead code, duplicate import, first-view budget, module-level state). I also did not run the full suite; the forge's check will. tsc is clean, and the agent-protocol, games and runner tests pass (142). gate --fix was run and its rewrap committed. It listed noteUse and lastUse as possibly duplicating other functions, which I left alone.

What changed

  • Finding 1, version 0 recorded for version 1 traffic. noteUse(…, 0) no longer runs inside gameAgentSync. It now runs in the /api/v1/games/agent/sync route registration, so only requests at the old path count as version 0. A box speaking only v1 leaves no version 0 row.
  • Finding 2, old runner routes not recorded. The old runner routes /api/v1/runner/heartbeat, /claim and /jobs/:id now record version 0 per runner. /jobs/:id looks the runner up from the job. The v1 door builds its own runner table with recording off, so v1 runner traffic is not counted as 0.
  • Tests. The box test now checks that the v1 heartbeat, logs, result and containers leave no version 0 row for that box. The old sync adds one. A new test checks that a runner on the old heartbeat is recorded as version 0. A runner's v1 round trip is checked to record no version 0.
  • Docs. docs/features/agent-protocol.md lists invalid_containers and the other invalid_* refusals. It says exactly which old paths record version 0. It also says what happens when a runner's logs lands and its result times out.

What I left alone

  • jobReporter retry. Nothing dedupes the resent lines. The job log is append-only, so a retry can show some lines twice. I documented that rather than add dedupe or reorder the two sends, because the lines have to arrive before the end.
  • readMessage dead branches. I left the containers and claim branches for non-box roles. They are unreachable behind mayRole, but they keep the switch exhaustive for the type checker.
  • helper role. It stays in the table with no token minted; the doc already says so.
Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs