@qvac/harness 1.0.0-rc.20 → 1.0.0-rc.21

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 (191) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist-lib/index.d.ts +7 -5
  3. package/dist-lib/index.js +5 -4
  4. package/dist-lib/lib/agent/resolve.js +2 -1
  5. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +2 -2
  6. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +1 -1
  7. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +1 -1
  8. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +8 -6
  9. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +1 -1
  10. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-prepare.js +1 -1
  11. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +2 -1
  12. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +8 -3
  13. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +10 -4
  14. package/dist-lib/lib/capabilities/completion-engine/actors/run-prepare.js +2 -1
  15. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +8 -8
  16. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +2 -2
  17. package/dist-lib/lib/capabilities/completion-engine/loop.d.ts +3 -2
  18. package/dist-lib/lib/capabilities/completion-engine/loop.js +1 -0
  19. package/dist-lib/lib/capabilities/completion-engine/round.js +9 -9
  20. package/dist-lib/lib/capabilities/completion-engine/tool-caller.js +3 -1
  21. package/dist-lib/lib/capabilities/completion-engine/tool-execution.d.ts +5 -41
  22. package/dist-lib/lib/capabilities/completion-engine/tool-execution.js +4 -133
  23. package/dist-lib/lib/capabilities/completion-engine/tool-outcome.d.ts +4 -0
  24. package/dist-lib/lib/capabilities/completion-engine/tool-outcome.js +42 -0
  25. package/dist-lib/lib/capabilities/completion-engine/tool-preflight.d.ts +43 -0
  26. package/dist-lib/lib/capabilities/completion-engine/tool-preflight.js +91 -0
  27. package/dist-lib/lib/capabilities/completion-engine/turns.js +7 -4
  28. package/dist-lib/lib/capabilities/tool-turns/attachments.d.ts +7 -0
  29. package/dist-lib/lib/capabilities/tool-turns/attachments.js +39 -0
  30. package/dist-lib/lib/capabilities/tool-turns/copy.d.ts +13 -0
  31. package/dist-lib/lib/capabilities/tool-turns/copy.js +29 -0
  32. package/dist-lib/lib/capabilities/tool-turns/result-turn.d.ts +22 -0
  33. package/dist-lib/lib/capabilities/tool-turns/result-turn.js +120 -0
  34. package/dist-lib/lib/capabilities/tool-turns/run-limits.d.ts +6 -0
  35. package/dist-lib/lib/capabilities/tool-turns/run-limits.js +32 -0
  36. package/dist-lib/lib/capabilities/tool-turns/skill-loads.d.ts +9 -0
  37. package/dist-lib/lib/capabilities/tool-turns/skill-loads.js +71 -0
  38. package/dist-lib/lib/capabilities/tool-turns.d.ts +8 -54
  39. package/dist-lib/lib/capabilities/tool-turns.js +6 -281
  40. package/dist-lib/lib/client/index.js +3 -3
  41. package/dist-lib/lib/connect/abort.d.ts +6 -0
  42. package/dist-lib/lib/connect/abort.js +52 -0
  43. package/dist-lib/lib/connect/bridges.d.ts +21 -0
  44. package/dist-lib/lib/connect/bridges.js +185 -0
  45. package/dist-lib/lib/connect/decode.d.ts +31 -0
  46. package/dist-lib/lib/connect/decode.js +114 -0
  47. package/dist-lib/lib/connect/host-run-log.d.ts +16 -0
  48. package/dist-lib/lib/connect/host-run-log.js +74 -0
  49. package/dist-lib/lib/connect/served.d.ts +7 -0
  50. package/dist-lib/lib/connect/served.js +45 -0
  51. package/dist-lib/lib/connect.d.ts +3 -3
  52. package/dist-lib/lib/connect.js +24 -468
  53. package/dist-lib/lib/context-window.d.ts +13 -7
  54. package/dist-lib/lib/context-window.js +13 -6
  55. package/dist-lib/lib/contract/agent.d.ts +12 -1
  56. package/dist-lib/lib/contract/completion.d.ts +5 -1
  57. package/dist-lib/lib/contract/drive.d.ts +130 -4
  58. package/dist-lib/lib/contract/ocr.d.ts +5 -1
  59. package/dist-lib/lib/contract-types.d.ts +5 -3
  60. package/dist-lib/lib/errors.js +3 -3
  61. package/dist-lib/lib/helpers/goal-check/asks.d.ts +25 -0
  62. package/dist-lib/lib/helpers/goal-check/asks.js +52 -0
  63. package/dist-lib/lib/helpers/goal-check/checks.d.ts +8 -0
  64. package/dist-lib/lib/helpers/goal-check/checks.js +92 -0
  65. package/dist-lib/lib/helpers/goal-check/commands.d.ts +7 -0
  66. package/dist-lib/lib/helpers/goal-check/commands.js +130 -0
  67. package/dist-lib/lib/helpers/goal-check/paths.d.ts +5 -0
  68. package/dist-lib/lib/helpers/goal-check/paths.js +44 -0
  69. package/dist-lib/lib/helpers/goal-check/shell.d.ts +9 -0
  70. package/dist-lib/lib/helpers/goal-check/shell.js +96 -0
  71. package/dist-lib/lib/helpers/goal-check.d.ts +35 -43
  72. package/dist-lib/lib/helpers/goal-check.js +34 -406
  73. package/dist-lib/lib/helpers/recent-map.d.ts +7 -0
  74. package/dist-lib/lib/helpers/recent-map.js +23 -0
  75. package/dist-lib/lib/helpers/tool-call-markup/emit.d.ts +6 -0
  76. package/dist-lib/lib/helpers/tool-call-markup/emit.js +15 -0
  77. package/dist-lib/lib/helpers/tool-call-markup/recover.d.ts +22 -0
  78. package/dist-lib/lib/helpers/tool-call-markup/recover.js +130 -0
  79. package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.d.ts +3 -0
  80. package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.js +56 -0
  81. package/dist-lib/lib/helpers/tool-call-markup/strip.d.ts +4 -0
  82. package/dist-lib/lib/helpers/tool-call-markup/strip.js +149 -0
  83. package/dist-lib/lib/helpers/tool-call-markup.d.ts +19 -24
  84. package/dist-lib/lib/helpers/tool-call-markup.js +17 -343
  85. package/dist-lib/lib/machine/completion/actions/completion.d.ts +12 -18
  86. package/dist-lib/lib/machine/completion/actions/completion.js +19 -178
  87. package/dist-lib/lib/machine/completion/actions/goal.d.ts +10 -0
  88. package/dist-lib/lib/machine/completion/actions/goal.js +93 -0
  89. package/dist-lib/lib/machine/completion/actions/prepare.js +2 -1
  90. package/dist-lib/lib/machine/completion/actions/reject-tool.d.ts +16 -0
  91. package/dist-lib/lib/machine/completion/actions/reject-tool.js +147 -0
  92. package/dist-lib/lib/machine/completion/actions/retry-round.d.ts +6 -0
  93. package/dist-lib/lib/machine/completion/actions/retry-round.js +50 -0
  94. package/dist-lib/lib/machine/completion/actions/rounds.d.ts +5 -0
  95. package/dist-lib/lib/machine/completion/actions/rounds.js +31 -0
  96. package/dist-lib/lib/machine/completion/actions/tools.d.ts +1 -16
  97. package/dist-lib/lib/machine/completion/actions/tools.js +16 -161
  98. package/dist-lib/lib/machine/completion/context.js +3 -3
  99. package/dist-lib/lib/machine/completion/implementations.d.ts +2 -2
  100. package/dist-lib/lib/machine/completion/implementations.js +6 -2
  101. package/dist-lib/lib/machine/completion/machine.d.ts +2 -2
  102. package/dist-lib/lib/machine/completion/metrics.d.ts +25 -0
  103. package/dist-lib/lib/machine/completion/metrics.js +68 -0
  104. package/dist-lib/lib/machine/completion/nudges.d.ts +5 -0
  105. package/dist-lib/lib/machine/completion/nudges.js +33 -0
  106. package/dist-lib/lib/machine/completion/rules.d.ts +5 -34
  107. package/dist-lib/lib/machine/completion/rules.js +4 -120
  108. package/dist-lib/lib/machine/completion/splice.d.ts +9 -0
  109. package/dist-lib/lib/machine/completion/splice.js +19 -0
  110. package/dist-lib/lib/machine/completion/types.d.ts +4 -13
  111. package/dist-lib/lib/machine/index.d.ts +3 -3
  112. package/dist-lib/lib/machine/index.js +2 -2
  113. package/dist-lib/lib/media-host.js +2 -2
  114. package/dist-lib/lib/media-wire.js +2 -2
  115. package/dist-lib/lib/residency/completion-runtime.d.ts +2 -1
  116. package/dist-lib/lib/residency/completion-runtime.js +44 -1
  117. package/dist-lib/lib/residency/index.d.ts +3 -2
  118. package/dist-lib/lib/residency/index.js +2 -2
  119. package/dist-lib/lib/round-host.js +3 -3
  120. package/dist-lib/lib/round-port.js +3 -3
  121. package/dist-lib/lib/run-log/redact.js +7 -7
  122. package/dist-lib/lib/serve/completion.d.ts +7 -0
  123. package/dist-lib/lib/serve/completion.js +553 -0
  124. package/dist-lib/lib/serve/downloads.d.ts +4 -0
  125. package/dist-lib/lib/serve/downloads.js +79 -0
  126. package/dist-lib/lib/serve/models.d.ts +5 -0
  127. package/dist-lib/lib/serve/models.js +120 -0
  128. package/dist-lib/lib/serve/noop.d.ts +1 -0
  129. package/dist-lib/lib/serve/noop.js +1 -0
  130. package/dist-lib/lib/serve/requests.d.ts +147 -0
  131. package/dist-lib/lib/serve/requests.js +1 -0
  132. package/dist-lib/lib/serve/rounds.d.ts +11 -0
  133. package/dist-lib/lib/serve/rounds.js +67 -0
  134. package/dist-lib/lib/serve/run-log-start.d.ts +38 -0
  135. package/dist-lib/lib/serve/run-log-start.js +254 -0
  136. package/dist-lib/lib/serve/skills.d.ts +32 -0
  137. package/dist-lib/lib/serve/skills.js +46 -0
  138. package/dist-lib/lib/serve.d.ts +5 -1
  139. package/dist-lib/lib/serve.js +14 -1071
  140. package/dist-lib/lib/skills-host/index.d.ts +4 -2
  141. package/dist-lib/lib/skills-host/index.js +20 -8
  142. package/dist-lib/lib/tools/fs/drive.d.ts +1 -17
  143. package/dist-lib/lib/tools/fs/remote-protocol.d.ts +1 -81
  144. package/dist-lib/lib/tools/fs/scope.d.ts +2 -17
  145. package/dist-lib/lib/tools/fs/search.d.ts +2 -18
  146. package/dist-lib/lib/tools/fs/walk.d.ts +1 -5
  147. package/dist-lib/lib/tools/pdf/build.d.ts +2 -2
  148. package/dist-lib/lib/tools/pdf/recognize.js +4 -8
  149. package/dist-lib/lib/tools/pdf/request.d.ts +3 -8
  150. package/dist-lib/lib/tools/pdf/request.js +4 -4
  151. package/dist-lib/lib/tools/pdf.d.ts +0 -1
  152. package/dist-lib/lib/tools/pdf.js +0 -1
  153. package/dist-lib/lib/tools/skill.js +13 -4
  154. package/dist-lib/lib/tools/skills/bundled-dir.d.ts +3 -0
  155. package/dist-lib/lib/tools/skills/bundled-dir.js +27 -24
  156. package/dist-lib/lib/tools/skills/bundled-hash.d.ts +1 -0
  157. package/dist-lib/lib/tools/skills/bundled-hash.js +4 -1
  158. package/dist-lib/lib/tools/skills/catalog-loader.d.ts +12 -0
  159. package/dist-lib/lib/tools/skills/catalog-loader.js +173 -0
  160. package/dist-lib/lib/tools/skills/catalog.d.ts +5 -8
  161. package/dist-lib/lib/tools/skills/catalog.js +1 -162
  162. package/dist-lib/lib/tools/skills/index.d.ts +1 -0
  163. package/dist-lib/lib/tools/skills/index.js +1 -0
  164. package/dist-lib/lib/tools/skills/skill-tree.d.ts +5 -0
  165. package/dist-lib/lib/tools/skills/skill-tree.js +54 -0
  166. package/dist-lib/lib/wire/download.d.ts +10 -0
  167. package/dist-lib/lib/wire/download.js +35 -0
  168. package/dist-lib/lib/wire/event.d.ts +41 -0
  169. package/dist-lib/lib/wire/event.js +306 -0
  170. package/dist-lib/lib/wire/fields.d.ts +3 -0
  171. package/dist-lib/lib/wire/fields.js +12 -0
  172. package/dist-lib/lib/wire/knowledge.d.ts +9 -0
  173. package/dist-lib/lib/wire/knowledge.js +68 -0
  174. package/dist-lib/lib/wire/round.d.ts +14 -0
  175. package/dist-lib/lib/wire/round.js +45 -0
  176. package/dist-lib/lib/wire/vectors.d.ts +6 -0
  177. package/dist-lib/lib/wire/vectors.js +33 -0
  178. package/dist-lib/lib/wire.d.ts +28 -77
  179. package/dist-lib/lib/wire.js +24 -489
  180. package/dist-lib/plugins/ocr/coverage.d.ts +3 -0
  181. package/dist-lib/plugins/ocr/coverage.js +67 -0
  182. package/dist-lib/plugins/ocr/ocr-runtime.d.ts +4 -2
  183. package/dist-lib/plugins/ocr/ocr-runtime.js +25 -4
  184. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
  185. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
  186. package/dist-lib/schema/harness.js +4 -1
  187. package/dist-lib/spec/hrpc/messages.js +25 -1
  188. package/dist-lib/spec/hyperschema/index.js +25 -1
  189. package/dist-lib/spec/hyperschema/schema.json +6 -1
  190. package/dist-lib/spec/hyperschema/types.d.ts +1 -0
  191. package/package.json +2 -2
@@ -1,344 +1,18 @@
1
- import { isRecord } from '../guards.js';
2
- import { namedMarkupBlocks, scanNamedMarkup } from './markup-scanner.js';
3
- const TOOL_RESPONSE = { open: '<tool_response>', close: '</tool_response>' };
4
- const TOOL_CALL = { open: '<tool_call>', close: '</tool_call>' };
5
- const MAX_NAME_LENGTH = 64;
6
- export const MAX_RAW_CALL_CHARS = 4096;
7
- export const MALFORMED_CALL_ID = 'call-malformed';
8
- export function clipRawCall(raw) {
9
- return raw.length > MAX_RAW_CALL_CHARS ? raw.slice(0, MAX_RAW_CALL_CHARS) : raw;
10
- }
11
- export function recoverMarkupToolCalls(text, knownTools) {
12
- const known = new Set(knownTools.map((tool) => tool.name));
13
- const calls = [];
14
- for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
15
- if (block.nested) {
16
- calls.push(...recoverLegacyCalls(text.slice(block.start, block.end), known));
17
- continue;
18
- }
19
- if (!block.name || !known.has(block.name)) {
20
- continue;
21
- }
22
- calls.push({
23
- id: '',
24
- name: block.name,
25
- arguments: decodeArguments(block.body).args,
26
- raw: clipRawCall(text.slice(block.start, block.end))
27
- });
28
- }
29
- return calls.map((call, index) => ({ ...call, id: `recovered-markup-${index}` }));
30
- }
31
- function recoverLegacyCalls(text, known) {
32
- const calls = [];
33
- let cursor = 0;
34
- for (;;) {
35
- const start = text.indexOf('<function=', cursor);
36
- if (start === -1) {
37
- return calls;
38
- }
39
- const nameStart = start + '<function='.length;
40
- const nameEnd = text.indexOf('>', nameStart);
41
- const close = nameEnd === -1 ? -1 : text.indexOf('</function>', nameEnd + 1);
42
- if (nameEnd === -1 || nameEnd - nameStart > MAX_NAME_LENGTH || close === -1) {
43
- return calls;
44
- }
45
- const name = text.slice(nameStart, nameEnd).trim();
46
- const end = close + '</function>'.length;
47
- if (name && known.has(name)) {
48
- calls.push({
49
- id: '',
50
- name,
51
- arguments: decodeLegacyArguments(text.slice(nameEnd + 1, close), 64),
52
- raw: clipRawCall(text.slice(start, end))
53
- });
54
- }
55
- cursor = end;
56
- }
57
- }
58
- export function redactToolCallMarkup(text, redactArgs) {
59
- let out = '';
60
- let cursor = 0;
61
- let changed = false;
62
- for (const block of namedMarkupBlocks(text, 'function', Infinity)) {
63
- const replacement = redactedMarkupBlock(block, redactArgs);
64
- if (replacement === null) {
65
- continue;
66
- }
67
- out += text.slice(cursor, block.start) + replacement;
68
- cursor = block.end;
69
- changed = true;
70
- }
71
- out += text.slice(cursor);
72
- return changed ? out : text;
73
- }
74
- function redactedMarkupBlock(block, redactArgs) {
75
- if (block.nested) {
76
- return '<function=redacted></function>';
77
- }
78
- const { args: decoded, ambiguous } = decodeArguments(block.body, Infinity);
79
- if (ambiguous) {
80
- return `<function=${block.name}></function>`;
81
- }
82
- const redacted = redactArgs(block.name, decoded);
83
- const replacement = deepEqual(decoded, redacted)
84
- ? null
85
- : functionMarkup({ name: block.name, arguments: redacted });
86
- return replacement;
87
- }
88
- export function redactBareParameterMarkup(text, isSensitiveName) {
89
- if (!/<parameter/i.test(text)) {
90
- return text;
91
- }
92
- let out = '';
93
- let cursor = 0;
94
- let changed = false;
95
- for (;;) {
96
- const open = scanNamedMarkup(text, 'parameter', { from: cursor, maxNameLength: Infinity }).token;
97
- if (!open) {
98
- break;
99
- }
100
- if (open.kind === 'close' || !isSensitiveName(open.name ?? '')) {
101
- cursor = open.end;
102
- continue;
103
- }
104
- const close = scanNamedMarkup(text, 'parameter', {
105
- from: open.end,
106
- maxNameLength: Infinity
107
- }).token;
108
- out += text.slice(cursor, open.end);
109
- changed = true;
110
- if (close?.kind === 'close') {
111
- out += `[redacted]${text.slice(close.start, close.end)}`;
112
- cursor = close.end;
113
- }
114
- else {
115
- out += '[redacted]';
116
- cursor = text.length;
117
- break;
118
- }
119
- }
120
- out += text.slice(cursor);
121
- return redactRunOnParameterOpeners(changed ? out : text, isSensitiveName);
122
- }
123
- function redactRunOnParameterOpeners(text, isSensitiveName) {
124
- const opener = /<parameter\s*=\s*([^\s<>]+)\s+(?=[^\s>])/gi;
125
- const closer = /<\/parameter>/gi;
126
- let out = '';
127
- let cursor = 0;
128
- for (let match = opener.exec(text); match; match = opener.exec(text)) {
129
- if (!isSensitiveName(match[1])) {
130
- continue;
131
- }
132
- const valueStart = match.index + match[0].length;
133
- closer.lastIndex = valueStart;
134
- const close = closer.exec(text);
135
- out += `${text.slice(cursor, valueStart)}[redacted]`;
136
- if (!close) {
137
- return out;
138
- }
139
- cursor = close.index;
140
- opener.lastIndex = close.index;
141
- }
142
- return cursor === 0 ? text : out + text.slice(cursor);
143
- }
144
- function deepEqual(a, b) {
145
- if (a === b)
146
- return true;
147
- if (Array.isArray(a) || Array.isArray(b)) {
148
- if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
149
- return false;
150
- }
151
- return a.every((value, index) => deepEqual(value, b[index]));
152
- }
153
- if (!isRecord(a) || !isRecord(b)) {
154
- return false;
155
- }
156
- const aKeys = Object.keys(a);
157
- const bKeys = Object.keys(b);
158
- if (aKeys.length !== bKeys.length) {
159
- return false;
160
- }
161
- return aKeys.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key]));
162
- }
163
- export function stripToolCallMarkup(text) {
164
- let out = '';
165
- let cursor = 0;
166
- for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
167
- out += text.slice(cursor, block.start);
168
- cursor = block.end;
169
- }
170
- out += text.slice(cursor);
171
- const withoutSpans = stripSpans(stripToolResponseBlocks(out), TOOL_CALL);
172
- return withoutSpans
173
- .replace(/<\/?(tool_call|tool_response)>/gi, '')
174
- .replace(/\n{3,}/g, '\n\n')
175
- .trim();
176
- }
177
- function lowerAscii(char) {
178
- return char.toLowerCase();
179
- }
180
- function stripSpans(text, span) {
181
- const hay = text.replace(/[A-Z]/g, lowerAscii);
182
- let out = '';
183
- let cursor = 0;
184
- for (;;) {
185
- const start = hay.indexOf(span.open, cursor);
186
- if (start === -1) {
187
- break;
188
- }
189
- out += text.slice(cursor, start);
190
- const end = hay.indexOf(span.close, start + span.open.length);
191
- if (end === -1) {
192
- return out;
193
- }
194
- cursor = end + span.close.length;
195
- }
196
- return out + text.slice(cursor);
197
- }
198
- export function stripToolResponseBlocks(text) {
199
- return stripSpans(text, TOOL_RESPONSE);
200
- }
201
- export function toolCallMarkup(calls) {
202
- return calls.map(markupForCall).join('\n');
203
- }
204
- function markupForCall(call) {
205
- return `<tool_call>\n${functionMarkup(call)}\n</tool_call>`;
206
- }
207
- function functionMarkup(call) {
208
- const params = Object.entries(call.arguments ?? {})
209
- .map(([key, value]) => `<parameter=${key}>\n${markupValue(value)}\n</parameter>\n`)
210
- .join('');
211
- return `<function=${call.name}>\n${params}</function>`;
212
- }
213
- function markupValue(value) {
214
- return typeof value === 'string' ? value : JSON.stringify(value ?? null);
215
- }
216
- const ARG_WRAPPER_KEYS = new Set(['parameters', 'arguments', 'args', 'input', 'params']);
217
- function isPlainObject(value) {
218
- return isRecord(value) && !Array.isArray(value);
219
- }
220
- export function normalizeToolArgs(args, declaredKeys) {
221
- const [only, ...rest] = Object.keys(args);
222
- if (only === undefined || rest.length) {
223
- return args;
224
- }
225
- if (!ARG_WRAPPER_KEYS.has(only) || declaredKeys.includes(only)) {
226
- return args;
227
- }
228
- const inner = args[only];
229
- if (isPlainObject(inner)) {
230
- return inner;
231
- }
232
- if (typeof inner === 'string') {
233
- try {
234
- const parsed = JSON.parse(inner);
235
- if (isPlainObject(parsed)) {
236
- return parsed;
237
- }
238
- }
239
- catch {
240
- return args;
241
- }
242
- }
243
- return args;
244
- }
245
- function decodeArguments(block, maxNameLength = MAX_NAME_LENGTH) {
246
- const args = {};
247
- let ambiguous = false;
248
- for (const param of namedMarkupBlocks(block, 'parameter', maxNameLength)) {
249
- if (!param.name)
250
- continue;
251
- ambiguous ||= param.nested;
252
- defineArgument(args, param.name, param.body);
253
- }
254
- return { args: ambiguous ? decodeLegacyArguments(block, maxNameLength) : args, ambiguous };
255
- }
256
- function decodeLegacyArguments(block, maxNameLength) {
257
- const args = {};
258
- let cursor = 0;
259
- for (;;) {
260
- const start = block.indexOf('<parameter=', cursor);
261
- if (start === -1) {
262
- return args;
263
- }
264
- const nameStart = start + '<parameter='.length;
265
- const nameEnd = block.indexOf('>', nameStart);
266
- if (nameEnd === -1 || nameEnd - nameStart > maxNameLength) {
267
- return args;
268
- }
269
- const end = block.indexOf('</parameter>', nameEnd + 1);
270
- if (end === -1) {
271
- return args;
272
- }
273
- const name = block.slice(nameStart, nameEnd).trim();
274
- if (name) {
275
- defineArgument(args, name, block.slice(nameEnd + 1, end));
276
- }
277
- cursor = end + '</parameter>'.length;
278
- }
279
- }
280
- function defineArgument(args, name, body) {
281
- const raw = body.trim();
282
- let value;
283
- try {
284
- value = JSON.parse(raw);
285
- }
286
- catch {
287
- value = raw;
288
- }
289
- Object.defineProperty(args, name, { value, enumerable: true, configurable: true, writable: true });
290
- }
291
- export function recoverSkillNamedCall(text, skillNames, skillToolName) {
292
- const known = new Set(skillNames);
293
- for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
294
- if (!known.has(block.name))
295
- continue;
296
- return [
297
- {
298
- id: 'recovered-skill-named-call',
299
- name: skillToolName,
300
- arguments: { name: block.name },
301
- raw: clipRawCall(text.slice(block.start, block.end))
302
- }
303
- ];
304
- }
305
- return [];
306
- }
307
- const SKILL_LOAD_KEYS = new Set(['name', 'skill', 'skill_name', 'file', 'path', 'filename']);
308
- export function recoverPrintedSkillLoad(text, skillToolName) {
309
- for (const match of text.matchAll(/\{[^{}]*\}/g)) {
310
- const candidate = parsePrintedCandidate(match[0]);
311
- if (!isPlainObject(candidate)) {
312
- continue;
313
- }
314
- const keys = Object.keys(candidate);
315
- if (keys.length === 0 || !keys.every((key) => SKILL_LOAD_KEYS.has(key))) {
316
- continue;
317
- }
318
- const name = candidate.name ?? candidate.skill ?? candidate.skill_name;
319
- const file = candidate.file ?? candidate.path ?? candidate.filename;
320
- if (typeof name !== 'string' || typeof file !== 'string') {
321
- continue;
322
- }
323
- if (!/\.(md|py)\s*$/i.test(file.replaceAll('"', '').trim())) {
324
- continue;
325
- }
326
- return [
327
- {
328
- id: 'recovered-printed-skill-load',
329
- name: skillToolName,
330
- arguments: { name, file },
331
- raw: clipRawCall(match[0])
332
- }
333
- ];
334
- }
335
- return [];
336
- }
337
- function parsePrintedCandidate(text) {
338
- try {
339
- return JSON.parse(text);
340
- }
341
- catch {
342
- return null;
343
- }
1
+ import { toolCallMarkup } from './tool-call-markup/emit.js';
2
+ import { MALFORMED_CALL_ID, MAX_RAW_CALL_CHARS, clipRawCall, normalizeToolArgs, recoverMarkupToolCalls } from './tool-call-markup/recover.js';
3
+ import { recoverPrintedSkillLoad, recoverSkillNamedCall } from './tool-call-markup/skill-recovery.js';
4
+ import { redactBareParameterMarkup, redactToolCallMarkup, stripToolCallMarkup, stripToolResponseBlocks } from './tool-call-markup/strip.js';
5
+ export class ToolCallMarkup {
6
+ static MALFORMED_CALL_ID = MALFORMED_CALL_ID;
7
+ static MAX_RAW_CALL_CHARS = MAX_RAW_CALL_CHARS;
8
+ static clipRawCall = clipRawCall;
9
+ static normalizeToolArgs = normalizeToolArgs;
10
+ static recoverMarkupToolCalls = recoverMarkupToolCalls;
11
+ static recoverPrintedSkillLoad = recoverPrintedSkillLoad;
12
+ static recoverSkillNamedCall = recoverSkillNamedCall;
13
+ static redactBareParameterMarkup = redactBareParameterMarkup;
14
+ static redactToolCallMarkup = redactToolCallMarkup;
15
+ static stripToolCallMarkup = stripToolCallMarkup;
16
+ static stripToolResponseBlocks = stripToolResponseBlocks;
17
+ static toolCallMarkup = toolCallMarkup;
344
18
  }
@@ -1,23 +1,17 @@
1
- import type { AnsweredPayload, CompletionActionArgs, CompletionContext, PreparingPayload, RetrievingOutput, RoundDonePayload } from '../types.ts';
2
- import type { GoalStatus, RunOutcome } from '../../../contract/run.ts';
1
+ import type { AnsweredPayload, CompletionActionArgs, CompletionContext, PreparingPayload, QueuedInput, RoundDonePayload } from '../types.ts';
2
+ import type { CompletionHistoryEntry, RunOutcome } from '../../../contract/run.ts';
3
3
  import type { EngineError } from '../../../helpers/engine-error.ts';
4
- export declare function startInitialRound({ context, params }: CompletionActionArgs<PreparingPayload>): Partial<CompletionContext>;
5
- export declare function startNextRound({ context }: CompletionActionArgs): Partial<CompletionContext>;
6
- export declare function retryMuteRound(args: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
7
- export declare function retryNarratedRound({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
8
- export declare function retryMalformedRound({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
9
- export declare function retryCappedSearch({ context }: CompletionActionArgs): Partial<CompletionContext>;
10
- export declare function retryCutRound({ context }: CompletionActionArgs): Partial<CompletionContext>;
4
+ export declare function silentTurn({ context, params }: CompletionActionArgs<AnsweredPayload>, reAsk?: CompletionHistoryEntry[]): Partial<CompletionContext>;
11
5
  export declare function wrapCutCompletion({ context }: CompletionActionArgs): Partial<CompletionContext>;
12
- export declare function continueWithFollowUps({ context }: CompletionActionArgs): Partial<CompletionContext>;
13
- export declare function addKnowledge({ context, params }: CompletionActionArgs<RetrievingOutput>): Partial<CompletionContext>;
14
- export declare function goalTurn(context: Readonly<CompletionContext>, stall?: string): string;
15
- export declare function continueStalledGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
16
- export declare function markGoalStalled({ context }: CompletionActionArgs): Partial<CompletionContext>;
17
- export declare function continueGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
18
- export declare function continueStop({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
19
- export declare function settleGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
20
- export declare function goalStatus(context: Readonly<CompletionContext>): GoalStatus;
6
+ export declare function cutTurns(context: Readonly<CompletionContext>): Partial<CompletionContext>;
7
+ export declare function closeFailureMemo(context: Readonly<CompletionContext>): Partial<CompletionContext>;
8
+ interface RoundClose {
9
+ last?: boolean;
10
+ }
11
+ export declare function endToolRound(context: Readonly<CompletionContext>, { last }?: RoundClose): Partial<CompletionContext>;
12
+ export declare function continuation(context: Readonly<CompletionContext>, { last }?: RoundClose): CompletionHistoryEntry[];
13
+ export declare function userTurns(queued: readonly QueuedInput[]): CompletionHistoryEntry[];
14
+ export declare function continueAfterAnswer(context: Readonly<CompletionContext>, turns: CompletionHistoryEntry[]): Partial<CompletionContext>;
21
15
  export declare function finishRound({ context, params }: CompletionActionArgs<RoundDonePayload>): Partial<CompletionContext>;
22
16
  export declare function finishWrapRound({ context, params }: CompletionActionArgs<RoundDonePayload>): Partial<CompletionContext>;
23
17
  export declare function wrapCompletion({ context, params }: CompletionActionArgs<RunOutcome>): Partial<CompletionContext>;
@@ -1,34 +1,9 @@
1
- import { hasRoundsLeft, stuckResult } from '../guards/retry.js';
2
- import { asksChecks, asksDeliverable, asksRun, citesPassed, deliverableAsk, deliverableChecked, existenceOnly, GOAL_CASES_ASK, GOAL_REVIEW_ASK, MAX_GOAL_CHECKS, MAX_PROOF_ASKS, mergeChecks, namedChecks, runAsk } from '../../../helpers/goal-check.js';
3
- import { activate, applyHistorySplice, applySplice, casesDue, checksMet, foldMetrics, goalMet, goalTokensLeft, imagesShownNote, MAX_SHOWN_IMAGES, nudgeWithChecks, nudgeWithPlan, retainFailureResults, settledSince } from '../rules.js';
4
- export function startInitialRound({ context, params }) {
5
- return applyPreparation(context, params);
6
- }
7
- export function startNextRound({ context }) {
8
- return {
9
- ...applyPreparation(context),
10
- ...closeFailureMemo(context),
11
- ...endToolRound(context),
12
- round: context.round + 1,
13
- mute: 0,
14
- cuts: 0,
15
- narrated: false,
16
- malformed: false,
17
- cappedRounds: 0
18
- };
19
- }
20
- export function retryMuteRound(args) {
21
- const { context } = args;
22
- const { goal } = context;
23
- const goalReAsk = goal && !context.steering.length && !goalMet(context);
24
- return {
25
- ...silentTurn(args, goalReAsk ? [{ role: 'user', content: goalTurn(context) }] : continuation(context)),
26
- goal: goalReAsk && !args.params.cutOff ? askedGoal(context, goal) : goal,
27
- mute: context.mute + 1,
28
- round: context.round + 1
29
- };
30
- }
31
- function silentTurn({ context, params }, reAsk = continuation(context)) {
1
+ import { GoalCheck } from '../../../helpers/goal-check.js';
2
+ import { foldMetrics } from '../metrics.js';
3
+ import { imagesShownNote, nudgeWithPlan } from '../nudges.js';
4
+ import { activate, goalMet, MAX_SHOWN_IMAGES, retainFailureResults } from '../rules.js';
5
+ import { applyHistorySplice } from '../splice.js';
6
+ export function silentTurn({ context, params }, reAsk = continuation(context)) {
32
7
  if (params.cutOff) {
33
8
  return {};
34
9
  }
@@ -41,45 +16,10 @@ function silentTurn({ context, params }, reAsk = continuation(context)) {
41
16
  steering: []
42
17
  };
43
18
  }
44
- export function retryNarratedRound({ context, params }) {
45
- return {
46
- narrated: true,
47
- history: [
48
- ...context.history,
49
- { role: 'assistant', content: params.narrated ?? '' },
50
- ...continuation(context)
51
- ],
52
- steering: [],
53
- round: context.round + 1
54
- };
55
- }
56
- export function retryMalformedRound({ context, params }) {
57
- return {
58
- ...applyHistorySplice(context, params.history),
59
- malformed: true,
60
- round: context.round + 1
61
- };
62
- }
63
- export function retryCappedSearch({ context }) {
64
- return {
65
- ...applyPreparation(context),
66
- ...closeFailureMemo(context),
67
- ...endToolRound(context),
68
- round: context.round + 1,
69
- mute: 0,
70
- cuts: 0,
71
- narrated: false,
72
- malformed: false,
73
- cappedRounds: context.cappedRounds + 1
74
- };
75
- }
76
- export function retryCutRound({ context }) {
77
- return { ...cutTurns(context), cuts: context.cuts + 1, round: context.round + 1 };
78
- }
79
19
  export function wrapCutCompletion({ context }) {
80
20
  return { ...cutTurns(context), outcome: 'mute' };
81
21
  }
82
- function cutTurns(context) {
22
+ export function cutTurns(context) {
83
23
  const said = context.lastRound?.contentText.trim();
84
24
  return {
85
25
  history: [
@@ -90,7 +30,7 @@ function cutTurns(context) {
90
30
  steering: []
91
31
  };
92
32
  }
93
- function closeFailureMemo(context) {
33
+ export function closeFailureMemo(context) {
94
34
  if (!context.roundProgressed)
95
35
  return {};
96
36
  return {
@@ -100,7 +40,7 @@ function closeFailureMemo(context) {
100
40
  roundProgressed: false
101
41
  };
102
42
  }
103
- function endToolRound(context, { last = false } = {}) {
43
+ export function endToolRound(context, { last = false } = {}) {
104
44
  return {
105
45
  history: [
106
46
  ...context.history,
@@ -128,7 +68,7 @@ function showing(turns, images) {
128
68
  }
129
69
  ];
130
70
  }
131
- function continuation(context, { last = false } = {}) {
71
+ export function continuation(context, { last = false } = {}) {
132
72
  const { plan } = context;
133
73
  return context.steering.length
134
74
  ? userTurns(context.steering)
@@ -141,10 +81,10 @@ function roundNudge(context, last) {
141
81
  }
142
82
  return last ? (policy.wrapUpNudge ?? policy.continuationNudge) : policy.continuationNudge;
143
83
  }
144
- function userTurns(queued) {
84
+ export function userTurns(queued) {
145
85
  return queued.map(({ text, content }) => ({ role: 'user', content: content ?? text }));
146
86
  }
147
- function continueAfterAnswer(context, turns) {
87
+ export function continueAfterAnswer(context, turns) {
148
88
  return {
149
89
  history: [...context.history, ...turns],
150
90
  outcome: null,
@@ -157,105 +97,6 @@ function continueAfterAnswer(context, turns) {
157
97
  cappedRounds: 0
158
98
  };
159
99
  }
160
- export function continueWithFollowUps({ context }) {
161
- return {
162
- ...continueAfterAnswer(context, userTurns(context.followUps)),
163
- followUps: [],
164
- edgeInputs: context.followUps
165
- };
166
- }
167
- export function addKnowledge({ context, params }) {
168
- return {
169
- ...applyHistorySplice(context, params.history),
170
- sources: params.sources ? [...context.sources, ...params.sources] : context.sources
171
- };
172
- }
173
- export function goalTurn(context, stall) {
174
- const { policy, goal, plan } = context;
175
- const nudge = stall ? `${policy.goalNudge}\n${stall}` : policy.goalNudge;
176
- const turn = nudgeWithChecks(nudgeWithPlan(`${nudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], !!goal && asksChecks(goal));
177
- const deliverable = goal?.deliverable && asksDeliverable(goal)
178
- ? `${turn}\n${deliverableAsk(goal.deliverable)}`
179
- : turn;
180
- const edited = goal?.edited?.at(-1);
181
- const asked = goal && edited && asksRun(goal) ? `${deliverable}\n${runAsk(edited)}` : deliverable;
182
- if (reviewDue(context)) {
183
- return `${asked}\n${GOAL_REVIEW_ASK}`;
184
- }
185
- return casesDue(context) ? `${asked}\n${GOAL_CASES_ASK}` : asked;
186
- }
187
- function reviewDue(context) {
188
- return !!context.goal?.checkable && context.goal.review === 'due' && checksMet(context);
189
- }
190
- function askedGoal(context, goal) {
191
- if (reviewDue(context)) {
192
- return { ...goal, review: 'asked' };
193
- }
194
- return casesDue(context) ? { ...goal, cases: true } : goal;
195
- }
196
- export function continueStalledGoal({ context }) {
197
- const { goal, policy } = context;
198
- const name = stuckResult(context)?.name;
199
- const stall = policy.goalStalledNudge.replaceAll('{call}', name ? `\`${name}\`` : 'your recent calls');
200
- return {
201
- ...applyPreparation(context),
202
- ...closeFailureMemo(context),
203
- history: [
204
- ...context.history,
205
- ...userTurns(context.steering),
206
- { role: 'user', content: goalTurn(context, stall) }
207
- ],
208
- steering: [],
209
- delivered: context.delivered || context.deliveredThisRound,
210
- deliveredThisRound: false,
211
- blockedRepeats: 0,
212
- roundProgressed: false,
213
- settledResults: settledSince(context.settledResults),
214
- round: context.round + 1,
215
- mute: 0,
216
- cuts: 0,
217
- narrated: false,
218
- malformed: false,
219
- cappedRounds: 0,
220
- goal: goal && askedGoal(context, { ...goal, turns: goal.turns + 1 })
221
- };
222
- }
223
- export function markGoalStalled({ context }) {
224
- const { goal } = context;
225
- return goal ? { goal: { ...goal, stalled: true } } : {};
226
- }
227
- export function continueGoal({ context }) {
228
- const { goal } = context;
229
- return {
230
- ...continueAfterAnswer(context, [{ role: 'user', content: goalTurn(context) }]),
231
- goal: goal && askedGoal(context, { ...goal, turns: goal.turns + 1 })
232
- };
233
- }
234
- export function continueStop({ context, params }) {
235
- return continueAfterAnswer(context, [{ role: 'user', content: params.continue ?? '' }]);
236
- }
237
- export function settleGoal({ context }) {
238
- const { goal } = context;
239
- return goal ? { goal: { ...goal, status: goalStatus(context) } } : {};
240
- }
241
- export function goalStatus(context) {
242
- if (goalMet(context)) {
243
- return 'met';
244
- }
245
- if (context.goal?.stalled) {
246
- return 'stalled';
247
- }
248
- if (!hasRoundsLeft(context)) {
249
- return 'rounds';
250
- }
251
- if (context.timeUp) {
252
- return 'time';
253
- }
254
- if (!goalTokensLeft(context)) {
255
- return 'tokens';
256
- }
257
- return 'open';
258
- }
259
100
  export function finishRound({ context, params }) {
260
101
  const metrics = { ...context.metrics };
261
102
  foldMetrics(metrics, context.round, params);
@@ -287,14 +128,14 @@ function adoptChecks(goal, text, calling) {
287
128
  if (goal?.review === 'asked') {
288
129
  return adoptReview(goal, text, calling);
289
130
  }
290
- if (goal?.named && !deliverableChecked(goal)) {
131
+ if (goal?.named && !GoalCheck.deliverableChecked(goal)) {
291
132
  return addChecks(goal, text);
292
133
  }
293
134
  if (!goal?.checkable || goal.named) {
294
135
  return goal;
295
136
  }
296
137
  const host = goal.checks.map((check) => check.name);
297
- const names = mergeChecks(host, namedChecks(text));
138
+ const names = GoalCheck.mergeChecks(host, GoalCheck.namedChecks(text));
298
139
  if (!names.length) {
299
140
  return goal;
300
141
  }
@@ -303,7 +144,7 @@ function adoptChecks(goal, text, calling) {
303
144
  }
304
145
  function adoptReview(goal, text, calling) {
305
146
  const passed = goal.checks.filter((check) => check.passed).map((check) => check.name);
306
- if (citesPassed(text, goal.proof ? [...passed, goal.proof] : passed)) {
147
+ if (GoalCheck.citesPassed(text, goal.proof ? [...passed, goal.proof] : passed)) {
307
148
  return { ...addChecks(goal, text), review: 'done' };
308
149
  }
309
150
  if (calling) {
@@ -313,14 +154,14 @@ function adoptReview(goal, text, calling) {
313
154
  return {
314
155
  ...addChecks(goal, text),
315
156
  reviewAsks,
316
- review: reviewAsks < MAX_PROOF_ASKS ? 'due' : 'done'
157
+ review: reviewAsks < GoalCheck.MAX_PROOF_ASKS ? 'due' : 'done'
317
158
  };
318
159
  }
319
160
  function addChecks(goal, text) {
320
161
  const known = goal.checks.map((check) => check.name);
321
- const fresh = namedChecks(text)
322
- .filter((name) => !known.includes(name) && !existenceOnly([name]))
323
- .slice(0, MAX_GOAL_CHECKS - known.length);
162
+ const fresh = GoalCheck.namedChecks(text)
163
+ .filter((name) => !known.includes(name) && !GoalCheck.existenceOnly([name]))
164
+ .slice(0, GoalCheck.MAX_GOAL_CHECKS - known.length);
324
165
  return fresh.length
325
166
  ? {
326
167
  ...goal,
@@ -0,0 +1,10 @@
1
+ import type { AnsweredPayload, CompletionActionArgs, CompletionContext, GoalState } from '../types.ts';
2
+ import type { GoalStatus } from '../../../contract/run.ts';
3
+ export declare function goalTurn(context: Readonly<CompletionContext>, stall?: string): string;
4
+ export declare function askedGoal(context: Readonly<CompletionContext>, goal: GoalState): GoalState;
5
+ export declare function continueStalledGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
6
+ export declare function markGoalStalled({ context }: CompletionActionArgs): Partial<CompletionContext>;
7
+ export declare function continueGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
8
+ export declare function continueStop({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
9
+ export declare function settleGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
10
+ export declare function goalStatus(context: Readonly<CompletionContext>): GoalStatus;