From 6e72bbb829a8736ef92f5fe298b69780459ddf29 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 1/7] refactor: export fixture-facing types PluginContext/ZodLike/CostRow, re-export CompressSettings (#1991 item 3) --- src/agent/dsh-native.ts | 2 +- src/agent/opencode-native.ts | 2 +- src/compress-settings.ts | 2 ++ src/registry.ts | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 274f3fbb2b..d23cb04ae0 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -76,7 +76,7 @@ type ToolDefinition = { type CommandOutcome = { kind: "success" | "error"; text: string }; -type PluginContext = { +export type PluginContext = { tools: { register: (definition: ToolDefinition) => unknown }; commands: { register: (command: { name: string; description: string; handler: (invocation?: CommandInvocation) => Promise }) => unknown }; agents: { currentInitiator?: () => AgentLike | undefined }; diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 25766c0578..de6c71dd32 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -299,7 +299,7 @@ export function _resetNativeStateForTest(): void { // ———— OpenCode 1.x native surface (V1 `.server()`) ———————————————————— -type ZodLike = typeof import("zod"); +export type ZodLike = typeof import("zod"); interface V1ToolContext { sessionID: string; diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 3c47ab2885..fdb852f558 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -4,6 +4,8 @@ import { findRoute, type CompressSettings, type ProviderRoutes } from "./config. import { configDir } from "./paths.js"; import { log as loggerLog } from "./logger.js"; +export type { CompressSettings } from "./config.js"; + /** Host-side policy default for `compress.stripImagesKeepRecent` (#617): how * many of the most recent messages keep their image payloads when stripping * is enabled. The strip mechanism itself lives in acp-kernel's wire layer diff --git a/src/registry.ts b/src/registry.ts index 4d93e84bdc..5e3481cd68 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -21,7 +21,7 @@ type RegistryShape = Record; // providers..models..cost ($/Mtok). Only rows with a usable input // price are stored — without an input anchor there is nothing to normalize // against, and half-inventing a profile would misprice every fold. -type CostRow = { input: number; output?: number; cache_read?: number; cache_write?: number }; +export type CostRow = { input: number; output?: number; cache_read?: number; cache_write?: number }; type CostsShape = Record; type LoadedRegistry = { reg: RegistryShape; costs: CostsShape | null }; From ca5353a6e8e871df6306422247531b4c204cde82 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 2/7] fix: make always-defaulted ProxyOptions fields optional, loadOptions supplies them (#1991 item 1) --- src/cli.ts | 4 ++-- src/config.ts | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index e60631b105..4254ac4b30 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -598,7 +598,7 @@ export async function main(): Promise { // affected range (rollback-form advisories cover latest too). advisoryBlocksVersion, onStaleInstall: createAutoRestartHandler({ - enabled: opts.autoRestartOnUpdate, + enabled: opts.autoRestartOnUpdate ?? false, packageName: PACKAGE_NAME, server, host: opts.host, @@ -619,7 +619,7 @@ export async function main(): Promise { advisoryUrl: opts.advisoryUrl, resolveProxy: (url) => resolveProxy(opts.routes, opts.proxy, url, opts.proxyFallback), onStaleInstall: createAutoRestartHandler({ - enabled: opts.autoRestartOnUpdate, + enabled: opts.autoRestartOnUpdate ?? false, packageName: PACKAGE_NAME, server, host: opts.host, diff --git a/src/config.ts b/src/config.ts index b8fcc919c5..3e1b3f324e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -630,13 +630,13 @@ export type ProxyOptions = { * plain-object key paths of client-fixed fields to strip before forward * for strict-schema gateways that 400 on unknown fields; per-provider * lists union onto it additively. Empty = byte-for-byte transparent. */ - compat: { roles: Record; dropFields?: string[] }; + compat?: { roles: Record; dropFields?: string[] }; /** #1455: how upstream stream failures are presented to the client on the * anthropic/openai wire — "protocol" (default) = protocol-native error * frames; "completion" = legacy synthesized-completion shape for hosts * whose SDK cannot surface in-band errors. Env BILI_STREAM_ERROR_SHAPE * wins over the file's compat.streamErrorShape. */ - streamErrorShape: "protocol" | "completion"; + streamErrorShape?: "protocol" | "completion"; /** Global-level image billing mode (#767); per-provider route entries * override it, env BILI_IMAGE_BILLING overrides both. undefined = auto * (= pixels for every host since #1843). */ @@ -653,20 +653,20 @@ export type ProxyOptions = { /** Where `passthrough` came from: "env" (ACP_PASSTHROUGH or --passthrough * flag), "file" (config `passthrough: true`), or null (default off). * Drives the #405 boot warning and the web panel's source display. */ - passthroughSource: "env" | "file" | null; + passthroughSource?: "env" | "file" | null; autoUpdate: boolean; /** Opt-in self-restart when a newer version is already installed on disk * (#811): re-exec at zero in-flight requests. Default OFF. */ - autoRestartOnUpdate: boolean; + autoRestartOnUpdate?: boolean; /** Dist-tag channel the auto-updater follows (default "latest"). */ - updateTag: string; + updateTag?: string; /** Critical-defect advisory watcher (#1481): runs INDEPENDENTLY of * autoUpdate and force-installs the owner-recommended version when the * local version falls inside an affected range. Default ON. */ - advisoryCheck: boolean; + advisoryCheck?: boolean; /** Tiered release-notes visibility (#1870): fetch + cache only — never * installs, never restarts. Default ON. */ - releaseNotesCheck: boolean; + releaseNotesCheck?: boolean; /** Override for the advisory document URL (env BILI_ADVISORY_URL wins). */ advisoryUrl?: string; /** Override for the release-notes document URL (env From ec047a0701e2464801ac6fb1c68ff44b6b0eec63 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20Session=20fixture=20ergonomics=20?= =?UTF-8?q?=E2=80=94=20zeroStats()=20factory=20+=20typed=20metadata=20acce?= =?UTF-8?q?ssors=20(#1991=20items=202,=208)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/session.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/session.ts b/src/session.ts index 7c97f0ea9d..413f4a809e 100644 --- a/src/session.ts +++ b/src/session.ts @@ -351,6 +351,29 @@ export type Session = { lockChain?: Promise; }; +/** Server-stamped anonymous-prefix-affinity record (#1115/#1486 lane, written + * in src/server.ts when an anonymous request resolves onto a pfa-* chain or + * mints a fresh one). Typed here so readers don't cast the Record bag. */ +export type AnonymousPrefixAffinityStamp = { + depth: number; + tailHash: string; + via: "prefix" | "new"; + lineage?: { parents: string[]; reason: "truncated" | "forked"; sharedPrefix?: number }; +}; + +export function peekAnonymousPrefixAffinity(session: Session): AnonymousPrefixAffinityStamp | undefined { + const v = session.metadata.anonymousPrefixAffinity; + if (!v || typeof v !== "object") return undefined; + return v as AnonymousPrefixAffinityStamp; +} + +/** Per-request-resolved context limit stamped by the server (src/server.ts) — + * the window actually in force for this session's traffic. */ +export function peekEffectiveContextLimit(session: Session): number | undefined { + const v = session.metadata.effectiveContextLimit; + return typeof v === "number" ? v : undefined; +} + // #833: wire paths resolve the kernel Config per request (global → provider → // model compress settings + self-heal + output headroom), while the plugin // status/tool API reads sessions with no request context and was falling back @@ -466,6 +489,12 @@ export async function initSessions(): Promise { } } +/** Zero-valued stats for a fresh session (#1991) — exported so test fixtures + * can build sessions without retyping every field. */ +export function zeroStats(): Session["stats"] { + return { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, retrieveDropped: 0, retrieveDelivered: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }; +} + export function getSession(id: string, meta?: { protocol?: Session["meta"]["protocol"]; upstreamOrigin?: string; label?: string }): Session { const existing = sessions.get(id); if (existing) { @@ -508,7 +537,7 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot const session: Session = { id, meta: { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin, label: meta?.label }, - stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, retrieveDropped: 0, retrieveDelivered: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + stats: zeroStats(), metadata: {}, state: createInitialState(), createdAt: Date.now(), From b65d8a21ce72493454bc8d55ef9090f61283e200 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 4/7] fix: OmpConfig/OpencodeConfig providers optional to match discovery tolerance at every read site (#1991 item 5) --- src/client-config.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/client-config.ts b/src/client-config.ts index 4e060ef4ed..5a4a7ce28f 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -76,7 +76,9 @@ export interface OmpProvider { } export interface OmpConfig { - providers: Record; + /** Discovery tolerates a lane with no discovered providers (`?? {}` at + * every read site) — optional here matches that (#1991 item 5). */ + providers?: Record; } export interface OpencodeProvider { @@ -86,7 +88,9 @@ export interface OpencodeProvider { } export interface OpencodeConfig { - providers: Record; + /** Discovery tolerates a lane with no discovered providers (`?? {}` at + * every read site) — optional here matches that (#1991 item 5). */ + providers?: Record; } /** opencode's built-in "zen" gateway (`opencode auth login`): the baseURL @@ -1037,7 +1041,7 @@ export function readPiConfig(piHome: string): PiConfig { * so it tolerates the file's base indent. */ export function parseOmpYaml(text: string): OmpConfig { - const result: OmpConfig = { providers: {} }; + const providers: Record = {}; let providersIndent = -1; let providerIndent = -1; let currentProvider: string | null = null; @@ -1050,7 +1054,7 @@ export function parseOmpYaml(text: string): OmpConfig { if (pending === undefined || currentProvider === null) { pending = undefined; return; } const win = toModelWindow(pending.id, pending.contextWindow, pending.maxOutput); if (win) { - const prov = result.providers[currentProvider]!; + const prov = providers[currentProvider]!; prov.models = [...(prov.models ?? []), win]; } pending = undefined; @@ -1072,7 +1076,7 @@ export function parseOmpYaml(text: string): OmpConfig { const m = /^([A-Za-z0-9_.-]+):/.exec(trimmed); if (m) { currentProvider = m[1]; - if (!result.providers[currentProvider]) result.providers[currentProvider] = {}; + if (!providers[currentProvider]) providers[currentProvider] = {}; } else { currentProvider = null; } @@ -1093,12 +1097,12 @@ export function parseOmpYaml(text: string): OmpConfig { flushPending(); } else if (modelsIndent < 0 || indent <= modelsIndent) { const m = /^baseUrl:\s*(\S+)/.exec(trimmed); - if (m) result.providers[currentProvider]!.baseUrl = m[1]; + if (m) providers[currentProvider]!.baseUrl = m[1]; } } } flushPending(); - return result; + return { providers }; } export function readOmpConfig(ompHome: string): OmpConfig { From 78bfe99a6119f2bb0cf3938fe53e47cd3914734a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 5/7] fix: V2PluginContext hook/tool.transform/command.transform accept synchronous registration returns (#1991 item 6) --- src/agent/opencode-v2.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 8d91dfab9b..d84db738d3 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -105,14 +105,14 @@ interface V2CatalogModelEntry { export interface V2PluginContext { session?: { - hook?: (name: string, cb: (e: V2HttpRequestEvent) => void | Promise) => void | Promise; + hook?: (name: string, cb: (e: V2HttpRequestEvent) => void | Promise) => V2Registration | void | Promise; synthetic?: (input: { sessionID: string; text: string; description?: string; resume?: boolean }) => Promise; }; tool?: { - transform?: (cb: (editor: V2ToolEditor) => void) => void | Promise; + transform?: (cb: (editor: V2ToolEditor) => void) => V2Registration | void | Promise; }; command?: { - transform?: (cb: (editor: V2CommandEditor) => void) => void | Promise; + transform?: (cb: (editor: V2CommandEditor) => void) => V2Registration | void | Promise; }; event?: { subscribe?: (opts?: { signal?: AbortSignal }) => AsyncIterable<{ type?: unknown; data?: Record }> | undefined }; /** v2.0.x stable (probed @opencode/cli 2.0.18): ModelApi.list() → From 070e475115f5c5c7547c65565f0b4d049be6aff5 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 6/7] fix: sibling .d.mts so tests can statically import the dist annotation checker (#1991 item 11) --- scripts/check-dist-import-annotations.d.mts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 scripts/check-dist-import-annotations.d.mts diff --git a/scripts/check-dist-import-annotations.d.mts b/scripts/check-dist-import-annotations.d.mts new file mode 100644 index 0000000000..849d82922b --- /dev/null +++ b/scripts/check-dist-import-annotations.d.mts @@ -0,0 +1,3 @@ +export declare function findRelativeImportRefs(text: string): string[]; + +export declare function checkDistImportAnnotations(distDir: string): Array<{ file: string; refs: string[] }>; From bfb73618b12b263ad65a4ce3c64393289eb7af64 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 20:14:33 +0800 Subject: [PATCH 7/7] test: guard cfg.providers reads against the now-optional OmpConfig/OpencodeConfig.providers (#1991 item 5) --- tests/launcher-max-outputs.test.ts | 4 ++-- tests/launcher.test.ts | 20 ++++++++++---------- tests/model-windows.test.ts | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/launcher-max-outputs.test.ts b/tests/launcher-max-outputs.test.ts index b79a47b9c1..e404e67013 100644 --- a/tests/launcher-max-outputs.test.ts +++ b/tests/launcher-max-outputs.test.ts @@ -71,7 +71,7 @@ test("parseOmpYaml: maxTokens at model-entry depth completes the entry (#971)", " contextWindow: 131072", ].join("\n"); const cfg = parseOmpYaml(yml); - assert.deepEqual(cfg.providers.sglang?.models, [ + assert.deepEqual(cfg.providers?.sglang?.models, [ { id: "qwen3.8-27b", contextWindow: 262144, maxOutput: 32768 }, { id: "other-model", contextWindow: 131072 }, ]); @@ -89,7 +89,7 @@ test("parseOpencodeProviders: limit object shape {context, output} (#971)", () = }, }, }); - assert.deepEqual(cfg.providers.sglang?.models, [ + assert.deepEqual(cfg.providers?.sglang?.models, [ { id: "qwen3.8-27b", contextWindow: 262144, maxOutput: 32768 }, { id: "legacy", contextWindow: 131072 }, ]); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index f9666f93ec..d814c4e473 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -2601,10 +2601,10 @@ test("parseOmpYaml: reads providers..baseUrl (skips non-matching)", () => " default: sglang-responses/qwen3.8-27b:high", ].join("\n"); const cfg = parseOmpYaml(yml); - assert.equal(cfg.providers["sglang-responses"].baseUrl, "http://127.0.0.1:8199/v1"); - assert.equal(cfg.providers["zhipuai"].baseUrl, "https://open.bigmodel.cn/api/coding/paas/v4"); - assert.equal(cfg.providers["ollama-chat"].baseUrl, "http://127.0.0.1:11435/v1"); - assert.equal(Object.keys(cfg.providers).length, 3); + assert.equal(cfg.providers?.["sglang-responses"]?.baseUrl, "http://127.0.0.1:8199/v1"); + assert.equal(cfg.providers?.["zhipuai"]?.baseUrl, "https://open.bigmodel.cn/api/coding/paas/v4"); + assert.equal(cfg.providers?.["ollama-chat"]?.baseUrl, "http://127.0.0.1:11435/v1"); + assert.equal(Object.keys(cfg.providers ?? {}).length, 3); }); test("parseOmpYaml: no providers key → {}", () => { @@ -2617,7 +2617,7 @@ test("readOmpConfig: reads models.yml from omp home", () => { try { fs.writeFileSync(path.join(home, "models.yml"), "providers:\n a:\n baseUrl: http://x:1/v1\n"); const cfg = readOmpConfig(home); - assert.equal(cfg.providers.a.baseUrl, "http://x:1/v1"); + assert.equal(cfg.providers?.a?.baseUrl, "http://x:1/v1"); } finally { rmrf(home); } @@ -2668,10 +2668,10 @@ test("readOpencodeConfig: reads provider baseURLs from opencode.json", () => { }), ); const cfg = readOpencodeConfig(cfgFile); - assert.deepEqual(cfg.providers["local"], { baseURL: "http://127.0.0.1:18081/v1" }); - assert.deepEqual(cfg.providers["remote"], { baseURL: "https://api.example.com/v1" }); - assert.equal(cfg.providers["noUrl"], undefined); - assert.equal(readOpencodeConfig(path.join(dir, "missing.json")).providers["local"], undefined); + assert.deepEqual(cfg.providers?.["local"], { baseURL: "http://127.0.0.1:18081/v1" }); + assert.deepEqual(cfg.providers?.["remote"], { baseURL: "https://api.example.com/v1" }); + assert.equal(cfg.providers?.["noUrl"], undefined); + assert.equal(readOpencodeConfig(path.join(dir, "missing.json")).providers?.["local"], undefined); } finally { rmrf(dir); } @@ -2711,7 +2711,7 @@ test("readOpencodeConfig: parses JSONC (comments + trailing commas)", () => { ].join("\n"), ); const cfg = readOpencodeConfig(cfgFile); - assert.deepEqual(cfg.providers["local"], { baseURL: "http://127.0.0.1:18081/v1" }); + assert.deepEqual(cfg.providers?.["local"], { baseURL: "http://127.0.0.1:18081/v1" }); } finally { rmrf(dir); } diff --git a/tests/model-windows.test.ts b/tests/model-windows.test.ts index 729e7d4a89..714db58036 100644 --- a/tests/model-windows.test.ts +++ b/tests/model-windows.test.ts @@ -31,9 +31,9 @@ test("parseOmpYaml: captures per-model contextWindow (models after baseUrl)", () " maxTokens: 32768", ].join("\n"); const cfg = parseOmpYaml(yml); - assert.equal(cfg.providers["sglang-responses"]?.baseUrl, "http://127.0.0.1:8199/v1"); + assert.equal(cfg.providers?.["sglang-responses"]?.baseUrl, "http://127.0.0.1:8199/v1"); // #971: maxTokens at model-entry depth now completes the entry as maxOutput. - assert.deepEqual(cfg.providers["sglang-responses"]?.models, [{ id: "qwen3.8-27b", contextWindow: 262144, maxOutput: 32768 }]); + assert.deepEqual(cfg.providers?.["sglang-responses"]?.models, [{ id: "qwen3.8-27b", contextWindow: 262144, maxOutput: 32768 }]); }); test("parseOmpYaml: baseUrl AFTER models: is still captured", () => { @@ -46,8 +46,8 @@ test("parseOmpYaml: baseUrl AFTER models: is still captured", () => { " baseUrl: http://after.example/v1", ].join("\n"); const cfg = parseOmpYaml(yml); - assert.equal(cfg.providers.prov?.baseUrl, "http://after.example/v1"); - assert.deepEqual(cfg.providers.prov?.models, [{ id: "m1", contextWindow: 32768 }]); + assert.equal(cfg.providers?.prov?.baseUrl, "http://after.example/v1"); + assert.deepEqual(cfg.providers?.prov?.models, [{ id: "m1", contextWindow: 32768 }]); }); test("parseOmpYaml: multiple models + multiple providers", () => { @@ -67,8 +67,8 @@ test("parseOmpYaml: multiple models + multiple providers", () => { " contextWindow: 3000", ].join("\n"); const cfg = parseOmpYaml(yml); - assert.deepEqual(cfg.providers.a?.models, [{ id: "m1", contextWindow: 1000 }, { id: "m2", contextWindow: 2000 }]); - assert.deepEqual(cfg.providers.b?.models, [{ id: "m1", contextWindow: 3000 }]); + assert.deepEqual(cfg.providers?.a?.models, [{ id: "m1", contextWindow: 1000 }, { id: "m2", contextWindow: 2000 }]); + assert.deepEqual(cfg.providers?.b?.models, [{ id: "m1", contextWindow: 3000 }]); }); test("readPiConfig: captures models[].contextWindow from models.json", () => { @@ -111,8 +111,8 @@ test("readOpencodeConfig: captures models..limit", () => { }), ); const cfg = readOpencodeConfig(file); - assert.equal(cfg.providers.prov?.baseURL, "http://prov/v1"); - assert.deepEqual(cfg.providers.prov?.models, [{ id: "glm-5.2", contextWindow: 1000000 }]); + assert.equal(cfg.providers?.prov?.baseURL, "http://prov/v1"); + assert.deepEqual(cfg.providers?.prov?.models, [{ id: "glm-5.2", contextWindow: 1000000 }]); }); test("parseCodexToml: model + model_context_window pair captured", () => {