Skip to content

Build your personal knowledge base with Trilium Notes

core: honor #versioningLimit for the cleanup operation

Adorian Dorand254d4e007cf
diff --git a/apps/server/internal.openapi.yaml b/apps/server/internal.openapi.yaml
index 3f1d573aaa..6f4f526dee 100644
--- a/apps/server/internal.openapi.yaml
+++ b/apps/server/internal.openapi.yaml
@@ -1866,9 +1866,9 @@ paths:
type: integer
minimum: -1
description: >
- Snapshots to keep per note, overriding the #versioningLimit label and the
- revisionSnapshotNumberLimit option. -1 keeps every snapshot, 0 keeps none.
- Omitted, the configured limits apply.
+ Snapshots to keep per note, standing in for the revisionSnapshotNumberLimit
+ option. -1 keeps every snapshot, 0 keeps none. Omitted, the option applies.
+ A note carrying a valid #versioningLimit follows its own label instead.
keepNamedSnapshots:
type: boolean
description: >
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 41b54a0b3e..43d62c6873 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -65,9 +65,12 @@ export interface RevisionPojo {
*/
export interface EraseExcessRevisionsOptions {
/**
- * How many snapshots to keep per note, overriding both the `#versioningLimit` label and the
- * `revisionSnapshotNumberLimit` option for this run. Negative keeps every snapshot (nothing is
- * excess), zero keeps none; omitted falls back to the configured limits.
+ * How many snapshots to keep per note, standing in for the `revisionSnapshotNumberLimit` option
+ * for this run. Negative keeps every snapshot (nothing is excess), zero keeps none; omitted
+ * falls back to the option itself.
+ *
+ * A note carrying a valid `#versioningLimit` follows its own label instead: that is a policy set
+ * on the note deliberately, and a one-off answer to the global setting does not overrule it.
*/
snapshotsToKeep?: number;
/**
diff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.ts
index 4eb21af950..fdaede56a5 100644
--- a/packages/trilium-core/src/becca/entities/bnote.ts
+++ b/packages/trilium-core/src/becca/entities/bnote.ts
@@ -1604,14 +1604,12 @@ class BNote extends AbstractBeccaEntity<BNote> {
/**
* Erases the oldest revision snapshots beyond the number to keep, newest kept first.
*
- * How many are kept is normally configured: the note's own `#versioningLimit` label if it
- * carries a valid one, else the `revisionSnapshotNumberLimit` option. A negative limit keeps
- * every snapshot — nothing is ever excess — while zero keeps none of them.
+ * A negative limit keeps every snapshot — nothing is ever excess — while zero keeps none.
*
* @returns how many snapshots were erased.
*/
eraseExcessRevisionSnapshots({ snapshotsToKeep, keepNamedSnapshots }: EraseExcessRevisionsOptions = {}): number {
- const limit = Number.isInteger(snapshotsToKeep) ? Number(snapshotsToKeep) : this.getConfiguredSnapshotLimit();
+ const limit = this.resolveSnapshotLimit(snapshotsToKeep);
if (limit < 0) {
return 0;
@@ -1637,11 +1635,24 @@ class BNote extends AbstractBeccaEntity<BNote> {
return revisionIds.length;
}
- /** The configured count to keep: the note's own label takes priority over the option. */
- private getConfiguredSnapshotLimit(): number {
+ /**
+ * How many snapshots this note keeps, most specific answer first: its own `#versioningLimit`,
+ * then the caller's override, then the `revisionSnapshotNumberLimit` option.
+ *
+ * The label outranks the override rather than the other way round. A label is a policy set on
+ * this note deliberately, while an override is a one-off answer standing in for the global
+ * setting — so it replaces that setting, and leaves a note that was given its own limit alone.
+ */
+ private resolveSnapshotLimit(override: number | undefined): number {
const labelled = parseInt(this.getLabelValue("versioningLimit") ?? "");
- return Number.isInteger(labelled) ? labelled : parseInt(optionService.getOption("revisionSnapshotNumberLimit"));
+ if (Number.isInteger(labelled)) {
+ return labelled;
+ }
+
+ return Number.isInteger(override)
+ ? Number(override)
+ : parseInt(optionService.getOption("revisionSnapshotNumberLimit"));
}
/**
diff --git a/packages/trilium-core/src/becca/entities/bnote_content.spec.ts b/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
index 9e3b600e8b..1112be89c7 100644
--- a/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
+++ b/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
@@ -528,16 +528,32 @@ describe("BNote content / misc getters", () => {
const eraseExcess = (note: BNote, options: EraseExcessRevisionsOptions) =>
getContext().init(() => note.eraseExcessRevisionSnapshots(options));
- it("keeps the newest snapshots the override asks for, whatever the configured limit says", () => {
+ it("keeps the newest snapshots the override asks for, in place of the global setting", () => {
const note = createNote();
- // Both configured limits are wider than the override, which still has the last word.
- getContext().init(() => note.setLabel("versioningLimit", "10"));
const [ , , newer, newest ] = createRevisions(note, [ undefined, undefined, undefined, undefined ]);
expect(eraseExcess(note, { snapshotsToKeep: 2 })).toBe(2);
expect(revisionIdsOf(note)).toEqual([ newer, newest ]);
});
+ it("leaves a note carrying its own #versioningLimit to that label, override or not", () => {
+ const note = createNote();
+ const ids = createRevisions(note, [ undefined, undefined, undefined ]);
+ // Labelled only now: saving a revision trims as it goes, so a limit set up front would
+ // have left nothing here to keep.
+ getContext().init(() => note.setLabel("versioningLimit", "3"));
+
+ // The label is a policy set on this note; an override stands in for the global setting
+ // and does not overrule it, so the harsher figure asked for is simply not applied here.
+ expect(eraseExcess(note, { snapshotsToKeep: 1 })).toBe(0);
+ expect(revisionIdsOf(note)).toEqual(ids);
+
+ // And the label governs just as firmly when it is the harsher of the two.
+ getContext().init(() => note.setLabel("versioningLimit", "1"));
+ expect(eraseExcess(note, { snapshotsToKeep: 10 })).toBe(2);
+ expect(revisionIdsOf(note)).toEqual([ ids[2] ]);
+ });
+
it("keeps everything at a negative override and nothing at zero", () => {
const note = createNote();
const ids = createRevisions(note, [ undefined, undefined ]);
diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.ts
index b7915dbfb3..0544ee9ed6 100644
--- a/packages/trilium-core/src/services/space_usage.ts
+++ b/packages/trilium-core/src/services/space_usage.ts
@@ -587,10 +587,10 @@ function collectContentSizeOf(noteIds: string[]): number {
* nothing narrower was asked for.
*
* An estimate rather than a promise, because the limit it answers for is one number across every
- * note, while the operation left to itself follows each note's own `#versioningLimit`. A note
- * labelled to keep more than the stated limit frees less than its share of this figure, so the
- * estimate reads high — the direction an estimate of reclaimable space should err in anyway. Run
- * the operation with the same options and the two agree exactly, the override outranking the label.
+ * note, while the operation follows each note's own `#versioningLimit` wherever one is set — a label
+ * outranks the limit asked for here, being a policy set on that note deliberately. A note labelled
+ * to keep more than the stated limit therefore frees less than its share of this figure, so the
+ * estimate reads high, which is the direction an estimate of reclaimable space should err in.
*
* @param revisionOptions how far the trimming being estimated would go; `snapshotsToKeep` defaults
* to 0, which dooms the lot.