@elementumai/edk 0.11.0 → 0.13.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 (222) hide show
  1. package/README.md +40 -6
  2. package/dist-sdk/src/catalog/factories.d.ts +7 -1
  3. package/dist-sdk/src/catalog/factories.js +2 -0
  4. package/dist-sdk/src/catalog/index.d.ts +2 -2
  5. package/dist-sdk/src/catalog/index.js +1 -1
  6. package/dist-sdk/src/catalog/org.js +79 -34
  7. package/dist-sdk/src/catalog/token.js +5 -0
  8. package/dist-sdk/src/catalog/types.d.ts +7 -1
  9. package/dist-sdk/src/catalog/types.js +3 -0
  10. package/dist-sdk/src/core/system-fields.d.ts +1 -1
  11. package/dist-sdk/src/core/system-fields.js +1 -1
  12. package/dist-sdk/src/core/triggers/on-demand.d.ts +2 -0
  13. package/dist-sdk/src/core/triggers/on-demand.js +3 -0
  14. package/dist-sdk/src/core/types/app.d.ts +6 -0
  15. package/dist-sdk/src/engine/build/context.d.ts +3 -0
  16. package/dist-sdk/src/engine/coverage-gaps.d.ts +5 -0
  17. package/dist-sdk/src/engine/coverage-gaps.js +9 -0
  18. package/dist-sdk/src/engine/ei/run.d.ts +20 -1
  19. package/dist-sdk/src/engine/ei/run.js +102 -14
  20. package/dist-sdk/src/engine/pull/authored-address.js +42 -6
  21. package/dist-sdk/src/engine/pull/authored-root.d.ts +88 -0
  22. package/dist-sdk/src/engine/pull/authored-root.js +306 -0
  23. package/dist-sdk/src/engine/pull/context.d.ts +10 -0
  24. package/dist-sdk/src/engine/pull/field-refs-registry.d.ts +114 -0
  25. package/dist-sdk/src/engine/pull/field-refs-registry.js +501 -0
  26. package/dist-sdk/src/engine/pull/org.d.ts +1 -0
  27. package/dist-sdk/src/engine/pull/org.js +2 -0
  28. package/dist-sdk/src/engine/pull/partition.d.ts +8 -0
  29. package/dist-sdk/src/engine/pull/partition.js +43 -0
  30. package/dist-sdk/src/engine/pull/pulled-roots.d.ts +89 -0
  31. package/dist-sdk/src/engine/pull/pulled-roots.js +203 -0
  32. package/dist-sdk/src/engine/pull/ref-only.d.ts +22 -0
  33. package/dist-sdk/src/engine/pull/ref-only.js +59 -1
  34. package/dist-sdk/src/engine/pull/source-resources.d.ts +39 -0
  35. package/dist-sdk/src/engine/pull/source-resources.js +110 -0
  36. package/dist-sdk/src/engine/pull/stream-root.d.ts +148 -0
  37. package/dist-sdk/src/engine/pull/stream-root.js +563 -0
  38. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.d.ts +47 -0
  39. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.js +115 -0
  40. package/dist-sdk/src/entities/access-policy/build.d.ts +2 -1
  41. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +9 -1
  42. package/dist-sdk/src/entities/access-policy/from-tf.js +25 -4
  43. package/dist-sdk/src/entities/access-policy/module.js +5 -0
  44. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +19 -0
  45. package/dist-sdk/src/entities/access-policy/to-tf.js +65 -8
  46. package/dist-sdk/src/entities/agent/build.d.ts +52 -1
  47. package/dist-sdk/src/entities/agent/build.js +34 -0
  48. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +68 -0
  49. package/dist-sdk/src/entities/agent/from-tf.d.ts +34 -1
  50. package/dist-sdk/src/entities/agent/from-tf.js +262 -2
  51. package/dist-sdk/src/entities/agent/index.d.ts +1 -1
  52. package/dist-sdk/src/entities/agent/to-tf.d.ts +8 -10
  53. package/dist-sdk/src/entities/agent/to-tf.js +158 -14
  54. package/dist-sdk/src/entities/agent-tool/from-tf.js +6 -0
  55. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +2 -2
  56. package/dist-sdk/src/entities/agent-tool/to-tf.js +2 -2
  57. package/dist-sdk/src/entities/app/build.d.ts +34 -14
  58. package/dist-sdk/src/entities/app/build.js +93 -16
  59. package/dist-sdk/src/entities/app/from-tf.js +25 -11
  60. package/dist-sdk/src/entities/app/index.d.ts +1 -0
  61. package/dist-sdk/src/entities/app/index.js +1 -0
  62. package/dist-sdk/src/entities/app/module.js +1 -0
  63. package/dist-sdk/src/entities/app/stage-keys.d.ts +20 -5
  64. package/dist-sdk/src/entities/app/stage-keys.js +43 -9
  65. package/dist-sdk/src/entities/app/to-tf.js +2 -0
  66. package/dist-sdk/src/entities/approval-chain/build.d.ts +46 -11
  67. package/dist-sdk/src/entities/approval-chain/build.js +120 -8
  68. package/dist-sdk/src/entities/approval-chain/from-tf.d.ts +24 -2
  69. package/dist-sdk/src/entities/approval-chain/from-tf.js +148 -6
  70. package/dist-sdk/src/entities/approval-chain/index.d.ts +4 -1
  71. package/dist-sdk/src/entities/approval-chain/index.js +4 -1
  72. package/dist-sdk/src/entities/approval-chain/module.js +23 -5
  73. package/dist-sdk/src/entities/approval-chain/printer.d.ts +1 -1
  74. package/dist-sdk/src/entities/approval-chain/printer.js +91 -5
  75. package/dist-sdk/src/entities/approval-chain/steps-tf.d.ts +77 -0
  76. package/dist-sdk/src/entities/approval-chain/steps-tf.js +255 -0
  77. package/dist-sdk/src/entities/approval-chain/steps.d.ts +71 -0
  78. package/dist-sdk/src/entities/approval-chain/steps.js +83 -0
  79. package/dist-sdk/src/entities/approval-chain/to-tf.d.ts +16 -6
  80. package/dist-sdk/src/entities/approval-chain/to-tf.js +37 -4
  81. package/dist-sdk/src/entities/automation/build.d.ts +50 -24
  82. package/dist-sdk/src/entities/automation/build.js +86 -33
  83. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +52 -8
  84. package/dist-sdk/src/entities/automation/from-tf.d.ts +38 -6
  85. package/dist-sdk/src/entities/automation/from-tf.js +855 -157
  86. package/dist-sdk/src/entities/automation/index.d.ts +2 -1
  87. package/dist-sdk/src/entities/automation/index.js +1 -0
  88. package/dist-sdk/src/entities/automation/module.d.ts +4 -3
  89. package/dist-sdk/src/entities/automation/module.js +33 -12
  90. package/dist-sdk/src/entities/automation/refs-read.d.ts +19 -0
  91. package/dist-sdk/src/entities/automation/refs-read.js +48 -0
  92. package/dist-sdk/src/entities/automation/run-target.d.ts +2 -0
  93. package/dist-sdk/src/entities/automation/run-target.js +5 -0
  94. package/dist-sdk/src/entities/automation/script-code.d.ts +2 -1
  95. package/dist-sdk/src/entities/automation/script-code.js +82 -1
  96. package/dist-sdk/src/entities/automation/script-lint.d.ts +13 -0
  97. package/dist-sdk/src/entities/automation/script-lint.js +418 -0
  98. package/dist-sdk/src/entities/automation/slack-message-refs.d.ts +9 -0
  99. package/dist-sdk/src/entities/automation/slack-message-refs.js +75 -0
  100. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +12 -1
  101. package/dist-sdk/src/entities/automation/task-output-labels.js +32 -1
  102. package/dist-sdk/src/entities/automation/tasks.d.ts +76 -18
  103. package/dist-sdk/src/entities/automation/tasks.js +52 -15
  104. package/dist-sdk/src/entities/automation/to-tf.d.ts +3 -0
  105. package/dist-sdk/src/entities/automation/to-tf.js +585 -97
  106. package/dist-sdk/src/entities/automation/triggers.d.ts +48 -23
  107. package/dist-sdk/src/entities/automation/triggers.js +27 -12
  108. package/dist-sdk/src/entities/automation/unsafe-script-output.d.ts +50 -0
  109. package/dist-sdk/src/entities/automation/unsafe-script-output.js +92 -0
  110. package/dist-sdk/src/entities/chart/build.d.ts +18 -2
  111. package/dist-sdk/src/entities/chart/from-tf.d.ts +2 -2
  112. package/dist-sdk/src/entities/chart/from-tf.js +28 -1
  113. package/dist-sdk/src/entities/chart/index.d.ts +1 -1
  114. package/dist-sdk/src/entities/chart/printer.js +5 -2
  115. package/dist-sdk/src/entities/chart/to-tf.js +38 -3
  116. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +3 -0
  117. package/dist-sdk/src/entities/dynamic-layout/condition-keys.d.ts +48 -0
  118. package/dist-sdk/src/entities/dynamic-layout/condition-keys.js +261 -0
  119. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +45 -6
  120. package/dist-sdk/src/entities/dynamic-layout/module.js +2 -1
  121. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +46 -12
  122. package/dist-sdk/src/entities/element/audit-field-guard.d.ts +9 -0
  123. package/dist-sdk/src/entities/element/audit-field-guard.js +88 -0
  124. package/dist-sdk/src/entities/element/build.js +2 -1
  125. package/dist-sdk/src/entities/element/from-tf.js +15 -2
  126. package/dist-sdk/src/entities/element/module.js +3 -0
  127. package/dist-sdk/src/entities/element/system-fields.d.ts +13 -0
  128. package/dist-sdk/src/entities/element/system-fields.js +23 -0
  129. package/dist-sdk/src/entities/element/to-tf.js +2 -0
  130. package/dist-sdk/src/entities/field/build.d.ts +9 -0
  131. package/dist-sdk/src/entities/field/from-tf.d.ts +29 -1
  132. package/dist-sdk/src/entities/field/from-tf.js +99 -6
  133. package/dist-sdk/src/entities/field/module.js +12 -2
  134. package/dist-sdk/src/entities/field/to-tf.d.ts +13 -3
  135. package/dist-sdk/src/entities/field/to-tf.js +90 -46
  136. package/dist-sdk/src/entities/org/category.js +1 -0
  137. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  138. package/dist-sdk/src/entities/org/index.js +1 -0
  139. package/dist-sdk/src/entities/org/role.js +5 -0
  140. package/dist-sdk/src/entities/org/voice-auth.d.ts +5 -0
  141. package/dist-sdk/src/entities/org/voice-auth.js +33 -0
  142. package/dist-sdk/src/entities/organization/build.d.ts +3 -0
  143. package/dist-sdk/src/entities/organization/build.js +2 -0
  144. package/dist-sdk/src/entities/organization/index.d.ts +1 -0
  145. package/dist-sdk/src/entities/organization/index.js +1 -0
  146. package/dist-sdk/src/entities/organization/to-tf.d.ts +2 -0
  147. package/dist-sdk/src/entities/organization/to-tf.js +59 -1
  148. package/dist-sdk/src/entities/organization/voice-auth.d.ts +60 -0
  149. package/dist-sdk/src/entities/organization/voice-auth.js +60 -0
  150. package/dist-sdk/src/entities/phone-service/build.d.ts +9 -1
  151. package/dist-sdk/src/entities/phone-service/build.js +1 -0
  152. package/dist-sdk/src/entities/phone-service/deserializer/print-phone-service.js +3 -0
  153. package/dist-sdk/src/entities/phone-service/from-tf.d.ts +1 -0
  154. package/dist-sdk/src/entities/phone-service/from-tf.js +8 -1
  155. package/dist-sdk/src/entities/phone-service/to-tf.js +16 -0
  156. package/dist-sdk/src/entities/record-create-form/from-tf.js +2 -2
  157. package/dist-sdk/src/entities/relationship/from-tf.js +7 -2
  158. package/dist-sdk/src/entities/relationship/module.js +2 -2
  159. package/dist-sdk/src/entities/relationship/pair-check.d.ts +6 -2
  160. package/dist-sdk/src/entities/relationship/pair-check.js +105 -18
  161. package/dist-sdk/src/entities/relationship/to-tf.js +5 -2
  162. package/dist-sdk/src/entities/role/permission-levels.d.ts +7 -0
  163. package/dist-sdk/src/entities/role/permission-levels.js +78 -0
  164. package/dist-sdk/src/entities/role/to-tf.js +32 -2
  165. package/dist-sdk/src/entities/service-account/module.d.ts +2 -1
  166. package/dist-sdk/src/entities/service-account/module.js +9 -1
  167. package/dist-sdk/src/entities/service-account/resource-type.d.ts +2 -0
  168. package/dist-sdk/src/entities/service-account/resource-type.js +2 -0
  169. package/dist-sdk/src/entities/service-account/role-fidelity.d.ts +5 -5
  170. package/dist-sdk/src/entities/service-account/role-fidelity.js +21 -13
  171. package/dist-sdk/src/entities/service-account/role-membership-check.d.ts +20 -0
  172. package/dist-sdk/src/entities/service-account/role-membership-check.js +80 -0
  173. package/dist-sdk/src/entities/shared/aspect-target-from-tf.d.ts +15 -0
  174. package/dist-sdk/src/entities/shared/aspect-target-from-tf.js +36 -0
  175. package/dist-sdk/src/entities/shared/filters/ast.d.ts +4 -0
  176. package/dist-sdk/src/entities/shared/filters/index.d.ts +85 -14
  177. package/dist-sdk/src/entities/shared/filters/index.js +18 -6
  178. package/dist-sdk/src/entities/shared/filters/option-lookups.d.ts +57 -0
  179. package/dist-sdk/src/entities/shared/filters/option-lookups.js +108 -0
  180. package/dist-sdk/src/entities/shared/filters/print.js +9 -5
  181. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +12 -7
  182. package/dist-sdk/src/entities/shared/filters/tf-codec.js +9 -5
  183. package/dist-sdk/src/entities/shared/raw-option-id.d.ts +30 -0
  184. package/dist-sdk/src/entities/shared/raw-option-id.js +41 -0
  185. package/dist-sdk/src/entities/shared/tool-field-ref.js +6 -2
  186. package/dist-sdk/src/entities/shared/types/field-types.d.ts +6 -3
  187. package/dist-sdk/src/entities/shared/types/field-types.js +3 -1
  188. package/dist-sdk/src/entities/skill/input-coverage.js +5 -2
  189. package/dist-sdk/src/entities/skill/module.js +2 -0
  190. package/dist-sdk/src/entities/sla/build.d.ts +96 -0
  191. package/dist-sdk/src/entities/sla/build.js +124 -0
  192. package/dist-sdk/src/entities/sla/from-tf.d.ts +40 -0
  193. package/dist-sdk/src/entities/sla/from-tf.js +164 -0
  194. package/dist-sdk/src/entities/sla/index.d.ts +1 -0
  195. package/dist-sdk/src/entities/sla/index.js +1 -0
  196. package/dist-sdk/src/entities/sla/module.d.ts +4 -0
  197. package/dist-sdk/src/entities/sla/module.js +73 -0
  198. package/dist-sdk/src/entities/sla/printer.d.ts +2 -0
  199. package/dist-sdk/src/entities/sla/printer.js +84 -0
  200. package/dist-sdk/src/entities/sla/to-tf.d.ts +12 -0
  201. package/dist-sdk/src/entities/sla/to-tf.js +81 -0
  202. package/dist-sdk/src/entities/table/build.d.ts +7 -2
  203. package/dist-sdk/src/entities/table/build.js +22 -1
  204. package/dist-sdk/src/entities/table/deserializer/normalize.d.ts +4 -1
  205. package/dist-sdk/src/entities/table/from-tf.js +89 -14
  206. package/dist-sdk/src/entities/table/index.d.ts +1 -1
  207. package/dist-sdk/src/entities/table/to-tf.js +41 -2
  208. package/dist-sdk/src/entities/task/from-tf.js +2 -1
  209. package/dist-sdk/src/entities/task/to-tf.js +1 -0
  210. package/dist-sdk/src/entities/view/from-tf.d.ts +8 -4
  211. package/dist-sdk/src/entities/view/from-tf.js +10 -9
  212. package/dist-sdk/src/entities/view/module.js +5 -0
  213. package/dist-sdk/src/entities/widget/from-tf.js +15 -4
  214. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +51 -4
  215. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  216. package/dist-sdk/src/graph/index.d.ts +20 -2
  217. package/dist-sdk/src/graph/index.js +19 -0
  218. package/dist-sdk/src/graph/modules.js +11 -1
  219. package/dist-sdk/src/graph/resource-types.d.ts +47 -0
  220. package/dist-sdk/src/graph/resource-types.js +62 -0
  221. package/dist-sdk/src/graph/types.d.ts +116 -6
  222. package/package.json +6 -3
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Restore a dynamic layout's authored condition keys on a re-pull (#2414).
3
+ *
4
+ * The platform stores a layout's conditions without their keys, so the export
5
+ * names them `condition_0001`, `condition_0002`, … in document order. State
6
+ * still holds the authored keys (`hasValidationMessage`), and every reference
7
+ * to them — a component's `show_when`, `hide_when`, `enable_when`,
8
+ * `disable_when` and `require_when`, a condition's `all`, `any` and `not`, a
9
+ * dependency's `condition` — would then differ from state, so adoption plans
10
+ * an update.
11
+ *
12
+ * Same pairing the provider applies on its own reads
13
+ * (`preserveDynamicLayoutConditionIDs`): by position from each component and
14
+ * dependency both sides share, following `all`/`any`/`not` children, and only
15
+ * when the two conditions have the same content; then any condition left
16
+ * whose content is unique in state. Several exported conditions can pair with
17
+ * one state key: the platform returns a condition referenced from several
18
+ * places (`all`, `not`, a component) as separate copies, and the rename
19
+ * collapses them back into the one authored condition, as the provider's read
20
+ * does. Content is compared with every referenced
21
+ * condition replaced by its own content, and every reference in the export
22
+ * (`elementum_text_field.x.id`, `data.elementum_role.y.id`, a stage's
23
+ * `local.<app>_stage_ids_by_key["<key>"]`, an option's
24
+ * `local.<field>_options_by_label["<label>"]`) replaced by the value state
25
+ * holds for it. A condition that cannot be compared keeps its exported key.
26
+ *
27
+ * Registered as the dynamic layout's `alignWithState` (module.ts), which
28
+ * `alignExportWithState` runs on a re-pull.
29
+ */
30
+ import type { StateAlignment } from "../../graph/index.js";
31
+ type Plain = null | string | number | boolean | Plain[] | {
32
+ [key: string]: Plain;
33
+ };
34
+ interface LayoutSide {
35
+ readonly conditions: Readonly<Record<string, unknown>>;
36
+ readonly components: Readonly<Record<string, unknown>>;
37
+ readonly dependencies: readonly unknown[];
38
+ /** Plain value of a field reference on this side, or undefined when it cannot be resolved. */
39
+ readonly plain: (value: unknown) => Plain | undefined;
40
+ }
41
+ /** Export condition key → state condition key, the provider's `preserveDynamicLayoutConditionIDs` pairing. */
42
+ export declare function pairLayoutConditions(exported: LayoutSide, state: LayoutSide): Map<string, string>;
43
+ /**
44
+ * The exported layout's attributes with its condition keys restored from the
45
+ * layout's state attributes, or the same object when nothing pairs.
46
+ */
47
+ export declare const restoreLayoutConditionKeys: StateAlignment;
48
+ export {};
@@ -0,0 +1,261 @@
1
+ import { isTfExpr } from "../../engine/tf/model.js";
2
+ const COMPONENT_REFERENCE_LISTS = ["show_when", "hide_when", "enable_when", "disable_when", "require_when"];
3
+ const GROUP_KINDS = ["all", "any"];
4
+ function asMap(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value) && !isTfExpr(value)
6
+ ? value
7
+ : undefined;
8
+ }
9
+ function stringList(value) {
10
+ return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
11
+ }
12
+ /**
13
+ * Plain data with nulls dropped and keys sorted, or undefined when a value
14
+ * cannot be resolved. Null and missing are the same value: state lists every
15
+ * optional attribute, the export only the ones it sets.
16
+ */
17
+ function plainValue(value, resolve) {
18
+ if (value === undefined || value === null)
19
+ return null;
20
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean")
21
+ return value;
22
+ if (Array.isArray(value)) {
23
+ const items = value.map((item) => plainValue(item, resolve));
24
+ return items.some((item) => item === undefined) ? undefined : items;
25
+ }
26
+ if (typeof value !== "object")
27
+ return undefined;
28
+ if (isTfExpr(value)) {
29
+ const resolved = resolve === undefined ? undefined : resolve(value);
30
+ return resolved === undefined ? undefined : plainValue(resolved, undefined);
31
+ }
32
+ const out = {};
33
+ for (const key of Object.keys(value).sort()) {
34
+ const child = plainValue(value[key], resolve);
35
+ if (child === undefined)
36
+ return undefined;
37
+ if (child !== null)
38
+ out[key] = child;
39
+ }
40
+ return out;
41
+ }
42
+ /** {@link conditionFingerprint} in the provider: the condition's content with every referenced condition replaced by its fingerprint. */
43
+ function fingerprint(side, id, visiting = new Set()) {
44
+ if (visiting.has(id))
45
+ return undefined;
46
+ const condition = side.conditions[id];
47
+ if (condition === undefined)
48
+ return undefined;
49
+ const normalized = side.plain(condition);
50
+ if (normalized === undefined || normalized === null || typeof normalized !== "object" || Array.isArray(normalized))
51
+ return undefined;
52
+ visiting.add(id);
53
+ try {
54
+ const next = { ...normalized };
55
+ for (const kind of GROUP_KINDS) {
56
+ const group = asMap(next[kind]);
57
+ if (group === undefined)
58
+ continue;
59
+ const refs = Array.isArray(group["conditions"]) ? group["conditions"] : [];
60
+ const children = [];
61
+ for (const ref of refs) {
62
+ const child = typeof ref === "string" ? fingerprint(side, ref, visiting) : undefined;
63
+ if (child === undefined)
64
+ return undefined;
65
+ children.push(child);
66
+ }
67
+ next[kind] = { ...group, conditions: children };
68
+ }
69
+ const negation = asMap(next["not"]);
70
+ if (negation !== undefined) {
71
+ const child = typeof negation["condition"] === "string" ? fingerprint(side, negation["condition"], visiting) : undefined;
72
+ if (child === undefined)
73
+ return undefined;
74
+ next["not"] = { ...negation, condition: child };
75
+ }
76
+ return JSON.stringify(next);
77
+ }
78
+ finally {
79
+ visiting.delete(id);
80
+ }
81
+ }
82
+ /** Export condition key → state condition key, the provider's `preserveDynamicLayoutConditionIDs` pairing. */
83
+ export function pairLayoutConditions(exported, state) {
84
+ const renames = new Map();
85
+ const pair = (exportedId, stateId) => {
86
+ if (renames.has(exportedId))
87
+ return;
88
+ const left = fingerprint(exported, exportedId);
89
+ if (left === undefined || left !== fingerprint(state, stateId))
90
+ return;
91
+ renames.set(exportedId, stateId);
92
+ const exportedCondition = asMap(exported.conditions[exportedId]) ?? {};
93
+ const stateCondition = asMap(state.conditions[stateId]) ?? {};
94
+ for (const kind of GROUP_KINDS) {
95
+ const exportedGroup = asMap(exportedCondition[kind]);
96
+ const stateGroup = asMap(stateCondition[kind]);
97
+ if (exportedGroup !== undefined && stateGroup !== undefined)
98
+ pairLists(stringList(exportedGroup["conditions"]), stringList(stateGroup["conditions"]));
99
+ }
100
+ const exportedNot = asMap(exportedCondition["not"])?.["condition"];
101
+ const stateNot = asMap(stateCondition["not"])?.["condition"];
102
+ if (typeof exportedNot === "string" && typeof stateNot === "string")
103
+ pair(exportedNot, stateNot);
104
+ };
105
+ const pairLists = (exportedRefs, stateRefs) => {
106
+ for (let i = 0; i < exportedRefs.length && i < stateRefs.length; i++)
107
+ pair(exportedRefs[i], stateRefs[i]);
108
+ };
109
+ for (const componentId of Object.keys(exported.components).sort()) {
110
+ const exportedComponent = asMap(exported.components[componentId]);
111
+ const stateComponent = asMap(state.components[componentId]);
112
+ if (exportedComponent === undefined || stateComponent === undefined)
113
+ continue;
114
+ for (const list of COMPONENT_REFERENCE_LISTS)
115
+ pairLists(stringList(exportedComponent[list]), stringList(stateComponent[list]));
116
+ }
117
+ for (let i = 0; i < exported.dependencies.length && i < state.dependencies.length; i++) {
118
+ const exportedDependency = asMap(exported.dependencies[i]);
119
+ const stateDependency = asMap(state.dependencies[i]);
120
+ if (exportedDependency === undefined || stateDependency === undefined)
121
+ continue;
122
+ const same = ["source_field_id", "target_field_id", "type"].every((key) => {
123
+ const left = exported.plain(exportedDependency[key]);
124
+ return left !== undefined && JSON.stringify(left) === JSON.stringify(state.plain(stateDependency[key]));
125
+ });
126
+ const exportedRef = exportedDependency["condition"];
127
+ const stateRef = stateDependency["condition"];
128
+ if (same && typeof exportedRef === "string" && typeof stateRef === "string")
129
+ pair(exportedRef, stateRef);
130
+ }
131
+ // Every valid condition is reachable from a component or dependency; this
132
+ // covers a remotely reordered graph. Only a fingerprint unique in state
133
+ // qualifies, so two authored conditions with equal content are never merged.
134
+ const stateByFingerprint = new Map();
135
+ for (const id of Object.keys(state.conditions).sort()) {
136
+ const print = fingerprint(state, id);
137
+ if (print !== undefined)
138
+ stateByFingerprint.set(print, [...(stateByFingerprint.get(print) ?? []), id]);
139
+ }
140
+ for (const id of Object.keys(exported.conditions).sort()) {
141
+ if (renames.has(id))
142
+ continue;
143
+ const print = fingerprint(exported, id);
144
+ const candidates = print === undefined ? undefined : stateByFingerprint.get(print);
145
+ if (candidates?.length === 1)
146
+ pair(id, candidates[0]);
147
+ }
148
+ // A rename onto a key the export keeps for another condition would merge
149
+ // the two. Dropping one rename can leave the key it vacated in use, which
150
+ // invalidates a rename onto that key in turn, so repeat until none drops.
151
+ for (let dropped = true; dropped;) {
152
+ dropped = false;
153
+ for (const [from, to] of [...renames]) {
154
+ if (from !== to && to in exported.conditions && !renames.has(to)) {
155
+ renames.delete(from);
156
+ dropped = true;
157
+ }
158
+ }
159
+ }
160
+ return renames;
161
+ }
162
+ function renameRef(value, renames) {
163
+ return typeof value === "string" ? (renames.get(value) ?? value) : value;
164
+ }
165
+ function renameList(value, renames) {
166
+ return Array.isArray(value) ? value.map((item) => renameRef(item, renames)) : value;
167
+ }
168
+ function withAttribute(map, key, value) {
169
+ return value === undefined ? map : { ...map, [key]: value };
170
+ }
171
+ /** JSON with object keys sorted, so two equal bodies print the same. */
172
+ function stableJson(value) {
173
+ if (Array.isArray(value))
174
+ return `[${value.map(stableJson).join(",")}]`;
175
+ if (typeof value === "object" && value !== null) {
176
+ const object = value;
177
+ return `{${Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${stableJson(object[key])}`).join(",")}}`;
178
+ }
179
+ return JSON.stringify(value) ?? "null";
180
+ }
181
+ /** The renamed conditions; copies of one authored condition collapse onto its key, anything else on one key is a bug. */
182
+ function collapseConditions(address, entries) {
183
+ const out = {};
184
+ const from = new Map();
185
+ for (const [exportedId, key, body] of entries) {
186
+ const earlier = from.get(key);
187
+ if (earlier !== undefined && stableJson(out[key]) !== stableJson(body)) {
188
+ throw new Error(`${address}: restoring authored condition keys from state would merge exported conditions "${earlier}" and "${exportedId}", ` +
189
+ `which differ, into "${key}". This is an EDK bug (edk#2414); please report it with the layout's state and export.`);
190
+ }
191
+ if (earlier === undefined)
192
+ from.set(key, exportedId);
193
+ out[key] = body;
194
+ }
195
+ return out;
196
+ }
197
+ /** The layout's attributes with every renamed condition key and reference rewritten. */
198
+ function renameConditions(address, attrs, renames) {
199
+ const next = { ...attrs };
200
+ const conditions = asMap(attrs["conditions"]);
201
+ if (conditions !== undefined) {
202
+ next["conditions"] = collapseConditions(address, Object.entries(conditions).map(([id, condition]) => {
203
+ let body = (asMap(condition) ?? {});
204
+ for (const kind of GROUP_KINDS) {
205
+ const group = asMap(body[kind]);
206
+ if (group !== undefined)
207
+ body = { ...body, [kind]: withAttribute(group, "conditions", renameList(group["conditions"], renames)) };
208
+ }
209
+ const negation = asMap(body["not"]);
210
+ if (negation !== undefined)
211
+ body = { ...body, not: withAttribute(negation, "condition", renameRef(negation["condition"], renames)) };
212
+ return [id, renames.get(id) ?? id, asMap(condition) === undefined ? condition : body];
213
+ }));
214
+ }
215
+ const components = asMap(attrs["components"]);
216
+ if (components !== undefined) {
217
+ next["components"] = Object.fromEntries(Object.entries(components).map(([id, component]) => {
218
+ const body = asMap(component);
219
+ if (body === undefined)
220
+ return [id, component];
221
+ let renamed = body;
222
+ for (const list of COMPONENT_REFERENCE_LISTS)
223
+ renamed = withAttribute(renamed, list, renameList(body[list], renames));
224
+ return [id, renamed];
225
+ }));
226
+ }
227
+ if (Array.isArray(attrs["dependencies"])) {
228
+ next["dependencies"] = attrs["dependencies"].map((dependency) => {
229
+ const body = asMap(dependency);
230
+ return body === undefined ? dependency : withAttribute(body, "condition", renameRef(body["condition"], renames));
231
+ });
232
+ }
233
+ return next;
234
+ }
235
+ /**
236
+ * The exported layout's attributes with its condition keys restored from the
237
+ * layout's state attributes, or the same object when nothing pairs.
238
+ */
239
+ export const restoreLayoutConditionKeys = (attrs, stateAttributes, { address, resolve }) => {
240
+ const exportedConditions = asMap(attrs["conditions"]);
241
+ const stateConditions = asMap(stateAttributes["conditions"]);
242
+ if (exportedConditions === undefined || stateConditions === undefined)
243
+ return attrs;
244
+ const exported = {
245
+ conditions: exportedConditions,
246
+ components: asMap(attrs["components"]) ?? {},
247
+ dependencies: Array.isArray(attrs["dependencies"]) ? attrs["dependencies"] : [],
248
+ plain: (value) => plainValue(value, resolve),
249
+ };
250
+ const state = {
251
+ conditions: stateConditions,
252
+ components: asMap(stateAttributes["components"]) ?? {},
253
+ dependencies: Array.isArray(stateAttributes["dependencies"]) ? stateAttributes["dependencies"] : [],
254
+ plain: (value) => plainValue(value, undefined),
255
+ };
256
+ const renames = pairLayoutConditions(exported, state);
257
+ for (const [from, to] of [...renames])
258
+ if (from === to)
259
+ renames.delete(from);
260
+ return renames.size === 0 ? attrs : renameConditions(address, attrs, renames);
261
+ };
@@ -2,7 +2,7 @@ import { unsafePlatformId } from "../../core/platform-id.js";
2
2
  import { dataRefLabel, optionalString, plainObject, reportAuthoringBlocker, resourceRefLabel } from "../../engine/pull/context.js";
3
3
  import { isTfExpr, isUuid, scopedLabelToBagKey, tfLabelToTsLabel, } from "../../engine/tf/index.js";
4
4
  import { filterFromTf } from "../access-policy/from-tf.js";
5
- import { fieldKeyFromTfValue } from "../field/from-tf.js";
5
+ import { elementAuditFieldReference, fieldKeyFromTfValue } from "../field/from-tf.js";
6
6
  import { filterOwnerForAspect } from "../relationship/from-tf.js";
7
7
  import { filterWireToTf } from "../shared/filters/tf-codec.js";
8
8
  import { resolveDataAspectTarget } from "../shared/aspect-target-from-tf.js";
@@ -207,7 +207,9 @@ function componentMap(values, layoutKey, owner, ctx, where) {
207
207
  if (automation["automationId"] !== undefined) {
208
208
  const label = resourceRefLabel(automation["automationId"], "elementum_automation");
209
209
  if (label !== undefined) {
210
- automation["automation"] = pulledRef("automation", scopedLabelToBagKey(owner.ownerTfLabel, label, "app"), owner.ownerRefName);
210
+ // The display name lets build look the automation up when pull
211
+ // leaves it platform-managed, so it is not in source (edk#2693).
212
+ automation["automation"] = pulledRef("automation", scopedLabelToBagKey(owner.ownerTfLabel, label, "app"), owner.ownerRefName, exportedAutomationName(label, ctx));
211
213
  }
212
214
  else
213
215
  ctx.diagnostics.push(`${where}.components.${id}.automationButton: automation_id is not a managed automation ref; dropped`);
@@ -235,8 +237,11 @@ function componentMap(values, layoutKey, owner, ctx, where) {
235
237
  return [];
236
238
  }
237
239
  input["parameterName"] = managed.name;
240
+ // The same automation, reached through its on-demand trigger,
241
+ // stays on the button. A different one needs its own display
242
+ // name so build can look it up when it is not in source.
238
243
  if (managed.automationLabel !== automationLabel) {
239
- input["automation"] = pulledRef("automation", scopedLabelToBagKey(owner.ownerTfLabel, managed.automationLabel, "app"), owner.ownerRefName);
244
+ input["automation"] = pulledRef("automation", scopedLabelToBagKey(owner.ownerTfLabel, managed.automationLabel, "app"), owner.ownerRefName, exportedAutomationName(managed.automationLabel, ctx));
240
245
  }
241
246
  delete input["parameterId"];
242
247
  }
@@ -284,7 +289,13 @@ function managedParameter(parameter, ctx) {
284
289
  if (parameter.path.length !== 2 || parameter.path[0] !== "parameter_ids" || typeof name !== "string")
285
290
  return undefined;
286
291
  if (parameter.$expr === "resource-attr") {
287
- return parameter.type === "elementum_on_demand_trigger" ? { automationLabel: parameter.label, name } : undefined;
292
+ if (parameter.type !== "elementum_on_demand_trigger")
293
+ return undefined;
294
+ // A single trigger shares the automation's label. A later trigger is
295
+ // `<auto>_<type>_<index>`, so the trigger label is not the automation.
296
+ // Follow the trigger's own `automation` attribute back to the owner.
297
+ const owning = owningAutomationLabel(parameter.label, ctx);
298
+ return { automationLabel: owning ?? parameter.label, name };
288
299
  }
289
300
  if (parameter.type !== "elementum_automation")
290
301
  return undefined;
@@ -292,6 +303,24 @@ function managedParameter(parameter, ctx) {
292
303
  const automationLabel = automationId === undefined ? undefined : resourceRefLabel(automationId, "elementum_automation");
293
304
  return automationLabel === undefined ? undefined : { automationLabel, name };
294
305
  }
306
+ /** Display name of an exported `elementum_automation`, when the stream has one. */
307
+ function exportedAutomationName(label, ctx) {
308
+ const exportedName = ctx.resourceByAddress?.(`elementum_automation.${label}`)?.attrs["name"];
309
+ return typeof exportedName === "string" && exportedName !== "" ? exportedName : undefined;
310
+ }
311
+ /**
312
+ * The automation an on-demand trigger belongs to. Export writes
313
+ * `automation = elementum_automation.<label>` with no `.id`.
314
+ */
315
+ function owningAutomationLabel(triggerLabel, ctx) {
316
+ const automation = ctx.resourceByAddress?.(`elementum_on_demand_trigger.${triggerLabel}`)?.attrs["automation"];
317
+ if (automation === undefined || !isTfExpr(automation) || automation.$expr !== "resource-attr" || automation.type !== "elementum_automation") {
318
+ return undefined;
319
+ }
320
+ if (automation.path.length === 0 || (automation.path.length === 1 && automation.path[0] === "id"))
321
+ return automation.label;
322
+ return undefined;
323
+ }
295
324
  function relatedItemsBlock(block, owner, ctx, where) {
296
325
  const aspectId = block["relatedAspectId"];
297
326
  delete block["relatedAspectId"];
@@ -302,6 +331,15 @@ function relatedItemsBlock(block, owner, ctx, where) {
302
331
  else
303
332
  ctx.diagnostics.push(`${where}: related_aspect_id is not resolvable; dropped`);
304
333
  }
334
+ const widgetId = block["widgetId"];
335
+ delete block["widgetId"];
336
+ if (widgetId !== undefined) {
337
+ const label = resourceRefLabel(widgetId, WIDGET_RESOURCE_TYPE);
338
+ if (label !== undefined)
339
+ block["widget"] = pulledRef("widget", scopedLabelToBagKey(owner.label, label, scopeOf(owner)), owner.ownerRefName);
340
+ else
341
+ ctx.diagnostics.push(`${where}: widget_id is not a managed widget ref; dropped`);
342
+ }
305
343
  const raw = block["filter"];
306
344
  delete block["filter"];
307
345
  if (raw === undefined)
@@ -438,7 +476,8 @@ function fieldKey(value, owner, ctx, where, site = where) {
438
476
  // A field pull did not extract keeps its platform id: its key would print
439
477
  // as nothing and build would drop the entry (#1966). With no id the pull
440
478
  // fails, naming the entry (#1978).
441
- const key = owner.unextracted === undefined ? resolved : unextractedFieldRefId(value, resolved, owner.unextracted, ctx, site);
479
+ const kept = owner.unextracted === undefined ? resolved : unextractedFieldRefId(value, resolved, owner.unextracted, ctx, site);
480
+ const key = kept === undefined || kept !== resolved ? kept : elementAuditFieldReference(value, kept, { ...owner, scope: scopeOf(owner) }, ctx);
442
481
  if (key === undefined) {
443
482
  ctx.diagnostics.push(`${where}: field reference ${JSON.stringify(value)} is not resolvable; dropped`);
444
483
  }
@@ -462,7 +501,7 @@ function lostStageRefs(block, owner, where) {
462
501
  continue;
463
502
  const pulled = pulledStageHandle(index, owner.stageHandles);
464
503
  if (pulled.kind === "lost")
465
- lost.push({ where: ref.where, index, handle: pulled.handle, owner: pulled.owner });
504
+ lost.push({ where: ref.where, index, handle: pulled.handle, owner: pulled.owner, ...(pulled.fallback === true && { fallback: true }) });
466
505
  }
467
506
  return lost;
468
507
  }
@@ -1,3 +1,4 @@
1
+ import { restoreLayoutConditionKeys } from "./condition-keys.js";
1
2
  import { NO_CANVAS, NO_CATALOG, NO_DATA, NO_LIFECYCLE } from "../../graph/types.js";
2
3
  export const DYNAMIC_LAYOUT_RESOURCE_TYPE = "elementum_dynamic_layout";
3
4
  export const dynamicLayoutModule = {
@@ -8,7 +9,7 @@ export const dynamicLayoutModule = {
8
9
  { parent: "app", child: "dynamic-layout" },
9
10
  { parent: "element", child: "dynamic-layout" },
10
11
  ],
11
- resource: { shape: "single", resourceType: DYNAMIC_LAYOUT_RESOURCE_TYPE, adoptable: true },
12
+ resource: { shape: "single", resourceType: DYNAMIC_LAYOUT_RESOURCE_TYPE, adoptable: true, alignWithState: restoreLayoutConditionKeys },
12
13
  data: NO_DATA,
13
14
  lifecycle: NO_LIFECYCLE,
14
15
  canvas: NO_CANVAS,
@@ -569,17 +569,22 @@ function automationButtonInput(rec, inputs, where, opts) {
569
569
  token: rec["automation"],
570
570
  value: resolveAutomation(rec["automation"], `${where}.automation`, opts),
571
571
  };
572
- const label = target?.value !== undefined && isTfExpr(target.value) && target.value.$expr === "resource-attr"
573
- ? target.value.label
574
- : undefined;
572
+ const value = target?.value;
575
573
  const name = rec["parameterName"];
576
- if (label === undefined || typeof name !== "string") {
574
+ const ref = value !== undefined && isTfExpr(value) && (value.$expr === "resource-attr" || value.$expr === "data-attr") && value.type === AUTOMATION_RESOURCE_TYPE
575
+ ? value
576
+ : undefined;
577
+ if (ref === undefined || typeof name !== "string") {
577
578
  opts.diagnostics.push(`${where}: parameterName needs a managed button automation to bind to; dropped`);
578
579
  return [];
579
580
  }
580
- out["parameter_id"] = isActiveAutomation(target?.token.refName ?? "", opts)
581
- ? expr.dataAttr(AUTOMATION_RESOURCE_TYPE, publishedAutomationLookup(label, inputs), ["parameter_ids", { index: name }])
582
- : expr.resourceAttr("elementum_on_demand_trigger", label, ["parameter_ids", { index: name }]);
581
+ const label = ref.label;
582
+ // A platform-managed automation is already a lookup of its published workflow.
583
+ out["parameter_id"] = ref.$expr === "data-attr"
584
+ ? expr.dataAttr(AUTOMATION_RESOURCE_TYPE, label, ["parameter_ids", { index: name }])
585
+ : isActiveAutomation(target?.token.refName ?? "", opts)
586
+ ? expr.dataAttr(AUTOMATION_RESOURCE_TYPE, publishedAutomationLookup(label, inputs), ["parameter_ids", { index: name }])
587
+ : expr.resourceAttr("elementum_on_demand_trigger", label, ["parameter_ids", { index: name }]);
583
588
  }
584
589
  if (Array.isArray(rec["fieldRefParams"])) {
585
590
  out["field_ref_params"] = rec["fieldRefParams"].flatMap((param, index) => {
@@ -593,6 +598,7 @@ function automationButtonInput(rec, inputs, where, opts) {
593
598
  function relatedItemsBlock(rec, where, opts) {
594
599
  const out = snakeObject(rec, where, opts);
595
600
  delete out["related"];
601
+ delete out["widget"];
596
602
  delete out["filter"];
597
603
  const related = rec["related"];
598
604
  if (related !== undefined) {
@@ -600,6 +606,11 @@ function relatedItemsBlock(rec, where, opts) {
600
606
  if (aspect !== undefined)
601
607
  out["related_aspect_id"] = aspect;
602
608
  }
609
+ if (rec["widget"] !== undefined) {
610
+ const widget = resolveWidget(rec["widget"], `${where}.widget`, opts);
611
+ if (widget !== undefined)
612
+ out["widget_id"] = widget;
613
+ }
603
614
  if (rec["filter"] !== undefined) {
604
615
  const foreignField = related === undefined
605
616
  ? undefined
@@ -722,7 +733,7 @@ function resolveField(value, where, opts) {
722
733
  opts.diagnostics.push(`${where}: field "${refName}" targets a different ${scopeOf(opts)}; dropped`);
723
734
  return undefined;
724
735
  }
725
- const resolved = fieldRefToTfValue(token?.fieldType ?? "", refName, opts.ownerTfLabel, scopeOf(opts), opts.fieldAddresses);
736
+ const resolved = fieldRefToTfValue(token?.fieldType ?? "", refName, opts.ownerTfLabel, scopeOf(opts), opts.fieldAddresses, token?.resourceKey);
726
737
  if (resolved === undefined)
727
738
  opts.diagnostics.push(`${where}: field "${refName}" has no Terraform mapping; dropped`);
728
739
  return resolved;
@@ -731,11 +742,12 @@ function typedValue(value, fieldValue, opts) {
731
742
  const rec = object(value);
732
743
  const token = fieldValue;
733
744
  if (typeof rec["string"] === "string" && token?.options?.includes(rec["string"])) {
745
+ const childPart = tsLabelToTfLabel(token.resourceKey ?? token.refName ?? "");
734
746
  const localName = token.tags?.includes("STATUS")
735
747
  ? `${opts.ownerTfLabel}_status_options_by_label`
736
748
  : token.tags?.includes("STAGE")
737
749
  ? `${opts.ownerTfLabel}_stage_options_by_label`
738
- : `${scopedResourceLabel(opts.ownerTfLabel, tsLabelToTfLabel(token.refName ?? ""), scopeOf(opts))}_options_by_label`;
750
+ : `${scopedResourceLabel(opts.ownerTfLabel, childPart, scopeOf(opts))}_options_by_label`;
739
751
  return { string: expr.local(localName, [{ index: rec["string"] }]) };
740
752
  }
741
753
  return snakeObject(rec, "dynamic layout typed value", opts);
@@ -748,13 +760,29 @@ function buttonAutomationProblem(token, opts) {
748
760
  }
749
761
  if (!isCanonicalTsLabel(refName))
750
762
  return `its automation key ${JSON.stringify(refName)} is not a canonical tsLabel`;
751
- const authored = opts.workspaceAspects?.get(workspaceAspectKey(scopeOf(opts), opts.ownerRefName))?.automationRefNames;
752
763
  // A reference to an automation build never declares would fail plan with
753
764
  // `Reference to undeclared resource` (#1877).
754
- if (authored !== undefined && !authored.has(refName))
755
- return `its automation ${JSON.stringify(refName)} is not in source`;
765
+ if (!automationInSource(refName, opts) && platformAutomationName(token, opts) === undefined) {
766
+ return `its automation ${JSON.stringify(refName)} is not in source, and the reference has no display name to look the live automation up by`;
767
+ }
756
768
  return undefined;
757
769
  }
770
+ function automationInSource(refName, opts) {
771
+ const authored = opts.workspaceAspects?.get(workspaceAspectKey(scopeOf(opts), opts.ownerRefName))?.automationRefNames;
772
+ return authored === undefined || authored.has(refName);
773
+ }
774
+ /**
775
+ * The display name of an automation pull left platform-managed, so build can
776
+ * look the live one up as a run-automation widget does (edk#2693). A pull
777
+ * that could not read the display name prints the key as the name.
778
+ */
779
+ function platformAutomationName(token, opts) {
780
+ const refName = token?.refName ?? "";
781
+ if (automationInSource(refName, opts))
782
+ return undefined;
783
+ const name = token?.name;
784
+ return typeof name === "string" && name !== "" && name !== refName ? name : undefined;
785
+ }
758
786
  function resolveAutomation(token, where, opts) {
759
787
  const problem = buttonAutomationProblem(token, opts);
760
788
  if (problem !== undefined) {
@@ -762,6 +790,12 @@ function resolveAutomation(token, where, opts) {
762
790
  return undefined;
763
791
  }
764
792
  const refName = token?.refName ?? "";
793
+ const platformName = platformAutomationName(token, opts);
794
+ if (platformName !== undefined) {
795
+ const label = `${opts.ownerTfLabel}_${tsLabelToTfLabel(refName)}`;
796
+ opts.orgRefs.rawLookup(AUTOMATION_RESOURCE_TYPE, label, platformName, { app_id: opts.objectId });
797
+ return expr.dataAttr(AUTOMATION_RESOURCE_TYPE, label, ["id"]);
798
+ }
765
799
  return expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, automationResourceLabel(opts.ownerTfLabel, refName, scopeOf(opts), scopeOf(opts) === "element" ? opts.ownerHandle : undefined), ["id"]);
766
800
  }
767
801
  function isActiveAutomation(refName, opts) {
@@ -0,0 +1,9 @@
1
+ import type { BuiltRoot } from "../../graph/types.js";
2
+ /**
3
+ * The build error for audit-field references, across the rendered files, on
4
+ * elements built without audit fields; empty when there are none. Each
5
+ * problem names the file, the resource and attribute that hold the
6
+ * reference, and, when the element authors its own field under that key,
7
+ * that field's ref.
8
+ */
9
+ export declare function missingElementAuditFieldErrors(files: ReadonlyMap<string, string>, roots: readonly BuiltRoot[]): readonly string[];
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The build guard for audit-field references on elements that don't have
3
+ * those fields (#2513). An element's audit-field ids are null unless the
4
+ * platform gave it those fields, and EDK cannot create them; a reference
5
+ * would reach the platform as a null field id (a column that vanishes, a
6
+ * filter that fails). Every field resolver addresses an element audit field
7
+ * through its `<ns>_<key>_field_id` local, so a scan of the rendered files
8
+ * sees string keys and FieldRefs alike. Trigger outputs are refused by the
9
+ * automation resolver.
10
+ */
11
+ const AUDIT_FIELD_LOCAL = /\blocal\.([a-z0-9_]+?)_((?:created|updated)_(?:at|by))_field_id\b/g;
12
+ const AUDIT_FIELD_KEY_BY_ATTR = {
13
+ created_at: "createdAt",
14
+ created_by: "createdBy",
15
+ updated_at: "updatedAt",
16
+ updated_by: "updatedBy",
17
+ };
18
+ const BLOCK_HEADER = /^(?:resource|data) "([^"]+)" "([^"]+)" \{$/gm;
19
+ const NAMED_LINE = /^\s*([a-z0-9_]+)\s*(?:=|(?:"[^"]*"\s*)*\{)/;
20
+ /**
21
+ * `<type>.<label>.<attribute path>: ` for the reference at `index` in
22
+ * rendered HCL: the enclosing block and every attribute or nested block that
23
+ * holds it (`elementum_layout.x_layout.sections.field_ids`). Empty when the
24
+ * text has no enclosing block.
25
+ */
26
+ function auditReferenceSite(content, index) {
27
+ const before = content.slice(0, index);
28
+ const block = [...before.matchAll(BLOCK_HEADER)].at(-1);
29
+ if (block === undefined)
30
+ return "";
31
+ const lines = before.slice(block.index + block[0].length + 1).split("\n");
32
+ const path = [];
33
+ let depth = 1;
34
+ for (const [i, line] of lines.entries()) {
35
+ const text = line.replace(/"(?:[^"\\]|\\.)*"/g, '""');
36
+ const name = NAMED_LINE.exec(text)?.[1];
37
+ if (name !== undefined) {
38
+ while (path.length > 0 && path.at(-1).depth >= depth)
39
+ path.pop();
40
+ path.push({ name, depth });
41
+ }
42
+ if (i === lines.length - 1)
43
+ break;
44
+ depth += (text.match(/[{[(]/g)?.length ?? 0) - (text.match(/[}\])]/g)?.length ?? 0);
45
+ while (path.length > 0 && path.at(-1).depth >= depth)
46
+ path.pop();
47
+ }
48
+ return `${[block[1], block[2], ...path.map((entry) => entry.name)].join(".")}: `;
49
+ }
50
+ /**
51
+ * The build error for audit-field references, across the rendered files, on
52
+ * elements built without audit fields; empty when there are none. Each
53
+ * problem names the file, the resource and attribute that hold the
54
+ * reference, and, when the element authors its own field under that key,
55
+ * that field's ref.
56
+ */
57
+ export function missingElementAuditFieldErrors(files, roots) {
58
+ const elementsWithoutAuditFields = new Map(roots
59
+ .filter((root) => root.kind === "element" && root.payload?.auditFields !== true)
60
+ .map((root) => [root.tfLabel, root]));
61
+ if (elementsWithoutAuditFields.size === 0)
62
+ return [];
63
+ const problems = [];
64
+ for (const [file, content] of files) {
65
+ if (file === "locals.tf")
66
+ continue;
67
+ const seen = new Set();
68
+ for (const match of content.matchAll(AUDIT_FIELD_LOCAL)) {
69
+ const element = elementsWithoutAuditFields.get(match[1]);
70
+ const key = AUDIT_FIELD_KEY_BY_ATTR[match[2]];
71
+ if (element === undefined || key === undefined)
72
+ continue;
73
+ const own = element.fieldKeys.has(key)
74
+ ? ` (a bare "${key}" names the platform audit field; for this element's own field use its field ref, fields.${key})`
75
+ : "";
76
+ const problem = `${file}: ${auditReferenceSite(content, match.index)}element "${element.refName}" field "${key}"${own}`;
77
+ if (seen.has(problem))
78
+ continue;
79
+ seen.add(problem);
80
+ problems.push(problem);
81
+ }
82
+ }
83
+ if (problems.length === 0)
84
+ return [];
85
+ return [`audit fields referenced on elements that do not have them:\n ${problems.join("\n ")}\n` +
86
+ "The platform adds Created on/by and Updated on/by to apps but only to some elements, and EDK cannot create them. " +
87
+ "Remove the reference, or set systemFields: { audit: true } on an element the platform did give them to."];
88
+ }