refactor(agent-core-v2): invert sessionLifecycle/MCP dependency via lifecycle event - #2803
Conversation
…ifecycle event - add onWillCreateSession to ISessionLifecycleService: a synchronous participation event fired before a session's services activate, exposing a session-domain facade (readSeed / contributeSeed / onSessionDispose) - workspaceMcp subscribes and activates ephemeral-server overlays itself: the configs travel as the new ISessionEphemeralMcpServers session seed, the stdio cwd is read from ISessionContext, the merged ISessionMcpHandle is contributed over the seed adapter's workspace projection, and the overlay shutdown is attached to the session's teardown - sessionLifecycle drops its IWorkspaceMcpService dependency, the overlay tracking map, handle-dispose wrapping, and the dispose backstop - rename ScopeOptions.extra to seeds and ScopeOptions.assemble to configureContainer
|
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6a87bd8d2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // The will-create moment is a business-lifecycle event; the DI | ||
| // container behind the participation surface stays this service's | ||
| // implementation detail. | ||
| this._onWillCreateSession.fire({ |
There was a problem hiding this comment.
Propagate will-create participant failures
When a will-create participant fails while contributing a session resource, this generic Emitter.fire path reports the exception via unexpected-error handling and then continues, so create({ mcpServers }) can return a live session even though the workspaceMcp participant failed before contributing the overlay handle or teardown. For this synchronous participation hook, participant failures should abort session materialization rather than leaving the session partially assembled.
AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L7-L7
Useful? React with 👍 / 👎.
Related Issue
No linked issue — the problem is explained below.
Problem
In the v2 engine,
SessionLifecycleServicedirectly injectedIWorkspaceMcpServiceto support per-session ephemeral MCP servers: it decided when a session overlay was needed, mapped the session's workDir onto MCP's stdio-cwd concept, and owned overlay teardown through a tracking map, a handle-dispose wrapper, and a dispose backstop. A foundational lifecycle service depended on an upstream capability domain, and the teardown bookkeeping existed only because the overlay's lifetime had no proper owner.What changed
Invert the dependency: the session lifecycle now announces a business-lifecycle event, and the MCP domain activates itself from it.
ISessionLifecycleServicegainsonWillCreateSession— thewillhalf of the existingonDidCreateSession, fired synchronously while a session is being created, before its services activate. Its participation surface speaks the session domain's own vocabulary (readSeed/contributeSeed/onSessionDispose), so participants never see DI-kernel mechanics (container, provide, ledger).ISessionEphemeralMcpServersseed, planted verbatim from the session's creation options.WorkspaceMcpServicesubscribes to the event and activates overlays itself: it reads the ephemeral-servers seed and the session's ownISessionContext.cwd, contributes the mergedISessionMcpHandle(replacing the seed adapter's workspace projection), and attaches the overlay's shutdown to the session's teardown — covering close / archive / delete / failed-create / workspace-teardown paths with no lifecycle-owned bookkeeping.SessionLifecycleServicedrops itsIWorkspaceMcpServicedependency, the overlay tracking map, the handle-dispose wrapper, and the dispose backstop.ScopeOptions.extra→seedsandScopeOptions.assemble→configureContainerto match the kernel's existing seed vocabulary and the hook's actual contract.Verified: full agent-core-v2 suite passes (310 files / 4893 tests, including rewritten lifecycle participant cases and new workspaceMcp activation cases); kap-server, klient, node-sdk, and acp-server typecheck clean.
Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.