@nylorun/harness 0.27.1-beta → 0.29.0-beta
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 +131 -0
- package/HOST_CONTRACT.md +15 -13
- package/README.md +3 -1
- package/dist/api/executors.d.ts +79 -11
- package/dist/api/harness.d.ts +83 -2
- package/dist/api/harness.js +43 -4
- package/dist/api/host.d.ts +30 -2
- package/dist/api/host.js +156 -8
- package/dist/api/index.d.ts +21 -6
- package/dist/api/index.js +19 -4
- package/dist/api/run.d.ts +3 -0
- package/dist/api/run.js +91 -16
- package/dist/flow/context.d.ts +3 -0
- package/dist/flow/context.js +2 -1
- package/dist/flow/engine.d.ts +2 -0
- package/dist/flow/engine.js +10 -7
- package/dist/flow/limits.d.ts +8 -5
- package/dist/flow/limits.js +11 -7
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/loop/compaction/config.d.ts +39 -0
- package/dist/loop/compaction/config.js +29 -0
- package/dist/loop/compaction/index.d.ts +18 -8
- package/dist/loop/compaction/index.js +57 -66
- package/dist/loop/config.d.ts +20 -0
- package/dist/loop/config.js +17 -0
- package/dist/loop/delegation.d.ts +3 -0
- package/dist/loop/delegation.js +14 -10
- package/dist/loop/invocation.d.ts +3 -0
- package/dist/loop/invocation.js +2 -0
- package/dist/loop/model/files.d.ts +77 -0
- package/dist/loop/model/files.js +168 -0
- package/dist/loop/model/json-answer.d.ts +26 -0
- package/dist/loop/model/json-answer.js +125 -0
- package/dist/loop/model/project.d.ts +14 -4
- package/dist/loop/model/project.js +70 -41
- package/dist/loop/options.js +11 -3
- package/dist/loop/run.js +15 -5
- package/dist/loop/state.d.ts +6 -1
- package/dist/loop/state.js +19 -0
- package/dist/loop/step/canonicalize.d.ts +11 -2
- package/dist/loop/step/canonicalize.js +21 -5
- package/dist/loop/step/resolve.js +1 -0
- package/dist/loop/step/run.js +20 -3
- package/dist/loop/step/runtime.d.ts +6 -0
- package/dist/loop/step/seal.js +8 -2
- package/dist/loop/step/step-context.js +1 -1
- package/dist/run/durable.d.ts +58 -3
- package/dist/run/durable.js +104 -33
- package/dist/tools/artifacts.d.ts +33 -0
- package/dist/tools/artifacts.js +323 -0
- package/dist/tools/engine-tools.d.ts +50 -0
- package/dist/tools/engine-tools.js +42 -0
- package/dist/tools/mcp.d.ts +92 -0
- package/dist/tools/mcp.js +240 -0
- package/dist/tools/memory.d.ts +16 -0
- package/dist/tools/memory.js +140 -0
- package/dist/tools/outputs.d.ts +23 -0
- package/dist/tools/outputs.js +193 -0
- package/dist/tools/results.d.ts +108 -0
- package/dist/tools/results.js +329 -0
- package/dist/tools/sandbox.d.ts +35 -0
- package/dist/tools/sandbox.js +129 -0
- package/dist/tools/skills.d.ts +94 -0
- package/dist/tools/skills.js +243 -0
- package/dist/tools/text-page.d.ts +20 -0
- package/dist/tools/text-page.js +64 -0
- package/dist/tools/tool-search.d.ts +56 -0
- package/dist/tools/tool-search.js +174 -0
- package/dist/tools/trace.d.ts +13 -0
- package/dist/tools/trace.js +34 -0
- package/dist/tools/wake.d.ts +13 -0
- package/dist/tools/wake.js +33 -0
- package/dist/types/execution.d.ts +35 -1
- package/package.json +7 -7
package/dist/run/durable.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { HarnessError } from "@nylorun/core/define";
|
|
2
2
|
import { effectIdOf } from "@nylorun/core/harness-api";
|
|
3
|
-
import { AgentManifestSchema } from "@nylorun/core/contracts";
|
|
4
|
-
import {
|
|
3
|
+
import { AgentManifestSchema, checkMemoryCall } from "@nylorun/core/contracts";
|
|
4
|
+
import { agentFrom, withHttpTarget } from "@nylorun/core/define";
|
|
5
|
+
import { TOOL_CALL_TOOL } from "../tools/tool-search.js";
|
|
5
6
|
import { definitionFor } from "../definition/agent-definition.js";
|
|
6
7
|
import { schemaFromJSON } from "@nylorun/core/define";
|
|
7
8
|
import { hashManifest } from "@nylorun/core/define";
|
|
8
9
|
import { withDeterministicIds } from "../utils/ids.js";
|
|
10
|
+
import { skillOfToolCall } from "../tools/trace.js";
|
|
9
11
|
import { execute } from "../loop/run.js";
|
|
10
12
|
import { initializeExecutionState } from "../loop/initial-state.js";
|
|
11
13
|
import { HostSuspension } from "../loop/host-suspension.js";
|
|
@@ -72,7 +74,7 @@ export async function runDurable(options) {
|
|
|
72
74
|
return result.outcome;
|
|
73
75
|
};
|
|
74
76
|
/** Tools of one agent. Each agent keeps its own patch order; children namespace their effects. */
|
|
75
|
-
const hostedTools = (ref) => {
|
|
77
|
+
const hostedTools = (agent, ref) => {
|
|
76
78
|
let patchTail = Promise.resolve();
|
|
77
79
|
return (tool) => ({
|
|
78
80
|
name: tool.name,
|
|
@@ -99,7 +101,7 @@ export async function runDurable(options) {
|
|
|
99
101
|
info: ctx.info,
|
|
100
102
|
state: ctx.state.entries(),
|
|
101
103
|
resume: ctx.resume,
|
|
102
|
-
}, scoped(ref, ctx.invocationId),
|
|
104
|
+
}, scoped(ref, ctx.invocationId), identityOf(agent, tool, args, ref));
|
|
103
105
|
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
104
106
|
await previous;
|
|
105
107
|
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
@@ -122,50 +124,62 @@ export async function runDurable(options) {
|
|
|
122
124
|
},
|
|
123
125
|
});
|
|
124
126
|
};
|
|
125
|
-
const sessionTools = options.
|
|
127
|
+
const sessionTools = options.mcp?.tools ?? [];
|
|
126
128
|
/** Rebuild one agent from its manifest with every tool routed through host effects. */
|
|
127
129
|
const hostedDefinition = (agent, ref) => {
|
|
128
|
-
const hostedTool = hostedTools(ref);
|
|
129
|
-
const
|
|
130
|
+
const hostedTool = hostedTools(agent, ref);
|
|
131
|
+
const engine = options.engineTools?.(agent);
|
|
132
|
+
const mine = [
|
|
133
|
+
...(engine?.tools ?? []),
|
|
134
|
+
...sessionTools.filter((tool) => (tool.agentId ?? manifest.id) === agent.id),
|
|
135
|
+
];
|
|
130
136
|
// Deferred tools stay out of the model's list; `tool_call` runs them (R2b C10).
|
|
131
137
|
const owned = mine.filter((tool) => !tool.deferred);
|
|
132
138
|
const deferred = new Map(mine.filter((tool) => tool.deferred).map((tool) => [tool.name, tool]));
|
|
133
139
|
const tools = {};
|
|
134
140
|
// Agents used as tools are rebuilt from their manifest body; the engine runs them. An
|
|
135
141
|
// HTTP tool keeps its target (the manifest's hash covers it) and runs as a host effect.
|
|
142
|
+
// An author's code tool names no target: the record fails it to the model.
|
|
136
143
|
for (const tool of agent.tools ?? [])
|
|
137
144
|
if (tool.http)
|
|
138
145
|
// A pinned `approval` stays for the definition's hash; the Tool Gate holds the call.
|
|
139
|
-
tools[tool.name] = withHttpTarget(hostedTool(tool), {
|
|
146
|
+
tools[tool.name] = withHttpTarget(hostedTool({ ...tool, target: { kind: "http", tool: tool.name } }), {
|
|
140
147
|
http: tool.http,
|
|
141
148
|
...(tool.approval === undefined ? {} : { approval: tool.approval }),
|
|
142
149
|
});
|
|
143
150
|
else if (!tool.agent)
|
|
144
151
|
tools[tool.name] = hostedTool(tool);
|
|
145
|
-
//
|
|
146
|
-
for (const tool of agent.runtime?.tools ?? [])
|
|
147
|
-
tools[tool.name] = hostedTool(tool);
|
|
152
|
+
// Our engine's own tools and the session's run as host effects too.
|
|
148
153
|
for (const tool of owned)
|
|
149
154
|
tools[tool.name] =
|
|
150
155
|
tool.name === TOOL_CALL_TOOL
|
|
151
156
|
? deferredToolCall(tool, deferred, hostedTool)
|
|
152
|
-
:
|
|
157
|
+
: tool.target?.kind === "runtime" && tool.target.operation === "memory"
|
|
158
|
+
? checkedMemoryTool(hostedTool(tool))
|
|
159
|
+
: hostedTool(tool);
|
|
153
160
|
const implementations = { tools };
|
|
154
|
-
return definitionFor(agentFrom(agent, implementations,
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
161
|
+
return definitionFor(agentFrom(agent, implementations, {
|
|
162
|
+
sessionTools: owned.map((tool) => ({
|
|
163
|
+
name: tool.name,
|
|
164
|
+
...(tool.instructions?.length ? { instructions: tool.instructions } : {}),
|
|
165
|
+
})),
|
|
166
|
+
...(engine?.instructions.length ? { sessionInstructions: engine.instructions } : {}),
|
|
167
|
+
}));
|
|
168
|
+
};
|
|
169
|
+
/** The root agent's summary calls, in the order they were made: a compaction edit names them. */
|
|
170
|
+
const summaries = [];
|
|
171
|
+
const modelCall = (ref) => async (call, ctx) => {
|
|
172
|
+
const identity = scoped(ref, ctx.invocationId);
|
|
173
|
+
// A compaction of an agent used as a tool lives in our state, not the session's transcript.
|
|
174
|
+
if (ctx.compaction && !ref)
|
|
175
|
+
summaries.push(effectIdOf(checkpoint.turnId, checkpoint.segment, "model", identity));
|
|
176
|
+
return (await effect("model", call,
|
|
177
|
+
// The call already holds the whole prompt; journaling the request would store it twice.
|
|
178
|
+
{ invocationId: ctx.invocationId }, identity, {
|
|
179
|
+
...(ref ? { agent: ref } : {}),
|
|
180
|
+
...(ctx.compaction ? { purpose: "summary" } : {}),
|
|
181
|
+
})).value;
|
|
162
182
|
};
|
|
163
|
-
const modelCall = (ref) => async (call, ctx) => (await effect("model", call,
|
|
164
|
-
// The call already holds the whole prompt; journaling the request would store it twice.
|
|
165
|
-
{
|
|
166
|
-
invocationId: ctx.invocationId,
|
|
167
|
-
...(ctx.compaction ? { compaction: ctx.compaction } : {}),
|
|
168
|
-
}, scoped(ref, ctx.invocationId), ref ? { agent: ref } : {})).value;
|
|
169
183
|
const delegation = {
|
|
170
184
|
child: (delegate, ref) => ({
|
|
171
185
|
definition: hostedDefinition(delegate.manifest, ref),
|
|
@@ -189,14 +203,25 @@ export async function runDurable(options) {
|
|
|
189
203
|
throw new HarnessError("execution.incompatible", "Reconstructed definition hash mismatch");
|
|
190
204
|
try {
|
|
191
205
|
const result = await withDeterministicIds(`${checkpoint.turnId}_${checkpoint.segment}`, () => execute(hosted, {
|
|
192
|
-
state: checkpoint.state ??
|
|
193
|
-
initializeExecutionState(hosted, { executionId: checkpoint.sessionId }),
|
|
206
|
+
state: withPinned(withLayout(checkpoint.state ??
|
|
207
|
+
initializeExecutionState(hosted, { executionId: checkpoint.sessionId }), options.mcp), options.pinned),
|
|
194
208
|
input: checkpoint.input,
|
|
195
209
|
info: checkpoint.info,
|
|
196
210
|
signal: options.signal,
|
|
197
211
|
onModelCall: modelCall(),
|
|
198
|
-
|
|
199
|
-
|
|
212
|
+
...(options.model === undefined ? {} : { model: options.model }),
|
|
213
|
+
...(options.files === undefined ? {} : { files: options.files }),
|
|
214
|
+
}, {
|
|
215
|
+
delegation,
|
|
216
|
+
...(options.yieldAfter ? { yieldAfter: options.yieldAfter } : {}),
|
|
217
|
+
...(options.loop ? { loop: options.loop } : {}),
|
|
218
|
+
}));
|
|
219
|
+
return {
|
|
220
|
+
status: result.status,
|
|
221
|
+
checkpoint: { ...checkpoint, state: result.state },
|
|
222
|
+
result,
|
|
223
|
+
...(summaries.length > 0 ? { summaryEffectIds: [...summaries] } : {}),
|
|
224
|
+
};
|
|
200
225
|
}
|
|
201
226
|
catch (error) {
|
|
202
227
|
// Parallel tool dispatch can suspend more than one action. Drain persistence before returning.
|
|
@@ -210,6 +235,52 @@ export async function runDurable(options) {
|
|
|
210
235
|
};
|
|
211
236
|
}
|
|
212
237
|
}
|
|
238
|
+
/** The state with the MCP tools' layout our engine follows (`mcpLayout`), when it has MCP tools. */
|
|
239
|
+
function withLayout(state, mcp) {
|
|
240
|
+
if (mcp === undefined)
|
|
241
|
+
return state;
|
|
242
|
+
const { factsHash, deferred } = mcp.layout;
|
|
243
|
+
return { ...state, mcpLayout: { factsHash, deferred: [...deferred] } };
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The state with the text our engine keeps at the head of the conversation: what it pinned
|
|
247
|
+
* before, or `pinned` when it has none (the session's first turn, or after a compaction).
|
|
248
|
+
*/
|
|
249
|
+
function withPinned(state, pinned) {
|
|
250
|
+
if (pinned === undefined) {
|
|
251
|
+
if (state.pinned === undefined)
|
|
252
|
+
return state;
|
|
253
|
+
const { pinned: _dropped, ...rest } = state;
|
|
254
|
+
return rest;
|
|
255
|
+
}
|
|
256
|
+
return state.pinned === undefined ? { ...state, pinned: [...pinned] } : state;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* `memory` (track T5): each command's parameters are checked before the call is sent
|
|
260
|
+
* (`checkMemoryCall`), so a call the Runtime would refuse is never journaled; the model reads the
|
|
261
|
+
* error text and may call again.
|
|
262
|
+
*/
|
|
263
|
+
function checkedMemoryTool(run) {
|
|
264
|
+
return {
|
|
265
|
+
...run,
|
|
266
|
+
async execute(args, ctx) {
|
|
267
|
+
const checked = checkMemoryCall(args);
|
|
268
|
+
if ("error" in checked)
|
|
269
|
+
return { kind: "failed", code: "tool.invalid-arguments", message: checked.error };
|
|
270
|
+
return run.execute(args, ctx);
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
/** What a tool effect names beside its input: the tool, what runs it, its skill and its agent. */
|
|
275
|
+
function identityOf(agent, tool, input, ref) {
|
|
276
|
+
const skill = skillOfToolCall(agent, tool, input);
|
|
277
|
+
return {
|
|
278
|
+
toolName: tool.name,
|
|
279
|
+
...(tool.target === undefined ? {} : { target: tool.target }),
|
|
280
|
+
...(skill === undefined ? {} : { skill }),
|
|
281
|
+
...(ref ? { agent: ref } : {}),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
213
284
|
/** Effect identities of an agent used as a tool live under its delegation. */
|
|
214
285
|
function scoped(ref, identity) {
|
|
215
286
|
return ref ? `${ref.delegationId}/${identity}` : identity;
|
|
@@ -217,9 +288,9 @@ function scoped(ref, identity) {
|
|
|
217
288
|
/**
|
|
218
289
|
* `tool_call` (R2b C10): runs a deferred tool by name, as that tool's own call. It checks the
|
|
219
290
|
* arguments against the tool's `inputSchema` first, so a mistake comes back to the model as a
|
|
220
|
-
* failed result it can fix. The effect then names the tool
|
|
221
|
-
*
|
|
222
|
-
* would a direct call.
|
|
291
|
+
* failed result it can fix. The effect then names the tool and its identity (`target`), so the
|
|
292
|
+
* record routes it and records `tool.completed` as for a direct call, the host shapes its result,
|
|
293
|
+
* and the Tool Gate holds it as it would a direct call.
|
|
223
294
|
*/
|
|
224
295
|
function deferredToolCall(tool, deferred, hostedTool) {
|
|
225
296
|
const hosted = new Map();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type ArtifactRequester } from "@nylorun/core/harness-api";
|
|
2
|
+
import type { DurableSessionTool, HostEffect } from "../run/durable.js";
|
|
3
|
+
import type { FailedFileOutcome, HarnessFiles, HarnessRun } from "../api/executors.js";
|
|
4
|
+
/** The harness's configuration of its artifact tools. */
|
|
5
|
+
export interface ArtifactToolOptions {
|
|
6
|
+
/** The most bytes one `read_artifact` page holds, escaped as JSON. */
|
|
7
|
+
readonly pageBytes: number;
|
|
8
|
+
}
|
|
9
|
+
/** True when `name` is one of our engine's artifact tools. */
|
|
10
|
+
export declare function isArtifactTool(name: string | undefined): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The artifact tools our engine offers an agent: `save_artifact` when the session has a sandbox,
|
|
13
|
+
* and `read_artifact`, which takes `path` only then. Each runs here (`harness`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function artifactToolsFor(sandboxed: boolean, options: ArtifactToolOptions): DurableSessionTool[];
|
|
16
|
+
/** What the model reads with the artifact tools, in their order. */
|
|
17
|
+
export declare function artifactInstructions(sandboxed: boolean): string[];
|
|
18
|
+
/** What an artifact tool runs with. */
|
|
19
|
+
export interface ArtifactToolContext extends ArtifactToolOptions {
|
|
20
|
+
readonly channel: ArtifactRequester;
|
|
21
|
+
readonly run: HarnessRun;
|
|
22
|
+
/** The run's sandbox files; undefined where the harness has none. */
|
|
23
|
+
readonly files?: HarnessFiles;
|
|
24
|
+
readonly signal: AbortSignal;
|
|
25
|
+
}
|
|
26
|
+
type Outcome = {
|
|
27
|
+
readonly kind: "completed";
|
|
28
|
+
readonly output: Record<string, unknown>;
|
|
29
|
+
readonly files?: readonly unknown[];
|
|
30
|
+
} | FailedFileOutcome;
|
|
31
|
+
/** Runs a call of one of our engine's artifact tools, as its outcome. */
|
|
32
|
+
export declare function runArtifactTool(effect: HostEffect, context: ArtifactToolContext): Promise<Outcome>;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Our engine's artifact tools (protocol 15), which run here, in the harness, over the Harness
|
|
3
|
+
* API's artifact seam (`artifact.read`, `artifact.write`) and the run's sandbox files:
|
|
4
|
+
*
|
|
5
|
+
* - `read_artifact` (every agent) reads an artifact the session may read in pages: a tool result
|
|
6
|
+
* the Runtime stored because it was too large to show, a file of an earlier turn. A text file
|
|
7
|
+
* comes back a page of at most `artifactPageBytes` at a time (escaped as JSON); any other file
|
|
8
|
+
* goes to the model as a file of the result when the turn's model reads it (`fileView`), and is
|
|
9
|
+
* `artifact.unsupported` when it does not. With
|
|
10
|
+
* a sandbox, `path` copies the artifact into the sandbox instead.
|
|
11
|
+
* - `save_artifact` (with a sandbox) saves a sandbox file, or text the model passes, as an
|
|
12
|
+
* artifact of the session. Its effect id is the write's key, so a write the harness repeats
|
|
13
|
+
* answers the version it wrote, and links the artifact to the call (`artifact.created` carries
|
|
14
|
+
* the call id).
|
|
15
|
+
*
|
|
16
|
+
* The model's tools are ours to name, describe and instruct; the Runtime decides what a run may
|
|
17
|
+
* read and write, and holds the limits. Expected problems (no such artifact, too large, not a
|
|
18
|
+
* file) are failed outcomes the model sees; only the run's own failures throw.
|
|
19
|
+
*/
|
|
20
|
+
import { READ_ARTIFACT_TOOL, SANDBOX_WORKSPACE, SAVE_ARTIFACT_TOOL, } from "@nylorun/core/define";
|
|
21
|
+
import { ARTIFACT_NAME_MAX, DEFAULT_CONTENT_TYPE, contentTypeFor, essence, isText, normalizeContentType, } from "@nylorun/core/contracts";
|
|
22
|
+
import { HarnessApiError, fromBase64, openArtifactWrite, readArtifact, writeArtifactBytes, } from "@nylorun/core/harness-api";
|
|
23
|
+
import { textPage } from "./text-page.js";
|
|
24
|
+
import { fileView } from "../loop/model/files.js";
|
|
25
|
+
const ARTIFACTS_INSTRUCTIONS = "Use save_artifact to hand the user a file you made: a path in the sandbox, or text you pass as content. " +
|
|
26
|
+
"The user sees it as an artifact of this session and can download it; reply with its name, not its bytes. " +
|
|
27
|
+
"Files you write under /workspace/outputs reach the user too: when your turn ends, they become the session's outputs folder.";
|
|
28
|
+
const READ_ARTIFACT_INSTRUCTIONS = "A tool result too large to show is saved as an artifact of this session: the result says truncated: true, with its artifactId, its size in bytes and a preview of its start and end. " +
|
|
29
|
+
"Images and other files a tool returns are saved as artifacts too. Use read_artifact to read more of one, a page at a time, only when the preview is not enough. " +
|
|
30
|
+
'Files from earlier turns appear as lines like [Uploaded artifact "name" · <artifactId> v<version> · read_artifact to view]: call read_artifact with that artifactId and version only when you need the file again.';
|
|
31
|
+
const SAVE_DESCRIPTION = `Save a file as an artifact of this session, for the user to download. Give path (a file in the sandbox; relative paths resolve under ${SANDBOX_WORKSPACE}) or content (text), not both. ` +
|
|
32
|
+
"Returns the artifact's id and version. Pass artifactId to save a new version of an artifact you saved before.";
|
|
33
|
+
const SAVE_SCHEMA = {
|
|
34
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
path: { description: "A file in the sandbox.", type: "string", minLength: 1 },
|
|
38
|
+
content: { description: "Text to save instead of a file.", type: "string" },
|
|
39
|
+
name: {
|
|
40
|
+
description: "The artifact's name. Default: the file's name.",
|
|
41
|
+
type: "string",
|
|
42
|
+
minLength: 1,
|
|
43
|
+
maxLength: ARTIFACT_NAME_MAX,
|
|
44
|
+
},
|
|
45
|
+
contentType: {
|
|
46
|
+
description: "The media type, e.g. image/png. Default: from the name's extension.",
|
|
47
|
+
type: "string",
|
|
48
|
+
minLength: 1,
|
|
49
|
+
},
|
|
50
|
+
artifactId: {
|
|
51
|
+
description: "Save a new version of this artifact.",
|
|
52
|
+
type: "string",
|
|
53
|
+
minLength: 1,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
additionalProperties: false,
|
|
57
|
+
};
|
|
58
|
+
function readDescription(pageBytes, sandboxed) {
|
|
59
|
+
return (`Read an artifact of this session, such as a tool result marked truncated or a file from an earlier turn. A text file returns up to ${pageBytes} bytes from offset, ` +
|
|
60
|
+
"with nextOffset while more remains: pass it as offset to read on. An image or another file you can read is shown to you." +
|
|
61
|
+
(sandboxed
|
|
62
|
+
? ` Pass path to copy the whole artifact into the sandbox instead (relative paths resolve under ${SANDBOX_WORKSPACE}), for a command to use.`
|
|
63
|
+
: ""));
|
|
64
|
+
}
|
|
65
|
+
function readSchema(pageBytes, sandboxed) {
|
|
66
|
+
return {
|
|
67
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
68
|
+
type: "object",
|
|
69
|
+
properties: {
|
|
70
|
+
artifactId: {
|
|
71
|
+
type: "string",
|
|
72
|
+
minLength: 1,
|
|
73
|
+
description: "The artifact to read, e.g. a truncated result's artifactId.",
|
|
74
|
+
},
|
|
75
|
+
version: {
|
|
76
|
+
description: "The version to read. Default: the latest.",
|
|
77
|
+
type: "integer",
|
|
78
|
+
minimum: 1,
|
|
79
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
80
|
+
},
|
|
81
|
+
offset: {
|
|
82
|
+
description: "The byte to start at. Default 0.",
|
|
83
|
+
type: "integer",
|
|
84
|
+
minimum: 0,
|
|
85
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
86
|
+
},
|
|
87
|
+
length: {
|
|
88
|
+
description: `How many bytes to read. Default and most: ${pageBytes}.`,
|
|
89
|
+
type: "integer",
|
|
90
|
+
minimum: 1,
|
|
91
|
+
maximum: pageBytes,
|
|
92
|
+
},
|
|
93
|
+
...(sandboxed
|
|
94
|
+
? {
|
|
95
|
+
path: {
|
|
96
|
+
description: "Copy the artifact to this file in the sandbox instead of reading it.",
|
|
97
|
+
type: "string",
|
|
98
|
+
minLength: 1,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
: {}),
|
|
102
|
+
},
|
|
103
|
+
required: ["artifactId"],
|
|
104
|
+
additionalProperties: false,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** True when `name` is one of our engine's artifact tools. */
|
|
108
|
+
export function isArtifactTool(name) {
|
|
109
|
+
return name === READ_ARTIFACT_TOOL || name === SAVE_ARTIFACT_TOOL;
|
|
110
|
+
}
|
|
111
|
+
const HARNESS = Object.freeze({ kind: "harness" });
|
|
112
|
+
/**
|
|
113
|
+
* The artifact tools our engine offers an agent: `save_artifact` when the session has a sandbox,
|
|
114
|
+
* and `read_artifact`, which takes `path` only then. Each runs here (`harness`).
|
|
115
|
+
*/
|
|
116
|
+
export function artifactToolsFor(sandboxed, options) {
|
|
117
|
+
return [
|
|
118
|
+
...(sandboxed
|
|
119
|
+
? [
|
|
120
|
+
{
|
|
121
|
+
name: SAVE_ARTIFACT_TOOL,
|
|
122
|
+
description: SAVE_DESCRIPTION,
|
|
123
|
+
inputSchema: SAVE_SCHEMA,
|
|
124
|
+
target: HARNESS,
|
|
125
|
+
},
|
|
126
|
+
]
|
|
127
|
+
: []),
|
|
128
|
+
{
|
|
129
|
+
name: READ_ARTIFACT_TOOL,
|
|
130
|
+
description: readDescription(options.pageBytes, sandboxed),
|
|
131
|
+
inputSchema: readSchema(options.pageBytes, sandboxed),
|
|
132
|
+
target: HARNESS,
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
/** What the model reads with the artifact tools, in their order. */
|
|
137
|
+
export function artifactInstructions(sandboxed) {
|
|
138
|
+
return [...(sandboxed ? [ARTIFACTS_INSTRUCTIONS] : []), READ_ARTIFACT_INSTRUCTIONS];
|
|
139
|
+
}
|
|
140
|
+
const failed = (code, message) => ({
|
|
141
|
+
kind: "failed",
|
|
142
|
+
code,
|
|
143
|
+
message,
|
|
144
|
+
});
|
|
145
|
+
/** Runs a call of one of our engine's artifact tools, as its outcome. */
|
|
146
|
+
export async function runArtifactTool(effect, context) {
|
|
147
|
+
try {
|
|
148
|
+
return effect.toolName === SAVE_ARTIFACT_TOOL
|
|
149
|
+
? await save(effect, context)
|
|
150
|
+
: await read(effect, context);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
// The seam's refusals are the model's to see; anything else is the run's.
|
|
154
|
+
if (error instanceof HarnessApiError) {
|
|
155
|
+
if (error.code === "not_found")
|
|
156
|
+
return failed("artifact.not_found", error.message);
|
|
157
|
+
if (error.code === "limit_exceeded")
|
|
158
|
+
return failed("artifact.too_large", error.message);
|
|
159
|
+
if (error.code === "invalid")
|
|
160
|
+
return failed("artifact.invalid_input", error.message);
|
|
161
|
+
}
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Whether the run's session has a sandbox (`turn.start.agent.sandbox`): its agents share it. */
|
|
166
|
+
function sandboxed(context) {
|
|
167
|
+
return context.files !== undefined && context.run.start.agent?.sandbox !== undefined;
|
|
168
|
+
}
|
|
169
|
+
function about(artifact) {
|
|
170
|
+
return {
|
|
171
|
+
artifactId: artifact.artifactId,
|
|
172
|
+
version: artifact.version,
|
|
173
|
+
name: artifact.name,
|
|
174
|
+
contentType: artifact.contentType,
|
|
175
|
+
size: artifact.size,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const isCount = (value, min) => typeof value === "number" && Number.isSafeInteger(value) && value >= min;
|
|
179
|
+
async function read(effect, context) {
|
|
180
|
+
const { channel, run, pageBytes, signal } = context;
|
|
181
|
+
const input = (effect.input ?? {});
|
|
182
|
+
const artifactId = typeof input.artifactId === "string" ? input.artifactId : "";
|
|
183
|
+
const wanted = input.version;
|
|
184
|
+
const offset = input.offset ?? 0;
|
|
185
|
+
const length = input.length ?? pageBytes;
|
|
186
|
+
const path = typeof input.path === "string" && input.path !== "" ? input.path : undefined;
|
|
187
|
+
if (artifactId === "" ||
|
|
188
|
+
(wanted !== undefined && !isCount(wanted, 1)) ||
|
|
189
|
+
!isCount(offset, 0) ||
|
|
190
|
+
!isCount(length, 1) ||
|
|
191
|
+
length > pageBytes)
|
|
192
|
+
return failed("artifact.invalid_input", `Give artifactId, a version of 1 or more if any, an offset of 0 or more, and a length of 1 to ${pageBytes}.`);
|
|
193
|
+
const chunkBytes = run.start.limits.chunkBytes;
|
|
194
|
+
const version = typeof wanted === "number" ? { version: wanted } : {};
|
|
195
|
+
if (path !== undefined) {
|
|
196
|
+
if (!sandboxed(context))
|
|
197
|
+
return failed("artifact.no_sandbox", "This session has no sandbox to copy an artifact into.");
|
|
198
|
+
const opened = await readArtifact(channel, {
|
|
199
|
+
runId: run.runId,
|
|
200
|
+
artifactId,
|
|
201
|
+
...version,
|
|
202
|
+
chunkBytes,
|
|
203
|
+
signal,
|
|
204
|
+
});
|
|
205
|
+
if (opened.artifact.kind !== "file")
|
|
206
|
+
return failed("artifact.not_text", `${opened.artifact.name} is a folder; read_artifact reads a file.`);
|
|
207
|
+
const written = await context.files.write(run, path, opened.chunks(), signal);
|
|
208
|
+
if (written.kind === "failed")
|
|
209
|
+
return written;
|
|
210
|
+
return { kind: "completed", output: { ...about(opened.artifact), path: written.path } };
|
|
211
|
+
}
|
|
212
|
+
const answer = await channel.request("artifact.read", { runId: run.runId, artifactId, ...version, offset, length: Math.min(length, chunkBytes) }, signal);
|
|
213
|
+
const { artifact } = answer;
|
|
214
|
+
if (artifact.kind !== "file")
|
|
215
|
+
return failed("artifact.not_text", `${artifact.name} is a folder; read_artifact reads a file.`);
|
|
216
|
+
if (!isText(artifact.contentType)) {
|
|
217
|
+
// A file the turn's model reads goes to it beside the result (protocol 12); for one it does
|
|
218
|
+
// not, the model is told why (protocol 15).
|
|
219
|
+
const file = {
|
|
220
|
+
mediaType: artifact.contentType,
|
|
221
|
+
reference: {
|
|
222
|
+
artifactId: artifact.artifactId,
|
|
223
|
+
version: artifact.version,
|
|
224
|
+
name: artifact.name,
|
|
225
|
+
size: artifact.size,
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const view = fileView(file, run.start.model, run.start.limits);
|
|
229
|
+
if (view.kind === "note")
|
|
230
|
+
return failed("artifact.unsupported", `${artifact.name} is ${essence(artifact.contentType)}: ${view.why}.`);
|
|
231
|
+
return { kind: "completed", output: about(artifact), files: [file] };
|
|
232
|
+
}
|
|
233
|
+
if (offset > artifact.size || (offset === artifact.size && artifact.size > 0))
|
|
234
|
+
return failed("artifact.invalid_input", `Offset ${offset} is past the end of ${artifact.name}, which is ${artifact.size} bytes.`);
|
|
235
|
+
const bytes = fromBase64(answer.base64);
|
|
236
|
+
const page = textPage(bytes, offset + bytes.byteLength >= artifact.size, pageBytes);
|
|
237
|
+
const next = offset + page.bytes;
|
|
238
|
+
return {
|
|
239
|
+
kind: "completed",
|
|
240
|
+
output: {
|
|
241
|
+
...about(artifact),
|
|
242
|
+
offset,
|
|
243
|
+
content: page.text,
|
|
244
|
+
...(next < artifact.size ? { nextOffset: next } : {}),
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async function save(effect, context) {
|
|
249
|
+
const { channel, run, signal } = context;
|
|
250
|
+
const input = (effect.input ?? {});
|
|
251
|
+
const path = typeof input.path === "string" && input.path !== "" ? input.path : undefined;
|
|
252
|
+
const content = typeof input.content === "string" ? input.content : undefined;
|
|
253
|
+
if ((path === undefined) === (content === undefined))
|
|
254
|
+
return failed("artifact.invalid_input", "Give path or content, not both.");
|
|
255
|
+
if (path !== undefined && !sandboxed(context))
|
|
256
|
+
return failed("artifact.no_sandbox", "This session has no sandbox to read a path from.");
|
|
257
|
+
const artifactId = typeof input.artifactId === "string" ? input.artifactId : undefined;
|
|
258
|
+
const name = typeof input.name === "string" && input.name !== ""
|
|
259
|
+
? input.name
|
|
260
|
+
: path !== undefined
|
|
261
|
+
? basename(path)
|
|
262
|
+
: "artifact.txt";
|
|
263
|
+
let contentType;
|
|
264
|
+
if (typeof input.contentType === "string") {
|
|
265
|
+
const declared = normalizeContentType(input.contentType);
|
|
266
|
+
if (declared === undefined)
|
|
267
|
+
return failed("artifact.invalid_input", `${input.contentType} is not a media type.`);
|
|
268
|
+
contentType = declared;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
contentType = contentTypeFor(name);
|
|
272
|
+
if (content !== undefined && contentType === DEFAULT_CONTENT_TYPE)
|
|
273
|
+
contentType = "text/plain; charset=utf-8";
|
|
274
|
+
}
|
|
275
|
+
const options = {
|
|
276
|
+
runId: run.runId,
|
|
277
|
+
// The call is the write: a repeat of it answers the version it wrote.
|
|
278
|
+
key: effect.effectId,
|
|
279
|
+
effectId: effect.effectId,
|
|
280
|
+
...(artifactId !== undefined ? { artifactId } : { name }),
|
|
281
|
+
contentType,
|
|
282
|
+
chunkBytes: run.start.limits.chunkBytes,
|
|
283
|
+
signal,
|
|
284
|
+
};
|
|
285
|
+
if (content !== undefined)
|
|
286
|
+
return saved(await writeArtifactBytes(channel, options, new TextEncoder().encode(content)));
|
|
287
|
+
const upload = await openArtifactWrite(channel, options);
|
|
288
|
+
if (upload.kind === "written")
|
|
289
|
+
return saved(upload);
|
|
290
|
+
try {
|
|
291
|
+
const file = await context.files.read(run, path, upload.maxBytes, signal);
|
|
292
|
+
if (file.kind !== "read") {
|
|
293
|
+
await upload.abort().catch(() => undefined);
|
|
294
|
+
return file.kind === "missing"
|
|
295
|
+
? failed("artifact.not_found", `${file.path} does not exist in the sandbox.`)
|
|
296
|
+
: file;
|
|
297
|
+
}
|
|
298
|
+
await upload.write(file.bytes);
|
|
299
|
+
return saved(await upload.commit());
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
await upload.abort().catch(() => undefined);
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function saved({ artifact }) {
|
|
307
|
+
return {
|
|
308
|
+
kind: "completed",
|
|
309
|
+
output: {
|
|
310
|
+
artifactId: artifact.artifactId,
|
|
311
|
+
version: artifact.version,
|
|
312
|
+
name: artifact.name,
|
|
313
|
+
contentType: artifact.contentType,
|
|
314
|
+
size: artifact.size,
|
|
315
|
+
sha256: artifact.sha256,
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/** The last segment of a POSIX path. */
|
|
320
|
+
function basename(path) {
|
|
321
|
+
const trimmed = path.replace(/\/+$/, "");
|
|
322
|
+
return trimmed.slice(trimmed.lastIndexOf("/") + 1) || trimmed;
|
|
323
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What our engine adds to each agent of a session (protocol 15): the model-facing text and tools
|
|
3
|
+
* that come with the session's Environment and the agent's skills. The Runtime states facts in
|
|
4
|
+
* `turn.start` (the Environment, its sandbox's workspace, the agent's skills); our engine words
|
|
5
|
+
* them and defines the tools, in one order, which keeps the prompt's prefix stable:
|
|
6
|
+
*
|
|
7
|
+
* - instructions: the skills' usage and catalog; the sandbox note; the skills' folder note;
|
|
8
|
+
* the artifact notes;
|
|
9
|
+
* - tools: the six sandbox tools; the skill tools; `save_artifact`; `read_artifact`;
|
|
10
|
+
* `schedule_wake` when the session's version sets `runtime.selfWake` (track T4); and `memory`
|
|
11
|
+
* when it sets `harness.memory` (track T5).
|
|
12
|
+
*
|
|
13
|
+
* They come after the agent's own and before the session's MCP tools (`mcpToolSet`), and are
|
|
14
|
+
* not hashed.
|
|
15
|
+
*/
|
|
16
|
+
import type { AgentManifest } from "@nylorun/core/define";
|
|
17
|
+
import type { TurnAgent, TurnMemory } from "@nylorun/core/harness-api";
|
|
18
|
+
import type { DurableSessionTool } from "../run/durable.js";
|
|
19
|
+
import { type SandboxToolLimits } from "./sandbox.js";
|
|
20
|
+
import type { McpToolConfig } from "./mcp.js";
|
|
21
|
+
import type { ToolResultConfig } from "./results.js";
|
|
22
|
+
import type { OutputsConfig } from "./outputs.js";
|
|
23
|
+
/** The harness's configuration of our engine's own tools. */
|
|
24
|
+
export interface EngineToolOptions {
|
|
25
|
+
/** The most bytes one `read_artifact` page holds, escaped as JSON. */
|
|
26
|
+
readonly artifactPageBytes: number;
|
|
27
|
+
/** The sandbox tools' limits: their schemas' maxima and defaults. */
|
|
28
|
+
readonly sandboxTools: SandboxToolLimits;
|
|
29
|
+
/** The most bytes of one skill file the skill tools read into the model's context. */
|
|
30
|
+
readonly skillReadBytes: number;
|
|
31
|
+
/** How our engine names and defers the session's MCP tools, and `tool_search`'s limits. */
|
|
32
|
+
readonly mcpTools: McpToolConfig;
|
|
33
|
+
/** How our engine shapes tool results to fit the record: inline caps, previews, step budget. */
|
|
34
|
+
readonly toolResults: ToolResultConfig;
|
|
35
|
+
/** Where our engine finds a turn's outputs folder, and the most bytes it saves of it. */
|
|
36
|
+
readonly outputs: OutputsConfig;
|
|
37
|
+
}
|
|
38
|
+
/** Our engine's tools for one agent, and the text the model reads with them, in order. */
|
|
39
|
+
export interface EngineTools {
|
|
40
|
+
readonly tools: readonly DurableSessionTool[];
|
|
41
|
+
readonly instructions: readonly string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Our engine's tools and text for `agent`, in a session on Agent `runsOn` (`turn.start.agent`)
|
|
45
|
+
* with its memory (`turn.start.memory`). `memory.root` is false for an agent used as a tool,
|
|
46
|
+
* which may only view memory.
|
|
47
|
+
*/
|
|
48
|
+
export declare function engineToolsFor(agent: Pick<AgentManifest, "skills">, runsOn: TurnAgent | undefined, options: Pick<EngineToolOptions, "artifactPageBytes" | "sandboxTools" | "skillReadBytes">, memory?: Pick<TurnMemory, "access" | "instructions"> & {
|
|
49
|
+
readonly root: boolean;
|
|
50
|
+
}): EngineTools;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { artifactInstructions, artifactToolsFor } from "./artifacts.js";
|
|
2
|
+
import { sandboxInstructions, sandboxTools } from "./sandbox.js";
|
|
3
|
+
import { skillCatalogText, skillFolderNote, skillTools } from "./skills.js";
|
|
4
|
+
import { wakeTool } from "./wake.js";
|
|
5
|
+
import { memoryTool } from "./memory.js";
|
|
6
|
+
/**
|
|
7
|
+
* Our engine's tools and text for `agent`, in a session on Agent `runsOn` (`turn.start.agent`)
|
|
8
|
+
* with its memory (`turn.start.memory`). `memory.root` is false for an agent used as a tool,
|
|
9
|
+
* which may only view memory.
|
|
10
|
+
*/
|
|
11
|
+
export function engineToolsFor(agent, runsOn, options, memory) {
|
|
12
|
+
const skills = Object.values(agent.skills ?? {});
|
|
13
|
+
const workspace = runsOn?.sandbox?.workspace;
|
|
14
|
+
const sandboxed = workspace !== undefined;
|
|
15
|
+
const instructions = [];
|
|
16
|
+
if (skills.length > 0)
|
|
17
|
+
instructions.push(...skillCatalogText(skills));
|
|
18
|
+
if (sandboxed) {
|
|
19
|
+
instructions.push(sandboxInstructions(workspace));
|
|
20
|
+
if (skills.length > 0)
|
|
21
|
+
instructions.push(skillFolderNote(skills));
|
|
22
|
+
}
|
|
23
|
+
instructions.push(...artifactInstructions(sandboxed));
|
|
24
|
+
// A session that may book its own next turn (track T4, D77).
|
|
25
|
+
const wake = runsOn?.selfWake === true ? wakeTool() : undefined;
|
|
26
|
+
if (wake)
|
|
27
|
+
instructions.push(wake.instructions);
|
|
28
|
+
// The Agent's memory (track T5, D72).
|
|
29
|
+
const remember = memory ? memoryTool(memory, memory.root) : undefined;
|
|
30
|
+
if (remember)
|
|
31
|
+
instructions.push(remember.instructions);
|
|
32
|
+
return {
|
|
33
|
+
tools: [
|
|
34
|
+
...(sandboxed ? sandboxTools(options.sandboxTools, workspace) : []),
|
|
35
|
+
...skillTools(skills),
|
|
36
|
+
...artifactToolsFor(sandboxed, { pageBytes: options.artifactPageBytes }),
|
|
37
|
+
...(wake ? [wake.tool] : []),
|
|
38
|
+
...(remember ? [remember.tool] : []),
|
|
39
|
+
],
|
|
40
|
+
instructions,
|
|
41
|
+
};
|
|
42
|
+
}
|