| name | python-context-management |
| description | Choose and design synchronous Python context managers for resource lifetime and temporary state restoration. Use this when drafting or reviewing `with` usage, `@contextmanager` versus class-based design, setup/cleanup failure handling, and ambient-state restoration. |
| complexity | medium |
| risk_profile | ["ambiguity_sensitive"] |
| inputs | ["The resource or state being managed (file, lock, connection, cwd, env var, etc.)","Whether acquisition is stateful, multi-step, or failure-prone","Whether the resource must be cleaned up on both normal and error paths","Whether ambient state must be fully restored after the block","Whether the manager may be reused across multiple `with` invocations"] |
| outputs | ["Decision: `with`, `@contextmanager`, or class-based manager, with clear rationale","Correct acquisition, cleanup, and error-handling code","Guidance on suppression policy, cause chaining, and cleanup-failure handling","`ExitStack` usage when dynamic or multi-resource cleanup is needed"] |
| use_when | ["Deciding whether to use `with`, `@contextmanager`, or a class-based `__enter__` / `__exit__` manager","Designing a custom context manager that must handle acquisition, cleanup, rollback, or ambient-state restoration","Reviewing code that uses manual `close()` and deciding if `with` is more appropriate","Connecting setup or cleanup failures to the existing semantic error model","Using `ExitStack` for dynamic or multi-resource cleanup"] |
| do_not_use_when | ["The resource involves `async with`, `async for`, or cancellation patterns","The task is generic dependency injection, bootstrap, or runtime orchestration","The task is pytest fixture design beyond lifetime discipline compatibility","The task is decorator-driven implicit lifetime management (`ContextDecorator` dual-use)"] |