Sync Execution Refactor Implementation Plan
For Claude: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
Goal: Make sync run execution easier to extend by removing duplicated pending-ledger writes from the foreground and background item paths.
Architecture: Keep workflows/sync/execution.py as the owner of sync execution effects. Extract small private methods on SyncRunExecutor for ledger writes so run_background_item() and run_foreground_item() read as orchestration: build request, start/queue run, claim pending, execute, record result.
Tech Stack: Existing Pydantic sync models, SyncLedgerStore, pytest sync workflow tests, ruff.
Read Before Coding#
MANUAL.mdalmanac/architecture/sync-and-automation.mdalmanac/style/refactoring.mddocs/python-port-live-agreement.mddocs/reference/cosmic-python/chapter_04_service_layer.md
Useful local line from Cosmic Python:
"It often makes sense to split out a service layer, sometimes called an orchestration layer or a use-case layer."
Scope#
In scope:
- Keep public sync behavior unchanged.
- Keep
SyncRunExecutorinworkflows/sync/execution.py. - Extract private methods for repeated ledger save/update mechanics.
- Update the refactor worklog.
Out of scope:
- No new sync modules.
- No changes to sync policy or candidate evaluation.
- No changes to foreground/background product behavior.
Tasks#
Task 1: Extract Pending Claim And Ledger Save Helpers#
Files:
- Modify:
src/codealmanac/workflows/sync/execution.py
Steps:
- Add
_save_entry(...)onSyncRunExecutor. - Add
_claim_pending(...)returning updated ledgers and the work item with its pending entry. - Use
_claim_pending(...)in bothrun_background_item(...)andrun_foreground_item(...). - Keep
pending_entry(...)arguments identical.
Task 2: Extract Failure Ledger Write Helper#
Files:
- Modify:
src/codealmanac/workflows/sync/execution.py
Steps:
- Add
_record_failure(...)onSyncRunExecutor. - Use it in worker-spawn failure and foreground ingest failure paths.
- Keep failure reasons unchanged:
worker-spawn-failedandingest-failed.
Task 3: Verify And Record#
Files:
- Modify:
docs/refactor-audit-2026-07-06/worklog.md
Steps:
- Run:
uv run pytest tests/test_sync_workflow.py -q
uv run ruff check src/codealmanac/workflows/sync/execution.py tests/test_sync_workflow.pyExpected: all pass.
- Add a short worklog section with the smell and verification.
- Run full gates:
uv run pytest
uv run ruff check .
git diff --checkExpected: all pass.
- Commit:
git add src/codealmanac/workflows/sync/execution.py docs/plans/2026-07-06-sync-execution-refactor.md docs/refactor-audit-2026-07-06/worklog.md
git commit -m "refactor: simplify sync ledger execution"