refactor(ai): extract _run_dir_loop into three focused helpers (#57) #67
Loading…
Reference in a new issue
No description provided.
Delete branch "refactor/issue-57-dir-loop-helpers"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #57.
_run_dir_loopwas ~160 lines holding four conceptual layers in one function: pre-loop setup, budget check + partial-flush, API call + response printing, and tool dispatch + done detection. Phase 3 dynamic turn allocation (#10) will inject more state into the same code path, so this debt is paid before that lands.Three new helpers above
_run_dir_loop_build_dir_loop_context()(ai.py:855) — pure setup. Builds the dir context, child summaries, survey block, filtered tool list, system prompt, and seed user message. Returns a_DirLoopContextnamedtuple. No writes._flush_partial_dir_entry()(ai.py:896) — idempotent partial-cache writer for the budget-exceeded path. Returns the partial summary string (or""if a dir entry already exists or no file entries were available to synthesize from). Idempotent via thecache.has_entry()guard so callers can call it without checking first._handle_turn_response()(ai.py:957) — per-turn response processing. Prints text blocks and tool decisions to stderr, appends the assistant message, dispatches tools (or nudges the agent to call submit_report), appends tool_results. Mutatesmessagesin place. Returns(done, summary)._run_dir_loopis now a ~25-line coordinator: build context, then a for-loop that calls budget check, API, and turn handler in sequence.Verification
python3 -c "import luminos_lib.ai"succeeds.Out of scope
_build_dir_loop_context,_flush_partial_dir_entry) are now naturally testable with a real_CacheManagerin a tempdir, which is a nice side effect._TOOL_DISPATCH/_DIR_TOOLSdeduplication — that's #56.Doc updates
Internals.md§4 updated in the wiki repo (separate commit, separate push) for the new structure and thefile:linerefs that drifted after the rewrite.