Repository navigation
fix(core): deduplicate geminiChat recovery continuation text #3966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
18273d3
549f27a
7bd70dc
24582d4
9eb7f72
3663e8b
08f2abd
556b015
25ea94e
347f6ab
a916c68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -113,6 +113,176 @@ const OUTPUT_RECOVERY_MESSAGE = | |||||||||||||||||||||||||||
| 'you were doing. Pick up mid-thought if that is where the cut happened. ' + | ||||||||||||||||||||||||||||
| 'Break remaining work into smaller pieces.'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const OUTPUT_RECOVERY_TAIL_CHARS = 1200; | ||||||||||||||||||||||||||||
| const RECOVERY_OVERLAP_MAX_SCAN_CHARS = 4000; | ||||||||||||||||||||||||||||
| const RECOVERY_OVERLAP_MIN_BYTES = 6; | ||||||||||||||||||||||||||||
| const RECOVERY_STRUCTURAL_OVERLAP_MIN_BYTES = 4; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function byteLength(text: string): number { | ||||||||||||||||||||||||||||
| return Buffer.byteLength(text, 'utf8'); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function isSignificantRecoveryOverlap(overlap: string): boolean { | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] Impact: Minimal — the gap is only 2 bytes and coincidence at truncation boundaries is rare. This is primarily a classification-accuracy concern: the implementation is looser than the JSDoc's "structural anchors" implies. Suggested fix: Scope to Markdown contexts (e.g., — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documented in 25ea94e (no behavior change). Taking your "add a comment acknowledging the intentional over-classification" path rather than scoping to Markdown contexts. Reasoning: the 4-byte structural floor vs the 6-byte prose floor only differs by a 2-byte window. Realistic prose at 4–5 bytes containing The new inline comment makes the trade-off explicit so future readers don't have to reverse-engineer the intent. |
||||||||||||||||||||||||||||
| const overlapBytes = byteLength(overlap); | ||||||||||||||||||||||||||||
| const hasMarkdownStructure = /[#|`\n]/.test(overlap); | ||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||
| overlapBytes >= RECOVERY_OVERLAP_MIN_BYTES || | ||||||||||||||||||||||||||||
| (hasMarkdownStructure && | ||||||||||||||||||||||||||||
| overlapBytes >= RECOVERY_STRUCTURAL_OVERLAP_MIN_BYTES) | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function findContainedRecoveryPrefixReplayLength( | ||||||||||||||||||||||||||||
| previousText: string, | ||||||||||||||||||||||||||||
| continuationText: string, | ||||||||||||||||||||||||||||
| ): number { | ||||||||||||||||||||||||||||
| const previousTail = | ||||||||||||||||||||||||||||
| previousText.length > RECOVERY_OVERLAP_MAX_SCAN_CHARS | ||||||||||||||||||||||||||||
| ? previousText.slice(-RECOVERY_OVERLAP_MAX_SCAN_CHARS) | ||||||||||||||||||||||||||||
| : previousText; | ||||||||||||||||||||||||||||
| const maxPrefix = Math.min( | ||||||||||||||||||||||||||||
| previousTail.length, | ||||||||||||||||||||||||||||
| continuationText.length, | ||||||||||||||||||||||||||||
| RECOVERY_OVERLAP_MAX_SCAN_CHARS, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (let length = maxPrefix; length > 0; length -= 1) { | ||||||||||||||||||||||||||||
| const prefix = continuationText.slice(0, length); | ||||||||||||||||||||||||||||
| if (isSignificantRecoveryOverlap(prefix) && previousTail.includes(prefix)) { | ||||||||||||||||||||||||||||
| return length; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function getRecoveryContinuationSuffix( | ||||||||||||||||||||||||||||
| previousText: string, | ||||||||||||||||||||||||||||
| continuationText: string, | ||||||||||||||||||||||||||||
| ): string { | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion]
Suggested change
— DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 549f27a — |
||||||||||||||||||||||||||||
| if (previousText.length === 0 || continuationText.length === 0) { | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The Consider adding a test where the continuation turn has only thought parts (no text parts), asserting continuationText passes through verbatim. — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked at this carefully — the
Rather than add a synthetic test that exports an internal helper just to hit defensive-only code, I documented the intent in 556b015. The new JSDoc on |
||||||||||||||||||||||||||||
| return continuationText; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion]
Suggested change
— DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 549f27a — |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| previousText.endsWith(continuationText) && | ||||||||||||||||||||||||||||
| isSignificantRecoveryOverlap(continuationText) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const maxOverlap = Math.min( | ||||||||||||||||||||||||||||
| previousText.length, | ||||||||||||||||||||||||||||
| continuationText.length, | ||||||||||||||||||||||||||||
| RECOVERY_OVERLAP_MAX_SCAN_CHARS, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (let length = maxOverlap; length > 0; length -= 1) { | ||||||||||||||||||||||||||||
| const overlap = continuationText.slice(0, length); | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| isSignificantRecoveryOverlap(overlap) && | ||||||||||||||||||||||||||||
| previousText.endsWith(overlap) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| return continuationText.slice(length); | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 08f2abd. Replaced the in-place mergedParts[previousTextIndex] = {
...previousTextPart,
text: previousTextPart.text + suffix,
};Currently no downstream caller appears to cache part references across the recovery merge, but the mutation invariant was fragile and the allocation is essentially free, so worth fixing defensively. (The same change happens to be necessary for the [Critical] line-404 fix, since the new code splices into |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+359
to
+367
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perf note (non-blocking): this loop is O(n²) — up to If you ever want to optimize:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed it's non-blocking — opted to document rather than refactor in this PR to keep the change focused on review polish. Added an inline comment in 3663e8b above the loop that:
Happy to do the actual optimization in a follow-up if/when the scan cap becomes a real concern — keeping this PR scoped to the dedup correctness work felt cleaner than mixing in a perf rewrite that isn't needed yet.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed it's fine at current scale — keeping the simple loop as-is. Did add an inline comment in 3663e8ba making the bound explicit so the next reader doesn't have to derive it:
See L285-L291. Recovery only fires after a MAX_TOKENS truncation (rare per session), so the ~16M-op worst case is not on any hot path. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Providers/models frequently resume a MAX_TOKENS recovery from an anchor | ||||||||||||||||||||||||||||
| // that appears near the tail of the previous response, rather than from the | ||||||||||||||||||||||||||||
| // exact last byte. Drop that replayed leading prefix before coalescing the | ||||||||||||||||||||||||||||
| // recovery model turn into durable history; otherwise later turns inherit | ||||||||||||||||||||||||||||
| // duplicated Markdown tables/prose even if the live UI suppresses them. | ||||||||||||||||||||||||||||
| const containedPrefixLength = findContainedRecoveryPrefixReplayLength( | ||||||||||||||||||||||||||||
| previousText, | ||||||||||||||||||||||||||||
| continuationText, | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The newline-normalization path in Consider adding a test: previousText ends without newline (e.g., — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a regression test in 556b015. The new |
||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| if (containedPrefixLength > 0) { | ||||||||||||||||||||||||||||
| const replayedPrefix = continuationText.slice(0, containedPrefixLength); | ||||||||||||||||||||||||||||
| let suffix = continuationText.slice(containedPrefixLength); | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| suffix.length > 0 && | ||||||||||||||||||||||||||||
| replayedPrefix.endsWith('\n') && | ||||||||||||||||||||||||||||
| !previousText.endsWith('\n') && | ||||||||||||||||||||||||||||
| !suffix.startsWith('\n') | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| suffix = `\n${suffix}`; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return suffix; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return continuationText; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function isPlainTextPart(part: Part | undefined): part is Part & { | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] — deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — fixed in 24582d4. |
||||||||||||||||||||||||||||
| text: string; | ||||||||||||||||||||||||||||
| } { | ||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||
| part !== undefined && | ||||||||||||||||||||||||||||
| typeof part.text === 'string' && | ||||||||||||||||||||||||||||
| part.thought !== true && | ||||||||||||||||||||||||||||
| part.functionCall === undefined && | ||||||||||||||||||||||||||||
| part.functionResponse === undefined | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function getPlainTextFromParts(parts: Part[] | undefined): string { | ||||||||||||||||||||||||||||
| return (parts ?? []) | ||||||||||||||||||||||||||||
| .filter(isPlainTextPart) | ||||||||||||||||||||||||||||
| .map((part) => part.text) | ||||||||||||||||||||||||||||
| .join(''); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function buildOutputRecoveryMessage(previousModelTurn: Content | undefined) { | ||||||||||||||||||||||||||||
| const previousText = | ||||||||||||||||||||||||||||
| previousModelTurn?.role === 'model' | ||||||||||||||||||||||||||||
| ? getPlainTextFromParts(previousModelTurn.parts) | ||||||||||||||||||||||||||||
| : ''; | ||||||||||||||||||||||||||||
| if (previousText.trim().length === 0) { | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion]
Suggested change
— DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 549f27a — same |
||||||||||||||||||||||||||||
| return OUTPUT_RECOVERY_MESSAGE; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const tail = | ||||||||||||||||||||||||||||
| previousText.length > OUTPUT_RECOVERY_TAIL_CHARS | ||||||||||||||||||||||||||||
| ? previousText.slice(-OUTPUT_RECOVERY_TAIL_CHARS) | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] 建议添加测试:使用 — deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — added a regression test in 7bd70dc. It uses a 1300-char previous response ( |
||||||||||||||||||||||||||||
| : previousText; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||
| `${OUTPUT_RECOVERY_MESSAGE}\n\n` + | ||||||||||||||||||||||||||||
| 'The previous assistant response ended with this exact suffix. ' + | ||||||||||||||||||||||||||||
| 'Do not repeat any line, table row, code line, or prose that already ' + | ||||||||||||||||||||||||||||
| 'appears in it; output only text that comes after this suffix:\n\n' + | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] — deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — fixed in 24582d4. Added Regression test |
||||||||||||||||||||||||||||
| '<previous_response_suffix>\n' + | ||||||||||||||||||||||||||||
| tail + | ||||||||||||||||||||||||||||
| '\n</previous_response_suffix>' | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function appendRecoveryContinuationParts( | ||||||||||||||||||||||||||||
| previousParts: Part[] | undefined, | ||||||||||||||||||||||||||||
| continuationParts: Part[] | undefined, | ||||||||||||||||||||||||||||
| ): Part[] { | ||||||||||||||||||||||||||||
| const mergedParts = [...(previousParts ?? [])]; | ||||||||||||||||||||||||||||
| const nextParts = [...(continuationParts ?? [])]; | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical]
Suggested change
— DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed real bug — traced the part ordering in Fixed in 08f2abd by scanning both sides for the plain-text anchor instead of locking onto the boundary indices, and splicing the matched text part instead of shifting the head. Added a regression test (
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] Consider adding a prominent comment above — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call — documented in 556b015. Added a JSDoc block above Re: the integration test for multiple adjacent unconsolidated text parts — deferring. Such an array shape is unreachable from the live recovery pipeline (every callsite of |
||||||||||||||||||||||||||||
| const previousLastPart = mergedParts[mergedParts.length - 1]; | ||||||||||||||||||||||||||||
| const continuationFirstPart = nextParts[0]; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| isPlainTextPart(previousLastPart) && | ||||||||||||||||||||||||||||
| isPlainTextPart(continuationFirstPart) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| const suffix = getRecoveryContinuationSuffix( | ||||||||||||||||||||||||||||
| previousLastPart.text, | ||||||||||||||||||||||||||||
| continuationFirstPart.text, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| if (suffix.length > 0) { | ||||||||||||||||||||||||||||
| previousLastPart.text += suffix; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| nextParts.shift(); | ||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reads as if // Always shift the first continuation text part: a non-empty suffix has
// already been appended to previousLastPart, and an empty suffix means
// the part was a pure replay and should be discarded.
nextParts.shift();
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call — added the explanatory comment in 3663e8b, very close to your suggested wording: if (suffix.length > 0) {
previousLastPart.text += suffix;
}
// Always drop the first continuation text part: a non-empty suffix has
// already been appended to previousLastPart above, and an empty suffix
// means the part was a pure replay of the previous tail and should be
// discarded so it does not duplicate into history.
nextParts.shift();Saves the double-take exactly where you flagged it.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed intentional, and added the clarifying comment you suggested (slightly reworded) in 3663e8ba: // Always drop the first continuation text part: a non-empty suffix has
// already been appended to previousLastPart above, and an empty suffix
// means the part was a pure replay of the previous tail and should be
// discarded so it does not duplicate into history.
nextParts.shift();See L419-L423. The two cases (non-empty suffix already merged vs. empty suffix = pure replay) both want the part gone, so the unconditional |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return [...mergedParts, ...nextParts]; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Options for retrying on rate-limit throttling errors returned as stream content. | ||||||||||||||||||||||||||||
| * Starts at 60s to match DashScope's per-minute quota window, then backs off | ||||||||||||||||||||||||||||
|
|
@@ -777,7 +947,9 @@ export class GeminiChat { | |||||||||||||||||||||||||||
| // (pushed by processStreamResponse). Push a recovery user | ||||||||||||||||||||||||||||
| // message so the model sees its partial output and continues. | ||||||||||||||||||||||||||||
| self.history.push( | ||||||||||||||||||||||||||||
| createUserContent([{ text: OUTPUT_RECOVERY_MESSAGE }]), | ||||||||||||||||||||||||||||
| createUserContent([ | ||||||||||||||||||||||||||||
| { text: buildOutputRecoveryMessage(lastEntry) }, | ||||||||||||||||||||||||||||
| ]), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| // Signal UI/turn to clear pending (incomplete) tool calls. | ||||||||||||||||||||||||||||
| // isContinuation tells the UI to keep the text buffer so the | ||||||||||||||||||||||||||||
|
|
@@ -1204,10 +1376,10 @@ export class GeminiChat { | |||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| precedingModel.parts = [ | ||||||||||||||||||||||||||||
| ...(precedingModel.parts ?? []), | ||||||||||||||||||||||||||||
| ...(modelContinuation.parts ?? []), | ||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||
| precedingModel.parts = appendRecoveryContinuationParts( | ||||||||||||||||||||||||||||
| precedingModel.parts, | ||||||||||||||||||||||||||||
| modelContinuation.parts, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| // Drop the (userRecovery, modelContinuation) pair. | ||||||||||||||||||||||||||||
| this.history.splice(len - 2, 2); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: these magic numbers deserve a sentence of rationale each, the way
RECOVERY_CONTAINED_TAIL_LOOKBACK_CHARSandRECOVERY_CONTAINED_PREFIX_MIN_BYTESalready get below. Specifically:OUTPUT_RECOVERY_TAIL_CHARS = 1200— why 1200? (token budget consideration? empirically enough context?)RECOVERY_OVERLAP_MAX_SCAN_CHARS = 4000— caps the inner loop; worth saying so.RECOVERY_OVERLAP_MIN_BYTES = 6— minimum length before a plain-text overlap is treated as significant (presumably to avoid coincidental short shared suffixes like". "or"the ").RECOVERY_STRUCTURAL_OVERLAP_MIN_BYTES = 4— lower floor when the overlap contains Markdown structure chars; explain why structural overlaps get a lower bar.Future readers will need to adjust these and the reasoning shouldn't have to be reverse-engineered from the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — done in 3663e8b. Each of the four constants now has a JSDoc block explaining the rationale:
OUTPUT_RECOVERY_TAIL_CHARS = 1200— pragmatic balance: large enough for ~200–400 tokens of trailing prose or a multi-row Markdown table to give the model coherent resume context, small enough to stay well under provider input budgets even combined with the rest of history.RECOVERY_OVERLAP_MAX_SCAN_CHARS = 4000— hard cap on both the suffix-anchored overlap scan and the contained-prefix scan, so recovery dedup stays O(min(prev, cont, 4000)) in iteration count.RECOVERY_OVERLAP_MIN_BYTES = 6— minimum bytes for a plain-text overlap to count as significant; explicitly calls out the". "/"the "/", and "false-positive class your comment named.RECOVERY_STRUCTURAL_OVERLAP_MIN_BYTES = 4— lower floor when the overlap contains#/|/backtick/\n, since those don't collide coincidentally the way short prose does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — added a JSDoc block per constant in 3663e8ba. Each rationale captures the angle you flagged:
OUTPUT_RECOVERY_TAIL_CHARS = 1200— large enough for ~200–400 tokens of trailing context (or a multi-row table), small enough to stay under any provider's input budget alongside the rest of history.RECOVERY_OVERLAP_MAX_SCAN_CHARS = 4000— explicit hard cap on the inner loops so dedup staysO(min(previous, continuation, 4000)).RECOVERY_OVERLAP_MIN_BYTES = 6— exactly the". "/"the "false-positive floor you suggested.RECOVERY_STRUCTURAL_OVERLAP_MIN_BYTES = 4— explains why structural overlaps get a lower bar ("| a ","## "are almost certainly replayed block markers).See geminiChat.ts L116-L153.