---
model: claude-opus-4-6
service: claude
session: (ad-hoc, not tracked via conf.toml)
timestamp: 2026-04-06T17:28:48Z
git_ref: 02b2ef1
scope: tests
substantive: true
raw_file: 20260406T172848Z_02b2ef1_prompt_io.raw.md
---

## Prompt

User asked to extend `tests/test_resource_cache.py` with a test
that reproduces the edge case fixed in commit `02b2ef18` (per-key
locking+user tracking in `maybe_open_context()`). The bug was
originally triggered in piker's `brokerd.kraken` backend where the
same `acm_func` was called with different kwargs, and the old
global `_Cache.users` counter caused:

- teardown skipped for one `ctx_key` bc another key's users kept
  the global count > 0
- re-entry hitting `assert not resources.get(ctx_key)` during the
  teardown window

User requested a test that would fail under the old code and pass
with the fix.

## Response summary

Designed and implemented `test_per_ctx_key_resource_lifecycle`
which verifies per-`ctx_key` resource isolation by:

1. Holding resource `'a'` open in a bg task
2. Opening+closing resource `'b'` (same `acm_func`, different
   kwargs) while `'a'` is still alive
3. Re-opening `'b'` and asserting cache MISS — proving `'b'` was
   torn down independently despite `'a'` keeping its own user
   count > 0

With the old global counter, phase 3 would produce a stale cache
HIT (leaked resource) or crash on the assert.

Also added a trivial `acm_with_resource(resource_id)` ACM helper
at module level.

## Files changed

- `tests/test_resource_cache.py` — add `acm_with_resource` ACM +
  `test_per_ctx_key_resource_lifecycle` test fn

## Human edits

None — committed as generated (pending user review).
