@qvac/harness 1.0.0-rc.11 → 1.0.0-rc.13
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.
- package/CHANGELOG.md +29 -0
- package/README.md +3 -1
- package/dist-lib/index.js +1 -2
- package/dist-lib/lib/agent/follow.js +9 -2
- package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +13 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +11 -6
- package/dist-lib/lib/capabilities/completion-engine/actors/retrieving.d.ts +2 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/retrieving.js +19 -11
- package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +3 -2
- package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +21 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +11 -2
- package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +4 -0
- package/dist-lib/lib/capabilities/completion-engine/driver.js +2 -1
- package/dist-lib/lib/capabilities/completion-engine/engine.js +39 -23
- package/dist-lib/lib/capabilities/completion-engine/event-mailbox.d.ts +20 -0
- package/dist-lib/lib/capabilities/completion-engine/event-mailbox.js +110 -0
- package/dist-lib/lib/capabilities/completion-engine/loop.d.ts +1 -0
- package/dist-lib/lib/capabilities/completion-engine/model-round.d.ts +4 -1
- package/dist-lib/lib/capabilities/completion-engine/model-round.js +2 -0
- package/dist-lib/lib/capabilities/completion-engine/round.d.ts +11 -0
- package/dist-lib/lib/capabilities/completion-engine/round.js +41 -22
- package/dist-lib/lib/capabilities/completion-engine/turns.d.ts +3 -0
- package/dist-lib/lib/capabilities/completion-engine/turns.js +16 -0
- package/dist-lib/lib/capabilities/tool-failed-note.d.ts +1 -0
- package/dist-lib/lib/capabilities/tool-failed-note.js +1 -0
- package/dist-lib/lib/capabilities/tool-turns.d.ts +3 -1
- package/dist-lib/lib/capabilities/tool-turns.js +8 -1
- package/dist-lib/lib/connect.js +4 -2
- package/dist-lib/lib/contract/completion.d.ts +2 -1
- package/dist-lib/lib/contract/knowledge.d.ts +10 -0
- package/dist-lib/lib/fold/index.d.ts +3 -2
- package/dist-lib/lib/fold/index.js +7 -6
- package/dist-lib/lib/helpers/goal-check.d.ts +12 -0
- package/dist-lib/lib/helpers/goal-check.js +89 -7
- package/dist-lib/lib/helpers/html-helpers.js +9 -6
- package/dist-lib/lib/helpers/input-budget.d.ts +8 -0
- package/dist-lib/lib/helpers/input-budget.js +9 -0
- package/dist-lib/lib/machine/completion/actions/completion.d.ts +1 -1
- package/dist-lib/lib/machine/completion/actions/completion.js +34 -14
- package/dist-lib/lib/machine/completion/actions/tools.js +19 -1
- package/dist-lib/lib/machine/completion/rules.d.ts +1 -0
- package/dist-lib/lib/machine/completion/rules.js +14 -5
- package/dist-lib/lib/machine/completion/types.d.ts +3 -0
- package/dist-lib/lib/plugins/registry.d.ts +1 -0
- package/dist-lib/lib/plugins/registry.js +49 -10
- package/dist-lib/lib/residency/index.d.ts +1 -1
- package/dist-lib/lib/residency/index.js +4 -2
- package/dist-lib/lib/serve.js +56 -15
- package/dist-lib/lib/tools/files/glob.js +2 -2
- package/dist-lib/lib/tools/files/read-file.js +134 -15
- package/dist-lib/lib/tools/fs/walk.d.ts +1 -0
- package/dist-lib/lib/tools/fs/walk.js +2 -1
- package/dist-lib/lib/tools/http-request/truncate-response.js +11 -2
- package/dist-lib/lib/tools/http-request.js +28 -11
- package/dist-lib/lib/tools/office.d.ts +1 -1
- package/dist-lib/lib/tools/office.js +34 -12
- package/dist-lib/lib/wire.js +23 -3
- package/dist-lib/plugins/knowledge/index.js +1 -0
- package/dist-lib/plugins/knowledge/manifest.d.ts +29 -0
- package/dist-lib/plugins/knowledge/manifest.js +41 -0
- package/dist-lib/plugins/knowledge/search-tool.js +8 -8
- package/dist-lib/plugins/knowledge/store.d.ts +35 -7
- package/dist-lib/plugins/knowledge/store.js +335 -143
- package/dist-lib/plugins/mcp/call.js +10 -3
- package/dist-lib/plugins/mcp/protocol.d.ts +4 -1
- package/dist-lib/plugins/mcp/protocol.js +11 -10
- package/dist-lib/plugins/mcp/tool-listing.d.ts +6 -0
- package/dist-lib/plugins/mcp/tool-listing.js +97 -0
- package/dist-lib/plugins/music/generate-music.js +2 -2
- package/dist-lib/plugins/python/language.js +2 -2
- package/dist-lib/plugins/run-report/story.js +4 -1
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
- package/dist-lib/schema/harness.js +4 -2
- package/dist-lib/spec/hrpc/messages.js +19 -4
- package/dist-lib/spec/hyperschema/index.js +19 -4
- package/dist-lib/spec/hyperschema/schema.json +12 -1
- package/dist-lib/spec/hyperschema/types.d.ts +2 -0
- package/package.json +9 -1
|
@@ -2,6 +2,19 @@ const SHELL_OPERATOR = /(&&|\|\||;|\||\n)/;
|
|
|
2
2
|
const RUNNER = /^(python[\d.]*|bash|sh|node|bun|pytest)$/;
|
|
3
3
|
const CHECKS_HEADING = /^[\s*#_]*checks:?[\s*_]*$/i;
|
|
4
4
|
const CHECK_LINE = /^\s*(?:[-*]|\d+[.)])?\s*`([^`]+)`\s*$/;
|
|
5
|
+
const FILE_TEST = /^-[sfed]$/;
|
|
6
|
+
const NOT_A_PATH = /^(-\w+|!|!?=+|<|>|\]\]?)$/;
|
|
7
|
+
const COMPARISON = /^(!?=+|-(eq|ne|lt|le|gt|ge))$/;
|
|
8
|
+
const TEST_SCRIPT = /^(tests?|checks?|verify|validate)(?=[_.-]|$)/;
|
|
9
|
+
const PACKAGE_TEST = /^tests?(?=[:_.-]|$)/;
|
|
10
|
+
const PACKAGE_MANAGER = /^(npm|yarn|pnpm|bun)$/;
|
|
11
|
+
const PYTHON = /^python[\d.]*$/;
|
|
12
|
+
const TEST_PROGRAM = /^(pytest|ctest|diff|cmp)$/;
|
|
13
|
+
const QUIET = /^(-[a-zA-Z]*q[a-zA-Z]*|--quiet)$/;
|
|
14
|
+
const BRACKETS = new Map([
|
|
15
|
+
['[', ']'],
|
|
16
|
+
['[[', ']]']
|
|
17
|
+
]);
|
|
5
18
|
export const MAX_GOAL_CHECKS = 8;
|
|
6
19
|
export const GOAL_CHECKS_ASK = '[This goal has no checks yet. Name the commands that will prove the task is done: write a ' +
|
|
7
20
|
'line `Checks:`, then one command per line in backticks, such as `test -s /app/result.json` ' +
|
|
@@ -12,6 +25,69 @@ export const GOAL_PROOF_ASK = '[The checks this goal has so far may only show th
|
|
|
12
25
|
"and comparing with what the task asks, or the task's own tests: write a line `Checks:`, " +
|
|
13
26
|
'then one command per line in backticks, with no pipe, ; or && in it. A command that repeats ' +
|
|
14
27
|
'one of those checks does not count. The goal is met only once each check ran on its own and exited 0.]';
|
|
28
|
+
export const MAX_PROOF_ASKS = 2;
|
|
29
|
+
export function asksChecks(goal) {
|
|
30
|
+
return (!!goal.checkable &&
|
|
31
|
+
!goal.named &&
|
|
32
|
+
(!goal.checks.length ||
|
|
33
|
+
existenceOnly(goal.checks.map((check) => check.name)) ||
|
|
34
|
+
goal.turns < MAX_PROOF_ASKS));
|
|
35
|
+
}
|
|
36
|
+
export function existenceOnly(checks) {
|
|
37
|
+
return (checks.length > 0 &&
|
|
38
|
+
checks.every((check) => check
|
|
39
|
+
.split(SHELL_OPERATOR)
|
|
40
|
+
.filter((part, at) => at % 2 === 0 && part.trim())
|
|
41
|
+
.every((part) => testsExistence(words(part)))));
|
|
42
|
+
}
|
|
43
|
+
function testsExistence(tokens) {
|
|
44
|
+
const close = BRACKETS.get(tokens[0]);
|
|
45
|
+
if ((tokens[0] !== 'test' && !close) || (close && tokens.at(-1) !== close)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const path = tokens.slice(2, close ? -1 : undefined);
|
|
49
|
+
return (FILE_TEST.test(tokens[1] ?? '') &&
|
|
50
|
+
path.length > 0 &&
|
|
51
|
+
!path.some((token) => NOT_A_PATH.test(token)));
|
|
52
|
+
}
|
|
53
|
+
export function provesResult(command) {
|
|
54
|
+
return exitSegments(command).find(proves);
|
|
55
|
+
}
|
|
56
|
+
function proves(segment) {
|
|
57
|
+
if (segment.split('"').length % 2 === 0 || segment.split("'").length % 2 === 0) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const all = words(segment);
|
|
61
|
+
const tokens = all.slice(Math.max(commandStart(all), 0));
|
|
62
|
+
const program = tokens[0]?.split('/').at(-1) ?? '';
|
|
63
|
+
const [, next = '', third = ''] = tokens;
|
|
64
|
+
const python = PYTHON.test(program);
|
|
65
|
+
if (program === 'test' || program === '[' || program === '[[') {
|
|
66
|
+
return tokens.some((token) => COMPARISON.test(token));
|
|
67
|
+
}
|
|
68
|
+
if ((python && tokens.includes('-c')) || (program === 'node' && tokens.includes('-e'))) {
|
|
69
|
+
return /\bassert\b/.test(segment);
|
|
70
|
+
}
|
|
71
|
+
const module = tokens.indexOf('-m');
|
|
72
|
+
return (TEST_PROGRAM.test(program) ||
|
|
73
|
+
(program === 'grep' && tokens.some((token) => QUIET.test(token))) ||
|
|
74
|
+
(PACKAGE_MANAGER.test(program) && PACKAGE_TEST.test(next === 'run' ? third : next)) ||
|
|
75
|
+
((program === 'cargo' || program === 'go') && next === 'test') ||
|
|
76
|
+
(program === 'make' && (next === 'test' || next === 'check')) ||
|
|
77
|
+
(program === 'node' && tokens.includes('--test')) ||
|
|
78
|
+
(python && module > 0 && /^(pytest|unittest)$/.test(tokens[module + 1] ?? '')) ||
|
|
79
|
+
TEST_SCRIPT.test(scriptOf(tokens) ?? ''));
|
|
80
|
+
}
|
|
81
|
+
function scriptOf(tokens) {
|
|
82
|
+
let at = 0;
|
|
83
|
+
if (RUNNER.test(tokens[0]?.split('/').at(-1) ?? '')) {
|
|
84
|
+
at++;
|
|
85
|
+
while (tokens[at]?.startsWith('-')) {
|
|
86
|
+
at++;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return tokens[at]?.split('/').at(-1);
|
|
90
|
+
}
|
|
15
91
|
export function namedChecks(text) {
|
|
16
92
|
const lines = text.split('\n');
|
|
17
93
|
const heading = lines.findIndex((line) => CHECKS_HEADING.test(line));
|
|
@@ -47,22 +123,25 @@ function normalize(command) {
|
|
|
47
123
|
return command.trim().replace(/\s+/g, ' ');
|
|
48
124
|
}
|
|
49
125
|
export function runsCheck(command, check) {
|
|
50
|
-
const
|
|
126
|
+
const named = words(check);
|
|
127
|
+
const wanted = named.slice(Math.max(commandStart(named), 0));
|
|
128
|
+
return exitSegments(command).some((segment) => invokes(words(segment), wanted));
|
|
129
|
+
}
|
|
130
|
+
function exitSegments(command) {
|
|
51
131
|
const parts = command.split(SHELL_OPERATOR);
|
|
52
132
|
for (let at = parts.length - 1; at >= 0; at -= 2) {
|
|
53
133
|
if (!parts[at].trim()) {
|
|
54
134
|
parts.splice(at === parts.length - 1 ? Math.max(at - 1, 0) : at, 2);
|
|
55
135
|
}
|
|
56
136
|
}
|
|
137
|
+
const segments = [];
|
|
57
138
|
for (let at = 0; at < parts.length; at += 2) {
|
|
58
139
|
const after = parts.slice(at + 1).filter((_, n) => n % 2 === 0);
|
|
59
|
-
if (parts[at - 1] !== '||' &&
|
|
60
|
-
|
|
61
|
-
invokes(words(parts[at]), wanted)) {
|
|
62
|
-
return true;
|
|
140
|
+
if (parts[at - 1] !== '||' && after.every((operator) => operator === '&&')) {
|
|
141
|
+
segments.push(parts[at].trim());
|
|
63
142
|
}
|
|
64
143
|
}
|
|
65
|
-
return
|
|
144
|
+
return segments;
|
|
66
145
|
}
|
|
67
146
|
function words(text) {
|
|
68
147
|
return text
|
|
@@ -72,7 +151,7 @@ function words(text) {
|
|
|
72
151
|
.map((word) => word.replace(/^['"]|['"]$/g, ''));
|
|
73
152
|
}
|
|
74
153
|
function invokes(tokens, wanted) {
|
|
75
|
-
let start = tokens
|
|
154
|
+
let start = commandStart(tokens);
|
|
76
155
|
if (start < 0) {
|
|
77
156
|
return false;
|
|
78
157
|
}
|
|
@@ -88,6 +167,9 @@ function invokes(tokens, wanted) {
|
|
|
88
167
|
}
|
|
89
168
|
return leads(tokens.slice(start), wanted);
|
|
90
169
|
}
|
|
170
|
+
function commandStart(tokens) {
|
|
171
|
+
return tokens.findIndex((token) => !/^\w+=/.test(token));
|
|
172
|
+
}
|
|
91
173
|
function leads(tokens, wanted) {
|
|
92
174
|
return (wanted.length > 0 &&
|
|
93
175
|
wanted.every((word, at) => tokens[at] !== undefined && samePath(tokens[at], word)));
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export function stripHtmlTags(text) {
|
|
2
|
-
|
|
3
|
-
let
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
const lastClose = text.lastIndexOf('>');
|
|
3
|
+
let out = '';
|
|
4
|
+
let at = 0;
|
|
5
|
+
let open = text.indexOf('<');
|
|
6
|
+
while (open !== -1 && open < lastClose) {
|
|
7
|
+
out += text.slice(at, open);
|
|
8
|
+
at = text.indexOf('>', open) + 1;
|
|
9
|
+
open = text.indexOf('<', at);
|
|
7
10
|
}
|
|
8
|
-
return out;
|
|
11
|
+
return out + text.slice(at);
|
|
9
12
|
}
|
|
10
13
|
const NAMED_ENTITIES = {
|
|
11
14
|
'<': '<',
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const MAX_PENDING_INPUTS = 64;
|
|
2
|
+
export declare const MAX_PENDING_INPUT_BYTES: number;
|
|
3
|
+
export declare function inputBytes<T extends {
|
|
4
|
+
text: string;
|
|
5
|
+
}>(input: T): number;
|
|
6
|
+
export declare function acceptsInput<T extends {
|
|
7
|
+
text: string;
|
|
8
|
+
}>(count: number, bytes: number, input: T): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
export const MAX_PENDING_INPUTS = 64;
|
|
3
|
+
export const MAX_PENDING_INPUT_BYTES = 256 * 1024;
|
|
4
|
+
export function inputBytes(input) {
|
|
5
|
+
return b4a.byteLength(JSON.stringify(input), 'utf8');
|
|
6
|
+
}
|
|
7
|
+
export function acceptsInput(count, bytes, input) {
|
|
8
|
+
return count < MAX_PENDING_INPUTS && bytes + inputBytes(input) <= MAX_PENDING_INPUT_BYTES;
|
|
9
|
+
}
|
|
@@ -19,7 +19,7 @@ export declare function continueStop({ context, params }: CompletionActionArgs<A
|
|
|
19
19
|
export declare function settleGoal({ context }: CompletionActionArgs): Partial<CompletionContext>;
|
|
20
20
|
export declare function finishRound({ context, params }: CompletionActionArgs<RoundDonePayload>): Partial<CompletionContext>;
|
|
21
21
|
export declare function finishWrapRound({ context, params }: CompletionActionArgs<RoundDonePayload>): Partial<CompletionContext>;
|
|
22
|
-
export declare function wrapCompletion({ params }: CompletionActionArgs<RunOutcome>): Partial<CompletionContext>;
|
|
22
|
+
export declare function wrapCompletion({ context, params }: CompletionActionArgs<RunOutcome>): Partial<CompletionContext>;
|
|
23
23
|
export declare function wrapMuteCompletion(args: CompletionActionArgs<AnsweredPayload>): Partial<CompletionContext>;
|
|
24
24
|
export declare function wrapSearchCappedCompletion({ context }: CompletionActionArgs): Partial<CompletionContext>;
|
|
25
25
|
export declare function wrapDeliveredCompletion({ context }: CompletionActionArgs): Partial<CompletionContext>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { hasRoundsLeft, stuckResult } from '../guards/retry.js';
|
|
2
|
-
import { mergeChecks, namedChecks } from '../../../helpers/goal-check.js';
|
|
2
|
+
import { asksChecks, mergeChecks, namedChecks } from '../../../helpers/goal-check.js';
|
|
3
3
|
import { activate, applyHistorySplice, applySplice, foldMetrics, goalMet, goalTokensLeft, nudgeWithChecks, nudgeWithPlan, retainFailureResults, settledSince } from '../rules.js';
|
|
4
4
|
export function startInitialRound({ context, params }) {
|
|
5
5
|
return applyPreparation(context, params);
|
|
@@ -102,21 +102,27 @@ function closeFailureMemo(context) {
|
|
|
102
102
|
roundProgressed: false
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
|
-
function endToolRound(context) {
|
|
105
|
+
function endToolRound(context, { last = false } = {}) {
|
|
106
106
|
return {
|
|
107
|
-
history: [...context.history, ...continuation(context)],
|
|
107
|
+
history: [...context.history, ...continuation(context, { last })],
|
|
108
108
|
steering: [],
|
|
109
109
|
delivered: context.delivered || context.deliveryEndsRun,
|
|
110
110
|
deliveredThisRound: false,
|
|
111
111
|
deliveryEndsRun: false
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
|
-
function continuation(context) {
|
|
115
|
-
const {
|
|
116
|
-
const nudge = deliveredThisRound ? policy.deliveredNudge : policy.continuationNudge;
|
|
114
|
+
function continuation(context, { last = false } = {}) {
|
|
115
|
+
const { plan } = context;
|
|
117
116
|
return context.steering.length
|
|
118
117
|
? userTurns(context.steering)
|
|
119
|
-
: [{ role: 'user', content: nudgeWithPlan(
|
|
118
|
+
: [{ role: 'user', content: nudgeWithPlan(roundNudge(context, last), plan) }];
|
|
119
|
+
}
|
|
120
|
+
function roundNudge(context, last) {
|
|
121
|
+
const { policy, deliveredThisRound } = context;
|
|
122
|
+
if (deliveredThisRound) {
|
|
123
|
+
return policy.deliveredNudge;
|
|
124
|
+
}
|
|
125
|
+
return last ? (policy.wrapUpNudge ?? policy.continuationNudge) : policy.continuationNudge;
|
|
120
126
|
}
|
|
121
127
|
function userTurns(queued) {
|
|
122
128
|
return queued.map(({ text, content }) => ({ role: 'user', content: content ?? text }));
|
|
@@ -153,7 +159,7 @@ export function addKnowledge({ context, params }) {
|
|
|
153
159
|
}
|
|
154
160
|
export function goalTurn({ policy, goal, plan }, stall) {
|
|
155
161
|
const nudge = stall ? `${policy.goalNudge}\n${stall}` : policy.goalNudge;
|
|
156
|
-
return nudgeWithChecks(nudgeWithPlan(`${nudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], goal
|
|
162
|
+
return nudgeWithChecks(nudgeWithPlan(`${nudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], !!goal && asksChecks(goal));
|
|
157
163
|
}
|
|
158
164
|
export function continueStalledGoal({ context }) {
|
|
159
165
|
const { goal, policy } = context;
|
|
@@ -268,20 +274,34 @@ function settledAfterSend(context, params) {
|
|
|
268
274
|
}
|
|
269
275
|
: {};
|
|
270
276
|
}
|
|
271
|
-
export function wrapCompletion({ params }) {
|
|
272
|
-
return { outcome: params };
|
|
277
|
+
export function wrapCompletion({ context, params }) {
|
|
278
|
+
return { ...swapStandingNudge(context), outcome: params };
|
|
279
|
+
}
|
|
280
|
+
function swapStandingNudge(context) {
|
|
281
|
+
const last = context.history.at(-1);
|
|
282
|
+
const [standing] = continuation(context);
|
|
283
|
+
if (!last || !standing || !('role' in last) || !('role' in standing)) {
|
|
284
|
+
return {};
|
|
285
|
+
}
|
|
286
|
+
if (last.role !== 'user' || last.content !== standing.content) {
|
|
287
|
+
return {};
|
|
288
|
+
}
|
|
289
|
+
return { history: [...context.history.slice(0, -1), ...continuation(context, { last: true })] };
|
|
273
290
|
}
|
|
274
291
|
export function wrapMuteCompletion(args) {
|
|
275
|
-
|
|
292
|
+
const turns = args.params.cutOff
|
|
293
|
+
? swapStandingNudge(args.context)
|
|
294
|
+
: silentTurn(args, continuation(args.context, { last: true }));
|
|
295
|
+
return { ...turns, outcome: 'mute' };
|
|
276
296
|
}
|
|
277
297
|
export function wrapSearchCappedCompletion({ context }) {
|
|
278
|
-
return { ...endToolRound(context), outcome: 'search-capped' };
|
|
298
|
+
return { ...endToolRound(context, { last: true }), outcome: 'search-capped' };
|
|
279
299
|
}
|
|
280
300
|
export function wrapDeliveredCompletion({ context }) {
|
|
281
|
-
return { ...endToolRound(context), outcome: 'delivered' };
|
|
301
|
+
return { ...endToolRound(context, { last: true }), outcome: 'delivered' };
|
|
282
302
|
}
|
|
283
303
|
export function wrapExhaustedCompletion({ context }) {
|
|
284
|
-
return { ...endToolRound(context), outcome: 'exhausted' };
|
|
304
|
+
return { ...endToolRound(context, { last: true }), outcome: 'exhausted' };
|
|
285
305
|
}
|
|
286
306
|
export function answerCompletion({ context, params }) {
|
|
287
307
|
const historyState = applyHistorySplice(context, params.history);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { activate, applyHistorySplice, applySplice, cutOffMessage, recordTool, retainFailureResults, settledSince } from '../rules.js';
|
|
2
2
|
import { failureResultRef, storeFailureResult, toolDeniedResult } from '../../../helpers/failure-result.js';
|
|
3
3
|
import { applyPreparation } from './completion.js';
|
|
4
|
+
import { asksChecks, existenceOnly, mergeChecks } from '../../../helpers/goal-check.js';
|
|
4
5
|
export function resolveTools({ context, params }) {
|
|
5
6
|
return {
|
|
6
7
|
toolQueue: [...params.calls],
|
|
@@ -214,7 +215,7 @@ function applyToolResult(context, payload, call) {
|
|
|
214
215
|
metrics: recordTool(context, call, payload.durationMs),
|
|
215
216
|
activated: activate(context.activated, payload.activate ? [payload.activate] : []),
|
|
216
217
|
plan: payload.plan ?? context.plan,
|
|
217
|
-
goal: applyChecks(context.goal, payload.checked),
|
|
218
|
+
goal: applyChecks(adoptProof(context.goal, payload.proved), payload.checked),
|
|
218
219
|
planMode: context.planMode && payload.leftPlanMode !== true,
|
|
219
220
|
pendingCall: null,
|
|
220
221
|
approval: { status: 'idle', key: null },
|
|
@@ -297,3 +298,20 @@ function applyChecks(goal, checked) {
|
|
|
297
298
|
checks: goal.checks.map((check) => checked.find(({ name }) => name === check.name) ?? check)
|
|
298
299
|
};
|
|
299
300
|
}
|
|
301
|
+
function adoptProof(goal, proved) {
|
|
302
|
+
if (!goal ||
|
|
303
|
+
!proved ||
|
|
304
|
+
!asksChecks(goal) ||
|
|
305
|
+
!existenceOnly(goal.checks.map((check) => check.name))) {
|
|
306
|
+
return goal;
|
|
307
|
+
}
|
|
308
|
+
const names = mergeChecks(goal.checks.map((check) => check.name), [proved.name]);
|
|
309
|
+
if (!names.length) {
|
|
310
|
+
return goal;
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
...goal,
|
|
314
|
+
checks: names.map((name) => goal.checks.find((check) => check.name === name) ?? proved),
|
|
315
|
+
named: true
|
|
316
|
+
};
|
|
317
|
+
}
|
|
@@ -42,6 +42,7 @@ export interface FinishedRound {
|
|
|
42
42
|
ctxSize?: number | undefined;
|
|
43
43
|
overflows?: number | undefined;
|
|
44
44
|
reasoningDeltas?: number | undefined;
|
|
45
|
+
keyed?: boolean | undefined;
|
|
45
46
|
}
|
|
46
47
|
export declare function foldMetrics(state: MetricsState, round: number, finished: FinishedRound): void;
|
|
47
48
|
type StoppedRound = Pick<FinishedRound, 'stats' | 'stopReason'> & {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isFailureResultReference } from '../../helpers/failure-result.js';
|
|
2
|
-
import { GOAL_CHECKS_ASK, GOAL_PROOF_ASK } from '../../helpers/goal-check.js';
|
|
2
|
+
import { asksChecks, GOAL_CHECKS_ASK, GOAL_PROOF_ASK } from '../../helpers/goal-check.js';
|
|
3
3
|
export function runsUnasked(facts) {
|
|
4
4
|
if (facts.permission === 'ask')
|
|
5
5
|
return false;
|
|
@@ -35,7 +35,7 @@ function planDone(plan) {
|
|
|
35
35
|
}
|
|
36
36
|
export function goalMet({ plan, goal }) {
|
|
37
37
|
const checks = goal?.checks ?? [];
|
|
38
|
-
return
|
|
38
|
+
return planDone(plan) && !(goal && asksChecks(goal)) && checks.every((check) => check.passed);
|
|
39
39
|
}
|
|
40
40
|
export function nudgeWithChecks(nudge, checks, unnamed) {
|
|
41
41
|
if (!checks.length) {
|
|
@@ -96,6 +96,9 @@ export function foldMetrics(state, round, finished) {
|
|
|
96
96
|
state.ctxSize = finished.ctxSize;
|
|
97
97
|
}
|
|
98
98
|
if (!stats) {
|
|
99
|
+
if (finished.keyed === false) {
|
|
100
|
+
delete state.cacheTokens;
|
|
101
|
+
}
|
|
99
102
|
return;
|
|
100
103
|
}
|
|
101
104
|
if (state.ttft === undefined && stats.timeToFirstToken) {
|
|
@@ -108,12 +111,15 @@ export function foldMetrics(state, round, finished) {
|
|
|
108
111
|
state.timedTokens += stats.generatedTokens;
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
|
-
if (stats.promptTokens) {
|
|
114
|
+
if (stats.promptTokens !== undefined) {
|
|
112
115
|
state.promptTokens = stats.promptTokens;
|
|
113
116
|
}
|
|
114
|
-
if (stats.cacheTokens) {
|
|
117
|
+
if (stats.cacheTokens !== undefined) {
|
|
115
118
|
state.cacheTokens = stats.cacheTokens;
|
|
116
119
|
}
|
|
120
|
+
if (finished.keyed === false && stats.cacheTokens === undefined) {
|
|
121
|
+
delete state.cacheTokens;
|
|
122
|
+
}
|
|
117
123
|
}
|
|
118
124
|
const ROUND_STATS = [
|
|
119
125
|
'promptTokens',
|
|
@@ -123,7 +129,7 @@ const ROUND_STATS = [
|
|
|
123
129
|
'tokensPerSecond',
|
|
124
130
|
'timeToFirstToken'
|
|
125
131
|
];
|
|
126
|
-
function roundSpeed(round, { stats, stopReason, durationMs, overflows, reasoningDeltas }) {
|
|
132
|
+
function roundSpeed(round, { stats, stopReason, durationMs, overflows, reasoningDeltas, keyed }) {
|
|
127
133
|
const speed = { round, durationMs: durationMs ?? 0 };
|
|
128
134
|
for (const key of ROUND_STATS) {
|
|
129
135
|
const value = stats?.[key];
|
|
@@ -131,6 +137,9 @@ function roundSpeed(round, { stats, stopReason, durationMs, overflows, reasoning
|
|
|
131
137
|
speed[key] = value;
|
|
132
138
|
}
|
|
133
139
|
}
|
|
140
|
+
if (keyed === false) {
|
|
141
|
+
speed.cacheTokens = 0;
|
|
142
|
+
}
|
|
134
143
|
if (stopReason) {
|
|
135
144
|
speed.stopReason = stopReason;
|
|
136
145
|
}
|
|
@@ -81,6 +81,7 @@ export interface RoundDonePayload {
|
|
|
81
81
|
response?: string;
|
|
82
82
|
history?: HistorySplice;
|
|
83
83
|
cacheable?: boolean;
|
|
84
|
+
keyed?: boolean;
|
|
84
85
|
fallbackResponse?: string;
|
|
85
86
|
cut?: string;
|
|
86
87
|
}
|
|
@@ -156,6 +157,7 @@ export interface ToolDonePayload {
|
|
|
156
157
|
plan?: PlanStep[];
|
|
157
158
|
leftPlanMode?: boolean;
|
|
158
159
|
checked?: GoalCheck[];
|
|
160
|
+
proved?: GoalCheck;
|
|
159
161
|
}
|
|
160
162
|
export interface BatchDonePayload {
|
|
161
163
|
history?: HistorySplice;
|
|
@@ -249,6 +251,7 @@ export interface RunPolicy {
|
|
|
249
251
|
maxToolRounds: number;
|
|
250
252
|
continuationNudge: string;
|
|
251
253
|
deliveredNudge: string;
|
|
254
|
+
wrapUpNudge?: string;
|
|
252
255
|
deniedMessage: string;
|
|
253
256
|
cutOffMessage: string;
|
|
254
257
|
contextBoundaryMessage: string;
|
|
@@ -344,8 +344,16 @@ export class PluginRegistry {
|
|
|
344
344
|
}
|
|
345
345
|
};
|
|
346
346
|
}
|
|
347
|
-
unload() {
|
|
348
|
-
|
|
347
|
+
async unload() {
|
|
348
|
+
const setups = [...this._held];
|
|
349
|
+
const outcomes = await Promise.allSettled(setups.map((setup) => setup.unload?.()));
|
|
350
|
+
outcomes.forEach((outcome, index) => {
|
|
351
|
+
if (outcome.status === 'fulfilled' &&
|
|
352
|
+
!this._loaded.some((loaded) => loaded.setup === setups[index])) {
|
|
353
|
+
this._forget(setups[index]);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
return outcomes;
|
|
349
357
|
}
|
|
350
358
|
unloadIdle() {
|
|
351
359
|
return Promise.allSettled(this._held.map(async (setup) => setup.unloadIdle?.()));
|
|
@@ -445,27 +453,58 @@ export class PluginRegistry {
|
|
|
445
453
|
await this._runtime.add(this._plugin(plugin, ++this._revision, place));
|
|
446
454
|
}
|
|
447
455
|
catch (error) {
|
|
448
|
-
|
|
449
|
-
|
|
456
|
+
const acquired = this._held.slice(setups);
|
|
457
|
+
const outcomes = await Promise.allSettled(acquired.map((setup) => setup.unload?.()));
|
|
458
|
+
const failures = [error];
|
|
459
|
+
outcomes.forEach((outcome, index) => {
|
|
460
|
+
if (outcome.status === 'fulfilled') {
|
|
461
|
+
this._forget(acquired[index]);
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
failures.push(outcome.reason);
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
throw failures.length === 1
|
|
468
|
+
? error
|
|
469
|
+
: new AggregateError(failures, 'Plugin setup and model cleanup failed', { cause: error });
|
|
450
470
|
}
|
|
451
471
|
}
|
|
452
472
|
async _drop(name) {
|
|
453
473
|
const index = this._loaded.findIndex((loaded) => loaded.plugin.name === name);
|
|
454
474
|
const [{ plugin, setup }] = this._loaded.splice(index, 1);
|
|
455
|
-
this._held.splice(this._held.indexOf(setup), 1);
|
|
456
475
|
const errors = this._cleanupErrors.length;
|
|
476
|
+
const failures = [];
|
|
457
477
|
try {
|
|
458
478
|
await this._runtime.remove(name);
|
|
459
479
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
480
|
+
catch (error) {
|
|
481
|
+
failures.push(error);
|
|
482
|
+
}
|
|
483
|
+
this._cleanupErrors.length = errors;
|
|
484
|
+
for (const service of plugin.provides ?? []) {
|
|
485
|
+
this._services.delete(service);
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
465
488
|
await setup.unload?.();
|
|
489
|
+
this._forget(setup);
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
failures.push(error);
|
|
493
|
+
}
|
|
494
|
+
if (failures.length === 1) {
|
|
495
|
+
throw failures[0];
|
|
496
|
+
}
|
|
497
|
+
if (failures.length > 1) {
|
|
498
|
+
throw new AggregateError(failures, `Plugin '${name}' removal and model cleanup failed`);
|
|
466
499
|
}
|
|
467
500
|
return index;
|
|
468
501
|
}
|
|
502
|
+
_forget(setup) {
|
|
503
|
+
const index = this._held.indexOf(setup);
|
|
504
|
+
if (index !== -1) {
|
|
505
|
+
this._held.splice(index, 1);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
469
508
|
_check(plugin, replacing) {
|
|
470
509
|
const others = this._loaded
|
|
471
510
|
.map((loaded) => loaded.plugin)
|
|
@@ -187,7 +187,7 @@ export declare class Residency {
|
|
|
187
187
|
loadOptions(model: string, agent?: LoadOptionKnobs): ModelLoadOptions;
|
|
188
188
|
acquireLease({ pool, key, open, signal, prepare, onLoad }: ModelLeaseOptions): Promise<ModelLease | null>;
|
|
189
189
|
completionRuntime(request: CompletionRuntimeRequest): Promise<import("./completion-runtime.ts").PreparedRuntime>;
|
|
190
|
-
servedRound(model: string): {
|
|
190
|
+
servedRound(model: string, load?: LoadOptionKnobs): {
|
|
191
191
|
key: string;
|
|
192
192
|
acquireRound: (invocationSignal?: BareAbortSignal) => Promise<import("../contract/completion.ts").RoundModelLease>;
|
|
193
193
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import ScopeLock from 'scope-lock';
|
|
2
2
|
import { HarnessError } from '../errors.js';
|
|
3
3
|
import { errorMessage, isRecord } from '../guards.js';
|
|
4
|
+
import { androidCpuPlacement } from '../hardware/cpu-placement.js';
|
|
4
5
|
import { AbortedError, raceAbort } from '../helpers/race-abort.js';
|
|
5
6
|
import { HarnessLog } from '../log.js';
|
|
6
7
|
import { ContextChoice as ContextClimb } from '../models/context-choice.js';
|
|
@@ -259,6 +260,7 @@ export class Residency {
|
|
|
259
260
|
slots: this._slots,
|
|
260
261
|
mainGpu: resolveMainGpu,
|
|
261
262
|
openclCacheDir,
|
|
263
|
+
cpuPlacement: androidCpuPlacement,
|
|
262
264
|
contextFor: (key) => this._contexts.chosen(key) ?? this.fit.chosen(key),
|
|
263
265
|
slotsFor: (key) => this.fit.chosenSlots(key)
|
|
264
266
|
};
|
|
@@ -839,8 +841,8 @@ export class Residency {
|
|
|
839
841
|
releaseWork();
|
|
840
842
|
}
|
|
841
843
|
}
|
|
842
|
-
servedRound(model) {
|
|
843
|
-
const loadOpts = this.loadOptions(model);
|
|
844
|
+
servedRound(model, load) {
|
|
845
|
+
const loadOpts = this.loadOptions(model, load);
|
|
844
846
|
const key = modelKey(model, loadOpts);
|
|
845
847
|
return { key, acquireRound: roundAcquirer(this, { model, key, loadOpts }) };
|
|
846
848
|
}
|