feat(autoresearch): expand Claude RTK rewriting for chains, guarded find, and rg - #3773
feat(autoresearch): expand Claude RTK rewriting for chains, guarded find, and rg#3773MattPua wants to merge 7 commits into
Conversation
Rewrite eligible segments inside top-level &&/; chains, guard rtk find behind an allowlist of supported primaries with a pipefail pipe fallback for pure file-list shapes, and route rg through native rtk rg when it is a real PATH executable (pipe fallback when it is a shell function). Adds scripts/rtk-bench.mjs, a deterministic benchmark applying the real hook policy. Generated-By: PostHog Code Task-Id: 5330cf5e-2ca2-4f33-82e9-173dd4a01af6
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
The Codex adapter has no command-rewrite channel, so its instruction-level guidance must track the hook policy: advertise the find predicate allowlist (rtk hard-fails on -not/-exec/! shapes), allow prefixing eligible segments of && chains, and advertise rg only when it is a real PATH executable, since rtk rg execs rg and fails where rg is only a shell function. Generated-By: PostHog Code Task-Id: 5330cf5e-2ca2-4f33-82e9-173dd4a01af6
Proves RTK compression preserves the information the agent needs, end to end: three sequential turns in one session against a seeded ~130-file repo must recover an exact file+line from compressed grep output, an exact file count through the find -not pipe fallback, and per-file git state from a compressed && chain — asserted as JSON side effects against seeded ground truth, never prose. RTK_DB_PATH isolates rtk telemetry so rtk gain must report tracked commands with positive savings, making a silently disabled hook fail instead of passing vacuously. Generated-By: PostHog Code Task-Id: 5330cf5e-2ca2-4f33-82e9-173dd4a01af6
…rolled bench, executable PATH check Review found the pipe fallbacks silently truncate large result sets (10 files per dir with +N markers), the bench had no main-policy control and could count erroring rewrites as savings, PATH detection accepted non-executable files, and the e2e sailed under rtk's truncation cap. Drops both pipe fallbacks: rtk-unsupported finds now run raw (they are used when the complete matched set matters) and rg routes only through native rtk rg when a real executable is on PATH, which matches the long-shipped grep proxy's caps and reports uncapped totals. findOnPath requires the execute bit. The bench now runs main's policy as a control arm via git show, captures exit status and stderr, and fidelity-gates every row (exit parity, output presence, per-row facts) — candidate-only failures fail the run, baseline-only failures are flagged as fixed. The e2e gains a fourth turn recovering an exact match total past rtk's 200-result cap, where only the uncapped summary header carries the truth. Generated-By: PostHog Code Task-Id: 5330cf5e-2ca2-4f33-82e9-173dd4a01af6
… bench facts, rg flag normalization Review found the segment splitter treats ; and && inside heredoc bodies and $(...) substitutions as separators, rewriting text that is document content or programmatically consumed output; the bench fidelity facts accepted any numeric summary; and rtk intercepts rg -h/--help/-V itself, changing command meaning. The splitter now refuses multiline input entirely and tracks paren depth, failing safe on unbalanced parens (case arms included). The rg guard normalizes clustered short flags and =-attached long forms and blocks help/version, mirrored into the Codex guidance. Bench facts are now exact values derived from raw output (match totals for anchored patterns, per-file git status facts, full passthrough line presence for under-cap rg), stderr counts toward token cost, temp paths are per-run private dirs with an upfront shim probe, and the baseline ref falls back to origin/main. Also strips shell escapes in find token guards and guards e2e cleanup against partial setup. Generated-By: PostHog Code Task-Id: 5330cf5e-2ca2-4f33-82e9-173dd4a01af6
Generated-By: PostHog Code Task-Id: b24a0c15-8e64-4fd4-a9eb-b8528f3bb9f4
Generated-By: PostHog Code Task-Id: b24a0c15-8e64-4fd4-a9eb-b8528f3bb9f4
|
Reviews (1): Last reviewed commit: "chore(agent): clarify RTK benchmark scop..." | Re-trigger Greptile |
|
This PR has had no activity for 7 days and has been marked stale. We are moving to the monorepo and tightening PR staleness in preparation, so it will be closed in 7 days if no further activity occurs. |
|
Hey @MattPua, we've migrated this repo into the PostHog/posthog monorepo, so this PR needs to be recreated there. Easiest path: check out the monorepo and run the Shout if you get stuck. |
Purpose
This PR reduces Claude agent context usage by routing more eligible Bash commands through RTK. It does not change RTK's compression algorithm. It fixes which commands safely reach it:
&∧chains instead of skipping the whole chainrgcommands through native RTKfindpredicates and unsafe shell shapes rawCodex receives matching developer guidance, but has no deterministic command-rewrite hook. The measured savings below apply only to Claude sessions.
Important
Production-weighted upper estimate: 13.6% less Bash output than
main.Across the measurable 30-day production sample, that is approximately 1.58 MB less output, or 396k estimated command-output tokens.
This weights observed production output bytes by command shape. Actual savings may be lower because a chain can contain both eligible and ineligible segments.
What changes
&∧chainsrgthrough native RTK only when a real executable existsfindpredicates RTK supportsmainand a live context-fidelity E2EBefore and after
1. Large
rgsearches: 86.4% less outputrg -n '^import' packages/core/srcmain1,474 matches in 511 filesOn
main: all 1,474 repetitive matches enter the conversationWith this PR: one exact summary plus 200 representative matches enter the conversation
Important
The agent keeps the exact answer,
1,474 matches across 511 files, while 86.4% of the output is removed from context.2. Batched commands: compression now works inside chains
On
main: the presence of&&disables RTK for the entire lineWith this PR: every eligible segment is compressed independently
Important
This small worktree saves 8.5%. The same change unlocks compression for the 28.9% of production Bash calls containing
&&, with larger savings when their output is larger.Command order, output facts, exit codes, and
&&short-circuit behavior remain unchanged.3. Unsupported
find: error becomes complete resultsOn
main: RTK receives syntax it does not supportWith this PR: the unsupported shape stays raw
Important
The result changes from an error with no data to all 102 requested files, avoiding a failed turn and retry.
Results
Production-calibrated estimate
The measurable 30-day Bash-span sample contains 7,745 calls and 11.7 MB of actual output. Unlike the larger command-frequency analysis below, this sample includes output payloads and can be weighted by bytes.
rgApplied to the sample's 11.7 MB of Bash output:
rg: approximately 72 KB removedWarning
This is not an observed production saving. It applies fidelity-validated fixture compression rates to production output-byte shares. It is best treated as an upper estimate until a production shadow evaluation or post-rollout measurement can compare actual output before and after rewriting.
Safety
&&short-circuit behaviorrgoutput modes rawrgthrough RTK only when RTK can execute a realrgbinaryfindpredicates raw instead of replacing results with an RTK errorHow did you test this?
117/117scoped RTK unit tests passed0find -not5/55/55/55/54/5findcase. Claude counted0instead of9once; RTK did not rewrite that command.Automatic notifications
Created with PostHog Code