Skip to content

perf: lazy load daemon handlers and report bundle size#608

Merged
thymikee merged 6 commits into
mainfrom
codex/bundle-size-lazy-routing
May 30, 2026
Merged

perf: lazy load daemon handlers and report bundle size#608
thymikee merged 6 commits into
mainfrom
codex/bundle-size-lazy-routing

Conversation

@thymikee
Copy link
Copy Markdown
Member

@thymikee thymikee commented May 28, 2026

Summary

Lazy-load daemon handler, platform, and command execution boundaries behind command-catalog-owned routing groups, keeping request admission, locking, provider scoping, and generic platform fallback unchanged.

Split command metadata from executable command/runtime modules so CLI help and MCP tools/list can enumerate command schemas without pulling daemon projection/runtime code.

Add a lightweight size reporting script and same-repo PR workflow that reports raw JS, gzip JS, npm tarball/unpacked size, and top changed chunks as a PR comment.

Review follow-up: runtime now uses a command-catalog constant, matched handlers returning null raise an internal routing mismatch, the handler-chain test covers a session-family command, and the size script uses execFileSync for npm dry-run execution.

Scope: 64 source files touched. Scope expanded from daemon routing into command metadata and provider seams to keep CLI, MCP, and Node.js boundaries thin.

Total Benefits

Cold CLI startup paths from the initial lazy-routing pass, measured against the PR merge-base (5083d0456) and PR head (8582d0d02) from built dist output on the same local machine. Each row is a fresh node bin/agent-device.mjs ... process, 35 measured runs after 5 warmups.

Command Base median PR median Median delta Base p95 PR p95
--version 65.0 ms 34.8 ms -30.2 ms (-46%) 81.3 ms 71.9 ms
--help 65.3 ms 44.7 ms -20.6 ms (-32%) 93.5 ms 47.9 ms
help 65.3 ms 46.3 ms -19.0 ms (-29%) 92.5 ms 66.3 ms
help open 65.6 ms 44.2 ms -21.4 ms (-33%) 92.8 ms 47.8 ms
open --help 65.4 ms 45.2 ms -20.2 ms (-31%) 82.7 ms 70.5 ms
help snapshot 65.7 ms 45.2 ms -20.5 ms (-31%) 119.7 ms 68.2 ms
snapshot --help 65.9 ms 45.4 ms -20.5 ms (-31%) 132.1 ms 55.6 ms

Follow-up optimization pass measured against the previous PR head before the command metadata/platform split:

Path Before median After median Delta
--version 32.3 ms 31.4 ms -0.8 ms
--help 42.2 ms 40.4 ms -1.8 ms
help snapshot 43.1 ms 40.1 ms -3.0 ms
no command 62.7 ms 40.5 ms -22.2 ms
MCP tools/list 46.6 ms 36.1 ms -10.5 ms

Dependency graph impact from dependency-cruiser:

Graph path Before After
circular dependency edges 54 0
command-metadata.ts static closure 92 modules 32 modules
mcp/command-tools.ts static closure 93 modules 33 modules
utils/args.ts static closure 100 modules 44 modules

Size tradeoff versus the pre-lazy baseline:

Metric Diff
JS raw +14.2 kB
JS gzip +14.0 kB
npm tarball +7.8 kB
npm unpacked +14.2 kB
largest shared chunk -66.2 kB raw

This is a startup/runtime-boundary win rather than a total package-size win. The package grows slightly because previously shared code is split into lazy chunks, but hot CLI/MCP paths load less work.

Validation

Verified with pnpm format, pnpm check:quick, pnpm check:fallow --base origin/main, pnpm build, dependency-cruiser, focused request-handler-chain Vitest, focused batch/MCP/help Vitest, focused provider Vitest, smoke CLI tests, and pnpm size:markdown.

Previous validation on this PR also passed pnpm check:mcp-metadata.

pnpm check:unit still fails in this sandbox on pre-existing environment-shaped failures: local server listen EPERM, Swift script typecheck exits, and one child-process replay test.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 97f6f45227

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/size-report.mjs Outdated
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use the approved exec helper path for process execution

This new script imports spawnSync directly, but the repo's AGENTS.md Hard Rules say not to import raw spawn/spawnSync outside src/utils/exec.ts, and that plain .mjs scripts should prefer execFile/execFileSync instead. Keeping this as a direct spawn creates a second process-execution path outside the repo's audited helper conventions; switch this npm invocation to execFileSync/execFile or route it through an approved helper.

Useful? React with 👍 / 👎.

@thymikee thymikee force-pushed the codex/bundle-size-lazy-routing branch from 97f6f45 to 320343a Compare May 28, 2026 15:36
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-05-30 14:49 UTC

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

Size Report

Metric Base Current Diff
JS raw 1.0 MB 1.1 MB +30.3 kB
JS gzip 325.1 kB 352.7 kB +27.7 kB
npm tarball 434.1 kB 449.6 kB +15.6 kB
npm unpacked 1.5 MB 1.5 MB +30.4 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 87.6 ms 28.7 ms -59.0 ms
CLI --help 88.5 ms 43.8 ms -44.7 ms

Top changed chunks:

Chunk Raw diff Gzip diff
dist/src/2415.js +241.0 kB +73.6 kB
dist/src/session.js +138.1 kB +41.8 kB
dist/src/9533.js +96.8 kB +30.8 kB
dist/src/cli.js -50.3 kB -16.3 kB
dist/src/args.js +46.3 kB +15.7 kB

@thymikee thymikee force-pushed the codex/bundle-size-lazy-routing branch 6 times, most recently from eb26072 to dc9296c Compare May 30, 2026 14:16
@thymikee thymikee force-pushed the codex/bundle-size-lazy-routing branch from dc9296c to bb10697 Compare May 30, 2026 14:47
@thymikee thymikee merged commit 8e1f8a9 into main May 30, 2026
19 checks passed
@thymikee thymikee deleted the codex/bundle-size-lazy-routing branch May 30, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant