Skip to content

refactor(agent-core-v2): register tools as scoped services#2196

Merged
sailist merged 4 commits into
MoonshotAI:mainfrom
sailist:refactor/agent-core-v2-tool-services
Jul 26, 2026
Merged

refactor(agent-core-v2): register tools as scoped services#2196
sailist merged 4 commits into
MoonshotAI:mainfrom
sailist:refactor/agent-core-v2-tool-services

Conversation

@sailist

@sailist sailist commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Register built-in agent tools as Agent-scoped DI services with profile-aware activation, centralize their contracts and implementations under agent/tools, and replace legacy delayed DI proxies with explicit scope activation modes.

Related Issue

No linked issue. The problem and rationale are described below.


1. Agent tool ownership and activation

Problem: Built-in tools were spread across feature domains and constructed through separate registrar paths, which made their ownership, profile filtering, and activation lifecycle inconsistent with the scoped DI model.

What was done:

  • Centralized built-in tool contracts and implementations under agent/tools.
  • Registered tool implementations as Agent-scoped DI services and added profile-aware activation.
  • Kept tool construction out of the general scope-creation pass and synchronized activated tools with the tool registry.

2. Scoped service construction

Problem: The legacy eager/delayed descriptor flag combined scope activation with proxy-based lazy initialization, including an idle callback path that obscured when real service instances were created.

What was done:

  • Added explicit OnScopeCreated and OnDemand activation modes.
  • Removed delayed proxies and idle initialization so first resolution creates and caches the real instance directly.
  • Migrated service registrations, tests, internal DI documentation, and authoring guidance to the new activation model.

What changed

  • Added focused coverage for profile-driven tool activation and scoped construction behavior.
  • Updated existing tool, scope, and integration tests for the unified registration model.
  • Verified the agent-core-v2 test suite, type checking, and domain-layer checks locally.

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 fix is effective or that my feature works.
  • Ran gen-changesets skill; this internal engine refactor needs no changeset.
  • Ran gen-docs skill, or this PR needs no user documentation update.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

sailist added 4 commits July 26, 2026 10:14
…file-aware activation

- replace module-level registerTool + Eager AgentBuiltinToolsRegistrar with registerAgentTool double registration (Agent-scope DI service + contribution table)
- add toolActivation domain: AgentToolActivationService filters contributions by the bound Profile's tool policy using declared names, resolves instances lazily via accessor.get, and re-activates on agent.status.updated
- rename BuiltinTool to AgentTool service interface; tools become Agent-scope services with decorator-injected dependencies (e.g. AgentTool -> SubagentTool/ISubagentTool)
- AgentLifecycleService.create runs one activation pass after restore and profile binding so tools reflect the Profile before the first turn
- update tool registrations, scripts, and tests; add toolActivationService tests
- move builtin tools from scattered domain folders (plan/tools,
  goal/tools, os/backends/node-local/tools, task/tools, etc.) into a
  unified agent/tools/ directory
- split each tool into a kebab-case definition file (bash.ts) and a
  registration file (bashTool.ts) pairing with its prompt markdown
- update imports across src, tests, kap-server, and TUI comments
Main's instantiateAll constructs every registered service at scope
creation, but agent tool constructors may legitimately throw when their
host capability is absent (e.g. WebSearchTool without a configured
provider), and profile-aware activation must stay the only resolution
path so the runtime registry holds real instances, never proxies.

- add SyncDescriptor.instantiateWithScope (default true) and let
  registerScopedService opt registrations out of the instantiateAll
  sweep
- registerAgentTool passes the opt-out, restoring lazy
  activation-driven construction on top of the eager-scope semantics
- regenerate docs/state-manifest.d.ts
- add explicit OnScopeCreated and OnDemand activation modes
- remove delayed proxy and idle initialization support
- migrate service registrations, tests, and DI guidance
@changeset-bot

changeset-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b49838b

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 Jul 26, 2026

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

commit: b49838b

@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: b49838bcc1

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

Comment on lines +47 to +48
eventBus.subscribe('agent.status.updated', () => {
void this.activate();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reactivate tools after installing the new allowlist

When a live agent changes profiles or calls update({ activeToolNames }), this subscription does not observe the new allowlist: useProfile() publishes agent.status.updated before setActiveTools(profile.tools), while an active-tools-only update publishes no status event at all. Consequently, a newly allowed contribution remains absent from the runtime registry for the next model request, so request-time policy cannot expose it; trigger activation after the active-tool state changes instead.

Useful? React with 👍 / 👎.

Comment on lines +195 to +197
// Activate the AgentTool contributions allowed by the bound Profile
// before the handle admits turns: restore and binding own the final
// `activeToolNames`, so this must run after both.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Move the activation sequencing note into the file header

This newly added statement-level comment violates the package convention that comments may appear only in the top-of-file header; incorporate the sequencing responsibility into that header or make the ordering self-explanatory through code structure.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

Comment on lines +781 to +783
// The registry only holds tools the bound Profile activated; the
// contribution table is the full static universe, so a valid-but-inactive
// name never trips the unknown-tool warning.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Move the contribution-table rationale into the file header

This method-local explanatory comment is newly introduced despite the package rule requiring all comments to live solely in the top-of-file header; move the repository-versus-runtime-registry responsibility there or encode it in a named helper.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

@sailist
sailist merged commit bf8e967 into MoonshotAI:main Jul 26, 2026
15 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