MCPcopy Index your code
hub / github.com/codehamr/codehamr / Truncate

Function Truncate

internal/ctx/ctx.go:85–96  ·  view source on GitHub ↗

Truncate collapses oversized tool outputs to first 2k + last 2k tokens; inputs at or under 6k pass through unchanged. Head/tail can't overlap: >6k tokens means >24k bytes, well over the 16k kept. Boundaries snap to a valid UTF-8 rune start so non-ASCII output never breaks mid-sequence.

(out string)

Source from the content-addressed store, hash-verified

83// inputs at or under 6k pass through unchanged. Head/tail can't overlap:
84// >6k tokens means >24k bytes, well over the 16k kept. Boundaries snap to a
85// valid UTF-8 rune start so non-ASCII output never breaks mid-sequence.
86func Truncate(out string) string {
87 total := Tokens(out)
88 if total <= ToolOutputCap {
89 return out
90 }
91 limit := ToolHeadTail * 4
92 head := runeBoundaryDown(out, limit)
93 tail := runeBoundaryUp(out, len(out)-limit)
94 marker := fmt.Sprintf("\n───── truncated: %d tokens total, first %d + last %d shown, the middle is OMITTED. This is a PARTIAL view; you can't review or conclude from code you can't see here, so re-run narrower (grep/sed/head/tail) to read the omitted span. ─────\n",
95 total, ToolHeadTail, ToolHeadTail)
96 return out[:head] + marker + out[tail:]
97}
98
99// runeBoundaryDown walks i left to a rune start so out[:i] never ends

Callers 3

Calls 3

TokensFunction · 0.85
runeBoundaryDownFunction · 0.85
runeBoundaryUpFunction · 0.85

Tested by 3