Skip to content

fix(agent-core-v2): make large file reads resumable - #3645

Merged
RealKai42 merged 4 commits into
mainfrom
fix/read-character-budgets
Sep 8, 2026
Merged

fix(agent-core-v2): make large file reads resumable#3645
RealKai42 merged 4 commits into
mainfrom
fix/read-character-budgets

Conversation

@RealKai42

@RealKai42 RealKai42 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. This addresses repeated partial reads caused by overlapping file-read and tool-output limits, including long records that could not be recovered with Read alone.

Problem

Read could return up to 100 KiB, then the general 50,000-character tool-output limit would replace that result with a roughly 5,000-character preview and a spill-file path. Separate line-count and per-line limits could discard more content. Large single-line records also lacked an in-tool continuation path, which made recovery depend on access to a shell tool.

What changed

  • Give Read a single character budget, including line numbers and status: 100,000 by default, with calls able to request up to 500,000 using max_chars. Configure both values with [read].default_max_chars and [read].max_chars; requests above the configured maximum are capped and reported.
  • Remove the fixed line-count, UTF-8 output-byte, and per-line character caps. Read results bypass general tool-result spilling.
  • Prefer complete lines, but return a fragment when one line cannot fit on its own page. Add zero-based column_offset for forward reads, preserve Unicode boundaries, and return exact Next Read arguments without decreasing the remaining source-line range before a line is finished. Out-of-range columns, offsets inside surrogate pairs, and columns combined with negative line offsets are rejected.
  • Preserve tail reads' newest-complete-lines behavior. If no complete line fits, return forward Read arguments for the entire unread range, so recovery requires no shell tool.
  • Read the last N lines in one scan when n_lines is omitted or covers the tail window, retaining a character-bounded suffix. Earlier-ending ranges retain two scans and detect extra lines beyond the counted EOF or changes in file size, modification time, or inode. Single-pass reads can include appended lines observed before EOF without requiring a stable file size.
  • Check UTF-16 with strict decoding first. If malformed sequences are found, decode the same bytes with replacements and return readable text with a lossy-decoding warning on every page. The warning counts toward the character budget, including tail recovery, and valid literal U+FFFD characters do not trigger it. UTF-8 behavior, other decoding callers, and the existing 10 MiB UTF-16 conversion limit are unchanged.
  • Update tool/context-recovery instructions, bilingual tool/configuration docs, configuration metadata, regression tests, snapshots, and CLI patch changesets.

Column positions refer to the displayed text of the current file, excluding line-number prefixes. This change does not introduce file snapshots, arbitrary binary/encoding support, or changes to general spill retention.

Validation

  • Latest tail-read changes: pnpm --filter @moonshot-ai/agent-core-v2 test test/os/backends/node-local/tools/read.test.ts test/agent/toolExecutor/toolExecutor.test.ts --disableConsoleIntercept --maxWorkers=4 — 133 tests passed, including single-pass tail reads, append detection, same-size edits, and file replacement. Static-file tail results also matched the previous implementation in 500 differential cases.
  • Full-suite validation at 251decf: pnpm --filter @moonshot-ai/agent-core-v2 test --disableConsoleIntercept --maxWorkers=4 --exclude '**/smoke-read-pr.test.ts' — 6,424 tests across 365 files passed. The flag avoids a Vitest worker-teardown RPC error in console interception observed locally; test assertions remain enabled. An unrelated untracked local smoke-test file was excluded; all tracked tests were included. A temporary-directory cleanup race in the tower suite passed on isolated rerun.
  • pnpm --filter @moonshot-ai/agent-core-v2 typecheck — passed.
  • pnpm --filter @moonshot-ai/agent-core-v2 lint:imports — passed.
  • pnpm lint — passed with warnings and no errors.
  • pnpm -C docs run build — passed.
  • Regression coverage includes a complete 4,621-line document, a 1.1-million-character line recovered through the actual Read/tool-result pipeline with no shell tools, fixed-budget Unicode fragments, mixed short/long ranges, tail recovery, invalid columns, lossy UTF-16 continuation, valid literal replacement characters, model-visible warnings for malformed input, and persisted configuration overrides.

Checklist

  • I have read the CONTRIBUTING document.
  • I have explained the problem above; no related issue is linked.
  • I have added tests that prove the behavior works.
  • Ran gen-changesets skill and included CLI patch changesets.
  • Reviewed gen-docs guidance and updated both documentation locales with translate-docs; the obsolete changelog-sync script referenced by the skill is absent, so no release changelog was regenerated.

@changeset-bot

changeset-bot Bot commented Sep 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 213aa1e

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 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-09-08T11:51:56.111013Z 213aa1e New commits
🔒 Security Review Completed 2026-09-08T11:49:20.010535Z 213aa1e 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 Sep 8, 2026

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

commit: 213aa1e

@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: 24ddebd564

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/tools/os/read/readTool.ts Outdated
Comment thread docs/en/configuration/config-files.md Outdated
Add column offsets and forward recovery for bounded long-line reads. Reject malformed UTF-16 in Read instead of replacing invalid input.
@RealKai42 RealKai42 changed the title fix(agent-core-v2): avoid repeated truncation of file reads fix(agent-core-v2): make large file reads resumable Sep 8, 2026

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

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/tools/os/read/readTool.ts Outdated
Return readable text after strict UTF-16 decoding fails, with a warning retained in budgeted read continuations.

@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: 251decf991

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/en/reference/tools.md Outdated
@RealKai42
RealKai42 merged commit 5000f98 into main Sep 8, 2026
16 checks passed
@RealKai42
RealKai42 deleted the fix/read-character-budgets branch September 8, 2026 11:47
@github-actions github-actions Bot mentioned this pull request Sep 8, 2026

@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: 213aa1e862

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (lineOffset >= 0) return await this.readForward(readLines(), request);
const rereadsFile = detectedEncoding === undefined && (args.n_lines ?? Infinity) < -lineOffset;
const result = await this.readTail(readLines, request);
if (!result.isError && rereadsFile) {

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 Recheck the file before returning tail recovery errors

When a two-pass tail request (n_lines < -line_offset) cannot fit even one complete line, finishPage returns an isError result that still contains a forward Next Read, so this guard skips the post-read stat check. If an active log is appended between the counting pass and reread while rangeEnd remains below the old EOF, the reread stops before observing the append and the continuation points at the old tail range, causing the caller to retrieve the wrong line. The fresh evidence in the final revision is the new !result.isError condition, which leaves this recovery-error path unchecked; perform the consistency check for reread results that expose a continuation as well.

Useful? React with 👍 / 👎.

arrrrny added a commit to arrrrny/kimi-code-sync that referenced this pull request Sep 9, 2026
Sync 147 upstream commits onto the fork. Notable upstream changes:
- Remove the protocol package and relocate wire types into
  agent-core-v2/src/events.ts (MoonshotAI#3646)
- Absorb stepRetry into the LLM requester turn state machine (already
  followed in the 2026-09-07 sync)
- NotifyUser tool + mid-turn update panel (MoonshotAI#3524)
- HEIC/HEIF/BMP image support (MoonshotAI#3649), resumable large file reads
  (MoonshotAI#3645), reasoning_details round-trip (MoonshotAI#3492), zstd updater
  manifests (MoonshotAI#3669)

Conflict resolutions (all by hand, fork features preserved):
- package.json: drop the empty simple-git-hooks block (upstream cleanup;
  not a fork feature).
- docs/config-manifest.toml: take the union, then regenerate with
  pnpm gen:config-manifest (33 sections, fork sections intact).
- test/agent/stepRetry/stepRetry.test.ts: keep upstream's slimmed
  retryBackoffDelays test — the fork had deleted the old fat version
  when stepRetry was absorbed; the util is fork-used code with no other
  coverage.
- protocol/src/events.ts: accept upstream's package removal; the fork's
  compaction.started model/model_display fields already auto-merged
  into their new home (agent/fullCompaction/compactionOps.ts). No source
  still imports @moonshot-ai/protocol.

Merge follow-ups:
- Regenerate state/wire/config manifests; freshness tests pass.
- node-sdk update-all-session-models flag test: drop the v1-engine
  parity half (legacy agent-core v1 is removed upstream); keep the v2
  registration pin.
- Fix pre-existing lint errors in merge-touched files so the
  lint-staged pre-commit gate passes: unused imports/variables
  (sdk-rpc-client-v2.ts, tui/commands/config.ts,
  check-import-boundaries.mjs), floating promises (sdk-rpc-client-v2.ts,
  fullCompaction.test.ts).
- Add compactionOps.ts (model_display) to .github/FORK_OWNED_FILES so
  the relocated compaction-model fields are guard-listed.

Verification:
- pnpm run typecheck green; pnpm run lint at fork baseline (no new
  findings vs pre-merge HEAD).
- FORK_OWNED_FILES markers all resolve.
- Fork suites pass: TUI commands + session CLI (75 tests),
  x-opencode-session (7), update-all-session-models flag, oauth
  openai-compatible.
- fullCompaction.test.ts: 2 failures, both pre-existing at pre-merge
  HEAD (baseline had 5; upstream's macOS stabilization fixed 3.
EOF
)
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.

1 participant