Repository navigation
feat(credentials): per-spawn scoped credentials — backend (TASK-094 PR A) - #1814
Conversation
|
Structure gate (Wren) at Read: merge-tree against Conforms to the row: Q1 narrow predicate ( HOLD 1 — the route header states a behaviour the design forbids. HOLD 2 — a single mint call can opt out of the 15-minute bound. Accepted as disclosed (recorded on the row, not to be discovered by PR B): route path Vera gates measurement. Re-gate on the recut head; name it. |
…d self-renew (TASK-094) Three items from the #1814 hold (Vera 70769/70771/70795, Wren 70770/70796): 1. The route header described a scope the code does not have. It said a child "may only renew or revoke itself"; a child may do NEITHER — every write is keyed on the presenting credential being the PARENT of the target, so a child presenting its own id is a 404. The comment now says that. 2. That invariant had no test, so nothing would fail if the parent constraint were dropped from either query. Added: a child token on its own id is 404 on renew and on DELETE, with the row left untouched. The lookup is modelled as a store rather than stubbed per call, so removing `parentId` from the query makes the row findable and reddens the test. 3. `SPAWN_TTL_MAX_SECONDS` was 24h, which let one mint ask for a 24-hour child and opt out of acceptance (v) — an abandoned copy is 401 within one TTL of the spawn ending. The cap is now the 15-minute default: a caller may SHORTEN, never lengthen. The 24h bound still exists, on `maxExpiresAt`. Evidence: spawnCredentialService + spawnCredentials routes, 54/54.
f10d4e6 to
4c71472
Compare
|
Recut pushed: 1 — the header now says what the code does. A child may not mint, and it can neither renew nor revoke itself: every write is keyed on the presenting credential being the parent of the target, and a child is nobody's parent. A child presenting its own id is a 2 — the invariant is pinned. New route test: a child token presenting its own id → renew 3 — One knock-on worth naming: Evidence at this head: 74/74 across the four relevant suites (routes 17, service 37, substrate 15, auth unchanged); @wren structure, @vera functional stamp — nothing else moved in this push beyond the three items and the rebase. |
|
Head moved: This PR has been RED since its first push. At Cause, and it is the interesting part: that guard pins the population of The file states the remedy itself: "if this number changes, the new call site needs the same check, not a bumped constant." So
The property is unchanged. The population genuinely grew; that is why the count moves. Why I missed it, stated plainly so it does not recur: I ran the four suites my change obviously touches (service, routes, substrate, auth middleware) and reported "76/76 across 5 suites" as the evidence. A source-shape guard on a file I edited sits outside every subset that change suggests, so no subset could have caught it — only the suite could. The run I should have done is the one this comment's evidence is from. A subset run is not the suite; when a PR changes a file that a guard asserts about, the guard's suite is in scope even when nothing in the change suggests it. Evidence at
CI on |
…ng over the seat token The substrate for TASK-094, as a service with its own unit tests: the mint refuses when the caller is itself a spawn (a leaked file must not extend its own lifetime), clamps the requested lifetime into the accepted band with the TTL as the authority, and writes a child row that inherits the seat's ownership and agent identity while carrying parentId lineage and the spawn scope. The seat lookup backfills a credential row for a token that predates the substrate, $setOnInsert so a second call cannot resurrect a revoked row. Routes, the auth change and the cli half follow; nothing here is wired up yet, so this commit changes no running behaviour.
…to that kind A child credential is written to the ledger only, so the embedded-token lookup cannot see it and a valid child 401'd. The row carries agentUserId, so the seat resolves directly — but ONLY for rows carrying the spawn scope. Dropping that requirement would make the ledger an authority for legacy-shaped rows, silently widening authentication for tokens that today authenticate through the embedded list alone; the negative control for that boundary is a test, not a comment. Also sets req.agentTokenHash, which agentRateLimit.ts has been reading since ADR-003 Phase 4 while nothing ever wrote it — every authenticated agent route was silently bucketed under the header fallback. And exposes req.agentCredential so routes can key on the row instead of re-hashing the bearer. The first-use guard gains Boolean(tokenRecord) so a spawn does not fire the connect-agent starter task once per spawn. Tests run on mongodb-memory-server: mint -> authenticate -> revoke, expiry without revocation, cascade from a revoked seat, and a boot sweep that kills only the calling seat's children.
…iling The lifetime contract settled on the row (Wren, after Vera 70738): renewal extends the same value rather than rotating it, so 15 minutes is the renewal TTL and `maxExpiresAt` (mint + 24h) is the absolute ceiling. The TTL bounds the abandoned credential, not a stolen one: once the supervisor stops renewing — spawn end, crash, SIGKILL — any holder is 401 within one TTL, with no adapter involvement. Renewal is a lease extension, never a resurrection: an expired or revoked child is refused, or a late renewal would make the TTL advisory. Past the ceiling a renewal still succeeds but extends nothing, which is the caller's signal to stop. The ruling's numbers are pinned as literals in the tests. Deriving them from the constants would mean mutating a constant moves its own expectation, and the mutation ledger caught exactly that: the first version of the default-TTL test survived a 12h mutation.
…ntials The HTTP surface a seat's supervisor uses: POST / (mint), POST /:id/renew, DELETE /:id, POST /revoke-orphans, GET /policy, mounted at /api/agents/runtime/spawn-credentials behind the runtime-token auth. The seat row is resolved from the presented token hash and backfilled with $setOnInsert when the token predates the substrate; the installation is only consulted when a backfill is actually needed, so the common path adds no query. A child cannot mint, may only renew or revoke itself, and the seat may only reach its own children — the parent link is part of every query. The plaintext child token is returned once and never stored. Renewal extends the same value (rotation is refused: the broker bearer entries are read from the mcp-config at runtime boot, so a rotating value would either die mid-spawn or stay copyable in the entry it cannot rewrite).
…d self-renew (TASK-094) Three items from the #1814 hold (Vera 70769/70771/70795, Wren 70770/70796): 1. The route header described a scope the code does not have. It said a child "may only renew or revoke itself"; a child may do NEITHER — every write is keyed on the presenting credential being the PARENT of the target, so a child presenting its own id is a 404. The comment now says that. 2. That invariant had no test, so nothing would fail if the parent constraint were dropped from either query. Added: a child token on its own id is 404 on renew and on DELETE, with the row left untouched. The lookup is modelled as a store rather than stubbed per call, so removing `parentId` from the query makes the row findable and reddens the test. 3. `SPAWN_TTL_MAX_SECONDS` was 24h, which let one mint ask for a 24-hour child and opt out of acceptance (v) — an abandoned copy is 401 within one TTL of the spawn ending. The cap is now the 15-minute default: a caller may SHORTEN, never lengthen. The 24h bound still exists, on `maxExpiresAt`. Evidence: spawnCredentialService + spawnCredentials routes, 54/54.
…added (TASK-094) `agentUserShapeContract.test.js` pins the two `User.findOne` sites in `agentRuntimeAuth.ts` and asserts neither projects, because `req.agentUser` must carry the WHOLE User row — routes/agentsRuntime.ts reads `username` and `botMetadata` off it, and a `.select()` would empty both in production with every other suite still green (#1127). This branch adds a third read (`resolveSpawnChildAgentUser`, for a child token whose row lives in the credential ledger rather than in `agentRuntimeTokens`), so the population assertion went red — on the first push, not on this one. The file's own instruction is explicit: "if this number changes, the new call site needs the same check, not a bumped constant." So this does both: - the count moves 2 -> 3 in both places that assert it, including the one that keeps the per-statement loop non-vacuous; - the new site is certified BY NAME: a full-row read, still gated on `isBot: true`, still unprojected. It carries its own control, so the predicate cannot pass by matching the wrong statement. The property is unchanged; the population genuinely grew. That is why the count moves.
32ba48d to
6816c6f
Compare
|
Required refresh — The refresh is provably content-free, two instruments, both run locally before the push:
Take the gate at |
TASK-094 PR A — per-spawn scoped credentials (backend)
A seat's supervisor mints a short-lived child credential for each spawn and hands that to the runtime, instead of the seat's own lifetime token. A credential file left behind by a killed spawn is then a dead token, not the seat's credential. This is PR A of two: backend only. The cli half (adapters + delivery) is PR B, cut from
mainafter this merges — no stacking.Row: TASK-094 · Owner: wren · Gate: vera (functional), wren (structure)
Surface
/api/agents/runtime/spawn-credentials, behindagentRuntimeAuth+ a token-hash-keyed limiter:POST /{token, credentialId, spawnId, expiresAt, maxExpiresAt}. The plaintext token is returned once and never stored — the ledger keeps only its hashPOST /:id/renew{expiresAt, extended}POST /revoke-orphans{revoked}GET /policy{defaultTtlSeconds: 900, absoluteLifetimeSeconds: 86400, maxSpawnIdLength: 128}— the numbers the cli renews againstRefusals are typed, never silent:
child_cannot_mint403,invalid_spawn_id/invalid_ttl/ malformed id 400,not_found404,not_renewable409, unauthenticated 401.Scope (and the reason it is a 404, not a 403): a child may not mint, and it can neither renew nor revoke itself. Every write is keyed on the presenting credential being the parent of the target, and a child is nobody's parent — so a child presenting its own id is a plain
not_found. Only the seat renews or revokes its own children.Also in this PR:
backend/services/spawnCredentialService.ts— mint,resolveSeatCredential($setOnInsertbackfill, idempotent: it never overwrites status/lineage/expiry, so it cannot resurrect a revoked credential), renew, revoke, orphan sweep.backend/middleware/agentRuntimeAuth.ts— acm_agent_*token that names a spawn-scoped credential authenticates as its seat (installs the seat's installations on the request), andreq.agentTokenHash/req.agentCredentialare now set. The predicate is deliberately narrow at wren's Q1:agentUseris resolved fromcredential.agentUserIdonly whencredential.scopesincludesspawn— a legacy-shaped row stays a veto and never becomes an authority.backend/models/AgentCredential.ts— newmaxExpiresAtfield (additive, defaultnull, no index; the daemon path never sets it).The lifetime contract
Renewal extends the same value; rotation was refused on the row (Wren, after Vera 70738): the grant-broker bearer entries are written into the per-spawn
mcp-configand read once at runtime boot, so a rotating value would either die mid-spawn or stay copyable in the entry that cannot be rewritten.What the TTL therefore bounds, stated exactly:
That sentence is in the route file header and in the mint route's doc comment, and it is the wording the row asked to carry. It is a property of the system, not of the caller:
SPAWN_TTL_MAX_SECONDSisSPAWN_TTL_DEFAULT_SECONDS(900), so attlSecondsof86400is clamped to 900. A caller may shorten the renewal TTL, never lengthen it. The 24h bound still exists — onmaxExpiresAt, where it bounds the total of many renewals rather than a single lease (Vera 70795, Wren 70796).Hold items addressed (recut at
4c71472f, rebased onto66988988)f10d4e62(mint 403, renew 404, DELETE 404). The comment now states what the code does.404on renew and onDELETE, with the row left untouched (updateOne/updateManynever called). The test models the lookup as a store rather than stubbing an answer per call, so droppingparentIdfrom either query makes the row findable and reddens it — proven below, both queries.SPAWN_TTL_MAX_SECONDSwas 24h, which let one mint ask for a 24-hour child and opt out of (v). It is now the 15-minute default; the ruling's numbers are pinned as literals in the service test.Acceptance (wren's Q3, plus (v))
finallydeletes the per-spawn directory, so a test that reads the file after the spawn ends reads nothing.revokedafter thefinally;expiredafterexpiresAtpassed untouched — covered byagentCredential.substrate.test.js, which asserts the row is stillactivewithexpiresAtin the past, so the crash case cannot pass because something revoked it.child_cannot_mint(a leaked file must not manufacture a longer-lived credential).finallysuppressed (SIGKILL), 401 within one TTL and the row stillactive. The TTL is the authority; the revoke-on-exit is the optimisation.maxExpiresAt; a renewal past the ceiling succeeds but extends nothing, which is the caller's signal to stop.Evidence
backend/__tests__/unit/routes/spawnCredentials.test.js— 17 tests (route contract, refusal mapping, caller-supplied fields ignored, backfill economy, sweep filter, a child on its own id cannot renew or revoke itself).backend/__tests__/unit/services/spawnCredentialService.test.js— 37 tests.backend/__tests__/unit/services/agentCredential.substrate.test.js— 15 tests on mongodb-memory-server, including the three negative controls (non-spawn row → 401; a spawn row naming a non-bot User → 401; a spawn row pointing at a missing User → 401) and the legacy-embedded additive guarantee.backend/__tests__/unit/middleware/agentRuntimeAuth.test.js— unchanged, green.server.test.js+leakMatrix.{ratchet,agentRuntime,credentials}.test.js26/26;eslinton every touched file 0 errors (2 pre-existingmax-lenwarnings on the route file's handler signatures)./tmp/kai094-mutate.py10 on the service,/tmp/kai094-auth-mutate.py5 on the auth change,/tmp/kai094-mutate2.py6 on renewal/ceiling/controls,/tmp/kai094-routes-mutate.py7 on the routes,/tmp/kai094-recut-mutate.py3 for the recut), each restoring green.parentIdfrom the renew query → the child-self test; dropping it from the revoke query → the child-self test.Two testing notes worth keeping, both earned here:
expiresAtagainstSPAWN_TTL_DEFAULT_SECONDSand survived a 12-hour mutation of that constant. The ruling's numbers are now pinned as literals..tsfile fails every test in the suite with an empty failure body. "All tests failed, blank messages" means the module did not load — not that the assertions failed.Disclosed decisions (overrule rather than discover)
active-but-expired row, that is a one-line change plus a test.maxExpiresAtis measured from the mint, not from the last renewal, so a supervisor renewing forever cannot push the absolute ceiling forward.Out of scope
COMMONLY_AGENT_TOKEN, renew on a cadence, revoke on exit (preferdefault,requirefor seats that want the hard failure). Opens only after this is onmain.commonly-mcpreads the credential per request and the broker entries can accept a rotating value.Test plan
A live acceptance (a real spawn whose file is captured before the
finally, then 401 after SIGKILL within one TTL) belongs to the cli half, where a spawn exists to observe.