Skip to content

fix(hook): emit contextcrawler as rewrite prefix; finish rtk rebrand on UI strings - #1

Merged
thehoff merged 1 commit into
developfrom
fix/finish-contextcrawler-rebrand
May 14, 2026
Merged

thehoff merged 1 commit into
developfrom
fix/finish-contextcrawler-rebrand

Conversation

@thehoff

@thehoff thehoff commented May 14, 2026

Copy link
Copy Markdown
Owner

Summary

  • The v0.1.0–v0.1.2 rename to `contextcrawler` missed three load-bearing emit codepaths, so freshly-installed users still see `rtk` as the rewrite prefix in hook output. On machines that only have the new binary on PATH (the documented install path), this fails with `command not found: rtk` when Claude Code tries to execute the rewritten command.
  • Reproduction on both macOS and Ubuntu (fresh `cargo install` of v0.1.2): `echo '{"tool_name":"Bash","tool_input":{"command":"git status"}}' | contextcrawler hook claude` emits `"command":"rtk git status"`.
  • Also flips three lingering `RTK` UI strings (`gain` dashboard header, two empty-state hints).

What changed

Surface Before After
Hook rewrite output `rtk git status` `contextcrawler git status`
Compound rewrite `rtk git add . && rtk cargo test` `contextcrawler git add . && contextcrawler cargo test`
`contextcrawler gain` header `RTK Token Savings (Global Scope)` `ContextCrawler Token Savings (Global Scope)`
`gain` empty-state "Run some rtk commands…" "Run some contextcrawler commands…"
`cc_economics` empty-state same same

Design choice — minimal-diff post-process at emit boundary

I deliberately did not touch the ~40 `rtk_cmd: "rtk X"` entries in `src/discover/rules.rs` or other internal `rtk` identifiers (struct/field/module names, `rtk_equivalent` classification keys). Those are kept as-is so the upstream-rebase surface against `rtk-ai/rtk` stays minimal — per the existing downstream design notes about retaining source-level `rtk` identifiers for rebase friction.

Instead, a new `display_rtk()` helper is applied at the three emit sites that produce user-visible rewrite strings. `rule.rtk_cmd` stays `"rtk git"` internally; the helper swaps the prefix on the way out.

Backward compatibility

The "already rewritten" passthrough checks (both compound-level and segment-level) were extended to recognize both `rtk ` and `contextcrawler ` prefixes. Users who already have `Bash(rtk:*)` allowlist entries in their CC permissions will keep working; the legacy prefix is still a valid form of "already rewritten" and is returned unchanged.

`test_rewrite_already_rtk` and `test_rewrite_mixed_compound_partial` explicitly cover this.

Test plan

  • `cargo test` — 1784 passed / 0 failed / 6 ignored
  • Rebuilt release binary, exercised `contextcrawler hook claude` end-to-end with the same JSON payload Claude Code sends. Simple and compound cases both flip to the new prefix; `permissionDecision: "allow"` is emitted on the simple case as expected.
  • Devel re-test on Framework (Ubuntu) after `cargo install --path rtk-fork`. End-to-end verification through actual Claude Code session, plus `/hooks` showing the loaded entry and a real Bash tool call populating `contextcrawler gain`.

Out of scope for this PR (filed as follow-ups)

  • `contextcrawler init` should chain `tirith init --shell ` with fail-open on missing binary (separate concern; will land as a second PR).
  • `rtk_cmd: "rtk "` values in `src/discover/rules.rs` still affect `contextcrawler discover` output ("you could have used rtk git"). Will be addressed once we decide whether `discover` should emit `contextcrawler` recommendations or keep `rtk` for upstream alignment.

🤖 Generated with Claude Code

The v0.1.0–v0.1.2 rename missed three load-bearing emit codepaths, so
freshly-installed users still saw `rtk` as the rewrite prefix in hook
output (which then failed on machines with only the new binary on PATH:
"command not found: rtk").

Reproduction before, on both macOS and Ubuntu:

  $ echo '{"tool_name":"Bash","tool_input":{"command":"git status"}}' \
      | contextcrawler hook claude
  {"hookSpecificOutput":{"updatedInput":{"command":"rtk git status"},...}}

After:

  {"hookSpecificOutput":{"updatedInput":{"command":"contextcrawler git status"},
                         "permissionDecision":"allow",...}}

Source changes:

* src/discover/registry.rs
  - Add display_rtk() helper that swaps the internal `rtk ` prefix on
    a rule's rtk_cmd for the user-facing `contextcrawler ` form.
    Internal classification keeps using rtk_cmd literals to keep
    upstream rebase friction minimal.
  - Wrap the three emit sites (rule.rtk_cmd format!, golangci-lint
    hardcoded literal, head/tail rewrite_line_range) so all rewrite
    output flows through the helper.
  - Extend the "already rewritten" passthrough checks at both compound
    (rewrite_command) and segment (rewrite_segment_inner) levels to
    accept either `rtk ` or `contextcrawler ` prefixes — preserves
    backward compatibility for users who already have allowlisted
    `rtk *` patterns in their Claude Code permissions.

* src/analytics/gain.rs
  - Header `RTK Token Savings` -> `ContextCrawler Token Savings`
    (Project and Global scopes).
  - Empty-state hint: "Run some rtk commands..." -> "Run some
    contextcrawler commands...".

* src/analytics/cc_economics.rs
  - Same empty-state hint rewording.

Test changes:

* src/hooks/hook_cmd.rs
  - Update existing assertions for the new prefix.
  - Add explicit `contextcrawler git status` passthrough case alongside
    the legacy `rtk` one in test_gemini_hook_uses_rewrite_command.

* src/discover/registry.rs test module
  - Flip ~100 rewrite-output assertions from `rtk X` to `contextcrawler X`.
  - Already-rewritten passthrough tests (test_rewrite_already_rtk,
    test_rewrite_mixed_compound_partial) keep `rtk` on the input side
    and verify both legacy and current prefixes pass through unchanged.

Verified end-to-end: rebuilt the release binary and exercised the hook
with the same JSON payload Claude Code sends; rewrite output is now
`contextcrawler git status` (simple) and `contextcrawler git add . &&
contextcrawler cargo test` (compound). `cargo test` is green (1784
passed / 0 failed / 6 ignored).

Internal `rtk` identifiers (struct/field/module names, rtk_cmd: rule
values, rtk_equivalent classification keys) are intentionally untouched
to keep upstream rebases against rtk-ai/rtk small.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thehoff
thehoff merged commit 0314cfd into develop May 14, 2026
4 checks passed
@thehoff
thehoff deleted the fix/finish-contextcrawler-rebrand branch May 14, 2026 13:03
thehoff added a commit that referenced this pull request May 25, 2026
First runner job revealed two unrelated issues:

1. `dtolnay/rust-toolchain@stable` fetched Rust 1.95.0, way ahead
   of the declared `rust-version = "1.80"` MSRV. Rust 1.95's
   clippy added new lints (doc_lazy_continuation, type_complexity
   tightening) plus an `incompatible_msrv` error for the existing
   `std::iter::repeat_n` usage (stable since 1.82). The lints
   firing on a clean codebase are toolchain drift, not bugs.

2. The clippy job ran with `-- -D warnings`, escalating every new
   advisory to a build failure. Combined with #1 above, the
   workflow was effectively unbuildable.

Fix: pin the toolchain to `1.82` (newest version still aligned
with the actual MSRV the code uses — `repeat_n` works) and drop
`-D warnings` from clippy so warnings are visible but non-fatal.
Re-tighten after a dedicated lint-cleanup pass lands.

Also collapses the duplicate `with:` block in the clippy job that
slipped in during the previous edit.

The `cargo test` job exited 143 (SIGTERM) on the previous run —
that was collateral from the workflow's job-failure cascade, not
a real test failure. Re-run with the fixed clippy gate will tell
us if the test job lands clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…hehoff#53)

Three-part attack on the codex 84.3%→95%+ compliance gap measured
post-v0.1.8.

## What

1. **`contextcrawler discover --codex`** — new subcommand scanning
   `~/.claude/plugins/data/codex-openai-codex/state/*/jobs/*.log`
   for `Running command:` lines. Reports `wrapped / total` %,
   canonicalised gap patterns, and top raw examples. Reproduces the
   84.3% hand-count exactly.

   `is_wrapped()` accepts: direct `contextcrawler ` prefix,
   `cd <dir> && contextcrawler ...` compositions, and standalone
   nav builtins. Documented limitation: a wrapped command piped
   into a raw filter is counted as wrapped (avoids mishandling
   quoted `|` inside `contextcrawler awk '$1|$2'`).

2. **AGENTS.md WRONG/RIGHT additions** in
   `hooks/codex/rtk-awareness.md` — explicit pairs for the 3
   gap patterns: `git -C <dir>`, `rg -n`, `nl -ba … | sed -n`.
   Adds a follow-up sentence calling out that these look
   "different enough" that codex might not register them as
   wrappable.

3. **Proxy bypass nudge** — when `contextcrawler proxy <tool>`
   is invoked for a tool that HAS a wrapped equivalent
   (sed/nl/grep/awk/git/rg/etc.), emit a one-line stderr note
   pointing at the wrapped form. Suppressible via
   `CONTEXTCRAWLER_NO_PROXY_NUDGE=1`. Don't auto-rewrite yet —
   measure adoption first, upgrade in a follow-up if needed.

## Why now

Post-v0.1.8 measurement: global savings jumped 24.8% → 70.6%
in 24h, but `proxy` became the new thehoff#1 leak — 307K input @ 0%
on 394 calls. Codex driving most of that volume with
`proxy sed`/`proxy nl` for read-with-line-numbers. The nudge
+ template patterns hit the codex side of the same problem
from two angles.

## Pinning tests
- `test_codex_template_pins_gap_patterns` — fails if `git -C`,
  `rg -n`, or `nl -ba` disappears from the template
- `test_proxy_wrapped_equivalent_known_tools` — sanity check
  per-tool coverage
- `test_proxy_wrapped_equivalent_message_does_not_recurse` —
  catches the "contextcrawler contextcrawler read" bug from
  the first implementation draft
- 15 new unit tests in `discover::codex`

## Verified
- `cargo build` clean
- `cargo test --bin contextcrawler` — 2134 pass, 0 fail
- All 7 integration suites green
- Empirical: `discover --codex` matches hand-count to 1 command
- Empirical: nudge fires on `proxy sed`, suppressed via env,
  silent on `proxy whoami`

## Success criteria (post-merge)
After deploy + 24h codex use, re-run `contextcrawler discover --codex`
and confirm ≥95% compliance (lift target +10.7pp from 84.3%).
Re-check `proxy` row in dashboard for absolute token-leak shrink.

Closes thehoff#53 (partial — auto-rewrite + read --range deferred to follow-ups).
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
`check_forbidden_pytest_args` only caught `-p VALUE` (space form) and
only when `looks_like_path(value)` returned true. Two bypass classes:

1. **Bare-relative path** -- the shared looks_like_path requires `./`,
   `/`, `~/`, `.\`, or a drive letter. `subdir/plugin.py` had no
   leading marker and fell through. Pytest loads it as a file regardless.
2. **Glued -p forms** -- argparse accepts `-pVALUE` and `-p=VALUE` in
   addition to `-p VALUE`. The old `a == "-p"` exact match missed both.

## Fix

New `pytest_p_value_is_filesystem_path` (pytest-specific, stricter than
shared `looks_like_path`):
- Anything `looks_like_path` catches
- Any value containing `/` or `\` (closes bypass thehoff#1)
- Any value ending in `.py` (a dotted module name never ends in `.py`)

New `pytest_p_value(a, next)` helper recognises:
- `-p VALUE` -- consumes next
- `-pVALUE` (glued)
- `-p=VALUE` (equals)

## Deny message now cites thehoff#49 (review P3)

Pre-PR review caught that `pyrbjvm_deny_message` hardcodes `(issue thehoff#36)`
(the umbrella). New `pyrbjvm_deny_message_with_issue(...)` variant lets
the `-p` deny cite thehoff#49 directly so operators land on the right tracker
entry. Other pyrbjvmdotnet denies continue to cite thehoff#36.

## Accepted residual risk (documented)

`pytest -p evil.payload` resolves through Python's import system. When
pytest is invoked as `python -m pytest`, `.` is on sys.path and the
attacker can plant `evil/payload.py` in cwd. We can't reject dotted
names categorically (`pytest_django.plugin` etc. are legitimate).
Documented in `pytest_p_value_is_filesystem_path`'s doc-comment as
accepted residual risk; remaining defense is the agent threat model.

## Pinning tests (4 new unit + 4 new integration)

Unit (src/core/utils.rs):
- pytest_rejects_p_bare_relative_path (the main thehoff#49 bypass)
- pytest_rejects_p_glued_and_equals_forms (the -pVALUE / -p=VALUE pair)
- pytest_p_deny_cites_issue_49 (P3 pin: -p cites thehoff#49, --rootdir thehoff#36)
- pytest_allows_p_with_module_name (extended negative coverage incl.
  dotted module names + glued legitimate forms)

Integration (tests/runtime_hardening.rs):
- pytest_p_bare_relative_path_is_rejected (asserts thehoff#49 in stderr)
- pytest_p_glued_form_is_rejected
- pytest_p_bare_py_extension_is_rejected
- pytest_p_legitimate_module_names_still_allowed

Updated existing `pytest_minus_p_with_path_is_rejected` to assert thehoff#49.

## Verified

- cargo build clean
- cargo test --bin contextcrawler: 2156 pass, 0 fail
- All 8 integration suites green (runtime_hardening 16 pass, was 12)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant