A local-first personal finance app
automation UI: dry run should show full demand (#7790)
Matt Fiddaman070144f1821e
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsxindex ed7c5494e1..9a8aca1178 100644--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsBody.tsx@@ -2,8 +2,10 @@ import { useEffect, useState } from 'react';import { Trans } from 'react-i18next';import { Button } from '@actual-app/components/button';+import { SvgInformationCircle } from '@actual-app/components/icons/v2';import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';+import { Tooltip } from '@actual-app/components/tooltip';import { View } from '@actual-app/components/view';import { send } from '@actual-app/core/platform/client/connection';import type {@@ -255,18 +257,46 @@ export function BudgetAutomationsBody({</Text></View><View style={{ textAlign: 'right', flexShrink: 0, minWidth: 220 }}>- <Text+ <Viewstyle={{- fontSize: 11,- textTransform: 'uppercase',- color: theme.pageTextSubdued,- letterSpacing: '0.04em',+ flexDirection: 'row',+ alignItems: 'center',+ justifyContent: 'flex-end',+ gap: 4,}}>- <Trans>- Projected for {{ month: formatMonthLabel(month, locale) }}- </Trans>- </Text>+ <Text+ style={{+ fontSize: 11,+ textTransform: 'uppercase',+ color: theme.pageTextSubdued,+ letterSpacing: '0.04em',+ }}+ >+ <Trans>+ Projected for {{ month: formatMonthLabel(month, locale) }}+ </Trans>+ </Text>+ <Tooltip+ content={+ <View style={{ maxWidth: 260 }}>+ <Trans>+ The projection shows the most that these automations could+ budget on their own. The actual amount may be smaller when+ To Budget is empty or when higher-priority categories run+ first.+ </Trans>+ </View>+ }+ placement="bottom end"+ >+ <SvgInformationCircle+ width={12}+ height={12}+ style={{ color: theme.pageTextSubdued, cursor: 'help' }}+ />+ </Tooltip>+ </View><Textstyle={{fontSize: 22,diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/ConflictBanner.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/ConflictBanner.tsxindex 4fb4e2f868..e5955bc838 100644--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/ConflictBanner.tsx+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/ConflictBanner.tsx@@ -1,8 +1,6 @@-import { SvgAlertTriangle } from '@actual-app/components/icons/v2';import { Text } from '@actual-app/components/text';-import { theme } from '@actual-app/components/theme';-import { View } from '@actual-app/components/view';+import { Error } from '#components/alerts';import {GlobalConflictDetail,GlobalConflictTitle,@@ -15,25 +13,20 @@ type ConflictBannerProps = {export function ConflictBanner({ conflict }: ConflictBannerProps) {return (- <View+ <Errorstyle={{- padding: '8px 22px',- backgroundColor: theme.errorBackground,- borderBottom: `1px solid ${theme.errorBorder}`,- color: theme.errorText,+ padding: '8px 12px',+ margin: '12px 24px 0',fontSize: 12,- flexDirection: 'row',- gap: 8,- alignItems: 'center',+ flexShrink: 0,}}>- <SvgAlertTriangle width={14} height={14} style={{ color: 'inherit' }} />- <Text style={{ color: 'inherit' }}>+ <Text><strong><GlobalConflictTitle conflict={conflict} />.</strong>{' '}<GlobalConflictDetail conflict={conflict} /></Text>- </View>+ </Error>);}diff --git a/packages/loot-core/src/server/budget/category-template-context.ts b/packages/loot-core/src/server/budget/category-template-context.tsindex 27ba413250..4a4cbae572 100644--- a/packages/loot-core/src/server/budget/category-template-context.ts+++ b/packages/loot-core/src/server/budget/category-template-context.ts@@ -49,6 +49,7 @@ export class CategoryTemplateContext {category: CategoryEntity,month: string,budgeted: number,+ skipAvailableClamp: boolean = false,) {// get all the needed setup valuesconst lastMonthSheet = monthUtils.sheetForMonth(@@ -96,6 +97,7 @@ export class CategoryTemplateContext {hideDecimal.data.length > 0? hideDecimal.data[0].value === 'true': false,+ skipAvailableClamp,);}@@ -286,7 +288,12 @@ export class CategoryTemplateContext {}// don't overbudget when using a priority unless income category- if (priority > 0 && available < 0 && !this.category.is_income) {+ if (+ priority > 0 &&+ available < 0 &&+ !this.category.is_income &&+ !this.skipAvailableClamp+ ) {this.fullAmount = (this.fullAmount || 0) + toBudget;const adjusted = Math.max(0, toBudget + available);if (toBudget > 0) scale *= adjusted / toBudget;@@ -383,6 +390,7 @@ export class CategoryTemplateContext {private goals: GoalTemplate[] = [];private priorities: Set<number> = new Set();readonly hideDecimal: boolean = false;+ readonly skipAvailableClamp: boolean = false;private remainderWeight: number = 0;private toBudgetAmount: number = 0; // amount that will be budgeted by the templatesprivate perTemplateContribution = new Map<Template, number>();@@ -406,6 +414,7 @@ export class CategoryTemplateContext {budgeted: number,currencyCode: string,hideDecimal: boolean = false,+ skipAvailableClamp: boolean = false,) {this.category = category;this.month = month;@@ -413,6 +422,7 @@ export class CategoryTemplateContext {this.previouslyBudgeted = budgeted;this.currency = getCurrency(currencyCode);this.hideDecimal = hideDecimal;+ this.skipAvailableClamp = skipAvailableClamp;// sort the template lines into regular template, goals, and remainder templatesif (templates) {templates.forEach(t => {diff --git a/packages/loot-core/src/server/budget/goal-template.test.ts b/packages/loot-core/src/server/budget/goal-template.test.tsindex 49cb5281d1..248a5a9c9d 100644--- a/packages/loot-core/src/server/budget/goal-template.test.ts+++ b/packages/loot-core/src/server/budget/goal-template.test.ts@@ -74,6 +74,9 @@ function setupAqlForCategoryLookup(cat: CategoryEntity) {dependencies: [],};}+ if (queryStr.includes('categories')) {+ return { data: [cat], dependencies: [] };+ }return { data: [], dependencies: [] };});}@@ -216,98 +219,39 @@ describe('dryRunCategoryTemplate', () => {expect(result).toEqual({ budgeted: 0, perTemplate: [0] });});- it('competes with sibling remainder templates under wide scope', async () => {- // Remainder triggers wide scope: the engine must see the sibling so- // weights divide the leftover proportionally instead of giving it all- // to the target.- const sibling: CategoryEntity = {- id: 'cat-sibling',- name: 'Other',- group: 'g1',- is_income: false,- };- setupSheetMock({ 'to-budget': 20000 });- setupAqlForWideScope(- [- {- category: sibling,- templates: [- {- type: 'remainder',- weight: 1,- directive: 'template',- priority: null,- },- ],- },- ],- [category, sibling],- );+ it('shows full demand even when To Budget cannot cover it', async () => {+ // With priority>0 and a constrained To Budget, the engine clamps the+ // actual budgeted amount to fit. The dry run should still report the+ // templates' full demand so the user sees what the rules want, not+ // what would survive clamping.+ setupSheetMock({ 'to-budget': 5000 });+ setupAqlForCategoryLookup(category);const templates: Template[] = [{- type: 'remainder',- weight: 3,+ type: 'periodic',+ amount: 1800,+ period: { period: 'month', amount: 1 },+ starting: '2026-05-01',directive: 'template',- priority: null,+ priority: 150,},- ];- const result = await dryRunCategoryTemplate({- month: '2024-01',- categoryId: category.id,- templates,- });- expect(result.budgeted).toBe(15000);- expect(result.perTemplate).toEqual([15000]);- });-- it('reads availBudget consumed by siblings for percentage of available funds', async () => {- // Sibling at priority 1 spends $50 of $200 first, leaving $150. The- // target's 50%-of-available-funds at priority 2 should then resolve to- // $75, not the $100 a narrow-scope run would produce.- const sibling: CategoryEntity = {- id: 'cat-sibling',- name: 'Other',- group: 'g1',- is_income: false,- };- setupSheetMock({ 'to-budget': 20000 });- setupAqlForWideScope(- [- {- category: sibling,- templates: [- {- type: 'periodic',- amount: 50,- period: { period: 'month', amount: 1 },- starting: '2024-01-01',- directive: 'template',- priority: 1,- },- ],- },- ],- [sibling, category],- );-- const templates: Template[] = [{- type: 'percentage',- percent: 50,- previous: false,- category: 'available funds',+ type: 'periodic',+ amount: 3200,+ period: { period: 'month', amount: 3 },+ starting: '2026-06-01',directive: 'template',- priority: 2,+ priority: 150,},];const result = await dryRunCategoryTemplate({- month: '2024-01',+ month: '2026-06',categoryId: category.id,templates,});- expect(result.budgeted).toBe(7500);- expect(result.perTemplate).toEqual([7500]);+ expect(result.budgeted).toBe(500000);+ expect(result.perTemplate).toEqual([180000, 320000]);});});diff --git a/packages/loot-core/src/server/budget/goal-template.ts b/packages/loot-core/src/server/budget/goal-template.tsindex 9839e4cacd..0feadd9a76 100644--- a/packages/loot-core/src/server/budget/goal-template.ts+++ b/packages/loot-core/src/server/budget/goal-template.ts@@ -214,6 +214,7 @@ async function computeTemplates(force: boolean,categoryTemplates: Record<CategoryEntity['id'], Template[]>,categories: CategoryEntity[] = [],+ skipAvailableClamp: boolean = false,): Promise<ComputedTemplates> {// setup categoriesconst isTracking = isTrackingBudget();@@ -247,6 +248,7 @@ async function computeTemplates(category,month,budgeted,+ skipAvailableClamp,);// don't use the funds that are not from templatesif (!templateContext.isGoalOnly()) {@@ -359,19 +361,21 @@ export async function dryRunCategoryTemplate({categoryId: CategoryEntity['id'];templates: Template[];}): Promise<DryRunCategoryResult> {- // Always run the full set of saved category templates and override the- // target's saved templates with the in-flight ones. Sibling categories- // consume from To Budget at each priority; running them too keeps the- // projection in sync with what an actual apply would produce — including- // the constrained-budget case where prior categories at the same priority- // exhaust the pool and clamp this category's amount.- const allCategoryTemplates = await getTemplates();- allCategoryTemplates[categoryId] = templates;+ // The projection answers "how much do these templates demand" — it+ // skips the priority clamp so future months (where To Budget is empty)+ // still show the templates' intended amount instead of 0.+ const { data: categoryData }: { data: CategoryEntity[] } = await aqlQuery(+ q('categories').filter({ id: categoryId }).select('*'),+ );+ if (categoryData.length === 0) {+ return { budgeted: 0, perTemplate: templates.map(() => 0) };+ }const { contexts } = await computeTemplates(month,true,- allCategoryTemplates,- [],+ { [categoryId]: templates },+ categoryData,+ true,);const ctx = contexts.find(c => c.category.id === categoryId);if (!ctx) return { budgeted: 0, perTemplate: templates.map(() => 0) };diff --git a/upcoming-release-notes/7790.md b/upcoming-release-notes/7790.mdnew file mode 100644index 0000000000..e7ef22f0d0--- /dev/null+++ b/upcoming-release-notes/7790.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [matt-fidd]+---++Automation UI: change dry run logic to only run on a single category