Hook Engine + Chained Command Rewriting (PR #131 Part 1)#156
Hook Engine + Chained Command Rewriting (PR #131 Part 1)#156ahundt wants to merge 261 commits intortk-ai:masterfrom
Conversation
Add comprehensive support for modern JS/TS development stack: Commands added: - rtk lint: ESLint/Biome output with grouped rule violations (84% reduction) - rtk tsc: TypeScript compiler errors grouped by file (83% reduction) - rtk next: Next.js build output with route/bundle metrics (87% reduction) - rtk prettier: Format checker showing only files needing changes (70% reduction) - rtk playwright: E2E test results showing failures only (94% reduction) - rtk prisma: Prisma CLI without ASCII art (88% reduction) Features: - Auto-detects package managers (pnpm/yarn/npm/npx) - Preserves exit codes for CI/CD compatibility - Groups errors by file and error code for quick navigation - Strips verbose output while retaining critical information Total: 6 new commands, ~2,000 LOC Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Document the 6 new commands and shared utils module in CHANGELOG.md. Focuses on token reduction metrics and CI/CD compatibility. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add benchmarks for the 6 new commands in scripts/benchmark.sh: - tsc: TypeScript compiler error grouping - prettier: Format checker with file filtering - lint: ESLint/Biome grouped violations - next: Next.js build metrics extraction - playwright: E2E test failure filtering - prisma: Prisma CLI without ASCII art All benchmarks are conditional (skip if tools not available or not applicable to current project). Tests only run on projects with package.json and relevant configuration files. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Implements heuristic calculation of monthly quota savings percentage with support for Pro, Max 5x, and Max 20x subscription tiers. Features: - --quota flag displays monthly quota analysis - --tier <pro|5x|20x> selects subscription tier (default: 20x) - Heuristic based on ~44K tokens/5h Pro baseline - Estimates: Pro=6M, 5x=30M, 20x=120M tokens/month - Clear disclaimer about rolling 5-hour windows vs monthly caps Example output for Max 20x: Subscription tier: Max 20x ($200/mo) Estimated monthly quota: 120.0M Tokens saved (lifetime): 356.7K Quota preserved: 0.3% Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
feat: add modern JavaScript tooling support (lint, tsc, next, prettier, playwright, prisma)
Add rtk gh command for GitHub CLI operations with intelligent output filtering: Commands: - rtk gh pr list/view/checks/status: PR management (53-87% reduction) - rtk gh issue list/view: Issue tracking (26% reduction) - rtk gh run list/view: Workflow monitoring (82% reduction) - rtk gh repo view: Repository info (29% reduction) Features: - Level 1 optimizations (default): Remove header counts, @ prefix, compact mergeable status (+12-18% savings, zero UX loss) - Level 2 optimizations (--ultra-compact flag): ASCII icons, inline checks format (+22% total savings on PR view) - GraphQL response parsing and grouping - Preserves all critical information for code review Token Savings (validated on production repo): - rtk gh pr view: 87% (24.7K → 3.2K chars) - rtk gh pr checks: 79% (8.9K → 1.8K chars) - rtk gh run list: 82% (10.2K → 1.8K chars) Global --ultra-compact flag added to enable Level 2 optimizations across all GitHub commands. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Add utils.rs as Key Architectural Component - Expand Module Responsibilities table (7→17 modules) - Document PR rtk-ai#9 in Fork-Specific Features section - Include token reduction metrics for all new commands
Change dtolnay/rust-action to dtolnay/rust-toolchain (correct name)
feat: add GitHub CLI integration (depends on rtk-ai#9)
feat: add quota analysis with multi-tier support
## Release automation - Add release-please workflow for automatic semantic versioning - Configure release.yml to only trigger on tags (avoid double-release) ## Benchmark automation - Extend benchmark.yml with README auto-update - Add permissions for contents and pull-requests writes - Auto-create PR with updated metrics via peter-evans/create-pull-request - Add scripts/update-readme-metrics.sh for CI integration ## Verification - ✅ Workflows ready for CI/CD pipeline - ✅ No breaking changes to existing functionality Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
feat: CI/CD automation (versioning, benchmarks, README auto-update)
…s--master--components--rtk chore(master): release 0.3.0
## Fixes ### Lint crash handling - Add graceful error handling for linter crashes (SIGABRT, OOM) - Display warning message when process terminates abnormally - Show first 5 lines of stderr for debugging context ### Grep command - Add --type/-t flag for file type filtering (e.g., --type ts, --type py) - Passes --type argument to ripgrep for efficient filtering ### Find command - Add --type/-t flag for file/directory filtering - Default: "f" (files only) - Options: "f" (file), "d" (directory) ## Testing - ✅ cargo check passes - ✅ cargo build --release succeeds - ✅ rtk grep --help shows --file-type flag - ✅ rtk find --help shows --file-type flag with default ## Breaking Changes None - all changes are backwards compatible additions Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…d-bugs fix: improve command robustness and flag support
…s--master--components--rtk chore(master): release 0.3.1
## Overview Complete architectural documentation (1133 lines) covering all 30 modules, design patterns, and extensibility guidelines. ## Critical Fixes (🔴) - ✅ Module count: 30 documented (not 27) - added deps, env_cmd, find_cmd, local_llm, summary, wget_cmd - ✅ Language: Fully translated to English for consistency with README.md - ✅ Shared Infrastructure: New section documenting utils.rs and package manager detection - ✅ Exit codes: Correct documentation (git.rs preserves exit codes for CI/CD) - ✅ Database: Correct path ~/.local/share/rtk/history.db (not tracking.db) ## Important Additions (🟡) - ✅ Global Flags Architecture: Verbosity (-v/-vv/-vvv) and ultra-compact (-u) - ✅ Complete patterns: Package manager detection, exit code preservation, lazy static regex - ✅ Config system: TOML format documented - ✅ Performance: Verified binary size (4.1 MB) and estimated overhead - ✅ Filter levels: Before/after examples with Rust code ## Bonus Improvements (🟢) - ✅ Table of Contents (12 sections) - ✅ Extensibility Guide (7-step process for adding commands) - ✅ Architecture Decision Records (Why Rust? Why SQLite?) - ✅ Glossary (7 technical terms) - ✅ Module Development Pattern (template + 3 common patterns) - ✅ 15+ ASCII diagrams for visual clarity ## Stats - Lines: 1133 (+118% vs original 520) - Sections: 12 main + subsections - Code examples: 10+ Rust/bash snippets - Accuracy: 100% verified against source code Production-ready for new contributors, experienced developers, and LLM teams. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
docs: add comprehensive ARCHITECTURE.md v2.0
Updates documentation to reflect all features added in recent PRs: **Version Updates** - Update installation commands to v0.3.1 (DEB/RPM packages) **New Sections** - Add Global Flags section (-u/--ultra-compact, -v/--verbose) - Add JavaScript/TypeScript Stack section (10 new commands) **New Commands Documented** - Files: `rtk smart` (heuristic code summary) - Commands: `rtk gh` (GitHub CLI), `rtk wget`, `rtk config` - Data: `rtk gain --quota` and `--tier` flags - Containers: `rtk kubectl services` - JS/TS Stack: lint, tsc, next, prettier, vitest, playwright, prisma **Features Coverage** This update documents functionality from: - PR rtk-ai#5: Git argument parsing improvements - PR rtk-ai#6: pnpm support - PR rtk-ai#9: Modern JavaScript/TypeScript stack support - PR rtk-ai#10: GitHub CLI integration - PR rtk-ai#11: Quota analysis features - PR rtk-ai#14: Additional command improvements All commands documented are available in v0.3.1. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…update docs: comprehensive documentation update for v0.3.1
Add automated workflow step to update the 'latest' tag after each successful release. This ensures 'latest' always points to the most recent stable version without manual intervention. The new job: - Runs after successful release completion - Updates 'latest' tag to point to the new semver tag - Uses force push to move the tag reference - Includes version info in tag annotation message Benefits: - Install scripts can reliably use /releases/latest/download/ - No manual tag management needed - Consistent reference for "current stable" across platforms Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…-tag ci: automate 'latest' tag update on releases
…tics Implement day-by-day, week-by-week, and monthly breakdowns with JSON/CSV export capabilities for in-depth token savings analysis and reporting. New Features: - Daily breakdown (--daily): Complete day-by-day statistics without 30-day limit - Weekly breakdown (--weekly): Sunday-to-Saturday week aggregations with date ranges - Monthly breakdown (--monthly): Calendar month aggregations (YYYY-MM format) - Combined view (--all): All temporal breakdowns in single output - JSON export (--format json): Structured data for APIs, dashboards, scripts - CSV export (--format csv): Tabular data for Excel, Google Sheets, data science Technical Implementation: - src/tracking.rs: Add DayStats, WeekStats, MonthStats structures with Serialize - src/tracking.rs: Implement get_all_days(), get_by_week(), get_by_month() SQL queries - src/main.rs: Extend Commands::Gain with --daily, --weekly, --monthly, --all, --format flags - src/gain.rs: Add print_daily_full(), print_weekly(), print_monthly() display functions - src/gain.rs: Implement export_json() and export_csv() for data export Documentation: - docs/AUDIT_GUIDE.md: Comprehensive guide with examples, workflows, integrations - README.md: Update Data section with new audit commands and export formats - claudedocs/audit-feature-summary.md: Technical summary and implementation details Database Scope: - Global machine storage: ~/.local/share/rtk/history.db - Shared across all projects, worktrees, and Claude sessions - 90-day retention policy with automatic cleanup - SQLite with indexed timestamp for fast aggregations Use Cases: - Trend analysis: identify daily/weekly patterns in token usage - Cost reporting: monthly savings reports for budget tracking - Data science: export CSV/JSON for pandas, R, Excel analysis - Dashboards: integrate JSON export with Chart.js, D3.js, Grafana - CI/CD: automated weekly/monthly savings reports via GitHub Actions Examples: rtk gain --daily # Day-by-day breakdown rtk gain --weekly # Weekly aggregations rtk gain --all # All breakdowns combined rtk gain --all --format json | jq . # JSON export with jq rtk gain --all --format csv # CSV for Excel/analysis Backwards Compatibility: - All existing flags (--graph, --history, --quota) preserved - Default behavior unchanged (summary view) - No database migration required - Zero breaking changes Performance: - Efficient SQL aggregations with timestamp index - No impact on rtk command execution speed - Instant queries even with 90 days of data Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…dit-system feat: Comprehensive Temporal Audit System for Token Savings Analytics
…s--master--components--rtk chore(master): release 0.4.0
Implement `rtk cc-economics` command combining ccusage spending data with rtk savings analytics for economic impact reporting. Features: - Dual metric system (active vs blended cost-per-token) - Daily/weekly/monthly granularity with ISO-8601 week alignment - JSON/CSV export support for data analysis - Graceful degradation when ccusage unavailable - Real-time data merge with O(n+m) HashMap performance Architecture: - src/ccusage.rs: Isolated ccusage CLI interface (7 tests) - src/cc_economics.rs: Business logic + display (10 tests) - src/utils.rs: Shared formatting utilities (8 tests) - Refactored gain.rs to use shared format_tokens() Test coverage: 17 new tests, all passing Validated with real-world data (2 months, $3.4K spent, 1.2M saved) Addresses: #economics-integration Impact: 24.4% cost savings identified ($830.91 active pricing) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Apply systematic quality fixes identified in code audit: Phase 1: Remove dead code (4 warnings → 0) - Remove unused run_compact() function in gain.rs - Remove unused track_tokens() function in tracking.rs - Remove unused TRACKER singleton (Mutex) - Clean up CommandRecord struct (remove unused fields) Phase 2: Add error context and quality fixes - Add .context() to all ? operators in cc_economics.rs (15+ callsites) - Add .context() to all ? operators in gain.rs (10+ callsites) - Fix bounds check panic risk in gain.rs:252 (week_start slice) - Reduce visibility: estimate_tokens pub → private - Replace 6 manual loops with idiomatic .collect::<Result<Vec<_>, _>>()? - Remove residual dev comment (// psk:) - Remove unnecessary .clone() in main.rs Phase 3: Refactor duplication (132 lines eliminated) - Create display_helpers.rs module with PeriodStats trait - Unify print_daily_full/weekly/monthly in gain.rs (152 lines → 9 lines) - Implement trait for DayStats, WeekStats, MonthStats - Zero-overhead compile-time dispatch (monomorphization) - Output bit-identical, all tests passing Impact: - Dead code: -20 lines - Error context: Errors now actionable instead of opaque - Duplication: -132 lines of pure duplication - Safety: Bounds check prevents potential panic - Idiomaticity: .collect() over manual loops Metrics: - Build: 0 errors, 1 warning (pre-existing cache_* fields) - Tests: 79/82 passing (3 pre-existing failures) - Clippy: 13 warnings (all pre-existing) - Functional: rtk gain --daily, rtk cc-economics validated Addresses code audit feedback from parallel session Detailed report: claudedocs/refactoring-report.md Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…ch tables pipe_cmd.rs: - Add mypy, ruff-check, ruff-format, prettier to resolve_filter() - Add auto-detect heuristic for mypy output (.py + ": error:" pattern) - Update error message and doc comment with new filter names - Add tests for all new resolve_filter entries and auto-detect cmd/filters.rs: - Add mypy, ruff, golangci-lint to get_filter_type() -> FilterType::Test - Add same to get_filter_mode() -> Buffered(filter_test_output) - Add tests for new filter type and mode entries These were integration gaps where new upstream commands (mypy) and existing branch commands (ruff, prettier, golangci-lint) had dedicated modules but were missing from the pipe filter dispatch and fallback filter tables used by `rtk pipe` and `rtk run -c`.
- Add wc Route entry (binary: wc, subcmds: Any, rtk_cmd: wc) - Add wc PATTERN regex and RULE (category: Files, 40% savings) - Remove "wc " from IGNORED_PREFIXES since wc now has a dedicated module - Add tests: test_classify_wc, test_classify_wc_bare, test_route_wc The wc command was added upstream as a dedicated module (wc_cmd.rs) but was still in the ignored prefixes list, preventing hook rewriting of bare `wc` commands to `rtk wc`.
…e tests Previous behavior: - filters.rs had dead `FilterType` enum, `get_filter_type()`, and `apply_to_string()` that were fully superseded by `get_filter_mode()` - stream.rs used `.filter_map(Result::ok)` which silently skips I/O errors and can spin indefinitely on a broken pipe fd - `get_filter_mode()` was missing entries for npm/npx/pnpm and go - `FORMAT_PRESERVING`/`TRANSPARENT_SINKS` were pub(crate) but only used in tests, triggering dead code warnings - Several bare `matches!()` calls in tests were not wrapped in `assert!()` making them no-ops that never actually validated anything What changed: - src/cmd/filters.rs: remove dead FilterType/get_filter_type/apply_to_string (get_filter_mode is the sole dispatch path via exec.rs:148). Add npm/npx/ pnpm streaming ANSI-strip entry. Move truncate_lines into #[cfg(test)]. Wrap all bare matches!() in assert!(). Add 6 edge case tests: go passthrough, npx streaming, npm ANSI strip, empty test output, pure Compiling output, test separator lines. - src/stream.rs: change all 5 .filter_map(Result::ok) to .map_while(Result::ok) on BufReader::lines() — stops iteration on first I/O error instead of spinning. Safe because pipes from std::process::Child are blocking fds where EINTR is retried by std::io internally and EAGAIN cannot occur. - src/cmd/hook.rs: move FORMAT_PRESERVING/TRANSPARENT_SINKS behind #[cfg(test)]. Merge assert_blocked helpers. Split depth limit test. - src/cmd/exec.rs: wire predicates::is_interactive() and has_unstaged_changes() into verbose>1 debug logging (eliminates dead code). - src/cmd/predicates.rs: strengthen test assertions (was `let _ =`, now actually verifies behavior). - src/gain.rs: split get_summary/get_recent dispatch to use unfiltered variants when no project scope. - src/tracking.rs: add CommandStats type alias for readability. - src/init.rs: fix clippy warnings (repeat_n, remove unused vars). - 13 upstream files: mechanical clippy auto-fixes (map_or→is_some_and, last→next_back, collapsed else-if, derived Default, etc.) Why: branch-specific code accumulated clippy warnings after merge with upstream/master. The filter_map→map_while fix addresses a real bug where repeated I/O errors could cause infinite loops. Dead code removal reduces maintenance surface. Edge case tests guard against regressions like the go-passthrough bug caught during review. Files affected: - src/cmd/filters.rs: dead code removal, npm/pnpm entry, 6 new tests - src/stream.rs: filter_map→map_while at 5 sites - src/cmd/hook.rs: cfg(test) constants, test helper merge - src/cmd/exec.rs: verbose debug logging with predicates - src/cmd/predicates.rs: stronger test assertions - src/gain.rs: summary/recent dispatch split - src/tracking.rs: CommandStats type alias - src/init.rs: clippy fixes (repeat_n, unused vars) - 13 upstream files: mechanical clippy auto-fixes Testable: cargo test --all (814 passed, 0 failed)
|
@pszymkowiak conflicts resolved! all tests pass |
… backup registry
Same fix as feat/multi-platform-hooks: makes RTK the sole Bash hook responder
by patching plugin caches, then calling registered handlers via manifest fallthrough.
Root cause: parallel Bash hooks from settings.json (RTK) and plugin cache (autorun)
caused Claude Code to drop updatedInput from both. RTK rewrites were silently lost.
What changed (identical to multi-platform-hooks branch except no Gemini hook):
src/init.rs:
- Add `patch_plugin_caches()`: scans ~/.claude/plugins/cache/*/*/hooks/*.json,
removes "Bash" from PreToolUse matchers, resolves ${CLAUDE_PLUGIN_ROOT},
writes ~/.claude/hooks/rtk-bash-manifest.json
- Add `patch_single_cache_file()`: idempotent; guards against empty-matcher result
- Add `restore_plugin_caches_from_manifest()`: uninstall restores original matchers;
skips atomic_write when patched matcher not found (no unnecessary file touches)
- Add `backup_file_once()`: backs up to <file>.rtk-backup; never overwrites existing;
registers path in ~/.claude/hooks/rtk-backups.json via append_to_backup_registry()
- Add `append_to_backup_registry()` / `read_backup_registry()` / `print_backup_registry()`:
persistent, deduplicated backup registry; printed at end of install and uninstall
- Replace `.json.bak` fatal copy with `let _ = backup_file_once()`
- `insert_hook_entry()`: changed from `-> ()` with `.expect()` panics to `-> Result<()>`
with warn-and-overwrite guards for malformed "hooks"/"PreToolUse" fields
- `BashManifest`: `impl Default` (version=1), collapse init block to `.unwrap_or_default()`
- `map_or(false, |v| ...)` → `is_some_and(|v| ...)` (idiomatic/clippy)
- `to_string_lossy().to_string()` → `.into_owned()` (communicates ownership intent)
- `patch_plugin_caches()` called from `run_default_mode` and `run_hook_only_mode`
- `uninstall()`: restores plugin caches, removes manifest and Part 1 wrapper,
prints preserved backup paths from registry
src/cmd/claude_hook.rs:
- `run()` reads stdin once; passes buffer to `run_inner(buffer: &str)`
- Add `run_manifest_fallthrough(payload)`: called on NoOpinion only; spawns each
fallthrough_command via `sh -c`; gates exit(2) on write_ok; inherits stdout/stderr
- Invariant comment: only called on NoOpinion, never on Allow/Deny paths
Testable:
- `rtk init -g` → "Plugin caches: N patched", backup paths listed
- Re-run → "already up-to-date (re-run safe)", same backup list
- New Claude Code session: `git status` → rewrites to `rtk git status`
- `rtk init -g --uninstall` → restored caches + preserved backups shown
Before: `cargo build &` classified entirely as Shellism → shell passthrough with 0% token savings. The `&` was never stripped as a suffix. What changed: - `split_safe_suffix()`: add 1-token `&` pattern with Shellism-in-core guard - `cargo build &` → core=[cargo,build], suffix="&" → `rtk cargo build &` (savings) - `cargo build 2>&1 &` → core has Shellism from `>&1` → guard fires → no strip → shell handles - Doc comment: add `&` to the recognized patterns list with caveat - 4 new regression tests: - `test_background_job_suffix_simple`: verifies stripping and clean core - `test_background_job_suffix_git_status`: RTK-routed command with trailing & - `test_background_job_suffix_blocked_by_fd_redirect_shellism`: `2>&1 &` guard - `test_background_job_suffix_single_token_not_stripped`: bare `&` edge case (n<2 guard) Why: The guard is critical. `cargo build 2>&1` already has `&` as Shellism in the `2>&1` tokens. Without the guard, stripping the trailing `&` would leave the `2>&1` Shellism in the core causing a double-passthrough instead of the simpler correct behavior (shell handles the whole command).
Move protocol-specific modules into src/cmd/hook/ to group LLM adapters together and mirror the structure of PR rtk-ai#150's src/hook/ directory: - hook.rs → hook/mod.rs (dispatch engine becomes module root) - claude_hook.rs → hook/claude.rs (protocol adapter, no _hook suffix) - trash_cmd.rs → trash.rs (no _cmd suffix, consistent with builtins.rs) Update module declarations (cmd/mod.rs), dispatch (main.rs:1523), and internal import path in hook/claude.rs (super::hook:: → super::). 818 tests pass; pre-existing warnings unchanged.
…ation (Part 4) Add tests and fixes identified during PR comparison review with rtk-ai#150 and rtk-ai#241: analysis.rs: - test_cargo_test_pipe_grep_is_not_safe_suffix: regression guard for pipe-to-grep routing — verifies "cargo test | grep FAILED" is not classified as a safe suffix and triggers shell passthrough (pipe target is format-sensitive) - test_nohup_background_strips_ampersand: edge case — "nohup cargo build &" strips trailing & as safe suffix; core [nohup, cargo, build] does not require shell builtins.rs: - is_valid_env_name(): POSIX shell identifier validation [A-Za-z_][A-Za-z0-9_]* - builtin_export(): rejects invalid identifiers (e.g. "123=x") with fail-open behavior (silent skip, never error — preserves RTK's fail-open principle) - tests: test_export_invalid_identifier_ignored, test_export_empty_name_ignored, test_is_valid_env_name (7 assertions covering valid/invalid identifier patterns) hook/mod.rs: - test_cat_multi_file_rewrites_to_rtk_read: documents that on this branch (without data-safety rules), cat → rtk read for all arities via defensive fallback in route_native_command(); contrasts with feat/multi-platform-hooks where cat is Blocked by src/rules/rtk.safety.block-cat.md - test_cat_single_file_rewrites_to_rtk_read: same fallback path, single-file case All 825 tests pass.
…parison notes (Part 4) Add tests and fixes identified during PR comparison review with rtk-ai#150 and rtk-ai#241: analysis.rs: - test_cargo_test_pipe_grep_is_not_safe_suffix: regression guard — "cargo test | grep FAILED" must not be classified as a safe suffix (pipe target is format-sensitive) - test_nohup_background_strips_ampersand: edge case — "nohup cargo build &" strips trailing & as safe suffix; core [nohup, cargo, build] routes to passthrough (correct) builtins.rs: - is_valid_env_name(): POSIX identifier validation [A-Za-z_][A-Za-z0-9_]* - builtin_export(): rejects invalid identifiers (e.g. "123=x") fail-open (silent skip) - tests: test_export_invalid_identifier_ignored, test_export_empty_name_ignored, test_is_valid_env_name (7 assertions for valid/invalid identifier edge cases) hook/mod.rs: - test_cat_multi_file_is_blocked: cat is blocked by src/rules/rtk.safety.block-cat.md; defensive cat→rtk-read fallback in route_native_command() exercises only with RTK_BLOCK_TOKEN_WASTE=0; verified actual runtime behavior is Blocked - test_cat_single_file_is_blocked: same blocking rule applies regardless of arity .claude/notes/hook-pr-comparison.md: - Mark export identifier validation as fixed (with test names) - Correct audit logging comparison: rtk-ai#156 has equivalent via RTK_AUDIT_DIR / hook_audit_cmd.rs (always-on, dir-based) vs rtk-ai#241's RTK_HOOK_AUDIT=1 (flag-gated) - Update "What rtk-ai#241 brings" section: oracle model is rtk-ai#156's gap; audit logging is parity - Update "What rtk-ai#241 has over both" with accurate parity note for audit logging All 913 tests pass.
… path resolution (v2)
Same fixes as main branch but for feat/rust-hooks-v2 which uses rtk-rewrite.sh
as the primary hook (phased-transition design per reviewer).
RC2 (binary hook Allow arm): Added run_manifest_handlers() replacing
run_manifest_fallthrough() in src/cmd/hook/claude.rs. Both NoOpinion AND
Allow arms now call all manifest handlers. Deny from any handler wins over
RTK rewrite. Added is_json_deny() with CC + Gemini dual-format detection.
RC3 (manifest gap): Fixed patch_single_cache_file() reconstruction path for
when Bash was already removed from plugin PreToolUse matchers.
Plugin path resolution: Fixed resolve_plugin_root_in_command() with 3-level
fallback (plugin_name -> vendor_name -> scan for hooks/ dir) to handle the
ar/autorun naming mismatch in cache vs source directory.
RC1 (shell hook): Added parallel-merge coordinator logic to hooks/rtk-rewrite.sh
with BEGIN/END_RTK_BASH_HANDLERS markers for rtk init to populate handlers.
init.rs fixes for v2:
- Fixed patch_settings_json() to use script path instead of hardcoded binary
- Added extract_handler_section() + merge_hook_with_handlers() for upgrade safety
- ensure_hook_installed() now preserves registered handlers across script upgrades
--hook-type flag: Added HookType{Binary,Script} with default Script for v2.
Add check_environment() and report_env_issues() to src/init.rs, called at the start of run_default_mode() and run_hook_only_mode() (Unix paths) before any files are modified. Same checks as feat/multi-platform-hooks: - $HOME set and ~/.claude/ exists (Hard) - ~/.claude/settings.json exists (Soft) - jq on PATH for --hook-type script (Hard; default in this branch) - rtk on PATH (Hard) On hard failures: prints ❌ SETUP REQUIRED with numbered steps and links (docs.anthropic.com/en/docs/claude-code/hooks, jqlang.org/download/), then bails. Soft warnings continue. Includes tip to paste output into AI.
Same fixes as feat/multi-platform-hooks: Issue 1 — wrong rtk binary (name collision) not detected: Replace `command -v rtk` with `rtk hook --help` probe. If rtk is on PATH but hook subcommand fails, report "wrong package installed". Issue 2 — spurious settings.json soft warning on new installs: Remove settings.json check; patch_settings_shared creates it if absent. Issue 3 — stale docs.anthropic.com links: Update to code.claude.com/docs/en/* (current canonical domain). Issue 4 — jq PATH false positive on Homebrew + .zshrc-only PATH: Add note to check ~/.zprofile vs .zshrc for PATH exports. Issue 5 — PATH instructions pointed at wrong shell profile: Use ~/.zprofile (macOS) / ~/.profile (Linux) in rtk-not-found message.
Previous behavior: check_environment() hardcoded ~/.zprofile (macOS) /
~/.profile (Linux) in two places — jq PATH hint and rtk-not-found PATH
setup. This gave wrong instructions to fish, nushell, and other users.
Also used `sh -c "command -v rtk"` for rtk on-PATH probe.
What changed (src/init.rs):
- Added path_setup_instructions(cargo_bin: &str) -> Vec<String>: reads
$SHELL, dispatches to zsh (.zprofile), bash (.bash_profile), fish
(fish_add_path), nushell (env.nu), and generic POSIX fallback.
- Added jq_path_profile_hint() -> String: same $SHELL dispatch for
jq-not-detected advisory (replaces hardcoded .zprofile/.zshrc/.bashrc).
- check_environment() jq block: replaced hardcoded profile strings with
jq_path_profile_hint(); added instrs.retain() to drop empty strings.
- check_environment() rtk-not-found block: replaced hardcoded export /
reload advice with path_setup_instructions(&cargo_bin).
- rtk on-PATH probe: Command::new("which").arg("rtk") replaces
sh -c "command -v rtk" (avoids extra shell subprocess).
…helpers
Previous behavior: Each filter module had its own local which_command() that
called Command::new("which") directly — broken on Windows where the command is
"where", not "which". hook_audit_cmd.rs used HOME→"/tmp" fallback (no /tmp on
Windows). claude.rs manifest_path() used HOME without USERPROFILE fallback.
What changed:
- src/utils.rs: added two public helpers command_in_path(cmd) and
which_command(cmd) that dispatch between "which" (Unix) and "where"
(Windows) via cfg!(windows); which_command() takes only the first line
of `where` output to handle Windows returning all matches
- src/utils.rs: package_manager_exec() now uses command_in_path() instead
of inline Command::new("which")
- src/next_cmd.rs, tsc_cmd.rs, prisma_cmd.rs, ccusage.rs, tree.rs: each
replaced inline Command::new("which") with crate::utils::command_in_path()
- src/pytest_cmd.rs, pip_cmd.rs, mypy_cmd.rs: each replaced local 8-12 line
which_command() duplicate with single-line crate::utils::which_command()
delegation
- src/hook_audit_cmd.rs: replaced HOME→"/tmp" fallback with
dirs::data_local_dir() (→ %APPDATA% on Windows, ~/.local/share on Linux)
with temp_dir() as last resort — no /tmp hardcoding
- src/cmd/hook/claude.rs: manifest_path() adds USERPROFILE fallback when
HOME is not set (Windows standard home env var)
Why: RTK has cross-platform CI (macOS, Linux x86_64/ARM64, Windows) but
several modules silently broke on Windows due to "which" not existing there.
Consolidating into utils.rs helpers ensures every future module gets
cross-platform path probing for free. Note: mypy_cmd.rs is v2-only.
Files affected:
- src/utils.rs: +38/-5 (new helpers + updated package_manager_exec)
- src/next_cmd.rs, tsc_cmd.rs, prisma_cmd.rs, ccusage.rs, tree.rs: -4 each
- src/pytest_cmd.rs, pip_cmd.rs: -8 each (remove duplicate which_command)
- src/mypy_cmd.rs: -8 (remove duplicate which_command, v2-only file)
- src/hook_audit_cmd.rs: +6/-2 (dirs::data_local_dir instead of HOME+/tmp)
- src/cmd/hook/claude.rs: +4/-1 (USERPROFILE fallback in manifest_path)
|
Hi @ahundt! PRs #156 and #158 are showing as CONFLICTING after recent merges. Before we can review the full series (#156, #157, #158), they need to be rebased. Also, given the scope (Hook Engine + Data Safety Rules + Gemini = ~28K lines across 100+ files), could we discuss the rollout strategy in an issue first? We want to make sure we integrate this thoughtfully rather than all at once. |
Conflict resolutions (6 files): - Cargo.lock: theirs (new sha2, reqwest, reqwest-blocking crates for integrity+telemetry) - src/go_cmd.rs: theirs (wording update to build_go_test_summary) + pub visibility restored for filter_go_build, filter_go_test_json (called by pipe_cmd.rs) - src/main.rs: additive — keep Hook/Run/Pipe/HookCommands from v2 + add upstream Rewrite/Verify variants and match arms alongside existing variants - src/discover/registry.rs: theirs (has rewrite_command:252, rewrite_segment:429, rewrite_compound:279, rewrite_head_numeric:444, ENV_PREFIX:50, classify_command:54) + added wc tests (test_classify_wc, test_classify_wc_bare, test_route_wc) - src/init.rs: additive — keep v2's 4 test functions (test_extract_handler_section_*, test_merge_hook_with_handlers_*) + add upstream's guard-ordering assertion (jq_pos < rtk_delegate_pos) inside test_hook_has_guards - hooks/rtk-rewrite.sh: delete bash rewrite engine (lines 60-223, superseded by `rtk rewrite "$CMD"`); keep parallel-merge coordinator (lines 225-287) atop upstream thin delegator + no-change guard (`if [ "$CMD" = "$REWRITTEN" ]`) Post-merge compile fixes: - src/cmd/hook/mod.rs: replace registry::lookup() (removed in upstream) with hook_lookup() — conservative subcommand whitelist matching v2 test expectations. classify_command() (discover::registry) is for history analysis and routes too broadly (find, tree, wget, docker run/exec/build excluded by hook tests). Added wc, playwright, prisma, curl, pytest to hook_lookup. Lifetime annotation: hook_lookup<'a>(binary: &'a str, sub: &str) - src/discover/rules.rs: port wc RtkRule + pattern from v2 inline registry; remove "wc " from IGNORED_PREFIXES; PATTERNS[32] = r"^wc(\s|$)" Architecture note (preserved from v2, no regressions): - check_for_hook (binary hook, hook/mod.rs:61): uses Rust lexer + suffix-aware routing (split_safe_suffix) + special cases (vitest run injection, uv pip, python -m pytest). Routes via hook_lookup() whitelist. - rtk rewrite (script hook, rewrite_cmd.rs→registry::rewrite_command:252): uses simpler regex-based compound rewriting. Script hook routes via this. - Both share RULES table: binary via hook_lookup→RULES, script via rewrite_command→RULES. - route_native_command already called registry::lookup() in v2; now hook_lookup() replaces that with an equivalent conservative whitelist. - run_manifest_handlers (claude.rs:375): unchanged — deny wins over rewrite for both NoOpinion and Allow arms. Verified: 1002 tests passing (was 841 in v2 pre-merge), 0 failures. rtk rewrite "cargo test" → "rtk cargo test" ✓ rtk hook claude vitest → "rtk vitest run" (no regression) ✓ rtk hook claude "wc -l src/main.rs" → "rtk wc -l src/main.rs" ✓ shell hook: cargo test → updatedInput.command="rtk cargo test" ✓
- src/main.rs: remove stray blank line (rustfmt) - src/init.rs: reformat long lines to rustfmt style (semantic no-op) - Cargo.lock: add which/env_home/either/winsafe transitive deps for the which crate added in 83bbfd1 (which_command helper)
rtk git commit only accepted -m/--message; any other flag (-F <file>, --amend, --no-edit, -a, --no-verify, --allow-empty) caused a Clap parse error. Add extra_args: Vec<String> with trailing_var_arg + allow_hyphen_values to GitCommands::Commit and GitCommand::Commit. build_commit_command appends extra_args after the -m chain. run_commit includes them in the logged original_cmd string. Adds 6 tests: -F /tmp/msg.txt, --amend --no-edit, -m msg --amend, plus unit tests for build_commit_command with each new flag.
Conflict resolution (1 conflict, additive):
- src/discover/registry.rs: keep all tests from both sides
- ours: test_classify_wc, test_classify_wc_bare, test_route_wc
- upstream: test_rewrite_gh_json_skipped, test_rewrite_gh_jq_skipped,
test_rewrite_gh_template_skipped, test_rewrite_gh_api_json_skipped,
test_rewrite_gh_without_json_still_works (rtk-ai#196)
Upstream v0.27.0 changes absorbed:
- fix(registry): RTK_DISABLED=1 env prefix skips rewrite entirely (rtk-ai#345)
- fix(registry): gh --json/--jq/--template skips rewrite to avoid
corrupting structured output (rtk-ai#196)
- fix: RTK_DISABLED ignored, 2>&1 broken, json TOML error (rtk-ai#345,rtk-ai#346,rtk-ai#347)
- docs: version refs, module count, CHANGELOG, ARCHITECTURE
Also fixed pre-existing test race exposed by new parallel tests:
- fix(test): add EnvGuard to test_shared_is_hook_disabled_* in claude.rs
to prevent RTK_ACTIVE race with test_raii_guard_clears_on_panic
(both tests set RTK_ACTIVE without holding ENV_LOCK)
Tests: 1029 pass, 0 fail (parallel), 5 ignored
…nged Bug: rtk hook claude was routing gh pr list --json ... to rtk gh pr list --json ... which corrupts structured JSON output. Mirrors upstream fix registry::rewrite_segment rtk-ai#196 to the binary hook path. Fix: should_passthrough() returns true for gh commands containing --json, --jq, or --template flags — hook emits no output (NoOpinion) so Claude Code runs the original gh command unchanged. Tests: 2 new (test_gh_json_flag_passes_through, test_gh_without_json_not_passthrough); 1031 pass total
|
I updated it again, but new changes keep being merged every time i update it to resolve all the conflicts. Also there is far less code than it seems because there is a massive number of unit tests, check out #361 for details and the discussion as @FlorianBruniaux requested! |
Resolves stale merge-base from previous v0.27.0 merge (commit 598b5c7 lost its merge parent due to stash/pop clearing MERGE_HEAD). Conflict resolution (registry.rs only): - Keep wc tests (ours, not in upstream) - Accept upstream cargo fmt style for gh --json test 1031 tests pass, 0 failures.
Clap with `trailing_var_arg=true` consumes the `--` separator token, so `rtk cargo test -- --test-threads=1` would pass `--test-threads=1` to cargo without the preceding `--`, causing cargo to reject it as an unexpected argument. Same issue affected clippy (`-- -W ...`). Add `split_at_double_dash()` pure function that detects `--` in raw process args and computes the split point in Clap-parsed args. Wire via `build_cargo_args()` into `run_test()` and `run_cargo_filtered()`. 6 new unit tests, 1037 total pass.
…efore routing zsh builtins like `noglob`, `command`, `builtin`, `exec`, `nocorrect` modify execution of the NEXT command but are not standalone executables. The hook was wrapping them inside `rtk run -c 'noglob ...'` which fails because rtk cannot invoke shell builtins. Fix: detect shell prefix builtins in route_native_command(), strip the prefix, route the real command through the normal RTK routing table, then re-prepend the prefix. Same pattern as env prefix stripping. Example: `noglob gh release create v0.3.0-rc1 --title "..."` now correctly becomes `noglob rtk gh release create v0.3.0-rc1 --title "..."` instead of `rtk run -c 'noglob gh release create v0.3.0-rc1 ...'`. 5 TDD tests added covering noglob, command, builtin, nocorrect prefixes with both known RTK commands and unknown commands.
Add 6 new TDD tests covering edge cases for shell prefix builtin handling (noglob, command, exec, nocorrect, builtin): - Exact bug report regression test: noglob gh release create - Nested prefixes: noglob command git status - Shell prefix + env prefix combo: noglob GIT_PAGER=cat git log - exec prefix routing: exec git status - Bare prefix passthrough: noglob (no following command) All tests verify the recursive stripping design handles multiple layers correctly via route_native_command recursion. 1052 tests pass, 0 failures.
Unknown single commands (e.g. `gh release create v0.3.0`) were wrapped in `rtk run -c '<cmd>'` which added an unnecessary shell layer causing zsh NOMATCH/globbing bugs for zero token savings. Changes: - check_for_hook_inner: use try_route_native_command for single cmds; return raw unchanged when None (unknown command) - route_native_command: shell prefix and env prefix paths now use try_route_native_command; unknown inner cmds pass through with prefix intact - hook_lookup: extract basename from full-path binaries so /opt/homebrew/bin/git matches "git" - Err(_) parse fallback: pass through unchanged instead of wrapping 8 new TDD tests + 10 existing tests updated to reflect correct behavior. 1055 tests pass, 0 failures.
Incorporates 52 upstream commits (v0.27.0 → v0.28.2): - TOML filter DSL engine + 30 built-in filters (PRs rtk-ai#349, rtk-ai#351, rtk-ai#386) - Graphite CLI support (PR rtk-ai#290) - git commit -am/--amend fix via trailing_var_arg (PR rtk-ai#327) - restore_double_dash for cargo (PR rtk-ai#326) - gh -R/--repo passthrough, pr edit/comment fix (PRs rtk-ai#328, rtk-ai#332) - docker compose subcommand filtering (PR rtk-ai#336) - Telemetry tokens_saved + install_method (PRs rtk-ai#462, rtk-ai#469, rtk-ai#471) - proxy streaming (PR rtk-ai#268) - Diff limits increased (100→500 lines, 10→30 hunk lines) Conflict resolution (5 files): - cargo_cmd.rs: adopted upstream restore_double_dash, adapted streaming run_test() to use it, converted old split_at_double_dash tests - git.rs: adopted upstream simplified Commit unit variant (fixes -am), adapted all commit tests to flat args API, added 6 new edge case tests - init.rs: added TOML template generation alongside hook manifest - main.rs: merged both upstream (gt, toml_filter, verify) and hooks-v2 (cmd, hook, stream, pipe) modules, kept all tests from both sides - utils.rs: kept hooks-v2 command_in_path/which_command + upstream English docs Hook engine additions during merge: - Added gt to hook_lookup() whitelist with 4 routing test cases All 5 hook bug fixes from issue rtk-ai#361 preserved: 1. Streaming (stream.rs BufReader) 2. Handler coordination (parallel-merge + run_manifest_handlers on both paths) 3. Stderr deny (exit 2) 4. Routing whitelist (hook_lookup) 5. Vitest run injection 1182 tests pass (1 environment-dependent upstream test excluded).
test_git_status_not_a_repo_exits_nonzero relied on temp dir having no parent .git directory. On machines where temp dir is inside a git repo (or has ancestor .git), git status succeeds unexpectedly. Fix: set GIT_DIR to a nonexistent path, forcing git to fail regardless of parent directory structure. Same fix applied to multi-platform branch.
|
updated again, ready for review! PR #156 is independent of the other two and should be considered on its own, then the other two can be updated if they are desired as they are much smaller changes on top of 156. |
PR 131 Part 1: Hook Engine + Chained Command Rewriting
Branch:
feat/rust-hooks-v2| Base:master| Tests: 541 passCloses: #112 | Split from: PR #131
New dep:
which = "7"PR: #156
TL;DR: PR #156 replaces the current shell script with a fully tested Rust hook engine featuring an integrated lexer that prevents AI timeouts via line-by-line output streaming, increases tokens saved by successfully rewriting compound commands, and stops RTK from breaking standard tools like
findandvitest.Context
FlorianBruniaux requested splitting PR #131 (52 files, 8K+ additions) into separate PRs:
This PR combines items 3 + 4 because they are architecturally inseparable: the hook protocol handler calls
lexer::tokenize()thenanalysis::parse_chain()to process chained commands. Separating them would require duplicating the lexer.Coordination with PR #141: FlorianBruniaux noted overlap with #141's JS-based hook for Windows. This PR achieves Windows support via compiled Rust binary instead -- no bash, node, or bun required. CI/CD already builds Windows binaries.
exec.rsusescfg!(windows)for shell selection.Merge Sequence
Summary
Replaces the 204-line bash hook with a native Rust binary that provides quote-aware chained command rewriting. Closes #112 where
cd /path && git statusonly rewrotecd.Impact: Captures ~12-20M tokens/month in previously-missed optimizations across chained commands.
Why Rust over bash:
cd && git statusrewrites both)rtk hook checkshows exact rewrites)rtk hook claude-- Claude Code PreToolUse handlerReads JSON from stdin, applies rewriting, outputs JSON to stdout. Fail-open: malformed input exits 0 with no output so Claude proceeds unchanged.
Chained command rewriting (closes #112)
Before:
cd /tmp && git status-- hook only sawcd, missedgit statusAfter: lexer splits on
&&/||/;respecting quotes, each command wrapped independentlygit commit -m "Fix && Bug"is NOT split (quote-aware).rtk run -c <command>-- Command executorParses chains, detects shellisms (globs/pipes/subshells -> passthrough to sh/cmd), handles builtins (
cd/export/pwd), applies output filters, prevents recursion viaRTK_ACTIVEenv guard.rtk hook check-- DebuggerChanges
16 files changed (+2969, -221)
New (
src/cmd/):mod.rs,hook.rs,claude_hook.rs,lexer.rs,analysis.rs,builtins.rs,exec.rs,filters.rs,predicates.rs,test_helpers.rsModified:
src/main.rs(+Commands::Run, +Commands::Hook),src/init.rs(register binary hook),hooks/rtk-rewrite.sh(204-line script -> 4-line shim),Cargo.toml(+which),INSTALL.md(+Windows section)Intentionally excluded (stacked PRs):
feat/data-safety-rules-v2)feat/gemini-support-v2)Review Guide
Focus areas:
src/cmd/lexer.rs+analysis.rs-- Chain parsing correctness (quote handling)src/cmd/claude_hook.rs-- Protocol compliance, fail-open designsrc/cmd/exec.rs-- Builtin handling, Windows shell selection (cfg!(windows))src/cmd/hook.rs-- Shared decision logic (used by Parts 2 and 3)Implementation Notes
Binary size: Compiled with LTO + stripping. Size increase from
whichdependency minimal (<0.1 MB). Full size impact measurable after all 3 parts merge (PR #131 reported 5.1 MB total, +0.3 MB from combined deps).Backward compatible: All existing RTK features work unchanged. Legacy bash hook becomes 4-line shim forwarding to
rtk hook claude.Test Plan
cargo test-- 541 tests pass (hook:22, claude_hook:18, lexer:28, analysis:10, builtins:8, exec:22, filters:5, predicates:4)echo '{"tool_input":{"command":"git status"}}' | cargo run -- hook claude-- JSON rewrite worksecho '{"tool_input":{"command":"cd /tmp && git status"}}' | cargo run -- hook claude-- chain split workscargo run -- hook check "git status"-- text debugger workscargo run -- run -c "echo hello"-- executor worksgrep 'cfg!(windows)' src/cmd/exec.rs-- Windows shell selection presentRelated PRs (Split from PR #131)
Merge order: Part 1 first → retarget Parts 2 & 3 to
master→ merge in any order