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
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ Environment variables take precedence over the config file. They are useful for
| `BILI_UPSTREAM_TIMEOUT_MS` | Idle budget (ms) for upstream requests: time-to-first-byte and time between body chunks (default `720000` = 12 min). A healthy stream that keeps producing chunks is never cut mid-flight; a silent one is. The same value drives the underlying HTTP client's transport timeouts, so this single knob bounds long local-model prefills end-to-end (#551). |
| `ACP_SESSION_HEADER` | Conversation-id header name (default `x-acp-session`). |
| `ACP_REASONING_KEEP` | Responses API only: set `none` to drop all reasoning items. Default routes reasoning through the compression pipeline so it is hidden automatically once its turn is summarized (prevents the unbounded accumulation that broke Codex's prompt-cache prefix). |
| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `<acp>` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. |
| `ACP_LOG_FILE` | Log file path (default XDG state path; `off` disables the file, keeps stderr). Auto-rotates at 10 MB. |
| `ACP_DUMP_SSE` | Directory to dump raw SSE frames for debugging. |
| `BILI_LOG_MASK_HOSTS` | Set `0` to turn OFF host masking in proxy logs (#897): non-public target hosts (private relays, internal domains) are logged verbatim instead of `<private-host>`. Default is ON (#255 — logs get pasted into public issues); credential-header masking is independent and always on. Real target hosts are always available without touching this flag: `GET /__bili/stats` → `blindTunnels`, `GET /__bili/health` (both loopback-only), and the `acp_status` output. |
Expand Down
4 changes: 2 additions & 2 deletions src/loop/adapter-anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CoreMessage } from "acp-kernel";
import { coreToAnthropic, extractSystem, buildSystem, type AnthropicRequestBody } from "acp-kernel/wire";
import { buildVisibilityMarker } from "../compress-loop.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripAcpTags } from "./tag-echo-filter.js";
import { degenerateTurnWarning } from "../degenerate-turn.js";
import { log as loggerLog } from "../logger.js";
import type {
Expand Down Expand Up @@ -323,7 +323,7 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
kind: "tool_call",
name: tb.name,
callId: tb.id,
arguments: tb.json,
arguments: stripAcpTags(tb.json),
} as ParsedStreamEvent;
} else if (thinkingIndexes.delete(upstreamIndex)) {
// Seal the current thinking segment so interleaved thinking
Expand Down
64 changes: 59 additions & 5 deletions src/loop/adapter-openai.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CoreMessage } from "acp-kernel";
import { coreToOpenai, injectOpenaiSystem } from "acp-kernel/wire";
import { buildVisibilityMarker } from "../compress-loop.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripAcpTags } from "./tag-echo-filter.js";
import { degenerateTurnWarning } from "../degenerate-turn.js";
import { log as loggerLog } from "../logger.js";
import { hardenOpenaiAssistantContent, systemToUser } from "../util.js";
Expand Down Expand Up @@ -253,7 +253,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
kind: "tool_call",
name: tc.name,
callId: tc.id,
arguments: tc.arguments,
arguments: stripAcpTags(tc.arguments),
} as ParsedStreamEvent;
}
}
Expand Down Expand Up @@ -282,16 +282,70 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
kind: "tool_call",
name: tc.name,
callId: tc.id,
arguments: tc.arguments,
arguments: stripAcpTags(tc.arguments),
passthrough: true,
} as ParsedStreamEvent;
}
}
// #933: a render tag echoed into a real tool call's arguments
// may span fragment boundaries, so per-fragment stripping is not
// enough. Everything is buffered by settle time: rebuild each
// tainted index as one chunk carrying the clean full arguments,
// suppress its remaining fragments, leave clean calls verbatim.
const taintedIdx = new Set<number>();
for (const [idx, tc] of pending) {
if (realIndexes.has(idx) && stripAcpTags(tc.arguments) !== tc.arguments) taintedIdx.add(idx);
}
const rebuiltTainted = new Set<number>();
const taintReplay = (obj: Record<string, unknown>): Buffer | null => {
const chs = obj["choices"];
if (!Array.isArray(chs)) return null;
let changed = false;
const outChoices = chs.map((c) => {
if (!c || typeof c !== "object") return c;
const co = c as Record<string, unknown>;
const d = co["delta"];
if (!d || typeof d !== "object") return c;
const dd = d as Record<string, unknown>;
const tcs = dd["tool_calls"];
if (!Array.isArray(tcs)) return c;
let chChanged = false;
const outTcs: unknown[] = [];
for (const tco of tcs) {
if (tco && typeof tco === "object" && typeof (tco as Record<string, unknown>)["index"] === "number") {
const idx = (tco as Record<string, unknown>)["index"] as number;
if (taintedIdx.has(idx)) {
if (rebuiltTainted.has(idx)) continue;
rebuiltTainted.add(idx);
const pt = pending.get(idx);
outTcs.push({ index: idx, id: pt?.id ?? "", type: "function", function: { name: pt?.name ?? "", arguments: stripAcpTags(pt?.arguments ?? "") } });
chChanged = true;
continue;
}
}
outTcs.push(tco);
}
if (!chChanged) return c;
changed = true;
return { ...co, delta: { ...dd, tool_calls: outTcs } };
});
if (!changed) return null;
return Buffer.from("data: " + JSON.stringify({ ...obj, choices: outChoices }) + "\n\n", "utf8");
};
for (const { json, parsed } of rawToolChunks) {
const filtered = filterRealToolFragments(parsed, realIndexes);
const obj = filtered === "keep" ? parsed : filtered;
if (obj === null) continue;
if (taintedIdx.size > 0) {
const rebuilt = taintReplay(obj);
if (rebuilt) {
yield { kind: "meta", chunk: rebuilt } as ParsedStreamEvent;
continue;
}
}
if (filtered === "keep") {
yield { kind: "meta", chunk: Buffer.from("data: " + json + "\n\n", "utf8") } as ParsedStreamEvent;
} else if (filtered !== null) {
} else {
yield { kind: "meta", chunk: Buffer.from("data: " + JSON.stringify(filtered) + "\n\n", "utf8") } as ParsedStreamEvent;
}
}
Expand All @@ -302,7 +356,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
kind: "tool_call",
name: tc.name,
callId: tc.id,
arguments: tc.arguments,
arguments: stripAcpTags(tc.arguments),
} as ParsedStreamEvent;
}
}
Expand Down
41 changes: 27 additions & 14 deletions src/loop/adapter-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { coreToResponsesWithToolImages as coreToResponses, patchResponsesInputWi
import { buildVisibilityMarker } from "../compress-loop.js";
import { hoistTrappedToolItems } from "../tool-pair-order.js";
import { hashId } from "../util.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, containsMarkerLineText, containsRenderTagText, ACP_NAME_ALT } from "./tag-echo-filter.js";
import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, stripAcpTags, containsMarkerLineText, containsRenderTagText, ACP_NAME_ALT } from "./tag-echo-filter.js";
import { degenerateTurnWarning } from "../degenerate-turn.js";
import { log as loggerLog } from "../logger.js";
import { ACP_TEXT_OPEN, ACP_TEXT_CLOSE, ACP_STATUS_OPEN, ACP_STATUS_CLOSE, ACP_SEARCH_OPEN, ACP_SEARCH_CLOSE, ACP_DECOMPRESS_OPEN, ACP_DECOMPRESS_CLOSE, COMPRESS_TOOL_NAME, PROXY_TOOL_NAMES } from "../compress-tool.js";
Expand Down Expand Up @@ -215,6 +215,7 @@ function buildMessageItemSequence(itemId: string, outputIndex: number, text: str
}

function buildFunctionCallEvents(fc: ToolCallEmit, itemId: string, outputIndex: number): Buffer {
const args = stripAcpTags(fc.arguments);
return Buffer.from(
[
`event: response.output_item.added\ndata: ${JSON.stringify({
Expand All @@ -225,17 +226,17 @@ function buildFunctionCallEvents(fc: ToolCallEmit, itemId: string, outputIndex:
`event: response.function_call_arguments.delta\ndata: ${JSON.stringify({
type: "response.function_call_arguments.delta",
item_id: itemId,
delta: fc.arguments,
delta: args,
})}\n\n`,
`event: response.function_call_arguments.done\ndata: ${JSON.stringify({
type: "response.function_call_arguments.done",
item_id: itemId,
arguments: fc.arguments,
arguments: args,
})}\n\n`,
`event: response.output_item.done\ndata: ${JSON.stringify({
type: "response.output_item.done",
output_index: outputIndex,
item: { type: "function_call", id: itemId, call_id: fc.callId, name: fc.name, arguments: fc.arguments },
item: { type: "function_call", id: itemId, call_id: fc.callId, name: fc.name, arguments: args },
})}\n\n`,
].join(""),
"utf8",
Expand Down Expand Up @@ -380,17 +381,20 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
arguments: "",
});
} else {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
}
} else if (item?.type === "custom_tool_call") {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
} else if (item?.type === "message" && round > 1 && !suppressTextLifecycle) {
const origId = typeof item.id === "string" ? item.id : "";
const mapped = { id: `msg-proxy-${round}-${hashId(origId || String(outputIndex))}`, index: outputIndex++ };
if (origId) remapped.set(origId, mapped);
yield { kind: "meta", chunk: rewriteItemEvent(type, obj, mapped), firstRoundOnly: false } as ParsedStreamEvent;
} else if (item?.type !== "message" || !suppressTextLifecycle) {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (
type === "response.content_part.added" ||
Expand Down Expand Up @@ -428,13 +432,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
const delta = typeof obj.delta === "string" ? obj.delta : "";
const fc = pending.get(itemId);
if (fc) fc.arguments += delta;
else yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
else {
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
}
} else if (type === "response.function_call_arguments.done") {
const itemId = typeof obj.item_id === "string" ? obj.item_id : "";
const args = typeof obj.arguments === "string" ? obj.arguments : "";
const fc = pending.get(itemId);
if (fc && args) fc.arguments = args;
else yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
else {
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
}
} else if (type === "response.output_item.done") {
const item = obj.item as Record<string, unknown> | undefined;
if (item?.type === "function_call") {
Expand All @@ -448,21 +458,23 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
kind: "tool_call",
name: fc.name,
callId: fc.callId,
arguments: fc.arguments,
arguments: stripAcpTags(fc.arguments),
} as ParsedStreamEvent;
} else {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
toolCallsEmitted++;
yield {
kind: "tool_call",
name: typeof item.name === "string" ? item.name : "",
callId: typeof item.call_id === "string" ? item.call_id : "",
arguments: typeof item.arguments === "string" ? item.arguments : "",
arguments: typeof item.arguments === "string" ? stripAcpTags(item.arguments) : "",
passthrough: true,
} as ParsedStreamEvent;
}
} else if (item?.type === "custom_tool_call") {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent;
} else if (item?.type === "message") {
const origId = typeof item.id === "string" ? item.id : "";
const mapped = remapped.get(origId);
Expand All @@ -474,7 +486,8 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (item?.type !== "message" || !suppressTextLifecycle) {
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent;
const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (type === "response.completed") {
yield* flushFilter();
Expand Down
Loading
Loading