@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,373 @@
1
+ export class RecoveryRequired extends Error {
2
+ operation;
3
+ constructor(operation) {
4
+ super(`The outcome of ${operation.name} (${operation.id}) is uncertain. Supply its result or explicitly authorize a retry.${operation.reason ? ` ${operation.reason}` : ''}`);
5
+ this.operation = operation;
6
+ }
7
+ }
8
+ export class AbortedError extends Error {
9
+ constructor() {
10
+ super('aborted');
11
+ this.name = 'AbortedError';
12
+ Error.captureStackTrace?.(this, AbortedError);
13
+ }
14
+ }
15
+ export class ToolPolicyChanged extends Error {
16
+ constructor(tool) {
17
+ super(`Tool effect changed after approval: ${tool}`);
18
+ this.name = 'ToolPolicyChanged';
19
+ }
20
+ }
21
+ export const EFFECT_DRAIN_TIMEOUT_MS = 5000;
22
+ const TIMED_OUT = Symbol('timed-out');
23
+ export async function raceDrain(promise, timeoutMs = EFFECT_DRAIN_TIMEOUT_MS) {
24
+ let timer;
25
+ const timeout = new Promise((resolve) => {
26
+ timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
27
+ });
28
+ try {
29
+ return await Promise.race([promise, timeout]);
30
+ }
31
+ finally {
32
+ if (timer !== undefined) {
33
+ clearTimeout(timer);
34
+ }
35
+ }
36
+ }
37
+ export function didDrainTimeOut(value) {
38
+ return value === TIMED_OUT;
39
+ }
40
+ const CAUSE_DEPTH = 3;
41
+ function tagCause(error, depth) {
42
+ if (depth <= 0 || error === undefined || error === null)
43
+ return undefined;
44
+ const object = typeof error === 'object' || typeof error === 'function' ? error : undefined;
45
+ const ownMessage = object ? ownData(object, 'message') : undefined;
46
+ let message;
47
+ if (error instanceof Error) {
48
+ message = error.message;
49
+ }
50
+ else if (typeof ownMessage === 'string') {
51
+ message = ownMessage;
52
+ }
53
+ else {
54
+ message = errorMessage(error);
55
+ }
56
+ let name = 'Error';
57
+ if (error instanceof Error) {
58
+ name = error.name;
59
+ }
60
+ else if (object && typeof ownData(object, 'name') === 'string') {
61
+ name = ownData(object, 'name');
62
+ }
63
+ const code = object && typeof ownData(object, 'code') === 'string'
64
+ ?
65
+ ownData(object, 'code')
66
+ : undefined;
67
+ const stack = error instanceof Error ? error.stack : undefined;
68
+ const cause = object ? tagCause(ownData(object, 'cause'), depth - 1) : undefined;
69
+ const taggedCause = { message, name };
70
+ if (code) {
71
+ taggedCause.code = code;
72
+ }
73
+ if (stack) {
74
+ taggedCause.stack = stack;
75
+ }
76
+ if (cause) {
77
+ taggedCause.cause = cause;
78
+ }
79
+ return taggedCause;
80
+ }
81
+ function ownData(object, key) {
82
+ try {
83
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
84
+ return descriptor && 'value' in descriptor ? descriptor.value : undefined;
85
+ }
86
+ catch {
87
+ return undefined;
88
+ }
89
+ }
90
+ function jsonClone(value) {
91
+ const active = new Set();
92
+ const clone = (current) => {
93
+ if (current === null ||
94
+ typeof current === 'string' ||
95
+ typeof current === 'boolean' ||
96
+ (typeof current === 'number' && Number.isFinite(current))) {
97
+ return current;
98
+ }
99
+ if (typeof current !== 'object')
100
+ throw new TypeError('Not JSON data');
101
+ if (active.has(current))
102
+ throw new TypeError('Circular JSON data');
103
+ active.add(current);
104
+ try {
105
+ if (Array.isArray(current)) {
106
+ const result = [];
107
+ for (let index = 0; index < current.length; index++) {
108
+ const descriptor = Object.getOwnPropertyDescriptor(current, String(index));
109
+ result.push(descriptor && 'value' in descriptor ? clone(descriptor.value) : null);
110
+ }
111
+ return result;
112
+ }
113
+ const prototype = Object.getPrototypeOf(current);
114
+ if (prototype !== Object.prototype && prototype !== null) {
115
+ throw new TypeError('Not a plain JSON object');
116
+ }
117
+ const result = {};
118
+ for (const key of Object.keys(current)) {
119
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
120
+ if (!descriptor || !('value' in descriptor)) {
121
+ throw new TypeError('Not JSON data');
122
+ }
123
+ Object.defineProperty(result, key, {
124
+ value: clone(descriptor.value),
125
+ enumerable: true,
126
+ configurable: true,
127
+ writable: true
128
+ });
129
+ }
130
+ return result;
131
+ }
132
+ finally {
133
+ active.delete(current);
134
+ }
135
+ };
136
+ return clone(value);
137
+ }
138
+ function normalizeOperation(value) {
139
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
140
+ return undefined;
141
+ }
142
+ let argsDescriptor;
143
+ try {
144
+ argsDescriptor = Object.getOwnPropertyDescriptor(value, 'args');
145
+ }
146
+ catch {
147
+ return undefined;
148
+ }
149
+ const id = ownData(value, 'id');
150
+ const name = ownData(value, 'name');
151
+ const effect = ownData(value, 'effect');
152
+ const args = ownData(value, 'args');
153
+ if (typeof id !== 'string' ||
154
+ typeof name !== 'string' ||
155
+ (effect !== 'read' && effect !== 'write') ||
156
+ !argsDescriptor ||
157
+ !('value' in argsDescriptor)) {
158
+ return undefined;
159
+ }
160
+ try {
161
+ const reason = ownData(value, 'reason');
162
+ const operation = {
163
+ id,
164
+ name,
165
+ args: jsonClone(args),
166
+ effect
167
+ };
168
+ if (typeof reason === 'string') {
169
+ operation.reason = reason;
170
+ }
171
+ return operation;
172
+ }
173
+ catch {
174
+ return undefined;
175
+ }
176
+ }
177
+ function normalizeTaggedEngineError(value) {
178
+ if (!value || typeof value !== 'object')
179
+ return undefined;
180
+ const tag = ownData(value, 'tag');
181
+ const message = ownData(value, 'message');
182
+ const name = ownData(value, 'name');
183
+ const stack = ownData(value, 'stack');
184
+ if (typeof message !== 'string' || typeof name !== 'string') {
185
+ return undefined;
186
+ }
187
+ if (tag === 'aborted') {
188
+ const aborted = { tag, message, name };
189
+ if (typeof stack === 'string') {
190
+ aborted.stack = stack;
191
+ }
192
+ return aborted;
193
+ }
194
+ if (tag === 'failed') {
195
+ const code = ownData(value, 'code');
196
+ const cause = ownData(value, 'cause');
197
+ const taggedCause = tagCause(cause, CAUSE_DEPTH);
198
+ const failed = {
199
+ tag,
200
+ message,
201
+ name
202
+ };
203
+ if (typeof code === 'string') {
204
+ failed.code = code;
205
+ }
206
+ if (typeof stack === 'string') {
207
+ failed.stack = stack;
208
+ }
209
+ if (taggedCause) {
210
+ failed.cause = taggedCause;
211
+ }
212
+ return failed;
213
+ }
214
+ if (tag === 'recovery') {
215
+ const operation = normalizeOperation(ownData(value, 'operation'));
216
+ if (!operation) {
217
+ return undefined;
218
+ }
219
+ const recovery = {
220
+ tag,
221
+ message,
222
+ name,
223
+ operation
224
+ };
225
+ if (typeof stack === 'string') {
226
+ recovery.stack = stack;
227
+ }
228
+ return recovery;
229
+ }
230
+ return undefined;
231
+ }
232
+ export function tagError(error) {
233
+ const tagged = normalizeTaggedEngineError(error);
234
+ if (tagged) {
235
+ return tagged;
236
+ }
237
+ const object = error && (typeof error === 'object' || typeof error === 'function') ? error : undefined;
238
+ const ownMessage = object ? ownData(object, 'message') : undefined;
239
+ let message;
240
+ if (error instanceof Error) {
241
+ message = error.message;
242
+ }
243
+ else if (typeof ownMessage === 'string') {
244
+ message = ownMessage;
245
+ }
246
+ else {
247
+ message = errorMessage(error);
248
+ }
249
+ let name = 'Error';
250
+ if (error instanceof Error) {
251
+ name = error.name;
252
+ }
253
+ else if (object && typeof ownData(object, 'name') === 'string') {
254
+ name = ownData(object, 'name');
255
+ }
256
+ const stack = error instanceof Error ? error.stack : undefined;
257
+ if (error instanceof RecoveryRequired) {
258
+ const operation = normalizeOperation(error.operation);
259
+ if (operation) {
260
+ const recovery = {
261
+ tag: 'recovery',
262
+ message,
263
+ name,
264
+ operation
265
+ };
266
+ if (stack) {
267
+ recovery.stack = stack;
268
+ }
269
+ return recovery;
270
+ }
271
+ }
272
+ if (error instanceof AbortedError) {
273
+ const aborted = { tag: 'aborted', message, name };
274
+ if (stack) {
275
+ aborted.stack = stack;
276
+ }
277
+ return aborted;
278
+ }
279
+ let code;
280
+ if (object && typeof ownData(object, 'code') === 'string') {
281
+ code = ownData(object, 'code');
282
+ }
283
+ const cause = object ? tagCause(ownData(object, 'cause'), CAUSE_DEPTH) : undefined;
284
+ const failed = {
285
+ tag: 'failed',
286
+ message,
287
+ name
288
+ };
289
+ if (code) {
290
+ failed.code = code;
291
+ }
292
+ if (stack) {
293
+ failed.stack = stack;
294
+ }
295
+ if (cause) {
296
+ failed.cause = cause;
297
+ }
298
+ return failed;
299
+ }
300
+ export function rehydrateError(value, fallbackMessage = 'Completion failed') {
301
+ if (!value) {
302
+ return new Error(fallbackMessage);
303
+ }
304
+ const normalized = normalizeTaggedEngineError(value) ?? tagError(value);
305
+ if (normalized.tag === 'recovery') {
306
+ const error = new RecoveryRequired(normalized.operation);
307
+ error.message = normalized.message || fallbackMessage;
308
+ error.name = normalized.name || error.name;
309
+ if (normalized.stack) {
310
+ error.stack = normalized.stack;
311
+ }
312
+ return error;
313
+ }
314
+ if (normalized.tag === 'aborted') {
315
+ const error = new AbortedError();
316
+ error.message = normalized.message || fallbackMessage;
317
+ error.name = normalized.name || error.name;
318
+ if (normalized.stack) {
319
+ error.stack = normalized.stack;
320
+ }
321
+ return error;
322
+ }
323
+ const options = {};
324
+ if (normalized.cause) {
325
+ options.cause = rehydrateCause(normalized.cause);
326
+ }
327
+ const error = new Error(normalized.message || fallbackMessage, options);
328
+ error.name = normalized.name || 'Error';
329
+ if (normalized.code) {
330
+ Object.assign(error, { code: normalized.code });
331
+ }
332
+ if (normalized.stack) {
333
+ error.stack = normalized.stack;
334
+ }
335
+ return error;
336
+ }
337
+ function rehydrateCause(value, depth = CAUSE_DEPTH) {
338
+ const cause = value.cause && depth > 1 ? rehydrateCause(value.cause, depth - 1) : undefined;
339
+ const error = new Error(value.message, cause ? { cause } : undefined);
340
+ error.name = value.name;
341
+ if (value.code) {
342
+ Object.assign(error, { code: value.code });
343
+ }
344
+ if (value.stack) {
345
+ error.stack = value.stack;
346
+ }
347
+ return error;
348
+ }
349
+ function errorMessage(value) {
350
+ if (typeof value === 'string')
351
+ return value;
352
+ if (value && typeof value === 'object') {
353
+ const message = ownData(value, 'message');
354
+ if (typeof message === 'string') {
355
+ return message;
356
+ }
357
+ }
358
+ if (value === null) {
359
+ return 'null';
360
+ }
361
+ if (value === undefined) {
362
+ return 'undefined';
363
+ }
364
+ if (typeof value === 'object') {
365
+ try {
366
+ return JSON.stringify(value);
367
+ }
368
+ catch {
369
+ return '[object with circular references]';
370
+ }
371
+ }
372
+ return String(value);
373
+ }
@@ -0,0 +1,2 @@
1
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
2
+ export declare function errorMessage(error: unknown): string;
@@ -0,0 +1,6 @@
1
+ export function isRecord(value) {
2
+ return typeof value === 'object' && value !== null;
3
+ }
4
+ export function errorMessage(error) {
5
+ return error instanceof Error ? error.message : String(error);
6
+ }
@@ -0,0 +1,20 @@
1
+ export { completionMachineDefinition, runEngine, type CompletionSnapshot as EngineSnapshot, type EngineResult, type EngineRun, type EngineBoundaryOptions, type EngineOperationPort } from './engine.ts';
2
+ export type { MachineDefinitionNode, MachineTransitionDefinition } from './engine.ts';
3
+ export { contextFromBoot, requestFromBoot } from '../run-log/protocol.ts';
4
+ export { brandCapturedRecord, isCapturedJson } from '../run-log/protocol.ts';
5
+ export type { EngineBoot, EngineStep, MachineTransitionTrace, HistoryReference } from '../run-log/protocol.ts';
6
+ export { emptyCompletionContext } from './completion/context.ts';
7
+ export { createCompletionMachine } from './completion/machine.ts';
8
+ export { replayRun, ReplayDivergence, ReplayTraceDivergence } from '../run-log/replay.ts';
9
+ export { createInvokeTracker } from './completion/ports.ts';
10
+ export type { EnginePorts, Invocation, InvokeTracker } from './completion/ports.ts';
11
+ export type { CompletionContext as CompletionMachineContext, CompletionActorOutput, CompletionActorOutputs, CompletionHistoryEntry, FailureResultReference, FailureResultValue, SettledCall, StoredFailureResult, Message, RunBackendUse, RunBudget, RunLifecycleEvent, RunOutcome, ToolCallDisplay, ToolExecutionOutcome, TrustedOrigin, TrustedSource, TrustedSourceFacts, ToolEffect, ToolDecision, ToolActivityEvent, ResourceDescriptor, ResolvedCallSnapshot, ToolPreflight, ToolExecutionSnapshot, ToolDonePayload, HookRejection, DirectToolOutput, SearchingOutput, PreparingOutput, InspectingOutput, SkillLoadOutput, SkillPreloadState, PreloadInspectionOutput, ResolvingOutput, ApprovalGate, ToolPreflightFacts, ToolPolicyRejection, RoundDonePayload, ToolRejectedPayload, DirectToolRejectedPayload, PreparingPayload, SkillReadyPayload, SkillFailedPayload, AnsweredPayload, ToolsResolvedPayload, ToolStartedPayload } from './completion/types.ts';
12
+ export type { EngineError, Operation } from './errors.ts';
13
+ export { AbortedError, RecoveryRequired, ToolPolicyChanged, didDrainTimeOut, raceDrain, rehydrateError, tagError } from './errors.ts';
14
+ export { stableStringify } from './stable-stringify.ts';
15
+ export { MAX_BLOCKED_REPEATS, MAX_CAPPED_ROUNDS, MAX_MUTE_ROUNDS, MAX_TOOL_ROUNDS, TOOL_CONTINUATION_NUDGE, foldMetrics, failureResultRef, isFailureResultReference, readFailureResult, storeFailureResult, spliceFrom, toolCallKey, TOOL_DENIED_MESSAGE, toolDeniedResult } from './completion/rules.ts';
16
+ export type { MetricsState } from './completion/rules.ts';
17
+ export { MACHINE_VERSION, RUN_LOG_VERSION, XSTATE_VERSION, RunIncompatible, readRunLog, copy, safeForkLength, unresolvedWriteIds, unsettledRedactedIds, forkRepeatsSettledWrite, recoveryAdvice } from '../run-log/run-journal.ts';
18
+ export type { ToolRejection } from './completion/types.ts';
19
+ export type { MachineLog } from '../run-log/persisted-actor.ts';
20
+ export type { ForkSegment, RecoveryResolution, Operation as RecoveryOperation, RunRecord } from '../run-log/run-journal.ts';
@@ -0,0 +1,11 @@
1
+ export { completionMachineDefinition, runEngine } from './engine.js';
2
+ export { contextFromBoot, requestFromBoot } from '../run-log/protocol.js';
3
+ export { brandCapturedRecord, isCapturedJson } from '../run-log/protocol.js';
4
+ export { emptyCompletionContext } from './completion/context.js';
5
+ export { createCompletionMachine } from './completion/machine.js';
6
+ export { replayRun, ReplayDivergence, ReplayTraceDivergence } from '../run-log/replay.js';
7
+ export { createInvokeTracker } from './completion/ports.js';
8
+ export { AbortedError, RecoveryRequired, ToolPolicyChanged, didDrainTimeOut, raceDrain, rehydrateError, tagError } from './errors.js';
9
+ export { stableStringify } from './stable-stringify.js';
10
+ export { MAX_BLOCKED_REPEATS, MAX_CAPPED_ROUNDS, MAX_MUTE_ROUNDS, MAX_TOOL_ROUNDS, TOOL_CONTINUATION_NUDGE, foldMetrics, failureResultRef, isFailureResultReference, readFailureResult, storeFailureResult, spliceFrom, toolCallKey, TOOL_DENIED_MESSAGE, toolDeniedResult } from './completion/rules.js';
11
+ export { MACHINE_VERSION, RUN_LOG_VERSION, XSTATE_VERSION, RunIncompatible, readRunLog, copy, safeForkLength, unresolvedWriteIds, unsettledRedactedIds, forkRepeatsSettledWrite, recoveryAdvice } from '../run-log/run-journal.js';
@@ -0,0 +1,24 @@
1
+ import type { EngineEvent } from './completion/types.ts';
2
+ export interface ReplayedRoundText {
3
+ contentText: string;
4
+ thinkingText?: string;
5
+ }
6
+ interface ReplayEvent extends EngineEvent {
7
+ text: string;
8
+ }
9
+ interface ReplayFinalRoundArgs {
10
+ runId: string;
11
+ round: number;
12
+ last: ReplayedRoundText;
13
+ response: string | undefined;
14
+ }
15
+ interface ReplaySuccessOutputArgs {
16
+ runId: string;
17
+ round: unknown;
18
+ context: Record<string, unknown> | undefined;
19
+ output: unknown;
20
+ }
21
+ export declare function readLastRoundText(context: Record<string, unknown> | undefined): ReplayedRoundText | undefined;
22
+ export declare function replayFinalRound(args: ReplayFinalRoundArgs): Generator<ReplayEvent>;
23
+ export declare function replaySuccessOutput(args: ReplaySuccessOutputArgs): Generator<ReplayEvent>;
24
+ export {};
@@ -0,0 +1,50 @@
1
+ import { isRecord } from '../run-log/patch.js';
2
+ export function readLastRoundText(context) {
3
+ const lastRound = context?.lastRound;
4
+ if (!isRecord(lastRound)) {
5
+ return undefined;
6
+ }
7
+ return {
8
+ contentText: typeof lastRound.contentText === 'string' ? lastRound.contentText : '',
9
+ thinkingText: typeof lastRound.thinkingText === 'string' ? lastRound.thinkingText : undefined
10
+ };
11
+ }
12
+ function* replayRoundText(runId, round, last) {
13
+ if (last.thinkingText) {
14
+ yield { type: 'thinking', text: last.thinkingText, runId, round };
15
+ }
16
+ if (last.contentText) {
17
+ yield { type: 'content', text: last.contentText, runId, round };
18
+ }
19
+ }
20
+ export function* replayFinalRound(args) {
21
+ const { runId, round, last, response } = args;
22
+ yield* replayRoundText(runId, round, last);
23
+ if (typeof response !== 'string') {
24
+ return;
25
+ }
26
+ const prefix = last.contentText;
27
+ if (response.length > prefix.length && response.startsWith(prefix)) {
28
+ const remainder = response.slice(prefix.length);
29
+ if (remainder) {
30
+ yield { type: 'content', text: remainder, runId, round };
31
+ }
32
+ }
33
+ }
34
+ export function* replaySuccessOutput(args) {
35
+ const { runId, round, context, output } = args;
36
+ if (!isRecord(output) || output.status !== 'success' || typeof output.response !== 'string') {
37
+ return;
38
+ }
39
+ const lastRound = readLastRoundText(context);
40
+ if (lastRound && typeof round === 'number') {
41
+ yield* replayFinalRound({ runId, round, last: lastRound, response: output.response });
42
+ return;
43
+ }
44
+ yield {
45
+ type: 'content',
46
+ text: output.response,
47
+ runId,
48
+ round: typeof round === 'number' ? round : undefined
49
+ };
50
+ }
@@ -0,0 +1 @@
1
+ export declare function stableStringify(value: unknown): string;
@@ -0,0 +1,11 @@
1
+ import { isRecord } from './guards.js';
2
+ export function stableStringify(value) {
3
+ if (Array.isArray(value)) {
4
+ return `[${value.map(stableStringify).join(',')}]`;
5
+ }
6
+ if (isRecord(value)) {
7
+ const keys = Object.keys(value).sort();
8
+ return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(',')}}`;
9
+ }
10
+ return JSON.stringify(value);
11
+ }
@@ -11,6 +11,9 @@ export interface ModelState {
11
11
  [key: string]: unknown;
12
12
  }
13
13
  export type NotifyFn = (state: ModelState) => void;
14
+ export declare class ModelUnloadError extends Error {
15
+ constructor(cause: unknown);
16
+ }
14
17
  export interface LlmDeviceConfig {
15
18
  device?: 'cpu' | 'gpu';
16
19
  gpuLayers?: number;
@@ -30,8 +33,8 @@ export interface CancelSdk {
30
33
  }
31
34
  export interface LoadTracker {
32
35
  readonly draining: boolean;
33
- track(pending: TrackedLoad): TrackedLoad;
34
- drain(sdk: CancelSdk): void;
36
+ track(pending: TrackedLoad, sdk?: Partial<CancelSdk>): TrackedLoad;
37
+ drain(sdk?: Partial<CancelSdk>): void;
35
38
  undrain(): void;
36
39
  }
37
40
  export declare function createLoadTracker(): LoadTracker;
@@ -75,7 +78,7 @@ interface PuddleEntry {
75
78
  declare class ModelPuddle extends ResourcePuddle<string, ModelHandle> {
76
79
  readonly resources: Map<string, PuddleEntry>;
77
80
  }
78
- type PoolLike = ModelPuddle;
81
+ export type PoolLike = ModelPuddle;
79
82
  export declare function modelOf(key: string): string;
80
83
  export declare function releaseModel(pool: PoolLike, key: string): void;
81
84
  export declare function loadedModels(pool: PoolLike): LoadedModel[];
@@ -131,18 +134,22 @@ interface LoadClockGuard {
131
134
  race: (pending: TrackedLoad) => Promise<string>;
132
135
  }
133
136
  export declare function loadClockGuard(model: string, sdk: Partial<CancelSdk>, { now, monoMs, sampleMs, jumpMs }?: LoadClockGuardOptions): LoadClockGuard;
137
+ export declare function resolveCtxSize(loadSdk: LoadSdk, model: string, opts: ModelLoadOptions): Promise<number>;
138
+ type UnloadFailureFn = (modelId: string, error: unknown) => void;
134
139
  export interface ModelPoolOptions {
135
140
  deviceConfig?: LlmDeviceConfig;
136
141
  loads?: LoadTracker;
142
+ onUnloadFailure?: UnloadFailureFn | undefined;
137
143
  }
138
- export declare function createModelPool(loadSdk: LoadSdk, notify: NotifyFn, { deviceConfig, loads }?: ModelPoolOptions): ModelPuddle;
144
+ export declare function createModelPool(loadSdk: LoadSdk, notify: NotifyFn, { deviceConfig, loads, onUnloadFailure }?: ModelPoolOptions): ModelPuddle;
139
145
  export interface EmbedPoolOptions {
140
146
  faults: GpuFaultState;
141
147
  batchSize?: number;
142
148
  openclCacheDir?: (() => string | undefined) | undefined;
143
149
  loads?: LoadTracker;
150
+ onUnloadFailure?: UnloadFailureFn | undefined;
144
151
  }
145
- export declare function createEmbedPool(loadSdk: LoadEmbedSdk, notify: NotifyFn, { faults, batchSize, openclCacheDir, loads }: EmbedPoolOptions): ModelPuddle;
152
+ export declare function createEmbedPool(loadSdk: LoadEmbedSdk, notify: NotifyFn, { faults, batchSize, openclCacheDir, loads, onUnloadFailure }: EmbedPoolOptions): ModelPuddle;
146
153
  export type VoiceLifecycleSdk = Pick<SdkModule, 'unloadModel' | 'modelRegistrySearch'> & Partial<CancelSdk> & {
147
154
  loadModel(params: {
148
155
  modelSrc: ModelSrc;
@@ -158,6 +165,7 @@ export interface VoicePoolOptions {
158
165
  modelConfig?: Record<string, unknown> | undefined;
159
166
  openclCacheDir?: (() => string | undefined) | undefined;
160
167
  loads?: LoadTracker;
168
+ onUnloadFailure?: UnloadFailureFn | undefined;
161
169
  }
162
- export declare function createVoicePool(loadSdk: LoadVoiceSdk, notify: NotifyFn, { modelType, addon, modelConfig, openclCacheDir, loads }: VoicePoolOptions): ModelPuddle;
170
+ export declare function createVoicePool(loadSdk: LoadVoiceSdk, notify: NotifyFn, { modelType, addon, modelConfig, openclCacheDir, loads, onUnloadFailure }: VoicePoolOptions): ModelPuddle;
163
171
  export {};