Skip to content

improve(gateway): avoid targetless session cache allocations - #127790

Merged
steipete merged 1 commit into
mainfrom
codex/perf-gateway-rpc-auth-context
Aug 22, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-gateway-rpc-auth-context

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Ordinary authenticated Gateway RPCs without a session target, such as status, allocated two empty session lookup maps during authorization even though they never performed a session lookup.

Why This Change Was Made

The authorization owner now initializes its request-scoped cache pair only on the first actual target resolution. Requests with targets still share exactly one initial freshness epoch, and commit-time guards continue to use separate fresh cache epochs.

User Impact

Targetless Gateway RPC authorization performs less allocation work with no protocol, authorization, output, error, or cache-freshness change.

Evidence

  • Exact current-main differential over 100,000 targetless calls: 200,000 map allocations before, 0 after.
  • Successful result and getter trace stayed exact: {"error":null} and key>sessionKey>keys>sessionKeys>agentId>agentId.
  • A hostile sessionKeys getter retained the exact thrown Error identity and getter order.
  • Original owner benchmark: 1,675.4 ns/call → 1,514.3 ns/call median across 7 × 1,000,000-call samples (9.62% lower).
  • Focused authorization/cache/routing coverage: 478 tests passed.
  • node scripts/check-changed.mjs -- src/gateway/session-sharing.ts: passed.
  • Build note: every non-UI full-build phase passed. The first local UI size gate was 9 bytes over because the long local branch name is embedded in the browser artifact; the exact base and candidate with stable GIT_BRANCH=main metadata both passed the UI budget.
  • Exact-current Codex gpt-5.6-sol/high autoreview: clean, correct 0.99.

@clawsweeper

clawsweeper Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS maintainer Maintainer-authored PR labels Aug 22, 2026

Copy link
Copy Markdown
Contributor Author

Land-ready evidence for exact head 5957e6e600e5b090cc56ea821bec5874dedcdd4f:

  • Root cause: resolveSessionMutationAuthorization eagerly created one session store/discovery cache pair (two empty Maps) before knowing whether an authenticated RPC had any session target.
  • Owner fix: initialize that pair only when resolveAuthorizedTarget first runs; all initial direct-incognito and mutation targets still share one freshness epoch, while assertCurrent and assertTargetCurrent retain their fresh commit-time lookups.
  • Deterministic current-base proof over 100,000 ordinary targetless calls: 200,000 map allocations before, 0 after.
  • Exact behavior proof: success remained { "error": null } with getter trace key>sessionKey>keys>sessionKeys>agentId>agentId; a hostile sessionKeys getter retained the exact thrown Error identity and trace key>sessionKey>keys>sessionKeys.
  • Owner benchmark: 7 × 1,000,000 calls, median 1,675.4 ns/call → 1,514.3 ns/call (-9.62%).
  • Focused authorization/cache/routing coverage — 478 tests passed.
  • node scripts/check-changed.mjs -- src/gateway/session-sharing.ts and git diff --check — passed.
  • Full non-UI build phases passed. The local UI budget's 9-byte overage was branch-name metadata only: exact base passed at 345,000 bytes and candidate with stable GIT_BRANCH=main metadata passed at 345,013 bytes.
  • Bundled exact-current Codex gpt-5.6-sol/high autoreview — correct (0.99), no actionable findings; TruffleHog clean.
  • Native scripts/pr review artifacts — validated READY FOR /prepare-pr, 0 findings, behavior sweep passed.
  • Exact-head CI — run 32555500261, 176 jobs, green.
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 127790 — passed in hosted_exact_or_recent_parent mode by reusing the exact-head hosted gates; no separate Testbox run was launched, and the remote tree equals the reviewed head.
  • Production LOC: +2/-2, net 0; tests: +0/-0. No API/config/protocol/authorization policy change or new helper/seam.

No implementation-coupled global-Map counter test was committed; existing semantic owner/sibling coverage plus the deterministic external allocation differential protect the contract without pinning implementation. No live proof is needed for this internal allocation-only change. The latest ClawSweeper entry is still its in-progress placeholder and contains no Rank-up moves: items to apply or skip.

@steipete
steipete merged commit 9d33b0b into main Aug 22, 2026
231 checks passed
@steipete
steipete deleted the codex/perf-gateway-rpc-auth-context branch August 22, 2026 06:02

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants