refactor(agent-core-v2): split agent lifecycle into existence, subagent, and session MCP domains - #1624
Conversation
…nt, and session MCP domains
Converge IAgentLifecycleService to agent existence only (create / fork /
get / list / remove + onDidCreate / onDidDispose):
- create() is create-or-get for explicit ids: concurrent in-flight
creations join and an existing agent is returned as-is, so the main
agent bootstrap can no longer race the first prompt (the web /goal
crash: a prompt enqueued while the agent lane was still initializing
killed the process as an unhandled rejection).
- Extract the subagent domain (ISessionSubagentService): run() plus the
SubagentStart/SubagentStop hook surface, with runAgentTurn /
mirrorAgentRun / the Agent tool moved under session/subagent/.
- Extract the session MCP domain (ISessionMcpService): the session-wide
connection manager, initial connect, and telemetry.
- Delete onDidCreateMain / notifyMainCreated: subscribers filter
onDidCreate by id, and AgentPluginService self-gates on main. The
ensureMainAgent helper stays as a thin wrapper over
create({ agentId: MAIN_AGENT_ID }).
- Slim the agent scope seed to IAgentScopeContext: add the AgentWireService
adapter (derives its log addressing) and let AgentMcpService resolve the
session manager and originals dir itself. The default permission mode is
applied in bindBootstrap.
- Migrate callers in kap-server / kimi-code and all affected tests.
🦋 Changeset detectedLatest commit: 067ad42 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b7428b5e2a
ℹ️ 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".
| // Create-or-get for explicit ids: join a concurrent in-flight creation or | ||
| // return the existing agent, so callers never see a duplicate scope or a | ||
| // not-yet-ready handle. Auto-minted ids always create fresh. |
There was a problem hiding this comment.
Move inline comments to module headers
packages/agent-core-v2/AGENTS.md requires: "Comments live solely in the top-of-file /** */ block — never beside functions, methods, or statements." This added method-body explanation, along with similar new inline comments in this file, leaves the v2 package out of conformance; please move the durable rationale into the file header or remove the inline narration.
Useful? React with 👍 / 👎.
…eation Subagent dispatchers (the Agent tool, AgentSwarm, and /init) now set the inherited permission mode on the child themselves after creation, keeping the dispatch posture out of the creation primitive. Fresh agents still start from the configured defaultPermissionMode.
…ecycle-split # Conflicts: # packages/agent-core-v2/src/session/agentLifecycle/agentLifecycle.ts # packages/agent-core-v2/src/session/agentLifecycle/agentLifecycleService.ts # packages/agent-core-v2/src/session/agentLifecycle/mainAgent.ts # packages/agent-core-v2/test/app/gateway/gateway.test.ts # packages/agent-core-v2/test/app/messageLegacy/messageLegacy.test.ts # packages/agent-core-v2/test/app/sessionExport/sessionExport.test.ts # packages/agent-core-v2/test/app/sessionLifecycle/sessionLifecycle.test.ts # packages/agent-core-v2/test/session/agentLifecycle/agentLifecycle.test.ts # packages/agent-core-v2/test/session/sessionActivity/sessionActivity.test.ts # packages/agent-core-v2/test/session/swarm/sessionSwarm.test.ts # packages/agent-core-v2/test/session/todo/sessionTodo.test.ts # packages/agent-core-v2/test/session/workspaceCommand/workspaceCommand.test.ts # packages/agent-core-v2/test/tool/tool.test.ts
…drain idle constructions before scope ready - wireRecordService.restore() synthesizes and prepends a current-version metadata envelope when the log lacks one, replacing the lifecycle-layer ensureWireMetadata; metadataRecord() in metadataOps is the single shared factory (restore healing + fork-time log copies). - InstantiationService tracks pending GlobalIdleValue constructions on the root container and exposes drainIdle(); doCreate drains them before markReady() so delayed services' constructor side effects (event subscriptions, journal registration) are live before the agent admits turns, with no reliance on incidental macrotask windows.
…ecycle-split # Conflicts: # packages/agent-core-v2/scripts/dep-graph/analyzer/analyze.ts # packages/agent-core-v2/src/agent/wireRecord/wireRecordService.ts # packages/agent-core-v2/src/app/sessionLifecycle/sessionLifecycleService.ts # packages/agent-core-v2/src/persistence/backends/node-fs/appendLogStore.ts # packages/agent-core-v2/src/session/agentLifecycle/agentLifecycle.ts # packages/agent-core-v2/src/session/agentLifecycle/agentLifecycleService.ts # packages/agent-core-v2/src/session/agentLifecycle/mainAgent.ts # packages/agent-core-v2/src/session/subagent/tools/agent.ts # packages/agent-core-v2/src/session/todo/sessionTodoService.ts # packages/agent-core-v2/test/persistence/backends/node-fs/appendLogStore.test.ts # packages/agent-core-v2/test/session/agentLifecycle/agentLifecycle.test.ts
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
Related Issue
No linked issue — internal refactor plus a crash fix; the problem is explained below.
Problem
/goal <text>on the web's new-conversation page crashed the v2 backend process. A fire-and-forgetGET /statusstarted main-agent creation, and the subsequentPOST /profile/POST /promptscalls received the not-yet-ready agent handle (registered beforemarkReady()). Enqueueing the prompt then hitactivity.beginwhile the lane was stillinitializing, and the coded error escaped the fire-and-forget prompt scheduler as an unhandled rejection that killed the process.IAgentLifecycleServicehad become a 500-line grab bag: registry, creation pipeline, agent runs, the session MCP subsystem, and main-only events all lived in one service.What changed
create()is now create-or-get for explicit ids: a concurrent in-flight creation is joined (never a duplicate scope) and an already-created agent is returned as-is, so the main-agent bootstrap can no longer race the first prompt. This removes the crash at its root.create/fork/get/list/remove+onDidCreate/onDidDispose.subagentdomain (ISessionSubagentService):run()plus the SubagentStart/SubagentStop hook surface, withrunAgentTurn/mirrorAgentRun/ the Agent tool moved undersession/subagent/.session/mcpdomain (ISessionMcpService): the session-wide shared MCP connection manager, initial connect, and telemetry.onDidCreateMain/notifyMainCreated: subscribers filteronDidCreateby id andAgentPluginServiceself-gates on main;ensureMainAgentstays as a thin helper overcreate({ agentId: MAIN_AGENT_ID }).IAgentScopeContext: a newAgentWireServiceadapter derives its log addressing, andAgentMcpServiceresolves the session manager and originals dir itself. The default permission mode is now applied inbindBootstrap.Checklist
gen-changesetsskill (@moonshot-ai/kimi-code: patch).