Skip to content

[Fix] Lazy compile protocol validators - #82064

Merged
steipete merged 2 commits into
openclaw:mainfrom
samzong:fix/gateway-protocol-validator-lazy
May 15, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
samzong:fix/gateway-protocol-validator-lazy

Conversation

@samzong

@samzong samzong commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: importing src/gateway/protocol/index.ts eagerly compiled every AJV protocol validator.
  • Why it matters: consumers that only need protocol constants or a small validator subset paid the full cold-import CPU and RSS cost.
  • What changed: protocol validators now compile on first use and then reuse the compiled AJV validator while preserving .errors compatibility.
  • What did not change: no schema changes, no protocol method semantics changes, no ACPX changes, no standalone generated validators.

Change Type

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope

  • Gateway / orchestration
  • API / contracts

Linked Issue/PR

Real behavior proof

  • Behavior addressed: Gateway protocol validators now lazy-compile on first use instead of compiling every AJV schema during cold import.
  • Real environment tested: macOS 26.4.1 local source checkout plus Blacksmith Testbox Linux via Crabbox (tbx_01krndx4fdwmd5yw2j64cdj5nt, tbx_01krne47hfznt4qtwn2t36c416, tbx_01krne7bsbzhwrr4dzt16zp08t).
  • Exact steps or command run after this patch:
    • node --import tsx --input-type=module <inline AJV compile counter script>
    • node scripts/run-vitest.mjs src/gateway/protocol/index.test.ts src/gateway/server.ios-client-id.test.ts
    • node scripts/crabbox-wrapper.mjs run --provider blacksmith-testbox --blacksmith-org openclaw --blacksmith-workflow .github/workflows/ci-check-testbox.yml --blacksmith-job check --blacksmith-ref main --timing-json --shell -- <baseline-vs-PR benchmark + targeted protocol tests>
    • node scripts/crabbox-wrapper.mjs run --provider blacksmith-testbox --blacksmith-org openclaw --blacksmith-workflow .github/workflows/ci-check-testbox.yml --blacksmith-job check --blacksmith-ref main --timing-json --shell -- "node scripts/run-vitest.mjs run --config test/vitest/vitest.e2e.config.ts src/gateway/server-network-runtime.e2e.test.ts test/gateway.multi.e2e.test.ts"
    • node scripts/crabbox-wrapper.mjs run --provider blacksmith-testbox --blacksmith-org openclaw --blacksmith-workflow .github/workflows/ci-check-testbox.yml --blacksmith-job check --blacksmith-ref main --timing-json --shell -- "pnpm check:changed"
  • Evidence after fix: Console output from Blacksmith Testbox tbx_01krndx4fdwmd5yw2j64cdj5nt and tbx_01krne47hfznt4qtwn2t36c416:
BENCH_RESULT origin-main {"samples":30,"summary":{"importMs":{"median":213.48828899999998},"rssDeltaMB":{"median":51.69140625},"compileCallsAfterImport":{"median":159},"compileMsAfterImport":{"median":120.55457800000016},"compileCallsAfterSingle":{"median":159},"compileCallsAfterAll":{"median":159},"validators":{"median":159}}}
BENCH_RESULT pr-82064 {"samples":30,"summary":{"importMs":{"median":92.903316},"rssDeltaMB":{"median":23.99609375},"compileCallsAfterImport":{"median":0},"compileMsAfterImport":{"median":0},"compileCallsAfterSingle":{"median":1},"compileCallsAfterAll":{"median":159},"validators":{"median":159}}}
RUN v4.1.6 /home/runner/_work/openclaw/openclaw
Test Files 3 passed (3)
Tests 75 passed (75)

src/gateway/server-network-runtime.e2e.test.ts (1 test) passed
  bootstraps env proxy dispatching when the gateway starts directly

test/gateway.multi.e2e.test.ts (1 test) passed
  spins up two gateways and exercises WS + HTTP + node pairing
  • Observed result after fix: protocol index import performed zero AJV compile calls; using one validator twice performed one compile call; compile-all still reached 159 validators; median import time dropped from 213.5ms to 92.9ms and median import RSS delta dropped from 51.7MB to 24.0MB on the same Testbox.
  • What was not tested: full pnpm test, full pnpm check, and Docker gateway-network lane were not run. Targeted Testbox E2E, changed gate, and protocol benchmark were run.
  • Before evidence: same-Testbox benchmark on origin/main showed 159 compile calls during import, median 213.5ms import time, and median 51.7MB RSS delta.

Root Cause

  • src/gateway/protocol/index.ts created an AJV instance and exported validators by running ajv.compile(...) at module top level.
  • Gateway and client paths import protocol constants and helpers from the same module, so validator compile work leaked into unrelated protocol imports.

Regression Test Plan

  • Coverage level that should have caught this: unit test.
  • Target test file: src/gateway/protocol/index.test.ts.
  • Scenario locked in: lazy validators compile on first use, reuse the compiled function, preserve .errors, and can all be proactively compiled in a smoke path.

User-visible / Behavior Changes

None. Protocol validation semantics stay the same; cold import CPU and RSS costs are deferred until validators are used.

Security Impact

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Human Verification

  • Verified scenarios: lazy import compile counter, single-validator reuse, compile-all smoke, protocol validator .errors compatibility, Gateway iOS client id validation consumer, Blacksmith Testbox gateway startup E2E, two-gateway WS/HTTP/node-pairing E2E, changed gate, TypeScript checks, lint, format, and build.
  • Edge cases checked: invalid validator input leaves readable .errors; valid input resets .errors to null; all exported validators can still be forced to compile during tests.
  • Not verified: full pnpm check, full pnpm test, and Docker gateway-network lane.

AI-assisted: Yes - Codex implemented and verified the change.

Risks and Mitigations

  • Risk: schema compile failures move from protocol module import time to first validator use.
    • Mitigation: src/gateway/protocol/index.test.ts includes a compile-all smoke test that forces every exported protocol validator to compile during test runs.
  • Risk: lazy wrappers could drift from AJV ValidateFunction error behavior.
    • Mitigation: tests assert .errors is readable after invalid input and resets to null after valid input.

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui gateway Gateway runtime size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 15, 2026
@steipete
steipete force-pushed the fix/gateway-protocol-validator-lazy branch from 00e354a to 1723501 Compare May 15, 2026 09:10
@steipete

Copy link
Copy Markdown
Contributor

Maintainer verification before merge.

Behavior addressed: Gateway protocol validators now lazy-compile on first use instead of compiling every AJV schema during cold import.
Real environment tested: local macOS checkout plus Blacksmith Testbox Linux via Crabbox.
Exact steps or command run after this patch:

  • local: node scripts/run-vitest.mjs src/gateway/protocol/index.test.ts src/gateway/server.ios-client-id.test.ts
  • local: git diff --check
  • Testbox tbx_01krndx4fdwmd5yw2j64cdj5nt: same-box 30-sample baseline-vs-PR protocol import benchmark, then node scripts/run-vitest.mjs src/gateway/protocol/index.test.ts src/gateway/server.ios-client-id.test.ts src/gateway/server-network-runtime.e2e.test.ts test/gateway.multi.e2e.test.ts
  • Testbox tbx_01krne47hfznt4qtwn2t36c416: node scripts/run-vitest.mjs run --config test/vitest/vitest.e2e.config.ts src/gateway/server-network-runtime.e2e.test.ts test/gateway.multi.e2e.test.ts
  • Testbox tbx_01krne7bsbzhwrr4dzt16zp08t: pnpm check:changed

Evidence after fix:

  • Protocol import median: 213.5ms on origin/main -> 92.9ms on PR, about 56.5% lower.
  • Protocol import RSS delta median: 51.7MB -> 24.0MB, about 53.6% lower.
  • AJV compile calls during protocol import: 159 -> 0.
  • First single validator use after import: 1 compile call on PR, then reused on second call.
  • Compile-all smoke: still compiles 159 validators successfully.

Observed result after fix: focused protocol validator tests passed, gateway direct-start E2E passed, two-gateway WS + HTTP + node-pairing E2E passed, and changed gate passed.
What was not tested: full pnpm test, full pnpm check, Docker gateway-network lane.

Thanks @samzong.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 15, 2026
@clawsweeper

clawsweeper Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: [Fix] Lazy compile protocol validators This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@steipete
steipete merged commit 9d51d2a into openclaw:main May 15, 2026
127 of 130 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via rebase merge onto main.

Gate/proof:

  • Local: node scripts/run-vitest.mjs src/gateway/protocol/index.test.ts src/gateway/server.ios-client-id.test.ts
  • Local: git diff --check
  • Testbox tbx_01krndx4fdwmd5yw2j64cdj5nt: baseline-vs-PR protocol import benchmark plus targeted protocol tests
  • Testbox tbx_01krne47hfznt4qtwn2t36c416: E2E gateway direct startup and two-gateway WS/HTTP/node-pairing tests
  • Testbox tbx_01krne7bsbzhwrr4dzt16zp08t: pnpm check:changed
  • GitHub checks: green before merge, including Real behavior proof, Critical Quality gateway/network runtime boundary, Security High shards, build artifacts, and changed-path checks

Landed commits:

Performance proof: median protocol import 213.5ms -> 92.9ms, import RSS delta 51.7MB -> 24.0MB, and AJV import-time compile calls 159 -> 0 on the same Blacksmith Testbox.

Thanks @samzong.

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

Labels

app: web-ui App: web-ui gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants