@elementumai/edk 0.9.15 → 0.9.16

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 (86) hide show
  1. package/dist-sdk/pull/util.d.ts +0 -15
  2. package/dist-sdk/pull/util.js +44 -5
  3. package/dist-sdk/src/engine/build/context.d.ts +2 -2
  4. package/dist-sdk/src/engine/progress.d.ts +4 -0
  5. package/dist-sdk/src/engine/progress.js +16 -4
  6. package/dist-sdk/src/engine/pull/context.d.ts +5 -0
  7. package/dist-sdk/src/engine/pull/ref-only.d.ts +4 -0
  8. package/dist-sdk/src/engine/pull/ref-only.js +11 -0
  9. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +1 -1
  10. package/dist-sdk/src/entities/access-policy/from-tf.js +47 -12
  11. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +5 -3
  12. package/dist-sdk/src/entities/access-policy/to-tf.js +23 -1
  13. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +27 -2
  14. package/dist-sdk/src/entities/agent/from-tf.d.ts +1 -0
  15. package/dist-sdk/src/entities/agent/from-tf.js +18 -3
  16. package/dist-sdk/src/entities/agent/to-tf.js +14 -0
  17. package/dist-sdk/src/entities/agent/tools.d.ts +79 -2
  18. package/dist-sdk/src/entities/agent/tools.js +2 -1
  19. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +11 -0
  20. package/dist-sdk/src/entities/agent-tool/from-tf.js +45 -3
  21. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +7 -0
  22. package/dist-sdk/src/entities/agent-tool/to-tf.js +72 -20
  23. package/dist-sdk/src/entities/app/module.js +8 -0
  24. package/dist-sdk/src/entities/app/to-tf.js +1 -0
  25. package/dist-sdk/src/entities/automation/build.d.ts +12 -5
  26. package/dist-sdk/src/entities/automation/build.js +30 -11
  27. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +4 -0
  28. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +108 -23
  29. package/dist-sdk/src/entities/automation/from-tf.d.ts +73 -5
  30. package/dist-sdk/src/entities/automation/from-tf.js +255 -30
  31. package/dist-sdk/src/entities/automation/index.d.ts +2 -1
  32. package/dist-sdk/src/entities/automation/index.js +2 -1
  33. package/dist-sdk/src/entities/automation/module.js +6 -7
  34. package/dist-sdk/src/entities/automation/output-field-types.d.ts +5 -0
  35. package/dist-sdk/src/entities/automation/output-field-types.js +12 -0
  36. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +20 -0
  37. package/dist-sdk/src/entities/automation/output-key-wire-name.js +22 -0
  38. package/dist-sdk/src/entities/automation/script-code.js +13 -4
  39. package/dist-sdk/src/entities/automation/tasks.d.ts +41 -5
  40. package/dist-sdk/src/entities/automation/tasks.js +31 -9
  41. package/dist-sdk/src/entities/automation/to-tf.d.ts +0 -2
  42. package/dist-sdk/src/entities/automation/to-tf.js +85 -36
  43. package/dist-sdk/src/entities/automation/triggers.d.ts +5 -2
  44. package/dist-sdk/src/entities/automation/types.d.ts +2 -0
  45. package/dist-sdk/src/entities/automation/types.js +5 -0
  46. package/dist-sdk/src/entities/automation/workflow-output.d.ts +35 -0
  47. package/dist-sdk/src/entities/automation/workflow-output.js +53 -0
  48. package/dist-sdk/src/entities/dashboard/from-tf.js +1 -1
  49. package/dist-sdk/src/entities/dashboard/to-tf.js +1 -0
  50. package/dist-sdk/src/entities/element/build.d.ts +11 -8
  51. package/dist-sdk/src/entities/element/build.js +14 -5
  52. package/dist-sdk/src/entities/element/locals.d.ts +2 -2
  53. package/dist-sdk/src/entities/element/locals.js +6 -2
  54. package/dist-sdk/src/entities/element/system-fields.d.ts +4 -0
  55. package/dist-sdk/src/entities/element/system-fields.js +12 -0
  56. package/dist-sdk/src/entities/element/to-tf.js +1 -0
  57. package/dist-sdk/src/entities/field/from-tf.js +47 -1
  58. package/dist-sdk/src/entities/relationship/from-tf.js +2 -2
  59. package/dist-sdk/src/entities/relationship/to-tf.js +1 -0
  60. package/dist-sdk/src/entities/search-table/from-tf.js +6 -6
  61. package/dist-sdk/src/entities/search-table/module.js +2 -2
  62. package/dist-sdk/src/entities/search-table/types.d.ts +1 -0
  63. package/dist-sdk/src/entities/search-table/types.js +1 -0
  64. package/dist-sdk/src/entities/shared/filters/ast.d.ts +2 -0
  65. package/dist-sdk/src/entities/shared/filters/index.d.ts +12 -2
  66. package/dist-sdk/src/entities/shared/filters/index.js +17 -1
  67. package/dist-sdk/src/entities/shared/filters/print.js +25 -11
  68. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +1 -1
  69. package/dist-sdk/src/entities/shared/filters/tf-codec.js +39 -7
  70. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +18 -2
  71. package/dist-sdk/src/entities/skill/module.js +9 -0
  72. package/dist-sdk/src/entities/skill/to-tf.js +18 -0
  73. package/dist-sdk/src/entities/skill/tools.d.ts +18 -4
  74. package/dist-sdk/src/entities/skill/tools.js +1 -1
  75. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +12 -0
  76. package/dist-sdk/src/entities/skill-tool/from-tf.js +58 -3
  77. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +6 -0
  78. package/dist-sdk/src/entities/skill-tool/to-tf.js +78 -43
  79. package/dist-sdk/src/entities/view/from-tf.js +30 -9
  80. package/dist-sdk/src/entities/view/to-tf.js +18 -10
  81. package/dist-sdk/src/entities/widget/from-tf.js +1 -1
  82. package/dist-sdk/src/entities/widget/to-tf.js +1 -0
  83. package/dist-sdk/src/graph/index.d.ts +13 -0
  84. package/dist-sdk/src/graph/index.js +5 -0
  85. package/dist-sdk/src/graph/types.d.ts +10 -0
  86. package/package.json +2 -1
@@ -156,6 +156,32 @@ function filterValueToWire(value) {
156
156
  ...(value.required !== undefined && { required: value.required }),
157
157
  };
158
158
  }
159
+ /** Validate every supplied identity; a malformed canonical value must not fall back to a legacy alias. */
160
+ function fieldOperandToTf(raw, codec) {
161
+ let identity;
162
+ for (const key of ["value", "field", "field_id"]) {
163
+ if (!Object.hasOwn(raw, key))
164
+ continue;
165
+ const candidate = raw[key];
166
+ if (candidate === null || candidate === undefined || Array.isArray(candidate) ||
167
+ (typeof candidate !== "string" && typeof candidate !== "object") ||
168
+ (typeof candidate === "string" && candidate.trim() === "")) {
169
+ throw new EntityTfError(`FIELD.${key} must be a non-empty field identity`);
170
+ }
171
+ const resolved = codec.field(candidate);
172
+ if (!(typeof resolved === "string" && resolved.trim() !== "") && !isTfExpr(resolved)) {
173
+ throw new EntityTfError(`FIELD.${key} must resolve to a non-empty field identity`);
174
+ }
175
+ // Compare resolved identities, not optional metadata on authored tokens.
176
+ if (identity !== undefined && JSON.stringify(identity) !== JSON.stringify(resolved)) {
177
+ throw new EntityTfError("FIELD value and legacy aliases disagree");
178
+ }
179
+ identity = resolved;
180
+ }
181
+ if (identity === undefined)
182
+ throw new EntityTfError("FIELD requires value (or legacy field)");
183
+ return identity;
184
+ }
159
185
  function wireFilterValueToTf(value, field, codec, role = "value", operator) {
160
186
  if (value === null || typeof value !== "object" || isTfExpr(value)) {
161
187
  throw new EntityTfError("invalid wire filter value");
@@ -191,12 +217,15 @@ function wireFilterValueToTf(value, field, codec, role = "value", operator) {
191
217
  };
192
218
  }
193
219
  if (type === "field") {
194
- const fieldValue = raw.value ?? raw.field ?? raw.field_id;
195
- if (fieldValue === undefined)
196
- throw new EntityTfError("field filter value requires value");
220
+ // left_value retains only identity. Validate every accepted spelling and
221
+ // alias before conversion can discard metadata or coerce its value.
222
+ if (role === "left" && ([raw.aspect, raw.aspect_id].some((aspect) => aspect != null && aspect !== "") ||
223
+ [raw.treatNullAsEquals, raw.treat_null_as_equals].some((flag) => flag != null && flag !== true))) {
224
+ throw new EntityTfError("filter FIELD left operand cannot represent aspect or nondefault treatNullAsEquals");
225
+ }
197
226
  return {
198
227
  type,
199
- field_id: codec.field(fieldValue),
228
+ field_id: fieldOperandToTf(raw, codec),
200
229
  ...((raw.aspect ?? raw.aspect_id) !== undefined && { aspect_id: (raw.aspect ?? raw.aspect_id) }),
201
230
  ...((raw.treatNullAsEquals ?? raw.treat_null_as_equals) !== undefined && { treat_null_as_equals: Boolean(raw.treatNullAsEquals ?? raw.treat_null_as_equals) }),
202
231
  };
@@ -330,7 +359,7 @@ export function filterTfToWire(value) {
330
359
  ...("field_id" in filter && filter.field_id !== undefined && { field: filter.field_id }),
331
360
  ...("left_value" in filter && filter.left_value !== undefined && {
332
361
  leftValue: filter.left_value.field_id !== undefined
333
- ? { type: "FIELD", field: filter.left_value.field_id }
362
+ ? { type: "FIELD", value: filter.left_value.field_id }
334
363
  : { type: "REFERENCE", value: tfReferenceToWire(filter.left_value.value_reference), realType: (filter.left_value.real_type ?? "text").toUpperCase() },
335
364
  }),
336
365
  };
@@ -435,7 +464,7 @@ export function filterFromTf(value, codec, fieldScope = "app") {
435
464
  leftValue: {
436
465
  type: "REFERENCE",
437
466
  realType: codec.referenceType?.(filter.left_value.value_reference, fallback) ?? fallback,
438
- value: codec.reference(filter.left_value.value_reference),
467
+ value: codec.reference(filter.left_value.value_reference, "left"),
439
468
  },
440
469
  };
441
470
  }
@@ -487,6 +516,9 @@ export function filterFromTf(value, codec, fieldScope = "app") {
487
516
  function filterValueFromTf(value, codec, fieldScope) {
488
517
  const type = value.type.toLowerCase();
489
518
  const literal = codec.literal?.(value.value, type) ?? value.value;
519
+ if (["text", "uuid", "number", "boolean", "date", "datetime"].includes(type) && typeof literal === "object" && literal !== null) {
520
+ throw new EntityTfError(`${type} filter literal contains an unresolved expression or object`);
521
+ }
490
522
  switch (type) {
491
523
  case "null": return { type: "NULL" };
492
524
  case "current_user": {
@@ -500,7 +532,7 @@ function filterValueFromTf(value, codec, fieldScope) {
500
532
  return {
501
533
  type: "REFERENCE",
502
534
  realType: codec.referenceType?.(value.value_reference, fallback) ?? fallback,
503
- value: codec.reference(value.value_reference),
535
+ value: codec.reference(value.value_reference, "value"),
504
536
  ...(value.multiple === true && { multiple: true }),
505
537
  };
506
538
  }
@@ -13,6 +13,7 @@
13
13
  * Unresolved field and automation platform ids print as `unsafePlatformId(...)`.
14
14
  */
15
15
  import { quote } from "../../../../pull/util.js";
16
+ import { sanitizeName, tfLabelToTsLabel } from "../../../engine/tf/index.js";
16
17
  import { sdkPackageSpecifier } from "../../../internal/sdk-package.js";
17
18
  import { safeIdentifier } from "../../../internal/safe-identifier.js";
18
19
  const TOOL_FACTORY_BY_TYPE_KEY = {
@@ -51,6 +52,7 @@ export function printSkillV2(skill, opts) {
51
52
  ]),
52
53
  ].sort();
53
54
  const usesUnsafePlatformId = skill.tools.some((tool) => tool.automationRawId !== undefined ||
55
+ tool.searchTableRawId !== undefined ||
54
56
  tool.target?.kind === "unresolved" ||
55
57
  tool.fields?.some((field) => field.rawFieldId !== undefined && !opts.foreignFieldRefsById?.has(field.rawFieldId)) === true ||
56
58
  (tool.filterVariables?.length ?? 0) > 0);
@@ -197,7 +199,14 @@ function printTool(tool, appRefName, crossOrg, foreignAppRefNames, foreignElemen
197
199
  if (tool.typeKey === "automation") {
198
200
  lines.push(tool.automationRawId !== undefined
199
201
  ? ` automation: unsafePlatformId(${quote(tool.automationRawId)}),`
200
- : ` automation: catalog.${appRefName}.automations${safeMember(tool.automationKey ?? "")},`);
202
+ : tool.automationForeignName !== undefined
203
+ ? // A cross-app/element automation reference - no typed catalog
204
+ // token for a foreign automation exists yet (mirrors the raw
205
+ // foreign-literal fallback used for an unresolved search table
206
+ // elsewhere in this printer). refName is a best-effort guess
207
+ // from the display name, not a verified catalog key.
208
+ ` automation: { __kind: "automation", refName: ${quote(tfLabelToTsLabel(sanitizeName(tool.automationForeignName)))}, name: ${quote(tool.automationForeignName)}, parent: ${quote(tool.automationForeignParentRefName ?? tool.automationForeignParentName ?? "")}, parentName: ${quote(tool.automationForeignParentName ?? "")}, parentKind: ${quote(tool.automationForeignParentKind ?? "app")} } /* TODO: foreign automation reference - no catalog token available; verify the parent/refName before deploying */,`
209
+ : ` automation: catalog.${appRefName}.automations${safeMember(tool.automationKey ?? "")},`);
201
210
  const inputs = tool.inputs ?? [];
202
211
  if (inputs.length > 0) {
203
212
  lines.push(" inputs: {");
@@ -277,7 +286,14 @@ function printTool(tool, appRefName, crossOrg, foreignAppRefNames, foreignElemen
277
286
  lines.push(` limit: ${tool.limit},`);
278
287
  }
279
288
  if (tool.typeKey === "searchTable") {
280
- if (tool.searchTableKey !== undefined) {
289
+ if (tool.searchTableRawId !== undefined) {
290
+ // The search table itself has no portable lookup at all (not even a
291
+ // name) - round-trip the raw platform id instead of dropping a live
292
+ // tool, the same unsafePlatformId() escape used elsewhere in this
293
+ // printer (mirrors print-agent-v2.ts's identical case, #1099).
294
+ lines.push(` table: unsafePlatformId(${quote(tool.searchTableRawId)}), // TODO: this search table has no portable lookup (no name, no local resource) - verify/replace with a real table reference if one becomes available`);
295
+ }
296
+ else if (tool.searchTableKey !== undefined) {
281
297
  lines.push(` table: catalog.${targetRefName ?? appRefName}.searchTables${safeMember(tool.searchTableKey)},`);
282
298
  }
283
299
  else {
@@ -109,6 +109,15 @@ export const skillModule = {
109
109
  ...(ctx.activeAutomationRefNames !== undefined && { activeAutomationRefNames: ctx.activeAutomationRefNames }),
110
110
  }),
111
111
  },
112
+ roundtrip: {
113
+ referenceAliases: [],
114
+ stringSuffixAliases: [
115
+ {
116
+ attribute: "instructions",
117
+ suffix: "\n\n## Lookup Notes\n\n- No lookup tools are required.",
118
+ },
119
+ ],
120
+ },
112
121
  catalog: {
113
122
  participates: true,
114
123
  refKind: "skill",
@@ -126,6 +126,24 @@ export function skillToTf(entity, ctx) {
126
126
  ctx.orgRefs.rawLookup("elementum_search_table", label, searchTableName, { table_id: tableId });
127
127
  return expr.dataAttr("elementum_search_table", label, ["id"]);
128
128
  },
129
+ foreignAutomationRef: (tfLabel, parentKind, _parentRefName, parentName, automationName) => {
130
+ // sanitizeName(parentName), not the refName: ei labels the export
131
+ // from the owner's DISPLAY name, and sanitizeName doesn't split a
132
+ // capital before a digit the way reversing a refName would need
133
+ // (same lesson as #1095's table-owned search-table label fix).
134
+ const parentType = parentKind === "element" ? "elementum_element" : "elementum_app";
135
+ const parentLabel = sanitizeName(parentName);
136
+ // ei labels a foreign automation data block `${lookup.Label}_${SanitizeName(name)}`
137
+ // (imports.go), not the bare automation label - a bare label both
138
+ // diverges from the pulled state (plan diff) and collides in
139
+ // ctx.orgRefs.rawLookup's `${type}.${label}` registry key across apps.
140
+ const automationLabel = `${parentLabel}_${tfLabel}`;
141
+ ctx.orgRefs.rawLookup(parentType, parentLabel, parentName);
142
+ ctx.orgRefs.rawLookup("elementum_automation", automationLabel, automationName, {
143
+ app_id: expr.dataAttr(parentType, parentLabel, ["id"]),
144
+ });
145
+ return expr.dataAttr("elementum_automation", automationLabel, ["id"]);
146
+ },
129
147
  tableColumnRef: (tableRefName, tableName, columnName) => {
130
148
  const sibling = ctx.workspaceAspects?.get(workspaceAspectKey("table", tableRefName));
131
149
  if (sibling !== undefined) {
@@ -131,11 +131,25 @@ export type SkillSearchRecordsToolOptions<Target extends ToolTarget = ToolTarget
131
131
  export declare function skillSearchRecords<Target extends ToolTarget>(opts: SkillSearchRecordsToolOptions<Target>): SkillTool;
132
132
  /** A search-table tool over an element-owned index targets its owning ELEMENT. */
133
133
  export type SkillSearchTableToolV2<Target extends ElementCatalogRef = ElementCatalogRef> = CommonToolOptions & TargetOption<Target> & {
134
- readonly table: SearchTableRef & {
134
+ /**
135
+ * The search table itself, or (pull-fidelity fallback, mirroring
136
+ * `fieldId`/`id` elsewhere in this file) a raw platform id when the
137
+ * search table's own identity has no portable lookup at all - the
138
+ * target aspect can still resolve fine even though the search table
139
+ * doesn't (live-confirmed on sanofi/eu, mirrors agent-tool's identical
140
+ * fallback, #1099/#1176).
141
+ */
142
+ readonly table: (SearchTableRef & {
135
143
  readonly __variant?: "snowflake" | "linked";
136
- };
144
+ }) | UnsafePlatformId;
137
145
  readonly queryDescription: string;
138
- readonly fields: readonly SkillSearchFieldInput<Target>[];
146
+ /**
147
+ * Optional, like `skillCreateRecord`/`skillUpdateRecord`'s `fields?` — a
148
+ * pulled tool whose search table has no portable lookup also has no
149
+ * resolvable field list to print (live-confirmed on sanofi/eu: a raw-id
150
+ * `table` with zero fields), so this can't be required (#1180 review).
151
+ */
152
+ readonly fields?: readonly SkillSearchFieldInput<Target>[];
139
153
  /** Maximum number of records the search returns. */
140
154
  readonly limit?: number;
141
155
  readonly filterVariables?: readonly SkillFilterVariable[];
@@ -146,7 +160,7 @@ export type SkillTableOwnedSearchTableToolV2 = CommonToolOptions & {
146
160
  readonly element?: never;
147
161
  readonly table: TableOwnedSearchTableRef;
148
162
  readonly queryDescription: string;
149
- readonly fields: readonly SkillTableSearchFieldInput[];
163
+ readonly fields?: readonly SkillTableSearchFieldInput[];
150
164
  readonly limit?: number;
151
165
  readonly filterVariables?: readonly SkillFilterVariable[];
152
166
  };
@@ -119,7 +119,7 @@ export function skillSearchTable(opts) {
119
119
  ...(target !== undefined ? { aspectId: target } : {}),
120
120
  searchTableId: opts.table,
121
121
  queryDescription: opts.queryDescription,
122
- fields: opts.fields.map(searchFieldToken),
122
+ fields: (opts.fields ?? []).map(searchFieldToken),
123
123
  ...(opts.limit !== undefined ? { limit: opts.limit } : {}),
124
124
  ...(opts.filterVariables !== undefined ? { filterVariables: opts.filterVariables.map(filterVariableToken) } : {}),
125
125
  },
@@ -84,6 +84,13 @@ export interface NormalizedSkillTool {
84
84
  * (out-of-stream automation). Printed as `unsafePlatformId(...)`.
85
85
  */
86
86
  readonly automationRawId?: string;
87
+ /** Display name of a foreign (cross-app/element) automation_id reference. */
88
+ readonly automationForeignName?: string;
89
+ /** Display name of the foreign automation's owning app/element. */
90
+ readonly automationForeignParentName?: string;
91
+ /** Canonical catalog refName of the owning app/element, when known. */
92
+ readonly automationForeignParentRefName?: string;
93
+ readonly automationForeignParentKind?: "app" | "element";
87
94
  readonly searchTableKey?: string;
88
95
  /** Foreign table lookup name when search_table_id is data.elementum_search_table. */
89
96
  readonly searchTableName?: string;
@@ -91,6 +98,11 @@ export interface NormalizedSkillTool {
91
98
  readonly searchTableOwnerName?: string;
92
99
  /** Canonical catalog refName of the owning organization table, when known. */
93
100
  readonly searchTableOwnerRefName?: string;
101
+ /**
102
+ * Bare `search_table_id` UUID when the search table itself has no
103
+ * portable lookup at all (not even a name). Printed as `unsafePlatformId(...)`.
104
+ */
105
+ readonly searchTableRawId?: string;
94
106
  readonly inputs?: readonly {
95
107
  readonly parameterName: string;
96
108
  readonly displayName?: string;
@@ -7,7 +7,7 @@
7
7
  import { EntityTfError } from "../../engine/entity.js";
8
8
  import { dataRefLabel, optionalString, resourceRefLabel } from "../../engine/pull/context.js";
9
9
  import { isTfExpr, sanitizeName, scopedLabelToBagKey, tfLabelToTsLabel } from "../../engine/tf/index.js";
10
- import { dataAspectDisplayName, dataAspectRefName } from "../shared/aspect-target-from-tf.js";
10
+ import { dataAspectDisplayName, dataAspectRefName, resolveDataAspectTarget } from "../shared/aspect-target-from-tf.js";
11
11
  import { resolveToolFieldKey } from "../shared/tool-field-ref.js";
12
12
  import { KNOWN_SKILL_TOOL_ATTRS, SKILL_TOOL_SUB_TYPE_BY_RESOURCE, UNAUTHORABLE_SKILL_TOOL_ATTRS, } from "./types.js";
13
13
  /**
@@ -128,7 +128,42 @@ export function skillToolFromTf(block, owner, ctx) {
128
128
  ? automation.label
129
129
  : undefined;
130
130
  const automationRawId = typeof automation === "string" && automation !== "" ? automation : undefined;
131
- if (automationLabel === undefined && automationRawId === undefined) {
131
+ // A foreign (cross-app/element) automation reference - the resource-attr
132
+ // branch above only recognizes a same-workspace managed automation.
133
+ // Live-confirmed on sanofi/eu: "PO Change Request" skill's
134
+ // "Change_Address" tool targets an automation owned by a *different*
135
+ // app. Resolve the owner the same way target_id's foreignApp/
136
+ // foreignElement branches do above (#1177).
137
+ let automationForeignName;
138
+ let automationForeignParentName;
139
+ let automationForeignParentRefName;
140
+ let automationForeignParentKind;
141
+ if (automationLabel === undefined &&
142
+ automationRawId === undefined &&
143
+ isTfExpr(automation) &&
144
+ automation.$expr === "data-attr" &&
145
+ automation.type === "elementum_automation") {
146
+ const automationName = ctx.dataName(automation.type, automation.label);
147
+ if (automationName === undefined) {
148
+ ctx.diagnostics.push(`${where}: automation_id references data.${automation.type}.${automation.label} but the stream has no matching name lookup; skipped`);
149
+ return undefined;
150
+ }
151
+ const ownerAttrs = ctx.dataAttrs?.(automation.type, automation.label);
152
+ const appOwnerLabel = dataRefLabel(ownerAttrs?.["app_id"], "elementum_app");
153
+ const elementOwnerLabel = appOwnerLabel === undefined ? dataRefLabel(ownerAttrs?.["app_id"], "elementum_element") : undefined;
154
+ const ownerKind = appOwnerLabel !== undefined ? "app" : elementOwnerLabel !== undefined ? "element" : undefined;
155
+ const ownerLabel = appOwnerLabel ?? elementOwnerLabel;
156
+ if (ownerKind === undefined || ownerLabel === undefined) {
157
+ ctx.diagnostics.push(`${where}: automation_id references data.${automation.type}.${automation.label}, whose owner app_id isn't a recognized app/element reference; skipped`);
158
+ return undefined;
159
+ }
160
+ const resolvedOwner = resolveDataAspectTarget(ctx, ownerKind, ownerLabel);
161
+ automationForeignName = automationName;
162
+ automationForeignParentName = resolvedOwner.name;
163
+ automationForeignParentRefName = resolvedOwner.key;
164
+ automationForeignParentKind = ownerKind;
165
+ }
166
+ if (automationLabel === undefined && automationRawId === undefined && automationForeignName === undefined) {
132
167
  ctx.diagnostics.push(`${where}: automation_id is not an automation/workflow_publish resource reference; skipped`);
133
168
  return undefined;
134
169
  }
@@ -168,7 +203,14 @@ export function skillToolFromTf(block, owner, ctx) {
168
203
  ...common,
169
204
  ...(automationRawId !== undefined
170
205
  ? { automationRawId }
171
- : { automationKey: scopedLabelToBagKey(owner.ownerTfLabel, automationLabel) }),
206
+ : automationForeignName !== undefined
207
+ ? {
208
+ automationForeignName,
209
+ ...(automationForeignParentName !== undefined && { automationForeignParentName }),
210
+ ...(automationForeignParentRefName !== undefined && { automationForeignParentRefName }),
211
+ ...(automationForeignParentKind !== undefined && { automationForeignParentKind }),
212
+ }
213
+ : { automationKey: scopedLabelToBagKey(owner.ownerTfLabel, automationLabel) }),
172
214
  ...(inputs.length > 0 && { inputs }),
173
215
  ...(outputs.length > 0 && { outputs }),
174
216
  };
@@ -178,6 +220,7 @@ export function skillToolFromTf(block, owner, ctx) {
178
220
  let tableOwnedSearchTable = false;
179
221
  let searchTableOwnerName;
180
222
  let searchTableOwnerRefName;
223
+ let searchTableRawId;
181
224
  if (subType.typeKey === "searchTable") {
182
225
  const value = attrs["search_table_id"];
183
226
  const resourceTypes = ["elementum_ai_search_table", "elementum_linked_ai_search_table", "elementum_table_search_table"];
@@ -213,6 +256,17 @@ export function skillToolFromTf(block, owner, ctx) {
213
256
  return undefined;
214
257
  }
215
258
  }
259
+ else if (typeof value === "string" && value !== "") {
260
+ // The search table's own identity couldn't be resolved to a name or a
261
+ // local resource at all - ei's own export already fell back to a raw
262
+ // UUID here (live-confirmed on sanofi/eu: the "PO Change Request"
263
+ // skill's "New_AddressSearch" tool). Round-trip it as
264
+ // unsafePlatformId() instead of silently dropping a live tool - a
265
+ // rebuild-and-plan on the dropped tool would otherwise DESTROY it.
266
+ // Mirrors agent-tool/from-tf.ts's identical fallback (#1099).
267
+ ctx.diagnostics.push(`${where}: search_table_id could not be resolved to a portable reference ("${value}"); kept as unresolved`);
268
+ searchTableRawId = value;
269
+ }
216
270
  else {
217
271
  ctx.diagnostics.push(`${where}: search_table_id is not a supported search-table resource reference; skipped`);
218
272
  return undefined;
@@ -393,6 +447,7 @@ export function skillToolFromTf(block, owner, ctx) {
393
447
  ...(searchTableName !== undefined && { searchTableName }),
394
448
  ...(searchTableOwnerName !== undefined && { searchTableOwnerName }),
395
449
  ...(searchTableOwnerRefName !== undefined && { searchTableOwnerRefName }),
450
+ ...(searchTableRawId !== undefined && { searchTableRawId }),
396
451
  ...(typeof attrs["result_limit"] === "number" && { limit: attrs["result_limit"] }),
397
452
  ...(handleDescription !== undefined && { handleDescription }),
398
453
  };
@@ -36,6 +36,12 @@ export interface SkillToolToTfContext {
36
36
  serviceAccountRef?(tfLabel: string, name: string): TfValue | void;
37
37
  /** Records a table-owned search-table data lookup for a pulled public ref. */
38
38
  tableSearchTableRef?(tfLabel: string, tableRefName: string, tableName: string, searchTableName: string): TfValue | void;
39
+ /**
40
+ * Records a cross-app/element automation data lookup (a `runAutomation`
41
+ * skill/agent tool targeting an automation this workspace doesn't own),
42
+ * returning the resolved `.id`. Live-confirmed on sanofi/eu (#1177).
43
+ */
44
+ foreignAutomationRef?(tfLabel: string, parentKind: "app" | "element", parentRefName: string, parentName: string, automationName: string): TfValue | void;
39
45
  /** Resolves a table-column token for a table-owned search tool. */
40
46
  tableColumnRef?(tableRefName: string, tableName: string, columnName: string): TfValue | void;
41
47
  /** App-local automations whose publish resource will be emitted in this build. */
@@ -206,13 +206,32 @@ export function skillToolToTf(entry, ctx) {
206
206
  return undefined;
207
207
  }
208
208
  if (automation?.parent !== undefined && automation.parent !== ctx.appRefName) {
209
- ctx.diagnostics.push(`${where}: cross-app automation targets are not supported; dropped`);
210
- return undefined;
209
+ // A genuinely foreign (cross-app/element) automation reference -
210
+ // live-confirmed on sanofi/eu: "PO Change Request" skill's
211
+ // "Change_Address" tool. Resolve it the same way a foreign search
212
+ // table/field does elsewhere in this file, via a caller-supplied
213
+ // lookup callback (#1177).
214
+ if (ctx.foreignAutomationRef !== undefined &&
215
+ typeof automation.parentName === "string" && automation.parentName !== "" &&
216
+ typeof automation.name === "string" && automation.name !== "") {
217
+ const lookup = ctx.foreignAutomationRef(automationTfLabel, automation.parentKind ?? "app", automation.parent, automation.parentName, automation.name);
218
+ if (lookup === undefined) {
219
+ ctx.diagnostics.push(`${where}: foreign automation lookup could not be materialized; dropped`);
220
+ return undefined;
221
+ }
222
+ attrs["automation_id"] = lookup;
223
+ }
224
+ else {
225
+ ctx.diagnostics.push(`${where}: cross-app automation targets are not supported; dropped`);
226
+ return undefined;
227
+ }
228
+ }
229
+ else {
230
+ const automationRefName = typeof automation?.refName === "string" && automation.refName !== ""
231
+ ? automation.refName
232
+ : automationTfLabel;
233
+ attrs["automation_id"] = automationRunTargetResourceAttr(ctx.appTfLabel, automationRefName, ctx.activeAutomationRefNames);
211
234
  }
212
- const automationRefName = typeof automation?.refName === "string" && automation.refName !== ""
213
- ? automation.refName
214
- : automationTfLabel;
215
- attrs["automation_id"] = automationRunTargetResourceAttr(ctx.appTfLabel, automationRefName, ctx.activeAutomationRefNames);
216
235
  }
217
236
  if (inner.inputs !== undefined && inner.inputs.length > 0) {
218
237
  attrs["inputs"] = inner.inputs.map((input) => ({
@@ -259,56 +278,72 @@ export function skillToolToTf(entry, ctx) {
259
278
  attrs["target_id"] = "";
260
279
  if (typeKey === "searchTable") {
261
280
  const table = inner.searchTableId;
262
- if (table?.refName === undefined || table.refName === "") {
263
- ctx.diagnostics.push(`${where}: no search table token; dropped`);
264
- return undefined;
281
+ // The search table itself (not its owner - that's the __variant/parent
282
+ // fallback below) has no portable lookup at all: unsafePlatformId()
283
+ // round-trips it as a raw id directly, before the refName check below,
284
+ // which would otherwise treat this escape as "no search table token"
285
+ // and drop a live tool. Scoped to a token with NO refName - one that
286
+ // still carries a refName has a real portable lookup available and
287
+ // must go through the normal parent-mismatch check + resolution below,
288
+ // not be short-circuited here just because it also happens to carry an
289
+ // __unsafe marker. Independent of whether a target aspect resolved -
290
+ // the target can still be fine even though the search table isn't
291
+ // (mirrors agent-tool/to-tf.ts's identical fix, #1099/#1176).
292
+ if (table?.__unsafe === "platformId" && table.refName === undefined && typeof table.value === "string" && table.value !== "") {
293
+ attrs["search_table_id"] = table.value;
265
294
  }
266
- if (target !== undefined && table.parent !== undefined && table.parent !== "" && table.parent !== target.refName) {
267
- ctx.diagnostics.push(`${where}: search table belongs to "${table.parent}", not target "${target.refName}"; dropped`);
295
+ else if (table?.refName === undefined || table.refName === "") {
296
+ ctx.diagnostics.push(`${where}: no search table token; dropped`);
268
297
  return undefined;
269
298
  }
270
- if (target === undefined) {
271
- if (table.__variant !== "table") {
272
- ctx.diagnostics.push(`${where}: missing target aspect is only valid for a table-owned search table; dropped`);
299
+ else {
300
+ if (target !== undefined && table.parent !== undefined && table.parent !== "" && table.parent !== target.refName) {
301
+ ctx.diagnostics.push(`${where}: search table belongs to "${table.parent}", not target "${target.refName}"; dropped`);
273
302
  return undefined;
274
303
  }
275
- const workspaceTable = table.parent === undefined
276
- ? undefined
277
- : ctx.workspaceAspects?.get(workspaceAspectKey("table", table.parent));
278
- if (workspaceTable !== undefined) {
279
- attrs["search_table_id"] = expr.resourceAttr("elementum_table_search_table", `tables_${workspaceTable.tfLabel}_${tsLabelToTfLabel(table.refName)}`, ["id"]);
280
- }
281
- else if (ctx.tableSearchTableRef !== undefined && table.parent !== undefined && table.parent !== "") {
282
- const lookup = ctx.tableSearchTableRef(`${sanitizeName(table.parent)}_${sanitizeName(table.name ?? table.refName)}`, table.parent, table.parentName ?? table.parent, table.name ?? table.refName);
283
- if (lookup !== undefined)
284
- attrs["search_table_id"] = lookup;
304
+ if (target === undefined) {
305
+ if (table.__variant !== "table") {
306
+ ctx.diagnostics.push(`${where}: missing target aspect is only valid for a table-owned search table; dropped`);
307
+ return undefined;
308
+ }
309
+ const workspaceTable = table.parent === undefined
310
+ ? undefined
311
+ : ctx.workspaceAspects?.get(workspaceAspectKey("table", table.parent));
312
+ if (workspaceTable !== undefined) {
313
+ attrs["search_table_id"] = expr.resourceAttr("elementum_table_search_table", `tables_${workspaceTable.tfLabel}_${tsLabelToTfLabel(table.refName)}`, ["id"]);
314
+ }
315
+ else if (ctx.tableSearchTableRef !== undefined && table.parent !== undefined && table.parent !== "") {
316
+ const lookup = ctx.tableSearchTableRef(`${sanitizeName(table.parent)}_${sanitizeName(table.name ?? table.refName)}`, table.parent, table.parentName ?? table.parent, table.name ?? table.refName);
317
+ if (lookup !== undefined)
318
+ attrs["search_table_id"] = lookup;
319
+ else {
320
+ ctx.diagnostics.push(`${where}: table-owned search table lookup could not be materialized; dropped`);
321
+ return undefined;
322
+ }
323
+ }
324
+ else if (table.__unsafe === "platformId" && typeof table.value === "string" && table.value !== "") {
325
+ attrs["search_table_id"] = table.value;
326
+ }
285
327
  else {
286
- ctx.diagnostics.push(`${where}: table-owned search table lookup could not be materialized; dropped`);
328
+ ctx.diagnostics.push(`${where}: table-owned search table has no portable lookup; dropped`);
287
329
  return undefined;
288
330
  }
289
331
  }
290
- else if (table.__unsafe === "platformId" && typeof table.value === "string" && table.value !== "") {
291
- attrs["search_table_id"] = table.value;
332
+ else if (target.localTfLabel === undefined) {
333
+ const targetName = aspectId?.name ?? aspectId?.refName ?? table.parent;
334
+ const lookupLabel = sanitizeName(`${targetName}_${table.name ?? table.refName}`);
335
+ ctx.foreignFieldRef("elementum_search_table", lookupLabel, target.value, table.name ?? table.refName);
336
+ attrs["search_table_id"] = expr.dataAttr("elementum_search_table", lookupLabel, ["id"]);
292
337
  }
293
338
  else {
294
- ctx.diagnostics.push(`${where}: table-owned search table has no portable lookup; dropped`);
295
- return undefined;
339
+ const resourceType = table.__variant === "linked"
340
+ ? "elementum_linked_ai_search_table"
341
+ : table.__variant === "table"
342
+ ? "elementum_table_search_table"
343
+ : "elementum_ai_search_table";
344
+ attrs["search_table_id"] = expr.resourceAttr(resourceType, scopedResourceLabel(target.localTfLabel, tsLabelToTfLabel(table.refName), target.kind === "element" ? "element" : "app"), ["id"]);
296
345
  }
297
346
  }
298
- else if (target.localTfLabel === undefined) {
299
- const targetName = aspectId?.name ?? aspectId?.refName ?? table.parent;
300
- const lookupLabel = sanitizeName(`${targetName}_${table.name ?? table.refName}`);
301
- ctx.foreignFieldRef("elementum_search_table", lookupLabel, target.value, table.name ?? table.refName);
302
- attrs["search_table_id"] = expr.dataAttr("elementum_search_table", lookupLabel, ["id"]);
303
- }
304
- else {
305
- const resourceType = table.__variant === "linked"
306
- ? "elementum_linked_ai_search_table"
307
- : table.__variant === "table"
308
- ? "elementum_table_search_table"
309
- : "elementum_ai_search_table";
310
- attrs["search_table_id"] = expr.resourceAttr(resourceType, scopedResourceLabel(target.localTfLabel, tsLabelToTfLabel(table.refName), target.kind === "element" ? "element" : "app"), ["id"]);
311
- }
312
347
  }
313
348
  if (typeKey === "updateRecord") {
314
349
  encodeOptionalHandleDescription(attrs, inner.handleDescription);
@@ -22,7 +22,7 @@ function viewFilter(value, owner, where, ctx) {
22
22
  ownerTfLabel: owner.ownerTfLabel,
23
23
  scope: owner.scope ?? "app",
24
24
  foreignFieldRefsById: ctx.foreignFieldRefsById,
25
- }, `${where}.filter`);
25
+ }, `${where}.filter`, ctx);
26
26
  }
27
27
  /** All three view resource kinds → the normalized shapes the app printer consumes. */
28
28
  export function viewsFromTf(layoutBlocks, listViewBlocks, calendarViewBlocks, kanbanViewBlocks, dashboardViewBlocks, agentViewBlocks, managedViewOrderBlocks, owner, ctx) {
@@ -106,6 +106,8 @@ function dashboardViewFromTf(block, owner, ctx) {
106
106
  const sort = managedViewSortFromTf(attrs["sort"], owner, where, ctx);
107
107
  const userIds = principalList(attrs["user_ids"], "elementum_user", `${where}: user_ids`, ctx);
108
108
  const groupIds = principalList(attrs["group_ids"], "elementum_group", `${where}: group_ids`, ctx);
109
+ const emptyUserIds = Array.isArray(attrs["user_ids"]) && attrs["user_ids"].length === 0;
110
+ const emptyGroupIds = Array.isArray(attrs["group_ids"]) && attrs["group_ids"].length === 0;
109
111
  const filter = viewFilter(attrs["filter"], owner, where, ctx);
110
112
  return {
111
113
  key: scopedLabelToBagKey(owner.ownerTfLabel, label, scope),
@@ -114,8 +116,8 @@ function dashboardViewFromTf(block, owner, ctx) {
114
116
  ...(typeof attrs["allow_record_creation"] === "boolean" && { allowRecordCreation: attrs["allow_record_creation"] }),
115
117
  ...(filter !== undefined && { filter }),
116
118
  ...(sort !== undefined && { sort }),
117
- ...(userIds !== undefined && { userIds }),
118
- ...(groupIds !== undefined && { groupIds }),
119
+ ...(userIds !== undefined ? { userIds } : emptyUserIds ? { userIds: [] } : {}),
120
+ ...(groupIds !== undefined ? { groupIds } : emptyGroupIds ? { groupIds: [] } : {}),
119
121
  };
120
122
  }
121
123
  function calendarViewFromTf(block, owner, ctx) {
@@ -163,6 +165,8 @@ function typedFieldViewFromTf(block, owner, resourceType, knownAttrs, includeAll
163
165
  const sort = managedViewSortFromTf(attrs["sort"], owner, where, ctx);
164
166
  const userIds = principalList(attrs["user_ids"], "elementum_user", `${where}: user_ids`, ctx);
165
167
  const groupIds = principalList(attrs["group_ids"], "elementum_group", `${where}: group_ids`, ctx);
168
+ const emptyUserIds = Array.isArray(attrs["user_ids"]) && attrs["user_ids"].length === 0;
169
+ const emptyGroupIds = Array.isArray(attrs["group_ids"]) && attrs["group_ids"].length === 0;
166
170
  const filter = viewFilter(attrs["filter"], owner, where, ctx);
167
171
  return {
168
172
  key: scopedLabelToBagKey(owner.ownerTfLabel, label, scope),
@@ -173,8 +177,8 @@ function typedFieldViewFromTf(block, owner, resourceType, knownAttrs, includeAll
173
177
  ...(typeof attrs["allow_record_creation"] === "boolean" && { allowRecordCreation: attrs["allow_record_creation"] }),
174
178
  ...(filter !== undefined && { filter }),
175
179
  ...(sort !== undefined && { sort }),
176
- ...(userIds !== undefined && { userIds }),
177
- ...(groupIds !== undefined && { groupIds }),
180
+ ...(userIds !== undefined ? { userIds } : emptyUserIds ? { userIds: [] } : {}),
181
+ ...(groupIds !== undefined ? { groupIds } : emptyGroupIds ? { groupIds: [] } : {}),
178
182
  };
179
183
  }
180
184
  function agentViewFromTf(block, owner, ctx) {
@@ -199,6 +203,8 @@ function agentViewFromTf(block, owner, ctx) {
199
203
  const sort = managedViewSortFromTf(attrs["sort"], owner, where, ctx);
200
204
  const userIds = principalList(attrs["user_ids"], "elementum_user", `${where}: user_ids`, ctx);
201
205
  const groupIds = principalList(attrs["group_ids"], "elementum_group", `${where}: group_ids`, ctx);
206
+ const emptyUserIds = Array.isArray(attrs["user_ids"]) && attrs["user_ids"].length === 0;
207
+ const emptyGroupIds = Array.isArray(attrs["group_ids"]) && attrs["group_ids"].length === 0;
202
208
  const filter = viewFilter(attrs["filter"], owner, where, ctx);
203
209
  return {
204
210
  key: scopedLabelToBagKey(owner.ownerTfLabel, label, scope),
@@ -207,8 +213,8 @@ function agentViewFromTf(block, owner, ctx) {
207
213
  ...(typeof attrs["allow_record_creation"] === "boolean" && { allowRecordCreation: attrs["allow_record_creation"] }),
208
214
  ...(filter !== undefined && { filter }),
209
215
  ...(sort !== undefined && { sort }),
210
- ...(userIds !== undefined && { userIds }),
211
- ...(groupIds !== undefined && { groupIds }),
216
+ ...(userIds !== undefined ? { userIds } : emptyUserIds ? { userIds: [] } : {}),
217
+ ...(groupIds !== undefined ? { groupIds } : emptyGroupIds ? { groupIds: [] } : {}),
212
218
  ...(typeof attrs["display_order"] === "number" && { displayOrder: attrs["display_order"] }),
213
219
  };
214
220
  }
@@ -334,6 +340,19 @@ function fieldRefFromTf(v, ownerLabel, ownerTfLabel, scope, ctx) {
334
340
  const fieldKey = fieldKeyFromTfValue(v, tfLabelToTsLabel(ownerLabel), ownerTfLabel, scope, ctx);
335
341
  if (fieldKey !== undefined)
336
342
  return fieldKey;
343
+ if (typeof v === "string" && v !== "") {
344
+ // The field itself couldn't be resolved to a portable reference at all
345
+ // - ei's own export already fell back to a bare platform-id string here
346
+ // (live-confirmed on sanofi/eu: two purchaseorder layout sections mix
347
+ // resolved field refs with raw ids). Keep it as-is instead of dropping
348
+ // the entry: printViewFieldRef recognizes a raw platform id and prints
349
+ // unsafePlatformId(...), the same fallback printManagedViewSort already
350
+ // uses for a sort field. Dropping it silently shrank the rebuilt
351
+ // section's field_ids, forcing a destructive replace on every plan
352
+ // (#1178).
353
+ ctx.diagnostics.push(`field reference "${v}" could not be resolved to a portable reference; kept as unresolved`);
354
+ return v;
355
+ }
337
356
  if (!isTfExpr(v)) {
338
357
  ctx.diagnostics.push(`field reference has an unrecognized (non-expression) shape; dropped`);
339
358
  return undefined;
@@ -390,6 +409,8 @@ function listViewFromTf(block, owner, ctx) {
390
409
  const density = listViewDensityFromTf(attrs["density"], where, ctx);
391
410
  const userIds = principalList(attrs["user_ids"], "elementum_user", `${where}: user_ids`, ctx);
392
411
  const groupIds = principalList(attrs["group_ids"], "elementum_group", `${where}: group_ids`, ctx);
412
+ const emptyUserIds = Array.isArray(attrs["user_ids"]) && attrs["user_ids"].length === 0;
413
+ const emptyGroupIds = Array.isArray(attrs["group_ids"]) && attrs["group_ids"].length === 0;
393
414
  return {
394
415
  key: scopedLabelToBagKey(owner.ownerTfLabel, label, scope),
395
416
  name,
@@ -399,8 +420,8 @@ function listViewFromTf(block, owner, ctx) {
399
420
  ...(typeof attrs["rows"] === "number" && { rows: attrs["rows"] }),
400
421
  ...(filter !== undefined && { filter }),
401
422
  ...(sort !== undefined && { sort }),
402
- ...(userIds !== undefined && { userIds }),
403
- ...(groupIds !== undefined && { groupIds }),
423
+ ...(userIds !== undefined ? { userIds } : emptyUserIds ? { userIds: [] } : {}),
424
+ ...(groupIds !== undefined ? { groupIds } : emptyGroupIds ? { groupIds: [] } : {}),
404
425
  };
405
426
  }
406
427
  /** `jsonencode({...})` → its decoded payload; undefined for any other shape. */