-
Notifications
You must be signed in to change notification settings - Fork 50
feat(init/docs): enforce rgai-first search policy and reduce token cost #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| # RTK auto-rewrite hook for Claude Code PreToolUse:Bash | ||
| # Transparently rewrites raw commands to their rtk equivalents. | ||
| # Outputs JSON with updatedInput to modify the command before execution. | ||
| # Source of truth: hooks/rtk-rewrite.sh (keep .claude/hooks copy in sync) | ||
|
|
||
| # Guards: skip silently if dependencies missing | ||
| if ! command -v rtk &>/dev/null || ! command -v jq &>/dev/null; then | ||
|
|
@@ -88,10 +89,20 @@ elif echo "$MATCH_CMD" | grep -qE '^cargo[[:space:]]+fmt([[:space:]]|$)'; then | |
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed 's/^cargo fmt/rtk cargo fmt/')" | ||
|
|
||
| # --- File operations --- | ||
| # Search priority (mandatory): rgai > rg > grep | ||
| # Tier 1: semantic intent search (grepai/rgai) -> rtk rgai | ||
| # Tier 2: exact search via ripgrep -> rtk grep (rtk grep runs rg -> grep fallback internally) | ||
| # Tier 3: exact search via grep -> rtk grep | ||
| elif echo "$MATCH_CMD" | grep -qE '^cat[[:space:]]+'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed 's/^cat /rtk read /')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^(rg|grep)[[:space:]]+'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed -E 's/^(rg|grep) /rtk grep /')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^(grepai|rgai)[[:space:]]+search([[:space:]]|$)'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed -E 's/^(grepai|rgai)[[:space:]]+search[[:space:]]+/rtk rgai /')" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rewrite rule sends This rule should land together with the |
||
| elif echo "$MATCH_CMD" | grep -qE '^rgai[[:space:]]+'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed -E 's/^rgai[[:space:]]+/rtk rgai /')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^rg[[:space:]]+'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed -E 's/^rg /rtk grep /')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^grep[[:space:]]+'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed -E 's/^grep /rtk grep /')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^ls([[:space:]]|$)'; then | ||
| REWRITTEN="${ENV_PREFIX}$(echo "$CMD_BODY" | sed 's/^ls/rtk ls/')" | ||
| elif echo "$MATCH_CMD" | grep -qE '^tree([[:space:]]|$)'; then | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
| # Test suite for rtk-rewrite.sh | ||
| # Feeds mock JSON through the hook and verifies the rewritten commands. | ||
| # | ||
| # Usage: bash ~/.claude/hooks/test-rtk-rewrite.sh | ||
| # Usage: bash hooks/test-rtk-rewrite.sh | ||
| # Override hook path: HOOK=/path/to/rtk-rewrite.sh bash hooks/test-rtk-rewrite.sh | ||
|
|
||
| HOOK="$HOME/.claude/hooks/rtk-rewrite.sh" | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| HOOK="${HOOK:-$SCRIPT_DIR/rtk-rewrite.sh}" | ||
| PASS=0 | ||
| FAIL=0 | ||
| TOTAL=0 | ||
|
|
@@ -109,6 +111,22 @@ test_rewrite "rg pattern src/" \ | |
| "rg pattern src/" \ | ||
| "rtk grep pattern src/" | ||
|
|
||
| test_rewrite "grepai search query" \ | ||
| "grepai search auth middleware" \ | ||
| "rtk rgai auth middleware" | ||
|
|
||
| test_rewrite "grepai search with flags" \ | ||
| "grepai search \"error handler\" --json --compact" \ | ||
| "rtk rgai \"error handler\" --json --compact" | ||
|
|
||
| test_rewrite "rgai search query (priority over rg/grep)" \ | ||
| "rgai search auth middleware --compact" \ | ||
| "rtk rgai auth middleware --compact" | ||
|
|
||
| test_rewrite "plain rgai" \ | ||
| "rgai auth middleware --json" \ | ||
| "rtk rgai auth middleware --json" | ||
|
|
||
| test_rewrite "cargo test" \ | ||
| "cargo test" \ | ||
| "rtk cargo test" | ||
|
|
@@ -149,6 +167,18 @@ test_rewrite "env + docker compose" \ | |
| "COMPOSE_PROJECT_NAME=test docker compose up -d" \ | ||
| "COMPOSE_PROJECT_NAME=test rtk docker compose up -d" | ||
|
|
||
| test_rewrite "env + grepai search" \ | ||
| "NODE_ENV=test grepai search token refresh --json" \ | ||
| "NODE_ENV=test rtk rgai token refresh --json" | ||
|
|
||
| test_rewrite "env + rg exact search" \ | ||
| "RG_IGNORE_DOT=1 rg token src/" \ | ||
| "RG_IGNORE_DOT=1 rtk grep token src/" | ||
|
|
||
| test_rewrite "env + grep exact search" \ | ||
| "LC_ALL=C grep -rn token src/" \ | ||
| "LC_ALL=C rtk grep -rn token src/" | ||
|
|
||
| echo "" | ||
|
|
||
| # ---- SECTION 3: New patterns ---- | ||
|
|
@@ -193,17 +223,17 @@ test_rewrite "docker exec -it db psql" \ | |
| "docker exec -it db psql" \ | ||
| "rtk docker exec -it db psql" | ||
|
|
||
| test_rewrite "find (NOT rewritten — different arg format)" \ | ||
| test_rewrite "find with native args" \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three tests changed from expecting no rewrite (empty string) to expecting rewrites ( |
||
| "find . -name '*.ts'" \ | ||
| "" | ||
| "rtk find . -name '*.ts'" | ||
|
|
||
| test_rewrite "tree (NOT rewritten — different arg format)" \ | ||
| test_rewrite "tree with path arg" \ | ||
| "tree src/" \ | ||
| "" | ||
| "rtk tree src/" | ||
|
|
||
| test_rewrite "wget (NOT rewritten — different arg format)" \ | ||
| test_rewrite "wget URL" \ | ||
| "wget https://example.com/file" \ | ||
| "" | ||
| "rtk wget https://example.com/file" | ||
|
|
||
| test_rewrite "gh api repos/owner/repo" \ | ||
| "gh api repos/owner/repo" \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Source: internal migration benchmark artifacts (private repository)" — this benchmark is not reproducible or verifiable by contributors. Either publish the benchmark script/dataset or move this table to internal docs.