@elementumai/edk 0.12.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 (193) hide show
  1. package/README.md +2 -0
  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/types/app.d.ts +6 -0
  13. package/dist-sdk/src/engine/build/context.d.ts +3 -0
  14. package/dist-sdk/src/engine/pull/authored-address.js +42 -6
  15. package/dist-sdk/src/engine/pull/authored-root.d.ts +88 -0
  16. package/dist-sdk/src/engine/pull/authored-root.js +306 -0
  17. package/dist-sdk/src/engine/pull/context.d.ts +10 -0
  18. package/dist-sdk/src/engine/pull/field-refs-registry.d.ts +114 -0
  19. package/dist-sdk/src/engine/pull/field-refs-registry.js +501 -0
  20. package/dist-sdk/src/engine/pull/org.d.ts +1 -0
  21. package/dist-sdk/src/engine/pull/org.js +2 -0
  22. package/dist-sdk/src/engine/pull/partition.d.ts +8 -0
  23. package/dist-sdk/src/engine/pull/partition.js +43 -0
  24. package/dist-sdk/src/engine/pull/pulled-roots.d.ts +89 -0
  25. package/dist-sdk/src/engine/pull/pulled-roots.js +203 -0
  26. package/dist-sdk/src/engine/pull/ref-only.d.ts +4 -0
  27. package/dist-sdk/src/engine/pull/ref-only.js +7 -1
  28. package/dist-sdk/src/engine/pull/source-resources.d.ts +39 -0
  29. package/dist-sdk/src/engine/pull/source-resources.js +110 -0
  30. package/dist-sdk/src/engine/pull/stream-root.d.ts +148 -0
  31. package/dist-sdk/src/engine/pull/stream-root.js +563 -0
  32. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.d.ts +47 -0
  33. package/dist-sdk/src/engine/workspace/reserved-catalog-keys.js +115 -0
  34. package/dist-sdk/src/entities/access-policy/build.d.ts +2 -1
  35. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +7 -0
  36. package/dist-sdk/src/entities/access-policy/from-tf.js +18 -3
  37. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +19 -0
  38. package/dist-sdk/src/entities/access-policy/to-tf.js +65 -8
  39. package/dist-sdk/src/entities/agent/build.d.ts +52 -1
  40. package/dist-sdk/src/entities/agent/build.js +34 -0
  41. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +68 -0
  42. package/dist-sdk/src/entities/agent/from-tf.d.ts +34 -1
  43. package/dist-sdk/src/entities/agent/from-tf.js +262 -2
  44. package/dist-sdk/src/entities/agent/index.d.ts +1 -1
  45. package/dist-sdk/src/entities/agent/to-tf.d.ts +8 -10
  46. package/dist-sdk/src/entities/agent/to-tf.js +158 -14
  47. package/dist-sdk/src/entities/agent-tool/from-tf.js +6 -0
  48. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +2 -2
  49. package/dist-sdk/src/entities/agent-tool/to-tf.js +2 -2
  50. package/dist-sdk/src/entities/app/build.d.ts +33 -13
  51. package/dist-sdk/src/entities/app/build.js +14 -12
  52. package/dist-sdk/src/entities/app/from-tf.js +24 -9
  53. package/dist-sdk/src/entities/app/index.d.ts +1 -0
  54. package/dist-sdk/src/entities/app/index.js +1 -0
  55. package/dist-sdk/src/entities/app/module.js +1 -0
  56. package/dist-sdk/src/entities/app/stage-keys.d.ts +2 -2
  57. package/dist-sdk/src/entities/app/stage-keys.js +3 -3
  58. package/dist-sdk/src/entities/app/to-tf.js +1 -0
  59. package/dist-sdk/src/entities/approval-chain/build.d.ts +46 -11
  60. package/dist-sdk/src/entities/approval-chain/build.js +120 -8
  61. package/dist-sdk/src/entities/approval-chain/from-tf.d.ts +24 -2
  62. package/dist-sdk/src/entities/approval-chain/from-tf.js +148 -6
  63. package/dist-sdk/src/entities/approval-chain/index.d.ts +4 -1
  64. package/dist-sdk/src/entities/approval-chain/index.js +4 -1
  65. package/dist-sdk/src/entities/approval-chain/module.js +23 -5
  66. package/dist-sdk/src/entities/approval-chain/printer.d.ts +1 -1
  67. package/dist-sdk/src/entities/approval-chain/printer.js +91 -5
  68. package/dist-sdk/src/entities/approval-chain/steps-tf.d.ts +77 -0
  69. package/dist-sdk/src/entities/approval-chain/steps-tf.js +255 -0
  70. package/dist-sdk/src/entities/approval-chain/steps.d.ts +71 -0
  71. package/dist-sdk/src/entities/approval-chain/steps.js +83 -0
  72. package/dist-sdk/src/entities/approval-chain/to-tf.d.ts +16 -6
  73. package/dist-sdk/src/entities/approval-chain/to-tf.js +37 -4
  74. package/dist-sdk/src/entities/automation/build.js +70 -27
  75. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +20 -5
  76. package/dist-sdk/src/entities/automation/from-tf.d.ts +26 -5
  77. package/dist-sdk/src/entities/automation/from-tf.js +593 -101
  78. package/dist-sdk/src/entities/automation/index.d.ts +1 -0
  79. package/dist-sdk/src/entities/automation/index.js +1 -0
  80. package/dist-sdk/src/entities/automation/module.d.ts +4 -3
  81. package/dist-sdk/src/entities/automation/module.js +33 -12
  82. package/dist-sdk/src/entities/automation/refs-read.d.ts +19 -0
  83. package/dist-sdk/src/entities/automation/refs-read.js +48 -0
  84. package/dist-sdk/src/entities/automation/slack-message-refs.d.ts +9 -0
  85. package/dist-sdk/src/entities/automation/slack-message-refs.js +75 -0
  86. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +11 -0
  87. package/dist-sdk/src/entities/automation/task-output-labels.js +23 -0
  88. package/dist-sdk/src/entities/automation/tasks.d.ts +29 -4
  89. package/dist-sdk/src/entities/automation/tasks.js +34 -11
  90. package/dist-sdk/src/entities/automation/to-tf.d.ts +3 -0
  91. package/dist-sdk/src/entities/automation/to-tf.js +337 -77
  92. package/dist-sdk/src/entities/automation/triggers.d.ts +0 -8
  93. package/dist-sdk/src/entities/automation/triggers.js +26 -11
  94. package/dist-sdk/src/entities/automation/unsafe-script-output.d.ts +50 -0
  95. package/dist-sdk/src/entities/automation/unsafe-script-output.js +92 -0
  96. package/dist-sdk/src/entities/chart/build.d.ts +9 -1
  97. package/dist-sdk/src/entities/chart/from-tf.js +3 -0
  98. package/dist-sdk/src/entities/chart/to-tf.js +19 -2
  99. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +2 -0
  100. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +44 -5
  101. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +46 -12
  102. package/dist-sdk/src/entities/element/audit-field-guard.d.ts +9 -0
  103. package/dist-sdk/src/entities/element/audit-field-guard.js +88 -0
  104. package/dist-sdk/src/entities/element/build.js +2 -1
  105. package/dist-sdk/src/entities/element/from-tf.js +15 -2
  106. package/dist-sdk/src/entities/element/module.js +3 -0
  107. package/dist-sdk/src/entities/element/system-fields.d.ts +13 -0
  108. package/dist-sdk/src/entities/element/system-fields.js +23 -0
  109. package/dist-sdk/src/entities/element/to-tf.js +1 -0
  110. package/dist-sdk/src/entities/field/build.d.ts +9 -0
  111. package/dist-sdk/src/entities/field/from-tf.d.ts +29 -1
  112. package/dist-sdk/src/entities/field/from-tf.js +99 -6
  113. package/dist-sdk/src/entities/field/module.js +4 -0
  114. package/dist-sdk/src/entities/field/to-tf.d.ts +5 -1
  115. package/dist-sdk/src/entities/field/to-tf.js +33 -12
  116. package/dist-sdk/src/entities/org/category.js +1 -0
  117. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  118. package/dist-sdk/src/entities/org/index.js +1 -0
  119. package/dist-sdk/src/entities/org/voice-auth.d.ts +5 -0
  120. package/dist-sdk/src/entities/org/voice-auth.js +33 -0
  121. package/dist-sdk/src/entities/organization/build.d.ts +3 -0
  122. package/dist-sdk/src/entities/organization/build.js +2 -0
  123. package/dist-sdk/src/entities/organization/index.d.ts +1 -0
  124. package/dist-sdk/src/entities/organization/index.js +1 -0
  125. package/dist-sdk/src/entities/organization/to-tf.d.ts +2 -0
  126. package/dist-sdk/src/entities/organization/to-tf.js +59 -1
  127. package/dist-sdk/src/entities/organization/voice-auth.d.ts +60 -0
  128. package/dist-sdk/src/entities/organization/voice-auth.js +60 -0
  129. package/dist-sdk/src/entities/phone-service/build.d.ts +9 -1
  130. package/dist-sdk/src/entities/phone-service/build.js +1 -0
  131. package/dist-sdk/src/entities/phone-service/deserializer/print-phone-service.js +3 -0
  132. package/dist-sdk/src/entities/phone-service/from-tf.d.ts +1 -0
  133. package/dist-sdk/src/entities/phone-service/from-tf.js +8 -1
  134. package/dist-sdk/src/entities/phone-service/to-tf.js +16 -0
  135. package/dist-sdk/src/entities/record-create-form/from-tf.js +2 -2
  136. package/dist-sdk/src/entities/relationship/from-tf.js +7 -2
  137. package/dist-sdk/src/entities/relationship/module.js +2 -2
  138. package/dist-sdk/src/entities/relationship/pair-check.d.ts +6 -2
  139. package/dist-sdk/src/entities/relationship/pair-check.js +105 -18
  140. package/dist-sdk/src/entities/relationship/to-tf.js +5 -2
  141. package/dist-sdk/src/entities/role/permission-levels.d.ts +7 -0
  142. package/dist-sdk/src/entities/role/permission-levels.js +78 -0
  143. package/dist-sdk/src/entities/role/to-tf.js +32 -2
  144. package/dist-sdk/src/entities/service-account/module.d.ts +2 -1
  145. package/dist-sdk/src/entities/service-account/module.js +4 -1
  146. package/dist-sdk/src/entities/service-account/resource-type.d.ts +2 -0
  147. package/dist-sdk/src/entities/service-account/resource-type.js +2 -0
  148. package/dist-sdk/src/entities/service-account/role-membership-check.d.ts +20 -0
  149. package/dist-sdk/src/entities/service-account/role-membership-check.js +80 -0
  150. package/dist-sdk/src/entities/shared/aspect-target-from-tf.d.ts +15 -0
  151. package/dist-sdk/src/entities/shared/aspect-target-from-tf.js +36 -0
  152. package/dist-sdk/src/entities/shared/filters/ast.d.ts +2 -0
  153. package/dist-sdk/src/entities/shared/filters/index.d.ts +85 -14
  154. package/dist-sdk/src/entities/shared/filters/index.js +18 -6
  155. package/dist-sdk/src/entities/shared/filters/option-lookups.d.ts +57 -0
  156. package/dist-sdk/src/entities/shared/filters/option-lookups.js +108 -0
  157. package/dist-sdk/src/entities/shared/filters/print.js +9 -5
  158. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +12 -7
  159. package/dist-sdk/src/entities/shared/filters/tf-codec.js +9 -5
  160. package/dist-sdk/src/entities/shared/raw-option-id.d.ts +30 -0
  161. package/dist-sdk/src/entities/shared/raw-option-id.js +41 -0
  162. package/dist-sdk/src/entities/shared/tool-field-ref.js +6 -2
  163. package/dist-sdk/src/entities/skill/input-coverage.js +5 -2
  164. package/dist-sdk/src/entities/sla/build.d.ts +96 -0
  165. package/dist-sdk/src/entities/sla/build.js +124 -0
  166. package/dist-sdk/src/entities/sla/from-tf.d.ts +40 -0
  167. package/dist-sdk/src/entities/sla/from-tf.js +164 -0
  168. package/dist-sdk/src/entities/sla/index.d.ts +1 -0
  169. package/dist-sdk/src/entities/sla/index.js +1 -0
  170. package/dist-sdk/src/entities/sla/module.d.ts +4 -0
  171. package/dist-sdk/src/entities/sla/module.js +73 -0
  172. package/dist-sdk/src/entities/sla/printer.d.ts +2 -0
  173. package/dist-sdk/src/entities/sla/printer.js +84 -0
  174. package/dist-sdk/src/entities/sla/to-tf.d.ts +12 -0
  175. package/dist-sdk/src/entities/sla/to-tf.js +81 -0
  176. package/dist-sdk/src/entities/table/build.d.ts +7 -2
  177. package/dist-sdk/src/entities/table/build.js +22 -1
  178. package/dist-sdk/src/entities/table/deserializer/normalize.d.ts +4 -1
  179. package/dist-sdk/src/entities/table/from-tf.js +89 -14
  180. package/dist-sdk/src/entities/table/index.d.ts +1 -1
  181. package/dist-sdk/src/entities/table/to-tf.js +41 -2
  182. package/dist-sdk/src/entities/task/from-tf.js +2 -1
  183. package/dist-sdk/src/entities/view/from-tf.js +7 -4
  184. package/dist-sdk/src/entities/widget/from-tf.js +13 -2
  185. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +49 -4
  186. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  187. package/dist-sdk/src/graph/index.d.ts +5 -0
  188. package/dist-sdk/src/graph/index.js +7 -0
  189. package/dist-sdk/src/graph/modules.js +11 -1
  190. package/dist-sdk/src/graph/resource-types.d.ts +47 -0
  191. package/dist-sdk/src/graph/resource-types.js +62 -0
  192. package/dist-sdk/src/graph/types.d.ts +78 -6
  193. package/package.json +6 -3
@@ -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
+ }
@@ -69,6 +69,7 @@ export function element(opts) {
69
69
  ...("dynamicSource" in field && field.dynamicSource === true ? { dynamicSource: true } : {}),
70
70
  ...(field.options !== undefined ? { options: picklistOptionLabels(field.options) } : {}),
71
71
  ...(field.tags !== undefined ? { tags: field.tags } : {}),
72
+ ...(field.resourceKey !== undefined ? { resourceKey: field.resourceKey } : {}),
72
73
  };
73
74
  }
74
75
  if (opts.readonlyFields !== undefined) {
@@ -109,7 +110,7 @@ export function element(opts) {
109
110
  };
110
111
  },
111
112
  def(refName) {
112
- assertNoSystemKeyCollision("element", opts.name, opts.fields, opts.systemFields);
113
+ assertNoSystemKeyCollision("element", opts.name, opts.fields, new Set(ELEMENT_SYSTEM_FIELD_KEYS));
113
114
  const displayBlocks = serializeDisplayBlocks(opts.displayBlocks, "LAYOUT");
114
115
  const recordCreateDisplayBlocks = serializeDisplayBlocks(opts.recordCreateDisplayBlocks, "CREATE");
115
116
  const payload = {
@@ -7,7 +7,7 @@ import { autoShareFromTf } from "../app/auto-share.js";
7
7
  import { DEPLOY_DEFAULT_SYSTEM_FIELD_NAMES } from "../../core/system-fields.js";
8
8
  import { EntityTfError } from "../../engine/entity.js";
9
9
  import { dataRefLabel, optionalBoolean, optionalString } from "../../engine/pull/context.js";
10
- import { tfLabelToTsLabel } from "../../engine/tf/index.js";
10
+ import { scopedLabelToBagKey, tfLabelToTsLabel } from "../../engine/tf/index.js";
11
11
  import { accessPolicyBlocksInLabelOrder, accessPolicyFromTf } from "../access-policy/from-tf.js";
12
12
  import { dashboardsFromTf } from "../dashboard/from-tf.js";
13
13
  import { dynamicLayoutCompanionWidgetLabels, dynamicLayoutsFromTf } from "../dynamic-layout/from-tf.js";
@@ -18,6 +18,7 @@ import { rolesFromTf } from "../role/from-tf.js";
18
18
  import { fieldKeysByDisplayName, viewsFromTf } from "../view/from-tf.js";
19
19
  import { widgetsFromTf } from "../widget/from-tf.js";
20
20
  import { ELEMENT_RESOURCE_TYPE } from "./module.js";
21
+ import { ELEMENT_SYSTEM_FIELD_KEYS } from "./system-fields.js";
21
22
  /** Attributes `elementFromTf` understands — anything else is dialect growth. */
22
23
  const KNOWN_ELEMENT_ATTRS = new Set([
23
24
  "name",
@@ -139,8 +140,20 @@ export function elementFromTf(elementBlock, fieldBlocks, ctx, viewBlocks = {
139
140
  fields.push(systemEntry("id", idName ?? DEPLOY_DEFAULT_SYSTEM_FIELD_NAMES.id, "TEXT", "HANDLE", idFieldDescription));
140
141
  }
141
142
  const owner = { type: ELEMENT_RESOURCE_TYPE, label, scope: "element", refName };
143
+ const usedFieldKeys = new Set();
144
+ const nonReservedBlocks = [];
145
+ const reservedBlocks = [];
146
+ const elementReserved = new Set(ELEMENT_SYSTEM_FIELD_KEYS);
142
147
  for (const block of fieldBlocks) {
143
- const entry = fieldFromTf(block, owner, ctx);
148
+ if (block.label === undefined) {
149
+ nonReservedBlocks.push(block);
150
+ continue;
151
+ }
152
+ const natural = scopedLabelToBagKey(label, block.label, "element");
153
+ (elementReserved.has(natural) ? reservedBlocks : nonReservedBlocks).push(block);
154
+ }
155
+ for (const block of [...nonReservedBlocks, ...reservedBlocks]) {
156
+ const entry = fieldFromTf(block, owner, ctx, usedFieldKeys);
144
157
  if (entry !== undefined)
145
158
  fields.push(entry);
146
159
  }
@@ -8,6 +8,7 @@ import { NO_CATALOG, } from "../../graph/types.js";
8
8
  import { appendFieldLocals } from "../field/locals.js";
9
9
  import { CALENDAR_VIEW_RESOURCE_TYPE, CUSTOM_VIEW_RESOURCE_TYPE, DASHBOARD_VIEW_RESOURCE_TYPE, KANBAN_VIEW_RESOURCE_TYPE, LAYOUT_RESOURCE_TYPE, LIST_VIEW_RESOURCE_TYPE } from "../view/types.js";
10
10
  import { elementAuditFieldIds, elementFromTf, referencesElementAuditFields } from "./from-tf.js";
11
+ import { missingElementAuditFieldErrors } from "./audit-field-guard.js";
11
12
  import { appendElementLocals } from "./locals.js";
12
13
  import { elementToTf } from "./to-tf.js";
13
14
  export const ELEMENT_RESOURCE_TYPE = "elementum_element";
@@ -79,6 +80,7 @@ export const elementModule = {
79
80
  diagnostics: ctx.diagnostics,
80
81
  workspaceAspects: ctx.workspaceAspects,
81
82
  ...(ctx.outOfScopeApps !== undefined && { outOfScopeApps: ctx.outOfScopeApps }),
83
+ ...(ctx.optionIds !== undefined && { optionIds: ctx.optionIds }),
82
84
  });
83
85
  return {
84
86
  blocks: result.blocks,
@@ -95,4 +97,5 @@ export const elementModule = {
95
97
  },
96
98
  },
97
99
  catalog: NO_CATALOG,
100
+ buildChecks: { validateRenderedFiles: missingElementAuditFieldErrors },
98
101
  };
@@ -2,3 +2,16 @@
2
2
  export declare const ELEMENT_AUDIT_SYSTEM_FIELD_KEYS: readonly ["createdAt", "createdBy", "updatedAt", "updatedBy"];
3
3
  export declare const ELEMENT_SYSTEM_FIELD_KEYS: readonly ["id", "title", "createdAt", "createdBy", "updatedAt", "updatedBy"];
4
4
  export type ElementSystemFieldKey = typeof ELEMENT_SYSTEM_FIELD_KEYS[number];
5
+ /**
6
+ * What pull extracts for a reference to an element's platform audit field
7
+ * when the element also authors its own field under that key (#2513). Print
8
+ * writes it as the bare key, and the own field's references stay the key and
9
+ * print as its field ref. It only exists between extract and print: a colon
10
+ * can't appear in a field key or a platform id.
11
+ */
12
+ export declare function platformAuditFieldToken(key: string): string;
13
+ /** The system-field key a {@link platformAuditFieldToken} names; undefined for anything else.
14
+ * Element audit keys and app system keys (status/closedAt/…) both use this token when
15
+ * a same-keyed custom field would otherwise retarget the platform reference (#1871 / #2513).
16
+ */
17
+ export declare function platformAuditFieldTokenKey(value: string): string | undefined;
@@ -1,3 +1,4 @@
1
+ import { SYSTEM_FIELD_KEYS } from "../../core/system-fields.js";
1
2
  /** System-field roles EDK can reference when those tagged fields exist on an element. */
2
3
  export const ELEMENT_AUDIT_SYSTEM_FIELD_KEYS = [
3
4
  "createdAt",
@@ -10,3 +11,25 @@ export const ELEMENT_SYSTEM_FIELD_KEYS = [
10
11
  "title",
11
12
  ...ELEMENT_AUDIT_SYSTEM_FIELD_KEYS,
12
13
  ];
14
+ /**
15
+ * What pull extracts for a reference to an element's platform audit field
16
+ * when the element also authors its own field under that key (#2513). Print
17
+ * writes it as the bare key, and the own field's references stay the key and
18
+ * print as its field ref. It only exists between extract and print: a colon
19
+ * can't appear in a field key or a platform id.
20
+ */
21
+ export function platformAuditFieldToken(key) {
22
+ return `platformAuditField:${key}`;
23
+ }
24
+ /** The system-field key a {@link platformAuditFieldToken} names; undefined for anything else.
25
+ * Element audit keys and app system keys (status/closedAt/…) both use this token when
26
+ * a same-keyed custom field would otherwise retarget the platform reference (#1871 / #2513).
27
+ */
28
+ export function platformAuditFieldTokenKey(value) {
29
+ const key = value.startsWith("platformAuditField:") ? value.slice("platformAuditField:".length) : undefined;
30
+ if (key === undefined)
31
+ return undefined;
32
+ if (ELEMENT_AUDIT_SYSTEM_FIELD_KEYS.includes(key))
33
+ return key;
34
+ return SYSTEM_FIELD_KEYS.includes(key) ? key : undefined;
35
+ }
@@ -301,6 +301,7 @@ export function elementToTf(entity, ctx) {
301
301
  objectId,
302
302
  orgRefs: ctx.orgRefs,
303
303
  diagnostics: ctx.diagnostics,
304
+ ...(ctx.optionIds !== undefined && { optionIds: ctx.optionIds }),
304
305
  fieldAddresses,
305
306
  // An element has no service accounts of its own. Hand the sibling app's
306
307
  // authored bag to the shared resolver so a managed account becomes a
@@ -41,6 +41,15 @@ export interface BaseFieldOptions {
41
41
  readonly defaultValue?: unknown;
42
42
  /** Platform semantic role tags for system fields (e.g. `["STATUS"]`). */
43
43
  readonly tags?: readonly string[];
44
+ /**
45
+ * Natural bag key when this field was re-keyed off a reserved system key
46
+ * (#1871). When renaming an already-applied field (for example `closedAt`
47
+ * → `closedAtCustom`), set this to the original key
48
+ * (`resourceKey: "closedAt"`) so the terraform address stays put. A
49
+ * brand-new field needs no `resourceKey`. Omitting it addresses the field
50
+ * from the bag key. Pull stamps this automatically when it re-keys.
51
+ */
52
+ readonly resourceKey?: string;
44
53
  }
45
54
  type ScalarExpression<T extends ExprType> = Expr<T, "single", ExprContext>;
46
55
  type WithExpression<T extends ExprType> = {
@@ -8,6 +8,23 @@
8
8
  import { type FromTfContext } from "../../engine/pull/context.js";
9
9
  import { type FieldOwnerScope, type TfBlock, type TfValue } from "../../engine/tf/index.js";
10
10
  import type { NormalizedField } from "../app/deserializer/normalize.js";
11
+ /**
12
+ * Bag key for a pulled custom field resource. A key that matches a reserved
13
+ * system-field key is rewritten to `<key>Custom` so pulled source builds
14
+ * under #1871 Q1 A (reject at build). When `usedKeys` is provided, the
15
+ * assignment is collision-safe: non-reserved naturals claim first, and a
16
+ * reserved key takes `<key>Custom`, then `<key>Custom2`, … with a warning.
17
+ * Terraform addresses stay on the natural key via the stamped `resourceKey` / {@link tfChildPartForFieldBagKey}.
18
+ */
19
+ export declare function bagKeyForPulledCustomField(ownerTfLabel: string, fieldLabel: string, scope: FieldOwnerScope, reservedKeys: ReadonlySet<string>, usedKeys?: Set<string>, diagnostics?: string[]): string;
20
+ /**
21
+ * Terraform child-part for a field bag key. Only a key that pull actually
22
+ * re-keyed from a reserved natural — carried as `resourceKey` / wire
23
+ * `__resourceKey` — keeps the address on that natural. A field whose own
24
+ * label is already `closed_at_custom` (bag key `closedAtCustom`) is not
25
+ * remapped, so Custom and Custom2 never share an address (Review B1).
26
+ */
27
+ export declare function tfChildPartForFieldBagKey(bagKey: string, resourceKey?: string): string;
11
28
  /** Whether one of the owner's own field resources is keyed `fieldKey`; true when the stream can't be listed. */
12
29
  export declare function ownerMayHaveCustomField(owner: FieldOwner, fieldKey: string, ctx: FromTfContext): boolean;
13
30
  export interface FieldOwner {
@@ -47,7 +64,7 @@ export declare function fieldBlockOwner(objectId: TfValue | undefined, owner: Pi
47
64
  * diagnostic (unknown resource type, or a field belonging to a different
48
65
  * owner — a recursive stream carries discovered aspects' fields too).
49
66
  */
50
- export declare function fieldFromTf(block: TfBlock, owner: FieldOwner, ctx: FromTfContext): NormalizedField | undefined;
67
+ export declare function fieldFromTf(block: TfBlock, owner: FieldOwner, ctx: FromTfContext, usedKeys?: Set<string>): NormalizedField | undefined;
51
68
  /**
52
69
  * `<resourceType>.<tfLabel>.id` → the field's bag key — shared by anything
53
70
  * that names a field by ref rather than owning it directly (agent-tool
@@ -61,6 +78,17 @@ export declare function fieldKeyFromTfRef(v: TfValue, ownerTfLabel?: string, sco
61
78
  * one app or element from becoming a same-owner field token accidentally.
62
79
  */
63
80
  export declare function fieldKeyFromTfValue(v: TfValue, ownerRefName: string, ownerTfLabel: string, scope: FieldOwnerScope, ctx: FromTfContext): string | undefined;
81
+ /**
82
+ * The key a view, layout or form entry prints from, when the owner also
83
+ * authors its own field under a system key (#2513 elements, #1871 apps). Pull
84
+ * resolves the platform system field and the own field to the same key, so
85
+ * this keeps them apart per reference: the platform field's local or data
86
+ * source becomes {@link platformAuditFieldToken} (printed as the bare key),
87
+ * the own field's resource stays the key (printed as its field ref), and a
88
+ * raw platform id stays raw, since pull can't tell whose it is (as
89
+ * `selfSystemField` does). Any other reference returns `key` unchanged.
90
+ */
91
+ export declare function elementAuditFieldReference(v: TfValue, key: string, owner: FieldOwner, ctx: FromTfContext): string;
64
92
  export interface DecodedForeignDataField {
65
93
  readonly key: string;
66
94
  readonly name: string;
@@ -10,15 +10,74 @@ import { DEPLOY_DEFAULT_SYSTEM_FIELD_NAMES, SYSTEM_FIELD_KEY_SET } from "../../c
10
10
  import { dataRefLabel, groupCatalogKey, optionalString, reportAuthoringBlocker, resourceRefLabel } from "../../engine/pull/context.js";
11
11
  import { bareChildResourceLabel, isTfExpr, sanitizeName, scopedLabelToBagKey, tfLabelToTsLabel, tsLabelToTfLabel } from "../../engine/tf/index.js";
12
12
  import { filterFromTf as deserializeFilterFromTf } from "../shared/filters/tf-codec.js";
13
+ import { optionLookupDecode } from "../shared/filters/option-lookups.js";
13
14
  import { __pulledExpression } from "../app/deserializer/pulled-expression.js";
14
15
  import { isAttachmentMediaType } from "./media-types.js";
15
16
  import { optionsFromTf } from "./options.js";
16
17
  import { isCalculationResultType } from "./calculation-types.js";
17
18
  import { KNOWN_FIELD_ATTRS, SUB_TYPE_BY_RESOURCE } from "./types.js";
18
- import { ELEMENT_AUDIT_SYSTEM_FIELD_KEYS, ELEMENT_SYSTEM_FIELD_KEYS } from "../element/system-fields.js";
19
+ import { ELEMENT_AUDIT_SYSTEM_FIELD_KEYS, ELEMENT_SYSTEM_FIELD_KEYS, platformAuditFieldToken } from "../element/system-fields.js";
19
20
  import { resolveDataAspectTarget, resolveDataTaskTarget } from "../shared/aspect-target-from-tf.js";
20
21
  const ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET = new Set(ELEMENT_AUDIT_SYSTEM_FIELD_KEYS);
21
22
  const ELEMENT_SYSTEM_FIELD_KEY_SET = new Set(ELEMENT_SYSTEM_FIELD_KEYS);
23
+ /**
24
+ * Bag key for a pulled custom field resource. A key that matches a reserved
25
+ * system-field key is rewritten to `<key>Custom` so pulled source builds
26
+ * under #1871 Q1 A (reject at build). When `usedKeys` is provided, the
27
+ * assignment is collision-safe: non-reserved naturals claim first, and a
28
+ * reserved key takes `<key>Custom`, then `<key>Custom2`, … with a warning.
29
+ * Terraform addresses stay on the natural key via the stamped `resourceKey` / {@link tfChildPartForFieldBagKey}.
30
+ */
31
+ export function bagKeyForPulledCustomField(ownerTfLabel, fieldLabel, scope, reservedKeys, usedKeys, diagnostics) {
32
+ const natural = scopedLabelToBagKey(ownerTfLabel, fieldLabel, scope);
33
+ if (!reservedKeys.has(natural)) {
34
+ if (usedKeys === undefined || !usedKeys.has(natural)) {
35
+ usedKeys?.add(natural);
36
+ return natural;
37
+ }
38
+ // Natural key already claimed (should not happen when non-reserved
39
+ // fields are assigned before reserved re-keys). Suffix until free.
40
+ let i = 2;
41
+ let candidate = `${natural}${i}`;
42
+ while (usedKeys.has(candidate)) {
43
+ i += 1;
44
+ candidate = `${natural}${i}`;
45
+ if (i > 1000) {
46
+ throw new EntityTfError(`no free bag key for custom field "${fieldLabel}" under owner "${ownerTfLabel}"`);
47
+ }
48
+ }
49
+ diagnostics?.push(`custom field "${fieldLabel}" keyed "${candidate}" because "${natural}" was already used`);
50
+ usedKeys.add(candidate);
51
+ return candidate;
52
+ }
53
+ let candidate = `${natural}Custom`;
54
+ let i = 2;
55
+ while (usedKeys?.has(candidate) === true) {
56
+ candidate = `${natural}Custom${i}`;
57
+ i += 1;
58
+ if (i > 1000) {
59
+ throw new EntityTfError(`no free bag key for system-keyed custom field "${natural}" under owner "${ownerTfLabel}"`);
60
+ }
61
+ }
62
+ if (candidate !== `${natural}Custom`) {
63
+ diagnostics?.push(`custom field with reserved key "${natural}" re-keyed to "${candidate}" because "${natural}Custom" was already used`);
64
+ }
65
+ usedKeys?.add(candidate);
66
+ return candidate;
67
+ }
68
+ /**
69
+ * Terraform child-part for a field bag key. Only a key that pull actually
70
+ * re-keyed from a reserved natural — carried as `resourceKey` / wire
71
+ * `__resourceKey` — keeps the address on that natural. A field whose own
72
+ * label is already `closed_at_custom` (bag key `closedAtCustom`) is not
73
+ * remapped, so Custom and Custom2 never share an address (Review B1).
74
+ */
75
+ export function tfChildPartForFieldBagKey(bagKey, resourceKey) {
76
+ if (resourceKey !== undefined && resourceKey !== "") {
77
+ return tsLabelToTfLabel(resourceKey);
78
+ }
79
+ return tsLabelToTfLabel(bagKey);
80
+ }
22
81
  /** A system field's authoring type, as the owner's own `fields.<key>` token carries it. */
23
82
  const SYSTEM_FIELD_TYPE_BY_KEY = {
24
83
  id: "HANDLE",
@@ -195,8 +254,9 @@ function selfSystemField(v, owner, ctx) {
195
254
  const systemKeys = scope === "element" ? ELEMENT_SYSTEM_FIELD_KEY_SET : SYSTEM_FIELD_KEY_SET;
196
255
  if (!systemKeys.has(fieldKey))
197
256
  return undefined;
198
- // An element's custom field may own an audit key; build then resolves that
199
- // key to the custom field, which would retarget this reference.
257
+ // Custom fields that would collide are re-keyed to `<key>Custom` (#1871), so
258
+ // a system-field local always names the platform field. Element audit still
259
+ // uses {@link elementAuditFieldReference} when a same-keyed custom remains.
200
260
  if (scope === "element" && ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET.has(fieldKey) && ownerMayHaveCustomField(owner, fieldKey, ctx))
201
261
  return undefined;
202
262
  // Title and ID carry their display names on the owner's own block; any
@@ -447,6 +507,8 @@ function dynamicConfigFromTf(value, owner, ctx, site) {
447
507
  reference: () => {
448
508
  throw new EntityTfError("dynamic picklist filter does not support value references");
449
509
  },
510
+ // #2582: a stored option lookup prints as filter.textOption(label).
511
+ ...optionLookupDecode,
450
512
  });
451
513
  }
452
514
  catch (e) {
@@ -665,7 +727,7 @@ function dataLookupKey(type, attrs) {
665
727
  * diagnostic (unknown resource type, or a field belonging to a different
666
728
  * owner — a recursive stream carries discovered aspects' fields too).
667
729
  */
668
- export function fieldFromTf(block, owner, ctx) {
730
+ export function fieldFromTf(block, owner, ctx, usedKeys) {
669
731
  const subType = SUB_TYPE_BY_RESOURCE.get(block.type ?? "");
670
732
  const fieldLabel = block.label;
671
733
  if (fieldLabel === undefined || fieldLabel === "") {
@@ -872,8 +934,11 @@ export function fieldFromTf(block, owner, ctx) {
872
934
  ctx.diagnostics.push(`${where}: unsupported include_type ${JSON.stringify(includeType)}; defaulted to AUTO`);
873
935
  }
874
936
  }
937
+ const reservedKeys = scope === "element" ? ELEMENT_SYSTEM_FIELD_KEY_SET : SYSTEM_FIELD_KEY_SET;
938
+ const natural = scopedLabelToBagKey(owner.label, fieldLabel, scope);
939
+ const key = bagKeyForPulledCustomField(owner.label, fieldLabel, scope, reservedKeys, usedKeys, ctx.diagnostics);
875
940
  return {
876
- key: scopedLabelToBagKey(owner.label, fieldLabel, scope),
941
+ key,
877
942
  ...(parentLinkage !== undefined && { parentLinkage }),
878
943
  ...(isCalculationResultType(calculationType) && { calculationType }),
879
944
  ...(calculationInclude !== undefined && { calculationInclude }),
@@ -881,6 +946,9 @@ export function fieldFromTf(block, owner, ctx) {
881
946
  id: ctx.importId(`${block.type}.${fieldLabel}`) ?? "",
882
947
  name: fieldName,
883
948
  type: subType.authoringType,
949
+ // Explicit re-key map for #1871: only reserved naturals that pull
950
+ // re-keyed carry resourceKey so build keeps the live TF address.
951
+ ...(reservedKeys.has(natural) ? { resourceKey: natural } : {}),
884
952
  ...(calculation !== undefined && { expression: __pulledExpression(calculation) }),
885
953
  // Only true flags are carried into authoring — false is the platform
886
954
  // default and stays unwritten (build re-emits explicit false; ei's
@@ -1014,7 +1082,10 @@ export function fieldKeyFromTfRef(v, ownerTfLabel, scope = "app") {
1014
1082
  if (!SUB_TYPE_BY_RESOURCE.has(v.type))
1015
1083
  return undefined;
1016
1084
  const bare = ownerTfLabel !== undefined ? bareChildResourceLabel(ownerTfLabel, v.label, scope) : v.label;
1017
- return ownerTfLabel !== undefined ? scopedLabelToBagKey(ownerTfLabel, v.label, scope) : tfLabelToTsLabel(bare);
1085
+ if (ownerTfLabel === undefined)
1086
+ return tfLabelToTsLabel(bare);
1087
+ const reserved = scope === "element" ? ELEMENT_SYSTEM_FIELD_KEY_SET : SYSTEM_FIELD_KEY_SET;
1088
+ return bagKeyForPulledCustomField(ownerTfLabel, v.label, scope, reserved);
1018
1089
  }
1019
1090
  /**
1020
1091
  * Resolve either a normal Terraform field reference or a raw platform field
@@ -1040,6 +1111,28 @@ export function fieldKeyFromTfValue(v, ownerRefName, ownerTfLabel, scope, ctx) {
1040
1111
  }
1041
1112
  return fieldKeyFromTfRef(v, ownerTfLabel, scope);
1042
1113
  }
1114
+ /**
1115
+ * The key a view, layout or form entry prints from, when the owner also
1116
+ * authors its own field under a system key (#2513 elements, #1871 apps). Pull
1117
+ * resolves the platform system field and the own field to the same key, so
1118
+ * this keeps them apart per reference: the platform field's local or data
1119
+ * source becomes {@link platformAuditFieldToken} (printed as the bare key),
1120
+ * the own field's resource stays the key (printed as its field ref), and a
1121
+ * raw platform id stays raw, since pull can't tell whose it is (as
1122
+ * `selfSystemField` does). Any other reference returns `key` unchanged.
1123
+ */
1124
+ export function elementAuditFieldReference(v, key, owner, ctx) {
1125
+ // Apps re-key colliding custom fields (#1871); only element audit still
1126
+ // shares a bag key with a possible same-keyed custom until that path re-keys.
1127
+ const scope = owner.scope ?? "app";
1128
+ if (scope !== "element" || !ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET.has(key))
1129
+ return key;
1130
+ if (ctx.resourcesOfType === undefined || !ownerMayHaveCustomField(owner, key, ctx))
1131
+ return key;
1132
+ if (typeof v === "string")
1133
+ return v;
1134
+ return isTfExpr(v) && (v.$expr === "local" || v.$expr === "data-attr") ? platformAuditFieldToken(key) : key;
1135
+ }
1043
1136
  /**
1044
1137
  * Decode `data.elementum_field.<label>` to the target's public field key.
1045
1138
  * ei labels are `elementum_element_<sanitize(displayName)>_<field>` (or
@@ -20,6 +20,10 @@ export const fieldModule = {
20
20
  resourceType: t.resourceType,
21
21
  implemented: t.implemented === true,
22
22
  authoringType: t.authoringType,
23
+ // The provider's Read drops an inactive option that isn't in prior state
24
+ // (`keepPicklistAPIOption`), so adoption re-imports the field so state
25
+ // holds the inactive options a re-pull prints (#2186).
26
+ ...(t.hasOptions === true && { stateScopedInactiveOptions: ["options"] }),
23
27
  })),
24
28
  },
25
29
  // Foreign relationship/relation fields are looked up by name under their
@@ -45,6 +45,8 @@ interface FieldTokenWire {
45
45
  readonly parentKind?: "app" | "element" | "task";
46
46
  readonly parentName?: string;
47
47
  readonly fieldType?: string;
48
+ /** Natural bag key when pull re-keyed off a reserved system key (#1871). */
49
+ readonly resourceKey?: string;
48
50
  }
49
51
  /**
50
52
  * One entry of `sort.orders` in the shape actually observed live on a
@@ -95,6 +97,8 @@ interface FieldConfigWire {
95
97
  export interface FieldInnerWire {
96
98
  readonly name?: string;
97
99
  readonly __refName?: string;
100
+ /** Natural bag key when pull re-keyed off a reserved system key (#1871). */
101
+ readonly __resourceKey?: string;
98
102
  readonly columnName?: string;
99
103
  readonly required?: boolean;
100
104
  readonly requiredOnClose?: boolean;
@@ -168,7 +172,7 @@ export interface FieldToTfResult {
168
172
  */
169
173
  export declare function claimFieldDisplayName(seen: Map<string, string>, field: FieldToTfResult): void;
170
174
  export declare function fieldToTf(typeKey: string, inner: FieldInnerWire, opts: FieldToTfOptions): FieldToTfResult | undefined;
171
- export declare function fieldRefToTfValue(fieldType: string, refName: string, ownerTfLabel: string, scope?: FieldOwnerScope, fieldOwnership?: FieldOwnership): TfExpr | undefined;
175
+ export declare function fieldRefToTfValue(fieldType: string, refName: string, ownerTfLabel: string, scope?: FieldOwnerScope, fieldOwnership?: FieldOwnership, resourceKey?: string): TfExpr | undefined;
172
176
  /**
173
177
  * Why build leaves out an entry: its field reference has no field in source
174
178
  * and no platform id (#1966, #1978). The gaps below state what apply then
@@ -20,10 +20,12 @@ import { expr, isCanonicalTsLabel, sanitizeName, scopedResourceLabel, snakeSlug,
20
20
  import { SYSTEM_FIELD_KEY_SET, systemFieldTag } from "../../core/system-fields.js";
21
21
  import { ELEMENT_AUDIT_SYSTEM_FIELD_KEYS, ELEMENT_SYSTEM_FIELD_KEYS } from "../element/system-fields.js";
22
22
  import { filterToTf as filterWireToTfCodec } from "../shared/filters/tf-codec.js";
23
+ import { optionSelectionLiteral, optionsLocalName } from "../shared/filters/option-lookups.js";
23
24
  import { foreignFieldDataLabel } from "../shared/foreign-ref.js";
24
25
  import { isAttachmentMediaType } from "./media-types.js";
25
26
  import { picklistLabel } from "./options.js";
26
27
  import { SUB_TYPE_BY_AUTHORING_TYPE, SUB_TYPE_BY_TYPE_KEY } from "./types.js";
28
+ import { tfChildPartForFieldBagKey } from "./from-tf.js";
27
29
  /** Reconstruct custom-field ownership from a loaded app/element payload. */
28
30
  export function authoredFieldKeysFromPayload(payload) {
29
31
  if (typeof payload !== "object" || payload === null)
@@ -210,7 +212,7 @@ function resolveFieldOnObject(token, object, opts, where, attrName = "relatedLab
210
212
  opts.diagnostics.push(`${where}: ${attrName} token "${refName}" resolves to table "${object.target.tfLabel}" — tables expose columns, not fields; dropped`);
211
213
  return undefined;
212
214
  }
213
- const value = fieldRefToTfValue(token.fieldType ?? "", refName, object.target.tfLabel, object.target.kind, object.fieldOwnership);
215
+ const value = fieldRefToTfValue(token.fieldType ?? "", refName, object.target.tfLabel, object.target.kind, object.fieldOwnership, token.resourceKey);
214
216
  if (value === undefined) {
215
217
  opts.diagnostics.push(`${where}: ${attrName} token "${refName}" has no terraform field resource mapping (fieldType ${JSON.stringify(token.fieldType ?? "")}); dropped`);
216
218
  }
@@ -389,6 +391,19 @@ function buildDynamicPicklistConfigAttr(cfg, opts, where, typeKey, field) {
389
391
  reference: () => {
390
392
  throw new EntityTfError(`${where}: dynamic picklist filter does not support value references`);
391
393
  },
394
+ // #2582: a Dropdown option is selected with filter.textOption(label).
395
+ literal: optionSelectionLiteral({
396
+ where: `${where}: config.dynamic.filter`,
397
+ address: where,
398
+ name: field.name,
399
+ ownerKind: "field",
400
+ optionsLocal: (token) => {
401
+ const target = resolvedObject.target;
402
+ return target === undefined || (target.kind !== "app" && target.kind !== "element")
403
+ ? undefined
404
+ : optionsLocalName(target.tfLabel, target.kind, token);
405
+ },
406
+ }),
392
407
  });
393
408
  let filterValue;
394
409
  try {
@@ -534,12 +549,14 @@ export function fieldToTf(typeKey, inner, opts) {
534
549
  }
535
550
  const bagKey = inner.__refName ?? snakeSlug(name);
536
551
  // The bag key IS the field's terraform label (via the bijection — the
537
- // stable naming contract, Sean 2026-07-22). Non-canonical keys have no
552
+ // stable naming contract, Sean 2026-07-22), except a #1871 re-key that
553
+ // carries `__resourceKey` keeps the natural system-key address so pull
554
+ // then plan does not destroy the live field. Non-canonical keys have no
538
555
  // well-defined address: hard error, not a degradation.
539
556
  if (!isCanonicalTsLabel(bagKey)) {
540
557
  throw new EntityTfError(`field bag key "${bagKey}" is not a canonical tsLabel (see src/engine/tf/labels.ts) — rename the key in fields: {}`);
541
558
  }
542
- const tfLabel = scopedResourceLabel(ownerTfLabel, tsLabelToTfLabel(bagKey), scope);
559
+ const tfLabel = scopedResourceLabel(ownerTfLabel, tfChildPartForFieldBagKey(bagKey, inner.__resourceKey), scope);
543
560
  const attrs = {
544
561
  object_id: objectId,
545
562
  name,
@@ -585,7 +602,7 @@ export function fieldToTf(typeKey, inner, opts) {
585
602
  if (typeof include.picklist === "object" && include.picklist !== null) {
586
603
  const field = include.picklist.picklistFieldId;
587
604
  if (field?.refName !== undefined) {
588
- const value = fieldRefToTfValue(field.fieldType ?? "PICKLIST", field.refName, ownerTfLabel, scope, opts.fieldOwnership);
605
+ const value = fieldRefToTfValue(field.fieldType ?? "PICKLIST", field.refName, ownerTfLabel, scope, opts.fieldOwnership, field.resourceKey);
589
606
  if (value !== undefined) {
590
607
  attrs["include_type"] = "PICKLIST";
591
608
  attrs["picklist_field_id"] = value;
@@ -725,7 +742,7 @@ export function fieldToTf(typeKey, inner, opts) {
725
742
  diagnostics.push(`field "${name}": parent value reference must target a Dropdown or Multi-Select field; dropped`);
726
743
  continue;
727
744
  }
728
- const parentTfLabel = scopedResourceLabel(opts.ownerTfLabel, tsLabelToTfLabel(ref.field.refName), opts.scope ?? "app");
745
+ const parentTfLabel = scopedResourceLabel(opts.ownerTfLabel, tfChildPartForFieldBagKey(ref.field.refName ?? "", ref.field.resourceKey), opts.scope ?? "app");
729
746
  parentValueIds.push(expr.local(`${parentTfLabel}_options_by_label`, [{ index: ref.label }]));
730
747
  }
731
748
  if (parentValueIds.length > 0)
@@ -819,7 +836,7 @@ function allowListToTf(typeKey, inner, opts, attrs, name) {
819
836
  if (token.parent !== undefined && token.parent !== opts.appRefName) {
820
837
  throw new EntityTfError(`${where}: parentGroupField must belong to the same app or element, not ${JSON.stringify(token.parent)}`);
821
838
  }
822
- const value = fieldRefToTfValue(token.fieldType, token.refName ?? "", opts.ownerTfLabel, opts.scope ?? "app", opts.fieldOwnership);
839
+ const value = fieldRefToTfValue(token.fieldType, token.refName ?? "", opts.ownerTfLabel, opts.scope ?? "app", opts.fieldOwnership, token.resourceKey);
823
840
  if (value === undefined)
824
841
  throw new EntityTfError(`${where}: parentGroupField ${JSON.stringify(token.refName ?? "")} does not resolve to a group field resource`);
825
842
  attrs["parent_group_field_id"] = value;
@@ -865,13 +882,13 @@ function scalarOrUndefined(v) {
865
882
  /**
866
883
  * Element system fields resolve through their `element/locals.ts` locals.
867
884
  * Elements have no status/stage fields; the audit fields exist only on some
868
- * elements, which the build checks against `systemFields.audit`. An element
869
- * may author its own field under an audit key (`createdAt: dateTimeField(…)`),
870
- * which shadows the audit field, so a key in `fieldAddresses` is that field.
885
+ * elements, which the build checks against `systemFields.audit`. Custom fields
886
+ * may not use reserved system keys (#1871); pull re-keys a collision to
887
+ * `<key>Custom`. A key in `fieldAddresses` is therefore always a custom field.
871
888
  */
872
889
  const ELEMENT_SYSTEM_FIELD_KEY_SET = new Set(ELEMENT_SYSTEM_FIELD_KEYS);
873
890
  const ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET = new Set(ELEMENT_AUDIT_SYSTEM_FIELD_KEYS);
874
- export function fieldRefToTfValue(fieldType, refName, ownerTfLabel, scope = "app", fieldOwnership) {
891
+ export function fieldRefToTfValue(fieldType, refName, ownerTfLabel, scope = "app", fieldOwnership, resourceKey) {
875
892
  if (!isCanonicalTsLabel(refName))
876
893
  return undefined;
877
894
  if (scope === "element" &&
@@ -879,9 +896,13 @@ export function fieldRefToTfValue(fieldType, refName, ownerTfLabel, scope = "app
879
896
  fieldOwnership === undefined) {
880
897
  throw new EntityTfError(`element field "${refName}" requires authored-field ownership metadata to distinguish a custom field from the platform audit field`);
881
898
  }
899
+ // Reserved system keys are never custom after #1871 reject/re-key. The
900
+ // fieldOwnership escape remains for leftover source that still keys a
901
+ // custom field as a system key (build rejects new source) and for element
902
+ // audit fields until that path is fully re-keyed.
882
903
  const isSystemField = scope === "element"
883
904
  ? ELEMENT_SYSTEM_FIELD_KEY_SET.has(refName) && !(ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET.has(refName) && fieldOwnership?.has(refName) === true)
884
- : SYSTEM_FIELD_KEY_SET.has(refName);
905
+ : SYSTEM_FIELD_KEY_SET.has(refName) && !(fieldOwnership?.has(refName) === true);
885
906
  if (isSystemField) {
886
907
  return expr.local(`${ownerTfLabel}_${tsLabelToTfLabel(refName)}_field_id`);
887
908
  }
@@ -895,7 +916,7 @@ export function fieldRefToTfValue(fieldType, refName, ownerTfLabel, scope = "app
895
916
  const subType = SUB_TYPE_BY_AUTHORING_TYPE.get(fieldType);
896
917
  if (subType === undefined)
897
918
  return undefined;
898
- return expr.resourceAttr(subType.resourceType, scopedResourceLabel(ownerTfLabel, tsLabelToTfLabel(refName), scope), ["id"]);
919
+ return expr.resourceAttr(subType.resourceType, scopedResourceLabel(ownerTfLabel, tfChildPartForFieldBagKey(refName, resourceKey), scope), ["id"]);
899
920
  }
900
921
  /**
901
922
  * Why build leaves out an entry: its field reference has no field in source
@@ -13,6 +13,7 @@ export const categoryModule = {
13
13
  // Apps reference categories (`category_id`), so build emits this data
14
14
  // source and the build-vs-export must scope it.
15
15
  referencedByBuild: true,
16
+ managedInOrganization: true,
16
17
  },
17
18
  authoring: { unit: "inline", bag: "categories" },
18
19
  parents: [{ parent: "org", child: "category" }],