# AGENTS.md This file provides guidance to the agent when working with code in this repository. ## Assistant-specific entry points `AGENTS.md` is the single source of agent guidance. Assistants that do not read it by default load it through thin pointer files: `CLAUDE.md` (Claude Code, via `@AGENTS.md`), `GEMINI.md` (Gemini CLI), `.github/copilot-instructions.md` (GitHub Copilot) and `.cursor/rules/agents.mdc` (Cursor). Keep those files as pointers only: never add rules to them, and put every change here. Keep the same pointer files on `master` and `beta`. ## Fork-specific branch workflow - Create new work branches in the user's fork rather than in the upstream repository. This rule applies to every fork. Use a work branch instead of working directly on `master`, unless the user explicitly requests a direct commit. - Name branches `/`, using the same Conventional Commits types as commit messages (for example `feat/dart-annotations`, `fix/utf16-sync`, `refactor/resolver-lookup`, `ci/release-workflow`). Keep the description descriptive, lowercase, and hyphenated, and do not include the number of any issue or pull request associated with the work. Refer to issues and pull requests in commit messages and PR descriptions instead. - Write commit messages and PR titles using [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `docs:`, `ci:`, `refactor:`, `test:`, `chore:`, with an optional scope such as `fix(db):`), as described in [CONTRIBUTING.md](CONTRIBUTING.md#commit-messages). ## Keeping agent guidance current Treat `AGENTS.md` on the original repository's `master` branch (`aovestdipaperino/tokensave`) as the baseline for shared repository guidance. Before changing this file or preparing a branch for merge, compare it with the latest upstream `master` version and incorporate missing shared instructions. Preserve branch-specific instructions and intentional differences; do not blindly replace the file. If upstream cannot be checked, disclose that limit. Update this file in the same change whenever build/test commands, architecture, tooling, contributor policies, or release workflows make its guidance outdated. Keep shared rules synchronized between `master` and `beta`, and verify the instructions against the affected branch's files before committing. Report any remaining differences and why they are needed in the handoff. ## Graphs and diagrams Every graph and diagram created or updated for repository documentation MUST use Mermaid. Embed Mermaid source in fenced `mermaid` code blocks in Markdown so GitHub can render it and contributors can edit it directly. ## Star history The README star-history chart must always represent `aovestdipaperino/tokensave`, including on branches and in forks (#1036). Keep its data source and any associated links tied to that repository; never replace its history with a fork's stars. Keep chart regeneration restricted to the upstream repository, and verify the configuration and generated assets when changing the chart or its workflow. Preserve the daily/manual schedule and avoid star-triggered runs unless a deliberate change is made (see #265). Keep this shared rule aligned between `master` and `beta`. ## README consistency and installation Keep `README.md` synchronized between `master` and `beta`. Installation instructions are the only permitted branch-specific difference in README content; keep all other content the same. Record this rule identically in both branches' `AGENTS.md` files. - On `master`, preserve the stable installation guide: Homebrew (`brew install aovestdipaperino/tap/tokensave`), crates.io (`cargo install tokensave`), `cargo binstall tokensave`, Windows Scoop (add `https://github.com/aovestdipaperino/scoop-bucket` as the `tokensave` bucket, then `scoop install tokensave`), and prebuilt binaries from the latest release added to `PATH`. Retain the Cargo medium/lite variants and the documented release archive table. - On `beta`, preserve installation directly from the beta branch: `cargo install --git https://github.com/aovestdipaperino/tokensave.git --branch beta --locked --force`. Retain the alternative of cloning `beta` and running `cargo build --release --locked` with the pinned toolchain, then adding the resulting binary to `PATH`, plus the source-based medium/lite variants. Keep the explanation that Homebrew, Scoop, crates.io, cargo-binstall, and published release binaries install stable releases; no prebuilt beta archives are published. Preserve the schema-migration/full-reindex notice, disposable-checkout advice, and link to the master guide for returning to stable. - Preserve these installation differences when merging or synchronizing the branches. ## Beta announcement `README.md` carries no beta announcement. The beta is described in the "Trying the beta" section of `docs/guide/INSTALLATION.md` on `master`, which also shows the explicit beta installation command. Keep that section current with the new release features available on `beta`: when adding or changing beta features, update its highlights and links, and describe only features already available on the beta branch. When those features reach a stable release, revise or remove the section so it continues to describe what is available in beta beyond stable. Keep the installation instructions accurate and link to the beta changelog for details. ## Documentation and GitHub Wiki User documentation is published both as Markdown in `docs/guide/` and as the [GitHub Wiki](https://github.com/aovestdipaperino/tokensave/wiki), and the two MUST stay aligned. `docs/guide/` is the single source: `.github/workflows/wiki.yml` regenerates the whole wiki from it with `scripts/wiki/sync_wiki.py` on every relevant push to `master`. Never edit the wiki directly, and never copy wiki content back by hand; direct wiki edits are overwritten. - Keep `README.md` a concise entry point: what tokensave is, the installation guide required above, the quick start, and the documentation table linking into `docs/guide/`. Put detailed explanations in the matching guide page. - When a change adds, removes, or changes user-visible behavior (a command, flag, MCP tool, configuration field, environment variable, supported agent, language, or default), update the affected `docs/guide/` pages, and the README if it mentions the behavior, in the same change. A pull request that changes such behavior without the matching documentation update is incomplete. - Describe each topic on one guide page and link to it elsewhere. The installation commands are the one intended duplicate: keep `docs/guide/INSTALLATION.md` consistent with the README installation guide and the branch-specific installation rules above. The README keeps only the common methods; less common ones, such as installing from upstream `master`, live only in `docs/guide/INSTALLATION.md` and are linked from the README. - New guide pages use uppercase, hyphenated file names, a single `# Title` (the wiki page is named after it), and an entry in both the `wiki-sidebar` list in `docs/guide/README.md` and the README documentation table. - Before finishing, run `python3 scripts/wiki/sync_wiki.py check` and `python3 -m unittest discover -s scripts/wiki`, and report the results. - The wiki describes `master` (stable) only. On `beta`, `docs/guide/` describes beta behavior and is not published; keep its page structure the same as on `master` so the branches can be synchronized like `README.md`. - Design notes, evaluations, and reports remain in `docs/` outside `docs/guide/` and are not published to the wiki. See [CONTRIBUTING.md](CONTRIBUTING.md#documentation) for the contributor-facing version of these rules. ## Contributing Follow [CONTRIBUTING.md](CONTRIBUTING.md) when changing this repository, including its mandatory [memory and resource management requirements](CONTRIBUTING.md#memory-and-resource-management). Use the resource review checklist for resource-sensitive changes and report the checks actually performed, their results, and any limitations in the pull request. Follow the [GitHub issue lifecycle rules](CONTRIBUTING.md#github-issue-lifecycle): create issues for identified bugs and enhancements, link PRs with "Refs #N" (never "Fixes #N" / "Closes #N"), and never close an issue (including via `gh issue close`) without explicit permission — issue closure is left to the repository maintainer. Follow the [issue author credit rules](CONTRIBUTING.md#crediting-issue-authors): when a PR implements an issue someone else proposed or substantially specified, add a `Co-authored-by:` trailer with their GitHub noreply address for each such person, and name them in the PR description. ## Rust memory safety and soundness review Apply these checks to changes that involve `unsafe` code, native dependencies (libSQL, tree-sitter grammars, `memmap2`), platform APIs, concurrency, filesystem state or callbacks. They add to the memory and resource rules in CONTRIBUTING.md and do not replace or relax any existing rule. [docs/MEMORY-SAFETY-AUDIT.md](docs/MEMORY-SAFETY-AUDIT.md) records the current inventory and findings (#947). Update it when a change adds, removes or re-justifies an `unsafe` site. - **Panic and unwind safety:** Do not assume a safe operation cannot panic. While an unsafe invariant holds or state is partially initialized, check every `Drop`, `Clone`, comparison, allocation, iterator and callback that runs. Unwinding must preserve memory safety and ownership, so use guards or rollback where needed. `catch_unwind` is not a substitute for soundness, and this rule does not relax any existing restriction on its use. - **Adversarial trait implementations:** Do not assume a safe implementation of `AsRef`, `Borrow`, `Deref`, `Clone`, `Ord` or an iterator trait returns stable, consistent or repeatable results, unless that guarantee is established. Validate or keep a value before using it as an unchecked index, length, pointer or allocation size. A safe API must stay sound when given an unusual but safe implementation. - **OS, process state and FFI:** Do not assume that environment variables, signal handlers, file descriptors and handles, paths, mapped memory or other process-wide OS state stay unchanged across concurrent activity or native calls. At FFI boundaries, check lifetime, ownership, synchronization and the platform contract. Library code must not call `std::env::set_var` or `remove_var`. Pass a child's environment with `Command::env`. A test that mutates the environment needs its own `[[test]]` target. If that target has more than one test, it must also hold a lock for the whole duration of the change. Call `process::exit` only from the main thread, and never from a `Drop` or a thread-local access. - **Verification and reporting:** For a relevant change, test error and panic paths and adversarial inputs. Use supported tools where they apply, such as Miri, sanitizers, Clippy and `cargo audit`, and report the actual results and limits in the PR. Separate confirmed, reachable defects from speculative or non-applicable upstream reports. Do not claim soundness or exploitability without evidence. Background: [Memory Safety Bugs in Rust Standard Library](https://forge-blog.github.io/posts/rust-std-memory-safety/) (Microsoft FORGE Lab). ## Issue closure verification and merged-result summaries Before closing an issue, and only with the explicit permission required above, the agent MUST: - Identify the linked pull requests and check their current state on GitHub. Distinguish open, closed without merging, and merged PRs; a closed PR is not evidence that its changes were merged. - For each merged PR, post a summary on the issue listing the new improvements. Link the PR and merge commit, identify the actual destination branch, and record validation results, limitations, and remaining work. Do not infer that a release was published merely because a PR merged. - For open or closed-but-unmerged PRs, state their actual status and do not describe their changes as merged or delivered to the destination branch. If no related PR exists, record that rather than inventing a merge status. Perform this verification and publish the applicable summary before closing the issue. For multiple related PRs, distinguish merged work from pending or unmerged work. If a PR merges after the issue was closed, update the issue with the merged improvements and actual destination branch. ## Summaries for related implementations When a topic spans multiple implementations or related pull requests, the agent MUST write and maintain a consolidated summary. Include each change's scope, linked issues/PRs, implementation and merge status, dependencies, validation results and limitations, and any remaining work. Update the summary as related changes are added or their status changes, and link it in the final handoff. Use an existing parent/tracking issue when available; a repository summary document may hold the detailed record. Do not describe published but unmerged work as merged, or benchmark candidates as completed implementations. ## SQL query inventory Keep [docs/QUERY-REPORT.md](docs/QUERY-REPORT.md) synchronized with every SQL query added, changed, moved, or removed in the report's documented scope, in the same change as the source modification. This includes SQL templates, shared fragments, dynamic builders, parameter bindings, migrations, triggers, and database maintenance statements. Update the affected SQL entries and construction/binding context, source links, and inventory counts. Add entries for new queries and remove obsolete entries; keep existing query IDs stable and assign unused IDs to new entries. If query coverage changes, update the report's scope and limitations. Before finishing, review the source diff for SQL changes and verify that each is reflected in the report. Git history records the changes; the report describes the current source. ## Named constants and single sources of truth These rules apply to new and modified code on every branch (#1069). The contributor-facing version is in [CONTRIBUTING.md](CONTRIBUTING.md#named-constants). - Do not scatter magic numbers or meaningful hard-coded strings through new or modified code. Give domain-specific values (limits, widths, timeouts, sizes, file and directory names, environment-variable keys including Cargo keys such as `OUT_DIR`, protocol identifiers, default settings and build artifacts) descriptive, appropriately scoped names. - Declare compile-time values with `const` where practical. Use `static`, lazy initialization or runtime configuration only when their semantics require it. Reuse one definition, or derive related values from it, instead of duplicating literals that must stay synchronized. - Pass named constants to runtime APIs, for example `std::env::var_os(cargo_env::OUT_DIR)`. Compile-time macros such as `env!`, `include_str!` and `concat!` require string literals: keep those literals, say where the authoritative definition lives in a nearby comment, and do not break compilation to remove them. Where practical, pass a full value through one key instead (for example `include_str!(env!("TOKENSAVE_BUILD_LOGO_ANSI_PATH"))`), and add a test that checks the literal against its definition. - Put named constants in dedicated `constants.rs` files or modules instead of declaring them throughout implementation files. Keep each module compact and grouped by feature or domain, place it close to its owner, reuse an existing module when one fits, and avoid a catch-all constants file. Expose constants only as widely as needed, and do not merge unrelated concepts because their values happen to match. - `build.rs` cannot import crate modules. Its constants live in `build/constants.rs`, which `build.rs` includes with `#[path]` and `tests/build_constants_test.rs` checks. - Conventional, self-explanatory literals such as `0` and `1`, and one-off values without domain meaning, may stay inline. Avoid needless abstraction. - **Review checklist:** during each implementation and PR review, search the changed code (including build scripts, tests and generated-asset paths) for repeated or meaningful literals. Replace them with named constants in the appropriate constants module, or record why an inline value is preferable. Check that constants modules stay compact and logically grouped. ## Rust style guide (master only) TokenSave has adopted [Google's Rust Style Guide](https://google.github.io/styleguide/rustguide.html) for Rust code on `master` (#845). It applies to every new or modified Rust file in `src/`, `tests/`, `benches/`, `examples/` and `build.rs`. `vendor/`, the `codegraph/` submodule and generated or vendored grammar sources are excluded. This section is an intentional difference between the branches. Do not copy it, the matching CONTRIBUTING.md section or the style cleanup to `beta` unless a later decision extends the adoption. This change integrates the rules only. The repository-wide audit and any cleanup of existing code remain follow-up work tracked by #845; adoption does not imply that the current codebase has been audited or is fully compliant. Precedence, highest first: 1. TokenSave's explicitly documented rules: this file, CONTRIBUTING.md (including path construction and memory/resource management), the crate lint configuration in `src/lib.rs`, and the exceptions listed below. 2. Google's Rust Style Guide. 3. The official [Rust Style Guide](https://doc.rust-lang.org/style-guide/) and [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/), as the Google guide directs. 4. Default `rustfmt` output from the pinned toolchain. Guide sections marked `[chrome]`, Chromium, Android or Fuchsia do not apply. These include small crates, flat file layout, `chromium::import!`, the bans on `async` and standard-library threads, and the gtest/Fuchsia test frameworks. TokenSave is application code (a CLI and MCP server), so, following the guide's "Apply API design rules in context" section, its API-design rules are strong recommendations rather than binding requirements. Project-specific exceptions: - **No `unwrap`/`expect` in library code.** `src/lib.rs` denies `clippy::unwrap_used` and `clippy::expect_used`. This overrides the guide's "Prefer `expect` over `unwrap`". Use pattern matching, `?`, `let ... else` or a typed error instead. Inline library tests inherit these crate lints unless explicitly overridden. In separate test crates and the binary entry point, where permitted by their lint configuration, prefer `expect` with a message that states the invariant. - **Errors instead of panics.** Library code reports broken invariants through `Result` or `Option`, using the guide's option to avoid panics entirely. - **`catch_unwind` as defense in depth.** `safe_extract` in `src/tokensave/extract.rs` catches extractor panics so that one bad file is skipped. Subprocess isolation remains the primary defense. This falls under the guide's defense-in-depth exception. Add no other `catch_unwind` use unless it has the same justification and is approved in review. - **Async, threads and `Arc`.** Tokio, `async`/`.await` and worker threads are part of the architecture. `Arc` is acceptable for state shared across tasks or threads. Justify any other `Rc`/`Arc` use as the guide requires. - **No `rustfmt.toml`.** The guide's `imports_granularity = Module`, `hex_literal_case = Lower` and `format_macro_matchers` options are not available on the pinned stable toolchain, and the guide allows them to be omitted. `max_width = 100` is already the default. Apply one-module-per-`use` and lowercase hex literals by hand in new code. - **Glob imports (tracked deviation).** `src/lib.rs` allows `clippy::wildcard_imports`, and existing code contains glob imports. New code must not add glob imports outside the guide's permitted cases: `use super::*` in inline modules (such as `mod tests`), `use Enum::*` in code focused on that enum, and `pub use submodule::*` re-exports. Existing glob imports are handled by the #845 audit, not in unrelated changes. Exceptions and upstream revisions: - Every deviation must either be listed above or be justified where it occurs, with `#[allow(lint, reason = "...")]` or a nearby comment, and be accepted in PR review. Do not deviate silently. - When Google revises the guide, update this section and CONTRIBUTING.md in a dedicated docs PR that names the revision reviewed and any rules that change. - Keep style-only cleanup behavior-preserving and in separate, reviewable PRs, split by module. Do not mix it with functional changes or reformat unrelated code in a feature PR. See [CONTRIBUTING.md](CONTRIBUTING.md#rust-coding-style) for which rules tooling enforces and which need manual review. ## Build, lint, and test Follow the mandatory [filesystem path construction rule](CONTRIBUTING.md#filesystem-path-construction): use one component per `Path`/`PathBuf` `join` or `push` call for hard-coded relative filesystem paths, as in PR #738. Preserve external path inputs, portable string formats, and documented path-syntax test fixtures. - Toolchain is pinned by `rust-toolchain.toml` (Rust 1.100.0, clippy + rustfmt). - `cargo build` builds the default `full` feature set (50+ languages). For fast dev cycles use `cargo build --no-default-features --features lite` (or `medium`). - `cargo test --workspace --locked --features test-transport` is the CI test command; CI also runs `cargo test --workspace --locked --no-default-features --features lite` and `cargo check --workspace --all-targets --locked --no-default-features --features medium` on Linux. On Windows set `RUST_MIN_STACK=8388608` (CI does this; async-heavy tests overflow the 1 MB MSVC default). - `cargo clippy --workspace --all-targets` (CI runs it with `continue-on-error: true`). - `cargo fmt --all -- --check`. - RustSec advisories: `cargo audit` (install with `cargo install cargo-audit --locked`) from the repository root. `.cargo/audit.toml` sets the policy and lists ignored advisories, each with a written rationale and tracking issue. `.github/workflows/advisories.yml` runs it daily on `master` and `beta` and on changes to `Cargo.lock`/`Cargo.toml`; it is not part of `CI OK`. Run it locally when a change adds or upgrades dependencies, and report new findings in the PR. - Pi extension prompt/relay tests: `node --experimental-strip-types --test tests/pi_extension_test.mjs` (Node 22.6+ with TypeScript stripping support; separate from Cargo tests; run by the `Pi extension` CI job). - Python comparison benchmark tests: `python3 -B -m unittest discover -s benchmarks/tests -v` (POSIX, Python 3.12 in CI; no repository downloads or benchmark binaries; run by the `Python comparison benchmark` CI job). - Documentation checks: `python3 scripts/wiki/sync_wiki.py check` (links, anchors, wiki sidebar) and `python3 -m unittest discover -s scripts/wiki`; preview the wiki with `python3 scripts/wiki/sync_wiki.py build --out /tmp/tokensave-wiki`. Run by `.github/workflows/wiki.yml`, separate from Cargo tests. - `src/lib.rs` denies `clippy::all`, `clippy::unwrap_used`, and `clippy::expect_used`, and warns on `clippy::pedantic` — avoid `unwrap()`/`expect()` in library code. - Language-specific tests: `cargo test --test integration rust_extraction_test::`; single test: `cargo test test_find_stale_files`; filter: `cargo test sync`. Lite-only local loop: `cargo test-lite`. - Integration tests live in `tests/` (one file per module/language) but compile into a single binary, `tests/integration.rs` (`autotests = false`): a new test file must be added there as a `mod`. Files that mutate env vars or the `cancel` flag are separate `[[test]]` targets in `Cargo.toml` instead. Helpers are shared via `crate::common` (`tests/common/mod.rs`); fixtures are in `tests/fixtures/`. See CONTRIBUTING.md "Test layout". - Optional local runner (#984): `scripts/dev-build [args]` uses Blaze on Linux/macOS when it is installed and usable, and Cargo otherwise (Windows: always Cargo); `--cargo` or `TOKENSAVE_BUILD_RUNNER=cargo` forces Cargo. It is local only: never add Blaze to CI, releases or required tooling, and validate with plain `cargo` before a PR. Runner tests: `python3 -B -m unittest discover -s scripts/blaze-bench -p 'test_*.py'` (POSIX, not in CI). Keep the runner's `[env]` export in step with `.cargo/config.toml`; the tests compare them. Details: `docs/BLAZE-EVALUATION.md`. ### Non-standard test/bench harnesses - Cargo vs Blaze local benchmark (#984): `python3 scripts/blaze-bench/blaze_bench.py --source --rev --work --output --markdown `. Linux/WSL only; needs Cargo, Blaze and a C compiler. See `scripts/blaze-bench/README.md`; results in `docs/BLAZE-EVALUATION.md`. - Grammar ASan evaluation (#958): `python3 -B scripts/grammar-asan/run.py --output /tmp/grammar-asan-new` on Linux GNU with the pinned nightly and Clang/compiler-rt. Runner tests: `python3 -B -m unittest discover -s scripts/grammar-asan -v`. See `scripts/grammar-asan/README.md` for instrumentation controls, bounds and evidence. This direct-parser audit complements the extraction-path harness proposed in PR #1005 and adds no CI workflow. Do not describe an incomplete run as a clean sanitizer result. - Graphify coexistence evaluation (#791): `python3 scripts/graphify_probe.py --graphify /path/to/pinned/graphify --benchmark-repo "$PWD" --output /tmp/graphify-results.json`. Requires Linux, GNU time, git, both CLIs, and the Graphify revision in `docs/GRAPHIFY-EVALUATION.md`. Uses disposable copies; does not install assistant integrations. The evaluation records limits and outstanding checks. - Pi real-session regressions: `node scripts/pi-tests/run.mjs /path/to/prepared/pi`; see `scripts/pi-tests/README.md` for the pinned Pi prerequisites and offline scope. - End-to-end agent workflow benchmark (#948): `python3 scripts/agent-bench/agent_bench.py prepare|run|report --work /private/dir ...`; see `scripts/agent-bench/README.md`. `run` makes paid real-model calls through Claude Code; `results.jsonl` holds metrics only unless `--keep-raw`. Synthetic tests: `python3 -m unittest discover -s scripts/agent-bench` (CI `Agent benchmark harness` job). Results and decision report: `docs/AGENT-WORKFLOW-BENCHMARK.md`. - Caveman benchmark harness (#797, draft PR #800): `python3 -m unittest discover -s scripts/caveman_bench -p 'test_*.py'` (synthetic provider responses only; Python 3.11+ and POSIX; CI `Caveman benchmark harness` job). The optional metered agent and manifest builder are `scripts/caveman_bench/anthropic_driver.py` and `configure_anthropic.py`; see `scripts/caveman_bench/README.md`. Never describe test results as paid trials or stock-agent activation evidence; paid runs need explicit model/pricing configuration and operator-provided credentials. - Resolver precision harness (#921): `python3 scripts/resolver-precision/resolver_precision.py --tokensave /path/to/tokensave --output /tmp/result.json --markdown /tmp/result.md`; see `scripts/resolver-precision/README.md`. Requires the pinned rust-analyzer, TypeScript/tsserver wrapper and Pyright versions. Once installed it runs offline against content-addressed fixtures. Matching tests: `python3 -m unittest discover -s scripts/resolver-precision`. Baseline: `docs/RESOLVER-PRECISION-BASELINE.md`. - Conditional Pi read-routing assessment: `python3 scripts/pi-eval/assess_reads.py --help`; see `scripts/pi-eval/READ-ROUTING.md`. This read-only assessment never enables interception; real-model traces and independent review are prerequisites for any later enforcement. - Tree-sitter grammar AddressSanitizer evaluation (#958): `python3 scripts/grammar-sanitizer/grammar_sanitizer.py --work-dir /private/dir --output /private/result.json`; see `scripts/grammar-sanitizer/README.md`. Requires Linux, the pinned nightly and a private work directory outside every checkout. It is manual and informational; the `workflow_dispatch` workflow is never part of normal CI. Preserve the JSON and logs, report resource costs and limitations, and open separate issues for confirmed native crashes with sensitive inputs removed. - Criterion bench against large real repos: `TOKENSAVE_BENCH_REPOS_DIR=~/tokensave-bench-cache cargo bench --bench large_repos`. Without `TOKENSAVE_BENCH_REPOS_DIR` it registers zero benchmarks. Filter with `cargo bench --bench large_repos -- 'scipy/tokensave_search'`. Repos and pinned refs are in `benches/repos.rs`; delete the `/.bench-ref` marker to force a re-fetch. - MCP test-matrix probe: `cargo build --release --bin tokensave && python3 scripts/mcp_probe/probe.py && python3 scripts/mcp_probe/build_matrix.py > matrix.md`. `scripts/mcp_probe/repos.toml` lists target repos (override with `TOKENSAVE_PROBE_REPOS`). - `tokensave bench` is a separate CLI benchmark (fixed query set, retrieval-savings report). - MCP footprint baseline (#917): `python3 benchmarks/mcp_footprint.py --binary target/release/tokensave --repo . --rev --output-dir benchmarks/mcp-footprint`. Stdlib only, any OS; optional `tiktoken` adds token counts. Results feed `docs/MCP-FOOTPRINT-BASELINE.md`; when a change moves the default `tools/list` or `initialize` instructions size, update that report and `DEFAULT_FOOTPRINT_BYTES` in `src/mcp/server.rs` together, with a reason in the PR. ## Experimental Graphify export (master candidate) - Optional external-reader validation: `python3 scripts/graphify/run.py --help`. Follow `scripts/graphify/README.md` for pinned offline prerequisites and the limits of its fixture timings. This command requires the candidate files. - `tokensave graphify-export` reads one selected indexed source file without automatic sync or migrations. Keep its source, field, node, edge, output and lock-wait bounds; retain read-only startup and no-clobber publication. - Run `cargo test --test integration graphify_export_test::` for focused checks. See [the pinned contract](docs/graphify/README.md). This candidate guidance is branch-specific until the implementation is merged; do not copy commands to branches that lack the implementation. ## Architecture Pipeline: source files → tree-sitter parse → language extractors → nodes/edges/unresolved refs → reference resolver → SQLite (libSQL) graph → graph queries → CLI + MCP server. - `src/main.rs` is the CLI + MCP entry point; `src/lib.rs` is the crate root (module declarations + lint config); `src/tokensave/mod.rs` is the `TokenSave` facade that orchestrates indexing/querying/syncing. - `src/extraction/` — one extractor per language, registered behind feature flags (`lite` always compiled; `medium`; `full` default; `lang-*` flags). - `src/db/` — SQLite persistence: connection, sequential migrations (`PRAGMA user_version`), query methods. - `src/resolution/` — cross-file reference resolution (resolver, touched-set tracking, variants). - `src/graph/` — higher-level algorithms: traversal, queries, SCC/cycles, imports, health. - `src/context/` — AI-ready context assembly (builder, formatter, ranking, read modes). - `src/mcp/` — stdio JSON-RPC server, tool definitions, handlers, transport. `src/mcp/roots.rs`: with no default project, a call without `graph_root` sends `roots/list` to the client during the call and answers from the project containing its roots (for MCP proxies; never cached, never sent outside a call). - `src/agents/` — agent integrations (Claude, Codex, Cursor, etc.) and git hooks. - `src/okf/` — Open Knowledge Format v0.3 (#858, #1060; earlier versions read with the §12.1 fallbacks): bounded bundle reader, conformance validator, shared consumer rules (`consumer.rs`: version read modes, bundle metadata, lifecycle, attestation; `trust.rs`: trust tiers, freshness), read-only index export, and import into project state (`okf//`, no schema change) searched by the `tokensave_knowledge` MCP tool (`tokensave okf validate|export|import|list|remove`); official sample bundles per OKF version in `tests/fixtures/okf/official/v0.2/` and `v0.3/`, a reconstructed Graphify bundle in `tests/fixtures/okf/graphify/`; see `docs/guide/OKF.md`. - `src/purge/` — `tokensave purge` (#1055): plan-first, ownership-checked deletion of tokensave-owned global and project state (only entries named in `purge/constants.rs`; links never followed), and `provenance.rs`, which detects the installer of the running executable for the binary-removal reminder and `--version --verbose`. The CLI side is `src/purge_command.rs`; integration cleanup is shared with `uninstall` through `agents::uninstall_agents`. - `src/caveman.rs` — opt-in, read-only Caveman discovery and the extra MCP `initialize` paragraph (#797); see `docs/CAVEMAN-COMPATIBILITY.md`. - `src/grep_annotate/` — `tokensave grep` (#688): argument classification for `grep`, `rg` and `git grep`, hit parsing, and read-only index lookups that add `#ts` facts to each `path:line:` hit; the runner is `src/grep_command.rs`. It is dispatched before startup maintenance, runs the named search binary unchanged, and must not sync, migrate or write. Focused checks: `cargo test --test integration grep_annotate_test::` and `cargo test --lib grep_annotate`; see `docs/guide/CLI-REFERENCE.md`. - `src/sync.rs` — incremental sync (content hashing, stale/new/removed detection). - `src/bench.rs` — `tokensave bench` harness. - `build.rs` compiles vendored grammars (WGSL, ActionScript, GDScript) only when their feature is enabled, generates the ANSI logo and records build metadata. Its named values, including the `VENDORED_GRAMMARS` table, are in `build/constants.rs`. ## Database channel compatibility - Creation origin uses SQLite `application_id` (`0` = legacy/unknown, `0x54535354` = stable, `0x54534254` = beta); it is independent of `user_version` and must never be silently relabeled on migration or open. - Validate origin and supported schema before write pragmas or migrations. Compatibility refusals must bypass crash recovery and forced rebuilds. - `compatibility::preflight` also refuses a `-wal` sidecar whose page size differs from the database's (`src/db/wal_guard.rs`, CVE-2026-119375, #940) before SQLite opens it. A new code path that opens a database with libSQL must call `preflight` or `check_wal_sidecar` first. - Stable state paths stay unchanged. Prerelease builds use `.tokensave/beta/` for project state and the global database. Use `get_tokensave_dir` for project paths; global user settings remain shared. See [docs/DATABASE-COMPATIBILITY.md](docs/DATABASE-COMPATIBILITY.md). ## Setup and environment - Rust 1.100.0 pinned via `rust-toolchain.toml`. - `.cargo/config.toml` sets `CFLAGS`/`CXXFLAGS=-DNDEBUG` (disable C/C++ assertions in vendored grammars), isolates `core.hooksPath` for tests that create real git repos, and reserves an 8 MB main-thread stack on Windows MSVC. - Env-var policy: tokensave-owned variables must start with `TOKENSAVE_`; enforced by `tests/env_var_namespace_test.rs`. External (OS/Cargo/git/agent) vars need an explicit allowlist rationale. Never print env-var values (some carry auth material). - `TOKENSAVE_LOG` controls structured stderr diagnostics (off by default). `--log-level` and `--debug` override it. Keep fields allowlisted and review hook and MCP stdout isolation when adding events. See [Debug diagnostics](docs/guide/TROUBLESHOOTING.md#debug-diagnostics). - `TOKENSAVE_WORKER_TOKEN` — auth for the hidden `extract-worker` subcommand (256-bit token, also required as first 32 bytes on stdin). Direct invocation fails. `TOKENSAVE_DISABLE_SUBPROCESS=1` opts out of the worker pool. - `TOKENSAVE_DISABLE_GREP_HOOK=1` — opt out of the Claude PreToolUse grep guardrail (useful for headless/subagent dispatch). - `TOKENSAVE_TELEMETRY` (falsey value) or `DO_NOT_TRACK` (any value but `0`/`false`/`no`/`off`/empty) — turns the worldwide counter off: no upload, total or flags request and no pending accumulation (#1066). The gate is `cloud::telemetry_enabled`; route any new telemetry call through it. Env can only disable, never re-enable `upload_enabled`. - `TOKENSAVE_AUTO_TRACK` — per-run override of the `auto_track` config field (truthy values enable; `0`/`false`/`no`/`off`/empty disable). - `TOKENSAVE_HOOK_DEBUG` — makes the `post-checkout` hook and `branch add --if-enabled` explain skipped actions on stderr and in `.tokensave/hook-debug.log` (initialized projects only; restarts at 256 KiB). Off by default so hooks stay quiet. - `TOKENSAVE_CAVEMAN_COMPATIBILITY` — per-run override of the user-level `caveman_compatibility` setting (default off; same parsing as `TOKENSAVE_AUTO_TRACK`). - `TOKENSAVE_AUTO_INIT` — per-run override of the user-level `auto_init` setting in `~/.tokensave/config.toml` (same parsing as `TOKENSAVE_AUTO_TRACK`). - `TOKENSAVE_MANAGE_PERMISSIONS` — per-run override of the user-level `manage_permissions` setting (#1067; same parsing). When off, every agent integration leaves its tool approvals (Claude `permissions.allow`, Codex `approval_mode`, Gemini/Qwen `trust`, Kiro `allowedTools`) alone; check `crate::agents::permissions_management_enabled` before writing or removing one. - `TOKENSAVE_USER_CONFIG_DIR` — optional absolute override for machine configuration files and locks, used by isolated release smoke runs. Set the agent home separately for isolated integration configuration. - `TOKENSAVE_STATE_DIR` — base directory for project state (#919), overriding `state_dir` in `~/.tokensave/config.toml`; empty selects the in-repo `.tokensave/` default. Resolution lives in `src/state_dir.rs` behind `config::get_tokensave_dir`; home-directory (global) state must use `config::get_global_tokensave_dir`, which is never relocated. `.cargo/config.toml` sets it empty (`force = false`) so tests ignore a developer's `state_dir`. - Bench vars: `TOKENSAVE_BENCH_REPOS_DIR` (required), `TOKENSAVE_BENCH_REPOS`, `TOKENSAVE_BENCH_SKIP_CLONE`; probe: `TOKENSAVE_PROBE_REPOS`. ## Gotchas and workflow quirks - **Vendored libsql patch (#367).** `vendor/libsql` is a copy of libsql 0.9.30 with a one-line fix for a double-close use-after-free (intermittent `STATUS_ACCESS_VIOLATION` on Windows). `tests/vendored_libsql_test.rs` fails if the patch and vendored copy drift. `cargo package` strips `[patch.crates-io]` and excludes `vendor/libsql`, so those tests pass trivially in a published crate. Delete the patch + vendor copy together once upstream ships the fix. - **Versioning is not SemVer.** Patch/minor/major encode required maintenance: patch = no action, minor = silent reinstall of agent integrations, major = reinstall + forced full reindex. The DB schema has its own version (`LATEST_VERSION` in `src/db/migrations.rs`, stored in `PRAGMA user_version`); reindex detection is schema-driven, not release-version-driven. See `TOKENSAVE-VERSIONING.md`. - **CHANGELOG.md is huge (~500 KB).** The release workflow extracts the `## []` block; run `scripts/prepare-release.py` before a release to promote `[Unreleased]` content into it (idempotent). - **CI disk-space flake.** The Linux runner ships ~10 GB free; the ~713 MB cache restore plus the cargo target dir (34 tree-sitter parsers) can exceed it, so CI frees disk space first. - **CI jobs are gated per job by changed paths.** The `changes` job in `.github/workflows/ci.yml` runs `dorny/paths-filter`, and each job runs only when one of its inputs changed (for example, docs-only changes run no Rust job). When a change adds a file that a build or test reads outside the existing filters (a new `include_str!` source, a fixture directory), extend the matching filters in the same change. `CI OK` aggregates the results and is the check to require; `workflow_dispatch` runs every job. - **No daemon; file watcher is opt-in.** Since 6.1.1 the design is on-demand staleness: every MCP tool call checks for stale files (30 s cooldown), and the server runs a catch-up sync on connect. CLI-only workflows rely on git hooks (`scripts/post-commit`, `scripts/post-checkout`) to keep the index fresh. An opt-in `notify` watcher (#937; `src/mcp/fs_watch.rs`, scoping in `src/tokensave/watch.rs`) runs inside `serve` only when `filesystem_watch_enabled = true` or `serve --watch`; it is off by default, must stay bounded, and feeds the existing sync rather than replacing the on-demand check. FreeBSD uses the kqueue backend in `src/mcp/fs_watch/freebsd.rs` with a shared file and directory watch budget. The native recreation tests in `tests/fs_watch_test.rs` exercise both recreation sequences without an extra file event or MCP tool call. - **`init` vs `sync`.** `sync ` only updates already-initialized projects. A pathless `sync` (what the `post-commit`/`post-merge` hooks run) and `serve` initialize an uninitialized git repository top level when the user-level `auto_init` setting allows it (default on, #876); the gate and its guards (temp dir, `$HOME`, bare/subdirectory, `max_auto_sync_files`) live in `src/auto_init.rs`. Repositories under the system temp dir are always skipped, so tests that expect the old behavior keep it; tests of auto-init give the child its own `TMPDIR`. - **Multi-agent work assumes git worktrees.** tokensave detects nested worktrees and serves the correct branch graph; changes accumulate independently and reconcile via merge/rebase. - **`codegraph` is a git submodule** (`codegraph/`), not part of the crate. - **`test-transport` feature** adds test-only JSON-RPC transport hooks in `src/mcp/`. - **Release workflow** builds per-platform binaries with `--locked` on the pinned toolchain and skips beta/prerelease tags. `release-beta.yml` is disabled: its jobs are gated on the `BETA_CHANNEL_ENABLED` repository variable (`vars`, because `env` is not available in a job-level `if:`), and beta installs from the `beta` branch. - **Publish through the Release workflow.** Create a stable draft with an existing tag, then dispatch `release.yml` with the tag and `publish_draft=true`. The same commit must pass the packaged-crate matrix and native release smoke checks before the workflow makes the draft public. See [release smoke tests](docs/RELEASE-SMOKE.md) for local commands, recovery and post-publication verification. A manual `cargo publish` bypasses the gate and leaves the binary distributions and checksums behind. The separate release-drift workflow checks the resulting stable package and GitHub release.