@elementumai/edk 0.9.20 → 0.10.2

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 (159) hide show
  1. package/README.md +19 -7
  2. package/dist-sdk/src/catalog/factories.d.ts +10 -2
  3. package/dist-sdk/src/catalog/factories.js +10 -1
  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/token.js +4 -0
  7. package/dist-sdk/src/catalog/types.d.ts +7 -1
  8. package/dist-sdk/src/catalog/types.js +1 -0
  9. package/dist-sdk/src/core/triggers/on-demand.d.ts +2 -2
  10. package/dist-sdk/src/core/triggers/on-demand.js +7 -0
  11. package/dist-sdk/src/core/types/app.d.ts +2 -2
  12. package/dist-sdk/src/engine/coverage-gaps.d.ts +86 -0
  13. package/dist-sdk/src/engine/coverage-gaps.js +156 -0
  14. package/dist-sdk/src/engine/ei/run.d.ts +21 -1
  15. package/dist-sdk/src/engine/ei/run.js +89 -2
  16. package/dist-sdk/src/engine/pull/catalog-ref.d.ts +16 -0
  17. package/dist-sdk/src/engine/pull/catalog-ref.js +24 -0
  18. package/dist-sdk/src/engine/pull/context.d.ts +68 -6
  19. package/dist-sdk/src/engine/pull/context.js +40 -1
  20. package/dist-sdk/src/engine/pull/procedure-refs.d.ts +8 -0
  21. package/dist-sdk/src/engine/pull/procedure-refs.js +28 -0
  22. package/dist-sdk/src/engine/pull/result-output.js +27 -1
  23. package/dist-sdk/src/engine/tf/index.d.ts +1 -1
  24. package/dist-sdk/src/engine/tf/index.js +1 -1
  25. package/dist-sdk/src/engine/tf/labels.d.ts +22 -5
  26. package/dist-sdk/src/engine/tf/labels.js +45 -3
  27. package/dist-sdk/src/engine/tf/parse.js +3 -2
  28. package/dist-sdk/src/engine/validation.d.ts +5 -0
  29. package/dist-sdk/src/engine/validation.js +12 -2
  30. package/dist-sdk/src/entities/access-policy/from-tf.js +38 -1
  31. package/dist-sdk/src/entities/access-policy/principal.d.ts +2 -0
  32. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.d.ts +3 -0
  33. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +42 -19
  34. package/dist-sdk/src/entities/agent/from-tf.js +11 -3
  35. package/dist-sdk/src/entities/agent/module.js +2 -0
  36. package/dist-sdk/src/entities/agent/to-tf.js +14 -3
  37. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +8 -1
  38. package/dist-sdk/src/entities/agent-tool/from-tf.js +11 -5
  39. package/dist-sdk/src/entities/agent-tool/module.js +3 -0
  40. package/dist-sdk/src/entities/app/auto-share.d.ts +38 -0
  41. package/dist-sdk/src/entities/app/auto-share.js +49 -0
  42. package/dist-sdk/src/entities/app/build.d.ts +17 -3
  43. package/dist-sdk/src/entities/app/build.js +56 -4
  44. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +6 -1
  45. package/dist-sdk/src/entities/app/from-tf.js +4 -0
  46. package/dist-sdk/src/entities/app/index.d.ts +2 -1
  47. package/dist-sdk/src/entities/app/index.js +1 -8
  48. package/dist-sdk/src/entities/app/module.js +2 -1
  49. package/dist-sdk/src/entities/app/to-tf.d.ts +6 -0
  50. package/dist-sdk/src/entities/app/to-tf.js +25 -2
  51. package/dist-sdk/src/entities/automation/build.d.ts +12 -2
  52. package/dist-sdk/src/entities/automation/build.js +105 -17
  53. package/dist-sdk/src/entities/automation/calculation-expression.d.ts +7 -7
  54. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +17 -4
  55. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +187 -27
  56. package/dist-sdk/src/entities/automation/from-tf.d.ts +9 -2
  57. package/dist-sdk/src/entities/automation/from-tf.js +397 -89
  58. package/dist-sdk/src/entities/automation/index.d.ts +1 -1
  59. package/dist-sdk/src/entities/automation/index.js +1 -1
  60. package/dist-sdk/src/entities/automation/module.d.ts +3 -0
  61. package/dist-sdk/src/entities/automation/module.js +70 -7
  62. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +21 -0
  63. package/dist-sdk/src/entities/automation/output-key-wire-name.js +38 -0
  64. package/dist-sdk/src/entities/automation/script-code.d.ts +5 -2
  65. package/dist-sdk/src/entities/automation/script-code.js +23 -5
  66. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +37 -0
  67. package/dist-sdk/src/entities/automation/task-output-labels.js +76 -0
  68. package/dist-sdk/src/entities/automation/tasks.d.ts +92 -20
  69. package/dist-sdk/src/entities/automation/tasks.js +48 -15
  70. package/dist-sdk/src/entities/automation/to-tf.d.ts +20 -3
  71. package/dist-sdk/src/entities/automation/to-tf.js +656 -65
  72. package/dist-sdk/src/entities/automation/triggers.d.ts +8 -4
  73. package/dist-sdk/src/entities/automation/types.d.ts +9 -0
  74. package/dist-sdk/src/entities/automation/types.js +19 -0
  75. package/dist-sdk/src/entities/automation/value-ref-tf.d.ts +1 -1
  76. package/dist-sdk/src/entities/automation/value-ref-tf.js +1 -1
  77. package/dist-sdk/src/entities/chart/from-tf.js +9 -1
  78. package/dist-sdk/src/entities/data-lookup/module.js +2 -0
  79. package/dist-sdk/src/entities/dynamic-layout/to-tf.d.ts +1 -0
  80. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +7 -5
  81. package/dist-sdk/src/entities/element/build.d.ts +3 -0
  82. package/dist-sdk/src/entities/element/build.js +1 -0
  83. package/dist-sdk/src/entities/element/from-tf.js +4 -0
  84. package/dist-sdk/src/entities/element/module.js +2 -1
  85. package/dist-sdk/src/entities/element/to-tf.d.ts +1 -1
  86. package/dist-sdk/src/entities/element/to-tf.js +12 -14
  87. package/dist-sdk/src/entities/field/build.d.ts +3 -2
  88. package/dist-sdk/src/entities/field/calc.d.ts +7 -7
  89. package/dist-sdk/src/entities/field/from-tf.d.ts +4 -4
  90. package/dist-sdk/src/entities/field/from-tf.js +255 -80
  91. package/dist-sdk/src/entities/field/module.d.ts +6 -0
  92. package/dist-sdk/src/entities/field/module.js +23 -2
  93. package/dist-sdk/src/entities/field/options.js +6 -1
  94. package/dist-sdk/src/entities/field/order.d.ts +16 -0
  95. package/dist-sdk/src/entities/field/order.js +41 -0
  96. package/dist-sdk/src/entities/field/to-tf.d.ts +4 -4
  97. package/dist-sdk/src/entities/field/to-tf.js +7 -1
  98. package/dist-sdk/src/entities/field/types.d.ts +7 -0
  99. package/dist-sdk/src/entities/field/types.js +32 -0
  100. package/dist-sdk/src/entities/role/from-tf.js +3 -3
  101. package/dist-sdk/src/entities/role/to-tf.d.ts +2 -0
  102. package/dist-sdk/src/entities/role/to-tf.js +12 -4
  103. package/dist-sdk/src/entities/service-account/from-tf.js +3 -3
  104. package/dist-sdk/src/entities/service-account/workspace-owner.d.ts +16 -0
  105. package/dist-sdk/src/entities/service-account/workspace-owner.js +22 -0
  106. package/dist-sdk/src/entities/shared/filters/ast.d.ts +15 -1
  107. package/dist-sdk/src/entities/shared/filters/ast.js +2 -0
  108. package/dist-sdk/src/entities/shared/filters/index.d.ts +5 -0
  109. package/dist-sdk/src/entities/shared/filters/index.js +5 -0
  110. package/dist-sdk/src/entities/shared/filters/print.js +7 -2
  111. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +5 -0
  112. package/dist-sdk/src/entities/shared/filters/tf-codec.js +42 -7
  113. package/dist-sdk/src/entities/shared/filters/types.d.ts +8 -3
  114. package/dist-sdk/src/entities/shared/ref.d.ts +5 -7
  115. package/dist-sdk/src/entities/shared/ref.js +5 -7
  116. package/dist-sdk/src/entities/shared/tool-field-ref.d.ts +9 -0
  117. package/dist-sdk/src/entities/shared/tool-field-ref.js +35 -2
  118. package/dist-sdk/src/entities/shared/types/field-types.d.ts +10 -0
  119. package/dist-sdk/src/entities/shared/types/field-types.js +4 -0
  120. package/dist-sdk/src/entities/skill/access-policy.js +3 -3
  121. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +3 -0
  122. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +12 -5
  123. package/dist-sdk/src/entities/skill/from-tf.js +4 -2
  124. package/dist-sdk/src/entities/skill/module.js +2 -0
  125. package/dist-sdk/src/entities/skill-tool/from-tf.js +41 -8
  126. package/dist-sdk/src/entities/skill-tool/module.js +3 -0
  127. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +0 -6
  128. package/dist-sdk/src/entities/skill-tool/to-tf.js +22 -9
  129. package/dist-sdk/src/entities/task/build.d.ts +8 -0
  130. package/dist-sdk/src/entities/task/build.js +10 -1
  131. package/dist-sdk/src/entities/task/from-tf.d.ts +4 -2
  132. package/dist-sdk/src/entities/task/from-tf.js +7 -2
  133. package/dist-sdk/src/entities/task/module.js +3 -2
  134. package/dist-sdk/src/entities/task/to-tf.d.ts +8 -4
  135. package/dist-sdk/src/entities/task/to-tf.js +32 -1
  136. package/dist-sdk/src/entities/view/from-tf.js +9 -1
  137. package/dist-sdk/src/entities/view/module.js +4 -1
  138. package/dist-sdk/src/entities/view/to-tf.d.ts +6 -7
  139. package/dist-sdk/src/entities/view/to-tf.js +30 -11
  140. package/dist-sdk/src/entities/view/types.d.ts +6 -0
  141. package/dist-sdk/src/entities/view/types.js +6 -0
  142. package/dist-sdk/src/entities/visual-flow/build.d.ts +16 -1
  143. package/dist-sdk/src/entities/visual-flow/build.js +9 -0
  144. package/dist-sdk/src/entities/visual-flow/from-tf.d.ts +14 -0
  145. package/dist-sdk/src/entities/visual-flow/from-tf.js +51 -3
  146. package/dist-sdk/src/entities/visual-flow/index.d.ts +1 -1
  147. package/dist-sdk/src/entities/visual-flow/index.js +1 -1
  148. package/dist-sdk/src/entities/visual-flow/printer.js +19 -0
  149. package/dist-sdk/src/entities/visual-flow/to-tf.js +87 -3
  150. package/dist-sdk/src/entities/widget/from-tf.js +3 -3
  151. package/dist-sdk/src/entities/widget/to-tf.d.ts +2 -0
  152. package/dist-sdk/src/entities/widget/to-tf.js +7 -6
  153. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +133 -9
  154. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  155. package/dist-sdk/src/graph/index.d.ts +4 -2
  156. package/dist-sdk/src/graph/index.js +3 -0
  157. package/dist-sdk/src/graph/modules.js +2 -1
  158. package/dist-sdk/src/graph/types.d.ts +41 -3
  159. package/package.json +7 -3
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { EntityTfError } from "../../engine/entity.js";
9
9
  import { SYSTEM_FIELD_KEY_SET } from "../../core/system-fields.js";
10
- import { dataRefLabel, optionalString, resourceRefLabel } from "../../engine/pull/context.js";
10
+ import { dataRefLabel, 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
13
  import { __pulledExpression } from "../app/deserializer/pulled-expression.js";
@@ -16,6 +16,7 @@ import { optionsFromTf } from "./options.js";
16
16
  import { isCalculationResultType } from "./calculation-types.js";
17
17
  import { KNOWN_FIELD_ATTRS, SUB_TYPE_BY_RESOURCE } from "./types.js";
18
18
  import { ELEMENT_AUDIT_SYSTEM_FIELD_KEYS } from "../element/system-fields.js";
19
+ import { resolveDataAspectTarget } from "../shared/aspect-target-from-tf.js";
19
20
  const ELEMENT_AUDIT_SYSTEM_FIELD_KEY_SET = new Set(ELEMENT_AUDIT_SYSTEM_FIELD_KEYS);
20
21
  /**
21
22
  * A related_object_id/related_label_field_id value → its raw platform id, if
@@ -35,52 +36,30 @@ function resolveRelatedId(v, ctx) {
35
36
  return undefined;
36
37
  }
37
38
  /**
38
- * `related_object_id` specifically (not `related_label_field_id`, which
39
- * names a field and has no equivalent name-based recovery) gets one more
40
- * fallback beyond `resolveRelatedId`: a `data.elementum_app.<label>` /
41
- * `data.elementum_element.<label>` foreign lookup carries no raw id, but the
42
- * data block's own `name = "..."` filter attribute (read via `ctx.dataName`)
43
- * IS the target's real display name - the same name `readAppNameMap`/
44
- * `readElementNameMap` index every already-pulled app/element by. When that
45
- * name matches one already in this workspace, resolve straight to a typed
46
- * `AppOrElementRef` instead of giving up - the reference then survives a
47
- * build exactly like a hand-authored `catalog.<ref>` would (see
48
- * agent-tool/from-tf.ts's `NormalizedToolTarget` for the same foreign-name
49
- * convention on tool targets). No match (the target isn't pulled into this
50
- * workspace yet, or `ctx` wasn't wired with the name maps) records the name
51
- * on `ctx.unresolvedForeignRefs` (when the caller wired it up) so
52
- * `--with-dependencies` can chase it down and pulls it as a workspace app/
53
- * element the same way it already does for agent-tool/automation targets —
54
- * then stays unresolvable this pull, same as before.
39
+ * `related_object_id` gets one more fallback beyond `resolveRelatedId`: a
40
+ * `data.elementum_app.<label>` / `data.elementum_element.<label>` foreign
41
+ * lookup carries no raw id, but its `namespace`/`name` filter attributes ARE
42
+ * the target's portable identity (see {@link dataAspectRef}).
55
43
  */
56
- function resolveRelatedObject(v, ctx) {
44
+ function resolveRelatedObject(v, ctx, namespaceFallback = false) {
57
45
  const raw = resolveRelatedId(v, ctx);
58
46
  if (raw !== undefined)
59
47
  return raw;
60
48
  if (v === undefined || !isTfExpr(v) || v.$expr !== "data-attr")
61
49
  return undefined;
62
- if (v.type === "elementum_app") {
63
- const attrs = ctx.dataAttrs?.("elementum_app", v.label);
64
- const namespace = typeof attrs?.namespace === "string" ? attrs.namespace : undefined;
65
- const name = ctx.dataName("elementum_app", v.label) ?? (typeof attrs?.name === "string" ? attrs.name : undefined);
66
- const refName = namespace !== undefined ? ctx.foreignAppRefNamespaces?.get(namespace) : name !== undefined ? ctx.foreignAppRefNames?.get(name) : undefined;
67
- if (refName !== undefined)
68
- return { __kind: "app", refName, name: name ?? refName, ...(namespace !== undefined ? { namespace } : {}) };
69
- if (name === undefined)
70
- return undefined;
71
- ctx.unresolvedForeignRefs?.apps.add(name);
72
- return undefined;
73
- }
74
- if (v.type === "elementum_element") {
75
- const attrs = ctx.dataAttrs?.("elementum_element", v.label);
76
- const namespace = typeof attrs?.namespace === "string" ? attrs.namespace : undefined;
77
- const name = ctx.dataName("elementum_element", v.label) ?? (typeof attrs?.name === "string" ? attrs.name : undefined);
78
- const refName = namespace !== undefined ? ctx.foreignElementRefNamespaces?.get(namespace) : name !== undefined ? ctx.foreignElementRefNames?.get(name) : undefined;
79
- if (refName !== undefined)
80
- return { __kind: "element", refName, name: name ?? refName, ...(namespace !== undefined ? { namespace } : {}) };
81
- if (name === undefined)
50
+ if (v.type === "elementum_app" || v.type === "elementum_element") {
51
+ const kind = v.type === "elementum_app" ? "app" : "element";
52
+ const resolved = dataAspectRef(kind, v.label, ctx);
53
+ if (resolved === undefined)
82
54
  return undefined;
83
- ctx.unresolvedForeignRefs?.elements.add(name);
55
+ // In the workspace, or a dynamic Dropdown (`namespaceFallback` is the
56
+ // related_aspect_id caller): author the portable ref. That is #1440's
57
+ // namespace-derived dynamic_config and #1569's unpulled source.
58
+ if (resolved.inWorkspace || namespaceFallback)
59
+ return resolved.ref;
60
+ // #1484: an unpulled relation target stays unresolved so the pull
61
+ // refuses it. dataAspectRef already recorded the name for
62
+ // --with-dependencies.
84
63
  return undefined;
85
64
  }
86
65
  if (v.type === "elementum_task") {
@@ -93,6 +72,102 @@ function resolveRelatedObject(v, ctx) {
93
72
  }
94
73
  return undefined;
95
74
  }
75
+ /**
76
+ * A `data.elementum_app|element.<label>` lookup → the aspect it names, the
77
+ * same identity views, widgets and relationships resolve: the workspace
78
+ * catalog when that app/element is pulled here, else a foreign ref build
79
+ * looks up again by the same namespace (or name) - the way automations
80
+ * author a foreign found record. A foreign one is also recorded on
81
+ * `ctx.unresolvedForeignRefs` so `pull app --with-dependencies` pulls it.
82
+ * Undefined when the export gave the lookup neither a namespace nor a name.
83
+ */
84
+ function dataAspectRef(kind, label, ctx) {
85
+ const type = kind === "app" ? "elementum_app" : "elementum_element";
86
+ const attrs = ctx.dataAttrs?.(type, label);
87
+ const namespace = typeof attrs?.namespace === "string" ? attrs.namespace : undefined;
88
+ const name = ctx.dataName(type, label) ?? (typeof attrs?.name === "string" ? attrs.name : undefined);
89
+ if (namespace === undefined && name === undefined)
90
+ return undefined;
91
+ const target = resolveDataAspectTarget(ctx, kind, label);
92
+ const byNamespace = kind === "app" ? ctx.foreignAppRefNamespaces : ctx.foreignElementRefNamespaces;
93
+ const byName = kind === "app" ? ctx.foreignAppRefNames : ctx.foreignElementRefNames;
94
+ const inWorkspace = (namespace !== undefined && byNamespace?.get(namespace) === target.key) ||
95
+ (name !== undefined && byName?.get(name) === target.key);
96
+ if (!inWorkspace && name !== undefined)
97
+ (kind === "app" ? ctx.unresolvedForeignRefs?.apps : ctx.unresolvedForeignRefs?.elements)?.add(name);
98
+ return {
99
+ ref: { __kind: kind, refName: target.key, name: target.name, ...(namespace !== undefined ? { namespace } : {}) },
100
+ inWorkspace,
101
+ };
102
+ }
103
+ /**
104
+ * The identity a self-sourced dynamic Dropdown authors for its own owner:
105
+ * the clone's `ctx.selfAspectRef`, else (pull, edk#1493) the owner itself,
106
+ * named from its own resource block. Pull used to resolve these refs through
107
+ * import ids and print raw `<appId>:<fieldId>` fallbacks, which drift on the
108
+ * next plan and count as coverage gaps.
109
+ */
110
+ function selfIdentity(owner, ctx) {
111
+ if (ctx.selfAspectRef !== undefined)
112
+ return ctx.selfAspectRef;
113
+ if (owner.refName === undefined)
114
+ return undefined;
115
+ const kind = owner.type === "elementum_app" ? "app" : owner.type === "elementum_element" ? "element" : owner.type === "elementum_task" ? "task" : undefined;
116
+ if (kind === undefined)
117
+ return undefined;
118
+ const attrs = ctx.resourceByAddress?.(`${owner.type}.${owner.label}`)?.attrs;
119
+ const name = typeof attrs?.["name"] === "string" ? attrs["name"] : undefined;
120
+ if (name === undefined)
121
+ return undefined;
122
+ const namespace = typeof attrs?.["namespace"] === "string" ? attrs["namespace"] : undefined;
123
+ return { __kind: kind, refName: owner.refName, name, ...(namespace !== undefined ? { namespace } : {}) };
124
+ }
125
+ /**
126
+ * A reference to the owner itself (`elementum_app.<owner>.id`) that no import
127
+ * id resolves, authored as `ctx.selfAspectRef` (clone, edk#1437).
128
+ */
129
+ function selfRelatedObject(v, owner, ctx) {
130
+ const self = selfIdentity(owner, ctx);
131
+ if (self === undefined || v === undefined || !isTfExpr(v) || v.$expr !== "resource-attr")
132
+ return undefined;
133
+ if (v.type !== owner.type || v.label !== owner.label || v.path.join(".") !== "id")
134
+ return undefined;
135
+ return { __kind: self.__kind, refName: self.refName, name: self.name, ...(self.namespace !== undefined ? { namespace: self.namespace } : {}) };
136
+ }
137
+ /** A field of the owner that no import id resolves, as `ctx.selfAspectRef`'s catalog field (edk#1437). */
138
+ function selfRelatedField(v, owner, ctx) {
139
+ const self = selfIdentity(owner, ctx);
140
+ if (self === undefined || v === undefined)
141
+ return undefined;
142
+ const fieldKey = fieldKeyFromTfRef(v, owner.label, owner.scope ?? "app");
143
+ if (fieldKey === undefined || !isTfExpr(v) || v.$expr !== "resource-attr")
144
+ return undefined;
145
+ // A typed fieldRef, not catalog.<self>.fields.<key>: the app file cannot
146
+ // import its own catalog entry.
147
+ const fieldType = SUB_TYPE_BY_RESOURCE.get(v.type)?.authoringType;
148
+ // The field's real display name, from its own resource block; without it
149
+ // the printer falls back to the bag key ("reason" for "Reason").
150
+ const fieldName = ctx.resourceByAddress?.(`${v.type}.${v.label}`)?.attrs?.["name"];
151
+ return {
152
+ ...(typeof fieldName === "string" ? { fieldName } : {}),
153
+ ownerRefName: self.refName,
154
+ ownerKind: self.__kind,
155
+ ownerName: self.name,
156
+ ...(self.namespace !== undefined ? { ownerNamespace: self.namespace } : {}),
157
+ fieldKey,
158
+ ...(fieldType !== undefined ? { fieldType } : {}),
159
+ };
160
+ }
161
+ /**
162
+ * `resolveRelatedField`, else the owner's own field as `ctx.selfAspectRef`'s
163
+ * (clone of a self-sourced Dropdown, edk#1481). Used wherever a dynamic
164
+ * config names a field of its source, which for a self-sourced Dropdown is
165
+ * the owner: the label field, filter fields, sort orders and parent mappings.
166
+ */
167
+ function resolveRelatedOrSelfField(v, owner, ctx) {
168
+ // Self first: in pull the owner's own fields also have import ids (edk#1493).
169
+ return selfRelatedField(v, owner, ctx) ?? resolveRelatedField(v, ctx);
170
+ }
96
171
  function resolveRelatedField(v, ctx) {
97
172
  const raw = resolveRelatedId(v, ctx);
98
173
  if (raw !== undefined)
@@ -122,6 +197,27 @@ function resolveRelatedField(v, ctx) {
122
197
  ...(fieldName !== undefined ? { fieldName } : {}),
123
198
  };
124
199
  }
200
+ const appLabel = dataRefLabel(objectId, "elementum_app");
201
+ const elementLabel = dataRefLabel(objectId, "elementum_element");
202
+ const ownerKind = appLabel !== undefined ? "app" : elementLabel !== undefined ? "element" : undefined;
203
+ const ownerLabel = appLabel ?? elementLabel;
204
+ const owner = ownerKind === undefined || ownerLabel === undefined ? undefined : dataAspectRef(ownerKind, ownerLabel, ctx);
205
+ if (ownerKind !== undefined && owner !== undefined && typeof attrs?.name === "string") {
206
+ const { refName, name, namespace } = owner.ref;
207
+ const field = decodeForeignDataField(v.label, { key: refName, name, ...(namespace !== undefined ? { namespace } : {}), kind: ownerKind }, ctx);
208
+ if (owner.inWorkspace)
209
+ return { ownerRefName: refName, fieldKey: field.key, fieldName: field.name };
210
+ return {
211
+ ownerRefName: refName,
212
+ ownerKind,
213
+ ownerName: name,
214
+ ...(namespace !== undefined ? { ownerNamespace: namespace } : {}),
215
+ fieldKey: field.key,
216
+ fieldName: field.name,
217
+ ...(field.fieldType !== undefined ? { fieldType: field.fieldType } : {}),
218
+ outsideWorkspace: true,
219
+ };
220
+ }
125
221
  }
126
222
  return undefined;
127
223
  }
@@ -131,8 +227,8 @@ function resolveRelatedField(v, ctx) {
131
227
  * ref-only dependency data address resolves to its generated catalog member;
132
228
  * a preserved raw id remains an explicit unresolved/unsafe fallback.
133
229
  */
134
- function dynamicPicklistFilterFieldRef(value, ctx) {
135
- const resolved = resolveRelatedField(value, ctx);
230
+ function dynamicPicklistFilterFieldRef(value, owner, ctx) {
231
+ const resolved = resolveRelatedOrSelfField(value, owner, ctx);
136
232
  if (typeof resolved === "string")
137
233
  return { kind: "unresolved", uuid: resolved, hint: "field" };
138
234
  if (resolved !== undefined) {
@@ -161,40 +257,110 @@ function dynamicPicklistRecordFieldRef(value, owner, ctx) {
161
257
  const fieldKey = fieldKeyFromTfRef(value, owner.label, scope) ?? fieldKeyFromTfValue(value, owner.refName, owner.label, scope, ctx);
162
258
  if (fieldKey === undefined)
163
259
  throw new EntityTfError("record_field filter value field is not a field on the owning record");
164
- return { kind: "field", appHandle: owner.refName, fieldKey };
260
+ // A clone authors the record's fields under the clone's refName (edk#1481).
261
+ return { kind: "field", appHandle: ctx.selfAspectRef?.refName ?? owner.refName, fieldKey };
165
262
  }
166
263
  /**
167
264
  * `dynamic_config` → `AppField.config` (picklist/multiPicklist only). Mirrors
168
265
  * `related_object_id`/`related_label_field_id` resolution exactly (both are
169
266
  * required by the wire schema, so either failing to resolve drops the whole
170
- * config — same convention as RELATION's own required pair). `parent_field_id`
171
- * is same-aspect (see `fieldKeyFromTfRef`'s use here vs. `resolveRelatedId`
172
- * for the cross-aspect id's above); its own failure to resolve degrades that
173
- * ONE optional attribute rather than the whole field.
267
+ * config — same convention as RELATION's own required pair).
268
+ *
269
+ * `filter`, `sort` and `parent_field_id` are different: the provider's update
270
+ * mutation cannot clear them, so removing one from an existing dynamic
271
+ * picklist (Dropdown or multi-select) forces replacement
272
+ * (picklist_dynamic_config.go). A part pull cannot reconstruct is an
273
+ * authoring blocker ({@link blockReplaceForcingDrop}): the part is left out of
274
+ * the extract, and a caller that would author this field refuses rather than
275
+ * print source whose first plan deletes and recreates it.
276
+ * `auto_relate` and `values_render_type` update in place, so those still
277
+ * degrade with a diagnostic. `parent_mappings` updates in place too, but the
278
+ * platform will not clear them ({@link blockUnclearableParentMappings}).
279
+ */
280
+ function blockReplaceForcingDrop(ctx, site, attr, reason) {
281
+ reportAuthoringBlocker(ctx, {
282
+ address: site.address,
283
+ description: `${site.where}: dynamic_config.${attr} ${reason}. Removing dynamic_config.${attr} forces replacement of this field ` +
284
+ `(the platform cannot clear it in place), so dropping it would make the next plan delete and recreate the field.`,
285
+ remedy: "Change the filter, sort or parent field of these fields' dynamic options in the Elementum UI to a form the EDK can represent, then pull again.",
286
+ });
287
+ }
288
+ /**
289
+ * Without its dynamic_config a Dropdown prints as `config: { static: { values: [] } }`,
290
+ * which plan rejects ("options or dynamic_config is required") and which would
291
+ * turn a live dynamic Dropdown static if "fixed" by hand. Every pull-style
292
+ * caller (pull, clone, post-apply refresh) must refuse or skip rather than
293
+ * write it (#1405, #1406).
294
+ */
295
+ function blockDynamicConfigDrop(ctx, site, reason, labelOnly = false) {
296
+ // When only the label field is unresolved the source app/element itself
297
+ // resolved, so pulling it (again) cannot help: the export names the label
298
+ // field in a form pull cannot decode. `pull element` has no
299
+ // --with-dependencies, so the remedy names the pull itself.
300
+ reportAuthoringBlocker(ctx, {
301
+ address: site.address,
302
+ description: `${site.where}: ${reason}. Dropping dynamic_config would author an empty static Dropdown, ` +
303
+ "which plan rejects and which would replace the live dynamic options.",
304
+ remedy: labelOnly
305
+ ? "The source app/element resolves, but the export's label field reference can't be decoded, so pulling the source won't help. " +
306
+ "Hand-author the Dropdown's dynamic config (source and label field) in the owning app/element file."
307
+ : "Make the Dropdown's source app/element and label field resolvable (pull the source into this workspace with " +
308
+ "`elementum pull app`/`pull element` first), then retry.",
309
+ });
310
+ }
311
+ /**
312
+ * `parent_mappings` can be changed in place, but the platform keeps existing
313
+ * mappings when the update clears them, so the provider reports
314
+ * "inconsistent result after apply" and the plan never converges (#1404).
315
+ * Dropping one in pull therefore makes source that cannot be applied: block.
316
+ */
317
+ function blockUnclearableParentMappings(ctx, site) {
318
+ reportAuthoringBlocker(ctx, {
319
+ address: site.address,
320
+ description: `${site.where}: dynamic_config parent mapping field IDs could not be reconstructed. The platform keeps parent mappings ` +
321
+ `when an update removes them, so dropping them would make every apply fail with "inconsistent result after apply".`,
322
+ remedy: "Pull the apps/elements that own the mapped parent/source fields into this workspace (`elementum pull app`/`pull element`), or change the Dropdown's parent mappings in the Elementum UI, then pull again.",
323
+ });
324
+ }
325
+ /**
326
+ * A field pull cannot extract at all. Leaving it out of the printed source
327
+ * would let the next plan destroy a live, possibly adopted field, so the pull
328
+ * refuses instead (edk#1484). The address also goes to `skipped` so a caller
329
+ * that does not author this field keeps it out of adoption.
174
330
  */
175
- function dynamicConfigFromTf(value, owner, ctx, where) {
331
+ function blockUnextractableField(ctx, site, reason, remedy) {
332
+ ctx.skipped.push(site.address);
333
+ reportAuthoringBlocker(ctx, {
334
+ address: site.address,
335
+ description: `${site.where}: ${reason}, so pull cannot write this field and leaving it out would let the next plan destroy it.`,
336
+ remedy,
337
+ });
338
+ }
339
+ const MALFORMED_FIELD_REMEDY = "The export carries a field the EDK cannot reconstruct. Update the Elementum CLI and pull again; if it persists, report the field's type to the EDK maintainers.";
340
+ function dynamicConfigFromTf(value, owner, ctx, site) {
341
+ const where = site.where;
176
342
  if (value === undefined)
177
343
  return undefined;
178
344
  if (typeof value !== "object" || value === null || isTfExpr(value) || Array.isArray(value)) {
179
- ctx.diagnostics.push(`${where}: dynamic_config has an unexpected shape; dropped`);
345
+ blockDynamicConfigDrop(ctx, site, "dynamic_config has an unexpected shape");
180
346
  return undefined;
181
347
  }
182
348
  const attrs = value;
183
- const relatedAspectId = resolveRelatedObject(attrs["related_aspect_id"], ctx);
184
- const relatedLabelFieldId = resolveRelatedField(attrs["related_label_field_id"], ctx);
349
+ const relatedAspectId = selfRelatedObject(attrs["related_aspect_id"], owner, ctx) ?? resolveRelatedObject(attrs["related_aspect_id"], ctx, /* namespaceFallback (#1440) */ true);
350
+ const relatedLabelFieldId = resolveRelatedOrSelfField(attrs["related_label_field_id"], owner, ctx);
185
351
  if (relatedAspectId === undefined || relatedLabelFieldId === undefined) {
186
352
  const missing = [
187
353
  relatedAspectId === undefined && "related_aspect_id",
188
354
  relatedLabelFieldId === undefined && "related_label_field_id",
189
355
  ].filter((v) => v !== false);
190
- ctx.diagnostics.push(`${where}: dynamic_config.${missing.join(" and ")} does not resolve to a catalog reference or raw platform id; dropped`);
356
+ blockDynamicConfigDrop(ctx, site, `dynamic_config.${missing.join(" and ")} does not resolve to a catalog reference or raw platform id`, relatedAspectId !== undefined);
191
357
  return undefined;
192
358
  }
193
359
  let parentFieldId;
194
360
  if (attrs["parent_field_id"] !== undefined) {
195
361
  parentFieldId = fieldKeyFromTfRef(attrs["parent_field_id"], owner.label, owner.scope ?? "app");
196
362
  if (parentFieldId === undefined) {
197
- ctx.diagnostics.push(`${where}: dynamic_config.parent_field_id could not be resolved to a same-aspect field; dropped`);
363
+ blockReplaceForcingDrop(ctx, site, "parent_field_id", "could not be resolved to a same-aspect field");
198
364
  }
199
365
  }
200
366
  const autoRelate = typeof attrs["auto_relate"] === "boolean" ? attrs["auto_relate"] : undefined;
@@ -203,15 +369,16 @@ function dynamicConfigFromTf(value, owner, ctx, where) {
203
369
  if (attrs["filter"] !== undefined) {
204
370
  try {
205
371
  filter = deserializeFilterFromTf(attrs["filter"], {
206
- field: (v) => dynamicPicklistFilterFieldRef(v, ctx),
372
+ field: (v) => dynamicPicklistFilterFieldRef(v, owner, ctx),
207
373
  recordField: (v) => dynamicPicklistRecordFieldRef(v, owner, ctx),
374
+ recordFieldOwner: (v) => resourceRefLabel(v, owner.type) === owner.label,
208
375
  reference: () => {
209
376
  throw new EntityTfError("dynamic picklist filter does not support value references");
210
377
  },
211
378
  });
212
379
  }
213
380
  catch (e) {
214
- ctx.diagnostics.push(`${where}: dynamic_config.filter ${e instanceof Error ? e.message : String(e)}; dropped`);
381
+ blockReplaceForcingDrop(ctx, site, "filter", e instanceof Error ? e.message : String(e));
215
382
  }
216
383
  }
217
384
  let sort;
@@ -239,21 +406,19 @@ function dynamicConfigFromTf(value, owner, ctx, where) {
239
406
  unresolvedField = true;
240
407
  return rawOrder;
241
408
  }
242
- const resolved = resolveRelatedField(field, ctx);
409
+ const resolved = resolveRelatedOrSelfField(field, owner, ctx);
243
410
  if (resolved === undefined)
244
411
  unresolvedField = true;
245
412
  return resolved === undefined ? rawOrder : { ...order, field: resolved };
246
413
  });
247
- if (unresolvedField) {
248
- ctx.diagnostics.push(`${where}: dynamic_config.sort has an unresolved order field; dropped sort`);
249
- }
250
- else {
414
+ if (unresolvedField)
415
+ blockReplaceForcingDrop(ctx, site, "sort", "has an unresolved order field");
416
+ else
251
417
  sort = { ...rawSort, orders };
252
- }
253
418
  }
254
419
  }
255
420
  else {
256
- ctx.diagnostics.push(`${where}: dynamic_config.sort is not a jsonencode(...) object; dropped`);
421
+ blockReplaceForcingDrop(ctx, site, "sort", "is not a jsonencode(...) object");
257
422
  }
258
423
  }
259
424
  let parentMappings;
@@ -270,15 +435,20 @@ function dynamicConfigFromTf(value, owner, ctx, where) {
270
435
  continue;
271
436
  }
272
437
  const mapping = rawMapping;
273
- const parentAspectFieldId = resolveRelatedId(mapping["parent_aspect_field_id"], ctx);
274
- const relatedAspectFieldId = resolveRelatedId(mapping["related_aspect_field_id"], ctx);
438
+ // Mapped fields usually live on another app (the parent's and this
439
+ // Dropdown's sources), so resolve them like related_label_field_id:
440
+ // raw/imported id, else the foreign data.elementum_field identity (#1404),
441
+ // else (clone) the owner's own field (edk#1481).
442
+ const parentAspectFieldId = resolveRelatedOrSelfField(mapping["parent_aspect_field_id"], owner, ctx);
443
+ const relatedAspectFieldId = resolveRelatedOrSelfField(mapping["related_aspect_field_id"], owner, ctx);
275
444
  if (parentAspectFieldId === undefined || relatedAspectFieldId === undefined) {
276
- ctx.diagnostics.push(`${where}: dynamic_config parent mapping field IDs could not be reconstructed; dropped`);
277
- continue;
445
+ blockUnclearableParentMappings(ctx, site);
446
+ parentMappings = undefined;
447
+ break;
278
448
  }
279
449
  parentMappings.push({ parentAspectFieldId, relatedAspectFieldId });
280
450
  }
281
- if (parentMappings.length === 0)
451
+ if (parentMappings !== undefined && parentMappings.length === 0)
282
452
  parentMappings = undefined;
283
453
  }
284
454
  }
@@ -349,14 +519,15 @@ function staticParentLinkageFromTf(value, optionsValue, owner, ctx, where) {
349
519
  */
350
520
  export function fieldFromTf(block, owner, ctx) {
351
521
  const subType = SUB_TYPE_BY_RESOURCE.get(block.type ?? "");
352
- if (subType === undefined) {
353
- ctx.diagnostics.push(`resource "${block.type}" is not a known field resource; skipped`);
354
- return undefined;
355
- }
356
522
  const fieldLabel = block.label;
357
523
  if (fieldLabel === undefined || fieldLabel === "") {
358
524
  throw new EntityTfError(`${block.type} block is missing its label`);
359
525
  }
526
+ if (subType === undefined) {
527
+ const address = `${block.type}.${fieldLabel}`;
528
+ blockUnextractableField(ctx, { address, where: `field (${address})` }, `resource "${block.type}" is not a known field resource`, MALFORMED_FIELD_REMEDY);
529
+ return undefined;
530
+ }
360
531
  if (subType.implemented !== true) {
361
532
  // Declared but not yet round-tripped - see FIELD_SUB_TYPES for the
362
533
  // current list of not-yet-implemented sub-types, if any.
@@ -385,7 +556,7 @@ export function fieldFromTf(block, owner, ctx) {
385
556
  const options = subType.hasOptions === true && attrs["dynamic_config"] === undefined
386
557
  ? optionsFromTf(attrs["options"], `${where} options`, ctx)
387
558
  : undefined;
388
- const config = subType.hasOptions === true ? dynamicConfigFromTf(attrs["dynamic_config"], owner, ctx, where) : undefined;
559
+ const config = subType.hasOptions === true ? dynamicConfigFromTf(attrs["dynamic_config"], owner, ctx, { address: `${block.type}.${fieldLabel}`, where }) : undefined;
389
560
  const parentLinkage = subType.hasOptions === true && attrs["dynamic_config"] === undefined
390
561
  ? staticParentLinkageFromTf(attrs["static_config"], attrs["options"], owner, ctx, where)
391
562
  : undefined;
@@ -442,8 +613,8 @@ export function fieldFromTf(block, owner, ctx) {
442
613
  // field ids are read-only outputs of that resource, never their own
443
614
  // resource address). Both properties are required by RelationFieldOptions,
444
615
  // so printing relationField(...) with either missing would be invalid
445
- // TypeScript (item 117) - skip the whole field when either side can't be
446
- // resolved, same convention as an unimplemented sub-type.
616
+ // TypeScript (item 117), and leaving the field out would let the next plan
617
+ // destroy it - refuse the pull when either side can't be resolved (#1484).
447
618
  let relatedObject;
448
619
  let relatedLabelField;
449
620
  let autoRelate;
@@ -457,8 +628,14 @@ export function fieldFromTf(block, owner, ctx) {
457
628
  relatedObject === undefined && "related_object_id",
458
629
  relatedLabelField === undefined && "related_label_field_id",
459
630
  ].filter((v) => v !== false);
460
- ctx.diagnostics.push(`${where}: ${missing.join(" and ")} does not resolve to a raw platform id - a RELATION field needs both to reconstruct relatedObject/relatedLabelField; field skipped`);
461
- ctx.skipped.push(`${block.type}.${fieldLabel}`);
631
+ const foreignTarget = attrs["related_object_id"];
632
+ const onlyForeignTargetMissing = relatedLabelField !== undefined &&
633
+ isTfExpr(foreignTarget) &&
634
+ foreignTarget.$expr === "data-attr" &&
635
+ (foreignTarget.type === "elementum_app" || foreignTarget.type === "elementum_element");
636
+ blockUnextractableField(ctx, { address: `${block.type}.${fieldLabel}`, where }, `${missing.join(" and ")} does not resolve to a raw platform id or a pulled app/element, and a RELATION field needs both relatedObject and relatedLabelField`, onlyForeignTargetMissing
637
+ ? "Pull the related apps and elements into this workspace (retry with --with-dependencies), then pull again."
638
+ : "The export names these relation fields' related object or label field in a form pull cannot resolve, and --with-dependencies cannot resolve it either. Change the relation field in the Elementum UI, then pull again.");
462
639
  return undefined;
463
640
  }
464
641
  }
@@ -518,14 +695,12 @@ export function fieldFromTf(block, owner, ctx) {
518
695
  const scope = owner.scope ?? "app";
519
696
  const calculation = subType.typeKey === "calculated" ? optionalString(attrs, "calculation", where, ctx) : undefined;
520
697
  if (subType.typeKey === "calculated" && calculation === undefined) {
521
- ctx.skipped.push(`${block.type}.${fieldLabel}`);
522
- ctx.diagnostics.push(`${where}: calculation is required; skipped`);
698
+ blockUnextractableField(ctx, { address: `${block.type}.${fieldLabel}`, where }, "calculation is missing", MALFORMED_FIELD_REMEDY);
523
699
  return undefined;
524
700
  }
525
701
  const calculationType = subType.typeKey === "calculated" ? attrs["calculation_type"] : undefined;
526
702
  if (subType.typeKey === "calculated" && !isCalculationResultType(calculationType)) {
527
- ctx.skipped.push(`${block.type}.${fieldLabel}`);
528
- ctx.diagnostics.push(`${where}: calculation_type is missing or unsupported; skipped rather than guessing the typed result`);
703
+ blockUnextractableField(ctx, { address: `${block.type}.${fieldLabel}`, where }, `calculation_type is missing or unsupported (${JSON.stringify(calculationType ?? null)}), and pull will not guess the typed result`, MALFORMED_FIELD_REMEDY);
529
704
  return undefined;
530
705
  }
531
706
  let calculationInclude;
@@ -2,3 +2,9 @@
2
2
  import { type EntityKindModule } from "../../graph/types.js";
3
3
  export declare const FIELD_DATA_RESOURCE_TYPE = "elementum_field";
4
4
  export declare const fieldModule: EntityKindModule;
5
+ /**
6
+ * `elementum_managed_field_order` orders an owner's custom fields. Build emits
7
+ * it from the authored `fields` key order; pull prints fields in its order.
8
+ * Like the managed view order, its import/state id is the owner's id.
9
+ */
10
+ export declare const managedFieldOrderModule: EntityKindModule;
@@ -1,7 +1,7 @@
1
1
  /** The field entity's registry entry (see src/graph/). */
2
2
  import { asNamedRef, namedFactoryEntry } from "../../catalog/render-helpers.js";
3
- import { NO_LIFECYCLE } from "../../graph/types.js";
4
- import { FIELD_SUB_TYPES } from "./types.js";
3
+ import { NO_CANVAS, NO_CATALOG, NO_DATA, NO_LIFECYCLE } from "../../graph/types.js";
4
+ import { FIELD_SUB_TYPES, MANAGED_FIELD_ORDER_RESOURCE_TYPE } from "./types.js";
5
5
  export const FIELD_DATA_RESOURCE_TYPE = "elementum_field";
6
6
  export const fieldModule = {
7
7
  kind: "field",
@@ -10,6 +10,7 @@ export const fieldModule = {
10
10
  parents: [
11
11
  { parent: "app", child: "field" },
12
12
  { parent: "element", child: "field" },
13
+ { parent: "task", child: "field" },
13
14
  ],
14
15
  resource: {
15
16
  shape: "subTypes",
@@ -70,3 +71,23 @@ export const fieldModule = {
70
71
  refFromAuthoring: () => undefined,
71
72
  },
72
73
  };
74
+ /**
75
+ * `elementum_managed_field_order` orders an owner's custom fields. Build emits
76
+ * it from the authored `fields` key order; pull prints fields in its order.
77
+ * Like the managed view order, its import/state id is the owner's id.
78
+ */
79
+ export const managedFieldOrderModule = {
80
+ kind: "managed-field-order",
81
+ stream: { role: "streamMember" },
82
+ authoring: { unit: "inline", bag: "fields" },
83
+ parents: [
84
+ { parent: "app", child: "managed-field-order" },
85
+ { parent: "element", child: "managed-field-order" },
86
+ { parent: "task", child: "managed-field-order" },
87
+ ],
88
+ resource: { shape: "single", resourceType: MANAGED_FIELD_ORDER_RESOURCE_TYPE, adoptable: true },
89
+ data: NO_DATA,
90
+ lifecycle: NO_LIFECYCLE,
91
+ canvas: NO_CANVAS,
92
+ catalog: NO_CATALOG,
93
+ };
@@ -73,7 +73,12 @@ export function optionsFromTf(v, where, ctx) {
73
73
  return undefined;
74
74
  const out = {};
75
75
  for (const { key, item } of withUniqueKeys(parsed, (p) => p.label, "option")) {
76
- out[key] = {
76
+ // Digit-only keys are JS array indices: Object.values reorders them and
77
+ // pull prints the wrong option order (edk#1449). A leading underscore is
78
+ // outside camelCase's key space (it strips non-alphanumerics) and is not
79
+ // an integer index, so the prefix cannot collide with another option.
80
+ const bagKey = /^(0|[1-9]\d*)$/.test(key) ? `_${key}` : key;
81
+ out[bagKey] = {
77
82
  id: "",
78
83
  label: item.label,
79
84
  ...(item.color !== undefined && { color: item.color }),
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The custom fields' order (#1670): `elementum_managed_field_order` lists an
3
+ * owner's field resources in authored `fields` key order. The platform's own
4
+ * order is whatever order the field creates finished in, so without it the
5
+ * default column order and every pull's field order drift between applies.
6
+ */
7
+ import { type TfBlock, type TfExpr } from "../../engine/tf/index.js";
8
+ import { type FieldOwnerScope } from "../../engine/tf/labels.js";
9
+ /** The owner's field order block, or undefined when it has no field resource. `fieldAddresses` iterates in authored order. */
10
+ export declare function managedFieldOrderToTf(ownerTfLabel: string, scope: FieldOwnerScope, objectId: TfExpr, fieldAddresses: ReadonlyMap<string, string>): TfBlock | undefined;
11
+ /**
12
+ * `fieldBlocks` in the order the owner's field order lists them, so pull prints
13
+ * fields in the platform's order instead of the parser's type-and-label order.
14
+ * Fields it doesn't list keep their relative order after the listed ones.
15
+ */
16
+ export declare function inManagedFieldOrder(fieldBlocks: readonly TfBlock[], orderBlocks: readonly TfBlock[]): readonly TfBlock[];
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The custom fields' order (#1670): `elementum_managed_field_order` lists an
3
+ * owner's field resources in authored `fields` key order. The platform's own
4
+ * order is whatever order the field creates finished in, so without it the
5
+ * default column order and every pull's field order drift between applies.
6
+ */
7
+ import { expr, isTfExpr } from "../../engine/tf/index.js";
8
+ import { scopedResourceLabel } from "../../engine/tf/labels.js";
9
+ import { MANAGED_FIELD_ORDER_RESOURCE_TYPE } from "./types.js";
10
+ /** The owner's field order block, or undefined when it has no field resource. `fieldAddresses` iterates in authored order. */
11
+ export function managedFieldOrderToTf(ownerTfLabel, scope, objectId, fieldAddresses) {
12
+ if (fieldAddresses.size === 0)
13
+ return undefined;
14
+ const fieldIds = [...fieldAddresses.values()].map((address) => {
15
+ const dot = address.indexOf(".");
16
+ return expr.resourceAttr(address.slice(0, dot), address.slice(dot + 1), ["id"]);
17
+ });
18
+ return {
19
+ kind: "resource",
20
+ type: MANAGED_FIELD_ORDER_RESOURCE_TYPE,
21
+ label: scopedResourceLabel(ownerTfLabel, "field_order", scope),
22
+ attrs: { object_id: objectId, field_ids: fieldIds },
23
+ };
24
+ }
25
+ /**
26
+ * `fieldBlocks` in the order the owner's field order lists them, so pull prints
27
+ * fields in the platform's order instead of the parser's type-and-label order.
28
+ * Fields it doesn't list keep their relative order after the listed ones.
29
+ */
30
+ export function inManagedFieldOrder(fieldBlocks, orderBlocks) {
31
+ const fieldIds = orderBlocks[0]?.attrs["field_ids"];
32
+ if (!Array.isArray(fieldIds))
33
+ return fieldBlocks;
34
+ const position = new Map();
35
+ for (const [index, value] of fieldIds.entries()) {
36
+ if (isTfExpr(value) && value.$expr === "resource-attr")
37
+ position.set(`${value.type}.${value.label}`, index);
38
+ }
39
+ const at = (block) => position.get(`${block.type}.${block.label}`) ?? Number.MAX_SAFE_INTEGER;
40
+ return [...fieldBlocks].sort((a, b) => at(a) - at(b));
41
+ }