Skip to content

feat(kimi): modernize Kimi adapter for Kimi Code CLI v0.18.0+#939

Open
Jucomu3P wants to merge 8 commits into
Gentleman-Programming:mainfrom
Jucomu3P:feat/kimi-production-ready
Open

feat(kimi): modernize Kimi adapter for Kimi Code CLI v0.18.0+#939
Jucomu3P wants to merge 8 commits into
Gentleman-Programming:mainfrom
Jucomu3P:feat/kimi-production-ready

Conversation

@Jucomu3P

@Jucomu3P Jucomu3P commented Jun 20, 2026

Copy link
Copy Markdown

Summary

This PR updates the Kimi adapter in gentle-ai to target the modern Kimi Code CLI (@moonshot-ai/kimi-code, TypeScript/npm-based, v0.18.0+) instead of the legacy Python/uv-based kimi-cli.

Key Changes

1. Version Pinning (internal/versions/versions.go)

  • Adds Renovate-tracked Kimi = "0.18.0" constant for @moonshot-ai/kimi-code.

2. Install Resolver (internal/installcmd/resolver.go)

  • Replaces uv tool install --python 3.13 kimi-cli with npm install -g --ignore-scripts @moonshot-ai/kimi-code@0.18.0.
  • Adds AgentKimi to npmBasedAgents so Node.js/npm preflight checks apply.
  • Removes the legacy uv-specific preflight validation.

3. Config Paths (internal/agents/kimi/adapter.go)

  • GlobalConfigDir: ~/.kimi~/.kimi-code
  • SystemPromptFile: ~/.kimi/KIMI.md~/.kimi-code/AGENTS.md
  • SystemPromptStrategy: StrategyJinjaModulesStrategyFileReplace
  • SkillsDir: ~/.config/agents/skills~/.kimi-code/skills
  • MCPConfigPath: ~/.kimi/mcp.json~/.kimi-code/mcp.json
  • SubAgentsDir: ~/.kimi/agents~/.kimi-code/agents
  • findKimi: adds npm global paths on Windows (AppData/Roaming/npm/kimi.cmd)
  • BootstrapTemplate: only writes AGENTS.md if missing (preserves user content)
  • PostInstallMessage: updated with new paths

4. Skill Registry (internal/skillregistry/registry.go)

  • Adds ~/.kimi-code/skills to UserSkillDirs().
  • Adds .kimi-code/skills to ProjectSkillDirs().

5. New Assets (internal/assets/kimi/)

  • AGENTS.md: Minimal base template for StrategyFileReplace injection.
  • engram-protocol.md: Adapted Engram persistent memory protocol for Kimi users.

Backwards Compatibility

The legacy Python kimi-cli is no longer supported. Users on the old version should run kimi migrate (built into the new CLI) to port their config and sessions to ~/.kimi-code before installing Gentle AI.

Testing

  • Unit tests for internal/agents/kimi/adapter_test.go should be updated to reflect new paths.
  • Golden/component tests should include Kimi in StrategyFileReplace scenarios.
  • Manual test: gentle-ai install kimi on a clean machine with npm.

Checklist

  • Adapter paths updated for new CLI data directory
  • Install resolver uses npm with pinned version
  • Skill registry includes new paths
  • Engram protocol asset created
  • Tests updated (follow-up needed)
  • Docs updated (docs/agents.md) (follow-up needed)

Summary by CodeRabbit

  • New Features

    • Introduced Engram persistent-memory protocol for advanced memory management and session tracking
  • Refactor

    • Migrated Kimi agent to modern npm-based installation (v0.18.0+)
    • Updated configuration directories from .kimi to .kimi-code
    • Renamed system prompt file from KIMI.md to AGENTS.md
    • Enhanced skills discovery to include updated directory structure

Jucomu3P added 8 commits June 19, 2026 21:48
Adds a Renovate-tracked pinned version for the new Kimi Code CLI
(TypeScript/npm-based), replacing the legacy Python/uv-based
kimi-cli install path.
Replaces the legacy uv/Python-based install path with the official
npm-based distribution for Kimi Code CLI v0.18.0+. Adds Kimi to
the npm-based agent set and removes the uv-specific preflight check.
Adds ~/.kimi-code/skills and .kimi-code/skills to the skill discovery
paths so that repo-local and user-level Kimi Code skills are indexed
by the skill registry.
Updates the Kimi adapter to target the new TypeScript-based Kimi Code
CLI instead of the legacy Python/uv-based kimi-cli:

- Config dir changed from ~/.kimi to ~/.kimi-code
- System prompt changed from KIMI.md (Jinja) to AGENTS.md (FileReplace)
- Skills dir changed to ~/.kimi-code/skills
- MCP config path changed to ~/.kimi-code/mcp.json
- findKimi now searches npm global paths on Windows
- BootstrapTemplate only writes AGENTS.md if missing (preserves user content)
- PostInstallMessage updated with new paths
Adds a minimal AGENTS.md skeleton that uses the same marker-based
section injection pattern as other StrategyFileReplace agents.
This preserves user-authored content while allowing Gentle AI to
manage persona, output style, engram, SDD, TDD, and trigger sections.
Adapts the Claude Code engram protocol to the Kimi Code CLI context.
This asset is injected into AGENTS.md via the Engram component and
provides the mandatory memory persistence protocol for Kimi users.
The markers must match the format expected by filemerge.InjectMarkdownSection:
<!-- gentle-ai:{section} --> and <!-- /gentle-ai:{section} -->.
An empty/minimal base allows the persona injector to write the full
persona content on first install, and subsequent injectors (SDD, Engram)
to append their managed sections with proper markers.

This avoids the preserveManagedSections split issue where the base
template headers would be preserved before the persona content.
@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Kimi agent integration is migrated from the legacy Python/uv-based kimi-cli to the npm-based @moonshot-ai/kimi-code (v0.18.0+). All config paths move from .kimi to .kimi-code, system prompt file renames from KIMI.md to AGENTS.md, and the install resolver switches from uv tool install to npm install. Skill registry roots are extended, and the Engram persistent-memory protocol document is introduced.

Changes

Kimi uv→npm migration

Layer / File(s) Summary
Version constant and npm install resolver
internal/versions/versions.go, internal/installcmd/resolver.go
Pins @moonshot-ai/kimi-code@0.18.0 as a versioned constant, rewrites resolveKimiInstall to use npm install --ignore-scripts (with sudo on non-writable Linux), adds Kimi to npmBasedAgents, and removes the old validateKimiInstallPreflight and uvInstallHint.
Adapter config paths, binary lookup, and system prompt strategy
internal/agents/kimi/adapter.go
Updates the header comment, Windows binary fallbacks to npm roaming kimi.cmd/kimi.exe, all config dir/file paths from .kimi to .kimi-code (including KIMI.mdAGENTS.md), MCPConfigPath, ConfigPath, SubAgentsDir, PostInstallMessage, and switches SystemPromptStrategy from StrategyJinjaModules to StrategyFileReplace.
BootstrapTemplate: AGENTS.md skeleton with no-overwrite guard
internal/agents/kimi/adapter.go
Reworks BootstrapTemplate to read from kimi/AGENTS.md and create it only if not already present, replacing the prior always-clobber write of kimi/KIMI.md.
Kimi asset files
internal/assets/kimi/AGENTS.md, internal/assets/kimi/engram-protocol.md
Adds a header comment to AGENTS.md and introduces the complete engram-protocol.md defining the mandatory Engram persistent-memory protocol.
Skill registry: .kimi-code paths and uniqueExistingDirs fix
internal/skillregistry/registry.go
Adds ~/.kimi-code/skills to UserSkillDirs, adds .kimi/skills and .kimi-code/skills to ProjectSkillDirs, and fixes uniqueExistingDirs to return the original input path instead of the cleaned path.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • Gentleman-Programming/gentle-ai#866: Both PRs modify internal/installcmd/resolver.go's npm preflight logic — the retrieved PR establishes the npmBasedAgents gate, and this PR extends it to include Kimi.

Suggested labels

type:feature, size:exception

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.73% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: modernizing the Kimi adapter to target the new Kimi Code CLI v0.18.0+, replacing the legacy Python/uv-based approach.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/agents/kimi/adapter.go`:
- Around line 285-292: The os.Stat call checking for skeletonPath only handles
the os.IsNotExist case and silently ignores other error types like permission or
I/O failures. Modify the error handling logic to first check if err is not nil,
then verify if it is specifically os.IsNotExist before proceeding with the file
creation. If the error is any other type (permission denied, I/O error, etc.),
return it as an error instead of silently continuing execution.

In `@internal/assets/kimi/engram-protocol.md`:
- Line 1: The markdown file has two structural issues to fix: First, change the
level-2 heading at the start of the document (## Engram Persistent Memory —
Protocol) to a level-1 heading using a single hash mark (#) instead of double
hash marks. Second, ensure all section headings at lines 74, 77, 80, 83, 86, and
89 have blank lines before and after them to comply with markdownlint
requirements. This means adding an empty line above and below each of these
headings to properly separate them from surrounding content.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8fbb18ce-c5ee-4884-88bc-6b697c7852cf

📥 Commits

Reviewing files that changed from the base of the PR and between 0a9ffdd and dc5f3df.

📒 Files selected for processing (6)
  • internal/agents/kimi/adapter.go
  • internal/assets/kimi/AGENTS.md
  • internal/assets/kimi/engram-protocol.md
  • internal/installcmd/resolver.go
  • internal/skillregistry/registry.go
  • internal/versions/versions.go

Comment on lines +285 to 292
// We write the skeleton only if AGENTS.md does not already exist so we
// do not clobber user-authored instructions on a re-install.
if _, err := os.Stat(skeletonPath); os.IsNotExist(err) {
content := assets.MustRead("kimi/AGENTS.md")
if _, err := filemerge.WriteFileAtomic(skeletonPath, []byte(content), 0o644); err != nil {
return fmt.Errorf("write AGENTS.md skeleton: %w", err)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Propagate non-NotExist stat failures in bootstrap.

At Line 287, non-ENOENT os.Stat errors are ignored, so permission/I/O failures can silently skip AGENTS.md creation and continue as “success.”

Suggested fix
-    if _, err := os.Stat(skeletonPath); os.IsNotExist(err) {
-        content := assets.MustRead("kimi/AGENTS.md")
-        if _, err := filemerge.WriteFileAtomic(skeletonPath, []byte(content), 0o644); err != nil {
-            return fmt.Errorf("write AGENTS.md skeleton: %w", err)
-        }
-    }
+    if _, err := os.Stat(skeletonPath); err != nil {
+        if !os.IsNotExist(err) {
+            return fmt.Errorf("stat AGENTS.md skeleton path: %w", err)
+        }
+        content := assets.MustRead("kimi/AGENTS.md")
+        if _, err := filemerge.WriteFileAtomic(skeletonPath, []byte(content), 0o644); err != nil {
+            return fmt.Errorf("write AGENTS.md skeleton: %w", err)
+        }
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// We write the skeleton only if AGENTS.md does not already exist so we
// do not clobber user-authored instructions on a re-install.
if _, err := os.Stat(skeletonPath); os.IsNotExist(err) {
content := assets.MustRead("kimi/AGENTS.md")
if _, err := filemerge.WriteFileAtomic(skeletonPath, []byte(content), 0o644); err != nil {
return fmt.Errorf("write AGENTS.md skeleton: %w", err)
}
}
// We write the skeleton only if AGENTS.md does not already exist so we
// do not clobber user-authored instructions on a re-install.
if _, err := os.Stat(skeletonPath); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("stat AGENTS.md skeleton path: %w", err)
}
content := assets.MustRead("kimi/AGENTS.md")
if _, err := filemerge.WriteFileAtomic(skeletonPath, []byte(content), 0o644); err != nil {
return fmt.Errorf("write AGENTS.md skeleton: %w", err)
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/agents/kimi/adapter.go` around lines 285 - 292, The os.Stat call
checking for skeletonPath only handles the os.IsNotExist case and silently
ignores other error types like permission or I/O failures. Modify the error
handling logic to first check if err is not nil, then verify if it is
specifically os.IsNotExist before proceeding with the file creation. If the
error is any other type (permission denied, I/O error, etc.), return it as an
error instead of silently continuing execution.

@@ -0,0 +1,101 @@
## Engram Persistent Memory — Protocol

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Resolve markdown heading-structure lint warnings.

Line 1 starts with a level-2 heading, and Lines 74/77/80/83/86/89 are missing the required surrounding blank lines per markdownlint hints.

Also applies to: 74-90

🧰 Tools
🪛 markdownlint-cli2 (0.22.1)

[warning] 1-1: First line in a file should be a top-level heading

(MD041, first-line-heading, first-line-h1)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/assets/kimi/engram-protocol.md` at line 1, The markdown file has two
structural issues to fix: First, change the level-2 heading at the start of the
document (## Engram Persistent Memory — Protocol) to a level-1 heading using a
single hash mark (#) instead of double hash marks. Second, ensure all section
headings at lines 74, 77, 80, 83, 86, and 89 have blank lines before and after
them to comply with markdownlint requirements. This means adding an empty line
above and below each of these headings to properly separate them from
surrounding content.

Source: Linters/SAST tools

@Alan-TheGentleman Alan-TheGentleman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for pushing the Kimi migration forward. This is the right area to modernize, but this PR is not safe to merge yet.

Blocking issues:

  • The install command uses --ignore-scripts, but the upstream package has a postinstall script that appears responsible for moving legacy Python kimi shims out of the way. Disabling it can produce a successful install that still runs the old CLI.
  • @moonshot-ai/kimi-code@0.18.0 requires Node >=22.19.0, but the preflight only checks for npm. Users with older Node can pass preflight and get a broken install.
  • The Kimi tests and related preflight expectations are not migrated yet. The adapter still needs coverage for .kimi-code, AGENTS.md, StrategyFileReplace, npm install behavior, and SDD injection expectations.
  • This is a breaking migration, so docs/migration guidance cannot be follow-up work.

Please decide the legacy-shim strategy, add Node version preflight, migrate the tests, and include the docs/migration update before the next review.

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.

2 participants