Harden background Git operations and avoid bundle reload for worktree changes #68

Merged
Christoph merged 1 commit from fix/background-refresh-and-locks into main 2026-11-03 21:08:47 +00:00
Owner

This change hardens background Git network operations, reduces unnecessary full-bundle reloads when only the working tree changes, and includes several related correctness and performance fixes.

Summary

  • Background remote ops: Introduces a unified run_remote_git_output and run_git_with_timeout flow used for network-facing commands. Background runs:

    • run without a terminal (GIT_TERMINAL_PROMPT=0, SSH_ASKPASS_REQUIRE=never) so they never block on passphrase/host-key dialogs.
    • disable optional index locks for read-only refreshes (GIT_OPTIONAL_LOCKS=0).
    • apply a 120s timeout (configurable constant BACKGROUND_REMOTE_TIMEOUT) and forcibly kill the Git process tree if it hangs; helpers are also terminated on Unix via a process-group kill (uses libc::kill behind cfg(unix)).
    • set conservative HTTP low-speed limits unless already configured (GIT_HTTP_LOW_SPEED_*).
    • write Git stdout/stderr to temporary files when running with timeout to avoid blocking pipes when a child is killed.
  • API and callsites:

    • fetch (sync) and fetch_commit_notes gained an optional background: Option<bool> parameter to request the no-dialog/timeout behavior; callers/tests were updated accordingly.
    • A new tauri command working_tree_snapshot(path) returns a compact snapshot (status, files, and a refs_fingerprint) used by the UI to detect working-tree-only changes.
  • Refresh behavior / bundle reloads:

    • Repository bundles now include a refs_fingerprint (HEAD + all refs). The periodic refresh can request a working-tree snapshot and, when refs_fingerprint matches the last bundle, update status and explorer files without reloading the full bundle.
    • Added tests exercising: working-tree-only refresh that updates files without reloading the bundle, that changed refs still reload the bundle, and that a snapshot started before staging cannot overwrite the staged display.
  • Commit paging cap:

    • Introduced MAX_COMMIT_PAGE = 5_000 and used it when bounding commit limits for bundle/page queries (keeps requested page sizes consistent across bundle and paged history).
  • Commit notes / credentials handling:

    • fetch_commit_notes_for_repo now routes through run_remote_git_output, accepts background, and centralizes authenticated vs unauthenticated remote handling. Errors from credentialed fetches now add an AUTH_FAILED: prefix when appropriate.
  • Performance and correctness improvements:

    • Staging operations and status lookups add a StatusLookup that builds a single HashMap for many path lookups (avoids n² scanning when staging lots of files).
    • worktree_blob_hashes now caches computed hashes for untracked/changed files (bounded cache) and avoids repeated hashing when file size and mtime are unchanged; it uses a small settle window before caching to avoid racy writes.
    • status_files_for_paths now uses a HashSet to test presence rather than re-scanning the list per-file.
  • Build change:

    • libc added as a dependency on Unix targets (Cargo.toml/Cargo.lock updated) to support process-group kill.

Files / behavior highlights (cohesive view, not exhaustive):

  • New/changed runtime helpers: src-tauri/src/git/process.rs (run_remote_git_output, run_git_with_timeout, env settings).
  • Background-aware fetch: src-tauri/src/git/sync.rs and src-tauri/src/git/notes.rs accept background and use the new runner.
  • Working-tree-only path: src-tauri/src/git/repository.rs adds WorkingTreeSnapshot, refs_fingerprint, and working_tree_snapshot command; repository bundles include refs_fingerprint.
  • Staging/status improvements: src-tauri/src/git/staging.rs (StatusLookup), src-tauri/src/git/status.rs (worktree hash cache), and related changes.
  • Tests and scripts updated/added: front-end fixture tests (scripts/status-refresh.test.mjs) updated/added to assert no bundle reload for worktree-only changes; several Rust regression/unit tests updated/added to exercise background timeout and signature changes; adjusted script credential tests to account for extra args passed to the remote runner.

Testing

  • Tests were added and existing tests updated in the diff, but no test execution results were provided.

Recommended manual checks for reviewers:

  • Run the full test suite (unit, integration, and the updated script tests).
  • Simulate a remote that hangs or prompts (e.g. an SSH helper that waits) to verify background fetch times out after ~120s and the helper process is killed on Unix.
  • Verify UI refresh: when refs fingerprint is unchanged, a working-tree-only change should update status and explorer files without incrementing bundle reloads; when refs changed, the bundle must reload.
  • Confirm credentialed fetches still follow askpass flow and that credential failures are surfaced as AUTH_FAILED: in the error string where applicable.

Compatibility / reviewer notes

  • Public/internal function signatures changed: fetch and fetch_commit_notes accept an additional optional background boolean. Front-end and any other callers were updated in this branch, but reviewers should scan for other callsites in dependent code.
  • The Unix-only process-group kill uses libc::kill (unsafe block) but is gated with #[cfg(unix)].
  • A new tauri command working_tree_snapshot was introduced; callers that expect a bundle to be reloaded on every refresh should be aware this adds a non-bundle refresh path.

No test execution results were provided.

This change hardens background Git network operations, reduces unnecessary full-bundle reloads when only the working tree changes, and includes several related correctness and performance fixes. Summary - Background remote ops: Introduces a unified `run_remote_git_output` and `run_git_with_timeout` flow used for network-facing commands. Background runs: - run without a terminal (`GIT_TERMINAL_PROMPT=0`, `SSH_ASKPASS_REQUIRE=never`) so they never block on passphrase/host-key dialogs. - disable optional index locks for read-only refreshes (`GIT_OPTIONAL_LOCKS=0`). - apply a 120s timeout (configurable constant `BACKGROUND_REMOTE_TIMEOUT`) and forcibly kill the Git process tree if it hangs; helpers are also terminated on Unix via a process-group kill (uses `libc::kill` behind cfg(unix)). - set conservative HTTP low-speed limits unless already configured (`GIT_HTTP_LOW_SPEED_*`). - write Git stdout/stderr to temporary files when running with timeout to avoid blocking pipes when a child is killed. - API and callsites: - `fetch` (sync) and `fetch_commit_notes` gained an optional `background: Option<bool>` parameter to request the no-dialog/timeout behavior; callers/tests were updated accordingly. - A new tauri command `working_tree_snapshot(path)` returns a compact snapshot (status, files, and a `refs_fingerprint`) used by the UI to detect working-tree-only changes. - Refresh behavior / bundle reloads: - Repository bundles now include a `refs_fingerprint` (HEAD + all refs). The periodic refresh can request a working-tree snapshot and, when `refs_fingerprint` matches the last bundle, update status and explorer files without reloading the full bundle. - Added tests exercising: working-tree-only refresh that updates files without reloading the bundle, that changed refs still reload the bundle, and that a snapshot started before staging cannot overwrite the staged display. - Commit paging cap: - Introduced `MAX_COMMIT_PAGE = 5_000` and used it when bounding commit limits for bundle/page queries (keeps requested page sizes consistent across bundle and paged history). - Commit notes / credentials handling: - `fetch_commit_notes_for_repo` now routes through `run_remote_git_output`, accepts `background`, and centralizes authenticated vs unauthenticated remote handling. Errors from credentialed fetches now add an `AUTH_FAILED:` prefix when appropriate. - Performance and correctness improvements: - Staging operations and status lookups add a `StatusLookup` that builds a single HashMap for many path lookups (avoids n² scanning when staging lots of files). - `worktree_blob_hashes` now caches computed hashes for untracked/changed files (bounded cache) and avoids repeated hashing when file size and mtime are unchanged; it uses a small settle window before caching to avoid racy writes. - `status_files_for_paths` now uses a HashSet to test presence rather than re-scanning the list per-file. - Build change: - `libc` added as a dependency on Unix targets (Cargo.toml/Cargo.lock updated) to support process-group kill. Files / behavior highlights (cohesive view, not exhaustive): - New/changed runtime helpers: `src-tauri/src/git/process.rs` (`run_remote_git_output`, `run_git_with_timeout`, env settings). - Background-aware fetch: `src-tauri/src/git/sync.rs` and `src-tauri/src/git/notes.rs` accept `background` and use the new runner. - Working-tree-only path: `src-tauri/src/git/repository.rs` adds `WorkingTreeSnapshot`, `refs_fingerprint`, and `working_tree_snapshot` command; repository bundles include `refs_fingerprint`. - Staging/status improvements: `src-tauri/src/git/staging.rs` (`StatusLookup`), `src-tauri/src/git/status.rs` (worktree hash cache), and related changes. - Tests and scripts updated/added: front-end fixture tests (`scripts/status-refresh.test.mjs`) updated/added to assert no bundle reload for worktree-only changes; several Rust regression/unit tests updated/added to exercise background timeout and signature changes; adjusted script credential tests to account for extra args passed to the remote runner. Testing - Tests were added and existing tests updated in the diff, but no test execution results were provided. Recommended manual checks for reviewers: - Run the full test suite (unit, integration, and the updated script tests). - Simulate a remote that hangs or prompts (e.g. an SSH helper that waits) to verify background fetch times out after ~120s and the helper process is killed on Unix. - Verify UI refresh: when refs fingerprint is unchanged, a working-tree-only change should update status and explorer files without incrementing bundle reloads; when refs changed, the bundle must reload. - Confirm credentialed fetches still follow askpass flow and that credential failures are surfaced as `AUTH_FAILED:` in the error string where applicable. Compatibility / reviewer notes - Public/internal function signatures changed: `fetch` and `fetch_commit_notes` accept an additional optional `background` boolean. Front-end and any other callers were updated in this branch, but reviewers should scan for other callsites in dependent code. - The Unix-only process-group kill uses `libc::kill` (unsafe block) but is gated with `#[cfg(unix)]`. - A new tauri command `working_tree_snapshot` was introduced; callers that expect a bundle to be reloaded on every refresh should be aware this adds a non-bundle refresh path. No test execution results were provided.
- Background fetches (repo and commit notes) run without a terminal, never
  open SSH dialogs and are killed with their helpers after 120 s, so a hung
  remote can no longer stall all background status updates.
- Git never prompts on a terminal; stalled HTTP transfers abort after 60 s.
- GIT_OPTIONAL_LOCKS=0 keeps the periodic `git status` from taking
  index.lock, which made concurrent add/commit fail sporadically.
- The bundle history limit matches paged history (5000), so auto-refresh no
  longer truncates history beyond 500 loaded commits.
- Auto-refresh reloads branches, tags, stashes and history only when a ref
  changed (new working_tree_snapshot command with a show-ref fingerprint).
- Untracked-file hashes for rename detection are cached by size and mtime.
- Staging lookups use a hash index instead of a scan per selected file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.