Skip to content

fix(kimi-code): load workspace directories before session - #4515

Merged
liruifengv merged 4 commits into
mainfrom
fix/tui-multi-root-file-mentions
Oct 29, 2026
Merged

liruifengv merged 4 commits into
mainfrom
fix/tui-multi-root-file-mentions

Conversation

@liruifengv

@liruifengv liruifengv commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

TUI @ file mentions miss files from additional workspace directories before the first session is created.

Bug Reproduction Steps

  1. Configure directory B as an additional workspace directory of directory A, using either legacy .kimi-code/local.toml or the workspace registry. Place a uniquely named file in B.
  2. Start the TUI in A without resuming or creating a session.
  3. Type @ followed by the file name before sending the first message. The B file is not suggested, even though it becomes available after a session is created.
  4. With the additional directory explicitly configured and fd unavailable, create more than 2,000 entries in A. The filesystem fallback exhausts its shared scan budget in A and never reaches B.

Root Cause

Sessionless startup seeded the mention provider only with CLI --add-dir values. Persisted directories were copied from session.summary.additionalDirs only after a session existed, so the existing pi-tui multi-root search never received them. This PR fixes the lifecycle gap by reading the workspace directory service before autocomplete is mounted, rather than creating a session early or reimplementing search.

The fallback also exhausted one shared 2,000-entry scan budget in traversal order. It now round-robins individual entries across active roots under the same global cap, so a large primary directory cannot starve additional roots and an idle root leaves capacity for the others.

Review follow-up separates the autocomplete-only startup snapshot from session-creation overrides. Initial create/resume passes only explicit CLI --add-dir values; the engine re-reads persisted directories at creation, so directories removed before the first prompt are not reintroduced. Missing Git Bash preserves sessionless startup with a notice, while genuine configuration errors still fail. Terminal cleanup after the trust prompt runs exactly once, and home-directory expansion matches the engine resolver.

Code Changes

KimiTUI.initMainTui
  init
+ if there is no session:
+   harness.getWorkspaceAdditionalDirs(workDir)
+     acquire workspace -> await dirs.ready -> snapshot -> release lease
+   normalize, merge, and deduplicate workspace and CLI directories
  setupAutocomplete
    FileMentionProvider
      fd: existing multi-root search
-     fallback: per-root budget multiplied by root count
+     fallback: shared global budget fairly allocated across roots
  • SDKRpcClientBase adds a non-abstract getWorkspaceAdditionalDirs capability. Its default implementation throws not_implemented; SDKRpcClientV2 returns the merged legacy-project, registry, and ephemeral directory set after awaiting readiness. It never silently substitutes an empty directory list.
  • First lazy creation and initial resume pass only explicit CLI directories and let the engine load the latest persisted configuration. Once a session has been bound, /new and current-session deletion replacements preserve live appState.additionalDirs; the first /new before any session still uses startup values.
  • CLI pending directories are preserved and deduplicated with the workspace snapshot. Main-root mentions remain relative; additional-root mentions remain absolute, and literal ~ / ~/ values expand consistently with the engine. Fallback roots are deduplicated by normalized absolute path before round-robin scanning, preserving the primary-root classification.
  • Regression tests cover stale snapshots, legacy config, registry data, readiness ordering, lease release, degraded startup, single-pass terminal cleanup, picker startup, CLI merging and resume, home expansion, actual editor completion, bounded fallback scanning, duplicate cwd roots, and live-directory preservation when deleting the current session.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Sessionless @ mention search Searched cwd and CLI --add-dir only Also searches persisted additional workspace directories TUI users with legacy local.toml or registry-managed source folders N/A — restores intended coverage without creating a session
Sessionless /add-dir list and CLI pending paths Showed only pending CLI paths, potentially empty and unnormalized Shows the merged workspace/CLI set with normalized, deduplicated paths TUI users inspecting directories before the first session or combining CLI flags with saved config Existing session-summary synchronization remains in place
Directory edits before initial create/resume The startup snapshot was passed back as an override, so a removed directory could be re-added as ephemeral Only explicit CLI directories override; the engine reads the latest persisted directory set TUI users who remove or edit workspace directories before the first prompt N/A — stale snapshots no longer override newer config
Current-session deletion after /add-dir The replacement could be mistaken for first lazy creation and lose unpersisted directories Session-binding history keeps replacements on live appState.additionalDirs while first creation still uses startup values TUI users deleting the current session after adding directories N/A — matches ordinary /new behavior
Directory-loading failures before startup Could remain hidden until session creation, or abort after the trust prompt had started the UI Missing Git Bash preserves sessionless startup with a notice; genuine config errors still fail, with one terminal cleanup pass Windows users without Git Bash, and users with malformed directory config Install Git Bash or correct the configuration; no silent cwd-only fallback
Filesystem fallback scanning One 2,000-entry budget shared by all roots The same global cap is round-robin across deduplicated active roots; UI still returns at most 50 candidates TUI users on systems without fd, especially with large or duplicate roots N/A — no unbounded scanning, duplicate-budget consumption, or per-root multiplication
Workspace directory discovery in the SDK No workspace-level directory query Adds getWorkspaceAdditionalDirs; unsupported backends reject explicitly TUI and other SDK consumers Existing SDK methods and v2 behavior are otherwise unchanged

Existing sessions, single-root projects, fd-backed ranking, mention insertion, slash commands, inline skills, and quoted mentions are covered by the unchanged existing test suites. No prompt text, wire schema, CLI flag, session format, or old configuration key was changed.

Verification:

  • 604 TUI tests passed, including new regressions for stale snapshots, degraded startup, single-pass cleanup, home expansion, resume overrides, shared fallback budgets, unused-capacity redistribution, duplicate cwd roots, and current-session deletion.
  • 84 node-sdk tests passed.
  • 1,171 pi-tui tests passed and 1 skipped.
  • App TypeScript check passed; targeted oxlint on the changed files reported 0 errors and 8 pre-existing warnings.
  • Real TUI acceptance passed: before any session existed, /add-dir list showed the additional directory, selecting its file inserted an absolute path, selecting a main-root file inserted a relative path, and shutdown exited with code 0.

Checklist

  • I have read the CONTRIBUTING document.
  • Related issue: N/A — no tracking issue for this internal bug fix.
  • 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.

Copilot AI balanced review requested due to automatic review settings October 29, 2026 08:53
@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d5baaa6

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/kimi-code 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

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T10:52:39.560193Z d5baaa6 Manual request
ℹ️ 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 Oct 29, 2026 •

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

commit: d5baaa6

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

ℹ️ 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 apps/kimi-code/src/tui/kimi-tui.ts Resolved
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved

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

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

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

ℹ️ 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 apps/kimi-code/src/tui/kimi-tui.ts Resolved
@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: d5baaa6784

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

@liruifengv
liruifengv merged commit df7efe9 into main Oct 29, 2026
28 of 29 checks passed
@liruifengv
liruifengv deleted the fix/tui-multi-root-file-mentions branch October 29, 2026 11:12
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of the mention-provider half of upstream MoonshotAI/kimi-code
df7efe9 (MoonshotAI#4515).

The filesystem fallback scanned one root to exhaustion before the next,
so a project directory holding more than the 2,000-entry cap starved the
source folders entirely: `@target-needle` found nothing even though the
file sat in a registered folder. The scan now interleaves the roots,
one entry at a time, so unused capacity on one root is spent on others.

`~` and `~/…` entries now resolve against the home directory instead of
being treated as relative paths, and a root equal to the working
directory is dropped instead of scanned twice.

The added case uses the existing 2,000-entry fixture and fails when the
scan is narrowed to the primary root. The working-directory case pins the
deduplication.

Also completes the MoonshotAI#4638 SDK half: `node-sdk` keeps its own error-code
table, so `wire.version_too_low` had to be added there as well; the
engine registry alone was not enough and the app typecheck caught it.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
d48f64d interleaves the mention fallback across roots, expands `~`
and dedupes a root equal to the working directory. The SDK's
getWorkspaceAdditionalDirs and the TUI's pre-session directory load were
already in the tree, so only the provider needed porting.

Ledger: closed 44 -> 45, remaining 54.
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