mpfaffenberger / mpfaffenberger/code_puppy
cli_runner.py: interactive_mode() is a ~700-line god function with the turn-handling sequence duplicated 3x, runtime pip-install, and dead shutdown_flag
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 814
- Forks
- 278
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 76
Description
Problem
code_puppy/cli_runner.py (1134 lines) concentrates several maintainability problems:
1. interactive_mode() is a ~700-line god function (lines 401-960)
It handles: welcome banners, shell passthrough, initial-command execution, the onboarding wizard, OAuth flows, the REPL loop, exit handling, slash-command dispatch, autosave-load UI, the plugin continuation loop, and Windows Ctrl+C re-arming. It cannot be summarized in one sentence and cannot be unit-tested.
2. The "run prompt -> emit response -> set history -> autosave" sequence is duplicated three times
- initial command path (lines ~460-505)
- main REPL turn (lines ~790-860)
- plugin continuation loop (lines ~910-950)
Each copy re-implements: run_prompt_with_attachments(...), result.output -> AgentResponseMessage emit, set_message_history(list(result.all_messages())), flush + asyncio.sleep(0.1), auto_save_session_if_enabled(). Any fix (e.g. issue #222, history not saved on error) must be applied in three places. Extract a single async def _run_turn(agent, prompt, renderer) -> TurnOutcome helper.
3. Runtime pip install from the REPL (lines ~510-530)
except ImportError:
emit_warning("Warning: prompt_toolkit not installed. Installing now...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", "prompt_toolkit"])
prompt_toolkit is a declared dependency; if the import fails the environment is broken and mutating it behind the user's back (which also fails under uvx/pipx read-only envs and corporate proxies) is surprising magic. Prefer a clear error message instructing reinstall.
4. Dead global shutdown_flag (lines 370-371)
global shutdown_flag; shutdown_flag = False -- set once, never read anywhere in the codebase. Delete it.
5. main_entry() swallows KeyboardInterrupt but prints a full traceback and returns 0 (lines ~1120-1130)
except KeyboardInterrupt:
sys.stderr.write(traceback.format_exc())
return 0
A user Ctrl+C gets a scary traceback dump and a success exit code. Conventional behavior: print nothing (or a one-liner) and exit 130.
Suggested fix
Extract _run_turn() and _handle_autosave_load() helpers, move the onboarding-wizard block into its own function, drop the runtime pip-install and shutdown_flag, and make Ctrl+C exit quiet with code 130.
Filed by Zen Reviewer A (code-puppy-60635a)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in code_puppy/cli_runner.py, reading interactive_mode() around lines 401-960 and main_entry() near lines 1120-1130. Trace the three duplicated turn-handling paths, the autosave-load and onboarding blocks, the prompt_toolkit import, and shutdown_flag usage. Done means the requested helpers and cleanup preserve the existing flows while Ctrl+C exits quietly with status 130 and no runtime installation occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100