From c1f9d99d8076dbad34c751a69302888e0676b843 Mon Sep 17 00:00:00 2001
From: Brian Hursey
Date: Mon, 23 Nov 2026 15:28:25 +0000
Subject: [PATCH] Stop scheduled prunes at the quota target
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Pruning previously deleted every photo in the plan; it now halts once usage is
back under the cleanup target, so a plan computed earlier cannot over-delete.
If the library is already below target the cleanup is cancelled and the owner is
notified instead. Notification emails also skip the "open Luminary" link when no
public URL is configured, and the admin panel keys the prune action off the
plan's freed bytes rather than the warning flag.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff
---
server/db.mjs | 2 +-
server/quota.mjs | 82 +++++++++++++++++++++++------------
src/components/AdminPanel.tsx | 10 ++---
3 files changed, 60 insertions(+), 34 deletions(-)
diff --git a/server/db.mjs b/server/db.mjs
index 5ced803..25efdb1 100644
--- a/server/db.mjs
+++ b/server/db.mjs
@@ -116,7 +116,7 @@ db.exec(`
CREATE TABLE IF NOT EXISTS user_quota (
user_id TEXT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
- bytes_limit INTEGER NOT NULL DEFAULT 100 * 1024 * 1024 * 1024,
+ bytes_limit INTEGER NOT NULL DEFAULT (100 * 1024 * 1024 * 1024),
prune_at INTEGER,
prune_plan TEXT,
prune_notified_at INTEGER,
diff --git a/server/quota.mjs b/server/quota.mjs
index c9b7715..6c18d92 100644
--- a/server/quota.mjs
+++ b/server/quota.mjs
@@ -40,7 +40,7 @@ const fmtDate = (ms) =>
minute: '2-digit',
});
-const appUrl = () => PUBLIC_URL || 'http://localhost:8787';
+const appUrl = () => PUBLIC_URL;
function safePlan(value) {
if (!value) return null;
@@ -185,6 +185,7 @@ function escapeHtml(value) {
async function pruneWarningEmail(user, state, plan, pruneAt) {
const when = fmtDate(pruneAt);
+ const link = appUrl();
const subject = `Luminary: ${fmtBytes(plan.freedBytes)} will be pruned in 24 hours`;
const text = [
`Hi ${user.name || user.email},`,
@@ -196,8 +197,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) {
planText(plan) || '• No individual items were identified.',
'',
`This should free about ${fmtBytes(plan.freedBytes)} and leave your library around ${fmtBytes(plan.projectedBytes)}.`,
- '',
- `Review your library now: ${appUrl()}`,
+ ...(link ? ['', `Review your library now: ${link}`] : []),
'',
'Deleting photos or emptying Trash before the scheduled time will cancel or reduce the cleanup.',
'— Luminary',
@@ -218,7 +218,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) {
${planHtml(plan) || '
No individual items were identified.
'}
This should free about ${fmtBytes(plan.freedBytes)} and leave your
library around ${fmtBytes(plan.projectedBytes)}.
Deleting photos or emptying Trash before the scheduled
time will cancel or reduce the cleanup. — Luminary
@@ -227,6 +227,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) {
}
async function quotaWarningEmail(user, state) {
+ const link = appUrl();
const subject = `Luminary: your storage is ${state.percent}% full`;
const text = [
`Hi ${user.name || user.email},`,
@@ -234,8 +235,7 @@ async function quotaWarningEmail(user, state) {
`You are nearing your Luminary storage quota: ${fmtBytes(state.used)} used of ${fmtBytes(state.limit)} (${state.percent}%).`,
'',
'Free some space by deleting photos you no longer need or emptying Trash. If the quota is exceeded, an administrator can schedule a 24-hour storage cleanup.',
- '',
- `Open Luminary: ${appUrl()}`,
+ ...(link ? ['', `Open Luminary: ${link}`] : []),
'',
'— Luminary',
].join('\n');
@@ -247,7 +247,7 @@ async function quotaWarningEmail(user, state) {
${fmtBytes(state.limit)} Luminary quota (${state.percent}%).
Delete photos you no longer need or empty Trash to free space. If the quota is exceeded,
an administrator can schedule a 24-hour storage cleanup.
`;
@@ -321,30 +321,21 @@ export function quotasForAdmin() {
});
}
-export async function sendTestNotification(to) {
- if (!mailStatus().configured) {
- throw new Error('Add the Gmail address and app password first');
- }
- await sendAppMail({
- to,
- subject: 'Luminary email notifications are working',
- text: 'This is a test message from Luminary. Quota warnings and prune notices are configured correctly.',
- html: '
This is a test message from Luminary. Quota warnings and prune notices are configured correctly.
',
- });
-}
-
-async function executePlan(userId, plan) {
+async function executePlan(userId, plan, targetBytes) {
let removedPhotos = 0;
let freedBytes = 0;
+ let current = quotaUsage(userId);
const eventIds = new Set();
- for (const event of plan.events ?? []) eventIds.add(event.id);
-
for (const event of plan.events ?? []) {
+ if (current <= targetBytes) break;
+ eventIds.add(event.id);
for (const photoId of event.photos ?? []) {
const photo = getPhoto(photoId);
if (!photo) continue;
- freedBytes += Number(photo.size) || 0;
+ const bytes = Number(photo.size) || 0;
+ freedBytes += bytes;
+ current -= bytes;
removePhoto(photoId);
clearDenoised(photoId);
await removeAsset(photoId);
@@ -352,9 +343,12 @@ async function executePlan(userId, plan) {
}
}
for (const entry of plan.photos ?? []) {
+ if (current <= targetBytes) break;
const photo = getPhoto(entry.id);
if (!photo) continue;
- freedBytes += Number(photo.size) || 0;
+ const bytes = Number(photo.size) || 0;
+ freedBytes += bytes;
+ current -= bytes;
removePhoto(entry.id);
clearDenoised(entry.id);
await removeAsset(entry.id);
@@ -377,8 +371,40 @@ async function pruneDueAccounts() {
continue;
}
try {
- const before = quotaUsage(row.user_id);
- const result = await executePlan(row.user_id, plan);
+ const stateBefore = quotaState(row.user_id);
+ const targetBytes = Math.floor(stateBefore.limit * QUOTA_TARGET_RATIO);
+ if (stateBefore.used <= targetBytes) {
+ saveQuota(row.user_id, {
+ pruneAt: null,
+ prunePlan: null,
+ pruneNotifiedAt: null,
+ });
+ const link = appUrl();
+ await sendAppMail({
+ to: user.email,
+ subject: 'Luminary: scheduled storage cleanup cancelled',
+ text: [
+ `Hi ${user.name || user.email},`,
+ '',
+ 'Your scheduled Luminary storage cleanup was cancelled because your library is now below the cleanup target. Nothing was deleted.',
+ ...(link ? ['', `Open Luminary: ${link}`] : []),
+ '',
+ '— Luminary',
+ ].join('\n'),
+ html: `
+
+
Scheduled cleanup cancelled
+
Hi ${escapeHtml(user.name || user.email)},
+
Your scheduled Luminary storage cleanup was cancelled because your library is
+ now below the cleanup target. Nothing was deleted.
+
— Luminary
+
+ `,
+ }).catch((err) => console.error(`[luminary] cancel email to ${user.email} failed: ${err.message}`));
+ continue;
+ }
+ const before = stateBefore.used;
+ const result = await executePlan(row.user_id, plan, targetBytes);
const after = quotaUsage(row.user_id);
saveQuota(row.user_id, {
pruneAt: null,
@@ -386,6 +412,7 @@ async function pruneDueAccounts() {
pruneNotifiedAt: null,
});
const state = quotaState(row.user_id);
+ const link = appUrl();
await sendAppMail({
to: user.email,
subject: 'Luminary: your scheduled storage cleanup is complete',
@@ -394,8 +421,7 @@ async function pruneDueAccounts() {
'',
`The scheduled Luminary cleanup removed ${result.removedPhotos} photo${result.removedPhotos === 1 ? '' : 's'} and freed ${fmtBytes(before - after)}.`,
`Your library is now ${fmtBytes(after)} of ${fmtBytes(state.limit)} (${state.percent}%).`,
- '',
- `Open Luminary: ${appUrl()}`,
+ ...(link ? ['', `Open Luminary: ${link}`] : []),
'',
'— Luminary',
].join('\n'),
diff --git a/src/components/AdminPanel.tsx b/src/components/AdminPanel.tsx
index fa946fc..c80c4d2 100644
--- a/src/components/AdminPanel.tsx
+++ b/src/components/AdminPanel.tsx
@@ -292,8 +292,8 @@ export default function AdminPanel({ user, onClose, onChanged, onRestored }: Pro
};
const scheduleQuotaPrune = (quota: AdminQuota) => {
- if (!quota.warn) {
- setError('This account only needs a prune after it reaches the warning threshold');
+ if (quota.plan.freedBytes <= 0) {
+ setError('There is nothing old enough to prune. The account is already below the cleanup target');
return;
}
if (!overview?.mail.configured) {
@@ -889,12 +889,12 @@ export default function AdminPanel({ user, onClose, onChanged, onRestored }: Pro
) : (