Skip to content

feat(native-chat): chat scratch folders expire after 30 days unused - #28394

Merged
brennanb2025 merged 18 commits into
mainfrom
brennanb2025/cs-scratch-expiry
Oct 15, 2026
Merged

brennanb2025 merged 18 commits into
mainfrom
brennanb2025/cs-scratch-expiry

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​115 $\color{#cf222e}{\Huge{\mathbf{−}}}$​336 $\color{#cf222e}{\Huge{\mathbf{−}}}$​221
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​248 $\color{#cf222e}{\Huge{\mathbf{−}}}$​321 $\color{#cf222e}{\Huge{\mathbf{−}}}$​73

ELI5

Each Orca chat has a private scratch folder on the machine that runs it, where its agent saves the HTML pages it shows inline. Today a chat's folder is only removed when the chat record is gone or its local workspace is proven deleted, so the folder of a chat nobody has touched in months lives forever. This PR removes a chat's scratch folder once the chat has gone 30 days without use. Live work and saves Orca has confirmed are never lost.

PR 5 of 6 in the chat-scratch stack. Depends only on PR 2 (#28389, rename the folder to chat-scratch). Nothing else in the stack depends on it.

What Changed

The problem. Scratch folders pile up for every chat that ever drew a visual. The only age rule on main asked the workspace catalog whether a chat's local workspace was deleted. That never fires for a workspace that still exists, never fires for SSH or other remote hosts, and needs about 200 lines of catalog code to answer.

User-facing change. In a chat that has been unused for 30 days, inline visuals show "Visualization unavailable" (the existing state for a missing file). The chat itself, its history and its record stay. Scratch folders written before the PR 2 rename (under the old native-chat-visuals folder) are removed once, along with that old folder.

Mechanism. The host's existing sweep (a few minutes after start, then every 6 hours) now applies these rules to each folder under <state dir>/chat-scratch/:

  1. No record: a folder no chat record maps to is removed (unchanged).
  2. 30 days unused: a chat's age is the later of its newest journal row (one MAX(ts) query) and the newest modification time anywhere in its folder. The folder scan is complete and recursive, never follows links, yields to the event loop, and stops at the first recent entry. Opening or reading a visual doesn't count as use.
  3. Live work vetoes: nothing is expired while the host holds a running agent, a message delivery, or an open dispatch for that chat.
  4. Final decision under locks: after the scan, the sweep takes the chat's existing per-session lane (the one agent starts run in) and then a new per-folder lock. Under both it rechecks the journal, the live-work facts and whether a save landed since the scan, then renames the folder to <hash>.expired. The recursive delete runs after both locks are released. A later run removes any .expired folder a cut-short delete left behind.
  5. Saves share the folder lock: an Orca save of a file inside a chat's scratch folder, both through the desktop's own file write and through a host's file-write call, runs under that folder's lock. The lock reuses the existing keyed promise queue. Saves never take the session lane.
  6. A save that lands after the scan cancels that expiry: the sweep registers the folder before it scans it. Any save into that folder that finishes while the entry exists, including one already running when the scan began, marks it stale before releasing the lock, and the locked recheck then skips the folder. The entry is removed when the sweep finishes with the folder. Nothing is stored on disk; the next run re-derives everything.

What each race now does:

  • An agent start queued behind the expiry runs after the rename and creates a fresh, empty folder.
  • An agent start that wins keeps the folder (its running agent vetoes).
  • A save that wins keeps the folder.
  • A save queued behind the rename fails with "file not found", because a plain write does not create missing parent folders. The editor keeps the unsaved draft.

A save may wait for one recheck plus one rename (a journal query, a few in-memory checks and one rename). It never waits for an agent start or for cleanup work, and a failed sweep never fails a save, because the queue's stored tail never rejects. Brennan approved this wait.

Removed: native-chat-visuals-workspace-verdict.ts (the workspace-catalog verdict), its wiring in the runtime and in orca-runtime-get-worktree-ps.ts, and its tests.

Why

  • Age from the chat's own facts. Journal activity and file times are on the host that runs the chat, so the rule works the same for local, SSH and other remote hosts, and for folder workspaces as well as git worktrees. The workspace-catalog rule only answered for local workspaces and still left idle chats' folders forever.
  • Lock the decision, not the work. Taking the session lane makes expiry ordered against agent starts, so a start cannot reuse a folder that is about to be deleted. The separate folder lock orders expiry against saves without making a save wait for an agent start. Scanning and deleting stay outside both locks, so the locks are held for milliseconds.
  • Invalidate rather than rescan under the lock. An in-place save changes only the file, not its folder's time, so a scan taken before the lock can be stale. Marking the sweep's entry stale on every settled save closes that gap without a second full scan under the lock and without stored state.
  • Considered and rejected: removing folders without any lock (an agent start racing the delete loses its new work); putting saves in the session lane (a save would wait behind a slow agent start); a durable "last used" timestamp (a second copy of facts the journal and file times already hold).

Differences from the common pattern:

  • intended: expiry renames the folder aside under the lock and deletes it afterwards, instead of deleting in place. Safe because the rename is atomic on the same file system and every leftover .expired folder is reaped by a later run.
  • intended: the old native-chat-visuals root is deleted on every run without checking its contents. Native chat has no real users yet and an update restarts the host, which ends every agent that could still write there; after the first run the call does nothing.

Linked Issue

N/A (maintainer PR, part of the chat-scratch stack).

Visual Proof

N/A: background cleanup. The only on-screen effect is the existing "Visualization unavailable" state for a visual whose folder expired.

Testing

  • One sweep fixture against a real temporary folder: a chat unused for 30 days is removed (and the old root is deleted); a recent journal row keeps a chat; a recent file nested in a subfolder keeps a chat; a chat with live work past 30 days is kept; an agent start queued behind expiry gets a fresh folder; a folder with no record is removed; and scan → completed save → locked recheck skips expiry. The existing tests for symlinks, failed removals and scheduling stay.

  • Checked that the start-race and save-race tests fail when their fix is removed.

  • File-write, files RPC and runtime suites pass; node typecheck and orca-ci-checks clean.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Live QA (M4Air, 2026-10-13)

Hidden Orca rig + a paired headless Orca server on the same Mac, real Claude chats, driven over CDP. Code under test: a local merge of PR 6 (80e42ed) and PR 5 (ec2ac14). All 7 QA rows passed.

  • The 30-day expiry itself can't be exercised live. Observed: after relaunching on a profile that still had a native-chat-visuals/ folder, that old root was still present 76 s after launch and gone at 155 s (the first sweep), and the chat's pre-rename visual shows "Visualization unavailable":

pre-rename visual after the old root was removed

A path that doesn't exist yet was authorized by walking up to the nearest
existing folder. A dangling symlink fails realpath with ENOENT, so the walk
stepped over it and approved the link's own path; the write then followed
the link and created its target outside the allowed folders. The walk now
continues from the link's target, and returns that real destination.
…atch folder

The per-chat folder agents write into is the chat's scratch space, not only
a visuals folder. Its root becomes <state>/chat-scratch/<hash> (the hash is
unchanged) and the agent variable becomes ORCA_CHAT_SCRATCH_DIR. The old
ORCA_CHAT_VISUALS_DIR is never set and is still stripped from what every
agent child inherits, Pi included. The visuals skill names the new variable
and says the folder is temporary.
…-write' into brennanb2025/cs-rename-chat-scratch
The launch parity snapshot already pins that every agent drops the
pre-rename variable.
A chat's scratch folder was removed only when its record was gone or its
local workspace was proven deleted, so the folder of a long-idle chat
lived forever.

The sweep now expires a chat's folder once its newest journal row and
the newest entry anywhere in the folder (complete, no-follow scan) are
older than 30 days, unless the host holds a running agent, a delivery or
an open dispatch for the chat. The final recheck and a rename to
<hash>.expired run in the chat's session lane and then a per-folder lock
that Orca scratch saves (desktop IPC and host RPC) also take; the delete
runs outside both. A save that settles after the age scan marks that
expiry stale. Leftover .expired folders and the pre-rename
native-chat-visuals root are removed.

The workspace-removed rule and its catalog verdict are deleted.
Following a dangling link by hand resolved relative targets against the
lexical parent, not the link's real folder: through a folder link the walk
could loop forever or authorize the wrong file. Refuse instead: realpath
can't say where a dangling link leads, but a write would follow it.
…-write' into brennanb2025/cs-rename-chat-scratch
…-work veto

- Remove the other-profile catalog options (running profile id, never-written
  profiles) that only the deleted workspace verdict used; history cleanup's
  reader is unchanged.
- The sweep keeps its original remove-and-warn block; an unused chat's
  folder is parked by one function. Drop the unused `now` option and the
  candidate identity check.
- The live-agent case runs against the host's real runWhileExecutionIdle.
…-write' into brennanb2025/cs-rename-chat-scratch
…ch-expiry

# Conflicts:
#	src/main/native-chat/native-chat-scratch-folder.ts
#	src/main/native-chat/native-chat-visuals-sweep.test.ts
#	src/main/native-chat/native-chat-visuals-sweep.ts
#	src/main/runtime/native-chat-visuals-workspace-verdict.ts
#	src/main/runtime/structured-agent-session-runtime.ts
…ch-expiry

# Conflicts:
#	src/main/runtime/runtime-file-commands-write-file-explorer-file.ts
…ch-expiry

# Conflicts:
#	src/main/runtime/files/file-mutations.ts
@brennanb2025
brennanb2025 marked this pull request as ready for review October 14, 2026 23:27
@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 95f4e32d-3a6c-4d12-9f0e-18566841e674
📥 Commits

Reviewing files that changed from the base of the PR and between db47fbf and 66a28ba.

📒 Files selected for processing: 2
  • config/scripts/ci-shard-timings.json
  • config/scripts/vitest-sqlite-runtime-files.mjs
💤 Files with no reviewable changes: 1
  • config/scripts/ci-shard-timings.json

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The sweep now expires unused native-chat scratch folders based on journal activity and folder modification times, with a 30-day retention period. It coordinates expiry with active execution and concurrent saves. The changes remove workspace-verdict-based cleanup and add tests for expiration, active chats, and save races.

Priority: ⚪ Not assessed

Merge Risk: ⚪ Minimal · up to 66a28

The added sweep test uses the project’s supported Node runtime, and removing the deleted test’s timing entry presents no identified merge risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 56.52% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 13 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: native-chat scratch folders expire after 30 days of inactivity.
Description check Passed The description covers the required explanation, changes, rationale, linked-issue exception, visual-proof rationale, testing, and agent-skill boundary. It omits the Review and Notes sections and most …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 35fef037-4db6-4308-96bb-a4e987604b68
📥 Commits

Reviewing files that changed from the base of the PR and between b0308f2 and ded46b8.

📒 Files selected for processing: 17
  • config/scripts/ci-shard-timings.json
  • config/scripts/vitest-sqlite-runtime-files.mjs
  • src/main/ipc/filesystem/filesystem-write-handlers.ts
  • src/main/native-chat/agent-session-journal/journal-row-table.ts
  • src/main/native-chat/agent-session-wire/structured-agent-session-conversation-lifetime.ts
  • src/main/native-chat/agent-session-wire/structured-agent-session-host.ts
  • src/main/native-chat/native-chat-scratch-folder-lock.ts
  • src/main/native-chat/native-chat-scratch-folder.ts
  • src/main/native-chat/native-chat-visuals-sweep.test.ts
  • src/main/native-chat/native-chat-visuals-sweep.ts
  • src/main/orca-profiles/other-profile-workspace-catalog.test.ts
  • src/main/orca-profiles/other-profile-workspace-catalog.ts
  • src/main/runtime/files/file-mutations.ts
  • src/main/runtime/native-chat-visuals-workspace-verdict.test.ts
  • src/main/runtime/native-chat-visuals-workspace-verdict.ts
  • src/main/runtime/orca-runtime-get-worktree-ps.ts
  • src/main/runtime/structured-agent-session-runtime.ts
💤 Files with no reviewable changes: 4
  • src/main/runtime/native-chat-visuals-workspace-verdict.test.ts
  • src/main/orca-profiles/other-profile-workspace-catalog.test.ts
  • config/scripts/ci-shard-timings.json
  • src/main/runtime/native-chat-visuals-workspace-verdict.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

…e the folder lock

Binary (base64) runtime saves now take the scratch folder lock like text saves, and each folder
keeps a set of expiry candidates so one sweep finishing no longer hides a save from another.
…ch-expiry

# Conflicts:
#	config/scripts/ci-shard-timings.json
@brennanb2025
brennanb2025 merged commit e2fa611 into main Oct 15, 2026
32 checks passed
@brennanb2025
brennanb2025 deleted the brennanb2025/cs-scratch-expiry branch October 15, 2026 01:23
brennanb2025 added a commit that referenced this pull request Oct 15, 2026
Main's #28394 (chat scratch folders expire after 30 days unused) named the
idle sweep's two dispatch checks, `hasOpenDispatch` and
`providerHoldsDispatch`, so its new `runWhileExecutionIdle` veto can reuse
them. That crossed this part's `store` reads handed to the same sweep: the
sweep now gets this part's `store` plus main's named checks. The veto keeps
main's own reads.
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.

1 participant