MoonshotAI / MoonshotAI/kimi-code

Agent in long sessions emits invalid Edit/Write tool calls due to context degradation

Open
#750 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

Problem

After a long session, the agent can get stuck in a loop where it repeatedly calls

d it can no longer reconstruct the exact

d context. It also loses the precise text block it wants to replace.

This often happens when the agent is trying to update a small file like
.agents/MEMORY.md` after a long task. The failure cascade looks like:

  1. Agent reads the file successfully.
  2. It makes an Edit with an empty or malformed

d fails.
3. It retries multiple times, each time emitting another invalid Edit/Write call.
4. The runtime eventually interrupts the agent.

Impact

  • Long-running sessions cannot reliably finish small edits.
  • The user has to manually restart the session.
  • The file can be left in a partially modified state (e.g., a missing markdown heading).

Suggested improvements

  • Before every Edit, re-read the target file if the previous Edit failed.
  • For small files or prepend operations, prefer Write with a full replacement instead of Edit.
  • Consider a context-management safeguard that detects repeated invalid tool calls and forces a file re-read or session restart.

Environment

  • kimi-code CLI built from main
  • Agent working on
    .agents/MEMORY.md`
  • Session interrupted after repeated Edit/Write failures

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report identifies the kimi-code CLI and .agents/MEMORY.md, but names no implementation file, entry point, or test. Start by reproducing a long-session failure involving Edit or Write, then trace the runtime handling of repeated invalid tool calls. Done means the recovery behavior is defined and verified for the reported failure cascade.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.