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
80 changes: 51 additions & 29 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1305,13 +1305,11 @@ export function apply(ctx: PluginContext): void {
const target = sessionModelTargetOf(ctx) ?? globalModelTarget(modelInfo.services);
refreshModelInfo(register.base, target);
const headers: Record<string, string> = { "x-bili-plugin": "dsh", "x-bili-plugin-conversation": sid };
// #3248: declare the divert intent for the request that was just
// converted by beforeSend (sequential dispatch — the flag is set by
// the conversion and consumed here, exactly once).
if (incMarkerArmed) {
headers[INCREMENTAL_TURN_MARKER_HEADER] = "1";
incMarkerArmed = false;
}
// #3370: the incremental-turn marker is no longer stamped here — a
// closure-level armed flag shared by every session raced concurrent
// requests across the awaited beforeSend → headersFor boundary. The
// marker now rides init.headers from the conversion itself (see
// maybeIncrementalNative), atomic with the body swap.
if (target !== undefined) {
const cached = modelInfo.byKey.get(modelInfoKey(target.provider, target.model));
if (cached !== undefined) {
Expand All @@ -1335,19 +1333,22 @@ export function apply(ctx: PluginContext): void {
await maybeIncrementalNative(url, init);
};

// #3248 incremental turns (#3248 marker divert, reference wiring for the
// native fetch lanes; opencode/omp-native follow the same recipe):
// #3248 incremental turns (#3370 hardened): reference wiring for the
// native fetch lanes; opencode/omp-native follow the same recipe:
// beforeSend — parse the anthropic body, plan (probe-gated), replace
// init.body with the delta payload, arm the marker flag
// + stash the ack context keyed by URL;
// headersFor — stamp the marker on the armed request;
// init.body with the delta payload and stamp the marker
// header on the SAME init (atomic: omp pattern) + stash
// the ack context keyed by URL;
// onResponse — read x-bili-applied-seq and record the ack (or fall
// back to full resend on 409/503).
// The lane shape-tests the body, so a marker'd but unconverted request
// (probe raced off, dead conversation, non-string body) rides the normal
// pipeline — behavior equals the pre-incremental world.
let incMarkerArmed = false;
const incPendingAcks = new Map<string, { sid: string; messages: IncrementalWireMessage[]; system: unknown; base: string }>();
// The old arms-a-shared-flag/consume-it-in-headersFor split raced when two
// sessions crossed the awaited hook boundary: the flag could be consumed
// by the WRONG request, shipping a delta body WITHOUT the marker — the
// proxy forwarded it verbatim and the model answered with no history
// (#3370). init.headers is merged by withHeaders and survives headersFor's
// own stamping, so the marker and the body can never part ways again.
const INC_PENDING_TTL_MS = 30_000; // matches omp-native / opencode-native
const incPendingAcks = new Map<string, { sid: string; messages: IncrementalWireMessage[]; system: unknown; base: string; at: number }>();
async function maybeIncrementalNative(url: string, init: RequestInit | undefined): Promise<void> {
if (!register.toolsReady || init === undefined) return;
const attr = attributionOf(ctx);
Expand All @@ -1373,26 +1374,36 @@ export function apply(ctx: PluginContext): void {
request: parsed as Record<string, unknown>,
});
if (plan.mode === "skip") return;
// #3370 single-flight (omp/opencode pattern): while a marker'd request
// for this URL is still awaiting its ack, later requests ride the
// normal pipeline — their acks could otherwise collide with the
// pending entry keyed by the same shared model endpoint URL.
const inflight = incPendingAcks.get(url);
if (inflight !== undefined && Date.now() - inflight.at < INC_PENDING_TTL_MS) return;
init.body = (plan as { body: string }).body;
incMarkerArmed = true;
incPendingAcks.set(url, { sid, messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, base });
const headers = new Headers(init.headers);
headers.set(INCREMENTAL_TURN_MARKER_HEADER, "1");
init.headers = headers;
incPendingAcks.set(url, { sid, messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, base, at: Date.now() });
}
state.onResponse = (url, response) => {
const pending = incPendingAcks.get(url);
if (pending === undefined) return;
if (response.status === 200) {
const seq = response.headers.get("x-bili-applied-seq");
if (seq !== null && /^\d+$/.test(seq)) {
incPendingAcks.delete(url);
noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system);
return;
}
// 200 without an ack header = a non-incremental response on this
// URL (single-flight skipped a concurrent request) — NOT ours; keep
// the pending entry for the marker'd response still in flight.
const seq = response.headers.get("x-bili-applied-seq");
if (response.status === 200 && (seq === null || !/^\d+$/.test(seq))) {
// 200 without an ack header CANNOT be the marker'd response — the
// lane stamps x-bili-applied-seq on every diverted response that
// reaches the forward (incremental-lane.ts writeHead wrap). It is
// a concurrent full-resend response on the shared model-endpoint
// URL: keep the pending entry for the marker'd response still in
// flight, or its ack is lost and the next turn 409s into a resync.
return;
}
incPendingAcks.delete(url);
if (response.status === 200) {
noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system);
return;
}
if (response.status === 409) {
// #3369: one drift re-seeds next turn; only a rejection streak
// permanently falls back to full resend.
Expand Down Expand Up @@ -1569,6 +1580,17 @@ export function _stateTakeoverGateForTest(): ((url: string, body?: { bytes: numb
return state.takeoverGate;
}

/** Test hook (#3370): expose the beforeSend/onResponse hooks so suites can
* drive the incremental conversion + ack seams directly (the fetch patch is
* not installed under NODE_TEST_CONTEXT). */
export function _stateBeforeSendForTest(): ((url: string, init: RequestInit | undefined) => Promise<void>) | undefined {
return state.beforeSend;
}

export function _stateOnResponseForTest(): ((url: string, response: Response) => void) | undefined {
return state.onResponse;
}

/** Test hook (#1268): expose the armed toolsReady gate so a suite can prove
* it resolves on the first registration attempt (success or failure). */
export function _stateToolsReadyForTest(): Promise<unknown> | undefined {
Expand Down
20 changes: 10 additions & 10 deletions src/agent/omp-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ state.beforeSend = ompIncrementalBeforeSend;
export function ompIncrementalOnResponse(url: string, response: Response): void {
const pending = incPendingOmp.get(url);
if (pending === undefined) return;
const seq = response.headers.get("x-bili-applied-seq");
if (response.status === 200 && (seq === null || !Number.isFinite(Number(seq)))) {
// 200 without an ack header CANNOT be the marker'd response — the
// lane stamps x-bili-applied-seq on every diverted response that
// reaches the forward (incremental-lane.ts writeHead wrap). It is a
// concurrent full-resend response on the shared model-endpoint URL:
// keep the pending entry for the marker'd response still in flight.
return;
}
incPendingOmp.delete(url);
try {
const seq = response.headers.get("x-bili-applied-seq");
if (response.status === 200) {
if (seq === null || !Number.isFinite(Number(seq))) {
// 200 without a valid ack header = a non-incremental response
// on this URL (single-flight skipped a concurrent request) —
// NOT ours; keep the pending entry so the marker'd response
// still in flight can record its ack.
return;
}
incPendingOmp.delete(url);
noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system);
return;
}
incPendingOmp.delete(url);
if (response.status === 409) {
// #3369: reset (re-seed next turn) unless the rejection circuit
// trips — one drift must not permanently kill the conversation.
Expand Down
20 changes: 10 additions & 10 deletions src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,21 +887,21 @@ export function __resetOpencodeIncrementalForTests(): void {
export function opencodeIncrementalOnResponse(url: string, response: Response): void {
const pending = incPendingOpencode.get(url);
if (pending === undefined) return; // non-incremental responses just clear nothing
const seq = response.headers.get("x-bili-applied-seq");
if (response.status === 200 && (seq === null || !Number.isFinite(Number(seq)))) {
// 200 without an ack header CANNOT be the marker'd response — the
// lane stamps x-bili-applied-seq on every diverted response that
// reaches the forward (incremental-lane.ts writeHead wrap). It is a
// concurrent full-resend response on the shared model-endpoint URL:
// keep the pending entry for the marker'd response still in flight.
return;
}
incPendingOpencode.delete(url);
try {
const seq = response.headers.get("x-bili-applied-seq");
if (response.status === 200) {
if (seq === null || !Number.isFinite(Number(seq))) {
// 200 without a valid ack header = a non-incremental response
// on this URL (single-flight skipped a concurrent request) —
// NOT ours; keep the pending entry so the marker'd response
// still in flight can record its ack.
return;
}
incPendingOpencode.delete(url);
noteIncrementalAck(pending.sid, Number(seq), pending.messages, pending.system);
return;
}
incPendingOpencode.delete(url);
if (response.status === 409) {
// Lane rejected the plan (seq mismatch): #3369 — reset (re-seed
// next turn) unless the rejection circuit trips; a single drift
Expand Down
45 changes: 19 additions & 26 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { asciiHeaderValue, detectProxyBase, destinationRoutedThroughProxy, fetch
import { createForkAdopter } from "./fork-adopt.js";
import { logHostEvent } from "./host-log.js";
import { plainHttpTrusted } from "../private-host.js";
import { allowPrivateHttpProviders, guardrailBashTimeout, guardrailMaxOutputBytes, guardrailRepetition } from "../knobs.js";
import { allowPrivateHttpProviders } from "../knobs.js";

export type Ctx = {
sessionManager?: { getSessionId?: () => string; getHeader?: () => unknown; getBranch?: () => unknown } | undefined;
Expand Down Expand Up @@ -743,14 +743,7 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
const agent = agentName(agentOverride);
const state: RegisterState = { retryIntervalMs: opts?.retryIntervalMs ?? RETRY_INTERVAL_MS };
// #3066: tool-output guardrails (output cap / bash timeout / repetition loop-break); host-independent, wired for pi + omp.
// #3331: resolve through knobs (env > config file > default) — bash-timeout
// default 60s (#3066 owner intent, anti-hang), 0 disables; cap and
// repetition stay on by default.
wireToolGuardrails(pi, {
bashTimeout: guardrailBashTimeout(),
maxBytes: guardrailMaxOutputBytes(),
repetition: guardrailRepetition(),
});
wireToolGuardrails(pi);
// #2336: one successful agent-providers report per plugin instance;
// retried on later session_starts while the proxy base is unknown
// (native mode resolves it asynchronously).
Expand Down Expand Up @@ -1315,13 +1308,12 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
});
// #3248 incremental turns: the ack channel. after_provider_response
// fires with status + headers BEFORE the stream is consumed, so the
// x-bili-applied-seq header is readable here. 200 + header → record
// the ack (the server seq is the only sinceSeq authority); 200 without
// a header → not the marker'd turn — keep the pending entry for the
// marker'd response still in flight; 409 → reset (re-seed next turn)
// unless the rejection circuit trips (#3369); 503 → drop the probe
// cache so the next turn re-probes (rollout flag or sidecar liveness
// changed on the lane).
// x-bili-applied-seq header is readable here. 200 → record the ack
// (the server seq is the only sinceSeq authority); 409 → sticky
// per-conversation fallback (seed always resyncs, so a delta 409 means
// genuine drift pi's own retry loop cannot repair); 503 → drop the
// probe cache so the next turn re-probes (rollout flag or sidecar
// liveness changed on the lane).
pi.on("after_provider_response", ((event: unknown, ctx: Ctx) => {
try {
const sid = sessionIdOf(ctx);
Expand All @@ -1330,20 +1322,21 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
if (pending === undefined) return;
const status = (event as { status?: unknown }).status;
const headers = (event as { headers?: Record<string, unknown> }).headers;
const seq = headers?.["x-bili-applied-seq"];
if (status === 200 && !(typeof seq === "string" && /^\d+$/.test(seq))) {
// 200 without an ack header CANNOT be the marker'd response —
// the lane stamps x-bili-applied-seq on every diverted
// response that reaches the forward (incremental-lane.ts
// writeHead wrap). It is a concurrent full-resend response
// for this conversation: keep the pending entry for the
// marker'd response still in flight.
return;
}
pendingIncrementalAcks.delete(sid);
if (status === 200) {
const seq = headers?.["x-bili-applied-seq"];
if (typeof seq !== "string" || !/^\d+$/.test(seq)) {
// No ack header = this response is not the marker'd
// turn (concurrent full-resend answered first) — keep
// the pending entry so the marker'd response still in
// flight can record its ack; state stays untouched.
return;
}
pendingIncrementalAcks.delete(sid);
noteIncrementalAck(sid, Number(seq), pending.messages, pending.system);
return;
}
pendingIncrementalAcks.delete(sid);
if (status === 409) {
// #3369: one 409 resets the conversation state — the next
// turn re-seeds with resync:true (never 409s) instead of
Expand Down
Loading
Loading