fix(cloud-agent-next): align wrapper runtime generation with the lease - #6080
Conversation
Modern-runtime-authorization sessions could wedge permanently after the wrapper restarted: every follow-up failed pre-dispatch with "Runtime credential proxy grant is unavailable". The session DO's runtime wrapper generation and the physical wrapper lease instance generation were independent counters. A restart cleared the stale runtime identity with incrementGeneration:true and then allocateWrapperRuntimeState incremented again (+2), while the lease advanced by one (+1). runtimeProxyFence() requires the two to be equal, so the grant could never be issued and no wrapper was ever restarted; clearAllocatedWrapperRuntimeState incremented again on each failed delivery, so the gap only grew. Make the lease the single source of truth for the delivered generation: AgentRuntime.send passes the allocated instance generation into allocateWrapperRuntimeState, and the cleanup paths stop bumping the counter independently. Stale connections stay fenced because clearing removes the connection and run identity. Add a regression test that drives the real AgentRuntime.send allocation and CloudAgentSession.issueRuntimeCredentialProxyGrant path. It fails on the previous code (runtime 9 vs lease 2) and passes after the fix.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryReviewed the wrapper runtime-generation/lease alignment fix and its tests; the source change correctly makes the physical lease the single source of truth for the delivered generation, and stale connections remain fenced by the freshly minted connection/run identity. No changed-line defects found. Files Reviewed (6 files)
Note: Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0 Review guidance: REVIEW.md from base branch |
Problem
Modern-runtime-authorization (scoped credentials) sessions could wedge permanently after the wrapper was restarted. Every follow-up message failed pre-dispatch with
Runtime credential proxy grant is unavailable, and no wrapper was ever restarted.Root cause
The session DO's
runtime.wrapperGenerationand the physical wrapper leaseinstance.instanceGenerationwere independent counters. On a restart with a stale active identity,AgentRuntime.sendcleared the identity withincrementGeneration: trueand thenallocateWrapperRuntimeStateincremented again (runtime +2), while the lease advanced by one (+1).CloudAgentSession.runtimeProxyFence()requires the two to be equal, soissueRuntimeCredentialProxyGrantalways returnednull.clearAllocatedWrapperRuntimeStateincremented again on each failed delivery, so the gap only grew and the session could never recover.Fix
AgentRuntime.sendpasses the allocated physical instance generation intoallocateWrapperRuntimeState, so a newly minted runtime identity adopts the lease generation.clearAllocatedWrapperRuntimeStateno longer increments the generation, and the clear that precedes reallocation insenddrops its now-dead increment.wrapperConnectionId/wrapperRunId, and every allocation mints a new connection/run identity.Tests
agent-runtime-runtime-proxy-grant.test.tsdrives the realAgentRuntime.sendallocation path and the realCloudAgentSession.issueRuntimeCredentialProxyGrantover shared storage. It fails on the previous code (expected 9 to be 2) and passes after the fix.Verification (run from
services/cloud-agent-next)pnpm test— 211 files, 6086 passed, 3 skippedpnpm test:integration— 32 files, 660 passedpnpm typecheck— passpnpm lint— 0 warnings / 0 errorspnpm format:check— cleanNot verified end to end: this change is validated at the allocation/grant contract level. A live sandbox-restart check against production/Axiom is still required after deploy.