Skip to content

A local-first personal finance app

fix infinite loop when remainder is impossible to solve (#7623)

Matt Fiddaman46687da7a8c0
diff --git a/packages/loot-core/src/server/budget/category-template-context.test.ts b/packages/loot-core/src/server/budget/category-template-context.test.ts
index 057d846aea..5615ee76ff 100644
--- a/packages/loot-core/src/server/budget/category-template-context.test.ts
+++ b/packages/loot-core/src/server/budget/category-template-context.test.ts
@@ -8,6 +8,7 @@ import type { Template } from '#types/models/templates';
import * as actions from './actions';
import { CategoryTemplateContext } from './category-template-context';
+import { distributeRemainder } from './goal-template';
import * as statements from './statements';
// Mock getSheetValue and getCategories
@@ -1106,6 +1107,41 @@ describe('CategoryTemplateContext', () => {
expect(result).toBe(101);
});
+ it('remainder loop terminates when per-context share rounds to zero', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const makeInstance = () =>
+ new TestCategoryTemplateContext(
+ [
+ {
+ type: 'remainder',
+ weight: 1,
+ directive: 'template',
+ priority: null,
+ },
+ ],
+ category,
+ '2024-01',
+ 0,
+ 0,
+ );
+
+ const contexts = [
+ makeInstance(),
+ makeInstance(),
+ makeInstance(),
+ makeInstance(),
+ makeInstance(),
+ ];
+
+ const remaining = distributeRemainder(contexts, 2);
+ expect(remaining).toBe(2);
+ });
+
it('remainder wont over budget', async () => {
const category: CategoryEntity = {
id: 'test',
diff --git a/packages/loot-core/src/server/budget/goal-template.ts b/packages/loot-core/src/server/budget/goal-template.ts
index 4317404285..0321573d33 100644
--- a/packages/loot-core/src/server/budget/goal-template.ts
+++ b/packages/loot-core/src/server/budget/goal-template.ts
@@ -11,6 +11,27 @@ import { getSheetValue, isTrackingBudget, setBudget, setGoal } from './actions';
import { CategoryTemplateContext } from './category-template-context';
import { checkTemplateNotes, storeNoteTemplates } from './template-notes';
+export function distributeRemainder(
+ templateContexts: CategoryTemplateContext[],
+ availBudget: number,
+): number {
+ let remainderContexts = templateContexts.filter(c => c.hasRemainder());
+ while (availBudget > 0 && remainderContexts.length > 0) {
+ let remainderWeight = 0;
+ remainderContexts.forEach(
+ context => (remainderWeight += context.getRemainderWeight()),
+ );
+ const perWeight = availBudget / remainderWeight;
+ const beforePass = availBudget;
+ remainderContexts.forEach(context => {
+ availBudget -= context.runRemainder(availBudget, perWeight);
+ });
+ if (availBudget === beforePass) break;
+ remainderContexts = templateContexts.filter(c => c.hasRemainder());
+ }
+ return availBudget;
+}
+
type Notification = {
type?: 'message' | 'error' | 'warning' | undefined;
pre?: string | undefined;
@@ -275,18 +296,7 @@ async function processTemplate(
}
// run remainder
- let remainderContexts = templateContexts.filter(c => c.hasRemainder());
- while (availBudget > 0 && remainderContexts.length > 0) {
- let remainderWeight = 0;
- remainderContexts.forEach(
- context => (remainderWeight += context.getRemainderWeight()),
- );
- const perWeight = availBudget / remainderWeight;
- remainderContexts.forEach(context => {
- availBudget -= context.runRemainder(availBudget, perWeight);
- });
- remainderContexts = templateContexts.filter(c => c.hasRemainder());
- }
+ availBudget = distributeRemainder(templateContexts, availBudget);
// finish
templateContexts.forEach(context => {
diff --git a/upcoming-release-notes/7623.md b/upcoming-release-notes/7623.md
new file mode 100644
index 0000000000..145000c5c1
--- /dev/null
+++ b/upcoming-release-notes/7623.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [matt-fidd]
+---
+
+Fix infinite loop when applying remainder templates with an amount that can not be divided