Skip to content
Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

### Fixes

- **Plugin passthrough re-issues a turn whose only visible output was a stripped tag echo (#870, PR #871)**: the plugin-mode direct pipe strips model-emitted render tags, and when that echo was a turn's ENTIRE visible content the emptied chunk was suppressed, so the host received a turn with no text and no tool call and aborted it. Releasing the echo instead is not an option (the #463 contract keeps a pure-echo turn swallowed, and #674/#673 forbid leaking the echo as content), so the pipe now re-issues: on a clean terminal (`stop` / `end_turn` / `stop_sequence`) with zero visible text, zero tool calls and no error finish reason, `src/plugin.ts` refetches the same request with a continuation nudge (`src/degenerate-retry.ts`: `DEGENERATE_RETRY_NUDGE`, `injectContinuationNudge`, `makeContinuationRefetch`) and relays the retry inside the original framing — Anthropic keeps a single `message_start`/`message_stop` with the retry's block indices offset, and the OpenAI terminal is the `choices[].finish_reason` chunk rather than `[DONE]`, so the first attempt's terminal never reaches the client twice. One retry per turn; if the retry is also empty the original turn passes through with the existing `[degenerate-turn]` warning, and an unissuable retry presents the original terminal with valid JSON. `appendTrailingUserText` moved to `src/wire-body.ts` so the fake-completion hint and the retry share one body builder. Tests: `tests/plugin-empty-turn-retry.test.ts` (8, driving the pipe with a fake response and a fake refetch).
The same gate now covers the unclosed case: when a render tag is dropped and its interior
survives as the turn's only visible text, the host receives orphan markup with no tool call and
stalls, so that interior is counted as markup rather than as visible output and the turn is re-issued. When the retry degenerates too, the client now receives an in-band stream error rather
than a silent empty turn: an empty turn is indistinguishable from a model that produced nothing,
and the host reports neither, so the session reads as idle while it is dead.

- **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry).
- **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `<state>/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half.
- **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn.
Expand Down
100 changes: 100 additions & 0 deletions src/degenerate-retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { fetchWithTimeout } from "./fetch-util.js";
import { hasCompactionTrigger } from "./codex-compact.js";
import { proxyDispatcher } from "./upstream-proxy.js";
import type { WireProtocol } from "./util.js";
import { appendTrailingUserText } from "./wire-body.js";

// #732/#821 ran this retry for the compress loop, which owns the request body
// AND the response framing. The plugin pipe (plugin mode, and proxy-mode chat
// SSE that skipped compress injection) owns neither: it forwards the agent's
// own body and passes the upstream stream through byte-identically, so a turn
// that ends with nothing visible — the render-tag echo case, where the tag
// filter empties the only text block — reaches the host as an empty completed
// turn and the host aborts it. The pipe can still re-issue the SAME body
// (plus this nudge) because it holds the upstream URL and the forwarded
// headers, and it can splice the retry's content into the open client stream.
// Bounded to one attempt per turn; the body mutation never touches session
// state, so the nudge is neither persisted nor replayed on the client's next
// request.
export const DEGENERATE_RETRY_NUDGE =
"[billion-context] Your previous response ended with no visible text and no tool call. Continue now: take your next concrete action.";

/** The retry body: the forwarded body with the continuation nudge appended as a
* trailing user turn. Null when the body cannot carry one. */
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer): string | null {
return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE);
}

export interface ContinuationRetryOpts {
protocol: WireProtocol;
/** The body that was forwarded upstream (the agent's own, in plugin mode). */
body: string | Buffer;
upstreamUrl: string;
reqHeaders: Record<string, string>;
proxyUrl?: string;
dispatcher?: object;
signal: AbortSignal;
log: (level: string, msg: string) => void;
/** Log prefix, normally the session id. */
label: string;
}

/** Build the re-request used when a turn ends with no visible output. Returns a
* function for the stream pipe: it yields the retry's response body, or null
* when the retry cannot be issued (unbuildable body, upstream error, client
* already gone) — the caller then passes the original empty turn through
* unchanged. */
export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Promise<ReadableStream<Uint8Array> | null> {
return async () => {
if (opts.signal.aborted) return null;
// A body whose final input item is a compaction trigger must never be
// re-issued: appending the nudge after it breaks the wire shape (#283:
// the trigger stays the last input item), and the trigger's terminal is
// decided by the compaction flow, not by an empty-turn retry. Parsed
// here, at retry time, so healthy turns pay nothing.
if (opts.protocol === "responses") {
try {
const parsed = JSON.parse(typeof opts.body === "string" ? opts.body : opts.body.toString("utf8")) as Record<string, unknown>;
if (hasCompactionTrigger(parsed["input"])) {
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request ends on a compaction trigger`);
return null;
}
} catch {
/* unparseable body: injectContinuationNudge below reports and skips */
}
}
const retryBody = injectContinuationNudge(opts.protocol, opts.body);
if (retryBody === null) {
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`);
return null;
}
try {
const r = await fetchWithTimeout(
opts.upstreamUrl,
{
method: "POST",
headers: opts.reqHeaders,
body: retryBody,
...(opts.dispatcher ? { dispatcher: opts.dispatcher } : opts.proxyUrl ? { dispatcher: proxyDispatcher(opts.proxyUrl) } : {}),
},
undefined,
opts.signal,
);
if (!r.response.ok || !r.response.body) {
r.clearTimer();
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry rejected (HTTP ${r.response.status}); passing the empty turn through`);
return null;
}
const body = r.response.body as ReadableStream<Uint8Array>;
// The retry stream is spliced into a response the caller already
// owns, so its own idle timer is dropped — a stalled retry then ends
// with the client abort instead of an upstream watchdog (the
// streamed re-request in reasoning-guard.ts does the same).
r.clearTimer();
return body;
} catch (e) {
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`);
return null;
}
};
}
43 changes: 5 additions & 38 deletions src/fake-completion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { WireProtocol } from "./util.js";
import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js";
import { appendTrailingUserText } from "./wire-body.js";

// #371 (root-cause follow-up to #361): a small model can WRITE a tool call as
// plain text (echoing the tool-call XML template from the context) instead of
Expand Down Expand Up @@ -82,43 +83,9 @@ export function isFakeCompletion(protocol: WireProtocol, rawText: string): boole
return hasToolCallStructure(rawText) && !hasToolBlock(protocol, rawText);
}

// Appends the hint as a trailing user message, merged into the last user
// message when present (avoids back-to-back user turns on strict providers).
// Returns the hinted JSON, or null if the body is unparseable (skip the retry).
/** Append the fake-completion hint to the request body as a trailing user turn
* (per-wire shapes live in `appendTrailingUserText`). Returns the hinted JSON,
* or null if the body is unparseable, in which case the retry is skipped. */
export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null {
const raw = typeof body === "string" ? body : body.toString("utf8");
let obj: Record<string, unknown>;
try {
obj = JSON.parse(raw) as Record<string, unknown>;
} catch {
return null;
}
if (protocol === "responses") {
const input = obj.input;
if (!Array.isArray(input)) return null;
const arr = input as Record<string, unknown>[];
const last = arr[arr.length - 1];
if (last && typeof last === "object" && last.role === "user") {
const c = last.content;
if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`;
else if (Array.isArray(c)) last.content = [...c, { type: "input_text", text: FAKE_COMPLETION_HINT }];
else last.content = FAKE_COMPLETION_HINT;
} else {
arr.push({ role: "user", content: [{ type: "input_text", text: FAKE_COMPLETION_HINT }] });
}
return JSON.stringify(obj);
}
const messages = obj.messages;
if (!Array.isArray(messages)) return null;
const arr = messages as Record<string, unknown>[];
const last = arr[arr.length - 1];
if (last && typeof last === "object" && last.role === "user") {
const c = last.content;
if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`;
else if (Array.isArray(c)) last.content = [...c, { type: "text", text: FAKE_COMPLETION_HINT }];
else last.content = FAKE_COMPLETION_HINT;
} else {
arr.push({ role: "user", content: FAKE_COMPLETION_HINT });
}
return JSON.stringify(obj);
return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT);
}
7 changes: 1 addition & 6 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ import { dumpsDir } from "../paths.js";
import { isStrictReasoningEcho, normalizeStrictEchoBody } from "../strict-echo.js";
import { log as loggerLog } from "../logger.js";
import { promptInputTotal, type WireProtocol } from "../util.js";
import { DEGENERATE_RETRY_NUDGE } from "../degenerate-retry.js";

export const MAX_LOOP_ROUNDS = 10;

// #732: ephemeral continuation prompt appended ONLY to the degenerate-turn
// retry body — never committed to coreMessages/session state, so it is neither
// persisted nor replayed on the client's next (client-authored) request.
const DEGENERATE_RETRY_NUDGE =
"[billion-context] Your previous response ended with no visible text and no tool call. Continue now: take your next concrete action.";

function isLoopThinking(m: CoreMessage): boolean {
return m.contentType === "reasoning" && typeof m.id === "string" && m.id.startsWith("acp_loop_");
}
Expand Down
Loading
Loading