# \[codex\] Prevent background Git fetches from leaking temp packs · PR \#3537 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/3537)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

State: closed

Source repository: pingdotgg/t3code

Source branch: codex/fix-git-fetch-temp-pack-leaks

Target branch: main

Head commit: 58340604f0b7967e7a5a3e532993f3443442d14e

[Discussion and reviews](https://git.cafe/pingdotgg/t3code/pull/3537)

[Commits](https://git.cafe/pingdotgg/t3code/pull/3537/commits?format=markdown)

[Files changed](https://git.cafe/pingdotgg/t3code/pull/3537/diffs?format=markdown)

## Description

---

## Summary

- remove only newly-created `tmp_pack_*` files after a failed background status fetch
- fail closed when a root or linked-worktree `FETCH_HEAD.lock` indicates concurrent fetch activity
- expose upstream refresh failures separately from stale status reads so the existing scheduler applies exponential backoff
- extend the status-fetch timeout from 5 seconds to 1 minute and cache failures for 30 seconds

## Root cause

The status refresher killed every fetch after 5 seconds. Repositories whose fetches took longer could leave `.git/objects/pack/tmp_pack_*` files behind on every attempt. The driver then swallowed the refresh failure while returning stale status, so the broadcaster observed a successful status read and never activated its failure backoff.

## Impact

Long-running or failed background fetches no longer accumulate newly-created temporary packs. Existing temporary packs are deliberately preserved, and cleanup is skipped whenever concurrent Git fetch activity cannot be ruled out.

This PR is stacked on #2679.

Closes #3525.

## Validation

- `vp check`
- `vp run typecheck`
- `vp test apps/server/src/vcs/GitVcsDriverCore.test.ts apps/server/src/vcs/VcsStatusBroadcaster.test.ts apps/server/src/git/GitWorkflowService.test.ts` (47 tests)

<!-- Macroscope's pull request summary starts here -->
<!-- Macroscope will only edit the content between these invisible markers, and the markers themselves will not be visible in the GitHub rendered markdown. -->
<!-- If you delete either of the start / end markers from your PR's description, Macroscope will append its summary at the bottom of the description. -->
> [!NOTE]
> ### Remove temporary Git pack files left by failed background fetches in `GitVcsDriverCore`
> - In [`GitVcsDriverCore.ts`](https://github.com/pingdotgg/t3code/pull/3537/files#diff-1c3e2b9763c6526006485633fb677c83dc54cc1ea5563a1e51aab707c35854d8), `fetchRemoteForStatus` now snapshots `tmp_pack_*` files before fetching and deletes newly created ones after a failed fetch, skipping cleanup when a `FETCH_HEAD.lock` is present to avoid interfering with concurrent worktree fetches.
> - Introduces a new `BackgroundPolicy` service that gates background work (VCS status refresh, provider health checks, etc.) based on client activity leases, host power state, and a configurable activity profile (balanced/performance/battery-saver).
> - Adds a full resource telemetry subsystem: a Rust native sidecar (`native/resource-monitor`), a `NativeTelemetryClient` and `DesktopTelemetryPublisher` to stream process metrics, and a `ResourceTelemetry` service that merges native and desktop telemetry into grouped snapshots with history.
> - Rewrites `ProcessDiagnostics` and `ProcessResourceMonitor` to source data from `ResourceTelemetry` rather than spawning `ps`/`powershell`; process signaling now requires a matching `startTimeMs` identity.
> - Adds background activity settings UI in [`SettingsPanels.tsx`](https://github.com/pingdotgg/t3code/pull/3537/files#diff-9dbbb4f86928d777550dead352b372341ccfb3f589782aa6d99b98f792c89d18) and wires the Git fetch interval through the new background activity override system.
> - Risk: `ProcessDiagnostics.signal` now rejects requests where `startTimeMs` does not match the live telemetry snapshot, and `ProcessResourceMonitor` no longer self-samples—both are breaking behavioral changes for callers relying on prior error shapes.
>
> <!-- Macroscope's review summary starts here -->
>
> <details>
> <summary>📊 <a href="https://app.macroscope.com">Macroscope</a> summarized 5834060. 71 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted</summary>
>
> ### 🗂️ Filtered Issues
> No issues evaluated.
>
>
> </details><!-- Macroscope's review summary ends here -->
>
<!-- macroscope-ui-refresh -->
<!-- Macroscope's pull request summary ends here -->

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Touches background `git fetch` and filesystem cleanup in `.git/objects/pack`; incorrect lock or cleanup logic could interfere with concurrent Git activity, though tests cover lock skip and pack removal.
> 
> **Overview**
> Background upstream fetches for VCS status no longer leave behind new `tmp_pack_*` files when they fail or time out. **`fetchRemoteForStatus`** snapshots pack dir contents before fetch, surfaces non-zero exits as **`GitCommandError`** (instead of swallowing them), and on failure removes only **new** `tmp_pack_*` files—skipping cleanup when **`FETCH_HEAD.lock`** is present on the common dir or linked worktrees.
> 
> Fetch tuning changes: status upstream timeout **5s → 1 minute**, failure cache cooldown **5s → 30s**.
> 
> A dedicated **`refreshStatusUpstream`** path is added on **`GitVcsDriver`** / **`GitWorkflowService`**. **`VcsStatusBroadcaster`** calls it before invalidating remote cache and reads remote status with **`refreshUpstream: false`**, so fetch failures can drive backoff without discarding cached snapshots.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 58340604f0b7967e7a5a3e532993f3443442d14e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
