@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
@@ -2,8 +2,6 @@ import b4a from 'b4a';
2
2
  import { randomBytes } from 'crypto';
3
3
  import EventEmitter from 'events';
4
4
  import fs from 'fs';
5
- import { z } from 'zod';
6
- import { HarnessError } from './errors.js';
7
5
  import { settleOnClose } from './rpc-session.js';
8
6
  import { harnessClient } from '../spec/calls.js';
9
7
  import HRPC from '../spec/hrpc/index.js';
@@ -12,23 +10,24 @@ import { unsupportedKvCacheDeletion } from './capabilities/kv-cache.js';
12
10
  import { checkGoal, checkMaxToolRounds } from './capabilities/tool-turns.js';
13
11
  import { Agent } from './agent/index.js';
14
12
  import { readModelSourcePreview } from './capabilities/preview-model-source.js';
15
- import { RunLog } from './run-log/index.js';
16
- import { AbortedError } from './helpers/engine-error.js';
17
13
  import { MediaHost } from './media-host.js';
18
- import { discardRequestWire, mediaRequestWire, mediaResultFrom, mediaTickFrom } from './media-wire.js';
14
+ import { discardRequestWire, mediaRequestWire } from './media-wire.js';
19
15
  import { RoundHost } from './round-host.js';
20
16
  import { HarnessLog } from './log.js';
21
17
  import { assertEmbedDevice } from './models/embed-config.js';
22
18
  import { addonTaskType } from './models/model-task.js';
23
- import { errorCode, errorMessage, isAskForm, isRecord } from './guards.js';
24
- import { pushQueue } from './queue.js';
25
- import { stageFile, sweepStaging } from './staging.js';
19
+ import { errorMessage, isRecord } from './guards.js';
20
+ import { sweepStaging } from './staging.js';
26
21
  import { sweepSpill } from './spill.js';
27
22
  import { readInstall, readSetup } from './tools/skills/manifest-parse.js';
28
- import { SKILL_STATE } from './tools/skills/skill-state.js';
29
23
  import { asToolset } from './tools/toolsets.js';
30
- import { fromDownloadWire, roundResultFrom, fromKnowledgeHitWire, fromKnowledgeStatusWire, fromKnowledgeUpsertResultWire, fromWire, toKnowledgeUpsertWire, unpackVectors } from './wire.js';
31
- export const HARNESS_DIED = 'HARNESS_DIED';
24
+ import { Wire } from './wire.js';
25
+ import { loadedModel, parseSkillState, errorDetails, knobs, explicitEmptyIdentityFields, frames, downloadFrames } from './connect/decode.js';
26
+ import { died, noop, abortable, awaitWithAbort } from './connect/abort.js';
27
+ import { abortedRound, servedRound, servedMedia } from './connect/served.js';
28
+ import { bridgeAttach, bridgeShow, bridgeFetch, bridgeDrive, sendInput, answerQuestion, approvalCall } from './connect/bridges.js';
29
+ import { bridgeRunLog, validateHostRunLogLease, acquireHostRunLogLease } from './connect/host-run-log.js';
30
+ export { HARNESS_DIED } from './connect/abort.js';
32
31
  const PAUSE_GRACE_MS = 10_000;
33
32
  export function connectHarness(transport, options = {}) {
34
33
  return new RemoteHarness(transport, options);
@@ -168,6 +167,12 @@ export class RemoteHarness extends EventEmitter {
168
167
  if (context.showImage) {
169
168
  payload.showImage = true;
170
169
  }
170
+ if (context.projectSkills?.length) {
171
+ payload.projectSkills = context.projectSkills;
172
+ }
173
+ if (context.workspaceSkills?.length) {
174
+ payload.workspaceSkills = context.workspaceSkills;
175
+ }
171
176
  if (context.maxToolRounds !== undefined && context.maxToolRounds !== 0) {
172
177
  payload.maxToolRounds = checkMaxToolRounds(context.maxToolRounds);
173
178
  }
@@ -560,7 +565,7 @@ export class RemoteHarness extends EventEmitter {
560
565
  media?.cancel(event.callId ?? '');
561
566
  continue;
562
567
  }
563
- const wire = fromWire(event);
568
+ const wire = Wire.fromWire(event);
564
569
  if (wire.type === 'aborted' || wire.type === 'paused') {
565
570
  emittedAborted = true;
566
571
  }
@@ -635,7 +640,7 @@ export class RemoteHarness extends EventEmitter {
635
640
  }
636
641
  context.signal?.addEventListener?.('abort', onAbort, { once: true });
637
642
  for await (const event of downloadFrames(stream)) {
638
- const wire = fromDownloadWire(event);
643
+ const wire = Wire.fromDownloadWire(event);
639
644
  if (wire.type === 'aborted') {
640
645
  emittedAborted = true;
641
646
  }
@@ -662,7 +667,7 @@ export class RemoteHarness extends EventEmitter {
662
667
  async embed(context) {
663
668
  const { client } = await this._ensure();
664
669
  const response = await abortable(client.embed({ model: context.model, texts: context.texts }), context.signal);
665
- const rows = unpackVectors(response.dimensions, response.vectors);
670
+ const rows = Wire.unpackVectors(response.dimensions, response.vectors);
666
671
  if (rows.length !== context.texts.length) {
667
672
  throw new Error(`embed returned ${rows.length} vectors for ${context.texts.length} texts`);
668
673
  }
@@ -681,7 +686,7 @@ export class RemoteHarness extends EventEmitter {
681
686
  height,
682
687
  data: b4a.from(data.buffer, data.byteOffset, data.byteLength)
683
688
  }), context.signal);
684
- return response.text;
689
+ return { text: response.text, coverage: response.coverage ?? 0 };
685
690
  }
686
691
  async detectLanguage(context) {
687
692
  const { client } = await this._ensure();
@@ -898,8 +903,8 @@ export class RemoteHarness extends EventEmitter {
898
903
  }
899
904
  async knowledgeUpsert(artifact) {
900
905
  const { client } = await this._ensure();
901
- const response = await client.knowledgeUpsert(toKnowledgeUpsertWire(artifact));
902
- return fromKnowledgeUpsertResultWire(response, artifact.chunks.length);
906
+ const response = await client.knowledgeUpsert(Wire.toKnowledgeUpsertWire(artifact));
907
+ return Wire.fromKnowledgeUpsertResultWire(response, artifact.chunks.length);
903
908
  }
904
909
  async knowledgeRemove(artifactId) {
905
910
  const { client } = await this._ensure();
@@ -914,12 +919,12 @@ export class RemoteHarness extends EventEmitter {
914
919
  k: query.k,
915
920
  focus: query.focus && [...query.focus]
916
921
  });
917
- return response.hits.map(fromKnowledgeHitWire);
922
+ return response.hits.map(Wire.fromKnowledgeHitWire);
918
923
  }
919
924
  async knowledgeStatus() {
920
925
  const { client } = await this._ensure();
921
926
  const response = await client.knowledgeStatus({});
922
- return fromKnowledgeStatusWire(response);
927
+ return Wire.fromKnowledgeStatusWire(response);
923
928
  }
924
929
  async list(options = {}) {
925
930
  const { client } = await this._ensure();
@@ -1046,452 +1051,3 @@ export class RemoteHarness extends EventEmitter {
1046
1051
  };
1047
1052
  }
1048
1053
  }
1049
- function loadedModel({ model, refs, fit }) {
1050
- if (!fit) {
1051
- return { model, refs };
1052
- }
1053
- const { verdict, reason, plan, deviceBytes, hostBytes } = fit;
1054
- return {
1055
- model,
1056
- refs,
1057
- fit: {
1058
- verdict,
1059
- reason,
1060
- plan: plan ?? undefined,
1061
- deviceBytes: deviceBytes || undefined,
1062
- hostBytes: hostBytes || undefined
1063
- }
1064
- };
1065
- }
1066
- function parseSkillState(value) {
1067
- return value ? value : SKILL_STATE.failed;
1068
- }
1069
- function errorDetails(err) {
1070
- return { error: errorMessage(err), code: errorCode(err) };
1071
- }
1072
- function isToolCallDisplay(value) {
1073
- return isRecord(value) && typeof value.label === 'string';
1074
- }
1075
- function died(message) {
1076
- return Object.assign(new Error(message), { code: HARNESS_DIED });
1077
- }
1078
- const noop = () => { };
1079
- function abortedRound(signal) {
1080
- const aborted = new Promise((_resolve, reject) => {
1081
- const abort = () => reject(new AbortedError());
1082
- if (signal?.aborted) {
1083
- abort();
1084
- }
1085
- signal?.addEventListener?.('abort', abort, { once: true });
1086
- });
1087
- aborted.catch(noop);
1088
- return aborted;
1089
- }
1090
- async function servedRound(events, io) {
1091
- let emit = io.emit;
1092
- for await (const event of events) {
1093
- if (io.signal?.aborted)
1094
- break;
1095
- if (event.type === 'content' || event.type === 'thinking') {
1096
- emit({ type: event.type, text: event.text ?? '' });
1097
- }
1098
- else if (event.type === 'model-round-retake') {
1099
- emit = io.retake();
1100
- }
1101
- else if (event.type === 'model-round-result') {
1102
- return roundResultFrom(event);
1103
- }
1104
- }
1105
- throw died('the harness ended a served round without its result');
1106
- }
1107
- async function servedMedia(events, io) {
1108
- for await (const event of events) {
1109
- if (io.signal?.aborted)
1110
- break;
1111
- if (event.type === 'media-round-tick') {
1112
- io.tick(mediaTickFrom(event.payload));
1113
- }
1114
- else if (event.type === 'media-round-result') {
1115
- return await mediaResultFrom(event);
1116
- }
1117
- }
1118
- throw died('the harness ended a served media round without its result');
1119
- }
1120
- function abortable(promise, signal) {
1121
- if (!signal)
1122
- return promise;
1123
- return new Promise((resolve, reject) => {
1124
- if (signal.aborted) {
1125
- promise.then(noop, noop);
1126
- reject(new HarnessError('EMBED_ABORTED'));
1127
- return;
1128
- }
1129
- const onAbort = () => reject(new HarnessError('EMBED_ABORTED'));
1130
- signal.addEventListener?.('abort', onAbort, { once: true });
1131
- promise.then(resolve, reject).finally(() => signal.removeEventListener?.('abort', onAbort));
1132
- });
1133
- }
1134
- function knobs(agent) {
1135
- return {
1136
- id: agent.id,
1137
- name: agent.name,
1138
- bio: agent.bio,
1139
- reasoning: agent.reasoning ?? undefined,
1140
- enabledTools: agent.enabledTools ?? undefined,
1141
- enabledSkills: agent.enabledSkills ?? undefined,
1142
- toolsScope: agent.toolsScope ?? undefined,
1143
- skillsScope: agent.skillsScope ?? undefined,
1144
- ctxSize: agent.ctxSize ?? undefined,
1145
- kvCacheK: agent.kvCacheK ?? undefined,
1146
- kvCacheV: agent.kvCacheV ?? undefined,
1147
- imageModelChoice: agent.imageModelChoice ?? undefined,
1148
- imageSteps: agent.imageSteps ?? undefined,
1149
- imageWidth: agent.imageWidth ?? undefined,
1150
- imageHeight: agent.imageHeight ?? undefined,
1151
- approvalMode: agent.approvalMode ?? undefined,
1152
- disabledToolsets: agent.disabledToolsets ?? undefined,
1153
- policy: agent.policy ?? undefined,
1154
- handoffs: agent.handoffs?.map(({ id, description }) => ({ id, description })),
1155
- plan: agent.plan ?? undefined,
1156
- model: agent.model ?? undefined
1157
- };
1158
- }
1159
- function explicitEmptyIdentityFields(context) {
1160
- const fields = [];
1161
- if (context.model === '') {
1162
- fields.push('model');
1163
- }
1164
- if (context.cacheKey === '') {
1165
- fields.push('cacheKey');
1166
- }
1167
- if (context.thinkingDefault === '') {
1168
- fields.push('thinkingDefault');
1169
- }
1170
- if (context.agent?.id === '') {
1171
- fields.push('agent.id');
1172
- }
1173
- if (context.agent?.name === '') {
1174
- fields.push('agent.name');
1175
- }
1176
- if (context.agent?.bio === '') {
1177
- fields.push('agent.bio');
1178
- }
1179
- return fields;
1180
- }
1181
- async function bridgeAttach(event, attach) {
1182
- try {
1183
- if (!attach) {
1184
- return { type: 'attach-result', message: 'the host cannot persist attachments' };
1185
- }
1186
- const bytes = await fs.promises.readFile(event.path ?? '');
1187
- const record = await attach({
1188
- fileName: event.fileName ?? '',
1189
- mimeType: event.mimeType ?? '',
1190
- bytes,
1191
- raw: event.raw === true
1192
- });
1193
- return {
1194
- type: 'attach-result',
1195
- attachment: {
1196
- id: record.id,
1197
- fileName: record.fileName,
1198
- mimeType: record.mimeType,
1199
- byteLength: record.byteLength
1200
- }
1201
- };
1202
- }
1203
- catch (err) {
1204
- return { type: 'attach-result', message: errorMessage(err) };
1205
- }
1206
- finally {
1207
- if (event.path) {
1208
- void fs.promises.unlink(event.path).catch(noop);
1209
- }
1210
- }
1211
- }
1212
- async function bridgeShow(event, context) {
1213
- if (!context.showImage) {
1214
- return { type: 'fetch-result', message: 'the host cannot show images' };
1215
- }
1216
- try {
1217
- const path = await context.showImage({
1218
- attachmentId: event.attachmentId ?? '',
1219
- signal: context.signal
1220
- });
1221
- return path ? { type: 'fetch-result', path } : { type: 'fetch-result' };
1222
- }
1223
- catch (err) {
1224
- return { type: 'fetch-result', message: errorMessage(err) };
1225
- }
1226
- }
1227
- async function bridgeFetch(event, context, staged) {
1228
- if (!context.fetchAttachment) {
1229
- return { type: 'fetch-result', message: 'the host cannot read attachments' };
1230
- }
1231
- try {
1232
- const metaOnly = event.meta === true;
1233
- const found = await context.fetchAttachment({
1234
- attachmentId: event.attachmentId ?? undefined,
1235
- meta: metaOnly ? true : undefined,
1236
- signal: context.signal
1237
- });
1238
- if (!found) {
1239
- return { type: 'fetch-result' };
1240
- }
1241
- const size = 'bytes' in found && found.bytes !== null ? found.bytes.byteLength : found.byteLength;
1242
- if (metaOnly) {
1243
- return {
1244
- type: 'fetch-result',
1245
- meta: true,
1246
- fileName: found.fileName,
1247
- mimeType: found.mimeType,
1248
- attachmentId: found.id,
1249
- size
1250
- };
1251
- }
1252
- if (!('bytes' in found) || found.bytes === null) {
1253
- return { type: 'fetch-result', message: 'attachment has no bytes' };
1254
- }
1255
- const file = await stageFile('fetch', found.bytes);
1256
- staged.push(file);
1257
- return {
1258
- type: 'fetch-result',
1259
- path: file,
1260
- fileName: found.fileName,
1261
- mimeType: found.mimeType,
1262
- attachmentId: found.id,
1263
- size
1264
- };
1265
- }
1266
- catch (err) {
1267
- return { type: 'fetch-result', message: errorMessage(err) };
1268
- }
1269
- }
1270
- const DriveRequestSchema = z.discriminatedUnion('op', [
1271
- z.looseObject({ op: z.literal('entry'), driveKey: z.string(), key: z.string() }),
1272
- z.looseObject({ op: z.literal('get'), driveKey: z.string(), key: z.string() }),
1273
- z.looseObject({ op: z.literal('read'), driveKey: z.string(), key: z.string() }),
1274
- z.looseObject({
1275
- op: z.literal('list'),
1276
- driveKey: z.string(),
1277
- folder: z.string(),
1278
- skip: z.array(z.string()),
1279
- recursive: z.boolean(),
1280
- limit: z.number(),
1281
- cursor: z.string().nullable()
1282
- }),
1283
- z.looseObject({
1284
- op: z.literal('write'),
1285
- driveKey: z.string(),
1286
- key: z.string(),
1287
- path: z.string(),
1288
- executable: z.boolean().optional()
1289
- }),
1290
- z.looseObject({
1291
- op: z.literal('search'),
1292
- driveKey: z.string(),
1293
- folder: z.string(),
1294
- queries: z.array(z.string()),
1295
- regex: z.boolean(),
1296
- caseSensitive: z.boolean(),
1297
- includeIgnored: z.boolean(),
1298
- maxMatches: z.number(),
1299
- maxEntries: z.number(),
1300
- maxMs: z.number()
1301
- })
1302
- ]);
1303
- async function bridgeDrive(event, context) {
1304
- if (!context.driveOp) {
1305
- return { type: 'drive-result', message: 'the host cannot proxy drives' };
1306
- }
1307
- try {
1308
- const payload = await context.driveOp(DriveRequestSchema.parse(event.payload));
1309
- return { type: 'drive-result', payload };
1310
- }
1311
- catch (err) {
1312
- return { type: 'drive-result', message: errorMessage(err) };
1313
- }
1314
- }
1315
- async function sendInput(input, send) {
1316
- const iterator = input[Symbol.asyncIterator]();
1317
- for (;;) {
1318
- const next = await iterator.next();
1319
- await new Promise((resolve) => setTimeout(resolve, 0));
1320
- if (next.done || !send(next.value)) {
1321
- return;
1322
- }
1323
- }
1324
- }
1325
- function answerQuestion(event, { askUser, reply }) {
1326
- const callId = event.callId;
1327
- if (!askUser) {
1328
- reply({ type: 'question-result', callId, message: 'the host cannot ask questions' });
1329
- return;
1330
- }
1331
- void Promise.resolve()
1332
- .then(() => askUser({
1333
- question: event.question ?? '',
1334
- options: Array.isArray(event.options) ? event.options.map(String) : undefined,
1335
- form: isAskForm(event.payload) ? event.payload : undefined
1336
- }))
1337
- .then((answer) => reply({ type: 'question-result', callId, answer: answer ?? '' }), (err) => reply({ type: 'question-result', callId, message: errorMessage(err) }))
1338
- .catch((err) => HarnessLog.warn('question reply failed', { callId, message: errorMessage(err) }));
1339
- }
1340
- async function bridgeRunLog({ event, context, runId, generation }) {
1341
- if (!context.runLog || !generation) {
1342
- return { type: 'run-log-result', message: 'the host does not own this run log' };
1343
- }
1344
- const step = event.payload;
1345
- if (!isEngineStep(step)) {
1346
- return { type: 'run-log-result', message: 'the worker sent an invalid run-log step' };
1347
- }
1348
- try {
1349
- await context.runLog.append(runId ?? '', generation, step);
1350
- return { type: 'run-log-result' };
1351
- }
1352
- catch (err) {
1353
- return { type: 'run-log-result', message: errorMessage(err) };
1354
- }
1355
- }
1356
- function isEngineStep(value) {
1357
- return (isRecord(value) &&
1358
- typeof value.seq === 'number' &&
1359
- Number.isSafeInteger(value.seq) &&
1360
- value.seq >= 0 &&
1361
- typeof value.expectedLength === 'number' &&
1362
- Number.isSafeInteger(value.expectedLength) &&
1363
- value.expectedLength >= 0 &&
1364
- Array.isArray(value.records));
1365
- }
1366
- function isDigestKey(value) {
1367
- return value instanceof Uint8Array && value.byteLength === 32;
1368
- }
1369
- function releaseLateHostLease(pending, runLog, runId) {
1370
- void pending
1371
- .then(async (lease) => {
1372
- if (!lease || typeof lease.generation !== 'string' || !lease.generation) {
1373
- throw new Error('host returned an invalid generation for an aborted run-log begin');
1374
- }
1375
- await runLog.release(runId, lease.generation);
1376
- })
1377
- .catch((err) => HarnessLog.info('aborted run-log begin cleanup failed', {
1378
- runId,
1379
- message: errorMessage(err)
1380
- }));
1381
- }
1382
- function validateHostRunLogLease(lease, runId) {
1383
- if (typeof lease.generation !== 'string' || !lease.generation || !isDigestKey(lease.digestKey)) {
1384
- throw new Error('host returned an invalid run-log lease');
1385
- }
1386
- if (lease.boot) {
1387
- RunLog.validateEngineBoot(lease.boot);
1388
- }
1389
- const metadata = lease.boot?.metadata;
1390
- if (metadata && metadata.runId !== runId) {
1391
- throw new Error('host returned a run-log boot for a different run');
1392
- }
1393
- }
1394
- function approvalCall(event) {
1395
- const identity = isRecord(event.payload) ? event.payload : {};
1396
- const approval = {
1397
- name: event.name ?? '',
1398
- arguments: isRecord(event.arguments) ? event.arguments : {},
1399
- resource: event.resource ?? null,
1400
- effect: event.effect === 'read' ? 'read' : 'write',
1401
- intent: event.intent || null,
1402
- display: isToolCallDisplay(event.display) ? event.display : null,
1403
- quote: event.question || null
1404
- };
1405
- if (typeof identity.callId === 'string') {
1406
- approval.callId = identity.callId;
1407
- }
1408
- if (typeof identity.round === 'number') {
1409
- approval.round = identity.round;
1410
- }
1411
- return approval;
1412
- }
1413
- async function acquireHostRunLogLease(runLog, runId, signal) {
1414
- if (!runId) {
1415
- throw new Error('host-owned run log requires a run id');
1416
- }
1417
- const pending = runLog.begin(runId);
1418
- try {
1419
- return await awaitWithAbort(pending, signal);
1420
- }
1421
- catch (err) {
1422
- if (!signal?.aborted) {
1423
- throw err;
1424
- }
1425
- releaseLateHostLease(pending, runLog, runId);
1426
- return null;
1427
- }
1428
- }
1429
- function awaitWithAbort(promise, signal) {
1430
- if (!signal)
1431
- return promise;
1432
- if (signal.aborted) {
1433
- return Promise.reject(new Error('operation aborted'));
1434
- }
1435
- return new Promise((resolve, reject) => {
1436
- let settled = false;
1437
- const cleanup = () => signal.removeEventListener?.('abort', onAbort);
1438
- const onAbort = () => {
1439
- if (settled)
1440
- return;
1441
- settled = true;
1442
- cleanup();
1443
- reject(new Error('operation aborted'));
1444
- };
1445
- signal.addEventListener?.('abort', onAbort, { once: true });
1446
- promise.then((value) => {
1447
- if (settled)
1448
- return;
1449
- settled = true;
1450
- cleanup();
1451
- resolve(value);
1452
- }, (error) => {
1453
- if (settled)
1454
- return;
1455
- settled = true;
1456
- cleanup();
1457
- reject(error);
1458
- });
1459
- });
1460
- }
1461
- async function* frames(stream) {
1462
- const queue = pushQueue();
1463
- let error = null;
1464
- stream.on('data', (frame) => queue.push(frame));
1465
- stream.on('end', () => {
1466
- HarnessLog.debug('client stream event', { event: 'end' });
1467
- queue.end();
1468
- });
1469
- stream.on('close', () => {
1470
- HarnessLog.debug('client stream event', { event: 'close' });
1471
- queue.end();
1472
- });
1473
- stream.on('error', (err) => {
1474
- HarnessLog.warn('client stream event', { event: 'error', err: err.message });
1475
- error = err;
1476
- queue.end();
1477
- });
1478
- yield* queue;
1479
- if (error) {
1480
- throw error;
1481
- }
1482
- }
1483
- async function* downloadFrames(stream) {
1484
- const queue = pushQueue();
1485
- let error = null;
1486
- stream.on('data', (frame) => queue.push(frame));
1487
- stream.on('end', () => queue.end());
1488
- stream.on('close', () => queue.end());
1489
- stream.on('error', (err) => {
1490
- error = err;
1491
- queue.end();
1492
- });
1493
- yield* queue;
1494
- if (error) {
1495
- throw error;
1496
- }
1497
- }
@@ -1,11 +1,17 @@
1
1
  import type { Message } from './contract/index.ts';
2
- export declare const TOOL_RESULT_TRUNCATION_MARKER = "\n[tool result truncated for context]\n";
3
- export declare function responseRoomChars(ctxSize: number | undefined, cacheTokens: number | undefined): number | undefined;
4
- export declare const TOOL_SEARCH_RESULT = "{\"tool_search\":";
5
- export declare function compactedFrom(message: Message): Message | undefined;
6
- export interface ContextCandidate {
2
+ declare function responseRoomChars(ctxSize: number | undefined, cacheTokens: number | undefined): number | undefined;
3
+ declare function compactedFrom(message: Message): Message | undefined;
4
+ interface ContextCandidate {
7
5
  history: Message[];
8
6
  droppedTurns: number;
9
7
  }
10
- export declare function contextCandidates(history: Message[], ...nudges: string[]): ContextCandidate[];
11
- export declare function compactContent(content: string, target: number): string;
8
+ declare function contextCandidates(history: Message[], ...nudges: string[]): ContextCandidate[];
9
+ declare function compactContent(content: string, target: number): string;
10
+ export type { ContextCandidate };
11
+ export declare class ContextWindow {
12
+ static TOOL_SEARCH_RESULT: string;
13
+ static compactContent: typeof compactContent;
14
+ static compactedFrom: typeof compactedFrom;
15
+ static contextCandidates: typeof contextCandidates;
16
+ static responseRoomChars: typeof responseRoomChars;
17
+ }
@@ -1,17 +1,17 @@
1
1
  import { Fold } from './fold/index.js';
2
- export const TOOL_RESULT_TRUNCATION_MARKER = '\n[tool result truncated for context]\n';
3
- export function responseRoomChars(ctxSize, cacheTokens) {
2
+ const TOOL_RESULT_TRUNCATION_MARKER = '\n[tool result truncated for context]\n';
3
+ function responseRoomChars(ctxSize, cacheTokens) {
4
4
  if (!ctxSize || !cacheTokens) {
5
5
  return undefined;
6
6
  }
7
7
  return Math.floor(Math.max(0, ctxSize - cacheTokens) / 2);
8
8
  }
9
- export const TOOL_SEARCH_RESULT = '{"tool_search":';
9
+ const TOOL_SEARCH_RESULT = '{"tool_search":';
10
10
  const compactedSources = new WeakMap();
11
- export function compactedFrom(message) {
11
+ function compactedFrom(message) {
12
12
  return compactedSources.get(message);
13
13
  }
14
- export function contextCandidates(history, ...nudges) {
14
+ function contextCandidates(history, ...nudges) {
15
15
  const isBoundary = (message) => Fold.isTurn(message, nudges);
16
16
  const { prefix, turns } = splitTurns(history, isBoundary);
17
17
  if (!turns.length) {
@@ -93,7 +93,7 @@ function longestTool(tools) {
93
93
  .filter((tool) => tool.target > TOOL_RESULT_TRUNCATION_MARKER.length)
94
94
  .sort((a, b) => b.target - a.target)[0];
95
95
  }
96
- export function compactContent(content, target) {
96
+ function compactContent(content, target) {
97
97
  const retained = target - TOOL_RESULT_TRUNCATION_MARKER.length;
98
98
  const head = Math.ceil(retained / 2);
99
99
  const tail = Math.floor(retained / 2);
@@ -109,3 +109,10 @@ function compactTool(selected, message, index) {
109
109
  compactedSources.set(compacted, selected.source);
110
110
  return compacted;
111
111
  }
112
+ export class ContextWindow {
113
+ static TOOL_SEARCH_RESULT = TOOL_SEARCH_RESULT;
114
+ static compactContent = compactContent;
115
+ static compactedFrom = compactedFrom;
116
+ static contextCandidates = contextCandidates;
117
+ static responseRoomChars = responseRoomChars;
118
+ }
@@ -1,12 +1,23 @@
1
1
  import type { LlmGenerationParams } from '../models/llm-presets.ts';
2
- import type { DriveRoot } from '../tools/fs/scope.ts';
2
+ import type { DriveRoot } from './drive.ts';
3
3
  import type { CliValidator } from '../tools/skills/cli-schema-types.ts';
4
4
  import type { AgentConfig, ResponseFormat, ToolDefinition } from './completion.ts';
5
5
  import type { CompiledPolicy } from './policy.ts';
6
6
  import type { Message } from './run.ts';
7
7
  export type { LlmGenerationParams };
8
+ export interface SkillFile {
9
+ readonly path: string;
10
+ readonly content: string;
11
+ }
12
+ export interface ProjectSkill {
13
+ readonly name: string;
14
+ readonly hash: string;
15
+ readonly files?: readonly SkillFile[];
16
+ }
17
+ export type WorkspaceSkill = ProjectSkill;
8
18
  export interface AgentSkill {
9
19
  readonly name: string;
20
+ readonly layered?: boolean;
10
21
  readonly description: string;
11
22
  readonly tools: readonly string[];
12
23
  readonly cliValidator?: CliValidator;
@@ -3,7 +3,7 @@ import type { AbortSignal as BareAbortSignal } from 'bare-abort-controller';
3
3
  import type { TextEvent } from '../helpers/thinking.ts';
4
4
  import type { LlmGenerationParams } from '../models/llm-presets.ts';
5
5
  import type { CapabilityProfile } from '../models/model-catalog-constants.ts';
6
- import type { DriveRoot } from '../tools/fs/scope.ts';
6
+ import type { DriveRoot } from './drive.ts';
7
7
  import type { CliValidator } from '../tools/skills/cli-schema-types.ts';
8
8
  import type { UsageBreakdown } from '../capabilities/usage-breakdown.ts';
9
9
  import type { KnowledgeGrant, KnowledgeSource } from './knowledge.ts';
@@ -148,9 +148,13 @@ export interface ToolSpill {
148
148
  readonly dir: string;
149
149
  sink(stream: string): SpillSink;
150
150
  }
151
+ export interface SkillTree {
152
+ readonly root: string;
153
+ }
151
154
  export interface ToolExecuteContext {
152
155
  currentTime?: number | undefined;
153
156
  agentId?: string | null | undefined;
157
+ skillTrees?: ReadonlyMap<string, SkillTree> | undefined;
154
158
  skills?: readonly string[] | undefined;
155
159
  skillsLoadedThisTurn?: readonly string[] | undefined;
156
160
  runId?: string;