From 1217c555629fa1bbdafe416ea2fb5fbc32e79eb3 Mon Sep 17 00:00:00 2001 From: can1357 Date: Wed, 4 Feb 2026 09:44:32 +0100 Subject: [PATCH] refactor(coding-agent/task): extracted truncateOutput function to shared tools module as truncateTail - Extracted truncateOutput function to shared tools module as truncateTail. - Updated executor to import and use truncateTail from tools module instead of local implementation. --- packages/coding-agent/src/task/executor.ts | 52 +++------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/packages/coding-agent/src/task/executor.ts b/packages/coding-agent/src/task/executor.ts index 48533ec2a..6bb9f705e 100644 --- a/packages/coding-agent/src/task/executor.ts +++ b/packages/coding-agent/src/task/executor.ts @@ -23,7 +23,7 @@ import { createAgentSession, discoverAuthStorage, discoverModels } from "../sdk" import type { AgentSession, AgentSessionEvent } from "../session/agent-session"; import type { AuthStorage } from "../session/auth-storage"; import { SessionManager } from "../session/session-manager"; -import type { ContextFileEntry } from "../tools"; +import { type ContextFileEntry, truncateTail } from "../tools"; import { jtdToJsonSchema } from "../tools/jtd-to-json-schema"; import { ToolAbortError } from "../tools/tool-errors"; import type { EventBus } from "../utils/event-bus"; @@ -210,51 +210,6 @@ export interface ExecutorOptions { settings?: Settings; } -/** - * Truncate output to byte and line limits. - */ -function truncateOutput(output: string): { text: string; truncated: boolean } { - let truncated = false; - let byteBudget = MAX_OUTPUT_BYTES; - let lineBudget = MAX_OUTPUT_LINES; - - let i = 0; - let lastNewlineIndex = -1; - while (i < output.length) { - const codePoint = output.codePointAt(i); - if (codePoint === undefined) break; - const codeUnitLength = codePoint > 0xffff ? 2 : 1; - const byteLen = codePoint <= 0x7f ? 1 : codePoint <= 0x7ff ? 2 : codePoint <= 0xffff ? 3 : 4; - if (byteBudget - byteLen < 0) { - truncated = true; - break; - } - byteBudget -= byteLen; - i += codeUnitLength; - - if (codePoint === 0x0a) { - lineBudget--; - lastNewlineIndex = i - 1; - if (lineBudget <= 0) { - truncated = true; - break; - } - } - } - - if (i < output.length) { - truncated = true; - } - - if (truncated && lineBudget <= 0 && lastNewlineIndex >= 0) { - output = output.slice(0, lastNewlineIndex); - } else { - output = output.slice(0, i); - } - - return { text: output, truncated }; -} - function parseStringifiedJson(value: unknown): unknown { if (typeof value !== "string") return value; const trimmed = value.trim(); @@ -1194,7 +1149,10 @@ export async function runSubprocess(options: ExecutorOptions): Promise