Skip to content

perf(providers): import the one provider you asked for, not all ten (5.1s → sub-second startup) - #179

Merged
0bserver07 merged 3 commits into
masterfrom
fix/lazy-provider-imports
Aug 6, 2026
Merged

perf(providers): import the one provider you asked for, not all ten (5.1s → sub-second startup)#179
0bserver07 merged 3 commits into
masterfrom
fix/lazy-provider-imports

Conversation

@0bserver07

Copy link
Copy Markdown
Owner

Two bugs a user hit on a real machine minutes after installing 0.9.2.2 from PyPI.

chimera code took 5.1 s to reach its prompt

Profiled on the box that surfaced it, not guessed:

run_code → CodingAgent.__init__ → create_provider
  → registry._ensure_builtins_registered      5.107 s   of a 5.805 s run
     ...essentially all importlib, nothing Chimera computes

_ensure_builtins_registered() imports all ten built-in provider modules — and the anthropic (445 ms) and openai (247 ms) vendor SDKs with them — in order to construct one provider. Talking to Anthropic cost the OpenAI SDK's import.

get_provider_factory now imports only the module that owns the requested name.

resolve a provider
before (eager, all ten) 1942 ms
after (lazy, one module) 566 ms
saving 1376 ms — 3.4x

(The 5.1 s the user hit was a cold-cache first run; 1.9 s warm. Either way this is the bulk of it.)

_ensure_builtins_registered() stays for callers that genuinely enumerate — list_providers in the factory's error path, chimera which — so nothing became unreachable. A test resolves every registered name from a cold interpreter with no eager registration at all.

Quitting dumped a traceback after "Bye!"

Bye!
Traceback (most recent call last):
  ...asyncio/runners.py, line 118, in run → CancelledError
During handling of the above exception, another exception occurred:
  ...chimera/cli/code.py, line 873, in run_code → KeyboardInterrupt

A Ctrl+C landing during asyncio's shutdown makes asyncio.run cancel the task and re-raise, after the REPL already printed its goodbye. A clean quit rendered as a crash. run_code now catches it — quitting is not an error.

Two things the tests get right on purpose

The behavioural assertion runs in a subprocess. In-process, sys.modules is already polluted by the suite itself, so "openai is not imported" would have passed no matter what the code did — vacuous. Falsified against the old eager path: it fails there.

The name→module table is re-derived, not hand-read. _BUILTIN_MODULES is a claim about another module's import side effects, and those rot silently. The tests import each module in a clean interpreter and compare its registrations against the map. This is not hypothetical — modal_endpoint, xai and acmecloud each also register compatible as a side effect, so a map written by reading source would have been wrong on day one.

Gates

Full suite 10,484 passed / 142 skipped, ruff clean, mypy 736 files 0 errors. The single failure is test_validation_split, documented in CLAUDE.md as env-sensitive locally and green in CI.

Also worth knowing

chimera code has no --no-mcp / --no-lsp / --no-plugins escape hatches (unlike otter), so a user with slow init had no way to bisect it. Not changed here — flagging it as a follow-up, since after this fix init is no longer dominated by one thing.

🤖 Generated with Claude Code

0bserver07 and others added 3 commits August 6, 2026 00:06
`chimera code` took 5.1s to reach its prompt on a modest Linux box. Profiling
put 5.107s of a 5.8s run inside `_ensure_builtins_registered` — essentially all
importlib work. It imports all ten built-in provider modules, and the anthropic
(445ms) and openai (247ms) vendor SDKs with them, in order to construct ONE
provider. Talking to Anthropic cost the OpenAI SDK's import.

`get_provider_factory` now imports only the module that owns the requested
name. On the machine that surfaced it: 1942ms -> 566ms to resolve a provider,
a 1376ms saving, 3.4x.

`_ensure_builtins_registered()` stays for callers that genuinely enumerate
(`list_providers` in the factory error path, `chimera which`), so nothing became
unreachable — a test resolves every registered name from a cold interpreter with
no eager registration at all.

The name->module table is derived, not hand-read: the tests import each module
in a clean interpreter and compare its registrations against the map. Reading
the source would have missed that modal_endpoint, xai and acmecloud each also
register `compatible` as an import side effect — a hand-written map would have
been wrong on its first day.

The behavioural assertion runs in a SUBPROCESS. In-process, sys.modules is
already polluted by the suite itself, so "openai is not imported" would have
passed no matter what the code did. Falsified against the old eager path: it
fails there.

Also fixes the traceback on quit. A Ctrl+C landing during asyncio's shutdown
made asyncio.run cancel the task and re-raise, dumping CancelledError +
KeyboardInterrupt AFTER "Bye!" — a clean quit that looked like a crash.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The 0.9.2.2 post-publish check installed from PyPI into a clean venv outside
the repo and confirmed `chimera --version`, `import chimera`, and that the
fixes were in the wheel. Every assertion was true. It exercised nothing a user
does first — and within minutes a user hit a 5.1s time-to-first-prompt and a
KeyboardInterrupt traceback on quit, neither of which an import can reach.

Playbook 14 now requires launching the entry point post-publish, timing it, and
quitting it: `time chimera code < /dev/null` would have caught both defects in
one command.

Two related traps recorded with it, both hit for real on this batch:

- Verify on a machine that is NOT the dev box. The same release installed fine
  here and failed on a user's Linux host, where `pip` was bound to a dead
  Python 3.8 while `python3` was 3.11.7. pip reports that as
  "(from versions: none)", which reads exactly like the package was never
  published, and sent us looking at PyPI instead of the interpreter.
- Non-interactive SSH does not load .zshrc. My first remote diagnosis reported
  the wrong Python and a wrong PATH — both artifacts of my own session, not
  facts about the machine. Re-run through `zsh -i -l -c`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`time chimera code < /dev/null` measures time-to-prompt but does NOT reproduce
the quit traceback: closing stdin raises EOFError, a different path from SIGINT.
Verified on the 0.9.2.2 tree — the redirect exits 0 and silent on the exact
build that dumps KeyboardInterrupt when you actually press Ctrl+C.

So the gate needs both lines. A gate that only tests the easy exit reports green
on a crash-on-quit, which is precisely how this defect shipped.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@0bserver07
0bserver07 merged commit 0c93b8d into master Aug 6, 2026
9 checks passed
@0bserver07
0bserver07 deleted the fix/lazy-provider-imports branch August 6, 2026 06:03
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