grep -r across a ~900k-file home tree doesn't just run slow — it hangs the
session that launched it. But users (and automated agents) reach for grep by
reflex. The fix had to be both a faster tool and a mechanism that made the slow
path unreachable, because a tool nobody remembers to use isn't a fix.
knowledge — a CLI front-end (~390 lines bash) over Tome, a Rust/Tantivy
full-text daemon (single 87KB main.rs). One entrypoint, two modes:
knowledge <query>— indexed BM25 content search over the Tantivy indexknowledge grep <args>— live 1:1 scan, forwards verbatim torg
Plus -f/--exact filename lookup, -e extension filter, -r recent (fast field
sort), --breadcrumb (find + header preview), --warp (find + cd), --reindex
(rebuild + reload the daemon, then poll status --json until it answers again).
Index roots cover the home tree and project directories, with skip lists for
node_modules, .git, target, binaries, and >50MB files. Live: 912,520
documents, query latency 388ms — versus a full-tree grep that never returns.
The knowledge grep bridge is where the real work is. grep flags don't map cleanly
onto rg: -r means --replace to rg (silent misfire, not an error) and -E is
meaningless, so both are dropped; --include=/--exclude= are translated to
-g/-g '!'; --hidden --no-ignore are always forced, because a tool claiming
to be an honest grep replacement can't silently skip dotfiles and gitignored paths
— exactly what debugging hunts for.
Enforcement lives in a PreToolUse hook (grep_redirect_helper.sh): simple
grep "word" path spans are rewritten in place to knowledge "word" — the span
only, leaving pipes/redirects/trailing commands intact. Anything else is denied,
with an escalating message keyed to a per-session counter: attempt #3 stops
suggesting flags and challenges the approach ("you are reaching for
full-scan-shaped thinking — what's the anchor?").
- Silent query narrowing.
knowledge ayam ikan rotikept onlyroti— each bare positional clobbered the last. Now joined. Silent wrong answers beat loud failures never. - Bundled-flag unbundling. Stripping
rfrom-rlnalso corrupted-er(=-e r), turning it into bare-e, which then ate the next token as the pattern. Fix: stop unbundling at the first argument-taking letter and copy the token's tail through untouched. - Vestigial state. The script still set a
DB=pointing at a SQLite mirror that had since gone stale; nothing read it.-dwas rewritten to a live walk when that mirror died.
Search went from session-hanging to sub-second at ~900k documents, and the
enforcement layer means the fast path is the only path — no discipline required.
The escalating-block design treats repeated misuse as a signal about strategy, not
syntax. Honest tradeoff, documented in the source: the index is import-stale (last
import timestamp is surfaced by knowledge -s), so the live rg escape hatch is a
first-class mode, not a fallback.