Files
oh-my-opencode/src/shared/dynamic-truncator.ts
T
YeonGyu-Kim 67ead7bf6d fix(dynamic-truncator): bound session.messages fetch to stop forever-hang on Read (#4086)
Root cause: `getContextWindowUsage` caches the *promise* of
`fetchContextWindowUsage` in a per-session WeakMap keyed by client. When
`ctx.client.session.messages({ path: { id: sessionID } })` never settles
(observed once `service=session.processor ... error=Aborted process`
takes hold), the cached pending promise wedges every concurrent and
later caller in the same session. The five hooks that share one
`createDynamicTruncator(ctx)` -- directory-agents-injector,
directory-readme-injector, rules-injector, tool-output-truncator, plus
indirect callers -- all await that same poisoned promise on every Read,
so the user-facing tool chain hangs forever and ESC cannot break it.
Reporters in #4086 land on this path consistently when reading AGENTS.md
files (which trigger directory-agents-injector via the directory walk).

Fix: race the underlying `session.messages` call against a 5s timeout
through a new `withFetchTimeout` helper. On timeout the catch block logs
the failure and returns `null`, which `dynamicTruncate` already treats
as the "context usage unavailable" signal and falls back to the static
truncation budget. Successful responses still cache as before. The
`message.updated finish=true` invalidation hook still clears poisoned
caches on the next completed turn so retries are clean.

Tests:
- Add a never-settling `session.messages` mock with a 50 ms override via
  the new `_setContextWindowUsageFetchTimeoutMsForTesting` hook (matches
  the established `_setXxxForTesting` pattern in `opencode-http-api.ts`
  and `prompt-async-gate.ts`).
- Three new BDD cases pin the fix: (1) single caller returns null fast,
  (2) parallel concurrent callers all unblock on the same cached promise
  instead of hanging, (3) invalidate + retry rehydrates cleanly.
- All 8 pre-existing tests in the file still pass (happy paths, cache
  reuse, invalidation, env/model fallback).

Verification:
- `bun test src/shared/dynamic-truncator.test.ts` -- 11 pass.
- `bun test src/shared/prompt-async-route-audit.test.ts` -- 6 pass
  (added log import, no raw prompt route added).
- `bun test` (full suite) -- 7009 pass, 1 skip, 1 pre-existing flake in
  `closeTmuxPane` mock.module test (reproduces on dev without this
  change; isolated run passes).
- `bun run typecheck` -- clean.
- `bun run build` -- clean (esm bundle + tsc + schema).
- Manual harness `.debugging/manual-qa.ts` (uncommitted) drives the same
  shape as the real hook chain and resolves the hang scenario in 51 ms.
2026-05-17 03:51:56 +09:00

345 lines
9.2 KiB
TypeScript

import type { PluginInput } from "@opencode-ai/plugin";
import {
resolveActualContextLimit,
type ContextLimitModelCacheState,
} from "./context-limit-resolver"
import { log } from "./logger"
import { normalizeSDKResponse } from "./normalize-sdk-response"
const CHARS_PER_TOKEN_ESTIMATE = 4;
const DEFAULT_TARGET_MAX_TOKENS = 50_000;
// Hard ceiling on how long `session.messages()` is allowed to block inside
// `fetchContextWindowUsage`. Without it, a stuck OpenCode RPC (observed when
// `session.processor` enters an "Aborted process" loop) would leave the cached
// promise pending forever and every hook that calls `truncator.truncate(...)`
// would hang on it (issue #4086).
export const DEFAULT_CONTEXT_WINDOW_USAGE_FETCH_TIMEOUT_MS = 5_000;
declare function setTimeout(callback: () => void, delay?: number): ReturnType<typeof globalThis.setTimeout>
declare function clearTimeout(timeout: ReturnType<typeof globalThis.setTimeout>): void
interface AssistantMessageInfo {
role: "assistant";
providerID?: string;
modelID?: string;
tokens: {
input: number;
output: number;
reasoning: number;
cache: { read: number; write: number };
};
}
interface MessageWrapper {
info: { role: string } & Partial<AssistantMessageInfo>;
}
type ContextWindowUsage = {
usedTokens: number;
remainingTokens: number;
usagePercentage: number;
}
type ContextWindowUsageClient = Pick<PluginInput["client"], "session">
const usageCacheByClient = new WeakMap<object, Map<string, Map<string, Promise<ContextWindowUsage | null>>>>()
// Test-only override for the fetch timeout used by `fetchContextWindowUsage`.
// `undefined` means "use the production default".
let contextWindowUsageFetchTimeoutMsForTesting: number | undefined = undefined
export function _setContextWindowUsageFetchTimeoutMsForTesting(
ms: number | undefined,
): void {
contextWindowUsageFetchTimeoutMsForTesting = ms
}
function createModelCacheKey(modelCacheState?: ContextLimitModelCacheState): string {
if (!modelCacheState) {
return "default"
}
const cachedLimits = modelCacheState.modelContextLimitsCache
? [...modelCacheState.modelContextLimitsCache.entries()]
.sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey))
.map(([modelKey, limit]) => `${modelKey}:${limit}`)
.join(",")
: ""
return `${modelCacheState.anthropicContext1MEnabled ? "1m" : "200k"}|${cachedLimits}`
}
function getUsageCache(
client: ContextWindowUsageClient,
modelCacheState?: ContextLimitModelCacheState,
): Map<string, Promise<ContextWindowUsage | null>> {
let cacheByModelState = usageCacheByClient.get(client)
if (!cacheByModelState) {
cacheByModelState = new Map()
usageCacheByClient.set(client, cacheByModelState)
}
const modelCacheKey = createModelCacheKey(modelCacheState)
let cache = cacheByModelState.get(modelCacheKey)
if (!cache) {
cache = new Map()
cacheByModelState.set(modelCacheKey, cache)
}
return cache
}
export function invalidateContextWindowUsageCache(ctx: PluginInput, sessionID?: string): void {
const cacheByModelState = usageCacheByClient.get(ctx.client)
if (!cacheByModelState) {
return
}
for (const cache of cacheByModelState.values()) {
if (sessionID) {
cache.delete(sessionID)
} else {
cache.clear()
}
}
}
export interface TruncationResult {
result: string;
truncated: boolean;
removedCount?: number;
}
export interface TruncationOptions {
targetMaxTokens?: number;
preserveHeaderLines?: number;
contextWindowLimit?: number;
}
function estimateTokens(text: string): number {
return Math.ceil(text.length / CHARS_PER_TOKEN_ESTIMATE);
}
export function truncateToTokenLimit(
output: string,
maxTokens: number,
preserveHeaderLines = 3,
): TruncationResult {
if (typeof output !== 'string') {
return { result: String(output ?? ''), truncated: false };
}
const currentTokens = estimateTokens(output);
if (currentTokens <= maxTokens) {
return { result: output, truncated: false };
}
const lines = output.split("\n");
if (lines.length <= preserveHeaderLines) {
const maxChars = maxTokens * CHARS_PER_TOKEN_ESTIMATE;
return {
result:
output.slice(0, maxChars) +
"\n\n[Output truncated due to context window limit]",
truncated: true,
};
}
const headerLines = lines.slice(0, preserveHeaderLines);
const contentLines = lines.slice(preserveHeaderLines);
const headerText = headerLines.join("\n");
const headerTokens = estimateTokens(headerText);
const truncationMessageTokens = 50;
const availableTokens = maxTokens - headerTokens - truncationMessageTokens;
if (availableTokens <= 0) {
return {
result:
headerText + "\n\n[Content truncated due to context window limit]",
truncated: true,
removedCount: contentLines.length,
};
}
const resultLines: string[] = [];
let currentTokenCount = 0;
for (const line of contentLines) {
const lineTokens = estimateTokens(line + "\n");
if (currentTokenCount + lineTokens > availableTokens) {
break;
}
resultLines.push(line);
currentTokenCount += lineTokens;
}
const truncatedContent = [...headerLines, ...resultLines].join("\n");
const removedCount = contentLines.length - resultLines.length;
return {
result:
truncatedContent +
`\n\n[${removedCount} more lines truncated due to context window limit]`,
truncated: true,
removedCount,
};
}
export async function getContextWindowUsage(
ctx: PluginInput,
sessionID: string,
modelCacheState?: ContextLimitModelCacheState,
): Promise<ContextWindowUsage | null> {
const cache = getUsageCache(ctx.client, modelCacheState)
const cached = cache.get(sessionID)
if (cached) {
return cached
}
const usagePromise = fetchContextWindowUsage(ctx, sessionID, modelCacheState)
cache.set(sessionID, usagePromise)
return usagePromise
}
function withFetchTimeout<T>(operation: Promise<T>, timeoutMs: number): Promise<T> {
if (timeoutMs <= 0) {
return operation
}
let timeoutID: ReturnType<typeof globalThis.setTimeout> | undefined
const timeoutPromise = new Promise<never>((_, reject) => {
timeoutID = setTimeout(
() =>
reject(
new Error(
`[dynamic-truncator] session.messages timed out after ${timeoutMs}ms`,
),
),
timeoutMs,
)
})
return Promise.race([operation, timeoutPromise]).finally(() => {
if (timeoutID !== undefined) clearTimeout(timeoutID)
})
}
async function fetchContextWindowUsage(
ctx: PluginInput,
sessionID: string,
modelCacheState?: ContextLimitModelCacheState,
): Promise<ContextWindowUsage | null> {
const fetchTimeoutMs =
contextWindowUsageFetchTimeoutMsForTesting ?? DEFAULT_CONTEXT_WINDOW_USAGE_FETCH_TIMEOUT_MS
try {
const response = await withFetchTimeout(
ctx.client.session.messages({
path: { id: sessionID },
}),
fetchTimeoutMs,
);
const messages = normalizeSDKResponse(response, [] as MessageWrapper[], { preferResponseOnMissingData: true })
const assistantMessages = messages
.filter((m) => m.info.role === "assistant")
.map((m) => m.info as AssistantMessageInfo);
if (assistantMessages.length === 0) return null;
const lastAssistant = assistantMessages[assistantMessages.length - 1];
const lastTokens = lastAssistant?.tokens;
if (!lastAssistant || !lastTokens) return null;
const actualLimit =
lastAssistant.providerID !== undefined
? resolveActualContextLimit(
lastAssistant.providerID,
lastAssistant.modelID ?? "",
modelCacheState,
)
: null;
if (!actualLimit) return null;
const usedTokens =
(lastTokens?.input ?? 0) +
(lastTokens?.cache?.read ?? 0) +
(lastTokens?.output ?? 0);
const remainingTokens = actualLimit - usedTokens;
return {
usedTokens,
remainingTokens,
usagePercentage: usedTokens / actualLimit,
};
} catch (error) {
log("[dynamic-truncator] fetchContextWindowUsage failed; falling back to null", {
sessionID,
error: error instanceof Error ? error.message : String(error),
})
return null;
}
}
export async function dynamicTruncate(
ctx: PluginInput,
sessionID: string,
output: string,
options: TruncationOptions = {},
modelCacheState?: ContextLimitModelCacheState,
): Promise<TruncationResult> {
if (typeof output !== 'string') {
return { result: String(output ?? ''), truncated: false };
}
const {
targetMaxTokens = DEFAULT_TARGET_MAX_TOKENS,
preserveHeaderLines = 3,
} = options;
const usage = await getContextWindowUsage(ctx, sessionID, modelCacheState);
if (!usage) {
// Fallback: apply conservative truncation when context usage unavailable
return truncateToTokenLimit(output, targetMaxTokens, preserveHeaderLines);
}
const maxOutputTokens = Math.min(
usage.remainingTokens * 0.5,
targetMaxTokens,
);
if (maxOutputTokens <= 0) {
return {
result: "[Output suppressed - context window exhausted]",
truncated: true,
};
}
return truncateToTokenLimit(output, maxOutputTokens, preserveHeaderLines);
}
export function createDynamicTruncator(
ctx: PluginInput,
modelCacheState?: ContextLimitModelCacheState,
) {
return {
truncate: (
sessionID: string,
output: string,
options?: TruncationOptions,
) => dynamicTruncate(ctx, sessionID, output, options, modelCacheState),
getUsage: (sessionID: string) =>
getContextWindowUsage(ctx, sessionID, modelCacheState),
truncateSync: (
output: string,
maxTokens: number,
preserveHeaderLines?: number,
) => truncateToTokenLimit(output, maxTokens, preserveHeaderLines),
};
}