farseer-cli 1.19.0 → 1.20.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 (155) hide show
  1. package/README.md +31 -8
  2. package/dist/commands/app.js +42 -3
  3. package/dist/commands/app.js.map +1 -1
  4. package/dist/commands/cells.js +120 -85
  5. package/dist/commands/cells.js.map +1 -1
  6. package/dist/commands/dashboards.js +246 -12
  7. package/dist/commands/dashboards.js.map +1 -1
  8. package/dist/commands/dimensions.js +52 -27
  9. package/dist/commands/dimensions.js.map +1 -1
  10. package/dist/commands/docs.js +82 -0
  11. package/dist/commands/docs.js.map +6 -0
  12. package/dist/commands/files.js +49 -0
  13. package/dist/commands/files.js.map +2 -2
  14. package/dist/commands/folders.js +76 -0
  15. package/dist/commands/folders.js.map +6 -0
  16. package/dist/commands/formula.js +38 -30
  17. package/dist/commands/formula.js.map +1 -1
  18. package/dist/commands/install.js +32 -30
  19. package/dist/commands/install.js.map +2 -2
  20. package/dist/commands/model.js +30 -5
  21. package/dist/commands/model.js.map +2 -2
  22. package/dist/commands/push.js +5 -3
  23. package/dist/commands/push.js.map +1 -1
  24. package/dist/commands/sheets.js +289 -52
  25. package/dist/commands/sheets.js.map +1 -1
  26. package/dist/commands/skills.js +157 -0
  27. package/dist/commands/skills.js.map +6 -0
  28. package/dist/commands/variables.js +246 -82
  29. package/dist/commands/variables.js.map +1 -1
  30. package/dist/index.js +93 -13
  31. package/dist/index.js.map +1 -1
  32. package/dist/mcp/resources/documentation.js +19 -12
  33. package/dist/mcp/resources/documentation.js.map +1 -1
  34. package/dist/mcp/tools/appTools.js +1 -7
  35. package/dist/mcp/tools/appTools.js.map +1 -1
  36. package/dist/mcp/tools/cellTools.js +6 -19
  37. package/dist/mcp/tools/cellTools.js.map +1 -1
  38. package/dist/mcp/tools/dashboardTools.js +30 -412
  39. package/dist/mcp/tools/dashboardTools.js.map +2 -2
  40. package/dist/mcp/tools/dataTools.js +15 -146
  41. package/dist/mcp/tools/dataTools.js.map +1 -1
  42. package/dist/mcp/tools/folderTools.js +12 -78
  43. package/dist/mcp/tools/folderTools.js.map +1 -1
  44. package/dist/mcp/tools/modelTools.js +23 -372
  45. package/dist/mcp/tools/modelTools.js.map +1 -1
  46. package/dist/mcp/tools/pipelineContract.js +5 -1
  47. package/dist/mcp/tools/pipelineContract.js.map +1 -1
  48. package/dist/mcp/tools/runTools.js +8 -69
  49. package/dist/mcp/tools/runTools.js.map +1 -1
  50. package/dist/mcp/tools/sheetTools.js +14 -264
  51. package/dist/mcp/tools/sheetTools.js.map +1 -1
  52. package/dist/mcp/tools/syncTools.js +5 -1
  53. package/dist/mcp/tools/syncTools.js.map +1 -1
  54. package/dist/mcp/utils/operationResponse.js +41 -0
  55. package/dist/mcp/utils/operationResponse.js.map +6 -0
  56. package/dist/operations/cells.js +444 -0
  57. package/dist/operations/cells.js.map +6 -0
  58. package/dist/operations/dashboards.js +357 -0
  59. package/dist/operations/dashboards.js.map +6 -0
  60. package/dist/operations/data.js +206 -0
  61. package/dist/operations/data.js.map +6 -0
  62. package/dist/operations/errors.js +64 -0
  63. package/dist/operations/errors.js.map +6 -0
  64. package/dist/operations/exportModel.js +59 -0
  65. package/dist/operations/exportModel.js.map +6 -0
  66. package/dist/operations/folderPath.js +128 -0
  67. package/dist/operations/folderPath.js.map +6 -0
  68. package/dist/operations/folders.js +80 -0
  69. package/dist/operations/folders.js.map +6 -0
  70. package/dist/operations/install.js +109 -0
  71. package/dist/operations/install.js.map +6 -0
  72. package/dist/operations/model.js +271 -0
  73. package/dist/operations/model.js.map +6 -0
  74. package/dist/operations/sheets.js +428 -0
  75. package/dist/operations/sheets.js.map +6 -0
  76. package/dist/pipeline/failStep.js +43 -0
  77. package/dist/pipeline/failStep.js.map +6 -0
  78. package/dist/pipeline/generated-reference.json +10 -10
  79. package/dist/pipeline/queryWorker.js +3 -1
  80. package/dist/pipeline/queryWorker.js.map +1 -1
  81. package/dist/pipeline/requests.js +3 -32
  82. package/dist/pipeline/requests.js.map +1 -1
  83. package/dist/pipeline/service.js +46 -2
  84. package/dist/pipeline/service.js.map +1 -1
  85. package/dist/services/appSyncService.js +59 -9
  86. package/dist/services/appSyncService.js.map +1 -1
  87. package/dist/services/apps.js +10 -0
  88. package/dist/services/apps.js.map +1 -1
  89. package/dist/services/farseerService.js +28 -6
  90. package/dist/services/farseerService.js.map +1 -1
  91. package/dist/skills/bundled.js +106 -0
  92. package/dist/skills/bundled.js.map +6 -0
  93. package/dist/skills/install.js +257 -0
  94. package/dist/skills/install.js.map +6 -0
  95. package/dist/skills/pipeline.js +10 -18
  96. package/dist/skills/pipeline.js.map +1 -1
  97. package/dist/utils/agentSandbox.js +1 -1
  98. package/dist/utils/entityResolver.js +72 -25
  99. package/dist/utils/entityResolver.js.map +1 -1
  100. package/dist/utils/errorFormatter.js +13 -0
  101. package/dist/utils/errorFormatter.js.map +1 -1
  102. package/dist/utils/formulaFilters.js +11 -2
  103. package/dist/utils/formulaFilters.js.map +1 -1
  104. package/dist/utils/operationCli.js +102 -0
  105. package/dist/utils/operationCli.js.map +6 -0
  106. package/docs/cli-guide.md +181 -1
  107. package/docs/pipeline-reference.md +13 -7
  108. package/docs/troubleshooting.md +18 -0
  109. package/package.json +1 -1
  110. package/skills/farseer-app-dev/SKILL.md +131 -0
  111. package/skills/farseer-app-dev/assignments.md +185 -0
  112. package/skills/farseer-app-dev/farseer-client-api-guide.md +907 -0
  113. package/skills/farseer-app-dev/farseer-client-release-notes.md +212 -0
  114. package/skills/farseer-app-dev/working-patterns.md +876 -0
  115. package/skills/farseer-authenticate-to-tenant/SKILL.md +60 -0
  116. package/skills/farseer-authenticate-to-tenant/app-authentication.md +91 -0
  117. package/skills/farseer-author-import-job/SKILL.md +114 -0
  118. package/skills/farseer-author-import-job/cell-writes.md +530 -0
  119. package/skills/farseer-author-import-job/deploying-a-farseer-app.md +383 -0
  120. package/skills/farseer-author-import-job/import-job-pattern.md +232 -0
  121. package/skills/farseer-build-model/SKILL.md +122 -0
  122. package/skills/farseer-build-model/formula-grain-alignment.md +612 -0
  123. package/skills/farseer-build-model/naming-conventions.md +435 -0
  124. package/skills/farseer-build-model/sparse-formulas-pattern.md +330 -0
  125. package/skills/farseer-build-pipeline/SKILL.md +190 -0
  126. package/skills/farseer-build-pipeline/erp-sales/README.md +108 -0
  127. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_invoice_lines.yaml +23 -0
  128. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_invoice_lines_raw.yaml +23 -0
  129. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_item_product_map.yaml +17 -0
  130. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_item_product_map_raw.yaml +11 -0
  131. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/gold/erp_sales_cells.yaml +40 -0
  132. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_lines.yaml +35 -0
  133. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_recon.yaml +43 -0
  134. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_unmapped_items.yaml +28 -0
  135. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/main.py +49 -0
  136. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/in/erp_sales_cells.yaml +17 -0
  137. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/in/erp_sales_cells_raw.yaml +14 -0
  138. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/load/erp_sales_to_model.yaml +47 -0
  139. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/main.py +44 -0
  140. package/skills/farseer-build-pipeline/erp-sales/item_to_product.csv +5 -0
  141. package/skills/farseer-build-pipeline/pipeline-app-failure-report-pattern.md +103 -0
  142. package/skills/farseer-build-pipeline/pipeline-source-encoding.md +68 -0
  143. package/skills/farseer-dashboards/SKILL.md +1 -1
  144. package/skills/farseer-help-center/SKILL.md +66 -0
  145. package/skills/farseer-modify-model/SKILL.md +158 -0
  146. package/skills/farseer-modify-model/model-validation-pattern.md +251 -0
  147. package/skills/farseer-optimize-model/SKILL.md +314 -0
  148. package/skills/farseer-optimize-model/stats-dump.ts +139 -0
  149. package/skills/farseer-pnl/SKILL.md +220 -0
  150. package/skills/farseer-review-model/SKILL.md +172 -0
  151. package/skills/farseer-run-apps/SKILL.md +102 -0
  152. package/skills/pipeline-authoring/SKILL.md +4 -4
  153. package/skills/pipeline-authoring/practices.md +15 -3
  154. package/dist/services/agentApprovalFetch.js +0 -90
  155. package/dist/services/agentApprovalFetch.js.map +0 -6
@@ -0,0 +1,158 @@
1
+ ---
2
+ name: farseer-modify-model
3
+ description: >-
4
+ How to change a Farseer model that is already in use without breaking it: decide the risk tier, snapshot the model and resolve drift first, find what consumes the change (OUT variables, sheets, apps, other tenants), keep OUT interfaces stable, backtest against the snapshot, and promote dev → test → production with a rollback point. Use when the user asks to change, fix, refactor or extend an existing model or a variable's grain, formula or properties, or asks what a change would affect or how risky it is. The farseer-model skill covers how to write the change.
5
+ metadata:
6
+ version: "1"
7
+ domain: modeling
8
+ ---
9
+
10
+ # Modify an Existing Farseer Model
11
+
12
+ ## When this skill applies
13
+
14
+ Any change to a model that already exists — on a tenant, in git, or both. Risk rises with how live the model is: a production tenant that planners work in carries the most.
15
+
16
+ Do **not** use this skill for:
17
+ - A new model from scratch → the `farseer-build-model` skill
18
+ - How to write a variable, table, member, link or cell → the `farseer-model` skill
19
+
20
+ The point of this skill is **enforcing the sequence** so impact analysis isn't skipped and prod isn't touched ahead of dev/test.
21
+
22
+ This skill has no write steps: make the change itself through the `farseer-model` skill. `farseer code` (Experimental) is the future path for a whole-model change.
23
+
24
+ The naming, sparse and grain reference files it links live in the `farseer-build-model` skill. If one does not open because this skill was installed on its own, `farseer skills show farseer-build-model --file <file>` prints it.
25
+
26
+ ## Risk tier (decide first — drives discipline)
27
+
28
+ | Tier | Triggers | Discipline |
29
+ |---|---|---|
30
+ | **T1** — low | Fix within the agreed scope; no change to ` OUT` interface; not a production tenant in use | Full skill, brisk |
31
+ | **T2** — medium | Scope extension; new ` OUT` variable; new module on existing model; new dimension link | Full skill + downstream interface review |
32
+ | **T3** — high | Breaking change to existing ` OUT` (grain / type / formula contract); change to a live production tenant; cross-tenant ripple potential | Full skill + telling the model's users first + dual-environment validation + rollback plan written before the change |
33
+
34
+ If you can't decide between two tiers, take the higher one. Demoting tier mid-flight is fine; promoting after the change is in prod is not.
35
+
36
+ ## Inputs to gather first
37
+
38
+ 1. **The ask, precisely** — one sentence: *"Add `Months` to the tables of `Gross Margin OUT`"* / *"Split `Freight Cost IN` by warehouse"*.
39
+ 2. **How live is the target** — a production tenant that planners work in means the model is live; the T3 risk premium applies.
40
+ 3. **Target instance(s)** — which env will the work be done on? Always start dev. If the project has only prod (no dev/test), **stop and flag** — that's a structural gap, raise before proceeding.
41
+ 4. **Live-state reads** — the impact survey uses tenant reads: `farseer model --variables` / `--variable "<Var>"` / `--table "<Table>"` (+ trailing `<tenant>` arg), or a fresh `farseer model --json <tenant>` when you need the full picture. If the CLI session is missing, use the `farseer-authenticate-to-tenant` skill or ask the user to paste live state.
42
+
43
+ ## Procedure
44
+
45
+ ### 1. Snapshot before touching anything (mandatory)
46
+
47
+ Capture the current structure and commit it before you change anything:
48
+
49
+ ```bash
50
+ farseer model --json --include-members <tenant> > snapshots/YYYY-MM-DD_pre-<label>.json
51
+ ```
52
+
53
+ `--include-members` adds every table's member rows, so a changed member or FK value shows in the diff too (slow on a large tenant).
54
+
55
+ Label it so it ties to this change:
56
+
57
+ - `YYYY-MM-DD_pre-<change-slug>.json` — for a fix or a requested change
58
+ - `YYYY-MM-DD_pre-refactor-<topic>.json` — for refactors
59
+
60
+ This snapshot is **both** the rollback point and the backtest baseline. Without it, step 5 has no anchor and step 7 has no diff target. Do not skip.
61
+
62
+ **What makes a snapshot a baseline:**
63
+
64
+ - **It includes formulas.** A read where every variable comes back without a formula is a failed read, not a baseline: the first model read on a cold tenant can return `formula: null` for every variable. Read again, and count the formulas before you trust the file.
65
+ - **It is a full read.** If the command prints `/model/export failed …; falling back to initTagMap`, the CLI read the model another way, without fixed members, notes, folder or `zConstraint` (every `members` empty, no `zConstraint` field): it is not a baseline. Read again.
66
+ - **Structure only — never cell data.** Values belong in the backtest (step 3), not in the snapshot.
67
+ - **Never overwrite a dated snapshot** with the same label — add a suffix (`-v2`) instead.
68
+ - **Diff it against the previous snapshot by category** when the diff is large: new variables, removed variables (flag these as potential breakage), changed formulas, new / removed / changed tables, and new / removed FK links (graph-shape changes — high impact).
69
+ - **A difference that no committed change explains** means someone edited the tenant outside the usual workflow: **surface this prominently** — it's a workflow-drift warning, not just a normal diff.
70
+
71
+ **Then diff the tenant against git and resolve every difference before you edit** — and again right before any push or promotion from git to a live tenant. Compare the fresh snapshot with the model as committed in git (and, for a promotion, with the target tenant's own snapshot). Every difference is drift in one of three shapes: in git but not on the tenant (committed, never pushed), on the tenant but not in git (edited on the tenant, never committed), or different in both. A push from git silently reverts the second kind and a pull silently loses the first. **Git wins only after you know why the tenant differs** — ask the person who edited the tenant, and commit what should stay before you overwrite it.
72
+
73
+ ### 2. Impact analysis (mandatory — easiest step to skip, highest damage when skipped)
74
+
75
+ Read the relevant reference first (the naming, sparse and grain files of the `farseer-build-model` skill). Then survey what consumes what you're about to change:
76
+
77
+ | Downstream surface | How to survey (reads only) |
78
+ |---|---|
79
+ | ` OUT` variables that reference the changed variable in formulas | search the formulas in the step 1 snapshot for the variable's name in both forms: quoted (`"Gross Margin OUT"`) and, when the name is a plain identifier (letters, digits, `_`, `$`), bare (`Gross_Margin_OUT`) — the model export writes such a name without quotes. Only zero hits in both forms means no formula reads it. `farseer model --variable "<Var>" <tenant>` shows one variable's formula |
80
+ | Sheets in the tenant | `farseer sheets list <tenant>`, then `farseer sheets rows "<Sheet>" <tenant> --json` for each candidate, which names each row's variable; a manual check in the Farseer UI where layout matters |
81
+ | Deployed apps that import or export the variable | `farseer app list --details <tenant>`; `farseer pull <org> <tenant>` in a scratch folder, then search `farseer-apps/<tenant>/` for the variable name |
82
+ | Cross-tenant ripple | Ask which other tenants read from or feed this one (a group split into several legal-entity tenants, or two tenants coupled by a data flow) and trace those flows before assuming the change is local |
83
+
84
+ **Interface preservation discipline (T2 / T3):** if the change touches an ` OUT` variable's grain, type, or formula contract, that's a **breaking** change for every downstream consumer. Either:
85
+
86
+ - Adapt the change so the ` OUT` interface stays stable (preferred), or
87
+ - Plan downstream migration in the same change (and surface that work in the impact report)
88
+
89
+ Surface the impact summary to the user **before** changing anything: *"This touches 4 OUT variables, 2 sheets and 1 deployed app. The reporting tenant reads two of them. Tier elevated to T2."*
90
+
91
+ ### 3. Identify validation regime + plan tests
92
+
93
+ Modification of an existing deployed model is always **backtest** per [`model-validation-pattern.md`](model-validation-pattern.md). Optionally + functional if the change introduces new scenarios that didn't exist before.
94
+
95
+ Plan:
96
+
97
+ - **Backtest scenario** — baseline snapshot from step 1 + a small set of cells / variables to diff after the change. Expected deltas listed explicitly (this is what we're trying to change); everything else should be unchanged.
98
+ - **Functional scenarios** (if applicable) — new inputs + expected outputs for the new behavior.
99
+
100
+ Read the baseline values of those cells now, before the change, and keep them with the snapshot: `farseer formula eval '<formula>' <tenant> --json` for one cell, or `farseer variable export "<Var>" --filter Versions=Actual --filter Months=<month> --output pre.csv` for a slice (with `FARSEER_TENANT` set). `formula eval` answers values, not grain questions ([`formula-grain-alignment.md`](../farseer-build-model/formula-grain-alignment.md) §4).
101
+
102
+ ### 4. Implement on **dev only**
103
+
104
+ Make the change on the **dev** tenant, through the `farseer-model` skill. Never start the change on a test or prod tenant.
105
+
106
+ - Run the naming checks of the `farseer-build-model` skill on every variable you add or rename, and on every variable whose `readonly`, folder or formula you change
107
+ - Read the variable's current state first (`farseer model --variable "<Var>" <tenant>`) if it's been a while — don't rely on stale memory of the model state
108
+ - Keep the change atomic: one purpose per push if possible. Bundling unrelated fixes is how `pre-` baselines stop being useful
109
+ - Before the change deletes an input variable, export its data, scoped (`farseer variable export "<Var>" --filter Versions=<version> --filter Years=<year> --output pre-<var>-<version>-<year>.csv`, one file per version and year it holds, with `FARSEER_TENANT` set), and keep it with the `pre-` snapshot: the snapshot is structure only, and a rollback loads the values back from this export
110
+
111
+ **Performance checklist before push** — quick self-check on any new/changed formula, not a full triage pass:
112
+
113
+ - Broadcasting onto `Versions`? Use a condition (`if(eq(carrier, 1), X)`), never a product (`X * carrier`) — a cost difference of tens of times (the two are equal only when the carrier holds `1` or null, never a stored `0`), [`formula-grain-alignment.md` §7](../farseer-build-model/formula-grain-alignment.md#7--avoid-it-at-the-start-design-time-performance-rules).
114
+ - Never combine a `Versions` broadcast and an FK hop in one product — split them.
115
+ - Lifting a coarse operand across an FK hop and the result must stay sparse? `if(hasValue(X), X)`, not `0 + X` (phantom zeros — [`sparse-formulas-pattern.md`](../farseer-build-model/sparse-formulas-pattern.md)).
116
+ - Check the change cannot make a formula produce a dense layout of cells (operand cells × every member it broadcasts over, incl. `Versions` × periods) — tens of millions is already the problem class; prevent it in the model (`formula-grain-alignment.md`, "Dense cell production").
117
+
118
+ ### 5. Read back on dev
119
+
120
+ For modification work, **always start with dev**.
121
+
122
+ After push:
123
+
124
+ - `farseer model --variables <tenant>` — does the tenant accept every symbol?
125
+ - Take a fresh snapshot (`farseer model --json --include-members <tenant>`) — does what we wrote match what the tenant has?
126
+ - Diff the fresh snapshot vs the `pre-` snapshot — confirm only the intended changes appear
127
+
128
+ ### 6. Run backtest + functional tests
129
+
130
+ Read the cells planned in step 3 again and compare them with the baseline values. Outcome:
131
+
132
+ - **`pass`** — diff matches expected deltas exactly; nothing else moved
133
+ - **`pass-with-expected-deltas`** — diff matches expected + small documented deltas (e.g. rounding noise, dim reshuffling that's known acceptable)
134
+ - **`fail`** — unexpected deltas; **stop and investigate before any promotion**
135
+
136
+ `fail` doesn't get swept under the rug. Either fix the change, narrow the scope, or surface the surprise to the user.
137
+
138
+ ### 7. Promote dev → test → prod (skip a stage only on the user's explicit confirmation)
139
+
140
+ **Recommended:** nothing reaches production until validated on **both** dev and test. If you deviate, say why, and only on the user's explicit confirmation.
141
+
142
+ Per-step:
143
+
144
+ - **dev → test** — make the same change on test; no CLI command copies a change between tenants, so repeat it and compare a fresh test snapshot with dev's. On test, UAT / key user validates the actual behavior end-to-end (not just the variable existing).
145
+ - **test → prod** — only with **explicit user confirmation** stating the prod target. When tenants are coupled by a data flow, agree the order across them first — never change one half without the other.
146
+ - **Post-prod snapshot** — `farseer model --json --include-members <prod tenant>` saved as `YYYY-MM-DD_post-<label>.json`. This is the next baseline.
147
+
148
+ If at any stage validation fails, **roll back** to the prior snapshot (restore every difference from the `pre-` snapshot — formulas, tables, fixed members, properties, FK columns and their member values — delete the variables the change added and recreate the ones it deleted, through the `farseer-model` skill; the snapshot is structure only, so a recreated input variable comes back empty until you load its values back from the export taken before the delete (`farseer variables import`; convert the export's dimension and value columns into its `--input` JSON); then diff a fresh snapshot against `pre-` and read the restored values back) rather than papering over. The snapshots exist exactly for this.
149
+
150
+ ## Constraints
151
+
152
+ - **Do not** change the model without the `pre-` snapshot in place. The snapshot is the rollback point and the backtest baseline.
153
+ - **Do not** skip the impact analysis (step 2). The most common failure mode for model modifications is silent breakage of a downstream consumer the modifier didn't know existed.
154
+ - **Do not** break an ` OUT` interface contract silently. Either preserve, or plan migration in the same change.
155
+ - **Do not** push to test before dev validates. Do not push to prod before test validates. If the user wants to skip a stage, name the risk — production stability over speed — and go ahead only on their explicit confirmation.
156
+ - **Do not** declare done without a passing backtest. `fail` outcomes get investigated, not papered over.
157
+ - **Do not** start a change on a live production tenant without telling its users first. T3 discipline applies.
158
+ - **Do not** bundle unrelated fixes in one push. One purpose per push. The `pre-` snapshot loses meaning otherwise.
@@ -0,0 +1,251 @@
1
+ # Model validation pattern — tests + backtests
2
+
3
+ How AI agents and human modelers validate a Farseer model: before declaring work done, after any modification, and when bringing a new model up against real client data.
4
+
5
+ This pattern applies whenever a Farseer model is built, modified, or refactored. Cells are named with the formula syntax (`"Table">"Member"`); the `farseer-model` skill has the write requests.
6
+
7
+ ---
8
+
9
+ ## 0. Definition of done — what "validated" actually means
10
+
11
+ A model change is **NOT done** until ALL of these are true for the regime that applies (see §1):
12
+
13
+ **Common to all regimes:**
14
+
15
+ - ✓ The validation regime that applies (functional / backtest / both) is identified in writing
16
+ - ✓ Outcome is recorded as one of: `pending` (scaffolded, not yet run) / `pass` (all instances passed) / `pass-with-expected-deltas` (passed with rationale per delta) / `partial-pass` (multi-instance test where some instances passed and at least one failed) / `fail` (no instances passed) / `baseline` (only for bootstrap)
17
+ - ✓ For `pass-with-expected-deltas`: written rationale per non-zero delta, recorded with the run
18
+ - ✓ For `partial-pass` (a test run on several tenants, where some passed and at least one failed): the status per tenant + rationale for the failing tenant(s)
19
+
20
+ **Regime-specific gates:** see §1 for which gate fires when.
21
+
22
+ **Functional regime — a reviewer can see the result:** give the domain
23
+ reviewer a thin, read-only sheet pinned to the test slice, with one column per
24
+ asserted OUT variable.
25
+
26
+ A functional test runner that passes 81/81 in a terminal a non-technical
27
+ reviewer never sees is not a complete deliverable. The confirmation sheet
28
+ makes the result inspectable on the tenant by the same domain experts
29
+ who own the test data.
30
+
31
+ ---
32
+
33
+ ## 1. Three regimes — which one applies, and when
34
+
35
+ The right validation depends on what kind of change is happening:
36
+
37
+ | Scenario | Regime | Trigger |
38
+ | --- | --- | --- |
39
+ | **A. New model from scratch, no client reference data** | Functional tests | First build; planner has no external truth to compare against yet |
40
+ | **B. Modification of an existing model** | Backtest (regression) | Any non-trivial variable/sheet/table change to a deployed model |
41
+ | **C. New model with client reference data available** | Backtest (validation) | Client provided exports/workbook; model must reproduce them |
42
+
43
+ These are **not mutually exclusive** — a major refactor to a model that has client reference data may need both functional tests (new logic correctness) and backtest (no regression on existing reference).
44
+
45
+ ### 1.A — New model from scratch (functional tests)
46
+
47
+ There is nothing external to compare against. Validation is *common-sense correctness*: do the formulas behave the way they should under known inputs?
48
+
49
+ - Edge cases mandatory: zero, negative, very large input (overflow / division-by-zero classes)
50
+ - Signature checks: revenue stays > 0 and COGS stays ≥ 0 **in storage** (everything is stored positive; a cost turns negative only where a formula presents it as a deduction), equity = assets - liabilities, etc. — each with its own pass/fail status
51
+
52
+ This is **not a unit-test framework** — these are scripted scenarios that an AI agent (or human) walks through deliberately: seed known inputs on a test slice, read the computed outputs back (§4.1), compare within a tolerance.
53
+
54
+ ### 1.B — Modification of an existing model (backtest)
55
+
56
+ If you are touching a model that has been deployed and run for clients, you must guarantee you didn't silently break anything.
57
+
58
+ **If a backtest baseline exists:**
59
+
60
+ - Read the model's outputs again for the latest reference dataset
61
+ - Compute delta against the reference outputs (variable-by-variable)
62
+ - Expected outcome: delta == 0 for every variable that was not intentionally changed
63
+ - For variables that were intentionally changed: document the reason with the run
64
+
65
+ **If NO backtest baseline exists — "bootstrap a baseline" sub-procedure:**
66
+
67
+ This is the common case for older client models that pre-date this pattern. Before making any modification:
68
+
69
+ 1. **Halt the planned change.** Do not modify a single variable or sheet config yet.
70
+ 2. Identify all relevant output variables for the model (typically: P&L final lines, BS final lines, cash flow final lines, key KPIs).
71
+ 3. Read the current production output of those variables with bounded reads, never a whole-variable export on a live tenant: `formula eval` totals per variable, version and period; `cells query --input` for the cells the change touches; `variable export --filter` on one version and month. Dump the whole time horizon only on a dev or test copy.
72
+ 4. Save the reads, with the list of captured variables and of those deliberately left out (intermediate derivatives that are pure functions of captured ones, derivable period aggregates) — e.g. `farseer formula eval '"<Var>"' <tenant> --filter Versions=<version> --filter Months=<month> --json` for a total, `farseer cells query <tenant> --input cells.json` for the touched cells, and `farseer variable export "<Var>" --filter Versions=<version> --filter Months=<month> --output baseline/<var>-<month>.csv` for a slice, with `FARSEER_TENANT` set
73
+ 5. **Commit the baseline as its own step** — separate commit from the modification work that follows. This makes "before" and "after" reviewable.
74
+ 6. Now proceed with the planned modification. After modification, read the same totals, cells and slices again and compare against the baseline.
75
+
76
+ The bootstrap baseline is the contract: "this is what the model produced before I touched it." Without it, claims of "I didn't break anything" are unverifiable.
77
+
78
+ ### 1.C — New model with client reference data (backtest)
79
+
80
+ The client has provided their existing workbook / export — the model must reproduce it cent-perfect (or document tolerances).
81
+
82
+ - One reconciliation row per (variable, period): model output, reference, delta, status
83
+ - Until every row is `pass` (or has a documented rationale as an expected delta), the model is **not deployed to production tenants**
84
+
85
+ ---
86
+
87
+ ## 4. AI agent contract
88
+
89
+ When an AI agent (Claude, Cursor, Copilot, …) is asked to build, modify, or refactor a Farseer model, it MUST:
90
+
91
+ 1. **Before any code change** — identify which regime applies (§1) and state it back to the user.
92
+ 2. **If regime B and no baseline exists** — execute the bootstrap sub-procedure (§1.B). Stop, capture the baseline and commit it as its own step BEFORE touching the modification.
93
+ 3. **Before saying "done"** — verify outcome is `pass` or `pass-with-expected-deltas`. If `fail` or `pending`, the work is NOT done; surface this to the user, don't paper over it.
94
+ 4. **Surface, don't bypass.** If validation reveals a deeper bug than the user's task scope, do not "fix" it silently. Document, surface, ask.
95
+
96
+ ### 4.1 Functional regime — seed, read back, compare
97
+
98
+ **Test-slice precondition — a usable test version is `active: true` AND outside
99
+ the planning rotation.** When the test slice is isolated on a dedicated
100
+ `Versions` member (rather than a dedicated test period on a live version), verify the member's ACTIVE flag before seeding:
101
+ `farseer model <tenant> --table Versions --json` → ACTIVE column. An
102
+ **inactive** version silently swallows every write — members and variables
103
+ resolve, `commit()` reports success, zero cells land — so the failure presents
104
+ as a passing seed step and a mysteriously all-null read-back. If the only candidate test
105
+ versions on a tenant are inactive, **escalate rather than write into a live
106
+ version** — or use the verify-without-writing fallback (§4.4).
107
+
108
+ **Seeding is a tenant write**: a dev or test tenant only, never production. This
109
+ guide has no write procedure — the `farseer-model` skill has the cell-write
110
+ request today, and `farseer code` (Experimental) is the future path. Where no
111
+ test slice can be written, use the read-only variant (§4.4).
112
+
113
+ **Reading back** — evaluate one formula per asserted cell, for example
114
+ `farseer formula eval 'sum("Margin OUT", "Product">"P-100", "Versions">"Plan", "Years">"Y2026", "Months">"M1")' <tenant> --json`
115
+ (the same evaluator as the API's `/evaluate`, which takes many cell queries in
116
+ one request). Variable names are **quoted** (they contain spaces / ` OUT`);
117
+ member filters use `"Table">"Member"`.
118
+
119
+ **Read back after the write has returned.** A cell write through
120
+ `/model/updateCells` returns after the engine has applied it and cleared the
121
+ read cache, so a read right after it normally sees the new values; after an
122
+ import or a job that is still running, wait for it to finish first. A read that
123
+ was already running when the write landed can still answer with the pre-write
124
+ values for a short while: if a read-back shows pre-write values, read again
125
+ after a minute before suspecting the formula. A variable with Calculate on
126
+ demand (`calculateOnDemand`), and every formula that reads it, is not
127
+ recalculated by the write. Don't call `POST /api/v3/evaluator/invalidateCache`
128
+ for a read-back: it recalculates the whole model and adds a command to the
129
+ tenant's command log.
130
+
131
+ ### 4.2 Backtest regime — compare against a reference
132
+
133
+ Same shape, but the comparison is against an external reference instead of
134
+ expected values: a Farseer export already confirmed accurate (a previous
135
+ baseline that signed off, or an audit pack the client accepted), or a client
136
+ workbook. Per (variable, period): model output, reference, delta, status. If
137
+ every status is `pass`, the outcome is `pass`. If some are `fail` and ALL of
138
+ them have a written rationale as expected deltas, the outcome can be
139
+ `pass-with-expected-deltas`. Otherwise `fail` — surface the offending rows to
140
+ the user, and do NOT mark the model deployable.
141
+
142
+ ### 4.2.1 Operating a real-data backtest (ground truth = a client workbook)
143
+
144
+ When the ground truth is a client workbook with personal data and the model
145
+ already carries a synthetic functional test rig, four operating rules:
146
+
147
+ 1. **Sibling tenant — do not wipe the functional rig.** Functional tests
148
+ (synthetic positions) and the backtest (real, personal data) must **not
149
+ share a tenant**: a wipe for real data destroys the synthetic rig and vice
150
+ versa, forcing a choice between regression and backtest. Use a separate
151
+ sibling tenant for the real data; the functional rig stays intact and the
152
+ personal data is physically isolated on one tenant. The model is the same;
153
+ only the tenant differs.
154
+
155
+ 2. **Non-circular expected — from the client's *computed* grid, not from the
156
+ drivers.** The expected figure MUST come from the template's **computed
157
+ columns** (its own "Total …" outputs), never from re-applying the model formula
158
+ to the same drivers — otherwise it is a tautology (model ≡ expected by
159
+ construction). Σ of those Total columns over the in-scope members = the sector
160
+ expected. A `dev2` that holds ONLY the scope → the model's grand Σ equals the
161
+ sector Σ (no filter needed in the eval).
162
+
163
+ 3. **Calibrate units against the expected aggregate = double confirmation.**
164
+ Before seeding, calibrate each parameter's unit by dividing the expected
165
+ aggregate by its carrier: e.g. `Σ_expected ÷ Σ FTE-months = per-FTE-month rate`
166
+ confirms BOTH the input unit AND that the model formula (`× FTE`) is correct,
167
+ simultaneously. If the calibration doesn't come out to a clean parameter, the
168
+ unit or the formula is wrong — catch it **before** the seed, not after a
169
+ mysterious delta.
170
+
171
+ 4. **A backtest catches bugs the functional test cannot.** A functional test on
172
+ synthetic tables has **complete FKs by construction**, so it masks missing
173
+ FKs. A real-data extract exposes them: an FK column from X to Y that the
174
+ model relies on, but that is absent or empty in the extracted members → the
175
+ FK resolves to NULL → every per-`Y` parameter returns null. **Rule: when
176
+ extracting real tables, cross-check EVERY FK the model relies on against the
177
+ FK values of the extracted members.**
178
+
179
+ **Outcome vocabulary in practice.** `pass-with-expected-deltas` is a legitimate v1
180
+ target: reconcile the bulk (gross + contributions + allowances) and document any
181
+ component that is a *sketch* in the model (e.g. a distribution not yet modelled)
182
+ as an expected-delta with amount + reason. Do not
183
+ force a `pass` through an approximation that hides the gap.
184
+
185
+ ### 4.3 What the AI does NOT do
186
+
187
+ - Does NOT push model changes to production tenants until validation passes.
188
+ - Does NOT silently fix a "validation revealed a bug" — surfaces it, asks the user, then either widens the change scope (with permission) or files it for follow-up.
189
+
190
+ ### 4.4 Sanctioned fallback — verify without writing
191
+
192
+ When no usable test slice exists (no active test version, no free test period,
193
+ or the tenant is live and any seed is a risk), the functional regime has a
194
+ **read-only variant**: instead of seeding synthetic inputs, find coordinates in
195
+ the **real data** that already exhibit each scenario shape, and assert the
196
+ formula's value there.
197
+
198
+ 1. **Export the input variables, scoped** to one version and a few months (`farseer variable export "<Var>" --filter Versions=<version> --filter Months=<month> --output <var>-<month>.csv`, one file per month, with `FARSEER_TENANT` set). Full grain only on a dev or test copy: an unfiltered export runs every cell through the engine of a live tenant.
199
+ 2. **Find coordinates already exhibiting each scenario shape** — e.g. a
200
+ no-revenue confirmation, a negative/storno entry, a large-magnitude value,
201
+ an aggregate rollup, an out-of-filter member.
202
+ 3. **Compute the expectation FROM THE EXPORT** — never hand-write it — and
203
+ assert the formula's output at those coordinates.
204
+ 4. **Bracket the run with a before/after aggregate measurement** of the
205
+ untouched active versions, proving the validation cycle changed nothing.
206
+
207
+ What it should achieve: every scenario closes to the cent or names its gap,
208
+ including a precision check at a large magnitude — arguably **stronger** than synthetic seeding (the shapes come
209
+ from the real data distribution), and it mutates nothing.
210
+
211
+ The two disciplines in bold above are what keep it honest: expectations
212
+ **computed from the export** (a hand-written expectation silently degenerates
213
+ into testing your own arithmetic), and the **no-mutation proof**.
214
+
215
+ **Honest limitation:** a shape absent from the data cannot be tested this way.
216
+ List every untestable scenario as **residual** with the test — never silently
217
+ drop it from the scenario set.
218
+
219
+ ### 4.5 Joining actual with plan — find where they meet before building report variables
220
+
221
+ Before writing any variable that puts actual next to plan / forecast (a
222
+ variance line, an "actual + forecast" splice, a report that shows both),
223
+ answer three questions **in this order**.
224
+
225
+ 1. **Get the client's control numbers first** — one total per version and
226
+ period you are about to show. They are the expected values of the backtest
227
+ (§4.2) and the only way to tell a modelling error from a data gap; without
228
+ them every "is this right?" turn ends in "no clear answer".
229
+ 2. **Read the plan side, then mirror the sibling.** At what grain is plan
230
+ entered (it may be an item the actual source never carries — a
231
+ Farseer-generated code, a planning bucket)? Which existing variable
232
+ already combines actual and plan for a **sibling measure**? Copy its
233
+ pattern (the version split, the meeting grain) rather than inventing one;
234
+ a second pattern for the same join is a reconciliation problem later.
235
+ 3. **Test the actual source for a multi-path FK with two pins.** Pin the
236
+ actual to two mutually exclusive members of the reporting dimension: if
237
+ the pinned sums add up to more than the unpinned total — in the incident
238
+ each pin returned the whole total — the same cells reach the target by two
239
+ paths; fix the link graph before building on it. If they add up to
240
+ **less**, holding only the cells where two grain dimensions share a target
241
+ member, the variable is read at a shared FK descendant and returns the
242
+ intersection (measured 2026-10-17) — read a one-sided roll-up there instead.
243
+
244
+ **A cross-version reference needs no special form.** `sum(V, Versions >
245
+ "Plan")` from inside another version works as the sole term, inside `if()`
246
+ and inside a sum (measured 2026-10-15 on a sandbox tenant). The one null
247
+ measured: a variable with rollup FORMULA read at a single version — a
248
+ FORMULA-rollup ratio of two cross-version references returned its value
249
+ unpinned and `null` under `Versions > "Plan"` and under the forecast
250
+ version. Don't put cross-version references in a FORMULA-rollup variable that
251
+ consumers read per version.