Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 12 additions & 5 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ function isReasoningRequiredError(e: unknown): boolean {
return e instanceof UpstreamHttpError && /must be passed back/i.test(e.body);
}

function proxyToolResultFailed(result: string): boolean {
// A successful compression receipt embeds the model-authored summary in later fingerprint
// lines. That summary may legitimately quote an earlier FAILED tool result; only the status
// line is authoritative for whether this tool call itself failed.
return (result.trimStart().split("\n", 1)[0] ?? "").includes("FAILED");
}

// #1453 client-facing labels for transport failures that end the turn in-band.
// Deliberately kind-level only: raw error text can embed endpoint hostnames/IPs,
// which must never reach the client stream (the full masked chain goes to the
Expand Down Expand Up @@ -1471,7 +1478,7 @@ export async function* runCompressLoop(
if (threadContinue) threadDelta.push(injection);
}
const anyCompressFailed = proxyResults.some(
(pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"),
(pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result),
);
if (!ctx.textProtocol && !anyCompressFailed) {
const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool);
Expand All @@ -1492,7 +1499,7 @@ export async function* runCompressLoop(
const absorbName = activeAbsorbToolName(ctx.session, ctx.config);
if (
ctx.refreshFolded &&
proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !pr.result.includes("FAILED"))
proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !proxyToolResultFailed(pr.result))
) {
try {
const refreshed = await ctx.refreshFolded(coreMessages);
Expand Down Expand Up @@ -1582,7 +1589,7 @@ export async function* runCompressLoop(
// carry server-invisible bytes upstream — exactly the seam #4186
// forbids.
const failedMutating = proxyResults.filter(
(pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"),
(pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result),
);

if (!probeWarned) {
Expand Down Expand Up @@ -1700,14 +1707,14 @@ export async function* runCompressLoop(
let committedTokens = 0;
let committedBlocks = 0;
for (const pr of proxyResults) {
if (pr.name === "compress" && !pr.result.includes("FAILED")) {
if (pr.name === "compress" && !proxyToolResultFailed(pr.result)) {
const saved = /~(\d[\d,]*) tokens saved/.exec(pr.result);
if (saved) committedTokens += Number(saved[1]!.replace(/,/g, ""));
const blocks = /(\d+) block\(s\)/.exec(pr.result);
if (blocks) committedBlocks += Number(blocks[1]);
}
}
const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !pr.result.includes("FAILED"));
const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !proxyToolResultFailed(pr.result));
const note = committedTokens > 0
? `compression committed (~${committedTokens.toLocaleString("en-US")} tokens saved${committedBlocks > 0 ? ` in ${committedBlocks} block(s)` : ""}); `
: absorbed ? "context work committed; " : "";
Expand Down
61 changes: 57 additions & 4 deletions tests/plugin-protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function textScript(): string[] {
];
}

function compressToolScript(): string[] {
const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary: "plugin e2e compress summary" });
function compressToolScript(summary = "plugin e2e compress summary with enough context about the folded range"): string[] {
const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary });
return [
anthropicSse("message_start", {
type: "message_start",
Expand Down Expand Up @@ -172,14 +172,15 @@ async function callPluginAnthropic(
h: Harness,
conversationId: string,
messages: AnthropicMessage[],
opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number } = {},
opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number; compressionProxy?: boolean; agent?: string } = {},
): Promise<{ raw: string; events: SseEvent[]; json: Record<string, unknown> | undefined }> {
const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, {
method: "POST",
headers: {
"content-type": "application/json",
"x-bili-plugin": "pi-plugin/0.0.1",
"x-bili-plugin": opts.agent ?? "pi-plugin/0.0.1",
"x-bili-plugin-conversation": conversationId,
...(opts.compressionProxy ? { "x-bili-plugin-compression": "proxy" } : {}),
...(opts.contextWindow !== undefined ? { "x-bili-plugin-context-window": String(opts.contextWindow) } : {}),
},
body: JSON.stringify({
Expand Down Expand Up @@ -848,6 +849,58 @@ test("plugin-reported context window becomes the nudge denominator and is visibl
}
});

function sizedPluginHistory(): AnthropicMessage[] {
const headFiller = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ".repeat(28);
const tailFiller = "enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate. ".repeat(28);
const history: AnthropicMessage[] = [];
for (let i = 1; i <= 2; i++) {
history.push({ role: "user", content: `turn-${i}-marker question: ${headFiller}` });
history.push({ role: "assistant", content: `turn-${i}-marker answer: ${headFiller}` });
}
for (let i = 3; i <= 5; i++) {
history.push({ role: "user", content: `turn-${i} padding question: ${tailFiller}` });
history.push({ role: "assistant", content: `turn-${i} padding answer: ${tailFiller}` });
}
return history;
}

test("plugin-bound proxy compression continues the tool loop instead of ending on the marker", async () => {
const h = await startHarness([compressToolScript(), textScript()], undefined, { visibilityMarkers: false });
try {
const { raw } = await callPluginAnthropic(h, "20261031_032204_cec004da", sizedPluginHistory(), {
agent: "hermes",
tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({
name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} },
})),
compressionProxy: true,
});

assert.equal(h.captured.length, 2, `proxy compression must re-request the model after executing compress; raw=${raw}`);
assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`);
} finally {
await h.close();
}
});

test("a successful compression summary containing FAILED still continues with markers hidden", async () => {
const summary = "summary records an earlier FAILED tool result while this compression itself succeeds";
const h = await startHarness([compressToolScript(summary), textScript()], undefined, { visibilityMarkers: false });
try {
const { raw } = await callPluginAnthropic(h, "plug-conv-failed-in-summary", sizedPluginHistory(), {
agent: "hermes",
tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({
name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} },
})),
compressionProxy: true,
});

assert.equal(h.captured.length, 2, `a success marker must not be classified by FAILED inside its summary; raw=${raw}`);
assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`);
} finally {
await h.close();
}
});

// Hermes defers plugin toolsets through tool_search, so a healthy plugin session sends requests
// whose tools array carries NONE of the ACP names. Those requests must still bind the plugin
// identity and budget (that is exactly what /acp reads) while compression falls back to the proxy —
Expand Down
Loading