caelum0x f7bdcf867f
Some checks failed
ci / cargo test (abacus) (push) Has been cancelled
ci / cargo test (agent-runtime) (push) Has been cancelled
ci / cargo test (context-passport) (push) Has been cancelled
ci / cargo test (game-economy) (push) Has been cancelled
ci / cargo test (ledgerline) (push) Has been cancelled
ci / cargo test (mint) (push) Has been cancelled
ci / cargo test (rago) (push) Has been cancelled
ci / cargo test (relay) (push) Has been cancelled
ci / cargo test (toolgate) (push) Has been cancelled
ci / cargo test (turnstile) (push) Has been cancelled
ci / cargo test (vend) (push) Has been cancelled
ci / node suites (push) Has been cancelled
feat(RIT-ACT): server-side agent-push enforcement + no-paste approvals UI
Gap 1 (server-side enforcement): a raw `git push --force`/branch-delete made
with an agent-session token, bypassing `rit push`'s own client-side hold,
landed directly before this change. New apps/rithub-agent-gateway sits in
front of the one real git-smart-HTTP write route (POST .../git-receive-pack,
Caddy handle block on railyard-box) as a pkt-line-aware reverse proxy: a
token the server already knows is agent-session-attributed (marked via the
new POST /approvals/api/agent-session/mark, called by every `rit` command
when RITHUB_AGENT_SESSION_ID is set) doing a delete or a push to a
protected branch is held into refs/pending/<id> and registered as a real
approval, same ledger/code path rit push already uses -- the client gets a
clean `ng ... held for approval` rejection. Every other push (human tokens,
unmarked tokens, agent pushes to non-protected branches) is a pure
byte-for-byte passthrough, zero behavior change.

Gap 2 (no pasted tokens): /approvals now uses the signed-in rithub.dev
session cookie plus a one-time real OAuth2 (PKCE, same "Rit CLI" engine
app `rit login` uses) authorize step -- no owner/repo fields, no token
field, anywhere on the page. GET /approvals/api/mine lists pending
approvals across every repo the signed-in human can push/admin; approve/
deny are one click each (POST /approvals/api/mine/:id/approve|deny).

Live-verified on rithub.dev with a fresh mail.tm test account: plain
`git push --force` and `git push origin :branch` with an agent-session
token both held (target ref/branch untouched); a human-token push landed
directly, unaffected; the signed-in web page approved the exact staged
commit and denied the delete leaving the branch untouched; full route
regression + clone/fetch stayed 200; no token/hash/email leaked in any API
response. Test account and repo deleted after.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-12-01 14:50:55 +03:00

82 lines
4.4 KiB
Markdown

# rithub-agent-gateway
Server-side enforcement for agent push approval (RIT-ACT gap 1, 2026-12-01).
Before this service existed, the hold logic that stops an agent-session
force-push/branch-delete/protected-branch push lived ONLY client-side in
`rit push`/`rit branch delete` (`packages/rithub-cli/src/push-approval.mjs`)
-- a raw `git push`/`git push --force`, made with the same credential but
bypassing `rit` entirely, landed directly. This service closes that: it
sits in front of exactly one real git-smart-HTTP route,
`POST /{owner}/{repo}(.git)/git-receive-pack`, and holds any such push made
with a token the server already knows is agent-session-attributed.
## How a token becomes "agent-session-attributed"
The wire protocol (HTTP Basic/`token`/`Bearer` auth on a `git push`) cannot
distinguish a human's PAT from an agent's -- both are ordinary Rithub
access tokens. So `rit` tells the server itself: every `rit` command, when
`RITHUB_AGENT_SESSION_ID` is set, calls
`POST /approvals/api/agent-session/mark` (added to `apps/rithub-approvals`,
`src/agentSessionTokens.mjs`) with that token as the bearer -- only a
SHA-256 hash of the token is ever stored. This gateway reads that same
ledger (`src/agentLedger.mjs`, read-only, same `RITHUB_APPROVALS_DATA_DIR`)
on every `git-receive-pack` request. An unmarked token (every human PAT,
and an agent token that has never once run a marking `rit` command) is
treated as human -- zero behavior change.
## What gets held
Same semantics as `rit push`'s own `protectedBranches` check: a push whose
target is a branch **delete**, or whose target is a **protected branch**
(the repo's own default branch, or `main`/`master`, or
`RITHUB_PROTECTED_BRANCHES`) is held -- regardless of whether the client
passed `--force` (the wire protocol carries no such flag to inspect).
- A branch delete is never forwarded to the engine at all -- the approval
is registered directly and the delete never happens until a human
approves it.
- A push is relayed to the engine with ONE pkt-line rewritten: the target
ref becomes `refs/pending/<id>` (the exact same unprotected staging ref
`rit push`'s own `stageHeldPush` uses) instead of the real branch, and
the declared old-rev becomes all-zero (new ref). The packfile bytes
themselves are never parsed or modified -- only this one header line is,
matching the architecture's "the proxy only inspects/rewrites refs, it
never re-implements pack-writing" rule
(`plans/rithub-docs/architecture/00-overview.md` §3).
- The approval this creates/confirms uses `apps/rithub-approvals`'s own
`/approvals/api/create` route (same ledger, same lock, same code path
`rit push` itself calls) -- an approval made this way is indistinguishable
from one the CLI would have made, and lands with exactly the approving
human's own permissions, same as every other approval.
- The git client gets back a real, synthesized `git-receive-pack` result:
`ng refs/heads/<branch> held for approval, see <url>` -- so `git push`
itself prints the hold reason and exits non-zero, same as any other
server-side rejection.
Everything else -- info/refs, git-upload-pack (clone/fetch), the web UI,
every REST API route, every human push, and an agent-session push to a
non-protected branch -- is a pure byte-for-byte reverse proxy to the real
engine. No parsing, no buffering, no behavior change.
## Known boundary (not closed this pass)
A raw `git push --force` by an agent-session token to a **non-protected**
branch is not held (the wire protocol has no "this was `--force`" bit to
read; true force/non-fast-forward detection needs either walking the
incoming packfile's ancestry or the engine's own `pre-receive` hook
calling back into a hold-check -- the `agent-gateway` Go service the
architecture doc describes, a larger, separate build,
`plans/rithub-docs/architecture/00-overview.md` §9 Phase 2). The two real,
closed cases are exactly the two described in the task this service
shipped for: a force-push/plain-push to a protected branch, and a branch
delete.
## Deploy
Same convention as `rithub-approvals`/`rithub-device-auth`: rsync this
directory to `/home/deploy/apps/rithub-agent-gateway` on `railyard-box`,
`pm2 start ecosystem.config.cjs`, add the Caddy `handle` block (matches
`method POST` + `path_regexp ^/[^/]+/[^/]+(\.git)?/git-receive-pack$`)
routing to `127.0.0.1:3363`, placed before the engine's own catch-all
`reverse_proxy 127.0.0.1:3350`, then `caddy reload`.