feat(anthropic-auto-compact): Add tool output truncation recovery layer for token limit handling (#63)
- Add storage.ts: Functions to find and truncate largest tool results - Add TruncateState and TRUNCATE_CONFIG for truncation tracking - Implement truncate-first recovery: truncate largest output -> retry (10x) -> compact (2x) -> revert (3x) - Move session error handling to immediate recovery instead of session.idle wait - Add compactionInProgress tracking to prevent concurrent execution This fixes GitHub issue #63: "prompt is too long" errors now trigger immediate recovery by truncating the largest tool outputs first before attempting compaction. 🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
163
src/hooks/anthropic-auto-compact/storage.ts
Normal file
163
src/hooks/anthropic-auto-compact/storage.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
|
||||
import { join } from "node:path"
|
||||
import { xdgData } from "xdg-basedir"
|
||||
|
||||
const OPENCODE_STORAGE = join(xdgData ?? "", "opencode", "storage")
|
||||
const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
|
||||
const PART_STORAGE = join(OPENCODE_STORAGE, "part")
|
||||
|
||||
const TRUNCATION_MESSAGE =
|
||||
"[TOOL RESULT TRUNCATED - Context limit exceeded. Original output was too large and has been truncated to recover the session. Please re-run this tool if you need the full output.]"
|
||||
|
||||
interface StoredToolPart {
|
||||
id: string
|
||||
sessionID: string
|
||||
messageID: string
|
||||
type: "tool"
|
||||
callID: string
|
||||
tool: string
|
||||
state: {
|
||||
status: "pending" | "running" | "completed" | "error"
|
||||
input: Record<string, unknown>
|
||||
output?: string
|
||||
error?: string
|
||||
}
|
||||
truncated?: boolean
|
||||
originalSize?: number
|
||||
}
|
||||
|
||||
export interface ToolResultInfo {
|
||||
partPath: string
|
||||
partId: string
|
||||
messageID: string
|
||||
toolName: string
|
||||
outputSize: number
|
||||
}
|
||||
|
||||
function getMessageDir(sessionID: string): string {
|
||||
if (!existsSync(MESSAGE_STORAGE)) return ""
|
||||
|
||||
const directPath = join(MESSAGE_STORAGE, sessionID)
|
||||
if (existsSync(directPath)) {
|
||||
return directPath
|
||||
}
|
||||
|
||||
for (const dir of readdirSync(MESSAGE_STORAGE)) {
|
||||
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID)
|
||||
if (existsSync(sessionPath)) {
|
||||
return sessionPath
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
function getMessageIds(sessionID: string): string[] {
|
||||
const messageDir = getMessageDir(sessionID)
|
||||
if (!messageDir || !existsSync(messageDir)) return []
|
||||
|
||||
const messageIds: string[] = []
|
||||
for (const file of readdirSync(messageDir)) {
|
||||
if (!file.endsWith(".json")) continue
|
||||
const messageId = file.replace(".json", "")
|
||||
messageIds.push(messageId)
|
||||
}
|
||||
|
||||
return messageIds
|
||||
}
|
||||
|
||||
export function findToolResultsBySize(sessionID: string): ToolResultInfo[] {
|
||||
const messageIds = getMessageIds(sessionID)
|
||||
const results: ToolResultInfo[] = []
|
||||
|
||||
for (const messageID of messageIds) {
|
||||
const partDir = join(PART_STORAGE, messageID)
|
||||
if (!existsSync(partDir)) continue
|
||||
|
||||
for (const file of readdirSync(partDir)) {
|
||||
if (!file.endsWith(".json")) continue
|
||||
try {
|
||||
const partPath = join(partDir, file)
|
||||
const content = readFileSync(partPath, "utf-8")
|
||||
const part = JSON.parse(content) as StoredToolPart
|
||||
|
||||
if (part.type === "tool" && part.state?.output && !part.truncated) {
|
||||
results.push({
|
||||
partPath,
|
||||
partId: part.id,
|
||||
messageID,
|
||||
toolName: part.tool,
|
||||
outputSize: part.state.output.length,
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results.sort((a, b) => b.outputSize - a.outputSize)
|
||||
}
|
||||
|
||||
export function findLargestToolResult(sessionID: string): ToolResultInfo | null {
|
||||
const results = findToolResultsBySize(sessionID)
|
||||
return results.length > 0 ? results[0] : null
|
||||
}
|
||||
|
||||
export function truncateToolResult(partPath: string): {
|
||||
success: boolean
|
||||
toolName?: string
|
||||
originalSize?: number
|
||||
} {
|
||||
try {
|
||||
const content = readFileSync(partPath, "utf-8")
|
||||
const part = JSON.parse(content) as StoredToolPart
|
||||
|
||||
if (!part.state?.output) {
|
||||
return { success: false }
|
||||
}
|
||||
|
||||
const originalSize = part.state.output.length
|
||||
const toolName = part.tool
|
||||
|
||||
part.truncated = true
|
||||
part.originalSize = originalSize
|
||||
part.state.output = TRUNCATION_MESSAGE
|
||||
|
||||
writeFileSync(partPath, JSON.stringify(part, null, 2))
|
||||
|
||||
return { success: true, toolName, originalSize }
|
||||
} catch {
|
||||
return { success: false }
|
||||
}
|
||||
}
|
||||
|
||||
export function getTotalToolOutputSize(sessionID: string): number {
|
||||
const results = findToolResultsBySize(sessionID)
|
||||
return results.reduce((sum, r) => sum + r.outputSize, 0)
|
||||
}
|
||||
|
||||
export function countTruncatedResults(sessionID: string): number {
|
||||
const messageIds = getMessageIds(sessionID)
|
||||
let count = 0
|
||||
|
||||
for (const messageID of messageIds) {
|
||||
const partDir = join(PART_STORAGE, messageID)
|
||||
if (!existsSync(partDir)) continue
|
||||
|
||||
for (const file of readdirSync(partDir)) {
|
||||
if (!file.endsWith(".json")) continue
|
||||
try {
|
||||
const content = readFileSync(join(partDir, file), "utf-8")
|
||||
const part = JSON.parse(content)
|
||||
if (part.truncated === true) {
|
||||
count++
|
||||
}
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
Reference in New Issue
Block a user