Skip to content

fix: compact with Cursor's own summary (summarize_action) - #205

Merged
cursor[bot] merged 7 commits into
mainfrom
fix/203-native-compaction
Nov 29, 2026
Merged

cursor[bot] merged 7 commits into
mainfrom
fix/203-native-compaction

Conversation

@oakimov

@oakimov oakimov commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Fixes #203

Problem

A compaction on a Cursor model reminted the conversation and sent the whole host transcript as one user message. When that message overflowed the window, Cursor's harness summarized it together with the compaction request, so the model answered something else and OpenCode failed with "Compaction summary did not match the required template".

Fix

Compaction now works the way Cursor CLI's /summarize does: Cursor summarizes the session's own conversation with an empty summarize_action, on the same conversation id. OpenCode's transcript is no longer sent as a prompt.

  • Idle conversation: summarized in place; the next turn continues on the summary's checkpoint.
  • Mid-turn (Cursor waits on tool results): the provider first ends that turn, as a new user turn would. It delivers the tool results the host has (2.x: the hook's messages; 1.x: the plugin reads the step's results from the session), cancels any call without one, saying the tool may have run, and drains to turn_ended. Then the conversation is summarized in place, once. Cursor CLI never compacts a busy conversation from the client, and neither host resumes the held Run after a compaction: 1.x adds a synthetic "Continue" message, 2.4 opens the next request with a <conversation-checkpoint> user message. The first Run after a compaction leaves that copy of the summary out, since Cursor's checkpoint holds it. A turn Cursor does not end cleanly is imported instead.
  • History Cursor does not hold (another model answered, no checkpoint, a history rewrite, a restart with unanswered tool calls): it is imported into a fresh conversation as earlier turns, then summarized.
  • OpenCode 2.x: the plugin answers the session compaction hook with Cursor's summary, so OpenCode skips its own request and template check. The hook's call adds the model's and variant's settings, as OpenCode's own model call would. Background summaries are declined; Cursor summarizes in the background itself. A failed summary fails the compaction without a 413, so OpenCode neither shrinks nor retries it.
  • OpenCode 1.x: the compaction call streams Cursor's summary as its reply.

Rebuilt history on every path (foreign history, unusable checkpoint, interrupted Run, compaction import) now goes as up to five prepend_user_messages instead of inside the request. Cursor keeps only the last five, so the provider never sends more.

The 80 % seed check is replaced: like Cursor CLI, the provider sends the whole history. Only a route measured to drop history silently is checked (src/effective-context-data.ts, for now gpt-5.4-mini at 56k input tokens). docs/effective-context.md describes how to find and record another one.

No OCP change is needed: compaction is still detected only from the chat.params compaction agent (1.x) and the 2.x compaction hook.

A/B results (live Cursor, 2026-11-28)

History with three codes planted at its start, middle and end; question: report the three codes.

Model History Before: history inside the request After: five earlier turns
Auto 3.0M chars (~2× window) request lost ("Reviewed step 38."), 0/3 3/3
claude-haiku-4-5 2.4M 3/3 3/3
gemini-4-neon 3.3M 3/3 3/3
gpt-5.5 3.0M 3/3 3/3
gpt-5.4-mini 0.8M request lost (0 input tokens) 1/3, newest ~56k tokens only (now checked)
  • summarize_action on Auto: summary text, a checkpoint with summary archives, an empty turn_ended; the next turn recalled every fact. At 219k tokens occupancy went from 219,259 to 6,299.
  • UserMessageAction.conversation_history, Cursor CLI's own history import, is ignored by Cursor's cloud for this client, so the import uses prepended turns.

Verification

  • bun test ./test: 3,710 pass. Three test/review-publish.test.ts tests timed out under full-suite load; that file passes alone (113 of 113). New test/native-compaction.test.ts (live, mid-turn on both majors, the 2.x summary copy, import, hook decline, a Stop, another Location's Run) and test/compaction-run.test.ts (wire bytes from Cursor's field numbers).
  • bun run typecheck, bun run build, test/architecture.test.ts clean.
  • Prompt cache: compaction keeps the conversation id and its RequestContext, so the next turn resumes the summarized checkpoint instead of starting cold.
  • Live end-to-end through OpenCode 2.4 on 4050150 (@nkoynov, in a NixOS VM): idle and mid-turn compactions and the import kept every planted fact; that run found the double summary and the dropped variant fixed here. A rerun on OpenCode 2.4 and a first run on 1.19: pending, maintainer.

@nkoynov, thank you for the detailed report and the repro numbers. They showed exactly where the request disappeared. This takes Cursor CLI's route instead of the size guard on your branch, so a long session's compaction no longer depends on OpenCode shrinking its transcript.

Fixes #203

Problem

A compaction on a Cursor model reminted the conversation and sent the whole host transcript as one user message. When that message overflowed the window, Cursor's harness summarized it together with the compaction request, so the model answered something else and OpenCode failed with "Compaction summary did not match the required template".

Fix

Compaction now works the way Cursor CLI's /summarize does: Cursor summarizes the session's own conversation with an empty summarize_action, on the same conversation id. OpenCode's transcript is no longer sent as a prompt.

  • Idle conversation: summarized in place; the next turn continues on the summary's checkpoint.
  • Mid-turn (Cursor waits on tool results): the provider first ends that turn, as a new user turn would. It delivers the tool results the host has (2.x: the hook's messages; 1.x: the plugin reads the step's results from the session), cancels any call without one, saying the tool may have run, and drains to turn_ended. Then the conversation is summarized in place, once. Cursor CLI never compacts a busy conversation from the client, and neither host resumes the held Run after a compaction: 1.x adds a synthetic "Continue" message, 2.4 opens the next request with a <conversation-checkpoint> user message. The first Run after a compaction leaves that copy of the summary out, since Cursor's checkpoint holds it. A turn Cursor does not end cleanly is imported instead.
  • History Cursor does not hold (another model answered, no checkpoint, a history rewrite, a restart with unanswered tool calls): it is imported into a fresh conversation as earlier turns, then summarized.
  • OpenCode 2.x: the plugin answers the session compaction hook with Cursor's summary, so OpenCode skips its own request and template check. The hook's call adds the model's and variant's settings, as OpenCode's own model call would. Background summaries are declined; Cursor summarizes in the background itself. A failed summary fails the compaction without a 413, so OpenCode neither shrinks nor retries it.
  • OpenCode 1.x: the compaction call streams Cursor's summary as its reply.

Rebuilt history on every path (foreign history, unusable checkpoint, interrupted Run, compaction import) now goes as up to five prepend_user_messages instead of inside the request. Cursor keeps only the last five, so the provider never sends more.

The 80 % seed check is replaced: like Cursor CLI, the provider sends the whole history. Only a route measured to drop history silently is checked (src/effective-context-data.ts, for now gpt-5.4-mini at 56k input tokens). docs/effective-context.md describes how to find and record another one.

The cache log runbook matches the in-place summary: the conversation id stays, and the log line is summarize: done. A history import, a foreign-history reseed, a history rewrite, and a restored post-compaction rebase still rotate the id.

No OCP change is needed: compaction is still detected only from the chat.params compaction agent (1.x) and the 2.x compaction hook.

A/B results (live Cursor, 2026-11-28)

History with three codes planted at its start, middle and end; question: report the three codes.

Model History Before: history inside the request After: five earlier turns
Auto 3.0M chars (~2× window) request lost ("Reviewed step 38."), 0/3 3/3
claude-haiku-4-5 2.4M 3/3 3/3
gemini-4-neon 3.3M 3/3 3/3
gpt-5.5 3.0M 3/3 3/3
gpt-5.4-mini 0.8M request lost (0 input tokens) 1/3, newest ~56k tokens only (now checked)
  • summarize_action on Auto: summary text, a checkpoint with summary archives, an empty turn_ended; the next turn recalled every fact. At 219k tokens occupancy went from 219,259 to 6,299.
  • UserMessageAction.conversation_history, Cursor CLI's own history import, is ignored by Cursor's cloud for this client, so the import uses prepended turns.

Verification

  • bun test ./test: 3,710 pass. Three test/review-publish.test.ts tests timed out under full-suite load; that file passes alone (113 of 113). New test/native-compaction.test.ts (live, mid-turn on both majors, the 2.x summary copy, import, hook decline, a Stop, another Location's Run) and test/compaction-run.test.ts (wire bytes from Cursor's field numbers).
  • bun run typecheck, bun run build, test/architecture.test.ts clean.
  • Prompt cache: compaction keeps the conversation id and its RequestContext, so the next turn resumes the summarized checkpoint instead of starting cold.
  • Live end-to-end through OpenCode 2.4 on 4050150 (@nkoynov, in a NixOS VM): idle and mid-turn compactions and the import kept every planted fact; that run found the double summary and the dropped variant fixed here. A rerun on OpenCode 2.4 and a first run on 1.19: pending, maintainer.

@nkoynov, thank you for the detailed report and the repro numbers. They showed exactly where the request disappeared. This takes Cursor CLI's route instead of the size guard on your branch, so a long session's compaction no longer depends on OpenCode shrinking its transcript.

Open in Web Open in Cursor 

OpenCode compaction on a Cursor model reminted the conversation and sent
the whole host transcript as one user message. When that overflowed,
Cursor summarized the message together with the compaction request, the
model answered without OpenCode's template, and compaction failed.

Compaction now asks Cursor to summarize the session's own conversation
with summarize_action, as Cursor CLI's /summarize does, on the same
conversation id:
- idle: summarize in place; the next turn continues on that checkpoint
- held Run mid-turn: summarize a fork of its checkpoint, keep the Run,
  summarize in place when the turn ends
- history Cursor does not hold: import it as earlier turns, then summarize
- OpenCode 2.x: answered from the session compaction hook (no template
  check); background summaries are declined

Rebuilt history now goes as up to five prepend_user_messages instead of
inside the request, so Cursor summarizes what overflows and keeps the
request. The 80% seed check applies only to routes with a measured silent
cutoff (gpt-5.4-mini).

Fixes #203
@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The reviewed changes appear safe to merge, with no outstanding findings.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge, with no outstanding findings.

Summary

The PR replaces prompt-based compaction with Cursor’s native summarize_action.

  • Cursor summarizes its conversation in place when OpenCode compacts.
  • Rebuilt history travels in up to five earlier user turns.
  • Only the measured gpt-5.4-mini route gets a seed size check.
  • The first ordinary turn replaces an imported compaction prompt.
  • Small test cleanups leave runtime behavior unchanged.

All four supplied previous findings are addressed: foreign-provider history triggers import, Stop cancels the summary, cancellation has a fixed deadline, and interrupted tool results remain failures. No new actionable issues were found. Tests were inspected but not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[OpenCode requests compaction] --> B{Cursor holds the history?}
  B -->|Yes| C[Finish any held turn]
  B -->|No| D[Import history into a fresh conversation]
  C --> E[Send summarize_action]
  D --> E
  E --> F[Return Cursor summary]
  F --> G[Continue on the summary checkpoint]
Loading

Reviews (6) · Last reviewed commit: "docs: keep the conversation id on in-pla..." · Reviewed by Greptile

Comment thread src/language-model.ts Resolved
Comment thread src/language-model.ts Resolved
@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.87469% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/language-model.ts 99.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…swer

- A summarize Run follows a host Stop for its whole length, not only its
  upload: the 2.x compaction hook has no abort signal and the Run is not in
  sessionManager. It sends cancel_action, drains until Cursor ends the Run
  (answering blob and request-context probes), and applies its checkpoint
  only once the summary completed.
- OpenCode 1.x sends compaction a serialized transcript with no assistant
  messages, so the classic plugin reads the session's messages and reports
  whether the latest real answer was Cursor's. Another model's answer, or
  unreadable messages, imports the transcript instead of summarizing a
  checkpoint that may miss those turns.
- Cover the new paths: hook prompt shapes, pump edges, an isolated import for
  a held Run without a checkpoint, a failed live summary after a fork, an
  aborted request, a failed snapshot save, nothing to compact.
@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The new summarize and history-import Runs refuse every tool exec and interaction query, so a Cursor stream cannot run host tools through a compaction.
  • The OpenCode 2 compaction hook never throws, and a declined or failed summary fails the compaction instead of falling back to a weaker path.
  • Compaction sends no host data to Cursor that the old transcript-as-prompt path did not already send.
  • No credentials are logged, and the hook strips accessToken, apiKey, authToken, baseURL and fetch from model settings before its call.
  • A hung summarize Run is bounded by the transport read-idle watchdog and the fixed cancel-grace deadline.

Comment thread src/language-model.ts Resolved
@nkoynov

nkoynov commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Ran 4050150 end to end in a NixOS VM: T3 0.1.14-nightly.20261127.5359, OpenCode 2.4.2 (our build with a few patches), the opencode2 plugin entry, real Cursor key, CURSOR_PROVIDER_DEBUG=1. Models: claude-opus-5-5 300k and 1M (Fast, High), plus opencode/big-pickle for the history-import case.

  • Compaction Runs skip the history size check, so a long session's compaction fails with "Compaction summary did not match the required template" #203 scenario: 7 turns reading about 1.5M characters of logs on Opus 5.5 300k, with facts planted in the first, middle and last turn. Cursor's occupancy reached about 248k, OpenCode auto-compacted once, then /compact at about 199k completed in live mode. The next turn was at 44.8k tokens and recalled all three facts.
  • Mid-turn auto compaction (Opus 5.5 1M with compaction.buffer lowered so the threshold is about 70k): the turn finished with all six values from six files. There were 6 compactions over 25 steps, about one every two file reads, which is what that threshold should give, and occupancy dropped to 36-37k each time. No compaction on every step.
  • History Cursor doesn't hold: big-pickle answered three turns, then I switched to Opus and ran /compact. It took the import path (reason=no-checkpoint), and the next turn recalled 3/3.
  • After compaction: a subagent finished and its report woke the thread. A background shell detached and finished; there was no completion note, but main doesn't post one either.

One thing that doesn't match the description: on 2.4 the held Run doesn't continue after a fork compaction. OpenCode's next request comes in as a fresh turn, the held Run is cancelled, and the live conversation is summarized right away. So every mid-turn compaction runs two summaries back to back (the fork took 18-105 s, the live one 25-45 s), and the turn goes on in a new Run:

19:20:06.047 compaction hook: answered sessionID=ses_… mode=fork chars=2278
19:20:06.309 continuation: wrote exec result execId=5 field=read_result frames=2 outLen=158
19:20:06.309 fresh turn: cancelled 1 pending exec(s) on prior session bf7a0736-… remainingPending=0
19:20:08.246 fresh turn drain: cancellation terminal code=canceled checkpoint=true
19:20:08.457 summarize: conversationId=f0ebabf7-… persist=true model=claude-opus-5-5 checkpointBytes=1093
19:20:51.759 summarize: done conversationId=f0ebabf7-… source=update chars=4619
19:20:51.912 outbound Run: model=claude-opus-5-5 … conversationId=f0ebabf7-… historyImport=false

Smaller one: the import Run went out as effort=medium, fast=false although the thread is on Opus 5.5 Fast High. The thread's normal Runs show effort=high, fast=true, so the compaction hook seems to drop the variant:

outbound Run: model=claude-opus-5-5 … params=[{"id":"context","value":"300k"},{"id":"effort","value":"medium"},{"id":"fast","value":"false"}] … historyImport=true

Not covered: another model answering after Cursor's turns, then compacting on Cursor. OpenCode's free models refuse a history with Cursor's assistant turns ("OpenAI Chat assistant messages only support text, reasoning, and tool-call content"), so I could only run the order other model first, Cursor second.

Typecheck, build and the touched tests pass (555). The full suite has the same failures as main 8e56dc2 on this machine (environment-dependent tests) and 43 more passing tests.

After a host Stop, the summary Run sent cancel_action and drained until
Cursor ended it. Frames Cursor kept sending renewed the read-idle
watchdog, so a Run Cursor never ended kept its stream and listeners
open. The drain now has the same fixed grace as a turn's Stop
(HOST_INTERRUPT_GRACE_MS, 3 s), after which the stream is destroyed.
Neither OpenCode major resumes the held Run after a compaction: 1.x adds
a synthetic "Continue" message and 2.4 opens the next request with a
<conversation-checkpoint> user message. The fork summary therefore never
let the held Run go on, and each mid-turn compaction ran two summaries.

A mid-turn compaction now ends the held turn first, as a new user turn
would: it delivers the tool results the host has (2.x from the hook's
messages, 1.x from the session through the plugin), cancels calls
without one and drains to turn_ended, then summarizes in place once. A
turn that does not end cleanly is imported instead. The first Run that
resumes the summarized checkpoint leaves out OpenCode's copy of the
summary.

The 2.x compaction hook's model call now carries the model's and the
variant's settings, as OpenCode's own call through its route does.
@oakimov

oakimov commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Thanks, @nkoynov. Both findings were right, and 364cdec fixes them.

  • Two summaries per mid-turn compaction. Neither OpenCode major resumes the held Run after a compaction: 2.4 opens the next request with a <conversation-checkpoint> user message, and 1.19 adds its synthetic "Continue" message. So the fork never paid off. A mid-turn compaction now ends the held turn first: it delivers the tool results OpenCode has, cancels any call without one, and waits for turn_ended. Then it summarizes the conversation in place once, as Cursor CLI's /summarize does on an idle conversation. The first Run after the compaction leaves out OpenCode's copy of the summary, since Cursor's checkpoint already holds it.
  • Variant on the compaction call. The hook calls the model directly, so it missed the settings OpenCode's route adds. It now merges the model's and the variant's settings, so an Opus Fast High thread summarizes with effort=high, fast=true.

The order you couldn't run, another model answering after Cursor's turns and then compacting on Cursor, is covered by the latest-answer check on 1.x and by the history check on 2.x. A live run of it is pending on my side.

Comment thread src/plugin.ts Resolved
The OpenCode 1.x plugin read the latest step's tool results for a
mid-turn compaction straight from stored parts and turned an interrupted
call's partial output into a successful result. It now runs those parts
through normalizeClassicInterruptedResults first, as the request path
does: an interrupted call stays a failure with its progress text, and a
shell timeout or non-zero exit keeps its notice.
@nkoynov

nkoynov commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Re-ran 7aa7034 on the same setup (T3 0.1.14-nightly.20261127.5359, OpenCode 2.4.2, real key):

  • Mid-turn auto compaction (Opus 5.5 1M, threshold about 70k): 6 compactions in one turn, each with exactly one summary (12-22 s, ended the held turn … outcome=drained), and the summary copy was left out of every resumed Run. The turn finished with all six values in about 3 minutes, down from 8.
  • History import: the import Run now goes out with effort=high, fast=true on Opus 5.5 Fast High (compaction hook: answered … variant=High), and the next turn recalled 3/3.
  • Long session + /compact (Opus 5.5 300k): one auto compaction mid-turn (248.9k down to 36.7k) and the manual one (180.4k down to 39.1k), one summary each, recall 3/3.

Both points from my last comment are fixed. A small thing I noticed, which 4050150 also did: the import Run carries a shorter system prompt than a normal first turn (24,907 vs 43,844 chars), and the first Run after the import then sends about 51k chars of user text for a one-line question, which lands in Cursor's conversation category (about 19k tokens):

outbound Run: model=claude-opus-5-5 … systemPromptLen=24907 historyImport=true userTextLen=102 historyMsgs=7
outbound Run: model=claude-opus-5-5 … systemPromptLen=0 historyImport=false userTextLen=51129 historyMsgs=1 checkpointLen=1012

Typecheck, build and the touched tests pass; the only failures are the same environment-dependent review-* tests that fail on main here.

A compaction's history-import Run froze the conversation's baseline from
the compaction call's prompt: OpenCode 2's compaction agent, or nothing
for a 1.x transcript. The first ordinary Run then resent the session
agent's whole prompt as a context update in Cursor's conversation
category, where a later summary could drop it.

That baseline is now provisional, kept in the snapshot so a restart
honours it, and the conversation's first ordinary Run replaces it, and
the frozen system rule, once with no update. Later prompt changes ride
updates as before.
@oakimov

oakimov commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Thanks, @nkoynov, and good catch on the system prompt. b6301e7 fixes it.

The import Run froze the conversation's baseline from the compaction call's own prompt: the compaction agent's on 2.4, or none at all for a 1.19 transcript. The first ordinary Run then saw a different agent and prompt, and resent the agent's whole prompt as a context update. That's your ~51k chars in Cursor's conversation category, where a later summary could even drop it.

An import's baseline is now provisional, and the mark is kept in the snapshot so a restart honours it. The conversation's first ordinary Run replaces the baseline and the frozen system rule once, with no update. After that it is frozen as before, and later prompt changes are sent as updates again.

I ran it live with the default model: an import carrying a compaction-agent prompt, then a turn whose agent prompt requires a closing token. The next Run's user text went from 5,559 to 329 chars. Cursor recalled the planted fact and followed the agent's rule on the resumed, summarized checkpoint, so it takes the replaced rule. With your 43.8k-char prompt, the saving should be the whole ~51k.

@nkoynov

nkoynov commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

b6301e7 fixes it: on the same setup the first Run after a history import sends 6,839 chars of user text (51,129 before, context epoch: replaced provisional baseline … baselineLen=43836), the host rules land in Cursor's rules category again, the import Run is still effort=high, fast=true, and recall is 3/3.
Mid-turn compaction smoke: 6 compactions, one summary each, the turn finished with all six values. Typecheck, build and the touched tests pass (230).

The cache log runbook still said compaction rotates conversationId. An in-place summary keeps that id and logs summarize: done. History import, foreign-history reseed, history rewrite, and a restored post-compaction rebase still rotate.

Co-authored-by: Oleksiy Akimov <[email protected]>
@caeliq-reviewer

Copy link
Copy Markdown

On ebd44bf, docs/cache-log-runbook.md matches in-place summarize. The conversation id stays, and the log line is summarize: done. A history import, a foreign-history reseed, a history rewrite, and a restored post-compaction rebase still rotate the id.

CI on this head is green. CodeQL reports no new alerts. codecov/patch passed.

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.

Compaction Runs skip the history size check, so a long session's compaction fails with "Compaction summary did not match the required template"

3 participants