# Todo 48.2 Outcome Cache and Sync Resource Lifecycle Plan **Status:** in_progress ## Goal Close the remaining Important batch findings by preserving complete execution semantics in cache/replay and keeping physical concurrency resources held while timed-out synchronous work is still running. ## Scope - Canonical key generation must tag non-JSON/normalizer-invalid arguments distinctly instead of collapsing them to `{}`. - Cache/replay `_ExecutionOutcome`, including `batch_timed_out`, rather than inferring phase semantics from error text. - Add per-scope generations: - `release_scope()` advances the generation and clears completed/in-flight mappings for the released generation; - late completion from an old generation cannot repopulate cache; - immediate scope reuse operates on a new generation. - For synchronous handlers dispatched to a worker thread: - return timeout promptly; - keep semaphore/conflict locks leased until the underlying shielded kernel task actually completes; - consume late exceptions and release resources in a completion callback; - async handlers remain cancellable normally. - Add explicit `deduplicated_from` result provenance and clone correlation safely: - outer `event_id` and tool reply ID match each requested call; - top-level payload `event_id` is rewritten only when it equals the primary result ID; - Runtime policy aggregation processes a coalesced logical result once, preventing duplicate farewells/templates. ## Files - Modify: `src/agent_lab/application/events/models.py` - Modify: `src/agent_lab/application/events/batch.py` - Modify: `src/agent_lab/application/tools.py` - Modify: `src/agent_lab/application/runtime.py` only if audit/provenance exposure needs alignment - Modify: `tests/test_event_batch.py` - Modify: `tests/test_debug_runtime.py` ## Requirement Execution Loop ### Plan 1. Add RED tests for invalid-vs-empty canonical keys, terminal timeout replay flags, release/reuse with late completion, and concurrent in-flight cleanup. 2. Add RED sync timeout lease tests proving conflicting/new work cannot start until the worker actually finishes. 3. Add RED different-ID payload correlation and single farewell/template tests. 4. Implement outcome caching, scope generations, deferred resource release, and explicit dedupe provenance. 5. Run focused/full verification and final bounded reviews. ### Observe / Update Close Todo 48 only when all named concurrency/cache findings are permanent regressions and no Critical/Important issues remain. ## Verification - `UV_CACHE_DIR=.uv-cache uv run pytest tests/test_event_batch.py tests/test_debug_runtime.py -q` - `UV_CACHE_DIR=.uv-cache uv run pytest` - `git diff --check`