Skip to content

refactor: batch recovery enums, registry index, and parameter cleanup#630

Merged
Muizzkolapo merged 9 commits into
mainfrom
refactor/batch-recovery-cleanup
May 24, 2026
Merged

refactor: batch recovery enums, registry index, and parameter cleanup#630
Muizzkolapo merged 9 commits into
mainfrom
refactor/batch-recovery-cleanup

Conversation

@Muizzkolapo
Copy link
Copy Markdown
Owner

@Muizzkolapo Muizzkolapo commented May 24, 2026

Summary

  • O(1) lookups: Replace linear scans in BatchResultReconciler.get_record_index() and BatchRegistryManager.get_batch_job_by_id()/update_status() with dict-based index lookups
  • RecoveryType/RecoveryPhase enums: Replace bare "retry"/"reprompt"/"done" strings with str enums (JSON round-trip transparent — existing recovery state files on disk load without breaking)
  • RecoveryContext + BatchIdentity dataclasses: Reduce 5 recovery functions from 11–13 positional params down to 5–6 using context objects
  • Quick wins: Eliminate TOCTOU race in _merge_carry_forward(), deduplicate schema compilation in BatchTaskPreparator, extract _attach_passthrough_context helper from 3 copy-pasted passthrough builders

15 files changed, 7311 tests pass, 2 skipped, ruff clean.

Test plan

  • All 7311 existing tests pass (no new tests needed — pure refactor)
  • ruff check and ruff format --check clean
  • Verification checks from spec:
    • No list().index() in reconciler
    • No carry_path.exists() TOCTOU guard
    • _prepare_schema called only twice (definition + inside _validate_config)
    • No bare "retry"/"reprompt" strings outside enum definitions
    • No linear scan in get_batch_job_by_id/update_status
    • BatchContextAdapter.to_processing_context called only 2x in batch_result_strategy (successful + helper)
  • RecoveryState JSON roundtrip preserved (str enums serialize transparently)

- Replace O(n) list().index() in BatchResultReconciler.get_record_index()
  with dict lookup built at init time
- Remove exists() guard before read_text() in _merge_carry_forward() to
  eliminate TOCTOU race; catch FileNotFoundError instead
- Make _validate_config() return compiled schema so prepare_tasks() avoids
  duplicate _prepare_schema() call (saves file I/O + compilation)
Replace bare "retry"/"reprompt"/"done" strings with RecoveryType and
RecoveryPhase enums in batch_constants.py. Update BatchJobEntry,
RecoveryState, processing.py, and processing_recovery.py to use enums.

Both enums inherit str so JSON round-trips are transparent — existing
recovery state files on disk deserialize without breaking.
Replace O(n) linear scans in get_batch_job_by_id() and update_status()
with O(1) dict lookups via _batch_id_index (batch_id → file_name).
Index is rebuilt under the existing lock on every cache mutation.
Add RecoveryContext (service/manager/provider/agent_config/output_directory/
action_name/start_time) and BatchIdentity (batch_id/file_name/entry) dataclasses.

Refactor handle_retry_recovery, handle_reprompt_recovery, check_and_submit_reprompt,
finalize_batch_output, and _finalize_and_cleanup from 11-13 positional params
down to 5-6 using the new context objects.
Deduplicate BatchContextAdapter.to_processing_context() calls from the
three passthrough builders (_build_exhausted_passthrough,
_build_unprocessed_passthrough, _build_failed_passthrough) into a
shared _attach_passthrough_context helper.
Update all test files that directly call handle_retry_recovery,
handle_reprompt_recovery, finalize_batch_output, check_and_submit_reprompt,
and cleanup_recovery to use the new RecoveryContext + BatchIdentity
parameter objects. No test logic changed.
- Use BatchStatus.SUBMITTED enum instead of "submitted" string literal
  in _remove_batch_placeholder
- Use lazy index invalidation in BatchRegistryManager — invalidate on
  mutation, rebuild on first lookup — avoids O(n) rebuild on every
  save/remove when lookups may not follow
@Muizzkolapo Muizzkolapo force-pushed the refactor/batch-recovery-cleanup branch from 7239dfe to b7bdd79 Compare May 24, 2026 09:26
@Muizzkolapo Muizzkolapo merged commit fc6eccc into main May 24, 2026
5 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant