Skip to content

gain: head -N / tail -N rewrites are tracked as a cat of the whole file, booking savings that don't exist #4366

Closed as duplicate

Description

@blakecscott

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:


Reported by @blakecscott; reproduced and written up with Claude Code.

Activity

  1. rtk-wshm-sync-bot commented on Sep 30, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    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.rs
    • src/tracking/mod.rs
    • src/hook/rewrite.rs

    Suggested Actions

    1. Compute baseline from the N-line window of the raw file for --head-lines/--tail-lines, not the whole file
    2. For producer|head/tail pipelines (Pipe producer is rewritten (git log | head, git diff 2>&1 | head) #4364), measure post-consumer output or skip tracking that segment
    3. Cross-check fix against related baseline-measurement bugs fix: ls tracking baseline should compare against ls not ls -la #561 and gain: savings are counted without a context ceiling — reported figure ~184x the real one on my install #3508
    4. Add regression test using the repro script's sqlite assertions on saved_tokens

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. TechWizard9999 commented on Oct 1, 2026

    @TechWizard9999
    Contributor

    Hi @KuSh , Can I work on this Issue ?

  3. KuSh commented on Oct 3, 2026

    @KuSh
    Collaborator

    The same booking happens on the stdin entry point, which #4367 does not touch. With the reproduction file from the description:

    rtk read - --tail-lines 5 < big.txt   →  cat - (stdin) | rtk read - | 6673 | 84 | 6589
    rtk read - --head-lines 5 < big.txt   →  cat - (stdin) | rtk read - | 6673 | 82 | 6591
    

    (original_cmd | rtk_cmd | input_tokens | output_tokens | saved_tokens, from a head build of #4367 at 4c292b2; develop 6f5503d is the same.) The hook rewrites none of cat big.txt | head -5, head -5 < big.txt or tail -n 5 < big.txt (all exit 1), so only a typed rtk read - reaches this today. It is the same rule as this issue's first bullet: the baseline for a windowed read is the window.

  4. KuSh commented on Oct 18, 2026

    @KuSh
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions