Skip to content

refactor(agent-core-v2): migrate agent domains to the agent runtime architecture - #3303

Open
sailist wants to merge 14 commits into
MoonshotAI:mainfrom
sailist:refact-089-08-25-remaining-agent-domains
Open

refactor(agent-core-v2): migrate agent domains to the agent runtime architecture#3303
sailist wants to merge 14 commits into
MoonshotAI:mainfrom
sailist:refact-089-08-25-remaining-agent-domains

Conversation

@sailist

@sailist sailist commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — internal architecture migration (the agent-core-v2 Agent Runtime standard in docs/features.md).

Problem

Agent-granular domains in agent-core-v2 were Agent-scoped DI services (IAgent*Service) with mutable registration surfaces and ad-hoc cross-domain runtime.get(...) edges. The Agent Runtime standard replaces this with per-domain opaque contract tokens, runtime-owned authoritative state, and resolve-only cross-domain access. This PR migrates the remaining agent domains to that architecture.

What changed

  • Runtime infrastructure: agent runtime durable definitions gained custom onUndo and blob dehydrate/rehydrate channels; the shared agent event vocabulary moved to a common location.
  • Domain migrations: contextMemory, tokenCounting, usage, permissionRules, permissionMode, profile, llmRequester, toolExecutor, loop, prompt, fullCompaction, and undo each moved to a self-contained Agent Runtime domain under src/features/ behind an opaque contract token. The legacy AgentSpace / defineAgentModel agent-model system was removed entirely.
  • Tools consolidation: the agent-side tool registry, selection, activation, policy, and MCP services were consolidated into a single AgentTools runtime. Plain and MCP tools enter as external provider/source contributions; the catalog, deferred disclosure/selection, and execution pipeline are internal to the runtime.
  • Dependency direction: migrated runtimes resolve each other via manager.resolve(agentContext, Contract); transitional bridges live inside not-yet-migrated legacy services and are removed as those domains land.
  • Wire compatibility: all durable Event2 type strings, zod schemas, replay/fold semantics, and journal paths stay byte-identical. Downstream edges (kap-server, klient, node-sdk, kimi-inspect, apps) were adapted to the new contracts while keeping public RPC wire names and payloads.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (internal refactor, no issue).
  • I have added tests that prove my feature works (each migrated domain carries its suite to test/features/<domain>/ with case names preserved).
  • Ran gen-changesets skill, or this PR needs no changeset — needs none: internal refactor, wire protocol and CLI behavior unchanged.
  • Ran gen-docs skill, or this PR needs no doc update — in-repo docs (target/ domain docs, manifests) were updated alongside the code.

@changeset-bot

changeset-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 4ea7774

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

@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: 4ea77749a6

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

.get(IAgentLifecycleService)
.resolve(agentContextOf(agent), AgentFullCompaction);
return {
begin: ([input]: [{ source?: 'manual' | 'auto'; instruction?: string }]) => {

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 Accept the compaction payload directly

When session(...).agent(...).compact() uses the memory or IPC transport, the facade supplies the compaction object as the sole argument and the dispatcher spreads that argument list into this function. Destructuring the received object as [input] therefore throws TypeError: object is not iterable before runtime.begin() runs, breaking every compaction request over both transports; accept the payload object directly instead.

AGENTS.md reference: packages/klient/AGENTS.md:L21-L25

Useful? React with 👍 / 👎.

const agent = resolved.like as IAgentScopeHandle;
const runtime = agent.accessor.get(IAgentLifecycleService).resolve(agentContextOf(agent), AgentLoop);
return {
cancelFromUser: () => runtime.cancelByUser(),

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 Preserve the requested turn ID when cancelling

When a caller uses agent.cancel({ turnId }), the contract forwards that ID specifically to avoid cancelling an unrelated turn, but this adapter drops the argument and unconditionally cancels whichever turn is current. A delayed cancellation for an already-finished turn can consequently terminate a newer turn; forward the optional ID to a turn-aware cancellation path or reject it if that behavior is no longer supported.

AGENTS.md reference: packages/klient/AGENTS.md:L14-L20

Useful? React with 👍 / 👎.

if (sessionId === null) return;
try {
await klient.session(sessionId).agent(agentId).service(IAgentLoopService).cancelFromUser();
await (klient.session(sessionId).agent(agentId).service('agentLoopService') as { cancelFromUser(): Promise<void> }).cancelFromUser();

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 Route inspector cancellation to an available service

When a turn is running and the inspector's Cancel action is clicked, this call always fails with 40001 unknown service: the migration removes the Agent-scoped agentLoopService, while kap-server's debug dispatcher only adds a compatibility view for agentPromptService (packages/kap-server/src/transport/dispatcher.ts:101-109). Add an AgentLoop debug adapter or invoke an existing cancellation route so the inspector can still stop turns.

AGENTS.md reference: apps/kimi-inspect/AGENTS.md:L22-L24

Useful? React with 👍 / 👎.

Comment on lines +345 to +346
const settlement = watchPromptSettlements(resolved.events);
settlement.settle(result.promptId, () => preparedMedia?.discard());

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 Subscribe before submitting media-backed prompts

When an external UserPromptSubmit hook blocks a prompt containing staged media, AgentPrompt.submit() emits prompt.completed before returning, but the settlement watcher is only created afterward. It therefore never observes that completion, enqueued remains true so the catch cleanup is bypassed, and the temporary IFileService objects owned by preparedMedia are never discarded; create and arm the watcher before awaiting submission, as the bundled-skill path already does.

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