-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(codebuddy): refuse leaked vendor tool-call scaffolding (#4596) #4776
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
Merged
lidge-jun
merged 2 commits into
codex/pw5-bounded-tool-wire-names
from
codex/pw6-codebuddy-scaffold-guard
Sep 16, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import type { AdapterEvent } from "../../types"; | ||
|
|
||
| /** Error code for a CodeBuddy turn whose output contains vendor agent scaffolding. */ | ||
| export const CODEBUDDY_SCAFFOLD_ERROR_CODE = "vendor_scaffold_detected"; | ||
|
|
||
| // The observed control protocol uses FULLWIDTH VERTICAL LINE (U+FF5C). Detection stays | ||
| // deliberately narrower than the marker spelling: a calls control line must be followed by an | ||
| // invoke line for a functions.* tool. That distinguishes an agent scaffold from prose quoting or | ||
| // discussing one tag. | ||
| const DSML_CALLS_LINE = "<||dsml|| calls>"; | ||
| const DSML_INVOKE_PREFIX = "<||dsml|| invoke name=\"functions."; | ||
|
|
||
| export interface CodeBuddyScaffoldFilterResult { | ||
| /** Bytes released from a suffix withheld by an earlier event on this channel. */ | ||
| releasedPending: string; | ||
| /** Safe bytes belonging to the event currently being processed. */ | ||
| text: string; | ||
| /** The earlier pending event still owns the extended candidate. */ | ||
| pendingContinues: boolean; | ||
| fail: boolean; | ||
| } | ||
|
|
||
| interface ScanResult { | ||
| safe: string; | ||
| held: string; | ||
| fail: boolean; | ||
| fence: "`" | "~" | null; | ||
| lineStart: boolean; | ||
| } | ||
|
|
||
| function prefixAtEnd(text: string, at: number, expected: string): boolean { | ||
| const rest = text.slice(at).toLowerCase(); | ||
| return rest.length < expected.length && expected.startsWith(rest); | ||
| } | ||
|
|
||
| /** | ||
| * Scan complete bytes and retain only a bounded suffix that can still become a control sequence. | ||
| * | ||
| * Control tags are recognized only at column zero and outside fenced Markdown. Inline code, | ||
| * quoted strings, blockquotes, indented source, and prose all add syntax before the tag and are | ||
| * therefore forwarded unchanged. A calls line alone is harmless; refusal requires the observed | ||
| * two-line calls-plus-functions-invoke grammar. | ||
| */ | ||
| function scan( | ||
| text: string, | ||
| initialFence: "`" | "~" | null, | ||
| initialLineStart: boolean, | ||
| ): ScanResult { | ||
| let fence = initialFence; | ||
| let lineStart = initialLineStart; | ||
| let index = 0; | ||
|
|
||
| while (index < text.length) { | ||
| if (lineStart) { | ||
| const fenceMarkers = fence ? [fence.repeat(3)] : ["```", "~~~"]; | ||
| const completeFence = fenceMarkers.find(marker => text.startsWith(marker, index)); | ||
| if (completeFence) { | ||
| fence = fence ? null : (completeFence[0] as "`" | "~"); | ||
| index += completeFence.length; | ||
| lineStart = false; | ||
| continue; | ||
| } | ||
| if (fenceMarkers.some(marker => prefixAtEnd(text, index, marker))) { | ||
| return { safe: text.slice(0, index), held: text.slice(index), fail: false, fence, lineStart }; | ||
| } | ||
|
|
||
| if (!fence) { | ||
| const lowered = text.slice(index).toLowerCase(); | ||
| if (lowered.startsWith(DSML_CALLS_LINE)) { | ||
| const afterCalls = index + DSML_CALLS_LINE.length; | ||
| let invokeAt = -1; | ||
| if (text[afterCalls] === "\n") invokeAt = afterCalls + 1; | ||
| else if (text[afterCalls] === "\r" && text[afterCalls + 1] === "\n") invokeAt = afterCalls + 2; | ||
| else if (afterCalls === text.length || (text[afterCalls] === "\r" && afterCalls + 1 === text.length)) { | ||
| return { safe: text.slice(0, index), held: text.slice(index), fail: false, fence, lineStart }; | ||
| } | ||
|
|
||
| if (invokeAt >= 0) { | ||
| const invokeRest = text.slice(invokeAt).toLowerCase(); | ||
| if (invokeRest.startsWith(DSML_INVOKE_PREFIX)) { | ||
| return { safe: text.slice(0, index), held: "", fail: true, fence, lineStart }; | ||
| } | ||
| if (invokeRest.length === 0 || DSML_INVOKE_PREFIX.startsWith(invokeRest)) { | ||
| return { safe: text.slice(0, index), held: text.slice(index), fail: false, fence, lineStart }; | ||
| } | ||
| } | ||
| } else if (prefixAtEnd(text, index, DSML_CALLS_LINE)) { | ||
| return { safe: text.slice(0, index), held: text.slice(index), fail: false, fence, lineStart }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const char = text[index]!; | ||
| index += 1; | ||
| lineStart = char === "\n"; | ||
| } | ||
|
|
||
| return { safe: text, held: "", fail: false, fence, lineStart }; | ||
| } | ||
|
|
||
| /** Streaming DSML control-sequence filter for one text or reasoning channel. */ | ||
| export class CodeBuddyScaffoldFilter { | ||
| private pending = ""; | ||
| private failed = false; | ||
| private fence: "`" | "~" | null = null; | ||
| private lineStart = true; | ||
|
|
||
| /** True while an earlier event owns an unresolved marker or fence prefix. */ | ||
| hasPending(): boolean { | ||
| return this.pending.length > 0; | ||
| } | ||
|
|
||
| push(chunk: string): CodeBuddyScaffoldFilterResult { | ||
| if (this.failed) { | ||
| return { releasedPending: "", text: "", pendingContinues: false, fail: false }; | ||
| } | ||
| if (!chunk) { | ||
| return { | ||
| releasedPending: "", | ||
| text: "", | ||
| pendingContinues: this.hasPending(), | ||
| fail: false, | ||
| }; | ||
| } | ||
|
|
||
| const priorPending = this.pending; | ||
| const result = scan(priorPending + chunk, this.fence, this.lineStart); | ||
| this.pending = result.held; | ||
| this.fence = result.fence; | ||
| this.lineStart = result.lineStart; | ||
| this.failed = result.fail; | ||
|
|
||
| const releasedLength = Math.min(priorPending.length, result.safe.length); | ||
| return { | ||
| releasedPending: result.safe.slice(0, releasedLength), | ||
| text: result.safe.slice(releasedLength), | ||
| pendingContinues: priorPending.length > 0 && result.safe.length === 0 && result.held.length > 0, | ||
| fail: result.fail, | ||
| }; | ||
| } | ||
|
|
||
| /** Release a suffix that never completed the two-line control grammar. */ | ||
| flush(): CodeBuddyScaffoldFilterResult { | ||
| if (this.failed) { | ||
| return { releasedPending: "", text: "", pendingContinues: false, fail: false }; | ||
| } | ||
| const text = this.pending; | ||
| this.pending = ""; | ||
| return { releasedPending: text, text: "", pendingContinues: false, fail: false }; | ||
| } | ||
| } | ||
|
|
||
| function codeBuddyScaffoldErrorMessage(): string { | ||
| return "CodeBuddy CLI emitted vendor tool-call markup in an assistant output channel. This route" | ||
| + " runs the CLI with its own tools and MCP servers disabled and Codex owns tool control, so" | ||
| + " the turn was refused rather than forwarding or executing vendor agent scaffolding."; | ||
| } | ||
|
|
||
| /** Guard both streamed channels while preserving event order around withheld marker prefixes. */ | ||
| export function guardCodeBuddyScaffolding(emit: (event: AdapterEvent) => void): (event: AdapterEvent) => void { | ||
| const textFilter = new CodeBuddyScaffoldFilter(); | ||
| const thinkingFilter = new CodeBuddyScaffoldFilter(); | ||
| type PendingChannel = "text" | "thinking"; | ||
| type EventSlot = { resolved: boolean; event?: AdapterEvent }; | ||
| const eventQueue: EventSlot[] = []; | ||
| const pendingSlots = new Map<PendingChannel, EventSlot>(); | ||
| let closed = false; | ||
|
|
||
| const channelEvent = (channel: PendingChannel, text: string): AdapterEvent => channel === "text" | ||
| ? { type: "text_delta", text } | ||
| : { type: "thinking_delta", thinking: text }; | ||
|
|
||
| const drainResolved = (): void => { | ||
| while (eventQueue[0]?.resolved) { | ||
| const slot = eventQueue.shift()!; | ||
| if (slot.event) emit(slot.event); | ||
| } | ||
| }; | ||
|
|
||
| const enqueueResolved = (event: AdapterEvent): void => { | ||
| eventQueue.push({ resolved: true, event }); | ||
| drainResolved(); | ||
| }; | ||
|
|
||
| const resolvePendingSlot = (channel: PendingChannel, text: string): void => { | ||
| const slot = pendingSlots.get(channel); | ||
| if (!slot) return; | ||
| slot.resolved = true; | ||
| if (text) slot.event = channelEvent(channel, text); | ||
| pendingSlots.delete(channel); | ||
| drainResolved(); | ||
| }; | ||
|
|
||
| const enqueuePendingSlot = (channel: PendingChannel): void => { | ||
| const slot: EventSlot = { resolved: false }; | ||
| eventQueue.push(slot); | ||
| pendingSlots.set(channel, slot); | ||
| }; | ||
|
|
||
| const flushAllPending = (): void => { | ||
| for (const channel of ["text", "thinking"] as const) { | ||
| if (!pendingSlots.has(channel)) continue; | ||
| const filter = channel === "text" ? textFilter : thinkingFilter; | ||
| resolvePendingSlot(channel, filter.flush().releasedPending); | ||
| } | ||
| drainResolved(); | ||
| }; | ||
|
|
||
| const refuse = (): void => { | ||
| if (closed) return; | ||
| flushAllPending(); | ||
| closed = true; | ||
| emit({ | ||
| type: "error", | ||
| message: codeBuddyScaffoldErrorMessage(), | ||
| status: 502, | ||
| errorType: "upstream_error", | ||
| code: CODEBUDDY_SCAFFOLD_ERROR_CODE, | ||
| retryable: false, | ||
| }); | ||
| }; | ||
|
|
||
| return (event: AdapterEvent): void => { | ||
| if (closed) return; | ||
| if (event.type === "text_delta" || event.type === "thinking_delta") { | ||
| const channel: PendingChannel = event.type === "text_delta" ? "text" : "thinking"; | ||
| const filter = channel === "text" ? textFilter : thinkingFilter; | ||
| const hadPending = filter.hasPending(); | ||
| const cleaned = filter.push(event.type === "text_delta" ? event.text : event.thinking); | ||
| if (hadPending && !cleaned.pendingContinues) resolvePendingSlot(channel, cleaned.releasedPending); | ||
| if (cleaned.text) { | ||
| enqueueResolved(event.type === "text_delta" | ||
| ? { ...event, text: cleaned.text } | ||
| : { ...event, thinking: cleaned.text }); | ||
| } | ||
| if (filter.hasPending() && !cleaned.pendingContinues) enqueuePendingSlot(channel); | ||
| if (cleaned.fail) refuse(); | ||
| return; | ||
| } | ||
| if (event.type === "done" || event.type === "error" || event.type === "incomplete") { | ||
| flushAllPending(); | ||
| closed = true; | ||
| emit(event); | ||
| return; | ||
| } | ||
| enqueueResolved(event); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When an answer uses a valid four-backtick outer fence to quote a three-backtick fenced example, this scanner consumes only the first three opening backticks and then treats the inner
```textline as the closing fence, even though it has trailing content. The subsequent DSML example is therefore parsed as live control markup and the turn incorrectly ends withvendor_scaffold_detected, contradicting the intended guarantee that fenced examples pass unchanged. Record the opening delimiter length and only close on a delimiter of at least that length with a valid closing-fence suffix.Useful? React with 👍 / 👎.