From 872e45ef6e2001f701a906ea5fbc9166de728f33 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 30 Oct 2026 23:24:46 +0800 Subject: [PATCH] =?UTF-8?q?agent:=20shared=20attach/recovery=20lifecycle?= =?UTF-8?q?=20=E2=80=94=20migrate=20the=20opencode=20lane=20(#5241=20phase?= =?UTF-8?q?=201,=20PR-1c)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Third lane migration: opencode's verifyAttachAndRecover swaps its private copy for the shared native-attach.ts lifecycle via a NativeLaneSpec, as pi (#5244) and omp (#5246) did. Pure transposition — the phase-0 stake matrix passes untouched. opencode specifics preserved through spec hooks: the historical hyphenated `bili-native-opencode:` log prefix stays byte-for-byte via the new optional `tag` spec field (default remains `bili-native()` — pi/omp messages unchanged). armNativeOpencode now calls armNativeLane("opencode") — the one-lane-per-process contract covers attach/spawn/inline modes alike. issue3304's cross-lane matrix deliberately co-loads TWO lanes in one test process; it now resets the guard between the dynamic imports (_resetArmedLaneForTest) — the guard encodes the production contract, and a test that consciously co-loads lanes opts out explicitly instead of the old silent cross-pollution. --- src/agent/native-attach.ts | 17 ++- src/agent/opencode-native.ts | 101 ++++++------------ .../issue3304-incremental-direct-send.test.ts | 7 ++ 3 files changed, 49 insertions(+), 76 deletions(-) diff --git a/src/agent/native-attach.ts b/src/agent/native-attach.ts index 99ec0dc485..97d64778fc 100644 --- a/src/agent/native-attach.ts +++ b/src/agent/native-attach.ts @@ -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 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()`. 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 @@ -95,6 +101,7 @@ export function _resetArmedLaneForTest(): void { * came up. */ export async function verifyAttachAndRecover(spec: NativeLaneSpec, attachOrigin: string): Promise { 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 @@ -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(); diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 02e4bcb910..449cd9bad2 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -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"; @@ -316,76 +317,30 @@ export function _setSpawnForTest(fn?: () => Promise): 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 { - // #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); @@ -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). diff --git a/tests/issue3304-incremental-direct-send.test.ts b/tests/issue3304-incremental-direct-send.test.ts index 8fb07944ca..5182de0c30 100644 --- a/tests/issue3304-incremental-direct-send.test.ts +++ b/tests/issue3304-incremental-direct-send.test.ts @@ -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" }] }); @@ -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 }> {