Skip to content

feat: add Kimi Code (Moonshot AI) preset and hook installer#1298

Open
Krishnachaitanyakc wants to merge 1 commit into
git-ai-project:mainfrom
Krishnachaitanyakc:feat/kimi-code-support-639-v2
Open

feat: add Kimi Code (Moonshot AI) preset and hook installer#1298
Krishnachaitanyakc wants to merge 1 commit into
git-ai-project:mainfrom
Krishnachaitanyakc:feat/kimi-code-support-639-v2

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown
Contributor

@Krishnachaitanyakc Krishnachaitanyakc commented May 7, 2026

Summary

Adds first-class support for kimi-cli, Moonshot AI's Python coding assistant, against the new agent-preset framework that landed on main in May 2026. Replaces the closed PR #751, which was scoped to the old per-preset arms and is structurally incompatible with the rewritten orchestrator.

Closes #639. Continues the work from #751 per @svarlamov's request to rebase on the simpler agent preset structure.

Preset (presets/kimi_code.rs)

  • Pure parser implementing AgentPreset::parse. Handles PreToolUse / PostToolUse for WriteFile, StrReplaceFile, and Shell.
  • Strict event handling: lifecycle events (SessionStart, Stop, SubagentStart, PostToolUseFailure, etc.) return PresetError rather than fabricating a phantom file-edit checkpoint.
  • transcript_source: None for now — kimi-cli stores sessions on disk under $KIMI_SHARE_DIR/sessions/... but the hook payload doesn't surface that path. A dedicated reader can land in a follow-up.
  • agent_id.model defaults to "unknown" because kimi-cli does not surface the active model in hook payloads.

Installer (mdm/agents/kimi_code.rs)

  • Writes [[hooks]] entries into ~/.kimi/config.toml per the upstream hook documentation. No matcher field — the preset itself filters tool events.
  • Idempotent: repeat install with the desired command is a no-op (compared structurally, not textually, so TOML round-trip whitespace doesn't cause spurious diffs).
  • Token-based ownership matching (is_git_ai_kimi_command): a hypothetical sibling preset like kimi-code-pro is never misidentified.
  • Uninstall removes only kimi-code entries; user-defined hooks survive. Empty hooks key is removed entirely so the file isn't left with a dangling hooks = [].

Tool classification (bash_tool.rs)

  • New Agent::KimiCode variant: WriteFile/StrReplaceFileFileEdit, ShellBash, all else → Skip.

Wiring

  • presets/mod.rs::resolve_preset accepts kimi-code.
  • mdm/agents/mod.rs::get_all_installers includes KimiCodeInstaller.
  • git_ai_handlers help text lists kimi-code.
  • tests/integration/repos/test_file.rs::AI_AUTHOR_NAMES and test_repo.rs::is_known_checkpoint_preset learn kimi-code.

Test plan

  • cargo fmt --check clean
  • cargo clippy --all-targets -- -D warnings clean (no #[allow(...)] annotations added)
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps clean
  • 14 preset unit tests (presets::kimi_code::tests::*) — pre/post for WriteFile/StrReplaceFile/Shell, lifecycle event rejection, unsupported tool rejection, missing field errors, invalid JSON, alias key acceptance, tolerance for extra tool_output field
  • 16 installer unit tests (mdm::agents::kimi_code::tests::*) — fresh install (s1), idempotency (s2), preserves unrelated hooks (s3), updates outdated path (s4), dedup with same and differing paths (s5/s5b), no matcher (s6), dry-run (s7), creates parent dir (s8); uninstall variants (u1-u5); invalid TOML/wrong type errors (e1-e3); ownership-token tests (substring lookalikes never match)
  • 11 integration tests (tests/integration/kimi_code.rs) — preset routing for all 4 event types, full lifecycle/tool rejection sweep (11 events × 6 tools), 3 E2E flows via real git-ai checkpoint kimi-code subprocess asserting assert_lines_and_blame and session metadata
  • Full lib test suite (1467 tests) passes locally on macOS in daemon mode

Open in Devin Review

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

Adds first-class support for kimi-cli, Moonshot AI's Python coding
assistant, against the new agent-preset framework that landed on main
in May 2026. Replaces the closed PR git-ai-project#751, which was scoped to the old
per-preset arms in git_ai_handlers and is structurally incompatible
with the rewritten orchestrator.

Preset (`presets/kimi_code.rs`):
  - Pure parser implementing `AgentPreset::parse`. Handles
    `PreToolUse` / `PostToolUse` for `WriteFile`, `StrReplaceFile`,
    and `Shell`. Accepts the alias keys (`tool_use_id` / `toolUseId`)
    in addition to kimi-cli's native `tool_call_id`.
  - Strict event handling: lifecycle events (`SessionStart`, `Stop`,
    `SubagentStart`, `PostToolUseFailure`, etc.) return `PresetError`
    rather than fabricating a phantom file-edit checkpoint.
  - `transcript_source: None` for now. kimi-cli stores sessions on
    disk under `$KIMI_SHARE_DIR/sessions/...` but the hook payload
    does not surface that path; a dedicated reader can land in a
    follow-up.

Installer (`mdm/agents/kimi_code.rs`):
  - Writes `[[hooks]]` entries into `~/.kimi/config.toml` per the
    upstream hook documentation. No `matcher` field; the preset
    itself filters tool events.
  - Idempotent — repeat install with the desired command is a no-op.
  - Token-based ownership matching (`is_git_ai_kimi_command`) so that
    a hypothetical sibling preset like `kimi-code-pro` is never
    misidentified or clobbered.
  - Uninstall removes only kimi-code entries; user-defined hooks
    survive.
  - 16 unit tests covering fresh install, idempotency, dedup with
    same and differing paths, dry-run, parent-dir creation, mixed
    uninstall, missing-config and corrupt-TOML tolerance.

Tool classification (`bash_tool.rs`):
  - New `Agent::KimiCode` variant. `WriteFile`/`StrReplaceFile` map
    to `FileEdit`, `Shell` maps to `Bash`, all else `Skip`.

Wiring:
  - `presets/mod.rs::resolve_preset` accepts `kimi-code`.
  - `mdm/agents/mod.rs::get_all_installers` includes `KimiCodeInstaller`.
  - `git_ai_handlers` help text lists `kimi-code`.
  - `tests/integration/repos/test_file.rs::AI_AUTHOR_NAMES` and
    `test_repo.rs::is_known_checkpoint_preset` learn `kimi-code`.

Tests: 14 preset unit tests, 16 installer unit tests, 11 integration
tests including 3 full E2E flows (`WriteFile` attribution, pre+post
isolation of human vs AI lines, `StrReplaceFile` attribution).
`cargo fmt --check`, `cargo clippy --all-targets -- -D warnings`,
`RUSTDOCFLAGS="-D warnings" cargo doc --no-deps`, and the full lib
test suite (1467 tests) all pass locally.

Closes git-ai-project#639
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.

Request to add support for **Kimi Code**

2 participants