@qvac/harness 1.0.0-rc.5 → 1.0.0-rc.6
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 +93 -18
- package/dist-lib/index.d.ts +3 -1
- package/dist-lib/index.js +42 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.js +13 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +8 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +21 -19
- package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +11 -4
- package/dist-lib/lib/capabilities/completion-engine/actors/skill-step.js +10 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +7 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +5 -16
- package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.d.ts +1 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +9 -11
- package/dist-lib/lib/capabilities/completion-engine/index.d.ts +3 -1
- package/dist-lib/lib/capabilities/completion-engine/index.js +2 -0
- package/dist-lib/lib/capabilities/completion-engine/model-round.d.ts +14 -1
- package/dist-lib/lib/capabilities/completion-engine/model-round.js +52 -0
- package/dist-lib/lib/capabilities/completion-engine/round.d.ts +3 -3
- package/dist-lib/lib/capabilities/completion-engine/turns.js +14 -4
- package/dist-lib/lib/capabilities/sdk-tool.d.ts +9 -1
- package/dist-lib/lib/capabilities/sdk-tool.js +21 -2
- package/dist-lib/lib/capabilities/tool-plan.d.ts +4 -0
- package/dist-lib/lib/capabilities/tool-plan.js +15 -0
- package/dist-lib/lib/connect.js +12 -2
- package/dist-lib/lib/contract/run.d.ts +2 -0
- package/dist-lib/lib/contract-types.d.ts +1 -0
- package/dist-lib/lib/errors.d.ts +4 -0
- package/dist-lib/lib/errors.js +6 -0
- package/dist-lib/lib/hardware/cpu-placement.d.ts +8 -0
- package/dist-lib/lib/hardware/cpu-placement.js +48 -0
- package/dist-lib/lib/helpers/goal-check.d.ts +2 -0
- package/dist-lib/lib/helpers/goal-check.js +18 -0
- package/dist-lib/lib/machine/completion/actions/completion.js +30 -11
- package/dist-lib/lib/machine/completion/context.js +2 -1
- package/dist-lib/lib/machine/completion/rules.d.ts +1 -1
- package/dist-lib/lib/machine/completion/rules.js +7 -8
- package/dist-lib/lib/machine/completion/types.d.ts +1 -0
- package/dist-lib/lib/models/model-pool.d.ts +4 -0
- package/dist-lib/lib/models/model-pool.js +4 -1
- package/dist-lib/lib/prompt.d.ts +2 -1
- package/dist-lib/lib/prompt.js +12 -12
- package/dist-lib/lib/residency/completion-runtime.d.ts +9 -1
- package/dist-lib/lib/residency/completion-runtime.js +29 -26
- package/dist-lib/lib/residency/index.d.ts +4 -0
- package/dist-lib/lib/residency/index.js +6 -1
- package/dist-lib/lib/serve.js +7 -5
- package/dist-lib/lib/tools/http-request.js +1 -0
- package/dist-lib/plugins/mcp/server.js +1 -1
- package/dist-lib/plugins/obsidian/readiness.js +2 -2
- package/dist-lib/plugins/obsidian/vault-read.js +17 -16
- 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 +2 -1
- package/dist-lib/spec/hrpc/messages.js +7 -3
- package/dist-lib/spec/hyperschema/index.js +7 -3
- package/dist-lib/spec/hyperschema/schema.json +5 -0
- package/dist-lib/spec/hyperschema/types.d.ts +1 -0
- package/package.json +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CompletionFinal, Tool as SdkTool, ToolDialect } from '@qvac/sdk';
|
|
2
2
|
import { type AbortSignal as BareAbortSignal } from 'bare-abort-controller';
|
|
3
3
|
import type { TextEvent } from '../../helpers/thinking.ts';
|
|
4
|
-
import type { LlmGenerationParams, Message, ResponseFormat, RunLifecycleEvent } from '../../contract/index.ts';
|
|
4
|
+
import type { CompletionSdk, LlmGenerationParams, Message, ResponseFormat, RoundModelLease, RunLifecycleEvent } from '../../contract/index.ts';
|
|
5
5
|
export interface ModelRoundRequest {
|
|
6
6
|
runId: string;
|
|
7
7
|
round: number;
|
|
@@ -17,11 +17,19 @@ export interface ModelRoundRequest {
|
|
|
17
17
|
promptPrefix: string;
|
|
18
18
|
continuationNudge: string;
|
|
19
19
|
deliveredNudge: string;
|
|
20
|
+
unforced?: {
|
|
21
|
+
generationParams?: Partial<LlmGenerationParams> | undefined;
|
|
22
|
+
} | undefined;
|
|
20
23
|
}
|
|
21
24
|
export interface ModelRoundResult {
|
|
22
25
|
contentText: string;
|
|
23
26
|
final: CompletionFinal;
|
|
24
27
|
cut?: string | undefined;
|
|
28
|
+
unproven?: string | null | undefined;
|
|
29
|
+
replay?: string | null | undefined;
|
|
30
|
+
reasoningDeltas?: number | undefined;
|
|
31
|
+
ctxSize?: number | undefined;
|
|
32
|
+
overflows?: number | undefined;
|
|
25
33
|
}
|
|
26
34
|
export type ModelRoundEvent = TextEvent;
|
|
27
35
|
type RoundReset = Extract<RunLifecycleEvent, {
|
|
@@ -37,10 +45,15 @@ interface RunOptions {
|
|
|
37
45
|
signal?: BareAbortSignal | undefined;
|
|
38
46
|
emit(event: ModelRoundEvent | RoundReset): void;
|
|
39
47
|
}
|
|
48
|
+
export interface ModelRoundServeOptions {
|
|
49
|
+
sdk?: CompletionSdk | undefined;
|
|
50
|
+
acquireRound?: ((signal?: BareAbortSignal) => Promise<RoundModelLease>) | undefined;
|
|
51
|
+
}
|
|
40
52
|
export declare class ModelRound {
|
|
41
53
|
port: ModelRoundPort;
|
|
42
54
|
constructor(port: ModelRoundPort);
|
|
43
55
|
run(request: ModelRoundRequest, options: RunOptions): Promise<ModelRoundResult>;
|
|
44
56
|
static run(port: ModelRoundPort, request: ModelRoundRequest, { signal, emit }: RunOptions): Promise<ModelRoundResult>;
|
|
57
|
+
static serve(request: ModelRoundRequest, io: ModelRoundIo, { sdk, acquireRound }: ModelRoundServeOptions): Promise<ModelRoundResult>;
|
|
45
58
|
}
|
|
46
59
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbortController } from 'bare-abort-controller';
|
|
2
2
|
import { raceAbort } from '../../helpers/race-abort.js';
|
|
3
|
+
import { runRecoveredRound } from './round.js';
|
|
3
4
|
export class ModelRound {
|
|
4
5
|
port;
|
|
5
6
|
constructor(port) {
|
|
@@ -41,5 +42,56 @@ export class ModelRound {
|
|
|
41
42
|
open = false;
|
|
42
43
|
}
|
|
43
44
|
}
|
|
45
|
+
static async serve(request, io, { sdk, acquireRound }) {
|
|
46
|
+
const rounds = runRecoveredRound(sdk, servedArgs(request, io.signal, acquireRound));
|
|
47
|
+
let emit = io.emit;
|
|
48
|
+
for await (const event of served(rounds)) {
|
|
49
|
+
if (event.type === 'served') {
|
|
50
|
+
return result(event.round);
|
|
51
|
+
}
|
|
52
|
+
if (event.type === 'content' || event.type === 'thinking') {
|
|
53
|
+
emit(event);
|
|
54
|
+
}
|
|
55
|
+
else if (event.type === 'round-reset') {
|
|
56
|
+
emit = io.retake();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
throw new Error('A served round ended without its result');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function servedArgs(request, signal, acquireRound) {
|
|
63
|
+
return {
|
|
64
|
+
modelId: request.modelId,
|
|
65
|
+
acquireRound,
|
|
66
|
+
history: request.history,
|
|
67
|
+
failureResults: {},
|
|
68
|
+
continuationNudge: request.continuationNudge,
|
|
69
|
+
deliveredNudge: request.deliveredNudge,
|
|
70
|
+
tools: request.tools,
|
|
71
|
+
generationParams: request.generationParams,
|
|
72
|
+
responseFormat: request.responseFormat,
|
|
73
|
+
toolDialect: request.toolDialect,
|
|
74
|
+
cacheKey: request.cacheKey,
|
|
75
|
+
signal,
|
|
76
|
+
round: request.round,
|
|
77
|
+
runId: request.runId,
|
|
78
|
+
promptPrefix: request.promptPrefix,
|
|
79
|
+
capture: false,
|
|
80
|
+
knownTools: request.tools,
|
|
81
|
+
unforced: request.unforced,
|
|
82
|
+
live: {
|
|
83
|
+
round: request.round,
|
|
84
|
+
kind: request.kind,
|
|
85
|
+
startedAt: Date.now(),
|
|
86
|
+
rawChars: 0,
|
|
87
|
+
reasoningDeltas: 0
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
async function* served(rounds) {
|
|
92
|
+
yield { type: 'served', round: yield* rounds };
|
|
93
|
+
}
|
|
94
|
+
function result({ contentText, final, cut, unproven, replay, reasoningDeltas, ctxSize, overflows }) {
|
|
95
|
+
return { contentText, final, cut, unproven, replay, reasoningDeltas, ctxSize, overflows };
|
|
44
96
|
}
|
|
45
97
|
const noop = () => { };
|
|
@@ -30,7 +30,7 @@ export interface RoundArgs {
|
|
|
30
30
|
continuationNudge: string;
|
|
31
31
|
deliveredNudge: string;
|
|
32
32
|
tools?: SdkTool[] | undefined;
|
|
33
|
-
generationParams?: LlmGenerationParams | undefined;
|
|
33
|
+
generationParams?: Partial<LlmGenerationParams> | undefined;
|
|
34
34
|
responseFormat?: ResponseFormat | undefined;
|
|
35
35
|
toolDialect?: ToolDialect | undefined;
|
|
36
36
|
cacheKey?: string | undefined;
|
|
@@ -41,7 +41,7 @@ export interface RoundArgs {
|
|
|
41
41
|
capture: boolean;
|
|
42
42
|
knownTools?: ReadonlyArray<KnownTool> | undefined;
|
|
43
43
|
unforced?: {
|
|
44
|
-
generationParams
|
|
44
|
+
generationParams?: Partial<LlmGenerationParams> | undefined;
|
|
45
45
|
} | undefined;
|
|
46
46
|
recordRequest?: ((request: ModelRequest, failures: readonly FailureTurn[]) => Promise<void>) | undefined;
|
|
47
47
|
fold?: RoundFold | undefined;
|
|
@@ -67,7 +67,7 @@ export interface RoundFold {
|
|
|
67
67
|
type RoundReset = Extract<RunLifecycleEvent, {
|
|
68
68
|
type: 'round-reset';
|
|
69
69
|
}>;
|
|
70
|
-
type RoundEvent = TextEvent | InferenceCallEvent | InferenceSentEvent | RoundReset | FoldEvent;
|
|
70
|
+
export type RoundEvent = TextEvent | InferenceCallEvent | InferenceSentEvent | RoundReset | FoldEvent;
|
|
71
71
|
export declare function runRecoveredRound(sdk: CompletionSdk | undefined, args: RoundArgs): AsyncGenerator<RoundEvent, RecoveredRound>;
|
|
72
72
|
export declare function recordWholeRound(args: RoundArgs): Promise<CompletionParams>;
|
|
73
73
|
export {};
|
|
@@ -3,7 +3,7 @@ import { isRecord } from '../../guards.js';
|
|
|
3
3
|
import { stripToolCallMarkup, toolCallMarkup } from '../../helpers/tool-call-markup.js';
|
|
4
4
|
import { Agent } from '../../agent/index.js';
|
|
5
5
|
import { DIRECT_CALL_ID, PRELOAD_ID_PREFIX, skillLoadedTurn, toolResultHistoryEntry, toolResultTurn } from '../tool-turns.js';
|
|
6
|
-
import {
|
|
6
|
+
import { annotateUserTimes, currentTimeAnnotation } from '../../prompt.js';
|
|
7
7
|
import { isFailureResultReference } from '../../helpers/failure-result.js';
|
|
8
8
|
export function trustedSourcesFrom(history) {
|
|
9
9
|
const sources = [];
|
|
@@ -29,6 +29,7 @@ export function sanitizeTurn(turn, answered = false) {
|
|
|
29
29
|
export function modelTurns(messages, nudge) {
|
|
30
30
|
const turns = [];
|
|
31
31
|
const consumed = new Set();
|
|
32
|
+
const lastUser = messages.findLastIndex((turn) => turn.role === 'user');
|
|
32
33
|
for (const [index, turn] of messages.entries()) {
|
|
33
34
|
if (consumed.has(index)) {
|
|
34
35
|
continue;
|
|
@@ -38,7 +39,8 @@ export function modelTurns(messages, nudge) {
|
|
|
38
39
|
}
|
|
39
40
|
const rendered = modelTurn(withoutHostFields(turn), messages[index + 1]?.role === 'tool');
|
|
40
41
|
if (rendered) {
|
|
41
|
-
|
|
42
|
+
const added = index === lastUser ? turn.context : stampedContext(turn);
|
|
43
|
+
turns.push(added ? { ...rendered, content: turnText(rendered.content, added) } : rendered);
|
|
42
44
|
}
|
|
43
45
|
if (isReplayedResult(turn) && messages[index + 1]?.role === 'assistant') {
|
|
44
46
|
turns.push({ role: 'user', content: nudge });
|
|
@@ -67,13 +69,21 @@ function preloadTurns({ messages, at, nudge, consumed }) {
|
|
|
67
69
|
});
|
|
68
70
|
}
|
|
69
71
|
function withoutHostFields(turn) {
|
|
70
|
-
if (!('id' in turn) && !('context' in turn))
|
|
72
|
+
if (!('id' in turn) && !('context' in turn) && !('sentAt' in turn)) {
|
|
71
73
|
return turn;
|
|
74
|
+
}
|
|
72
75
|
const rest = { ...turn };
|
|
73
76
|
delete rest.id;
|
|
74
77
|
delete rest.context;
|
|
78
|
+
delete rest.sentAt;
|
|
75
79
|
return rest;
|
|
76
80
|
}
|
|
81
|
+
function stampedContext(turn) {
|
|
82
|
+
if (turn.role !== 'user' || !turn.sentAt || turn.context?.time !== undefined) {
|
|
83
|
+
return turn.context;
|
|
84
|
+
}
|
|
85
|
+
return { ...turn.context, time: currentTimeAnnotation(new Date(turn.sentAt)) };
|
|
86
|
+
}
|
|
77
87
|
function isPreloadId(id) {
|
|
78
88
|
return id.startsWith(PRELOAD_ID_PREFIX);
|
|
79
89
|
}
|
|
@@ -195,7 +205,7 @@ export function annotateCompletionHistoryLastUserTime(history, now) {
|
|
|
195
205
|
if (!entry || isFailureResultReference(entry) || entry.role !== 'user') {
|
|
196
206
|
continue;
|
|
197
207
|
}
|
|
198
|
-
result[index] =
|
|
208
|
+
result[index] = annotateUserTimes([entry], now)[0] ?? entry;
|
|
199
209
|
break;
|
|
200
210
|
}
|
|
201
211
|
return result;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Tool as SdkTool } from '@qvac/sdk';
|
|
2
|
-
import type { CompletionHistoryEntry, ToolDefinition } from '../contract/index.ts';
|
|
2
|
+
import type { AgentSkill, CompletionHistoryEntry, Message, ToolDefinition } from '../contract/index.ts';
|
|
3
3
|
export declare const TOOL_SEARCH_TOOL_NAME: string;
|
|
4
4
|
export declare function defers(tool: ToolDefinition): boolean;
|
|
5
5
|
export declare function deferredForRun(tool: ToolDefinition): ToolDefinition;
|
|
@@ -7,5 +7,13 @@ export declare function toSdkTool(tool: ToolDefinition): SdkTool;
|
|
|
7
7
|
export declare function toSdkTools(tools: readonly ToolDefinition[]): SdkTool[] | undefined;
|
|
8
8
|
export declare function renderedTools(tools: SdkTool[] | undefined): SdkTool[] | undefined;
|
|
9
9
|
export declare function loadedTools(history: readonly CompletionHistoryEntry[]): Set<string>;
|
|
10
|
+
export declare function loadTurns(sent: readonly SdkTool[] | undefined, names: Iterable<string>, history: readonly CompletionHistoryEntry[]): Message[];
|
|
11
|
+
interface SkillLoad {
|
|
12
|
+
sent: readonly SdkTool[] | undefined;
|
|
13
|
+
enabled: readonly ToolDefinition[];
|
|
14
|
+
history: readonly CompletionHistoryEntry[];
|
|
15
|
+
}
|
|
16
|
+
export declare function skillLoadTurns(skill: Pick<AgentSkill, 'tools'> | undefined, { sent, enabled, history }: SkillLoad): Message[];
|
|
10
17
|
export declare function unloadedTools(tools: readonly ToolDefinition[], history: readonly CompletionHistoryEntry[]): Set<string>;
|
|
11
18
|
export declare function withToolSearch(tools: ToolDefinition[]): ToolDefinition[];
|
|
19
|
+
export {};
|
|
@@ -70,7 +70,7 @@ function field(name, schema, required) {
|
|
|
70
70
|
return `${name}${optional}: ${shape(schema)}${fallback}${description}`;
|
|
71
71
|
}
|
|
72
72
|
export function defers(tool) {
|
|
73
|
-
return tool.defer === true
|
|
73
|
+
return (tool.defer === true || tool.skillGated === true) && !tool.endsRun && !tool.callsTools;
|
|
74
74
|
}
|
|
75
75
|
export function deferredForRun(tool) {
|
|
76
76
|
const deferred = { ...tool, defer: true };
|
|
@@ -94,7 +94,8 @@ export function toSdkTool(tool) {
|
|
|
94
94
|
description: tool.prompt?.description ?? tool.description,
|
|
95
95
|
parameters: sdkParameters(intentParameters(tool, tool.prompt?.parameters ?? tool.parameters))
|
|
96
96
|
};
|
|
97
|
-
|
|
97
|
+
const group = tool.group ?? tool.owningSkill ?? tool.toolset ?? undefined;
|
|
98
|
+
return defers(tool) ? { ...sent, deferLoading: true, group } : sent;
|
|
98
99
|
}
|
|
99
100
|
export function toSdkTools(tools) {
|
|
100
101
|
const searching = tools.some(defers);
|
|
@@ -111,6 +112,24 @@ export function renderedTools(tools) {
|
|
|
111
112
|
export function loadedTools(history) {
|
|
112
113
|
return loadedToolNames(history.filter((entry) => !isFailureResultReference(entry)));
|
|
113
114
|
}
|
|
115
|
+
export function loadTurns(sent, names, history) {
|
|
116
|
+
const turns = [];
|
|
117
|
+
for (const name of new Set(names)) {
|
|
118
|
+
const tool = sent?.find((each) => each.name === name && each.deferLoading === true);
|
|
119
|
+
const seen = [...history, ...turns];
|
|
120
|
+
if (tool && !loadedTools(seen).has(name)) {
|
|
121
|
+
const messages = seen.filter((entry) => !isFailureResultReference(entry));
|
|
122
|
+
const content = executeToolSearch([tool], { query: name, limit: 1 }, messages);
|
|
123
|
+
turns.push({ role: 'tool', content });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return turns;
|
|
127
|
+
}
|
|
128
|
+
export function skillLoadTurns(skill, { sent, enabled, history }) {
|
|
129
|
+
const granted = new Set(skill?.tools.map((entry) => Agent.parseToolGrant(entry).name));
|
|
130
|
+
const names = enabled.filter(({ name }) => granted.has(name)).map(({ name }) => name);
|
|
131
|
+
return loadTurns(sent, names, history);
|
|
132
|
+
}
|
|
114
133
|
export function unloadedTools(tools, history) {
|
|
115
134
|
const loaded = loadedTools(history);
|
|
116
135
|
return new Set(tools.filter((tool) => defers(tool) && !loaded.has(tool.name)).map(({ name }) => name));
|
|
@@ -32,5 +32,9 @@ interface KeyedResolvedCall extends ResolvedCall {
|
|
|
32
32
|
}
|
|
33
33
|
export declare function planToolCall(resolved: KeyedResolvedCall, opts: PlanToolCallOptions): ToolPolicyFacts;
|
|
34
34
|
export declare function skillNameOf(args: Record<string, unknown>): string;
|
|
35
|
+
export declare function withCatalogSkillName<Call extends {
|
|
36
|
+
name: string;
|
|
37
|
+
arguments: Record<string, unknown>;
|
|
38
|
+
}>(call: Call, catalog: readonly CompletionSkillEntry[]): Call;
|
|
35
39
|
export declare function skillFileKey(skillName: string, args: Record<string, unknown>): string | null;
|
|
36
40
|
export {};
|
|
@@ -64,6 +64,21 @@ export function planToolCall(resolved, opts) {
|
|
|
64
64
|
export function skillNameOf(args) {
|
|
65
65
|
return unquoteArg(args.name ?? args.skill_name ?? args.skill);
|
|
66
66
|
}
|
|
67
|
+
export function withCatalogSkillName(call, catalog) {
|
|
68
|
+
if (call.name !== Agent.SKILL_TOOL_NAME)
|
|
69
|
+
return call;
|
|
70
|
+
const asked = skillNameOf(call.arguments).toLowerCase();
|
|
71
|
+
const entry = catalog.find((skill) => skill.name.toLowerCase() === asked) ??
|
|
72
|
+
catalog.find((skill) => skill.displayName?.toLowerCase() === asked) ??
|
|
73
|
+
soleGrantingSkill(catalog, asked);
|
|
74
|
+
return entry && entry.name !== call.arguments.name
|
|
75
|
+
? { ...call, arguments: { ...call.arguments, name: entry.name } }
|
|
76
|
+
: call;
|
|
77
|
+
}
|
|
78
|
+
function soleGrantingSkill(catalog, toolName) {
|
|
79
|
+
const owners = catalog.filter((skill) => skill.tools.some((grant) => Agent.parseToolGrant(grant).name === toolName));
|
|
80
|
+
return owners.length === 1 ? owners[0] : undefined;
|
|
81
|
+
}
|
|
67
82
|
export function skillFileKey(skillName, args) {
|
|
68
83
|
const file = skillFileArg(args);
|
|
69
84
|
return mainFileRequested(skillName, file)
|
package/dist-lib/lib/connect.js
CHANGED
|
@@ -26,6 +26,7 @@ import { asToolset } from './tools/toolsets.js';
|
|
|
26
26
|
import { fromDownloadWire, fromKnowledgeHitWire, fromKnowledgeStatusWire, fromKnowledgeUpsertResultWire, fromWire, toKnowledgeUpsertWire, unpackVectors } from './wire.js';
|
|
27
27
|
export const HARNESS_DIED = 'HARNESS_DIED';
|
|
28
28
|
const PAUSE_GRACE_MS = 10_000;
|
|
29
|
+
const IN_PROCESS_ONLY_FIELDS = ['modelRound', 'roundCut'];
|
|
29
30
|
export function connectHarness(transport, options = {}) {
|
|
30
31
|
return new RemoteHarness(transport, options);
|
|
31
32
|
}
|
|
@@ -116,6 +117,7 @@ export class RemoteHarness extends EventEmitter {
|
|
|
116
117
|
: this._hop(context);
|
|
117
118
|
}
|
|
118
119
|
async *_hop(context) {
|
|
120
|
+
refuseInProcessOnly(context);
|
|
119
121
|
const runId = context.runId ?? b4a.toHex(randomBytes(16));
|
|
120
122
|
if (context.signal?.aborted) {
|
|
121
123
|
yield { type: 'aborted', runId };
|
|
@@ -166,7 +168,7 @@ export class RemoteHarness extends EventEmitter {
|
|
|
166
168
|
generationParams: context.generationParams,
|
|
167
169
|
responseFormat: context.responseFormat,
|
|
168
170
|
captureInferenceCalls: context.captureInferenceCalls,
|
|
169
|
-
messages: context.messages?.map(({ role, content, attachments, toolCalls, toolCallId, name, id, context: added }) => ({
|
|
171
|
+
messages: context.messages?.map(({ role, content, attachments, toolCalls, toolCallId, name, id, context: added, sentAt }) => ({
|
|
170
172
|
role,
|
|
171
173
|
content,
|
|
172
174
|
attachments: attachments?.map(({ path: file }) => ({ path: file })),
|
|
@@ -174,7 +176,8 @@ export class RemoteHarness extends EventEmitter {
|
|
|
174
176
|
toolCallId,
|
|
175
177
|
name,
|
|
176
178
|
id,
|
|
177
|
-
context: added
|
|
179
|
+
context: added,
|
|
180
|
+
sentAt
|
|
178
181
|
})),
|
|
179
182
|
toolConfigs: context.toolConfigs,
|
|
180
183
|
grantedDirs: context.grantedDirs,
|
|
@@ -968,6 +971,13 @@ function died(message) {
|
|
|
968
971
|
return Object.assign(new Error(message), { code: HARNESS_DIED });
|
|
969
972
|
}
|
|
970
973
|
const noop = () => { };
|
|
974
|
+
function refuseInProcessOnly(context) {
|
|
975
|
+
for (const field of IN_PROCESS_ONLY_FIELDS) {
|
|
976
|
+
if (context[field] !== undefined) {
|
|
977
|
+
throw new HarnessError('CAPABILITY_UNSUPPORTED', [field]);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
971
981
|
function abortable(promise, signal) {
|
|
972
982
|
if (!signal)
|
|
973
983
|
return promise;
|
|
@@ -18,6 +18,7 @@ export interface Message {
|
|
|
18
18
|
name?: string | null;
|
|
19
19
|
id?: string | null;
|
|
20
20
|
context?: TurnContext | null;
|
|
21
|
+
sentAt?: number | undefined;
|
|
21
22
|
[key: string]: unknown;
|
|
22
23
|
}
|
|
23
24
|
export interface TurnContext {
|
|
@@ -53,6 +54,7 @@ export interface RunGoal {
|
|
|
53
54
|
ms?: number;
|
|
54
55
|
tokens?: number;
|
|
55
56
|
checks?: string[];
|
|
57
|
+
checksProve?: boolean;
|
|
56
58
|
}
|
|
57
59
|
export type ToolEffect = 'read' | 'write';
|
|
58
60
|
export type ToolDecision = 'auto' | 'allowed' | 'denied' | 'skip-all' | 'blocked';
|
package/dist-lib/lib/errors.d.ts
CHANGED
|
@@ -200,6 +200,10 @@ declare const HARNESS_ERRORS: {
|
|
|
200
200
|
readonly code: 102610;
|
|
201
201
|
readonly message: (filePath: string) => string;
|
|
202
202
|
};
|
|
203
|
+
readonly OBSIDIAN_PATH_IS_FOLDER: {
|
|
204
|
+
readonly code: 102611;
|
|
205
|
+
readonly message: (folder: string) => string;
|
|
206
|
+
};
|
|
203
207
|
readonly SKILL_FILE_NOT_FOUND: {
|
|
204
208
|
readonly code: 102701;
|
|
205
209
|
readonly message: (name: string, file: string) => string;
|
package/dist-lib/lib/errors.js
CHANGED
|
@@ -145,6 +145,12 @@ const HARNESS_ERRORS = {
|
|
|
145
145
|
code: 102610,
|
|
146
146
|
message: (filePath) => `Destination already exists: ${filePath}`
|
|
147
147
|
},
|
|
148
|
+
OBSIDIAN_PATH_IS_FOLDER: {
|
|
149
|
+
code: 102611,
|
|
150
|
+
message: (folder) => `"${folder || '.'}" is a folder, not a note. List it with ` +
|
|
151
|
+
(folder ? `\`obsidian files folder="${folder}"\`` : '`obsidian files`') +
|
|
152
|
+
', then read one note by its path.'
|
|
153
|
+
},
|
|
148
154
|
SKILL_FILE_NOT_FOUND: {
|
|
149
155
|
code: 102701,
|
|
150
156
|
message: (name, file) => `ENOENT: ${name}/${file}`
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface CpuPlacement {
|
|
2
|
+
threads: number;
|
|
3
|
+
mask: string;
|
|
4
|
+
}
|
|
5
|
+
export type ReadSysFile = (path: string) => string | null;
|
|
6
|
+
export declare function parseCpuList(list: string): number[];
|
|
7
|
+
export declare function performanceCorePlacement(read?: ReadSysFile, hostPlatform?: string): CpuPlacement | undefined;
|
|
8
|
+
export declare function androidCpuPlacement(): CpuPlacement | undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { platform } from 'os';
|
|
3
|
+
const CPU_ROOT = '/sys/devices/system/cpu';
|
|
4
|
+
function readSysFile(path) {
|
|
5
|
+
try {
|
|
6
|
+
return String(fs.readFileSync(path, 'utf8'));
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function cpuRange(part) {
|
|
13
|
+
const [from, to = from] = part.split('-').map(Number);
|
|
14
|
+
if (!Number.isInteger(from) || !Number.isInteger(to) || to < from) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return Array.from({ length: to - from + 1 }, (_, i) => from + i);
|
|
18
|
+
}
|
|
19
|
+
export function parseCpuList(list) {
|
|
20
|
+
const ranges = list.trim().split(',').map(cpuRange);
|
|
21
|
+
return ranges.some((range) => range === null) ? [] : ranges.flat().filter((cpu) => cpu !== null);
|
|
22
|
+
}
|
|
23
|
+
function readCores(read, possible) {
|
|
24
|
+
return parseCpuList(possible).flatMap((cpu) => {
|
|
25
|
+
const maxFreq = Number(read(`${CPU_ROOT}/cpu${cpu}/cpufreq/cpuinfo_max_freq`)?.trim());
|
|
26
|
+
return Number.isFinite(maxFreq) && maxFreq > 0 ? [{ cpu, maxFreq }] : [];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function placementOf(cores) {
|
|
30
|
+
const slowest = Math.min(...cores.map((core) => core.maxFreq));
|
|
31
|
+
const fast = cores.filter((core) => core.maxFreq > slowest);
|
|
32
|
+
if (fast.length === 0) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
const mask = fast.reduce((bits, core) => bits | (1n << BigInt(core.cpu)), 0n);
|
|
36
|
+
return { threads: fast.length, mask: mask.toString(16) };
|
|
37
|
+
}
|
|
38
|
+
export function performanceCorePlacement(read = readSysFile, hostPlatform = platform()) {
|
|
39
|
+
if (hostPlatform !== 'android')
|
|
40
|
+
return undefined;
|
|
41
|
+
const possible = read(`${CPU_ROOT}/possible`);
|
|
42
|
+
return possible === null ? undefined : placementOf(readCores(read, possible));
|
|
43
|
+
}
|
|
44
|
+
let cached = null;
|
|
45
|
+
export function androidCpuPlacement() {
|
|
46
|
+
cached ??= { placement: performanceCorePlacement() };
|
|
47
|
+
return cached.placement;
|
|
48
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const MAX_GOAL_CHECKS = 8;
|
|
2
2
|
export declare const GOAL_CHECKS_ASK: string;
|
|
3
|
+
export declare const GOAL_PROOF_ASK: string;
|
|
3
4
|
export declare function namedChecks(text: string): string[];
|
|
5
|
+
export declare function mergeChecks(host: readonly string[], named: readonly string[]): string[];
|
|
4
6
|
export declare function runsCheck(command: string, check: string): boolean;
|
|
@@ -7,6 +7,11 @@ export const GOAL_CHECKS_ASK = '[This goal has no checks yet. Name the commands
|
|
|
7
7
|
'line `Checks:`, then one command per line in backticks, such as `test -s /app/result.json` ' +
|
|
8
8
|
'or `python3 check.py`. Each must exit 0 only once the task is done, with no pipe, ; or && ' +
|
|
9
9
|
'in it. The goal is met only once each of them ran on its own and exited 0.]';
|
|
10
|
+
export const GOAL_PROOF_ASK = '[The checks this goal has so far may only show that the output exists, not that it is right. Name the ' +
|
|
11
|
+
'commands that will prove the result is correct, such as running the program on an input ' +
|
|
12
|
+
"and comparing with what the task asks, or the task's own tests: write a line `Checks:`, " +
|
|
13
|
+
'then one command per line in backticks, with no pipe, ; or && in it. A command that repeats ' +
|
|
14
|
+
'one of those checks does not count. The goal is met only once each check ran on its own and exited 0.]';
|
|
10
15
|
export function namedChecks(text) {
|
|
11
16
|
const lines = text.split('\n');
|
|
12
17
|
const heading = lines.findIndex((line) => CHECKS_HEADING.test(line));
|
|
@@ -28,6 +33,19 @@ export function namedChecks(text) {
|
|
|
28
33
|
}
|
|
29
34
|
return named.slice(0, MAX_GOAL_CHECKS);
|
|
30
35
|
}
|
|
36
|
+
export function mergeChecks(host, named) {
|
|
37
|
+
const known = new Set(host.map(normalize));
|
|
38
|
+
const fresh = named
|
|
39
|
+
.filter((name) => !known.has(normalize(name)))
|
|
40
|
+
.slice(0, host.length ? MAX_GOAL_CHECKS - 1 : MAX_GOAL_CHECKS);
|
|
41
|
+
if (!fresh.length) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
return [...host.slice(0, MAX_GOAL_CHECKS - fresh.length), ...fresh];
|
|
45
|
+
}
|
|
46
|
+
function normalize(command) {
|
|
47
|
+
return command.trim().replace(/\s+/g, ' ');
|
|
48
|
+
}
|
|
31
49
|
export function runsCheck(command, check) {
|
|
32
50
|
const wanted = words(check);
|
|
33
51
|
const parts = command.split(SHELL_OPERATOR);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { hasRoundsLeft } from '../guards/retry.js';
|
|
2
|
-
import { namedChecks } from '../../../helpers/goal-check.js';
|
|
2
|
+
import { mergeChecks, namedChecks } from '../../../helpers/goal-check.js';
|
|
3
3
|
import { activate, applyHistorySplice, applySplice, foldMetrics, goalMet, goalStalled, goalTokensLeft, nudgeWithChecks, nudgeWithPlan, retainFailureResults } from '../rules.js';
|
|
4
4
|
export function startInitialRound({ context, params }) {
|
|
5
5
|
return applyPreparation(context, params);
|
|
@@ -19,9 +19,18 @@ export function startNextRound({ context }) {
|
|
|
19
19
|
}
|
|
20
20
|
export function retryMuteRound(args) {
|
|
21
21
|
const { context } = args;
|
|
22
|
-
return {
|
|
22
|
+
return {
|
|
23
|
+
...silentTurn(args, muteReAsk(context)),
|
|
24
|
+
mute: context.mute + 1,
|
|
25
|
+
round: context.round + 1
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function muteReAsk(context) {
|
|
29
|
+
return context.goal && !context.steering.length && !goalMet(context)
|
|
30
|
+
? [{ role: 'user', content: goalTurn(context) }]
|
|
31
|
+
: continuation(context);
|
|
23
32
|
}
|
|
24
|
-
function silentTurn({ context, params }) {
|
|
33
|
+
function silentTurn({ context, params }, reAsk = continuation(context)) {
|
|
25
34
|
if (params.cutOff) {
|
|
26
35
|
return {};
|
|
27
36
|
}
|
|
@@ -29,7 +38,7 @@ function silentTurn({ context, params }) {
|
|
|
29
38
|
history: [
|
|
30
39
|
...context.history,
|
|
31
40
|
{ role: 'assistant', content: params.response?.trim() ?? '' },
|
|
32
|
-
...
|
|
41
|
+
...reAsk
|
|
33
42
|
],
|
|
34
43
|
steering: []
|
|
35
44
|
};
|
|
@@ -142,7 +151,7 @@ export function addKnowledge({ context, params }) {
|
|
|
142
151
|
};
|
|
143
152
|
}
|
|
144
153
|
export function goalTurn({ policy, goal, plan }) {
|
|
145
|
-
return nudgeWithChecks(nudgeWithPlan(`${policy.goalNudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], goal?.checkable);
|
|
154
|
+
return nudgeWithChecks(nudgeWithPlan(`${policy.goalNudge}\nGoal: ${goal?.objective ?? ''}`, plan), goal?.checks ?? [], goal?.checkable && !goal.named);
|
|
146
155
|
}
|
|
147
156
|
export function continueGoal({ context }) {
|
|
148
157
|
const { goal } = context;
|
|
@@ -201,13 +210,16 @@ export function finishWrapRound({ context, params }) {
|
|
|
201
210
|
};
|
|
202
211
|
}
|
|
203
212
|
function adoptChecks(goal, text) {
|
|
204
|
-
if (!goal?.checkable || goal.
|
|
213
|
+
if (!goal?.checkable || goal.named) {
|
|
214
|
+
return goal;
|
|
215
|
+
}
|
|
216
|
+
const host = goal.checks.map((check) => check.name);
|
|
217
|
+
const names = mergeChecks(host, namedChecks(text));
|
|
218
|
+
if (!names.length) {
|
|
205
219
|
return goal;
|
|
206
220
|
}
|
|
207
|
-
const
|
|
208
|
-
return named
|
|
209
|
-
? { ...goal, checks: named.map((name) => ({ name, passed: false, output: null })) }
|
|
210
|
-
: goal;
|
|
221
|
+
const checks = names.map((name) => goal.checks.find((check) => check.name === name) ?? { name, passed: false, output: null });
|
|
222
|
+
return { ...goal, checks, named: true };
|
|
211
223
|
}
|
|
212
224
|
function settledAfterSend(context, params) {
|
|
213
225
|
return params.history && params.history.at < context.history.length
|
|
@@ -249,9 +261,16 @@ export function answerCompletion({ context, params }) {
|
|
|
249
261
|
: history,
|
|
250
262
|
response: params.response || context.response,
|
|
251
263
|
metrics,
|
|
252
|
-
outcome: context.outcome ?? 'answered'
|
|
264
|
+
outcome: metGoalAnswered(context) ? 'answered' : (context.outcome ?? 'answered')
|
|
253
265
|
};
|
|
254
266
|
}
|
|
267
|
+
function metGoalAnswered(context) {
|
|
268
|
+
const round = context.lastRound;
|
|
269
|
+
return (context.outcome === 'mute' &&
|
|
270
|
+
context.goal !== null &&
|
|
271
|
+
goalMet(context) &&
|
|
272
|
+
Boolean(round?.contentText.trim() || round?.replay?.trim()));
|
|
273
|
+
}
|
|
255
274
|
export function failCompletion({ params }) {
|
|
256
275
|
const failure = 'tag' in params
|
|
257
276
|
? params
|
|
@@ -65,7 +65,8 @@ export function emptyCompletionContext(opts) {
|
|
|
65
65
|
tokens: opts.goal.tokens ?? null,
|
|
66
66
|
turns: 0,
|
|
67
67
|
status: 'open',
|
|
68
|
-
checks: (opts.goal.checks ?? []).map((name) => ({ name, passed: false, output: null }))
|
|
68
|
+
checks: (opts.goal.checks ?? []).map((name) => ({ name, passed: false, output: null })),
|
|
69
|
+
named: (opts.goal.checksProve && Boolean(opts.goal.checks?.length)) || undefined
|
|
69
70
|
}
|
|
70
71
|
: null
|
|
71
72
|
};
|
|
@@ -11,7 +11,7 @@ export declare const MAX_ROUND_REPEATS = 3;
|
|
|
11
11
|
export declare const MAX_ROUND_CALLS = 32;
|
|
12
12
|
export declare function nudgeWithPlan(nudge: string, plan: readonly PlanStep[]): string;
|
|
13
13
|
export declare function goalMet({ plan, goal }: Readonly<CompletionContext>): boolean;
|
|
14
|
-
export declare function nudgeWithChecks(nudge: string, checks: readonly GoalCheck[],
|
|
14
|
+
export declare function nudgeWithChecks(nudge: string, checks: readonly GoalCheck[], unnamed?: boolean): string;
|
|
15
15
|
export declare function goalTokensLeft({ goal, metrics }: Readonly<CompletionContext>): boolean;
|
|
16
16
|
export declare function goalStalled({ outcome }: Readonly<CompletionContext>): boolean;
|
|
17
17
|
export declare function retainFailureResults(history: readonly CompletionHistoryEntry[], values: Readonly<Record<string, FailureResultValue>>, failedCalls: Readonly<Record<string, SettledCall>>): Record<string, FailureResultValue>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isFailureResultReference } from '../../helpers/failure-result.js';
|
|
2
|
-
import { GOAL_CHECKS_ASK } from '../../helpers/goal-check.js';
|
|
2
|
+
import { 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,20 +35,19 @@ function planDone(plan) {
|
|
|
35
35
|
}
|
|
36
36
|
export function goalMet({ plan, goal }) {
|
|
37
37
|
const checks = goal?.checks ?? [];
|
|
38
|
-
return (planDone(plan) &&
|
|
39
|
-
(checks.length > 0 || !goal?.checkable) &&
|
|
40
|
-
checks.every((check) => check.passed));
|
|
38
|
+
return (planDone(plan) && (goal?.named || !goal?.checkable) && checks.every((check) => check.passed));
|
|
41
39
|
}
|
|
42
|
-
export function nudgeWithChecks(nudge, checks,
|
|
40
|
+
export function nudgeWithChecks(nudge, checks, unnamed) {
|
|
43
41
|
if (!checks.length) {
|
|
44
|
-
return
|
|
42
|
+
return unnamed ? `${nudge}\n${GOAL_CHECKS_ASK}` : nudge;
|
|
45
43
|
}
|
|
44
|
+
const asked = unnamed ? `${nudge}\n${GOAL_PROOF_ASK}` : nudge;
|
|
46
45
|
const open = checks.filter((check) => !check.passed);
|
|
47
46
|
if (!open.length) {
|
|
48
|
-
return
|
|
47
|
+
return asked;
|
|
49
48
|
}
|
|
50
49
|
const lines = open.map(({ name, output }) => output === null ? `- ${name}: not run yet` : `- ${name}: failed. Its last output:\n${output}`);
|
|
51
|
-
return (`${
|
|
50
|
+
return (`${asked}\nChecks: the goal is met only once each of these runs as its own command and ` +
|
|
52
51
|
`exits 0, with no pipe or ; after it to hide its exit code.\n${lines.join('\n')}`);
|
|
53
52
|
}
|
|
54
53
|
export function goalTokensLeft({ goal, metrics }) {
|