Skip to content

Add shared-base worktree overlay core APIs - #168

Merged
Shengyu Fu (shengyfu) merged 8 commits into
mainfrom
shengyfu-shared-worktree-indexes
Oct 4, 2026
Merged

Shengyu Fu (shengyfu) merged 8 commits into
mainfrom
shengyfu-shared-worktree-indexes

Conversation

@shengyfu

@shengyfu Shengyu Fu (shengyfu) commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Start the tgrep-side foundation for reusing one content index across multiple worktrees, without changing existing CLI or server behavior.

  • Add tgrep_core::shared::SharedBase, which shares one validated Arc<IndexReader> and its path table across independent worktree-rooted HybridIndex views.
  • Save only per-worktree postings, masks, and deletion tombstones in atomically replaced checkpoints; never merge the overlay into the shared base.
  • Bind checkpoints to a fingerprint of the exact base path table, lookup table, and postings, plus the canonical worktree root. Reject incompatible, malformed, missing, and wrong-base/root checkpoints rather than silently falling back to the base.
  • Validate shared snapshots' physical sections, metadata counts, contiguous posting layout, valid trigrams, sorted/unique in-range posting IDs, and nonzero location masks, while accepting valid empty indexes and short files.
  • Prevent checkpoint publication into the base by directory identity. Unix staging/replacement/cleanup use an open directory handle; Windows retains non-delete-sharing handles on the canonical parent and all ancestors throughout publication.
  • Reject directory-shaped checkpoint paths instead of silently normalizing them into filenames.
  • Preserve existing Unicode-root checkpoint strings and encode non-Unicode roots losslessly using platform-native units.
  • Document the API, its immutability requirements, and its integration boundaries.

Design

Shared worktree index architecture and rollout

The design document uses the generic term agent runtime and includes diagrams for repository-scoped ownership, per-worktree search, and immutable base generations. It distinguishes the implemented core foundation from proposed Git discovery, synchronization, and daemon integration.

Recommended rollout: merge this backwards-compatible foundation independently once review and required checks are satisfied, then implement base-generation management, worktree synchronization, and daemon/agent runtime integration in focused follow-up PRs rather than expanding this PR into the entire feature.

Backward compatibility and scope

Existing CLI commands, RPC protocol, on-disk index format, and single-root server behavior remain unchanged. Existing HybridIndex::open and ordinary IndexReader::open/validate_lookup behavior is preserved; stricter integrity and coverage checks apply only to the new shared-base API.

This PR is a core-library increment, not automatic shared indexing in the CLI. Git-delta discovery, repository-scoped daemon registration, per-worktree watchers/visibility/caches, and runtime lifecycle integration remain follow-up work. Callers must supply all worktree differences and reconcile restored overlays before serving searches, and keep the base files immutable while readers reference them.

Checkpoint replacement is atomic visibility, not power-loss durability: file contents are synced, but the parent directory is not synced after replacement. Missing or stale checkpoints require reconciliation or rebuilding. Existing Unicode-root checkpoints remain compatible; older readers reject the new native-root representation.

Validation

  • Latest CI: full workspace builds, benchmark builds, and tests passed on Linux, macOS, and Windows; formatting and Clippy passed.
  • Local Windows targeted run: 264 core unit tests, 30 shared-worktree integration tests, and 7 snapshot consistency tests passed.
  • Regression coverage includes reader sharing, worktree isolation, masks/tombstones/empty files, exact-base identity, restoration, malformed sections/metadata/postings, duplicate or descending posting IDs with conflicting masks, native-root identity, repeated saves, Windows failure recovery, renamed parents/bases, symlink redirection, directory-shaped destination paths, cleanup, and private Unix checkpoint permissions.
  • cargo test -p tgrep-core --lib --test shared_worktrees --test snapshot_consistency --locked --quiet
  • cargo clippy --workspace --all-targets --locked --quiet -- -D warnings
  • cargo fmt --all -- --check
  • git diff --check

Introduce shared immutable readers and independent worktree overlays with atomic delta-only checkpoints. Preserve existing CLI, server, RPC, and index-format behavior.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 17:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Introduces a new core API for sharing a single immutable base content index across multiple worktrees via independent live overlays and JSON checkpoints, without changing existing CLI/server behavior.

Changes:

  • Added tgrep_core::shared::SharedBase for shared base reader + per-worktree overlay creation, saving, and restoration with strict base/root identity checks.
  • Added IndexReader::snapshot_id() to fingerprint base lookup/postings/path table identity independent of directory.
  • Added regression tests and documentation for shared-worktree behavior and checkpoint invariants.
File Description
tgrep-core/​src/​shared.rs Adds the shared-base API, overlay checkpoint format, validation, and save/restore logic.
tgrep-core/​src/​reader.rs Adds snapshot fingerprinting used to bind checkpoints to exact base bytes.
tgrep-core/​src/​hybrid.rs Refactors reader opening to support SharedBase creating worktrees from a shared Arc<IndexReader>.
tgrep-core/​src/​lib.rs Exposes the new shared module publicly.
tgrep-core/​tests/​shared_worktrees.rs Adds regression tests covering reader sharing, overlay isolation, checkpoint validity, and failure cases.
tgrep-core/​Cargo.toml Moves tempfile to normal dependencies to support library usage.
README.md Documents the new shared-worktree core API and its integration boundaries.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tgrep-core/src/shared.rs Outdated
Comment thread tgrep-core/src/shared.rs Outdated
Document atomic visibility separately from power-loss durability. Exercise repeated saves with changed overlays and Windows sharing-violation recovery while retaining tempfile's existing atomic overwrite implementation.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 03:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Base validation and checkpoint path/root handling contain correctness and containment gaps.

Review effort: Balanced
Findings: 2 High severity

Open (2)
Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Support non-Unicode filesystem paths in checkpoint serialization

tgrep-core/​src/​shared.rs:168

Serializing PathBuf through Serde JSON fails for canonical roots containing non-UTF-8 Unix bytes or unpaired Windows UTF-16. create_worktree accepts those valid filesystem paths, but save_overlay can never checkpoint them, so this public API has an undocumented path restriction. Encode the platform-native root bytes losslessly in the checkpoint, or reject unsupported roots when creating the view and document that constraint.

Comment thread tgrep-core/src/reader.rs
Comment thread tgrep-core/src/shared.rs Outdated
Capture the proposed agent runtime integration, query flow, immutable base lifecycle, compatibility boundaries, and staged implementation plan with architecture diagrams.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 20:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Snapshot identity can omit malformed postings, and checkpoint replacement can escape its validated destination through path-resolution races.

Review effort: Balanced
Findings: 2 High severity

Open (2)

Reject mismatched empty sections and inconsistent metadata counts only for shared bases. Persist checkpoints using the validated canonical destination and preserve non-Unicode root identities with platform-native encodings while retaining legacy Unicode strings.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 20:53
Keep filesystem round-trips on Linux and Windows, and test Unix byte serialization independently of filesystem filename restrictions on macOS.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Checkpoint parent validation retains a TOCTOU race that can redirect writes into the protected base directory.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (2)

Comment thread tgrep-core/src/shared.rs Outdated
Copilot AI balanced review requested due to automatic review settings October 3, 2026 20:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Base validation remains incomplete, and a parent-directory race can bypass snapshot write protection.

Review effort: Balanced
Findings: 2 High severity

Open (2)

Comment thread tgrep-core/src/reader.rs
Use handle-relative temporary files and replacement on Unix, and hold non-delete-sharing ancestor handles for Windows path-based publication. Exclude bases by directory identity and cover parent replacement, renamed bases, cleanup, private permissions, and Windows locking.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 21:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Checkpoint path parsing can overwrite an unintended file when the supplied path ends with a separator or current-directory component.

Review effort: Balanced
Findings: 2 High severity

Open (2)
Resolved since last review (1)

Comment thread tgrep-core/src/shared.rs Outdated
Reject misaligned, overlapping, gapped, or unreferenced postings and validate every shared-base posting reference and location mask without tightening ordinary readers. Preserve exact checkpoint file-path semantics by rejecting trailing separators and current-directory suffixes.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 21:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Snapshot validation accepts duplicate posting IDs that can cause false-negative search results.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (3)

Comment thread tgrep-core/src/reader.rs
Reject duplicate and descending file IDs before a base can reach query-time deduplication, preserving distinct masks rather than silently discarding them. Keep ordinary readers unchanged and cover conflicting-mask duplicates.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 21:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Cross-platform atomic filesystem publication and snapshot-integrity logic warrant final human review despite passing CI and comprehensive tests.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@shengyfu
Shengyu Fu (shengyfu) merged commit ad8fffa into main Oct 4, 2026
12 checks passed
@shengyfu
Shengyu Fu (shengyfu) deleted the shengyfu-shared-worktree-indexes branch October 4, 2026 01:20
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.

2 participants