[PR #13556] fix(snapshot): unify cwd to worktree root and prune stale tmp pack files #14714

Open
opened 2026-02-16 18:19:29 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13556

State: open
Merged: No


Summary

  • Unify all snapshot git commands to use Instance.worktree as cwd, matching restore() and revert() which already do
  • Add stale tmp_pack_* cleanup after git gc to prevent disk bloat from failed gc runs

Problem

Inconsistent cwd across snapshot functions:

restore() and revert() use Instance.worktree as cwd, but track(), patch(), diff(), and diffFull() used Instance.directory — which can be a subdirectory of the worktree. This meant git add . and git diff ... -- . were scoped to the invocation directory rather than the full worktree, creating inconsistent snapshot scope depending on where opencode was launched from.

Stale tmp files after failed gc:

When git gc fails, it leaves tmp_pack_* files in objects/pack/. The existing cleanup() logged a warning and returned early, never cleaning up these orphaned files.

Fix

  1. Change cwd from Instance.directory to Instance.worktree in track(), patch(), diff(), diffFull(), and cleanup() — all snapshot git commands now consistently use worktree root
  2. Add pruneStale() that runs after every cleanup() (success or failure) and removes tmp_pack_* files older than 24 hours from objects/pack/

Testing

bun test test/snapshot/snapshot.test.ts
# 43 pass, 1 skip, 0 fail

New tests:

  • snapshot from subdirectory covers worktree and respects gitignore — invokes snapshot from a subdirectory via Instance.provide({ directory: sub }), verifies files at worktree root are tracked (proving worktree scope) and .gitignore patterns are respected
  • cleanup removes stale tmp files — creates fake tmp_pack_* files with different ages, verifies only 24h+ stale ones are removed while recent ones are preserved

🤖 GENERATED WITH ASSISTANCE OF OhMyOpenCode

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13556 **State:** open **Merged:** No --- ## Summary - Unify all snapshot git commands to use `Instance.worktree` as cwd, matching `restore()` and `revert()` which already do - Add stale `tmp_pack_*` cleanup after `git gc` to prevent disk bloat from failed gc runs ## Problem **Inconsistent cwd across snapshot functions:** `restore()` and `revert()` use `Instance.worktree` as cwd, but `track()`, `patch()`, `diff()`, and `diffFull()` used `Instance.directory` — which can be a subdirectory of the worktree. This meant `git add .` and `git diff ... -- .` were scoped to the invocation directory rather than the full worktree, creating inconsistent snapshot scope depending on where opencode was launched from. **Stale tmp files after failed gc:** When `git gc` fails, it leaves `tmp_pack_*` files in `objects/pack/`. The existing `cleanup()` logged a warning and returned early, never cleaning up these orphaned files. ## Fix 1. Change cwd from `Instance.directory` to `Instance.worktree` in `track()`, `patch()`, `diff()`, `diffFull()`, and `cleanup()` — all snapshot git commands now consistently use worktree root 2. Add `pruneStale()` that runs after every `cleanup()` (success or failure) and removes `tmp_pack_*` files older than 24 hours from `objects/pack/` ## Testing ```bash bun test test/snapshot/snapshot.test.ts # 43 pass, 1 skip, 0 fail ``` New tests: - `snapshot from subdirectory covers worktree and respects gitignore` — invokes snapshot from a subdirectory via `Instance.provide({ directory: sub })`, verifies files at worktree root are tracked (proving worktree scope) and `.gitignore` patterns are respected - `cleanup removes stale tmp files` — creates fake `tmp_pack_*` files with different ages, verifies only 24h+ stale ones are removed while recent ones are preserved --- 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
yindo added the pull-request label 2026-02-16 18:19:29 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14714