cache PlanExeConfig / PlanExeDotEnv / PlanExeLLMConfig loaders#673
Merged
cache PlanExeConfig / PlanExeDotEnv / PlanExeLLMConfig loaders#673
Conversation
#671 cached `_load_llm_config` in llm_factory, but the underlying loaders still re-parsed on every direct caller — most notably `run_plan_pipeline.resolve_luigi_workers` calling `PlanExeLLMConfig.load(...)` directly. Each call also fans out into two `PlanExeConfig.load` invocations (one direct, one via `PlanExeDotEnv.load`), so a single LLMConfig load logs the ".env not found" / "Optional configuration file '.env' not found" / per-API-key warnings every time. Move the work in each loader's `load(...)` classmethod into a module-level `@functools.cache`-decorated `_load_cached(...)` helper. The classmethod normalizes inputs to hashable form (stripping `ModelProfileEnum` to its `str` value) and delegates. Each cache key is a plain tuple of inputs; `cache_clear()` is available on each `_load_cached` if tests need to reset. Also reorder `PlanExeLLMConfig.load`: filter by whitelisted classes BEFORE substituting env vars. With `PLANEXE_LLM_CONFIG_WHITELISTED_CLASSES=OpenRouter` set in production, the entries needing `OPENAI_API_KEY` / `DASHSCOPE_API_KEY` / `DEEPSEEK_API_KEY` are dropped before substitution looks for those vars, so those warnings stop firing entirely — even on the first cache miss. Each Luigi worker subprocess still parses once on startup. That's a hard limit of in-process caching — one batch of warnings per fresh process, not zero.
Refactor in 6300ca7 ("cache PlanExeConfig / PlanExeDotEnv / PlanExeLLMConfig loaders") accidentally moved instance methods out of two classes: - `PlanExeDotEnv.update_os_environ`, `get`, `get_absolute_path_to_file`, `get_absolute_path_to_dir`, `__repr__` - `PlanExeLLMConfig.load_llm_config`, `substitute_env_vars`, `filter_by_whitelisted_classes`, `__repr__` The methods ended up indented at module level after a `return` statement inside `_load_cached`, so they parsed as nested functions inside `_load_cached`'s body and were unreachable — not class methods. Production blew up at app.py:17 with `AttributeError: 'PlanExeDotEnv' object has no attribute 'update_os_environ'` on container start. Move the methods back inside their respective class bodies and keep the cached `_load_cached` after the class definition.
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.
Summary
Follow-up to #671. That PR cached
_load_llm_configinllm_factory, but the underlying loaders still re-parsed on every direct caller — most visiblyrun_plan_pipeline.resolve_luigi_workerscallingPlanExeLLMConfig.load(...)directly. EachPlanExeLLMConfig.loadalso fans out into twoPlanExeConfig.loadcalls (one direct, one viaPlanExeDotEnv.load), so the production logs kept emitting:…on essentially every Luigi task.
Changes
Move the body of each loader's
load(...)classmethod into a module-level@functools.cache-decorated_load_cached(...)helper.PlanExeConfig.load(...)PlanExeDotEnv.load()(singleton — no args)PlanExeLLMConfig.load(...)The classmethod normalizes inputs to hashable form (stripping
ModelProfileEnumto itsstrvalue) and delegates. Cache keys are plain tuples of normalized inputs.cache_clear()is available on each_load_cachedif a test ever needs to reset.Reorder
PlanExeLLMConfig.load: filter by whitelisted classes BEFORE substituting env vars. WithPLANEXE_LLM_CONFIG_WHITELISTED_CLASSES=OpenRouterin production, entries that needOPENAI_API_KEY/DASHSCOPE_API_KEY/DEEPSEEK_API_KEYare dropped before substitution looks for those vars — those warnings stop firing entirely, even on first cache miss.Each Luigi worker subprocess still parses once on startup. That's a hard limit of in-process caching across subprocess boundaries — expect one batch of warnings per fresh process, not zero.
Test plan
Optional file '.env' not found/Environment variable 'X' not foundlines appear at most once per worker process (not once per task).PLANEXE_LLM_CONFIG_WHITELISTED_CLASSES=OpenRouter, verifyOPENAI_API_KEY/DASHSCOPE_API_KEY/DEEPSEEK_API_KEYwarnings no longer appear at all.get_llm("openrouter-…")still resolves to a working model.🤖 Generated with Claude Code