Skip to content

fix(agent-core-v2): initialize loop after profile and mode selection - #5141

Merged
sailist merged 7 commits into
mainfrom
refact-376-11-15-tool-filtering-comments
Nov 24, 2026
Merged

sailist merged 7 commits into
mainfrom
refact-376-11-15-tool-filtering-comments

Conversation

@sailist

@sailist sailist commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

本地 SDK / TUI 新建会话时,Loop 会在默认 profile 绑定前启动,使初始工具 baseline 没有按 profile 过滤。启动后再进入 Tower 也只改变 active-tool overlay,导致 TowerSpawn 等工具未进入模型的静态工具列表。

本 PR 让 profile 与初始模式选择先于工具 baseline 初始化,同时保留旧会话恢复后 pending 工作自动继续的语义。

Bug Reproduction Steps

  1. 通过本地 SDK / TUI 创建会话,在默认 profile 绑定前完成 Loop 初始化,得到未按 profile 过滤的工具 baseline。
  2. 在会话已经开始工作后进入 Tower。
  3. 检查首个 Tower 请求:Tower 工具只进入运行时 active-tool overlay,没有进入启动时固定的工具 baseline,因此 TUI 与 GUI 的模型可见工具不同。

Root Cause

根因是初始化顺序:新建 agent 过早启动 Loop,而 Tower 后续激活只扩大运行时策略,不会也不应该在请求期重建工具 baseline。

这是生命周期修复,不是在请求期合并新增工具的绕过方案。新建会话延迟到首次实际工作才启动;恢复会话因输入与 profile 已齐全而立即启动;未激活的 Tower 在启动后禁止新的 enter。

Code Changes

核心思路是让工具 baseline 只在信息齐全时初始化一次,而不是在后续请求中修补它。

flowchart TD
    A[创建 agent] --> B{存在持久化状态}
    B -- 否 --> C[绑定 profile 并选择初始模式]
    C --> D[首次 submit / notify / operation]
    B -- 是 --> E[恢复后立即启动]
    D --> F[初始化一次工具 baseline]
    E --> F
    F --> G{Tower 是否已启动前激活}
    G -- 是 --> H[Tower 工具进入初始 baseline]
    G -- 否 --> I[拒绝 loop-already-started]
Loading
决策 选择 未选择 理由
新会话启动时机 首次提交、通知或 operation 请求期合并后续 active 工具 避免请求期工具列表变化及缓存失效
旧会话恢复 恢复完成后立即启动 与新会话一样延迟 恢复信息已齐全,且需要继续 pending turn / inbox
启动后进入 Tower 拒绝新的 enter,并提示创建新会话 重建已有 baseline 保持 baseline 不可变;已激活 Tower 的重复 enter 仍保持幂等

主要改动:

  • 增加 Loop 启动状态与持久化状态判断;新建 agent 延迟启动,恢复 agent 立即启动,restore 前的提交保持排队。
  • compaction、goal continuation 与 SDK context import 显式等待启动,避免延迟初始化后出现未启动错误。
  • Tower 启动前激活的工具进入初始 baseline;启动后新的 enter 返回 loop-already-started,并补充 telemetry 原因与中英文指南。
  • 补充工具注册、策略筛选和 baseline 语义的说明,以及 @moonshot-ai/agent-core-v2 的 patch changeset。

阅读顺序:先看 agentLifecycleService.ts 的创建与恢复分流,再看 loopService.ts 的启动入口,最后看 towerService.ts 的启动门禁。其余为测试、注释、文档和 changeset。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖旧行为的人群 逃生口
新建空 agent 的启动时机 创建时立即启动,可能早于默认 profile 绑定 首次 submit、notify 或 operation 才启动 本地 SDK / TUI 的新会话 无;这是初始化顺序纠正
有持久化状态的 agent 创建时启动 恢复完成后仍立即启动 旧版本写入的会话、pending turn / inbox 不需要;旧恢复语义保留
未激活 Tower 在启动后 enter 允许 enter,但新增工具不进入已有 baseline 返回 loop-already-started 启用 Tower 实验的 TUI、Desktop / Web 用户 在新会话首次工作前开启 Tower
已激活 Tower 重复 enter 幂等返回成功 保持幂等返回成功 恢复或重复开启 Tower 的会话 不需要
operation、goal continuation、context import 假设 Loop 已启动 显式等待启动后继续 SDK、KAP、compaction 与 goal 调用方 不需要;原有工作继续执行
请求期工具 baseline 不合并后续 active 工具 仍不合并,baseline 保持稳定 所有客户端与旧会话 不需要;行为未变

验证结果:

  • pnpm lint:通过,包括注释检查、oxlint、导入边界和仓库守卫。
  • agent-core-v2、kap-server、kimi-code-sdk 三个包 typecheck:通过。
  • 5 个聚焦测试文件共 574 个用例全部通过,覆盖新建不启动、恢复立即启动、启动失败清理、operation 启动、Tower 启动前工具进入首个 LLM 请求、启动后拒绝 enter,以及 KAP 会话场景;测试声明总数净零。
  • pnpm -C docs run build:通过,中英文 Tower 指南已同步。
  • 未在本地运行全量测试;由 PR CI 继续覆盖。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 321dbdf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/agent-core-v2 Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T05:48:11.443394Z 321dbdf New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

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

commit: 321dbdf

@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: 3eb6c2c8c8

ℹ️ 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 331 to 335
if (this.startRequested) return this.startPromise;
this.startRequested = true;
if (this.loop !== undefined) return;
this.startPromise ??= this.initializeLoop(undefined, tools).finally(() => {
this.startPromise = undefined;

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 Allow loop initialization to retry after rejection

If initializeLoop() rejects during a fresh agent's first operation, the finally clears startPromise but leaves startRequested true, so every later start() returns undefined without creating this.loop. A subsequent requester then dereferences an absent loop, while callers such as importContext() can queue reminders and report success even though flushReminders() is a no-op, silently losing the imported context; reset the requested state after failure or gate this fast path on a successfully initialized loop.

AGENTS.md reference: AGENTS.md:L210-L212

Useful? React with 👍 / 👎.

Comment on lines +334 to +336
// Restored agents have all initial inputs and start immediately. New agents
// start on their first submission or operation, after initial mode selection.
if (hasPersistedState) await loop.start(opts.initialTools);

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 Start the loop before standalone shell commands

When an SDK user creates a fresh session and calls runShellCommand() before submitting a prompt, this branch now leaves the loop uninitialized and the shell-command path never calls start(). Its input/output records are therefore left only in pendingReminders; history() cannot see them, flushReminders() returns immediately, and closing the session discards them, breaking the existing shell-command history contract for that client flow.

AGENTS.md reference: AGENTS.md:L190-L192

Useful? React with 👍 / 👎.

Comment on lines +334 to +336
// Restored agents have all initial inputs and start immediately. New agents
// start on their first submission or operation, after initial mode selection.
if (hasPersistedState) await loop.start(opts.initialTools);

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 Preserve explicit initial tools across deferred startup

For a newly forked agent, fork() supplies sourceLoop.baselineTools() through opts.initialTools, but hasPersistedState is false and this is now the only place that forwards those tools to start(). The later initializeHistory() or first submission starts without the argument and recomputes tools from current policy, so a parent whose frozen baseline differs from the current profile/registry produces a child with a different LLM tool schema instead of fork parity; retain the supplied baseline until deferred startup.

AGENTS.md reference: AGENTS.md:L206-L208

Useful? React with 👍 / 👎.

@boxcounter

boxcounter commented Nov 21, 2026 •

Copy link
Copy Markdown

我在 Kimi Code (macOS app)里遇到同样的问题。

以下是我的 agent 在我本地环境里的验证:
(它建议我新建会话,在对话之前先开启 tower 模式。但我测试发现这个建议无效,包括重启 Kimi Code -> 新会话 -> /tower on -> 开始对话,也会遇到当前这个 PR 里描述的问题,即 tower mode 的工具不完整)


已确认:本会话确实命中了 PR #5141 描述的工具基线问题,不是工具被配置禁用。

本会话 wire.jsonl 的直接证据:

  • tool_select.initialized 登记了 131 个工具,其中包含全部 13 个 Tower 工具。
  • 实际发给模型的 llm.tools_snapshot 只有 120 个工具,Tower 工具只剩:
    • TowerInit
    • TowerStatus
    • TowerTeardown
  • 缺少的 11 个工具中,10 个是 TowerPlan、TowerSpawn、TowerInbox、TowerMerge 等核心工具;另 1 个是独立 deferred 加载的 mcp__desktop_browser__run,与 Tower 问题无关。
  • disallowedTools 是空数组,没有任何工具被显式禁用。

事件顺序也坐实了触发机制:

  1. profile.bind 先生成,其 activeToolNames 里只有上述 3 个 Tower 工具。
  2. 约 120ms 后才发生 tower_mode.enter。
  3. 随后的工具初始化和模型快照仍沿用不完整的 profile 工具集,没有补入其余 10 个 Tower 工具。

这与 PR #5141 的根因描述一致:Tower 后续激活改变了运行时 overlay,但没有重建模型静态工具 baseline。

因此当前会话继续等待或重试不会补挂这些工具。有效规避方式是:新建会话,先执行 /tower on,再提交第一条 Lumir 工作指令。

@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch from 3eb6c2c to f0d43c4 Compare November 21, 2026 05:41
@sailist
sailist changed the base branch from main to loop-refact November 21, 2026 05:41

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

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

await loop.start(opts.initialTools);
// Restored agents have all initial inputs and start immediately. New agents
// start on their first submission or operation, after initial mode selection.
if (hasPersistedState) await loop.start(opts.initialTools);

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 Start after queuing pre-restore submissions

When an onDidCreateScope listener submits or notifies synchronously, admit() sees that restoration is not ready and only queues the input; hasPersistedState does not include these in-memory queues, so this branch skips startup and the prompt never launches unless another input happens to arrive later. The old unconditional startup drained this supported creation-time path, so startup should also be triggered when pending in-memory requests exist after restoration.

AGENTS.md reference: AGENTS.md:L191-L193

Useful? React with 👍 / 👎.

Comment on lines 359 to 360
this.startPromise ??= this.initializeLoop(undefined, tools).finally(() => {
this.startPromise = undefined;

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 Serialize close with deferred initialization

When a client submits the first prompt and immediately closes or deletes the session, admit() starts this asynchronous initialization without awaiting it, while close() does not wait for startPromise. If initialization has not assigned this.loop yet, shutdown skips loop.close(), clears the admitted prompt, and can then race with initializeLoop() installing a loop after the scope has been closed; startup must be cancelled or awaited before shutdown finishes.

Useful? React with 👍 / 👎.

Comment on lines 436 to 437
if (!this.isActive && this.loop.started) return { entered: false, reason: 'loop-already-started' };
const owner = await this.resolveTowerOwner();

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 Make Tower activation atomic with first startup

When two clients share a fresh session, or a background notification arrives while /tower on is resolving its owner or preparing the base, the loop can start after this one-time check but before the Tower tools are added at the end of resolveEnter(). The command then reports success and persists Tower mode, yet initialization has already frozen a baseline without TowerSpawn and the other Tower schemas, recreating the failure this change is intended to prevent; recheck or reserve startup across the awaited activation sequence.

AGENTS.md reference: AGENTS.md:L111-L116

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch 2 times, most recently from 5a0c98a to 55e63e5 Compare November 21, 2026 13:41

@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: 55e63e5a16

ℹ️ 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 thread docs/en/guides/tower.md Outdated

If HEAD is detached (no branch is checked out), or you need to change the base, run `/tower <base-branch>` instead, for example `/tower feature/my-objective`.

Activate Tower before sending the first task in a session. After work has started, `/tower on` and `/tower <base-branch>` cannot newly enable Tower; create a new session and activate Tower before sending work.

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 Update Tower re-entry guidance for the new restriction

When Tower exits via /tower off or because Goal, Plan, or swarm mode is activated, the new startup gate makes /tower on fail for the rest of that session, but this guide still says at line 118 that users can return later with /tower on, and packages/agent-core-v2/src/features/tower/injection/tower-mode-exit-reminder.md explicitly tells them to re-enter that way. Update both messages so Tower users are not directed into a recovery path that is now guaranteed to fail.

AGENTS.md reference: AGENTS.md:L199-L201

Useful? React with 👍 / 👎.

Comment on lines +1 to +3
---
'@moonshot-ai/agent-core-v2': patch
---

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 Add changesets for the affected products

This only records an @moonshot-ai/agent-core-v2 library release, although the new Tower activation and re-entry restriction is directly visible to CLI and desktop/web users through their /tower and session-configuration flows. Add product changesets for @moonshot-ai/kimi-code and kimi-code-app; otherwise their release notes omit the workflow-breaking requirement to enable Tower before the loop starts.

AGENTS.md reference: AGENTS.md:L169-L170

Useful? React with 👍 / 👎.

await this.wire.drainPersisted().catch(() => undefined);
const snapshot = this.journal.snapshot();
this.profileAdapter.restore(snapshot);
// Capture the baseline once; createLoopFromSnapshot stores it in the loop profile.

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 Remove the newly added implementation comments

This commit adds implementation narration here and in profileAdapter.ts, toolActivationService.ts, evaluate.ts, toolSelectService.ts, towerService.ts, agentLifecycleService.ts, and sdk-rpc-client-v2.ts, despite the repository-wide rule that JS/TS code must not contain explanatory comments and should express these constraints through naming, types, tests, or lint rules instead. These paths being present in .commentignore only prevents the automated check from catching the violation; remove the newly introduced comments rather than relying on that exemption.

AGENTS.md reference: AGENTS.md:L79-L82

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch from 55e63e5 to 94c4150 Compare November 23, 2026 08:23
@sailist
sailist changed the base branch from refact-421-11-23-loop to main November 23, 2026 08:23
@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch from 94c4150 to 4006756 Compare November 23, 2026 08:27

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

ℹ️ 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 +922 to +924
const registration = this.toolRegistry.register(accessor.get(tool.id), {
disclosure: 'deferred',
});

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 Keep Tower tools active for restricted main profiles

When KAP session creation selects a restricted main profile such as coder or explore together with tower_mode, enter() now only registers these tools; it no longer calls addActiveTool() as the previous implementation did. AgentToolSelectService.activeEntries() and execution policy subsequently filter every Tower tool because those profiles do not list them, so Tower reports successful activation while exposing no usable orchestration tools. Preserve the activation overlay or otherwise make Tower tools active for every profile from which enter() is supported.

AGENTS.md reference: AGENTS.md:L191-L193

Useful? React with 👍 / 👎.

Comment on lines +523 to 524
this.unregisterTowerTools();
void this.dispatcher.dispatch(new TowerModeExit({ agentId: this.agentCtx.agentId }));

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 Stop advertising unloaded Tower tools to static-tool models

If Tower was enabled before startup on a model without dynamic tool loading, its deferred schemas are frozen into the loop baseline; after /tower off, this unregisters the executors but requestTools(..., false) still strips only the deferred marker and sends those schemas on later requests. The model can therefore keep selecting tools that now fail as unavailable, contrary to the new exit reminder and changeset claiming they were unloaded; retain executors for that capability or filter the static baseline when exiting.

AGENTS.md reference: AGENTS.md:L191-L193

Useful? React with 👍 / 👎.

Comment on lines +2330 to 2332
await loop.start();
loop.remind(historyEntryFromContext(message));
loop.flushReminders();

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 Recheck import idleness after starting the loop

When two SDK clients share a fresh session, one can call importContext() while the other submits the first prompt: the import passes the busy check, yields here while deferred initialization runs, and the prompt can start a turn before this await resolves. The import then appends and flushes its context into that live turn instead of returning TURN_AGENT_BUSY, violating the method's existing busy contract; start before checking or repeat the busy check after this await.

AGENTS.md reference: AGENTS.md:L111-L116

Useful? React with 👍 / 👎.

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

ℹ️ 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 +922 to +924
const registration = this.toolRegistry.register(accessor.get(tool.id), {
disclosure: 'deferred',
});

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 Skip Tower tools already registered by the active profile

When a KAP or SDK caller creates the main agent with the tower-worker profile, tool activation has already registered the six worker-shared Tower tools contributed in towerFeature.ts. /tower on then registers the main-only tools but throws when it reaches the existing TowerSend entry because the registry rejects duplicate names. This leaves Tower inactive after adoptTowerRoster() has claimed workspace ownership and after several partial registrations; reuse existing registrations or register only currently absent tools.

AGENTS.md reference: AGENTS.md:L191-L193

Useful? React with 👍 / 👎.

Start new agents on their first submission or operation while restoring
persisted agents immediately. Gate Tower activation after startup so its
tools enter the initial baseline.
Derive startup state from the loop and its initialization promise, allow
failed starts to retry, and start source agents before forking. Preserve
initial tool baselines and align reminder and history initialization tests.
Start loops before driving reminder hooks and seeding browser histories so
deferred initialization cannot leave reminder assertions empty.
@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch from df95ddc to 19a302e Compare November 24, 2026 05:28

@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: 19a302e7df

ℹ️ 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 +398 to 399
await context.runtime.get(IAgentLoopService).start();
launchContinuationTurn(context, state.goalId);

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 Recheck goal idleness after loop startup

When a fresh session has a paused or blocked goal and one KAP client resumes it with continuation while another client submits the first prompt, canLaunchContinuation() can pass before this await, but start() yields long enough for the prompt to begin a turn. launchContinuationTurn() then queues a separate goal-continuation prompt instead of attaching continuation state to the now-live turn, causing the user turn and an unintended follow-up continuation to run sequentially; recheck idleness after startup before launching.

AGENTS.md reference: AGENTS.md:L112-L117

Useful? React with 👍 / 👎.

Comment on lines 345 to +346
stage = 'start';
await loop.start(opts.initialTools);
if (shouldStartImmediately) await loop.start(opts.initialTools);

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 Wait for MCP discovery before serving context tools

For a newly created session with configured MCP servers, this branch skips loop.start(), so an immediate KAP GET .../context/summary or GET .../context/items reads previewRequestContent() without awaiting ISessionMcpHandle.ready. Because MCP schemas are registered only as servers become connected, these endpoints can omit tool definitions and undercount the context even though creation previously awaited startup and MCP discovery; preserve that readiness guarantee for context readers without necessarily starting a turn.

AGENTS.md reference: AGENTS.md:L192-L194

Useful? React with 👍 / 👎.

@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: 321dbdf0c2

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

private admit(input: UserMessage): { readonly id: string; readonly entry: UserMessage } {
if (this.disposing) throw abortError('Agent loop disposed');
if (this.dispatcher.restorePhase === 'ready') {
void this.start().catch(() => this.close().catch(() => undefined));

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 Propagate first-start failures to prompt callers

When deferred initialization rejects—for example because MCP readiness or task-history reconciliation fails—this fire-and-forget catch discards the error and closes the loop. prompts.close() then resolves the admitted prompt's launched promise as undefined, so submitClient() and the REST gateway can return without an error even though the first prompt was cancelled and never ran. Await startup as part of submission or otherwise propagate its rejection to the caller instead of converting it into a silent close.

AGENTS.md reference: AGENTS.md:L212-L214

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the refact-376-11-15-tool-filtering-comments branch from fed5d09 to 321dbdf Compare November 24, 2026 06:22
@sailist
sailist merged commit de1a3b6 into main Nov 24, 2026
70 of 72 checks passed
@sailist
sailist deleted the refact-376-11-15-tool-filtering-comments branch November 24, 2026 09:40
7Sageer pushed a commit that referenced this pull request Nov 25, 2026
…minal (#5345) (#5369)

* fix(agent-core-v2): initialize loop after profile and mode selection (#5141)

* docs: clarify tool registration and filtering

* fix(agent-core-v2): initialize loop after profile and mode selection

Start new agents on their first submission or operation while restoring
persisted agents immediately. Gate Tower activation after startup so its
tools enter the initial baseline.

* fix(agent-core-v2): restore deferred loop initialization

Derive startup state from the loop and its initialization promise, allow
failed starts to retry, and start source agents before forking. Preserve
initial tool baselines and align reminder and history initialization tests.

* test(desktop): initialize loops for browser reminder tests

Start loops before driving reminder hooks and seeding browser histories so
deferred initialization cannot leave reminder assertions empty.

* feat(agent-core-v2): toggle tower mode mid-session with deferred tools

* test(agent-core-v2): cover tower deferred tools end to end

* test(agent-core-v2): script tower tests with the testkit model

* refactor(core): replace loop.drained/loop.idle with loop end info on turn.ended (#5337)

* fix(llm): validate deferred tool arguments against the visible tool view (#5231)

* fix(desktop): resolve ssh.exe absolute path on Windows for remote terminal (#5345)

---------

Co-authored-by: Haozhe <[email protected]>
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
…minal (#5345) (#5369)

* fix(agent-core-v2): initialize loop after profile and mode selection (#5141)

* docs: clarify tool registration and filtering

* fix(agent-core-v2): initialize loop after profile and mode selection

Start new agents on their first submission or operation while restoring
persisted agents immediately. Gate Tower activation after startup so its
tools enter the initial baseline.

* fix(agent-core-v2): restore deferred loop initialization

Derive startup state from the loop and its initialization promise, allow
failed starts to retry, and start source agents before forking. Preserve
initial tool baselines and align reminder and history initialization tests.

* test(desktop): initialize loops for browser reminder tests

Start loops before driving reminder hooks and seeding browser histories so
deferred initialization cannot leave reminder assertions empty.

* feat(agent-core-v2): toggle tower mode mid-session with deferred tools

* test(agent-core-v2): cover tower deferred tools end to end

* test(agent-core-v2): script tower tests with the testkit model

* refactor(core): replace loop.drained/loop.idle with loop end info on turn.ended (#5337)

* fix(llm): validate deferred tool arguments against the visible tool view (#5231)

* fix(desktop): resolve ssh.exe absolute path on Windows for remote terminal (#5345)

---------

Co-authored-by: Haozhe <[email protected]>
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
…minal (#5345) (#5369)

* fix(agent-core-v2): initialize loop after profile and mode selection (#5141)

* docs: clarify tool registration and filtering

* fix(agent-core-v2): initialize loop after profile and mode selection

Start new agents on their first submission or operation while restoring
persisted agents immediately. Gate Tower activation after startup so its
tools enter the initial baseline.

* fix(agent-core-v2): restore deferred loop initialization

Derive startup state from the loop and its initialization promise, allow
failed starts to retry, and start source agents before forking. Preserve
initial tool baselines and align reminder and history initialization tests.

* test(desktop): initialize loops for browser reminder tests

Start loops before driving reminder hooks and seeding browser histories so
deferred initialization cannot leave reminder assertions empty.

* feat(agent-core-v2): toggle tower mode mid-session with deferred tools

* test(agent-core-v2): cover tower deferred tools end to end

* test(agent-core-v2): script tower tests with the testkit model

* refactor(core): replace loop.drained/loop.idle with loop end info on turn.ended (#5337)

* fix(llm): validate deferred tool arguments against the visible tool view (#5231)

* fix(desktop): resolve ssh.exe absolute path on Windows for remote terminal (#5345)

---------

Co-authored-by: Haozhe <[email protected]>
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
…oonshotAI#5141)

Port upstream de1a3b6: the loop starts lazily only when persisted state, a profile or tools require it; Tower registers its tools as deferred disclosures on enter and unregisters them on exit; profile tool tables and Tower reminders move to the deferred contract.
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.

3 participants