@qvac/harness 0.7.0 → 1.0.0-rc.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 (204) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +239 -2
  3. package/dist-lib/index.d.ts +58 -3
  4. package/dist-lib/index.js +736 -239
  5. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.d.ts +1 -0
  6. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.js +10 -0
  7. package/dist-lib/lib/capabilities/completion-engine/actors/answering.d.ts +3 -0
  8. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +17 -0
  9. package/dist-lib/lib/capabilities/completion-engine/actors/approval.d.ts +4 -0
  10. package/dist-lib/lib/capabilities/completion-engine/actors/approval.js +29 -0
  11. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.d.ts +4 -0
  12. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +73 -0
  13. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +41 -0
  14. package/dist-lib/lib/capabilities/completion-engine/actors/execute.js +49 -0
  15. package/dist-lib/lib/capabilities/completion-engine/actors/executing.d.ts +3 -0
  16. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +102 -0
  17. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.d.ts +47 -0
  18. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +87 -0
  19. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.d.ts +2 -0
  20. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.js +32 -0
  21. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.d.ts +3 -0
  22. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.js +75 -0
  23. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.d.ts +3 -0
  24. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.js +152 -0
  25. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.d.ts +3 -0
  26. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +94 -0
  27. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.d.ts +9 -0
  28. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.js +11 -0
  29. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.d.ts +3 -0
  30. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.js +114 -0
  31. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.d.ts +3 -0
  32. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +198 -0
  33. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.d.ts +3 -0
  34. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.js +71 -0
  35. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.d.ts +18 -0
  36. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.js +70 -0
  37. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.d.ts +3 -0
  38. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +60 -0
  39. package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +21 -0
  40. package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +48 -0
  41. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.d.ts +3 -0
  42. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +156 -0
  43. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.d.ts +7 -0
  44. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +61 -0
  45. package/dist-lib/lib/capabilities/completion-engine/driver.d.ts +25 -0
  46. package/dist-lib/lib/capabilities/completion-engine/driver.js +553 -0
  47. package/dist-lib/lib/capabilities/completion-engine/durable.d.ts +61 -0
  48. package/dist-lib/lib/capabilities/completion-engine/durable.js +150 -0
  49. package/dist-lib/lib/capabilities/completion-engine/operations.d.ts +6 -0
  50. package/dist-lib/lib/capabilities/completion-engine/operations.js +239 -0
  51. package/dist-lib/lib/capabilities/completion-engine/ports.d.ts +3 -0
  52. package/dist-lib/lib/capabilities/completion-engine/ports.js +267 -0
  53. package/dist-lib/lib/capabilities/completion-engine/session.d.ts +76 -0
  54. package/dist-lib/lib/capabilities/completion-engine/session.js +70 -0
  55. package/dist-lib/lib/capabilities/completion-loop/index.d.ts +80 -0
  56. package/dist-lib/lib/capabilities/completion-loop/index.js +158 -0
  57. package/dist-lib/lib/capabilities/completion-loop/metrics.d.ts +5 -0
  58. package/dist-lib/lib/capabilities/completion-loop/metrics.js +29 -0
  59. package/dist-lib/lib/capabilities/completion-loop/round.d.ts +33 -0
  60. package/dist-lib/lib/capabilities/completion-loop/round.js +353 -0
  61. package/dist-lib/lib/capabilities/completion-loop/tool-execution.d.ts +60 -0
  62. package/dist-lib/lib/capabilities/completion-loop/tool-execution.js +270 -0
  63. package/dist-lib/lib/capabilities/completion-loop/turns.d.ts +23 -0
  64. package/dist-lib/lib/capabilities/completion-loop/turns.js +112 -0
  65. package/dist-lib/lib/capabilities/completion-runtime.d.ts +46 -0
  66. package/dist-lib/lib/capabilities/completion-runtime.js +74 -0
  67. package/dist-lib/lib/capabilities/completion.d.ts +18 -335
  68. package/dist-lib/lib/capabilities/completion.js +20 -1301
  69. package/dist-lib/lib/capabilities/download.d.ts +1 -1
  70. package/dist-lib/lib/capabilities/download.js +7 -2
  71. package/dist-lib/lib/capabilities/invoke.js +5 -3
  72. package/dist-lib/lib/capabilities/tool-plan.d.ts +35 -0
  73. package/dist-lib/lib/capabilities/tool-plan.js +66 -0
  74. package/dist-lib/lib/capabilities/tool-turns.d.ts +11 -2
  75. package/dist-lib/lib/capabilities/tool-turns.js +30 -2
  76. package/dist-lib/lib/capabilities/types.d.ts +287 -0
  77. package/dist-lib/lib/capabilities/types.js +1 -0
  78. package/dist-lib/lib/completion-lifecycle.d.ts +14 -0
  79. package/dist-lib/lib/completion-lifecycle.js +126 -0
  80. package/dist-lib/lib/connect.d.ts +10 -3
  81. package/dist-lib/lib/connect.js +329 -23
  82. package/dist-lib/lib/contract-types.d.ts +19 -2
  83. package/dist-lib/lib/harness-lifecycle.d.ts +66 -0
  84. package/dist-lib/lib/harness-lifecycle.js +129 -0
  85. package/dist-lib/lib/helpers/detach-json.d.ts +1 -0
  86. package/dist-lib/lib/helpers/detach-json.js +146 -0
  87. package/dist-lib/lib/helpers/markup-scanner.d.ts +26 -0
  88. package/dist-lib/lib/helpers/markup-scanner.js +156 -0
  89. package/dist-lib/lib/helpers/markup-stream-filter.js +138 -35
  90. package/dist-lib/lib/helpers/race-abort.d.ts +9 -4
  91. package/dist-lib/lib/helpers/race-abort.js +2 -7
  92. package/dist-lib/lib/helpers/recovery-required.d.ts +1 -0
  93. package/dist-lib/lib/helpers/recovery-required.js +1 -0
  94. package/dist-lib/lib/helpers/run-outcome.d.ts +10 -2
  95. package/dist-lib/lib/helpers/run-outcome.js +8 -5
  96. package/dist-lib/lib/helpers/tool-call-markup.d.ts +2 -0
  97. package/dist-lib/lib/helpers/tool-call-markup.js +197 -82
  98. package/dist-lib/lib/hooks/built-in/arg-repair.d.ts +1 -0
  99. package/dist-lib/lib/hooks/built-in/arg-repair.js +1 -0
  100. package/dist-lib/lib/hooks/built-in/egress.d.ts +1 -0
  101. package/dist-lib/lib/hooks/built-in/egress.js +2 -1
  102. package/dist-lib/lib/hooks/built-in/sandbox.d.ts +1 -0
  103. package/dist-lib/lib/hooks/built-in/sandbox.js +6 -1
  104. package/dist-lib/lib/hooks/built-in/skill-catalog.d.ts +1 -0
  105. package/dist-lib/lib/hooks/built-in/skill-catalog.js +2 -1
  106. package/dist-lib/lib/hooks/pipeline.d.ts +4 -1
  107. package/dist-lib/lib/hooks/pipeline.js +24 -2
  108. package/dist-lib/lib/hooks/run.d.ts +2 -1
  109. package/dist-lib/lib/hooks/run.js +19 -7
  110. package/dist-lib/lib/hooks/types.d.ts +17 -6
  111. package/dist-lib/lib/machine/completion/actions/approval.d.ts +3 -0
  112. package/dist-lib/lib/machine/completion/actions/approval.js +16 -0
  113. package/dist-lib/lib/machine/completion/actions/completion.d.ts +23 -0
  114. package/dist-lib/lib/machine/completion/actions/completion.js +142 -0
  115. package/dist-lib/lib/machine/completion/actions/skills.d.ts +9 -0
  116. package/dist-lib/lib/machine/completion/actions/skills.js +83 -0
  117. package/dist-lib/lib/machine/completion/actions/tools.d.ts +16 -0
  118. package/dist-lib/lib/machine/completion/actions/tools.js +173 -0
  119. package/dist-lib/lib/machine/completion/context.d.ts +9 -0
  120. package/dist-lib/lib/machine/completion/context.js +44 -0
  121. package/dist-lib/lib/machine/completion/contract.d.ts +6 -0
  122. package/dist-lib/lib/machine/completion/contract.js +1 -0
  123. package/dist-lib/lib/machine/completion/guards/retry.d.ts +9 -0
  124. package/dist-lib/lib/machine/completion/guards/retry.js +25 -0
  125. package/dist-lib/lib/machine/completion/implementations.d.ts +81 -0
  126. package/dist-lib/lib/machine/completion/implementations.js +152 -0
  127. package/dist-lib/lib/machine/completion/machine.d.ts +375 -0
  128. package/dist-lib/lib/machine/completion/machine.js +511 -0
  129. package/dist-lib/lib/machine/completion/ports.d.ts +65 -0
  130. package/dist-lib/lib/machine/completion/ports.js +224 -0
  131. package/dist-lib/lib/machine/completion/rules.d.ts +39 -0
  132. package/dist-lib/lib/machine/completion/rules.js +115 -0
  133. package/dist-lib/lib/machine/completion/types.d.ts +493 -0
  134. package/dist-lib/lib/machine/completion/types.js +1 -0
  135. package/dist-lib/lib/machine/engine.d.ts +93 -0
  136. package/dist-lib/lib/machine/engine.js +483 -0
  137. package/dist-lib/lib/machine/errors.d.ts +44 -0
  138. package/dist-lib/lib/machine/errors.js +373 -0
  139. package/dist-lib/lib/machine/guards.d.ts +2 -0
  140. package/dist-lib/lib/machine/guards.js +6 -0
  141. package/dist-lib/lib/machine/index.d.ts +20 -0
  142. package/dist-lib/lib/machine/index.js +11 -0
  143. package/dist-lib/lib/machine/replay-output.d.ts +24 -0
  144. package/dist-lib/lib/machine/replay-output.js +50 -0
  145. package/dist-lib/lib/machine/stable-stringify.d.ts +1 -0
  146. package/dist-lib/lib/machine/stable-stringify.js +11 -0
  147. package/dist-lib/lib/models/model-handle.d.ts +5 -1
  148. package/dist-lib/lib/models/model-handle.js +8 -3
  149. package/dist-lib/lib/models/model-list.d.ts +5 -3
  150. package/dist-lib/lib/models/model-pool.d.ts +18 -7
  151. package/dist-lib/lib/models/model-pool.js +126 -38
  152. package/dist-lib/lib/run-log/history-codec.d.ts +23 -0
  153. package/dist-lib/lib/run-log/history-codec.js +973 -0
  154. package/dist-lib/lib/run-log/patch.d.ts +6 -0
  155. package/dist-lib/lib/run-log/patch.js +93 -0
  156. package/dist-lib/lib/run-log/persisted-actor.d.ts +18 -0
  157. package/dist-lib/lib/run-log/persisted-actor.js +218 -0
  158. package/dist-lib/lib/run-log/protocol-types.d.ts +153 -0
  159. package/dist-lib/lib/run-log/protocol-types.js +1 -0
  160. package/dist-lib/lib/run-log/protocol.d.ts +27 -0
  161. package/dist-lib/lib/run-log/protocol.js +1056 -0
  162. package/dist-lib/lib/run-log/replay.d.ts +25 -0
  163. package/dist-lib/lib/run-log/replay.js +129 -0
  164. package/dist-lib/lib/run-log/run-journal.d.ts +125 -0
  165. package/dist-lib/lib/run-log/run-journal.js +783 -0
  166. package/dist-lib/lib/run-log-inspect.d.ts +25 -0
  167. package/dist-lib/lib/run-log-inspect.js +627 -0
  168. package/dist-lib/lib/run-log-protocol.d.ts +2 -0
  169. package/dist-lib/lib/run-log-protocol.js +1 -0
  170. package/dist-lib/lib/run-log-redact.d.ts +27 -0
  171. package/dist-lib/lib/run-log-redact.js +1107 -0
  172. package/dist-lib/lib/serve.js +308 -12
  173. package/dist-lib/lib/spawn.js +3 -2
  174. package/dist-lib/lib/tool-call-display.d.ts +1 -21
  175. package/dist-lib/lib/tools/exec.d.ts +2 -1
  176. package/dist-lib/lib/tools/exec.js +57 -2
  177. package/dist-lib/lib/tools/generate-image.js +1 -1
  178. package/dist-lib/lib/tools/generate-music.js +1 -1
  179. package/dist-lib/lib/tools/http-request.d.ts +2 -1
  180. package/dist-lib/lib/tools/http-request.js +2 -1
  181. package/dist-lib/lib/tools/mcp-call.js +8 -1
  182. package/dist-lib/lib/tools/operations/builders.d.ts +1 -0
  183. package/dist-lib/lib/tools/operations/builders.js +1 -0
  184. package/dist-lib/lib/tools/operations/compile.js +11 -1
  185. package/dist-lib/lib/tools/operations/manifest.js +1 -0
  186. package/dist-lib/lib/tools/outbound-guard.d.ts +8 -6
  187. package/dist-lib/lib/tools/outbound-guard.js +25 -8
  188. package/dist-lib/lib/tools/skills/catalog.d.ts +1 -0
  189. package/dist-lib/lib/tools/skills/catalog.js +35 -0
  190. package/dist-lib/lib/tools/skills/cli-schema-types.d.ts +1 -0
  191. package/dist-lib/lib/tools/skills/cli-schema.js +25 -0
  192. package/dist-lib/lib/wire.d.ts +5 -0
  193. package/dist-lib/lib/wire.js +128 -5
  194. package/dist-lib/schema/harness.js +22 -6
  195. package/dist-lib/spec/hrpc/messages.js +100 -18
  196. package/dist-lib/spec/hyperschema/index.js +100 -18
  197. package/dist-lib/spec/hyperschema/schema.json +74 -1
  198. package/dist-lib/spec/hyperschema/types.d.ts +11 -3
  199. package/package.json +19 -1
  200. package/patches/@qvac%2Finference@0.20.0.patch +67 -0
  201. package/patches/bare-stow@0.1.5.patch +68 -0
  202. package/patches/hypercore-blob-server@1.15.0.patch +15 -0
  203. package/patches/localwatch@1.4.3.patch +13 -0
  204. package/patches/pyodide@314.0.2.patch +144 -0
@@ -0,0 +1,511 @@
1
+ import { and, emit, or, setup, stateIn } from 'xstate';
2
+ import { tagError } from '../errors.js';
3
+ import { emptyCompletionContext } from './context.js';
4
+ import { completionActions, completionGuards } from './implementations.js';
5
+ import { createCompletionActors } from './ports.js';
6
+ const MAX_DELAY = 2 ** 31 - 1;
7
+ const withParent = ({ self }) => ({ parent: self });
8
+ const selected = (value) => value;
9
+ const activeOrResuming = or([stateIn('#completion.active'), stateIn('#completion.resuming')]);
10
+ const activeResumingOrPausing = or([activeOrResuming, stateIn('#completion.pausing')]);
11
+ function directToolRouting() {
12
+ return [
13
+ {
14
+ guard: 'directRejected',
15
+ target: 'tools.rejecting',
16
+ actions: {
17
+ type: 'rejectTool',
18
+ params: ({ event }) => {
19
+ const direct = selected(event.output.directTool);
20
+ return {
21
+ call: direct.call,
22
+ execution: direct.execution,
23
+ rejection: selected(direct.rejection)
24
+ };
25
+ }
26
+ }
27
+ },
28
+ {
29
+ guard: 'directNeedsApproval',
30
+ target: 'tools.approving',
31
+ actions: [
32
+ {
33
+ type: 'startDirectTool',
34
+ params: ({ event }) => selected(event.output.directTool)
35
+ },
36
+ {
37
+ type: 'requestApproval',
38
+ params: ({ event }) => selected(event.output.directTool?.execution).gate.call
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ guard: 'directAutoRuns',
44
+ target: 'tools.executing',
45
+ actions: {
46
+ type: 'startDirectTool',
47
+ params: ({ event }) => selected(event.output.directTool)
48
+ }
49
+ },
50
+ { guard: 'noDirectTool', target: 'round' }
51
+ ];
52
+ }
53
+ const noClock = () => 0;
54
+ const createCompletionMachineImplementation = (context = emptyCompletionContext(), now = noClock, deps) => setup({
55
+ types: {},
56
+ actors: createCompletionActors(deps),
57
+ actions: completionActions,
58
+ guards: {
59
+ ...completionGuards,
60
+ errorInPausing: stateIn('#completion.pausing'),
61
+ errorInActiveOrResumingAndRecoveryRequired: and([activeOrResuming, 'needsRecovery']),
62
+ errorInActiveOrResumingAndInterrupted: and([activeOrResuming, 'interrupted']),
63
+ errorInFinishing: stateIn('#completion.active.finishing'),
64
+ errorInRoutableState: activeResumingOrPausing
65
+ },
66
+ delays: {
67
+ deadline: ({ context }) => {
68
+ const deadline = context.durable?.runBudget.deadlineMs;
69
+ return deadline === undefined ? MAX_DELAY : clamp(deadline - now());
70
+ }
71
+ }
72
+ }).createMachine({
73
+ context: ({ input }) => structuredContext(input ?? context),
74
+ id: 'completion',
75
+ initial: 'active',
76
+ output: ({ context }) => context.outcome === 'failed'
77
+ ? { status: 'failure', error: context.failMessage ?? 'Completion failed' }
78
+ : { status: 'success', response: context.response, metrics: context.metrics },
79
+ on: {
80
+ RECOVERY_REQUIRED: { target: '.paused', actions: 'recordRecovery' },
81
+ OUTPUT: { actions: emit(({ event }) => event.event) },
82
+ FACTS: { actions: 'recordFacts' },
83
+ 'xstate.error.actor.*': [
84
+ { guard: 'errorInPausing', target: '#completion.paused', actions: 'recordRecovery' },
85
+ {
86
+ guard: 'errorInActiveOrResumingAndRecoveryRequired',
87
+ target: '#completion.pausing',
88
+ actions: 'recordRecovery'
89
+ },
90
+ {
91
+ guard: 'errorInActiveOrResumingAndInterrupted',
92
+ target: '#completion.pausing',
93
+ actions: 'recordPause'
94
+ },
95
+ {
96
+ guard: 'errorInFinishing',
97
+ target: '#completion.failed',
98
+ actions: { type: 'failCompletion', params: errorParams }
99
+ },
100
+ {
101
+ guard: 'errorInRoutableState',
102
+ target: '#completion.active.finishing',
103
+ actions: { type: 'failCompletion', params: errorParams }
104
+ }
105
+ ]
106
+ },
107
+ states: {
108
+ active: {
109
+ initial: 'idle',
110
+ after: { deadline: { actions: 'markTimeUp' } },
111
+ on: {
112
+ PAUSE: { target: '#completion.pausing', actions: 'recordPause' },
113
+ FAIL: {
114
+ target: '.finishing',
115
+ actions: { type: 'failCompletion', params: ({ event }) => event.value }
116
+ }
117
+ },
118
+ states: {
119
+ history: { type: 'history', history: 'deep' },
120
+ idle: {
121
+ tags: ['idle'],
122
+ on: { START: 'preparing' }
123
+ },
124
+ preparing: {
125
+ tags: ['running', 'preparing'],
126
+ invoke: {
127
+ src: 'preparing',
128
+ input: withParent,
129
+ onDone: [
130
+ {
131
+ guard: 'preloadNeeded',
132
+ target: 'preloadChecking',
133
+ actions: {
134
+ type: 'requestPreload',
135
+ params: ({ event }) => selected(event.output.preload)
136
+ }
137
+ },
138
+ {
139
+ guard: 'directSearchNeeded',
140
+ target: 'searching',
141
+ actions: {
142
+ type: 'queueDirectSearch',
143
+ params: ({ event }) => event.output
144
+ }
145
+ },
146
+ {
147
+ target: 'round',
148
+ actions: {
149
+ type: 'startInitialRound',
150
+ params: ({ event }) => event.output.preparation
151
+ }
152
+ }
153
+ ]
154
+ }
155
+ },
156
+ preloadChecking: {
157
+ tags: ['running', 'skill', 'policy-check'],
158
+ invoke: {
159
+ src: 'inspectPreload',
160
+ input: withParent,
161
+ onDone: [
162
+ {
163
+ guard: and(['preloadFailed', 'searchPending']),
164
+ target: 'searching',
165
+ actions: { type: 'settlePreloadFailure', params: ({ event }) => event.output }
166
+ },
167
+ {
168
+ guard: 'preloadFailed',
169
+ target: 'round',
170
+ actions: { type: 'settlePreloadFailure', params: ({ event }) => event.output }
171
+ },
172
+ {
173
+ target: 'preloading',
174
+ actions: { type: 'authorizePreload', params: ({ event }) => event.output }
175
+ }
176
+ ]
177
+ }
178
+ },
179
+ preloading: {
180
+ tags: ['running', 'skill'],
181
+ invoke: {
182
+ src: 'preloading',
183
+ input: withParent,
184
+ onDone: [
185
+ {
186
+ guard: and(['preloadFailed', 'searchPending']),
187
+ target: 'searching',
188
+ actions: { type: 'settlePreloadFailure', params: ({ event }) => event.output }
189
+ },
190
+ {
191
+ guard: 'preloadFailed',
192
+ target: 'round',
193
+ actions: { type: 'settlePreloadFailure', params: ({ event }) => event.output }
194
+ },
195
+ {
196
+ guard: 'searchPending',
197
+ target: 'searching',
198
+ actions: { type: 'settleSkill', params: ({ event }) => event.output }
199
+ },
200
+ {
201
+ target: 'round',
202
+ actions: { type: 'settleSkill', params: ({ event }) => event.output }
203
+ }
204
+ ]
205
+ }
206
+ },
207
+ searching: {
208
+ tags: ['running', 'preparing'],
209
+ invoke: {
210
+ src: 'searching',
211
+ input: withParent,
212
+ onDone: directToolRouting()
213
+ }
214
+ },
215
+ round: {
216
+ always: [
217
+ {
218
+ guard: 'timeUp',
219
+ target: 'wrapping',
220
+ actions: { type: 'wrapCompletion', params: 'time-capped' }
221
+ },
222
+ { target: 'generating' }
223
+ ]
224
+ },
225
+ generating: {
226
+ tags: ['running', 'streaming'],
227
+ invoke: {
228
+ src: 'generating',
229
+ input: withParent,
230
+ onDone: {
231
+ target: 'resolving',
232
+ actions: { type: 'finishRound', params: ({ event }) => event.output }
233
+ }
234
+ }
235
+ },
236
+ resolving: {
237
+ tags: ['running', 'tool-resolution'],
238
+ invoke: {
239
+ src: 'resolving',
240
+ input: withParent,
241
+ onDone: [
242
+ {
243
+ guard: 'roundHasToolCalls',
244
+ target: 'tools',
245
+ actions: { type: 'resolveTools', params: ({ event }) => event.output }
246
+ },
247
+ {
248
+ guard: 'narratedIntent',
249
+ target: 'round',
250
+ actions: { type: 'retryNarratedRound', params: ({ event }) => event.output }
251
+ },
252
+ {
253
+ guard: 'hasAnswer',
254
+ target: 'answering'
255
+ },
256
+ { guard: 'canMuteRetry', target: 'round', actions: 'retryMuteRound' },
257
+ { target: 'wrapping', actions: 'wrapMuteCompletion' }
258
+ ]
259
+ }
260
+ },
261
+ answering: {
262
+ tags: ['running', 'streaming'],
263
+ invoke: {
264
+ src: 'answering',
265
+ input: withParent,
266
+ onDone: {
267
+ target: 'finishing',
268
+ actions: { type: 'answerCompletion', params: ({ event }) => event.output }
269
+ }
270
+ }
271
+ },
272
+ tools: {
273
+ initial: 'selecting',
274
+ states: {
275
+ selecting: {
276
+ always: [
277
+ {
278
+ guard: and(['hasQueuedTool', 'repeatOfFailed']),
279
+ target: 'rejecting',
280
+ actions: {
281
+ type: 'rejectRepeatedTool',
282
+ params: ({ context }) => {
283
+ const call = context.toolQueue[0];
284
+ return {
285
+ call,
286
+ prior: context.failedCalls[call.key]
287
+ };
288
+ }
289
+ }
290
+ },
291
+ {
292
+ guard: 'hasQueuedTool',
293
+ target: 'inspecting',
294
+ actions: {
295
+ type: 'selectNextTool',
296
+ params: ({ context }) => context.toolQueue[0]
297
+ }
298
+ },
299
+ {
300
+ guard: 'repeatsBlocked',
301
+ target: '#completion.active.wrapping',
302
+ actions: 'wrapExhaustedCompletion'
303
+ },
304
+ {
305
+ guard: and(['allResolvedToolsCapped', 'canRetrySearchCap', 'hasRoundsLeft']),
306
+ target: '#completion.active.round',
307
+ actions: 'retryCappedSearch'
308
+ },
309
+ {
310
+ guard: 'allResolvedToolsCapped',
311
+ target: '#completion.active.wrapping',
312
+ actions: 'wrapSearchCappedCompletion'
313
+ },
314
+ {
315
+ guard: 'hasRoundsLeft',
316
+ target: '#completion.active.round',
317
+ actions: 'startNextRound'
318
+ },
319
+ { target: '#completion.active.wrapping', actions: 'wrapExhaustedCompletion' }
320
+ ]
321
+ },
322
+ inspecting: {
323
+ tags: ['running', 'tool'],
324
+ invoke: {
325
+ src: 'inspecting',
326
+ input: withParent,
327
+ onDone: [
328
+ {
329
+ guard: 'toolRejected',
330
+ target: 'rejecting',
331
+ actions: {
332
+ type: 'rejectTool',
333
+ params: ({ event }) => ({
334
+ call: event.output.call,
335
+ execution: event.output.execution,
336
+ rejection: selected(event.output.rejection)
337
+ })
338
+ }
339
+ },
340
+ {
341
+ guard: 'skillLoadAllowed',
342
+ target: 'loadingSkill',
343
+ actions: {
344
+ type: 'startSkillTool',
345
+ params: ({ event }) => selected(event.output.execution)
346
+ }
347
+ },
348
+ {
349
+ guard: {
350
+ type: 'needsApproval',
351
+ params: ({ event }) => event.output.execution
352
+ },
353
+ target: 'approving',
354
+ actions: [
355
+ {
356
+ type: 'startTool',
357
+ params: ({ event }) => selected(event.output.execution)
358
+ },
359
+ {
360
+ type: 'requestApproval',
361
+ params: ({ event }) => selected(event.output.execution).gate.call
362
+ }
363
+ ]
364
+ },
365
+ {
366
+ guard: 'toolAutoRuns',
367
+ target: 'executing',
368
+ actions: {
369
+ type: 'startTool',
370
+ params: ({ event }) => selected(event.output.execution)
371
+ }
372
+ }
373
+ ]
374
+ }
375
+ },
376
+ rejecting: {
377
+ tags: ['running', 'tool'],
378
+ invoke: {
379
+ src: 'rejecting',
380
+ input: withParent,
381
+ onDone: [
382
+ {
383
+ guard: 'directExecution',
384
+ target: '#completion.active.preparing',
385
+ actions: { type: 'finishDirectTool', params: ({ event }) => event.output }
386
+ },
387
+ {
388
+ target: 'selecting',
389
+ actions: { type: 'finishTool', params: ({ event }) => event.output }
390
+ }
391
+ ]
392
+ }
393
+ },
394
+ loadingSkill: {
395
+ tags: ['running', 'skill'],
396
+ invoke: {
397
+ src: 'loadingSkill',
398
+ input: withParent,
399
+ onDone: {
400
+ target: 'selecting',
401
+ actions: { type: 'settleToolSkill', params: ({ event }) => event.output }
402
+ }
403
+ }
404
+ },
405
+ approving: {
406
+ tags: ['running', 'tool', 'approval'],
407
+ invoke: {
408
+ src: 'approving',
409
+ input: withParent,
410
+ onDone: [
411
+ { guard: 'approved', target: 'executing', actions: 'approveCompletion' },
412
+ {
413
+ target: 'rejecting',
414
+ actions: {
415
+ type: 'rejectDeniedApproval',
416
+ params: ({ context }) => selected({
417
+ call: context.currentCall,
418
+ execution: context.execution
419
+ })
420
+ }
421
+ }
422
+ ]
423
+ }
424
+ },
425
+ executing: {
426
+ tags: ['running', 'tool'],
427
+ invoke: {
428
+ src: 'executing',
429
+ input: withParent,
430
+ onDone: [
431
+ {
432
+ guard: 'directExecution',
433
+ target: '#completion.active.preparing',
434
+ actions: {
435
+ type: 'finishDirectTool',
436
+ params: ({ event }) => event.output
437
+ }
438
+ },
439
+ {
440
+ target: 'selecting',
441
+ actions: { type: 'finishTool', params: ({ event }) => event.output }
442
+ }
443
+ ]
444
+ }
445
+ }
446
+ }
447
+ },
448
+ wrapping: {
449
+ tags: ['running', 'streaming'],
450
+ invoke: {
451
+ src: 'wrapping',
452
+ input: withParent,
453
+ onDone: {
454
+ target: 'answering',
455
+ actions: { type: 'finishWrapRound', params: ({ event }) => event.output }
456
+ }
457
+ }
458
+ },
459
+ finishing: {
460
+ invoke: {
461
+ src: 'finishing',
462
+ input: withParent,
463
+ onDone: [
464
+ { guard: 'failedOutcome', target: '#completion.failed' },
465
+ { target: '#completion.done' }
466
+ ]
467
+ }
468
+ }
469
+ }
470
+ },
471
+ pausing: {
472
+ tags: ['paused'],
473
+ invoke: {
474
+ src: 'cleanup',
475
+ input: withParent,
476
+ onDone: 'paused'
477
+ }
478
+ },
479
+ resuming: {
480
+ on: { PAUSE: { target: 'pausing', actions: 'recordPause' } },
481
+ invoke: {
482
+ src: 'resume',
483
+ input: withParent,
484
+ onDone: 'active.history'
485
+ }
486
+ },
487
+ paused: {
488
+ tags: ['paused'],
489
+ on: { RESUME: { target: 'resuming', actions: 'resumeRun' } }
490
+ },
491
+ done: { type: 'final', tags: ['terminal', 'success'] },
492
+ failed: { type: 'final', tags: ['terminal', 'error'] }
493
+ }
494
+ });
495
+ export const createCompletionMachine = createCompletionMachineImplementation;
496
+ function clamp(ms) {
497
+ return Math.min(MAX_DELAY, Math.max(0, ms));
498
+ }
499
+ function errorParams({ event }) {
500
+ const error = tagError(event.error);
501
+ if (error?.tag === 'failed') {
502
+ return error;
503
+ }
504
+ return {
505
+ error: error?.message ?? 'Completion failed',
506
+ name: error?.name ?? 'Error'
507
+ };
508
+ }
509
+ function structuredContext(context) {
510
+ return JSON.parse(JSON.stringify(context));
511
+ }
@@ -0,0 +1,65 @@
1
+ import { type ActorRef, type Snapshot } from 'xstate';
2
+ import type { RunJournal } from '../../run-log/run-journal.ts';
3
+ import type { CompletionActorOutputs, CompletionContext, CompletionMachineEvent, EngineEvent, EngineFacts } from './types.ts';
4
+ export type Stage = keyof CompletionActorOutputs;
5
+ export interface Invocation {
6
+ context: CompletionContext;
7
+ signal: AbortSignal;
8
+ stage: string;
9
+ emit(event: EngineEvent): void;
10
+ commit(facts: EngineFacts): void;
11
+ commitSettledResult(operationId: string): void;
12
+ }
13
+ export interface EnginePorts {
14
+ stages: {
15
+ [S in Stage]: (cx: Invocation) => Promise<CompletionActorOutputs[S]>;
16
+ };
17
+ rebuild(cx: Invocation): Promise<void>;
18
+ lifecycle(phase: 'runPause' | 'runResume', context: CompletionContext): Promise<void>;
19
+ settledResultFacts?(operationId: string, current: EngineFacts | null): EngineFacts | undefined;
20
+ now(): number;
21
+ }
22
+ export interface InvokeTracker {
23
+ trackInvoke(): () => void;
24
+ waitForInvokes(): Promise<void>;
25
+ drainTimedOut(): Promise<boolean>;
26
+ }
27
+ export interface EngineDeps {
28
+ ports: EnginePorts;
29
+ journal: RunJournal;
30
+ invokes: InvokeTracker;
31
+ emitContent?(event: EngineEvent): void;
32
+ }
33
+ type CompletionParentSnapshot = Snapshot<unknown> & {
34
+ context: CompletionContext;
35
+ };
36
+ export type CompletionParent = Pick<ActorRef<CompletionParentSnapshot, CompletionMachineEvent>, 'send' | 'getSnapshot'>;
37
+ export interface InvocationInput {
38
+ parent: CompletionParent;
39
+ }
40
+ export declare function createInvokeTracker(): InvokeTracker;
41
+ export declare function createCompletionActors(deps?: EngineDeps): CompletionActors;
42
+ type CompletionActors = ReturnType<typeof makeCompletionActors>;
43
+ declare function makeCompletionActors(deps?: EngineDeps): {
44
+ preparing: import("xstate").PromiseActorLogic<import("./types.ts").PreparingOutput, InvocationInput, import("xstate").EventObject>;
45
+ inspectPreload: import("xstate").PromiseActorLogic<import("./types.ts").PreloadInspectionOutput, InvocationInput, import("xstate").EventObject>;
46
+ preloading: import("xstate").PromiseActorLogic<import("./types.ts").SkillLoadOutput, InvocationInput, import("xstate").EventObject>;
47
+ searching: import("xstate").PromiseActorLogic<import("./types.ts").SearchingOutput, InvocationInput, import("xstate").EventObject>;
48
+ generating: import("xstate").PromiseActorLogic<import("./types.ts").RoundDonePayload, InvocationInput, import("xstate").EventObject>;
49
+ resolving: import("xstate").PromiseActorLogic<import("./types.ts").ResolvingOutput, InvocationInput, import("xstate").EventObject>;
50
+ inspecting: import("xstate").PromiseActorLogic<import("./types.ts").InspectingOutput, InvocationInput, import("xstate").EventObject>;
51
+ rejecting: import("xstate").PromiseActorLogic<import("./types.ts").ToolDonePayload, InvocationInput, import("xstate").EventObject>;
52
+ loadingSkill: import("xstate").PromiseActorLogic<import("./types.ts").SkillLoadOutput, InvocationInput, import("xstate").EventObject>;
53
+ approving: import("xstate").PromiseActorLogic<{
54
+ approved: boolean;
55
+ }, InvocationInput, import("xstate").EventObject>;
56
+ executing: import("xstate").PromiseActorLogic<import("./types.ts").ToolDonePayload, InvocationInput, import("xstate").EventObject>;
57
+ wrapping: import("xstate").PromiseActorLogic<import("./types.ts").RoundDonePayload & {
58
+ response: string;
59
+ }, InvocationInput, import("xstate").EventObject>;
60
+ answering: import("xstate").PromiseActorLogic<import("./types.ts").AnsweredPayload, InvocationInput, import("xstate").EventObject>;
61
+ finishing: import("xstate").PromiseActorLogic<void, InvocationInput, import("xstate").EventObject>;
62
+ cleanup: import("xstate").PromiseActorLogic<void, InvocationInput, import("xstate").EventObject>;
63
+ resume: import("xstate").PromiseActorLogic<void, InvocationInput, import("xstate").EventObject>;
64
+ };
65
+ export {};