Repository navigation
Description
Summary
When the hook rewrites head -N file or tail -N file to rtk read file --head-lines N / --tail-lines N, tracking records the row as cat file with the whole file as input_tokens. The command the agent wrote only ever prints N lines, and 0.50's read returns those same N lines, so nothing was saved, but gain books nearly the whole file as savings.
The same happens when a producer is rewritten in front of a safe consumer: grep … | head -3 is tracked as the full grep output, though only three lines leave the pipeline.
This is separate from the missing context ceiling in #3508. It's wrong on gain's own terms (bash output bytes): the bash output of head -5 file is five lines. It's the same family as #561 (the ls baseline measured against ls -la).
Environment
rtk 0.50.0 (Homebrew), macOS 27.0.1, Claude Code hook (rtk hook claude).
Reproduction
mkdir /tmp/rtk-gain && cd /tmp/rtk-gain
seq -f 'line %g of a sample text file used to reproduce the head baseline' 1 400 > big.txt
export RTK_DB_PATH=$PWD/h.db
for c in "head -5 big.txt" "tail -n 5 big.txt" "grep -n sample big.txt | head -3" "cat big.txt"; do
rw=$(jq -n --arg c "$c" '{tool_name:"Bash",tool_input:{command:$c}}' | rtk hook claude | jq -r '.hookSpecificOutput.updatedInput.command')
echo "$c -> $rw"; bash -c "$rw" >/dev/null
done
sqlite3 -header -column h.db 'select original_cmd, rtk_cmd, input_tokens, output_tokens, saved_tokens from commands'| Command the agent wrote | Rewritten to | Tracked as | saved_tokens |
What the command prints (bytes / 4) |
|---|---|---|---|---|
head -5 big.txt |
rtk read big.txt --head-lines 5 |
cat big.txt, input 6673 |
6591 | 81 |
tail -n 5 big.txt |
rtk read big.txt --tail-lines 5 |
cat big.txt, input 6673 |
6589 | 83 |
grep -n sample big.txt | head -3 |
rtk grep -n sample big.txt | head -3 |
grep -n sample big.txt, input 7846 |
7397 | 50 |
cat big.txt (control) |
rtk read big.txt |
cat big.txt, input 6673 |
0 | 6673 |
The control is right: a full cat saves nothing and is booked as nothing. The three rows above it are booked at roughly the size of the whole file for commands that print 50–83 tokens.
Impact
On one install, rows from head/tail reads were 28% of lifetime gain (222K of 788K tokens), and every full-file read in the same history saved exactly 0. Partial reads were the only reads that ever claimed a saving.
Expected
The baseline should be what the command the agent wrote would print:
- For
--head-lines N/--tail-lines N, the N-line window of the raw file, not the file. - For a producer rewritten in front of
head/tail(grep/rgpiped intohead -N/tail -Nreturn the wrong lines after the #3343 safe-consumer rewrite #4207, Pipe producer is rewritten (git log | head, git diff 2>&1 | head) #4364), the producer's output is not what the agent reads. Either skip tracking that segment or measure after the consumer.
Reported by @blakecscott; reproduced and written up with Claude Code.
Activity
rtk-wshm-sync-bot commentedon Sep 30, 2026
🔍 Automated Triage
bughighSummary
gain mis-measures head/tail and pipe-producer rewrites against the full source output instead of the actual bytes printed, booking phantom savings
📁 Relevant files
src/cmds/gain.rssrc/tracking/mod.rssrc/hook/rewrite.rsSuggested Actions
lsnotls -la#561 andgain: savings are counted without a context ceiling — reported figure ~184x the real one on my install #3508Triaged automatically by wshm · This is an automated analysis, not a human review.
TechWizard9999 commentedon Oct 1, 2026
Hi @KuSh , Can I work on this Issue ?
KuSh commentedon Oct 3, 2026
The same booking happens on the stdin entry point, which #4367 does not touch. With the reproduction file from the description:
(
original_cmd | rtk_cmd | input_tokens | output_tokens | saved_tokens, from a head build of #4367 at 4c292b2;develop6f5503d is the same.) The hook rewrites none ofcat big.txt | head -5,head -5 < big.txtortail -n 5 < big.txt(all exit 1), so only a typedrtk read -reaches this today. It is the same rule as this issue's first bullet: the baseline for a windowed read is the window.fix(read): fix head/tail tracking baseline and test for rtk-ai#4366
KuSh commentedon Oct 18, 2026
Closing as a duplicate of #2805, which tracks the same baseline problem for head/tail reads and pipelines, along with the wider discussion and data. The stdin case noted here (
rtk read - --head-lines/--tail-lines) follows the same rule and belongs there too. Please follow up in #2805.