anthropics / anthropics/claude-code

[FEATURE] Make Bash output truncation configurable by the agent

Aperta
#93,905 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area:bash enhancement
Lingua principale
Python
Stelle
145k
Fork
23.1k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

### Preflight Checklist

- [x] I have searched [existing requests](https://github.com/anthropics/claude-code/issues?q=is%3Aissue%20label%3Aenhancement) and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)

### Problem Statement

Currently, Claude models habitually pipe command output through `head` or `tail` to avoid flooding their own context windows while selecting which end of the output is more likely to be useful. This requires rerunning a command if "the important part" turns out to be somewhere else; this is unfortunate when the command is fifteen minutes of unit testing with no cache.

Claude Code automatically truncates long command outputs and dumps them to a file for the agent to read later. Claude doesn't rely on this -- it's not a habit it picked up in training, and it doesn't have control over the size of the truncation window and which end of the output it gets.

### Proposed Solution

The Bash tool should have optional parameters that Claude is strongly nudged to use in preference to `head` and `tail` pipes: `head` and `tail`, specifying how much of the beginning and end of output to dump directly into the tool result (saving the full output as a file for Claude to reference later, even if the entire input fits in range). If neither is specified, use the current default truncation behaviors. If exactly one is specified, emit only the head or tail of the result, identical to the output having been piped through the real `head` or `tail` command. If both are specified, emit both with some kind of divider between them (or perhaps in two separate fields of the tool call response, if Claude "sees" it in such a formatted way), adding a reminder in all truncation cases about where the complete output can be found.

If truncation-as-specified would generate more output than the current truncation limit because Claude specified a line count rather than a byte count and the lines were (in aggregate) too long, truncate more aggressively than specified (with a note that the tool did so). If Claude's request _inherently_ exceeds the limit (limit was specified in bytes and it was Too Many Bytes), reject the tool call with a validation error and hints to choose more appropriate amounts of content.

If a command appears to be getting piped through head or tail at the end (as reflected by a Bash AST parse), the first time per context (reset, rewind, and compact may all reset this) that this appears to happen, reject the tool call with a polite error message reminding Claude that it is better to use these fields instead because it will keep all the output, but if it really specifically needs `head` or `tail`, it can try the command again. `head` and `tail` in the middle of a pipeline are probably appropriate (they're manipulating the input to some other tool), as are `head` and `tail` on their own reading a file or stream; more subtly (and one of the cases the heuristic would be a false positive!), `head` on an infinite stream is appropriate. (`yes | head -n 10` terminates; `yes > stdout.tmp` and truncating it later does not! It is unlikely that any simple heuristic will catch this perfectly, which is why the "if you really meant it, just do it again" escape hatch is important.)

### Alternative Solutions

I now have a PreToolUse hook catching Bash commands and checking (via regex) whether they appear to be piping into head or tail (with some heuristics to remove obvious false positives) and, the first time this happens, rejecting it with an error like I described, asking Claude to redirect to a file instead (then head or tail that file) or to just rely on Claude Code's built-in truncation. This is a reasonably similar result to implementing it as a feature in the harness, but since Claude has this behavior of trying to truncate output itself (and sometimes losing data when doing so), it would be to the benefit of all users (rather than only those of us who have written such a hook) for the harness to handle it directly. In addition to the PreToolUse hook, there are SessionStart and PreCompact hooks that reset its "already warned" flag so it will renew the warning if it has reason to believe Claude might have forgotten the rule.

### Priority

Low - Nice to have

### Feature Category

API and model interactions

### Use Case Example

Example scenario:

1. I'm working on a huge vibe-coded OSS repository with three thousand unit tests. Running the subset of tests marked as "not slow" takes 20 minutes.
2. Claude makes some changes, runs the tests, and habitually pipes the output through `tail -n 20` to get a result summary.
3. **New behavior:** Claude Code rejects the command with an error saying Claude should use the `tail:` arg instead.
4. **New behavior:** Claude rewrites the command, omitting the `| tail -n 20` but providing `"tail": "-n 20"` as a tool argument instead.
5. The unit tests run. 20 minutes later, they are done and 347 of them have failed.
6. Claude discovers there are 347 test failures and it can see three of them. It has regrets about skipping the rest of the output.
7. **New behavior:** The entire output was captured, so Claude reads it (or writes a little Python script to summarize it, or asks a subagent to analyze it) and figures out the underlying issue.

Without this change (and without a hook nudging it to different behavior), Claude has indeed lost details on almost all the test failures so it has to spend another 20 minutes rerunning the tests, this time redirecting the output to a file. (This feature request -- and the hook I now have installed -- are inspired by Claude literally doing exactly that. Although in the time I have spent typing this up, I think the second copy of the test pass just finished...)

### Additional Context

_No response_

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Start by locating the Bash tool implementation, its tool-argument schema, and the existing command-output truncation path. Review how tool validation, captured output files, and session state are tested. Done means the requested head/tail controls, validation, full-output reference, and pipe warning behavior are covered without changing the default behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
bash
Ambito
api, cli
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.