Skip to content

refactor(agent-server): split claude_code.py into focused modules (#122) - #750

Merged
vybe merged 1 commit into
devfrom
AndriiPasternak31/split-claude-code-modules
May 10, 2026
Merged

vybe merged 1 commit into
devfrom
AndriiPasternak31/split-claude-code-modules

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Summary

Splits the 2137-LOC docker/base-image/agent_server/services/claude_code.py monolith into six focused services. claude_code.py keeps the AgentRuntime adapter, execute_claude_code chat path, and the execution lock; everything else moves out and is re-imported, so the public API is unchanged.

New module Responsibility
_runtime_config.py Guardrails runtime config loader (shared by chat + task paths)
error_classifier.py Exit-code / signal / empty-result diagnosis + metadata recovery
headless_executor.py execute_headless_task + HeadlessRunContext orchestrator
jsonl_recovery.py Fallback parsing of ~/.claude/projects/<uuid>.jsonl
stream_parser.py Streaming + batch stream-json parsers (token-accounting invariant lives here)
subprocess_lifecycle.py pgroup capture/terminate, _drain_bounded deadlock guard (#728)

Net diff is +2466 / -1758 across 26 files; the new module bodies were lifted directly from claude_code.py to keep behaviour identical.

Why

claude_code.py had grown to ~2.1k lines mixing the chat path, headless task path, two parsers, three error-classification surfaces, JSONL recovery, and subprocess lifecycle. Extracting each concern lets future work (#728 drain budget, SESSION_TAB resume path, GUARD-003 runtime config) land in a smaller blast radius.

What also changed

  • Tests — import paths follow the new module split; new pins added for chat-wallclock timeout, drain-bounded, JSONL recovery, signal-exit classification, session-id parser, persist-session plumbing, and result-recovery (test-runner.md catalog updated with the seven new/moved entries).
  • tests/unit/conftest.py — pins TRINITY_DB_PATH to tempfile.gettempdir() so DatabaseManager() at import doesn't trip on read-only /data on the host.
  • task_execution_service.py — comment-only update redirecting the cache-token accounting note to stream_parser.process_stream_line.
  • process_registry.py — comment-only update reflecting that publish_log is now called from both the chat and task paths.

Test plan

  • pytest tests/unit/test_chat_wallclock_timeout.py tests/unit/test_drain_bounded.py tests/unit/test_jsonl_recovery.py tests/unit/test_signal_exit_classification.py tests/unit/test_empty_result_classification.py tests/unit/test_claude_code_result_recovery.py tests/unit/test_claude_code_session_id_parser.py tests/unit/test_session_persistence_flag.py — module-boundary pins
  • pytest tests/unit/ — broader unit suite still green after import-path updates
  • Build base agent image (./scripts/deploy/build-base-image.sh) and run a chat + a scheduled task against an agent — confirms both execute_claude_code and execute_headless_task paths exercise the re-imported helpers

Closes #122

🤖 Generated with Claude Code

Extracts the 2137-LOC claude_code.py monolith into six focused services so
the chat path, headless task path, parsing, error classification, JSONL
recovery, and subprocess teardown each live in their own module:

- _runtime_config.py    — guardrails config loader (shared by chat + task)
- error_classifier.py   — exit-code/signal/empty-result diagnosis + recovery
- headless_executor.py  — execute_headless_task + HeadlessRunContext
- jsonl_recovery.py     — fallback parsing of ~/.claude/projects/<uuid>.jsonl
- stream_parser.py      — stream-json line + batch parsers
- subprocess_lifecycle.py — pgroup capture/terminate, _drain_bounded (#728)

claude_code.py keeps the AgentRuntime adapter, execute_claude_code chat
path, and the execution lock; everything else is re-imported from the new
modules so the public API is unchanged.

Tests updated to follow the new import paths and pin the new module
boundaries (chat wallclock timeout, drain bounded, JSONL recovery, signal
exit, session-id parser, etc.); test-runner.md catalog updated with the
seven new/moved unit suites. Adds TRINITY_DB_PATH default in
tests/unit/conftest.py so DatabaseManager() at import doesn't trip on
read-only /data on the host.

Comment-only update in task_execution_service.py points the cache-token
accounting note at the new stream_parser.process_stream_line; comment-only
update in process_registry.py reflects that publish_log is now called from
both paths.

Closes #122

Co-Authored-By: Claude <[email protected]>

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean module split with no functional regressions. Token-accounting fix is correct and well-tested. All public API callers unaffected. Test isolation improvements are a bonus.

@vybe
vybe merged commit ec0e4ef into dev May 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants