-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Strip internal Git context tags from agent responses #4343
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
1c8ea0a
8a4d107
fb9a903
2bae88a
5bb9dbc
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| GitContextEchoSanitizer, | ||
| stripGitContextEchoes, | ||
| stripGitContextEchoesFromAssistantMessages, | ||
| } from "./git_context_sanitizer"; | ||
|
|
||
| describe("GitContextEchoSanitizer", () => { | ||
| it("strips an echoed Git-context tag split across stream chunks", () => { | ||
| const sanitizer = new GitContextEchoSanitizer(); | ||
|
|
||
| const output = [ | ||
| sanitizer.push("Done.<dyad-git-con"), | ||
| sanitizer.push('text commit="fake">'), | ||
| sanitizer.push("</dyad-git-context>"), | ||
| sanitizer.finish(), | ||
| ].join(""); | ||
|
|
||
| expect(output).toBe("Done."); | ||
| }); | ||
|
|
||
| it("preserves ordinary text around internal tag markup", () => { | ||
| expect( | ||
| stripGitContextEchoes( | ||
| 'Before <dyad-git-context commit="fake">unexpected</dyad-git-context> after', | ||
| ), | ||
| ).toBe("Before unexpected after"); | ||
| }); | ||
|
|
||
| it("does not strip similarly named user text", () => { | ||
| expect(stripGitContextEchoes("Use <dyad-git-contextual> here.")).toBe( | ||
| "Use <dyad-git-contextual> here.", | ||
| ); | ||
| }); | ||
|
|
||
| it("drops a distinctive internal tag fragment if the stream ends", () => { | ||
| const sanitizer = new GitContextEchoSanitizer(); | ||
|
|
||
| expect(sanitizer.push("Done.<dyad-git-con")).toBe("Done."); | ||
| expect(sanitizer.finish()).toBe(""); | ||
| }); | ||
|
|
||
| it("removes echoes from assistant message text only", () => { | ||
| const tag = '<dyad-git-context commit="fake"></dyad-git-context>'; | ||
| const messages = stripGitContextEchoesFromAssistantMessages([ | ||
| { role: "user", content: `User literal: ${tag}` }, | ||
| { | ||
| role: "assistant", | ||
| content: [ | ||
| { type: "text", text: `Finished.${tag}` }, | ||
| { | ||
| type: "tool-call", | ||
| toolCallId: "call-1", | ||
| toolName: "read_file", | ||
| input: {}, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| expect(messages[0]).toEqual({ | ||
| role: "user", | ||
| content: `User literal: ${tag}`, | ||
| }); | ||
| expect(messages[1]).toMatchObject({ | ||
| role: "assistant", | ||
| content: [ | ||
| { type: "text", text: "Finished." }, | ||
| { type: "tool-call", toolCallId: "call-1" }, | ||
| ], | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import type { ModelMessage } from "ai"; | ||
|
|
||
| const GIT_CONTEXT_TAG_MARKERS = [ | ||
| "<dyad-git-context", | ||
| "</dyad-git-context", | ||
| ] as const; | ||
|
|
||
| /** | ||
| * Removes internal Git-context tag markup without exposing partial tags while | ||
| * model text is streaming. Text between an opening and closing tag is | ||
| * preserved; only the internal markup itself is metadata. | ||
| */ | ||
| export class GitContextEchoSanitizer { | ||
| private pending = ""; | ||
|
|
||
| push(chunk: string): string { | ||
| this.pending += chunk; | ||
| let output = ""; | ||
|
|
||
| while (this.pending.length > 0) { | ||
| const markerIndex = findNextMarkerIndex(this.pending); | ||
| if (markerIndex === -1) { | ||
| const heldSuffixLength = getPotentialMarkerSuffixLength(this.pending); | ||
| const emitLength = this.pending.length - heldSuffixLength; | ||
| output += this.pending.slice(0, emitLength); | ||
| this.pending = this.pending.slice(emitLength); | ||
| break; | ||
| } | ||
|
|
||
| output += this.pending.slice(0, markerIndex); | ||
| this.pending = this.pending.slice(markerIndex); | ||
|
|
||
| const tagEndIndex = this.pending.indexOf(">"); | ||
| if (tagEndIndex === -1) { | ||
| break; | ||
| } | ||
| this.pending = this.pending.slice(tagEndIndex + 1); | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| finish(): string { | ||
| const pending = this.pending; | ||
| this.pending = ""; | ||
| return startsWithMarker(pending) || | ||
| startsWithDistinctivePartialMarker(pending) | ||
| ? "" | ||
| : pending; | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| export function stripGitContextEchoes(text: string): string { | ||
| const sanitizer = new GitContextEchoSanitizer(); | ||
| return sanitizer.push(text) + sanitizer.finish(); | ||
| } | ||
|
|
||
| export function stripGitContextEchoesFromAssistantMessages( | ||
| messages: ModelMessage[], | ||
| ): ModelMessage[] { | ||
| return messages.map((message) => { | ||
| if (message.role !== "assistant") { | ||
| return message; | ||
| } | ||
|
|
||
| if (typeof message.content === "string") { | ||
| return { | ||
| ...message, | ||
| content: stripGitContextEchoes(message.content), | ||
| }; | ||
| } | ||
|
|
||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| const content = message.content.map((part) => { | ||
| if (part.type !== "text") { | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| return part; | ||
| } | ||
| const text = stripGitContextEchoes(part.text); | ||
| return { ...part, text }; | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| return { ...message, content }; | ||
| }); | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| function findNextMarkerIndex(text: string): number { | ||
| const normalized = text.toLowerCase(); | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| let earliest = -1; | ||
| for (const marker of GIT_CONTEXT_TAG_MARKERS) { | ||
| let searchFrom = 0; | ||
| while (searchFrom < normalized.length) { | ||
| const index = normalized.indexOf(marker, searchFrom); | ||
| if (index === -1) { | ||
| break; | ||
| } | ||
| if (isTagBoundary(normalized[index + marker.length])) { | ||
| if (earliest === -1 || index < earliest) { | ||
| earliest = index; | ||
| } | ||
| break; | ||
| } | ||
| searchFrom = index + 1; | ||
| } | ||
| } | ||
| return earliest; | ||
| } | ||
|
|
||
| function getPotentialMarkerSuffixLength(text: string): number { | ||
| const normalized = text.toLowerCase(); | ||
| const maxLength = Math.min( | ||
| normalized.length, | ||
| Math.max(...GIT_CONTEXT_TAG_MARKERS.map((marker) => marker.length - 1)), | ||
| ); | ||
|
|
||
| for (let length = maxLength; length > 0; length -= 1) { | ||
| const suffix = normalized.slice(-length); | ||
| if (GIT_CONTEXT_TAG_MARKERS.some((marker) => marker.startsWith(suffix))) { | ||
| return length; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| function startsWithMarker(text: string): boolean { | ||
| const normalized = text.toLowerCase(); | ||
| return GIT_CONTEXT_TAG_MARKERS.some( | ||
| (marker) => | ||
| normalized.startsWith(marker) && isTagBoundary(normalized[marker.length]), | ||
| ); | ||
| } | ||
|
|
||
| function startsWithDistinctivePartialMarker(text: string): boolean { | ||
| const normalized = text.toLowerCase(); | ||
| const minimumDistinctivePrefix = "<dyad-git-"; | ||
| return ( | ||
| normalized.length >= minimumDistinctivePrefix.length && | ||
| GIT_CONTEXT_TAG_MARKERS.some((marker) => marker.startsWith(normalized)) | ||
| ); | ||
| } | ||
|
|
||
| function isTagBoundary(character: string | undefined): boolean { | ||
| return ( | ||
| character === undefined || | ||
| character === ">" || | ||
| character === "/" || | ||
| /\s/.test(character) | ||
|
keppo-bot[bot] marked this conversation as resolved.
|
||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,10 @@ import { | |
| } from "./prepare_step_utils"; | ||
| import { deleteTodos, loadTodos, saveTodos } from "./todo_persistence"; | ||
| import { ensureDyadGitignored } from "@/ipc/handlers/gitignoreUtils"; | ||
| import { | ||
| GitContextEchoSanitizer, | ||
| stripGitContextEchoesFromAssistantMessages, | ||
| } from "./git_context_sanitizer"; | ||
| import { TOOL_DEFINITIONS } from "./tool_definitions"; | ||
| import { | ||
| parseAiMessagesJson, | ||
|
|
@@ -1442,6 +1446,7 @@ export async function handleLocalAgentStream( | |
|
|
||
| let inThinkingBlock = false; | ||
| let streamErrorFromIteration: unknown; | ||
| const gitContextEchoSanitizer = new GitContextEchoSanitizer(); | ||
|
|
||
| try { | ||
| for await (const part of fullStream) { | ||
|
|
@@ -1483,12 +1488,19 @@ export async function handleLocalAgentStream( | |
| break; | ||
|
|
||
| case "text-delta": | ||
| passProducedChatText = true; | ||
| chunk += part.text; | ||
| maybeCaptureRetryReplayText( | ||
| activeRetryReplayEvents, | ||
| part.text, | ||
| ); | ||
| { | ||
| const sanitizedText = gitContextEchoSanitizer.push( | ||
|
keppo-bot[bot] marked this conversation as resolved.
Outdated
|
||
| part.text, | ||
| ); | ||
| if (sanitizedText.length > 0) { | ||
| passProducedChatText = true; | ||
| chunk += sanitizedText; | ||
| maybeCaptureRetryReplayText( | ||
| activeRetryReplayEvents, | ||
| sanitizedText, | ||
| ); | ||
| } | ||
| } | ||
| break; | ||
|
|
||
| case "reasoning-start": | ||
|
|
@@ -1647,6 +1659,15 @@ export async function handleLocalAgentStream( | |
| } | ||
| } | ||
|
|
||
| const trailingText = gitContextEchoSanitizer.finish(); | ||
| if (trailingText.length > 0) { | ||
| passProducedChatText = true; | ||
| fullResponse += trailingText; | ||
| maybeCaptureRetryReplayText(activeRetryReplayEvents, trailingText); | ||
| await updateResponseInDb(placeholderMessageId, fullResponse); | ||
| sendChunk(fullResponse); | ||
| } | ||
|
|
||
| // Close thinking block if still open | ||
| if (inThinkingBlock) { | ||
| const closingThinkBlock = "</think>\n"; | ||
|
|
@@ -1716,7 +1737,9 @@ export async function handleLocalAgentStream( | |
| try { | ||
| const response = await streamResult.response; | ||
| steps = (await streamResult.steps) ?? []; | ||
| responseMessages = response.messages; | ||
| responseMessages = stripGitContextEchoesFromAssistantMessages( | ||
|
keppo-bot[bot] marked this conversation as resolved.
Contributor
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. 🟡 MEDIUM Sanitizing before mid-turn compaction slicing can misalign the offset responseMessages is now the sanitized array, but the mid-turn compaction path slices it using prevStepMessages.length taken from the unsanitized steps[...].response.messages. stripGitContextEchoesFromAssistantMessages can remove whole messages (an assistant message whose only content was an echoed tag returns an empty array), so the two lengths can disagree and responseMessages.slice(prevStepMessages.length) then drops real post-compaction messages or keeps pre-compaction ones. That silently corrupts the persisted transcript and the history replayed for the rest of the turn. 💡 Suggestion: Compute the slice from the raw response.messages and sanitize afterwards (sanitize messagesToAccumulate), so the offset and the array being sliced come from the same source. |
||
| response.messages, | ||
| ); | ||
| } catch (err) { | ||
| if ( | ||
| shouldRetryTransientStreamError({ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.