Skip to content

feat(agent-core): keep head and tail of user messages during compaction - #1313

Merged
RealKai42 merged 2 commits into
mainfrom
kaiyi/el-paso-v2
Jul 2, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
kaiyi/el-paso-v2

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

What

Changes the compaction retention strategy for real user messages:

  • ≤ 20k tokens: everything is kept verbatim (unchanged).
  • > 20k tokens: instead of keeping only the most recent 20k, keep the oldest 2k (COMPACT_USER_MESSAGE_HEAD_TOKENS) plus the most recent 18k. The head can extend into the cut-off beginning of the message whose end anchors the tail, so a single oversized message keeps both its start and its most recent part.

The model is told about this in three places:

  1. An elision marker (<system-reminder>, origin {kind: 'injection', variant: 'compaction_elision'}) inserted between the head and tail segments, stating what sits above/below and roughly how many tokens were omitted. Injection origin means the next compaction drops it (markers never stack), undo skips it, and the UI transcript never shows it.
  2. A sentence appended to the compaction summary prefix.
  3. The Context Management section of the default system prompt (its previous wording was already stale).

Boundary semantics

The tail boundary message is now truncated keeping its end (the more recent part), matching "most recent 18k" at token level; the head boundary keeps its beginning. Previously the boundary message always kept its beginning.

Compatibility

New record field keptHeadUserMessageCount (agent-core CompactionResult, protocol type + zod schema):

  • Present ⟺ the selection split into head + tail ⟺ the live context holds one marker message, so the wire-transcript reducer computes foldedLength = keptUserMessageCount + 2 instead of + 1.
  • Records without it restore with the original tail-only selection that produced them, keeping resumed histories byte-identical to what the session actually had.
  • apps/vis model-mode projection mirrors the same head/marker/tail rebuild.

Tests

Failing tests were written first, then the implementation. New coverage: selection unit tests (split, boundary directions, surrogate pairs, default budgets), marker text, transcript folded length, and integration tests (split shape + resume round-trip, no marker stacking across repeated compactions, no marker under budget, legacy-record restore). Full suites pass: agent-core 3418, protocol 503, vis-server 141, repo-wide typecheck clean. Snapshot/token-count updates in full.test.ts are due to the longer summary prefix and were reviewed line by line.

Compaction used to keep only the most recent 20k tokens of real user
input, so the original task statement was the first thing to vanish in
long sessions. Now, when the user-message pool fits the 20k budget it is
still kept whole; when it overflows, the oldest 2k tokens and the most
recent 18k are kept instead, with an elision marker between the two
segments telling the model what was omitted and that the summary covers
it. The summary prefix and the default system prompt describe the new
shape as well.

The new `keptHeadUserMessageCount` record field keeps restore and the
wire-transcript folded length consistent: records without it (written by
older versions) restore with the original tail-only selection that
produced them, and the vis model-mode projection mirrors the same
head/marker/tail rebuild.
@changeset-bot

changeset-bot Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c04fd6c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 2, 2026 •

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

commit: c04fd6c

@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: 76ee835a76

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

export function buildCompactionElisionText(omittedTokens: number): string {
return [
'<system-reminder>',
`Some of this conversation's user messages were omitted here during compaction: the messages above this note are the oldest user input, the messages below are the most recent, and roughly ${String(omittedTokens)} tokens in between were dropped. The omitted content is covered by the compaction summary at the end of the conversation.`,

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 Avoid claiming overflow-dropped content was summarized

When the compaction request itself overflows, FullCompaction.compactionRound trims historyForModel and records droppedCount, and those trimmed messages are explicitly not covered by the generated summary. In that scenario, the new head/tail marker can replace user content that the summarizer never saw, but this text tells the next model the omitted content is covered by the summary, which can make it trust a summary blind spot as complete; the same absolute claim in the summary prefix should be qualified or driven by droppedCount.

Useful? React with 👍 / 👎.

@RealKai42

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c04fd6cdff

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

@RealKai42
RealKai42 merged commit 329846c into main Jul 2, 2026
9 checks passed
@RealKai42
RealKai42 deleted the kaiyi/el-paso-v2 branch July 2, 2026 11:24
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Aug 15, 2026
…on (MoonshotAI#1313)

* feat(agent-core): keep head and tail of user messages during compaction

Compaction used to keep only the most recent 20k tokens of real user
input, so the original task statement was the first thing to vanish in
long sessions. Now, when the user-message pool fits the 20k budget it is
still kept whole; when it overflows, the oldest 2k tokens and the most
recent 18k are kept instead, with an elision marker between the two
segments telling the model what was omitted and that the summary covers
it. The summary prefix and the default system prompt describe the new
shape as well.

The new `keptHeadUserMessageCount` record field keeps restore and the
wire-transcript folded length consistent: records without it (written by
older versions) restore with the original tail-only selection that
produced them, and the vis model-mode projection mirrors the same
head/marker/tail rebuild.

* style(agent-core): drop redundant spread over slice in head selection
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