@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,224 @@
1
+ import { fromPromise } from 'xstate';
2
+ import { AbortedError, RecoveryRequired, didDrainTimeOut, raceDrain, tagError } from '../errors.js';
3
+ export function createInvokeTracker() {
4
+ let active = 0;
5
+ let idle = Promise.resolve();
6
+ let settleIdle = () => { };
7
+ let stuck = null;
8
+ return {
9
+ trackInvoke() {
10
+ if (active === 0) {
11
+ idle = new Promise((resolve) => {
12
+ settleIdle = resolve;
13
+ });
14
+ }
15
+ active++;
16
+ let released = false;
17
+ return () => {
18
+ if (released)
19
+ return;
20
+ released = true;
21
+ active--;
22
+ if (active === 0) {
23
+ settleIdle();
24
+ }
25
+ };
26
+ },
27
+ waitForInvokes() {
28
+ return idle;
29
+ },
30
+ async drainTimedOut() {
31
+ const waiting = idle;
32
+ if (stuck === waiting) {
33
+ return true;
34
+ }
35
+ if (!didDrainTimeOut(await raceDrain(waiting))) {
36
+ return false;
37
+ }
38
+ stuck = waiting;
39
+ return true;
40
+ }
41
+ };
42
+ }
43
+ export function createCompletionActors(deps) {
44
+ return makeCompletionActors(deps);
45
+ }
46
+ function makeCompletionActors(deps) {
47
+ return {
48
+ preparing: worker('preparing', deps),
49
+ inspectPreload: worker('inspectPreload', deps),
50
+ preloading: worker('preloading', deps),
51
+ searching: worker('searching', deps),
52
+ generating: worker('generating', deps),
53
+ resolving: worker('resolving', deps),
54
+ inspecting: worker('inspecting', deps),
55
+ rejecting: worker('rejecting', deps),
56
+ loadingSkill: worker('loadingSkill', deps),
57
+ approving: worker('approving', deps),
58
+ executing: worker('executing', deps),
59
+ wrapping: worker('wrapping', deps),
60
+ answering: worker('answering', deps),
61
+ finishing: worker('finishing', deps),
62
+ cleanup: fromPromise(async ({ input }) => {
63
+ if (!deps)
64
+ return;
65
+ try {
66
+ if (await deps.invokes.drainTimedOut()) {
67
+ throw new AbortedError();
68
+ }
69
+ const release = deps.invokes.trackInvoke();
70
+ try {
71
+ await lifecycle({ deps, phase: 'runPause', context: contextOf(input.parent) });
72
+ }
73
+ finally {
74
+ release();
75
+ }
76
+ }
77
+ catch (error) {
78
+ throw tagError(error);
79
+ }
80
+ }),
81
+ resume: fromPromise(async ({ input, signal }) => {
82
+ if (!deps)
83
+ return;
84
+ const release = deps.invokes.trackInvoke();
85
+ const context = contextOf(input.parent);
86
+ try {
87
+ await deps.ports.rebuild(invocation({
88
+ parent: input.parent,
89
+ context,
90
+ stage: 'resume',
91
+ signal,
92
+ isLive: () => !signal.aborted,
93
+ journal: deps.journal,
94
+ canCommitSettledResult: () => !signal.aborted,
95
+ settledResultFacts: deps.ports.settledResultFacts
96
+ }));
97
+ for (const receipt of deps.journal.receipts.values()) {
98
+ if (receipt.status === 'retry' &&
99
+ (receipt.name === 'hook:runPause' || receipt.name === 'hook:runResume')) {
100
+ const phase = receipt.name === 'hook:runPause' ? 'runPause' : 'runResume';
101
+ await lifecycle({ deps, phase, context, signal, operationId: receipt.id });
102
+ }
103
+ }
104
+ await lifecycle({ deps, phase: 'runResume', context, signal });
105
+ }
106
+ catch (error) {
107
+ throw tagError(error);
108
+ }
109
+ finally {
110
+ release();
111
+ }
112
+ })
113
+ };
114
+ }
115
+ function worker(stage, deps) {
116
+ return fromPromise(async ({ input, signal }) => {
117
+ if (!deps) {
118
+ throw new Error('Completion runtime implementations were not provided');
119
+ }
120
+ const { journal, ports } = deps;
121
+ const context = contextOf(input.parent);
122
+ const release = deps.invokes.trackInvoke();
123
+ let acceptingOutput = true;
124
+ const isLive = () => acceptingOutput && !signal.aborted;
125
+ try {
126
+ await journal.flush();
127
+ if (signal.aborted) {
128
+ throw new AbortedError();
129
+ }
130
+ const emitContent = deps.emitContent
131
+ ? (event) => {
132
+ if (isLive()) {
133
+ deps.emitContent?.(event);
134
+ }
135
+ }
136
+ : undefined;
137
+ const output = await ports.stages[stage](invocation({
138
+ parent: input.parent,
139
+ context,
140
+ stage,
141
+ signal,
142
+ emitContent,
143
+ isLive,
144
+ journal,
145
+ canCommitSettledResult: () => acceptingOutput,
146
+ settledResultFacts: ports.settledResultFacts
147
+ }));
148
+ acceptingOutput = false;
149
+ await journal.flush();
150
+ if (signal.aborted) {
151
+ throw new AbortedError();
152
+ }
153
+ const uncertain = journal.uncertainWrite();
154
+ if (uncertain) {
155
+ throw new RecoveryRequired(uncertain);
156
+ }
157
+ return output;
158
+ }
159
+ catch (error) {
160
+ acceptingOutput = false;
161
+ throw tagError(error);
162
+ }
163
+ finally {
164
+ acceptingOutput = false;
165
+ release();
166
+ }
167
+ });
168
+ }
169
+ function invocation({ parent, context, stage, signal, emitContent, isLive, journal, canCommitSettledResult, settledResultFacts }) {
170
+ const committedReceipts = new Set();
171
+ return {
172
+ context,
173
+ signal,
174
+ stage,
175
+ emit: (event) => {
176
+ if (!isLive())
177
+ return;
178
+ if ((event.type === 'content' || event.type === 'thinking') && emitContent) {
179
+ emitContent(event);
180
+ }
181
+ else {
182
+ parent.send({ type: 'OUTPUT', event });
183
+ }
184
+ },
185
+ commit: (facts) => {
186
+ if (!isLive() || contextOf(parent).durable === facts) {
187
+ return;
188
+ }
189
+ parent.send({ type: 'FACTS', value: facts });
190
+ },
191
+ commitSettledResult: (operationId) => {
192
+ if (!canCommitSettledResult() || journal.sealed || journal.failure) {
193
+ return;
194
+ }
195
+ if (committedReceipts.has(operationId)) {
196
+ return;
197
+ }
198
+ const receipt = journal.getReceipt(operationId);
199
+ if (receipt?.status !== 'result') {
200
+ return;
201
+ }
202
+ const projected = settledResultFacts?.(operationId, contextOf(parent).durable);
203
+ if (!projected) {
204
+ return;
205
+ }
206
+ committedReceipts.add(operationId);
207
+ parent.send({ type: 'FACTS', value: projected });
208
+ }
209
+ };
210
+ }
211
+ function contextOf(parent) {
212
+ return parent.getSnapshot().context;
213
+ }
214
+ async function lifecycle({ deps, phase, context, signal, operationId = `${context.runId}:${phase}-${context.pauseCount}` }) {
215
+ await deps.journal.flush();
216
+ if (signal?.aborted) {
217
+ throw new AbortedError();
218
+ }
219
+ await deps.journal.operation({ id: operationId, name: `hook:${phase}`, args: {}, effect: 'write' }, async () => {
220
+ if (signal?.aborted)
221
+ throw new AbortedError();
222
+ await deps.ports.lifecycle(phase, context);
223
+ });
224
+ }
@@ -0,0 +1,39 @@
1
+ import type { CompletionHistoryEntry, CompletionStats, FailureResultReference, HistorySplice, RunBackendUse, SettledCall, FailureResultValue, CompletionContext, StoredFailureResult } from './types.ts';
2
+ export declare const MAX_TOOL_ROUNDS = 10;
3
+ export declare const MAX_MUTE_ROUNDS = 1;
4
+ export declare const MAX_CAPPED_ROUNDS = 1;
5
+ export declare const MAX_BLOCKED_REPEATS = 3;
6
+ export declare const TOOL_CONTINUATION_NUDGE = "[Tool results above. Continue the user's request: call the next tool, or answer.]";
7
+ export declare const TOOL_DENIED_MESSAGE: string;
8
+ export interface ToolDeniedResult {
9
+ error: string;
10
+ denied: true;
11
+ }
12
+ export declare function toolDeniedResult(): ToolDeniedResult;
13
+ export declare function toolCallKey(name: string, args: Record<string, unknown>): string;
14
+ export declare function failureResultRef(key: string, round: number, callId: string): string;
15
+ export declare function failureResultReference(ref: string, callId: string): FailureResultReference;
16
+ export declare function isFailureResultReference(entry: CompletionHistoryEntry): entry is FailureResultReference;
17
+ export declare function storeFailureResult(value: unknown): StoredFailureResult;
18
+ export declare function readFailureResult(value: StoredFailureResult): unknown;
19
+ export declare function retainFailureResults(history: readonly CompletionHistoryEntry[], values: Readonly<Record<string, FailureResultValue>>, failedCalls: Readonly<Record<string, SettledCall>>): Record<string, FailureResultValue>;
20
+ export interface ApplyHistorySpliceOptions {
21
+ failedCalls?: Readonly<Record<string, SettledCall>>;
22
+ failureResults?: Readonly<Record<string, FailureResultValue>>;
23
+ }
24
+ export declare function applyHistorySplice(context: Readonly<CompletionContext>, splice?: HistorySplice, options?: ApplyHistorySpliceOptions): Pick<CompletionContext, 'history' | 'failureResults'>;
25
+ export interface MetricsState {
26
+ ttft?: number;
27
+ promptTokensPerSecond?: number;
28
+ tokens: number;
29
+ timedTokens: number;
30
+ durationMs: number;
31
+ promptTokens?: number;
32
+ cacheTokens?: number;
33
+ ctxSize?: number | undefined;
34
+ backends?: RunBackendUse[];
35
+ stopReason?: string | undefined;
36
+ }
37
+ export declare function foldMetrics(state: MetricsState, stats?: CompletionStats, stopReason?: string): void;
38
+ export declare function spliceFrom(base: readonly CompletionHistoryEntry[], next: readonly CompletionHistoryEntry[]): HistorySplice;
39
+ export declare function applySplice(history: CompletionHistoryEntry[], splice?: HistorySplice): CompletionHistoryEntry[];
@@ -0,0 +1,115 @@
1
+ import { isRecord } from '../guards.js';
2
+ import { stableStringify } from '../stable-stringify.js';
3
+ export const MAX_TOOL_ROUNDS = 10;
4
+ export const MAX_MUTE_ROUNDS = 1;
5
+ export const MAX_CAPPED_ROUNDS = 1;
6
+ export const MAX_BLOCKED_REPEATS = 3;
7
+ export const TOOL_CONTINUATION_NUDGE = "[Tool results above. Continue the user's request: call the next tool, or answer.]";
8
+ export const TOOL_DENIED_MESSAGE = 'The user declined this call. Do not retry it or call it with different arguments to get ' +
9
+ 'around the refusal. Tell the user plainly what you could not do, and never present a ' +
10
+ 'result you did not obtain.';
11
+ export function toolDeniedResult() {
12
+ return { error: TOOL_DENIED_MESSAGE, denied: true };
13
+ }
14
+ export function toolCallKey(name, args) {
15
+ try {
16
+ return `${name}:${stableStringify(args)}`;
17
+ }
18
+ catch {
19
+ return name;
20
+ }
21
+ }
22
+ export function failureResultRef(key, round, callId) {
23
+ return `failure:${stableStringify([key, round, callId])}`;
24
+ }
25
+ export function failureResultReference(ref, callId) {
26
+ return { type: 'qvac-failure-result-ref', ref, callId };
27
+ }
28
+ export function isFailureResultReference(entry) {
29
+ if (!isRecord(entry) || Array.isArray(entry)) {
30
+ return false;
31
+ }
32
+ const prototype = Object.getPrototypeOf(entry);
33
+ if (prototype !== Object.prototype && prototype !== null) {
34
+ return false;
35
+ }
36
+ return (Object.hasOwn(entry, 'type') &&
37
+ entry.type === 'qvac-failure-result-ref' &&
38
+ Object.hasOwn(entry, 'ref') &&
39
+ typeof entry.ref === 'string' &&
40
+ Object.hasOwn(entry, 'callId') &&
41
+ typeof entry.callId === 'string' &&
42
+ Reflect.ownKeys(entry).length === 3);
43
+ }
44
+ export function storeFailureResult(value) {
45
+ return value === undefined ? { undefined: true } : { value };
46
+ }
47
+ export function readFailureResult(value) {
48
+ return 'undefined' in value ? undefined : value.value;
49
+ }
50
+ export function retainFailureResults(history, values, failedCalls) {
51
+ const retained = new Set();
52
+ for (const entry of history) {
53
+ if (isFailureResultReference(entry)) {
54
+ retained.add(entry.ref);
55
+ }
56
+ }
57
+ for (const call of Object.values(failedCalls)) {
58
+ retained.add(call.resultRef);
59
+ }
60
+ const keys = Object.keys(values);
61
+ if (keys.every((key) => retained.has(key))) {
62
+ return { ...values };
63
+ }
64
+ return Object.fromEntries(keys.filter((key) => retained.has(key)).map((key) => [key, values[key]]));
65
+ }
66
+ export function applyHistorySplice(context, splice, options = {}) {
67
+ const failedCalls = options.failedCalls ?? context.failedCalls;
68
+ const failureResults = options.failureResults ?? context.failureResults;
69
+ const history = applySplice(context.history, splice);
70
+ return {
71
+ history,
72
+ failureResults: retainFailureResults(history, failureResults, failedCalls)
73
+ };
74
+ }
75
+ export function foldMetrics(state, stats, stopReason) {
76
+ state.stopReason = stopReason;
77
+ if (!stats) {
78
+ return;
79
+ }
80
+ if (state.ttft === undefined && stats.timeToFirstToken) {
81
+ state.ttft = stats.timeToFirstToken;
82
+ }
83
+ if (state.promptTokensPerSecond === undefined && stats.promptTokensPerSecond) {
84
+ state.promptTokensPerSecond = stats.promptTokensPerSecond;
85
+ }
86
+ if (stats.generatedTokens) {
87
+ state.tokens += stats.generatedTokens;
88
+ if (stats.tokensPerSecond) {
89
+ state.durationMs += (stats.generatedTokens / stats.tokensPerSecond) * 1000;
90
+ state.timedTokens += stats.generatedTokens;
91
+ }
92
+ }
93
+ if (stats.promptTokens) {
94
+ state.promptTokens = stats.promptTokens;
95
+ }
96
+ if (stats.cacheTokens) {
97
+ state.cacheTokens = stats.cacheTokens;
98
+ }
99
+ }
100
+ export function spliceFrom(base, next) {
101
+ let at = 0;
102
+ while (at < base.length && at < next.length && base[at] === next[at]) {
103
+ at++;
104
+ }
105
+ return { at, messages: next.slice(at) };
106
+ }
107
+ export function applySplice(history, splice) {
108
+ if (!splice) {
109
+ return history;
110
+ }
111
+ if (splice.at === history.length && !splice.messages.length) {
112
+ return history;
113
+ }
114
+ return [...history.slice(0, splice.at), ...splice.messages];
115
+ }