Skip to content

refactor(agent-core-v2): invert sessionLifecycle/MCP dependency via lifecycle event - #2803

Merged
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:refactor/agent-core-v2-session-mcp-events
Aug 11, 2026
Merged

refactor(agent-core-v2): invert sessionLifecycle/MCP dependency via lifecycle event#2803
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:refactor/agent-core-v2-session-mcp-events

Conversation

@sailist

@sailist sailist commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

In the v2 engine, SessionLifecycleService directly injected IWorkspaceMcpService to 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.

  • ISessionLifecycleService gains onWillCreateSession — the will half of the existing onDidCreateSession, 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).
  • Ephemeral MCP server configs travel as session-domain data: the new ISessionEphemeralMcpServers seed, planted verbatim from the session's creation options.
  • WorkspaceMcpService subscribes to the event and activates overlays itself: it reads the ephemeral-servers seed and the session's own ISessionContext.cwd, contributes the merged ISessionMcpHandle (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.
  • SessionLifecycleService drops its IWorkspaceMcpService dependency, the overlay tracking map, the handle-dispose wrapper, and the dispose backstop.
  • Also renames ScopeOptions.extraseeds and ScopeOptions.assembleconfigureContainer to 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

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

…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
@changeset-bot

changeset-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a6a87bd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Aug 11, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@a6a87bd
npx https://pkg.pr.new/@moonshot-ai/kimi-code@a6a87bd

commit: a6a87bd

@sailist
sailist merged commit 2acf22f into MoonshotAI:main Aug 11, 2026
15 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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({

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant