Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions code_puppy/cli_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
plugins.load_plugin_callbacks()


def _save_autosave_snapshot() -> bool:
"""Persist the current autosave snapshot if autosave is enabled."""
from code_puppy.config import auto_save_session_if_enabled

return auto_save_session_if_enabled()


async def main():
"""Main async entry point for Code Puppy CLI."""
parser = argparse.ArgumentParser(description="Code Puppy - A code generation agent")
Expand Down Expand Up @@ -332,6 +339,7 @@ def _uvx_protective_sigint_handler(_sig, _frame):
# Default to interactive mode (no args = same as -i)
await interactive_mode(message_renderer, initial_command=initial_command)
finally:
_save_autosave_snapshot()
if message_renderer:
message_renderer.stop()
if bus_renderer:
Expand Down Expand Up @@ -774,6 +782,7 @@ async def interactive_mode(message_renderer, initial_command: str = None) -> Non
from code_puppy.messaging import emit_warning

emit_warning("🍩 Wiggum loop stopped due to cancellation")
_save_autosave_snapshot()
continue
# Get the structured response
agent_response = result.output
Expand Down Expand Up @@ -810,9 +819,7 @@ async def interactive_mode(message_renderer, initial_command: str = None) -> Non
get_queue_console().print_exception()

# Auto-save session if enabled (moved outside the try block to avoid being swallowed)
from code_puppy.config import auto_save_session_if_enabled

auto_save_session_if_enabled()
_save_autosave_snapshot()

# ================================================================
# WIGGUM LOOP: Re-run prompt if wiggum mode is active
Expand Down Expand Up @@ -882,7 +889,7 @@ async def interactive_mode(message_renderer, initial_command: str = None) -> Non
await asyncio.sleep(0.1)

# Auto-save
auto_save_session_if_enabled()
_save_autosave_snapshot()

except KeyboardInterrupt:
emit_warning("\n🍩 Wiggum loop interrupted by Ctrl+C")
Expand Down Expand Up @@ -1046,6 +1053,8 @@ async def execute_single_prompt(prompt: str, message_renderer) -> None:
from code_puppy.messaging import emit_error

emit_error(f"Error executing prompt: {str(e)}")
finally:
_save_autosave_snapshot()


def main_entry():
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cli_runner_full_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ async def fake_input(*a, **kw):
@pytest.mark.anyio
async def test_prompt_returns_none_cancelled(self):
call_count = 0
mock_autosave = MagicMock()

async def fake_input(*a, **kw):
nonlocal call_count
Expand All @@ -697,8 +698,10 @@ async def fake_input(*a, **kw):
"code_puppy.cli_runner.parse_prompt_attachments": MagicMock(
return_value=_mock_parse_result("write hello")
),
"code_puppy.cli_runner._save_autosave_snapshot": mock_autosave,
},
)
mock_autosave.assert_called()

@pytest.mark.anyio
async def test_prompt_cancelled_wiggum_active(self):
Expand Down
Loading