@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,501 @@
1
+ /**
2
+ * Cross-pull registry of platform field ids → dependency catalog identities.
3
+ *
4
+ * `pull app` starts each run with empty in-memory maps unless we hydrate from
5
+ * disk: a prior `pull element tickets` writes the element and name map, but
6
+ * the field-id map was discarded when that command exited — so a later
7
+ * `pull app knowledge` still can't resolve foreign createRecord field_ids.
8
+ *
9
+ * Persist entries as they're learned during extract (`rememberPulledFieldIdentities`)
10
+ * and hydrate at the next pull's start. State fills gaps for already-adopted
11
+ * fields without forcing a re-pull. The pull orchestrators pass the resources
12
+ * of their backend-agnostic `readStateSnapshot` read; a bare local
13
+ * `terraform.tfstate` read sees nothing on a declared remote backend.
14
+ */
15
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
16
+ import { join, resolve } from "node:path";
17
+ import { randomUUID } from "node:crypto";
18
+ import { existsSync } from "node:fs";
19
+ import { SYSTEM_FIELD_KEYS } from "../../core/system-fields.js";
20
+ import { AUTHORING_TYPE_BY_RESOURCE_TYPE, ENTITY_KIND_BY_ID, STREAM_ROOT_RESOURCE_TYPES, knownResourceTypes, } from "../../graph/index.js";
21
+ import { SCOPE_OWNER_RESOURCE_TYPES } from "../../graph/resource-types.js";
22
+ import { childPartFromScopedLabel, ownerFromScopedChildLabel, sanitizeName, scopedResourceLabel, tfLabelToTsLabel, tsLabelToTfLabel, } from "../tf/labels.js";
23
+ const MANIFEST_VERSION = 2;
24
+ /** System-field bag key → stream-root attr (`title` → `title_field_id`), same bijection as view locals. */
25
+ const SYSTEM_FIELD_KEY_BY_ID_ATTR = new Map(SYSTEM_FIELD_KEYS.map((key) => [`${tsLabelToTfLabel(key)}_field_id`, key]));
26
+ function fieldResourceTypes() {
27
+ const field = ENTITY_KIND_BY_ID.get("field");
28
+ return field === undefined ? [] : knownResourceTypes(field.resource);
29
+ }
30
+ /** Option-bearing field sub-types (typeKeys from the field kind module). */
31
+ function picklistFieldResourceTypes() {
32
+ const field = ENTITY_KIND_BY_ID.get("field");
33
+ if (field?.resource.shape !== "subTypes")
34
+ return new Set();
35
+ return new Set(field.resource.rows
36
+ .filter((row) => row.key === "picklist" || row.key === "multiPicklist")
37
+ .map((row) => row.resourceType));
38
+ }
39
+ /**
40
+ * The current options of every static Dropdown/Multi-Select field in OpenTofu
41
+ * state, by field (#2568). The platform exports scalar Dropdown assignments
42
+ * through an options-by-label expression, but exports Multi-Select assignments
43
+ * and some filter values as raw option UUIDs. State is the only authoritative
44
+ * place shared by both shapes that holds each option's id, label and field.
45
+ */
46
+ export function picklistOptionsByFieldFromState(resources) {
47
+ const out = new Map();
48
+ const picklistTypes = picklistFieldResourceTypes();
49
+ for (const resource of resources) {
50
+ if (resource.mode !== undefined && resource.mode !== "managed")
51
+ continue;
52
+ if (typeof resource.type !== "string" || !picklistTypes.has(resource.type))
53
+ continue;
54
+ if (typeof resource.name !== "string" || resource.name === "")
55
+ continue;
56
+ const labelById = new Map();
57
+ for (const instance of resource.instances ?? []) {
58
+ const attrs = instance.attributes;
59
+ if (attrs === null || typeof attrs !== "object")
60
+ continue;
61
+ const options = attrs.options;
62
+ if (!Array.isArray(options))
63
+ continue;
64
+ for (const option of options) {
65
+ if (option === null || typeof option !== "object")
66
+ continue;
67
+ const id = option.id;
68
+ const label = option.label;
69
+ // Removing an option deactivates it, and the provider keeps it in
70
+ // state as `active = false`. It is not a current option (#2568).
71
+ if (option.active === false)
72
+ continue;
73
+ if (typeof id === "string" && id !== "" && typeof label === "string" && label !== "")
74
+ labelById.set(id, label);
75
+ }
76
+ }
77
+ if (labelById.size > 0)
78
+ out.set(resource.name, labelById);
79
+ }
80
+ return out;
81
+ }
82
+ /** State-backed values for each static field's `<field>_options_by_label` local. */
83
+ export function picklistOptionLocalValuesFromState(resources) {
84
+ const out = new Map();
85
+ const picklistTypes = picklistFieldResourceTypes();
86
+ for (const resource of resources) {
87
+ if (resource.mode !== "managed" ||
88
+ typeof resource.type !== "string" ||
89
+ !picklistTypes.has(resource.type) ||
90
+ typeof resource.name !== "string" ||
91
+ resource.name === "")
92
+ continue;
93
+ for (const instance of resource.instances ?? []) {
94
+ const attrs = instance.attributes;
95
+ if (attrs === null || typeof attrs !== "object")
96
+ continue;
97
+ const options = attrs.options;
98
+ if (!Array.isArray(options))
99
+ continue;
100
+ for (const option of options) {
101
+ if (option === null || typeof option !== "object")
102
+ continue;
103
+ const id = option.id;
104
+ const label = option.label;
105
+ if (typeof id === "string" && id !== "" && typeof label === "string" && label !== "") {
106
+ out.set(`${resource.name}_options_by_label[${JSON.stringify(label)}]`, id);
107
+ }
108
+ }
109
+ }
110
+ }
111
+ return out;
112
+ }
113
+ function manifestPath(orgRoot) {
114
+ return join(resolve(orgRoot), ".edk-state", "field-refs-by-id.json");
115
+ }
116
+ function importLeafId(importId) {
117
+ return importId.slice(importId.lastIndexOf(":") + 1);
118
+ }
119
+ function ownerScopeFromScopedLabel(scopedLabel) {
120
+ if (scopedLabel.startsWith("apps_")) {
121
+ const rest = scopedLabel.slice("apps_".length);
122
+ const sep = rest.indexOf("_");
123
+ if (sep === -1)
124
+ return undefined;
125
+ return { scope: "app", ownerTfLabel: rest.slice(0, sep) };
126
+ }
127
+ if (scopedLabel.startsWith("elements_")) {
128
+ const rest = scopedLabel.slice("elements_".length);
129
+ const sep = rest.indexOf("_");
130
+ if (sep === -1)
131
+ return undefined;
132
+ return { scope: "element", ownerTfLabel: rest.slice(0, sep) };
133
+ }
134
+ return undefined;
135
+ }
136
+ function fieldKeyFromScopedResourceName(scopedLabel, scope, ownerTfLabel) {
137
+ try {
138
+ const childPart = childPartFromScopedLabel(ownerTfLabel, scopedLabel, scope);
139
+ return tfLabelToTsLabel(childPart);
140
+ }
141
+ catch {
142
+ return undefined;
143
+ }
144
+ }
145
+ /**
146
+ * Repair persisted field identities before extraction when state proves that
147
+ * an export label is an alias for the workspace's authored label.
148
+ *
149
+ * Some export payloads carry raw field UUIDs rather than resource references.
150
+ * Relabelling the parsed document cannot change those values, so their
151
+ * registry entries must move to the same authored bag key as well.
152
+ */
153
+ export function preserveAuthoredFieldRefKeys(fieldRefsById, importIds, aliases) {
154
+ const fieldTypes = new Set(fieldResourceTypes());
155
+ for (const { from, to } of aliases) {
156
+ const fromDot = from.indexOf(".");
157
+ const toDot = to.indexOf(".");
158
+ if (fromDot === -1 || toDot === -1)
159
+ continue;
160
+ const type = from.slice(0, fromDot);
161
+ if (!fieldTypes.has(type) || to.slice(0, toDot) !== type)
162
+ continue;
163
+ const importedId = importIds.get(from);
164
+ if (importedId === undefined)
165
+ continue;
166
+ const canonicalId = importedId.split(/[:/]/).at(-1) ?? importedId;
167
+ const registryId = fieldRefsById.has(importedId)
168
+ ? importedId
169
+ : fieldRefsById.has(canonicalId) ? canonicalId : undefined;
170
+ if (registryId === undefined)
171
+ continue;
172
+ const label = to.slice(toDot + 1);
173
+ const owner = ownerScopeFromScopedLabel(label);
174
+ if (owner === undefined)
175
+ continue;
176
+ const fieldKey = fieldKeyFromScopedResourceName(label, owner.scope, owner.ownerTfLabel);
177
+ if (fieldKey === undefined)
178
+ continue;
179
+ fieldRefsById.set(registryId, { ...fieldRefsById.get(registryId), fieldKey });
180
+ }
181
+ }
182
+ /** display name (lower) → bag key, built from persisted/bootstrap field identities. */
183
+ export function buildFieldNamesByOwnerRefName(fieldRefsById) {
184
+ const out = new Map();
185
+ // Managed UUID entries first. A later `refOnly:` display-name guess must
186
+ // not overwrite the bag key already bound in state (#1431).
187
+ const ordered = [...fieldRefsById.entries()].sort(([left], [right]) => {
188
+ const leftRef = left.startsWith("refOnly:") ? 1 : 0;
189
+ const rightRef = right.startsWith("refOnly:") ? 1 : 0;
190
+ return leftRef - rightRef;
191
+ });
192
+ for (const [id, { ownerRefName, fieldKey, fieldName }] of ordered) {
193
+ if (fieldName === undefined || fieldName === "")
194
+ continue;
195
+ let names = out.get(ownerRefName);
196
+ if (names === undefined) {
197
+ names = new Map();
198
+ out.set(ownerRefName, names);
199
+ }
200
+ const lower = fieldName.toLowerCase();
201
+ if (id.startsWith("refOnly:") && names.has(lower))
202
+ continue;
203
+ names.set(lower, fieldKey);
204
+ }
205
+ return out;
206
+ }
207
+ function bootstrapSystemFieldRefsFromStreamRoot(out, resource, streamRootTypes) {
208
+ if (typeof resource.type !== "string" || !streamRootTypes.has(resource.type))
209
+ return;
210
+ if (typeof resource.name !== "string" || resource.name === "")
211
+ return;
212
+ const ownerRefName = tfLabelToTsLabel(resource.name);
213
+ for (const instance of resource.instances ?? []) {
214
+ const attrs = instance.attributes;
215
+ if (attrs === null || typeof attrs !== "object")
216
+ continue;
217
+ for (const [attr, fieldKey] of SYSTEM_FIELD_KEY_BY_ID_ATTR) {
218
+ const id = attrs[attr];
219
+ if (typeof id !== "string" || id === "")
220
+ continue;
221
+ out.set(id, { ownerRefName, fieldKey });
222
+ }
223
+ }
224
+ }
225
+ async function localStateResources(orgRoot) {
226
+ const statePath = join(resolve(orgRoot), "terraform.tfstate");
227
+ if (!existsSync(statePath))
228
+ return [];
229
+ try {
230
+ const raw = await readFile(statePath, "utf8");
231
+ // eslint-disable-next-line no-restricted-syntax -- local workspace state is machine-owned JSON
232
+ return JSON.parse(raw).resources ?? [];
233
+ }
234
+ catch {
235
+ return [];
236
+ }
237
+ }
238
+ function bootstrapFromState(resources) {
239
+ const fieldRefsById = new Map();
240
+ const fieldTypes = new Set(fieldResourceTypes());
241
+ const streamRootTypes = STREAM_ROOT_RESOURCE_TYPES;
242
+ for (const resource of resources) {
243
+ bootstrapSystemFieldRefsFromStreamRoot(fieldRefsById, resource, streamRootTypes);
244
+ if (typeof resource.type !== "string" || !fieldTypes.has(resource.type))
245
+ continue;
246
+ if (typeof resource.name !== "string" || resource.name === "")
247
+ continue;
248
+ const owner = ownerFromScopedChildLabel(resource.name);
249
+ if (owner === undefined)
250
+ continue;
251
+ const fieldKey = fieldKeyFromScopedResourceName(resource.name, owner.scope, owner.ownerTfLabel);
252
+ if (fieldKey === undefined)
253
+ continue;
254
+ const ownerRefName = tfLabelToTsLabel(owner.ownerTfLabel);
255
+ // Same resourceType -> authoringType lookup collectFieldRefEntriesFromExtract
256
+ // uses (via the field's own `type` attr) below, but state instances carry
257
+ // no such attribute — the resource TYPE itself is the discriminant here.
258
+ const fieldType = AUTHORING_TYPE_BY_RESOURCE_TYPE.get(resource.type);
259
+ for (const instance of resource.instances ?? []) {
260
+ const attrs = instance.attributes;
261
+ if (attrs === null || typeof attrs !== "object")
262
+ continue;
263
+ const id = attrs.id;
264
+ if (typeof id !== "string" || id === "")
265
+ continue;
266
+ const fieldName = attrs.name;
267
+ fieldRefsById.set(id, {
268
+ ownerRefName,
269
+ fieldKey,
270
+ ...(typeof fieldName === "string" && fieldName !== "" ? { fieldName } : {}),
271
+ ...(fieldType !== undefined ? { fieldType } : {}),
272
+ });
273
+ }
274
+ }
275
+ return fieldRefsById;
276
+ }
277
+ /** The key a ref-only data lookup derives from a display name ("Bid Evaluation" → bidEvaluation). */
278
+ function isDisplayNameSlug(fieldKey, fieldName) {
279
+ return fieldName !== undefined && fieldKey === tfLabelToTsLabel(sanitizeName(fieldName));
280
+ }
281
+ export async function readOptionIdIndex(orgRoot) {
282
+ const resources = await localStateResources(orgRoot);
283
+ const picklistTypes = picklistFieldResourceTypes();
284
+ const idsByLocal = new Map();
285
+ for (const resource of resources) {
286
+ if (resource.mode !== "managed" || typeof resource.type !== "string" || typeof resource.name !== "string" || resource.name === "")
287
+ continue;
288
+ const isApp = resource.type === SCOPE_OWNER_RESOURCE_TYPES.app;
289
+ if (!isApp && !picklistTypes.has(resource.type))
290
+ continue;
291
+ const local = `${resource.name}_${isApp ? "status_" : ""}options_by_label`;
292
+ const ids = new Map();
293
+ for (const instance of resource.instances ?? []) {
294
+ const attrs = instance.attributes;
295
+ const options = attrs?.[isApp ? "status_options" : "options"];
296
+ if (!Array.isArray(options))
297
+ continue;
298
+ for (const option of options) {
299
+ if (option === null || typeof option !== "object")
300
+ continue;
301
+ const { id, label } = option;
302
+ if (typeof id === "string" && id !== "" && typeof label === "string" && label !== "")
303
+ ids.set(label, id);
304
+ }
305
+ }
306
+ if (ids.size > 0)
307
+ idsByLocal.set(local, ids);
308
+ }
309
+ const labelById = (await readManifest(orgRoot)).picklistOptionLabelById;
310
+ for (const ids of idsByLocal.values()) {
311
+ for (const [label, id] of ids)
312
+ labelById.set(id, label);
313
+ }
314
+ return { idsByLocal, labelById };
315
+ }
316
+ /** Authored field identities persisted by earlier pulls. Reads the manifest only. */
317
+ export async function readManifestFieldRefs(orgRoot) {
318
+ return (await readManifest(orgRoot)).entries;
319
+ }
320
+ async function readManifest(orgRoot) {
321
+ const entries = new Map();
322
+ const picklistOptionLabelById = new Map();
323
+ try {
324
+ const raw = await readFile(manifestPath(orgRoot), "utf8");
325
+ // eslint-disable-next-line no-restricted-syntax -- local machine-owned JSON
326
+ const parsed = JSON.parse(raw);
327
+ if (parsed.entries !== null && typeof parsed.entries === "object") {
328
+ for (const [id, identity] of Object.entries(parsed.entries)) {
329
+ if (typeof identity?.ownerRefName === "string" &&
330
+ identity.ownerRefName !== "" &&
331
+ typeof identity.fieldKey === "string" &&
332
+ identity.fieldKey !== "") {
333
+ entries.set(id, {
334
+ ownerRefName: identity.ownerRefName,
335
+ fieldKey: identity.fieldKey,
336
+ ...(typeof identity.fieldName === "string" && identity.fieldName !== "" ? { fieldName: identity.fieldName } : {}),
337
+ ...(typeof identity.fieldType === "string" && identity.fieldType !== "" ? { fieldType: identity.fieldType } : {}),
338
+ });
339
+ }
340
+ }
341
+ }
342
+ if (parsed.picklistOptionLabels !== null && typeof parsed.picklistOptionLabels === "object") {
343
+ for (const [id, label] of Object.entries(parsed.picklistOptionLabels)) {
344
+ if (typeof label === "string" && label !== "")
345
+ picklistOptionLabelById.set(id, label);
346
+ }
347
+ }
348
+ }
349
+ catch {
350
+ // missing or corrupt — treat as empty
351
+ }
352
+ return { entries, picklistOptionLabelById };
353
+ }
354
+ /**
355
+ * Hydrate the in-memory registry for a pull run.
356
+ *
357
+ * `stateResources` is the pull's `readStateSnapshot` read, so a declared
358
+ * backend is seen too; without it only a local root `terraform.tfstate` is.
359
+ * The manifest key is authored and wins, except a key that is only the
360
+ * display-name slug a ref-only lookup guessed: there the managed state key
361
+ * wins (#1431), and the correction is persisted.
362
+ */
363
+ export async function loadPulledFieldRefsContext(orgRoot, stateResources) {
364
+ const resources = stateResources ?? (await localStateResources(orgRoot));
365
+ const manifest = await readManifest(orgRoot);
366
+ const corrected = new Map();
367
+ for (const [id, identity] of bootstrapFromState(resources)) {
368
+ const prior = manifest.entries.get(id);
369
+ if (prior === undefined) {
370
+ manifest.entries.set(id, identity);
371
+ continue;
372
+ }
373
+ if (prior.fieldKey === identity.fieldKey || !isDisplayNameSlug(prior.fieldKey, prior.fieldName ?? identity.fieldName))
374
+ continue;
375
+ const fixed = { ...prior, fieldKey: identity.fieldKey };
376
+ manifest.entries.set(id, fixed);
377
+ corrected.set(id, fixed);
378
+ }
379
+ await mergeFieldRefsRegistry(orgRoot, corrected);
380
+ // Options come from state only (#2568). The manifest's id → label copy has
381
+ // no owning field and is never pruned, so pull does not read it.
382
+ return {
383
+ fieldRefsById: manifest.entries,
384
+ picklistOptionsByField: picklistOptionsByFieldFromState(resources),
385
+ fieldNamesByOwnerRefName: buildFieldNamesByOwnerRefName(manifest.entries),
386
+ };
387
+ }
388
+ /** Back-compat alias — most callers only need the field-id map. */
389
+ export async function loadFieldRefsRegistry(orgRoot) {
390
+ return (await loadPulledFieldRefsContext(orgRoot)).fieldRefsById;
391
+ }
392
+ /** The registry file's own entries (no state-derived refs), as a pull found them before it changed any (edk#2628). */
393
+ export async function readFieldRefsRegistryEntries(orgRoot) {
394
+ return (await readManifest(orgRoot)).entries;
395
+ }
396
+ /**
397
+ * Put one root's registry entries back to `entries` (taken by {@link readFieldRefsRegistryEntries} before the
398
+ * pull), leaving every other root's entries as they are. A failed pull that restores the root's source calls
399
+ * this, so a later pull does not print refs to fields the restored source doesn't have (edk#2628).
400
+ */
401
+ export async function restoreOwnerFieldRefs(orgRoot, ownerRefName, entries) {
402
+ const { entries: current, picklistOptionLabelById } = await readManifest(orgRoot);
403
+ const next = Object.fromEntries([...current].filter(([, identity]) => identity.ownerRefName !== ownerRefName));
404
+ for (const [id, identity] of entries) {
405
+ if (identity.ownerRefName === ownerRefName)
406
+ next[id] = identity;
407
+ }
408
+ const picklistOptionLabels = Object.fromEntries(picklistOptionLabelById);
409
+ const manifest = {
410
+ version: MANIFEST_VERSION,
411
+ entries: next,
412
+ ...(Object.keys(picklistOptionLabels).length > 0 ? { picklistOptionLabels } : {}),
413
+ };
414
+ await mkdir(join(resolve(orgRoot), ".edk-state"), { recursive: true });
415
+ const tmpPath = `${manifestPath(orgRoot)}.tmp-${randomUUID()}`;
416
+ await writeFile(tmpPath, JSON.stringify(manifest, null, 2), "utf8");
417
+ await rename(tmpPath, manifestPath(orgRoot));
418
+ }
419
+ /** Persist new entries — best-effort, never blocks the pull. */
420
+ export async function mergeFieldRefsRegistry(orgRoot, additions, picklistAdditions = new Map()) {
421
+ if (additions.size === 0 && picklistAdditions.size === 0)
422
+ return;
423
+ const path = manifestPath(orgRoot);
424
+ const { entries: existingEntries, picklistOptionLabelById: existingPicklist } = await readManifest(orgRoot);
425
+ const entries = Object.fromEntries(existingEntries);
426
+ for (const [id, identity] of additions)
427
+ entries[id] = identity;
428
+ const picklistOptionLabels = Object.fromEntries(existingPicklist);
429
+ for (const [id, label] of picklistAdditions)
430
+ picklistOptionLabels[id] = label;
431
+ const manifest = {
432
+ version: MANIFEST_VERSION,
433
+ entries,
434
+ ...(Object.keys(picklistOptionLabels).length > 0 ? { picklistOptionLabels } : {}),
435
+ };
436
+ try {
437
+ await mkdir(join(resolve(orgRoot), ".edk-state"), { recursive: true });
438
+ const tmpPath = `${path}.tmp-${randomUUID()}`;
439
+ await writeFile(tmpPath, JSON.stringify(manifest, null, 2), "utf8");
440
+ await rename(tmpPath, path);
441
+ }
442
+ catch {
443
+ // best-effort
444
+ }
445
+ }
446
+ /** Export tf labels that may key import addresses for this root. */
447
+ function ownerTfLabelsForImportLookup(root) {
448
+ const labels = [];
449
+ const seen = new Set();
450
+ const add = (label) => {
451
+ if (label === undefined || label === "" || seen.has(label))
452
+ return;
453
+ seen.add(label);
454
+ labels.push(label);
455
+ };
456
+ // Namespace is the live export/tf label. After authored-dir rebind,
457
+ // tsLabelToTfLabel(eDKCloudBuilder) is e_d_k_cloud_builder and misses
458
+ // import keys like apps_edkcloudbuilder_edkcloudbuilder_title.
459
+ add(root.namespace);
460
+ add(tsLabelToTfLabel(root.refName));
461
+ return labels;
462
+ }
463
+ function importedFieldId(extract, ownerTfLabels, fieldKey, scope) {
464
+ const childPart = tsLabelToTfLabel(fieldKey);
465
+ const fieldTypes = fieldResourceTypes();
466
+ for (const ownerTfLabel of ownerTfLabels) {
467
+ const label = scopedResourceLabel(ownerTfLabel, childPart, scope);
468
+ const imported = fieldTypes.map((type) => extract.importIds.get(`${type}.${label}`)).find((id) => id !== undefined);
469
+ if (imported !== undefined)
470
+ return imported;
471
+ }
472
+ return undefined;
473
+ }
474
+ /** Collect field-id identities from one stream-root extract's import blocks. */
475
+ export function collectFieldRefEntriesFromExtract(root, extract, scope) {
476
+ const out = new Map();
477
+ const ownerTfLabels = ownerTfLabelsForImportLookup(root);
478
+ for (const field of root.fields) {
479
+ const imported = importedFieldId(extract, ownerTfLabels, field.key, scope);
480
+ if (imported !== undefined) {
481
+ const fieldName = "name" in field.field && typeof field.field.name === "string" ? field.field.name : undefined;
482
+ const fieldType = "type" in field.field && typeof field.field.type === "string" ? field.field.type : undefined;
483
+ out.set(importLeafId(imported), {
484
+ ownerRefName: root.refName,
485
+ fieldKey: field.key,
486
+ ...(fieldName !== undefined && fieldName !== "" ? { fieldName } : {}),
487
+ ...(fieldType !== undefined && fieldType !== "" ? { fieldType } : {}),
488
+ });
489
+ }
490
+ }
491
+ return out;
492
+ }
493
+ /** Merge extract-derived field ids into the pull ctx and persist for later runs. */
494
+ export async function rememberPulledFieldIdentities(orgRoot, target, root, extract, scope) {
495
+ const additions = new Map();
496
+ for (const [id, identity] of collectFieldRefEntriesFromExtract(root, extract, scope)) {
497
+ target.set(id, identity);
498
+ additions.set(id, identity);
499
+ }
500
+ await mergeFieldRefsRegistry(orgRoot, additions);
501
+ }
@@ -56,6 +56,7 @@ export interface OrgExtract {
56
56
  readonly aiProviders: readonly OrgIdentity[];
57
57
  readonly groups: readonly OrgIdentity[];
58
58
  readonly phoneProviders: readonly OrgIdentity[];
59
+ readonly voiceAuths: readonly OrgIdentity[];
59
60
  readonly storedFunctions?: readonly OrgStoredFunction[];
60
61
  /** Non-fatal export anomalies; callers report these and continue the pull. */
61
62
  readonly diagnostics?: readonly string[];
@@ -33,6 +33,7 @@ export function extractOrg(doc) {
33
33
  aiProviders: [],
34
34
  groups: [],
35
35
  phoneProviders: [],
36
+ voiceAuths: [],
36
37
  };
37
38
  let apps;
38
39
  let storedFunctions = [];
@@ -100,6 +101,7 @@ export function extractOrg(doc) {
100
101
  aiProviders,
101
102
  groups: withTsLabels(raw.groups),
102
103
  phoneProviders: withTsLabels(raw.phoneProviders),
104
+ voiceAuths: withTsLabels(raw.voiceAuths),
103
105
  storedFunctions,
104
106
  diagnostics,
105
107
  apps,
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generic stream partition: terraform resource blocks → buckets by entity
3
+ * kind, using the registry's TYPE_TO_KIND + matchType. Unknown types and
4
+ * known-but-unimplemented subtypes are reported, never silently dropped.
5
+ */
6
+ import { type StreamPartition } from "../../graph/index.js";
7
+ import type { TfDocument } from "../tf/index.js";
8
+ export declare function partitionStream(doc: TfDocument): StreamPartition;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Generic stream partition: terraform resource blocks → buckets by entity
3
+ * kind, using the registry's TYPE_TO_KIND + matchType. Unknown types and
4
+ * known-but-unimplemented subtypes are reported, never silently dropped.
5
+ */
6
+ import { matchResourceType } from "../../graph/index.js";
7
+ export function partitionStream(doc) {
8
+ const byKind = new Map();
9
+ const familyByKind = new Map();
10
+ const reported = [];
11
+ const unknown = [];
12
+ const allResources = [];
13
+ const push = (map, kind, block) => {
14
+ const list = map.get(kind);
15
+ if (list !== undefined)
16
+ list.push(block);
17
+ else
18
+ map.set(kind, [block]);
19
+ };
20
+ for (const block of doc.blocks) {
21
+ if (block.kind !== "resource")
22
+ continue;
23
+ allResources.push(block);
24
+ const type = block.type ?? "";
25
+ const hit = matchResourceType(type);
26
+ if (hit === undefined) {
27
+ unknown.push(`${type}.${block.label ?? "?"}`);
28
+ continue;
29
+ }
30
+ switch (hit.match) {
31
+ case "extract":
32
+ push(byKind, hit.module.kind, block);
33
+ break;
34
+ case "family":
35
+ push(familyByKind, hit.module.kind, block);
36
+ break;
37
+ case "reported":
38
+ reported.push(`${type}.${block.label ?? "?"}`);
39
+ break;
40
+ }
41
+ }
42
+ return { byKind, familyByKind, reported, unknown, allResources, allBlocks: doc.blocks };
43
+ }
@@ -0,0 +1,89 @@
1
+ /** What a pull wrote to a root's own file (edk#2430). */
2
+ interface PulledSource {
3
+ readonly sha256: string;
4
+ /** ISO time of the pull. */
5
+ readonly pulledAt: string;
6
+ /** The pull run that wrote it; roots one run wrote are the same age however far apart their times are. */
7
+ readonly pullRun?: string;
8
+ /**
9
+ * Hash of each compared group (a relationship pair) built from exactly this text, keyed by group; set by
10
+ * whole-workspace builds that see the file unchanged.
11
+ */
12
+ readonly compared?: Readonly<Record<string, string>>;
13
+ }
14
+ interface PulledAge {
15
+ readonly pulledAt: string;
16
+ readonly pullRun?: string;
17
+ }
18
+ /**
19
+ * A root's compared group (one relationship pair) against its last pull on this machine:
20
+ * - `unchecked`: this build doesn't compare (smart apply's build of the starting commit).
21
+ * - `unreadable`: the pull record or the root file couldn't be read, so it isn't known.
22
+ * - `unrecorded`: no pull on this machine recorded the root.
23
+ * - `unchanged` / `edited`: the group's blocks match, or don't, what was pulled. `fileEdited` says an
24
+ * unchanged group's file was still edited elsewhere.
25
+ * - `unknown`: the file was edited and no build recorded this group as pulled, so whether the edit
26
+ * touched it isn't known.
27
+ */
28
+ export type PulledSourceStatus = {
29
+ readonly state: "unchecked";
30
+ } | {
31
+ readonly state: "unreadable";
32
+ readonly what: "record" | "file";
33
+ } | {
34
+ readonly state: "unrecorded";
35
+ } | (PulledAge & {
36
+ readonly state: "unchanged";
37
+ readonly fileEdited: boolean;
38
+ }) | (PulledAge & {
39
+ readonly state: "edited" | "unknown";
40
+ });
41
+ /**
42
+ * Record that `elementum pull <kind> <refName>` just wrote this root's own file. With `source`, also record
43
+ * what it wrote. `markPulled: false` records only the source, for a file a pull writes as a dependency without
44
+ * that counting as pulling the root.
45
+ */
46
+ export declare function recordPulledRoot(orgRoot: string, kind: string, refName: string, source?: string, options?: {
47
+ readonly markPulled?: boolean;
48
+ }): Promise<void>;
49
+ /** One root's pull record, as {@link readPulledRootRecord} reads it and {@link restorePulledRootRecord} puts it back. */
50
+ export interface PulledRootRecord {
51
+ readonly pulled: boolean;
52
+ readonly source?: PulledSource;
53
+ }
54
+ /**
55
+ * This root's pull record before a pull rewrites it, so a pull that restores the root's source after a failed
56
+ * adoption can restore the record too (edk#2628). `undefined` when the record can't be read.
57
+ */
58
+ export declare function readPulledRootRecord(orgRoot: string, kind: string, refName: string): Promise<PulledRootRecord | undefined>;
59
+ /** Put back a record {@link readPulledRootRecord} read. */
60
+ export declare function restorePulledRootRecord(orgRoot: string, kind: string, refName: string, record: PulledRootRecord): Promise<void>;
61
+ /** Was this root's own file actually written by a real pull, not just cloned/hand-authored? */
62
+ export declare function wasRootPulled(orgRoot: string, kind: string, refName: string): Promise<boolean>;
63
+ export interface PulledSourceInput {
64
+ readonly kind: string;
65
+ readonly refName: string;
66
+ /** The root file's text, or `undefined` when it couldn't be read. */
67
+ readonly source: string | undefined;
68
+ /** A stable hash of each compared group the root builds now, keyed by group. */
69
+ readonly compared: ReadonlyMap<string, string>;
70
+ }
71
+ /** A root against its last pull: `group(key)` is one compared group's status. */
72
+ export interface PulledRootStatus {
73
+ readonly group: (key: string) => PulledSourceStatus;
74
+ }
75
+ /** Pull records aren't compared in this build (smart apply's build of the starting commit). */
76
+ export declare const UNCHECKED_PULL: PulledRootStatus;
77
+ /**
78
+ * Each root's compared groups against what the last pull on this machine wrote. A file that still matches its
79
+ * pull is unchanged throughout; with `record: true` its current group hashes are recorded, so a later edit
80
+ * elsewhere in the file isn't mistaken for a change to them. An edited file is judged per group by those
81
+ * hashes, and a group they don't have (none recorded yet, or a pair that only formed since, such as after
82
+ * the other side was pulled) isn't known.
83
+ */
84
+ export declare function pulledSourceStatuses(orgRoot: string, roots: readonly PulledSourceInput[], options: {
85
+ readonly record: boolean;
86
+ }): Promise<PulledRootStatus[]>;
87
+ /** A stable hash of blocks: key order and block order don't matter. */
88
+ export declare function blocksSha256(blocks: readonly unknown[]): string;
89
+ export {};