Skip to content

feat: add AWS CLI and psql modules#216

Merged
pszymkowiak merged 1 commit intortk-ai:masterfrom
itai-sagi:feat/aws-psql-support
Mar 4, 2026
Merged

feat: add AWS CLI and psql modules#216
pszymkowiak merged 1 commit intortk-ai:masterfrom
itai-sagi:feat/aws-psql-support

Conversation

@itai-sagi
Copy link
Contributor

Summary

  • rtk aws: Specialized filters for 8 high-frequency AWS CLI commands (STS identity, S3 ls, EC2 describe-instances, ECS list/describe-services, RDS describe-db-instances, CloudFormation list/describe-stacks). Generic fallback forces --output json and compresses via json_cmd::filter_json_string() at depth 4. Achieves 60%+ token savings on verbose AWS table/text output.
  • rtk psql: Detects psql table format (strips ----+---- borders, (N rows) footer, column padding → tab-separated) and expanded format (-[ RECORD N ]- blocks → [N] key=val one-liners). Passthrough for COPY/SET/notices. Overflow limits: 30 table rows, 20 expanded records.
  • Hook rewrite patterns added for both aws and psql in distributable and dev hooks.

Test plan

  • 32 new inline tests (16 aws_cmd + 16 psql_cmd) all passing
  • Full test suite: 428 tests pass, 0 failures
  • cargo fmt --all clean
  • cargo clippy --all-targets — no new warnings
  • Manual test with real AWS CLI (if configured): rtk aws sts get-caller-identity
  • Manual test with real psql (if available): rtk psql -c "SELECT 1" mydb

🤖 Generated with Claude Code

@pszymkowiak
Copy link
Collaborator

Thanks for adding AWS CLI and psql support — great additions to RTK's ecosystem! Tests pass (428/428), build is clean, and existing commands are unaffected. Here are some items to address
before merge:

Critical

  1. Revert commit polluting the PR: There's a revert commit that adds noise to the diff. Please clean up the git history (interactive rebase) so only AWS/psql changes are included.
  2. run_generic --output injection: The --output json flag is injected into all AWS commands via run_generic, but not all AWS subcommands support --output. This could break commands like
    aws s3 cp or aws s3 sync. Consider only injecting --output json for commands that support structured output (describe, list, get, etc.), or catch the error and fallback to raw execution.
  3. psql tracking on failed commands: Token savings are recorded even when the psql command fails (non-zero exit code). Tracking should only happen on successful executions to avoid
    skewing metrics.

Improvements

  1. Duplicate lazy_static! blocks: Several identical regex patterns are defined in multiple lazy_static! blocks across both modules. Factor them into shared constants or a single block.
  2. Test fixtures should use real command output: Per project guidelines (see CLAUDE.md > Testing Strategy), tests should use include_str!("../tests/fixtures/...") with real command
    output, not synthetic/hardcoded strings. This ensures filters work against actual CLI output formats.
  3. Token savings threshold too low: Tests assert ≥30% savings, but RTK's project standard is ≥60%. Please raise the threshold or justify why these specific filters have lower savings
    expectations.

Minor

  • Consider adding snapshot tests (assert_snapshot!) for output format validation
  • AWS module is +845 lines — could some shared patterns (JSON flattening, table formatting) be extracted to utils.rs?

Overall solid work, looking forward to the next iteration! 🔧

@itai-sagi itai-sagi force-pushed the feat/aws-psql-support branch 3 times, most recently from 9ec3583 to dc72be6 Compare February 22, 2026 10:42
@itai-sagi
Copy link
Contributor Author

@pszymkowiak lmk how this look, love the RTK. 🎊

AWS CLI (rtk aws):
- Specialized filters for sts, s3 ls, ec2, ecs, rds, cloudformation
- Generic fallback injects --output json for structured ops only
  (describe-*, list-*, get-*); mutating ops (s3 cp, deploy) pass through
- join_with_overflow() and truncate_iso_date() helpers in utils.rs

psql (rtk psql):
- Table format: strips separators/padding, outputs tab-separated
- Expanded format: compresses -[ RECORD N ]- blocks to key=val one-liners
- Passthrough for COPY results, notices, and non-table output
- 40%+ savings (table), 60%+ savings (expanded display)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@itai-sagi itai-sagi force-pushed the feat/aws-psql-support branch from dc72be6 to 6a80e32 Compare February 23, 2026 11:51
@pszymkowiak
Copy link
Collaborator

Re-reviewed after your updates — all 3 critical items fixed.

Fixes verified:

  • ✅ Clean single commit (no more revert noise)
  • --output json only injected for structured ops (is_structured_operation guard) — s3 cp/sync/mb safe
  • ✅ psql tracking only on success (exits before tracking on failure)

Tested locally:

  • 451/451 tests pass (37 new: 19 AWS + 18 psql)
  • rtk aws sts get-caller-identity → exit code 253 preserved on credential error
  • rtk psql -c "SELECT 1" → exit code 2 preserved on connection error
  • Existing commands unaffected (git status, ls, --version all work)
  • clippy clean (no new warnings)
  • Hook patterns correct: aws won't match awslocal, psql won't match psql2
  • Both hook files (.claude/hooks/ and hooks/) in sync
  • json_cmd integration verified (filter_json_string signature matches)

Non-blocking notes:

  • psql table savings ~40% (below 60% standard, but justified — stripping borders only, data preserved)
  • AWS module missing tee support (psql has it, minor inconsistency)

LGTM, ready to merge.

@pszymkowiak pszymkowiak merged commit b934466 into rtk-ai:master Mar 4, 2026
FlorianBruniaux added a commit that referenced this pull request Mar 5, 2026
rtk aws and rtk psql modules exist since PR #216 but were missing
from the registry rules — rewrite was silently skipping them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
FlorianBruniaux added a commit that referenced this pull request Mar 5, 2026
Cover all command categories added in PR #241 and missing from
the previous test suite:
- aws / psql (PR #216 modules, gap detected during non-reg run)
- Python: ruff, pytest, python -m pytest, pip, uv pip
- Go: go test/build/vet, golangci-lint
- JS/TS: vitest, pnpm vitest, prisma, prettier, pnpm list
- Compound operators: || and ; rewrites, 4-segment chains,
  mixed supported/unsupported segments, all-unsupported → None
- sudo prefix rewrite, env var prefix rewrite
- find with native flags
- Registry invariants: PATTERNS/RULES aligned, all rules valid,
  all patterns are valid regex

Before: 559 tests. After: 607 tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
FlorianBruniaux added a commit that referenced this pull request Mar 5, 2026
Adds aws_cmd, psql_cmd (PR #216) and rewrite_cmd (this PR) to the
Complete Module Map table. Updates total from 52 to 55 to match main.rs.

Fixes CI validate-docs.sh module count mismatch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
pszymkowiak pushed a commit that referenced this pull request Mar 5, 2026
)

* feat: add `rtk rewrite` command — single source of truth for hook rewrites

Implements `rtk rewrite <cmd>` as the canonical rewrite engine for all
LLM hook integrations (Claude Code, Gemini CLI, future tools).

- Add `rewrite_prefixes` field to `RtkRule` in discover/registry.rs
- Add public `rewrite_command()` with compound command support (&&, ||, ;, |)
- Add `rewrite_segment()`, `rewrite_compound()`, `strip_word_prefix()` helpers
- Handle already-rtk commands (exit 0, identical output)
- Handle unsupported/ignored commands (exit 1, no output)
- Add 20 unit tests covering all branches and edge cases
- Create `src/rewrite_cmd.rs` thin CLI wrapper
- Register `Commands::Rewrite` in main.rs
- Simplify `.claude/hooks/rtk-rewrite.sh` from 357 → 60 lines

Hooks no longer need duplicate mapping logic — a single
`REWRITTEN=$(rtk rewrite "$CMD") || exit 0` handles everything.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: fix version references and module count for 0.22.2

- README.md, CLAUDE.md, ARCHITECTURE.md: 0.20.1 → 0.22.2
- ARCHITECTURE.md: module count 48 → 51 (added rewrite_cmd + 2 from master)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* ci: update hook coverage check to verify registry (not hook script)

Since PR #241, the hook delegates to `rtk rewrite` — command mappings
live in src/discover/registry.rs, not the bash hook script.

Update the "Verify hook coverage" CI step to:
- Check that the hook calls `rtk rewrite` (new architecture)
- Check that registry.rs has rewrite_prefixes for all Python/Go commands

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: support single `&` background operator in compound rewrites

Per feedback from @xDelph: AI agents increasingly use `cmd1 & cmd2`
for parallel execution. This commit adds support alongside existing
`&&`, `||`, `;`, and `|` operators.

Changes:
- rewrite_compound: add match arm for single `&` (after `&&` check)
- rewrite_command: add `" & "` to has_compound detection
- init: show "installed/updated" vs "already up to date" so users
  know whether rtk init changed the hook on re-run

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: add Python/Go commands to rewrite registry

Add `ruff`, `pytest`, `pip`, `go`, and `golangci-lint` to both
PATTERNS and RULES in registry.rs so the CI coverage check passes
and `rtk rewrite` correctly identifies these commands.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update version reference to 0.23.0 in README

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update version reference to 0.23.0 in CLAUDE.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update version and module count to 0.23.0/52 in ARCHITECTURE.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: address P0 review feedback on rtk-rewrite PR

P0.1 - Replace 218-line bash hook with 58-line thin delegating hook.
  All rewrite logic now lives exclusively in `rtk rewrite` (Rust registry).
  `rtk init` installs the thin hook via include_str!.

P0.2 - Fix `head -20 file` crash at runtime.
  Generic prefix replacement produced `rtk read -20 file` (invalid clap args).
  Now translates `head -N file` → `rtk read file --max-lines N` and skips
  unsupported head flags (e.g. -c) by returning exit 1.

P0.3 - Add version guard in hook for rtk < 0.23.0.
  Prints a warning to stderr instead of silently doing nothing.

Also adds missing registry entries vs old hook:
  - gh release, cargo install
  - docker run/exec/build, kubectl describe/apply
  - tree, diff

474 tests pass, 0 clippy warnings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* style: cargo fmt

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(discover): extract rules/patterns into rules.rs

Per aeppling review: registry.rs was too large (~1500 lines).
Extract all static data into src/discover/rules.rs:
- RtkRule struct (with pub fields)
- PATTERNS const array
- RULES const array
- IGNORED_PREFIXES and IGNORED_EXACT const arrays

registry.rs now contains only logic + tests.
rules.rs is the single place to add a new command mapping.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update version reference to 0.24.0 in README

* docs: update version references to 0.24.0 in CLAUDE.md and ARCHITECTURE.md

* fix(discover): add aws and psql to rewrite registry

rtk aws and rtk psql modules exist since PR #216 but were missing
from the registry rules — rewrite was silently skipping them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test(discover): exhaustive regression tests for rewrite registry

Cover all command categories added in PR #241 and missing from
the previous test suite:
- aws / psql (PR #216 modules, gap detected during non-reg run)
- Python: ruff, pytest, python -m pytest, pip, uv pip
- Go: go test/build/vet, golangci-lint
- JS/TS: vitest, pnpm vitest, prisma, prettier, pnpm list
- Compound operators: || and ; rewrites, 4-segment chains,
  mixed supported/unsupported segments, all-unsupported → None
- sudo prefix rewrite, env var prefix rewrite
- find with native flags
- Registry invariants: PATTERNS/RULES aligned, all rules valid,
  all patterns are valid regex

Before: 559 tests. After: 607 tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(migration): document breaking change + detect outdated hook in rtk config

CHANGELOG [Unreleased]:
- Migration required: rtk init --global needed after upgrade
- Documents upgrade path and explains no immediate breakage

rtk init --show / rtk config now detects old hook (inline if-else)
vs new thin delegator (rtk rewrite) and prints actionable warning:

  ⚠️  Hook: ~/.claude/hooks/rtk-rewrite.sh (outdated — inline logic, not thin delegator)
     → Run `rtk init --global` to upgrade to the single source of truth hook

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update hook references for thin delegator (0.24.0)

- ARCHITECTURE.md: fix hook description (shell script → thin delegator)
- INSTALL.md: add upgrade path for old hook users (pre-0.24 breaking change)
- SECURITY.md: add registry.rs + hook files to Tier 1 critical files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(arch): fix module count 52→55, add aws/psql/rewrite to module map

Adds aws_cmd, psql_cmd (PR #216) and rewrite_cmd (this PR) to the
Complete Module Map table. Updates total from 52 to 55 to match main.rs.

Fixes CI validate-docs.sh module count mismatch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* ci: fix hook coverage check to search rules.rs instead of registry.rs

After the PR #241 refactor, rewrite_prefixes constants moved from
registry.rs to rules.rs. Update grep to search the whole discover/
directory so it finds them in the right file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants