@qvac/harness 0.7.1 → 1.0.0-rc.1

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 (201) hide show
  1. package/README.md +224 -0
  2. package/dist-lib/index.d.ts +55 -3
  3. package/dist-lib/index.js +721 -236
  4. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.d.ts +1 -0
  5. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.js +10 -0
  6. package/dist-lib/lib/capabilities/completion-engine/actors/answering.d.ts +3 -0
  7. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +17 -0
  8. package/dist-lib/lib/capabilities/completion-engine/actors/approval.d.ts +4 -0
  9. package/dist-lib/lib/capabilities/completion-engine/actors/approval.js +29 -0
  10. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.d.ts +4 -0
  11. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +73 -0
  12. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +41 -0
  13. package/dist-lib/lib/capabilities/completion-engine/actors/execute.js +49 -0
  14. package/dist-lib/lib/capabilities/completion-engine/actors/executing.d.ts +3 -0
  15. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +102 -0
  16. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.d.ts +47 -0
  17. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +87 -0
  18. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.d.ts +2 -0
  19. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.js +32 -0
  20. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.d.ts +3 -0
  21. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.js +75 -0
  22. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.d.ts +3 -0
  23. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.js +152 -0
  24. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.d.ts +3 -0
  25. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +94 -0
  26. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.d.ts +9 -0
  27. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.js +11 -0
  28. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.d.ts +3 -0
  29. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.js +114 -0
  30. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.d.ts +3 -0
  31. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +198 -0
  32. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.d.ts +3 -0
  33. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.js +71 -0
  34. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.d.ts +18 -0
  35. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.js +70 -0
  36. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.d.ts +3 -0
  37. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +60 -0
  38. package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +21 -0
  39. package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +48 -0
  40. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.d.ts +3 -0
  41. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +156 -0
  42. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.d.ts +7 -0
  43. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +61 -0
  44. package/dist-lib/lib/capabilities/completion-engine/driver.d.ts +25 -0
  45. package/dist-lib/lib/capabilities/completion-engine/driver.js +553 -0
  46. package/dist-lib/lib/capabilities/completion-engine/durable.d.ts +61 -0
  47. package/dist-lib/lib/capabilities/completion-engine/durable.js +150 -0
  48. package/dist-lib/lib/capabilities/completion-engine/operations.d.ts +6 -0
  49. package/dist-lib/lib/capabilities/completion-engine/operations.js +239 -0
  50. package/dist-lib/lib/capabilities/completion-engine/ports.d.ts +3 -0
  51. package/dist-lib/lib/capabilities/completion-engine/ports.js +267 -0
  52. package/dist-lib/lib/capabilities/completion-engine/session.d.ts +76 -0
  53. package/dist-lib/lib/capabilities/completion-engine/session.js +70 -0
  54. package/dist-lib/lib/capabilities/completion-loop/index.d.ts +80 -0
  55. package/dist-lib/lib/capabilities/completion-loop/index.js +158 -0
  56. package/dist-lib/lib/capabilities/completion-loop/metrics.d.ts +5 -0
  57. package/dist-lib/lib/capabilities/completion-loop/metrics.js +29 -0
  58. package/dist-lib/lib/capabilities/completion-loop/round.d.ts +33 -0
  59. package/dist-lib/lib/capabilities/completion-loop/round.js +353 -0
  60. package/dist-lib/lib/capabilities/completion-loop/tool-execution.d.ts +60 -0
  61. package/dist-lib/lib/capabilities/completion-loop/tool-execution.js +270 -0
  62. package/dist-lib/lib/capabilities/completion-loop/turns.d.ts +23 -0
  63. package/dist-lib/lib/capabilities/completion-loop/turns.js +112 -0
  64. package/dist-lib/lib/capabilities/completion-runtime.d.ts +46 -0
  65. package/dist-lib/lib/capabilities/completion-runtime.js +74 -0
  66. package/dist-lib/lib/capabilities/completion.d.ts +18 -335
  67. package/dist-lib/lib/capabilities/completion.js +20 -1301
  68. package/dist-lib/lib/capabilities/download.d.ts +1 -1
  69. package/dist-lib/lib/capabilities/download.js +7 -2
  70. package/dist-lib/lib/capabilities/invoke.js +5 -3
  71. package/dist-lib/lib/capabilities/tool-plan.d.ts +35 -0
  72. package/dist-lib/lib/capabilities/tool-plan.js +66 -0
  73. package/dist-lib/lib/capabilities/tool-turns.d.ts +11 -2
  74. package/dist-lib/lib/capabilities/tool-turns.js +30 -2
  75. package/dist-lib/lib/capabilities/types.d.ts +287 -0
  76. package/dist-lib/lib/capabilities/types.js +1 -0
  77. package/dist-lib/lib/completion-lifecycle.d.ts +14 -0
  78. package/dist-lib/lib/completion-lifecycle.js +126 -0
  79. package/dist-lib/lib/connect.d.ts +10 -3
  80. package/dist-lib/lib/connect.js +329 -23
  81. package/dist-lib/lib/contract-types.d.ts +19 -2
  82. package/dist-lib/lib/harness-lifecycle.d.ts +66 -0
  83. package/dist-lib/lib/harness-lifecycle.js +129 -0
  84. package/dist-lib/lib/helpers/detach-json.d.ts +1 -0
  85. package/dist-lib/lib/helpers/detach-json.js +146 -0
  86. package/dist-lib/lib/helpers/markup-scanner.d.ts +26 -0
  87. package/dist-lib/lib/helpers/markup-scanner.js +156 -0
  88. package/dist-lib/lib/helpers/markup-stream-filter.js +138 -35
  89. package/dist-lib/lib/helpers/race-abort.d.ts +9 -4
  90. package/dist-lib/lib/helpers/race-abort.js +2 -7
  91. package/dist-lib/lib/helpers/recovery-required.d.ts +1 -0
  92. package/dist-lib/lib/helpers/recovery-required.js +1 -0
  93. package/dist-lib/lib/helpers/run-outcome.d.ts +10 -2
  94. package/dist-lib/lib/helpers/run-outcome.js +8 -5
  95. package/dist-lib/lib/helpers/tool-call-markup.d.ts +2 -0
  96. package/dist-lib/lib/helpers/tool-call-markup.js +197 -82
  97. package/dist-lib/lib/hooks/built-in/arg-repair.d.ts +1 -0
  98. package/dist-lib/lib/hooks/built-in/arg-repair.js +1 -0
  99. package/dist-lib/lib/hooks/built-in/egress.d.ts +1 -0
  100. package/dist-lib/lib/hooks/built-in/egress.js +2 -1
  101. package/dist-lib/lib/hooks/built-in/sandbox.d.ts +1 -0
  102. package/dist-lib/lib/hooks/built-in/sandbox.js +6 -1
  103. package/dist-lib/lib/hooks/built-in/skill-catalog.d.ts +1 -0
  104. package/dist-lib/lib/hooks/built-in/skill-catalog.js +2 -1
  105. package/dist-lib/lib/hooks/pipeline.d.ts +4 -1
  106. package/dist-lib/lib/hooks/pipeline.js +24 -2
  107. package/dist-lib/lib/hooks/run.d.ts +2 -1
  108. package/dist-lib/lib/hooks/run.js +19 -7
  109. package/dist-lib/lib/hooks/types.d.ts +17 -6
  110. package/dist-lib/lib/machine/completion/actions/approval.d.ts +3 -0
  111. package/dist-lib/lib/machine/completion/actions/approval.js +16 -0
  112. package/dist-lib/lib/machine/completion/actions/completion.d.ts +23 -0
  113. package/dist-lib/lib/machine/completion/actions/completion.js +142 -0
  114. package/dist-lib/lib/machine/completion/actions/skills.d.ts +9 -0
  115. package/dist-lib/lib/machine/completion/actions/skills.js +83 -0
  116. package/dist-lib/lib/machine/completion/actions/tools.d.ts +16 -0
  117. package/dist-lib/lib/machine/completion/actions/tools.js +173 -0
  118. package/dist-lib/lib/machine/completion/context.d.ts +9 -0
  119. package/dist-lib/lib/machine/completion/context.js +44 -0
  120. package/dist-lib/lib/machine/completion/contract.d.ts +6 -0
  121. package/dist-lib/lib/machine/completion/contract.js +1 -0
  122. package/dist-lib/lib/machine/completion/guards/retry.d.ts +9 -0
  123. package/dist-lib/lib/machine/completion/guards/retry.js +25 -0
  124. package/dist-lib/lib/machine/completion/implementations.d.ts +81 -0
  125. package/dist-lib/lib/machine/completion/implementations.js +152 -0
  126. package/dist-lib/lib/machine/completion/machine.d.ts +375 -0
  127. package/dist-lib/lib/machine/completion/machine.js +511 -0
  128. package/dist-lib/lib/machine/completion/ports.d.ts +65 -0
  129. package/dist-lib/lib/machine/completion/ports.js +224 -0
  130. package/dist-lib/lib/machine/completion/rules.d.ts +39 -0
  131. package/dist-lib/lib/machine/completion/rules.js +115 -0
  132. package/dist-lib/lib/machine/completion/types.d.ts +493 -0
  133. package/dist-lib/lib/machine/completion/types.js +1 -0
  134. package/dist-lib/lib/machine/engine.d.ts +93 -0
  135. package/dist-lib/lib/machine/engine.js +483 -0
  136. package/dist-lib/lib/machine/errors.d.ts +44 -0
  137. package/dist-lib/lib/machine/errors.js +373 -0
  138. package/dist-lib/lib/machine/guards.d.ts +2 -0
  139. package/dist-lib/lib/machine/guards.js +6 -0
  140. package/dist-lib/lib/machine/index.d.ts +20 -0
  141. package/dist-lib/lib/machine/index.js +11 -0
  142. package/dist-lib/lib/machine/replay-output.d.ts +24 -0
  143. package/dist-lib/lib/machine/replay-output.js +50 -0
  144. package/dist-lib/lib/machine/stable-stringify.d.ts +1 -0
  145. package/dist-lib/lib/machine/stable-stringify.js +11 -0
  146. package/dist-lib/lib/models/model-pool.d.ts +14 -6
  147. package/dist-lib/lib/models/model-pool.js +95 -30
  148. package/dist-lib/lib/run-log/history-codec.d.ts +23 -0
  149. package/dist-lib/lib/run-log/history-codec.js +973 -0
  150. package/dist-lib/lib/run-log/patch.d.ts +6 -0
  151. package/dist-lib/lib/run-log/patch.js +93 -0
  152. package/dist-lib/lib/run-log/persisted-actor.d.ts +18 -0
  153. package/dist-lib/lib/run-log/persisted-actor.js +218 -0
  154. package/dist-lib/lib/run-log/protocol-types.d.ts +153 -0
  155. package/dist-lib/lib/run-log/protocol-types.js +1 -0
  156. package/dist-lib/lib/run-log/protocol.d.ts +27 -0
  157. package/dist-lib/lib/run-log/protocol.js +1056 -0
  158. package/dist-lib/lib/run-log/replay.d.ts +25 -0
  159. package/dist-lib/lib/run-log/replay.js +129 -0
  160. package/dist-lib/lib/run-log/run-journal.d.ts +125 -0
  161. package/dist-lib/lib/run-log/run-journal.js +783 -0
  162. package/dist-lib/lib/run-log-inspect.d.ts +25 -0
  163. package/dist-lib/lib/run-log-inspect.js +627 -0
  164. package/dist-lib/lib/run-log-protocol.d.ts +2 -0
  165. package/dist-lib/lib/run-log-protocol.js +1 -0
  166. package/dist-lib/lib/run-log-redact.d.ts +27 -0
  167. package/dist-lib/lib/run-log-redact.js +1107 -0
  168. package/dist-lib/lib/serve.js +307 -11
  169. package/dist-lib/lib/spawn.js +3 -2
  170. package/dist-lib/lib/tool-call-display.d.ts +1 -21
  171. package/dist-lib/lib/tools/exec/adapters/python/wheels.js +2 -1
  172. package/dist-lib/lib/tools/exec.d.ts +2 -1
  173. package/dist-lib/lib/tools/exec.js +57 -2
  174. package/dist-lib/lib/tools/generate-image.js +1 -1
  175. package/dist-lib/lib/tools/generate-music.js +1 -1
  176. package/dist-lib/lib/tools/http-request.d.ts +2 -1
  177. package/dist-lib/lib/tools/http-request.js +2 -1
  178. package/dist-lib/lib/tools/mcp-call.js +8 -1
  179. package/dist-lib/lib/tools/operations/builders.d.ts +1 -0
  180. package/dist-lib/lib/tools/operations/builders.js +1 -0
  181. package/dist-lib/lib/tools/operations/compile.js +11 -1
  182. package/dist-lib/lib/tools/operations/manifest.js +1 -0
  183. package/dist-lib/lib/tools/outbound-guard.d.ts +8 -6
  184. package/dist-lib/lib/tools/outbound-guard.js +25 -8
  185. package/dist-lib/lib/tools/skills/catalog.d.ts +1 -0
  186. package/dist-lib/lib/tools/skills/catalog.js +35 -0
  187. package/dist-lib/lib/tools/skills/cli-schema-types.d.ts +1 -0
  188. package/dist-lib/lib/tools/skills/cli-schema.js +25 -0
  189. package/dist-lib/lib/wire.d.ts +5 -0
  190. package/dist-lib/lib/wire.js +128 -5
  191. package/dist-lib/schema/harness.js +20 -5
  192. package/dist-lib/spec/hrpc/messages.js +95 -15
  193. package/dist-lib/spec/hyperschema/index.js +95 -15
  194. package/dist-lib/spec/hyperschema/schema.json +69 -1
  195. package/dist-lib/spec/hyperschema/types.d.ts +10 -3
  196. package/package.json +20 -2
  197. package/patches/@qvac%2Finference@0.20.0.patch +67 -0
  198. package/patches/bare-stow@0.1.5.patch +68 -0
  199. package/patches/hypercore-blob-server@1.15.0.patch +15 -0
  200. package/patches/localwatch@1.4.3.patch +13 -0
  201. package/patches/pyodide@314.0.2.patch +144 -0
@@ -0,0 +1,973 @@
1
+ import { isRecord } from './patch.js';
2
+ import { stableStringify } from '../machine/stable-stringify.js';
3
+ const marker = '$qvacHistory';
4
+ const failureResultType = 'qvac-failure-result-ref';
5
+ export const FAILURE_RESULT_REF_MACHINE_VERSION = 8;
6
+ const baseIndexCache = new WeakMap();
7
+ const compactHistoryCache = new WeakMap();
8
+ const materializedCache = new WeakMap();
9
+ const materializedValueCache = new WeakMap();
10
+ const messageKeyCache = new WeakMap();
11
+ const deepFrozenCache = new WeakMap();
12
+ const validatedHistoryCache = new WeakMap();
13
+ const MATERIALIZED_HISTORY_CACHE_LIMIT = 16;
14
+ export function requestMessages(request) {
15
+ if (!isRecord(request) || !Array.isArray(request.messages))
16
+ return undefined;
17
+ for (const message of request.messages) {
18
+ if (hasFailureResultType(message) && !isRoleContentMessage(message)) {
19
+ throw new Error('Request message uses the reserved failure-result reference type');
20
+ }
21
+ }
22
+ return request.messages;
23
+ }
24
+ export function compactSnapshotHistory(snapshot, base) {
25
+ if (!isRecord(snapshot) ||
26
+ !isRecord(snapshot.context) ||
27
+ !Array.isArray(snapshot.context.history)) {
28
+ return snapshot;
29
+ }
30
+ const refs = snapshotFailureResultRefs(snapshot);
31
+ const history = compactHistory(snapshot.context.history, base, refs);
32
+ if (history === snapshot.context.history) {
33
+ return snapshot;
34
+ }
35
+ return replacePath(snapshot, ['context', 'history'], history);
36
+ }
37
+ export function materializeSnapshotHistory(snapshot, base, allowLegacyInlineFailureResults = false) {
38
+ if (!isRecord(snapshot) || !isRecord(snapshot.context))
39
+ return snapshot;
40
+ const history = snapshot.context.history;
41
+ if (!isHistoryReference(history)) {
42
+ return snapshot;
43
+ }
44
+ const refs = snapshotFailureResultRefs(snapshot, allowLegacyInlineFailureResults);
45
+ const full = materializeHistory(history, base, refs);
46
+ return replacePath(snapshot, ['context', 'history'], full);
47
+ }
48
+ export function compactMachineEvent(event, base) {
49
+ if (!isRecord(event) || typeof event.type !== 'string' || !isRecord(event.output)) {
50
+ return event;
51
+ }
52
+ const paths = eventHistoryPaths(completedActorName(event.type));
53
+ let compacted = event;
54
+ for (const path of paths) {
55
+ compacted = mapPath(compacted, path, (value) => compactSplice(value, base));
56
+ }
57
+ return compacted;
58
+ }
59
+ export function materializeMachineEvent(event, base) {
60
+ if (!isRecord(event) || typeof event.type !== 'string' || !isRecord(event.output)) {
61
+ return event;
62
+ }
63
+ const paths = eventHistoryPaths(completedActorName(event.type));
64
+ let materialized = event;
65
+ for (const path of paths) {
66
+ materialized = mapPath(materialized, path, (value) => materializeSplice(value, base));
67
+ }
68
+ return materialized;
69
+ }
70
+ export function validateSnapshotHistory(snapshot, base, options = {}) {
71
+ const allowLegacyInlineFailureResults = options.allowLegacyInlineFailureResults ?? false;
72
+ const validateMachineContext = options.validateMachineContext ?? true;
73
+ if (!isRecord(snapshot) || !isRecord(snapshot.context)) {
74
+ return;
75
+ }
76
+ const refs = validateMachineContext
77
+ ? snapshotFailureResultRefs(snapshot, allowLegacyInlineFailureResults)
78
+ : undefined;
79
+ const referenced = new Set();
80
+ if (Object.hasOwn(snapshot.context, 'history')) {
81
+ for (const ref of validateHistoryList(snapshot.context.history, base, refs)) {
82
+ referenced.add(ref);
83
+ }
84
+ }
85
+ if (!validateMachineContext) {
86
+ return;
87
+ }
88
+ if (isRecord(snapshot.context.failedCalls) && !Array.isArray(snapshot.context.failedCalls)) {
89
+ for (const value of Object.values(snapshot.context.failedCalls)) {
90
+ if (isRecord(value) && typeof value.resultRef === 'string') {
91
+ referenced.add(value.resultRef);
92
+ }
93
+ }
94
+ }
95
+ if (isRecord(snapshot.context.rejectedCall)) {
96
+ const rejectedCall = snapshot.context.rejectedCall;
97
+ if (Object.hasOwn(rejectedCall, 'resultRef')) {
98
+ if (typeof rejectedCall.resultRef !== 'string' || !refs?.has(rejectedCall.resultRef)) {
99
+ throw new Error('Rejected call points outside the canonical failure result table');
100
+ }
101
+ referenced.add(rejectedCall.resultRef);
102
+ }
103
+ }
104
+ if (refs) {
105
+ for (const ref of refs) {
106
+ if (!referenced.has(ref)) {
107
+ throw new Error('Canonical failure result is not referenced by machine context');
108
+ }
109
+ }
110
+ }
111
+ }
112
+ export function snapshotFailureResultRefs(snapshot, allowLegacyInlineFailureResults = false) {
113
+ if (!isRecord(snapshot) || !isRecord(snapshot.context)) {
114
+ return new Set();
115
+ }
116
+ const { context } = snapshot;
117
+ const refs = new Set();
118
+ const hasFailureResults = Object.hasOwn(context, 'failureResults');
119
+ if (hasFailureResults) {
120
+ if (!isRecord(context.failureResults) || Array.isArray(context.failureResults)) {
121
+ throw new Error('Malformed failure result table');
122
+ }
123
+ for (const key of Object.keys(context.failureResults)) {
124
+ const item = context.failureResults[key];
125
+ if (!key || !validFailureResult(item)) {
126
+ throw new Error('Malformed canonical failure result');
127
+ }
128
+ refs.add(key);
129
+ }
130
+ }
131
+ if (Object.hasOwn(context, 'failedCalls')) {
132
+ if (!isRecord(context.failedCalls) || Array.isArray(context.failedCalls)) {
133
+ throw new Error('Malformed failed-call table');
134
+ }
135
+ for (const value of Object.values(context.failedCalls)) {
136
+ if (!isRecord(value)) {
137
+ throw new Error('Failed call points outside the canonical failure result table');
138
+ }
139
+ if (allowLegacyInlineFailureResults &&
140
+ !Object.hasOwn(value, 'resultRef') &&
141
+ !hasFailureResults &&
142
+ Object.hasOwn(value, 'result')) {
143
+ continue;
144
+ }
145
+ if (typeof value.resultRef !== 'string' ||
146
+ !refs.has(value.resultRef) ||
147
+ !isRecord(value.outcome) ||
148
+ typeof value.outcome.kind !== 'string') {
149
+ throw new Error('Failed call points outside the canonical failure result table');
150
+ }
151
+ }
152
+ }
153
+ return refs;
154
+ }
155
+ export function validateMachineEventHistory(event, base, failureRefs) {
156
+ if (!isRecord(event) || typeof event.type !== 'string' || !isRecord(event.output)) {
157
+ return;
158
+ }
159
+ const paths = eventHistoryPaths(completedActorName(event.type));
160
+ for (const path of paths) {
161
+ const value = getPath(event, path);
162
+ if (value === undefined || value === null) {
163
+ continue;
164
+ }
165
+ if (!isRecord(value) ||
166
+ typeof value.at !== 'number' ||
167
+ !Number.isSafeInteger(value.at) ||
168
+ value.at < 0) {
169
+ throw new Error('Malformed history splice in machine event');
170
+ }
171
+ validateHistoryList(value.messages, base, failureRefs);
172
+ }
173
+ const actor = completedActorName(event.type);
174
+ if (actor !== undefined && FAILURE_RESULT_OUTPUT_ACTORS.has(actor) && failureRefs !== undefined) {
175
+ validateFailureOutput(event.output, failureRefs);
176
+ }
177
+ }
178
+ export function foldMachineEventHistory(previous, patch, options) {
179
+ const filtered = historyProjectionPatch(patch);
180
+ if (!filtered) {
181
+ return previous;
182
+ }
183
+ const projectedEvent = applyProjectionPatch(previous ?? {}, filtered);
184
+ const next = mergeProjection(previous, projectedEvent);
185
+ if (options.validate !== false) {
186
+ validateHistoryProjection(next, options);
187
+ }
188
+ return freezeJson(next);
189
+ }
190
+ function applyProjectionPatch(base, patch) {
191
+ if (isReplacementPatch(patch)) {
192
+ return freezeJson(copyJson(patch.$));
193
+ }
194
+ if (isAppendPatch(patch)) {
195
+ const array = Array.isArray(base) ? base.slice(0, Math.min(base.length, patch.t)) : [];
196
+ return freezeJson(array.concat(copyJson(patch.a)));
197
+ }
198
+ const objectPatch = patch;
199
+ const source = isRecord(base) ? base : Object.create(null);
200
+ let result = source;
201
+ for (const key of objectPatch.d ?? []) {
202
+ if (Object.hasOwn(result, key)) {
203
+ if (result === source) {
204
+ result = cloneRecord(source);
205
+ }
206
+ delete result[key];
207
+ }
208
+ }
209
+ for (const key of Object.keys(objectPatch.o)) {
210
+ const child = objectPatch.o[key];
211
+ if (!child) {
212
+ continue;
213
+ }
214
+ const had = Object.hasOwn(result, key);
215
+ const previous = had ? result[key] : undefined;
216
+ const value = applyProjectionPatch(previous, child);
217
+ if (had && value === previous) {
218
+ continue;
219
+ }
220
+ if (result === source) {
221
+ result = cloneRecord(source);
222
+ }
223
+ setOwn(result, key, value);
224
+ }
225
+ return result === source ? source : Object.freeze(result);
226
+ }
227
+ function compactSplice(value, base) {
228
+ if (!isRecord(value) || !Array.isArray(value.messages))
229
+ return value;
230
+ const messages = compactHistory(value.messages, base);
231
+ if (messages === value.messages) {
232
+ return value;
233
+ }
234
+ return replaceOwn(value, 'messages', messages);
235
+ }
236
+ function materializeSplice(value, base) {
237
+ if (!isRecord(value) || !isHistoryReference(value.messages))
238
+ return value;
239
+ return replaceOwn(value, 'messages', materializeHistory(value.messages, base));
240
+ }
241
+ function compactHistory(messages, base, failureRefs) {
242
+ validateHistoryList(messages, base, failureRefs);
243
+ if (!base?.length || isHistoryReference(messages)) {
244
+ return messages;
245
+ }
246
+ const cacheable = isDeepFrozen(base);
247
+ const cachedByBase = cacheable ? compactHistoryCache.get(messages) : undefined;
248
+ const cached = cachedByBase?.get(base);
249
+ const keys = new Array(messages.length);
250
+ let matchesCached = cached !== undefined && cached.keys.length === messages.length;
251
+ for (let index = 0; index < messages.length; index++) {
252
+ const key = messageKey(messages[index]);
253
+ keys[index] = key;
254
+ if (matchesCached && cached && cached.keys[index] !== key) {
255
+ matchesCached = false;
256
+ }
257
+ }
258
+ if (matchesCached && cached) {
259
+ return cached.value;
260
+ }
261
+ const indexes = indexBase(base);
262
+ let references = 0;
263
+ const items = messages.map((message, offset) => {
264
+ if (isFailureResultReference(message)) {
265
+ return { inline: message };
266
+ }
267
+ const key = keys[offset];
268
+ const indexedItem = indexes.get(key);
269
+ if (indexedItem === undefined) {
270
+ return { inline: message };
271
+ }
272
+ references++;
273
+ return typeof indexedItem === 'number' ? { index: indexedItem } : indexedItem;
274
+ });
275
+ const value = references === 0
276
+ ? messages
277
+ : { [marker]: { version: 1, base: 'request.messages', items } };
278
+ if (cacheable) {
279
+ const memo = cachedByBase ?? new WeakMap();
280
+ memo.set(base, { keys, value });
281
+ if (!cachedByBase) {
282
+ compactHistoryCache.set(messages, memo);
283
+ }
284
+ }
285
+ return value;
286
+ }
287
+ function indexBase(base) {
288
+ const cacheable = isDeepFrozen(base);
289
+ const cached = cacheable ? baseIndexCache.get(base) : undefined;
290
+ if (cached) {
291
+ return cached;
292
+ }
293
+ const indexes = new Map();
294
+ for (let index = 0; index < base.length; index++) {
295
+ if (isFailureResultReference(base[index])) {
296
+ continue;
297
+ }
298
+ const key = messageKey(base[index]);
299
+ if (!indexes.has(key)) {
300
+ indexes.set(key, cacheable ? Object.freeze({ index }) : index);
301
+ }
302
+ }
303
+ if (cacheable) {
304
+ baseIndexCache.set(base, indexes);
305
+ }
306
+ return indexes;
307
+ }
308
+ function materializeHistory(value, base, failureRefs) {
309
+ validateHistoryList(value, base, failureRefs);
310
+ const cacheable = isDeepFrozen(value) && base !== undefined && isDeepFrozen(base);
311
+ const perBase = cacheable ? materializedCache.get(value) : undefined;
312
+ const cached = base && perBase ? perBase.get(base) : undefined;
313
+ if (cached) {
314
+ return cached;
315
+ }
316
+ if (!base) {
317
+ throw new Error('History reference has no original request message base');
318
+ }
319
+ const signature = stableStringify(value);
320
+ const byValue = cacheable ? materializedValueCache.get(base) : undefined;
321
+ const bySignature = byValue?.get(signature);
322
+ if (bySignature) {
323
+ if (cacheable && byValue) {
324
+ byValue.delete(signature);
325
+ byValue.set(signature, bySignature);
326
+ const memo = perBase ?? new WeakMap();
327
+ memo.set(base, bySignature);
328
+ if (!perBase) {
329
+ materializedCache.set(value, memo);
330
+ }
331
+ }
332
+ return bySignature;
333
+ }
334
+ const history = value[marker].items.map((item) => {
335
+ if (isIndexedHistoryItem(item)) {
336
+ const message = base[item.index];
337
+ return isDeepFrozen(message) ? message : freezeJson(copyJson(message));
338
+ }
339
+ return freezeJson(copyJson(item.inline));
340
+ });
341
+ const result = Object.freeze(history);
342
+ if (cacheable) {
343
+ const memo = perBase ?? new WeakMap();
344
+ memo.set(base, result);
345
+ if (!perBase) {
346
+ materializedCache.set(value, memo);
347
+ }
348
+ }
349
+ if (cacheable) {
350
+ const valueMemo = byValue ?? new Map();
351
+ valueMemo.set(signature, result);
352
+ if (valueMemo.size > MATERIALIZED_HISTORY_CACHE_LIMIT) {
353
+ const oldest = valueMemo.keys().next().value;
354
+ if (typeof oldest === 'string') {
355
+ valueMemo.delete(oldest);
356
+ }
357
+ }
358
+ if (!byValue) {
359
+ materializedValueCache.set(base, valueMemo);
360
+ }
361
+ }
362
+ return result;
363
+ }
364
+ function validateHistoryList(value, base, failureRefs) {
365
+ const cacheable = isRecord(value) && isDeepFrozen(value) && (base === undefined || isDeepFrozen(base));
366
+ const memo = cacheable ? validatedHistoryCache.get(value) : undefined;
367
+ const cached = base ? memo?.byBase.get(base) : memo?.withoutBase;
368
+ if (cached) {
369
+ validateFailureReferenceSet(cached, failureRefs);
370
+ return cached;
371
+ }
372
+ const result = collectHistoryFailureReferences(value, base);
373
+ if (cacheable && isRecord(value)) {
374
+ const entries = memo ?? { byBase: new WeakMap() };
375
+ if (base) {
376
+ entries.byBase.set(base, result);
377
+ }
378
+ else {
379
+ entries.withoutBase = result;
380
+ }
381
+ if (!memo) {
382
+ validatedHistoryCache.set(value, entries);
383
+ }
384
+ }
385
+ validateFailureReferenceSet(result, failureRefs);
386
+ return result;
387
+ }
388
+ function collectHistoryFailureReferences(value, base) {
389
+ const references = new Set();
390
+ if (Array.isArray(value)) {
391
+ for (const item of value) {
392
+ const ref = validateHistoryEntry(item);
393
+ if (ref) {
394
+ references.add(ref);
395
+ }
396
+ }
397
+ }
398
+ else if (isHistoryReference(value)) {
399
+ for (const item of validateHistoryReference(value, base)) {
400
+ const ref = validateHistoryReferenceItem(item, base);
401
+ if (ref) {
402
+ references.add(ref);
403
+ }
404
+ }
405
+ }
406
+ else if (isRecord(value) && Object.hasOwn(value, marker)) {
407
+ throw new Error('Malformed history reference');
408
+ }
409
+ else {
410
+ throw new Error('Machine history must be a message list or a validated reference');
411
+ }
412
+ return [...references];
413
+ }
414
+ function validateHistoryReference(value, base) {
415
+ const keys = Object.keys(value);
416
+ const reference = value[marker];
417
+ if (keys.length !== 1 ||
418
+ !isRecord(reference) ||
419
+ Object.keys(reference).length !== 3 ||
420
+ reference.version !== 1 ||
421
+ reference.base !== 'request.messages' ||
422
+ !Array.isArray(reference.items) ||
423
+ !base) {
424
+ throw new Error('Malformed history reference');
425
+ }
426
+ return reference.items;
427
+ }
428
+ function validateHistoryReferenceItem(item, base) {
429
+ if (!isRecord(item)) {
430
+ throw new Error('Malformed history reference item');
431
+ }
432
+ const itemKeys = Object.keys(item);
433
+ if (itemKeys.length !== 1) {
434
+ throw new Error('Malformed history reference item');
435
+ }
436
+ if ('index' in item) {
437
+ const index = item.index;
438
+ if (itemKeys[0] !== 'index' ||
439
+ typeof index !== 'number' ||
440
+ !Number.isSafeInteger(index) ||
441
+ index < 0 ||
442
+ !base ||
443
+ index >= base.length) {
444
+ throw new Error('History reference points outside the original request');
445
+ }
446
+ return undefined;
447
+ }
448
+ if (!isInlineHistoryItem(item)) {
449
+ throw new Error('Malformed history reference item');
450
+ }
451
+ const inline = item.inline;
452
+ if (itemKeys[0] !== 'inline' || !isRecord(inline)) {
453
+ throw new Error('Malformed inline history message');
454
+ }
455
+ return validateHistoryEntry(inline);
456
+ }
457
+ function validateHistoryEntry(value) {
458
+ if (!hasFailureResultType(value) || isRoleContentMessage(value)) {
459
+ return undefined;
460
+ }
461
+ if (!isFailureResultReference(value)) {
462
+ throw new Error('Malformed failure-result reference');
463
+ }
464
+ return value.ref;
465
+ }
466
+ function validateFailureReferenceSet(refs, failureRefs) {
467
+ if (!failureRefs)
468
+ return;
469
+ for (const ref of refs) {
470
+ if (!failureRefs.has(ref)) {
471
+ throw new Error('History points outside the canonical failure result table');
472
+ }
473
+ }
474
+ }
475
+ function historyProjectionPatch(patch) {
476
+ if (isReplacementPatch(patch)) {
477
+ return { $: projectionFromEvent(patch.$) };
478
+ }
479
+ if (!isObjectPatch(patch))
480
+ return undefined;
481
+ const objectPatch = patch;
482
+ const o = Object.create(null);
483
+ if (Object.hasOwn(objectPatch.o, 'type')) {
484
+ o.type = objectPatch.o.type;
485
+ }
486
+ const outputPatch = Object.hasOwn(objectPatch.o, 'output')
487
+ ? outputHistoryPatch(objectPatch.o.output)
488
+ : undefined;
489
+ if (outputPatch) {
490
+ o.output = outputPatch;
491
+ }
492
+ if (!Object.keys(o).length) {
493
+ return undefined;
494
+ }
495
+ return { o };
496
+ }
497
+ function projectionFromEvent(event) {
498
+ if (!isRecord(event))
499
+ return {};
500
+ const projection = Object.create(null);
501
+ if (typeof event.type === 'string') {
502
+ projection.type = event.type;
503
+ }
504
+ if (Object.hasOwn(event, 'output')) {
505
+ projection.output = projectionFromOutput(event.output);
506
+ }
507
+ return projection;
508
+ }
509
+ function outputHistoryPatch(patch) {
510
+ if (isReplacementPatch(patch)) {
511
+ return { $: projectionFromOutput(patch.$) };
512
+ }
513
+ if (!isObjectPatch(patch))
514
+ return undefined;
515
+ const objectPatch = patch;
516
+ const o = Object.create(null);
517
+ const d = [];
518
+ for (const key of ['history', 'preparation', 'preload', 'failed']) {
519
+ if (Object.hasOwn(objectPatch.o, key)) {
520
+ const filtered = key === 'failed'
521
+ ? failureOutputPatch(objectPatch.o[key])
522
+ : outputHistoryChildPatch(key, objectPatch.o[key]);
523
+ if (filtered) {
524
+ o[key] = filtered;
525
+ }
526
+ }
527
+ if (objectPatch.d?.includes(key)) {
528
+ d.push(key);
529
+ }
530
+ }
531
+ if (!Object.keys(o).length && !d.length) {
532
+ return undefined;
533
+ }
534
+ return d.length ? { o, d } : { o };
535
+ }
536
+ function outputHistoryChildPatch(key, child) {
537
+ if (!child)
538
+ return undefined;
539
+ if (key === 'history')
540
+ return child;
541
+ return slotHistoryPatch(child);
542
+ }
543
+ function slotHistoryPatch(patch) {
544
+ if (isReplacementPatch(patch)) {
545
+ return { $: slotProjection(patch.$) };
546
+ }
547
+ if (!isObjectPatch(patch))
548
+ return undefined;
549
+ const objectPatch = patch;
550
+ const o = Object.create(null);
551
+ if (Object.hasOwn(objectPatch.o, 'history')) {
552
+ const child = objectPatch.o.history;
553
+ if (child) {
554
+ o.history = child;
555
+ }
556
+ }
557
+ const d = objectPatch.d?.includes('history') ? ['history'] : [];
558
+ if (!Object.keys(o).length && !d.length) {
559
+ return undefined;
560
+ }
561
+ return d.length ? { o, d } : { o };
562
+ }
563
+ function projectionFromOutput(output) {
564
+ if (!isRecord(output))
565
+ return null;
566
+ const result = Object.create(null);
567
+ if (Object.hasOwn(output, 'history')) {
568
+ result.history = output.history;
569
+ }
570
+ if (Object.hasOwn(output, 'failed')) {
571
+ result.failed = projectFailureOutput(output.failed);
572
+ }
573
+ for (const key of ['preparation', 'preload']) {
574
+ if (!Object.hasOwn(output, key))
575
+ continue;
576
+ result[key] = slotProjection(output[key]);
577
+ }
578
+ return result;
579
+ }
580
+ function slotProjection(value) {
581
+ if (!isRecord(value)) {
582
+ return Object.create(null);
583
+ }
584
+ const result = Object.create(null);
585
+ if (Object.hasOwn(value, 'history')) {
586
+ result.history = value.history;
587
+ }
588
+ return result;
589
+ }
590
+ function mergeProjection(previous, event) {
591
+ const before = isRecord(previous) ? previous : {};
592
+ const next = isRecord(event) ? event : {};
593
+ const result = cloneRecord(before);
594
+ if (Object.hasOwn(next, 'type')) {
595
+ setOwn(result, 'type', next.type);
596
+ }
597
+ if (Object.hasOwn(next, 'output')) {
598
+ const oldOutput = before.output;
599
+ const eventOutput = next.output;
600
+ if (isRecord(oldOutput) && isRecord(eventOutput)) {
601
+ const mergedOutput = cloneRecord(oldOutput);
602
+ for (const key of Object.keys(eventOutput)) {
603
+ setOwn(mergedOutput, key, eventOutput[key]);
604
+ }
605
+ setOwn(result, 'output', mergedOutput);
606
+ }
607
+ else {
608
+ setOwn(result, 'output', eventOutput);
609
+ }
610
+ }
611
+ return result;
612
+ }
613
+ export function validateHistoryProjection(value, options) {
614
+ if (!isRecord(value) || !isRecord(value.output))
615
+ return;
616
+ const { base, failureRefs, validateFailureOutputs = true } = options;
617
+ const actor = typeof value.type === 'string' ? completedActorName(value.type) : undefined;
618
+ const output = value.output;
619
+ if (validateFailureOutputs &&
620
+ failureRefs !== undefined &&
621
+ actor !== undefined &&
622
+ FAILURE_RESULT_OUTPUT_ACTORS.has(actor)) {
623
+ validateFailureOutput(output, failureRefs);
624
+ }
625
+ if (actor === 'preparing') {
626
+ for (const key of ['preparation', 'preload']) {
627
+ validateSlotHistoryProjection(output, key, { base, failureRefs });
628
+ }
629
+ }
630
+ else if (actor !== undefined && HISTORY_OUTPUT_ACTORS.has(actor)) {
631
+ if (Object.hasOwn(output, 'history')) {
632
+ validateHistorySplice(output.history, { base, failureRefs });
633
+ }
634
+ }
635
+ }
636
+ function validateSlotHistoryProjection(output, key, options) {
637
+ if (!Object.hasOwn(output, key))
638
+ return;
639
+ const slot = output[key];
640
+ if (!isRecord(slot)) {
641
+ throw new Error('Malformed history splice container');
642
+ }
643
+ if (Object.hasOwn(slot, 'history')) {
644
+ validateHistorySplice(slot.history, options);
645
+ }
646
+ }
647
+ function validateHistorySplice(value, options) {
648
+ if (!isRecord(value) ||
649
+ typeof value.at !== 'number' ||
650
+ !Number.isSafeInteger(value.at) ||
651
+ value.at < 0) {
652
+ throw new Error('Malformed history splice in machine event');
653
+ }
654
+ validateHistoryList(value.messages, options.base, options.failureRefs);
655
+ }
656
+ function isHistoryReference(value) {
657
+ return isRecord(value) && Object.hasOwn(value, marker);
658
+ }
659
+ function hasFailureResultType(value) {
660
+ return isRecord(value) && value.type === failureResultType;
661
+ }
662
+ function projectFailureOutput(value) {
663
+ if (!isRecord(value)) {
664
+ return value;
665
+ }
666
+ const result = Object.create(null);
667
+ if (Object.hasOwn(value, 'resultRef')) {
668
+ result.resultRef = value.resultRef;
669
+ }
670
+ return result;
671
+ }
672
+ function failureOutputPatch(patch) {
673
+ if (!patch) {
674
+ return undefined;
675
+ }
676
+ if (isReplacementPatch(patch)) {
677
+ return { $: projectFailureOutput(patch.$) };
678
+ }
679
+ if (!isObjectPatch(patch)) {
680
+ return undefined;
681
+ }
682
+ const objectPatch = patch;
683
+ const o = Object.create(null);
684
+ if (Object.hasOwn(objectPatch.o, 'resultRef')) {
685
+ const resultRef = objectPatch.o.resultRef;
686
+ if (resultRef) {
687
+ o.resultRef = resultRef;
688
+ }
689
+ }
690
+ const d = objectPatch.d?.includes('resultRef') ? ['resultRef'] : [];
691
+ if (!Object.keys(o).length && !d.length) {
692
+ return undefined;
693
+ }
694
+ return d.length ? { o, d } : { o };
695
+ }
696
+ function validateFailureOutput(output, refs) {
697
+ if (!Object.hasOwn(output, 'failed')) {
698
+ return;
699
+ }
700
+ const failed = output.failed;
701
+ if (!isRecord(failed) ||
702
+ Object.keys(failed).length !== 1 ||
703
+ typeof failed.resultRef !== 'string' ||
704
+ failed.resultRef.length === 0 ||
705
+ !refs.has(failed.resultRef)) {
706
+ throw new Error('Failure output points outside the canonical failure result table');
707
+ }
708
+ }
709
+ function isRoleContentMessage(value) {
710
+ return isRecord(value) && typeof value.role === 'string' && Object.hasOwn(value, 'content');
711
+ }
712
+ function isFailureResultReference(value) {
713
+ if (!isRecord(value) || !hasFailureResultType(value)) {
714
+ return false;
715
+ }
716
+ const keys = Object.keys(value);
717
+ return (keys.length === 3 &&
718
+ keys.includes('type') &&
719
+ keys.includes('ref') &&
720
+ keys.includes('callId') &&
721
+ typeof value.ref === 'string' &&
722
+ value.ref.length > 0 &&
723
+ typeof value.callId === 'string' &&
724
+ value.callId.length > 0);
725
+ }
726
+ function validFailureResult(value) {
727
+ if (!isRecord(value) ||
728
+ Array.isArray(value) ||
729
+ Object.keys(value).length !== 2 ||
730
+ typeof value.name !== 'string' ||
731
+ value.name.length === 0 ||
732
+ !isRecord(value.result)) {
733
+ return false;
734
+ }
735
+ const resultKeys = Object.keys(value.result);
736
+ return ((resultKeys.length === 1 && Object.hasOwn(value.result, 'value')) ||
737
+ (resultKeys.length === 1 && value.result.undefined === true));
738
+ }
739
+ function isIndexedHistoryItem(item) {
740
+ return Object.hasOwn(item, 'index');
741
+ }
742
+ function isInlineHistoryItem(item) {
743
+ return Object.hasOwn(item, 'inline');
744
+ }
745
+ function isReplacementPatch(patch) {
746
+ return Object.hasOwn(patch, '$');
747
+ }
748
+ function isAppendPatch(patch) {
749
+ return Object.hasOwn(patch, 't');
750
+ }
751
+ function isObjectPatch(patch) {
752
+ return isRecord(Reflect.get(patch, 'o'));
753
+ }
754
+ function completedActorName(type) {
755
+ const prefix = 'xstate.done.actor.';
756
+ if (!type.startsWith(prefix)) {
757
+ return undefined;
758
+ }
759
+ return type.slice(type.lastIndexOf('.') + 1);
760
+ }
761
+ function messageKey(value) {
762
+ if (value && typeof value === 'object') {
763
+ const frozen = isDeepFrozen(value);
764
+ const cached = messageKeyCache.get(value);
765
+ if (frozen && cached) {
766
+ return cached.key;
767
+ }
768
+ if (cached?.primitiveFields &&
769
+ isRecord(value) &&
770
+ hasSamePrimitiveFields(value, cached.primitiveFields)) {
771
+ return cached.key;
772
+ }
773
+ const key = stableStringify(value);
774
+ if (frozen) {
775
+ messageKeyCache.set(value, { key });
776
+ }
777
+ else {
778
+ const primitiveFields = getPrimitiveFields(value);
779
+ if (primitiveFields) {
780
+ messageKeyCache.set(value, { key, primitiveFields });
781
+ }
782
+ }
783
+ return key;
784
+ }
785
+ return stableStringify(value);
786
+ }
787
+ function getPrimitiveFields(value) {
788
+ if (!isRecord(value) || Array.isArray(value)) {
789
+ return undefined;
790
+ }
791
+ const prototype = Object.getPrototypeOf(value);
792
+ if (prototype !== Object.prototype && prototype !== null) {
793
+ return undefined;
794
+ }
795
+ const fields = [];
796
+ for (const key of Object.keys(value)) {
797
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
798
+ if (!descriptor || !('value' in descriptor)) {
799
+ return undefined;
800
+ }
801
+ const field = descriptor.value;
802
+ if (field !== null && !['undefined', 'string', 'number', 'boolean'].includes(typeof field)) {
803
+ return undefined;
804
+ }
805
+ fields.push([key, field]);
806
+ }
807
+ return fields;
808
+ }
809
+ function hasSamePrimitiveFields(value, fields) {
810
+ const keys = Object.keys(value);
811
+ if (keys.length !== fields.length) {
812
+ return false;
813
+ }
814
+ return fields.every(([key, field], index) => {
815
+ if (keys[index] !== key)
816
+ return false;
817
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
818
+ return Boolean(descriptor && 'value' in descriptor && Object.is(descriptor.value, field));
819
+ });
820
+ }
821
+ const HISTORY_OUTPUT_ACTORS = new Set([
822
+ 'preloading',
823
+ 'generating',
824
+ 'resolving',
825
+ 'rejecting',
826
+ 'executing',
827
+ 'wrapping',
828
+ 'loadingSkill'
829
+ ]);
830
+ const FAILURE_RESULT_OUTPUT_ACTORS = new Set(['rejecting', 'executing']);
831
+ function eventHistoryPaths(actor) {
832
+ if (actor === 'preparing') {
833
+ return [
834
+ ['output', 'preparation', 'history'],
835
+ ['output', 'preload', 'history']
836
+ ];
837
+ }
838
+ if (actor !== undefined && HISTORY_OUTPUT_ACTORS.has(actor)) {
839
+ return [['output', 'history']];
840
+ }
841
+ return [];
842
+ }
843
+ function getPath(value, path) {
844
+ let current = value;
845
+ for (const key of path) {
846
+ if (!isRecord(current) || !Object.hasOwn(current, key))
847
+ return undefined;
848
+ current = current[key];
849
+ }
850
+ return current;
851
+ }
852
+ function mapPath(value, path, map) {
853
+ const key = path[0];
854
+ if (key === undefined || !isRecord(value) || !Object.hasOwn(value, key)) {
855
+ return value;
856
+ }
857
+ const current = value[key];
858
+ const updated = path.length === 1 ? map(current) : mapPath(current, path.slice(1), map);
859
+ return updated === current ? value : replaceOwn(value, key, updated);
860
+ }
861
+ function replacePath(value, path, next) {
862
+ return mapPath(value, path, () => next);
863
+ }
864
+ function replaceOwn(value, key, next) {
865
+ const result = Object.create(Object.getPrototypeOf(value));
866
+ for (const ownKey of Object.keys(value)) {
867
+ Object.defineProperty(result, ownKey, {
868
+ value: ownKey === key ? next : value[ownKey],
869
+ writable: true,
870
+ enumerable: true,
871
+ configurable: true
872
+ });
873
+ }
874
+ if (!Object.hasOwn(value, key)) {
875
+ Object.defineProperty(result, key, {
876
+ value: next,
877
+ writable: true,
878
+ enumerable: true,
879
+ configurable: true
880
+ });
881
+ }
882
+ return result;
883
+ }
884
+ function cloneRecord(value) {
885
+ const result = Object.create(Object.getPrototypeOf(value));
886
+ for (const key of Object.keys(value)) {
887
+ setOwn(result, key, value[key]);
888
+ }
889
+ return result;
890
+ }
891
+ function setOwn(record, key, value) {
892
+ Object.defineProperty(record, key, {
893
+ value,
894
+ writable: true,
895
+ enumerable: true,
896
+ configurable: true
897
+ });
898
+ }
899
+ function isDeepFrozen(value) {
900
+ if (value === null || typeof value === 'string' || typeof value === 'boolean') {
901
+ return true;
902
+ }
903
+ if (typeof value === 'number') {
904
+ return Number.isFinite(value);
905
+ }
906
+ if (typeof value !== 'object' || !Object.isFrozen(value)) {
907
+ return false;
908
+ }
909
+ const cached = deepFrozenCache.get(value);
910
+ if (cached !== undefined) {
911
+ return cached;
912
+ }
913
+ deepFrozenCache.set(value, false);
914
+ const frozen = isFrozenJsonContainer(value);
915
+ deepFrozenCache.set(value, frozen);
916
+ return frozen;
917
+ }
918
+ function isFrozenJsonContainer(value) {
919
+ const array = Array.isArray(value);
920
+ const prototype = Object.getPrototypeOf(value);
921
+ if ((array && prototype !== Array.prototype) ||
922
+ (!array && prototype !== Object.prototype && prototype !== null)) {
923
+ return false;
924
+ }
925
+ const keys = Reflect.ownKeys(value);
926
+ const lengthDescriptor = array ? Object.getOwnPropertyDescriptor(value, 'length') : undefined;
927
+ const arrayLength = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
928
+ if (array && typeof arrayLength !== 'number') {
929
+ return false;
930
+ }
931
+ for (const key of keys) {
932
+ if (typeof key !== 'string' || !isFrozenJsonProperty(value, key, arrayLength)) {
933
+ return false;
934
+ }
935
+ }
936
+ return !array || keys.length === arrayLength + 1;
937
+ }
938
+ function isFrozenJsonProperty(value, key, arrayLength) {
939
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
940
+ if (!descriptor || !('value' in descriptor)) {
941
+ return false;
942
+ }
943
+ if (typeof arrayLength === 'number' && key === 'length') {
944
+ return descriptor.value === arrayLength;
945
+ }
946
+ if (!descriptor.enumerable) {
947
+ return false;
948
+ }
949
+ if (typeof arrayLength === 'number') {
950
+ const index = Number(key);
951
+ if (!Number.isSafeInteger(index) ||
952
+ index < 0 ||
953
+ String(index) !== key ||
954
+ index >= arrayLength) {
955
+ return false;
956
+ }
957
+ }
958
+ return isDeepFrozen(descriptor.value);
959
+ }
960
+ function copyJson(value) {
961
+ if (value === undefined)
962
+ return undefined;
963
+ return JSON.parse(JSON.stringify(value));
964
+ }
965
+ function freezeJson(value) {
966
+ if (value && typeof value === 'object' && !Object.isFrozen(value)) {
967
+ for (const child of Object.values(value)) {
968
+ freezeJson(child);
969
+ }
970
+ Object.freeze(value);
971
+ }
972
+ return value;
973
+ }