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
17 changes: 12 additions & 5 deletions src/agent/native-attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ export type NativeLaneEventHandler = (level: "info" | "error", message: string)
* as additional optional hooks when that lane migrates — never silently
* unified. */
export interface NativeLaneSpec {
/** Lane tag for log lines (`bili-native(<lane>): …`). */
/** Lane tag for defaults and diagnostics; see `tag` for the exact log
* prefix. */
lane: string;
/** Exact `bili-native…` prefix for the lifecycle's host-event lines.
* Defaults to `bili-native(<lane>)`. pi/omp use the default;
* opencode historically emits `bili-native-opencode…` (hyphen, no
* parens) and a pure transposition keeps that byte-for-byte. */
tag?: string;
/** The lane's module-level intercept state singleton. */
state: NativeInterceptState;
/** Test seam, read at CALL time so `_setSpawnForTest` after arming still
Expand Down Expand Up @@ -95,6 +101,7 @@ export function _resetArmedLaneForTest(): void {
* came up. */
export async function verifyAttachAndRecover(spec: NativeLaneSpec, attachOrigin: string): Promise<string | undefined> {
const { state } = spec;
const tag = spec.tag ?? `bili-native(${spec.lane})`;
// #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
Expand All @@ -115,19 +122,19 @@ export async function verifyAttachAndRecover(spec: NativeLaneSpec, attachOrigin:
const back = await waitForProxyVersion(pinned);
if (back !== undefined) {
spec.publish(back);
spec.onEvent("info", `bili-native(${spec.lane}): attach target ${pinned} is healthy again — attached, no second instance spawned`);
spec.onEvent("info", `${tag}: attach target ${pinned} is healthy again — attached, no second instance spawned`);
return back;
}
spec.onEvent(
"error",
explicitAttachOrigin !== undefined
? `bili-native(${spec.lane}): 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(${spec.lane}): 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.`,
? `${tag}: 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.`
: `${tag}: 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;
}
spec.onEvent("error", `bili-native(${spec.lane}): attach target ${attachOrigin} is not healthy — falling back to a spawned proxy`);
spec.onEvent("error", `${tag}: attach target ${attachOrigin} is not healthy — falling back to a spawned proxy`);
spec.unpublish();
state.attach = false;
if (spec.clearOrigin !== undefined) spec.clearOrigin();
Expand Down
101 changes: 30 additions & 71 deletions src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ import { ACP_TOOLS_OPENAI } from "../compress-tool.js";
import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST, unwrapUpstream, wrapUpstream } from "../launcher.js";
import { createAcpCommandHooks, showAcpText } from "./opencode-acp-command.js";
import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js";
import { createLiveOriginResolver, installNativeFetchIntercept, isModelApiUrl, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, replaceRequestPayload, replaceRequestTarget, routedBiliModelUrl, setDeclaredModelEndpoints, type LiveOriginResolverDeps, type NativeInterceptState, type UnavailableReason } from "./native-intercept.js";
import { createLiveOriginResolver, installNativeFetchIntercept, isModelApiUrl, noteRoutedOrigin, readyOrigin, replaceRequestPayload, replaceRequestTarget, routedBiliModelUrl, setDeclaredModelEndpoints, type LiveOriginResolverDeps, type NativeInterceptState, type UnavailableReason } from "./native-intercept.js";
import { loadDeclaredModelEndpoints } from "../model-endpoints.js";
import { armNativeLane, verifyAttachAndRecover as verifyAttachAndRecoverShared, type NativeLaneSpec } from "./native-attach.js";
import { clearIncrementalProbe, createPrepareDirectSend, incrementalCacheRetouchEnabled, 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 { createOpencodeV2Setup, type V2HttpRequestEvent, type V2State } from "./opencode-v2.js";
import { alignedMsgIdsForWire, harvestOpencodeMsgIds } from "./opencode-msgids.js";
import { asciiHeaderValue, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, waitForProxyVersion, type ForwardedToolResult } from "./shared.js";
import { asciiHeaderValue, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, type ForwardedToolResult } from "./shared.js";
import { callLegacyAcpConfig, isLegacyAcpSession, loadLegacyAcp, type LegacyAcpModule } from "./opencode-legacy.js";
import { logHostEvent } from "./host-log.js";
import { mediaBytesOfWireBody } from "./media-bytes.js";
Expand Down Expand Up @@ -316,76 +317,30 @@ 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 (instance discovery may find another
* healthy one first), re-arming respawn as a pure spawn for subsequent
* deaths. Resolves to the origin the client should use — the (recovered)
* attach origin, the fallback origin, or undefined when nothing came up. */
/** #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
* opencode-specific surface: tag keeps the historical hyphenated
* `bili-native-opencode` log prefix byte-for-byte. */
const laneSpec: NativeLaneSpec = {
lane: "opencode",
tag: "bili-native-opencode",
state,
spawnForTest: () => _spawnForTest,
bootstrap,
publish: (origin) => {
state.origin = origin;
process.env.BILLION_CONTEXT_PROXY = origin;
},
unpublish: () => {
delete process.env.BILLION_CONTEXT_PROXY;
},
onEvent: (level, message) => logHostEvent(level, "[native-client]", message),
};

/** Doc comment moved to native-attach.ts#verifyAttachAndRecover. */
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.
state.origin = home;
process.env.BILLION_CONTEXT_PROXY = home;
return home;
}
const pinned = explicitAttachOrigin ?? state.routedOrigin ?? (await observeRoutedOrigin(state));
if (pinned !== undefined) {
const back = await waitForProxyVersion(pinned);
if (back !== undefined) {
state.origin = back;
process.env.BILLION_CONTEXT_PROXY = back;
logHostEvent("info", "[native-client]", `bili-native-opencode: attach target ${pinned} is healthy again — attached, no second instance spawned`);
return back;
}
logHostEvent(
"error",
"[native-client]",
explicitAttachOrigin !== undefined
? `bili-native-opencode: 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-opencode: 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-opencode: attach target ${attachOrigin} is not healthy — falling back to a spawned proxy`);
delete process.env.BILLION_CONTEXT_PROXY;
state.attach = false;
state.origin = undefined;
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) {
state.origin = o;
process.env.BILLION_CONTEXT_PROXY = o;
}
return o;
});
state.ready = landed;
return landed;
return verifyAttachAndRecoverShared(laneSpec, attachOrigin);
}

const plan = planNativeOpencode(process.env, loadConfigFile().opencode?.inline);
Expand All @@ -396,6 +351,10 @@ const plan = planNativeOpencode(process.env, loadConfigFile().opencode?.inline);
* spawn mode stays test-guarded because bootstrap forks a real proxy. */
export function armNativeOpencode(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). Covers attach/spawn/inline alike.
armNativeLane("opencode");
markNativeHost(process.env, "opencode");
// #1295: declared custom-wire endpoints — same config source as the proxy;
// takes effect on the next host start (config is read once at arm time).
Expand Down
7 changes: 7 additions & 0 deletions tests/issue3304-incremental-direct-send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import { installNativeFetchIntercept, withRestoredBody, _resetForTest, type NativeInterceptState } from "../src/agent/native-intercept.ts";
import { createPrepareDirectSend, incrementalEnvelopeConversationId, INCREMENTAL_TURN_ID_HEADER, rememberOriginalBody, originalBodyForTurn, __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts";
import { _resetArmedLaneForTest } from "../src/agent/native-attach.ts";

const ORIGINAL_BODY = JSON.stringify({ model: "claude-test", max_tokens: 1024, messages: [{ role: "user", content: "hello" }] });
const DELTA_BODY = JSON.stringify({ conversationId: "conv-1", sinceSeq: 3, path: "/v1/messages", request: { model: "claude-test" }, messages: [{ role: "assistant", content: "hi" }] });
Expand Down Expand Up @@ -383,6 +384,12 @@ import { createServer } from "node:http";
import type { AddressInfo } from "node:net";

const { maybeIncrementalOpencode } = await import("../src/agent/opencode-native.ts");
// #3304 deliberately drives TWO lanes in one test process (cross-lane
// matrix). The one-lane-per-process guard (#5241 phase 1) encodes the
// PRODUCTION contract — each lane rides its own host process — so a test
// that consciously co-loads lanes resets the guard between them instead of
// spawning per-lane subprocesses like the phase-0 stakes do.
_resetArmedLaneForTest();
const { ompIncrementalBeforeSend, _setOmpOriginForTest, __resetOmpIncrementalForTests } = await import("../src/agent/omp-native.ts");

function fakeLane(enabled: boolean): Promise<{ base: string; close: () => Promise<void> }> {
Expand Down
Loading