Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
37 changes: 37 additions & 0 deletions TECHNICAL-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,43 @@ remove <client>` best-effort queries the live proxy (instance file →
`GET /__bili/sessions`, 2.5 s timeout, silent on failure) and prints a note
when sessions of that client were active within the last **10 minutes**.

## Degraded direct fallback — refuse, self-heal, announce (#4516, #3304, #2924, #4079)

When a native lane's proxy is unavailable, model traffic falls back to direct
sends (`directSendOrRefuse`, `src/agent/native-intercept.ts`). That path is a
degraded mode with three contracts, all shared by every lane (the gateless
lanes pi/omp/opencode-V1 had none of them before #4516 — a 1.06M-token raw
history rode straight to the provider):

- **Size arm** — before ANY degraded send, the body is priced with the same
doom math as dsh's gate (`bodyProvablyOverWindow`): stage 1 is the raw
byte floor vs the window, stage 2 strips structural media bytes (#4111
direction: media-heavy bodies whose core TEXT fits are NOT refused). The
bar never assumes a window below `MIN_ASSUMED_WINDOW_TOKENS` (1M, #4079 —
declared windows can be wrong-small); bodies in that band are left to
preflight. Missing data (no `x-bili-plugin-context-window` header,
unreadable body) fails OPEN to the direct send — a local guard must never
false-block on its own ignorance. Verdict: 422
`x-bili-gate: oversize-refusal-fallback`, retryable, wire-shaped per
upstream dialect, message names floor/core/window and the unavailability
reason (`bootstrap-failed` vs `dead` — the latter only after the lane held
an origin and lost it).
- **Self-heal** — a failed bootstrap no longer means
direct-for-process-lifetime. Each request that finds no live origin fires
the owner's `respawn` (singleFlight — concurrent requests share ONE
attempt) under a 15 s cooldown, then holds out for the in-flight attempt
under `state.spawnWaitMs ?? readyTimeoutMs ?? 15_000`: the pi spawn lane
sets `SPAWN_BUDGET_MS + 10 s` so a slow cold start (#4515) is waited out
instead of refused at the generic cap. Recovery needs no host restart.
- **Announce** — every TRANSITION into/out of the degraded state logs one
line to bili.log (proxy recovered / goes direct / refused), not one line
per request; `state.lastUnavailableReason` carries WHY into the refusal
wording so the user can tell "still starting" from "died".

The opencode V2 resolver lanes (`createLiveOriginResolver`) keep their own
probe-driven retry; this contract covers the plain intercept path they do
not. Attribution reporting into the panel remains dsh-only (see #4516).

## Unified ACP tag-echo remediation — the four invariants (#2023/#2066/#2190/#2248)

One PR folds the three remediation legs together (refs-run residue #2023,
Expand Down
39 changes: 15 additions & 24 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { adminRelayEnabled, resolveResignSettings } from "../config.js";
import { dshContextTabEnabled, dshContextTabLabel } from "../knobs.js";
import { MIN_ASSUMED_WINDOW_TOKENS } from "../util.js";
import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js";
import { installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, routedBiliModelUrl, type GateVerdict, type NativeInterceptState } from "./native-intercept.js";
import { bodyProvablyOverWindow as sharedBodyProvablyOverWindow, installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, routedBiliModelUrl, type GateVerdict, type NativeInterceptState } from "./native-intercept.js";
import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportAttributionHealth, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js";
import { createForkAdopter, sideShapedBody } from "./fork-adopt.js";
import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js";
Expand Down Expand Up @@ -593,33 +593,24 @@ function effectiveContextWindow(ctx: PluginContext): number | undefined {
return modelInfo.byKey.get(modelInfoKey(target.provider, target.model))?.contextWindow;
}

/** #4111: the shared two-stage doom predicate for the gate's oversize arms
* (the `threw` arm and the unattributed arm). Stage 1 prices the RAW body at
* bytes/4 — exactly the old math, cheap — and disarms when that is not even
* provisionally over the #4079-floored window. Stage 2 runs ONLY on the
* would-refuse path: re-price on media-excluded core bytes and disarm when
* the core text alone still fits (the #4111 false refusal was media mass
* billed as text). Returns the refusal's numbers when the body is PROVABLY
* over (core text alone cannot fit), undefined otherwise. No body, unreadable
* body, or unresolvable window → undefined — the arms keep #1117 direct
* semantics and can never false-refuse from missing data. */
// Gate face of the shared doom math (#4111/#4516): keeps the gate's
// init-body signature for its call sites and tests, decoding init.body into
// the raw string the shared form (native-intercept.ts) prices with the
// media-aware walk (media-bytes.ts).
function rawBodyStringOf(init: RequestInit | undefined): string | undefined {
const body: unknown = init?.body;
if (typeof body === "string") return body;
if (body instanceof Uint8Array) return new TextDecoder().decode(body);
if (body instanceof ArrayBuffer) return new TextDecoder().decode(new Uint8Array(body));
return undefined;
}

export function bodyProvablyOverWindow(
body: { bytes: number } | undefined,
init: RequestInit | undefined,
declaredWindow: number | undefined,
): { coreBytes: number; mediaBytes: number; floorTokens: number; window: number; declaredWindow: number } | undefined {
if (body === undefined || declaredWindow === undefined) return undefined;
const window = Math.max(declaredWindow, MIN_ASSUMED_WINDOW_TOKENS);
if (Math.ceil(body.bytes / 4) <= window) return undefined;
const rawBody: unknown = init?.body;
let raw: string | undefined;
if (typeof rawBody === "string") raw = rawBody;
else if (rawBody instanceof Uint8Array) raw = new TextDecoder().decode(rawBody);
else if (rawBody instanceof ArrayBuffer) raw = new TextDecoder().decode(new Uint8Array(rawBody));
const mediaBytes = raw === undefined ? 0 : mediaBytesOfWireBody(raw);
const coreBytes = Math.max(body.bytes - mediaBytes, 0);
if (Math.ceil(coreBytes / 4) <= window) return undefined;
return { coreBytes, mediaBytes, floorTokens: Math.ceil(coreBytes / 4), window, declaredWindow };
): ReturnType<typeof sharedBodyProvablyOverWindow> {
return sharedBodyProvablyOverWindow(body?.bytes, rawBodyStringOf(init), declaredWindow);
}

function refreshModelInfo(origin: string | undefined, target?: { provider: string; model: string }): void {
Expand Down
Loading
Loading