Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fdd472f
fix(core): strip leaked protocol summary tags
yiliang114 Jul 9, 2026
339fde7
refactor(core): remove stale analysis block stripper
yiliang114 Jul 9, 2026
58e10a1
test(cli): add protocol tag leak interactive mock
yiliang114 Jul 9, 2026
4e1bf7f
docs: add protocol tag tmux evidence report
yiliang114 Jul 9, 2026
60fd164
test(core): cover tagged retry discard evidence
yiliang114 Jul 9, 2026
f27f787
test(core): cover protocol tag retry filtering
yiliang114 Jul 9, 2026
9649f46
test(integration): cover protocol tag filtering after retries
yiliang114 Jul 9, 2026
bb3fc21
test(core): trim protocol tag retry coverage
yiliang114 Jul 10, 2026
9ab6634
test(integration): assert exact retry replay
yiliang114 Jul 10, 2026
d90f0ed
fix(core): sanitize protocol tags at chat stream boundary
yiliang114 Jul 10, 2026
2de3c14
Merge remote-tracking branch 'origin/main' into fix/leaked-analysis-s…
yiliang114 Jul 10, 2026
e39b766
fix(core): harden protocol stream retry handling
yiliang114 Jul 10, 2026
ea7417d
chore: drop unrelated merge formatting
yiliang114 Jul 10, 2026
b3c1bf6
test(integration): cover hidden SSE disconnect retry
yiliang114 Jul 10, 2026
10327d9
test(core): harden sanitizer coverage and add review feedback
yiliang114 Jul 10, 2026
b15f03d
Merge remote-tracking branch 'origin/main' into fix/leaked-analysis-s…
yiliang114 Jul 10, 2026
8f2a57b
Merge branch 'main' into fix/leaked-analysis-summary-tags-main
yiliang114 Jul 10, 2026
92d488b
Merge remote-tracking branch 'origin/fix/leaked-analysis-summary-tags…
yiliang114 Jul 10, 2026
ae739fc
test(cli): isolate cli entry fallback fixture
yiliang114 Jul 10, 2026
efd3a1d
fix(core): harden protocol tag sanitization
yiliang114 Jul 10, 2026
b885016
Merge branch 'fix/leaked-analysis-summary-tags-main' of https://githu…
yiliang114 Jul 10, 2026
dc3f0c1
Merge branch 'main' into fix/leaked-analysis-summary-tags-main
yiliang114 Jul 10, 2026
77779d4
fix(core): preserve literal protocol tag mentions
yiliang114 Jul 10, 2026
52d1807
Merge remote-tracking branch 'origin/fix/leaked-analysis-summary-tags…
yiliang114 Jul 10, 2026
c6be994
test(webui): stabilize heartbeat prompt cleanup test
yiliang114 Jul 10, 2026
5721915
fix(core): preserve visible protocol tag literals
yiliang114 Jul 10, 2026
cfad489
Merge branch 'fix/leaked-analysis-summary-tags-main' of https://githu…
yiliang114 Jul 10, 2026
c4e49af
fix(core): align streaming sanitizer with batch on summary-internal a…
yiliang114 Jul 10, 2026
9efac29
fix(core): stop unmatched "<" from breaking streaming tag detection
yiliang114 Jul 10, 2026
16bcd7c
fix(core): retry leaked protocol turns
yiliang114 Jul 10, 2026
eb89c03
test(integration): isolate protocol retry scenario
yiliang114 Jul 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions integration-tests/fake-openai-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type FakeOpenAIToolCall = {

export type FakeOpenAIResponse = {
content?: string;
contentChunks?: string[];
disconnectAfterContentChunks?: number;
toolCalls?: FakeOpenAIToolCall[];
finishReason?: 'stop' | 'tool_calls' | 'length';
usage?: {
Expand Down Expand Up @@ -217,7 +219,7 @@ function writeNonStreamed(
index: 0,
message: {
role: 'assistant',
content: message.content ?? null,
content: message.content ?? message.contentChunks?.join('') ?? null,
...(message.toolCalls ? { tool_calls: message.toolCalls } : {}),
},
finish_reason: finishReason(message),
Expand Down Expand Up @@ -253,12 +255,19 @@ function writeStreamed(
choices: [{ index: 0, delta, finish_reason }],
...(usage ? { usage } : {}),
});
const send = (payload: unknown) => {
res.write(`data: ${JSON.stringify(payload)}\n\n`);
const send = (payload: unknown, callback?: () => void) => {
res.write(`data: ${JSON.stringify(payload)}\n\n`, callback);
};

send(chunk({ role: 'assistant' }));
if (message.content) {
for (const [index, content] of (message.contentChunks ?? []).entries()) {
if (message.disconnectAfterContentChunks === index + 1) {
send(chunk({ content }), () => res.destroy());
return;
}
send(chunk({ content }));
}
if (!message.contentChunks && message.content) {
send(chunk({ content: message.content }));
}
for (const [index, toolCall] of (message.toolCalls ?? []).entries()) {
Expand Down
127 changes: 127 additions & 0 deletions integration-tests/interactive/protocol-tags-interactive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import stripAnsi from 'strip-ansi';
import {
startFakeOpenAIServer,
type FakeOpenAIServer,
} from '../fake-openai-server.js';
import { TestRig, type } from '../test-helper.js';

describe('Interactive protocol tag filtering', () => {
let fakeServer: FakeOpenAIServer | undefined;
let rig: TestRig;

beforeEach(() => {
rig = new TestRig();
});

afterEach(async () => {
await fakeServer?.close();
fakeServer = undefined;
await rig.cleanup();
});

it.skipIf(process.platform === 'win32')(
'retries hidden protocol text across SSE disconnect and completed streams',
async () => {
fakeServer = await startFakeOpenAIServer(({ requestIndex }) => {
if (requestIndex === 0) {
return {
contentChunks: [
'<analysis>hidden before disconnect',
'</analysis><summary>WRONG_FIRST_ATTEMPT</summary>',
],
disconnectAfterContentChunks: 1,
};
}

if (requestIndex === 1) {
return {
contentChunks: ['<analysis>hidden completed attempt</analysis>'],
};
}

return {
contentChunks: [
'<analysis>retry scratchpad that must not render',
'</analysis><summary>VISIBLE_TMUX_RETRY_SUMMARY_DONE',
'</summary>',
],
usage: {
prompt_tokens: 20,
completion_tokens: 8,
total_tokens: 28,
},
};
});

await rig.setup('interactive-protocol-tag-filtering-http-retry', {
settings: {
security: {
auth: {
selectedType: 'openai',
},
},
},
});

const { ptyProcess, promise } = rig.runInteractive(
'--auth-type',
'openai',
'--openai-api-key',
'fake-key',
'--openai-base-url',
fakeServer.baseUrl,
'--model',
'fake-model',
);

try {
const isReady = await rig.poll(
() => /YOLO (模式|mode)/i.test(stripAnsi(rig._interactiveOutput)),
30000,
200,
);
expect(isReady, 'CLI did not start up in interactive mode').toBe(true);

await type(ptyProcess, 'Return the deterministic retry response.');
await type(ptyProcess, '\r');

const sawVisibleSummary = await rig.waitForText(
'VISIBLE_TMUX_RETRY_SUMMARY_DONE',
15000,
);
expect(
sawVisibleSummary,
'Expected visible summary marker after HTTP retries',
).toBe(true);
expect(fakeServer.requests).toHaveLength(3);
expect(fakeServer.requests[1]!.body).toEqual(
fakeServer.requests[0]!.body,
);
expect(fakeServer.requests[2]!.body).toEqual(
fakeServer.requests[0]!.body,
);

const renderedOutput = stripAnsi(rig._interactiveOutput);
expect(renderedOutput).toContain('VISIBLE_TMUX_RETRY_SUMMARY_DONE');
expect(renderedOutput).not.toContain('<analysis>');
expect(renderedOutput).not.toContain('</analysis>');
expect(renderedOutput).not.toContain('<summary>');
expect(renderedOutput).not.toContain('</summary>');
expect(renderedOutput).not.toContain('retry scratchpad');
expect(renderedOutput).not.toContain('hidden before disconnect');
expect(renderedOutput).not.toContain('hidden completed attempt');
expect(renderedOutput).not.toContain('WRONG_FIRST_ATTEMPT');
} finally {
ptyProcess.kill();
await promise;
}
},
);
});
138 changes: 2 additions & 136 deletions packages/core/src/agents/subagent-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,141 +5,7 @@
*/

import { AgentTerminateMode } from './runtime/agent-types.js';

function isClosingAnalysisTag(tag: string): boolean {
return tag.startsWith('</');
}

function isSelfClosingTag(tag: string): boolean {
return tag.endsWith('/>');
}

function findAnalysisCloseEnd(text: string, from: number): number {
const tagPattern = /<\/?analysis\b[^>]*>/gi;
let depth = 1;
tagPattern.lastIndex = from;

while (true) {
const match = tagPattern.exec(text);
if (!match) {
return -1;
}

const tag = match[0].toLowerCase();
if (isClosingAnalysisTag(tag)) {
depth -= 1;
if (depth === 0) {
return match.index + match[0].length;
}
} else if (!isSelfClosingTag(tag)) {
depth += 1;
}
}
}

function stripClosedAnalysisBlocks(text: string): string {
const tagPattern = /<\/?analysis\b[^>]*>/gi;
let result = '';
let index = 0;

while (true) {
tagPattern.lastIndex = index;
const match = tagPattern.exec(text);
if (!match) {
return result + text.slice(index);
}

const tag = match[0].toLowerCase();
const start = match.index;
const end = start + match[0].length;

if (isClosingAnalysisTag(tag)) {
result += text.slice(index, end);
index = end;
continue;
}

if (isSelfClosingTag(tag)) {
result += text.slice(index, start) + ' ';
index = end;
continue;
}

const closeEnd = findAnalysisCloseEnd(text, end);
if (closeEnd === -1) {
result += text.slice(index, end);
index = end;
continue;
}

result += text.slice(index, start) + ' ';
index = closeEnd;
}
}

function stripVisibleTags(text: string): string {
return stripClosedAnalysisBlocks(text)
.replace(/<\/?summary\b[^>]*>/gi, ' ')
.replace(/ {2,}/g, ' ');
}

function stripAnalysisOutsideSummary(text: string): string {
const tagPattern = /<\/?(?:analysis|summary)\b[^>]*>/gi;
let result = '';
let index = 0;
let summaryDepth = 0;

while (true) {
tagPattern.lastIndex = index;
const match = tagPattern.exec(text);
if (!match) {
return result + text.slice(index);
}

const tag = match[0].toLowerCase();
const start = match.index;
const end = start + match[0].length;

if (summaryDepth > 0) {
result += text.slice(index, end);
if (tag.startsWith('<summary')) {
summaryDepth += 1;
} else if (tag.startsWith('</summary')) {
summaryDepth -= 1;
}
index = end;
continue;
}

if (tag.startsWith('<summary')) {
result += text.slice(index, end);
summaryDepth = 1;
index = end;
continue;
}

if (!tag.startsWith('<analysis')) {
result += text.slice(index, end);
index = end;
continue;
}

result += text.slice(index, start);
if (isSelfClosingTag(tag)) {
index = end;
continue;
}

const closeEnd = findAnalysisCloseEnd(text, end);
if (closeEnd !== -1) {
index = closeEnd;
} else {
const rest = text.slice(end);
const summaryIndex = rest.search(/<summary\b[^>]*>/i);
index = summaryIndex === -1 ? text.length : end + summaryIndex;
}
}
}
import { stripAnalysisSummaryProtocolTags } from '../utils/protocol-tag-sanitizer.js';

/** Keeps successful subagent scratchpad tags out of the parent model context. */
export function toModelVisibleSubagentResult(
Expand All @@ -150,5 +16,5 @@ export function toModelVisibleSubagentResult(
return text;
}

return stripVisibleTags(stripAnalysisOutsideSummary(text).trim()).trim();
return stripAnalysisSummaryProtocolTags(text);
}
Loading
Loading