Skip to content

fix(compaction): place summary instruction after conversation history - #42012

Closed
akenra wants to merge 1 commit into
anomalyco:devfrom
akenra:fix/compaction-prompt-ordering
Closed

fix(compaction): place summary instruction after conversation history#42012
akenra wants to merge 1 commit into
anomalyco:devfrom
akenra:fix/compaction-prompt-ordering

Conversation

@akenra

@akenra akenra commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #41801
Refs #41268
Refs #41358
Refs #36682

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

/compact (and auto-compaction) sends the summarization request as one user message: summary instruction first, then the serialized conversation. The conversation is formatted as a chat log ([User]: / [Assistant]:) and ends with a real user question from the past session, so the model answers that old question (or reproduces the transcript) instead of producing the summary — the instruction at the start is diluted, especially on 1M-context models like opencode-go/deepseek-v4-flash where the whole session fits.

The summary template itself says "using the conversation history above", i.e. history-first was the intended layout; the call site inverts it.

Change: put the conversation first, then "End of conversation history.", then the summary instruction + template, ending with an explicit guard that the history is reference material only and only the anchored summary must be output. Applied to both paths:

  1. packages/opencode/src/session/compaction.ts — manual /compact and CLI auto-compaction.
  2. packages/core/src/session/compaction.ts — runner/make() auto-compaction.

The experimental.session.compacting plugin hook still works; a plugin-provided prompt now lands after the history.

How did you verify your code works?

  • tsgo --noEmit typecheck passes for packages/opencode and packages/core.
  • New regression test session.compaction.process > places the summary instruction after the conversation history in packages/opencode/test/session/compaction.test.ts asserts the history precedes the instruction and that the guard line is the last content after any [User]: turn.
  • Full test/session/compaction.test.ts (54 pass), test/session/revert-compact.test.ts (8 pass) and packages/core/test/session-compaction.test.ts (2 pass) run green.
  • oxlint on both changed source files: 0 warnings, 0 errors.
  • Manual verification on a long session with opencode-go/deepseek-v4-flash (1M context): /compact now returns the anchored summary instead of answering the last question from the session.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Aug 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@akenra

akenra commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Runtime verification (reported by PR author):

Tested on OpenCode Desktop 1.18.16 (Windows) with opencode-go/deepseek-v4-flash (1M context). The same prompt-layout patch was applied directly to the Desktop bundle (app.asar) for testing.

Result: a ~350K-token session that previously would not compact correctly (the model answered an old question from the conversation instead of summarizing) now produces the proper anchored summary (## Objective / ## Important Details / ## Work State / ## Next Move / ## Relevant Files) via /compact.

This confirms the ordering fix resolves the reported behavior on the exact model/stack from #41801.

@akenra
akenra force-pushed the fix/compaction-prompt-ordering branch from 03a7154 to fe5962e Compare August 12, 2026 14:49
@akenra
akenra force-pushed the fix/compaction-prompt-ordering branch from fe5962e to 5284e2d Compare August 12, 2026 14:51
@rekram1-node

Copy link
Copy Markdown
Collaborator

Yeah the smaller models are having issues, ty for pr going to do a bit more cleanup but ill add u as a coauthor on my version

@rekram1-node

Copy link
Copy Markdown
Collaborator

going to go w/ this PR instead (added u as a coauthor):
#42045

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.

Manual /compact with DeepSeek V4 Flash degenerates into thousands of repeated lines

2 participants