Skip to content

Prune clipboard image files that fall off the visible history - #14819

Open
JJDizz1L wants to merge 1 commit into
omacom:quattrofrom
JJDizz1L:clipboard-prune-images
Open

JJDizz1L wants to merge 1 commit into
omacom:quattrofrom
JJDizz1L:clipboard-prune-images

Conversation

@JJDizz1L

Copy link
Copy Markdown

The clipboard stores up to historyLimit entries (500) but only displays 50, and nothing ever deleted the image files of entries that aged out, were deleted, or were cleared — capture.sh writes one file per copy while every history mutation only rewrites the JSON. On my machine that meant 1,628 files / 1.6GB with only 99 still referenced.

This change:

  • Lowers historyLimit to 50 so storage matches what the picker shows.
  • Adds ClipboardHistory.evictedImagePaths(previous, next) and calls it from every history mutation (add, remove, clear, load-trim); evicted paths are deleted through the new hidden omarchy-clipboard-prune-images helper, which only removes files inside clipboard-images/.
  • Adds a one-time migration sweeping image files the current history no longer references.
  • Extends test/shell.d/clipboard-test.sh (JS unit tests, QML wiring assertions, helper safety tests).

Verified: clipboard-test.sh and ./test/cli pass; migration checked against a scratch home (orphan removed, idempotent, missing history/dir safe). Display behavior is unchanged — existing displayRows tests cover that.

@greptile-apps

greptile-apps Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 2/5

[Medium impact] The changes affect local clipboard history and its saved image files.

Do not merge until cleanup preserves retained images across folder aliases and concurrent copies.

Findings

  1. P1 Cleanup deletes images through aliases ▶
  2. P1 Cleanup deletes a recopied image ▶
  3. P1 Migration deletes a new capture ▶
  4. P2 Read failures leave temporary files ▶
  5. P2 Source checks miss broken cleanup. ▶

Summary

Clipboard history now keeps 50 entries and removes image files when their entries leave history.

  • Clipboard history keeps 50 entries and drops image files with them.
  • A one-time sweep removes old clipboard images no longer in history.

Reviews (3) · Last reviewed commit: "Prune clipboard image files that fall of..." · Reviewed by Greptile

Comment thread bin/omarchy-clipboard-prune-images Resolved
if (!paths || paths.length === 0) return
var command = [root.omarchyPath + "/bin/omarchy-clipboard-prune-images"]
for (var i = 0; i < paths.length; i++) command.push(paths[i])
Quickshell.execDetached(command)

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.

P1 Cleanup deletes a recopied image

pruneImages starts deletion without waiting, but capture.sh reuses the same filename whenever the same image is copied. If an evicted image is copied again before the helper deletes it, capture reuses the existing file and adds its path back to history. The pending helper then deletes that live image, breaking its preview and later paste. Coordinate capture and deletion so a scheduled removal cannot delete a newly retained file.

Comment thread migrations/1791593012.sh Outdated
Comment on lines +23 to +30
while IFS= read -r -d '' file; do
if grep -Fxq "$file" "$referenced"; then
continue
else
rm -f -- "$file"
removed=$((removed + 1))
fi
done < <(find "$image_dir" -maxdepth 1 -type f -print0)

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.

P1 Migration deletes a new capture

The migration reads history once, then deletes files against that old list while clipboard capture can still run. capture.sh creates temporary files inside the scanned folder and moves each finished image there before emitting its history entry. The sweep can delete an image being captured, or one saved after the history snapshot, leaving a missing image in history. Run the sweep with capture paused or coordinate both operations before deleting.

Comment thread migrations/1791593012.sh Resolved
Comment thread bin/omarchy-clipboard-prune-images Resolved
@jandrusk

Copy link
Copy Markdown

Nice find on the 1.6 GB of orphaned images. A few things before this is safe to merge:

Blocking: the migration can delete every image if the history can't be parsed. Migrations run under bash -euo pipefail, so if jq fails on a malformed or partially written clipboard-history.json, the || true swallows it, $referenced ends up empty or partial, and the loop removes everything in clipboard-images/. I'd bail out instead:

if [[ -r $history_path ]]; then
  jq -r '.. | objects | select(.type == "image") | .path // empty' "$history_path" | sort -u >"$referenced" || {
    echo "Could not read clipboard history; leaving images alone"
    exit 0
  }
fi

The prefix check in omarchy-clipboard-prune-images doesn't stop ... $path == $image_dir/* matches $image_dir/../../.config/foo, so the "cannot delete outside of it" comment isn't quite true. Resolving first fixes it:

real=$(realpath -e -- "$path" 2>/dev/null) || continue
[[ $real == "$image_dir"/* && -f $real ]] || continue
rm -f -- "$real"

Smaller things:

  • capture.sh writes its in-flight clipboard.XXXXXX temp files into the same directory and honors XDG_STATE_HOME. The migration's find -type f can delete a temp file mid-capture (maybe skip clipboard.*), and the helper and migration hardcode ~/.local/state.
  • Dropping historyLimit from 500 to 50 also silently trims 450 text entries on first load (loadHistory saves the trimmed list). That seems like a maintainer product call rather than part of an image cleanup fix.

Overlap: #14919 ("Bound clipboard history and safely collect removed captures") goes after the same leak, so it'd be good to compare the two.

@omarchybot

Copy link
Copy Markdown
Collaborator

Thanks for tracking this down. I read the diff and the discussion so far. I would classify it as a bug fix, since image files in clipboard-images/ are never deleted once their history entries go. Every item here also gets a second model's classification, and that check could not run on this pass: Codex Medium answers, but its review budget for today is used up. So this has no class label yet and has not been run on a test machine. It will go round again once the second opinion is available, and that pass will cover the points already raised above and the overlap with #14919.

@JJDizz1L

Copy link
Copy Markdown
Author

Rebased onto current quattro (resolves the conflicts in Clipboard.qml, ClipboardHistory.js, and clipboard-test.sh from the i18n/SizedImage/test-rewrite changes) and addressed the review findings:

  • Helper path escape (greptile P2 / jandrusk): resolves each path with realpath -e and confines to the image dir, so .. and symlinks pointing outside can no longer delete outside it.
  • Recopy race (greptile P1): QML now saves history before queueing the delete, and the helper re-reads the history file at delete time, skipping paths that are referenced again (capture.sh reuses filenames by content hash).
  • Migration race (greptile P1): skips clipboard.* in-flight temp files and files newer than 5 minutes, so a capture landing during the sweep is not deleted.
  • Read-failure deletion (greptile P2 / jandrusk): both helper and migration bail out (leak, don't delete) when an existing history file cannot be parsed. Also fixed: jq ... | sort reports sort's status, so the check no longer uses a pipeline.
  • Custom state dir (greptile P2): helper and migration use ${XDG_STATE_HOME:-$HOME/.local/state}, same rule as capture.sh.

Tests: test/shell.d/clipboard-test.sh (97 checks) and ./test/cli (136 checks) pass, including new cases for ../symlink confinement, still-referenced skip, unreadable-history bail, XDG handling, and migration tmp-file/recency skipping.

Note: historyLimit 500 -> 50 is kept as originally intended; flagging for maintainer signoff since it also trims text entries.

Comment thread migrations/1791593012.sh Resolved
Comment thread test/shell.d/clipboard-test.sh Outdated
Comment on lines +189 to +203
const clipboardQml = require('fs').readFileSync(require('path').join(root, 'shell/plugins/clipboard/Clipboard.qml'), 'utf8')

assert(
/property int historyLimit: 50/.test(clipboardQml),
'clipboard stores as many entries as it displays'
)
assert(
/function pruneImages\(paths\)[\s\S]*omarchy-clipboard-prune-images/.test(clipboardQml),
'clipboard deletes evicted image files through the prune helper'
)
assertEqual(
(clipboardQml.match(/root\.pruneImages\(/g) || []).length,
4,
'clipboard prunes images on add, remove, clear, and history load'
)

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.

P2 Source checks miss broken cleanup. These assertions check the spelling of historyLimit, the helper name, and the number of calls, rather than whether cleanup works. AGENTS.md explicitly forbids tests that reread source to confirm an edit. Remove these checks or replace them with a test that performs a history change and checks the files left behind. The repeated evictedImagePaths cases at lines 158–187 also add no coverage beyond lines 68–97.

File Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread test/shell.d/clipboard-test.sh Resolved
The clipboard stores up to historyLimit entries but only displays 50, and
nothing ever deleted the image files of entries that aged out, so
clipboard-images/ grew without bound. Keep as many entries as are shown and
delete each evicted entry's image file when it leaves the history, and sweep
unreferenced images once via migration.
@JJDizz1L
JJDizz1L force-pushed the clipboard-prune-images branch from bf085dd to 077dc55 Compare November 20, 2026 07:04
@JJDizz1L

Copy link
Copy Markdown
Author

Second pass, addressing the new review (all three threads):

  • Custom state dir reads wrong history (P1): you were right — Clipboard.qml line 21 always saves history under $HOME/.local/state, while my helper/migration read it under $XDG_STATE_HOME. Fixed: history is now always read from $HOME/.local/state/omarchy/clipboard-history.json (matching the picker), and deletions/sweeps cover both the XDG and $HOME image dirs. New tests cover the split layout both ways.
  • Test-rule violations (P2 x2): removed all QML string-grep assertions and the migration-direct tests per AGENTS.md and agents/skills/migrations.md. The sweep logic moved into bin/omarchy-clipboard-prune-images --sweep (tested as a command); migrations/1791593012.sh is now a thin wrapper. Migration itself verified by hand against scratch homes (sweep, idempotent, missing history/dir safe) instead of in test/.
  • Recopy/migration races + unreadable history (carried P1/P2s): unchanged from last push (save-before-prune, helper re-check, tmp/recency guards, bail on unreadable history) — but the history-path fix above is what makes the re-check actually read the right file.

One more hardening from a real incident: while hand-testing the migration I mixed HOME with my ambient XDG_STATE_HOME and swept my own image dir. --sweep now refuses to delete anything when the history references zero images (leak, don't delete) — verified that this guard blocks exactly that scenario. No test in the suite can touch real dirs anymore: every helper/sweep invocation pins XDG_STATE_HOME to the scratch home (suite passes with ambient XDG set and unset, 96 checks).

Commit is signed now (previous force-push showed Unverified — that was my rebase, fixed with -S).

Comment on lines +87 to +90
if grep -Fxq "$file" "$referenced"; then
continue
else
rm -f -- "$file"

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.

P1 Cleanup deletes images through aliases. If ~/.local/state is a symlink to a custom XDG_STATE_HOME, both entries in image_dirs reach the same files under different names. History stores the custom path. The sweep keeps that path on its first pass, but the second pass uses the home-based path, fails this exact-text check, and deletes retained images older than five minutes. Resolve both folder names and saved paths before comparing them, and scan each resolved folder only once.

Comment on lines +49 to +55
else
echo "Could not read clipboard history; leaving images alone" >&2
return 1
fi
elif [[ -e $history_path ]]; then
echo "Could not read clipboard history; leaving images alone" >&2
return 1

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.

P2 Read failures leave temporary files. load_referenced creates two temporary files, then returns early when history cannot be read. Neither file is removed: cleanup of raw is below the return, and callers install the trap for referenced only after success. Remove both files on failure or install cleanup before attempting the read.

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.

3 participants