diff --git a/CONFIGURATION.md b/CONFIGURATION.md index a34cf976ba..63f62cc372 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -1110,6 +1110,7 @@ Targets are **references into the `providers` table**: each entry is the string "enabled": true, "targets": ["glm/glm-4.9-flash", "claude/claude-haiku-4.5"], "budget": { "totalTimeoutMs": 50000, "targetTimeoutMs": 25000, "maxSummaryBytes": 65536 }, + "concurrency": 8, "autoFold": true, "autoFoldTargetTokens": 40000 } @@ -1121,6 +1122,10 @@ A recipe carries: `baseUrl` (HTTPS; loopback HTTP allowed for local development; Up to 16 targets per chain are supported. Saving an enabled chain through the Web API validates that every reference resolves (unknown provider or model → HTTP 400). At runtime an unresolvable reference is logged once and disables the chain until fixed — it never falls back to summarizing with the main model. `secret:` values are stored separately from the main JSON configuration in the private `billion-context.json.summary-credentials.json` file and are never returned by the configuration API. On Windows, protect this file and its parent directory with an administrator-only ACL; stale `.lock` files require manual removal after confirming no proxy process is writing the store. The total budget is shared across all targets and compression entry points, and cancellation or session-state changes discard generated results without folding. The agent-side plugin bridge (dsh / pi / opencode / codex MCP) sizes each `compress` call's HTTP wait to `totalTimeoutMs` plus a small overhead margin — learned from the proxy at bootstrap — so raising this budget no longer caps long folds at a hidden 60-second client-side timeout (#2652). +`concurrency` (integer `1..32`, default `4`) sizes the **process-wide summary queue**: every external-summary call on this host — preflight auto-folds for *all* sessions plus every model-initiated `compress` call — shares one pool of in-flight requests. Raise it when your summarizer has headroom (local relay, self-hosted model, high-quota flash endpoint); keep the conservative default on a small provider quota. A chain's value resizes the shared pool only while that chain's work is in flight; if several chains with different values run concurrently, the pool grows to the largest demand and returns to the base size once those batches settle — one session can never permanently resize the pool for everyone else. Note the knob is **grow-only**: one session raising the pool must not throttle another's in-flight work, so values below the default (`1..3`) are accepted for validation but never shrink the pool below `4` — do not set a low value expecting quota protection; configure the chain's own `budget`/`targetTimeoutMs` instead. + +Interaction with the budget: the achievable fold rate of a batch is bounded by how many calls fit in the pool (`concurrency`) and how long each may take (`targetTimeoutMs`) — the budget alone does not control the outcome. A saturated pool does not surface as a visible error: a batch still queued when its total deadline expires is discarded as `batch=deadline` with no attempts recorded (`attempts=[]`), and the fold pass stops there instead of queueing further rounds behind the congestion. For auto-fold this is treated as transient scheduler congestion, **not** a chain failure: no 10-minute backoff arms, and the next turn retries into the (hopefully drained) pool. If you see those lines while direct calls to your target succeed quickly, the pool — not the timeout — is the binding constraint. + Two optional knobs turn the chain into a **proxy-driven auto-folder** (`autoFold`): with `"autoFold": true` the proxy stops waiting for the model to call the compression tool at all — the same zero-model folding engine that guards the overflow window also folds the conversation down to `autoFoldTargetTokens` (default: half the model window) *before* forwarding, using the external chain and kernel-chosen ranges. The model never sees a nudge and never spends thinking tokens drafting compression plans. The knob rides whole-chain replace semantics like the rest of the block; it only takes effect while the chain itself is enabled — setting either knob on a disabled (or omitted-`enabled`) chain has no effect and is logged once as a warning rather than failing silently. `autoFoldTargetTokens` must be an integer in `[8192, 10000000]`. Failure is fail-open: if the chain errors at fold time, the request forwards as-is, a 10-minute backoff suppresses auto-folding, and classic advisory nudges return for that window. One exception (#2662): a zero-progress fold caused purely by queue drops — batches submitted but no summary call ever dispatched because the shared pool is saturated — does not arm the backoff, since that is scheduler congestion rather than a chain failure; auto-fold simply retries on the next turn. #### Agent-reported providers (fallback layer) diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 108b45bb18..980b634a1e 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -1113,6 +1113,7 @@ URL 键通道(或带 `bind` 的命名条目)可以向上游发送**自己的 "enabled": true, "targets": ["glm/glm-4.9-flash", "claude/claude-haiku-4.5"], "budget": { "totalTimeoutMs": 50000, "targetTimeoutMs": 25000, "maxSummaryBytes": 65536 }, + "concurrency": 8, "autoFold": true, "autoFoldTargetTokens": 40000 } @@ -1124,6 +1125,10 @@ URL 键通道(或带 `bind` 的命名条目)可以向上游发送**自己的 每条链最多 16 个目标。通过 Web API 保存启用的链时会校验每个引用可解析(未知 provider 或 model → HTTP 400)。运行时遇到不可解析的引用会记录一次警告并禁用整条链直至修复 —— 绝不会回退用主模型写摘要。`secret:` 值单独存储在私有的 `billion-context.json.summary-credentials.json` 文件中,不随主 JSON 配置下发,配置 API 也不会返回。Windows 上请用仅管理员的 ACL 保护该文件及其父目录;确认没有代理进程在写存储后,残留的 `.lock` 文件需手工删除。总预算由全部目标与压缩入口共享;取消或会话状态变化会丢弃已生成但未落盘的结果。代理端插件桥(dsh / pi / opencode / codex MCP)会把每次 `compress` 调用的 HTTP 等待时间设为 `totalTimeoutMs` 加少量开销余量 —— 启动时从代理学习得到 —— 因此调高该预算不会再让长折叠被隐藏的 60 秒客户端超时截断(#2652)。 +`concurrency`(整数 `1..32`,默认 `4`)决定**进程级摘要队列**的大小:本机所有外部摘要调用 —— *全部* 会话的 preflight 自动折叠加上每次模型发起的 `compress` 调用 —— 共享同一个在途请求池。当你的摘要服务有余量(本地中继、自托管模型、高配额 flash 端点)时调大它;小配额 provider 保持保守默认即可。某条链设置的值只在该链的工作在途期间调整共享池大小;多条不同取值的链并发运行时,池子增长到最大需求,待这些批次结束后回到基础值 —— 任何单个会话都无法永久改变其他会话看到的池大小。注意该旋钮是**只增不减**的:单个会话调大池子不应压制其他会话的在途工作,因此低于默认值的取值(`1..3`)能通过校验但永远不会把池子缩到 `4` 以下 —— 不要指望设小值来保护配额,请改配该链自己的 `budget`/`targetTimeoutMs`。 + +与 budget 的交互:一个批次能达到的折叠速率由池能容纳的并发数(`concurrency`)和每次调用的时限(`targetTimeoutMs`)共同决定 —— 单看 budget 并不能解释结果。池饱和不会表现为可见错误:批次还在排队就耗尽总时限的话,会被整体丢弃为 `batch=deadline` 且没有任何尝试记录(`attempts=[]`),折叠过程也会就地停止而不是继续排在拥塞后面。对 auto-fold 而言这被当作暂时的调度拥塞而**非**链路故障:不会触发下文所述的 10 分钟退避,下一个回合会在(有望已排空的)池子上重试。如果你看到这些日志行、而直接调用目标却很快成功,说明瓶颈是池而不是超时。 + 两个可选旋钮可把这条链升级为**代理驱动的自动折叠器**(`autoFold`):`"autoFold": true` 后代理不再等模型调压缩工具 —— 原本只守溢出窗口的零模型折叠引擎同样会在转发前把对话折到 `autoFoldTargetTokens`(默认:模型窗口的一半)以下,范围由内核选定、摘要由外部链完成。主模型完全看不到 nudge,也不会把思考预算花在起草压缩方案上。该旋钮随整链替换语义生效,且只在链本身启用时才起作用——在链未启用(或省略 `enabled`)时设置任一旋钮均无效,并会记录一次警告而非静默失效;`autoFoldTargetTokens` 必须是 `[8192, 10000000]` 内的整数。失败时 fail-open:链在折叠时出错则照常转发请求,退避 10 分钟内暂停自动折叠并恢复经典 advisory nudge。一个例外(#2662):若零进展完全由队列丢弃造成——批次已提交但因共享池饱和、没有任何一次摘要调用真正派发——则不触发退避(那是调度拥塞而非链故障),auto-fold 在下一轮直接重试。 #### Agent 上报的 providers(兜底层) diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 344988b237..3f76683fe7 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -38,7 +38,7 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx // The plan rides the request Config rail (#833: ctx.config is the // session's effective Config — same cascade that produced the wire). let plan; - try { plan = configuredSummaryPlan((ctx.config as ResolvedKernelConfig).externalSummary); } + try { plan = configuredSummaryPlan((ctx.config as ResolvedKernelConfig).externalSummary, ctx.session.id); } catch { return compressResult("[Compression FAILED: external summary configuration is invalid. Nothing compressed.]", "refused", 0); } if (!plan) return applyRanges(parseCompressInput(input, callId), ctx); const parsed = parseCompressInput(withOptionalSummaries(input), callId); diff --git a/src/external-summary-http.ts b/src/external-summary-http.ts index d2c7dd384b..91818c0f25 100644 --- a/src/external-summary-http.ts +++ b/src/external-summary-http.ts @@ -4,6 +4,7 @@ import { fetchWithTimeout } from "./fetch-util.js"; import { extractSummaryFromSse, extractSummaryText, summaryPayload, type PreflightProtocol } from "./preflight.js"; import { normalizeSseLineEndings } from "./sse-util.js"; import { proxyDispatcher } from "./upstream-proxy.js"; +import { PACKAGE_NAME, VERSION } from "./version.js"; /** Internal resolved request plan, not a user-config schema or credential store. */ export interface SummaryHttpTarget { @@ -15,6 +16,8 @@ export interface SummaryHttpTarget { readonly contextWindow?: number; readonly outputTokens?: number; readonly proxyUrl?: string; + /** #2760: stable conversation id, stamped as x-opencode-session on OpenCode Go targets only. */ + readonly sessionId?: string; } function record(value: unknown): Record | undefined { @@ -108,6 +111,19 @@ function parseSummary(protocol: PreflightProtocol, text: string): string { return summary; } +/** #2760: OpenCode Go (`opencode.ai/zen/go`) requires a stable x-opencode-session + * header for routing + prompt cache and answers HTTP 400 MissingSessionID without + * one. Matched on host + exact `/zen/go` or `/zen/go/...` so adjacent endpoints + * (a hypothetical `/zen/golang`) are left untouched too. */ +export function isOpenCodeGoTarget(rawUrl: string): boolean { + try { + const parsed = new URL(rawUrl); + return parsed.hostname.toLowerCase() === "opencode.ai" && (parsed.pathname === "/zen/go" || parsed.pathname.startsWith("/zen/go/")); + } catch { + return false; + } +} + export function createSummaryHttpCandidate(target: SummaryHttpTarget, maxResponseBytes: number): SummaryCandidate { let url: URL; try { url = new URL(target.url); } catch { throw new Error("Invalid external summary endpoint"); } @@ -121,6 +137,13 @@ export function createSummaryHttpCandidate(target: SummaryHttpTarget, maxRespons const plan = { ...target, url: url.href, headers }; plan.headers.set("content-type", "application/json"); if (plan.protocol === "anthropic" && !plan.headers.has("anthropic-version")) plan.headers.set("anthropic-version", "2023-06-01"); + // #2760: OpenCode Go rejects requests lacking a stable x-opencode-session + // (HTTP 400 MissingSessionID). Stamp it plus bili's own user agent for Go + // targets only — never the client's inbound session/auth headers. + if (isOpenCodeGoTarget(plan.url) && typeof plan.sessionId === "string" && plan.sessionId.length > 0) { + plan.headers.set("x-opencode-session", plan.sessionId); + plan.headers.set("user-agent", `${PACKAGE_NAME}/${VERSION}`); + } return { async summarize(work: Readonly, signal: AbortSignal): Promise { signal.throwIfAborted(); @@ -136,8 +159,10 @@ export function createSummaryHttpCandidate(target: SummaryHttpTarget, maxRespons payload[plan.protocol === "responses" ? "max_output_tokens" : "max_tokens"] = plan.outputTokens; } } - // Reuse the existing codecs without carrying the main request's auth, - // session headers, or model. Each candidate dispatches exactly once. + // Reuse the existing codecs without carrying the main request's auth or + // model. Each candidate dispatches exactly once. (The Go-specific + // x-opencode-session / user-agent above are derived from bili's own + // session id, not copied from the client's inbound headers — #2760.) const { response, clearTimer } = await fetchWithTimeout(plan.url, { method: "POST", headers: plan.headers, body: JSON.stringify(payload), redirect: "manual", dispatcher: proxyDispatcher(plan.proxyUrl), }, undefined, signal); diff --git a/src/external-summary-runtime.ts b/src/external-summary-runtime.ts index 2913ced84e..80717a43d8 100644 --- a/src/external-summary-runtime.ts +++ b/src/external-summary-runtime.ts @@ -1,12 +1,13 @@ -import { performance } from "node:perf_hooks"; import { SummaryCredentialStore } from "./external-summary-credentials.js"; import { createSummaryHttpCandidate } from "./external-summary-http.js"; -import { parseExternalSummarySettings, type ExternalSummarySettings } from "./external-summary-settings.js"; +import { parseExternalSummarySettings, SUMMARY_CONCURRENCY_DEFAULT, type ExternalSummarySettings } from "./external-summary-settings.js"; import { ExternalSummaryExecutor, type ExternalSummaryBatchResult, type SummaryCandidate, type SummaryWork } from "./external-summary.js"; import { log as loggerLog } from "./logger.js"; -// One shared queue across all sessions and all compression entry points. -const executor = new ExternalSummaryExecutor(4); +// One shared queue across all sessions and all compression entry points; its +// base size is the config default — a chain's `concurrency` resizes it for +// that batch's lifetime (#2657). +const executor = new ExternalSummaryExecutor(SUMMARY_CONCURRENCY_DEFAULT); // Test seam (#2662): saturate the process-wide pool deterministically. export const _executorForTest = executor; @@ -18,23 +19,23 @@ const seenCandidateWarnings = new Set(); export class ConfiguredSummaryPlan { private readonly candidates: readonly SummaryCandidate[]; private readonly settings: ExternalSummarySettings; - readonly deadline: number; /** #2662: summarize() invocations vs candidate calls actually dispatched — * calls > 0 && attempts === 0 means no candidate call was ever dispatched * (shared-pool congestion, or the plan's budget already exhausted before * reaching the pool — congestion, not a chain failure). */ readonly stats = { calls: 0, attempts: 0 }; + private readonly sessionId: string | undefined; // `raw` may be an already-parsed chain off the request rail or raw JSON // from a hand-edited file — re-parse here so invalid settings fail // loudly at plan build, never silently use main-model summaries. - constructor(raw: unknown, store = new SummaryCredentialStore(), env: NodeJS.ProcessEnv = process.env) { + constructor(raw: unknown, store = new SummaryCredentialStore(), env: NodeJS.ProcessEnv = process.env, sessionId?: string) { // inlineKeys: the rail may carry agent-registry recipes whose keys // were resolved in the agent's memory (#2336) — literal apiKey bytes // instead of a credentialRef. this.settings = parseExternalSummarySettings(raw, { inlineKeys: true }); + this.sessionId = sessionId; const proxyUrl = env.BILI_UPSTREAM_PROXY?.trim() || undefined; - this.deadline = performance.now() + this.settings.budget.totalTimeoutMs; this.candidates = this.settings.targets.map((target) => { try { const key = target.apiKey !== undefined ? target.apiKey : store.resolve(target.credentialRef ?? "", env); @@ -42,7 +43,7 @@ export class ConfiguredSummaryPlan { const headers: Record = target.protocol === "anthropic" ? { "x-api-key": key } : target.protocol === "google" ? { "x-goog-api-key": key } : { authorization: `Bearer ${key}` }; - return createSummaryHttpCandidate({ ...target, headers, proxyUrl }, this.settings.budget.maxSummaryBytes * 4 + 65536); + return createSummaryHttpCandidate({ ...target, headers, proxyUrl, ...(this.sessionId !== undefined ? { sessionId: this.sessionId } : {}) }, this.settings.budget.maxSummaryBytes * 4 + 65536); } catch (error) { // Log WHY this target is unusable (once per signature) so a // misconfigured chain is diagnosable instead of silently degrading @@ -60,15 +61,12 @@ export class ConfiguredSummaryPlan { } async summarize(work: readonly SummaryWork[], signal?: AbortSignal): Promise { - const remaining = Math.floor(this.deadline - performance.now()); - if (remaining <= 0) { - this.stats.calls += 1; - return { status: "deadline", results: [] }; - } - const batch = await executor.executeBatch(work, this.candidates, { - ...this.settings.budget, totalTimeoutMs: remaining, - targetTimeoutMs: Math.min(remaining, this.settings.budget.targetTimeoutMs), - }, signal); + // Deliberately NO cached deadline here: preflight reuses one plan across + // many summarize() calls, so each must get the FULL configured budget — + // a construction-time clock point went stale mid-round and starved every + // later range (#2639). The model-tool path packs all ranges into one call, + // where executeBatch's shared deadline still bounds the whole batch. + const batch = await executor.executeBatch(work, this.candidates, { ...this.settings.budget }, signal, { concurrency: this.settings.concurrency }); this.stats.calls += 1; for (const item of batch.results) this.stats.attempts += item.attempts.length; return batch; @@ -80,8 +78,8 @@ export class ConfiguredSummaryPlan { * `mergeCompress` with whole-chain-replace semantics). undefined/disabled → * undefined (legacy in-model summaries). Invalid settings throw — they must * fail loudly, never silently fall back to main-model summaries. */ -export function configuredSummaryPlan(settings: unknown): ConfiguredSummaryPlan | undefined { +export function configuredSummaryPlan(settings: unknown, sessionId?: string): ConfiguredSummaryPlan | undefined { if (!settings || typeof settings !== "object") return undefined; if ((settings as { enabled?: unknown }).enabled !== true) return undefined; - return new ConfiguredSummaryPlan(settings); + return new ConfiguredSummaryPlan(settings, new SummaryCredentialStore(), process.env, sessionId); } diff --git a/src/external-summary-settings.ts b/src/external-summary-settings.ts index 7237278ecf..c4ecaf78a6 100644 --- a/src/external-summary-settings.ts +++ b/src/external-summary-settings.ts @@ -24,6 +24,12 @@ export interface ExternalSummarySettings { enabled: boolean; targets: ExternalSummaryTarget[]; budget: SummaryBudget; + /** [#2657] Shared summary-queue pool size while this chain's work is in + * flight: integer [SUMMARY_CONCURRENCY_MIN, SUMMARY_CONCURRENCY_MAX], + * default SUMMARY_CONCURRENCY_DEFAULT. Absent → the executor's base + * size; present → the pool grows to it for the batch lifetime (the + * largest request among in-flight batches wins; see the executor). */ + concurrency?: number; /** [#autoFold] Proxy-driven growth folding: when true, preflight folds * the conversation down to `autoFoldTargetTokens` (default half the * model window) BEFORE forwarding — the model never sees a nudge and @@ -41,6 +47,7 @@ export interface ExternalSummaryChain { enabled: boolean; targets: string[]; budget?: SummaryBudget; + concurrency?: number; autoFold?: boolean; autoFoldTargetTokens?: number; } @@ -49,6 +56,12 @@ const SUMMARY_DEFAULT_BUDGET: Readonly = { totalTimeoutMs: 50_000, targetTimeoutMs: 25_000, maxSummaryBytes: 64 * 1024, }; +/** [#2657] Shared summary-queue pool size: one process-wide executor serves + * every session, so the knob sizes THAT pool (it cannot be per-session). */ +const SUMMARY_CONCURRENCY_MIN = 1; +const SUMMARY_CONCURRENCY_MAX = 32; +export const SUMMARY_CONCURRENCY_DEFAULT = 4; + export function validSummaryCredentialName(value: string): boolean { return /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(value); } @@ -99,6 +112,18 @@ function parseAutoFold(settings: Record): { autoFold?: boolean; return { autoFold, ...(autoFoldTargetTokens !== undefined ? { autoFoldTargetTokens } : {}) }; } +/** [#2657] Pool sizing rides the same enabled-gate as autoFold: parsed only + * on an enabled chain, absent → undefined → the executor's base size. */ +function parseConcurrency(settings: Record): { concurrency?: number } { + if (settings.concurrency === undefined) return {}; + const value = settings.concurrency; + if (typeof value !== "number" || !Number.isSafeInteger(value) + || value < SUMMARY_CONCURRENCY_MIN || value > SUMMARY_CONCURRENCY_MAX) { + throw new Error(`External summary concurrency must be an integer between ${SUMMARY_CONCURRENCY_MIN} and ${SUMMARY_CONCURRENCY_MAX}`); + } + return { concurrency: value }; +} + function parseBudget(raw: unknown): SummaryBudget { const rawBudget = raw === undefined ? {} : object(raw); knownKeys(rawBudget, ["totalTimeoutMs", "targetTimeoutMs", "maxSummaryBytes"]); @@ -136,7 +161,7 @@ function noteAutoFoldIgnored(settings: Record): void { * against the providers table at expansion time (expandExternalSummaryChain). */ export function parseExternalSummaryChain(value: unknown): ExternalSummaryChain { const settings = object(value); - knownKeys(settings, ["enabled", "targets", "budget", "autoFold", "autoFoldTargetTokens"]); + knownKeys(settings, ["enabled", "targets", "budget", "concurrency", "autoFold", "autoFoldTargetTokens"]); if (settings.enabled !== undefined && typeof settings.enabled !== "boolean") throw new Error("External summary enabled must be boolean"); const enabled = settings.enabled === true; if (!enabled) { @@ -156,7 +181,7 @@ export function parseExternalSummaryChain(value: unknown): ExternalSummaryChain if (slash <= 0 || slash === target.length - 1) throw new Error(`External summary target "${target}" must reference a provider and model as "provider/model"`); return target; }); - return { enabled, targets, budget: parseBudget(settings.budget), ...parseAutoFold(settings) }; + return { enabled, targets, budget: parseBudget(settings.budget), ...parseConcurrency(settings), ...parseAutoFold(settings) }; } /** Expand chain references against the named providers table. THROWS on an @@ -187,7 +212,8 @@ export function expandExternalSummaryChain(chain: ExternalSummaryChain, recipes: const credentialRef = recipe.apiKeyEnv ? `env:${recipe.apiKeyEnv}` : `secret:${recipe.credentialRef}`; return { name, protocol: recipe.api, url: derivedSummaryEndpoint(recipe, model, stream), model, ...(inlineKey !== undefined ? { apiKey: inlineKey } : { credentialRef }), contextWindow, outputTokens, stream }; }); - return { enabled: true, targets, budget: chain.budget ?? SUMMARY_DEFAULT_BUDGET, ...parseAutoFold(chain as unknown as Record) }; + return { enabled: true, targets, budget: chain.budget ?? SUMMARY_DEFAULT_BUDGET + , ...(chain.concurrency !== undefined ? { concurrency: chain.concurrency } : {}), ...parseAutoFold(chain as unknown as Record) }; } /** Derive the full request endpoint from a recipe's baseUrl + api type — @@ -223,7 +249,7 @@ function derivedSummaryEndpoint(recipe: NamedProviderRecipe, model: string, stre export function parseExternalSummarySettings(value: unknown, options: { inlineKeys?: boolean } = {}): ExternalSummarySettings { const inlineKeys = options.inlineKeys === true; const settings = object(value); - knownKeys(settings, ["enabled", "targets", "budget", "autoFold", "autoFoldTargetTokens"]); + knownKeys(settings, ["enabled", "targets", "budget", "concurrency", "autoFold", "autoFoldTargetTokens"]); if (settings.enabled !== undefined && typeof settings.enabled !== "boolean") throw new Error("External summary enabled must be boolean"); const enabled = settings.enabled === true; if (!enabled) { @@ -268,7 +294,7 @@ export function parseExternalSummarySettings(value: unknown, options: { inlineKe } return { name, protocol, url: url.href, model, ...(inlineKey !== undefined ? { apiKey: inlineKey } : { credentialRef }), contextWindow, outputTokens, stream: target.stream === true }; }); - return { enabled: true, targets, budget: parseBudget(settings.budget), ...parseAutoFold(settings) }; + return { enabled: true, targets, budget: parseBudget(settings.budget), ...parseConcurrency(settings), ...parseAutoFold(settings) }; } const seenExpansionWarnings = new Set(); diff --git a/src/external-summary-surface.ts b/src/external-summary-surface.ts index 1d6cbdc436..a22d59a66b 100644 --- a/src/external-summary-surface.ts +++ b/src/external-summary-surface.ts @@ -14,7 +14,7 @@ export function externalSummaryEnabled(config: unknown): boolean { /** Upper bound (ms) on how long the proxy needs to execute a summary-producing * tool call while the external chain is active: the chain's totalTimeoutMs batch - * deadline. The ConfiguredSummaryPlan clock bounds the whole batch to it, so one + * deadline. The executor's per-batch deadline bounds the whole batch to it, so one * compress call cannot run longer. Advertised in the plugin manifest so agent-side * tool bridges size their HTTP wait to the ACTUAL server ceiling instead of a fixed * constant that silently starves any budget raised above it (#2652). Undefined when diff --git a/src/external-summary.ts b/src/external-summary.ts index 2be464a7fa..4023372a1e 100644 --- a/src/external-summary.ts +++ b/src/external-summary.ts @@ -16,6 +16,14 @@ export interface SummaryBudget { readonly maxSummaryBytes: number; } +/** [#2657] Per-batch pool-size request for the shared queue. The pool grows + * to the LARGEST request among in-flight work and returns to its base size + * once that work settles — one batch never permanently resizes the pool for + * everyone else, so chains with different values can coexist safely. */ +interface SummaryPoolOptions { + readonly concurrency?: number; +} + interface SummaryAttempt { readonly targetIndex: number; readonly outcome: "success" | "error" | "timeout" | "invalid_summary" | "cancelled"; @@ -64,16 +72,33 @@ function awaitAttempt(operation: Promise, signal: AbortSignal): P export class ExternalSummaryExecutor { private active = 0; private readonly waiting: Waiter[] = []; + private readonly baseConcurrency: number; + private readonly scopedConcurrency = new Map(); - constructor(private readonly concurrency: number) { + constructor(concurrency: number) { if (!positiveInteger(concurrency)) throw new TypeError("Summary concurrency must be a positive integer"); + this.baseConcurrency = concurrency; + } + + async execute(work: SummaryWork, candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal, options?: SummaryPoolOptions): Promise { + const endScope = this.beginScope(options); + try { + return await this.executeUntil(work, candidates, budget, signal); + } finally { + endScope(); + } } - async execute(work: SummaryWork, candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal): Promise { - return this.executeUntil(work, candidates, budget, signal); + async executeBatch(work: readonly SummaryWork[], candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal, options?: SummaryPoolOptions): Promise { + const endScope = this.beginScope(options); + try { + return await this.executeBatchInner(work, candidates, budget, signal); + } finally { + endScope(); + } } - async executeBatch(work: readonly SummaryWork[], candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal): Promise { + private async executeBatchInner(work: readonly SummaryWork[], candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal): Promise { const results: ExternalSummaryResult[] = []; if (signal?.aborted) return { status: "cancelled", results }; const limits = { ...budget }; @@ -84,18 +109,48 @@ export class ExternalSummaryExecutor { const deadline = performance.now() + limits.totalTimeoutMs; const requests = work.map((request) => Object.freeze({ ...request })); const targets = [...candidates]; - for (const request of requests) { - if (signal?.aborted) return { status: "cancelled", results }; - if (performance.now() >= deadline) return { status: "deadline", results }; - const result = await this.executeUntil(request, targets, limits, signal, deadline); - results.push(result); - if (signal?.aborted) return { status: "cancelled", results }; - if (result.status === "cancelled" || result.status === "deadline") { - return { status: result.status, results }; + // #2667: workers pull indices from a shared counter so one batch runs up + // to `concurrency` ranges in flight instead of strictly serial — the pool + // now bounds a batch as well as cross-session calls. Results stay + // index-aligned with `work`: the reported list is the DENSE PREFIX of + // settled ranges (downstream zips entries with their work slot), and an + // early stop (caller abort / any range hitting the shared deadline) + // leaves unstarted ranges unreported rather than fabricated. + const completed = new Map(); + let itemDeadline = false; + const stop = new AbortController(); + const onAbort = (): void => stop.abort(); + signal?.addEventListener("abort", onAbort, { once: true }); + const nextIndex = { value: 0 }; + const worker = async (): Promise => { + for (;;) { + if (stop.signal.aborted) return; + const index = nextIndex.value++; + if (index >= requests.length) return; + const result = await this.executeUntil(requests[index], targets, limits, signal, deadline); + completed.set(index, result); + if (result.status === "deadline") itemDeadline = true; + if (result.status === "cancelled" || result.status === "deadline") { + stop.abort(); + return; + } } - if (performance.now() >= deadline) return { status: "deadline", results }; + }; + try { + await Promise.all(Array.from({ length: Math.min(this.effectiveConcurrency(), requests.length) }, () => worker())); + } finally { + signal?.removeEventListener("abort", onAbort); } - return { status: "finished", results }; + let prefix = 0; + while (prefix < requests.length && completed.has(prefix)) prefix += 1; + const settled: ExternalSummaryResult[] = []; + for (let index = 0; index < prefix; index++) { + const value = completed.get(index); + if (value !== undefined) settled.push(value); + } + if (signal?.aborted) return { status: "cancelled", results: settled }; + if (itemDeadline || performance.now() >= deadline) return { status: "deadline", results: settled }; + return { status: "finished", results: settled }; } private async executeUntil(work: SummaryWork, candidates: readonly SummaryCandidate[], budget: SummaryBudget, signal?: AbortSignal, sharedDeadline?: number): Promise { @@ -170,6 +225,36 @@ export class ExternalSummaryExecutor { } } + /** [#2657] Register one batch's pool-size demand for its lifetime; the + * returned release restores the previous sizing. No-op when the batch + * requests no size (the base pool applies). */ + private beginScope(options?: SummaryPoolOptions): () => void { + const requested = options?.concurrency; + if (requested === undefined) return () => {}; + if (!positiveInteger(requested)) throw new TypeError("Summary concurrency must be a positive integer"); + const live = this.scopedConcurrency.get(requested) ?? 0; + if (live === 0) { + this.scopedConcurrency.set(requested, 1); + this.drain(); + } else { + this.scopedConcurrency.set(requested, live + 1); + } + let released = false; + return () => { + if (released) return; + released = true; + const remaining = (this.scopedConcurrency.get(requested) ?? 1) - 1; + if (remaining > 0) this.scopedConcurrency.set(requested, remaining); + else this.scopedConcurrency.delete(requested); + }; + } + + private effectiveConcurrency(): number { + let cap = this.baseConcurrency; + for (const requested of this.scopedConcurrency.keys()) if (requested > cap) cap = requested; + return cap; + } + private acquire(signal: AbortSignal): Promise { if (signal.aborted) return Promise.resolve(undefined); return new Promise((resolve) => { @@ -190,7 +275,7 @@ export class ExternalSummaryExecutor { } private drain(): void { - while (this.active < this.concurrency && this.waiting.length > 0) { + while (this.active < this.effectiveConcurrency() && this.waiting.length > 0) { const waiter = this.waiting.shift(); if (!waiter) break; waiter.signal.removeEventListener("abort", waiter.abort); diff --git a/src/preflight.ts b/src/preflight.ts index 47eb758089..f640423bce 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -55,6 +55,12 @@ const MAX_SUMMARY_OUTPUT_TOKENS = 32768; // #574: bound on upstream summarization calls per invocation — the multi-range // walk can otherwise spend a call per viable range in a block-dense history. export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16; +// #2678/#2667: how many disjoint ranges one preflight round nominates into a +// SINGLE external-summary batch call. Sized at the executor pool's default +// concurrency (src/external-summary-runtime.ts): beyond that, extra work items +// only queue inside the same pass. Ranges past the cap fold through the +// serial walk in later rounds exactly as before. +const MAX_PREFLIGHT_SUMMARY_BATCH = 4; // #1819: net-shrink monotonicity tolerance for fold acceptance. A weak // summarizer can regurgitate a verbose re-narration that EXCEEDS its own // range: the flat maxSummaryLength cap bounds absolute size only, so such a @@ -900,7 +906,10 @@ function emptyCompletionDetail(json: Record): string | null { * one opaque "failed or exceeded their budget" line. */ export function describeExternalSummaryFailure(batch: ExternalSummaryBatchResult): string { if (batch.status === "failed") return `batch=failed reason=${batch.reason}`; - const result = batch.results[0]; + // #2678: multi-range batches report the FIRST failing range, not slot 0 — + // slot 0 may have succeeded while a later range is what stopped the batch. + // Single-range callers see identical output (find falls back to results[0]). + const result = batch.results.find((entry) => entry.status !== "success") ?? batch.results[0]; if (!result) return `batch=${batch.status} (no result)`; const parts = [`batch=${batch.status}`, `result=${result.status}`]; if (result.status === "failed") parts.push(`reason=${result.reason}`); @@ -1234,7 +1243,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // The plan rides the request Config rail: deps.config is this // request's resolved Config (three-level cascade), so the plan // always matches the settings the wire path itself resolved. - deps = { ...deps, externalSummary: configuredSummaryPlan((deps.config as ResolvedKernelConfig).externalSummary) }; + deps = { ...deps, externalSummary: configuredSummaryPlan((deps.config as ResolvedKernelConfig).externalSummary, deps.session.id) }; } catch (error) { const detail = error instanceof Error ? error.message : String(error); deps.log("warn", `[external-summary] configuration unavailable; using legacy preflight: ${detail}`); @@ -1284,6 +1293,14 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa let budgetHit = false; let transientRetryBudget = TRANSIENT_EMPTY_RETRY_BUDGET; let rangesTried = 0; + // #2657×#2678 fusion: set when a round's whole external-summary batch died + // in the shared pool's queue (deadline, zero dispatched attempts). Later + // rounds would queue behind the same holders with fresh budgets each, + // serializing the whole turn behind the congestion — stop instead and let + // the next turn retry into a (hopefully) drained pool. Zero dispatched + // attempts keeps the #2662 no-backoff path intact. + let poolQueueDrop = false; + const triedKeys = new Set(); let rangesRemaining = 0; // #1933 F3: scale the depth budgets with the entry overshoot (coverage-bound // note atop the file). A raised budget is only a ceiling — well-behaved @@ -1319,6 +1336,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa failure = ABORTED_FAILURE; break; } + if (poolQueueDrop) break; // Re-run the pipeline each round: a successful compress hides its // range behind a new block, changing the visible view; refs stay // stable per-session snapshots (#387), but which ranges are @@ -1488,7 +1506,244 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa let futileBail = false; let bailRemaining = 0; let bailDeficit = 0; - for (let oi = 0; oi < ordered.length; oi++) { + // Shared fold commit for this round: applies one parsed range to the + // session state and nets its accounting, exactly as the serial walk did + // inline before #2678 hoisted it so the batch path below commits through + // the same code (one apply path, no drifted copies). Returns false when + // the kernel refused the fold (the caller records the skip and moves on). + const commitFold = (skipKey: string, startRef: string, endRef: string, summary: string, effectiveMessageIds: readonly string[], externalFold: boolean): boolean => { + const ctx: RewriteCtx = { + core: deps.core, + config: activeConfig, + messages, + session: deps.session, + log: (msg) => deps.log("info", msg), + }; + const creditBefore = deps.session.stats.compressCreditTokens; + // #2146: internal lane — must not arm the model-facing loop breaker. + const fold = parseCompressInput({ content: [{ startId: startRef, endId: endRef, summary, topic: deps.compressReason === "growth" ? "preflight auto-fold (growth)" : "preflight overflow compress" }] }); + if (externalFold) for (const range of fold.ranges) range.compressCallId = newExternalCallId(); + const applied = applyRanges(fold, ctx, { loopTracking: false }); + if (applied.outcome === "refused") { + deps.log("warn", `[preflight] ${applied.text}`); + noteSkip(`${skipKey}: apply failed — ${safePrefix(applied.text.replace(/^\[Compression FAILED[:\s]*/, ""), 200)}`); + skipSet.add(skipKey); + return false; + } + // #1993: a fold succeeded after an earlier rejection in this walk — the + // payload shrank, so the stale failure no longer describes the end + // state; clear it so a later exhaustion reports its own reason (and a + // fitting result reports none at all). + failure = undefined; + // The summary itself re-enters the payload; net its cost against + // both the folded size and the session's input baseline. Without a + // baseline currentTokens is char-based, so net the folded span's + // char count against it instead of the token-based credit. + const compressed = deps.session.stats.compressCreditTokens - creditBefore; + const folded = baselineKnown ? compressed : messages.filter((message) => effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); + currentTokens = Math.max(0, currentTokens - folded + countText(summary)); + deps.session.stats.lastInputTokens += defaultCountTokens(summary); + appliedThisRound += 1; + result.compressedRanges += 1; + return true; + }; + // #2678/#2667: fold several disjoint ranges in ONE external-summary call + // so the executor's pool runs them concurrently — the pass's wall clock + // becomes the slowest chunk instead of k sequential ~20-40s calls (the + // shared per-pass deadline used to cut multi-range passes off with whole + // ranges never dispatched). Nomination mirrors the walk below: same span + // choice (oldest full-budget chunk first), same preview/content/chunking + // gates, and a rejected preview or empty render does NOT poison the + // range's skipSet. A range the batch cannot summarize is left out of the + // skipSet on purpose: later rounds re-nominate it while siblings keep + // folding in batches, and once the batch applies nothing in a round the + // serial walk below takes it over with halving/transient retries. Gated + // on the external lane (the legacy streaming lane has no batch executor) + // and on ≥2 viable ranges (a single range keeps the legacy path + // byte-identically). + if (!emergencyFold && ordered.length >= 2 && !budgetHit) { + const batchPlan = deps.externalSummary; + if (batchPlan) { + type BatchNomination = { + skipKey: string; + startRef: string; + endRef: string; + content: string; + chunks: string[]; + instructions: string; + effectiveMessageIds: string[]; + spanUnits: number; + offset: number; + }; + const nominations: BatchNomination[] = []; + let workTotal = 0; + let reservedCalls = 0; + for (let oi = 0; oi < ordered.length && nominations.length < MAX_PREFLIGHT_SUMMARY_BATCH; oi++) { + const range = ordered[oi]; + const skipKey = `${range.startRef}:${range.endRef}`; + if (skipSet.has(skipKey)) continue; + const { refToIdx } = refMaps(messages, deps.session.state); + const startIdx = refToIdx.get(range.startRef); + const endIdx = refToIdx.get(range.endRef); + if (startIdx === undefined || endIdx === undefined || startIdx > endIdx) { + skipSet.add(skipKey); + const why = startIdx === undefined + ? `start ref ${range.startRef} is absent from the current messages` + : endIdx === undefined + ? `end ref ${range.endRef} is absent from the current messages` + : `position resolution inverted the range (${startIdx} > ${endIdx})`; + deps.log("warn", `[preflight] skipping range ${skipKey}: ${why}`); + noteSkip(`${skipKey}: ${why}`); + continue; + } + if (!triedKeys.has(skipKey)) { + triedKeys.add(skipKey); + rangesTried += 1; + } + const spans = splitChunks(messages, startIdx, endIdx, budget, baselineKnown ? 0 : minChars, countText).slice().reverse(); + const span = spans.pop(); + if (!span) continue; + const [cs, ce] = span; + const maps = refMaps(messages, deps.session.state); + let startRef = maps.idxToRef.get(cs); + let endRef = maps.idxToRef.get(ce); + if (!startRef || !endRef) { + deps.log("warn", `[preflight] dropping span ${cs}..${ce} of range ${skipKey}: boundary message has no ref in the current state`); + noteSkip(`${skipKey}: span ${cs}..${ce} boundary message has no ref`); + continue; + } + if (refNum(startRef) > refNum(endRef)) { + deps.log("warn", `[preflight] normalized reversed range ${startRef}–${endRef} → ${endRef}–${startRef} (non-monotonic refs after client history rewrite, #1001)`); + [startRef, endRef] = [endRef, startRef]; + } + const preview = deps.core.applyCompression({ + messages, + state: deps.session.state, + config: activeConfig, + ranges: [{ startRef, endRef, summary: "x".repeat(Math.max(MIN_SUMMARY_CHARS, activeConfig.compress.minSummaryLength)) }], + }); + const previousBlockIds = new Set(deps.session.state.blocks.map((block) => block.blockId)); + const planned = preview.state.blocks.find((block) => !previousBlockIds.has(block.blockId)); + if (!planned) { + const verdict = safePrefix([...preview.result.errors, ...preview.result.warnings].join("; "), 300) + || "the kernel created no block and reported no error"; + deps.log("warn", `[preflight] preview rejected range ${skipKey}: ${verdict}`); + noteSkip(`${skipKey}: preview rejected — ${verdict}`); + // No skipSet here, mirroring the walk: a rejected span (e.g. a + // sub-minimum tail) must not poison the whole range — its older + // chunks may still be foldable in later rounds. + continue; + } + // Direct raw messages render host-side (#781 image notes live in + // BiliMessage sidecars the kernel never sees); consumed child + // blocks render through the kernel from the original state. + // Byte-identical to the walk's renderer below. + const idxById = new Map(messages.map((m, i) => [m.id, i])); + const byRaw = deps.session.state?.messageRefs?.byRaw; + const parts: string[] = []; + const droppedParts: string[] = []; + for (const id of planned.directMessageIds) { + const i = idxById.get(id); + if (i === undefined) { droppedParts.push(`message ${id} not found in current messages`); continue; } + const m = messages[i]; + let text = m.text ?? ""; + const notes = imagePlaceholdersForSummary(m, byRaw?.[m.id]); + if (notes.length > 0) { + const note = notes.join(" "); + text = text === IMAGE_PLACEHOLDER ? note : text ? `${text}\n${note}` : note; + } + if (!text) { droppedParts.push(`message ${id} rendered no text`); continue; } + const label = + m.contentType === "tool-call" + ? `assistant tool-call ${m.toolName ?? "?"}` + : m.contentType === "tool-result" + ? `tool result ${m.toolName ?? "?"}` + : m.contentType === "reasoning" + ? "assistant reasoning" + : m.role; + parts.push(`[${label}]\n${text}`); + } + for (const nid of planned.directBlockIds) { + const nb = deps.session.state.blocks.find((b) => b.blockId === nid); + if (!nb) { droppedParts.push(`child block ${nid} missing from state`); continue; } + const label = nb.topic ? `${nb.blockId}: ${nb.topic}` : nb.blockId; + parts.push(`[summarized ${label}]\n${nb.summary}`); + } + if (droppedParts.length > 0) { + deps.log("debug", `[preflight] range ${skipKey}: ${droppedParts.length} part(s) rendered nothing (${droppedParts.slice(0, 3).join("; ")})`); + } + const content = parts.join("\n\n"); + if (content.length === 0) { + deps.log("warn", `[preflight] planned block for range ${skipKey} rendered no text: ${droppedParts.slice(0, 5).join("; ") || "all parts empty"}`); + noteSkip(`${skipKey}: planned block rendered no text (${droppedParts.slice(0, 3).join("; ") || "all parts empty"})`); + // No skipSet here either (walk parity): an empty render for one + // span does not make the rest of the range unrenderable. + continue; + } + const spanUnits = baselineKnown + ? planned.compressedTokens + : messages.filter((message) => planned.effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); + const chunks = splitSummaryContent(content, budget, countText); + // Track reservations locally: they count against the pass + // budget only once the batch actually fires (≥2 nominations). + // A sub-batch set falls through to the walk, which reserves + // its own calls — committing here too would double-book them. + if (summaryCalls + reservedCalls + chunks.length > summaryBudget) break; + reservedCalls += chunks.length; + // Same system prompt summarizeRange builds for the serial path + // (per-chunk length budget spread over maxSummaryLength, #1775). + const maxSummary = activeConfig.compress.maxSummaryLength; + const perChunkBudget = maxSummary > 0 ? Math.floor((maxSummary - (chunks.length - 1) * SUMMARY_JOIN_GAP) / chunks.length) : undefined; + const instructions = + buildCompressSystemPrompt(deps.prompts, deps.surface?.promptSections) + + `\n\nTASK: The conversation segment below (messages ${startRef}–${endRef}) must be compressed because the session context exceeds the current model's window. Write a tier-1 compression summary of the segment following every rule above. Output ONLY the summary text — no preamble, no closing remarks, no tool calls.` + + (perChunkBudget !== undefined && perChunkBudget >= MIN_SUMMARY_CHARS + ? `\n\nLENGTH BUDGET: Your ENTIRE response must be AT MOST ${perChunkBudget} characters total — longer output is rejected by the pipeline. Be dense: compact bullets, no filler or repetition.` + : ""); + nominations.push({ skipKey, startRef, endRef, content, chunks, instructions, effectiveMessageIds: [...planned.effectiveMessageIds], spanUnits, offset: workTotal }); + workTotal += chunks.length; + } + if (nominations.length >= 2) { + summaryCalls += reservedCalls; + const workItems = nominations.flatMap((nom) => nom.chunks.map((chunk) => ({ content: chunk, instructions: nom.instructions, minSummaryChars: MIN_SUMMARY_CHARS, maxSummaryChars: deps.config.compress.maxSummaryLength }))); + const batch = await batchPlan.summarize(workItems, deps.signal); + const assembled = nominations.map((nom): string | undefined => { + const entries = batch.results.slice(nom.offset, nom.offset + nom.chunks.length); + if (entries.length < nom.chunks.length || entries.some((entry) => entry.status !== "success")) return undefined; + const candidate = entries.map((entry) => (entry.status === "success" ? entry.summary : "")).join("\n\n"); + // Acceptance identical to the walk: absolute cap, then net- + // shrink monotonicity (#861/#1775/#1819), rescue-truncate + // when truncating still nets savings. + const shrinkOk = (text: string): boolean => countText(text) <= nom.spanUnits * NET_SHRINK_TOLERANCE; + const maxSummary = activeConfig.compress.maxSummaryLength; + if (maxSummary <= 0 || candidate.length <= maxSummary) { + return shrinkOk(candidate) ? candidate : undefined; + } + if (candidate.length < nom.content.length) { + const rescued = truncateSummaryToLimit(candidate, maxSummary, activeConfig.compress.minSummaryLength); + if (rescued !== null && shrinkOk(rescued)) { + deps.log("warn", `[preflight] range ${nom.skipKey}: assembled summary ${candidate.length} chars exceeded maxSummaryLength (${maxSummary}); truncated to ${rescued.length} chars`); + return rescued; + } + } + return undefined; + }); + const successful = assembled.filter((candidate) => candidate !== undefined).length; + deps.log("info", `[external-summary] completed (preflight) ${successful}/${nominations.length} range(s); calls=${workItems.length}; ${describeExternalSummaryFailure(batch)}`); + if (batch.status === "deadline" && batch.results.every((entry) => entry.attempts.length === 0)) { + poolQueueDrop = true; + deps.log("warn", `[external-summary] the whole batch died in the shared pool queue before a single candidate dispatched (deadline, 0 attempts) — pool congestion; stopping this fold instead of queueing further rounds, the next turn retries (#2657 × #2678 fusion)`); + } + for (let ni = 0; ni < nominations.length; ni++) { + const candidate = assembled[ni]; + if (candidate === undefined) continue; + const nom = nominations[ni]; + commitFold(nom.skipKey, nom.startRef, nom.endRef, candidate, nom.effectiveMessageIds, true); + } + } + } + } + for (let oi = 0; oi < ordered.length && appliedThisRound === 0 && !poolQueueDrop; oi++) { const range = ordered[oi]; if (decisionTokens < textTarget) break; if (deps.signal?.aborted) { @@ -1540,7 +1795,10 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa noteSkip(`${skipKey}: ${why}`); continue; } - rangesTried += 1; + if (!triedKeys.has(skipKey)) { + triedKeys.add(skipKey); + rangesTried += 1; + } // minUnits only in the char regime: with the optimistic token budget a // sub-minimum chunk is already rare, and keeping minUnits = 0 there // preserves the historical packing exactly. @@ -1825,43 +2083,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa skipSet.add(skipKey); break; } - const ctx: RewriteCtx = { - core: deps.core, - config: activeConfig, - messages, - session: deps.session, - log: (msg) => deps.log("info", msg), - }; - const creditBefore = deps.session.stats.compressCreditTokens; - // #2146: internal lane — must not arm the model-facing loop breaker. - const fold = parseCompressInput({ content: [{ startId: startRef, endId: endRef, summary, topic: deps.compressReason === "growth" ? "preflight auto-fold (growth)" : "preflight overflow compress" }] }); - // Tag external-chain folds so display surfaces (panel ⚡ext, web - // session detail) can tell them apart from model-written summaries; - // the kernel only parses compressCallId from the tool-call id, so we - // attach the marker after parsing (same as the tool path does). - if (outcome?.external || externalFold) for (const range of fold.ranges) range.compressCallId = newExternalCallId(); - const applied = applyRanges(fold, ctx, { loopTracking: false }); - if (applied.outcome === "refused") { - deps.log("warn", `[preflight] ${applied.text}`); - noteSkip(`${skipKey}: apply failed — ${safePrefix(applied.text.replace(/^\[Compression FAILED[:\s]*/, ""), 200)}`); - skipSet.add(skipKey); - break; - } - // #1993: a fold succeeded after an earlier rejection in this walk — the - // payload shrank, so the stale failure no longer describes the end - // state; clear it so a later exhaustion reports its own reason (and a - // fitting result reports none at all). - failure = undefined; - // The summary itself re-enters the payload; net its cost against - // both the folded size and the session's input baseline. Without a - // baseline currentTokens is char-based, so net the folded span's - // char count against it instead of the token-based credit. - const compressed = deps.session.stats.compressCreditTokens - creditBefore; - const folded = baselineKnown ? compressed : messages.filter((message) => planned.effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); - currentTokens = Math.max(0, currentTokens - folded + countText(summary)); - deps.session.stats.lastInputTokens += defaultCountTokens(summary); - appliedThisRound += 1; - result.compressedRanges += 1; + commitFold(skipKey, startRef, endRef, summary, planned.effectiveMessageIds, outcome?.external || externalFold); break; } if (appliedThisRound > 0) break; @@ -1906,9 +2128,11 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } else if (relaxed && result.compressedRanges > 0) { failure = { kind: "exhausted", detail: `${relaxedExhaustedDetail}${skipNote}` }; } else if (result.compressedRanges === 0) { - const cause = skipReasons.length > 0 - ? skipReasons.slice(0, 3).join(" | ") - : "each was below minCompressRange, had an unusable summary, or failed to apply"; + const cause = poolQueueDrop + ? "the shared summary pool was saturated and no summary call was ever dispatched (scheduler congestion, not a chain failure; auto-fold retries next turn)" + : skipReasons.length > 0 + ? skipReasons.slice(0, 3).join(" | ") + : "each was below minCompressRange, had an unusable summary, or failed to apply"; failure = { kind: "exhausted", detail: rangesTried === 0 ? `no viable range could be compressed: ${cause}${unusableNote}` : `no range could be compressed across ${rangesTried} viable range${rangesTried === 1 ? "" : "s"}: ${cause}${unusableNote}` }; diff --git a/src/web/client.ts b/src/web/client.ts index d7fabd7a7c..a25f92dac2 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -2138,6 +2138,10 @@ export const WEB_CLIENT = `(function () { field(budget, "summary-" + key, label, s.budget && s.budget[key] || fallback, (value) => mutate((v) => { if (!v.budget) v.budget = {}; v.budget[key] = value; }), "number").disabled = !!cfg.parseError; }); box.appendChild(budget); + const pool = document.createElement("div"); pool.className = "summary-grid"; + const conc = field(pool, "summary-concurrency", t("summary.concurrency"), s.concurrency, (value) => mutate((v) => { v.concurrency = value; }), "number"); + conc.max = "32"; conc.disabled = !!cfg.parseError; + box.appendChild(pool); } render(); fe.onchange = render; diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 234bf3cf46..6b823204f6 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -19,6 +19,7 @@ export const MESSAGES: Record> = { "summary.total": "总时限 (ms)", "summary.timeout": "单目标时限 (ms)", "summary.bytes": "摘要上限 (bytes)", + "summary.concurrency": "并发池大小 (1-32,默认 4)", "summary.up": "上移", "summary.down": "下移", "summary.remove": "移除目标", @@ -454,6 +455,7 @@ export const MESSAGES: Record> = { "summary.total": "Total deadline (ms)", "summary.timeout": "Per-target timeout (ms)", "summary.bytes": "Summary limit (bytes)", + "summary.concurrency": "Pool size (1-32, default 4)", "summary.up": "Move up", "summary.down": "Move down", "summary.remove": "Remove target", diff --git a/tests/external-summary-batch.test.ts b/tests/external-summary-batch.test.ts index 201e8043d6..b35195b3a1 100644 --- a/tests/external-summary-batch.test.ts +++ b/tests/external-summary-batch.test.ts @@ -30,11 +30,18 @@ test("external summary batch: each range uses the ordered fallback chain", async const result = await new ExternalSummaryExecutor(2).executeBatch(work, [ { summarize: async (request) => { calls.push(`primary:${request.content}`); throw new Error("401"); } }, { summarize: async (request) => { calls.push(`backup:${request.content}`); return "backup summary"; } }, - { summarize: async () => { assert.fail("successful backup stops the chain"); } }, + { summarize: async () => assert.fail("successful backup stops the chain") }, ], budget); assert.equal(result.status, "finished"); assert.deepEqual(result.results.map((range) => range.status === "success" ? range.targetIndex : -1), [1, 1, 1]); - assert.deepEqual(calls, work.flatMap((range) => [`primary:${range.content}`, `backup:${range.content}`])); + // #2667: ranges now run concurrently up to the pool size, so CROSS-range + // call order is no longer pinned (it was an artifact of the old serial + // loop). What stays load-bearing is the PER-RANGE ordered chain — primary + // before backup within each range, and a successful backup stopping the + // chain before the third target. + for (const content of work.map((range) => range.content)) { + assert.deepEqual(calls.filter((call) => call.endsWith(`:${content}`)), [`primary:${content}`, `backup:${content}`]); + } }); test("external summary batch: one deadline covers all ranges, not one renewed budget per range", async () => { @@ -144,6 +151,103 @@ test("external summary batch: event-loop stalls cannot accept late success or di assert.equal(calls, 1); }); +async function waitFor(flag: () => boolean, what: string): Promise { + for (let i = 0; i < 1000; i++) { + if (flag()) return; + await new Promise((resolve) => setImmediate(resolve)); + } + assert.fail(`timed out waiting for ${what}`); +} + +test("external summary batch: a scoped pool admits concurrent work while it is in flight (#2657)", async () => { + const executor = new ExternalSummaryExecutor(1); + let releaseA!: () => void; + const gateA = new Promise((resolve) => { releaseA = resolve; }); + let aStarted = false; + let bStarted = false; + const candidateA: SummaryCandidate = { summarize: async () => { aStarted = true; await gateA; return "A"; } }; + const candidateB: SummaryCandidate = { summarize: async () => { bStarted = true; return "B"; } }; + try { + const pendingA = executor.execute(work[0], [candidateA], budget, undefined, { concurrency: 4 }); + await waitFor(() => aStarted, "A to start"); + // Base pool is 1, yet B must be admitted by A's in-flight scope of 4. + const pendingB = executor.execute(work[1], [candidateB], budget); + await waitFor(() => bStarted, "B to start while A holds its slot"); + releaseA(); + assert.equal((await pendingA).status, "success"); + assert.equal((await pendingB).status, "success"); + } finally { + releaseA(); + } +}); + +test("external summary batch: the pool returns to base size once the scoped work settles (#2657)", async () => { + const executor = new ExternalSummaryExecutor(1); + let releaseA!: () => void; + const gateA = new Promise((resolve) => { releaseA = resolve; }); + let aStarted = false; + const candidateA: SummaryCandidate = { summarize: async () => { aStarted = true; await gateA; return "A"; } }; + try { + const pendingA = executor.execute(work[0], [candidateA], budget, undefined, { concurrency: 4 }); + await waitFor(() => aStarted, "A to start"); + releaseA(); + assert.equal((await pendingA).status, "success"); + // With A settled the pool is back to base 1: B must queue behind C. + let releaseC!: () => void; + const gateC = new Promise((resolve) => { releaseC = resolve; }); + let cStarted = false; + let bStarted = false; + const candidateC: SummaryCandidate = { summarize: async () => { cStarted = true; await gateC; return "C"; } }; + const candidateB: SummaryCandidate = { summarize: async () => { bStarted = true; return "B"; } }; + try { + const pendingC = executor.execute(work[2], [candidateC], budget); + await waitFor(() => cStarted, "C to start"); + const pendingB = executor.execute(work[1], [candidateB], budget); + // B must stay queued for every tick until C releases the only permit. + for (let i = 0; i < 50 && !bStarted; i++) await new Promise((resolve) => setImmediate(resolve)); + assert.equal(bStarted, false); + releaseC(); + await waitFor(() => bStarted, "B to start after C released"); + assert.equal((await pendingB).status, "success"); + assert.equal((await pendingC).status, "success"); + } finally { + releaseC(); + } + } finally { + releaseA(); + } +}); + +test("external summary batch: an oversized scope yields to a larger in-flight demand (#2657)", async () => { + const executor = new ExternalSummaryExecutor(1); + let releaseA!: () => void; + const gateA = new Promise((resolve) => { releaseA = resolve; }); + let aStarted = false; + let bStarted = false; + const candidateA: SummaryCandidate = { summarize: async () => { aStarted = true; await gateA; return "A"; } }; + const candidateB: SummaryCandidate = { summarize: async () => { bStarted = true; return "B"; } }; + try { + const pendingA = executor.execute(work[0], [candidateA], budget, undefined, { concurrency: 8 }); + await waitFor(() => aStarted, "A to start"); + const pendingB = executor.execute(work[1], [candidateB], budget, undefined, { concurrency: 3 }); + await waitFor(() => bStarted, "B to start under the larger in-flight demand"); + releaseA(); + assert.equal((await pendingA).status, "success"); + assert.equal((await pendingB).status, "success"); + } finally { + releaseA(); + } +}); + +test("external summary batch: an invalid pool scope rejects without dispatch (#2657)", async () => { + const executor = new ExternalSummaryExecutor(1); + const candidate: SummaryCandidate = { summarize: async () => assert.fail("must not dispatch") }; + // Non-positive values reject before dispatch; the 1..32 product range is + // owned by the settings layer, the executor takes any positive integer. + await assert.rejects(executor.execute(work[0], [candidate], budget, undefined, { concurrency: 0 }), TypeError); + await assert.rejects(executor.executeBatch(work, [candidate], budget, undefined, { concurrency: -1 }), TypeError); +}); + test("external summary batch: invalid operation plans fail without dispatch", async () => { const executor = new ExternalSummaryExecutor(1); const candidates = [{ summarize: async () => assert.fail("must not dispatch") }]; diff --git a/tests/external-summary-executor-parallel.test.ts b/tests/external-summary-executor-parallel.test.ts new file mode 100644 index 0000000000..c52e71a016 --- /dev/null +++ b/tests/external-summary-executor-parallel.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { ExternalSummaryExecutor, type SummaryCandidate, type SummaryWork } from "../src/external-summary.ts"; + +// #2667: executeBatch used to serialize every range regardless of pool size — +// these pin the bound-parallel contract: at most `concurrency` ranges in +// flight, results stay index-aligned with the work array (dense prefix), and +// an early stop (caller abort / shared deadline) leaves unstarted ranges +// undispatched rather than fabricated. + +const budget = { totalTimeoutMs: 1000, targetTimeoutMs: 500, maxSummaryBytes: 4096 }; +const work: SummaryWork[] = [ + { content: "range one", instructions: "Preserve constraints" }, + { content: "range two", instructions: "Preserve errors" }, + { content: "range three", instructions: "Preserve paths" }, +]; +const flush = (): Promise => new Promise((resolve) => setImmediate(resolve)); + +test("executor batch: a batch runs up to the pool size concurrently and never beyond", async () => { + let inFlight = 0; + let maxInFlight = 0; + const started: string[] = []; + const gates: Array<() => void> = []; + const candidate: SummaryCandidate = { summarize: (request) => new Promise((resolve) => { + started.push(request.content); + inFlight += 1; + maxInFlight = Math.max(maxInFlight, inFlight); + gates.push(() => { + inFlight -= 1; + resolve(`s:${request.content}`); + }); + }) }; + const pending = new ExternalSummaryExecutor(2).executeBatch(work, [candidate], budget); + await flush(); + await flush(); + assert.deepEqual(started, ["range one", "range two"], "the pool admits exactly two ranges before any settles"); + assert.equal(maxInFlight, 2); + gates.shift()!(); + await flush(); + assert.deepEqual(started, ["range one", "range two", "range three"], "the third range starts only after a slot frees"); + assert.equal(maxInFlight, 2, "in-flight never exceeds the pool size"); + while (gates.length > 0) gates.shift()!(); + const result = await pending; + assert.equal(result.status, "finished"); + assert.deepEqual(result.results.map((range) => range.status === "success" ? range.summary : null), [ + "s:range one", "s:range two", "s:range three", + ]); +}); + +test("executor batch: results stay index-aligned when completion order differs from work order", async () => { + const candidate: SummaryCandidate = { summarize: (request) => request.content === "range one" + ? new Promise((resolve) => setTimeout(() => resolve(`s:${request.content}`), 30)) + : Promise.resolve(`s:${request.content}`) }; + const result = await new ExternalSummaryExecutor(2).executeBatch(work, [candidate], budget); + assert.equal(result.status, "finished"); + assert.deepEqual(result.results.map((range) => range.status === "success" ? range.summary : null), + work.map((range) => `s:${range.content}`), "results[i] reports work[i] regardless of who finished first"); +}); + +test("executor batch: caller cancellation stops unstarted ranges and keeps the dense prefix aligned", async () => { + const controller = new AbortController(); + // Both admitted ranges stay in flight (signal-ignoring, resolve at 50ms) + // until the abort lands at 20ms — so no worker is idle to claim the third + // index before the stop latch trips. Their late successes must be rejected. + let dispatches = 0; + const candidate: SummaryCandidate = { summarize: () => new Promise((resolve) => { + dispatches += 1; + if (dispatches === 1) setTimeout(() => controller.abort(), 20); + setTimeout(() => resolve("late"), 50); + }) }; + const result = await new ExternalSummaryExecutor(2).executeBatch(work, [candidate], budget, controller.signal); + assert.equal(result.status, "cancelled"); + assert.equal(result.results.length, 2, "dense prefix: only the two ranges in flight at abort time are reported"); + for (const range of result.results) assert.notEqual(range.status, "success", "late successes after the abort are rejected"); + // The stop latch must have prevented the third range from ever starting: + // give the event loop a full turn past the abort and count real dispatches. + await new Promise((resolve) => setTimeout(resolve, 80)); + assert.equal(dispatches, 2, `exactly the two pool slots dispatched, never the third range (got ${dispatches})`); +}); + +test("executor batch: one range hitting the shared deadline stops unstarted ranges", async (t) => { + let elapsed = 0; + t.mock.method(performance, "now", () => elapsed); + let calls = 0; + const candidate: SummaryCandidate = { summarize: (request) => { + calls += 1; + if (calls === 1) { + elapsed = 80; + return Promise.resolve("slow ok"); + } + elapsed = 150; + return Promise.resolve("too late"); + } }; + const result = await new ExternalSummaryExecutor(1).executeBatch(work, [candidate], { ...budget, totalTimeoutMs: 100 }); + assert.equal(result.status, "deadline"); + assert.deepEqual(result.results.map((range) => range.status), ["success", "deadline"]); + assert.equal(calls, 2, "the third range never dispatched once the shared deadline passed"); +}); diff --git a/tests/external-summary-settings.test.ts b/tests/external-summary-settings.test.ts index 93eba1ac1f..0979f4b369 100644 --- a/tests/external-summary-settings.test.ts +++ b/tests/external-summary-settings.test.ts @@ -317,6 +317,34 @@ test("autoFold validation: boolean only, target within [8192, 10M]", () => { assert.equal(off.autoFold, undefined); }); +test("concurrency rides both the chain form and the expanded rail form (#2657)", () => { + const chain = parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"], concurrency: 12 }); + assert.equal(chain.concurrency, 12); + const plan = expandExternalSummaryChain(chain, recipes); + assert.equal(plan.concurrency, 12); + assert.doesNotThrow(() => parseExternalSummarySettings(plan)); + // Absent → undefined → the executor keeps its base size. + const lazy = expandExternalSummaryChain(parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"] }), recipes); + assert.equal(lazy.concurrency, undefined); + // Whole-chain replace carries it like every other field. + const merged = mergeCompress({ externalSummary: { ...chain, targets: ["remote/resp-model"] } }, {})?.externalSummary; + assert.equal(merged?.concurrency, 12); +}); + +test("concurrency validation: integer within [1, 32] on both forms (#2657)", () => { + assert.doesNotThrow(() => parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"], concurrency: 1 })); + assert.doesNotThrow(() => parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"], concurrency: 32 })); + const plan = expandExternalSummaryChain(parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"] }), recipes); + for (const bad of [0, 33, -4, 2.5, "12"]) { + assert.throws(() => parseExternalSummaryChain({ enabled: true, targets: ["glm/glm-4.9-flash"], concurrency: bad }), /concurrency must be an integer between 1 and 32/, JSON.stringify(bad)); + assert.throws(() => parseExternalSummarySettings({ ...plan, concurrency: bad }), /concurrency must be an integer between 1 and 32/, JSON.stringify(bad)); + } + // A disabled chain never reads the rest of the object (early return). + const off = parseExternalSummaryChain({ enabled: false, targets: [], concurrency: "junk" as unknown as number }); + assert.equal(off.enabled, false); + assert.equal(off.concurrency, undefined); +}); + test("autoFold set on a disabled chain warns instead of silently no-oping", () => { const warns: string[] = []; setLogCapture((level, msg) => { if (level === "warn") warns.push(msg); }); diff --git a/tests/fork-adoption-wires.test.ts b/tests/fork-adoption-wires.test.ts index 67220544d3..97d01a8d06 100644 --- a/tests/fork-adoption-wires.test.ts +++ b/tests/fork-adoption-wires.test.ts @@ -1,15 +1,30 @@ import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import { listSessions } from "../src/session.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; import type { ProxyOptions } from "../src/config.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { startChatRelay } from "./e2e/chat-relay.ts"; +// Hermetic state dirs before any scenario boots a proxy (#2806 review): these +// anonymous-fork scenarios reattach through prefix-affinity.json, and without +// isolation every chain accumulated in the shared default state dir by sibling +// test files (and prior runs) hydrates into this process while those writers +// keep churning the same file underneath — cross-writer interference that +// flipped the fork/anchor assertions on CI once the suite grew heavier. Same +// idiom as tests/prefix-affinity-orphan.test.ts (#2719) and +// tests/abort-rewind-cache-seam.test.ts (#1613). +const hermeticRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-2806-forkwires-")); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = hermeticRoot; +test.after(() => fs.rmSync(hermeticRoot, { recursive: true, force: true })); + /** * #629 anonymous fork block-adoption, per wire. fork-adoption.test.ts proves * the whole lane (grow → proxy fold → mid-list edit → fork mint → seeded @@ -478,6 +493,11 @@ test("forkAdoption disabled leaves the responses fork at zero compression state" const run = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); + // Sibling scenarios above leave adopted forked children in the process-wide + // session table (#2806 review): the assertion below scans EVERY forked + // session, so reset the table to this scenario's scope — same idiom as + // fork-side-request-lane.test.ts / issue2407-google-khat-lane.test.ts. + _resetSessionsForTest(); const state: RelayState = { upstreamReqs: [], compressed: false }; const relay = startMockUpstream(state); await listen(relay); diff --git a/tests/issue2033-anonymous-anchor.test.ts b/tests/issue2033-anonymous-anchor.test.ts index f9bff560c0..62b7ff6e91 100644 --- a/tests/issue2033-anonymous-anchor.test.ts +++ b/tests/issue2033-anonymous-anchor.test.ts @@ -1,10 +1,24 @@ import assert from "node:assert/strict"; import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; import { once } from "node:events"; import test from "node:test"; process.env.NODE_ENV = "test"; +// Hermetic state dirs (#2806 review): A1/A2 drive genuine ANONYMOUS clients — +// prefix affinity mints pfa-* sessions and persists them to +// prefix-affinity.json in the shared default state dir. Without isolation the +// scenarios hydrate and co-write the chains of every sibling test file running +// concurrently in the same CI job (dozens of foreign chains showed up in the +// suites' own hydration logs), which flipped the anchor assertions. Same idiom +// as tests/prefix-affinity-orphan.test.ts (#2719). +const hermeticRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-2806-anonanchor-")); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = hermeticRoot; +test.after(() => fs.rmSync(hermeticRoot, { recursive: true, force: true })); + // #2033: effectiveTokenCount took the anonymous-prefix-affinity branch BEFORE // checking lastUsageGradeTokens (src/server.ts), so an anonymous session that // held a REAL usage anchor but whose latest turn wrote an estimate-grade diff --git a/tests/issue2639-ext-summary-fresh-budget.test.ts b/tests/issue2639-ext-summary-fresh-budget.test.ts new file mode 100644 index 0000000000..fa051ceb2d --- /dev/null +++ b/tests/issue2639-ext-summary-fresh-budget.test.ts @@ -0,0 +1,75 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { test } from "node:test"; +import type { NamedProviderRecipe } from "../src/config.js"; +import { _liveUpstreamTimersForTest } from "../src/fetch-util.js"; +import { expandExternalSummaryChain, parseExternalSummaryChain } from "../src/external-summary-settings.js"; +import { ConfiguredSummaryPlan } from "../src/external-summary-runtime.js"; +import type { SummaryWork } from "../src/external-summary.js"; + +process.env.NODE_ENV = "test"; + +// #2639 — the plan baked its budget as ONE construction-time wall-clock point and +// capped every summarize() against it (`remaining <= 0 -> { status: "deadline", +// results: [] }`). Preflight reuses one plan across every range/round, so after +// ~totalTimeoutMs of folding the point went stale and every later range returned +// "deadline (no result)" without reaching the summarizer. Each summarize() call is +// an independent batch and must get the FULL configured budget fresh. +const WORK: readonly SummaryWork[] = [{ + content: "Prior evidence and decisions", + reference: "Read-only current task", + instructions: "Preserve unfinished objectives", +}]; +const SUMMARY_TEXT = "Retained decisions, evidence, constraints and unfinished objectives."; +const BUDGET = { totalTimeoutMs: 2000, targetTimeoutMs: 1000, maxSummaryBytes: 65536 }; +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +test("#2639: reusing one plan across summarize() calls gives each fold a fresh budget (no starvation)", async (t) => { + t.after(() => assert.equal(_liveUpstreamTimersForTest(), 0)); + let requests = 0; + const server = http.createServer((req, res) => { + void (async () => { + const chunks: Buffer[] = []; + for await (const chunk of req) chunks.push(Buffer.from(chunk)); + requests += 1; + res.setHeader("content-type", "application/json"); + res.end(JSON.stringify({ status: "completed", output_text: SUMMARY_TEXT })); + })().catch(() => { res.writeHead(500); res.end(); }); + }); + await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); }); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + try { + const recipes: Record = { + mock: { baseUrl: `http://127.0.0.1:${address.port}`, api: "responses", apiKey: "test-summary-key", models: { "mock-model": {} } }, + }; + const settings = expandExternalSummaryChain( + parseExternalSummaryChain({ enabled: true, targets: ["mock/mock-model"], budget: BUDGET }), + recipes, + ); + // ONE plan reused across many calls — exactly how preflight drives it. + const plan = new ConfiguredSummaryPlan(settings); + + // Round 1: a fold right after construction reaches the summarizer. + const first = await plan.summarize(WORK); + assert.equal(first.status, "finished", "the first fold's batch completes"); + assert.equal(first.results[0]?.status, "success", "the first range was summarized"); + assert.equal(requests, 1, "the first fold dispatched"); + + // Elapse PAST the whole batch budget while the SAME plan sits idle — + // the preflight case where earlier rounds consumed wall-clock time. + await sleep(BUDGET.totalTimeoutMs + 500); + + // Round 2: pre-fix this hit `remaining <= 0` against the stale + // construction-time deadline and returned { status: "deadline", results: + // [] } ("no result") WITHOUT dispatching. Post-fix each call gets a fresh + // full budget, so the range is summarized normally. + const second = await plan.summarize(WORK); + assert.notEqual(second.status, "deadline", "a later fold must not be harvested by a stale construction-time deadline (#2639)"); + assert.equal(second.status, "finished", "the later fold's batch completes"); + assert.equal(second.results[0]?.status, "success", "the later range was summarized, not dropped"); + assert.equal(requests, 2, "the later range was dispatched, not harvested by a stale deadline"); + } finally { + await new Promise((resolve) => { server.close(() => resolve()); server.closeAllConnections(); }); + } +}); diff --git a/tests/issue2678-preflight-batch-fold.test.ts b/tests/issue2678-preflight-batch-fold.test.ts new file mode 100644 index 0000000000..e55e544e6b --- /dev/null +++ b/tests/issue2678-preflight-batch-fold.test.ts @@ -0,0 +1,286 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #2678/#2667: the preflight fold pass used to submit ONE external-summary +// request per range, sequentially awaited inside the session lock — a k-range +// pass cost k × (20-40s) of wall clock and hit the shared 180s deadline with +// whole ranges never dispatched. These E2E tests drive the real proxy with the +// external-summary lane armed and pin the new shape: several disjoint ranges +// are nominated into ONE summarize() call whose work items the executor's pool +// runs CONCURRENTLY (measured as overlapping request intervals on the summary +// upstream), a batch-failed range is left out of the skipSet so later rounds +// retry it (re-nominated into later batches while siblings fold; the serial +// walk takes over with halving once the batch applies nothing in a round) +// instead of dropping it silently, and nomination picks the OLDEST full-budget +// chunk of a multi-chunk range (the walk's first choice) without poisoning the +// rest of the range on a rejected sub-minimum tail. + +const SUMMARY_TEXT = + "PREFLIGHT BATCH SUMMARY: the segment covered iterative debugging of a proxy pipeline. " + + "Decisions: batch nomination over per-range serialization because the pass deadline bounds total wall clock. " + + "Files touched: src/preflight.ts:1487, src/external-summary.ts:76. Outcome: folded and verified."; + +function okSse(inputTokens: number): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: inputTokens } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +function conversation(n: number, tag: string): Array<{ role: string; content: string }> { + const msgs: Array<{ role: string; content: string }> = []; + for (let i = 0; i < n; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: `${tag} Message ${i}. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return msgs; +} + +type Interval = { start: number; end: number; body: string }; + +function startMainUpstream(): Promise<{ server: http.Server; url: string }> { + return new Promise((resolve) => { + const server = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse(1000)); + }); + server.listen(0, "127.0.0.1", () => resolve({ server, url: `http://127.0.0.1:${(server.address() as { port: number }).port}` })); + }); +} + +/** External summary upstream: records [start,end] per request (for overlap + * measurement) and holds each request briefly so concurrent dispatch is + * observable; failMark makes every request containing it answer HTTP 500. */ +function startSummaryUpstream(failMark?: string): Promise<{ server: http.Server; url: string; intervals: Interval[] }> { + return new Promise((resolve) => { + const intervals: Interval[] = []; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + const start = Date.now(); + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + const finish = (): void => { + intervals.push({ start, end: Date.now(), body }); + if (failMark !== undefined && body.includes(failMark)) { + res.writeHead(500, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "boom" })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ choices: [{ message: { content: SUMMARY_TEXT }, finish_reason: "stop" }] })); + }; + setTimeout(finish, 50); + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ server, url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, intervals })); + }); +} + +function maxOverlap(intervals: Interval[]): number { + let peak = 0; + let live = 0; + const points = intervals.flatMap((iv) => [[iv.start, 1], [iv.end, -1]] as const); + points.sort((a, b) => a[0] - b[0] || a[1] - b[1]); + for (const [, delta] of points) { + live += delta; + peak = Math.max(peak, live); + } + return peak; +} + +async function startProxy(mainUrl: string, sumUrl: string, context: number): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + process.env.E2E_BATCH_SUM_KEY = "k"; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: mainUrl, + routes: { [mainUrl]: { models: { "claude-small": { context } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000, { compress: { minCompressRange: 100, maxSummaryLength: 20000, minSummaryLength: 50 } }), + compress: { + injectTool: true, + injectNudge: true, + externalSummary: { enabled: true, targets: ["sum/sm"] }, + }, + namedProviders: { sum: { baseUrl: sumUrl, api: "openai", apiKeyEnv: "E2E_BATCH_SUM_KEY", models: { sm: {} } } }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return proxy; +} + +test("#2678: several preflight ranges fold in ONE external-summary batch that runs concurrently", async () => { + const main = await startMainUpstream(); + const sum = await startSummaryUpstream(); + // ~20k-token payload vs a 12k window: comfortably over, far below the + // emergency-digest coverage bound, and long enough to expose several + // viable ranges in the first round (cf. preflight-multi-range sizing). + const proxy = await startProxy(main.url, sum.url, 12_000); + try { + const r = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${main.url}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "batch-fold-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: conversation(40, "t1-batch") }), + }); + assert.equal(r.status, 200, "the over-window turn must be folded and forwarded"); + await r.text(); + + assert.ok(sum.intervals.length >= 2, `expected at least two summary work items (got ${sum.intervals.length})`); + assert.ok(maxOverlap(sum.intervals) >= 2, + `work items must run concurrently inside the executor pool (max overlap ${maxOverlap(sum.intervals)} — the old serial loop never overlaps)`); + + const s = listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + assert.ok(activeBlocks.length >= 2, `the batch must apply several ranges before re-judging (got ${activeBlocks.length} block(s))`); + } finally { + proxy.close(); + await once(proxy, "close"); + main.server.close(); + await once(main.server, "close"); + sum.server.close(); + await once(sum.server, "close"); + } +}); + +test("#2678: a batch-failed range is retried across later rounds instead of dropped silently", async () => { + const main = await startMainUpstream(); + // The oldest range's content carries MARKER_0_, so every summarization of + // it (batch AND walk retries) answers 500 while the rest succeed. + const sum = await startSummaryUpstream("MARKER_0_"); + const proxy = await startProxy(main.url, sum.url, 12_000); + try { + const r = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${main.url}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "batch-fallback-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: conversation(40, "t2-fallback") }), + }); + assert.equal(r.status, 200, "one poisoned range must not brick the turn — the rest fold and the payload fits"); + await r.text(); + + const s = listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + assert.ok(activeBlocks.length >= 1, `healthy batch ranges must still apply (got ${activeBlocks.length} block(s))`); + // While siblings keep folding, the failed range is re-nominated into + // LATER batches (the serial walk takes over only once the batch applies + // nothing in a round) — a drop-silently failure mode would show exactly + // one attempt. + const failedAttempts = sum.intervals.filter((iv) => iv.body.includes("MARKER_0_")).length; + assert.ok(failedAttempts >= 2, `the failed range must be retried beyond its batch attempt (got ${failedAttempts} attempt(s))`); + } finally { + proxy.close(); + await once(proxy, "close"); + main.server.close(); + await once(main.server, "close"); + sum.server.close(); + await once(sum.server, "close"); + } +}); + +test("#2678: nomination picks the oldest chunk of a multi-chunk range without poisoning it", async () => { + const main = await startMainUpstream(); + const sum = await startSummaryUpstream(); + const proxy = await startProxy(main.url, sum.url, 12_000); + try { + // Four normal alternating messages, then a long SAME-ROLE assistant run + // (five ~10k-char messages, one ~40k-char message that overflows the + // 0.6×12k chunk budget on its own, one sub-minimum ~50-char message), + // then four normal tail messages. The kernel packs the big range into + // several chronological chunks whose NEWEST chunk is a sub-minimum tail. + const msgs: Array<{ role: string; content: string }> = []; + for (let i = 0; i < 4; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: `t3-span Message ${i}. ` + `MARKER_${i}_content_`.repeat(250) }); + } + for (let i = 4; i <= 8; i++) { + msgs.push({ role: "assistant", content: `t3-span run ${i}. ` + `MARKER_${i}_run_`.repeat(769) }); + } + msgs.push({ role: "assistant", content: `t3-span big 9. ` + `MARKER_9_big_`.repeat(3333) }); + msgs.push({ role: "assistant", content: `tiny 10. ` + `MARKER_10_tail_`.repeat(3) }); + for (let i = 11; i <= 14; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: `t3-span tail ${i}. ` + `MARKER_${i}_content_`.repeat(250) }); + } + const r = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${main.url}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "batch-span-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: msgs }), + }); + assert.equal(r.status, 200, "the over-window turn must be folded and forwarded"); + await r.text(); + + // The first dispatch wave must carry the OLDEST full-budget chunk of the + // big range (MARKER_4_ sits in it). The tail-first selection named the + // sub-minimum newest chunk, had its preview rejected, and poisoned the + // whole range — so under that selection no batch ever contained + // MARKER_4_. + const t0 = Math.min(...sum.intervals.map((iv) => iv.start)); + assert.ok(sum.intervals.some((iv) => iv.body.includes("MARKER_4_") && iv.start - t0 <= 10), + `the first batch wave must include the oldest chunk of the multi-chunk range (first bodies: ${sum.intervals.slice(0, 4).map((iv) => iv.body.slice(0, 60)).join(" | ")})`); + + const s = listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + // Big range: four foldable chunks plus the tail range — at least five + // blocks. A poisoned big range yields only the single tail block. + assert.ok(activeBlocks.length >= 5, `the multi-chunk range must keep folding chunk by chunk (got ${activeBlocks.length} block(s))`); + } finally { + proxy.close(); + await once(proxy, "close"); + main.server.close(); + await once(main.server, "close"); + sum.server.close(); + await once(sum.server, "close"); + } +}); + +test("#2678: a batch-failed range is retried in later rounds while siblings keep folding", async () => { + const main = await startMainUpstream(); + const sum = await startSummaryUpstream("MARKER_0_"); + const proxy = await startProxy(main.url, sum.url, 12_000); + try { + const r = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${main.url}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "batch-retry-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: conversation(40, "t4-retry") }), + }); + assert.equal(r.status, 200, "one poisoned range must not brick the turn — the rest fold and the payload fits"); + await r.text(); + + const s = listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + assert.ok(activeBlocks.length >= 2, `healthy batch ranges must still apply across rounds (got ${activeBlocks.length} block(s))`); + // The failed range is re-nominated into later batches every round until + // the pass ends — at least three attempts, not the single one a + // drop-silently or poison-once mode would show. + const failedAttempts = sum.intervals.filter((iv) => iv.body.includes("MARKER_0_")).length; + assert.ok(failedAttempts >= 3, `the failed range must be retried across multiple rounds (got ${failedAttempts} attempt(s))`); + } finally { + proxy.close(); + await once(proxy, "close"); + main.server.close(); + await once(main.server, "close"); + sum.server.close(); + await once(sum.server, "close"); + } +}); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index e7a2180c67..d130726b68 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -152,6 +152,48 @@ export const WIRE_RULES: readonly WireRule[] = [ }, ]; +// --------------------------------------------------------------------------- +// Header-level upstream constraints (#2760). Orthogonal to the per-protocol +// BODY validators below: these bind on HTTP headers at a specific upstream +// host, not on the request body. Each rule has a matching header validator + +// validation-parity fake (tests/wire-contract-go-session.test.ts), checked by +// the ledger self-check in tests/wire-contract.test.ts. The ledger grows. +// --------------------------------------------------------------------------- + +interface WireHeaderRule { + readonly id: string; + readonly summary: string; + readonly provenance: string; +} + +export const WIRE_HEADER_RULES: readonly WireHeaderRule[] = [ + { + id: "WC-016", + summary: "OpenCode Go (/zen/go) rejects any request lacking a stable x-opencode-session header — HTTP 400 MissingSessionID; bili's independent summary calls must stamp it plus bili's own user agent, derived from bili's own conversation id, never copied from the client's inbound session/auth headers", + provenance: "#2760: user-reported production 400 against https://opencode.ai/zen/go/v1/chat/completions (real-call A/B, 2026-10-11) — a non-streaming Go summary call returns HTTP 400 MissingSessionID until a stable x-opencode-session is added; stream=true does NOT help. Official contract https://opencode.ai/docs/go/#where-can-i-use-it requires typical coding-agent traffic, the client's own user agent, and a per-conversation stable x-opencode-session for routing + prompt cache.", + }, +]; + +/** The exact upstream error Go answers when x-opencode-session is absent (#2760). */ +export function goMissingSessionIdEnvelope(): { status: number; body: string } { + return { + status: 400, + body: JSON.stringify({ + error: { type: "MissingSessionID", message: "Request is missing x-opencode-session and cannot be routed efficiently. Please see https://opencode.ai/docs/go/#where-can-i-use-it" }, + }), + }; +} + +/** WC-016: models OpenCode Go's strictest header validation — a non-empty + * x-opencode-session is mandatory. Apply only to requests destined for Go. */ +export function validateGoSessionHeaders(headers: Record): string[] { + const value = headers["x-opencode-session"]; + const text = Array.isArray(value) ? value[0] : value; + if (typeof text !== "string" || text.trim().length === 0) + return ["WC-016 OpenCode Go requires a stable x-opencode-session header (HTTP 400 MissingSessionID, #2760)"]; + return []; +} + const ANTHROPIC_TOOL_NAME_RE = /^[a-zA-Z0-9_-]{1,128}$/; const OPENAI_TOOL_NAME_RE = /^[a-zA-Z0-9_-]{1,64}$/; const GOOGLE_FN_NAME_RE = /^[a-zA-Z0-9_]+$/; diff --git a/tests/wire-contract-go-session.test.ts b/tests/wire-contract-go-session.test.ts new file mode 100644 index 0000000000..221268a511 --- /dev/null +++ b/tests/wire-contract-go-session.test.ts @@ -0,0 +1,151 @@ +// WC-016 (#2760) gates: OpenCode Go requires a stable x-opencode-session on every +// independent summary call, else HTTP 400 MissingSessionID. These drive the REAL +// summary path (candidate / compress-tool lane / preflight+auto-fold lane) against +// an in-memory validation-parity fake standing in for opencode.ai/zen/go, proving +// the header rides the outbound request, is stable across a batch, isolated across +// sessions, and never invented off-Go or without a conversation id. +import assert from "node:assert/strict"; +import { afterEach, test } from "node:test"; +import { createCore, defaultConfig, defaultPrompts, type Config, type CoreMessage } from "acp-kernel"; +import { withFetchTransport } from "../src/fetch-transport.js"; +import { createSummaryHttpCandidate, isOpenCodeGoTarget, type SummaryHttpTarget } from "../src/external-summary-http.js"; +import { ExternalSummaryExecutor, type SummaryWork } from "../src/external-summary.js"; +import { applyConfiguredCompression } from "../src/external-summary-compress.js"; +import { preflightCompress } from "../src/preflight.js"; +import { createSession } from "../src/session.js"; +import type { RewriteCtx } from "../src/stream.js"; +import { PACKAGE_NAME, VERSION } from "../src/version.js"; +import { _liveUpstreamTimersForTest, type FetchOptions } from "../src/fetch-util.js"; +import { validateGoSessionHeaders, goMissingSessionIdEnvelope } from "./wire-contract-fakes.js"; + +process.env.NODE_ENV = "test"; + +const GO_URL = "https://opencode.ai/zen/go/v1/chat/completions"; +const SUMMARY_TEXT = "Retained decisions, evidence, constraints and unfinished objectives."; +const GO_COMPLETION = JSON.stringify({ + id: "chatcmpl_go", object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 3, total_tokens: 13 }, +}); +const work: SummaryWork = { content: "Prior evidence and decisions to retain.", reference: "Read-only current task context.", instructions: "Preserve unfinished objectives." }; +const budget = { totalTimeoutMs: 4000, targetTimeoutMs: 1500, maxSummaryBytes: 2048 }; + +interface Captured { url: string; headers: Record } + +/** An in-memory OpenCode Go validation-parity fake: enforces WC-016 exactly like + * the real upstream (400 MissingSessionID when x-opencode-session is absent) and + * records the headers bili actually sent. Routed in via the fetch-transport seam + * so the candidate keeps the real opencode.ai hostname (detection still fires). */ +function goFake(capture: Captured[]): (url: string, options: FetchOptions) => Promise { + return async (_url, options) => { + const headers = Object.fromEntries(new Headers(options.headers ?? {}).entries()); + capture.push({ url: _url, headers }); + if (validateGoSessionHeaders(headers).length > 0) { + const env = goMissingSessionIdEnvelope(); + return new Response(env.body, { status: env.status, headers: { "content-type": "application/json" } }); + } + return new Response(GO_COMPLETION, { status: 200, headers: { "content-type": "application/json" } }); + }; +} + +afterEach(() => assert.equal(_liveUpstreamTimersForTest(), 0)); + +test("WC-016: Go target detection is scoped to opencode.ai/zen/go only", () => { + assert.equal(isOpenCodeGoTarget(GO_URL), true); + assert.equal(isOpenCodeGoTarget("https://opencode.ai/zen/go/v1/chat/completions?alt=sse"), true); + assert.equal(isOpenCodeGoTarget("https://OPENCODE.AI/zen/go/v1/chat/completions"), true, "host match is case-insensitive"); + assert.equal(isOpenCodeGoTarget("https://opencode.ai/zen/v1/messages"), false, "the zen Anthropic proxy under /zen/v1 is NOT Go"); + assert.equal(isOpenCodeGoTarget("https://opencode.ai/v1/chat/completions"), false, "non-/zen/go paths are untouched"); + assert.equal(isOpenCodeGoTarget("https://opencode.ai/zen/golang/v1/chat/completions"), false, "the /zen/go match is segment-bounded: a hypothetical /zen/golang is NOT Go"); + assert.equal(isOpenCodeGoTarget("https://opencode.ai/zen/go"), true, "the bare /zen/go root is Go"); + assert.equal(isOpenCodeGoTarget("https://evil.example.com/zen/go/v1/chat/completions"), false, "other hosts are untouched"); + assert.equal(isOpenCodeGoTarget("not-a-url"), false); +}); + +function goTarget(sessionId?: string): SummaryHttpTarget { + return { protocol: "openai", url: GO_URL, model: "deepseek-v4.1-flash", headers: { authorization: "Bearer go-test-key" }, ...(sessionId !== undefined ? { sessionId } : {}) }; +} + +test("WC-016: a Go summary candidate stamps x-opencode-session and bili's own user agent", async () => { + const capture: Captured[] = []; + const result = await withFetchTransport(goFake(capture), () => + new ExternalSummaryExecutor(1).execute(work, [createSummaryHttpCandidate(goTarget("sess-stable-id"), 4096)], budget)); + assert.equal(result.status, "success"); + if (result.status === "success") assert.equal(result.summary, SUMMARY_TEXT); + assert.equal(capture.length, 1); + assert.equal(capture[0].headers["x-opencode-session"], "sess-stable-id", "the stable conversation id rides the outbound Go call"); + assert.equal(capture[0].headers["user-agent"], `${PACKAGE_NAME}/${VERSION}`, "bili's own user agent, not a generic SDK name"); + assert.equal(capture[0].headers["authorization"], "Bearer go-test-key", "its own credential is still present"); +}); + +test("WC-016 negative control: a Go candidate with no session id fails on the upstream MissingSessionID 400", async () => { + const capture: Captured[] = []; + const result = await withFetchTransport(goFake(capture), () => + new ExternalSummaryExecutor(1).execute(work, [createSummaryHttpCandidate(goTarget(), 4096)], budget)); + assert.equal(result.status, "failed"); + assert.deepEqual(result.attempts, [{ targetIndex: 0, outcome: "error" }]); + assert.equal(capture[0].headers["x-opencode-session"], undefined, "no session id is fabricated — the failure stays honest"); +}); + +test("WC-016: a non-Go target keeps the existing no-session-header invariant", async () => { + const capture: Captured[] = []; + await withFetchTransport(goFake(capture), () => + new ExternalSummaryExecutor(1).execute(work, [createSummaryHttpCandidate({ protocol: "responses", url: "http://127.0.0.1:9/summary", model: "m", headers: { authorization: "Bearer k" }, sessionId: "must-not-appear" }, 4096)], budget)); + assert.equal(capture.length, 1); + assert.equal(capture[0].headers["x-opencode-session"], undefined, "sessionId is ignored off-Go"); + assert.equal(capture[0].headers["user-agent"], undefined, "no user-agent override off-Go"); +}); + +// A resolved-config rail carrying a single OpenCode Go target (expanded form, inline key). +function goContext(sessionId: string): RewriteCtx { + const core = createCore(); + const config = defaultConfig(400_000) as Config & { externalSummary?: unknown }; + config.externalSummary = { enabled: true, targets: [{ name: "go", protocol: "openai", url: GO_URL, model: "deepseek-v4.1-flash", apiKey: "go-test-key" }] }; + config.preserveRecentMessages = 0; + config.preserveRecentTokens = 0; + config.compress.minCompressRange = 100; + const session = createSession(sessionId); + const messages: CoreMessage[] = [ + { id: "h1", role: "assistant", contentType: "text", text: "A".repeat(2000) }, + { id: "h2", role: "assistant", contentType: "text", text: "B".repeat(2000) }, + { id: "current-task", role: "user", contentType: "text", text: "Current task: finish tests." }, + ]; + session.state = core.processTurn({ messages, state: session.state, config, tokenCount: 10_000, renderTags: "text-only" }).state; + return { core, config, session, messages, log: () => {} } as RewriteCtx; +} + +// BOTH compression modes execute the compress tool through applyConfiguredCompression +// (proxy mode server-side; plugin mode intercepts the agent's tool_use), so this lane +// covers both. A multi-range batch must stamp ONE stable id; a different session must +// stamp a different id. +test("WC-016 BOTH modes: the compress-tool lane stamps a stable x-opencode-session across a batch and isolates sessions", async () => { + const ref = (ctx: RewriteCtx, id: string) => ctx.session.state.messageRefs.byRaw[id]; + const captureA: Captured[] = []; + const ctxA = goContext("sess-A"); + const inputA = { content: [{ startId: ref(ctxA, "h1"), endId: ref(ctxA, "h1") }, { startId: ref(ctxA, "h2"), endId: ref(ctxA, "h2") }] }; + const resultA = await withFetchTransport(goFake(captureA), () => applyConfiguredCompression(inputA, ctxA)); + assert.equal(resultA.outcome, "applied", resultA.text); + assert.equal(captureA.length, 2, "two ranges -> two outbound Go summary calls"); + for (const c of captureA) assert.equal(c.headers["x-opencode-session"], "sess-A", "every call in the batch carries the SAME session id"); + + const captureB: Captured[] = []; + const ctxB = goContext("sess-B"); + const inputB = { content: [{ startId: ref(ctxB, "h1"), endId: ref(ctxB, "h1") }] }; + const resultB = await withFetchTransport(goFake(captureB), () => applyConfiguredCompression(inputB, ctxB)); + assert.equal(resultB.outcome, "applied", resultB.text); + assert.equal(captureB.length, 1); + assert.equal(captureB[0].headers["x-opencode-session"], "sess-B", "a different session stamps a different id"); +}); + +// The preflight + auto-fold lane dispatches summaries through preflightCompress. +test("WC-016 BOTH modes: the preflight/auto-fold lane stamps the session id on its Go summary calls", async () => { + const capture: Captured[] = []; + const ctx = goContext("preflight-sess"); + ctx.config.modelContextLimit = 1000; + ctx.session.stats.lastInputTokens = 10_000; + const result = await withFetchTransport(goFake(capture), () => + preflightCompress({ core: ctx.core, config: ctx.config, session: ctx.session, prompts: defaultPrompts, protocol: "responses", url: "http://127.0.0.1:9/main-must-not-summarize", headers: { authorization: "Bearer wrong-main-key" }, model: "main-model", log: () => {} }, ctx.messages)); + assert.ok(result.compressedRanges > 0, JSON.stringify(result)); + assert.ok(capture.length > 0, "the overflow was folded through the configured Go chain, not the main endpoint"); + for (const c of capture) assert.equal(c.headers["x-opencode-session"], "preflight-sess", "every auto-fold summary call carries the session id"); +}); diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 6fb4aba300..cf7440d31e 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -52,7 +52,7 @@ import { retrieveToolsFor, IMAGE_FULL_TOOL_GOOGLE, } from "../src/compress-tool.ts"; -import { WIRE_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, type Wire } from "./wire-contract-fakes.ts"; +import { WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateGoSessionHeaders, type Wire } from "./wire-contract-fakes.ts"; type ToolShape = Record; @@ -181,6 +181,13 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => { } }); +test("wire-contract ledger: every header rule has a live enforcement clause", () => { + for (const rule of WIRE_HEADER_RULES) { + const enforced = validateGoSessionHeaders({}).some((v) => v.startsWith(rule.id)); + assert.ok(enforced, `header rule ${rule.id} (${rule.summary}) has no live enforcement clause in the fakes`); + } +}); + // --------------------------------------------------------------------------- // Layer A: kernel exports (supersedes tests/kernel-pin-wire-schema.test.ts #1302) // ---------------------------------------------------------------------------