GeezerVison is showing github.com — fix(core): keep opencode tmp files on disk and sweep them after 7 days by opencode-agent[bot] · Pull Request #64031 · anomalyco/opencode
Conversation
opencode-agent
Bot
commented
Nov 15, 2026
•
edited by opencodejr
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Issue for this PR
Related: #49073
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
global.tmp was os.tmpdir()/opencode. On many Linux setups that directory is RAM. Arch, Fedora, openSUSE Tumbleweed, Debian 13 and Ubuntu 24.10+ mount /tmp as tmpfs, and Fedora and archinstall default to zram swap. Agent scratch work (clones, builds, node_modules), generated images and app/phone uploads pile up there in memory until a reboot. One report had 17 GB of agent scratch directories; another had repeated OOM kills of a long-running server. Nothing cleaned that directory.
global.tmp now lives on disk at <XDG_DATA_HOME>/opencode/tmp (~/.local/share/opencode/tmp), next to shell/ and tool-output/. workerd keeps its own roots.
- A new
TmpRetention global node sweeps global.tmp at startup and then hourly. Each top-level entry is one unit (a scratch project, say), and so is each child of uploads/ and images/. A unit is deleted whole, and only when no entry in it was created or changed for 7 days, judged by the newer of mtime and ctime with lstat. A project still in use is never deleted in part: one recent file keeps all of it, including old node_modules. ctime catches archives and cp -p copies that keep old mtimes. The walk stops at the first recent entry, so a unit in use costs a few stats; a stale one is walked once and then removed. A timestamp of 0 counts as recent. The node is a dependency of InstructionBuiltIns, the node that advertises the directory.
- The
<env> line now says files there untouched for 7 days are deleted automatically.
Not changed: the short-lived opencode-untracked-* git index files still use os.tmpdir() and are removed by acquireUseRelease. Existing /tmp/opencode contents are not migrated; tmpfs drops them on reboot anyway. On Windows this also moves tmp off %TEMP%, which touches #64019. A unit idle for 7 days can be deleted just as something starts writing into it again; that window is one sweep.
How did you verify your code works?
- New
packages/core/test/tmp-retention.test.ts: stale units are deleted whole, including per-upload units; a unit with one recent file keeps its old node_modules; a freshly created file with an old mtime is kept.
- Updated the
global-roots, global and instructions/builtins tests. The builtins test now uses its own temp directory and removes it afterwards, since the sweep runs there.
bun test passes for tmp-retention, builtins, tool-output, shell-retention, agent and tool-image-generate in core, for global and global-roots in util, and for updater-install in cli. bun run typecheck passes in core and util. oxlint, the effect ast-grep rules and prettier are clean on the changed files.
- Cost of a full walk: 127k entries (a 2.9 GB
node_modules) took 3.6 s with a warm cache. That is the worst case for one stale unit, paid once before it is removed.
Screenshots / recordings
N/A
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Requested by: dan via Slack
Added by the opencode bot (AI-generated).
Fixes #55006
Fixes #44696
Related: #49073, #64019, #56391
Public API: default tmp directory (Global.Path.tmp, /api/info paths.tmp) moves from os.tmpdir()/opencode to <XDG_DATA_HOME>/opencode/tmp; TMPDIR/TEMP no longer move it, with no override (behaviour change)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works as described. With the PR, debug paths shows the tmp folder under the data directory instead of /tmp/opencode. I put 10-day-old nested files, a fresh file and a 1970-dated file in it and started OpenCode from the PR with opencode-drive. At startup the old files and their old empty folders were deleted, and the fresh and 1970-dated files were kept. The new and updated tests pass, and tsc --noEmit is clean in core and util.
Public API: the default Global.Path.tmp moves from os.tmpdir()/opencode to <XDG_DATA_HOME>/opencode/tmp. /api/info paths.tmp, app uploads, generated images and the <env> prompt now point there. The HTTP, SDK and config shapes are unchanged. TMPDIR/TEMP (on Windows) no longer change where OpenCode's tmp folder goes, and there is no replacement setting. Anything that relied on /tmp/opencode will need the new path.
Concerns:
- On Windows, the hourly cleanup can delete the extra links to the running binary that
RetainedImage keeps in this folder (see the inline comment).
TMPDIR/TEMP are no longer honoured, and nothing replaces them. Users who point their temp folder at another drive or a RAM disk on purpose (#64019) lose that control, which makes #64019 worse rather than just touching it. Could a deliberately set env var still win, or could you add a setting for this folder?
Fixes #55006
Fixes #44696
Fixes #56391
Related: #49073, #64019
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
entries,
|
|
|
(entry) => {
|
|
|
const target = path.join(current, entry.name)
|
|
|
if (entry.type !== "Directory") return Effect.sync(() => void files.push(target))
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, global.tmp also holds RetainedImage links: opencode-upgrade-<pid>-*.exe (the package-removal step of uninstall, via updater.removal → retaining(…, global.tmp)) and the opencode-service-* links that uninstall moves here. These are hard links, so their mtime is the binary's mtime, which is usually older than 7 days. Any OpenCode process that cleans up during an uninstall will delete them while their process is still running. That is exactly what they exist to prevent: Bun spins forever when a package replacement meets the last link of a running image. Could the cleanup skip the opencode-(service|upgrade)-<pid>-*.exe pattern (RetainedImage already sweeps dead ones), or could those links move out of tmp?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new commit only changes a test: the builtins test now deletes its temporary directory when it finishes. That test passes. Neither earlier point is addressed: the Windows cleanup problem with the uninstall copies of the running binary (open inline thread), and TMPDIR/TEMP no longer having any effect on OpenCode's tmp folder, with no setting to replace them (#64019).
Public API: unchanged since the last review. The default tmp folder (/api/info paths.tmp) still moves from os.tmpdir()/opencode to <XDG_DATA_HOME>/opencode/tmp.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Windows thread is addressed. The cleanup now uses the newer of mtime and ctime, and creating or renaming a hard link updates the change time, so the uninstall/upgrade copies of the running binary are no longer deleted straight away. I checked this with a hard link whose mtime was years old: an immediate TmpRetention.cleanup kept it, and a run with "now" 8 days ahead deleted it along with an old project folder. The new tests are small and clear. Core tests and tsc --noEmit pass.
Public API impact is unchanged from the last review: the default tmp folder (paths.tmp) still moves to <XDG_DATA_HOME>/opencode/tmp, and the earlier point about TMPDIR/TEMP still stands. This commit reverts the FileRetention epoch change, so #56391 is no longer fixed by this PR.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|