farseer-cli 1.19.0 → 1.21.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 (165) hide show
  1. package/README.md +32 -9
  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 +72 -31
  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 +474 -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 +102 -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 +33 -371
  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 +275 -0
  73. package/dist/operations/model.js.map +6 -0
  74. package/dist/operations/sheets.js +766 -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 +11 -11
  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/dimensionMembers.js +54 -0
  99. package/dist/utils/dimensionMembers.js.map +6 -0
  100. package/dist/utils/entityResolver.js +74 -25
  101. package/dist/utils/entityResolver.js.map +1 -1
  102. package/dist/utils/errorFormatter.js +13 -0
  103. package/dist/utils/errorFormatter.js.map +1 -1
  104. package/dist/utils/formulaFilters.js +11 -2
  105. package/dist/utils/formulaFilters.js.map +1 -1
  106. package/dist/utils/operationCli.js +102 -0
  107. package/dist/utils/operationCli.js.map +6 -0
  108. package/docs/cli-guide.md +195 -1
  109. package/docs/pipeline-reference.md +138 -24
  110. package/docs/troubleshooting.md +18 -0
  111. package/package.json +2 -2
  112. package/skills/farseer-app-dev/SKILL.md +131 -0
  113. package/skills/farseer-app-dev/assignments.md +185 -0
  114. package/skills/farseer-app-dev/farseer-client-api-guide.md +907 -0
  115. package/skills/farseer-app-dev/farseer-client-release-notes.md +212 -0
  116. package/skills/farseer-app-dev/working-patterns.md +876 -0
  117. package/skills/farseer-authenticate-to-tenant/SKILL.md +60 -0
  118. package/skills/farseer-authenticate-to-tenant/app-authentication.md +91 -0
  119. package/skills/farseer-author-import-job/SKILL.md +114 -0
  120. package/skills/farseer-author-import-job/cell-writes.md +530 -0
  121. package/skills/farseer-author-import-job/deploying-a-farseer-app.md +383 -0
  122. package/skills/farseer-author-import-job/import-job-pattern.md +232 -0
  123. package/skills/farseer-build-model/SKILL.md +122 -0
  124. package/skills/farseer-build-model/formula-grain-alignment.md +612 -0
  125. package/skills/farseer-build-model/naming-conventions.md +435 -0
  126. package/skills/farseer-build-model/sparse-formulas-pattern.md +330 -0
  127. package/skills/farseer-build-pipeline/SKILL.md +190 -0
  128. package/skills/farseer-build-pipeline/erp-sales/README.md +108 -0
  129. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_invoice_lines.yaml +23 -0
  130. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_invoice_lines_raw.yaml +23 -0
  131. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_item_product_map.yaml +17 -0
  132. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/bronze/erp_sales_item_product_map_raw.yaml +11 -0
  133. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/gold/erp_sales_cells.yaml +40 -0
  134. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_lines.yaml +35 -0
  135. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_recon.yaml +43 -0
  136. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/Models/silver/erp_sales_unmapped_items.yaml +28 -0
  137. package/skills/farseer-build-pipeline/erp-sales/erp-sales-etl/main.py +49 -0
  138. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/in/erp_sales_cells.yaml +17 -0
  139. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/in/erp_sales_cells_raw.yaml +14 -0
  140. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/Models/load/erp_sales_to_model.yaml +47 -0
  141. package/skills/farseer-build-pipeline/erp-sales/erp-sales-load/main.py +44 -0
  142. package/skills/farseer-build-pipeline/erp-sales/item_to_product.csv +5 -0
  143. package/skills/farseer-build-pipeline/pipeline-app-failure-report-pattern.md +103 -0
  144. package/skills/farseer-build-pipeline/pipeline-source-encoding.md +68 -0
  145. package/skills/farseer-dashboards/SKILL.md +222 -20
  146. package/skills/farseer-dashboards/cards.md +59 -0
  147. package/skills/farseer-dashboards/charts.md +245 -0
  148. package/skills/farseer-dashboards/check.md +91 -0
  149. package/skills/farseer-dashboards/measures.md +88 -0
  150. package/skills/farseer-dashboards/page.md +99 -0
  151. package/skills/farseer-dashboards/reporting-period.md +82 -0
  152. package/skills/farseer-dashboards/standard-set.md +182 -0
  153. package/skills/farseer-dashboards/tables.md +174 -0
  154. package/skills/farseer-help-center/SKILL.md +66 -0
  155. package/skills/farseer-modify-model/SKILL.md +158 -0
  156. package/skills/farseer-modify-model/model-validation-pattern.md +251 -0
  157. package/skills/farseer-optimize-model/SKILL.md +314 -0
  158. package/skills/farseer-optimize-model/stats-dump.ts +139 -0
  159. package/skills/farseer-pnl/SKILL.md +220 -0
  160. package/skills/farseer-review-model/SKILL.md +172 -0
  161. package/skills/farseer-run-apps/SKILL.md +102 -0
  162. package/skills/pipeline-authoring/SKILL.md +4 -4
  163. package/skills/pipeline-authoring/practices.md +17 -4
  164. package/dist/services/agentApprovalFetch.js +0 -90
  165. package/dist/services/agentApprovalFetch.js.map +0 -6
@@ -0,0 +1,383 @@
1
+ # Deploying a Farseer app
2
+
3
+ **Read when the user asks to "deploy", "make an app", "register a script as an
4
+ app".**
5
+
6
+ ---
7
+
8
+ ## Apps vs local-run tooling — don't confuse them
9
+
10
+ Two different things live in a project:
11
+
12
+ | | Deployable **app** | Local-run **tooling** |
13
+ |---|---|---|
14
+ | Where | `farseer-apps/<tenant>/` (manifest + code) | outside `farseer-apps/` |
15
+ | Runs | on the Farseer tenant (scheduled / on-demand) | locally via `tsx`/SDK |
16
+ | Deployed? | yes — `farseer push` | **never** — validation runners, scaffold deployers, probes |
17
+
18
+ This doc is about **deployable apps**. Validation, scaffold and probe
19
+ scripts are not apps and are never pushed.
20
+
21
+ ---
22
+
23
+ ## The core distinction: loose scripts ≠ a deployable app
24
+
25
+ A deployable app is **manifest + code**, both under `farseer-apps/<tenant>/`,
26
+ authored there **directly** (git-tracked — git holds the history; `farseer
27
+ push`/`pull` keep it in sync with the tenant). There is no separate editable
28
+ copy to mirror.
29
+
30
+ | Half | Path | What it is |
31
+ |---|---|---|
32
+ | Manifest | `apps/<Name>.json` | The registered app — name, description, entrypoint, arguments, `_remote.id` |
33
+ | Code | `files/Scripts/<Name>/…` (folder) **or** `files/Scripts/<file>.ts` (flat) | The `.ts` the app runs |
34
+
35
+ Code with **no owning manifest** is an **orphan** ("not an app yet"). It still
36
+ runs via `farseer run-script`, but no app is registered. The single most common
37
+ mistake is building a working run-script and stopping there.
38
+
39
+ ### Two code layouts (both supported)
40
+
41
+ - **Folder-per-app** (preferred for multi-script apps): code in
42
+ `files/Scripts/<Name>/`; the manifest's `entrypoint` names a file there.
43
+ Organised — keeps a many-file app's code together.
44
+ - **Flat / entrypoint** (what `farseer pull` emits for simple apps): code files
45
+ sit **directly** under `files/Scripts/`, the manifest carries an `entrypoint`
46
+ but **no per-app subfolder**.
47
+
48
+ ---
49
+
50
+ ## Procedure
51
+
52
+ ### 1. Author the manifest — `farseer-apps/<tenant>/apps/<Name>.json`
53
+
54
+ ```json
55
+ {
56
+ "name": "import-actuals",
57
+ "description": "One-line what + usage (e.g. import-actuals.ts <year> <month>).",
58
+ "entrypoint": "import-actuals.ts",
59
+ "arguments": [
60
+ { "name": "Year", "defaultValue": "2025" },
61
+ { "name": "Month", "defaultValue": "1" }
62
+ ]
63
+ }
64
+ ```
65
+
66
+ - **`entrypoint`** — the orchestrator `.ts` (the one you actually invoke).
67
+ **Give it a name unique on the tenant** (`<app-slug>.ts`), never a generic
68
+ `main.ts` / `index.ts`: a second app whose entrypoint name already exists
69
+ elsewhere on the tenant binds to the OTHER app's folder and would run its
70
+ code ([Gotchas](#gotchas)).
71
+ - **Folder layout:** the `entrypoint` names a file in `files/Scripts/<Name>/`.
72
+ `farseer push` sends the entrypoint, not a list of scripts: `farseer app show`
73
+ lists the scripts the tenant gives the app, and `farseer app configure
74
+ --source-folder` sets the folder they come from.
75
+ - **Flat layout:** omit the per-app folder; the `entrypoint` names a file that
76
+ lives directly under `files/Scripts/`.
77
+ - **Runtime secrets** are **secret-typed app arguments** — a `secret: true`
78
+ argument whose value is pasted once on the tenant, never on disk / in git. See
79
+ [`app-authentication.md`](../farseer-authenticate-to-tenant/app-authentication.md).
80
+ - **Omit `_remote`** — the tenant assigns `_remote.id` on first push.
81
+
82
+ ### 1a. The arg-reading contract — `FARSEER_ARGS`, not per-arg env vars
83
+
84
+ A **deployed app** (run from the UI, the API or a schedule) receives **all**
85
+ of its manifest arguments as a **single JSON blob** in
86
+ `process.env.FARSEER_ARGS`, keyed by the manifest's exact argument names
87
+ (e.g. `{"Year":"2026","Month":"1"}`). It does **not** set one env var per
88
+ argument, and **neither does anything local**: `farseer run` passes values
89
+ only as positional argv, and `farseer run-script` passes no arguments at all
90
+ (measured 2026-10-15). A script that reads `process.env.<argName>` therefore
91
+ reads `undefined` everywhere and silently takes its default.
92
+
93
+ **Symptom:** the run log shows the correct `Parameters: {...}` line (the
94
+ value *was* passed), but the script's own argument-validation guard rejects
95
+ it as empty anyway, or a `?? default` quietly wins (a scaffolder ran in dry
96
+ mode despite `DRY_RUN=false`). That's the tell — the arg-reading mechanism is
97
+ wrong, not the input control (dropdown/manifest default); don't touch those.
98
+
99
+ Every channel, both runtimes, and how to test it:
100
+ [App arguments end to end](#app-arguments-end-to-end).
101
+
102
+ ### 1b. Never read a manifest argument by POSITION
103
+
104
+ `farseer run` does not pass an empty optional argument as an empty string — it
105
+ **drops it from argv**. Everything after it shifts one place left, and a script
106
+ reading `process.argv[n]` reads a neighbour's value with no error anywhere.
107
+
108
+ Field report, 2026-09-20, a write-back app on a sandbox instance:
109
+
110
+ ```
111
+ declared: Company=… Year=… Version=… ThresholdEur="" ThresholdPct="" DRY_RUN=false
112
+ arrived: ["…", "…", "…", "false"]
113
+ ```
114
+
115
+ `false` landed in the `ThresholdEur` slot; `DRY_RUN` fell back to its default
116
+ `true`; the app **previewed instead of writing** and reported success. The
117
+ operator had asked for a write in the run dialog. Nothing in the log said
118
+ otherwise — the app printed its mode from the value it believed it had.
119
+
120
+ Three rules, in order of how much they save you:
121
+
122
+ 1. **Read by name**, never by index. Name-keyed values cannot
123
+ shift.
124
+ 2. **A mode flag is honoured only as the literal `true`/`false`.** Anything
125
+ else means the value is not what you think it is: fall back to the SAFE
126
+ side (the one that writes nothing) and say so.
127
+ 3. **Print the resolved arguments and mode before the first write.**
128
+ The incident above was invisible for one run only
129
+ because no such line existed.
130
+
131
+ Two layout rules follow from the same behaviour: put optional arguments
132
+ **last**, after the mode flag, and name them as plain identifiers — the CLI does not check argument names,
133
+ and `farseer run --arg` cannot set a name that contains `=`.
134
+
135
+ **Fix — read by name.** A **deployed** app must carry the reader in its own
136
+ files: on the tenant it sees `farseer-client` and its own files only. Read
137
+ `FARSEER_ARGS` first, and fall back only to env vars **you exported in your
138
+ own shell** for a local run (`run-script` itself sets none).
139
+ **Never read argv once `FARSEER_ARGS` is present** — with two arguments and a
140
+ value containing a space, argv cannot recover where one argument ends.
141
+
142
+ The minimal inline reader is 4 lines:
143
+
144
+ ```ts
145
+ const args: Record<string, unknown> = (() => {
146
+ try { return process.env.FARSEER_ARGS ? JSON.parse(process.env.FARSEER_ARGS) : {}; }
147
+ catch { return {}; }
148
+ })();
149
+ const year = String(args.Year ?? process.env.Year ?? "").trim();
150
+ ```
151
+
152
+ ### 2. Put the code under `files/Scripts/`
153
+
154
+ Author it there directly — folder `files/Scripts/<Name>/…` (folder layout) or
155
+ flat files `files/Scripts/<file>.ts` (flat layout). `farseer run-script` and the
156
+ app runtime resolve code at this canonical path, so no copy/mirror step is
157
+ needed.
158
+
159
+ ### 3. Push
160
+
161
+ ```bash
162
+ cd <the folder that holds farseer-apps/>
163
+ farseer push <tenant> -y
164
+ ```
165
+
166
+ - `farseer push` authenticates via `farseer login`
167
+ (OAuth, when the user is a tenant admin) or an admin API key. There is no
168
+ mandatory UI step.
169
+ - Push only after the user explicitly confirms; for `staging` or `production`, confirm **per action** and restate the tenant.
170
+ - The tenant registers the app and writes back `_remote.id`.
171
+ - **Push only what you mean to:** a plain `farseer push <tenant>` pushes every
172
+ local change under `farseer-apps/<tenant>/` (other apps, other scripts).
173
+ Since CLI 1.10.0, `farseer push <tenant> --only Scripts/<file>` (repeatable,
174
+ paths relative to the tenant's files folder) pushes just those files and
175
+ leaves other local edits alone, per the CLI's help. Run `farseer status
176
+ <tenant>` first either way.
177
+
178
+ ### 4. Verify — don't trust, confirm
179
+
180
+ - Confirm the field/row count actually landed on the tenant before reporting
181
+ success.
182
+ - **The new app (and any new folder) copied its parent folder's shares** —
183
+ everyone who sees the parent sees it at once. If it must start unshared,
184
+ remove those copies before any data.
185
+ - **`farseer app show <tenant> "<App>"`** — the entrypoint must carry the ▶
186
+ marker and every listed script must come from the app's OWN folder. Scripts
187
+ from another folder = the entrypoint-name collision in [Gotchas](#gotchas).
188
+ - **Definition of done for an app with arguments: run it once from the
189
+ platform UI** with non-default values, including one containing a space.
190
+ Only the tenant runner builds `FARSEER_ARGS`; `run-script` passes no
191
+ arguments and a scheduled run with no arguments takes the defaults, so
192
+ neither exercises the read path (see below).
193
+
194
+ ---
195
+
196
+ ## Organising the Apps sidebar with folders
197
+
198
+ **The Apps sidebar supports folders — but this is invisible from the RemoteJobs
199
+ API.** `GET /remoteJobs/{id}` has NO folder field. Folder membership lives
200
+ exclusively in the **folder tree**: the Apps root is a folder item, and each app
201
+ is a folder item inside it whose `reference` is the remoteJob id.
202
+
203
+ > **Lesson for agents, wider than apps.** For EVERY sidebar surface — variables,
204
+ > sheets, files, apps — the organisational structure lives in the folders API,
205
+ > not on the object. An agent once told a user outright that "apps cannot be
206
+ > organised into folders", right up until the user's existing folder appeared in
207
+ > `getFolderItems('Apps')`. **Before claiming "X cannot be organised", call
208
+ > `getFolderItems(<root>)`.** An object with no folder field does not mean there
209
+ > are no folders.
210
+
211
+ ```ts
212
+ // create a thematic folder in the Apps root
213
+ const f = await client.folders.create({ parentId: APPS_ROOT, name: '1 · Data', allowedTypes: null });
214
+ // move an app: id = the app's FOLDER-ITEM id (from getFolderItems), NOT the remoteJob id
215
+ await client.folders.moveItemsBatch([{ id: appFolderItemId, parentId: Number(f.id), order: 1 }]);
216
+ ```
217
+
218
+ - `order` (a float) sets the sequence inside a folder — use it instead of
219
+ numbering the app names.
220
+ - **Same two-ID-space hazard as deleting an app**: a folder item's `id` is not
221
+ its `reference` (the remoteJob id). Mixing the two is the error class that has
222
+ already deleted a dimension table once.
223
+
224
+ Recommended delivery shape: numbered **thematic** folders following the order of
225
+ the monthly ritual, apps with clean names, and sequence inside each folder
226
+ carried by `order` rather than by prefixes in the names.
227
+
228
+ ## App arguments end to end
229
+
230
+ One place for the whole path an argument travels — define → deliver → read →
231
+ test. Measured 2026-10-15 on a sandbox tenant (CLI 1.13) unless marked
232
+ otherwise; the tenant had no job runner connected, so server-side delivery is
233
+ from the job-runner source,
234
+ while server-side **validation** was measured (it runs before the runner is
235
+ needed).
236
+
237
+ ### Define — manifest + `farseer push`, not the SDK
238
+
239
+ Declare arguments in `apps/<Name>.json` and push:
240
+
241
+ ```json
242
+ "arguments": [
243
+ { "name": "Mode", "dataType": "string", "defaultValue": "all",
244
+ "options": ["all", "fast", "slow"] },
245
+ { "name": "DRY_RUN", "dataType": "boolean", "defaultValue": "true" }
246
+ ]
247
+ ```
248
+
249
+ - **`options`** (two or more) render a dropdown in the Run dialog;
250
+ **`dataType: "boolean"`** renders a true/false choice; **`"fixed": true`**
251
+ renders read-only and always submits the default — `fixed` locks one
252
+ script to one pipeline, `options` exposes exactly the valid choices. Set
253
+ them in the manifest or in the UI (App → Edit argument → Options).
254
+ - **Do not create or update apps with `client.remoteJobs.create/update` on
255
+ SDK ≤ 1.8.12** (the version older CLIs wrote into the tenant folder's
256
+ `package.json` — filed as `farseerdev/farseer#3135`; this CLI writes 1.18.1,
257
+ and `farseer pull` / `farseer install` move an older CLI-written pin forward). Its serializer drops
258
+ `dataType`, `options` and `fixed` and sends only the legacy
259
+ `type: variable|constant`. The platform reads `constant` as
260
+ `dataType: "string", fixed: true` — a **locked** argument — and
261
+ `variable` as a plain string; with neither it answers `400 Argument "X" is
262
+ missing both "dataType" and legacy "type" field`. Fixed in 1.8.19 (the
263
+ argument-schema upgrade —
264
+ [`farseer-client-release-notes.md`](../farseer-app-dev/farseer-client-release-notes.md));
265
+ re-checked on 1.14.0 (2026-10-17, sandbox tenant): `dataType`, `options`
266
+ and `fixed` sent through `remoteJobs.create` read back unchanged. If an
267
+ app must be created by script on ≤ 1.8.12 (one app per entity, say), create it, then
268
+ `farseer pull` → correct the manifests → `farseer push`, and check that no
269
+ pulled argument says `"fixed": true` unless you meant it. (The serializer
270
+ difference confirmed against the published 1.8.12 and 1.8.19 packages.)
271
+ - **Names:** plain identifiers, unique per app — the read side matches them
272
+ exactly (see below), and `farseer run --arg Name=Value` splits at the first
273
+ `=`, so a name containing `=` cannot be set locally.
274
+
275
+ ### Deliver — what the script receives
276
+
277
+ | Runtime | Arguments arrive as | Notes |
278
+ |---|---|---|
279
+ | **Tenant runner** (UI, API, schedule) | `FARSEER_ARGS` = JSON object keyed by the **exact manifest names** | server validates first (below); positional argv also carries the values — never read it |
280
+ | **Local `farseer run <tenant> "<App>"`** | **positional argv only**, no `FARSEER_ARGS` | values passed through a shell: split on spaces (`"one two"` → 2 argv items), `""` dropped (later positions shift — §1b), shell syntax interpreted (`;` ends the command, `$VAR` expands). Override names are matched case-sensitively; an unknown name is ignored and the default used. Filed as `farseerdev/farseer#3130` |
281
+ | **Local `farseer run-script`** | **nothing** — no argv, no per-argument env vars | only env vars you exported in your shell reach the script |
282
+ | **Local, `FARSEER_ARGS` exported by you** — `FARSEER_ARGS='{"Year":"2026"}' farseer run <tenant> "<App>"` | `FARSEER_ARGS` exactly as you wrote it (measured: byte-identical, including a value with a space and an escaped quote) | **the supported local way to exercise the read path.** Nothing is merged locally: an argument missing from the blob is **absent** (the server would fill its default), and `""` stays `""` (the server would replace it by the default) — spell every argument out. A `--arg` override changes argv only, never the blob |
283
+
284
+ **Server-side validation** (`POST /remoteJobs/{id}/run`, measured — it runs
285
+ before a runner is needed):
286
+
287
+ - **`options` are ENFORCED on API runs**, not just a UI hint: a value outside
288
+ the list → `400 Argument "<Name>" value "<v>" is not in allowed options`,
289
+ case-sensitive and untrimmed (`"Fast"` and `"fast "` both rejected). The
290
+ 400 leaves **no run record** — nothing in the run history shows the
291
+ attempt.
292
+ - A boolean must be the literal `"true"` / `"false"` — `"yes"` →
293
+ `400 … must be "true" or "false"`.
294
+ - **`""` is replaced by the argument's `defaultValue`** (so `""` passes even
295
+ on a dropdown), and a `fixed` argument always takes its default (source:
296
+ `normalizeRunArguments`). An empty value therefore never means "not set"
297
+ on the server unless the default is itself empty.
298
+
299
+ **Callers that run an app over the API** (an app chaining itself, or
300
+ triggering another): every argument value must be one the target accepts.
301
+ `""` never carries meaning — on the server it becomes the default, locally it
302
+ stays `""` — so use an explicit sentinel such as `all`. An app that passed
303
+ `""` for "all" keeps working only while that argument is free text — the day someone turns it into a dropdown, continuation runs
304
+ start failing with the 400 above and a multi-run chain stops halfway unless
305
+ the caller logs the HTTP error. Give a
306
+ dropdown an explicit "all / not set" option (plain identifier, no spaces) as
307
+ its default, send that instead of `""`, send canonical forms (`"8"`, not
308
+ `"08"`), and log the response body of every run call.
309
+
310
+ ### Environment the script sees
311
+
312
+ | | Local `farseer run` / `run-script` (measured) | Tenant runner (job-runner source, not measured live) |
313
+ |---|---|---|
314
+ | tenant | `TENANT_ID` = the tenant **slug** | `TENANT_ID` |
315
+ | URL | `FARSEER_URL` (`https://<org>.farseer.io/api/v3`) | `FARSEER_URL` |
316
+ | credential | `FARSEER_TOKEN` (browser login) **or** the API-key variable (api-key mode) — mutually exclusive | the API-key variable |
317
+ | job identity | — | `JOB_NAME` (the app's name), `JOB_UUID` (this run), `SOURCE_FOLDER_ID` |
318
+ | arguments | — | `FARSEER_ARGS` |
319
+
320
+ The API-key variable exported in your shell next to the token `farseer run`
321
+ injects crashes the SDK (`Cannot set both`).
322
+ No variable is documented to tell the two runtimes apart — `JOB_UUID` is
323
+ set only by the runner, an implementation detail; an explicit
324
+ `FARSEER_RUNTIME` is requested upstream (`farseerdev/farseer#3136`).
325
+ `JOB_NAME` lets one script serve N apps (entity in the app name), and
326
+ `JOB_UUID` finds the app's own run (`remoteJobRuns.listByRemoteJobId(id)`)
327
+ — e.g. to see who started it.
328
+
329
+ ### Test — the one test that exercises the read path
330
+
331
+ The three production incidents behind this section (a scaffolder that
332
+ ignored `DRY_RUN=false`; a copy app that refused to start with every
333
+ argument supplied; an allocation app that failed in front of a client when
334
+ two arguments with spaces merged in argv) all passed local testing, because
335
+ local runs do not build `FARSEER_ARGS` at all. And a schedule that passes no
336
+ arguments takes the defaults and never fails — **accepting the defaults in
337
+ the Run dialog still sends them**, so the first UI run is the first real
338
+ test. Hence the Definition-of-done line in §4: run the app from the
339
+ platform UI with non-default values, one of them containing a space, and
340
+ check the resolved arguments it prints.
341
+
342
+ Before that, exercise the read path locally by exporting the blob yourself
343
+ (measured on a sandbox tenant 2026-10-15 — the script receives it
344
+ byte-identical):
345
+
346
+ ```bash
347
+ FARSEER_ARGS='{"Year":"2026","Mode":"all","Entity":"one two"}' farseer run <tenant> "<App>"
348
+ ```
349
+
350
+ Write **every** argument into the blob — locally nothing fills a default —
351
+ and never `""`. This does not replace the UI run: it cannot exercise the
352
+ server's defaults, `options` validation or `fixed` arguments.
353
+
354
+ ## Gotchas
355
+
356
+ - **No entrypoint after push** → the `entrypoint` names no file in the tenant's
357
+ `files/`; `farseer push` then leaves the app without one and says nothing.
358
+ Fix the name.
359
+ - **`farseer run-script` can't find the script** → it must live under the
360
+ tenant's `files/` (it looks in `files/Scripts/` first, then by file name in
361
+ any folder under `files/`).
362
+ - **Multi-realm tenants** → sign in once with `farseer login --org <org>`;
363
+ every other command picks the realm from the tenant's organisation (only
364
+ `login` and `logout` take `--realm`).
365
+ - **CWD drift** → `farseer pull` creates `farseer-apps/<tenant>/` in the
366
+ *current* directory (or under `FARSEER_APPS_DIR`), and `install`, `push`,
367
+ `status`, `run` and `run-script` look for it there. Always `cd` into the
368
+ folder that holds `farseer-apps/` first.
369
+ - **Entrypoint-name collision — the app runs ANOTHER app's code.** Measured
370
+ 2026-10-15 on a sandbox tenant: a second app whose entrypoint is a bare
371
+ `main.ts`, pushed to a tenant where another app's folder already holds a
372
+ `main.ts`, binds to that **other** folder and script (`push` reports
373
+ `Created`; `app show` lists the other folder's scripts; raw
374
+ `GET /remoteJobs/{id}` shows the foreign `sourceFolderId`). Renaming the
375
+ entrypoint and pushing again does **not** move `sourceFolderId`. Repair
376
+ without losing the app's id and run history:
377
+ `farseer app configure <tenant> "<App>" --source-folder "Scripts/<App>"
378
+ --entrypoint <script>.ts` (`--id <n>` when names collide). Any later push
379
+ of a manifest that still says bare `main.ts` breaks it again — so give
380
+ every entrypoint a unique name (§1) and check `app show` after the first
381
+ deploy (§4). (Deleting and re-pushing
382
+ the app also repairs it, at the cost of its id and history. Filed as
383
+ `farseerdev/farseer#3131`.)
@@ -0,0 +1,232 @@
1
+ # Import-job pattern — argument-driven, label-keyed, idempotent
2
+
3
+ An import job is how recurring data reaches a Farseer model: GL actuals, source
4
+ extracts, version copies. Done well, a job can be re-run any number of times and
5
+ the model never duplicates or double-counts. Done badly, every re-run silently
6
+ overwrites or piles onto the last, and there is no clean way to back it out.
7
+
8
+ Three properties separate a production-grade import job from a throwaway script:
9
+
10
+ 1. **It writes into locked `IMPORT_` variables** — never into hand-editable inputs.
11
+ 2. **It is argument-driven** — period, year, version, source/target are inputs, not
12
+ hardcoded constants.
13
+ 3. **Its labels encode its scope** — so a re-run finds and reverses its own prior
14
+ run before re-applying, making the whole job idempotent.
15
+
16
+ > **New recurring imports are Simple Pipelines**:
17
+ > see the [`farseer-build-pipeline`](../farseer-build-pipeline/SKILL.md) skill. The three properties above
18
+ > carry over unchanged — a pipeline load writes into locked `IMPORT_` targets,
19
+ > takes its period as arguments, and replaces its slice through labels +
20
+ > `undo_previous`. The SDK mechanics below apply to existing TypeScript apps.
21
+
22
+ This is the "why + design" doc. The operational walkthrough is the
23
+ [`farseer-author-import-job`](SKILL.md) skill; the
24
+ SDK mechanics live in [`cell-writes.md`](cell-writes.md)
25
+ and [`working-patterns.md`](../farseer-app-dev/working-patterns.md).
26
+
27
+ ---
28
+
29
+ ## 1. Import targets are locked `IMPORT_` variables
30
+
31
+ A variable that receives imported data should:
32
+
33
+ - Carry the **`IMPORT_` prefix** so it is unmistakable in the model tree and in
34
+ formulas, and so it lands in the `@folder "IMPORT"` group.
35
+ - Be **`@readonly true` (locked)** — imported data is a feed, not a planner input.
36
+ A locked import variable cannot be hand-edited in a sheet, so the import remains
37
+ the single source of truth for that data.
38
+
39
+ If an existing import target lacks the prefix, **offer a rename** — but don't block the work on it; the
40
+ lock matters more than the prefix.
41
+
42
+ > **Why locked matters most.** Per Farseer semantics any cell can be overwritten —
43
+ > a manual edit, an import, or an automation. If the import target is *unlocked*,
44
+ > a planner can silently overwrite an imported number in a sheet, and the next
45
+ > re-import will fight that edit. Locking the import target removes the conflict
46
+ > entirely.
47
+
48
+ ---
49
+
50
+ ## 2. Argument-driven, never hardcoded
51
+
52
+ Design the job — and the app that wraps it — to take **bounded arguments** from the
53
+ start, rather than editing the script each run:
54
+
55
+ | Argument | Typical values | Role |
56
+ |---|---|---|
57
+ | Period / Month | `1`–`12` | which month the load belongs to |
58
+ | Year | `2024`, `2025` | which year |
59
+ | Version | `Actual`, `Plan`, `Budget` | which version it writes |
60
+ | Source / Target | `Actual → Plan` | for version-copy / fc-preparation jobs |
61
+
62
+ The app manifest declares these as `arguments` (see
63
+ [`deploying-a-farseer-app.md`](deploying-a-farseer-app.md)). The benefit is twofold:
64
+ the same app handles every period without code edits, **and** the argument values
65
+ become the natural source of the job's labels (next section).
66
+
67
+ ---
68
+
69
+ ## 3. Labels are the idempotency key
70
+
71
+ `labels` are not decoration. They are the mechanism by which a re-run locates and
72
+ reverses its own prior run:
73
+
74
+ > `labels` is what makes the undo idempotent on re-run — the import job locates its
75
+ > own previous run **by label match**.
76
+
77
+ So the labels must encode **exactly the scope the job owns**. A GL load for January
78
+ 2025 carries `["GL", "2025", "1"]`. If the job is version-specific or copies
79
+ between versions, the version (and source→target) belong in the labels too:
80
+ `["fc-preparation", "Plan", "Y2025", "<source>2<target>"]`.
81
+
82
+ The re-run protocol is then:
83
+
84
+ 1. Build labels from the arguments.
85
+ 2. Find the prior job with the **same** labels.
86
+ 3. Reverse it, then apply the new run.
87
+
88
+ Two SDK primitives do step 2–3 — pick by reversibility:
89
+
90
+ | Primitive | Behavior | Use when |
91
+ |---|---|---|
92
+ | `importJobs.revert(id)` ↔ `commit(id)` | status flips `COMMITED ↔ REVERTED`; re-commit **re-applies** the cells | **preferred** — reversible; the month is never left empty if the new run fails |
93
+ | `job.undoPrevious()` / `deleteById(id, {undoData:true})` | **destructive** — deletes prior cells, cannot be rolled back | only when you genuinely want the prior cells gone |
94
+
95
+ The reversible pattern (revert prior → commit new → on failure re-commit prior)
96
+ guarantees the model is never momentarily empty for that scope. Without correct
97
+ labels, neither primitive can find the right prior run, and re-imports duplicate.
98
+
99
+ > **`createImportJob(...)` returns a wrapper of closures, not the job** — it has no
100
+ > `.id`. Resolve the id via `importJobs.list(labels)`, re-filtered client-side by
101
+ > label **and** status (`COMMITED`), because the server returns every job whose labels include these, newest first.
102
+
103
+ ---
104
+
105
+ ## 4. Deployment — instantiate arguments, use secret arguments for credentials
106
+
107
+ When the job runs against a tenant:
108
+
109
+ - **Instantiate the arguments** for the run (period/year/version) rather than
110
+ baking them in.
111
+ - **Source credentials are secret-typed app arguments**, not literals in the
112
+ script and not files committed to the repo. Declare the credential argument as
113
+ `secret: true` in the app manifest; the value is pasted **once, in the Farseer
114
+ UI**, and the platform injects it at run time. After deploying, **tell the user
115
+ exactly where in the Farseer interface to paste the secret** (the app's argument
116
+ field) — the deploy is not "done" until that is communicated.
117
+ - Legacy `Files/secrets/` upload is a fallback only; prefer the secret argument.
118
+
119
+ See [`deploying-a-farseer-app.md`](deploying-a-farseer-app.md) for manifest +
120
+ argument shape, and [`app-authentication.md`](../farseer-authenticate-to-tenant/app-authentication.md) for the secret-argument
121
+ precedence.
122
+
123
+ ---
124
+
125
+ ## 0. Never leave an import job hanging over an INPUT variable
126
+
127
+ An import job that stays on the tenant over an ` IN` variable is an
128
+ anti-pattern whatever else is true of it, because **undo deletes the job's
129
+ cells** — it does not restore the values they overwrote. One accidental undo
130
+ on a lingering job takes the planner's input slice with it.
131
+
132
+ The pattern that resolves it: **write through the job, then remove the trace**
133
+ — commit, verify by re-reading, then delete the job RECORD while keeping the
134
+ cells. A version-prep run should finish with zero lingering jobs.
135
+
136
+ The price, to accept consciously: you lose the undo trail and the job's own
137
+ idempotency, so the writing script must be idempotent by construction
138
+ (pre-clear plus absolute SET, not append).
139
+
140
+ **Where NOT to apply it:** on `IMPORT…` variables. They are locked, nobody
141
+ plans into them, and the undo trail there is worth keeping.
142
+
143
+ ## 1a. Mechanics that bite at scale
144
+
145
+ | Trap | Rule |
146
+ |---|---|
147
+ | `importJobs.list([label])` returns **HTTP 400** for a label containing brackets | **Keep a label to letters, digits, `-` and `_`.** Do not put a variable name — or anything else unsanitised — straight into a label. |
148
+ | **Silent truncation above ~100 000 rows per job** | Split into jobs well under 100k. 20 000 rows per job has worked well. Nothing reports the truncation. |
149
+ | `job.addRows()` throws on large sets (SDK stack overflow) | For large sets bypass the SDK helper and post the rows in HTTP batches yourself. |
150
+ | `commit().importedCells` systematically under-counts | It counts only the final flush batch. **The only valid check is re-reading the tenant.** If an app claims "wrote N", that N must come from the tenant, not from the commit report. |
151
+
152
+ ## 5. `importZeros` — a zero is not imported unless you ask for it
153
+
154
+ `ImportJobMetadata.importZeros` is a **required** boolean — there is no field
155
+ default; omit it and the create fails (TS compile error, or `400` over raw
156
+ HTTP). It alone decides what a source value of `0` does. **`overwriteCells`
157
+ does not enter into it.**
158
+
159
+ > **"The default" means the SDK's convenience overload, not the field.**
160
+ > `createImportJob({ columns })` hardcodes `importZeros: false` +
161
+ > `overwriteCells: false`.
162
+ > That is where the word "default" comes from, and conflating the two is how
163
+ > someone ends up believing a zero was skipped "by default" on a job that
164
+ > never set the field at all — which cannot happen.
165
+
166
+ Measured on a sandbox tenant, 2026-10-01, importing `0` four times with
167
+ `overwriteCells: true` on every job and only `importZeros` varying:
168
+
169
+ | Target coordinate | State before | `importZeros` | Result |
170
+ |---|---|---|---|
171
+ | empty | — | `false` | **no cell created** |
172
+ | holds `500` | value | `false` | **`500` survives** — the zero is skipped, not applied |
173
+ | empty | — | `true` | **`0` materialised** |
174
+ | holds `500` | value | `true` | **overwritten to `0`** |
175
+
176
+ The commit report agrees and is the cheapest check: `importedCells: []` for the
177
+ `false` pass, `count: 2` for the `true` pass.
178
+
179
+ Three consequences worth stating, because each one has cost someone a day:
180
+
181
+ 1. **Expected-row counts must be computed over NON-ZERO source rows** whenever
182
+ `importZeros: false` — which is what the `columns` overload gives you. A reconciliation that counts every source row will read short and
183
+ false-red forever.
184
+ 2. **A zero import cannot clear a slice.** With `importZeros: false` it is a
185
+ no-op; with `true` it writes an explicit `0`, which is a materialised cell and
186
+ not the same thing as empty. To actually clear, use `model.updateCells` with
187
+ `value: null`.
188
+ 3. **A skipped zero is only harmless if the target's seed formula is fenced.**
189
+ If an ` IN` carries an unfenced seed (no `if(hasDMember(Versions>Actual), …)`),
190
+ the cell the import declined to create falls through to the formula, which
191
+ emits the Actual value into a plan version. That is one more reason seed
192
+ formulas are fenced to Actual.
193
+
194
+ Two earlier write-ups of this looked like a contradiction — one reported that
195
+ imports skip zeros, the other that a zero import leaves a materialised zero.
196
+ Both were right; neither recorded the flag it ran under.
197
+
198
+ ### Mechanics the SDK shape does not make obvious
199
+
200
+ - `POST /importJobs` requires `source` (e.g. `EXTERNAL_DATA`); without it the
201
+ create 400s with a bare `{"code":"BadRequest"}`.
202
+ - `POST /importJobs/{id}/detectDimensions` must run **after** the rows and
203
+ **before** `commit`. Skip it and commit fails with
204
+ `Column <X> has no detected dimensions. Please refresh` (with a closing "!").
205
+ - Every call needs the `X-TENANT-ID` header alongside the bearer token.
206
+ - Order: `create` → `importJobRows/batch` → `detectDimensions` → `commit`.
207
+
208
+ ---
209
+
210
+ ## How a review reads import jobs
211
+
212
+ The model-analysis pass enumerates every app and classifies each variable an import
213
+ job writes:
214
+
215
+ - **OK** — locked `IMPORT_` target.
216
+ - **CRITICAL** — import writes into an **unlocked** variable (a planner can overwrite
217
+ it; re-imports fight manual edits).
218
+ - **Confusion risk** — an `IMPORT_` variable that is *also* left unlocked for sheet
219
+ entry (input and feed at once).
220
+
221
+ It also checks how jobs touch **dimension tables**: additive (append new members) is
222
+ the recommended posture; jobs that **overwrite FK keys** every run are flagged,
223
+ especially when a sheet also exposes that FK for editing.
224
+
225
+ ---
226
+
227
+ ## Cross-references
228
+
229
+ - [`SKILL.md`](SKILL.md) — operational skill.
230
+ - [`cell-writes.md`](cell-writes.md) — insert vs import decision + `createImportJob` shapes.
231
+ - [`working-patterns.md`](../farseer-app-dev/working-patterns.md) — id resolution + reversible re-import.
232
+ - [`deploying-a-farseer-app.md`](deploying-a-farseer-app.md) — manifest, arguments, secret arguments.