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

POST /api/v1/admin/releases/:sha/rollback plans, then rolls the forge back to a kept release, with the releases page's rules #7227

opened this

Merged 37d 16h ago, live in production

Adds POST /api/v1/admin/releases/:sha/rollback, so a forge admin's script can roll the forge back to a kept release. It plans by default and only swaps when the body has {"dryRun": false}.

Notes

opened this

Adds POST /api/v1/admin/releases/:sha/rollback, so a forge admin's script can roll the forge back to a kept release. It plans by default and only swaps when the body has {"dryRun": false}. It follows the same rules as /releases and the terminal: mayRollBack, canRollBackTo and rollBackTo from src/release/gate.ts are called as they are, unchanged.

What changed

  • src/http/api/rollback.ts (new): rollbackRoutes, registered with one line beside reportRoutes in src/http/api.ts. In order:
    • admin_gate.ts already refuses a person who may not administer with not_admin.
    • requireScope(ctx, "admin"): an rw or ro token is forbidden.
    • mayRollBack: an agent, or someone who is not a writer of the forge's repository, is forbidden.
    • The sha must be one the page lists and its checkout must still be on disk; otherwise not_found.
    • A plan answers 200 with { dryRun: true, from, to, allowed, message? } and changes nothing.
    • An apply answers 409 not_rollable when the page would offer no button, 409 not_rolled_back with rollBackTo's own line when the swap refuses, and { dryRun: false, from, to, rolledBack: true } on success, before this process drains.
  • src/release/words.ts: two shared sentences, ALREADY_LIVE and NOT_ROLLABLE, for the case where the page shows no Roll back button. The page itself has no sentence for this today; it just hides the button.
  • src/contract/openapi/routes/postApiV1AdminReleasesShaRollback.ts: the route's OpenAPI description. The index line was written by bun scripts/openapi-routes.ts.
  • test/api-rollback.test.ts covers:
    • the plan is the default and nothing drains (a spy on handover.drain);
    • the plan explains why the live release and a release whose schema is behind are not allowed;
    • applying a schema-behind release is 409 with those words;
    • applying with self-swap off goes through rollBackTo and refuses with its own line;
    • a member gets not_admin, and an admin's rw or ro token gets forbidden;
    • no token gets 401;
    • an unknown sha, a malformed sha, or a release whose checkout was pruned gets not_found;
    • a dryRun that is not a boolean gets 422.

What to look at

  • Release lookup: it uses listReleases, the page's newest-20 list, so the API offers exactly what the page offers.
  • Plan with a refusal: a refused plan still answers 200 with allowed: false. Only an apply turns a refusal into a 409.

Not done

  • No MCP tool. The issue limited the change to these three files, and undoing a deploy stays a person's call: mayRollBack already refuses agents.
  • /releases and the terminal are untouched.

bun x tsc --noEmit is clean. Run here and passing: the new test plus the openapi*, admin*, release-gate, releases-page, hollow, api-list-shape, mcp-releases and token-admin-scope suites (452 tests). scripts/fast.ts named 946 test files for this diff (about 82 minutes), over its budget, so I chose those suites by hand instead. The forge's check runs everything on the push.

To try it: with an admin-scope token, send curl -X POST -H "authorization: Bearer $T" $FORGE/api/v1/admin/releases/<sha>/rollback to get the plan. Add -d '{"dryRun":false}' to apply it.

🤖 Generated with Claude Code

Closes #7217

2 earlier comments
commented

Adds POST /api/v1/admin/releases/:sha/rollback, so a forge admin's script can roll the forge back to a kept release. It plans by default and only swaps when the body has {"dryRun": false}. It follows the same rules as /releases and the terminal: mayRollBack, canRollBackTo and rollBackTo from src/release/gate.ts are called as they are, unchanged.

What changed

  • src/http/api/rollback.ts (new): rollbackRoutes, registered with one line beside reportRoutes in src/http/api.ts. In order:
    • admin_gate.ts already refuses a person who may not administer with not_admin.
    • requireScope(ctx, "admin"): an rw or ro token is forbidden.
    • mayRollBack: an agent, or someone who is not a writer of the forge's repository, is forbidden.
    • The sha must be one the page lists and its checkout must still be on disk; otherwise not_found.
    • A plan answers 200 with { dryRun: true, from, to, allowed, message? } and changes nothing.
    • An apply answers 409 not_rollable when the page would offer no button, 409 not_rolled_back with rollBackTo's own line when the swap refuses, and { dryRun: false, from, to, rolledBack: true } on success, before this process drains.
  • src/release/words.ts: two shared sentences, ALREADY_LIVE and NOT_ROLLABLE, for the case where the page shows no Roll back button. The page itself has no sentence for this today; it just hides the button.
  • src/contract/openapi/routes/postApiV1AdminReleasesShaRollback.ts: the route's OpenAPI description. The index line was written by bun scripts/openapi-routes.ts.
  • test/api-rollback.test.ts covers:
    • the plan is the default and nothing drains (a spy on handover.drain);
    • the plan explains why the live release and a release whose schema is behind are not allowed;
    • applying a schema-behind release is 409 with those words;
    • applying with self-swap off goes through rollBackTo and refuses with its own line;
    • a member gets not_admin, and an admin's rw or ro token gets forbidden;
    • no token gets 401;
    • an unknown sha, a malformed sha, or a release whose checkout was pruned gets not_found;
    • a dryRun that is not a boolean gets 422.

What to look at

  • Release lookup: it uses listReleases, the page's newest-20 list, so the API offers exactly what the page offers.
  • Plan with a refusal: a refused plan still answers 200 with allowed: false. Only an apply turns a refusal into a 409.

Not done

  • No MCP tool. The issue limited the change to these three files, and undoing a deploy stays a person's call: mayRollBack already refuses agents.
  • /releases and the terminal are untouched.

bun x tsc --noEmit is clean. Run here and passing: the new test plus the openapi*, admin*, release-gate, releases-page, hollow, api-list-shape, mcp-releases and token-admin-scope suites (452 tests). scripts/fast.ts named 946 test files for this diff (about 82 minutes), over its budget, so I chose those suites by hand instead. The forge's check runs everything on the push.

To try it: with an admin-scope token, send curl -X POST -H "authorization: Bearer $T" $FORGE/api/v1/admin/releases/<sha>/rollback to get the plan. Add -d '{"dryRun":false}' to apply it.

🤖 Generated with Claude Code

commented

Adds POST /api/v1/admin/releases/:sha/rollback, so a forge admin's script can roll the forge back to a kept release. It plans by default and only swaps on {"dryRun": false}. It uses mayRollBack, canRollBackTo and rollBackTo from src/release/gate.ts unchanged, so the API follows the same rules as /releases and the terminal.

What changed

  • src/http/api/rollback.ts (new): rollbackRoutes, registered with one line beside reportRoutes in src/http/api.ts. In order:
    • admin_gate.ts refuses a person who may not administer with not_admin.
    • requireScope(ctx, "admin"): an rw or ro token is forbidden.
    • mayRollBack: an agent is forbidden, and so is everyone when FORGE_REPO names no repository on this forge.
    • The sha must be one the page lists and its checkout must still be on disk; otherwise not_found.
    • A plan answers 200 with { dryRun, from?, to, allowed, message? } and changes nothing.
    • An apply answers 409 not_rollable when the page would offer no Roll back button, and 409 not_rolled_back with rollBackTo's own line when the swap refuses. On success it answers { dryRun, from?, to, rolledBack }, then this process drains.
    • from is the live commit, and is left out when the forge does not know which commit is live.
  • src/release/words.ts: two shared sentences, ALREADY_LIVE and NOT_ROLLABLE, for the case where the page shows no Roll back button.
  • src/contract/openapi/routes/postApiV1AdminReleasesShaRollback.ts: the route's OpenAPI description, with from marked optional. The index line was generated by bun scripts/openapi-routes.ts.
  • README.md: one sentence in the /releases paragraph names the API route and says it has the same rules as the page (never an agent).
  • test/api-rollback.test.ts covers:
    • the plan is the default and nothing drains (a spy on handover.drain);
    • the plan leaves out from when neither FORGE_COMMIT nor FORGE_APP_COMMIT is set;
    • the plan explains why the live release and a release whose schema is behind are not allowed;
    • applying a schema-behind release is 409 with those words;
    • applying with self-swap off goes through rollBackTo and refuses;
    • a member gets not_admin, and an admin's rw or ro token gets forbidden;
    • an admin-scope agent, and an admin on a forge whose FORGE_REPO names no repository, get forbidden on both the plan and the apply, and nothing drains;
    • no token gets 401, and an unknown, malformed or pruned sha gets not_found;
    • a dryRun that is not a boolean gets 422.

Where I departed from the review

  • The second mayRollBack caller: the review asked for a site admin who does not write alice/forge. That account can't exist. personalOrDirect (src/core/repos.ts:925) gives every site admin write on every repository, and the admin gate lets a person through only if they are a site admin. So the test pins the other case where mayRollBack refuses a person: FORGE_REPO names no repository here.
  • Proof the line is covered: with line 26 of rollback.ts (if (!mayRollBack(...)) return forbidden();) deleted, both new tests fail.
  • from when unknown: I left it out rather than declaring it nullable. That keeps the field a plain string in the contract.

Not done: no MCP tool, because agents may not roll back.

Checks: bun x tsc --noEmit is clean. The new test passes, along with the openapi*, admin*, releases-page, readme-front-door and policy suites.

To try it: send curl -X POST -H "authorization: Bearer $ADMIN_TOKEN" $FORGE/api/v1/admin/releases/<sha>/rollback to get the plan. Add -d '{"dryRun":false}' to apply it.

🤖 Generated with Claude Code

commented

This pull adds POST /api/v1/admin/releases/:sha/rollback (src/http/api/rollback.ts), so an admin can roll back through the API. It works in two steps: it first plans the rollback, then rolls back to a release the forge has kept. It follows the same rules as the admin releases page. mayRollBack is pinned by a test, and the README describes the endpoint. A from the forge doesn't recognise is left out of the answer rather than guessed at.

This update merges main again. The only conflict was in src/http/api.ts: main added takeBackRoutes (./api/take_back.ts) in the same two places this branch added rollbackRoutes. Both imports and both registrations are kept, rollbackRoutes(router) then takeBackRoutes(router), right after reportRoutes(router). The README and the OpenAPI route index merged without conflicts.

bun x tsc --noEmit passes, and the rollback, take-back, release-rollback and OpenAPI admin, route and coverage tests pass (318 tests, 0 failures).

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs