@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
@@ -0,0 +1,93 @@
1
+ import { hasRoundsLeft, stuckResult } from '../guards/retry.js';
2
+ import { GoalCheck } from '../../../helpers/goal-check.js';
3
+ import { nudgeWithChecks, nudgeWithPlan } from '../nudges.js';
4
+ import { casesDue, checksMet, goalMet, goalTokensLeft, settledSince } from '../rules.js';
5
+ import { applyPreparation, closeFailureMemo, continueAfterAnswer, userTurns } from './completion.js';
6
+ export function goalTurn(context, stall) {
7
+ const { policy, goal, plan } = context;
8
+ const nudge = stall ? `${policy.goalNudge}\n${stall}` : policy.goalNudge;
9
+ const turn = nudgeWithChecks(nudgeWithPlan(`${nudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], !!goal && GoalCheck.asksChecks(goal));
10
+ const deliverable = goal?.deliverable && GoalCheck.asksDeliverable(goal)
11
+ ? `${turn}\n${GoalCheck.deliverableAsk(goal.deliverable)}`
12
+ : turn;
13
+ const edited = goal?.edited?.at(-1);
14
+ const asked = goal && edited && GoalCheck.asksRun(goal)
15
+ ? `${deliverable}\n${GoalCheck.runAsk(edited)}`
16
+ : deliverable;
17
+ if (reviewDue(context)) {
18
+ return `${asked}\n${GoalCheck.GOAL_REVIEW_ASK}`;
19
+ }
20
+ return casesDue(context) ? `${asked}\n${GoalCheck.GOAL_CASES_ASK}` : asked;
21
+ }
22
+ function reviewDue(context) {
23
+ return !!context.goal?.checkable && context.goal.review === 'due' && checksMet(context);
24
+ }
25
+ export function askedGoal(context, goal) {
26
+ if (reviewDue(context)) {
27
+ return { ...goal, review: 'asked' };
28
+ }
29
+ return casesDue(context) ? { ...goal, cases: true } : goal;
30
+ }
31
+ export function continueStalledGoal({ context }) {
32
+ const { goal, policy } = context;
33
+ const name = stuckResult(context)?.name;
34
+ const stall = policy.goalStalledNudge.replaceAll('{call}', name ? `\`${name}\`` : 'your recent calls');
35
+ return {
36
+ ...applyPreparation(context),
37
+ ...closeFailureMemo(context),
38
+ history: [
39
+ ...context.history,
40
+ ...userTurns(context.steering),
41
+ { role: 'user', content: goalTurn(context, stall) }
42
+ ],
43
+ steering: [],
44
+ delivered: context.delivered || context.deliveredThisRound,
45
+ deliveredThisRound: false,
46
+ blockedRepeats: 0,
47
+ roundProgressed: false,
48
+ settledResults: settledSince(context.settledResults),
49
+ round: context.round + 1,
50
+ mute: 0,
51
+ cuts: 0,
52
+ narrated: false,
53
+ malformed: false,
54
+ cappedRounds: 0,
55
+ goal: goal && askedGoal(context, { ...goal, turns: goal.turns + 1 })
56
+ };
57
+ }
58
+ export function markGoalStalled({ context }) {
59
+ const { goal } = context;
60
+ return goal ? { goal: { ...goal, stalled: true } } : {};
61
+ }
62
+ export function continueGoal({ context }) {
63
+ const { goal } = context;
64
+ return {
65
+ ...continueAfterAnswer(context, [{ role: 'user', content: goalTurn(context) }]),
66
+ goal: goal && askedGoal(context, { ...goal, turns: goal.turns + 1 })
67
+ };
68
+ }
69
+ export function continueStop({ context, params }) {
70
+ return continueAfterAnswer(context, [{ role: 'user', content: params.continue ?? '' }]);
71
+ }
72
+ export function settleGoal({ context }) {
73
+ const { goal } = context;
74
+ return goal ? { goal: { ...goal, status: goalStatus(context) } } : {};
75
+ }
76
+ export function goalStatus(context) {
77
+ if (goalMet(context)) {
78
+ return 'met';
79
+ }
80
+ if (context.goal?.stalled) {
81
+ return 'stalled';
82
+ }
83
+ if (!hasRoundsLeft(context)) {
84
+ return 'rounds';
85
+ }
86
+ if (context.timeUp) {
87
+ return 'time';
88
+ }
89
+ if (!goalTokensLeft(context)) {
90
+ return 'tokens';
91
+ }
92
+ return 'open';
93
+ }
@@ -1,5 +1,6 @@
1
1
  import { applyPreparation } from './completion.js';
2
- import { activate, applyHistorySplice } from '../rules.js';
2
+ import { activate } from '../rules.js';
3
+ import { applyHistorySplice } from '../splice.js';
3
4
  export function requestPrepare({ context, params }) {
4
5
  const { preparation, step } = params;
5
6
  return {
@@ -0,0 +1,16 @@
1
+ import type { CompletionActionArgs, CompletionContext, GatedToolExecutionSnapshot, RejectDeniedApprovalParams, RepeatOfFailedCallParams, ResolvedCallSnapshot, ToolExecutionSnapshot, ToolRejection } from '../types.ts';
2
+ export interface RejectToolParams {
3
+ call: ResolvedCallSnapshot;
4
+ execution: ToolExecutionSnapshot | null;
5
+ rejection: ToolRejection;
6
+ }
7
+ export declare function rejectTool({ context, params }: CompletionActionArgs<RejectToolParams>): Partial<CompletionContext>;
8
+ export declare function rejectDeniedApproval({ context, params }: CompletionActionArgs<RejectDeniedApprovalParams>): Partial<CompletionContext>;
9
+ export declare function rejectRepeatedTool({ params }: CompletionActionArgs<RepeatOfFailedCallParams>): Partial<CompletionContext>;
10
+ export declare function rejectCutOffTool({ context, params }: CompletionActionArgs<ResolvedCallSnapshot>): Partial<CompletionContext>;
11
+ export declare const PLAN_MODE_REFUSAL: string;
12
+ export interface RejectPlanModeWriteParams {
13
+ call: ResolvedCallSnapshot;
14
+ execution: GatedToolExecutionSnapshot;
15
+ }
16
+ export declare function rejectPlanModeWrite({ params }: CompletionActionArgs<RejectPlanModeWriteParams>): Partial<CompletionContext>;
@@ -0,0 +1,147 @@
1
+ import { cutOffMessage } from '../rules.js';
2
+ import { failureResultRef, storeFailureResult, toolDeniedResult } from '../../../helpers/failure-result.js';
3
+ export function rejectTool({ context, params }) {
4
+ const { call, execution, rejection } = params;
5
+ const memoized = memoizedFailure({
6
+ context,
7
+ call,
8
+ result: rejection.result,
9
+ outcome: rejection.outcome
10
+ });
11
+ return {
12
+ currentCall: call,
13
+ execution,
14
+ failureResults: memoized.failureResults ?? context.failureResults,
15
+ rejectedCall: rejectionPayload({
16
+ call,
17
+ ...memoized.value,
18
+ activity: rejection.activity,
19
+ effect: rejection.effect,
20
+ outcome: rejection.outcome,
21
+ deniedKey: rejection.deniedKey,
22
+ emitCall: rejection.emitCall ?? true,
23
+ emitResult: rejection.emitResult ?? true,
24
+ capped: rejection.capped ?? false,
25
+ source: rejection.source,
26
+ repeat: rejection.repeat
27
+ })
28
+ };
29
+ }
30
+ export function rejectDeniedApproval({ context, params }) {
31
+ const { call, execution } = params;
32
+ const facts = execution.gate.facts;
33
+ const result = toolDeniedResult(context.policy.deniedMessage);
34
+ return {
35
+ pendingCall: null,
36
+ approval: { status: 'denied', key: context.approval.key },
37
+ rejectedCall: {
38
+ call,
39
+ result,
40
+ activity: {
41
+ type: 'tool-activity',
42
+ name: call.name,
43
+ args: call.args,
44
+ resource: facts.resource,
45
+ effect: facts.effect,
46
+ decision: 'denied'
47
+ },
48
+ effect: facts.effect,
49
+ outcome: { kind: 'denied', reason: result.error },
50
+ deniedKey: facts.key,
51
+ emitCall: false,
52
+ emitResult: true,
53
+ capped: false,
54
+ source: execution.origin
55
+ }
56
+ };
57
+ }
58
+ function memoizedFailure({ context, call, result, outcome }) {
59
+ if (!outcome || outcome.kind === 'denied') {
60
+ return { value: { result } };
61
+ }
62
+ const ref = failureResultRef(call.key, context.round, call.id);
63
+ const value = {
64
+ name: call.name,
65
+ result: storeFailureResult(result)
66
+ };
67
+ const prior = context.failureResults[ref];
68
+ if (prior) {
69
+ throw new Error(`Failure result reference collision: ${ref}`);
70
+ }
71
+ return {
72
+ value: { resultRef: ref },
73
+ failureResults: { ...context.failureResults, [ref]: value }
74
+ };
75
+ }
76
+ function rejectionPayload(payload) {
77
+ const result = payload.resultRef === undefined ? { result: payload.result } : { resultRef: payload.resultRef };
78
+ return {
79
+ call: payload.call,
80
+ ...result,
81
+ activity: payload.activity,
82
+ effect: payload.effect,
83
+ outcome: payload.outcome,
84
+ deniedKey: payload.deniedKey,
85
+ emitCall: payload.emitCall,
86
+ emitResult: payload.emitResult,
87
+ capped: payload.capped,
88
+ source: payload.source,
89
+ repeat: payload.repeat
90
+ };
91
+ }
92
+ export function rejectRepeatedTool({ params }) {
93
+ const { call, prior } = params;
94
+ return {
95
+ currentCall: call,
96
+ rejectedCall: {
97
+ call,
98
+ resultRef: prior.resultRef,
99
+ activity: null,
100
+ emitCall: false,
101
+ emitResult: false,
102
+ capped: false,
103
+ source: 'tool-result',
104
+ repeat: prior.outcome
105
+ }
106
+ };
107
+ }
108
+ export function rejectCutOffTool({ context, params }) {
109
+ return {
110
+ currentCall: params,
111
+ rejectedCall: {
112
+ call: params,
113
+ result: { error: cutOffMessage(context.policy, context.lastRound), guidance: true },
114
+ activity: null,
115
+ emitCall: true,
116
+ emitResult: true,
117
+ capped: false,
118
+ source: 'tool-result'
119
+ }
120
+ };
121
+ }
122
+ export const PLAN_MODE_REFUSAL = 'Plan mode: this write was not run. Call update_plan with your steps and submit true to ask ' +
123
+ 'the user to approve the plan; writes run once they do.';
124
+ export function rejectPlanModeWrite({ params }) {
125
+ const { call, execution } = params;
126
+ return {
127
+ currentCall: call,
128
+ execution,
129
+ rejectedCall: {
130
+ call,
131
+ result: { error: PLAN_MODE_REFUSAL, guidance: true },
132
+ activity: {
133
+ type: 'tool-activity',
134
+ name: call.name,
135
+ args: call.args,
136
+ resource: execution.gate.facts.resource,
137
+ effect: 'write',
138
+ decision: 'blocked'
139
+ },
140
+ effect: 'write',
141
+ emitCall: true,
142
+ emitResult: true,
143
+ capped: false,
144
+ source: execution.origin
145
+ }
146
+ };
147
+ }
@@ -0,0 +1,6 @@
1
+ import type { AnsweredPayload, CompletionActionArgs, CompletionContext } from '../types.ts';
2
+ export declare function retryMuteRound(args: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
3
+ export declare function retryNarratedRound({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
4
+ export declare function retryMalformedRound({ context, params }: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
5
+ export declare function retryCappedSearch({ context }: CompletionActionArgs): Partial<CompletionContext>;
6
+ export declare function retryCutRound({ context }: CompletionActionArgs): Partial<CompletionContext>;
@@ -0,0 +1,50 @@
1
+ import { goalMet } from '../rules.js';
2
+ import { applyHistorySplice } from '../splice.js';
3
+ import { applyPreparation, closeFailureMemo, continuation, cutTurns, endToolRound, silentTurn } from './completion.js';
4
+ import { askedGoal, goalTurn } from './goal.js';
5
+ export function retryMuteRound(args) {
6
+ const { context } = args;
7
+ const { goal } = context;
8
+ const goalReAsk = goal && !context.steering.length && !goalMet(context);
9
+ return {
10
+ ...silentTurn(args, goalReAsk ? [{ role: 'user', content: goalTurn(context) }] : continuation(context)),
11
+ goal: goalReAsk && !args.params.cutOff ? askedGoal(context, goal) : goal,
12
+ mute: context.mute + 1,
13
+ round: context.round + 1
14
+ };
15
+ }
16
+ export function retryNarratedRound({ context, params }) {
17
+ return {
18
+ narrated: true,
19
+ history: [
20
+ ...context.history,
21
+ { role: 'assistant', content: params.narrated ?? '' },
22
+ ...continuation(context)
23
+ ],
24
+ steering: [],
25
+ round: context.round + 1
26
+ };
27
+ }
28
+ export function retryMalformedRound({ context, params }) {
29
+ return {
30
+ ...applyHistorySplice(context, params.history),
31
+ malformed: true,
32
+ round: context.round + 1
33
+ };
34
+ }
35
+ export function retryCappedSearch({ context }) {
36
+ return {
37
+ ...applyPreparation(context),
38
+ ...closeFailureMemo(context),
39
+ ...endToolRound(context),
40
+ round: context.round + 1,
41
+ mute: 0,
42
+ cuts: 0,
43
+ narrated: false,
44
+ malformed: false,
45
+ cappedRounds: context.cappedRounds + 1
46
+ };
47
+ }
48
+ export function retryCutRound({ context }) {
49
+ return { ...cutTurns(context), cuts: context.cuts + 1, round: context.round + 1 };
50
+ }
@@ -0,0 +1,5 @@
1
+ import type { CompletionActionArgs, CompletionContext, PreparingPayload, RetrievingOutput } from '../types.ts';
2
+ export declare function startInitialRound({ context, params }: CompletionActionArgs<PreparingPayload>): Partial<CompletionContext>;
3
+ export declare function startNextRound({ context }: CompletionActionArgs): Partial<CompletionContext>;
4
+ export declare function continueWithFollowUps({ context }: CompletionActionArgs): Partial<CompletionContext>;
5
+ export declare function addKnowledge({ context, params }: CompletionActionArgs<RetrievingOutput>): Partial<CompletionContext>;
@@ -0,0 +1,31 @@
1
+ import { applyHistorySplice } from '../splice.js';
2
+ import { applyPreparation, closeFailureMemo, continueAfterAnswer, endToolRound, userTurns } from './completion.js';
3
+ export function startInitialRound({ context, params }) {
4
+ return applyPreparation(context, params);
5
+ }
6
+ export function startNextRound({ context }) {
7
+ return {
8
+ ...applyPreparation(context),
9
+ ...closeFailureMemo(context),
10
+ ...endToolRound(context),
11
+ round: context.round + 1,
12
+ mute: 0,
13
+ cuts: 0,
14
+ narrated: false,
15
+ malformed: false,
16
+ cappedRounds: 0
17
+ };
18
+ }
19
+ export function continueWithFollowUps({ context }) {
20
+ return {
21
+ ...continueAfterAnswer(context, userTurns(context.followUps)),
22
+ followUps: [],
23
+ edgeInputs: context.followUps
24
+ };
25
+ }
26
+ export function addKnowledge({ context, params }) {
27
+ return {
28
+ ...applyHistorySplice(context, params.history),
29
+ sources: params.sources ? [...context.sources, ...params.sources] : context.sources
30
+ };
31
+ }
@@ -1,13 +1,6 @@
1
- import type { BatchDonePayload, CompletionActionArgs, CompletionContext, DirectToolOutput, GatedToolExecutionSnapshot, RejectDeniedApprovalParams, PreparingOutput, RepeatOfFailedCallParams, ResolvedCallSnapshot, ToolDonePayload, ToolExecutionSnapshot, ToolRejection, ToolsResolvedPayload } from '../types.ts';
1
+ import type { BatchDonePayload, CompletionActionArgs, CompletionContext, DirectToolOutput, GatedToolExecutionSnapshot, PreparingOutput, ResolvedCallSnapshot, ToolDonePayload, ToolsResolvedPayload } from '../types.ts';
2
2
  export declare function resolveTools({ context, params }: CompletionActionArgs<ToolsResolvedPayload>): Partial<CompletionContext>;
3
3
  export declare function selectNextTool({ params }: CompletionActionArgs<ResolvedCallSnapshot>): Partial<CompletionContext>;
4
- export interface RejectToolParams {
5
- call: ResolvedCallSnapshot;
6
- execution: ToolExecutionSnapshot | null;
7
- rejection: ToolRejection;
8
- }
9
- export declare function rejectTool({ context, params }: CompletionActionArgs<RejectToolParams>): Partial<CompletionContext>;
10
- export declare function rejectDeniedApproval({ context, params }: CompletionActionArgs<RejectDeniedApprovalParams>): Partial<CompletionContext>;
11
4
  export declare function queueDirectSearch({ context, params }: CompletionActionArgs<PreparingOutput>): Partial<CompletionContext>;
12
5
  export declare function startDirectTool({ params }: CompletionActionArgs<DirectToolOutput>): Partial<CompletionContext>;
13
6
  export declare function startTool({ params }: CompletionActionArgs<GatedToolExecutionSnapshot>): Partial<CompletionContext>;
@@ -15,11 +8,3 @@ export declare function finishTool({ context, params }: CompletionActionArgs<Too
15
8
  export declare function finishBatch({ context, params }: CompletionActionArgs<BatchDonePayload>): Partial<CompletionContext>;
16
9
  export declare function endRunOnTool(args: CompletionActionArgs<ToolDonePayload>): Partial<CompletionContext>;
17
10
  export declare function finishDirectTool({ context, params }: CompletionActionArgs<ToolDonePayload>): Partial<CompletionContext>;
18
- export declare function rejectRepeatedTool({ params }: CompletionActionArgs<RepeatOfFailedCallParams>): Partial<CompletionContext>;
19
- export declare function rejectCutOffTool({ context, params }: CompletionActionArgs<ResolvedCallSnapshot>): Partial<CompletionContext>;
20
- export declare const PLAN_MODE_REFUSAL: string;
21
- export interface RejectPlanModeWriteParams {
22
- call: ResolvedCallSnapshot;
23
- execution: GatedToolExecutionSnapshot;
24
- }
25
- export declare function rejectPlanModeWrite({ params }: CompletionActionArgs<RejectPlanModeWriteParams>): Partial<CompletionContext>;
@@ -1,7 +1,7 @@
1
- import { activate, applyHistorySplice, applySplice, cutOffMessage, recordTool, retainFailureResults, settledSince } from '../rules.js';
2
- import { failureResultRef, storeFailureResult, toolDeniedResult } from '../../../helpers/failure-result.js';
1
+ import { activate, recordTool, retainFailureResults, settledSince } from '../rules.js';
2
+ import { applyHistorySplice, applySplice } from '../splice.js';
3
3
  import { applyPreparation } from './completion.js';
4
- import { deliverableChecked, existenceOnly, MAX_GOAL_CHECKS, MAX_PROVING, mergeChecks, runsCode, runsCodeCheck, runsDeliverable, runsScript, samePath, sourceFile } from '../../../helpers/goal-check.js';
4
+ import { GoalCheck } from '../../../helpers/goal-check.js';
5
5
  export function resolveTools({ context, params }) {
6
6
  return {
7
7
  toolQueue: [...params.calls],
@@ -13,95 +13,6 @@ export function resolveTools({ context, params }) {
13
13
  export function selectNextTool({ params }) {
14
14
  return { currentCall: params, rejectedCall: null };
15
15
  }
16
- export function rejectTool({ context, params }) {
17
- const { call, execution, rejection } = params;
18
- const memoized = memoizedFailure({
19
- context,
20
- call,
21
- result: rejection.result,
22
- outcome: rejection.outcome
23
- });
24
- return {
25
- currentCall: call,
26
- execution,
27
- failureResults: memoized.failureResults ?? context.failureResults,
28
- rejectedCall: rejectionPayload({
29
- call,
30
- ...memoized.value,
31
- activity: rejection.activity,
32
- effect: rejection.effect,
33
- outcome: rejection.outcome,
34
- deniedKey: rejection.deniedKey,
35
- emitCall: rejection.emitCall ?? true,
36
- emitResult: rejection.emitResult ?? true,
37
- capped: rejection.capped ?? false,
38
- source: rejection.source,
39
- repeat: rejection.repeat
40
- })
41
- };
42
- }
43
- export function rejectDeniedApproval({ context, params }) {
44
- const { call, execution } = params;
45
- const facts = execution.gate.facts;
46
- const result = toolDeniedResult(context.policy.deniedMessage);
47
- return {
48
- pendingCall: null,
49
- approval: { status: 'denied', key: context.approval.key },
50
- rejectedCall: {
51
- call,
52
- result,
53
- activity: {
54
- type: 'tool-activity',
55
- name: call.name,
56
- args: call.args,
57
- resource: facts.resource,
58
- effect: facts.effect,
59
- decision: 'denied'
60
- },
61
- effect: facts.effect,
62
- outcome: { kind: 'denied', reason: result.error },
63
- deniedKey: facts.key,
64
- emitCall: false,
65
- emitResult: true,
66
- capped: false,
67
- source: execution.origin
68
- }
69
- };
70
- }
71
- function memoizedFailure({ context, call, result, outcome }) {
72
- if (!outcome || outcome.kind === 'denied') {
73
- return { value: { result } };
74
- }
75
- const ref = failureResultRef(call.key, context.round, call.id);
76
- const value = {
77
- name: call.name,
78
- result: storeFailureResult(result)
79
- };
80
- const prior = context.failureResults[ref];
81
- if (prior) {
82
- throw new Error(`Failure result reference collision: ${ref}`);
83
- }
84
- return {
85
- value: { resultRef: ref },
86
- failureResults: { ...context.failureResults, [ref]: value }
87
- };
88
- }
89
- function rejectionPayload(payload) {
90
- const result = payload.resultRef === undefined ? { result: payload.result } : { resultRef: payload.resultRef };
91
- return {
92
- call: payload.call,
93
- ...result,
94
- activity: payload.activity,
95
- effect: payload.effect,
96
- outcome: payload.outcome,
97
- deniedKey: payload.deniedKey,
98
- emitCall: payload.emitCall,
99
- emitResult: payload.emitResult,
100
- capped: payload.capped,
101
- source: payload.source,
102
- repeat: payload.repeat
103
- };
104
- }
105
16
  export function queueDirectSearch({ context, params }) {
106
17
  return {
107
18
  ...applyPreparation(context, params.preparation),
@@ -236,91 +147,35 @@ function answeredReads(context, payload) {
236
147
  }
237
148
  return payload.durationMs === undefined || !memo.length ? context.answeredReads : [];
238
149
  }
239
- export function rejectRepeatedTool({ params }) {
240
- const { call, prior } = params;
241
- return {
242
- currentCall: call,
243
- rejectedCall: {
244
- call,
245
- resultRef: prior.resultRef,
246
- activity: null,
247
- emitCall: false,
248
- emitResult: false,
249
- capped: false,
250
- source: 'tool-result',
251
- repeat: prior.outcome
252
- }
253
- };
254
- }
255
- export function rejectCutOffTool({ context, params }) {
256
- return {
257
- currentCall: params,
258
- rejectedCall: {
259
- call: params,
260
- result: { error: cutOffMessage(context.policy, context.lastRound), guidance: true },
261
- activity: null,
262
- emitCall: true,
263
- emitResult: true,
264
- capped: false,
265
- source: 'tool-result'
266
- }
267
- };
268
- }
269
- export const PLAN_MODE_REFUSAL = 'Plan mode: this write was not run. Call update_plan with your steps and submit true to ask ' +
270
- 'the user to approve the plan; writes run once they do.';
271
- export function rejectPlanModeWrite({ params }) {
272
- const { call, execution } = params;
273
- return {
274
- currentCall: call,
275
- execution,
276
- rejectedCall: {
277
- call,
278
- result: { error: PLAN_MODE_REFUSAL, guidance: true },
279
- activity: {
280
- type: 'tool-activity',
281
- name: call.name,
282
- args: call.args,
283
- resource: execution.gate.facts.resource,
284
- effect: 'write',
285
- decision: 'blocked'
286
- },
287
- effect: 'write',
288
- emitCall: true,
289
- emitResult: true,
290
- capped: false,
291
- source: execution.origin
292
- }
293
- };
294
- }
295
150
  function addProving(goal, wrote) {
296
151
  if (!goal || !wrote) {
297
152
  return goal;
298
153
  }
299
154
  const written = wrote.map(({ path }) => path);
300
- const checks = goal.checks.map((check) => runsScript(check.name, written) ? { ...check, passed: false, output: null } : check);
155
+ const checks = goal.checks.map((check) => GoalCheck.runsScript(check.name, written) ? { ...check, passed: false, output: null } : check);
301
156
  const edited = editCode(goal, written);
302
157
  const changed = wrote.filter((write) => write.proving !== undefined);
303
158
  if (!changed.length) {
304
159
  return { ...goal, ...edited, checks };
305
160
  }
306
- const kept = (goal.proving ?? []).filter((path) => !changed.some((write) => samePath(write.path, path)));
161
+ const kept = (goal.proving ?? []).filter((path) => !changed.some((write) => GoalCheck.samePath(write.path, path)));
307
162
  const added = changed.filter((write) => write.proving).map(({ path }) => path);
308
- return { ...goal, ...edited, checks, proving: [...kept, ...added].slice(-MAX_PROVING) };
163
+ return { ...goal, ...edited, checks, proving: [...kept, ...added].slice(-GoalCheck.MAX_PROVING) };
309
164
  }
310
165
  function editCode(goal, written) {
311
- const code = written.filter(sourceFile);
166
+ const code = written.filter(GoalCheck.sourceFile);
312
167
  if (!code.length) {
313
168
  return {};
314
169
  }
315
- const edited = code.reduce((list, path) => [...list.filter((known) => !samePath(known, path)), path], goal.edited ?? []);
316
- return { edited: edited.slice(-MAX_PROVING), proof: undefined };
170
+ const edited = code.reduce((list, path) => [...list.filter((known) => !GoalCheck.samePath(known, path)), path], goal.edited ?? []);
171
+ return { edited: edited.slice(-GoalCheck.MAX_PROVING), proof: undefined };
317
172
  }
318
173
  function adoptRun(goal, ran) {
319
174
  if (!goal?.edited?.length || !ran) {
320
175
  return goal;
321
176
  }
322
- const proof = runsCode(ran, goal.edited) ??
323
- runsCodeCheck(ran, goal.checks.map((check) => check.name));
177
+ const proof = GoalCheck.runsCode(ran, goal.edited) ??
178
+ GoalCheck.runsCodeCheck(ran, goal.checks.map((check) => check.name));
324
179
  return proof ? { ...goal, proof } : goal;
325
180
  }
326
181
  function applyChecks(goal, checked) {
@@ -337,14 +192,14 @@ function adoptProof(goal, proved) {
337
192
  return goal;
338
193
  }
339
194
  if (goal.named) {
340
- return goal.checks.length < MAX_GOAL_CHECKS && runsOutput(goal, proved.name)
195
+ return goal.checks.length < GoalCheck.MAX_GOAL_CHECKS && runsOutput(goal, proved.name)
341
196
  ? { ...goal, checks: [...goal.checks, proved] }
342
197
  : goal;
343
198
  }
344
- if (!goal.checkable || !existenceOnly(goal.checks.map((check) => check.name))) {
199
+ if (!goal.checkable || !GoalCheck.existenceOnly(goal.checks.map((check) => check.name))) {
345
200
  return goal;
346
201
  }
347
- const names = mergeChecks(goal.checks.map((check) => check.name), [proved.name]);
202
+ const names = GoalCheck.mergeChecks(goal.checks.map((check) => check.name), [proved.name]);
348
203
  if (!names.length) {
349
204
  return goal;
350
205
  }
@@ -356,6 +211,6 @@ function adoptProof(goal, proved) {
356
211
  }
357
212
  function runsOutput(goal, name) {
358
213
  return (!!goal.deliverable &&
359
- !deliverableChecked(goal) &&
360
- runsDeliverable(name, goal.deliverable, goal.proving ?? []));
214
+ !GoalCheck.deliverableChecked(goal) &&
215
+ GoalCheck.runsDeliverable(name, goal.deliverable, goal.proving ?? []));
361
216
  }
@@ -1,6 +1,6 @@
1
- import { existenceOnly, pathsOf } from '../../helpers/goal-check.js';
1
+ import { GoalCheck } from '../../helpers/goal-check.js';
2
2
  export function emptyCompletionContext(opts) {
3
- const existence = !opts.goal?.checksProve && existenceOnly(opts.goal?.checks ?? []);
3
+ const existence = !opts.goal?.checksProve && GoalCheck.existenceOnly(opts.goal?.checks ?? []);
4
4
  return {
5
5
  runId: opts.runId ?? '',
6
6
  policy: opts.policy,
@@ -71,7 +71,7 @@ export function emptyCompletionContext(opts) {
71
71
  checks: (opts.goal.checks ?? []).map((name) => ({ name, passed: false, output: null })),
72
72
  named: (opts.goal.checksProve && Boolean(opts.goal.checks?.length)) || undefined,
73
73
  review: 'due',
74
- deliverable: existence ? pathsOf(opts.goal.checks ?? []) : undefined
74
+ deliverable: existence ? GoalCheck.pathsOf(opts.goal.checks ?? []) : undefined
75
75
  }
76
76
  : null
77
77
  };
@@ -83,8 +83,8 @@ export declare const completionActions: {
83
83
  wrapDeliveredCompletion: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, undefined, never, never, never, never, never>;
84
84
  wrapExhaustedCompletion: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, undefined, never, never, never, never, never>;
85
85
  startTool: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./types.ts").GatedToolExecutionSnapshot, never, never, never, never, never>;
86
- rejectTool: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./actions/tools.ts").RejectToolParams, never, never, never, never, never>;
87
- rejectPlanModeWrite: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./actions/tools.ts").RejectPlanModeWriteParams, never, never, never, never, never>;
86
+ rejectTool: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./actions/reject-tool.ts").RejectToolParams, never, never, never, never, never>;
87
+ rejectPlanModeWrite: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./actions/reject-tool.ts").RejectPlanModeWriteParams, never, never, never, never, never>;
88
88
  finishDirectTool: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./types.ts").ToolDonePayload, never, never, never, never, never>;
89
89
  finishTool: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./types.ts").ToolDonePayload, never, never, never, never, never>;
90
90
  finishBatch: import("xstate").ActionFunction<CompletionContext, CompletionMachineEvent, CompletionMachineEvent, import("./types.ts").BatchDonePayload, never, never, never, never, never>;