Repository navigation
perf(gateway): reduce operator authority check allocations - #179165
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
cede7db to
9462cd8
Compare
|
Codex review: needs maintainer review before merge. What this changesThis PR reduces repeated authority-check allocations and adds benchmarks for operator checks and Gateway turns. Example: [email protected] completes a Gateway turn with three tools and five committed message appends.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this maintainer-authored PR open as requested; the focused optimization preserves the reviewed authority contracts, with no actionable code finding. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherGateway authentication and plugin grants feed admitted agent authority, which checks model execution, tools, and transcript writes. flowchart TD
A[Authenticated operator] --> B[Gateway auth policy]
B --> C[Live plugin grant]
C --> D[Admitted run authority]
D --> E[Model and tool execution]
D --> F[Transcript write guards]
Technical reviewBest possible solution: Keep these owner-local optimizations if the declared performance comparison demonstrates the allocation gain without a material runtime regression. Do we have a high-confidence way to reproduce the issue? Current main visibly repeats principal serialization, eager profile preparation, and assertion construction; supplied Gateway counts establish execution frequency, but this read-only review ran no target code. Is this the best way to solve the issue? The smallest complete approach is to reuse immutable policy provenance and traversal functions at existing owners while preparing profiles only for callbacks; the diff follows that approach without caching live plugin answers. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 8a207cc23fb4. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +24, Tests +21, Docs +76, Other +517. Total +638 across 9 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes:
Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 21, 2026, 3:09 AM ET / 07:09 UTC. |
What Problem This Solves
Operator turns with access-policy plugins repeatedly allocate principal serialization, profile copies, and assertion closures while checking authority.
User Impact
Authority checks allocate less without delaying revocation or changing access decisions. Every live plugin, grant, role, config, and contextual-ingress check remains in place. No configuration or migration is required.
Why This Change Was Made
The auth-policy owner recognizes immutable policies it already issued for the same config; contextual-ingress policies remain uncached. Retained access grants avoid preparing a plugin profile unless a policy callback needs one. Composed source traversals and bound model checks reuse their functions rather than rebuilding them on every assertion. This reuses facts and functions, never a plugin's answer.
The apparent repeated checks around plugin/host callbacks are retained: callbacks can synchronously revoke the original authority. The generic plugin invocation/async context machinery is outside this change.
Evidence
Five real source-Gateway turns on baseline
ed8e68c9d15d1259b8d1155a4078f6759bb01e9band the final candidate produced identical live plugin-assertion counts:[1662, 1642, 1642, 1642, 1642]. Median: 1,642/turn, 547.33/tool completion, 328.4/committed message append. Each turn completed three tools and five message appends (six raw transcript updates). Admission/warmup is outside capture; the retained policy's authorize/resume callbacks remain zero in the turn window on both versions. These grouped runs establish count equivalence, not a timing comparison.Five concurrent pairs per scenario (25 pairs total, 50 processes), Node v26.8.2 on Linux x86-64. Each process performs 100,000 checks after 20,000 warmups, with HeapProfiler sampling at 32 KiB including collected objects. Baseline is
ed8e68c9d15d1259b8d1155a4078f6759bb01e9b; candidate is that source plus the final runtime changes. The same benchmark harness runs on both sides; the four changed runtime files are identical in the rebased PR head. Both sides share a start deadline under the exclusive measurement lock; A/B CPU halves 96–111 and 112–127 swap each pair.Changes are the median of each pair’s
(candidate − baseline) / baseline, not the ratio of column medians. Auth-policy recapture is below sampling resolution: candidate range 0–0.33 bytes/check, rather than a claim that every process allocates literally zero bytes.The predeclared 60% allocation target was not met by the composed guards: bound model execution improves 56.2%, admission 40.0%, and transcript guards 38.0%. The bound-model result is a 2.28× reduction in median allocated bytes; its remaining allocation is close to the retained-plugin check itself. Generic plugin invocation/frame allocations are deliberately unchanged. Retained-grant timing is neutral (+0.6% paired median, range −9.2% to +6.4%); every other probe improves elapsed and main-thread CPU time.
All 50 processes rejected a revoked plugin grant on the next check outside capture. Each composed probe still performs one auth-policy check, one resume-owner check, and one live plugin assertion per invocation. No live check or plugin answer is memoized.
Timing, allocation spread, load, and reproduction
Times below include the allocation profiler and are owner-call measurements, not end-to-end turn latency. Values are median [minimum–maximum]; with five samples, nearest-rank p95 is the maximum.
Recorded
mpstat -P 96-127context: mean observed CPU utilization (%) by pair, including benchmark CPU. All pairs are retained, including higher-load admission samples; load does not invalidate the concurrent comparison.Owner workload, invoked separately on each side with the same start timestamp:
Repeat for all five scenario names in the script. The real-turn count recipe is in Operator authority allocations.
Focused validation: 126 tests passed across the five files below; after the final lazy-profile adjustment, the five original-grant recovery/revocation cases passed again. Changed-file oxfmt and lint, one
pnpm tsgo:core(native tsrs), andgit diff --checkpassed. Independent Codex review found no actionable P0–P2 findings, including a follow-up review of the final profile sharing. CI run 39558320084 passed on9462cd879218321bd347e7e698aef50773aebae1; requiredopenclaw/ci-gatechecks are successful. A later rebase conflict only combined the benchmark launcher’s new optional memory preload with the auth-mode argument; runtime code is unchanged and the resolved script passed formatting/lint.src/gateway/server/ws-origin-policy.test.tssrc/config/sessions/session-source-authority.test.tssrc/gateway/operator-run-recovery.test.tssrc/gateway/operator-role-policy.test.tssrc/agents/admitted-run-context.test.tsRuntime production delta: +24 lines (+37/−13). This growth records same-config policy provenance without a cast and retains reusable traversal functions; eager serialization/copies/closures disappear from repeated checks. The requested non-test, non-
docs/diff is +617 net lines: +24 runtime, +517 benchmark tooling, +76 benchmark documentation. The larger diff is required to make the plugin-enabled owner and real-turn reproduction rerunnable; it adds no production capability or test-only runtime hook. Serialization is still required for fresh principals, serialized recovery policies and contextual ingress.The tiny plugin-enabled fixture proves the non-owner/tool path, not deployment capacity. The full deployment-shaped Gateway A/B and overall GC effect remain the lead's pre-merge checks. This PR is intentionally left open.
Recheck trace and proof scope
tool-authority.runtimechecks admitted authority directly and again through ordinary host capabilities. Reply checks add an explicit operator assertion; registration composes both (at least three operator checks, ordinarily five with host capabilities). Transcript rewrite composes outer/inner write admission and captured transcript guards. These are source-derived lower bounds, not per-turn measurements.Recovery checks current identity/config before and after policy callbacks, and rechecks the original grant after other policy callbacks. Existing regression tests cover a callback revoking the original grant and role changes during source assertions. A repeated composition frame alone does not prove a duplicated leaf check.
The owner microbenchmark uses a real registered synthetic access-policy plugin and non-owner identity; it measures isolated owner calls, not storage or complete model turns. The opt-in Gateway driver separately counts real
chat.send --stress-toolsturns. Its per-tool/per-append figures normalize all assertions in the turn window and do not attribute assertions to individual effects.