Skip to content

feat: truncate oversized inputs to fit instead of failing the summary - #1

Merged
fightingentropy merged 1 commit into
mainfrom
feat/head-tail-truncate-to-fit
Jun 30, 2026
Merged

feat: truncate oversized inputs to fit instead of failing the summary#1
fightingentropy merged 1 commit into
mainfrom
feat/head-tail-truncate-to-fit

Conversation

@fightingentropy

Copy link
Copy Markdown
Owner

What

When an assembled prompt exceeds the model's input-token limit, the summary engine threw Input token count exceeds model input limit and aborted the run. This replaces that hard failure with a head+tail truncate-to-fit: keep the document's opening and its conclusion, drop the middle, and proceed. A degraded summary of a truncated document beats no summary — which is exactly the inputs a summarizer is most useful for (long PDFs, multi-hour transcripts).

Changes

  • clipHeadAndTail helper (src/content/link-preview/content/cleaner.ts): head+tail windowing (~75/25), snaps cuts to nearby sentence/line boundaries, inserts a compact omission marker, guarantees output ≤ budget, and falls back to a head-only clip for tiny budgets. clipAtSentenceBoundary is left unchanged.
  • applyContentBudget now uses it, so the --max-extract-characters path keeps the closing section instead of clipping head-only.
  • fitUserTextToInputTokenBudget (src/run/summary-engine.ts): shrinks the prompt to the token budget (re-checking the real gpt-tokenizer count, tightening a few times if the estimate runs long) and logs the truncation under --verbose instead of erroring.
  • Tests: the three input-limit tests now assert truncate-and-proceed (LLM is called, run succeeds) rather than the old throw; added unit tests for clipHeadAndTail and the fit helper.

Because buildTaggedPrompt places <content> last, a head+tail clip of the assembled prompt naturally preserves the instructions (head) and the closing tag (tail), dropping only the content's middle.

Why head+tail (not head-only)

For a summary the closing material — conclusions, results, recommendations — is often the highest-value passage, and the existing truncation everywhere was head-only (slice(0, n)), discarding it.

Verification

bun run check (format + lint + perf guard + full coverage) green: 1346 passed, 0 failed; bun run typecheck clean.

Context

Inspired by NousResearch/hermes-agent#54843 (which replaced per-page LLM summarization with truncate-and-store in their agent's web-extract tool), adapted to a summarizer where the summary is the deliverable — so the borrowed idea is the truncation mechanism (graceful degradation + head+tail), not their "drop the summarizer" thesis.

🤖 Generated with Claude Code

When an assembled prompt exceeds the model's input-token limit, the engine
threw "Input token count exceeds model input limit" and aborted the run.
Replace that hard failure with a head+tail truncate-to-fit: keep the
document's opening and its conclusion, drop the middle, and proceed — a
degraded summary of a truncated document beats no summary.

- Add a shared clipHeadAndTail helper (head+tail windowing, boundary-aware,
  output bounded to the budget, head-only fallback for tiny budgets) and
  route the --max-extract-characters content budget through it so the
  closing section survives explicit truncation too.
- Add fitUserTextToInputTokenBudget in the summary engine: shrink the prompt
  to the token budget (re-checking the real token count) and note it under
  --verbose instead of erroring.
- Update the input-limit tests to assert truncate-and-proceed rather than the
  old throw; add unit tests for clipHeadAndTail and the fit helper.

Inspired by NousResearch/hermes-agent#54843 (truncate-and-store over LLM
summarization), adapted to a summarizer where the summary is the product.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@fightingentropy
fightingentropy merged commit b32cafc into main Jun 30, 2026
@fightingentropy
fightingentropy deleted the feat/head-tail-truncate-to-fit branch June 30, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants