Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/agent/native-attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface NativeLaneSpec {
publish: (origin: string) => void;
/** Drop the published origin (spawn fallback unfreezes the preset env). */
unpublish: () => void;
/** Fallback-branch state clear. Default (when omitted): the shared
* lifecycle writes `state.origin = undefined` directly. omp overrides
* this so its publishOrigin() test-pin guard (a suite-pinned probe base
* must survive a mid-suite attach verify / respawn landing — see
* _setOmpOriginForTest) stays in command of state.origin writes. */
clearOrigin?: () => void;
/** Lane event sink (logHostEvent / persistClientEvent). */
onEvent: NativeLaneEventHandler;
}
Expand Down Expand Up @@ -124,7 +130,8 @@ export async function verifyAttachAndRecover(spec: NativeLaneSpec, attachOrigin:
spec.onEvent("error", `bili-native(${spec.lane}): attach target ${attachOrigin} is not healthy — falling back to a spawned proxy`);
spec.unpublish();
state.attach = false;
state.origin = undefined;
if (spec.clearOrigin !== undefined) spec.clearOrigin();
else state.origin = undefined;
const start = singleFlight(spec.spawnForTest() ?? spec.bootstrap);
state.respawn = start;
// Publish whatever origin lands on BOTH the shared state and the env —
Expand Down
95 changes: 29 additions & 66 deletions src/agent/omp-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js";
import { createBiliPlugin } from "./pi.js";
import { applyOmpFirstEventTimeout, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js";
import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js";
import { installNativeFetchIntercept, noteRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js";
import { loadDeclaredModelEndpoints } from "../model-endpoints.js";
import { armNativeLane, verifyAttachAndRecover as verifyAttachAndRecoverShared, type NativeLaneSpec } from "./native-attach.js";
import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js";
import * as pfChannel from "./pf-channel.js";
import { fetchProxyVersion, waitForProxyVersion } from "./shared.js";
import { fetchProxyVersion } from "./shared.js";
import { logHostEvent } from "./host-log.js";

/** Decides whether the native bootstrap should run in this process. */
Expand Down Expand Up @@ -276,71 +277,29 @@ export function _setSpawnForTest(fn?: () => Promise<string | undefined>): void {
_spawnForTest = fn;
}

/** #1135/#1365: the attached origin can go stale — at startup (the owning
* launcher exited before this process started) or at runtime (it exits while
* this session still rides the shared proxy, #1130). Probe before trusting
* it: healthy → keep attaching (a transient blip costs nothing — the session
* never migrates). DEAD + routed-channel evidence (#1365: /bili/-baked model
* traffic was observed at some origin) → the session's context lives at THAT
* origin, so wait for the pinned target to come back (bounded by
* BILI_ATTACH_HEALTH_DEADLINE_MS) instead of spawning — a second instance
* would serve tools while the model channel stays pinned elsewhere and every
* bili tool call 404s against it (unrecoverable split); the env is preserved
* so the user's target stays declared. DEAD with no evidence after the grace
* window (BILI_ATTACH_EVIDENCE_GRACE_MS) → unfreeze the preset env and fall
* back to spawning our own proxy, re-arming respawn as a pure spawn. Resolves
* to the origin the client should use — the (recovered) attach origin, the
* fallback origin, or undefined when nothing came up. */
export async function verifyAttachAndRecover(attachOrigin: string): Promise<string | undefined> {
// #1365: routed evidence outranks the planned origin — probe where the
// model channel actually points, not where the env says it should. An
// EXPLICIT BILLION_CONTEXT_ATTACH outranks both: it names the plugin API
// endpoint directly, and a routed model origin may be a plain model-only
// path (e.g. an admin-gated port) that never exposes the plugin manifest.
const explicitAttachOrigin = nativeAttachOrigin(process.env);
const home = explicitAttachOrigin ?? state.routedOrigin ?? attachOrigin;
const version = await fetchProxyVersion(home).catch(() => undefined);
if (version !== undefined) {
// Restore the readyOrigin short-circuit — a runtime recovery clears
// state.origin before re-probing, and a transient blip must not leave
// it dangling.
publishOrigin(home);
return home;
}
const pinned = explicitAttachOrigin ?? state.routedOrigin ?? (await observeRoutedOrigin(state));
if (pinned !== undefined) {
const back = await waitForProxyVersion(pinned);
if (back !== undefined) {
publishOrigin(back);
logHostEvent("info", "[native-client]", `bili-native(omp): attach target ${pinned} is healthy again — attached, no second instance spawned`);
return back;
}
logHostEvent(
"error",
"[native-client]",
explicitAttachOrigin !== undefined
? `bili-native(omp): explicit attach target ${pinned} unreachable within the health deadline — refusing to spawn a second instance (your declared endpoint stays authoritative). Start your proxy at ${pinned} or unset BILLION_CONTEXT_ATTACH; bili keeps re-checking and self-heals when it comes back.`
: `bili-native(omp): attach target ${pinned} is down and this process's model channel is pinned to it — refusing to spawn a second instance (bili tools would 404 against the other one). Start your proxy at ${pinned} or unset BILLION_CONTEXT_PROXY; bili keeps re-checking and self-heals when it comes back.`,
);
state.origin = undefined;
return undefined;
}
logHostEvent("error", "[native-client]", `bili-native(omp): attach target ${attachOrigin} is not healthy — falling back to a spawned proxy`);
delete process.env.BILLION_CONTEXT_PROXY;
state.attach = false;
publishOrigin(undefined, false); // env already cleared above
/** #1135/#1365: shared since #5241 phase 1 — the lifecycle itself lives in
* native-attach.ts (one implementation for all lanes, behavior frozen by
* tests/contract-v2-stake-verify-attach.test.ts). This lane spec is the
* omp-specific surface: every origin write funnels through publishOrigin()
* so a test-pinned probe base (_setOmpOriginForTest) survives attach
* verify / respawn landing mid-suite; clearOrigin routes the fallback's
* state clear through the same guard. */
const laneSpec: NativeLaneSpec = {
lane: "omp",
state,
spawnForTest: () => _spawnForTest,
bootstrap,
publish: (origin) => publishOrigin(origin),
unpublish: () => {
delete process.env.BILLION_CONTEXT_PROXY;
},
clearOrigin: () => publishOrigin(undefined, false),
onEvent: (level, message) => logHostEvent(level, "[native-client]", message),
};

const start = singleFlight(_spawnForTest ?? bootstrap);
state.respawn = start;
// Publish whatever origin lands on BOTH the shared state and the env —
// env readers (compaction reporter, /acp status) must never disagree with
// what the route resolves, whoever produced the origin.
const landed = start().then((o) => {
if (o !== undefined) publishOrigin(o);
return o;
});
state.ready = landed;
return landed;
/** Doc comment moved to native-attach.ts#verifyAttachAndRecover. */
export async function verifyAttachAndRecover(attachOrigin: string): Promise<string | undefined> {
return verifyAttachAndRecoverShared(laneSpec, attachOrigin);
}

const plan = planNativeOmp(process.env);
Expand All @@ -351,6 +310,10 @@ const plan = planNativeOmp(process.env);
* spawn mode stays test-guarded because bootstrap forks a real proxy. */
export function armNativeOmp(p: typeof plan): void {
if (p.mode === "off") return;
// #5241 phase 1: one native lane per process — a second, different lane
// would overwrite this lane's BILLION_CONTEXT_PROXY and fetch intercept
// (see native-attach.ts).
armNativeLane("omp");
markNativeHost(process.env, "omp");
if (p.mode === "attach") {
state.attach = true;
Expand Down
Loading