|
| 1 | +# QwenLM/qwen-code#3737 — `fix(core): preserve reasoning_content in rewind, compression, and merge paths (#3579)` |
| 2 | + |
| 3 | +- URL: https://github.com/QwenLM/qwen-code/pull/3737 |
| 4 | +- Head SHA: `480f9e71b4c4` |
| 5 | +- Author: fyc09 |
| 6 | +- Size: +48 / -363 across 8 files |
| 7 | +- Closes the #3579 series (after #3590, #3682) |
| 8 | + |
| 9 | +## Summary |
| 10 | + |
| 11 | +Three remaining paths still silently dropped DeepSeek-class |
| 12 | +`reasoning_content` from history: |
| 13 | + |
| 14 | +1. **Rewind / double-ESC** — `AppContainer.tsx` called |
| 15 | + `geminiClient.stripThoughtsFromHistory()` after truncating |
| 16 | + to the rewind point. |
| 17 | +2. **Chat compression** — `chatCompressionService.ts` built the |
| 18 | + acknowledgment model turn with no thought part, so converters |
| 19 | + that key off the presence of *any* thought in a model turn |
| 20 | + broke the reasoning chain across the compression boundary. |
| 21 | +3. **Idle/merge** — the `stripThoughtsFromHistory` and |
| 22 | + `stripThoughtsFromHistoryKeepRecent` helpers themselves were |
| 23 | + load-bearing for a use case the project no longer wants to |
| 24 | + support; this PR retires them outright. |
| 25 | + |
| 26 | +## Specific references |
| 27 | + |
| 28 | +`packages/cli/src/ui/AppContainer.tsx:1739-1745` (post-patch): |
| 29 | + |
| 30 | +```tsx |
| 31 | +- // 3. Truncate API history and strip stale thinking blocks |
| 32 | ++ // 3. Truncate API history to the target point. |
| 33 | ++ // Do NOT strip thought parts — reasoning models (e.g. DeepSeek) |
| 34 | ++ // require reasoning_content continuity across all turns ... |
| 35 | + geminiClient.truncateHistory(apiTruncateIndex); |
| 36 | +- geminiClient.stripThoughtsFromHistory(); |
| 37 | +``` |
| 38 | + |
| 39 | +`packages/core/src/core/client.ts:204-210` removes the |
| 40 | +`stripThoughtsFromHistory()` pass-through on `GeminiClient`. |
| 41 | + |
| 42 | +`packages/core/src/core/geminiChat.ts:792-919` (the deletion block) |
| 43 | +removes both `stripThoughtsFromHistory()` and |
| 44 | +`stripThoughtsFromHistoryKeepRecent(keepTurns)` from `GeminiChat`, |
| 45 | +and `geminiChat.test.ts:2039-2227` removes their dedicated tests. |
| 46 | +This is a real public-API surface reduction. |
| 47 | + |
| 48 | +`packages/core/src/services/chatCompressionService.ts:265-292` — |
| 49 | +the new logic detects whether any preserved model turn carries |
| 50 | +`thought: true` and, if so, appends a sentinel thought part to |
| 51 | +the compression acknowledgment model turn: |
| 52 | + |
| 53 | +```ts |
| 54 | +const hasThoughtContentInKeep = historyToKeep.some( |
| 55 | + (content) => |
| 56 | + content.role === 'model' && |
| 57 | + content.parts?.some((part) => 'thought' in part && part.thought), |
| 58 | +); |
| 59 | + |
| 60 | +const ackParts: Part[] = [ |
| 61 | + { text: 'Got it. Thanks for the additional context!' }, |
| 62 | +]; |
| 63 | +if (hasThoughtContentInKeep) { |
| 64 | + ackParts.push({ text: ' ', thought: true }); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Design analysis |
| 69 | + |
| 70 | +The PR implements a clean policy reversal: instead of stripping |
| 71 | +thoughts on three control paths (rewind, compression-ack, idle), |
| 72 | +preserve them and remove the strippers entirely. That matches |
| 73 | +the framing in #3579 — DeepSeek-class providers require an |
| 74 | +unbroken `reasoning_content` chain across the *entire* assistant |
| 75 | +sequence, so any helper that selectively drops thoughts is a |
| 76 | +loaded gun. |
| 77 | + |
| 78 | +Two design points worth noting: |
| 79 | + |
| 80 | +1. **Sentinel thought part with `text: ' '`.** The acknowledgment |
| 81 | + only carries a thought part *when* prior turns had one. The |
| 82 | + chosen sentinel is a single-space text with `thought: true`. |
| 83 | + That works for downstream converters that key off "is there a |
| 84 | + thought part on this turn" — but it's brittle if any consumer |
| 85 | + ever asserts non-empty thought content. A short comment |
| 86 | + right above the `ackParts.push` line explaining "sentinel, |
| 87 | + intentionally minimal" would help future maintainers. |
| 88 | +2. **API surface removal vs deprecation.** Deleting public |
| 89 | + methods on `GeminiClient` and `GeminiChat` is fine for an |
| 90 | + internal/CLI-only surface, but if anything in |
| 91 | + `packages/cli`-adjacent extension code calls them, this is |
| 92 | + a hard break. A `rg "stripThoughtsFromHistory"` across the |
| 93 | + whole repo (already implied by the test cleanup) plus a note |
| 94 | + in the PR body would close the loop. |
| 95 | + |
| 96 | +The test cleanup is mechanical: every `stripThoughtsFromHistory: vi.fn()` |
| 97 | +mock entry is removed across `client.test.ts`, `config.test.ts`, |
| 98 | +and `geminiChat.test.ts`. No assertion logic for the new behavior |
| 99 | +was added in `geminiChat.test.ts` itself; the new behavior is |
| 100 | +exercised through `chatCompressionService` (presumed to have its |
| 101 | +own tests, not visible in the diff). |
| 102 | + |
| 103 | +## Risks |
| 104 | + |
| 105 | +- Compression test coverage: the diff doesn't show a new test in |
| 106 | + `chatCompressionService.test.ts` pinning the |
| 107 | + `hasThoughtContentInKeep ? thought-part-present : not` branch. |
| 108 | + This is the only new code path; it should have a direct test. |
| 109 | +- The `text: ' '` sentinel is invisible in user-facing logs but |
| 110 | + may surface in trace dumps. Worth confirming no downstream |
| 111 | + formatter trims whitespace-only text and drops the thought part |
| 112 | + with it. |
| 113 | +- Prior fixes #3590 and #3682 closed the resume/idle and |
| 114 | + model-switch paths. After this PR, any *new* control path that |
| 115 | + wants to mutate history needs to learn this lesson without the |
| 116 | + guardrail of helpers — the helpers are gone. A short |
| 117 | + `CHANGELOG`/comment block in `geminiChat.ts` explaining |
| 118 | + *why* these helpers no longer exist would prevent re-introduction. |
| 119 | + |
| 120 | +## Verdict |
| 121 | + |
| 122 | +`merge-after-nits` |
| 123 | + |
| 124 | +## Suggested nits |
| 125 | + |
| 126 | +- Add at least one test in `chatCompressionService.test.ts` |
| 127 | + pinning both branches of the `hasThoughtContentInKeep` check. |
| 128 | +- Add a short tombstone comment in `geminiChat.ts` (where the |
| 129 | + methods used to live) saying "intentionally removed in #3737 |
| 130 | + — reasoning_content must remain contiguous; do not reintroduce". |
| 131 | +- Confirm in PR body that `rg stripThoughtsFromHistory` returns |
| 132 | + zero hits across the whole repo after this PR. |
0 commit comments