Skip to content

refactor: split runtime.go and extract pkg/modelerrors#2010

Open
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:board/refacto-runtime-0fc5230f
Open

refactor: split runtime.go and extract pkg/modelerrors#2010
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:board/refacto-runtime-0fc5230f

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Mar 9, 2026

Summary

Break up the oversized runtime.go (2510 lines) by extracting cohesive groups of functions into dedicated files, and move pure error classification utilities into a new reusable package.

Changes

New files in pkg/runtime

  • model_picker.go: findModelPickerTool, handleChangeModel, handleRevertModel, setModelAndEmitInfo
  • agent_delegation.go: CurrentAgentSubAgentNames, RunAgent, handleTaskTransfer, runSubSession, handleHandoff, plus shared helpers (validateAgentInList, buildTaskSystemMessage, agentNames)

New package pkg/modelerrors

  • ContextOverflowError type and detection
  • HTTP status code extraction (Anthropic, Gemini, OpenAI)
  • Retryable error classification (IsRetryableModelError, IsRetryableStatusCode)
  • Exponential backoff with jitter (CalculateBackoff, SleepWithContext)
  • User-friendly error formatting (FormatError)
  • Full unit test coverage in modelerrors_test.go

Net impact

  • runtime.go: 2510 → ~2050 lines
  • -238 net lines overall (deduplication + moving tests to the right package)
  • Zero behavior changes — all existing tests pass

Break up the oversized runtime.go (2510 lines) by extracting cohesive
groups of functions into dedicated files, and move pure error
classification utilities into a new reusable package.

New files in pkg/runtime:
- model_picker.go: model change/revert tool handlers
- agent_delegation.go: sub-agent dispatch, task transfer, handoffs

New package pkg/modelerrors:
- ContextOverflowError type and detection
- HTTP status code extraction (Anthropic, Gemini, OpenAI)
- Retryable error classification
- Exponential backoff with jitter
- User-friendly error formatting

Assisted-By: docker-agent
@dgageot dgageot requested a review from a team as a code owner March 9, 2026 16:33
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

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

Review Summary

Assessment: 🟢 APPROVE

This is a clean refactoring that successfully extracts error handling utilities into a new pkg/modelerrors package and splits the oversized runtime.go into more focused files:

  • New package pkg/modelerrors: Consolidates error classification, retry logic, and backoff calculations with comprehensive test coverage
  • agent_delegation.go: Extracts agent delegation and task transfer logic
  • model_picker.go: Isolates model picker tool handlers
  • Net reduction: ~238 lines through deduplication

Analysis

Reviewed 2803 lines across 7 files. The refactoring maintains identical behavior while improving code organization:

✅ All moved code preserves exact functionality
✅ Import paths correctly updated to new package structure
✅ Constants properly migrated (e.g., DefaultFallbackCooldownmodelerrors.DefaultCooldown)
✅ Comprehensive test coverage in new modelerrors_test.go
✅ No behavioral changes introduced
✅ Go 1.26.0 ensures goroutine-safe standard library usage

Findings

No bugs detected in the changed code. The extraction is methodical and preserves all existing semantics.

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.

1 participant