Skip to content

feat(api): the decisions of an agent rollout, and reading a release (part of #240) - #446

Merged
samuelmbabhazi merged 3 commits into
mainfrom
feat/240-agent-rollout-domain
Nov 24, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
feat/240-agent-rollout-domain

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

Part of #240, step 1c of ADR 0028 (#436). The building blocks of the agent rollout: what it decides for each server, and how the control plane reads a release. Nothing calls them yet. The next pull request adds the rollout's table, queue, API and audit events on top.

What changed

Release versions (fleet/domain/agent-upgrade.ts), with the agent's rules (agent/internal/upgrade):

  • parseReleaseVersion accepts MAJOR.MINOR.PATCH with an optional prerelease. It refuses build metadata, leading zeros, numbers over 9 digits, texts over 64 characters, and dev.
  • compareVersions follows semantic versioning precedence, prereleases included.
  • agentArchive and releaseFile give the names and addresses that release.yml publishes.

The rollout's decisions, pure:

  • upgradeCandidates lists the servers to upgrade: not removed, on a release older than the target, on amd64 or arm64. A dev agent is left out, since the agent refuses to upgrade one.
  • nextUpgradeStep decides, for the server whose turn it is: send, wait, skip, done, finish, or fail with a reason:
    • Before sending. A server that is gone, already on the target version, or that cannot take a release is skipped. A server that is not connected gets 5 minutes (node_unavailable). A server with any other unfinished action, which is how a deployment touching it shows, gets 30 minutes (node_busy). This is ADR 0028's "never while a deployment touches that server".
    • After sending. An action that did not succeed stops the rollout (action_failed, with the action's reason). An action that never ends, because the server never came back, stops it after 35 minutes (not_confirmed).
    • After success. The new version showing up means done. A heartbeat recorded more than 150 seconds after the success, still on the previous version, means rolled_back: the previous binary stops within 2 minutes of its result (the agent's restartWithin), so a later heartbeat comes from a process started after it, and the guard put the previous binary back. With no new version within 25 minutes, the rollout stops with not_confirmed.

Reading a release (fleet/infrastructure/github-agent-releases.ts):

  • It reads the archive's SHA-256 from the release's checksums.txt: one line, lowercase hex, listed once.
  • It reads the archive's .sigstore.json bundle: a JSON object with a Sigstore bundle media type.
  • It uses HTTPS only, to GitHub's release hosts only, and checks every redirect against that list. No credentials, no other port, at most 5 redirects, 20 seconds, and 64 KiB per file, declared or streamed.
  • It returns null on any failure, and logs only the kind of failure.
  • Nothing it reads is trusted: the agent checks the digest and verifies the bundle offline (feat(agent): verify an upgrade offline against its Sigstore signature (part of #240) #441).

Why

ADR 0028 section 2: the control plane names the version, relays the digest and the bundle, sends UpgradeAgent to one server at a time, waits for that agent to report the new version, and stops at the first failure. The decisions are pure so they can be tested on their own, including every timing.

How it was tested

  • agent-upgrade.spec.ts, 16 tests:
    • versions, valid and malicious;
    • precedence, checked on every pair of 13 versions;
    • candidates;
    • every branch of nextUpgradeStep, at each timing boundary.
    • Coverage: 100 % of lines, 96 % of branches.
  • github-agent-releases.spec.ts:
    • the happy path;
    • a redirect to release storage;
    • redirects refused to another host, plain HTTP, credentials, another port, a lookalike host and file:;
    • a redirect loop, and a redirect without a location;
    • files over their bounds, declared or streamed;
    • a missing digest or bundle, an invalid bundle, a network failure;
    • the checksums.txt parser.
    • Coverage: 97 % of lines.
  • pnpm nx run-many -t lint,typecheck,test -p api passes.

Security considerations

Release versions with the agent's rules, the servers a rollout lists, and the pure decision for the server whose turn it is: send, wait, skip, move on, or stop with a reason (ADR 0028).

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ef4bab3 and 9133f55.

Files selected for processing: 5

  • apps/api/src/modules/fleet/application/ports.ts
  • apps/api/src/modules/fleet/domain/agent-upgrade.spec.ts
  • apps/api/src/modules/fleet/domain/agent-upgrade.ts
  • apps/api/src/modules/fleet/infrastructure/github-agent-releases.spec.ts
  • apps/api/src/modules/fleet/infrastructure/github-agent-releases.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@samuelmbabhazi
samuelmbabhazi merged commit 534a6a4 into main Nov 24, 2026
25 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/240-agent-rollout-domain branch November 24, 2026 11:27
samuelmbabhazi added a commit that referenced this pull request Nov 24, 2026
… (#447)

## Task

Part of #240, step 1c of ADR 0028: the control plane rolls
`UpgradeAgent` out to an organization's servers, one at a time. It is
stacked on #446, which adds the decisions and the release reader. It
uses the version from each hello, added by #445; until #445 merges, a
turn sees the new version at the agent's next `CollectFacts`. The web UI
is the next pull request.

## What changed

**The rollout's table (`agent_upgrades`):**
- It holds:
  - the version, which is the control plane's own;
  - the servers, in turn order;
  - the position, and the action of the current turn;
  - when the turn started;
  - the failure: server, reason, and the action's reason.
- Constraints:
- at most one running rollout per organization, through a partial unique
index;
  - a version pattern;
  - between 1 and 1,000 servers;
  - known failure reasons;
  - a failure exactly when failed, an end time exactly when ended.
- Forced row security, and the table is in `TENANT_TABLES`.
- The runtime role may select, insert and update it, never delete:
rollouts are kept as their history.

**The rollout's runs (`AgentUpgrades.advance`, a pg-boss queue and its
worker):**
- Each run reads the turn under the tenant scope and decides with
`nextUpgradeStep` (#446).
- It ends every turn it can right away: skipped servers, servers already
on the version. It returns when a turn has to wait.
- **Sending.** The release's digest and bundle are read outside any
transaction. `UpgradeAgent` is then enqueued under the key
`au:<rollout>:<node>`, with a 30-minute deadline, and the turn's new
start is written in the same transaction.
- **Concurrency.** Every write is a compare-and-swap on the position and
action just read, so a concurrent run rolls back instead of doing a turn
twice.
- **Wake-ups.**
  - A finished action wakes its rollout through the key.
- `fleet.node.agent_version_changed` (#445) and `fleet.node.ready` wake
the organization's running rollout.
  - Otherwise the rollout looks again every 15 seconds.
- **Audit:** `fleet.agent_upgrade.started` with the actor, then
`node_upgraded` or `node_skipped`, then `succeeded` or `failed` with the
server and the reasons.

**HTTP (`node.manage`):**
- `GET /organizations/{organizationId}/agent-upgrades`:
  - the control plane's version;
  - the servers on an older release;
  - the newest rollout.
- `POST /organizations/{organizationId}/agent-upgrades` with
`{version}`, answering 202:
- **The version must still be the control plane's.** The client names
the version it saw, so a control plane upgraded in the meantime refuses
the rollout. This also keeps the route-role matrix's rule that an empty
body stops at validation.
- **The release is read first.** The release of each needed architecture
must be readable before anything starts.
- **Errors** are stable 409 codes: `agent_upgrade_unavailable`,
`agent_upgrade_version_changed`, `agent_upgrade_running`,
`agent_upgrade_not_needed`, `agent_upgrade_too_many` and
`agent_release_unavailable`.

**Docs:** a SPEC section on the routes, the turns, the timings and the
audit events. The route-role matrix and `openapi.json` are regenerated.

## Why

ADR 0028, section 2. The control plane sends `UpgradeAgent` to one
server at a time. It never sends it while a deployment touches that
server, which shows as any other unfinished action there. It waits for
that agent to report the new version, and stops at the first failure.

## How it was tested

- **Unit (`agent-upgrades.spec.ts`):**
  - **Start:**
- candidates are listed by name, and the releases are read for both
architectures;
    - every refusal leaves nothing behind.
- **A full rollout:** A, then B. No second action goes out while A's
runs, the action carries the release, and every audit event is checked.
  - **The other turns:**
    - stopping at the first failure, with the agent's reason;
    - skipped servers;
    - waiting while another action runs;
    - a release unavailable at a server's turn;
    - the overview.
  - Coverage: 97 % of lines.
- **Integration (`agent-upgrades.integration.spec.ts`, real PostgreSQL,
through HTTP and the restricted runtime role):**
- **Access:** the overview, 404 for roles without `node.manage`, 400 for
an empty or malformed version.
- **The rollout:** 202, then 409 for a second one. The run puts the
`upgradeAgent` action in the database; after the agent's success and new
version, the rollout succeeds with its three audit events, and a new
start answers `agent_upgrade_not_needed`.
- **The table itself:** through the admin login, the partial unique
index, the failure check and the version pattern.
- **Local checks:**
- `pnpm nx run-many -t lint,typecheck,test -p api` passes, and so does
the web typecheck.
- The whole API integration suite passes locally on PostgreSQL 18,
except the three known 23001 tests and two files that need Docker. A
first run caught the missing runtime grant: the queue reads the table
inside the agent stream.

**Size:** about 1,050 lines of code and 600 of tests, over the 500-line
target. The table, the queue and the routes only make sense together,
and the decisions are already in their own pull request.

## Security considerations

- **No URL from a client.** The rollout's target is the control plane's
own version, never one a client supplies; the body only confirms it.
- **What the control plane relays.** It sends only what the release
published. The agent rebuilds the address, checks the digest and
verifies the bundle offline (#441, #442).
- **No new outside service.** The control plane reaches GitHub's release
hosts, which ADR 0028 names.
- **No secret moves.** The bundle and the digest are public.
- **Every state change is audited, and rollouts cannot be deleted.**


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Organization administrators can start agent upgrades and check the
rollout’s status, target version, and pending servers.
* Upgrades proceed server by server, with progress and success or
failure details recorded. Servers that are already up to date or
otherwise ineligible may be skipped.
* **Documentation**
* Added specification details for the upgrade endpoints, rollout
behavior, and retained rollout history.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: samuel mbabhazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant