feat(context): compact successful write/edit tool results to receipts (#289)
A successful file_write/file_edit echoes the whole file body (+ diff) back in its tool result — up to ~30k chars, which pushed the traced Gemma4 request past its context window. The write already happened and its full output is durable in the event log/CAS; the model only needs a receipt that it landed. Replace each exit=0 write result with a one-line receipt (path + elision marker) in the context builder; reads, gate output, and nonzero-exit writes stay verbatim. Derived-only pass over the transcript — authoritative events are untouched, so it's replay-safe. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+41
-2
@@ -94,16 +94,23 @@ class DefaultContextPackBuilder(
|
||||
// raw ToolCallRequest JSON into a dotted-line format the fingerprint parser can't read.
|
||||
val deduped = dedupeRepeatedToolCalls(stamped)
|
||||
|
||||
// #289: a successful file_write/file_edit echoes the whole file body (+ diff) back in its
|
||||
// tool result — up to ~30k chars, which pushed the traced Gemma4 request past its context
|
||||
// window. The write already happened and its full output is durable in the event log/CAS;
|
||||
// the model only needs a receipt that it landed. Compact those results to a one-line receipt
|
||||
// (reads and gate output stay verbatim). Derived-only — authoritative events are untouched.
|
||||
val compacted = compactWriteReceipts(deduped)
|
||||
|
||||
// Stage 1 (FORMAT_COMPRESS): lossless json→dotted-line compaction of structured entries.
|
||||
val formatted = if (policy.enabled(CompressionStage.FORMAT_COMPRESS)) {
|
||||
deduped.map { entry ->
|
||||
compacted.map { entry ->
|
||||
if (classifier.classify(entry) == ContextClass.STRUCTURED) {
|
||||
reencode(entry, formatCompressor.compress(entry.content))
|
||||
} else {
|
||||
entry
|
||||
}
|
||||
}
|
||||
} else deduped
|
||||
} else compacted
|
||||
|
||||
// Stage 3 (TOKEN_PRUNE): prune freeform prose, preserving protected spans. When TIER_SPLIT
|
||||
// is on, the newest TIER0_TURNS freeform turns are left full-fidelity (tier 0).
|
||||
@@ -283,6 +290,38 @@ class DefaultContextPackBuilder(
|
||||
}
|
||||
}
|
||||
|
||||
// A successful tool result is framed "[<tool> exit=<code>]\n..." by renderToolResult; failures
|
||||
// use ERROR:/FATAL: sentinels (never matched here). Only exit=0 writes are compacted.
|
||||
private val successFrame = Regex("""^\[(\S+) exit=(\d+)]""")
|
||||
private val writeToolNames = setOf("file_write", "file_edit")
|
||||
|
||||
private fun compactWriteReceipts(entries: List<ContextEntry>): List<ContextEntry> {
|
||||
val writeCallPaths = entries
|
||||
.filter { it.sourceType == "assistantToolCall" && toolCallName(it.content) in writeToolNames }
|
||||
.associate { it.sourceId to toolCallPath(it.content) }
|
||||
if (writeCallPaths.isEmpty()) return entries
|
||||
return entries.map { entry ->
|
||||
if (entry.sourceType != "toolResult" || entry.sourceId !in writeCallPaths) return@map entry
|
||||
val header = entry.content.substringBefore('\n')
|
||||
val match = successFrame.find(header) ?: return@map entry
|
||||
// A nonzero-exit write is a Success carrying a real advisory (e.g. a partial patch) —
|
||||
// keep it verbatim; only a clean exit=0 write body is pure echo we can drop.
|
||||
if (match.groupValues[2] != "0") return@map entry
|
||||
val path = writeCallPaths[entry.sourceId].orEmpty()
|
||||
reencode(entry, "$header wrote $path — succeeded; body elided (full result in event log)".trim())
|
||||
}
|
||||
}
|
||||
|
||||
private fun toolCallName(content: String): String? = runCatching {
|
||||
Json.parseToJsonElement(content).jsonObject["function"]?.jsonObject?.get("name")?.jsonPrimitive?.content
|
||||
}.getOrNull()
|
||||
|
||||
private fun toolCallPath(content: String): String? = runCatching {
|
||||
val args = Json.parseToJsonElement(content).jsonObject["function"]?.jsonObject
|
||||
?.get("arguments")?.jsonPrimitive?.content ?: return null
|
||||
Json.parseToJsonElement(args).jsonObject["path"]?.jsonPrimitive?.content
|
||||
}.getOrNull()
|
||||
|
||||
private fun toolCallFingerprint(content: String): String? = runCatching {
|
||||
val obj = Json.parseToJsonElement(content).jsonObject
|
||||
val fn = obj["function"]?.jsonObject ?: return null
|
||||
|
||||
Reference in New Issue
Block a user