App lifecycle: async open/close + DI seam - #5
Merged
Conversation
App had no shutdown path: SessionFactory's Redis client and the MCP lifecycle were never closed, and serve.py had no FastAPI lifespan. App.open() now loads the project, starts MCPLifecycle, and guarantees aclose() runs on exit; aclose() closes the Redis session client and MCP servers and is idempotent. The constructor also accepts an optional session_factory to inject a fake in tests. serve.py wires this through a FastAPI lifespan so `compose stop` shuts down cleanly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
This was referenced Jul 26, 2026
…rrect
- aclose(): try/finally so the Redis session client and the MCP lifecycle are
both torn down even if the first step raises.
- open(): load() and MCPLifecycle.startup() now run inside the try, so a
failure there no longer leaks an injected session_factory.
- aclose() shuts down the process-wide MCP registry only when this App
started it (`_started_mcp`), so a nested App or a bare App().aclose() no
longer kills servers it doesn't own.
- serve: `api.state.deck` is initialized to None and every endpoint answers
503 before the lifespan starts (/health reports {"status": "starting"})
instead of raising AttributeError or reporting an empty inventory as ok.
- serve: drop the second load() per boot — load() stashes App.inventory and
/health reads it, so graphs are compiled once.
- tests: cover the session_factory DI seam (bypasses from_settings, closed
once, closed when load() raises), the MCP ownership guard, and the
pre-startup 503s.
- docs: App docstring + README recommend `async with App.open()`.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1
What
App.open(): an async classmethod context manager that builds the app,runs
load(), startsMCPLifecycle, and guaranteesaclose()runs onexit (even on error).
App.aclose(): closes theSessionFactory's Redis client and shuts downMCPLifecycle. Idempotent — a second call is a no-op, guarded by a_closedflag.App(session_factory=...): the constructor now accepts an optionalSessionFactoryoverride so tests can inject a fake (e.g. one wrappingfakeredis) instead of building a real Redis client from settings.
serve.py:create_app()now wiresApp.open()/aclose()through aFastAPI
lifespan, socompose stop(SIGTERM) closes the Redis client andMCP servers cleanly instead of leaking them.
Why
Apphad no shutdown path — the Redis client and MCP lifecycle were neverclosed, and there was no seam to swap infrastructure in tests. This lands the
minimal DI + lifecycle surface the issue asks for, without adding any new
catalog/execution logic (agentdeck still just owns configuration).
Decisions / notes
MCPLifecycleis a process-wide classmethod registry (not per-App), soopen()/aclose()call itsstartup()/shutdown()directly — both arealready individually idempotent, matching
App.aclose()'s idempotency.serve.py's lifespan callsdeck.load()a second time (afteropen()already ran it once) purely to capture the inventory dict for
/health;load()is idempotent (refresh=True) and cheap, so this is deliberate,not a leftover.
Test plan
ruff check agentdeck/ tests/ty check agentdeckpytest tests/ -q(addedtest_open_close_lifecycle: open → session_forplumbing (no live model, SQLite fallback) → aclose, then a second
aclose()to confirm idempotency)🤖 Generated with Claude Code