Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/dedupe-adjacent-output-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** [#5688] OpenRouter's Anthropic Messages surface rejects a history that
* contains two ADJACENT `{role:"system", content:[], output_config:{...}}`
* effort directives ("Two adjacent output_config directives are not
* allowed"). Clients that emit per-turn effort directives (pi's
* insertThinkingLevelMessages: one before every leveled assistant message +
* one trailing) are healthy on their own — every directive is separated by
* its assistant message. But bili's fold reconstruction can remove the
* message BETWEEN two directives (reasoning hygiene, CCR excerpting, range
* trimming, empty-message pruning), leaving them adjacent and the session
* deterministically 400s on every retry.
*
* Repair at egress: collapse runs of adjacent effort directives down to the
* LAST one (later effort wins — the same semantics the trailing active
* directive relies on). Detection is deliberately narrow: a system message
* whose content is an EMPTY array (or absent / empty string) carrying an
* output_config object — exactly the directive shape, nothing else. Ordinary
* system messages (text content) are never touched. Returns the input array
* unchanged when nothing was adjacent.
*
* Shared by every anthropic-wire egress surface that renders from a core
* view fold reconstruction can leave adjacent in: the steady prepare egress
* (prepare-anthropic), the nudge-decide side body (same file), and the loop
* round-2 rebuild (loop/adapter-anthropic buildRequest). */
export function dedupeAdjacentOutputConfig<T>(messages: T[]): T[] {
const isDirective = (m: unknown): boolean => {
const msg = m as { role?: unknown; content?: unknown; output_config?: unknown };
if (msg?.role !== "system") return false;
if (msg.output_config === null || typeof msg.output_config !== "object") return false;
if (Array.isArray(msg.content) && msg.content.length > 0) return false;
if (typeof msg.content === "string" && msg.content.length > 0) return false;
return true;
};
const out: T[] = [];
let removed = 0;
for (const m of messages) {
if (out.length > 0 && isDirective(m) && isDirective(out[out.length - 1])) {
out[out.length - 1] = m; // later effort wins
removed++;
continue;
}
out.push(m);
}
return removed > 0 ? out : messages;
}
6 changes: 5 additions & 1 deletion src/loop/adapter-anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CoreMessage } from "acp-kernel";
import { coreToAnthropic, type AnthropicRequestBody } from "acp-kernel/wire";
import { dedupeAdjacentOutputConfig } from "../dedupe-adjacent-output-config.js";
import { appendSystemText, declaredInclusiveFromUsage } from "../util.js";
import { stampAnthropicSystemCacheControl } from "./cache-control.js";
import { buildVisibilityMarker } from "./core.js";
Expand Down Expand Up @@ -307,7 +308,10 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
// steady path does (`anthropicCacheMarks ?? cacheControls`) — a
// client-managed session has no bili marks, and dropping its
// breakpoint leaves the post-fold prefix unwritten to cache.
const messages = coreToAnthropic(coreMessages, cacheMarks ?? clientCacheControls);
// [#5688] round-2 re-renders from the core view fold reconstruction
// can leave adjacent effort directives in — collapse like the
// steady egress or the rebuild 400s where the main request passed.
const messages = dedupeAdjacentOutputConfig(coreToAnthropic(coreMessages, cacheMarks ?? clientCacheControls));
// #1876: same append-not-merge rebuild as the steady path's
// injectSystem — both must emit byte-identical system (F2 seam).
const system = originalSystem !== undefined ? appendSystemText(systemPrompt, originalSystem) : systemPrompt;
Expand Down
5 changes: 5 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,11 @@ export function warnReasoningPairs(
log("warn", `[${sessionId}] reasoning-pair-violated: ${violators.length} assistant tool-call message(s) lack reasoning_content while ${withRc} carry it — strict-echo upstreams (DeepSeek thinking mode) will reject the request; first seen: ${violationNamed(tracked.fresh, tracked.ongoing, tracked.maxRepeat)} (#684/#762/#2864)`);
}

// [#5688] shared egress repair for adjacent per-turn effort directives —
// implementation moved to its own leaf module so loop/ can import it without
// a server.ts import cycle; re-exported here for existing consumers.
export { dedupeAdjacentOutputConfig } from "./dedupe-adjacent-output-config.js";

/** [#684] Anthropic-wire twin of the openai sentinel, narrowed by [#1327]:
* warn only when bili itself lost thinking — a tool_use block that rode an
* inbound assistant message WITH a thinking block now rides an outbound
Expand Down
18 changes: 14 additions & 4 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforg
import type { CompressReasoningConfig } from "../reasoning-drop.js";
import type { ProxyOptions } from "../config.js";
import { pluginAgentHeader } from "../plugin.js";
import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, makeFoldPointReasoningDroppable, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js";
import { applyReasoningDrop, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, makeFoldPointReasoningDroppable, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js";
import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js";
import { excludeDeadRefRanges } from "../acp-status.js";
import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js";
Expand Down Expand Up @@ -530,7 +530,9 @@ export async function prepareAnthropic(
}
} else if (useDecide) {
const ranges = turn.nudge.compressibleRanges ?? [];
const sideBody: Record<string, unknown> = { ...parsed, messages: [...rebuiltMessages, { role: "user", content: buildDecisionPrompt(ranges) }], system: systemOut, tools: toolsOut, stream: false, max_tokens: decide.maxTokens };
// [#5688] the side call shares the main request's upstream —
// un-deduped rebuiltMessages would 400 the decision too.
const sideBody: Record<string, unknown> = { ...parsed, messages: [...dedupeAdjacentOutputConfig(rebuiltMessages), { role: "user", content: buildDecisionPrompt(ranges) }], system: systemOut, tools: toolsOut, stream: false, max_tokens: decide.maxTokens };
delete sideBody.prompt_cache_key;
stripDecisionReasoningFields("anthropic", sideBody);
const outcome = await runNudgeDecision({ req, opts, protocol: "anthropic", sideBody, session, log });
Expand Down Expand Up @@ -597,8 +599,16 @@ export async function prepareAnthropic(
snapshotMessages(session, originalMessages);
markDirty(session);

const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages, system: systemOut, tools: toolsOut };
warnAnthropicThinkingPairs(parsed.messages, rebuiltMessages, log, sessionId);
// [#5688] fold reconstruction can leave two per-turn effort directives
// ({role:"system", content:[], output_config}) adjacent — OpenRouter rejects
// that shape with a hard 400 on every retry. Collapse to the latter before
// egress; the helper returns the input array unchanged on the clean path.
const dedupedMessages = dedupeAdjacentOutputConfig(rebuiltMessages);
if (dedupedMessages !== rebuiltMessages) {
log("warn", `[${sessionId}] adjacent-output-config: collapsed duplicate effort directive(s) after fold reconstruction (#5688)`);
}
warnAnthropicThinkingPairs(parsed.messages, dedupedMessages, log, sessionId);
const rebuilt: AnthropicRequestBody = { ...parsed, messages: dedupedMessages, system: systemOut, tools: toolsOut };
// prompt_cache_key is the omp plugin's session id stamped for the proxy's
// identity chain (#268), not part of the Anthropic Messages API — strip it
// so the real upstream never sees a field it doesn't know.
Expand Down
53 changes: 53 additions & 0 deletions tests/issue5688-adjacent-output-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// #5688: fold reconstruction must not leave two adjacent output_config effort
// directives in the outbound history — OpenRouter rejects that shape with a
// hard 400 ("Two adjacent output_config directives are not allowed") and the
// session dies on every retry until the directives are collapsed.
import { test } from "node:test";
import assert from "node:assert/strict";
import { dedupeAdjacentOutputConfig } from "../src/server.js";

type Msg = Record<string, any>;
const dir = (effort: string): Msg => ({ role: "system", content: [], output_config: { effort } });
const asst = (): Msg => ({ role: "assistant", content: [{ type: "text", text: "ok" }] });
const user = (): Msg => ({ role: "user", content: "q" });
const isDir = (m: Msg) => m.output_config !== undefined;

test("clean history returns the same array reference (fast path)", () => {
const msgs = [user(), dir("max"), asst(), user(), dir("low"), asst(), user()];
assert.equal(dedupeAdjacentOutputConfig(msgs), msgs);
});

test("collapses two adjacent directives, keeping the latter effort", () => {
const msgs = [user(), dir("max"), dir("low"), asst(), user()];
const out = dedupeAdjacentOutputConfig(msgs);
assert.equal(out.length, 4);
assert.deepEqual(out.filter(isDir), [dir("low")]);
});

test("collapses a run of three adjacent directives down to the last", () => {
const out = dedupeAdjacentOutputConfig([user(), dir("max"), dir("high"), dir("low"), asst(), user()]);
assert.deepEqual(out.filter(isDir), [dir("low")]);
});

test("text-bearing system messages are never treated as directives", () => {
const sysText: Msg = { role: "system", content: [{ type: "text", text: "note" }], output_config: { effort: "max" } };
const msgs = [user(), sysText, dir("low"), asst(), user()];
const out = dedupeAdjacentOutputConfig(msgs);
assert.equal(out.length, 5);
assert.ok(out.includes(sysText));
});

test("empty system messages without output_config are left alone", () => {
const msgs = [user(), { role: "system", content: [] }, { role: "system", content: [] }, asst(), user()];
assert.equal(dedupeAdjacentOutputConfig(msgs), msgs);
});

test("directive with absent content field is still detected", () => {
const out = dedupeAdjacentOutputConfig([user(), dir("low"), { role: "system", output_config: { effort: "max" } }, asst(), user()]);
assert.deepEqual(out.filter(isDir), [{ role: "system", output_config: { effort: "max" } }]);
});

test("non-adjacent directives are preserved (only adjacent runs collapse)", () => {
const out = dedupeAdjacentOutputConfig([user(), dir("max"), asst(), user(), dir("low"), asst(), user()]);
assert.equal(out.filter(isDir).length, 2);
});
Loading
Loading