@elementumai/edk 0.9.19 → 0.10.1

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 (240) hide show
  1. package/README.md +159 -7
  2. package/dist-sdk/src/catalog/factories.d.ts +33 -2
  3. package/dist-sdk/src/catalog/factories.js +40 -1
  4. package/dist-sdk/src/catalog/from-authoring.d.ts +13 -8
  5. package/dist-sdk/src/catalog/from-authoring.js +34 -9
  6. package/dist-sdk/src/catalog/index.d.ts +2 -2
  7. package/dist-sdk/src/catalog/index.js +2 -2
  8. package/dist-sdk/src/catalog/render.js +6 -6
  9. package/dist-sdk/src/catalog/token.js +11 -0
  10. package/dist-sdk/src/catalog/types.d.ts +40 -1
  11. package/dist-sdk/src/catalog/types.js +12 -0
  12. package/dist-sdk/src/core/triggers/on-demand.d.ts +40 -4
  13. package/dist-sdk/src/core/triggers/on-demand.js +17 -2
  14. package/dist-sdk/src/core/types/app.d.ts +2 -2
  15. package/dist-sdk/src/engine/build/context.d.ts +1 -1
  16. package/dist-sdk/src/engine/coverage-gaps.d.ts +86 -0
  17. package/dist-sdk/src/engine/coverage-gaps.js +156 -0
  18. package/dist-sdk/src/engine/ei/run.d.ts +169 -0
  19. package/dist-sdk/src/engine/ei/run.js +1219 -0
  20. package/dist-sdk/src/engine/logging.d.ts +17 -0
  21. package/dist-sdk/src/engine/logging.js +18 -0
  22. package/dist-sdk/src/engine/pull/catalog-ref.d.ts +16 -0
  23. package/dist-sdk/src/engine/pull/catalog-ref.js +24 -0
  24. package/dist-sdk/src/engine/pull/context.d.ts +68 -6
  25. package/dist-sdk/src/engine/pull/context.js +40 -1
  26. package/dist-sdk/src/engine/pull/procedure-refs.d.ts +8 -0
  27. package/dist-sdk/src/engine/pull/procedure-refs.js +28 -0
  28. package/dist-sdk/src/engine/pull/ref-only.d.ts +5 -0
  29. package/dist-sdk/src/engine/pull/ref-only.js +10 -7
  30. package/dist-sdk/src/engine/pull/result-output.d.ts +6 -0
  31. package/dist-sdk/src/engine/pull/result-output.js +113 -3
  32. package/dist-sdk/src/engine/tf/expr.js +1 -1
  33. package/dist-sdk/src/engine/tf/index.d.ts +2 -2
  34. package/dist-sdk/src/engine/tf/index.js +1 -1
  35. package/dist-sdk/src/engine/tf/labels.d.ts +28 -5
  36. package/dist-sdk/src/engine/tf/labels.js +54 -3
  37. package/dist-sdk/src/engine/tf/model.d.ts +22 -1
  38. package/dist-sdk/src/engine/tf/parse.js +93 -4
  39. package/dist-sdk/src/engine/tf/render.d.ts +4 -2
  40. package/dist-sdk/src/engine/tf/render.js +41 -7
  41. package/dist-sdk/src/engine/validation.d.ts +44 -0
  42. package/dist-sdk/src/engine/validation.js +95 -0
  43. package/dist-sdk/src/entities/access-policy/from-tf.js +38 -1
  44. package/dist-sdk/src/entities/access-policy/principal.d.ts +2 -0
  45. package/dist-sdk/src/entities/access-policy/to-tf.js +5 -2
  46. package/dist-sdk/src/entities/agent/build.d.ts +4 -16
  47. package/dist-sdk/src/entities/agent/build.js +40 -5
  48. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.d.ts +3 -0
  49. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +46 -21
  50. package/dist-sdk/src/entities/agent/from-tf.js +11 -3
  51. package/dist-sdk/src/entities/agent/module.js +2 -0
  52. package/dist-sdk/src/entities/agent/to-tf.js +20 -6
  53. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +10 -1
  54. package/dist-sdk/src/entities/agent-tool/from-tf.js +18 -6
  55. package/dist-sdk/src/entities/agent-tool/module.js +3 -0
  56. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +2 -2
  57. package/dist-sdk/src/entities/agent-tool/to-tf.js +27 -5
  58. package/dist-sdk/src/entities/app/auto-share.d.ts +38 -0
  59. package/dist-sdk/src/entities/app/auto-share.js +49 -0
  60. package/dist-sdk/src/entities/app/build.d.ts +105 -22
  61. package/dist-sdk/src/entities/app/build.js +107 -10
  62. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +22 -1
  63. package/dist-sdk/src/entities/app/from-tf.d.ts +1 -0
  64. package/dist-sdk/src/entities/app/from-tf.js +11 -2
  65. package/dist-sdk/src/entities/app/index.d.ts +7 -6
  66. package/dist-sdk/src/entities/app/index.js +2 -8
  67. package/dist-sdk/src/entities/app/module.js +5 -2
  68. package/dist-sdk/src/entities/app/to-tf.d.ts +6 -0
  69. package/dist-sdk/src/entities/app/to-tf.js +40 -13
  70. package/dist-sdk/src/entities/automation/build.d.ts +56 -6
  71. package/dist-sdk/src/entities/automation/build.js +230 -26
  72. package/dist-sdk/src/entities/automation/calculation-expression.d.ts +7 -7
  73. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +41 -5
  74. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +470 -58
  75. package/dist-sdk/src/entities/automation/from-tf.d.ts +37 -8
  76. package/dist-sdk/src/entities/automation/from-tf.js +791 -131
  77. package/dist-sdk/src/entities/automation/generated/task-output-contracts.d.ts +33 -0
  78. package/dist-sdk/src/entities/automation/generated/task-output-contracts.js +183 -0
  79. package/dist-sdk/src/entities/automation/index.d.ts +2 -2
  80. package/dist-sdk/src/entities/automation/index.js +1 -1
  81. package/dist-sdk/src/entities/automation/module.d.ts +3 -0
  82. package/dist-sdk/src/entities/automation/module.js +107 -15
  83. package/dist-sdk/src/entities/automation/output-field-types.d.ts +7 -0
  84. package/dist-sdk/src/entities/automation/output-field-types.js +36 -0
  85. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +9 -0
  86. package/dist-sdk/src/entities/automation/output-key-wire-name.js +13 -0
  87. package/dist-sdk/src/entities/automation/script-code.d.ts +20 -0
  88. package/dist-sdk/src/entities/automation/script-code.js +109 -5
  89. package/dist-sdk/src/entities/automation/system-ref-name.js +4 -0
  90. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +39 -0
  91. package/dist-sdk/src/entities/automation/task-output-labels.js +85 -0
  92. package/dist-sdk/src/entities/automation/tasks.d.ts +116 -22
  93. package/dist-sdk/src/entities/automation/tasks.js +86 -24
  94. package/dist-sdk/src/entities/automation/to-tf.d.ts +24 -3
  95. package/dist-sdk/src/entities/automation/to-tf.js +1082 -132
  96. package/dist-sdk/src/entities/automation/triggers.d.ts +12 -6
  97. package/dist-sdk/src/entities/automation/triggers.js +4 -0
  98. package/dist-sdk/src/entities/automation/types.d.ts +9 -0
  99. package/dist-sdk/src/entities/automation/types.js +19 -0
  100. package/dist-sdk/src/entities/automation/value-ref-tf.d.ts +1 -1
  101. package/dist-sdk/src/entities/automation/value-ref-tf.js +1 -1
  102. package/dist-sdk/src/entities/chart/from-tf.js +9 -1
  103. package/dist-sdk/src/entities/chart/to-tf.js +10 -3
  104. package/dist-sdk/src/entities/data-lookup/module.js +2 -0
  105. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +24 -31
  106. package/dist-sdk/src/entities/dynamic-layout/to-tf.d.ts +1 -0
  107. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +7 -5
  108. package/dist-sdk/src/entities/element/build.d.ts +28 -13
  109. package/dist-sdk/src/entities/element/build.js +5 -1
  110. package/dist-sdk/src/entities/element/from-tf.d.ts +11 -0
  111. package/dist-sdk/src/entities/element/from-tf.js +62 -1
  112. package/dist-sdk/src/entities/element/index.d.ts +5 -3
  113. package/dist-sdk/src/entities/element/index.js +1 -0
  114. package/dist-sdk/src/entities/element/module.js +9 -3
  115. package/dist-sdk/src/entities/element/to-tf.d.ts +1 -1
  116. package/dist-sdk/src/entities/element/to-tf.js +16 -16
  117. package/dist-sdk/src/entities/field/build.d.ts +17 -3
  118. package/dist-sdk/src/entities/field/build.js +3 -0
  119. package/dist-sdk/src/entities/field/calc.d.ts +7 -7
  120. package/dist-sdk/src/entities/field/from-tf.d.ts +5 -4
  121. package/dist-sdk/src/entities/field/from-tf.js +244 -49
  122. package/dist-sdk/src/entities/field/media-types.d.ts +6 -0
  123. package/dist-sdk/src/entities/field/media-types.js +30 -0
  124. package/dist-sdk/src/entities/field/module.js +2 -0
  125. package/dist-sdk/src/entities/field/options.js +6 -1
  126. package/dist-sdk/src/entities/field/to-tf.d.ts +26 -5
  127. package/dist-sdk/src/entities/field/to-tf.js +137 -15
  128. package/dist-sdk/src/entities/field/types.d.ts +2 -0
  129. package/dist-sdk/src/entities/field/types.js +27 -0
  130. package/dist-sdk/src/entities/file-reader/from-tf.js +14 -5
  131. package/dist-sdk/src/entities/file-reader/module.js +1 -0
  132. package/dist-sdk/src/entities/file-reader/types.d.ts +2 -0
  133. package/dist-sdk/src/entities/file-reader/types.js +1 -1
  134. package/dist-sdk/src/entities/portal/build.d.ts +26 -0
  135. package/dist-sdk/src/entities/portal/build.js +37 -0
  136. package/dist-sdk/src/entities/portal/from-tf.d.ts +27 -0
  137. package/dist-sdk/src/entities/portal/from-tf.js +75 -0
  138. package/dist-sdk/src/entities/portal/index.d.ts +1 -0
  139. package/dist-sdk/src/entities/portal/index.js +1 -0
  140. package/dist-sdk/src/entities/portal/module.d.ts +3 -0
  141. package/dist-sdk/src/entities/portal/module.js +28 -0
  142. package/dist-sdk/src/entities/portal/to-tf.d.ts +12 -0
  143. package/dist-sdk/src/entities/portal/to-tf.js +73 -0
  144. package/dist-sdk/src/entities/portal/types.d.ts +1 -0
  145. package/dist-sdk/src/entities/portal/types.js +1 -0
  146. package/dist-sdk/src/entities/relationship/module.js +2 -0
  147. package/dist-sdk/src/entities/relationship/pair-check.d.ts +7 -0
  148. package/dist-sdk/src/entities/relationship/pair-check.js +68 -0
  149. package/dist-sdk/src/entities/relationship/to-tf.d.ts +2 -0
  150. package/dist-sdk/src/entities/relationship/to-tf.js +12 -3
  151. package/dist-sdk/src/entities/role/from-tf.js +3 -3
  152. package/dist-sdk/src/entities/role/to-tf.d.ts +2 -0
  153. package/dist-sdk/src/entities/role/to-tf.js +12 -4
  154. package/dist-sdk/src/entities/search-table/module.js +1 -0
  155. package/dist-sdk/src/entities/search-table/to-tf.d.ts +1 -0
  156. package/dist-sdk/src/entities/search-table/to-tf.js +1 -1
  157. package/dist-sdk/src/entities/service-account/build.d.ts +15 -2
  158. package/dist-sdk/src/entities/service-account/from-tf.d.ts +2 -4
  159. package/dist-sdk/src/entities/service-account/from-tf.js +20 -8
  160. package/dist-sdk/src/entities/service-account/role-fidelity.d.ts +56 -0
  161. package/dist-sdk/src/entities/service-account/role-fidelity.js +243 -0
  162. package/dist-sdk/src/entities/service-account/to-tf.d.ts +14 -0
  163. package/dist-sdk/src/entities/service-account/to-tf.js +17 -0
  164. package/dist-sdk/src/entities/service-account/workspace-owner.d.ts +16 -0
  165. package/dist-sdk/src/entities/service-account/workspace-owner.js +22 -0
  166. package/dist-sdk/src/entities/shared/aspect-target-from-tf.d.ts +1 -1
  167. package/dist-sdk/src/entities/shared/aspect-target-from-tf.js +5 -2
  168. package/dist-sdk/src/entities/shared/filters/ast.d.ts +23 -1
  169. package/dist-sdk/src/entities/shared/filters/ast.js +2 -0
  170. package/dist-sdk/src/entities/shared/filters/index.d.ts +7 -0
  171. package/dist-sdk/src/entities/shared/filters/index.js +19 -4
  172. package/dist-sdk/src/entities/shared/filters/print.js +24 -3
  173. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +7 -0
  174. package/dist-sdk/src/entities/shared/filters/tf-codec.js +46 -7
  175. package/dist-sdk/src/entities/shared/filters/types.d.ts +15 -2
  176. package/dist-sdk/src/entities/shared/ref.d.ts +5 -7
  177. package/dist-sdk/src/entities/shared/ref.js +5 -7
  178. package/dist-sdk/src/entities/shared/tool-field-ref.d.ts +9 -0
  179. package/dist-sdk/src/entities/shared/tool-field-ref.js +35 -2
  180. package/dist-sdk/src/entities/shared/types/field-types.d.ts +20 -5
  181. package/dist-sdk/src/entities/shared/types/field-types.js +8 -0
  182. package/dist-sdk/src/entities/skill/access-policy.js +3 -3
  183. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +3 -0
  184. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +14 -6
  185. package/dist-sdk/src/entities/skill/from-tf.js +70 -15
  186. package/dist-sdk/src/entities/skill/input-coverage.d.ts +7 -0
  187. package/dist-sdk/src/entities/skill/input-coverage.js +11 -0
  188. package/dist-sdk/src/entities/skill/module.js +2 -0
  189. package/dist-sdk/src/entities/skill/to-tf.js +14 -4
  190. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +2 -0
  191. package/dist-sdk/src/entities/skill-tool/from-tf.js +49 -10
  192. package/dist-sdk/src/entities/skill-tool/module.js +3 -0
  193. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +2 -8
  194. package/dist-sdk/src/entities/skill-tool/to-tf.js +49 -14
  195. package/dist-sdk/src/entities/table/catalog.d.ts +14 -0
  196. package/dist-sdk/src/entities/table/catalog.js +66 -0
  197. package/dist-sdk/src/entities/table/module.js +2 -1
  198. package/dist-sdk/src/entities/table/to-tf.js +2 -2
  199. package/dist-sdk/src/entities/task/build.d.ts +8 -0
  200. package/dist-sdk/src/entities/task/build.js +10 -1
  201. package/dist-sdk/src/entities/task/from-tf.d.ts +4 -2
  202. package/dist-sdk/src/entities/task/from-tf.js +7 -2
  203. package/dist-sdk/src/entities/task/module.js +2 -2
  204. package/dist-sdk/src/entities/task/to-tf.d.ts +8 -4
  205. package/dist-sdk/src/entities/task/to-tf.js +28 -1
  206. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.d.ts +49 -9
  207. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.js +71 -18
  208. package/dist-sdk/src/entities/view/agent-view-layout-to-tf.d.ts +19 -4
  209. package/dist-sdk/src/entities/view/agent-view-layout-to-tf.js +82 -10
  210. package/dist-sdk/src/entities/view/agent-view-layout.d.ts +53 -23
  211. package/dist-sdk/src/entities/view/agent-view-layout.js +1 -1
  212. package/dist-sdk/src/entities/view/from-tf.d.ts +3 -2
  213. package/dist-sdk/src/entities/view/from-tf.js +133 -13
  214. package/dist-sdk/src/entities/view/module.js +30 -3
  215. package/dist-sdk/src/entities/view/to-tf.d.ts +20 -8
  216. package/dist-sdk/src/entities/view/to-tf.js +86 -18
  217. package/dist-sdk/src/entities/view/types.d.ts +4 -15
  218. package/dist-sdk/src/entities/view/types.js +15 -15
  219. package/dist-sdk/src/entities/visual-flow/build.d.ts +16 -1
  220. package/dist-sdk/src/entities/visual-flow/build.js +9 -0
  221. package/dist-sdk/src/entities/visual-flow/from-tf.d.ts +14 -0
  222. package/dist-sdk/src/entities/visual-flow/from-tf.js +51 -3
  223. package/dist-sdk/src/entities/visual-flow/index.d.ts +1 -1
  224. package/dist-sdk/src/entities/visual-flow/index.js +1 -1
  225. package/dist-sdk/src/entities/visual-flow/printer.js +19 -0
  226. package/dist-sdk/src/entities/visual-flow/to-tf.js +87 -3
  227. package/dist-sdk/src/entities/widget/build.d.ts +16 -1
  228. package/dist-sdk/src/entities/widget/build.js +11 -1
  229. package/dist-sdk/src/entities/widget/from-tf.js +3 -3
  230. package/dist-sdk/src/entities/widget/to-tf.d.ts +2 -0
  231. package/dist-sdk/src/entities/widget/to-tf.js +41 -14
  232. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +148 -13
  233. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  234. package/dist-sdk/src/graph/index.d.ts +12 -0
  235. package/dist-sdk/src/graph/index.js +21 -0
  236. package/dist-sdk/src/graph/modules.js +2 -0
  237. package/dist-sdk/src/graph/types.d.ts +83 -4
  238. package/dist-sdk/src/internal/sdk-package.d.ts +1 -1
  239. package/dist-sdk/src/internal/sdk-package.js +1 -0
  240. package/package.json +16 -3
@@ -35,9 +35,11 @@ import { sdkPackageSpecifier } from "../../../internal/sdk-package.js";
35
35
  import { safeIdentifier } from "../../../internal/safe-identifier.js";
36
36
  import { isRefExpr } from "../../shared/filters/ast.js";
37
37
  import { printFilter as printCanonicalFilter } from "../../shared/filters/print.js";
38
+ import { isUuid } from "../../../engine/tf/labels.js";
38
39
  import { UNRESOLVED_FIELD_MARKER } from "../../shared/ref.js";
39
40
  import { UNRESOLVED_REFERENCE_MARKER } from "../filter.js";
40
41
  import { INJECTED_TS_IGNORE, scriptFileText } from "../script-code.js";
42
+ import { isRequiredSecret } from "../tasks.js";
41
43
  import { unresolvedReferenceComment } from "../../../engine/pull/ref-only.js";
42
44
  // ---------------------------------------------------------------------------
43
45
  // Factory name mapping — decoded task `factory` → v2 factory function name.
@@ -53,6 +55,7 @@ const TASK_FACTORY_V2 = {
53
55
  executeScript: "executeScript",
54
56
  createRecord: "createRecord",
55
57
  updateField: "updateField",
58
+ bulkUpdateFields: "bulkUpdateFields",
56
59
  apiRequest: "apiRequest",
57
60
  aiAgent: "aiAgent",
58
61
  aiSearchTable: "aiSearchTable",
@@ -113,23 +116,281 @@ const ARG_RENAMES = {
113
116
  generateReport: { recordRef: "record" },
114
117
  };
115
118
  const REVISION_PROPERTY = /^([ \t]*revision[ \t]*:[ \t]*)(["'`])([^"'`\r\n]+)\2[ \t]*,?[ \t]*$/m;
116
- /** Keep the authored publish token across pull; live workflow versions are not author intent. */
117
- export function preserveAuthoredAutomationRevision(freshSource, existingSource) {
119
+ /** Numeric dotted revision parts, or undefined when the token is not dotted numeric. */
120
+ function revisionParts(revision) {
121
+ if (!/^\d+(\.\d+)*$/.test(revision))
122
+ return undefined;
123
+ return revision.split(".").map(Number);
124
+ }
125
+ /** True when `a` is at least `b`; non-numeric tokens only compare equal. */
126
+ function revisionAtLeast(a, b) {
127
+ if (a === b)
128
+ return true;
129
+ const pa = revisionParts(a);
130
+ const pb = revisionParts(b);
131
+ if (pa === undefined || pb === undefined)
132
+ return false;
133
+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
134
+ const d = (pa[i] ?? 0) - (pb[i] ?? 0);
135
+ if (d !== 0)
136
+ return d > 0;
137
+ }
138
+ return true;
139
+ }
140
+ /** workflow_revision of the state workflow_publish whose id is `platformId`. */
141
+ function stateWorkflowRevision(context) {
142
+ if (context?.platformId === undefined || context.stateResources === undefined)
143
+ return undefined;
144
+ for (const resource of context.stateResources) {
145
+ if (resource.type !== "elementum_workflow_publish")
146
+ continue;
147
+ for (const instance of resource.instances ?? []) {
148
+ const attrs = instance.attributes;
149
+ if (attrs?.id === context.platformId && typeof attrs.workflow_revision === "string")
150
+ return attrs.workflow_revision;
151
+ }
152
+ }
153
+ return undefined;
154
+ }
155
+ /**
156
+ * Keep the authored publish token across pull; live workflow versions are not
157
+ * author intent (#693). The token must still agree with what state tracks, or a
158
+ * fresh-state pull + adopt plans a republish that downgrades the live revision
159
+ * (#1590): adoption imports the live revision, and the provider's Read keeps
160
+ * whatever revision state holds. So the authored token wins only when it is at
161
+ * least the baseline — the state revision, or the freshly printed live revision
162
+ * when state has no entry yet (adoption is about to import it). An older
163
+ * authored token is replaced by that baseline. Without readable state the
164
+ * pre-#1590 behaviour (always keep authored) is unchanged.
165
+ */
166
+ export function preserveAuthoredAutomationRevision(freshSource, existingSource, context) {
118
167
  const authored = REVISION_PROPERTY.exec(existingSource)?.[3];
119
168
  if (authored === undefined || authored === "")
120
169
  return freshSource;
121
- return freshSource.replace(REVISION_PROPERTY, (_line, prefix) => `${prefix}${JSON.stringify(authored)},`);
170
+ let keep = authored;
171
+ if (context?.stateResources !== undefined) {
172
+ const baseline = stateWorkflowRevision(context) ?? REVISION_PROPERTY.exec(freshSource)?.[3];
173
+ if (baseline !== undefined && !revisionAtLeast(authored, baseline))
174
+ keep = baseline;
175
+ }
176
+ return freshSource.replace(REVISION_PROPERTY, (_line, prefix) => `${prefix}${JSON.stringify(keep)},`);
122
177
  }
178
+ /** Index just past the string literal opening at `i` (`'` or `"`), or -1 when unterminated. */
179
+ function skipQuoted(source, i) {
180
+ const q = source[i];
181
+ for (let j = i + 1; j < source.length; j++) {
182
+ const ch = source[j];
183
+ if (ch === "\\") {
184
+ j++;
185
+ continue;
186
+ }
187
+ if (ch === q)
188
+ return j + 1;
189
+ if (ch === "\n")
190
+ return -1;
191
+ }
192
+ return -1;
193
+ }
194
+ /** Index just past the template literal opening at `i`, skipping `${...}` holes, or -1. */
195
+ function skipTemplate(source, i) {
196
+ for (let j = i + 1; j < source.length; j++) {
197
+ const ch = source[j];
198
+ if (ch === "\\") {
199
+ j++;
200
+ continue;
201
+ }
202
+ if (ch === "`")
203
+ return j + 1;
204
+ if (ch === "$" && source[j + 1] === "{") {
205
+ const end = skipBalanced(source, j + 1);
206
+ if (end < 0)
207
+ return -1;
208
+ j = end - 1;
209
+ }
210
+ }
211
+ return -1;
212
+ }
213
+ /** Index just past the regex literal opening at `i`, or -1. */
214
+ function skipRegex(source, i) {
215
+ let inClass = false;
216
+ for (let j = i + 1; j < source.length; j++) {
217
+ const ch = source[j];
218
+ if (ch === "\\") {
219
+ j++;
220
+ continue;
221
+ }
222
+ if (ch === "\n")
223
+ return -1;
224
+ if (inClass) {
225
+ if (ch === "]")
226
+ inClass = false;
227
+ }
228
+ else if (ch === "[") {
229
+ inClass = true;
230
+ }
231
+ else if (ch === "/") {
232
+ let k = j + 1;
233
+ while (k < source.length && /[a-z]/i.test(source[k]))
234
+ k++;
235
+ return k;
236
+ }
237
+ }
238
+ return -1;
239
+ }
240
+ /** Keyword operators after which an expression, and so a regex literal, can begin. */
241
+ const REGEX_PRECEDING_KEYWORDS = new Set([
242
+ "return", "typeof", "instanceof", "in", "of", "new", "delete", "void", "throw", "case", "do", "else", "yield", "await",
243
+ ]);
244
+ const WORD_CHAR = /[\w$\u0080-￿]/;
123
245
  /**
124
- * Extract a string-literal (or `+`-concatenated literals) description from
125
- * authored automation source. Pull reprints often omit description entirely
126
- * because Go export cannot read it back from the platform today.
246
+ * Walk TypeScript source token by token, skipping comments, strings, template
247
+ * literals and regex literals. `visit` sees the first character of every
248
+ * other token (each punctuator, word, number, string or template literal),
249
+ * with the bracket depth relative to `start`: an opening bracket is seen at
250
+ * its inner depth and a closing bracket at its outer depth, so both brackets
251
+ * of a group at `start` are seen at depth 1 and 0. `visit` returns true to
252
+ * stop. Returns the index where the walk stopped, or -1 on malformed input or
253
+ * end of source.
254
+ *
255
+ * Regex or division: `/` starts a regex literal only where an expression can
256
+ * begin, i.e. at the start of input, after a punctuator other than a closing
257
+ * bracket (`( , = : [ ! & | ? { ; + - * % < > ~ ^`, their compound forms,
258
+ * `=>`, prefix `++`/`--`), or after a keyword operator (`return`, `typeof`,
259
+ * …). After `)`, `]`, `}`, an identifier, a property name (even a keyword
260
+ * one, as in `sizes.in`), a number, a postfix `++`/`--`, or a string,
261
+ * template or regex literal, it divides. Treating `/` after `}` (and `)`) as
262
+ * division misreads a regex that opens a statement after a block or an
263
+ * `if (…)` condition; that is the accepted trade-off of not running a real
264
+ * parser, and such a statement is rare in pulled source.
127
265
  */
128
- export function extractAuthoredAutomationDescription(source) {
129
- const header = /(?:^|\n)[ \t]*description[ \t]*:/m.exec(source);
130
- if (header === null || header.index === undefined)
266
+ function walkSource(source, start, visit) {
267
+ let depth = 0;
268
+ let regexAllowed = true;
269
+ let afterDot = false;
270
+ for (let i = start; i < source.length;) {
271
+ const ch = source[i];
272
+ const next = source[i + 1];
273
+ if (ch === "/" && next === "/") {
274
+ const eol = source.indexOf("\n", i);
275
+ i = eol < 0 ? source.length : eol + 1;
276
+ continue;
277
+ }
278
+ if (ch === "/" && next === "*") {
279
+ const close = source.indexOf("*/", i + 2);
280
+ if (close < 0)
281
+ return -1;
282
+ i = close + 2;
283
+ continue;
284
+ }
285
+ if (/\s/.test(ch)) {
286
+ i++;
287
+ continue;
288
+ }
289
+ if (ch === '"' || ch === "'" || ch === "`") {
290
+ if (visit(i, depth))
291
+ return i;
292
+ const end = ch === "`" ? skipTemplate(source, i) : skipQuoted(source, i);
293
+ if (end < 0)
294
+ return -1;
295
+ i = end;
296
+ regexAllowed = false;
297
+ afterDot = false;
298
+ continue;
299
+ }
300
+ if (WORD_CHAR.test(ch)) {
301
+ if (visit(i, depth))
302
+ return i;
303
+ let end = i + 1;
304
+ while (end < source.length && WORD_CHAR.test(source[end]))
305
+ end++;
306
+ // A property name (after `.`) is never a keyword operator.
307
+ regexAllowed = !afterDot && REGEX_PRECEDING_KEYWORDS.has(source.slice(i, end));
308
+ afterDot = false;
309
+ i = end;
310
+ continue;
311
+ }
312
+ if (ch === "/" && regexAllowed) {
313
+ const end = skipRegex(source, i);
314
+ if (end < 0)
315
+ return -1;
316
+ i = end;
317
+ regexAllowed = false;
318
+ afterDot = false;
319
+ continue;
320
+ }
321
+ if ((ch === "+" || ch === "-") && next === ch) {
322
+ if (visit(i, depth))
323
+ return i;
324
+ // Postfix after an operand keeps the operand's role; prefix begins one.
325
+ i += 2;
326
+ afterDot = false;
327
+ continue;
328
+ }
329
+ if (ch === "{" || ch === "(" || ch === "[")
330
+ depth++;
331
+ if (ch === "}" || ch === ")" || ch === "]") {
332
+ depth--;
333
+ if (depth < 0)
334
+ return -1;
335
+ }
336
+ if (visit(i, depth))
337
+ return i;
338
+ regexAllowed = !(ch === "}" || ch === ")" || ch === "]" || ch === ".");
339
+ afterDot = ch === ".";
340
+ i++;
341
+ }
342
+ return -1;
343
+ }
344
+ /** Index just past the bracket group opening at `open`, or -1. */
345
+ function skipBalanced(source, open) {
346
+ const end = walkSource(source, open, (index, depth) => depth === 0 && index > open);
347
+ return end < 0 ? -1 : end + 1;
348
+ }
349
+ /**
350
+ * Top-level properties of the options object passed to `automation({ ... })`.
351
+ * Keys nested deeper (a step's or trigger input's `description` inside the
352
+ * actions chain) are not included, whatever their order relative to `actions`.
353
+ * Undefined when no `automation({` call is found.
354
+ */
355
+ function automationOptionProperties(source) {
356
+ let objectStart = -1;
357
+ walkSource(source, 0, (index) => {
358
+ const match = /^automation\s*\(\s*\{/.exec(source.slice(index, index + 64));
359
+ if (match === null || /[\w$.]/.test(source[index - 1] ?? ""))
360
+ return false;
361
+ objectStart = index + match[0].length - 1;
362
+ return true;
363
+ });
364
+ if (objectStart < 0)
131
365
  return undefined;
132
- let i = header.index + header[0].length;
366
+ const properties = [];
367
+ let expectKey = true;
368
+ const end = walkSource(source, objectStart, (index, depth) => {
369
+ if (depth === 0)
370
+ return true;
371
+ if (index === objectStart)
372
+ return false;
373
+ if (depth !== 1)
374
+ return false;
375
+ const ch = source[index];
376
+ if (ch === ",") {
377
+ expectKey = true;
378
+ return false;
379
+ }
380
+ if (!expectKey)
381
+ return false;
382
+ expectKey = false;
383
+ const key = /^(?:([A-Za-z_$][\w$]*)|"([^"\\\n]*)"|'([^'\\\n]*)')\s*:/.exec(source.slice(index, index + 256));
384
+ if (key !== null) {
385
+ properties.push({ key: key[1] ?? key[2] ?? key[3], keyStart: index, valueStart: index + key[0].length });
386
+ }
387
+ return false;
388
+ });
389
+ return { properties, complete: end >= 0 };
390
+ }
391
+ /** A string literal or `+`-concatenation of string literals starting at `start`, or undefined. */
392
+ function parseStringConcat(source, start) {
393
+ let i = start;
133
394
  const parts = [];
134
395
  while (i < source.length) {
135
396
  while (i < source.length && /\s/.test(source[i]))
@@ -148,6 +409,8 @@ export function extractAuthoredAutomationDescription(source) {
148
409
  i += 2;
149
410
  continue;
150
411
  }
412
+ if (q === "`" && ch === "$" && source[i + 1] === "{")
413
+ return undefined;
151
414
  if (ch === q)
152
415
  break;
153
416
  raw += ch;
@@ -175,6 +438,21 @@ export function extractAuthoredAutomationDescription(source) {
175
438
  const joined = parts.join("");
176
439
  return joined === "" ? undefined : joined;
177
440
  }
441
+ /**
442
+ * Extract a string-literal (or `+`-concatenated literals) description from
443
+ * authored automation source. Pull reprints often omit description entirely
444
+ * because Go export cannot read it back from the platform today.
445
+ *
446
+ * Only the automation's own `description` counts: the top-level key of the
447
+ * `automation({ ... })` options object, in any key order. A notification
448
+ * step's or on-demand input's `description` inside the actions chain is not
449
+ * the automation's.
450
+ */
451
+ export function extractAuthoredAutomationDescription(source) {
452
+ // A description read before an unscannable part is still the top-level one.
453
+ const description = automationOptionProperties(source)?.properties.find((property) => property.key === "description");
454
+ return description === undefined ? undefined : parseStringConcat(source, description.valueStart);
455
+ }
178
456
  /**
179
457
  * Keep a non-empty authored `description` across pull.
180
458
  *
@@ -183,31 +461,49 @@ export function extractAuthoredAutomationDescription(source) {
183
461
  * drops the attribute. Without this merge, pull → reprint → build clears
184
462
  * description from config while state still has it, forcing an adopt update.
185
463
  * Prefer a freshly pulled description when present; only reinstate the
186
- * authored value when the fresh source omitted it.
464
+ * authored value when the fresh source omitted it. Both checks and the
465
+ * insertion point are scoped to the automation's own options object, so a
466
+ * step's `description` neither hides nor supplies the automation's.
467
+ *
468
+ * Fail safe: when the fresh source cannot be scanned to its end (for example
469
+ * a pulled script body the scanner cannot tokenize), the authored description
470
+ * is still restored once the scan has reached the top-level `actions` key.
471
+ * The pull printer emits every other option, description included, before
472
+ * `actions`, so at that point the fresh source provably has no description of
473
+ * its own and the insertion point was read exactly. A scan that stops before
474
+ * `actions` leaves the fresh source unchanged.
187
475
  */
188
476
  export function preserveAuthoredAutomationDescription(freshSource, existingSource) {
189
477
  const authored = extractAuthoredAutomationDescription(existingSource);
190
478
  if (authored === undefined)
191
479
  return freshSource;
192
- if (extractAuthoredAutomationDescription(freshSource) !== undefined)
480
+ const options = automationOptionProperties(freshSource);
481
+ if (options === undefined)
193
482
  return freshSource;
194
- const revisionLine = REVISION_PROPERTY.exec(freshSource);
195
- if (revisionLine !== null && revisionLine.index !== undefined) {
196
- const insertAt = revisionLine.index + revisionLine[0].length;
197
- const indent = /^[ \t]*/.exec(revisionLine[0])?.[0] ?? " ";
198
- return `${freshSource.slice(0, insertAt)}\n${indent}description: ${JSON.stringify(authored)},${freshSource.slice(insertAt)}`;
199
- }
200
- const nameLine = /^([ \t]*name[ \t]*:[ \t]*)(["'`])([^"'`\r\n]*)\2[ \t]*,?[ \t]*$/m.exec(freshSource);
201
- if (nameLine !== null && nameLine.index !== undefined) {
202
- const insertAt = nameLine.index + nameLine[0].length;
203
- const indent = /^[ \t]*/.exec(nameLine[0])?.[0] ?? " ";
483
+ const { properties } = options;
484
+ if (properties.some((property) => property.key === "description"))
485
+ return freshSource;
486
+ if (!options.complete && !properties.some((property) => property.key === "actions"))
487
+ return freshSource;
488
+ for (const key of ["revision", "name"]) {
489
+ const property = properties.find((candidate) => candidate.key === key);
490
+ if (property === undefined)
491
+ continue;
492
+ const lineStart = freshSource.lastIndexOf("\n", property.keyStart) + 1;
493
+ const lineEnd = freshSource.indexOf("\n", property.keyStart);
494
+ const line = freshSource.slice(lineStart, lineEnd < 0 ? freshSource.length : lineEnd);
495
+ // Insert on the line after a single-line `key: "literal",` property only.
496
+ if (!/^[ \t]*[\w$"']+[ \t]*:[ \t]*(["'`])[^"'`\r\n]*\1[ \t]*,?[ \t]*$/.test(line))
497
+ continue;
498
+ const insertAt = lineStart + line.length;
499
+ const indent = /^[ \t]*/.exec(line)?.[0] ?? " ";
204
500
  return `${freshSource.slice(0, insertAt)}\n${indent}description: ${JSON.stringify(authored)},${freshSource.slice(insertAt)}`;
205
501
  }
206
502
  return freshSource;
207
503
  }
208
504
  /** Pull merge: preserve authored revision + description across live reprint. */
209
- export function preserveAuthoredAutomationPullMeta(freshSource, existingSource) {
210
- return preserveAuthoredAutomationDescription(preserveAuthoredAutomationRevision(freshSource, existingSource), existingSource);
505
+ export function preserveAuthoredAutomationPullMeta(freshSource, existingSource, context) {
506
+ return preserveAuthoredAutomationDescription(preserveAuthoredAutomationRevision(freshSource, existingSource, context), existingSource);
211
507
  }
212
508
  function emptyImports() {
213
509
  return {
@@ -215,15 +511,27 @@ function emptyImports() {
215
511
  factories: new Set(),
216
512
  aliases: new Map(),
217
513
  usesTemplate: false,
514
+ usesSecret: false,
218
515
  usesUnresolvedField: false,
219
516
  usesUnsafePlatformId: false,
220
517
  usesOrg: false,
221
518
  usesScriptInput: false,
222
519
  usesRefType: false,
520
+ usesTableSearchTableRef: false,
223
521
  };
224
522
  }
225
523
  /** Folder (inside the automations folder) that holds printed script files. */
226
524
  export const SCRIPT_FILES_DIR = "scripts";
525
+ /**
526
+ * Key of the placeholder printed for a forEach item reference to a sibling
527
+ * loop (no authored form, #1320). Build fails on it; re-pull archives a file
528
+ * that still carries it.
529
+ */
530
+ export const UNRESOLVED_FOR_EACH_ITEM_KEY = "__edkUnresolvedForEachItem";
531
+ /** True when automation source still carries the unbuildable sibling-loop placeholder. */
532
+ export function hasUnresolvedForEachItem(source) {
533
+ return source.includes(`${UNRESOLVED_FOR_EACH_ITEM_KEY}:`);
534
+ }
227
535
  /** Reprint extract-time owner handles as the authored catalog key. */
228
536
  function catalogOwnerHandle(state, appHandle) {
229
537
  if (appHandle === state.ownerHandle || appHandle === state.appRefName)
@@ -320,6 +628,8 @@ function printAutomation(ast, opts, externalScripts) {
320
628
  taskFactoriesByRefKey,
321
629
  ...(opts.foreignAppRefNames !== undefined && { foreignAppRefNames: opts.foreignAppRefNames }),
322
630
  ...(opts.foreignElementRefNames !== undefined && { foreignElementRefNames: opts.foreignElementRefNames }),
631
+ ...(opts.foreignAppRefNamespaces !== undefined && { foreignAppRefNamespaces: opts.foreignAppRefNamespaces }),
632
+ ...(opts.foreignElementRefNamespaces !== undefined && { foreignElementRefNamespaces: opts.foreignElementRefNamespaces }),
323
633
  ...(opts.foreignTaskRefNames !== undefined && { foreignTaskRefNames: opts.foreignTaskRefNames }),
324
634
  };
325
635
  const triggerCode = printTrigger(ast.trigger, state);
@@ -450,8 +760,13 @@ function renderHeader(ast, imports, _opts) {
450
760
  return alias ? `${name} as ${alias}` : name;
451
761
  });
452
762
  lines.push(`import { ${sortedSpecifiers.join(", ")} } from "${sdkPackageSpecifier("automations")}";`);
453
- if (imports.usesTemplate || imports.usesUnresolvedField || imports.usesUnsafePlatformId || imports.usesRefType) {
763
+ if (imports.usesTableSearchTableRef) {
764
+ lines.push(`import { tableSearchTableRef } from "${sdkPackageSpecifier("catalog")}";`);
765
+ }
766
+ if (imports.usesSecret || imports.usesTemplate || imports.usesUnresolvedField || imports.usesUnsafePlatformId || imports.usesRefType) {
454
767
  const coreNamed = [];
768
+ if (imports.usesSecret)
769
+ coreNamed.push("secret");
455
770
  if (imports.usesTemplate)
456
771
  coreNamed.push("template");
457
772
  if (imports.usesUnresolvedField)
@@ -484,7 +799,10 @@ function printTrigger(trigger, state) {
484
799
  if (i.multiple)
485
800
  parts.push(`multiple: ${i.multiple}`);
486
801
  if (i.type !== "record" && i.defaultValue !== undefined && i.defaultValue !== null) {
487
- parts.push(`defaultValue: ${literalJson(i.defaultValue)}`);
802
+ const user = i.defaultValue;
803
+ parts.push(`defaultValue: ${user.kind === "user" && typeof user.email === "string"
804
+ ? printRef(i.defaultValue, state, DEFAULT_CTX)
805
+ : literalJson(i.defaultValue)}`);
488
806
  }
489
807
  if (i.description !== undefined)
490
808
  parts.push(`description: ${quote(i.description)}`);
@@ -988,7 +1306,7 @@ function printTaskArgs(task, state, indent, refCtx) {
988
1306
  const lines = [];
989
1307
  for (const [k, v] of Object.entries(task.args)) {
990
1308
  // createRecord / updateField field-value maps → v2 `fields` array.
991
- if (k === "values" && (task.factory === "createRecord" || task.factory === "updateField")) {
1309
+ if (k === "values" && (task.factory === "createRecord" || task.factory === "updateField" || task.factory === "bulkUpdateFields")) {
992
1310
  lines.push(`${pad}fields: ${printFieldAssignments(v, target, state, indent + 1, refCtx)},`);
993
1311
  continue;
994
1312
  }
@@ -999,6 +1317,27 @@ function printTaskArgs(task, state, indent, refCtx) {
999
1317
  continue;
1000
1318
  }
1001
1319
  const outKey = renames[k] ?? k;
1320
+ const aspectId = typeof v === "string"
1321
+ ? v
1322
+ : isRefExpr(v) && v.kind === "literal" && typeof v.value === "string"
1323
+ ? v.value
1324
+ : undefined;
1325
+ if ((k === "app" || k === "relatedApp") && aspectId !== undefined && isUuid(aspectId)) {
1326
+ const catalogRef = state.foreignAppRefNamespaces?.get(aspectId)
1327
+ ?? state.foreignElementRefNamespaces?.get(aspectId)
1328
+ ?? state.foreignAppRefNames?.get(aspectId)
1329
+ ?? state.foreignElementRefNames?.get(aspectId);
1330
+ if (catalogRef !== undefined) {
1331
+ lines.push(`${pad}${safeKey(outKey)}: catalog.${catalogRef},`);
1332
+ continue;
1333
+ }
1334
+ // A raw aspect id is not a workspace app. A uuid refName makes build
1335
+ // look up an app that does not exist, and null fails the task's load.
1336
+ // The platform id has no refName, so build emits it as the literal.
1337
+ state.imports.usesUnsafePlatformId = true;
1338
+ lines.push(`${pad}${safeKey(outKey)}: unsafePlatformId(${quote(aspectId)}) as any /* unresolved aspect */${unresolvedReferenceComment(aspectId, state.unresolvedReferences)},`);
1339
+ continue;
1340
+ }
1002
1341
  if (task.factory === "aiSearchTable" && (k === "table" || outKey === "table")) {
1003
1342
  if (typeof v === "string" || (isRefExpr(v) && v.kind === "unresolved")) {
1004
1343
  const uuid = typeof v === "string" ? v : v.uuid;
@@ -1052,6 +1391,17 @@ function printFieldAssignments(values, target, state, indent, refCtx) {
1052
1391
  fieldExpr = (`{ __kind: "field", refName: ${quote(fieldKey)}, name: ${quote(fieldName)}, fieldType: ${quote(fieldType)}, ` +
1053
1392
  `parent: ${quote(parentRef)}, parentKind: "task", parentName: ${quote(parentName)} }`);
1054
1393
  }
1394
+ else if (target !== undefined &&
1395
+ !target.inCatalog &&
1396
+ assignment?.foreignOwnerNamespace !== undefined &&
1397
+ !fieldKey.startsWith(UNRESOLVED_FIELD_MARKER)) {
1398
+ const fieldType = assignment.foreignFieldType ?? "TEXT";
1399
+ const parentName = assignment.foreignOwnerName ?? target.name ?? target.refName;
1400
+ fieldExpr = (`{ __kind: "field", refName: ${quote(fieldKey)}, name: ${quote(assignment.foreignFieldName ?? fieldKey)}, fieldType: ${quote(fieldType)}, ` +
1401
+ `parent: ${quote(target.refName)}, parentKind: ${quote(target.kind)}, parentName: ${quote(parentName)}, ` +
1402
+ `parentNamespace: ${quote(assignment.foreignOwnerNamespace)} } ` +
1403
+ `/* TODO: foreign field — type unconfirmed (ei's elementum_field data source doesn't expose it) and refName is a best-effort guess; verify against "${parentName}" */`);
1404
+ }
1055
1405
  else if (target && !fieldKey.startsWith(UNRESOLVED_FIELD_MARKER)) {
1056
1406
  // `taskPrimaryTarget` already remapped the owner and resolved a
1057
1407
  // pulled foreign element. Remapping again via `catalogOwnerHandle`
@@ -1060,11 +1410,23 @@ function printFieldAssignments(values, target, state, indent, refCtx) {
1060
1410
  fieldExpr = `catalog.${target.refName}.fields${safeMember(fieldKey)}`;
1061
1411
  }
1062
1412
  else {
1063
- const uuid = fieldKey.startsWith(UNRESOLVED_FIELD_MARKER)
1413
+ const rawKey = fieldKey.startsWith(UNRESOLVED_FIELD_MARKER)
1064
1414
  ? fieldKey.slice(UNRESOLVED_FIELD_MARKER.length)
1065
1415
  : fieldKey;
1066
- state.imports.usesUnsafePlatformId = true;
1067
- fieldExpr = `unsafePlatformId(${quote(uuid)})`;
1416
+ const foreignOwner = assignment?.foreignOwnerName;
1417
+ const foreignKind = assignment?.foreignEntityType;
1418
+ if ((foreignKind === "app" || foreignKind === "element") && foreignOwner !== undefined && foreignOwner !== "" && !isUuid(rawKey) && assignment?.foreignFieldType !== undefined) {
1419
+ fieldExpr = `catalog.${foreignOwner}.fields${safeMember(rawKey)}`;
1420
+ }
1421
+ else if (!isUuid(rawKey)) {
1422
+ // A bag key with no known owner and type is not a TEXT field. Inventing
1423
+ // one makes the next build address elementum_text_field.
1424
+ fieldExpr = `/* unresolved field ${quote(rawKey)} */ null`;
1425
+ }
1426
+ else {
1427
+ state.imports.usesUnsafePlatformId = true;
1428
+ fieldExpr = `unsafePlatformId(${quote(rawKey)})`;
1429
+ }
1068
1430
  }
1069
1431
  const valueExpr = printArgValue("", assignment?.value ?? rawValue, state, indent + 2, refCtx);
1070
1432
  const updateMode = assignment?.updateMode === "add" ? `, updateMode: "add"` : "";
@@ -1076,21 +1438,47 @@ function printFieldAssignments(values, target, state, indent, refCtx) {
1076
1438
  * The task's primary target, used to resolve `values:` field keys to
1077
1439
  * `<target>.fields.<key>` tokens (or inline field stubs for tasks).
1078
1440
  * Reads the `app` / `relatedApp` arg.
1441
+ *
1442
+ * Non-recursive element/app exports often leave `object_id` as a raw platform
1443
+ * UUID. The `app:` printer already joins those via
1444
+ * `foreign*RefNamespaces` (#1427); this must use the same join so system-field
1445
+ * bag keys (Title/ID — no `foreignFieldType` on the assignment) still print as
1446
+ * `catalog.<target>.fields.<key>` instead of an unresolved-field null stub.
1079
1447
  */
1080
1448
  function taskPrimaryTarget(args, state) {
1081
1449
  for (const k of ["app", "relatedApp"]) {
1082
1450
  const v = args[k];
1083
1451
  if (isRefExpr(v) && v.kind === "app") {
1084
1452
  if (v.entityType === "task") {
1085
- const refName = (v.name !== undefined ? state.foreignTaskRefNames?.get(v.name) : undefined) ?? v.appHandle;
1086
- return { kind: "task", refName, name: v.name ?? refName };
1453
+ const foreignRefName = v.name !== undefined ? state.foreignTaskRefNames?.get(v.name) : undefined;
1454
+ const refName = foreignRefName ?? v.appHandle;
1455
+ return { kind: "task", refName, name: v.name ?? refName, inCatalog: foreignRefName !== undefined };
1087
1456
  }
1088
1457
  if (v.entityType === "element") {
1089
- const refName = (v.name !== undefined ? state.foreignElementRefNames?.get(v.name) : undefined) ?? v.appHandle;
1090
- return { kind: "element", refName, name: v.name };
1458
+ const foreignRefName = v.name !== undefined ? state.foreignElementRefNames?.get(v.name) : undefined;
1459
+ return { kind: "element", refName: foreignRefName ?? v.appHandle, name: v.name, inCatalog: v.name === undefined || foreignRefName !== undefined };
1091
1460
  }
1092
- const refName = (v.name !== undefined ? state.foreignAppRefNames?.get(v.name) : undefined) ?? catalogOwnerHandle(state, v.appHandle);
1093
- return { kind: "app", refName, name: v.name };
1461
+ const foreignRefName = v.name !== undefined ? state.foreignAppRefNames?.get(v.name) : undefined;
1462
+ const refName = foreignRefName ?? catalogOwnerHandle(state, v.appHandle);
1463
+ return { kind: "app", refName, name: v.name, inCatalog: v.entityType === undefined || v.name === undefined || foreignRefName !== undefined };
1464
+ }
1465
+ const aspectId = typeof v === "string"
1466
+ ? v
1467
+ : isRefExpr(v) && v.kind === "literal" && typeof v.value === "string"
1468
+ ? v.value
1469
+ : undefined;
1470
+ if (aspectId !== undefined && isUuid(aspectId)) {
1471
+ // Same order as the `app:` printer: app namespace, element namespace,
1472
+ // then the name maps. Checking elements first would print fields against
1473
+ // a different catalog ref than the app argument.
1474
+ const appRef = state.foreignAppRefNamespaces?.get(aspectId)
1475
+ ?? state.foreignAppRefNames?.get(aspectId);
1476
+ if (appRef !== undefined)
1477
+ return { kind: "app", refName: appRef, inCatalog: true };
1478
+ const elementRef = state.foreignElementRefNamespaces?.get(aspectId)
1479
+ ?? state.foreignElementRefNames?.get(aspectId);
1480
+ if (elementRef !== undefined)
1481
+ return { kind: "element", refName: elementRef, inCatalog: true };
1094
1482
  }
1095
1483
  }
1096
1484
  return undefined;
@@ -1103,6 +1491,10 @@ function printArgValue(name, value, state, indent, refCtx) {
1103
1491
  const calc = trackFactory("calc", state);
1104
1492
  return `${calc}.raw(${quote(value.text)}, ${quote(value.resultType)})`;
1105
1493
  }
1494
+ if (isRequiredSecret(value)) {
1495
+ const fn = trackFactory("requiredSecret", state);
1496
+ return `${fn}(${quote(value.variable)})`;
1497
+ }
1106
1498
  if (isRefExpr(value))
1107
1499
  return printRef(value, state, refCtx);
1108
1500
  if (isFilter(value))
@@ -1226,25 +1618,26 @@ function printRef(ref, state, refCtx) {
1226
1618
  // item" - at build time it binds to whichever loop's replaceForEach
1227
1619
  // pass runs closest to this position, not to any outer ancestor.
1228
1620
  // refCtx.selfTaskRefKeys (accumulated by formatForEach on the way
1229
- // down) holds the full ancestor chain, so checking membership alone
1230
- // is not enough - only the LAST entry is actually addressable as
1231
- // ctx.item here. Live-confirmed (conciergeforfield, sanofi/us): two
1232
- // bugs of this shape. (1) A task nested inside "Matched Message"
1233
- // referenced "Matched Product" - a SIBLING loop, not an ancestor at
1234
- // all. (2) A task nested inside "Matched Message", itself nested
1235
- // inside "Matched Product", referenced "Matched Product" directly -
1236
- // a real ancestor, but not the nearest one, so `ctx.item` there binds
1237
- // to "Matched Message" instead. Both silently attributed the
1238
- // reference to the wrong loop (built HCL pointed at the wrong
1239
- // for_each task's inner.refs, which doesn't carry that key: "Invalid
1240
- // index" on every plan). EDK's authoring model has no primitive for
1241
- // "an outer loop's current item" - same escape shape as taskRef's
1242
- // out-of-scope cases above, not a claim that ctx.actions[...] is
1243
- // actually valid at runtime either.
1621
+ // down) holds the full ancestor chain; only its LAST entry is
1622
+ // addressable as ctx.item. Every other ancestor is addressable by
1623
+ // name as ctx.loops.<name>.item (#1320) - the platform offers and
1624
+ // resolves an outer loop's current item by that loop's task id.
1625
+ // Live-confirmed (conciergeforfield, sanofi/us): a task nested inside
1626
+ // "Matched Message", itself nested inside "Matched Product",
1627
+ // referenced "Matched Product" directly.
1244
1628
  const nearest = refCtx.selfTaskRefKeys?.[refCtx.selfTaskRefKeys.length - 1];
1245
1629
  if (nearest === ref.forEachTask) {
1246
1630
  return printPathAccess(`${ctxName}.item`, ref.path);
1247
1631
  }
1632
+ if (refCtx.selfTaskRefKeys?.includes(ref.forEachTask)) {
1633
+ return printPathAccess(`${ctxName}.loops${safeMember(ref.forEachTask)}.item`, ref.path);
1634
+ }
1635
+ // What's left is a loop that does NOT enclose this task - a sibling
1636
+ // (live on conciergeforfield: a task inside "Matched Message"
1637
+ // referenced a sibling "Matched Product"). The platform never offers
1638
+ // that reference and its validation flags it invalid, so EDK has no
1639
+ // public form for it.
1640
+ //
1248
1641
  // Cannot route this through `ctx.actions[...]` the way the taskRef
1249
1642
  // out-of-scope escapes above do: `ctx.actions` is a Proxy that turns
1250
1643
  // ANY property access into a valid `{ kind: "task", ... }` reference,
@@ -1257,10 +1650,7 @@ function printRef(ref, state, refCtx) {
1257
1650
  // way, but automationToTf's valueToTf recognizes the marker and
1258
1651
  // fails the build loudly instead of building a different accepted
1259
1652
  // reference (review feedback on PR #1097, cursor[bot]).
1260
- const relation = refCtx.selfTaskRefKeys?.includes(ref.forEachTask)
1261
- ? "an outer ancestor forEach, not the nearest enclosing one"
1262
- : "a sibling forEach, not an ancestor";
1263
- return `({ __edkUnresolvedForEachItem: ${quote(ref.forEachTask)}, path: [${ref.path.map(quote).join(", ")}] } as any /* TODO: "${ref.forEachTask}" is ${relation} - EDK cannot express a reference to another loop's current item; re-express by hand before this automation can build */)`;
1653
+ return `({ ${UNRESOLVED_FOR_EACH_ITEM_KEY}: ${quote(ref.forEachTask)}, path: [${ref.path.map(quote).join(", ")}] } as any /* TODO: "${ref.forEachTask}" is a sibling forEach, not an enclosing one - EDK cannot express a reference to a sibling loop's current item; re-express by hand before this automation can build */)`;
1264
1654
  }
1265
1655
  case "variableParamRef":
1266
1656
  return `${ctxName}.variables${safeMember(ref.varTask)}${safeMember(ref.name)}`;
@@ -1287,12 +1677,13 @@ function printRef(ref, state, refCtx) {
1287
1677
  // pulled into this workspace (board issue #40, app-only) resolves to a
1288
1678
  // typed token; otherwise a literal stub (field type is unconfirmed —
1289
1679
  // ei's `elementum_field` data source doesn't expose it).
1680
+ const namespace = ref.foreign.namespace;
1290
1681
  const foreignRefName = ref.foreign.entityType === "element"
1291
- ? state.foreignElementRefNames?.get(ref.foreign.name)
1682
+ ? (namespace !== undefined ? state.foreignElementRefNamespaces?.get(namespace) : undefined) ?? state.foreignElementRefNames?.get(ref.foreign.name)
1292
1683
  : ref.foreign.entityType === "task"
1293
1684
  ? state.foreignTaskRefNames?.get(ref.foreign.name)
1294
1685
  : ref.foreign.entityType === "app"
1295
- ? state.foreignAppRefNames?.get(ref.foreign.name)
1686
+ ? (namespace !== undefined ? state.foreignAppRefNamespaces?.get(namespace) : undefined) ?? state.foreignAppRefNames?.get(ref.foreign.name)
1296
1687
  : undefined;
1297
1688
  if (ref.foreign.entityType === "task") {
1298
1689
  const parentRef = foreignRefName ?? ref.appHandle;
@@ -1334,7 +1725,8 @@ function printRef(ref, state, refCtx) {
1334
1725
  : undefined;
1335
1726
  if (foreignRefName !== undefined)
1336
1727
  return `catalog.${foreignRefName}`;
1337
- return `{ __kind: ${quote(ref.entityType)}, refName: ${quote(ref.appHandle)}, name: ${quote(ref.name)} }`;
1728
+ const namespace = ref.namespace !== undefined ? `, namespace: ${quote(ref.namespace)}` : "";
1729
+ return `{ __kind: ${quote(ref.entityType)}, refName: ${quote(ref.appHandle)}, name: ${quote(ref.name)}${namespace} }`;
1338
1730
  }
1339
1731
  case "agent":
1340
1732
  return `catalog.${catalogOwnerHandle(state, ref.appHandle)}.agents${safeMember(ref.agentKey)}`;
@@ -1354,6 +1746,15 @@ function printRef(ref, state, refCtx) {
1354
1746
  case "tableColumn":
1355
1747
  return `catalog.tables${safeMember(ref.tableHandle)}.columns${safeMember(ref.columnKey)}`;
1356
1748
  case "searchTable":
1749
+ if (ref.ownerKind === "table") {
1750
+ state.imports.usesTableSearchTableRef = true;
1751
+ const parts = [`name: ${quote(ref.name ?? ref.tableKey)}`, `parent: ${quote(ref.appHandle)}`];
1752
+ if (ref.parentName !== undefined)
1753
+ parts.push(`parentName: ${quote(ref.parentName)}`);
1754
+ if (ref.id !== undefined)
1755
+ parts.push(`id: ${quote(ref.id)}`);
1756
+ return `tableSearchTableRef({ ${parts.join(", ")} })`;
1757
+ }
1357
1758
  // Search tables are element- or table-owned, never app-owned. Remapping
1358
1759
  // the automation owner's export handle to its authored app refName
1359
1760
  // would bind an unpulled same-handle foreign element's table onto the
@@ -1361,6 +1762,12 @@ function printRef(ref, state, refCtx) {
1361
1762
  return `catalog.${ref.appHandle}.searchTables${safeMember(ref.tableKey)}`;
1362
1763
  case "user":
1363
1764
  return `${trackFactory("userRef", state)}(${quote(ref.email)})`;
1765
+ case "secret":
1766
+ state.imports.usesSecret = true;
1767
+ return `secret(${quote(ref.key)})`;
1768
+ case "orgGroup":
1769
+ state.imports.usesOrg = true;
1770
+ return `org.groups${safeMember(ref.groupKey)}`;
1364
1771
  case "documentModel":
1365
1772
  return `catalog.${catalogOwnerHandle(state, ref.appHandle)}.fileReaders${safeMember(ref.documentModelKey)}`;
1366
1773
  case "documentModelField":
@@ -1543,6 +1950,11 @@ const RESERVED_IDENT = new Set([
1543
1950
  "while", "with", "yield",
1544
1951
  ]);
1545
1952
  function safeKey(name) {
1953
+ // `__proto__: v` and `"__proto__": v` in an object literal both invoke the
1954
+ // prototype setter instead of defining a key; only a computed key defines
1955
+ // an own `__proto__` property.
1956
+ if (name === "__proto__")
1957
+ return `[${quote(name)}]`;
1546
1958
  if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) || RESERVED_IDENT.has(name)) {
1547
1959
  return quote(name);
1548
1960
  }