qmd/test
James Risberg 244ddf5ecb feat: AST-aware chunking for code files via tree-sitter
Add opt-in AST-aware chunk boundary detection for code files using
web-tree-sitter. When enabled with `--chunk-strategy auto`, code files
(.ts, .tsx, .js, .jsx, .py, .go, .rs) are chunked at function, class,
and import boundaries instead of arbitrary text positions. Default
behavior (`regex`) is unchanged — no surprises on upgrade.

In testing on QMD's own codebase, AST mode split 42% fewer function
bodies across chunk boundaries compared to regex-only chunking.

Usage:
  qmd embed --chunk-strategy auto
  qmd query "search terms" --chunk-strategy auto

What's included:
- Language detection from file extension with support for TypeScript,
  JavaScript (including arrow functions and function expressions),
  Python, Go, and Rust
- Per-language tree-sitter queries with scored break points aligned to
  the existing markdown scale (class=100, function=90, type=80, import=60)
- AST break points merged with regex break points — highest score wins
  at each position, so embedded markdown (comments, docstrings) still
  benefits from regex patterns
- Refactored chunking core: chunkDocumentWithBreakPoints() extracted,
  mergeBreakPoints() added, async chunkDocumentAsync() wrapper for AST
- ChunkStrategy type ("auto" | "regex") threaded through
  generateEmbeddings(), hybridQuery(), structuredSearch(), CLI, and SDK
- getASTStatus() health check wired into `qmd status`
- Parse failures log a warning and fall back to regex — never crash

Hardening:
- Grammar packages are optionalDependencies with pinned versions to
  prevent ABI breaks from semver drift
- web-tree-sitter is a direct dependency (pinned)
- Errors are logged (not silently swallowed) for debuggability
- Tested on both Node.js and Bun (Bun is actually faster)

Testing:
- 26 unit tests (test/ast.test.ts) — all 4 languages, error handling
- 7 integration tests (test/store.test.ts) — merge, equivalence, bypass
- Standalone test-ast-chunking.mjs with 63 synthetic tests and a
  real-collection performance scanner (npx tsx test-ast-chunking.mjs ~/code)
- Validated end-to-end with qmd embed + qmd query on QMD's own codebase
- Zero markdown regressions across all test paths

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 01:22:39 -04:00
..
eval-docs Add 6 synthetic evaluation documents 2025-12-21 13:10:35 -04:00
ast.test.ts feat: AST-aware chunking for code files via tree-sitter 2026-03-22 01:22:39 -04:00
cli.test.ts fix: bound memory usage during embed 2026-03-13 17:39:17 +08:00
collections-config.test.ts fix(test): reset currentIndexName between test files 2026-02-18 15:53:58 -04:00
Containerfile test: restructure container smoke tests for interactive use 2026-02-22 11:09:36 -04:00
eval-bm25.test.ts test: move all tests to flat test/ directory 2026-02-15 21:37:47 -04:00
eval-harness.ts refactor: move CLI and MCP to subdirectories, MCP consumes SDK 2026-03-10 11:39:55 -04:00
eval.test.ts test: move all tests to flat test/ directory 2026-02-15 21:37:47 -04:00
formatter.test.ts refactor: move CLI and MCP to subdirectories, MCP consumes SDK 2026-03-10 11:39:55 -04:00
intent.test.ts refactor: move CLI and MCP to subdirectories, MCP consumes SDK 2026-03-10 11:39:55 -04:00
launcher-detection.test.sh fix: prioritize package-lock.json in launcher to prevent Bun false positive 2026-03-12 01:46:38 -07:00
llm.test.ts fix(ci): guard LLM calls in CI and increase test timeouts 2026-03-10 13:28:37 -04:00
mcp.test.ts fix(ci): guard LLM calls in CI and increase test timeouts 2026-03-10 13:28:37 -04:00
multi-collection-filter.test.ts refactor: replace bash wrapper with standard #!/usr/bin/env node shebang 2026-02-22 11:09:36 -04:00
rrf-trace.test.ts feat(query): add --explain score traces for hybrid search 2026-03-07 14:35:10 -04:00
sdk.test.ts fix: bound memory usage during embed 2026-03-13 17:39:17 +08:00
smoke-install.sh test: restructure container smoke tests for interactive use 2026-02-22 11:09:36 -04:00
store-paths.test.ts test: move all tests to flat test/ directory 2026-02-15 21:37:47 -04:00
store.helpers.unit.test.ts fix: skip vector cleanup when sqlite-vec is unavailable 2026-03-12 13:51:20 +00:00
store.test.ts feat: AST-aware chunking for code files via tree-sitter 2026-03-22 01:22:39 -04:00
structured-search.test.ts feat: redesign SDK search API with unified search() and ExpandedQuery type 2026-03-10 11:04:45 -04:00