Skip to content

Commit a5d7686

Browse files
committed
fix(qa): recognize Code Mode write results
1 parent a46424a commit a5d7686

2 files changed

Lines changed: 211 additions & 2 deletions

File tree

‎extensions/qa-lab/src/providers/mock-openai/mock-openai-assistant-text.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,52 @@ function readCompletedImageGenerationMediaPath(prompt: string): string | undefin
6767

6868
export const QA_COMPACTION_RETRY_FINAL_MARKER = "Protocol note: replay unsafe after write.";
6969

70+
function isCompactionRetryWritePatch(value: unknown): boolean {
71+
if (typeof value !== "string") {
72+
return false;
73+
}
74+
const lines = value.split(/\r?\n/);
75+
for (let index = 0; index < lines.length - 1; index += 1) {
76+
if (
77+
lines[index] !== "--- compaction-retry-summary.txt" ||
78+
lines[index + 1] !== "+++ compaction-retry-summary.txt"
79+
) {
80+
continue;
81+
}
82+
let sectionEnd = lines.length;
83+
for (let candidate = index + 2; candidate < lines.length - 1; candidate += 1) {
84+
if (lines[candidate]?.startsWith("--- ") && lines[candidate + 1]?.startsWith("+++ ")) {
85+
sectionEnd = candidate;
86+
break;
87+
}
88+
}
89+
if (lines.slice(index + 2, sectionEnd).includes("+Replay safety: unsafe after write.")) {
90+
return true;
91+
}
92+
}
93+
return false;
94+
}
95+
7096
export function isCanonicalCompactionRetryWriteResult(toolOutput: string): boolean {
71-
return /^Successfully wrote \d+ bytes to compaction-retry-summary\.txt\.?$/i.test(
72-
toolOutput.trim(),
97+
if (
98+
/^Successfully wrote \d+ bytes to compaction-retry-summary\.txt\.?$/i.test(toolOutput.trim())
99+
) {
100+
return true;
101+
}
102+
const parsed = parseToolOutputJson(toolOutput);
103+
if (!parsed || parsed.status !== "completed" || parsed.replaySafe !== false) {
104+
return false;
105+
}
106+
const value = parsed.value;
107+
if (!value || typeof value !== "object" || Array.isArray(value)) {
108+
return false;
109+
}
110+
const result = value as Record<string, unknown>;
111+
return (
112+
result.changed === true &&
113+
result.created === true &&
114+
result.firstChangedLine === 1 &&
115+
isCompactionRetryWritePatch(result.patch)
73116
);
74117
}
75118

‎extensions/qa-lab/src/providers/mock-openai/server.test.ts‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ const QA_EMPTY_RESPONSE_RETRY_INSTRUCTION =
3131
"The previous attempt did not produce a user-visible answer. Continue from the current state and produce the visible answer now. Do not restart from scratch.";
3232
const QA_SETTLED_TOOL_TERMINAL_CONTINUATION_INSTRUCTION =
3333
"The previous assistant turn completed its tool calls but did not produce a user-visible answer. Continue from the current transcript and produce the final user-visible answer now. Do not repeat completed tool calls or restart from scratch.";
34+
const QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT = {
35+
status: "completed",
36+
value: {
37+
changed: true,
38+
created: true,
39+
diff: "+1 Replay safety: unsafe after write.",
40+
patch: [
41+
"--- compaction-retry-summary.txt",
42+
"+++ compaction-retry-summary.txt",
43+
"@@ -0,0 +1,1 @@",
44+
"+Replay safety: unsafe after write.",
45+
"",
46+
].join("\n"),
47+
firstChangedLine: 1,
48+
},
49+
output: [],
50+
replaySafe: false,
51+
telemetry: {
52+
catalogSize: 32,
53+
sources: { openclaw: 32, mcp: 0, client: 0 },
54+
counterScope: "qaFixtureScope01",
55+
searchCount: 0,
56+
describeCount: 0,
57+
callCount: 1,
58+
},
59+
} as const;
3460

3561
afterEach(async () => {
3662
while (cleanups.length > 0) {
@@ -2267,6 +2293,146 @@ describe("qa mock openai server", () => {
22672293
expect(outputText(await finalReply.json())).toBe("Protocol note: replay unsafe after write.");
22682294
});
22692295

2296+
it("finishes Anthropic Code Mode compaction retry after the structured write result", async () => {
2297+
const server = await startMockServer();
2298+
const callId = "toolu_compaction_retry_write";
2299+
const response = await postJson(server, "/v1/messages", {
2300+
model: "claude-opus-4-8",
2301+
max_tokens: 256,
2302+
messages: [
2303+
makeAnthropicUserText(
2304+
"Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit.",
2305+
),
2306+
{
2307+
role: "assistant",
2308+
content: [{ type: "tool_use", id: callId, name: "exec", input: {} }],
2309+
},
2310+
makeAnthropicToolResult(callId, JSON.stringify(QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT)),
2311+
],
2312+
});
2313+
2314+
expect(response.status).toBe(200);
2315+
const body = requireRecord(await response.json(), "Anthropic compaction retry response");
2316+
expect(body.stop_reason).toBe("end_turn");
2317+
expect(requireArray(body.content, "Anthropic response content")).toContainEqual({
2318+
type: "text",
2319+
text: "Protocol note: replay unsafe after write.",
2320+
});
2321+
});
2322+
2323+
it("accepts semantic Code Mode write evidence across diff formatting changes", async () => {
2324+
const server = await startMockServer();
2325+
const callId = "toolu_compaction_retry_write_semantic";
2326+
const response = await postJson(server, "/v1/messages", {
2327+
model: "claude-opus-4-8",
2328+
max_tokens: 256,
2329+
messages: [
2330+
makeAnthropicUserText(
2331+
"Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit.",
2332+
),
2333+
{
2334+
role: "assistant",
2335+
content: [{ type: "tool_use", id: callId, name: "exec", input: {} }],
2336+
},
2337+
makeAnthropicToolResult(
2338+
callId,
2339+
JSON.stringify({
2340+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT,
2341+
value: {
2342+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT.value,
2343+
diff: "display formatting may change independently",
2344+
patch: [
2345+
"--- compaction-retry-summary.txt",
2346+
"+++ compaction-retry-summary.txt",
2347+
"@@ -0,0 +1 @@",
2348+
"+Replay safety: unsafe after write.",
2349+
].join("\r\n"),
2350+
},
2351+
}),
2352+
),
2353+
],
2354+
});
2355+
2356+
expect(response.status).toBe(200);
2357+
const body = requireRecord(await response.json(), "Anthropic compaction retry response");
2358+
expect(requireArray(body.content, "Anthropic response content")).toContainEqual({
2359+
type: "text",
2360+
text: "Protocol note: replay unsafe after write.",
2361+
});
2362+
});
2363+
2364+
it.each([
2365+
{
2366+
label: "failed status",
2367+
result: { ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT, status: "failed" },
2368+
},
2369+
{
2370+
label: "replay-safe result",
2371+
result: { ...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT, replaySafe: true },
2372+
},
2373+
{
2374+
label: "unchanged result",
2375+
result: {
2376+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT,
2377+
value: { changed: false },
2378+
},
2379+
},
2380+
{
2381+
label: "wrong target patch",
2382+
result: {
2383+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT,
2384+
value: {
2385+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT.value,
2386+
patch:
2387+
"--- other.txt\n+++ other.txt\n@@ -0,0 +1,1 @@\n+Replay safety: unsafe after write.\n",
2388+
},
2389+
},
2390+
},
2391+
{
2392+
label: "content added under another target",
2393+
result: {
2394+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT,
2395+
value: {
2396+
...QA_COMPACTION_RETRY_CODE_MODE_WRITE_RESULT.value,
2397+
patch: [
2398+
"--- compaction-retry-summary.txt",
2399+
"+++ compaction-retry-summary.txt",
2400+
"@@ -0,0 +1 @@",
2401+
"+Unrelated content.",
2402+
"--- other.txt",
2403+
"+++ other.txt",
2404+
"@@ -0,0 +1 @@",
2405+
"+Replay safety: unsafe after write.",
2406+
].join("\n"),
2407+
},
2408+
},
2409+
},
2410+
])("rejects Anthropic Code Mode compaction retry result with $label", async ({ result }) => {
2411+
const server = await startMockServer();
2412+
const callId = "toolu_compaction_retry_invalid";
2413+
const response = await postJson(server, "/v1/messages", {
2414+
model: "claude-opus-4-8",
2415+
max_tokens: 256,
2416+
messages: [
2417+
makeAnthropicUserText(
2418+
"Compaction retry mutating tool check: read COMPACTION_RETRY_CONTEXT.md, then create compaction-retry-summary.txt and keep replay safety explicit.",
2419+
),
2420+
{
2421+
role: "assistant",
2422+
content: [{ type: "tool_use", id: callId, name: "exec", input: {} }],
2423+
},
2424+
makeAnthropicToolResult(callId, JSON.stringify(result)),
2425+
],
2426+
});
2427+
2428+
expect(response.status).toBe(200);
2429+
const body = requireRecord(await response.json(), "Anthropic compaction retry response");
2430+
expect(requireArray(body.content, "Anthropic response content")).not.toContainEqual({
2431+
type: "text",
2432+
text: "Protocol note: replay unsafe after write.",
2433+
});
2434+
});
2435+
22702436
it("does not finish a compacted retry when failure text quotes the write success marker", async () => {
22712437
const server = await startMockServer();
22722438

0 commit comments

Comments
 (0)