| name | knowdb-local-search |
|---|---|
| description | Step-by-step guide for querying a KnowDB knowledge base using bash and grep. Use when answering questions from documents ingested into db/. |
For use by local coding agents with filesystem access.
The db/ directory is the knowledge base. All commands below assume the repo root as working directory.
At session start — before your first search — unconditionally generate a
stable id and write it to .session_id at the repo root (overwrite; a
new session gets a new id):
date -u +%Y%m%dT%H%M%SZ-$RANDOM > .session_idcat db/_manifest.jsonReturns { "<doc_id>": { "originalFilename": "...", "title": "..." }, ... }.
Each doc_id is an 8-hex string that maps to a subdirectory under db/.
title is the human-readable document name — cite it (not the filename or
doc_id) when referring to a document.
cat db/<doc_id>/_index.mdShows the full heading tree: section titles and their chunk IDs. Read this before searching to identify which chunks are relevant.
_index.md is also your breadcrumb: it maps every chunk ID to its
heading title and shows the full root→chunk path. After a grep hit, read
_index.md to recover where the chunk sits — its title, parent heading,
and siblings — instead of cat-ing parent chunks just to orient.
If unsure which document to look in, scan all heading trees at once:
grep -ril "<keyword>" db/*/_index.mdThis is fast and low-noise — headings only, no body content.
Search within one document (recommended):
grep -rinE "<keyword>" db/<doc_id>/Search across all documents:
grep -rinE "<keyword>" db/ --include="*.md" --exclude="_index.md"Keyword contract: the keyword is one or more literal terms joined by
| (simple OR) — a|b matches a OR b. Whitespace is literal (a
space matches a space, not multiple keywords). Matching is case-
insensitive by default. Other regex metacharacters are not supported;
behavior is undefined and gap recording falls back to a single raw-
keyword topic.
Useful flags:
-rrecursive,-icase-insensitive,-nshow line numbers-llist matching files only (for a quick overview)-Penable Perl-compatible regex:grep -rinP "a|b" db/<doc_id>/
Each result shows the file path (<doc_id>/<chunk_id>.md) and the matching line.
Read the surrounding lines before fetching the full chunk.
| Need | Command |
|---|---|
| Browse chunk IDs in a document | ls db/<doc_id>/ |
| Preview first line of each chunk | for f in db/<doc_id>/*.md; do echo "$f:"; head -1 "$f"; done |
| Read one chunk in full | cat db/<doc_id>/<chunk_id>.md |
| Read only matching lines with context | grep -in -C 3 "<pattern>" db/<doc_id>/<chunk_id>.md |
| Find parent chunk | strip the last -XX segment: 01-02-03 → parent is 01-02 |
| Read parent chunk | cat db/<doc_id>/<parent_id>.md |
| Read siblings | ls db/<doc_id>/ | grep "^<parent_id>-" |
| Read the whole document (chunked nav not enough) | cat $(ls db/<doc_id>/*.md | grep -v _index.md | sort) — body-only concat in chunk-ID order; no headings (unlike browser reconstruct_document) — read _index.md alongside for the heading tree |
KnowDB has no explicit [[ref]] registry — related material is found by
shared terms. After reading a chunk that matters, take its salient terms
and grep them across the other documents:
grep -rinlP "<term1>|<term2>" db/ --include="*.md" --exclude="_index.md" \
| grep -v "db/<current_doc_id>/"This surfaces connected material elsewhere in the knowledge base that a single-document (scoped) search would miss.
- manifest →
_index.md→ scoped grep →cat— always in this order. - Read grep output before fetching full chunks — the matching line is often enough.
- Use
-Cfor context when a chunk is long:grep -in -C 3 "keyword" <file>returns matching lines with 3 lines of context each side. - Scope every search to a
doc_idsubdirectory once you know the target document. - Never
cata full document just to scan it — grep +_index.mdfirst. Read the whole document only when you genuinely need it as continuous text. - Chunk ID encodes position in the heading tree:
01= first top-level section,01-02= its second subsection,01-02-03= one level deeper. Use this to navigate without reading files. _index.mdis the breadcrumb — read it to learn a chunk's title, parent and siblings instead ofcat-ing parent chunks to orient.- Follow lateral links: after reading a strong chunk, grep its key terms across the other documents (Step 5) — it surfaces implicit cross-document connections a scoped search misses.