perf(providers): import the one provider you asked for, not all ten (5.1s → sub-second startup) - #179
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two bugs a user hit on a real machine minutes after installing 0.9.2.2 from PyPI.
chimera codetook 5.1 s to reach its promptProfiled on the box that surfaced it, not guessed:
_ensure_builtins_registered()imports all ten built-in provider modules — and theanthropic(445 ms) andopenai(247 ms) vendor SDKs with them — in order to construct one provider. Talking to Anthropic cost the OpenAI SDK's import.get_provider_factorynow imports only the module that owns the requested name.(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_providersin 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!"
A Ctrl+C landing during asyncio's shutdown makes
asyncio.runcancel the task and re-raise, after the REPL already printed its goodbye. A clean quit rendered as a crash.run_codenow 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.modulesis 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_MODULESis 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,xaiandacmecloudeach also registercompatibleas 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,
ruffclean,mypy736 files 0 errors. The single failure istest_validation_split, documented in CLAUDE.md as env-sensitive locally and green in CI.Also worth knowing
chimera codehas no--no-mcp/--no-lsp/--no-pluginsescape hatches (unlikeotter), 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