Skip to content

refactor(agent-core-v2): extract swarm into a scope-organized feature - #2874

Merged
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:refact/swarm-featurize
Aug 13, 2026
Merged

refactor(agent-core-v2): extract swarm into a scope-organized feature#2874
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:refact/swarm-featurize

Conversation

@sailist

@sailist sailist commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — internal structural refactor; see Problem below.

Problem

In agent-core-v2, the swarm capability is scattered across three directories — src/agent/swarm/ (swarm mode), src/session/swarm/ (batch scheduling), and src/agent/tools/agent-swarm/ (AgentSwarm tool) — each self-registering through static registerScopedService / registerAgentToolService side effects. The repo has standardized on self-contained Feature units (registerFeature + IFeatureAssemblyService, see packages/agent-core-v2/docs/features.md; plan was the first port), and swarm should follow the same mechanism so features are added, removed, and assembled uniformly.

What changed

Pure structural move — zero runtime behavior change:

  • Feature unit: the three directories move to src/features/swarm/{agent,session,tools/agent-swarm}. New swarmFeature.ts (SwarmFeature extends Feature, module-level registerFeature) carries the three runtime registrations (IAgentSwarmService, ISessionSwarmService, IAgentSwarmTool) via contribute*, preserving ScopeActivation.OnScopeCreated — the services subscribe to turn.ended / onBeforeExecuteTool in their constructors, so lazy activation would be a behavior change.
  • Static channels unchanged: swarmOps.ts (wire vocabulary SwarmModel, swarm_mode.enter / swarm_mode.exit) stays a static import=register channel, hoisted to the feature root per docs/features.md. Wire keys are unchanged, so session persistence/replay stays compatible.
  • Exports and imports: src/index.ts switches to precise leaf exports for the new paths (no barrel); import sites updated (sessionLegacyService, core-api, plus the deep-path imports in kap-server events-zod.ts and kimi-inspect panels.ts).
  • Tests: moved to test/features/swarm/ with import-path updates only. The test harness now re-asserts feature-contributed service overrides after scope creation so seeded stubs keep winning over feature contributions (previously guaranteed by static registration's skip-when-seeded semantics).

Verified: pnpm --filter @moonshot-ai/agent-core-v2 test (312 files / 4956 tests pass), pnpm --filter @moonshot-ai/kap-server build, pnpm --filter @moonshot-ai/agent-core-v2 lint:imports, and the wire/state/config manifest generation all pass; the regenerated wire manifest shows only the expected path-comment changes.

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. (refactor: existing tests moved and all passing)
  • Ran gen-changesets skill, or this PR needs no changeset. (no changeset: internal refactor with no published behavior change; the plan feature port shipped without one either)
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e74248b

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 13, 2026

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

commit: e74248b

@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: 817231acd6

ℹ️ 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".

this.contributeService(LifecycleScope.Session, ISessionSwarmService, SessionSwarmService, {
activation: ScopeActivation.OnScopeCreated,
});
this.contributeTool(IAgentSwarmTool, AgentSwarmTool, { name: 'AgentSwarm', domain: 'swarm' });

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 Keep AgentSwarm in the static tool universe

When AgentSwarm is contributed only through the feature record, it no longer appears in getAgentToolContributions(). SubagentTool.knownToolReferences() still uses that static table plus the caller agent's active registry to describe other subagent profiles; so if the current caller profile does not activate AgentSwarm (for example coder) and any workspace/session/global tool restriction makes buildProfileDescriptions() take its effectiveTools branch, the advertised tools for the agent subagent profile omit AgentSwarm even though spawned agents can still use it. Please keep a static descriptor entry for AgentSwarm or teach those descriptor-only readers to include feature-contributed tools.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in fbaf603. SubagentTool.knownToolReferences() now reads the full AgentToolContribution collection (static registrations and feature-contributed tools alike) instead of the static contribution table, so the per-profile tool listings keep advertising feature-contributed tools such as AgentSwarm even when the caller profile does not activate them and a workspace/session restriction forces explicit enumeration. Added a regression test covering exactly that case (caller profile without AgentSwarm + global tool restriction).

@sailist
sailist force-pushed the refact/swarm-featurize branch from c8c32ba to 714dafc Compare August 13, 2026 03:14
- move src/agent/swarm, src/session/swarm, and src/agent/tools/agent-swarm
  into src/features/swarm/{agent,session,tools/agent-swarm}; swarmOps.ts
  stays a static import=register wire channel at the feature root
- add SwarmFeature carrying the three runtime registrations
  (IAgentSwarmService, ISessionSwarmService, IAgentSwarmTool) with
  ScopeActivation.OnScopeCreated preserved
- switch src/index.ts to precise leaf exports and update import sites,
  including the kap-server and kimi-inspect deep-path imports
- move tests to test/features/swarm and re-assert service overrides in
  the test harness so stubs keep winning over feature contributions
…riptions

SubagentTool.knownToolReferences() now reads the full AgentToolContribution
collection (static registrations and feature contributions alike) instead
of the static contribution table. A caller profile that does not activate
a feature-contributed tool (e.g. AgentSwarm) no longer drops it from the
per-profile tool listings the description advertises for spawned profiles
when a workspace/session restriction forces explicit enumeration.

Add a regression test with a caller profile lacking AgentSwarm under a
global tool restriction.
- add sessionProfile.ts/sessionAgentConfig.ts route helpers that resume
  the session and dispatch title/metadata and the agent_config patch to
  the native v2 services directly
- drop updateProfile from ISessionLegacyService, leaving only the
  status rollup and the goal read in the legacy adapter
- wire shape and client-visible behavior unchanged
@sailist
sailist force-pushed the refact/swarm-featurize branch from 714dafc to e74248b Compare August 13, 2026 05:10
@sailist
sailist merged commit 314b394 into MoonshotAI:main Aug 13, 2026
14 checks passed
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