@dizzlkheinz/ynab-mcpb 0.18.3 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (346) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/CLAUDE.md +87 -8
  3. package/bin/ynab-mcp-server.cjs +2 -2
  4. package/bin/ynab-mcp-server.js +3 -3
  5. package/biome.json +39 -0
  6. package/dist/bundle/index.cjs +67 -67
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +27 -27
  9. package/dist/server/YNABMCPServer.d.ts +3 -4
  10. package/dist/server/YNABMCPServer.js +111 -116
  11. package/dist/server/budgetResolver.d.ts +6 -5
  12. package/dist/server/budgetResolver.js +46 -36
  13. package/dist/server/cacheKeys.js +6 -6
  14. package/dist/server/cacheManager.js +14 -11
  15. package/dist/server/completions.d.ts +2 -2
  16. package/dist/server/completions.js +20 -15
  17. package/dist/server/config.d.ts +10 -5
  18. package/dist/server/config.js +24 -7
  19. package/dist/server/deltaCache.d.ts +2 -2
  20. package/dist/server/deltaCache.js +22 -16
  21. package/dist/server/deltaCache.merge.d.ts +2 -2
  22. package/dist/server/diagnostics.d.ts +4 -4
  23. package/dist/server/diagnostics.js +38 -32
  24. package/dist/server/errorHandler.d.ts +5 -12
  25. package/dist/server/errorHandler.js +219 -217
  26. package/dist/server/prompts.d.ts +2 -2
  27. package/dist/server/prompts.js +45 -45
  28. package/dist/server/rateLimiter.js +4 -4
  29. package/dist/server/requestLogger.d.ts +1 -1
  30. package/dist/server/requestLogger.js +40 -35
  31. package/dist/server/resources.d.ts +3 -3
  32. package/dist/server/resources.js +55 -52
  33. package/dist/server/responseFormatter.js +6 -6
  34. package/dist/server/securityMiddleware.d.ts +2 -2
  35. package/dist/server/securityMiddleware.js +22 -20
  36. package/dist/server/serverKnowledgeStore.js +1 -1
  37. package/dist/server/toolRegistry.d.ts +3 -3
  38. package/dist/server/toolRegistry.js +47 -40
  39. package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
  40. package/dist/tools/__tests__/deltaTestUtils.js +2 -2
  41. package/dist/tools/accountTools.d.ts +9 -8
  42. package/dist/tools/accountTools.js +47 -47
  43. package/dist/tools/adapters.d.ts +13 -8
  44. package/dist/tools/adapters.js +21 -11
  45. package/dist/tools/budgetTools.d.ts +8 -7
  46. package/dist/tools/budgetTools.js +22 -22
  47. package/dist/tools/categoryTools.d.ts +9 -8
  48. package/dist/tools/categoryTools.js +68 -59
  49. package/dist/tools/compareTransactions/formatter.d.ts +3 -3
  50. package/dist/tools/compareTransactions/formatter.js +9 -9
  51. package/dist/tools/compareTransactions/index.d.ts +6 -6
  52. package/dist/tools/compareTransactions/index.js +58 -43
  53. package/dist/tools/compareTransactions/matcher.d.ts +1 -1
  54. package/dist/tools/compareTransactions/matcher.js +28 -15
  55. package/dist/tools/compareTransactions/parser.d.ts +2 -2
  56. package/dist/tools/compareTransactions/parser.js +144 -138
  57. package/dist/tools/compareTransactions/types.d.ts +4 -4
  58. package/dist/tools/compareTransactions.d.ts +1 -1
  59. package/dist/tools/compareTransactions.js +1 -1
  60. package/dist/tools/deltaFetcher.d.ts +2 -2
  61. package/dist/tools/deltaFetcher.js +16 -15
  62. package/dist/tools/deltaSupport.d.ts +4 -4
  63. package/dist/tools/deltaSupport.js +35 -41
  64. package/dist/tools/exportTransactions.d.ts +5 -4
  65. package/dist/tools/exportTransactions.js +61 -59
  66. package/dist/tools/monthTools.d.ts +7 -6
  67. package/dist/tools/monthTools.js +31 -29
  68. package/dist/tools/payeeTools.d.ts +7 -6
  69. package/dist/tools/payeeTools.js +28 -28
  70. package/dist/tools/reconcileAdapter.d.ts +2 -2
  71. package/dist/tools/reconcileAdapter.js +21 -11
  72. package/dist/tools/reconciliation/analyzer.d.ts +4 -4
  73. package/dist/tools/reconciliation/analyzer.js +136 -57
  74. package/dist/tools/reconciliation/csvParser.d.ts +3 -3
  75. package/dist/tools/reconciliation/csvParser.js +128 -104
  76. package/dist/tools/reconciliation/executor.d.ts +4 -4
  77. package/dist/tools/reconciliation/executor.js +148 -109
  78. package/dist/tools/reconciliation/index.d.ts +10 -10
  79. package/dist/tools/reconciliation/index.js +96 -83
  80. package/dist/tools/reconciliation/matcher.d.ts +3 -3
  81. package/dist/tools/reconciliation/matcher.js +17 -16
  82. package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
  83. package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
  84. package/dist/tools/reconciliation/recommendationEngine.js +40 -40
  85. package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
  86. package/dist/tools/reconciliation/reportFormatter.js +79 -54
  87. package/dist/tools/reconciliation/signDetector.d.ts +1 -1
  88. package/dist/tools/reconciliation/types.d.ts +19 -16
  89. package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
  90. package/dist/tools/schemas/common.d.ts +1 -1
  91. package/dist/tools/schemas/common.js +1 -1
  92. package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
  93. package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
  94. package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
  95. package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
  96. package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
  97. package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
  98. package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
  99. package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
  100. package/dist/tools/schemas/outputs/index.d.ts +14 -14
  101. package/dist/tools/schemas/outputs/index.js +14 -14
  102. package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
  103. package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
  104. package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
  105. package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
  106. package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
  107. package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
  108. package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
  109. package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
  110. package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
  111. package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
  112. package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
  113. package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
  114. package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
  115. package/dist/tools/schemas/shared/commonOutputs.js +15 -9
  116. package/dist/tools/transactionReadTools.d.ts +11 -0
  117. package/dist/tools/transactionReadTools.js +202 -0
  118. package/dist/tools/transactionSchemas.d.ts +309 -0
  119. package/dist/tools/transactionSchemas.js +235 -0
  120. package/dist/tools/transactionTools.d.ts +6 -302
  121. package/dist/tools/transactionTools.js +7 -2054
  122. package/dist/tools/transactionUtils.d.ts +31 -0
  123. package/dist/tools/transactionUtils.js +364 -0
  124. package/dist/tools/transactionWriteTools.d.ts +20 -0
  125. package/dist/tools/transactionWriteTools.js +1342 -0
  126. package/dist/tools/utilityTools.d.ts +5 -4
  127. package/dist/tools/utilityTools.js +11 -11
  128. package/dist/types/index.d.ts +7 -7
  129. package/dist/types/index.js +6 -6
  130. package/dist/types/reconciliation.d.ts +1 -1
  131. package/dist/types/toolRegistration.d.ts +14 -12
  132. package/dist/utils/amountUtils.js +1 -1
  133. package/dist/utils/dateUtils.js +4 -4
  134. package/dist/utils/errors.d.ts +3 -3
  135. package/dist/utils/errors.js +4 -4
  136. package/dist/utils/money.d.ts +2 -2
  137. package/dist/utils/money.js +8 -8
  138. package/dist/utils/validationError.d.ts +1 -1
  139. package/dist/utils/validationError.js +1 -1
  140. package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
  141. package/docs/assets/schemas/reconciliation-v2.json +360 -336
  142. package/docs/plans/2025-12-25-transaction-tools-refactor-design.md +211 -0
  143. package/docs/plans/2025-12-25-transaction-tools-refactor.md +905 -0
  144. package/esbuild.config.mjs +53 -50
  145. package/meta.json +12548 -12548
  146. package/package.json +98 -109
  147. package/scripts/analyze-bundle.mjs +33 -30
  148. package/scripts/create-pr-description.js +169 -120
  149. package/scripts/run-all-tests.js +205 -0
  150. package/scripts/run-domain-integration-tests.js +28 -18
  151. package/scripts/run-generate-mcpb.js +19 -17
  152. package/scripts/run-throttled-integration-tests.js +92 -83
  153. package/scripts/test-delta-params.mjs +149 -120
  154. package/scripts/test-recommendations.ts +36 -32
  155. package/scripts/tmpTransaction.ts +80 -43
  156. package/scripts/validate-env.js +98 -91
  157. package/scripts/verify-build.js +78 -76
  158. package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
  159. package/src/__tests__/performance.test.ts +723 -671
  160. package/src/__tests__/setup.ts +442 -395
  161. package/src/__tests__/smoke.e2e.test.ts +41 -39
  162. package/src/__tests__/testRunner.ts +314 -295
  163. package/src/__tests__/testUtils.ts +456 -364
  164. package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
  165. package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
  166. package/src/index.ts +68 -59
  167. package/src/server/CLAUDE.md +480 -0
  168. package/src/server/YNABMCPServer.ts +821 -794
  169. package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
  170. package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
  171. package/src/server/__tests__/budgetResolver.test.ts +466 -423
  172. package/src/server/__tests__/cacheManager.test.ts +891 -874
  173. package/src/server/__tests__/completions.integration.test.ts +115 -106
  174. package/src/server/__tests__/completions.test.ts +334 -313
  175. package/src/server/__tests__/config.test.ts +98 -86
  176. package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
  177. package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
  178. package/src/server/__tests__/deltaCache.test.ts +946 -759
  179. package/src/server/__tests__/diagnostics.test.ts +825 -792
  180. package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
  181. package/src/server/__tests__/errorHandler.test.ts +402 -397
  182. package/src/server/__tests__/prompts.test.ts +424 -347
  183. package/src/server/__tests__/rateLimiter.test.ts +313 -309
  184. package/src/server/__tests__/requestLogger.test.ts +443 -403
  185. package/src/server/__tests__/resources.template.test.ts +196 -185
  186. package/src/server/__tests__/resources.test.ts +294 -288
  187. package/src/server/__tests__/security.integration.test.ts +487 -421
  188. package/src/server/__tests__/securityMiddleware.test.ts +519 -444
  189. package/src/server/__tests__/server-startup.integration.test.ts +509 -490
  190. package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
  191. package/src/server/__tests__/toolRegistration.test.ts +239 -210
  192. package/src/server/__tests__/toolRegistry.test.ts +907 -845
  193. package/src/server/budgetResolver.ts +221 -181
  194. package/src/server/cacheKeys.ts +6 -6
  195. package/src/server/cacheManager.ts +498 -484
  196. package/src/server/completions.ts +267 -243
  197. package/src/server/config.ts +35 -14
  198. package/src/server/deltaCache.merge.ts +146 -128
  199. package/src/server/deltaCache.ts +352 -309
  200. package/src/server/diagnostics.ts +257 -242
  201. package/src/server/errorHandler.ts +747 -744
  202. package/src/server/prompts.ts +181 -176
  203. package/src/server/rateLimiter.ts +131 -129
  204. package/src/server/requestLogger.ts +350 -322
  205. package/src/server/resources.ts +442 -374
  206. package/src/server/responseFormatter.ts +41 -37
  207. package/src/server/securityMiddleware.ts +223 -205
  208. package/src/server/serverKnowledgeStore.ts +67 -67
  209. package/src/server/toolRegistry.ts +508 -474
  210. package/src/tools/CLAUDE.md +604 -0
  211. package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
  212. package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
  213. package/src/tools/__tests__/accountTools.test.ts +685 -638
  214. package/src/tools/__tests__/adapters.test.ts +142 -108
  215. package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
  216. package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
  217. package/src/tools/__tests__/budgetTools.test.ts +442 -413
  218. package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
  219. package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
  220. package/src/tools/__tests__/categoryTools.test.ts +656 -625
  221. package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
  222. package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
  223. package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
  224. package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
  225. package/src/tools/__tests__/compareTransactions.test.ts +352 -332
  226. package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
  227. package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
  228. package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
  229. package/src/tools/__tests__/deltaSupport.test.ts +211 -184
  230. package/src/tools/__tests__/deltaTestUtils.ts +37 -33
  231. package/src/tools/__tests__/exportTransactions.test.ts +205 -200
  232. package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
  233. package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
  234. package/src/tools/__tests__/monthTools.test.ts +561 -512
  235. package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
  236. package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
  237. package/src/tools/__tests__/payeeTools.test.ts +486 -434
  238. package/src/tools/__tests__/transactionSchemas.test.ts +1204 -0
  239. package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
  240. package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
  241. package/src/tools/__tests__/transactionUtils.test.ts +1016 -0
  242. package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
  243. package/src/tools/__tests__/utilityTools.test.ts +68 -58
  244. package/src/tools/accountTools.ts +293 -271
  245. package/src/tools/adapters.ts +120 -63
  246. package/src/tools/budgetTools.ts +121 -116
  247. package/src/tools/categoryTools.ts +379 -339
  248. package/src/tools/compareTransactions/formatter.ts +131 -119
  249. package/src/tools/compareTransactions/index.ts +249 -214
  250. package/src/tools/compareTransactions/matcher.ts +259 -209
  251. package/src/tools/compareTransactions/parser.ts +517 -487
  252. package/src/tools/compareTransactions/types.ts +38 -38
  253. package/src/tools/compareTransactions.ts +1 -1
  254. package/src/tools/deltaFetcher.ts +281 -260
  255. package/src/tools/deltaSupport.ts +264 -259
  256. package/src/tools/exportTransactions.ts +230 -218
  257. package/src/tools/monthTools.ts +180 -165
  258. package/src/tools/payeeTools.ts +152 -140
  259. package/src/tools/reconcileAdapter.ts +297 -246
  260. package/src/tools/reconciliation/CLAUDE.md +506 -0
  261. package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +135 -112
  262. package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -227
  263. package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -335
  264. package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
  265. package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
  266. package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
  267. package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
  268. package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
  269. package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
  270. package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
  271. package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -986
  272. package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
  273. package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -530
  274. package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -71
  275. package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -58
  276. package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
  277. package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +58 -43
  278. package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
  279. package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
  280. package/src/tools/reconciliation/analyzer.ts +582 -406
  281. package/src/tools/reconciliation/csvParser.ts +656 -609
  282. package/src/tools/reconciliation/executor.ts +1290 -1128
  283. package/src/tools/reconciliation/index.ts +580 -528
  284. package/src/tools/reconciliation/matcher.ts +256 -240
  285. package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
  286. package/src/tools/reconciliation/recommendationEngine.ts +357 -345
  287. package/src/tools/reconciliation/reportFormatter.ts +349 -276
  288. package/src/tools/reconciliation/signDetector.ts +89 -83
  289. package/src/tools/reconciliation/types.ts +164 -153
  290. package/src/tools/reconciliation/ynabAdapter.ts +17 -15
  291. package/src/tools/schemas/CLAUDE.md +546 -0
  292. package/src/tools/schemas/common.ts +1 -1
  293. package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
  294. package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
  295. package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
  296. package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
  297. package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
  298. package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
  299. package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
  300. package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
  301. package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
  302. package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
  303. package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
  304. package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
  305. package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
  306. package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
  307. package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
  308. package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
  309. package/src/tools/schemas/outputs/index.ts +163 -163
  310. package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
  311. package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
  312. package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
  313. package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
  314. package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
  315. package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
  316. package/src/tools/schemas/shared/commonOutputs.ts +27 -19
  317. package/src/tools/toolCategories.ts +114 -114
  318. package/src/tools/transactionReadTools.ts +327 -0
  319. package/src/tools/transactionSchemas.ts +484 -0
  320. package/src/tools/transactionTools.ts +107 -2990
  321. package/src/tools/transactionUtils.ts +621 -0
  322. package/src/tools/transactionWriteTools.ts +2110 -0
  323. package/src/tools/utilityTools.ts +46 -41
  324. package/src/types/CLAUDE.md +477 -0
  325. package/src/types/__tests__/index.test.ts +51 -51
  326. package/src/types/index.ts +43 -39
  327. package/src/types/integration-tests.d.ts +26 -26
  328. package/src/types/reconciliation.ts +29 -29
  329. package/src/types/toolAnnotations.ts +30 -30
  330. package/src/types/toolRegistration.ts +43 -32
  331. package/src/utils/CLAUDE.md +508 -0
  332. package/src/utils/__tests__/dateUtils.test.ts +174 -168
  333. package/src/utils/__tests__/money.test.ts +193 -187
  334. package/src/utils/amountUtils.ts +5 -5
  335. package/src/utils/baseError.ts +5 -5
  336. package/src/utils/dateUtils.ts +29 -26
  337. package/src/utils/errors.ts +14 -14
  338. package/src/utils/money.ts +66 -52
  339. package/src/utils/validationError.ts +1 -1
  340. package/tsconfig.json +29 -29
  341. package/tsconfig.prod.json +16 -16
  342. package/vitest-reporters/split-json-reporter.ts +247 -204
  343. package/vitest.config.ts +99 -95
  344. package/.prettierignore +0 -10
  345. package/.prettierrc.json +0 -10
  346. package/eslint.config.js +0 -49
@@ -1,56 +1,60 @@
1
- import type * as ynab from 'ynab';
2
- import type { SaveTransaction } from 'ynab/dist/models/SaveTransaction.js';
3
- import { YNABAPIError, YNABErrorCode } from '../../server/errorHandler.js';
4
- import type { ProgressCallback } from '../../server/toolRegistry.js';
5
- import { toMilli, toMoneyValue, addMilli } from '../../utils/money.js';
6
- import type { ReconciliationAnalysis, TransactionMatch, BankTransaction } from './types.js';
7
- import type { ReconcileAccountRequest } from './index.js';
1
+ import type * as ynab from "ynab";
2
+ import type { SaveTransaction } from "ynab/dist/models/SaveTransaction.js";
3
+ import { YNABAPIError, YNABErrorCode } from "../../server/errorHandler.js";
4
+ import type { ProgressCallback } from "../../server/toolRegistry.js";
5
+ import { addMilli, toMilli, toMoneyValue } from "../../utils/money.js";
8
6
  import {
9
- generateCorrelationKey,
10
- correlateResults,
11
- toCorrelationPayload,
12
- } from '../transactionTools.js';
7
+ correlateResults,
8
+ generateCorrelationKey,
9
+ toCorrelationPayload,
10
+ } from "../transactionTools.js";
11
+ import type { ReconcileAccountRequest } from "./index.js";
12
+ import type {
13
+ BankTransaction,
14
+ ReconciliationAnalysis,
15
+ TransactionMatch,
16
+ } from "./types.js";
13
17
 
14
18
  export interface AccountSnapshot {
15
- balance: number; // milliunits
16
- cleared_balance: number; // milliunits
17
- uncleared_balance: number; // milliunits
19
+ balance: number; // milliunits
20
+ cleared_balance: number; // milliunits
21
+ uncleared_balance: number; // milliunits
18
22
  }
19
23
 
20
24
  export interface ExecutionOptions {
21
- ynabAPI: ynab.API;
22
- analysis: ReconciliationAnalysis;
23
- params: ReconcileAccountRequest;
24
- budgetId: string;
25
- accountId: string;
26
- initialAccount: AccountSnapshot;
27
- currencyCode: string;
28
- /**
29
- * Optional progress callback for emitting MCP progress notifications.
30
- * When provided, progress updates are sent during bulk operations.
31
- */
32
- sendProgress?: ProgressCallback;
25
+ ynabAPI: ynab.API;
26
+ analysis: ReconciliationAnalysis;
27
+ params: ReconcileAccountRequest;
28
+ budgetId: string;
29
+ accountId: string;
30
+ initialAccount: AccountSnapshot;
31
+ currencyCode: string;
32
+ /**
33
+ * Optional progress callback for emitting MCP progress notifications.
34
+ * When provided, progress updates are sent during bulk operations.
35
+ */
36
+ sendProgress?: ProgressCallback;
33
37
  }
34
38
 
35
39
  export interface ExecutionActionRecord {
36
- type: string;
37
- transaction: Record<string, unknown> | null;
38
- reason: string;
39
- bulk_chunk_index?: number;
40
- correlation_key?: string;
41
- duplicate?: boolean;
40
+ type: string;
41
+ transaction: Record<string, unknown> | null;
42
+ reason: string;
43
+ bulk_chunk_index?: number;
44
+ correlation_key?: string;
45
+ duplicate?: boolean;
42
46
  }
43
47
 
44
48
  export interface ExecutionSummary {
45
- bank_transactions_count: number;
46
- ynab_transactions_count: number;
47
- matches_found: number;
48
- missing_in_ynab: number;
49
- missing_in_bank: number;
50
- transactions_created: number;
51
- transactions_updated: number;
52
- dates_adjusted: number;
53
- dry_run: boolean;
49
+ bank_transactions_count: number;
50
+ ynab_transactions_count: number;
51
+ matches_found: number;
52
+ missing_in_ynab: number;
53
+ missing_in_bank: number;
54
+ transactions_created: number;
55
+ transactions_updated: number;
56
+ dates_adjusted: number;
57
+ dry_run: boolean;
54
58
  }
55
59
 
56
60
  /**
@@ -62,31 +66,33 @@ export interface ExecutionSummary {
62
66
  * mirror `transaction_failures` rather than represent an independent count
63
67
  */
64
68
  export interface BulkOperationDetails {
65
- chunks_processed: number;
66
- bulk_successes: number;
67
- sequential_fallbacks: number;
68
- duplicates_detected: number;
69
- failed_transactions: number; // Backward-compatible alias for transaction_failures
70
- bulk_chunk_failures: number; // API-level failures (entire chunk failed)
71
- transaction_failures: number; // Per-transaction failures (from correlation or sequential)
72
- sequential_attempts?: number; // Number of sequential creations attempted during fallback
69
+ chunks_processed: number;
70
+ bulk_successes: number;
71
+ sequential_fallbacks: number;
72
+ duplicates_detected: number;
73
+ failed_transactions: number; // Backward-compatible alias for transaction_failures
74
+ bulk_chunk_failures: number; // API-level failures (entire chunk failed)
75
+ transaction_failures: number; // Per-transaction failures (from correlation or sequential)
76
+ sequential_attempts?: number; // Number of sequential creations attempted during fallback
73
77
  }
74
78
 
75
79
  export interface ExecutionResult {
76
- summary: ExecutionSummary;
77
- account_balance: {
78
- before: AccountSnapshot;
79
- after: AccountSnapshot;
80
- };
81
- actions_taken: ExecutionActionRecord[];
82
- recommendations: string[];
83
- balance_reconciliation?: Awaited<ReturnType<typeof buildBalanceReconciliation>>;
84
- bulk_operation_details?: BulkOperationDetails;
80
+ summary: ExecutionSummary;
81
+ account_balance: {
82
+ before: AccountSnapshot;
83
+ after: AccountSnapshot;
84
+ };
85
+ actions_taken: ExecutionActionRecord[];
86
+ recommendations: string[];
87
+ balance_reconciliation?: Awaited<
88
+ ReturnType<typeof buildBalanceReconciliation>
89
+ >;
90
+ bulk_operation_details?: BulkOperationDetails;
85
91
  }
86
92
 
87
93
  interface UpdateFlags {
88
- needsClearedUpdate: boolean;
89
- needsDateUpdate: boolean;
94
+ needsClearedUpdate: boolean;
95
+ needsDateUpdate: boolean;
90
96
  }
91
97
 
92
98
  const MONEY_EPSILON_MILLI = 100; // $0.10
@@ -98,1155 +104,1311 @@ const BATCH_DELAY_MS = 200; // Delay between batch chunks to avoid rate limiting
98
104
  const MAX_MEMO_LENGTH = 500; // YNAB's maximum memo length
99
105
 
100
106
  function chunkArray<T>(array: T[], size: number): T[][] {
101
- if (size <= 0) {
102
- throw new Error('chunk size must be positive');
103
- }
104
- const chunks: T[][] = [];
105
- for (let i = 0; i < array.length; i += size) {
106
- chunks.push(array.slice(i, i + size));
107
- }
108
- return chunks;
107
+ if (size <= 0) {
108
+ throw new Error("chunk size must be positive");
109
+ }
110
+ const chunks: T[][] = [];
111
+ for (let i = 0; i < array.length; i += size) {
112
+ chunks.push(array.slice(i, i + size));
113
+ }
114
+ return chunks;
109
115
  }
110
116
 
111
117
  function sleep(ms: number): Promise<void> {
112
- return new Promise((resolve) => setTimeout(resolve, ms));
118
+ return new Promise((resolve) => setTimeout(resolve, ms));
113
119
  }
114
120
 
115
121
  function truncateMemo(memo: string | null | undefined): string {
116
- if (!memo) return 'Auto-reconciled from bank statement';
117
- if (memo.length <= MAX_MEMO_LENGTH) return memo;
118
- return memo.substring(0, MAX_MEMO_LENGTH - 3) + '...';
122
+ if (!memo) return "Auto-reconciled from bank statement";
123
+ if (memo.length <= MAX_MEMO_LENGTH) return memo;
124
+ return `${memo.substring(0, MAX_MEMO_LENGTH - 3)}...`;
119
125
  }
120
126
 
121
127
  interface StatementWindow {
122
- start?: Date;
123
- end?: Date;
128
+ start?: Date;
129
+ end?: Date;
124
130
  }
125
131
 
126
132
  interface PreparedBulkCreateEntry {
127
- bankTransaction: BankTransaction;
128
- saveTransaction: SaveTransaction;
129
- amountMilli: number;
130
- correlationKey: string;
133
+ bankTransaction: BankTransaction;
134
+ saveTransaction: SaveTransaction;
135
+ amountMilli: number;
136
+ correlationKey: string;
131
137
  }
132
138
 
133
139
  function parseISODate(dateStr: string | undefined): Date | undefined {
134
- if (!dateStr) return undefined;
135
- const d = new Date(dateStr);
136
- return Number.isNaN(d.getTime()) ? undefined : d;
140
+ if (!dateStr) return undefined;
141
+ const d = new Date(dateStr);
142
+ return Number.isNaN(d.getTime()) ? undefined : d;
137
143
  }
138
144
 
139
145
  function resolveStatementWindow(
140
- params: ReconcileAccountRequest,
141
- analysisDateRange?: string | undefined,
146
+ params: ReconcileAccountRequest,
147
+ analysisDateRange?: string | undefined,
142
148
  ): StatementWindow | undefined {
143
- const start = parseISODate(params.statement_start_date);
144
- const end =
145
- parseISODate(params.statement_end_date ?? params.statement_date) ??
146
- // If only start provided, end stays undefined
147
- undefined;
148
-
149
- if (start || end) {
150
- const window: StatementWindow = {};
151
- if (start) window.start = start;
152
- if (end) window.end = end;
153
- return window;
154
- }
155
-
156
- if (analysisDateRange && analysisDateRange.includes(' to ')) {
157
- const [rawStart, rawEnd] = analysisDateRange.split(' to ').map((part) => part.trim());
158
- const parsedStart = parseISODate(rawStart);
159
- const parsedEnd = parseISODate(rawEnd);
160
- if (parsedStart || parsedEnd) {
161
- const window: StatementWindow = {};
162
- if (parsedStart) window.start = parsedStart;
163
- if (parsedEnd) window.end = parsedEnd;
164
- return window;
165
- }
166
- }
167
-
168
- return undefined;
149
+ const start = parseISODate(params.statement_start_date);
150
+ const end =
151
+ parseISODate(params.statement_end_date ?? params.statement_date) ??
152
+ // If only start provided, end stays undefined
153
+ undefined;
154
+
155
+ if (start || end) {
156
+ const window: StatementWindow = {};
157
+ if (start) window.start = start;
158
+ if (end) window.end = end;
159
+ return window;
160
+ }
161
+
162
+ if (analysisDateRange?.includes(" to ")) {
163
+ const [rawStart, rawEnd] = analysisDateRange
164
+ .split(" to ")
165
+ .map((part) => part.trim());
166
+ const parsedStart = parseISODate(rawStart);
167
+ const parsedEnd = parseISODate(rawEnd);
168
+ if (parsedStart || parsedEnd) {
169
+ const window: StatementWindow = {};
170
+ if (parsedStart) window.start = parsedStart;
171
+ if (parsedEnd) window.end = parsedEnd;
172
+ return window;
173
+ }
174
+ }
175
+
176
+ return undefined;
169
177
  }
170
178
 
171
- function isWithinStatementWindow(dateStr: string, window: StatementWindow): boolean {
172
- const date = parseISODate(dateStr);
173
- if (!date) return false;
179
+ function isWithinStatementWindow(
180
+ dateStr: string,
181
+ window: StatementWindow,
182
+ ): boolean {
183
+ const date = parseISODate(dateStr);
184
+ if (!date) return false;
174
185
 
175
- if (window.start && date < window.start) return false;
176
- if (window.end && date > window.end) return false;
177
- return true;
186
+ if (window.start && date < window.start) return false;
187
+ if (window.end && date > window.end) return false;
188
+ return true;
178
189
  }
179
190
 
180
- export async function executeReconciliation(options: ExecutionOptions): Promise<ExecutionResult> {
181
- const {
182
- analysis,
183
- params,
184
- ynabAPI,
185
- budgetId,
186
- accountId,
187
- initialAccount,
188
- currencyCode,
189
- sendProgress,
190
- } = options;
191
- const actions_taken: ExecutionActionRecord[] = [];
192
-
193
- const summary: ExecutionSummary = {
194
- bank_transactions_count: analysis.summary.bank_transactions_count,
195
- ynab_transactions_count: analysis.summary.ynab_transactions_count,
196
- matches_found: analysis.auto_matches.length,
197
- missing_in_ynab: analysis.summary.unmatched_bank,
198
- missing_in_bank: analysis.summary.unmatched_ynab,
199
- transactions_created: 0,
200
- transactions_updated: 0,
201
- dates_adjusted: 0,
202
- dry_run: params.dry_run,
203
- };
204
-
205
- // Progress tracking for MCP notifications
206
- // Pre-filter matches to only count those that will actually be updated
207
- // This ensures accurate progress percentages (skipped matches don't inflate total)
208
- const matchesNeedingUpdate = analysis.auto_matches.filter((match) => {
209
- const flags = computeUpdateFlags(match, params);
210
- return flags.needsClearedUpdate || flags.needsDateUpdate;
211
- });
212
- const totalOperations =
213
- (params.auto_create_transactions ? analysis.unmatched_bank.length : 0) +
214
- matchesNeedingUpdate.length +
215
- (params.auto_unclear_missing ? analysis.unmatched_ynab.length : 0);
216
- let completedOperations = 0;
217
-
218
- const reportProgress = async (message: string): Promise<void> => {
219
- if (sendProgress && totalOperations > 0) {
220
- await sendProgress({
221
- progress: completedOperations,
222
- total: totalOperations,
223
- message,
224
- });
225
- }
226
- };
227
-
228
- let afterAccount: AccountSnapshot = { ...initialAccount };
229
- let accountSnapshotDirty = false;
230
- const statementTargetMilli = resolveStatementBalanceMilli(
231
- analysis.balance_info,
232
- params.statement_balance,
233
- );
234
- let clearedDeltaMilli = addMilli(initialAccount.cleared_balance ?? 0, -statementTargetMilli);
235
- const balanceToleranceMilli =
236
- Math.max(0, params.amount_tolerance_cents ?? DEFAULT_TOLERANCE_CENTS) * CENTS_TO_MILLI;
237
- let balanceAligned = false;
238
-
239
- const applyClearedDelta = (delta: number) => {
240
- if (delta === 0) return;
241
- clearedDeltaMilli = addMilli(clearedDeltaMilli, delta);
242
- };
243
-
244
- const recordAlignmentIfNeeded = (trigger: string, { log = true } = {}) => {
245
- if (balanceAligned) {
246
- return true;
247
- }
248
- if (Math.abs(clearedDeltaMilli) <= balanceToleranceMilli) {
249
- balanceAligned = true;
250
- if (log) {
251
- const deltaDisplay = toMoneyValue(clearedDeltaMilli, currencyCode).value_display;
252
- const toleranceDisplay = toMoneyValue(balanceToleranceMilli, currencyCode).value_display;
253
- actions_taken.push({
254
- type: 'balance_checkpoint',
255
- transaction: null,
256
- reason: `Cleared delta ${deltaDisplay} within ±${toleranceDisplay} after ${trigger} - halting newest-to-oldest pass`,
257
- });
258
- }
259
- return true;
260
- }
261
- return false;
262
- };
263
-
264
- recordAlignmentIfNeeded('initial balance check', { log: false });
265
-
266
- const orderedUnmatchedBank = params.auto_create_transactions
267
- ? sortByDateDescending(analysis.unmatched_bank)
268
- : [];
269
- const orderedAutoMatches = sortMatchesByBankDateDescending(analysis.auto_matches);
270
- const statementWindow = resolveStatementWindow(params, analysis.summary.statement_date_range);
271
- const orderedUnmatchedYNAB = sortByDateDescending(
272
- statementWindow
273
- ? analysis.unmatched_ynab.filter((txn) => isWithinStatementWindow(txn.date, statementWindow))
274
- : analysis.unmatched_ynab,
275
- );
276
-
277
- let bulkOperationDetails: BulkOperationDetails | undefined;
278
-
279
- // STEP 1: Auto-create missing transactions (bank -> YNAB)
280
- if (params.auto_create_transactions && !balanceAligned) {
281
- const buildPreparedEntry = (bankTxn: BankTransaction): PreparedBulkCreateEntry => {
282
- const amountMilli = bankTxn.amount;
283
- const saveTransaction: SaveTransaction = {
284
- account_id: accountId,
285
- amount: amountMilli,
286
- date: bankTxn.date,
287
- payee_name: bankTxn.payee ?? undefined,
288
- memo: truncateMemo(bankTxn.memo),
289
- cleared: 'cleared',
290
- approved: true,
291
- // Note: import_id intentionally omitted so transactions can match with bank imports
292
- };
293
- const correlationKey = generateCorrelationKey(toCorrelationPayload(saveTransaction));
294
- return {
295
- bankTransaction: bankTxn,
296
- saveTransaction,
297
- amountMilli,
298
- correlationKey,
299
- };
300
- };
301
-
302
- const recordCreateAction = (args: {
303
- entry: PreparedBulkCreateEntry;
304
- createdTxn: ynab.TransactionDetail | null;
305
- chunkIndex?: number;
306
- prefix?: string;
307
- }) => {
308
- const { entry, createdTxn, chunkIndex, prefix } = args;
309
- summary.transactions_created += 1;
310
- const action: ExecutionActionRecord = {
311
- type: 'create_transaction',
312
- transaction: createdTxn as unknown as Record<string, unknown> | null,
313
- reason: `${prefix ?? 'Created missing transaction'}: ${
314
- entry.bankTransaction.payee ?? 'Unknown'
315
- } (${formatDisplay(entry.bankTransaction.amount, currencyCode)})`,
316
- correlation_key: entry.correlationKey,
317
- };
318
- if (chunkIndex !== undefined) {
319
- action.bulk_chunk_index = chunkIndex;
320
- }
321
- actions_taken.push(action);
322
- };
323
-
324
- const processSequentialEntries = async (
325
- entries: PreparedBulkCreateEntry[],
326
- options: { chunkIndex?: number; fallbackError?: unknown } = {},
327
- ) => {
328
- let sequentialAttempts = 0;
329
- for (const entry of entries) {
330
- if (balanceAligned) break;
331
- if (options.fallbackError) {
332
- sequentialAttempts += 1;
333
- }
334
- try {
335
- const response = await ynabAPI.transactions.createTransaction(budgetId, {
336
- transaction: entry.saveTransaction,
337
- });
338
- const createdTransaction = response.data.transaction ?? null;
339
- const recordArgs: Parameters<typeof recordCreateAction>[0] = {
340
- entry,
341
- createdTxn: createdTransaction,
342
- prefix: options.fallbackError
343
- ? 'Created missing transaction after bulk fallback'
344
- : 'Created missing transaction',
345
- };
346
- if (options.chunkIndex !== undefined) {
347
- recordArgs.chunkIndex = options.chunkIndex;
348
- }
349
- recordCreateAction(recordArgs);
350
- accountSnapshotDirty = true;
351
- applyClearedDelta(entry.amountMilli);
352
- // Report progress for sequential/fallback operations
353
- completedOperations += 1;
354
- await reportProgress(`Created ${completedOperations} of ${totalOperations} transactions`);
355
- const trigger = options.chunkIndex
356
- ? `creating ${entry.bankTransaction.payee ?? 'missing transaction'} (chunk ${options.chunkIndex})`
357
- : `creating ${entry.bankTransaction.payee ?? 'missing transaction'}`;
358
- recordAlignmentIfNeeded(trigger);
359
- } catch (error) {
360
- const ynabError = normalizeYnabError(error);
361
- if (bulkOperationDetails) {
362
- bulkOperationDetails.transaction_failures += 1; // Canonical counter for per-transaction failures
363
- }
364
- const failureReason = ynabError.message || 'Unknown error occurred';
365
- const statusSuffix = ynabError.status ? ` (HTTP ${ynabError.status})` : '';
366
- const failureAction: ExecutionActionRecord = {
367
- type: 'create_transaction_failed',
368
- transaction: entry.saveTransaction as unknown as Record<string, unknown>,
369
- reason: options.fallbackError
370
- ? `Bulk fallback failed for ${entry.bankTransaction.payee ?? 'Unknown'} (${failureReason}${statusSuffix})`
371
- : `Failed to create transaction ${entry.bankTransaction.payee ?? 'Unknown'} (${failureReason}${statusSuffix})`,
372
- correlation_key: entry.correlationKey,
373
- };
374
- if (options.chunkIndex !== undefined) {
375
- failureAction.bulk_chunk_index = options.chunkIndex;
376
- }
377
- actions_taken.push(failureAction);
378
-
379
- if (shouldPropagateYnabError(ynabError)) {
380
- throw attachStatusToError(ynabError, error);
381
- }
382
- }
383
- }
384
- // Update sequential_attempts metric if this was a fallback operation
385
- if (bulkOperationDetails && options.fallbackError && sequentialAttempts > 0) {
386
- bulkOperationDetails.sequential_attempts =
387
- (bulkOperationDetails.sequential_attempts ?? 0) + sequentialAttempts;
388
- }
389
- };
390
-
391
- const processBulkChunk = async (chunk: PreparedBulkCreateEntry[], chunkIndex: number) => {
392
- // bulkOperationDetails is guaranteed to be defined when this function is called
393
- // (it's only called from within the bulk operation block where it's initialized)
394
- const bulkDetails = bulkOperationDetails!;
395
-
396
- const payload = chunk.map((entry) => entry.saveTransaction);
397
- const response = await ynabAPI.transactions.createTransactions(budgetId, {
398
- transactions: payload,
399
- });
400
- const responseData = response.data;
401
- const duplicateImportIds = new Set(responseData.duplicate_import_ids ?? []);
402
- const correlationRequests = chunk.map((entry) =>
403
- toCorrelationPayload(entry.saveTransaction),
404
- ) as Parameters<typeof correlateResults>[0];
405
- const correlated = correlateResults(correlationRequests, responseData, duplicateImportIds);
406
- const transactionMap = new Map<string, ynab.TransactionDetail>();
407
- for (const transaction of responseData.transactions ?? []) {
408
- if (transaction.id) {
409
- transactionMap.set(transaction.id, transaction);
410
- }
411
- }
412
- for (const result of correlated) {
413
- const entry = chunk[result.request_index];
414
- if (!entry) continue;
415
- if (result.status === 'created') {
416
- const createdTransaction = result.transaction_id
417
- ? (transactionMap.get(result.transaction_id) ?? null)
418
- : null;
419
- recordCreateAction({
420
- entry,
421
- createdTxn: createdTransaction,
422
- chunkIndex,
423
- prefix: 'Created missing transaction via bulk',
424
- });
425
- accountSnapshotDirty = true;
426
- applyClearedDelta(entry.amountMilli);
427
- recordAlignmentIfNeeded(
428
- `creating ${entry.bankTransaction.payee ?? 'missing transaction'} via bulk chunk ${chunkIndex}`,
429
- );
430
- } else if (result.status === 'duplicate') {
431
- bulkDetails.duplicates_detected += 1;
432
- actions_taken.push({
433
- type: 'create_transaction_duplicate',
434
- transaction: {
435
- transaction_id: result.transaction_id ?? null,
436
- import_id: entry.saveTransaction.import_id,
437
- },
438
- reason: `Duplicate import detected for ${
439
- entry.bankTransaction.payee ?? 'Unknown'
440
- } (import_id ${entry.saveTransaction.import_id})`,
441
- bulk_chunk_index: chunkIndex,
442
- correlation_key: result.correlation_key,
443
- duplicate: true,
444
- });
445
- } else {
446
- bulkDetails.transaction_failures += 1; // Canonical counter for per-transaction failures
447
- actions_taken.push({
448
- type: 'create_transaction_failed',
449
- transaction: entry.saveTransaction as unknown as Record<string, unknown>,
450
- reason:
451
- result.error ?? `Bulk create failed for ${entry.bankTransaction.payee ?? 'Unknown'}`,
452
- bulk_chunk_index: chunkIndex,
453
- correlation_key: result.correlation_key,
454
- });
455
- }
456
- }
457
- };
458
-
459
- if (params.dry_run) {
460
- for (const bankTxn of orderedUnmatchedBank) {
461
- if (balanceAligned) break;
462
- const entry = buildPreparedEntry(bankTxn);
463
- summary.transactions_created += 1;
464
- actions_taken.push({
465
- type: 'create_transaction',
466
- transaction: entry.saveTransaction as unknown as Record<string, unknown>,
467
- reason: `Would create missing transaction: ${bankTxn.payee ?? 'Unknown'} (${formatDisplay(bankTxn.amount, currencyCode)})`,
468
- correlation_key: entry.correlationKey,
469
- });
470
- applyClearedDelta(entry.amountMilli);
471
- recordAlignmentIfNeeded(`creating ${bankTxn.payee ?? 'missing transaction'}`);
472
- }
473
- } else if (orderedUnmatchedBank.length >= 2) {
474
- bulkOperationDetails = {
475
- chunks_processed: 0,
476
- bulk_successes: 0,
477
- sequential_fallbacks: 0,
478
- duplicates_detected: 0,
479
- failed_transactions: 0,
480
- bulk_chunk_failures: 0,
481
- transaction_failures: 0,
482
- };
483
-
484
- let nextBankIndex = 0;
485
- while (nextBankIndex < orderedUnmatchedBank.length && !balanceAligned) {
486
- const batch: PreparedBulkCreateEntry[] = [];
487
- let projectedDelta = clearedDeltaMilli;
488
- while (nextBankIndex < orderedUnmatchedBank.length) {
489
- const bankTxn = orderedUnmatchedBank[nextBankIndex];
490
- if (!bankTxn) {
491
- nextBankIndex += 1;
492
- continue;
493
- }
494
- const entry = buildPreparedEntry(bankTxn);
495
- batch.push(entry);
496
- nextBankIndex += 1;
497
- projectedDelta = addMilli(projectedDelta, entry.amountMilli);
498
- if (Math.abs(projectedDelta) <= balanceToleranceMilli) {
499
- break;
500
- }
501
- }
502
-
503
- if (batch.length === 0) {
504
- break;
505
- }
506
-
507
- const chunks = chunkArray(batch, MAX_BULK_CREATE_CHUNK);
508
- for (const chunk of chunks) {
509
- if (balanceAligned) break;
510
- bulkOperationDetails.chunks_processed += 1;
511
- const chunkIndex = bulkOperationDetails.chunks_processed;
512
- try {
513
- await processBulkChunk(chunk, chunkIndex);
514
- bulkOperationDetails.bulk_successes += 1;
515
- // Report progress after successful chunk processing
516
- completedOperations += chunk.length;
517
- await reportProgress(
518
- `Created ${completedOperations} of ${totalOperations} transactions`,
519
- );
520
- } catch (error) {
521
- const ynabError = normalizeYnabError(error);
522
- const failureReason = ynabError.message || 'unknown error';
523
- bulkOperationDetails.bulk_chunk_failures += 1; // API-level failure (entire chunk failed)
524
-
525
- if (shouldPropagateYnabError(ynabError)) {
526
- bulkOperationDetails.transaction_failures += chunk.length;
527
- throw attachStatusToError(ynabError, error);
528
- }
529
-
530
- bulkOperationDetails.sequential_fallbacks += 1;
531
- actions_taken.push({
532
- type: 'bulk_create_fallback',
533
- transaction: null,
534
- reason: `Bulk chunk #${chunkIndex} failed (${failureReason}${
535
- ynabError.status ? ` (HTTP ${ynabError.status})` : ''
536
- }) - falling back to sequential creation`,
537
- bulk_chunk_index: chunkIndex,
538
- });
539
- await processSequentialEntries(chunk, { chunkIndex, fallbackError: ynabError });
540
- }
541
- }
542
- }
543
- } else {
544
- const entries = orderedUnmatchedBank.map((bankTxn) => buildPreparedEntry(bankTxn));
545
- await processSequentialEntries(entries);
546
- }
547
- }
548
-
549
- // STEP 2: Update matched YNAB transactions (cleared status / date)
550
- // Collect all updates for batch processing
551
- if (!balanceAligned) {
552
- const transactionsToUpdate: ynab.SaveTransactionWithIdOrImportId[] = [];
553
-
554
- for (const match of orderedAutoMatches) {
555
- if (balanceAligned) break;
556
- const flags = computeUpdateFlags(match, params);
557
- if (!flags.needsClearedUpdate && !flags.needsDateUpdate) continue;
558
- if (!match.ynabTransaction) continue;
559
-
560
- // Build minimal update payload - only include ID and fields that are changing
561
- // Including unnecessary fields (like amount, payee_name) can cause unexpected behavior
562
- // BUT we must include memo to fix existing memos that exceed YNAB's 500 char limit
563
- const updatePayload: ynab.SaveTransactionWithIdOrImportId = {
564
- id: match.ynabTransaction.id,
565
- };
566
-
567
- // Truncate memo if it exists and is too long (YNAB validates on update even if not changed)
568
- if (match.ynabTransaction.memo) {
569
- updatePayload.memo = truncateMemo(match.ynabTransaction.memo);
570
- }
571
-
572
- // Only include fields that are actually changing
573
- if (flags.needsDateUpdate) {
574
- updatePayload.date = match.bankTransaction.date;
575
- }
576
- if (flags.needsClearedUpdate) {
577
- updatePayload.cleared = 'cleared' as ynab.TransactionClearedStatus;
578
- }
579
-
580
- if (params.dry_run) {
581
- summary.transactions_updated += 1;
582
- if (flags.needsDateUpdate) summary.dates_adjusted += 1;
583
- actions_taken.push({
584
- type: 'update_transaction',
585
- transaction: {
586
- transaction_id: match.ynabTransaction.id,
587
- new_date: flags.needsDateUpdate ? match.bankTransaction.date : undefined,
588
- cleared: flags.needsClearedUpdate ? 'cleared' : undefined,
589
- },
590
- reason: `Would update transaction: ${updateReason(match, flags, currencyCode)}`,
591
- });
592
- if (flags.needsClearedUpdate) {
593
- applyClearedDelta(match.ynabTransaction.amount);
594
- if (
595
- recordAlignmentIfNeeded(
596
- `clearing ${match.ynabTransaction.id ?? 'transaction'} (dry run)`,
597
- )
598
- ) {
599
- break;
600
- }
601
- }
602
- } else {
603
- transactionsToUpdate.push(updatePayload);
604
- if (flags.needsDateUpdate) summary.dates_adjusted += 1;
605
- if (flags.needsClearedUpdate) {
606
- applyClearedDelta(match.ynabTransaction.amount);
607
- if (recordAlignmentIfNeeded(`clearing ${match.ynabTransaction.id}`)) {
608
- break;
609
- }
610
- }
611
- }
612
- }
613
-
614
- // Batch update all transactions in a single API call
615
- // YNAB API has a limit of ~100 transactions per batch, so we chunk the updates
616
- if (!params.dry_run && transactionsToUpdate.length > 0) {
617
- const updateChunks = chunkArray(transactionsToUpdate, MAX_BULK_UPDATE_CHUNK);
618
-
619
- for (let chunkIdx = 0; chunkIdx < updateChunks.length; chunkIdx++) {
620
- const chunk = updateChunks[chunkIdx]!;
621
- try {
622
- const response = await ynabAPI.transactions.updateTransactions(budgetId, {
623
- transactions: chunk,
624
- });
625
-
626
- const updatedTransactions = response.data.transactions ?? [];
627
- summary.transactions_updated += updatedTransactions.length;
628
-
629
- for (const updatedTransaction of updatedTransactions) {
630
- const match = orderedAutoMatches.find(
631
- (m) => m.ynabTransaction?.id === updatedTransaction.id,
632
- );
633
- const flags = match
634
- ? computeUpdateFlags(match, params)
635
- : { needsClearedUpdate: false, needsDateUpdate: false };
636
- actions_taken.push({
637
- type: 'update_transaction',
638
- transaction: updatedTransaction as unknown as Record<string, unknown> | null,
639
- reason: `Updated transaction: ${match ? updateReason(match, flags, currencyCode) : 'cleared'}`,
640
- });
641
- }
642
- accountSnapshotDirty = true;
643
- // Report progress after successful batch update
644
- completedOperations += updatedTransactions.length;
645
- await reportProgress(`Updated ${completedOperations} of ${totalOperations} transactions`);
646
- } catch (error) {
647
- const ynabError = normalizeYnabError(error);
648
- const failureReason = ynabError.message || 'Unknown error occurred';
649
- const statusSuffix = ynabError.status ? ` (HTTP ${ynabError.status})` : '';
650
- actions_taken.push({
651
- type: 'batch_update_failed',
652
- transaction: null,
653
- reason: `Failed to update chunk ${chunkIdx + 1}/${updateChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
654
- });
655
-
656
- if (shouldPropagateYnabError(ynabError)) {
657
- throw attachStatusToError(ynabError, error);
658
- }
659
- }
660
-
661
- // Add delay between chunks to avoid rate limiting (except after last chunk)
662
- if (chunkIdx < updateChunks.length - 1) {
663
- await sleep(BATCH_DELAY_MS);
664
- }
665
- }
666
- }
667
- }
668
-
669
- // STEP 3: Auto-unclear YNAB transactions missing from bank
670
- const shouldRunSanityPass = params.auto_unclear_missing && !balanceAligned;
671
-
672
- // Diagnostic logging for auto_unclear_missing debugging
673
- actions_taken.push({
674
- type: 'diagnostic_step3_entry',
675
- transaction: null,
676
- reason: `STEP 3 diagnostics: auto_unclear_missing=${params.auto_unclear_missing}, balanceAligned=${balanceAligned}, shouldRunSanityPass=${shouldRunSanityPass}, orderedUnmatchedYNAB.length=${orderedUnmatchedYNAB.length}`,
677
- });
678
-
679
- if (orderedUnmatchedYNAB.length > 0) {
680
- const unmatchedDetails = orderedUnmatchedYNAB.slice(0, 10).map((t) => ({
681
- id: t.id,
682
- date: t.date,
683
- cleared: t.cleared,
684
- amount: formatDisplay(t.amount, currencyCode),
685
- payee: t.payee ?? 'Unknown',
686
- }));
687
- actions_taken.push({
688
- type: 'diagnostic_unmatched_ynab',
689
- transaction: { unmatched_transactions: unmatchedDetails } as unknown as Record<
690
- string,
691
- unknown
692
- >,
693
- reason: `First ${Math.min(10, orderedUnmatchedYNAB.length)} unmatched YNAB transactions (cleared status and amounts)`,
694
- });
695
- }
696
-
697
- if (shouldRunSanityPass) {
698
- const transactionsToUnclear: ynab.SaveTransactionWithIdOrImportId[] = [];
699
-
700
- for (const ynabTxn of orderedUnmatchedYNAB) {
701
- if (ynabTxn.cleared !== 'cleared') continue;
702
- if (balanceAligned) break;
703
-
704
- if (params.dry_run) {
705
- summary.transactions_updated += 1;
706
- actions_taken.push({
707
- type: 'update_transaction',
708
- transaction: { transaction_id: ynabTxn.id, cleared: 'uncleared' },
709
- reason: `Would mark transaction ${ynabTxn.id} as uncleared - not present on statement`,
710
- });
711
- applyClearedDelta(-ynabTxn.amount);
712
- if (recordAlignmentIfNeeded(`unclearing ${ynabTxn.id} (dry run)`)) {
713
- break;
714
- }
715
- } else {
716
- // Minimal update payload - only include ID and the field we're changing
717
- transactionsToUnclear.push({
718
- id: ynabTxn.id,
719
- cleared: 'uncleared' as ynab.TransactionClearedStatus,
720
- });
721
- applyClearedDelta(-ynabTxn.amount);
722
- if (recordAlignmentIfNeeded(`unclearing ${ynabTxn.id}`)) {
723
- break;
724
- }
725
- }
726
- }
727
-
728
- // Batch update all unclear operations in a single API call
729
- // YNAB API has a limit of ~100 transactions per batch, so we chunk the updates
730
- if (!params.dry_run && transactionsToUnclear.length > 0) {
731
- const unclearChunks = chunkArray(transactionsToUnclear, MAX_BULK_UPDATE_CHUNK);
732
-
733
- for (let chunkIdx = 0; chunkIdx < unclearChunks.length; chunkIdx++) {
734
- const chunk = unclearChunks[chunkIdx]!;
735
- try {
736
- const response = await ynabAPI.transactions.updateTransactions(budgetId, {
737
- transactions: chunk,
738
- });
739
-
740
- const updatedTransactions = response.data.transactions ?? [];
741
- summary.transactions_updated += updatedTransactions.length;
742
-
743
- for (const updatedTransaction of updatedTransactions) {
744
- actions_taken.push({
745
- type: 'update_transaction',
746
- transaction: updatedTransaction as unknown as Record<string, unknown> | null,
747
- reason: `Marked transaction ${updatedTransaction.id} as uncleared - not found on statement`,
748
- });
749
- }
750
- accountSnapshotDirty = true;
751
- // Report progress after successful unclear batch
752
- completedOperations += updatedTransactions.length;
753
- await reportProgress(
754
- `Marked ${completedOperations} of ${totalOperations} transactions uncleared`,
755
- );
756
- } catch (error) {
757
- const ynabError = normalizeYnabError(error);
758
- const failureReason = ynabError.message || 'Unknown error occurred';
759
- const statusSuffix = ynabError.status ? ` (HTTP ${ynabError.status})` : '';
760
- actions_taken.push({
761
- type: 'batch_unclear_failed',
762
- transaction: null,
763
- reason: `Failed to unclear chunk ${chunkIdx + 1}/${unclearChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
764
- });
765
-
766
- if (shouldPropagateYnabError(ynabError)) {
767
- throw attachStatusToError(ynabError, error);
768
- }
769
- }
770
-
771
- // Add delay between chunks to avoid rate limiting (except after last chunk)
772
- if (chunkIdx < unclearChunks.length - 1) {
773
- await sleep(BATCH_DELAY_MS);
774
- }
775
- }
776
- }
777
- }
778
-
779
- // STEP 4: Mark all matched transactions as reconciled when balance aligns
780
- if (balanceAligned && !params.dry_run) {
781
- const transactionsToReconcile: ynab.SaveTransactionWithIdOrImportId[] = [];
782
-
783
- for (const match of orderedAutoMatches) {
784
- if (!match.ynabTransaction) continue;
785
- // Only reconcile transactions that are not already reconciled
786
- if (match.ynabTransaction.cleared === 'reconciled') continue;
787
-
788
- transactionsToReconcile.push({
789
- id: match.ynabTransaction.id,
790
- cleared: 'reconciled' as ynab.TransactionClearedStatus,
791
- });
792
- }
793
-
794
- // Batch update all reconciliations in chunks
795
- if (transactionsToReconcile.length > 0) {
796
- const reconcileChunks = chunkArray(transactionsToReconcile, MAX_BULK_UPDATE_CHUNK);
797
-
798
- for (let chunkIdx = 0; chunkIdx < reconcileChunks.length; chunkIdx++) {
799
- const chunk = reconcileChunks[chunkIdx]!;
800
- try {
801
- const response = await ynabAPI.transactions.updateTransactions(budgetId, {
802
- transactions: chunk,
803
- });
804
-
805
- const reconciledTransactions = response.data.transactions ?? [];
806
- summary.transactions_updated += reconciledTransactions.length;
807
-
808
- for (const reconciledTransaction of reconciledTransactions) {
809
- const match = orderedAutoMatches.find(
810
- (m) => m.ynabTransaction?.id === reconciledTransaction.id,
811
- );
812
- actions_taken.push({
813
- type: 'update_transaction',
814
- transaction: reconciledTransaction as unknown as Record<string, unknown> | null,
815
- reason: `Marked as reconciled: ${match?.bankTransaction.payee ?? 'transaction'} (${formatDisplay(reconciledTransaction.amount, currencyCode)})`,
816
- });
817
- }
818
- accountSnapshotDirty = true;
819
- } catch (error) {
820
- const ynabError = normalizeYnabError(error);
821
- const failureReason = ynabError.message || 'Unknown error occurred';
822
- const statusSuffix = ynabError.status ? ` (HTTP ${ynabError.status})` : '';
823
- actions_taken.push({
824
- type: 'batch_reconcile_failed',
825
- transaction: null,
826
- reason: `Failed to reconcile chunk ${chunkIdx + 1}/${reconcileChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
827
- });
828
-
829
- if (shouldPropagateYnabError(ynabError)) {
830
- throw attachStatusToError(ynabError, error);
831
- }
832
- }
833
-
834
- // Add delay between chunks to avoid rate limiting (except after last chunk)
835
- if (chunkIdx < reconcileChunks.length - 1) {
836
- await sleep(BATCH_DELAY_MS);
837
- }
838
- }
839
-
840
- actions_taken.push({
841
- type: 'reconciliation_complete',
842
- transaction: null,
843
- reason: `Marked ${transactionsToReconcile.length} matched transaction(s) as reconciled - balance aligned within tolerance`,
844
- });
845
- }
846
- }
847
-
848
- // STEP 5: Balance reconciliation snapshot (only once per execution)
849
- let balance_reconciliation: ExecutionResult['balance_reconciliation'];
850
- if (params.statement_balance !== undefined && params.statement_date) {
851
- balance_reconciliation = await buildBalanceReconciliation({
852
- ynabAPI,
853
- budgetId,
854
- accountId,
855
- statementDate: params.statement_date,
856
- statementBalance: params.statement_balance,
857
- analysis,
858
- });
859
- }
860
-
861
- // STEP 6: Recommendations and balance changes
862
- if (!params.dry_run && accountSnapshotDirty) {
863
- afterAccount = await refreshAccountSnapshot(ynabAPI, budgetId, accountId);
864
- }
865
-
866
- const balanceChangeMilli =
867
- params.dry_run || !accountSnapshotDirty ? 0 : afterAccount.balance - initialAccount.balance;
868
-
869
- const recommendations = buildRecommendations({
870
- summary,
871
- params,
872
- analysis,
873
- balanceChangeMilli,
874
- currencyCode,
875
- });
876
-
877
- const result: ExecutionResult = {
878
- summary,
879
- account_balance: {
880
- before: initialAccount,
881
- after: afterAccount,
882
- },
883
- actions_taken,
884
- recommendations,
885
- };
886
-
887
- if (balance_reconciliation !== undefined) {
888
- result.balance_reconciliation = balance_reconciliation;
889
- }
890
-
891
- if (bulkOperationDetails) {
892
- // Ensure failed_transactions mirrors transaction_failures for backward compatibility
893
- bulkOperationDetails.failed_transactions = bulkOperationDetails.transaction_failures;
894
- result.bulk_operation_details = bulkOperationDetails;
895
- }
896
-
897
- return result;
191
+ export async function executeReconciliation(
192
+ options: ExecutionOptions,
193
+ ): Promise<ExecutionResult> {
194
+ const {
195
+ analysis,
196
+ params,
197
+ ynabAPI,
198
+ budgetId,
199
+ accountId,
200
+ initialAccount,
201
+ currencyCode,
202
+ sendProgress,
203
+ } = options;
204
+ const actions_taken: ExecutionActionRecord[] = [];
205
+
206
+ const summary: ExecutionSummary = {
207
+ bank_transactions_count: analysis.summary.bank_transactions_count,
208
+ ynab_transactions_count: analysis.summary.ynab_transactions_count,
209
+ matches_found: analysis.auto_matches.length,
210
+ missing_in_ynab: analysis.summary.unmatched_bank,
211
+ missing_in_bank: analysis.summary.unmatched_ynab,
212
+ transactions_created: 0,
213
+ transactions_updated: 0,
214
+ dates_adjusted: 0,
215
+ dry_run: params.dry_run,
216
+ };
217
+
218
+ // Progress tracking for MCP notifications
219
+ // Pre-filter matches to only count those that will actually be updated
220
+ // This ensures accurate progress percentages (skipped matches don't inflate total)
221
+ const matchesNeedingUpdate = analysis.auto_matches.filter((match) => {
222
+ const flags = computeUpdateFlags(match, params);
223
+ return flags.needsClearedUpdate || flags.needsDateUpdate;
224
+ });
225
+ const totalOperations =
226
+ (params.auto_create_transactions ? analysis.unmatched_bank.length : 0) +
227
+ matchesNeedingUpdate.length +
228
+ (params.auto_unclear_missing ? analysis.unmatched_ynab.length : 0);
229
+ let completedOperations = 0;
230
+
231
+ const reportProgress = async (message: string): Promise<void> => {
232
+ if (sendProgress && totalOperations > 0) {
233
+ await sendProgress({
234
+ progress: completedOperations,
235
+ total: totalOperations,
236
+ message,
237
+ });
238
+ }
239
+ };
240
+
241
+ let afterAccount: AccountSnapshot = { ...initialAccount };
242
+ let accountSnapshotDirty = false;
243
+ const statementTargetMilli = resolveStatementBalanceMilli(
244
+ analysis.balance_info,
245
+ params.statement_balance,
246
+ );
247
+ let clearedDeltaMilli = addMilli(
248
+ initialAccount.cleared_balance ?? 0,
249
+ -statementTargetMilli,
250
+ );
251
+ const balanceToleranceMilli =
252
+ Math.max(0, params.amount_tolerance_cents ?? DEFAULT_TOLERANCE_CENTS) *
253
+ CENTS_TO_MILLI;
254
+ let balanceAligned = false;
255
+
256
+ const applyClearedDelta = (delta: number) => {
257
+ if (delta === 0) return;
258
+ clearedDeltaMilli = addMilli(clearedDeltaMilli, delta);
259
+ };
260
+
261
+ const recordAlignmentIfNeeded = (trigger: string, { log = true } = {}) => {
262
+ if (balanceAligned) {
263
+ return true;
264
+ }
265
+ if (Math.abs(clearedDeltaMilli) <= balanceToleranceMilli) {
266
+ balanceAligned = true;
267
+ if (log) {
268
+ const deltaDisplay = toMoneyValue(
269
+ clearedDeltaMilli,
270
+ currencyCode,
271
+ ).value_display;
272
+ const toleranceDisplay = toMoneyValue(
273
+ balanceToleranceMilli,
274
+ currencyCode,
275
+ ).value_display;
276
+ actions_taken.push({
277
+ type: "balance_checkpoint",
278
+ transaction: null,
279
+ reason: `Cleared delta ${deltaDisplay} within ±${toleranceDisplay} after ${trigger} - halting newest-to-oldest pass`,
280
+ });
281
+ }
282
+ return true;
283
+ }
284
+ return false;
285
+ };
286
+
287
+ recordAlignmentIfNeeded("initial balance check", { log: false });
288
+
289
+ const orderedUnmatchedBank = params.auto_create_transactions
290
+ ? sortByDateDescending(analysis.unmatched_bank)
291
+ : [];
292
+ const orderedAutoMatches = sortMatchesByBankDateDescending(
293
+ analysis.auto_matches,
294
+ );
295
+ const statementWindow = resolveStatementWindow(
296
+ params,
297
+ analysis.summary.statement_date_range,
298
+ );
299
+ const orderedUnmatchedYNAB = sortByDateDescending(
300
+ statementWindow
301
+ ? analysis.unmatched_ynab.filter((txn) =>
302
+ isWithinStatementWindow(txn.date, statementWindow),
303
+ )
304
+ : analysis.unmatched_ynab,
305
+ );
306
+
307
+ let bulkOperationDetails: BulkOperationDetails | undefined;
308
+
309
+ // STEP 1: Auto-create missing transactions (bank -> YNAB)
310
+ if (params.auto_create_transactions && !balanceAligned) {
311
+ const buildPreparedEntry = (
312
+ bankTxn: BankTransaction,
313
+ ): PreparedBulkCreateEntry => {
314
+ const amountMilli = bankTxn.amount;
315
+ const saveTransaction: SaveTransaction = {
316
+ account_id: accountId,
317
+ amount: amountMilli,
318
+ date: bankTxn.date,
319
+ payee_name: bankTxn.payee ?? undefined,
320
+ memo: truncateMemo(bankTxn.memo),
321
+ cleared: "cleared",
322
+ approved: true,
323
+ // Note: import_id intentionally omitted so transactions can match with bank imports
324
+ };
325
+ const correlationKey = generateCorrelationKey(
326
+ toCorrelationPayload(saveTransaction),
327
+ );
328
+ return {
329
+ bankTransaction: bankTxn,
330
+ saveTransaction,
331
+ amountMilli,
332
+ correlationKey,
333
+ };
334
+ };
335
+
336
+ const recordCreateAction = (args: {
337
+ entry: PreparedBulkCreateEntry;
338
+ createdTxn: ynab.TransactionDetail | null;
339
+ chunkIndex?: number;
340
+ prefix?: string;
341
+ }) => {
342
+ const { entry, createdTxn, chunkIndex, prefix } = args;
343
+ summary.transactions_created += 1;
344
+ const action: ExecutionActionRecord = {
345
+ type: "create_transaction",
346
+ transaction: createdTxn as unknown as Record<string, unknown> | null,
347
+ reason: `${prefix ?? "Created missing transaction"}: ${
348
+ entry.bankTransaction.payee ?? "Unknown"
349
+ } (${formatDisplay(entry.bankTransaction.amount, currencyCode)})`,
350
+ correlation_key: entry.correlationKey,
351
+ };
352
+ if (chunkIndex !== undefined) {
353
+ action.bulk_chunk_index = chunkIndex;
354
+ }
355
+ actions_taken.push(action);
356
+ };
357
+
358
+ const processSequentialEntries = async (
359
+ entries: PreparedBulkCreateEntry[],
360
+ options: { chunkIndex?: number; fallbackError?: unknown } = {},
361
+ ) => {
362
+ let sequentialAttempts = 0;
363
+ for (const entry of entries) {
364
+ if (balanceAligned) break;
365
+ if (options.fallbackError) {
366
+ sequentialAttempts += 1;
367
+ }
368
+ try {
369
+ const response = await ynabAPI.transactions.createTransaction(
370
+ budgetId,
371
+ {
372
+ transaction: entry.saveTransaction,
373
+ },
374
+ );
375
+ const createdTransaction = response.data.transaction ?? null;
376
+ const recordArgs: Parameters<typeof recordCreateAction>[0] = {
377
+ entry,
378
+ createdTxn: createdTransaction,
379
+ prefix: options.fallbackError
380
+ ? "Created missing transaction after bulk fallback"
381
+ : "Created missing transaction",
382
+ };
383
+ if (options.chunkIndex !== undefined) {
384
+ recordArgs.chunkIndex = options.chunkIndex;
385
+ }
386
+ recordCreateAction(recordArgs);
387
+ accountSnapshotDirty = true;
388
+ applyClearedDelta(entry.amountMilli);
389
+ // Report progress for sequential/fallback operations
390
+ completedOperations += 1;
391
+ await reportProgress(
392
+ `Created ${completedOperations} of ${totalOperations} transactions`,
393
+ );
394
+ const trigger = options.chunkIndex
395
+ ? `creating ${entry.bankTransaction.payee ?? "missing transaction"} (chunk ${options.chunkIndex})`
396
+ : `creating ${entry.bankTransaction.payee ?? "missing transaction"}`;
397
+ recordAlignmentIfNeeded(trigger);
398
+ } catch (error) {
399
+ const ynabError = normalizeYnabError(error);
400
+ if (bulkOperationDetails) {
401
+ bulkOperationDetails.transaction_failures += 1; // Canonical counter for per-transaction failures
402
+ }
403
+ const failureReason = ynabError.message || "Unknown error occurred";
404
+ const statusSuffix = ynabError.status
405
+ ? ` (HTTP ${ynabError.status})`
406
+ : "";
407
+ const failureAction: ExecutionActionRecord = {
408
+ type: "create_transaction_failed",
409
+ transaction: entry.saveTransaction as unknown as Record<
410
+ string,
411
+ unknown
412
+ >,
413
+ reason: options.fallbackError
414
+ ? `Bulk fallback failed for ${entry.bankTransaction.payee ?? "Unknown"} (${failureReason}${statusSuffix})`
415
+ : `Failed to create transaction ${entry.bankTransaction.payee ?? "Unknown"} (${failureReason}${statusSuffix})`,
416
+ correlation_key: entry.correlationKey,
417
+ };
418
+ if (options.chunkIndex !== undefined) {
419
+ failureAction.bulk_chunk_index = options.chunkIndex;
420
+ }
421
+ actions_taken.push(failureAction);
422
+
423
+ if (shouldPropagateYnabError(ynabError)) {
424
+ throw attachStatusToError(ynabError, error);
425
+ }
426
+ }
427
+ }
428
+ // Update sequential_attempts metric if this was a fallback operation
429
+ if (
430
+ bulkOperationDetails &&
431
+ options.fallbackError &&
432
+ sequentialAttempts > 0
433
+ ) {
434
+ bulkOperationDetails.sequential_attempts =
435
+ (bulkOperationDetails.sequential_attempts ?? 0) + sequentialAttempts;
436
+ }
437
+ };
438
+
439
+ const processBulkChunk = async (
440
+ chunk: PreparedBulkCreateEntry[],
441
+ chunkIndex: number,
442
+ ) => {
443
+ // bulkOperationDetails is guaranteed to be defined when this function is called
444
+ // (it's only called from within the bulk operation block where it's initialized)
445
+ if (!bulkOperationDetails) {
446
+ throw new Error("Bulk operation details not initialized");
447
+ }
448
+ const bulkDetails = bulkOperationDetails;
449
+
450
+ const payload = chunk.map((entry) => entry.saveTransaction);
451
+ const response = await ynabAPI.transactions.createTransactions(budgetId, {
452
+ transactions: payload,
453
+ });
454
+ const responseData = response.data;
455
+ const duplicateImportIds = new Set(
456
+ responseData.duplicate_import_ids ?? [],
457
+ );
458
+ const correlationRequests = chunk.map((entry) =>
459
+ toCorrelationPayload(entry.saveTransaction),
460
+ ) as Parameters<typeof correlateResults>[0];
461
+ const correlated = correlateResults(
462
+ correlationRequests,
463
+ responseData,
464
+ duplicateImportIds,
465
+ );
466
+ const transactionMap = new Map<string, ynab.TransactionDetail>();
467
+ for (const transaction of responseData.transactions ?? []) {
468
+ if (transaction.id) {
469
+ transactionMap.set(transaction.id, transaction);
470
+ }
471
+ }
472
+ for (const result of correlated) {
473
+ const entry = chunk[result.request_index];
474
+ if (!entry) continue;
475
+ if (result.status === "created") {
476
+ const createdTransaction = result.transaction_id
477
+ ? (transactionMap.get(result.transaction_id) ?? null)
478
+ : null;
479
+ recordCreateAction({
480
+ entry,
481
+ createdTxn: createdTransaction,
482
+ chunkIndex,
483
+ prefix: "Created missing transaction via bulk",
484
+ });
485
+ accountSnapshotDirty = true;
486
+ applyClearedDelta(entry.amountMilli);
487
+ recordAlignmentIfNeeded(
488
+ `creating ${entry.bankTransaction.payee ?? "missing transaction"} via bulk chunk ${chunkIndex}`,
489
+ );
490
+ } else if (result.status === "duplicate") {
491
+ bulkDetails.duplicates_detected += 1;
492
+ actions_taken.push({
493
+ type: "create_transaction_duplicate",
494
+ transaction: {
495
+ transaction_id: result.transaction_id ?? null,
496
+ import_id: entry.saveTransaction.import_id,
497
+ },
498
+ reason: `Duplicate import detected for ${
499
+ entry.bankTransaction.payee ?? "Unknown"
500
+ } (import_id ${entry.saveTransaction.import_id})`,
501
+ bulk_chunk_index: chunkIndex,
502
+ correlation_key: result.correlation_key,
503
+ duplicate: true,
504
+ });
505
+ } else {
506
+ bulkDetails.transaction_failures += 1; // Canonical counter for per-transaction failures
507
+ actions_taken.push({
508
+ type: "create_transaction_failed",
509
+ transaction: entry.saveTransaction as unknown as Record<
510
+ string,
511
+ unknown
512
+ >,
513
+ reason:
514
+ result.error ??
515
+ `Bulk create failed for ${entry.bankTransaction.payee ?? "Unknown"}`,
516
+ bulk_chunk_index: chunkIndex,
517
+ correlation_key: result.correlation_key,
518
+ });
519
+ }
520
+ }
521
+ };
522
+
523
+ if (params.dry_run) {
524
+ for (const bankTxn of orderedUnmatchedBank) {
525
+ if (balanceAligned) break;
526
+ const entry = buildPreparedEntry(bankTxn);
527
+ summary.transactions_created += 1;
528
+ actions_taken.push({
529
+ type: "create_transaction",
530
+ transaction: entry.saveTransaction as unknown as Record<
531
+ string,
532
+ unknown
533
+ >,
534
+ reason: `Would create missing transaction: ${bankTxn.payee ?? "Unknown"} (${formatDisplay(bankTxn.amount, currencyCode)})`,
535
+ correlation_key: entry.correlationKey,
536
+ });
537
+ applyClearedDelta(entry.amountMilli);
538
+ recordAlignmentIfNeeded(
539
+ `creating ${bankTxn.payee ?? "missing transaction"}`,
540
+ );
541
+ }
542
+ } else if (orderedUnmatchedBank.length >= 2) {
543
+ bulkOperationDetails = {
544
+ chunks_processed: 0,
545
+ bulk_successes: 0,
546
+ sequential_fallbacks: 0,
547
+ duplicates_detected: 0,
548
+ failed_transactions: 0,
549
+ bulk_chunk_failures: 0,
550
+ transaction_failures: 0,
551
+ };
552
+
553
+ let nextBankIndex = 0;
554
+ while (nextBankIndex < orderedUnmatchedBank.length && !balanceAligned) {
555
+ const batch: PreparedBulkCreateEntry[] = [];
556
+ let projectedDelta = clearedDeltaMilli;
557
+ while (nextBankIndex < orderedUnmatchedBank.length) {
558
+ const bankTxn = orderedUnmatchedBank[nextBankIndex];
559
+ if (!bankTxn) {
560
+ nextBankIndex += 1;
561
+ continue;
562
+ }
563
+ const entry = buildPreparedEntry(bankTxn);
564
+ batch.push(entry);
565
+ nextBankIndex += 1;
566
+ projectedDelta = addMilli(projectedDelta, entry.amountMilli);
567
+ if (Math.abs(projectedDelta) <= balanceToleranceMilli) {
568
+ break;
569
+ }
570
+ }
571
+
572
+ if (batch.length === 0) {
573
+ break;
574
+ }
575
+
576
+ const chunks = chunkArray(batch, MAX_BULK_CREATE_CHUNK);
577
+ for (const chunk of chunks) {
578
+ if (balanceAligned) break;
579
+ bulkOperationDetails.chunks_processed += 1;
580
+ const chunkIndex = bulkOperationDetails.chunks_processed;
581
+ try {
582
+ await processBulkChunk(chunk, chunkIndex);
583
+ bulkOperationDetails.bulk_successes += 1;
584
+ // Report progress after successful chunk processing
585
+ completedOperations += chunk.length;
586
+ await reportProgress(
587
+ `Created ${completedOperations} of ${totalOperations} transactions`,
588
+ );
589
+ } catch (error) {
590
+ const ynabError = normalizeYnabError(error);
591
+ const failureReason = ynabError.message || "unknown error";
592
+ bulkOperationDetails.bulk_chunk_failures += 1; // API-level failure (entire chunk failed)
593
+
594
+ if (shouldPropagateYnabError(ynabError)) {
595
+ bulkOperationDetails.transaction_failures += chunk.length;
596
+ throw attachStatusToError(ynabError, error);
597
+ }
598
+
599
+ bulkOperationDetails.sequential_fallbacks += 1;
600
+ actions_taken.push({
601
+ type: "bulk_create_fallback",
602
+ transaction: null,
603
+ reason: `Bulk chunk #${chunkIndex} failed (${failureReason}${
604
+ ynabError.status ? ` (HTTP ${ynabError.status})` : ""
605
+ }) - falling back to sequential creation`,
606
+ bulk_chunk_index: chunkIndex,
607
+ });
608
+ await processSequentialEntries(chunk, {
609
+ chunkIndex,
610
+ fallbackError: ynabError,
611
+ });
612
+ }
613
+ }
614
+ }
615
+ } else {
616
+ const entries = orderedUnmatchedBank.map((bankTxn) =>
617
+ buildPreparedEntry(bankTxn),
618
+ );
619
+ await processSequentialEntries(entries);
620
+ }
621
+ }
622
+
623
+ // STEP 2: Update matched YNAB transactions (cleared status / date)
624
+ // Collect all updates for batch processing
625
+ if (!balanceAligned) {
626
+ const transactionsToUpdate: ynab.SaveTransactionWithIdOrImportId[] = [];
627
+
628
+ for (const match of orderedAutoMatches) {
629
+ if (balanceAligned) break;
630
+ const flags = computeUpdateFlags(match, params);
631
+ if (!flags.needsClearedUpdate && !flags.needsDateUpdate) continue;
632
+ if (!match.ynabTransaction) continue;
633
+
634
+ // Build minimal update payload - only include ID and fields that are changing
635
+ // Including unnecessary fields (like amount, payee_name) can cause unexpected behavior
636
+ // BUT we must include memo to fix existing memos that exceed YNAB's 500 char limit
637
+ const updatePayload: ynab.SaveTransactionWithIdOrImportId = {
638
+ id: match.ynabTransaction.id,
639
+ };
640
+
641
+ // Truncate memo if it exists and is too long (YNAB validates on update even if not changed)
642
+ if (match.ynabTransaction.memo) {
643
+ updatePayload.memo = truncateMemo(match.ynabTransaction.memo);
644
+ }
645
+
646
+ // Only include fields that are actually changing
647
+ if (flags.needsDateUpdate) {
648
+ updatePayload.date = match.bankTransaction.date;
649
+ }
650
+ if (flags.needsClearedUpdate) {
651
+ updatePayload.cleared = "cleared" as ynab.TransactionClearedStatus;
652
+ }
653
+
654
+ if (params.dry_run) {
655
+ summary.transactions_updated += 1;
656
+ if (flags.needsDateUpdate) summary.dates_adjusted += 1;
657
+ actions_taken.push({
658
+ type: "update_transaction",
659
+ transaction: {
660
+ transaction_id: match.ynabTransaction.id,
661
+ new_date: flags.needsDateUpdate
662
+ ? match.bankTransaction.date
663
+ : undefined,
664
+ cleared: flags.needsClearedUpdate ? "cleared" : undefined,
665
+ },
666
+ reason: `Would update transaction: ${updateReason(match, flags, currencyCode)}`,
667
+ });
668
+ if (flags.needsClearedUpdate) {
669
+ applyClearedDelta(match.ynabTransaction.amount);
670
+ if (
671
+ recordAlignmentIfNeeded(
672
+ `clearing ${match.ynabTransaction.id ?? "transaction"} (dry run)`,
673
+ )
674
+ ) {
675
+ break;
676
+ }
677
+ }
678
+ } else {
679
+ transactionsToUpdate.push(updatePayload);
680
+ if (flags.needsDateUpdate) summary.dates_adjusted += 1;
681
+ if (flags.needsClearedUpdate) {
682
+ applyClearedDelta(match.ynabTransaction.amount);
683
+ if (recordAlignmentIfNeeded(`clearing ${match.ynabTransaction.id}`)) {
684
+ break;
685
+ }
686
+ }
687
+ }
688
+ }
689
+
690
+ // Batch update all transactions in a single API call
691
+ // YNAB API has a limit of ~100 transactions per batch, so we chunk the updates
692
+ if (!params.dry_run && transactionsToUpdate.length > 0) {
693
+ const updateChunks = chunkArray(
694
+ transactionsToUpdate,
695
+ MAX_BULK_UPDATE_CHUNK,
696
+ );
697
+
698
+ for (let chunkIdx = 0; chunkIdx < updateChunks.length; chunkIdx++) {
699
+ const chunk = updateChunks[chunkIdx];
700
+ if (!chunk) continue;
701
+ try {
702
+ const response = await ynabAPI.transactions.updateTransactions(
703
+ budgetId,
704
+ {
705
+ transactions: chunk,
706
+ },
707
+ );
708
+
709
+ const updatedTransactions = response.data.transactions ?? [];
710
+ summary.transactions_updated += updatedTransactions.length;
711
+
712
+ for (const updatedTransaction of updatedTransactions) {
713
+ const match = orderedAutoMatches.find(
714
+ (m) => m.ynabTransaction?.id === updatedTransaction.id,
715
+ );
716
+ const flags = match
717
+ ? computeUpdateFlags(match, params)
718
+ : { needsClearedUpdate: false, needsDateUpdate: false };
719
+ actions_taken.push({
720
+ type: "update_transaction",
721
+ transaction: updatedTransaction as unknown as Record<
722
+ string,
723
+ unknown
724
+ > | null,
725
+ reason: `Updated transaction: ${match ? updateReason(match, flags, currencyCode) : "cleared"}`,
726
+ });
727
+ }
728
+ accountSnapshotDirty = true;
729
+ // Report progress after successful batch update
730
+ completedOperations += updatedTransactions.length;
731
+ await reportProgress(
732
+ `Updated ${completedOperations} of ${totalOperations} transactions`,
733
+ );
734
+ } catch (error) {
735
+ const ynabError = normalizeYnabError(error);
736
+ const failureReason = ynabError.message || "Unknown error occurred";
737
+ const statusSuffix = ynabError.status
738
+ ? ` (HTTP ${ynabError.status})`
739
+ : "";
740
+ actions_taken.push({
741
+ type: "batch_update_failed",
742
+ transaction: null,
743
+ reason: `Failed to update chunk ${chunkIdx + 1}/${updateChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
744
+ });
745
+
746
+ if (shouldPropagateYnabError(ynabError)) {
747
+ throw attachStatusToError(ynabError, error);
748
+ }
749
+ }
750
+
751
+ // Add delay between chunks to avoid rate limiting (except after last chunk)
752
+ if (chunkIdx < updateChunks.length - 1) {
753
+ await sleep(BATCH_DELAY_MS);
754
+ }
755
+ }
756
+ }
757
+ }
758
+
759
+ // STEP 3: Auto-unclear YNAB transactions missing from bank
760
+ const shouldRunSanityPass = params.auto_unclear_missing && !balanceAligned;
761
+
762
+ // Diagnostic logging for auto_unclear_missing debugging
763
+ actions_taken.push({
764
+ type: "diagnostic_step3_entry",
765
+ transaction: null,
766
+ reason: `STEP 3 diagnostics: auto_unclear_missing=${params.auto_unclear_missing}, balanceAligned=${balanceAligned}, shouldRunSanityPass=${shouldRunSanityPass}, orderedUnmatchedYNAB.length=${orderedUnmatchedYNAB.length}`,
767
+ });
768
+
769
+ if (orderedUnmatchedYNAB.length > 0) {
770
+ const unmatchedDetails = orderedUnmatchedYNAB.slice(0, 10).map((t) => ({
771
+ id: t.id,
772
+ date: t.date,
773
+ cleared: t.cleared,
774
+ amount: formatDisplay(t.amount, currencyCode),
775
+ payee: t.payee ?? "Unknown",
776
+ }));
777
+ actions_taken.push({
778
+ type: "diagnostic_unmatched_ynab",
779
+ transaction: {
780
+ unmatched_transactions: unmatchedDetails,
781
+ } as unknown as Record<string, unknown>,
782
+ reason: `First ${Math.min(10, orderedUnmatchedYNAB.length)} unmatched YNAB transactions (cleared status and amounts)`,
783
+ });
784
+ }
785
+
786
+ if (shouldRunSanityPass) {
787
+ const transactionsToUnclear: ynab.SaveTransactionWithIdOrImportId[] = [];
788
+
789
+ for (const ynabTxn of orderedUnmatchedYNAB) {
790
+ if (ynabTxn.cleared !== "cleared") continue;
791
+ if (balanceAligned) break;
792
+
793
+ if (params.dry_run) {
794
+ summary.transactions_updated += 1;
795
+ actions_taken.push({
796
+ type: "update_transaction",
797
+ transaction: { transaction_id: ynabTxn.id, cleared: "uncleared" },
798
+ reason: `Would mark transaction ${ynabTxn.id} as uncleared - not present on statement`,
799
+ });
800
+ applyClearedDelta(-ynabTxn.amount);
801
+ if (recordAlignmentIfNeeded(`unclearing ${ynabTxn.id} (dry run)`)) {
802
+ break;
803
+ }
804
+ } else {
805
+ // Minimal update payload - only include ID and the field we're changing
806
+ transactionsToUnclear.push({
807
+ id: ynabTxn.id,
808
+ cleared: "uncleared" as ynab.TransactionClearedStatus,
809
+ });
810
+ applyClearedDelta(-ynabTxn.amount);
811
+ if (recordAlignmentIfNeeded(`unclearing ${ynabTxn.id}`)) {
812
+ break;
813
+ }
814
+ }
815
+ }
816
+
817
+ // Batch update all unclear operations in a single API call
818
+ // YNAB API has a limit of ~100 transactions per batch, so we chunk the updates
819
+ if (!params.dry_run && transactionsToUnclear.length > 0) {
820
+ const unclearChunks = chunkArray(
821
+ transactionsToUnclear,
822
+ MAX_BULK_UPDATE_CHUNK,
823
+ );
824
+
825
+ for (let chunkIdx = 0; chunkIdx < unclearChunks.length; chunkIdx++) {
826
+ const chunk = unclearChunks[chunkIdx];
827
+ if (!chunk) continue;
828
+ try {
829
+ const response = await ynabAPI.transactions.updateTransactions(
830
+ budgetId,
831
+ {
832
+ transactions: chunk,
833
+ },
834
+ );
835
+
836
+ const updatedTransactions = response.data.transactions ?? [];
837
+ summary.transactions_updated += updatedTransactions.length;
838
+
839
+ for (const updatedTransaction of updatedTransactions) {
840
+ actions_taken.push({
841
+ type: "update_transaction",
842
+ transaction: updatedTransaction as unknown as Record<
843
+ string,
844
+ unknown
845
+ > | null,
846
+ reason: `Marked transaction ${updatedTransaction.id} as uncleared - not found on statement`,
847
+ });
848
+ }
849
+ accountSnapshotDirty = true;
850
+ // Report progress after successful unclear batch
851
+ completedOperations += updatedTransactions.length;
852
+ await reportProgress(
853
+ `Marked ${completedOperations} of ${totalOperations} transactions uncleared`,
854
+ );
855
+ } catch (error) {
856
+ const ynabError = normalizeYnabError(error);
857
+ const failureReason = ynabError.message || "Unknown error occurred";
858
+ const statusSuffix = ynabError.status
859
+ ? ` (HTTP ${ynabError.status})`
860
+ : "";
861
+ actions_taken.push({
862
+ type: "batch_unclear_failed",
863
+ transaction: null,
864
+ reason: `Failed to unclear chunk ${chunkIdx + 1}/${unclearChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
865
+ });
866
+
867
+ if (shouldPropagateYnabError(ynabError)) {
868
+ throw attachStatusToError(ynabError, error);
869
+ }
870
+ }
871
+
872
+ // Add delay between chunks to avoid rate limiting (except after last chunk)
873
+ if (chunkIdx < unclearChunks.length - 1) {
874
+ await sleep(BATCH_DELAY_MS);
875
+ }
876
+ }
877
+ }
878
+ }
879
+
880
+ // STEP 4: Mark all matched transactions as reconciled when balance aligns
881
+ if (balanceAligned && !params.dry_run) {
882
+ const transactionsToReconcile: ynab.SaveTransactionWithIdOrImportId[] = [];
883
+
884
+ for (const match of orderedAutoMatches) {
885
+ if (!match.ynabTransaction) continue;
886
+ // Only reconcile transactions that are not already reconciled
887
+ if (match.ynabTransaction.cleared === "reconciled") continue;
888
+
889
+ transactionsToReconcile.push({
890
+ id: match.ynabTransaction.id,
891
+ cleared: "reconciled" as ynab.TransactionClearedStatus,
892
+ });
893
+ }
894
+
895
+ // Batch update all reconciliations in chunks
896
+ if (transactionsToReconcile.length > 0) {
897
+ const reconcileChunks = chunkArray(
898
+ transactionsToReconcile,
899
+ MAX_BULK_UPDATE_CHUNK,
900
+ );
901
+
902
+ for (let chunkIdx = 0; chunkIdx < reconcileChunks.length; chunkIdx++) {
903
+ const chunk = reconcileChunks[chunkIdx];
904
+ if (!chunk) continue;
905
+ try {
906
+ const response = await ynabAPI.transactions.updateTransactions(
907
+ budgetId,
908
+ {
909
+ transactions: chunk,
910
+ },
911
+ );
912
+
913
+ const reconciledTransactions = response.data.transactions ?? [];
914
+ summary.transactions_updated += reconciledTransactions.length;
915
+
916
+ for (const reconciledTransaction of reconciledTransactions) {
917
+ const match = orderedAutoMatches.find(
918
+ (m) => m.ynabTransaction?.id === reconciledTransaction.id,
919
+ );
920
+ actions_taken.push({
921
+ type: "update_transaction",
922
+ transaction: reconciledTransaction as unknown as Record<
923
+ string,
924
+ unknown
925
+ > | null,
926
+ reason: `Marked as reconciled: ${match?.bankTransaction.payee ?? "transaction"} (${formatDisplay(reconciledTransaction.amount, currencyCode)})`,
927
+ });
928
+ }
929
+ accountSnapshotDirty = true;
930
+ } catch (error) {
931
+ const ynabError = normalizeYnabError(error);
932
+ const failureReason = ynabError.message || "Unknown error occurred";
933
+ const statusSuffix = ynabError.status
934
+ ? ` (HTTP ${ynabError.status})`
935
+ : "";
936
+ actions_taken.push({
937
+ type: "batch_reconcile_failed",
938
+ transaction: null,
939
+ reason: `Failed to reconcile chunk ${chunkIdx + 1}/${reconcileChunks.length} (${chunk.length} transaction(s)): ${failureReason}${statusSuffix}`,
940
+ });
941
+
942
+ if (shouldPropagateYnabError(ynabError)) {
943
+ throw attachStatusToError(ynabError, error);
944
+ }
945
+ }
946
+
947
+ // Add delay between chunks to avoid rate limiting (except after last chunk)
948
+ if (chunkIdx < reconcileChunks.length - 1) {
949
+ await sleep(BATCH_DELAY_MS);
950
+ }
951
+ }
952
+
953
+ actions_taken.push({
954
+ type: "reconciliation_complete",
955
+ transaction: null,
956
+ reason: `Marked ${transactionsToReconcile.length} matched transaction(s) as reconciled - balance aligned within tolerance`,
957
+ });
958
+ }
959
+ }
960
+
961
+ // STEP 5: Balance reconciliation snapshot (only once per execution)
962
+ let balance_reconciliation: ExecutionResult["balance_reconciliation"];
963
+ if (params.statement_balance !== undefined && params.statement_date) {
964
+ balance_reconciliation = await buildBalanceReconciliation({
965
+ ynabAPI,
966
+ budgetId,
967
+ accountId,
968
+ statementDate: params.statement_date,
969
+ statementBalance: params.statement_balance,
970
+ analysis,
971
+ });
972
+ }
973
+
974
+ // STEP 6: Recommendations and balance changes
975
+ if (!params.dry_run && accountSnapshotDirty) {
976
+ afterAccount = await refreshAccountSnapshot(ynabAPI, budgetId, accountId);
977
+ }
978
+
979
+ const balanceChangeMilli =
980
+ params.dry_run || !accountSnapshotDirty
981
+ ? 0
982
+ : afterAccount.balance - initialAccount.balance;
983
+
984
+ const recommendations = buildRecommendations({
985
+ summary,
986
+ params,
987
+ analysis,
988
+ balanceChangeMilli,
989
+ currencyCode,
990
+ });
991
+
992
+ const result: ExecutionResult = {
993
+ summary,
994
+ account_balance: {
995
+ before: initialAccount,
996
+ after: afterAccount,
997
+ },
998
+ actions_taken,
999
+ recommendations,
1000
+ };
1001
+
1002
+ if (balance_reconciliation !== undefined) {
1003
+ result.balance_reconciliation = balance_reconciliation;
1004
+ }
1005
+
1006
+ if (bulkOperationDetails) {
1007
+ // Ensure failed_transactions mirrors transaction_failures for backward compatibility
1008
+ bulkOperationDetails.failed_transactions =
1009
+ bulkOperationDetails.transaction_failures;
1010
+ result.bulk_operation_details = bulkOperationDetails;
1011
+ }
1012
+
1013
+ return result;
898
1014
  }
899
1015
 
900
1016
  export interface NormalizedYnabError {
901
- status?: number;
902
- name?: string;
903
- message: string;
904
- detail?: string;
1017
+ status?: number;
1018
+ name?: string;
1019
+ message: string;
1020
+ detail?: string;
905
1021
  }
906
1022
 
907
1023
  const FATAL_YNAB_STATUS_CODES = new Set([400, 401, 403, 404, 429, 500, 503]);
908
1024
 
909
1025
  export function normalizeYnabError(error: unknown): NormalizedYnabError {
910
- const parseStatus = (value: unknown): number | undefined => {
911
- if (typeof value === 'number' && Number.isFinite(value)) return value;
912
- if (typeof value === 'string') {
913
- const numeric = Number(value);
914
- if (Number.isFinite(numeric)) return numeric;
915
- }
916
- return undefined;
917
- };
918
-
919
- if (error instanceof Error) {
920
- const status =
921
- parseStatus((error as { status?: unknown }).status) ??
922
- parseStatus((error as { response?: { status?: unknown } }).response?.status);
923
- const detailSource = (error as { detail?: unknown }).detail;
924
- const detail =
925
- typeof detailSource === 'string' && detailSource.trim().length > 0 ? detailSource : undefined;
926
-
927
- const result: NormalizedYnabError = {
928
- name: error.name,
929
- message: error.message || 'Unknown error occurred',
930
- };
931
-
932
- if (status !== undefined) result.status = status;
933
- if (detail !== undefined) result.detail = detail;
934
-
935
- return result;
936
- }
937
-
938
- if (error && typeof error === 'object') {
939
- const errObj = (error as { error?: unknown }).error ?? error;
940
- const status = parseStatus(
941
- (errObj as { id?: unknown }).id ?? (errObj as { status?: unknown }).status,
942
- );
943
- const detailCandidate =
944
- (errObj as { detail?: unknown }).detail ??
945
- (errObj as { message?: unknown }).message ??
946
- (errObj as { name?: unknown }).name;
947
- const detail =
948
- typeof detailCandidate === 'string' && detailCandidate.trim().length > 0
949
- ? detailCandidate
950
- : undefined;
951
- const message =
952
- detail ??
953
- (typeof errObj === 'string' && errObj.trim().length > 0 ? errObj : 'Unknown error occurred');
954
- const name =
955
- typeof (errObj as { name?: unknown }).name === 'string'
956
- ? ((errObj as { name: string }).name as string)
957
- : undefined;
958
-
959
- const result: NormalizedYnabError = { message };
960
-
961
- if (status !== undefined) result.status = status;
962
- if (name !== undefined) result.name = name;
963
- if (detail !== undefined) result.detail = detail;
964
-
965
- return result;
966
- }
967
-
968
- if (typeof error === 'string') {
969
- return { message: error };
970
- }
971
-
972
- return { message: 'Unknown error occurred' };
1026
+ const parseStatus = (value: unknown): number | undefined => {
1027
+ if (typeof value === "number" && Number.isFinite(value)) return value;
1028
+ if (typeof value === "string") {
1029
+ const numeric = Number(value);
1030
+ if (Number.isFinite(numeric)) return numeric;
1031
+ }
1032
+ return undefined;
1033
+ };
1034
+
1035
+ if (error instanceof Error) {
1036
+ const status =
1037
+ parseStatus((error as { status?: unknown }).status) ??
1038
+ parseStatus(
1039
+ (error as { response?: { status?: unknown } }).response?.status,
1040
+ );
1041
+ const detailSource = (error as { detail?: unknown }).detail;
1042
+ const detail =
1043
+ typeof detailSource === "string" && detailSource.trim().length > 0
1044
+ ? detailSource
1045
+ : undefined;
1046
+
1047
+ const result: NormalizedYnabError = {
1048
+ name: error.name,
1049
+ message: error.message || "Unknown error occurred",
1050
+ };
1051
+
1052
+ if (status !== undefined) result.status = status;
1053
+ if (detail !== undefined) result.detail = detail;
1054
+
1055
+ return result;
1056
+ }
1057
+
1058
+ if (error && typeof error === "object") {
1059
+ const errObj = (error as { error?: unknown }).error ?? error;
1060
+ const status = parseStatus(
1061
+ (errObj as { id?: unknown }).id ??
1062
+ (errObj as { status?: unknown }).status,
1063
+ );
1064
+ const detailCandidate =
1065
+ (errObj as { detail?: unknown }).detail ??
1066
+ (errObj as { message?: unknown }).message ??
1067
+ (errObj as { name?: unknown }).name;
1068
+ const detail =
1069
+ typeof detailCandidate === "string" && detailCandidate.trim().length > 0
1070
+ ? detailCandidate
1071
+ : undefined;
1072
+ const message =
1073
+ detail ??
1074
+ (typeof errObj === "string" && errObj.trim().length > 0
1075
+ ? errObj
1076
+ : "Unknown error occurred");
1077
+ const name =
1078
+ typeof (errObj as { name?: unknown }).name === "string"
1079
+ ? ((errObj as { name: string }).name as string)
1080
+ : undefined;
1081
+
1082
+ const result: NormalizedYnabError = { message };
1083
+
1084
+ if (status !== undefined) result.status = status;
1085
+ if (name !== undefined) result.name = name;
1086
+ if (detail !== undefined) result.detail = detail;
1087
+
1088
+ return result;
1089
+ }
1090
+
1091
+ if (typeof error === "string") {
1092
+ return { message: error };
1093
+ }
1094
+
1095
+ return { message: "Unknown error occurred" };
973
1096
  }
974
1097
 
975
1098
  export function shouldPropagateYnabError(error: NormalizedYnabError): boolean {
976
- return error.status !== undefined && FATAL_YNAB_STATUS_CODES.has(error.status);
1099
+ return (
1100
+ error.status !== undefined && FATAL_YNAB_STATUS_CODES.has(error.status)
1101
+ );
977
1102
  }
978
1103
 
979
- function attachStatusToError(error: NormalizedYnabError, originalError?: unknown): Error {
980
- const message = error.message || 'YNAB API error';
981
-
982
- const isKnownCode =
983
- error.status === YNABErrorCode.BAD_REQUEST ||
984
- error.status === YNABErrorCode.UNAUTHORIZED ||
985
- error.status === YNABErrorCode.FORBIDDEN ||
986
- error.status === YNABErrorCode.NOT_FOUND ||
987
- error.status === YNABErrorCode.TOO_MANY_REQUESTS ||
988
- error.status === YNABErrorCode.INTERNAL_SERVER_ERROR;
989
-
990
- if (isKnownCode) {
991
- return new YNABAPIError(error.status as YNABErrorCode, message, originalError);
992
- }
993
-
994
- const statusFragment = error.status ? ` (HTTP ${error.status})` : '';
995
- const detailFragment =
996
- error.detail && !message.includes(error.detail) ? ` (${error.detail})` : '';
997
- const err = new Error(`${message}${statusFragment}${detailFragment}`);
998
- if (error.status !== undefined) {
999
- (err as { status?: number }).status = error.status;
1000
- }
1001
- if (error.name) {
1002
- err.name = error.name;
1003
- }
1004
- return err;
1104
+ function attachStatusToError(
1105
+ error: NormalizedYnabError,
1106
+ originalError?: unknown,
1107
+ ): Error {
1108
+ const message = error.message || "YNAB API error";
1109
+
1110
+ const isKnownCode =
1111
+ error.status === YNABErrorCode.BAD_REQUEST ||
1112
+ error.status === YNABErrorCode.UNAUTHORIZED ||
1113
+ error.status === YNABErrorCode.FORBIDDEN ||
1114
+ error.status === YNABErrorCode.NOT_FOUND ||
1115
+ error.status === YNABErrorCode.TOO_MANY_REQUESTS ||
1116
+ error.status === YNABErrorCode.INTERNAL_SERVER_ERROR;
1117
+
1118
+ if (isKnownCode) {
1119
+ return new YNABAPIError(
1120
+ error.status as YNABErrorCode,
1121
+ message,
1122
+ originalError,
1123
+ );
1124
+ }
1125
+
1126
+ const statusFragment = error.status ? ` (HTTP ${error.status})` : "";
1127
+ const detailFragment =
1128
+ error.detail && !message.includes(error.detail) ? ` (${error.detail})` : "";
1129
+ const err = new Error(`${message}${statusFragment}${detailFragment}`);
1130
+ if (error.status !== undefined) {
1131
+ (err as { status?: number }).status = error.status;
1132
+ }
1133
+ if (error.name) {
1134
+ err.name = error.name;
1135
+ }
1136
+ return err;
1005
1137
  }
1006
1138
 
1007
1139
  function formatDisplay(amount: number, currency: string): string {
1008
- return toMoneyValue(amount, currency).value_display;
1140
+ return toMoneyValue(amount, currency).value_display;
1009
1141
  }
1010
1142
 
1011
- function computeUpdateFlags(match: TransactionMatch, params: ReconcileAccountRequest): UpdateFlags {
1012
- const ynabTxn = match.ynabTransaction;
1013
- const bankTxn = match.bankTransaction;
1014
- if (!ynabTxn) {
1015
- return { needsClearedUpdate: false, needsDateUpdate: false };
1016
- }
1017
- const needsClearedUpdate = Boolean(
1018
- params.auto_update_cleared_status && ynabTxn.cleared !== 'cleared',
1019
- );
1020
- const needsDateUpdate = Boolean(params.auto_adjust_dates && ynabTxn.date !== bankTxn.date);
1021
- return { needsClearedUpdate, needsDateUpdate };
1143
+ function computeUpdateFlags(
1144
+ match: TransactionMatch,
1145
+ params: ReconcileAccountRequest,
1146
+ ): UpdateFlags {
1147
+ const ynabTxn = match.ynabTransaction;
1148
+ const bankTxn = match.bankTransaction;
1149
+ if (!ynabTxn) {
1150
+ return { needsClearedUpdate: false, needsDateUpdate: false };
1151
+ }
1152
+ const needsClearedUpdate = Boolean(
1153
+ params.auto_update_cleared_status && ynabTxn.cleared !== "cleared",
1154
+ );
1155
+ const needsDateUpdate = Boolean(
1156
+ params.auto_adjust_dates && ynabTxn.date !== bankTxn.date,
1157
+ );
1158
+ return { needsClearedUpdate, needsDateUpdate };
1022
1159
  }
1023
1160
 
1024
- function updateReason(match: TransactionMatch, flags: UpdateFlags, _currency: string): string {
1025
- const parts: string[] = [];
1026
- if (flags.needsClearedUpdate) {
1027
- parts.push('marked as cleared');
1028
- }
1029
- if (flags.needsDateUpdate) {
1030
- parts.push(`date adjusted to ${match.bankTransaction.date}`);
1031
- }
1032
- return parts.join(', ');
1161
+ function updateReason(
1162
+ match: TransactionMatch,
1163
+ flags: UpdateFlags,
1164
+ _currency: string,
1165
+ ): string {
1166
+ const parts: string[] = [];
1167
+ if (flags.needsClearedUpdate) {
1168
+ parts.push("marked as cleared");
1169
+ }
1170
+ if (flags.needsDateUpdate) {
1171
+ parts.push(`date adjusted to ${match.bankTransaction.date}`);
1172
+ }
1173
+ return parts.join(", ");
1033
1174
  }
1034
1175
 
1035
1176
  async function buildBalanceReconciliation(args: {
1036
- ynabAPI: ynab.API;
1037
- budgetId: string;
1038
- accountId: string;
1039
- statementDate: string;
1040
- statementBalance: number;
1041
- analysis: ReconciliationAnalysis;
1177
+ ynabAPI: ynab.API;
1178
+ budgetId: string;
1179
+ accountId: string;
1180
+ statementDate: string;
1181
+ statementBalance: number;
1182
+ analysis: ReconciliationAnalysis;
1042
1183
  }) {
1043
- const { ynabAPI, budgetId, accountId, statementDate, statementBalance } = args;
1044
- const ynabMilli = await clearedBalanceAsOf(ynabAPI, budgetId, accountId, statementDate);
1045
- const bankMilli = toMilli(statementBalance);
1046
- const discrepancy = bankMilli - ynabMilli;
1047
- const status = discrepancy === 0 ? 'PERFECTLY_RECONCILED' : 'DISCREPANCY_FOUND';
1048
-
1049
- const precision_calculations = {
1050
- bank_statement_balance_milliunits: bankMilli,
1051
- ynab_calculated_balance_milliunits: ynabMilli,
1052
- discrepancy_milliunits: discrepancy,
1053
- discrepancy_dollars: discrepancy / 1000,
1054
- };
1055
-
1056
- const discrepancy_analysis = discrepancy === 0 ? undefined : buildLikelyCauses(discrepancy);
1057
-
1058
- const result: {
1059
- status: string;
1060
- precision_calculations: typeof precision_calculations;
1061
- discrepancy_analysis?: ReturnType<typeof buildLikelyCauses>;
1062
- final_verification: {
1063
- balance_matches_exactly: boolean;
1064
- all_transactions_accounted: boolean;
1065
- audit_trail_complete: boolean;
1066
- reconciliation_complete: boolean;
1067
- };
1068
- } = {
1069
- status,
1070
- precision_calculations,
1071
- final_verification: {
1072
- balance_matches_exactly: discrepancy === 0,
1073
- all_transactions_accounted: discrepancy === 0,
1074
- audit_trail_complete: discrepancy === 0,
1075
- reconciliation_complete: discrepancy === 0,
1076
- },
1077
- };
1078
-
1079
- if (discrepancy_analysis !== undefined) {
1080
- result.discrepancy_analysis = discrepancy_analysis;
1081
- }
1082
-
1083
- return result;
1184
+ const { ynabAPI, budgetId, accountId, statementDate, statementBalance } =
1185
+ args;
1186
+ const ynabMilli = await clearedBalanceAsOf(
1187
+ ynabAPI,
1188
+ budgetId,
1189
+ accountId,
1190
+ statementDate,
1191
+ );
1192
+ const bankMilli = toMilli(statementBalance);
1193
+ const discrepancy = bankMilli - ynabMilli;
1194
+ const status =
1195
+ discrepancy === 0 ? "PERFECTLY_RECONCILED" : "DISCREPANCY_FOUND";
1196
+
1197
+ const precision_calculations = {
1198
+ bank_statement_balance_milliunits: bankMilli,
1199
+ ynab_calculated_balance_milliunits: ynabMilli,
1200
+ discrepancy_milliunits: discrepancy,
1201
+ discrepancy_dollars: discrepancy / 1000,
1202
+ };
1203
+
1204
+ const discrepancy_analysis =
1205
+ discrepancy === 0 ? undefined : buildLikelyCauses(discrepancy);
1206
+
1207
+ const result: {
1208
+ status: string;
1209
+ precision_calculations: typeof precision_calculations;
1210
+ discrepancy_analysis?: ReturnType<typeof buildLikelyCauses>;
1211
+ final_verification: {
1212
+ balance_matches_exactly: boolean;
1213
+ all_transactions_accounted: boolean;
1214
+ audit_trail_complete: boolean;
1215
+ reconciliation_complete: boolean;
1216
+ };
1217
+ } = {
1218
+ status,
1219
+ precision_calculations,
1220
+ final_verification: {
1221
+ balance_matches_exactly: discrepancy === 0,
1222
+ all_transactions_accounted: discrepancy === 0,
1223
+ audit_trail_complete: discrepancy === 0,
1224
+ reconciliation_complete: discrepancy === 0,
1225
+ },
1226
+ };
1227
+
1228
+ if (discrepancy_analysis !== undefined) {
1229
+ result.discrepancy_analysis = discrepancy_analysis;
1230
+ }
1231
+
1232
+ return result;
1084
1233
  }
1085
1234
 
1086
1235
  async function clearedBalanceAsOf(
1087
- api: ynab.API,
1088
- budgetId: string,
1089
- accountId: string,
1090
- dateISO: string,
1236
+ api: ynab.API,
1237
+ budgetId: string,
1238
+ accountId: string,
1239
+ dateISO: string,
1091
1240
  ): Promise<number> {
1092
- const response = await api.transactions.getTransactionsByAccount(budgetId, accountId);
1093
- const asOf = new Date(dateISO);
1094
- const cleared = response.data.transactions.filter(
1095
- (txn) => txn.cleared === 'cleared' && new Date(txn.date) <= asOf,
1096
- );
1097
- const sum = cleared.reduce((acc, txn) => addMilli(acc, txn.amount ?? 0), 0);
1098
- return sum;
1241
+ const response = await api.transactions.getTransactionsByAccount(
1242
+ budgetId,
1243
+ accountId,
1244
+ );
1245
+ const asOf = new Date(dateISO);
1246
+ const cleared = response.data.transactions.filter(
1247
+ (txn) => txn.cleared === "cleared" && new Date(txn.date) <= asOf,
1248
+ );
1249
+ const sum = cleared.reduce((acc, txn) => addMilli(acc, txn.amount ?? 0), 0);
1250
+ return sum;
1099
1251
  }
1100
1252
 
1101
1253
  async function refreshAccountSnapshot(
1102
- api: ynab.API,
1103
- budgetId: string,
1104
- accountId: string,
1254
+ api: ynab.API,
1255
+ budgetId: string,
1256
+ accountId: string,
1105
1257
  ): Promise<AccountSnapshot> {
1106
- const accountsApi = api.accounts as typeof api.accounts & {
1107
- getAccount?: (budgetId: string, accountId: string) => Promise<ynab.AccountResponse>;
1108
- };
1109
- const response = accountsApi.getAccount
1110
- ? await accountsApi.getAccount(budgetId, accountId)
1111
- : await accountsApi.getAccountById(budgetId, accountId);
1112
- const account = response.data.account;
1113
- return {
1114
- balance: account.balance,
1115
- cleared_balance: account.cleared_balance,
1116
- uncleared_balance: account.uncleared_balance,
1117
- };
1258
+ const accountsApi = api.accounts as typeof api.accounts & {
1259
+ getAccount?: (
1260
+ budgetId: string,
1261
+ accountId: string,
1262
+ ) => Promise<ynab.AccountResponse>;
1263
+ };
1264
+ const response = accountsApi.getAccount
1265
+ ? await accountsApi.getAccount(budgetId, accountId)
1266
+ : await accountsApi.getAccountById(budgetId, accountId);
1267
+ const account = response.data.account;
1268
+ return {
1269
+ balance: account.balance,
1270
+ cleared_balance: account.cleared_balance,
1271
+ uncleared_balance: account.uncleared_balance,
1272
+ };
1118
1273
  }
1119
1274
 
1120
1275
  function buildLikelyCauses(discrepancyMilli: number) {
1121
- const causes = [] as {
1122
- cause_type: string;
1123
- description: string;
1124
- confidence: number;
1125
- amount_milliunits: number;
1126
- suggested_resolution: string;
1127
- evidence: unknown[];
1128
- }[];
1129
-
1130
- const abs = Math.abs(discrepancyMilli);
1131
- if (abs % 1000 === 0 || abs % 500 === 0) {
1132
- causes.push({
1133
- cause_type: 'bank_fee',
1134
- description: 'Round amount suggests a bank fee or interest adjustment.',
1135
- confidence: 0.8,
1136
- amount_milliunits: discrepancyMilli,
1137
- suggested_resolution:
1138
- discrepancyMilli < 0
1139
- ? 'Create bank fee transaction and mark cleared'
1140
- : 'Record interest income',
1141
- evidence: [],
1142
- });
1143
- }
1144
-
1145
- return causes.length > 0
1146
- ? {
1147
- confidence_level: Math.max(...causes.map((cause) => cause.confidence)),
1148
- likely_causes: causes,
1149
- risk_assessment: 'LOW',
1150
- }
1151
- : undefined;
1276
+ const causes = [] as {
1277
+ cause_type: string;
1278
+ description: string;
1279
+ confidence: number;
1280
+ amount_milliunits: number;
1281
+ suggested_resolution: string;
1282
+ evidence: unknown[];
1283
+ }[];
1284
+
1285
+ const abs = Math.abs(discrepancyMilli);
1286
+ if (abs % 1000 === 0 || abs % 500 === 0) {
1287
+ causes.push({
1288
+ cause_type: "bank_fee",
1289
+ description: "Round amount suggests a bank fee or interest adjustment.",
1290
+ confidence: 0.8,
1291
+ amount_milliunits: discrepancyMilli,
1292
+ suggested_resolution:
1293
+ discrepancyMilli < 0
1294
+ ? "Create bank fee transaction and mark cleared"
1295
+ : "Record interest income",
1296
+ evidence: [],
1297
+ });
1298
+ }
1299
+
1300
+ return causes.length > 0
1301
+ ? {
1302
+ confidence_level: Math.max(...causes.map((cause) => cause.confidence)),
1303
+ likely_causes: causes,
1304
+ risk_assessment: "LOW",
1305
+ }
1306
+ : undefined;
1152
1307
  }
1153
1308
 
1154
1309
  function buildRecommendations(args: {
1155
- summary: ExecutionSummary;
1156
- params: ReconcileAccountRequest;
1157
- analysis: ReconciliationAnalysis;
1158
- balanceChangeMilli: number;
1159
- currencyCode: string;
1310
+ summary: ExecutionSummary;
1311
+ params: ReconcileAccountRequest;
1312
+ analysis: ReconciliationAnalysis;
1313
+ balanceChangeMilli: number;
1314
+ currencyCode: string;
1160
1315
  }): string[] {
1161
- const { summary, params, analysis, balanceChangeMilli, currencyCode } = args;
1162
- const recommendations: string[] = [];
1163
-
1164
- if (summary.dates_adjusted > 0) {
1165
- recommendations.push(
1166
- `✅ Adjusted ${summary.dates_adjusted} transaction date(s) to match bank statement dates`,
1167
- );
1168
- }
1169
-
1170
- if (analysis.summary.unmatched_bank > 0 && !params.auto_create_transactions) {
1171
- recommendations.push(
1172
- `Consider enabling auto_create_transactions to automatically create ${analysis.summary.unmatched_bank} missing transaction(s)`,
1173
- );
1174
- }
1175
-
1176
- if (!params.auto_adjust_dates && analysis.auto_matches.length > 0) {
1177
- recommendations.push(
1178
- 'Consider enabling auto_adjust_dates to align YNAB dates with bank statement dates',
1179
- );
1180
- }
1181
-
1182
- if (analysis.summary.unmatched_ynab > 0) {
1183
- recommendations.push(
1184
- `${analysis.summary.unmatched_ynab} transaction(s) exist in YNAB but not on the bank statement — review for duplicates or pending items`,
1185
- );
1186
- }
1187
-
1188
- if (params.dry_run) {
1189
- recommendations.push('Dry run only — re-run with dry_run=false to apply these changes');
1190
- }
1191
-
1192
- if (Math.abs(balanceChangeMilli) > MONEY_EPSILON_MILLI) {
1193
- recommendations.push(
1194
- `Account balance changed by ${toMoneyValue(balanceChangeMilli, currencyCode).value_display} during reconciliation`,
1195
- );
1196
- }
1197
-
1198
- return recommendations;
1316
+ const { summary, params, analysis, balanceChangeMilli, currencyCode } = args;
1317
+ const recommendations: string[] = [];
1318
+
1319
+ if (summary.dates_adjusted > 0) {
1320
+ recommendations.push(
1321
+ `✅ Adjusted ${summary.dates_adjusted} transaction date(s) to match bank statement dates`,
1322
+ );
1323
+ }
1324
+
1325
+ if (analysis.summary.unmatched_bank > 0 && !params.auto_create_transactions) {
1326
+ recommendations.push(
1327
+ `Consider enabling auto_create_transactions to automatically create ${analysis.summary.unmatched_bank} missing transaction(s)`,
1328
+ );
1329
+ }
1330
+
1331
+ if (!params.auto_adjust_dates && analysis.auto_matches.length > 0) {
1332
+ recommendations.push(
1333
+ "Consider enabling auto_adjust_dates to align YNAB dates with bank statement dates",
1334
+ );
1335
+ }
1336
+
1337
+ if (analysis.summary.unmatched_ynab > 0) {
1338
+ recommendations.push(
1339
+ `${analysis.summary.unmatched_ynab} transaction(s) exist in YNAB but not on the bank statement — review for duplicates or pending items`,
1340
+ );
1341
+ }
1342
+
1343
+ if (params.dry_run) {
1344
+ recommendations.push(
1345
+ "Dry run only — re-run with dry_run=false to apply these changes",
1346
+ );
1347
+ }
1348
+
1349
+ if (Math.abs(balanceChangeMilli) > MONEY_EPSILON_MILLI) {
1350
+ recommendations.push(
1351
+ `Account balance changed by ${toMoneyValue(balanceChangeMilli, currencyCode).value_display} during reconciliation`,
1352
+ );
1353
+ }
1354
+
1355
+ return recommendations;
1199
1356
  }
1200
1357
 
1201
1358
  export type { ExecutionResult as LegacyReconciliationResult };
1202
1359
 
1203
1360
  function resolveStatementBalanceMilli(
1204
- balanceInfo: ReconciliationAnalysis['balance_info'],
1205
- provided?: number,
1361
+ balanceInfo: ReconciliationAnalysis["balance_info"],
1362
+ provided?: number,
1206
1363
  ): number {
1207
- if (typeof provided === 'number' && Number.isFinite(provided)) {
1208
- return toMilli(provided);
1209
- }
1210
-
1211
- return (
1212
- extractMoneyValue(balanceInfo?.target_statement) ??
1213
- extractMoneyValue(balanceInfo?.current_cleared) ??
1214
- 0
1215
- );
1364
+ if (typeof provided === "number" && Number.isFinite(provided)) {
1365
+ return toMilli(provided);
1366
+ }
1367
+
1368
+ return (
1369
+ extractMoneyValue(balanceInfo?.target_statement) ??
1370
+ extractMoneyValue(balanceInfo?.current_cleared) ??
1371
+ 0
1372
+ );
1216
1373
  }
1217
1374
 
1218
1375
  function extractMoneyValue(value: unknown): number | undefined {
1219
- if (typeof value === 'number' && Number.isFinite(value)) {
1220
- return toMilli(value);
1221
- }
1222
- if (
1223
- value &&
1224
- typeof value === 'object' &&
1225
- 'value_milliunits' in value &&
1226
- typeof (value as { value_milliunits: unknown }).value_milliunits === 'number'
1227
- ) {
1228
- return (value as { value_milliunits: number }).value_milliunits;
1229
- }
1230
- return undefined;
1376
+ if (typeof value === "number" && Number.isFinite(value)) {
1377
+ return toMilli(value);
1378
+ }
1379
+ if (
1380
+ value &&
1381
+ typeof value === "object" &&
1382
+ "value_milliunits" in value &&
1383
+ typeof (value as { value_milliunits: unknown }).value_milliunits ===
1384
+ "number"
1385
+ ) {
1386
+ return (value as { value_milliunits: number }).value_milliunits;
1387
+ }
1388
+ return undefined;
1231
1389
  }
1232
1390
 
1233
1391
  function sortByDateDescending<T extends { date: string }>(items: T[]): T[] {
1234
- return [...items].sort((a, b) => compareDates(b.date, a.date));
1392
+ return [...items].sort((a, b) => compareDates(b.date, a.date));
1235
1393
  }
1236
1394
 
1237
- function sortMatchesByBankDateDescending(matches: TransactionMatch[]): TransactionMatch[] {
1238
- return [...matches].sort((a, b) => compareDates(b.bankTransaction.date, a.bankTransaction.date));
1395
+ function sortMatchesByBankDateDescending(
1396
+ matches: TransactionMatch[],
1397
+ ): TransactionMatch[] {
1398
+ return [...matches].sort((a, b) =>
1399
+ compareDates(b.bankTransaction.date, a.bankTransaction.date),
1400
+ );
1239
1401
  }
1240
1402
 
1241
1403
  function compareDates(dateA: string, dateB: string): number {
1242
- return toChronoValue(dateA) - toChronoValue(dateB);
1404
+ return toChronoValue(dateA) - toChronoValue(dateB);
1243
1405
  }
1244
1406
 
1245
1407
  function toChronoValue(date: string): number {
1246
- const parsed = Date.parse(date);
1247
- if (!Number.isNaN(parsed)) {
1248
- return parsed;
1249
- }
1250
- const fallback = Date.parse(`${date}T00:00:00Z`);
1251
- return Number.isNaN(fallback) ? 0 : fallback;
1408
+ const parsed = Date.parse(date);
1409
+ if (!Number.isNaN(parsed)) {
1410
+ return parsed;
1411
+ }
1412
+ const fallback = Date.parse(`${date}T00:00:00Z`);
1413
+ return Number.isNaN(fallback) ? 0 : fallback;
1252
1414
  }