@contractspec/lib.surface-runtime 0.2.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 (232) hide show
  1. package/README.md +164 -0
  2. package/dist/adapters/ai-sdk-stub.d.ts +5 -0
  3. package/dist/adapters/ai-sdk-stub.js +13 -0
  4. package/dist/adapters/blocknote-stub.d.ts +6 -0
  5. package/dist/adapters/blocknote-stub.js +31 -0
  6. package/dist/adapters/dnd-kit-adapter.d.ts +13 -0
  7. package/dist/adapters/dnd-kit-adapter.js +44 -0
  8. package/dist/adapters/dnd-kit-stub.d.ts +6 -0
  9. package/dist/adapters/dnd-kit-stub.js +8 -0
  10. package/dist/adapters/floating-ui-stub.d.ts +6 -0
  11. package/dist/adapters/floating-ui-stub.js +19 -0
  12. package/dist/adapters/index.d.ts +11 -0
  13. package/dist/adapters/index.js +176 -0
  14. package/dist/adapters/interfaces.d.ts +75 -0
  15. package/dist/adapters/interfaces.js +1 -0
  16. package/dist/adapters/motion-stub.d.ts +7 -0
  17. package/dist/adapters/motion-stub.js +27 -0
  18. package/dist/adapters/motion-stub.test.d.ts +1 -0
  19. package/dist/adapters/resizable-panels-stub.d.ts +6 -0
  20. package/dist/adapters/resizable-panels-stub.js +46 -0
  21. package/dist/adapters/resizable-panels-stub.test.d.ts +1 -0
  22. package/dist/browser/adapters/ai-sdk-stub.js +12 -0
  23. package/dist/browser/adapters/blocknote-stub.js +30 -0
  24. package/dist/browser/adapters/dnd-kit-adapter.js +43 -0
  25. package/dist/browser/adapters/dnd-kit-stub.js +7 -0
  26. package/dist/browser/adapters/floating-ui-stub.js +18 -0
  27. package/dist/browser/adapters/index.js +175 -0
  28. package/dist/browser/adapters/interfaces.js +0 -0
  29. package/dist/browser/adapters/motion-stub.js +26 -0
  30. package/dist/browser/adapters/resizable-panels-stub.js +45 -0
  31. package/dist/browser/evals/golden-context.js +0 -0
  32. package/dist/browser/evals/golden-harness.js +848 -0
  33. package/dist/browser/examples/pm-workbench.bundle.js +476 -0
  34. package/dist/browser/i18n/catalogs/en.js +71 -0
  35. package/dist/browser/i18n/catalogs/es.js +32 -0
  36. package/dist/browser/i18n/catalogs/fr.js +32 -0
  37. package/dist/browser/i18n/catalogs/index.js +133 -0
  38. package/dist/browser/i18n/index.js +173 -0
  39. package/dist/browser/i18n/keys.js +19 -0
  40. package/dist/browser/i18n/messages.js +143 -0
  41. package/dist/browser/index.js +2466 -0
  42. package/dist/browser/react/BundleProvider.js +47 -0
  43. package/dist/browser/react/BundleRenderer.js +726 -0
  44. package/dist/browser/react/OverlayConflictResolver.js +255 -0
  45. package/dist/browser/react/PatchProposalCard.js +255 -0
  46. package/dist/browser/react/RegionRenderer.js +128 -0
  47. package/dist/browser/react/SlotRenderer.js +118 -0
  48. package/dist/browser/react/WidgetPalette.js +59 -0
  49. package/dist/browser/react/index.js +792 -0
  50. package/dist/browser/runtime/apply-surface-patch.js +322 -0
  51. package/dist/browser/runtime/audit-events.js +137 -0
  52. package/dist/browser/runtime/build-context.js +55 -0
  53. package/dist/browser/runtime/extension-registry.js +58 -0
  54. package/dist/browser/runtime/field-renderer-registry.js +145 -0
  55. package/dist/browser/runtime/index.js +1496 -0
  56. package/dist/browser/runtime/overlay-alignment.js +83 -0
  57. package/dist/browser/runtime/overlay-signer.js +15 -0
  58. package/dist/browser/runtime/override-store.js +52 -0
  59. package/dist/browser/runtime/planner-prompt.js +67 -0
  60. package/dist/browser/runtime/planner-tools.js +77 -0
  61. package/dist/browser/runtime/policy-eval.js +155 -0
  62. package/dist/browser/runtime/preference-adapter.js +67 -0
  63. package/dist/browser/runtime/resolve-bundle.js +767 -0
  64. package/dist/browser/runtime/resolve-preferences.js +59 -0
  65. package/dist/browser/runtime/rollback.js +347 -0
  66. package/dist/browser/runtime/widget-registry.js +36 -0
  67. package/dist/browser/spec/define-module-bundle.js +113 -0
  68. package/dist/browser/spec/index.js +319 -0
  69. package/dist/browser/spec/types.js +0 -0
  70. package/dist/browser/spec/validate-bundle.js +65 -0
  71. package/dist/browser/spec/validate-surface-patch.js +206 -0
  72. package/dist/browser/spec/verification-snapshot-types.js +0 -0
  73. package/dist/browser/telemetry/index.js +20 -0
  74. package/dist/browser/telemetry/surface-metrics.js +20 -0
  75. package/dist/evals/golden-context.d.ts +24 -0
  76. package/dist/evals/golden-context.js +1 -0
  77. package/dist/evals/golden-harness.d.ts +29 -0
  78. package/dist/evals/golden-harness.js +849 -0
  79. package/dist/evals/golden-harness.test.d.ts +1 -0
  80. package/dist/examples/pm-workbench.bundle.d.ts +177 -0
  81. package/dist/examples/pm-workbench.bundle.js +477 -0
  82. package/dist/i18n/catalogs/en.d.ts +1 -0
  83. package/dist/i18n/catalogs/en.js +72 -0
  84. package/dist/i18n/catalogs/es.d.ts +1 -0
  85. package/dist/i18n/catalogs/es.js +33 -0
  86. package/dist/i18n/catalogs/fr.d.ts +1 -0
  87. package/dist/i18n/catalogs/fr.js +33 -0
  88. package/dist/i18n/catalogs/index.d.ts +3 -0
  89. package/dist/i18n/catalogs/index.js +134 -0
  90. package/dist/i18n/index.d.ts +5 -0
  91. package/dist/i18n/index.js +174 -0
  92. package/dist/i18n/keys.d.ts +20 -0
  93. package/dist/i18n/keys.js +20 -0
  94. package/dist/i18n/messages.d.ts +5 -0
  95. package/dist/i18n/messages.js +144 -0
  96. package/dist/index.d.ts +4 -0
  97. package/dist/index.js +2467 -0
  98. package/dist/node/adapters/ai-sdk-stub.js +12 -0
  99. package/dist/node/adapters/blocknote-stub.js +30 -0
  100. package/dist/node/adapters/dnd-kit-adapter.js +43 -0
  101. package/dist/node/adapters/dnd-kit-stub.js +7 -0
  102. package/dist/node/adapters/floating-ui-stub.js +18 -0
  103. package/dist/node/adapters/index.js +175 -0
  104. package/dist/node/adapters/interfaces.js +0 -0
  105. package/dist/node/adapters/motion-stub.js +26 -0
  106. package/dist/node/adapters/resizable-panels-stub.js +45 -0
  107. package/dist/node/evals/golden-context.js +0 -0
  108. package/dist/node/evals/golden-harness.js +848 -0
  109. package/dist/node/examples/pm-workbench.bundle.js +476 -0
  110. package/dist/node/i18n/catalogs/en.js +71 -0
  111. package/dist/node/i18n/catalogs/es.js +32 -0
  112. package/dist/node/i18n/catalogs/fr.js +32 -0
  113. package/dist/node/i18n/catalogs/index.js +133 -0
  114. package/dist/node/i18n/index.js +173 -0
  115. package/dist/node/i18n/keys.js +19 -0
  116. package/dist/node/i18n/messages.js +143 -0
  117. package/dist/node/index.js +2466 -0
  118. package/dist/node/react/BundleProvider.js +47 -0
  119. package/dist/node/react/BundleRenderer.js +726 -0
  120. package/dist/node/react/OverlayConflictResolver.js +255 -0
  121. package/dist/node/react/PatchProposalCard.js +255 -0
  122. package/dist/node/react/RegionRenderer.js +128 -0
  123. package/dist/node/react/SlotRenderer.js +118 -0
  124. package/dist/node/react/WidgetPalette.js +59 -0
  125. package/dist/node/react/index.js +792 -0
  126. package/dist/node/runtime/apply-surface-patch.js +322 -0
  127. package/dist/node/runtime/audit-events.js +137 -0
  128. package/dist/node/runtime/build-context.js +55 -0
  129. package/dist/node/runtime/extension-registry.js +58 -0
  130. package/dist/node/runtime/field-renderer-registry.js +145 -0
  131. package/dist/node/runtime/index.js +1496 -0
  132. package/dist/node/runtime/overlay-alignment.js +83 -0
  133. package/dist/node/runtime/overlay-signer.js +15 -0
  134. package/dist/node/runtime/override-store.js +52 -0
  135. package/dist/node/runtime/planner-prompt.js +67 -0
  136. package/dist/node/runtime/planner-tools.js +77 -0
  137. package/dist/node/runtime/policy-eval.js +155 -0
  138. package/dist/node/runtime/preference-adapter.js +67 -0
  139. package/dist/node/runtime/resolve-bundle.js +767 -0
  140. package/dist/node/runtime/resolve-preferences.js +59 -0
  141. package/dist/node/runtime/rollback.js +347 -0
  142. package/dist/node/runtime/widget-registry.js +36 -0
  143. package/dist/node/spec/define-module-bundle.js +113 -0
  144. package/dist/node/spec/index.js +319 -0
  145. package/dist/node/spec/types.js +0 -0
  146. package/dist/node/spec/validate-bundle.js +65 -0
  147. package/dist/node/spec/validate-surface-patch.js +206 -0
  148. package/dist/node/spec/verification-snapshot-types.js +0 -0
  149. package/dist/node/telemetry/index.js +20 -0
  150. package/dist/node/telemetry/surface-metrics.js +20 -0
  151. package/dist/react/BundleProvider.d.ts +13 -0
  152. package/dist/react/BundleProvider.js +48 -0
  153. package/dist/react/BundleRenderer.d.ts +22 -0
  154. package/dist/react/BundleRenderer.js +727 -0
  155. package/dist/react/OverlayConflictResolver.d.ts +15 -0
  156. package/dist/react/OverlayConflictResolver.js +256 -0
  157. package/dist/react/PatchProposalCard.d.ts +13 -0
  158. package/dist/react/PatchProposalCard.js +256 -0
  159. package/dist/react/RegionRenderer.d.ts +13 -0
  160. package/dist/react/RegionRenderer.js +129 -0
  161. package/dist/react/SlotRenderer.d.ts +13 -0
  162. package/dist/react/SlotRenderer.js +119 -0
  163. package/dist/react/WidgetPalette.d.ts +12 -0
  164. package/dist/react/WidgetPalette.js +60 -0
  165. package/dist/react/index.d.ts +7 -0
  166. package/dist/react/index.js +793 -0
  167. package/dist/runtime/apply-surface-patch.d.ts +15 -0
  168. package/dist/runtime/apply-surface-patch.js +323 -0
  169. package/dist/runtime/apply-surface-patch.test.d.ts +1 -0
  170. package/dist/runtime/audit-events.d.ts +70 -0
  171. package/dist/runtime/audit-events.js +138 -0
  172. package/dist/runtime/audit-events.test.d.ts +1 -0
  173. package/dist/runtime/build-context.d.ts +9 -0
  174. package/dist/runtime/build-context.js +56 -0
  175. package/dist/runtime/extension-registry.d.ts +39 -0
  176. package/dist/runtime/extension-registry.js +59 -0
  177. package/dist/runtime/field-renderer-registry.d.ts +23 -0
  178. package/dist/runtime/field-renderer-registry.js +146 -0
  179. package/dist/runtime/field-renderer-registry.test.d.ts +1 -0
  180. package/dist/runtime/index.d.ts +16 -0
  181. package/dist/runtime/index.js +1497 -0
  182. package/dist/runtime/overlay-alignment.d.ts +49 -0
  183. package/dist/runtime/overlay-alignment.js +84 -0
  184. package/dist/runtime/overlay-alignment.test.d.ts +1 -0
  185. package/dist/runtime/overlay-signer.d.ts +15 -0
  186. package/dist/runtime/overlay-signer.js +16 -0
  187. package/dist/runtime/override-store.d.ts +44 -0
  188. package/dist/runtime/override-store.js +53 -0
  189. package/dist/runtime/override-store.test.d.ts +1 -0
  190. package/dist/runtime/planner-prompt.d.ts +39 -0
  191. package/dist/runtime/planner-prompt.js +68 -0
  192. package/dist/runtime/planner-prompt.test.d.ts +1 -0
  193. package/dist/runtime/planner-tools.d.ts +106 -0
  194. package/dist/runtime/planner-tools.js +78 -0
  195. package/dist/runtime/planner-tools.test.d.ts +1 -0
  196. package/dist/runtime/policy-eval.d.ts +23 -0
  197. package/dist/runtime/policy-eval.js +156 -0
  198. package/dist/runtime/preference-adapter.d.ts +6 -0
  199. package/dist/runtime/preference-adapter.js +68 -0
  200. package/dist/runtime/resolve-bundle.d.ts +68 -0
  201. package/dist/runtime/resolve-bundle.js +768 -0
  202. package/dist/runtime/resolve-bundle.test.d.ts +1 -0
  203. package/dist/runtime/resolve-preferences.d.ts +9 -0
  204. package/dist/runtime/resolve-preferences.js +60 -0
  205. package/dist/runtime/resolve-preferences.test.d.ts +1 -0
  206. package/dist/runtime/rollback.d.ts +21 -0
  207. package/dist/runtime/rollback.js +348 -0
  208. package/dist/runtime/rollback.test.d.ts +1 -0
  209. package/dist/runtime/widget-registry.d.ts +26 -0
  210. package/dist/runtime/widget-registry.js +37 -0
  211. package/dist/runtime/widget-registry.test.d.ts +1 -0
  212. package/dist/spec/define-module-bundle.d.ts +17 -0
  213. package/dist/spec/define-module-bundle.js +114 -0
  214. package/dist/spec/define-module-bundle.test.d.ts +1 -0
  215. package/dist/spec/index.d.ts +5 -0
  216. package/dist/spec/index.js +320 -0
  217. package/dist/spec/types.d.ts +494 -0
  218. package/dist/spec/types.js +1 -0
  219. package/dist/spec/validate-bundle.d.ts +23 -0
  220. package/dist/spec/validate-bundle.js +66 -0
  221. package/dist/spec/validate-bundle.test.d.ts +1 -0
  222. package/dist/spec/validate-surface-patch.d.ts +39 -0
  223. package/dist/spec/validate-surface-patch.js +207 -0
  224. package/dist/spec/validate-surface-patch.test.d.ts +1 -0
  225. package/dist/spec/verification-snapshot-types.d.ts +23 -0
  226. package/dist/spec/verification-snapshot-types.js +1 -0
  227. package/dist/spec/verification-snapshot.test.d.ts +5 -0
  228. package/dist/telemetry/index.d.ts +5 -0
  229. package/dist/telemetry/index.js +21 -0
  230. package/dist/telemetry/surface-metrics.d.ts +17 -0
  231. package/dist/telemetry/surface-metrics.js +21 -0
  232. package/package.json +920 -0
@@ -0,0 +1,1497 @@
1
+ // @bun
2
+ // src/telemetry/surface-metrics.ts
3
+ import {
4
+ createCounter,
5
+ createHistogram
6
+ } from "@contractspec/lib.observability/metrics";
7
+ var METER_NAME = "@contractspec/lib.surface-runtime";
8
+ var resolutionDurationMs = createHistogram("bundle_surface_resolution_duration_ms", "Time to resolve bundle spec to surface plan in milliseconds", METER_NAME);
9
+ var patchAcceptanceCounter = createCounter("bundle_surface_patch_acceptance_total", "Total AI patch proposals accepted by user", METER_NAME);
10
+ var patchRejectionCounter = createCounter("bundle_surface_patch_rejection_total", "Total AI patch proposals rejected by user", METER_NAME);
11
+ var policyDenialCounter = createCounter("bundle_surface_policy_denial_total", "Total policy denials (actions denied by policy)", METER_NAME);
12
+ var surfaceFallbackCounter = createCounter("bundle_surface_fallback_total", "Total surface or layout fallbacks during resolution", METER_NAME);
13
+ var missingRendererCounter = createCounter("bundle_surface_renderer_missing_total", "Total slots with no renderer for requested node kind", METER_NAME);
14
+ // src/runtime/resolve-preferences.ts
15
+ var DIMENSION_KEYS = [
16
+ "guidance",
17
+ "density",
18
+ "dataDepth",
19
+ "control",
20
+ "media",
21
+ "pace",
22
+ "narrative"
23
+ ];
24
+ var SCOPE_ORDER = [
25
+ "user",
26
+ "workspace-user",
27
+ "bundle",
28
+ "surface",
29
+ "entity",
30
+ "session"
31
+ ];
32
+ function mergeByScope(layers, ctx) {
33
+ const merged = { ...ctx.preferences };
34
+ const sourceByDimension = {};
35
+ for (const scope of SCOPE_ORDER) {
36
+ const layer = layers[scope];
37
+ if (!layer)
38
+ continue;
39
+ for (const dim of DIMENSION_KEYS) {
40
+ const val = layer[dim];
41
+ if (val !== undefined) {
42
+ merged[dim] = val;
43
+ sourceByDimension[dim] = scope;
44
+ }
45
+ }
46
+ }
47
+ for (const dim of DIMENSION_KEYS) {
48
+ if (sourceByDimension[dim] === undefined) {
49
+ sourceByDimension[dim] = "session";
50
+ }
51
+ }
52
+ return { canonical: merged, sourceByDimension };
53
+ }
54
+ function resolveConstraints(_canonical, _ctx) {
55
+ return { constrained: {}, notes: [] };
56
+ }
57
+ function resolvePreferenceProfile(ctx) {
58
+ const layers = {
59
+ session: ctx.preferences
60
+ };
61
+ const { canonical, sourceByDimension } = mergeByScope(layers, ctx);
62
+ const { constrained, notes } = resolveConstraints(canonical, ctx);
63
+ return {
64
+ canonical,
65
+ sourceByDimension,
66
+ constrained,
67
+ notes
68
+ };
69
+ }
70
+
71
+ // src/spec/validate-surface-patch.ts
72
+ var VALID_OPS = [
73
+ "insert-node",
74
+ "replace-node",
75
+ "remove-node",
76
+ "move-node",
77
+ "resize-panel",
78
+ "set-layout",
79
+ "reveal-field",
80
+ "hide-field",
81
+ "promote-action",
82
+ "set-focus"
83
+ ];
84
+ var VALID_NODE_KINDS = [
85
+ "metric-strip",
86
+ "data-view",
87
+ "entity-card",
88
+ "entity-header",
89
+ "entity-summary",
90
+ "entity-section",
91
+ "entity-field",
92
+ "entity-activity",
93
+ "entity-relations",
94
+ "entity-timeline",
95
+ "entity-comments",
96
+ "entity-attachments",
97
+ "entity-view-switcher",
98
+ "entity-automation-panel",
99
+ "rich-doc",
100
+ "chat-thread",
101
+ "assistant-panel",
102
+ "action-bar",
103
+ "timeline",
104
+ "board",
105
+ "table",
106
+ "calendar",
107
+ "form",
108
+ "chart",
109
+ "relation-graph",
110
+ "custom-widget"
111
+ ];
112
+ function validateSurfaceNode(node, path) {
113
+ if (!node.nodeId || typeof node.nodeId !== "string") {
114
+ throw new Error(`${path}: nodeId must be a non-empty string`);
115
+ }
116
+ if (!node.kind || !VALID_NODE_KINDS.includes(node.kind)) {
117
+ throw new Error(`${path}: kind must be one of ${VALID_NODE_KINDS.join(", ")}`);
118
+ }
119
+ if (node.children) {
120
+ for (let i = 0;i < node.children.length; i++) {
121
+ const child = node.children[i];
122
+ if (child)
123
+ validateSurfaceNode(child, `${path}.children[${i}]`);
124
+ }
125
+ }
126
+ }
127
+ function validateSurfacePatchOp(op, index) {
128
+ const path = `ops[${index}]`;
129
+ if (!op || typeof op !== "object" || !("op" in op)) {
130
+ throw new Error(`${path}: must be an object with op field`);
131
+ }
132
+ const opType = op.op;
133
+ if (!VALID_OPS.includes(opType)) {
134
+ throw new Error(`${path}: op must be one of ${VALID_OPS.join(", ")}`);
135
+ }
136
+ switch (op.op) {
137
+ case "insert-node":
138
+ if (!op.slotId || typeof op.slotId !== "string") {
139
+ throw new Error(`${path}: insert-node requires slotId string`);
140
+ }
141
+ if (!op.node) {
142
+ throw new Error(`${path}: insert-node requires node`);
143
+ }
144
+ validateSurfaceNode(op.node, `${path}.node`);
145
+ if (op.index !== undefined && typeof op.index !== "number") {
146
+ throw new Error(`${path}: insert-node index must be number if present`);
147
+ }
148
+ break;
149
+ case "replace-node":
150
+ if (!op.nodeId || typeof op.nodeId !== "string") {
151
+ throw new Error(`${path}: replace-node requires nodeId string`);
152
+ }
153
+ if (!op.node) {
154
+ throw new Error(`${path}: replace-node requires node`);
155
+ }
156
+ validateSurfaceNode(op.node, `${path}.node`);
157
+ break;
158
+ case "remove-node":
159
+ if (!op.nodeId || typeof op.nodeId !== "string") {
160
+ throw new Error(`${path}: remove-node requires nodeId string`);
161
+ }
162
+ break;
163
+ case "move-node":
164
+ if (!op.nodeId || typeof op.nodeId !== "string") {
165
+ throw new Error(`${path}: move-node requires nodeId string`);
166
+ }
167
+ if (!op.toSlotId || typeof op.toSlotId !== "string") {
168
+ throw new Error(`${path}: move-node requires toSlotId string`);
169
+ }
170
+ if (op.index !== undefined && typeof op.index !== "number") {
171
+ throw new Error(`${path}: move-node index must be number if present`);
172
+ }
173
+ break;
174
+ case "resize-panel":
175
+ if (!op.persistKey || typeof op.persistKey !== "string") {
176
+ throw new Error(`${path}: resize-panel requires persistKey string`);
177
+ }
178
+ if (!Array.isArray(op.sizes) || op.sizes.some((s) => typeof s !== "number")) {
179
+ throw new Error(`${path}: resize-panel requires sizes number[]`);
180
+ }
181
+ break;
182
+ case "set-layout":
183
+ if (!op.layoutId || typeof op.layoutId !== "string") {
184
+ throw new Error(`${path}: set-layout requires layoutId string`);
185
+ }
186
+ break;
187
+ case "reveal-field":
188
+ case "hide-field":
189
+ if (!op.fieldId || typeof op.fieldId !== "string") {
190
+ throw new Error(`${path}: ${op.op} requires fieldId string`);
191
+ }
192
+ break;
193
+ case "promote-action": {
194
+ if (!op.actionId || typeof op.actionId !== "string") {
195
+ throw new Error(`${path}: promote-action requires actionId string`);
196
+ }
197
+ const validPlacements = ["header", "inline", "context", "assistant"];
198
+ if (!op.placement || !validPlacements.includes(op.placement)) {
199
+ throw new Error(`${path}: promote-action placement must be one of ${validPlacements.join(", ")}`);
200
+ }
201
+ break;
202
+ }
203
+ case "set-focus":
204
+ if (!op.targetId || typeof op.targetId !== "string") {
205
+ throw new Error(`${path}: set-focus requires targetId string`);
206
+ }
207
+ break;
208
+ default:
209
+ throw new Error(`${path}: unknown op "${opType}"`);
210
+ }
211
+ }
212
+ function validateSurfacePatch(ops) {
213
+ if (!Array.isArray(ops)) {
214
+ throw new Error("Patch ops must be an array");
215
+ }
216
+ for (let i = 0;i < ops.length; i++) {
217
+ const op = ops[i];
218
+ if (op)
219
+ validateSurfacePatchOp(op, i);
220
+ }
221
+ }
222
+ function validateSurfaceNodeAgainstKinds(node, allowedKinds, path) {
223
+ if (!allowedKinds.includes(node.kind)) {
224
+ throw new Error(`${path}: kind "${node.kind}" not in allowed list [${allowedKinds.join(", ")}]`);
225
+ }
226
+ if (node.children) {
227
+ for (let i = 0;i < node.children.length; i++) {
228
+ const child = node.children[i];
229
+ if (child)
230
+ validateSurfaceNodeAgainstKinds(child, allowedKinds, `${path}.children[${i}]`);
231
+ }
232
+ }
233
+ }
234
+ function validatePatchProposal(ops, constraints) {
235
+ if (!Array.isArray(ops)) {
236
+ throw new Error("Patch ops must be an array");
237
+ }
238
+ const { allowedOps, allowedSlots, allowedNodeKinds } = constraints;
239
+ for (let i = 0;i < ops.length; i++) {
240
+ const op = ops[i];
241
+ if (!op)
242
+ continue;
243
+ const path = `ops[${i}]`;
244
+ if (!allowedOps.includes(op.op)) {
245
+ throw new Error(`${path}: op "${op.op}" not in allowed list [${allowedOps.join(", ")}]`);
246
+ }
247
+ switch (op.op) {
248
+ case "insert-node":
249
+ if (!allowedSlots.includes(op.slotId)) {
250
+ throw new Error(`${path}: slotId "${op.slotId}" not in allowed slots [${allowedSlots.join(", ")}]`);
251
+ }
252
+ if (op.node) {
253
+ validateSurfaceNodeAgainstKinds(op.node, allowedNodeKinds, `${path}.node`);
254
+ }
255
+ break;
256
+ case "move-node":
257
+ if (!allowedSlots.includes(op.toSlotId)) {
258
+ throw new Error(`${path}: toSlotId "${op.toSlotId}" not in allowed slots [${allowedSlots.join(", ")}]`);
259
+ }
260
+ break;
261
+ case "replace-node":
262
+ if (op.node) {
263
+ validateSurfaceNodeAgainstKinds(op.node, allowedNodeKinds, `${path}.node`);
264
+ }
265
+ break;
266
+ default:
267
+ break;
268
+ }
269
+ }
270
+ validateSurfacePatch(ops);
271
+ }
272
+
273
+ // src/runtime/apply-surface-patch.ts
274
+ import { Logger } from "@contractspec/lib.observability";
275
+ var logger = new Logger("@contractspec/lib.surface-runtime");
276
+ function findNode(nodes, nodeId) {
277
+ for (const n of nodes) {
278
+ if (n.nodeId === nodeId)
279
+ return n;
280
+ if (n.children) {
281
+ const found = findNode(n.children, nodeId);
282
+ if (found)
283
+ return found;
284
+ }
285
+ }
286
+ return;
287
+ }
288
+ function collectNodeIds(nodes) {
289
+ const ids = new Set;
290
+ for (const n of nodes) {
291
+ ids.add(n.nodeId);
292
+ if (n.children)
293
+ collectNodeIds(n.children).forEach((id) => ids.add(id));
294
+ }
295
+ return ids;
296
+ }
297
+ function validateOp(op, nodeIds) {
298
+ if (op.op === "remove-node" || op.op === "replace-node" || op.op === "move-node") {
299
+ if (!nodeIds.has(op.nodeId)) {
300
+ throw new Error(`Patch op references unknown nodeId: ${op.nodeId}`);
301
+ }
302
+ }
303
+ if (op.op === "insert-node" && !op.node?.nodeId) {
304
+ throw new Error("insert-node requires node with nodeId");
305
+ }
306
+ }
307
+ function produceInverse(op, plan) {
308
+ switch (op.op) {
309
+ case "insert-node":
310
+ return op.node ? { op: "remove-node", nodeId: op.node.nodeId } : null;
311
+ case "remove-node": {
312
+ const node = findNode(plan.nodes, op.nodeId);
313
+ return node ? { op: "insert-node", slotId: "primary", node } : null;
314
+ }
315
+ case "replace-node": {
316
+ const prev = findNode(plan.nodes, op.nodeId);
317
+ return prev ? { op: "replace-node", nodeId: op.nodeId, node: prev } : null;
318
+ }
319
+ case "set-layout":
320
+ return { op: "set-layout", layoutId: plan.layoutId };
321
+ case "reveal-field":
322
+ return { op: "hide-field", fieldId: op.fieldId };
323
+ case "hide-field":
324
+ return { op: "reveal-field", fieldId: op.fieldId };
325
+ case "move-node":
326
+ case "resize-panel":
327
+ case "set-focus":
328
+ case "promote-action":
329
+ return null;
330
+ default:
331
+ return null;
332
+ }
333
+ }
334
+ function applySurfacePatch(plan, ops) {
335
+ if (ops.length === 0)
336
+ return { plan, inverseOps: [] };
337
+ validateSurfacePatch(ops);
338
+ const nodeIds = collectNodeIds(plan.nodes);
339
+ for (const op of ops) {
340
+ validateOp(op, nodeIds);
341
+ }
342
+ let nextNodes = [...plan.nodes];
343
+ let nextLayoutId = plan.layoutId;
344
+ const inverseOps = [];
345
+ for (const op of ops) {
346
+ const inv = produceInverse(op, {
347
+ ...plan,
348
+ nodes: nextNodes,
349
+ layoutId: nextLayoutId
350
+ });
351
+ if (inv)
352
+ inverseOps.unshift(inv);
353
+ switch (op.op) {
354
+ case "insert-node":
355
+ if (op.node)
356
+ nextNodes = [...nextNodes, op.node];
357
+ break;
358
+ case "remove-node":
359
+ nextNodes = nextNodes.filter((n) => n.nodeId !== op.nodeId);
360
+ break;
361
+ case "replace-node": {
362
+ const replace = (nodes) => nodes.map((n) => n.nodeId === op.nodeId ? op.node : { ...n, children: n.children ? replace(n.children) : undefined });
363
+ nextNodes = replace(nextNodes);
364
+ break;
365
+ }
366
+ case "set-layout":
367
+ nextLayoutId = op.layoutId;
368
+ break;
369
+ case "move-node":
370
+ case "resize-panel":
371
+ case "set-focus":
372
+ case "reveal-field":
373
+ case "hide-field":
374
+ case "promote-action":
375
+ break;
376
+ }
377
+ }
378
+ const result = {
379
+ plan: { ...plan, nodes: nextNodes, layoutId: nextLayoutId },
380
+ inverseOps
381
+ };
382
+ logger.info("bundle.surface.patch.applied", {
383
+ bundleKey: plan.bundleKey,
384
+ surfaceId: plan.surfaceId,
385
+ opCount: ops.length,
386
+ opTypes: [...new Set(ops.map((o) => o.op))]
387
+ });
388
+ return result;
389
+ }
390
+
391
+ // src/runtime/override-store.ts
392
+ function buildOverrideTargetKey(bundleKey, surfaceId, routeId) {
393
+ return routeId ? `${bundleKey}:${routeId}:${surfaceId}` : `${bundleKey}:${surfaceId}`;
394
+ }
395
+ function createOverrideStoreWithApprovalGate(store, options) {
396
+ const { requireApprovalForWorkspacePatches = false, requestApproval } = options;
397
+ return {
398
+ list: store.list.bind(store),
399
+ async save(scope, targetKey, patch, saveOptions) {
400
+ if (requireApprovalForWorkspacePatches && scope === "workspace" && requestApproval) {
401
+ const approved = await requestApproval({ scope, targetKey, patch });
402
+ if (!approved) {
403
+ throw new Error("Workspace overlay save rejected: approval required and not granted");
404
+ }
405
+ }
406
+ return store.save(scope, targetKey, patch, saveOptions);
407
+ },
408
+ remove: store.remove.bind(store)
409
+ };
410
+ }
411
+ function createInMemoryOverrideStore() {
412
+ const overrides = new Map;
413
+ function nextId() {
414
+ return `ov_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
415
+ }
416
+ return {
417
+ async list(scope, targetKey) {
418
+ return Array.from(overrides.values()).filter((o) => o.scope === scope && o.targetKey === targetKey);
419
+ },
420
+ async save(scope, targetKey, patch, options) {
421
+ const overrideId = options?.overrideId ?? nextId();
422
+ const stored = {
423
+ overrideId,
424
+ scope,
425
+ targetKey,
426
+ patch,
427
+ createdAt: new Date().toISOString(),
428
+ createdBy: options?.createdBy
429
+ };
430
+ overrides.set(overrideId, stored);
431
+ return overrideId;
432
+ },
433
+ async remove(overrideId) {
434
+ overrides.delete(overrideId);
435
+ }
436
+ };
437
+ }
438
+
439
+ // src/runtime/resolve-bundle.ts
440
+ import { traceAsync } from "@contractspec/lib.observability/tracing";
441
+ import { Logger as Logger2 } from "@contractspec/lib.observability";
442
+ var logger2 = new Logger2("@contractspec/lib.surface-runtime");
443
+ function getOpTarget(op) {
444
+ switch (op.op) {
445
+ case "hide-field":
446
+ case "reveal-field":
447
+ return `field:${op.fieldId}`;
448
+ case "remove-node":
449
+ case "replace-node":
450
+ case "move-node":
451
+ return `node:${op.nodeId}`;
452
+ case "insert-node":
453
+ return op.slotId ? `slot:${op.slotId}` : null;
454
+ default:
455
+ return null;
456
+ }
457
+ }
458
+ function matchRoute(pathPattern, route) {
459
+ if (pathPattern === route)
460
+ return true;
461
+ const patternParts = pathPattern.split("/");
462
+ const routeParts = route.split("/");
463
+ if (patternParts.length !== routeParts.length)
464
+ return false;
465
+ for (let i = 0;i < patternParts.length; i++) {
466
+ const p = patternParts[i];
467
+ const r = routeParts[i];
468
+ if (p?.startsWith(":"))
469
+ continue;
470
+ if (p !== r)
471
+ return false;
472
+ }
473
+ return true;
474
+ }
475
+ function selectSurface(spec, ctx) {
476
+ const route = spec.routes.find((r) => matchRoute(r.path, ctx.route)) ?? spec.routes[0];
477
+ if (!route) {
478
+ throw new Error("No routes declared in bundle spec.");
479
+ }
480
+ const fallbackRoute = !spec.routes.find((r) => matchRoute(r.path, ctx.route));
481
+ let surface = spec.surfaces[route.defaultSurface];
482
+ let fallback;
483
+ if (!surface && route.candidateSurfaces?.length) {
484
+ for (const sid of route.candidateSurfaces) {
485
+ surface = spec.surfaces[sid];
486
+ if (surface) {
487
+ fallback = `surface=${sid}`;
488
+ break;
489
+ }
490
+ }
491
+ }
492
+ if (!surface) {
493
+ const overview = Object.values(spec.surfaces).find((s) => s.kind === "overview");
494
+ if (overview) {
495
+ surface = overview;
496
+ fallback = "surface=overview";
497
+ }
498
+ }
499
+ if (!surface) {
500
+ throw new Error(`Default surface "${route.defaultSurface}" was not found.`);
501
+ }
502
+ return {
503
+ surface,
504
+ routeId: route.routeId,
505
+ fallback: fallback ?? (fallbackRoute ? "route" : undefined)
506
+ };
507
+ }
508
+ function selectLayout(surface, ctx, spec) {
509
+ let preferredLayoutId;
510
+ if (ctx.activeViewId && ctx.entity?.type && spec?.entities?.viewKinds) {
511
+ const viewKind = spec.entities.viewKinds[ctx.activeViewId];
512
+ const entityType = spec.entities.entityTypes[ctx.entity.type];
513
+ if (viewKind?.defaultLayoutId && entityType?.supportedViews?.includes(ctx.activeViewId)) {
514
+ preferredLayoutId = viewKind.defaultLayoutId;
515
+ }
516
+ }
517
+ const matching = surface.layouts.filter((candidate) => candidate.when?.(ctx) ?? true);
518
+ let layout;
519
+ if (preferredLayoutId) {
520
+ layout = matching.find((l) => l.layoutId === preferredLayoutId) ?? surface.layouts.find((l) => l.layoutId === preferredLayoutId) ?? matching[matching.length - 1] ?? surface.layouts[0];
521
+ } else {
522
+ layout = matching[matching.length - 1] ?? surface.layouts[0];
523
+ }
524
+ if (!layout) {
525
+ throw new Error(`Surface "${surface.surfaceId}" has no layouts.`);
526
+ }
527
+ const layoutFallback = matching.length === 0 && surface.layouts.length > 0;
528
+ return { layout, layoutFallback: layoutFallback || undefined };
529
+ }
530
+ function resolveDataRecipes(recipes, ctx) {
531
+ const bindings = {};
532
+ for (const r of recipes) {
533
+ if (r.when?.(ctx) ?? true) {
534
+ const key = r.hydrateInto ?? r.recipeId;
535
+ bindings[key] = { recipeId: r.recipeId, source: r.source, _stub: true };
536
+ }
537
+ }
538
+ return bindings;
539
+ }
540
+ function allowFromDecision(decision) {
541
+ if (!decision)
542
+ return true;
543
+ return decision.effect === "allow" || decision.effect === "allow-session-only";
544
+ }
545
+ function generateResolutionId() {
546
+ return `res_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
547
+ }
548
+ var OVERLAY_SCOPE_ORDER = [
549
+ "system",
550
+ "workspace",
551
+ "team",
552
+ "user",
553
+ "session"
554
+ ];
555
+ function buildAdaptation(ctx) {
556
+ const profile = resolvePreferenceProfile(ctx);
557
+ const notes = [...profile.notes];
558
+ const constrainedEntries = Object.entries(profile.constrained);
559
+ if (constrainedEntries.length > 0) {
560
+ notes.push(`constrained: ${constrainedEntries.map(([k, v]) => `${k}=${v}`).join(", ")}`);
561
+ }
562
+ return {
563
+ appliedDimensions: profile.canonical,
564
+ notes
565
+ };
566
+ }
567
+ async function resolveBundle(spec, ctx, options) {
568
+ return traceAsync("surface.resolveBundle", async (span) => {
569
+ const start = performance.now();
570
+ try {
571
+ const plan = await resolveBundleInternal(spec, ctx, options);
572
+ const durationMs = performance.now() - start;
573
+ resolutionDurationMs.record(durationMs, {
574
+ bundleKey: spec.meta.key,
575
+ surfaceId: plan.surfaceId,
576
+ fallback: plan.surfaceId === "_error" ? "error" : plan.audit.reasons.some((r) => r.startsWith("fallback=")) ? "yes" : "no"
577
+ });
578
+ span.setAttribute("bundle.key", spec.meta.key);
579
+ span.setAttribute("surface.id", plan.surfaceId);
580
+ span.setAttribute("resolution.duration_ms", durationMs);
581
+ logger2.info("bundle.surface.resolved", {
582
+ bundleKey: spec.meta.key,
583
+ surfaceId: plan.surfaceId,
584
+ layoutId: plan.layoutId,
585
+ resolutionMs: Math.round(durationMs),
586
+ fallback: plan.audit.reasons.some((r) => r.startsWith("fallback=")),
587
+ nodeCount: plan.nodes.length
588
+ });
589
+ return plan;
590
+ } catch (err) {
591
+ const durationMs = performance.now() - start;
592
+ resolutionDurationMs.record(durationMs, {
593
+ bundleKey: spec.meta.key,
594
+ surfaceId: "_error",
595
+ fallback: "error"
596
+ });
597
+ return createErrorPlan(spec, ctx, err);
598
+ }
599
+ }, "@contractspec/lib.surface-runtime");
600
+ }
601
+ async function resolveBundleInternal(spec, ctx, options) {
602
+ const {
603
+ surface,
604
+ routeId,
605
+ fallback: surfaceFallback
606
+ } = selectSurface(spec, ctx);
607
+ const { layout, layoutFallback } = selectLayout(surface, ctx, spec);
608
+ let bindings = resolveDataRecipes(surface.data, ctx);
609
+ const redactBinding = options?.policy?.redactBinding;
610
+ if (redactBinding) {
611
+ const redacted = {};
612
+ for (const [k, v] of Object.entries(bindings)) {
613
+ redacted[k] = redactBinding(k, ctx) ?? v;
614
+ }
615
+ bindings = redacted;
616
+ }
617
+ let nodes = [];
618
+ const evaluateNode = options?.policy?.evaluateNode;
619
+ if (evaluateNode) {
620
+ nodes = nodes.filter((n) => allowFromDecision(evaluateNode(n, ctx)));
621
+ }
622
+ const reasons = [
623
+ `surface=${surface.surfaceId}`,
624
+ `layout=${layout.layoutId}`,
625
+ `density=${ctx.preferences.density}`,
626
+ `narrative=${ctx.preferences.narrative}`
627
+ ];
628
+ if (surfaceFallback) {
629
+ reasons.push(`fallback=${surfaceFallback}`);
630
+ surfaceFallbackCounter.add(1, {
631
+ bundleKey: spec.meta.key,
632
+ type: "surface"
633
+ });
634
+ }
635
+ if (layoutFallback) {
636
+ reasons.push("fallback=layout");
637
+ surfaceFallbackCounter.add(1, { bundleKey: spec.meta.key, type: "layout" });
638
+ }
639
+ const resolutionId = generateResolutionId();
640
+ const plan = {
641
+ bundleKey: spec.meta.key,
642
+ surfaceId: surface.surfaceId,
643
+ layoutId: layout.layoutId,
644
+ layoutRoot: layout.root,
645
+ nodes,
646
+ actions: surface.actions ?? [],
647
+ commands: surface.commands ?? [],
648
+ bindings,
649
+ adaptation: buildAdaptation(ctx),
650
+ overlays: [],
651
+ ai: {
652
+ plannerId: spec.ai?.plannerId
653
+ },
654
+ audit: {
655
+ resolutionId,
656
+ createdAt: new Date().toISOString(),
657
+ reasons
658
+ },
659
+ locale: ctx.locale
660
+ };
661
+ options?.audit?.emit({
662
+ eventId: `evt_${resolutionId}`,
663
+ at: new Date().toISOString(),
664
+ actorId: ctx.actorId,
665
+ source: "system",
666
+ bundleKey: spec.meta.key,
667
+ surfaceId: surface.surfaceId,
668
+ eventType: "surface.resolved",
669
+ payload: {
670
+ resolutionId,
671
+ layoutId: layout.layoutId,
672
+ reasons,
673
+ nodeCount: nodes.length
674
+ }
675
+ });
676
+ let finalPlan = plan;
677
+ if (options?.overlayMerge) {
678
+ const { overrideStore, scopes = OVERLAY_SCOPE_ORDER } = options.overlayMerge;
679
+ const targetKey = buildOverrideTargetKey(spec.meta.key, surface.surfaceId, routeId);
680
+ const appliedOverlays = [];
681
+ const conflicts = [];
682
+ const seenTargets = new Map;
683
+ const conflictKeys = new Set;
684
+ for (const scope of scopes) {
685
+ if (scope === "session")
686
+ continue;
687
+ const stored = await overrideStore.list(scope, targetKey);
688
+ for (const ov of stored) {
689
+ if (ov.patch.length === 0)
690
+ continue;
691
+ for (const op of ov.patch) {
692
+ const opTarget = getOpTarget(op);
693
+ if (opTarget) {
694
+ const prev = seenTargets.get(opTarget);
695
+ if (prev && prev.scope !== scope) {
696
+ const ck = [opTarget, prev.scope, scope].sort().join(":");
697
+ if (!conflictKeys.has(ck)) {
698
+ conflictKeys.add(ck);
699
+ conflicts.push({
700
+ targetKey: opTarget,
701
+ scopeA: prev.scope,
702
+ scopeB: scope,
703
+ overlayIdA: prev.overlayId,
704
+ overlayIdB: ov.overrideId
705
+ });
706
+ }
707
+ }
708
+ seenTargets.set(opTarget, { scope, overlayId: ov.overrideId });
709
+ }
710
+ }
711
+ try {
712
+ const { plan: next } = applySurfacePatch(finalPlan, ov.patch);
713
+ finalPlan = next;
714
+ appliedOverlays.push({
715
+ overlayId: ov.overrideId,
716
+ scope,
717
+ appliedOps: ov.patch.length
718
+ });
719
+ } catch (err) {
720
+ logger2.warn("bundle.overlay.apply.failed", {
721
+ overlayId: ov.overrideId,
722
+ scope,
723
+ error: err instanceof Error ? err.message : String(err)
724
+ });
725
+ }
726
+ }
727
+ }
728
+ finalPlan = {
729
+ ...finalPlan,
730
+ overlays: appliedOverlays,
731
+ overlayConflicts: conflicts.length > 0 ? conflicts : undefined
732
+ };
733
+ }
734
+ return finalPlan;
735
+ }
736
+ function createErrorPlan(spec, ctx, err) {
737
+ const message = err instanceof Error ? err.message : String(err);
738
+ const errorLayoutRoot = {
739
+ type: "stack",
740
+ direction: "vertical",
741
+ children: [{ type: "slot", slotId: "primary" }]
742
+ };
743
+ return {
744
+ bundleKey: spec.meta.key,
745
+ surfaceId: "_error",
746
+ layoutId: "_error",
747
+ layoutRoot: errorLayoutRoot,
748
+ nodes: [],
749
+ actions: [],
750
+ commands: [],
751
+ bindings: {},
752
+ adaptation: {
753
+ appliedDimensions: ctx.preferences,
754
+ notes: [`resolution failed: ${message}`]
755
+ },
756
+ overlays: [],
757
+ ai: {},
758
+ audit: {
759
+ resolutionId: generateResolutionId(),
760
+ createdAt: new Date().toISOString(),
761
+ reasons: ["fallback=error", `error=${message}`]
762
+ },
763
+ locale: ctx.locale
764
+ };
765
+ }
766
+
767
+ // src/runtime/build-context.ts
768
+ var DEFAULT_PREFERENCES = {
769
+ guidance: "hints",
770
+ density: "standard",
771
+ dataDepth: "detailed",
772
+ control: "standard",
773
+ media: "text",
774
+ pace: "balanced",
775
+ narrative: "top-down"
776
+ };
777
+ function buildContext(params) {
778
+ const {
779
+ route,
780
+ params: routeParams = {},
781
+ query = {},
782
+ tenantId = "default",
783
+ workspaceId,
784
+ actorId,
785
+ device = "desktop",
786
+ preferences: partialPrefs = {},
787
+ capabilities = [],
788
+ featureFlags = [],
789
+ mode,
790
+ locale,
791
+ timezone,
792
+ entity,
793
+ conversation,
794
+ activeViewId
795
+ } = params;
796
+ const preferences = {
797
+ ...DEFAULT_PREFERENCES,
798
+ ...partialPrefs
799
+ };
800
+ return {
801
+ tenantId,
802
+ workspaceId,
803
+ actorId,
804
+ route,
805
+ params: routeParams,
806
+ query,
807
+ device,
808
+ mode,
809
+ locale,
810
+ timezone,
811
+ entity,
812
+ conversation,
813
+ activeViewId,
814
+ preferences,
815
+ capabilities,
816
+ featureFlags
817
+ };
818
+ }
819
+
820
+ // src/runtime/preference-adapter.ts
821
+ var defaultPreferenceAdapter = {
822
+ async resolve(ctx) {
823
+ return resolvePreferenceProfile(ctx);
824
+ },
825
+ async savePreferencePatch(_args) {}
826
+ };
827
+
828
+ // src/runtime/planner-prompt.ts
829
+ var SAFETY_INSTRUCTIONS = `
830
+ ## Safety instructions (mandatory)
831
+ - Do NOT emit JSX, HTML, or raw markup.
832
+ - Do NOT invent node kinds; use only the allowed kinds listed above.
833
+ - Do NOT call undeclared tools.
834
+ - Prefer fewer high-confidence patches over many speculative ones.
835
+ - Explain why a patch helps the user.
836
+ `;
837
+ function compilePlannerPrompt(input) {
838
+ const {
839
+ bundleMeta,
840
+ surfaceId,
841
+ allowedPatchOps,
842
+ allowedSlots,
843
+ allowedNodeKinds,
844
+ actions,
845
+ preferences,
846
+ basePrompt,
847
+ planSummary,
848
+ entity
849
+ } = input;
850
+ const parts = [];
851
+ parts.push(`# Bundle: ${bundleMeta.title} (${bundleMeta.key}@${bundleMeta.version})`);
852
+ parts.push(`# Surface: ${surfaceId}`);
853
+ parts.push("");
854
+ if (basePrompt) {
855
+ parts.push("## Base instructions");
856
+ parts.push(basePrompt);
857
+ parts.push("");
858
+ }
859
+ parts.push("## Allowed patch operations");
860
+ parts.push(allowedPatchOps.join(", "));
861
+ parts.push("");
862
+ parts.push("## Allowed slots");
863
+ parts.push(allowedSlots.join(", "));
864
+ parts.push("");
865
+ parts.push("## Allowed node kinds");
866
+ parts.push(allowedNodeKinds.join(", "));
867
+ parts.push("");
868
+ if (actions.length > 0) {
869
+ parts.push("## Visible actions");
870
+ for (const a of actions) {
871
+ parts.push(`- ${a.actionId}: ${a.title}`);
872
+ }
873
+ parts.push("");
874
+ }
875
+ parts.push("## Current preference profile");
876
+ parts.push(`guidance=${preferences.guidance}, density=${preferences.density}, ` + `dataDepth=${preferences.dataDepth}, control=${preferences.control}, ` + `media=${preferences.media}, pace=${preferences.pace}, narrative=${preferences.narrative}`);
877
+ parts.push("");
878
+ if (planSummary) {
879
+ parts.push("## Current plan summary");
880
+ parts.push(planSummary);
881
+ parts.push("");
882
+ }
883
+ if (entity) {
884
+ parts.push("## Entity context");
885
+ parts.push(`${entity.type}: ${entity.id}`);
886
+ parts.push("");
887
+ }
888
+ parts.push(SAFETY_INSTRUCTIONS.trim());
889
+ return parts.join(`
890
+ `);
891
+ }
892
+
893
+ // src/runtime/planner-tools.ts
894
+ var PROPOSE_PATCH_TOOL_SCHEMA = {
895
+ type: "object",
896
+ properties: {
897
+ proposalId: { type: "string", description: "Unique proposal identifier" },
898
+ ops: {
899
+ type: "array",
900
+ description: "Surface patch operations to propose",
901
+ items: {
902
+ type: "object",
903
+ required: ["op"],
904
+ properties: {
905
+ op: {
906
+ type: "string",
907
+ enum: [
908
+ "insert-node",
909
+ "replace-node",
910
+ "remove-node",
911
+ "move-node",
912
+ "resize-panel",
913
+ "set-layout",
914
+ "reveal-field",
915
+ "hide-field",
916
+ "promote-action",
917
+ "set-focus"
918
+ ]
919
+ },
920
+ slotId: { type: "string" },
921
+ nodeId: { type: "string" },
922
+ toSlotId: { type: "string" },
923
+ index: { type: "number" },
924
+ node: {
925
+ type: "object",
926
+ properties: {
927
+ nodeId: { type: "string" },
928
+ kind: { type: "string" },
929
+ title: { type: "string" },
930
+ props: { type: "object" },
931
+ children: { type: "array" }
932
+ }
933
+ },
934
+ persistKey: { type: "string" },
935
+ sizes: { type: "array", items: { type: "number" } },
936
+ layoutId: { type: "string" },
937
+ fieldId: { type: "string" },
938
+ actionId: { type: "string" },
939
+ placement: {
940
+ type: "string",
941
+ enum: ["header", "inline", "context", "assistant"]
942
+ },
943
+ targetId: { type: "string" }
944
+ }
945
+ }
946
+ }
947
+ },
948
+ required: ["proposalId", "ops"]
949
+ };
950
+ var proposePatchToolConfig = {
951
+ name: "propose-patch",
952
+ description: "Propose surface patches (layout changes, node insertions, etc.) for user approval. " + "Only use allowed ops, slots, and node kinds from the planner context.",
953
+ schema: PROPOSE_PATCH_TOOL_SCHEMA,
954
+ automationSafe: false,
955
+ requiresApproval: true
956
+ };
957
+ function buildSurfacePatchProposal(proposalId, ops) {
958
+ return {
959
+ proposalId,
960
+ source: "assistant",
961
+ ops,
962
+ approvalState: "proposed"
963
+ };
964
+ }
965
+
966
+ // src/runtime/field-renderer-registry.ts
967
+ var FALLBACK_FIELD_KIND = "text";
968
+ var CORE_FIELD_KINDS = {
969
+ text: {
970
+ fieldKind: "text",
971
+ viewer: "text-viewer",
972
+ editor: "text-editor",
973
+ summaryViewer: "text-summary",
974
+ tableCell: "text-cell",
975
+ filters: ["contains", "equals", "startsWith", "endsWith"],
976
+ validators: ["required", "maxLength", "regex"]
977
+ },
978
+ number: {
979
+ fieldKind: "number",
980
+ viewer: "number-viewer",
981
+ editor: "number-editor",
982
+ summaryViewer: "number-summary",
983
+ tableCell: "number-cell",
984
+ filters: ["equals", "gt", "gte", "lt", "lte", "between"],
985
+ validators: ["required", "min", "max", "integer"]
986
+ },
987
+ date: {
988
+ fieldKind: "date",
989
+ viewer: "date-viewer",
990
+ editor: "date-editor",
991
+ summaryViewer: "date-summary",
992
+ tableCell: "date-cell",
993
+ filters: ["equals", "before", "after", "between"],
994
+ validators: ["required", "min", "max"]
995
+ },
996
+ select: {
997
+ fieldKind: "select",
998
+ viewer: "select-viewer",
999
+ editor: "select-editor",
1000
+ summaryViewer: "select-summary",
1001
+ tableCell: "select-cell",
1002
+ filters: ["equals", "in"],
1003
+ validators: ["required", "oneOf"]
1004
+ },
1005
+ checkbox: {
1006
+ fieldKind: "checkbox",
1007
+ viewer: "checkbox-viewer",
1008
+ editor: "checkbox-editor",
1009
+ summaryViewer: "checkbox-summary",
1010
+ tableCell: "checkbox-cell",
1011
+ filters: ["equals"],
1012
+ validators: ["required"]
1013
+ }
1014
+ };
1015
+ var STUB_FIELD_KINDS = {
1016
+ relation: {
1017
+ fieldKind: "relation",
1018
+ viewer: "relation-viewer-stub",
1019
+ summaryViewer: "relation-chip-stub",
1020
+ tableCell: "relation-chip-stub"
1021
+ },
1022
+ rollup: {
1023
+ fieldKind: "rollup",
1024
+ viewer: "rollup-viewer-stub",
1025
+ summaryViewer: "rollup-chip-stub",
1026
+ tableCell: "rollup-cell-stub"
1027
+ },
1028
+ formula: {
1029
+ fieldKind: "formula",
1030
+ viewer: "formula-viewer-stub",
1031
+ summaryViewer: "formula-summary-stub",
1032
+ tableCell: "formula-cell-stub"
1033
+ },
1034
+ people: {
1035
+ fieldKind: "people",
1036
+ viewer: "people-viewer-stub",
1037
+ editor: "people-picker-stub",
1038
+ summaryViewer: "people-chip-stub",
1039
+ tableCell: "people-cell-stub"
1040
+ },
1041
+ options: {
1042
+ fieldKind: "options",
1043
+ viewer: "options-viewer-stub",
1044
+ editor: "options-editor-stub",
1045
+ summaryViewer: "options-chip-stub",
1046
+ tableCell: "options-cell-stub"
1047
+ },
1048
+ instance: {
1049
+ fieldKind: "instance",
1050
+ viewer: "instance-viewer-stub",
1051
+ summaryViewer: "instance-chip-stub",
1052
+ tableCell: "instance-cell-stub"
1053
+ },
1054
+ url: {
1055
+ fieldKind: "url",
1056
+ viewer: "url-viewer",
1057
+ editor: "url-editor",
1058
+ summaryViewer: "url-summary",
1059
+ tableCell: "url-cell",
1060
+ validators: ["required", "regex"]
1061
+ }
1062
+ };
1063
+ function createFieldRendererRegistry() {
1064
+ const all = { ...CORE_FIELD_KINDS, ...STUB_FIELD_KINDS };
1065
+ const fallback = CORE_FIELD_KINDS[FALLBACK_FIELD_KIND];
1066
+ if (!fallback) {
1067
+ throw new Error(`Fallback field kind "${FALLBACK_FIELD_KIND}" not found`);
1068
+ }
1069
+ return {
1070
+ get(kind) {
1071
+ return all[kind];
1072
+ },
1073
+ has(kind) {
1074
+ return kind in all;
1075
+ },
1076
+ getOrFallback(kind) {
1077
+ return all[kind] ?? fallback;
1078
+ }
1079
+ };
1080
+ }
1081
+ function createMutableFieldRendererRegistry() {
1082
+ const base = { ...CORE_FIELD_KINDS, ...STUB_FIELD_KINDS };
1083
+ const custom = new Map;
1084
+ const fallback = CORE_FIELD_KINDS[FALLBACK_FIELD_KIND];
1085
+ if (!fallback) {
1086
+ throw new Error(`Fallback field kind "${FALLBACK_FIELD_KIND}" not found`);
1087
+ }
1088
+ return {
1089
+ get(kind) {
1090
+ return custom.get(kind) ?? base[kind];
1091
+ },
1092
+ has(kind) {
1093
+ return custom.has(kind) || kind in base;
1094
+ },
1095
+ getOrFallback(kind) {
1096
+ return custom.get(kind) ?? base[kind] ?? fallback;
1097
+ },
1098
+ registerFieldRenderer(kind, entry) {
1099
+ if (entry.fieldKind !== kind) {
1100
+ throw new Error(`Field renderer entry fieldKind "${entry.fieldKind}" must match kind "${kind}"`);
1101
+ }
1102
+ custom.set(kind, entry);
1103
+ }
1104
+ };
1105
+ }
1106
+
1107
+ // src/runtime/widget-registry.ts
1108
+ function validateTrust(trust) {
1109
+ if (trust === "ephemeral-ai") {
1110
+ throw new Error("Widgets cannot be registered with ephemeral-ai trust. Registration is a code/package concern.");
1111
+ }
1112
+ if (trust !== "core" && trust !== "workspace" && trust !== "signed-plugin") {
1113
+ throw new Error(`Invalid widget trust: ${trust}`);
1114
+ }
1115
+ }
1116
+ function createWidgetRegistry() {
1117
+ const entries = new Map;
1118
+ return {
1119
+ register(entry) {
1120
+ validateTrust(entry.trust);
1121
+ if (entry.nodeKind !== "custom-widget") {
1122
+ throw new Error(`Widget "${entry.widgetKey}" must have nodeKind "custom-widget"`);
1123
+ }
1124
+ entries.set(entry.widgetKey, entry);
1125
+ },
1126
+ get(widgetKey) {
1127
+ return entries.get(widgetKey);
1128
+ },
1129
+ has(widgetKey) {
1130
+ return entries.has(widgetKey);
1131
+ },
1132
+ list() {
1133
+ return Array.from(entries.values());
1134
+ },
1135
+ listByTrust(trust) {
1136
+ return Array.from(entries.values()).filter((e) => e.trust === trust);
1137
+ }
1138
+ };
1139
+ }
1140
+
1141
+ // src/runtime/extension-registry.ts
1142
+ function createActionRegistry() {
1143
+ const entries = new Map;
1144
+ return {
1145
+ register(entry) {
1146
+ entries.set(entry.actionId, entry);
1147
+ },
1148
+ get(actionId) {
1149
+ return entries.get(actionId);
1150
+ },
1151
+ has(actionId) {
1152
+ return entries.has(actionId);
1153
+ },
1154
+ list() {
1155
+ return Array.from(entries.values());
1156
+ }
1157
+ };
1158
+ }
1159
+ function createCommandRegistry() {
1160
+ const entries = new Map;
1161
+ return {
1162
+ register(entry) {
1163
+ entries.set(entry.commandId, entry);
1164
+ },
1165
+ get(commandId) {
1166
+ return entries.get(commandId);
1167
+ },
1168
+ has(commandId) {
1169
+ return entries.has(commandId);
1170
+ },
1171
+ list() {
1172
+ return Array.from(entries.values());
1173
+ }
1174
+ };
1175
+ }
1176
+ function createBundleExtensionRegistry(options) {
1177
+ const actionRegistry = options.actionRegistry ?? createActionRegistry();
1178
+ const commandRegistry = options.commandRegistry ?? createCommandRegistry();
1179
+ return {
1180
+ registerWidget(entry) {
1181
+ options.widgetRegistry.register(entry);
1182
+ },
1183
+ registerFieldRenderer(kind, entry) {
1184
+ options.fieldRendererRegistry.registerFieldRenderer(kind, entry);
1185
+ },
1186
+ registerAction(entry) {
1187
+ actionRegistry.register(entry);
1188
+ },
1189
+ registerCommand(entry) {
1190
+ commandRegistry.register(entry);
1191
+ }
1192
+ };
1193
+ }
1194
+
1195
+ // src/runtime/overlay-alignment.ts
1196
+ import { applyOverlayModifications } from "@contractspec/lib.overlay-engine/merger";
1197
+ function toOverlayScopeContext(ctx) {
1198
+ return {
1199
+ tenantId: ctx.tenantId,
1200
+ userId: ctx.actorId,
1201
+ device: ctx.device,
1202
+ tags: ctx.featureFlags
1203
+ };
1204
+ }
1205
+ function toOverlayTargetRef(_ctx, target) {
1206
+ return {
1207
+ presentation: target.surfaceId ?? target.routeId,
1208
+ bundleKey: target.bundleKey,
1209
+ surfaceId: target.surfaceId,
1210
+ routeId: target.routeId,
1211
+ entityType: target.entityType
1212
+ };
1213
+ }
1214
+ function toOverlayAppliesTo(ctx, target, scope) {
1215
+ const scopeCtx = toOverlayScopeContext(ctx);
1216
+ return {
1217
+ ...scopeCtx,
1218
+ ...target,
1219
+ ...scope === "user" && ctx.actorId ? { userId: ctx.actorId } : {},
1220
+ ...scope === "workspace" && ctx.workspaceId ? { tags: [...scopeCtx.tags ?? [], `workspace:${ctx.workspaceId}`] } : {}
1221
+ };
1222
+ }
1223
+ function toOverlayRenderableField(field) {
1224
+ return {
1225
+ key: field.fieldId,
1226
+ label: field.title,
1227
+ visible: field.visible,
1228
+ required: field.required,
1229
+ order: 0
1230
+ };
1231
+ }
1232
+ function fromOverlayRenderableField(overlay) {
1233
+ return {
1234
+ fieldId: overlay.key,
1235
+ title: overlay.label ?? overlay.key,
1236
+ visible: overlay.visible ?? true,
1237
+ required: overlay.required ?? false
1238
+ };
1239
+ }
1240
+ function toOverlayRenderable(fields) {
1241
+ return {
1242
+ fields: fields.map(toOverlayRenderableField)
1243
+ };
1244
+ }
1245
+ function mergeOverlayResultIntoFields(fields, merged) {
1246
+ const fieldMap = new Map(fields.map((f) => [f.fieldId, { ...f }]));
1247
+ const result = [];
1248
+ for (const overlay of merged.fields) {
1249
+ const existing = fieldMap.get(overlay.key);
1250
+ if (existing) {
1251
+ result.push({
1252
+ ...existing,
1253
+ title: overlay.label ?? existing.title,
1254
+ visible: overlay.visible ?? existing.visible,
1255
+ required: overlay.required ?? existing.required
1256
+ });
1257
+ }
1258
+ }
1259
+ return result;
1260
+ }
1261
+ function applyEntityFieldOverlays(fields, overlays, options) {
1262
+ if (!overlays.length)
1263
+ return fields;
1264
+ const target = toOverlayRenderable(fields);
1265
+ const merged = applyOverlayModifications(target, overlays, options);
1266
+ return mergeOverlayResultIntoFields(fields, merged);
1267
+ }
1268
+
1269
+ // src/runtime/overlay-signer.ts
1270
+ import {
1271
+ signOverlay,
1272
+ verifyOverlaySignature
1273
+ } from "@contractspec/lib.overlay-engine/signer";
1274
+ function signWorkspaceOverlay(spec, privateKey, options) {
1275
+ return signOverlay(spec, privateKey, options);
1276
+ }
1277
+ function verifyWorkspaceOverlay(overlay) {
1278
+ return verifyOverlaySignature(overlay);
1279
+ }
1280
+
1281
+ // src/runtime/audit-events.ts
1282
+ function generateEventId() {
1283
+ return `evt_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
1284
+ }
1285
+ function emitPatchProposed(audit, args) {
1286
+ audit.emit({
1287
+ eventId: generateEventId(),
1288
+ at: new Date().toISOString(),
1289
+ actorId: args.actorId,
1290
+ source: args.proposal.source === "assistant" ? "assistant" : "user",
1291
+ bundleKey: args.bundleKey,
1292
+ surfaceId: args.surfaceId,
1293
+ eventType: "patch.proposed",
1294
+ payload: {
1295
+ proposalId: args.proposal.proposalId,
1296
+ source: args.proposal.source,
1297
+ opsCount: args.proposal.ops.length
1298
+ }
1299
+ });
1300
+ }
1301
+ function emitPatchApproved(audit, args) {
1302
+ audit.emit({
1303
+ eventId: generateEventId(),
1304
+ at: new Date().toISOString(),
1305
+ actorId: args.actorId,
1306
+ source: "user",
1307
+ bundleKey: args.bundleKey,
1308
+ surfaceId: args.surfaceId,
1309
+ eventType: "patch.approved",
1310
+ payload: {
1311
+ proposalId: args.proposalId,
1312
+ source: args.source,
1313
+ opsCount: args.opsCount,
1314
+ reason: args.reason
1315
+ }
1316
+ });
1317
+ }
1318
+ function emitPatchRejected(audit, args) {
1319
+ audit.emit({
1320
+ eventId: generateEventId(),
1321
+ at: new Date().toISOString(),
1322
+ actorId: args.actorId,
1323
+ source: "user",
1324
+ bundleKey: args.bundleKey,
1325
+ surfaceId: args.surfaceId,
1326
+ eventType: "patch.rejected",
1327
+ payload: {
1328
+ proposalId: args.proposalId,
1329
+ source: args.source,
1330
+ opsCount: args.opsCount,
1331
+ reason: args.reason
1332
+ }
1333
+ });
1334
+ }
1335
+ function emitOverlaySaved(audit, args) {
1336
+ audit.emit({
1337
+ eventId: generateEventId(),
1338
+ at: new Date().toISOString(),
1339
+ actorId: args.actorId,
1340
+ source: "user",
1341
+ bundleKey: args.bundleKey,
1342
+ eventType: "overlay.saved",
1343
+ payload: {
1344
+ overlayId: args.overlayId,
1345
+ scope: args.scope,
1346
+ opsCount: args.opsCount
1347
+ }
1348
+ });
1349
+ }
1350
+ function emitOverlayApplied(audit, args) {
1351
+ audit.emit({
1352
+ eventId: generateEventId(),
1353
+ at: new Date().toISOString(),
1354
+ source: "system",
1355
+ bundleKey: args.bundleKey,
1356
+ surfaceId: args.surfaceId,
1357
+ eventType: "overlay.applied",
1358
+ payload: {
1359
+ overlayId: args.overlayId,
1360
+ opsCount: args.opsCount
1361
+ }
1362
+ });
1363
+ }
1364
+ function emitOverlayFailed(audit, args) {
1365
+ audit.emit({
1366
+ eventId: generateEventId(),
1367
+ at: new Date().toISOString(),
1368
+ source: "system",
1369
+ bundleKey: args.bundleKey,
1370
+ surfaceId: args.surfaceId,
1371
+ eventType: "overlay.failed",
1372
+ payload: {
1373
+ overlayId: args.overlayId,
1374
+ error: args.error
1375
+ }
1376
+ });
1377
+ }
1378
+ function emitPolicyDenied(audit, args) {
1379
+ audit.emit({
1380
+ eventId: generateEventId(),
1381
+ at: new Date().toISOString(),
1382
+ actorId: args.actorId,
1383
+ source: "policy",
1384
+ bundleKey: args.bundleKey,
1385
+ surfaceId: args.surfaceId,
1386
+ eventType: "policy.denied",
1387
+ payload: {
1388
+ targetId: args.targetId,
1389
+ reason: args.reason
1390
+ }
1391
+ });
1392
+ }
1393
+ function emitPolicyRedacted(audit, args) {
1394
+ audit.emit({
1395
+ eventId: generateEventId(),
1396
+ at: new Date().toISOString(),
1397
+ actorId: args.actorId,
1398
+ source: "policy",
1399
+ bundleKey: args.bundleKey,
1400
+ surfaceId: args.surfaceId,
1401
+ eventType: "policy.redacted",
1402
+ payload: {
1403
+ targetId: args.targetId,
1404
+ redactions: args.redactions
1405
+ }
1406
+ });
1407
+ }
1408
+
1409
+ // src/runtime/rollback.ts
1410
+ function rollbackSurfacePatches(plan, approvalStack, count = 1) {
1411
+ if (count <= 0 || approvalStack.length === 0) {
1412
+ return { plan, revertedCount: 0, remainingStack: approvalStack };
1413
+ }
1414
+ const toRevert = approvalStack.slice(-count);
1415
+ const remaining = approvalStack.slice(0, -count);
1416
+ const currentPlan = plan;
1417
+ const allInverseOps = [];
1418
+ for (const meta of toRevert) {
1419
+ if (meta.inverseOps.length > 0) {
1420
+ allInverseOps.push(...meta.inverseOps);
1421
+ }
1422
+ }
1423
+ if (allInverseOps.length === 0) {
1424
+ return { plan, revertedCount: 0, remainingStack: approvalStack };
1425
+ }
1426
+ const result = applySurfacePatch(currentPlan, allInverseOps);
1427
+ return {
1428
+ plan: result.plan,
1429
+ revertedCount: toRevert.length,
1430
+ remainingStack: remaining
1431
+ };
1432
+ }
1433
+
1434
+ // src/runtime/policy-eval.ts
1435
+ function evaluatePatchProposalPolicy(ops, ctx, policy) {
1436
+ const evaluate = policy?.evaluatePatchProposal;
1437
+ if (!evaluate)
1438
+ return "allow";
1439
+ return evaluate(ops, ctx);
1440
+ }
1441
+ function evaluateAndEmitPatchPolicy(ops, ctx, args) {
1442
+ const effect = evaluatePatchProposalPolicy(ops, ctx, args.policy);
1443
+ if (effect === "deny") {
1444
+ if (args.audit) {
1445
+ emitPolicyDenied(args.audit, {
1446
+ bundleKey: args.bundleKey,
1447
+ surfaceId: args.surfaceId,
1448
+ targetId: args.targetId ?? "patch-proposal",
1449
+ reason: "Policy denied patch proposal",
1450
+ actorId: ctx.actorId
1451
+ });
1452
+ }
1453
+ return false;
1454
+ }
1455
+ return true;
1456
+ }
1457
+ export {
1458
+ verifyWorkspaceOverlay,
1459
+ toOverlayTargetRef,
1460
+ toOverlayScopeContext,
1461
+ toOverlayRenderableField,
1462
+ toOverlayRenderable,
1463
+ signWorkspaceOverlay,
1464
+ rollbackSurfacePatches,
1465
+ resolvePreferenceProfile,
1466
+ resolveBundle,
1467
+ proposePatchToolConfig,
1468
+ mergeOverlayResultIntoFields,
1469
+ fromOverlayRenderableField,
1470
+ evaluatePatchProposalPolicy,
1471
+ evaluateAndEmitPatchPolicy,
1472
+ emitPolicyRedacted,
1473
+ emitPolicyDenied,
1474
+ emitPatchRejected,
1475
+ emitPatchProposed,
1476
+ emitPatchApproved,
1477
+ emitOverlaySaved,
1478
+ emitOverlayFailed,
1479
+ emitOverlayApplied,
1480
+ defaultPreferenceAdapter,
1481
+ createWidgetRegistry,
1482
+ createOverrideStoreWithApprovalGate,
1483
+ createMutableFieldRendererRegistry,
1484
+ createInMemoryOverrideStore,
1485
+ createFieldRendererRegistry,
1486
+ createCommandRegistry,
1487
+ createBundleExtensionRegistry,
1488
+ createActionRegistry,
1489
+ compilePlannerPrompt,
1490
+ buildSurfacePatchProposal,
1491
+ buildOverrideTargetKey,
1492
+ buildContext,
1493
+ applySurfacePatch,
1494
+ applyEntityFieldOverlays,
1495
+ PROPOSE_PATCH_TOOL_SCHEMA,
1496
+ FALLBACK_FIELD_KIND
1497
+ };