@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.
Files changed (75) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/HOST_CONTRACT.md +15 -13
  3. package/README.md +3 -1
  4. package/dist/api/executors.d.ts +79 -11
  5. package/dist/api/harness.d.ts +83 -2
  6. package/dist/api/harness.js +43 -4
  7. package/dist/api/host.d.ts +30 -2
  8. package/dist/api/host.js +156 -8
  9. package/dist/api/index.d.ts +21 -6
  10. package/dist/api/index.js +19 -4
  11. package/dist/api/run.d.ts +3 -0
  12. package/dist/api/run.js +91 -16
  13. package/dist/flow/context.d.ts +3 -0
  14. package/dist/flow/context.js +2 -1
  15. package/dist/flow/engine.d.ts +2 -0
  16. package/dist/flow/engine.js +10 -7
  17. package/dist/flow/limits.d.ts +8 -5
  18. package/dist/flow/limits.js +11 -7
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.js +9 -0
  21. package/dist/loop/compaction/config.d.ts +39 -0
  22. package/dist/loop/compaction/config.js +29 -0
  23. package/dist/loop/compaction/index.d.ts +18 -8
  24. package/dist/loop/compaction/index.js +57 -66
  25. package/dist/loop/config.d.ts +20 -0
  26. package/dist/loop/config.js +17 -0
  27. package/dist/loop/delegation.d.ts +3 -0
  28. package/dist/loop/delegation.js +14 -10
  29. package/dist/loop/invocation.d.ts +3 -0
  30. package/dist/loop/invocation.js +2 -0
  31. package/dist/loop/model/files.d.ts +77 -0
  32. package/dist/loop/model/files.js +168 -0
  33. package/dist/loop/model/json-answer.d.ts +26 -0
  34. package/dist/loop/model/json-answer.js +125 -0
  35. package/dist/loop/model/project.d.ts +14 -4
  36. package/dist/loop/model/project.js +70 -41
  37. package/dist/loop/options.js +11 -3
  38. package/dist/loop/run.js +15 -5
  39. package/dist/loop/state.d.ts +6 -1
  40. package/dist/loop/state.js +19 -0
  41. package/dist/loop/step/canonicalize.d.ts +11 -2
  42. package/dist/loop/step/canonicalize.js +21 -5
  43. package/dist/loop/step/resolve.js +1 -0
  44. package/dist/loop/step/run.js +20 -3
  45. package/dist/loop/step/runtime.d.ts +6 -0
  46. package/dist/loop/step/seal.js +8 -2
  47. package/dist/loop/step/step-context.js +1 -1
  48. package/dist/run/durable.d.ts +58 -3
  49. package/dist/run/durable.js +104 -33
  50. package/dist/tools/artifacts.d.ts +33 -0
  51. package/dist/tools/artifacts.js +323 -0
  52. package/dist/tools/engine-tools.d.ts +50 -0
  53. package/dist/tools/engine-tools.js +42 -0
  54. package/dist/tools/mcp.d.ts +92 -0
  55. package/dist/tools/mcp.js +240 -0
  56. package/dist/tools/memory.d.ts +16 -0
  57. package/dist/tools/memory.js +140 -0
  58. package/dist/tools/outputs.d.ts +23 -0
  59. package/dist/tools/outputs.js +193 -0
  60. package/dist/tools/results.d.ts +108 -0
  61. package/dist/tools/results.js +329 -0
  62. package/dist/tools/sandbox.d.ts +35 -0
  63. package/dist/tools/sandbox.js +129 -0
  64. package/dist/tools/skills.d.ts +94 -0
  65. package/dist/tools/skills.js +243 -0
  66. package/dist/tools/text-page.d.ts +20 -0
  67. package/dist/tools/text-page.js +64 -0
  68. package/dist/tools/tool-search.d.ts +56 -0
  69. package/dist/tools/tool-search.js +174 -0
  70. package/dist/tools/trace.d.ts +13 -0
  71. package/dist/tools/trace.js +34 -0
  72. package/dist/tools/wake.d.ts +13 -0
  73. package/dist/tools/wake.js +33 -0
  74. package/dist/types/execution.d.ts +35 -1
  75. package/package.json +7 -7
@@ -0,0 +1,174 @@
1
+ /**
2
+ * Deferred MCP tools (R2b C10, Q20), run by our engine: `tool_search` finds an agent's deferred
3
+ * tools, ranked with BM25 over their names and descriptions, and returns each match's name,
4
+ * description and `inputSchema`; `tool_call` runs one by name, as that tool's own call
5
+ * (`deferredToolCall` in `run/durable.ts`). `tool_search` is our engine's tool (`harness`): it
6
+ * reads only the run's MCP tools (`McpToolSet`), so the same query finds the same tools while the
7
+ * session's layout stands; a tool the turn disables is never there.
8
+ */
9
+ import { z } from "zod";
10
+ import { normalizedSchemasFor, tool, ToolError } from "@nylorun/core/define";
11
+ /** Finds deferred tools by a query (BM25 over their names and descriptions). */
12
+ export const TOOL_SEARCH_TOOL = "tool_search";
13
+ /** Runs a deferred tool by name, as a call of that tool. */
14
+ export const TOOL_CALL_TOOL = "tool_call";
15
+ /** BM25's term saturation and length normalization, the usual values. */
16
+ const K1 = 1.2;
17
+ const B = 0.75;
18
+ const HARNESS = Object.freeze({ kind: "harness" });
19
+ async function runsInHarness() {
20
+ throw new ToolError("tools.runtime-only", "tool_search and tool_call run in Nylorun's harness. Connect the agent to a Runtime to use them.");
21
+ }
22
+ /**
23
+ * `tool_search` (ours, `harness`) and `tool_call`, as our engine gives them to an agent with
24
+ * deferred tools. Their schemas reach the model; change them deliberately.
25
+ */
26
+ export function deferredToolsTools(config) {
27
+ return [searchTool(config), callTool()].map((item, index) => {
28
+ const schemas = normalizedSchemasFor(item);
29
+ const manifest = {
30
+ name: item.name,
31
+ ...(item.description === undefined ? {} : { description: item.description }),
32
+ inputSchema: schemas.inputSchema.jsonSchema,
33
+ };
34
+ // `tool_call` makes the effect of the tool it names; it is never one itself.
35
+ return index === 0 ? { ...manifest, target: HARNESS } : manifest;
36
+ });
37
+ }
38
+ /**
39
+ * The note the model reads beside `tool_search`: each server with deferred tools, how many, and
40
+ * its own instructions cut to `config.serverInstructionsMaxChars`.
41
+ */
42
+ export function deferredToolsNote(servers, config) {
43
+ const max = config.serverInstructionsMaxChars;
44
+ const lines = servers.map((server) => {
45
+ const head = `- ${server.serverName}: ${server.tools} tool${server.tools === 1 ? "" : "s"}`;
46
+ const text = server.instructions?.trim();
47
+ if (!text)
48
+ return head;
49
+ const cut = text.length > max ? `${text.slice(0, max)}…` : text;
50
+ return `${head}. The server says: ${cut}`;
51
+ });
52
+ return [
53
+ "Some tools of these MCP servers are not in your tool list. Find them with tool_search (a few words of what you need), then run one with tool_call, passing its name and arguments that match its inputSchema.",
54
+ ...lines,
55
+ ].join("\n");
56
+ }
57
+ function searchTool(config) {
58
+ return tool({
59
+ name: TOOL_SEARCH_TOOL,
60
+ description: "Search the tools not in your tool list by name and description. Returns the best matches, each with its name, description and inputSchema. Run one with tool_call.",
61
+ inputSchema: z.object({
62
+ query: z.string().min(1).describe("Words of what the tool does, or of its name."),
63
+ limit: z
64
+ .number()
65
+ .int()
66
+ .min(1)
67
+ .max(config.toolSearchMaxLimit)
68
+ .optional()
69
+ .describe(`How many tools to return. Default ${config.toolSearchDefaultLimit}.`),
70
+ }),
71
+ effects: "read",
72
+ execute: runsInHarness,
73
+ });
74
+ }
75
+ function callTool() {
76
+ return tool({
77
+ name: TOOL_CALL_TOOL,
78
+ description: "Run a tool tool_search found, by its name. arguments must match that tool's inputSchema; the result is the tool's own.",
79
+ inputSchema: z.object({
80
+ name: z.string().min(1).describe("The tool's name, as tool_search returned it."),
81
+ arguments: z
82
+ .record(z.string(), z.unknown())
83
+ .optional()
84
+ .describe("The tool's arguments, matching its inputSchema. Default {}."),
85
+ }),
86
+ effects: "write",
87
+ execute: runsInHarness,
88
+ });
89
+ }
90
+ /** True when `effect` calls our engine's `tool_search`. */
91
+ export function isToolSearch(effect) {
92
+ return effect.target?.kind === "harness" && effect.toolName === TOOL_SEARCH_TOOL;
93
+ }
94
+ /** Runs `tool_search` over `deferred`, the deferred tools of the call's agent. */
95
+ export function runToolSearch(effect, deferred, config) {
96
+ const input = (effect.input ?? {});
97
+ const query = typeof input.query === "string" ? input.query : "";
98
+ const limit = typeof input.limit === "number" && Number.isSafeInteger(input.limit)
99
+ ? Math.min(Math.max(input.limit, 1), config.toolSearchMaxLimit)
100
+ : config.toolSearchDefaultLimit;
101
+ if (query.trim() === "")
102
+ return {
103
+ kind: "failed",
104
+ code: "tools.invalid_input",
105
+ message: "Give a query: words of what the tool does.",
106
+ };
107
+ const found = rank(deferred, query).slice(0, limit);
108
+ return {
109
+ kind: "completed",
110
+ output: {
111
+ tools: found.map((tool) => ({
112
+ name: tool.name,
113
+ ...(tool.description === undefined ? {} : { description: tool.description }),
114
+ inputSchema: tool.inputSchema,
115
+ })),
116
+ ...(found.length === 0
117
+ ? {
118
+ message: `No tool matches '${query}'. Try other words; ${deferred.length} tools are searchable.`,
119
+ }
120
+ : {}),
121
+ },
122
+ };
123
+ }
124
+ /**
125
+ * Lower-case words of `text`: `camelCase`, `snake_case` and `kebab-case` split into words, and a
126
+ * plural `s` dropped (`issues` finds `issue`).
127
+ */
128
+ export function tokens(text) {
129
+ return text
130
+ .replace(/([a-z0-9])([A-Z])/gu, "$1 $2")
131
+ .toLowerCase()
132
+ .split(/[^\p{L}\p{N}]+/u)
133
+ .filter((word) => word.length > 0)
134
+ .map((word) => word.length > 3 && word.endsWith("s") && !word.endsWith("ss") ? word.slice(0, -1) : word);
135
+ }
136
+ /**
137
+ * The tools that match `query`, best first, by BM25 over each tool's name (counted twice, with
138
+ * the server's own name for it) and description. A tool no word of the query is in is left out;
139
+ * ties keep the given order.
140
+ */
141
+ export function rank(tools, query) {
142
+ const words = [...new Set(tokens(query))];
143
+ if (words.length === 0 || tools.length === 0)
144
+ return [];
145
+ const docs = tools.map((tool) => {
146
+ const own = tool.target?.kind === "mcp" ? tool.target.tool : "";
147
+ const name = [...tokens(tool.name), ...tokens(own)];
148
+ const terms = [...name, ...name, ...tokens(tool.description ?? "")];
149
+ const counts = new Map();
150
+ for (const term of terms)
151
+ counts.set(term, (counts.get(term) ?? 0) + 1);
152
+ return { tool, counts, length: terms.length };
153
+ });
154
+ const average = docs.reduce((sum, doc) => sum + doc.length, 0) / docs.length || 1;
155
+ const idf = new Map(words.map((word) => {
156
+ const df = docs.filter((doc) => doc.counts.has(word)).length;
157
+ return [word, Math.log(1 + (docs.length - df + 0.5) / (df + 0.5))];
158
+ }));
159
+ return docs
160
+ .map((doc, order) => {
161
+ let score = 0;
162
+ for (const word of words) {
163
+ const tf = doc.counts.get(word) ?? 0;
164
+ if (tf === 0)
165
+ continue;
166
+ score +=
167
+ idf.get(word) * ((tf * (K1 + 1)) / (tf + K1 * (1 - B + (B * doc.length) / average)));
168
+ }
169
+ return { tool: doc.tool, score, order };
170
+ })
171
+ .filter((item) => item.score > 0)
172
+ .sort((a, b) => b.score - a.score || a.order - b.order)
173
+ .map((item) => item.tool);
174
+ }
@@ -0,0 +1,13 @@
1
+ import { type AgentManifest } from "@nylorun/core/define";
2
+ import type { EffectTarget } from "@nylorun/core/harness-api";
3
+ /**
4
+ * The skill of `manifest` a call of our engine's tool ran, which the effect names (`skill`):
5
+ * `load_skill` and `read_skill_resource` name it in their input. A sandbox call (`bash`, `read`,
6
+ * `grep`, `glob`, …) is matched on the skills' folder, `/skills/<name>@<hash>/` (or
7
+ * `/skills/<name>/`), in its input's text: exact for a path argument, and for `bash` only when the
8
+ * command names the folder. The first skill of the agent the input names wins.
9
+ */
10
+ export declare function skillOfToolCall(manifest: Pick<AgentManifest, "skills"> | undefined, tool: {
11
+ readonly name: string;
12
+ readonly target?: EffectTarget;
13
+ }, input: unknown): string | undefined;
@@ -0,0 +1,34 @@
1
+ import { SKILLS_MOUNT } from "@nylorun/core/define";
2
+ import { isSkillTool } from "./skills.js";
3
+ const MOUNT = new RegExp(`${SKILLS_MOUNT}/([^/@\\s"'\`]+)(?:@[0-9a-f]{12})?/`, "g");
4
+ /**
5
+ * The skill of `manifest` a call of our engine's tool ran, which the effect names (`skill`):
6
+ * `load_skill` and `read_skill_resource` name it in their input. A sandbox call (`bash`, `read`,
7
+ * `grep`, `glob`, …) is matched on the skills' folder, `/skills/<name>@<hash>/` (or
8
+ * `/skills/<name>/`), in its input's text: exact for a path argument, and for `bash` only when the
9
+ * command names the folder. The first skill of the agent the input names wins.
10
+ */
11
+ export function skillOfToolCall(manifest, tool, input) {
12
+ const skills = manifest?.skills;
13
+ if (!skills)
14
+ return undefined;
15
+ if (tool.target?.kind === "harness" && isSkillTool(tool.name)) {
16
+ const name = input?.name;
17
+ return typeof name === "string" && skills[name] !== undefined ? name : undefined;
18
+ }
19
+ if (tool.target?.kind !== "sandbox")
20
+ return undefined;
21
+ for (const text of strings(input))
22
+ for (const match of text.matchAll(MOUNT))
23
+ if (skills[match[1]] !== undefined)
24
+ return match[1];
25
+ return undefined;
26
+ }
27
+ /** The string values of a tool's input, one level deep: its path, pattern or command. */
28
+ function strings(input) {
29
+ if (typeof input === "string")
30
+ return [input];
31
+ if (!input || typeof input !== "object")
32
+ return [];
33
+ return Object.values(input).filter((value) => typeof value === "string");
34
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * `schedule_wake` (track T4, D77): the agent books its own session's next turn, "check the deploy
3
+ * again in two hours". Offered when the version the session runs sets `runtime.selfWake`
4
+ * (`turn.start.agent.selfWake`). The Runtime serves it (a `runtime` effect): it writes the booking
5
+ * in the call's own transaction, keyed by the call, so a replayed turn books once. A session holds
6
+ * one booking; a new one replaces it. The booked turn starts with the note as its message.
7
+ */
8
+ import type { DurableSessionTool } from "../run/durable.js";
9
+ /** The tool, and the text the model reads with it. */
10
+ export declare function wakeTool(): {
11
+ tool: DurableSessionTool;
12
+ instructions: string;
13
+ };
@@ -0,0 +1,33 @@
1
+ const RUNTIME_WAKE = { kind: "runtime", operation: "schedule_wake" };
2
+ /** The tool, and the text the model reads with it. */
3
+ export function wakeTool() {
4
+ return {
5
+ tool: {
6
+ name: "schedule_wake",
7
+ description: "Book this session's next turn for later, when nobody may be here to ask: give `in` (a duration such as 30m, 2h or 1d) or `at` (an RFC 3339 time with an offset), and a `note` that will be your message at that turn. A session holds one booking: a new one replaces it, and `cancel: true` removes it. Returns when it is booked for.",
8
+ inputSchema: {
9
+ type: "object",
10
+ properties: {
11
+ in: {
12
+ type: "string",
13
+ pattern: "^[1-9][0-9]*(s|m|h|d)$",
14
+ description: "How long from now: 30m, 2h, 1d.",
15
+ },
16
+ at: {
17
+ type: "string",
18
+ description: "An RFC 3339 time with an offset, such as 2026-11-01T09:00:00+05:30.",
19
+ },
20
+ note: {
21
+ type: "string",
22
+ minLength: 1,
23
+ description: "What to do then, in your own words: your message at that turn.",
24
+ },
25
+ cancel: { type: "boolean", description: "Remove the session's booking instead." },
26
+ },
27
+ additionalProperties: false,
28
+ },
29
+ target: RUNTIME_WAKE,
30
+ },
31
+ instructions: "You can book this session's next turn with schedule_wake, to check on something later or keep a routine going. Booking again replaces the booking you have.",
32
+ };
33
+ }
@@ -1,9 +1,19 @@
1
1
  import type { ModelAdapter } from "@nylorun/core/define";
2
+ import type { ModelDescriptor } from "@nylorun/core/harness-api";
3
+ import type { FileContext } from "../loop/model/files.js";
2
4
  import type { InputEvent, MessageInput, TranscriptEntry } from "@nylorun/core/define";
3
5
  import type { ObserveEvent } from "@nylorun/core/define";
4
6
  import type { JsonObject, JsonValue, Tripwire } from "@nylorun/core/define";
5
7
  import type { AgentRef, RequiredInteraction, ToolExecutionResume, ToolOutcome, ToolResult } from "@nylorun/core/define";
6
- export type ExecutionInput = MessageInput | InputEvent | {
8
+ export type ExecutionInput = MessageInput | InputEvent
9
+ /**
10
+ * Messages that waited in the session's queue while a turn ran (protocol 17): one turn takes
11
+ * them all, each its own user message, in order.
12
+ */
13
+ | {
14
+ readonly kind: "messages";
15
+ readonly messages: readonly MessageInput[];
16
+ } | {
7
17
  readonly kind: "continue";
8
18
  } | {
9
19
  readonly kind: "settle";
@@ -83,6 +93,20 @@ export interface ExecutionState {
83
93
  * Runtime persists this with the checkpoint.
84
94
  */
85
95
  readonly state?: JsonObject;
96
+ /**
97
+ * Which of the session's MCP tools our engine defers (R2b C10, `McpLayout`), decided once for
98
+ * the facts whose hash it names and kept while they stay the same.
99
+ */
100
+ readonly mcpLayout?: {
101
+ readonly factsHash: string;
102
+ readonly deferred: readonly string[];
103
+ };
104
+ /**
105
+ * Text our engine keeps at the head of the conversation, after the instructions: the Agent's
106
+ * memory index (track T5), pinned at the session's first turn so the prompt's prefix stays
107
+ * the same. A compaction drops it, and the next segment pins the index of that moment.
108
+ */
109
+ readonly pinned?: readonly string[];
86
110
  }
87
111
  export type ExecutionEvent = (ObserveEvent | {
88
112
  readonly type: "execution.completed" | "execution.paused" | "execution.cancelled" | "execution.failed" | "observation.failed";
@@ -102,6 +126,16 @@ export interface RunOptions<Info = unknown> {
102
126
  readonly signal?: AbortSignal;
103
127
  readonly onEvent?: (event: ExecutionEvent) => void | Promise<void>;
104
128
  readonly record?: (state: ExecutionState) => void | Promise<void>;
129
+ /**
130
+ * The model the calls use (`turn.start.model`, protocol 15): its window budgets the prompt
131
+ * before any answer reports one.
132
+ */
133
+ readonly model?: ModelDescriptor;
134
+ /**
135
+ * How the turn's files reach the model (`representFiles`): the model, the Runtime limits and
136
+ * the run's artifact reads. Without it every file goes as its media part.
137
+ */
138
+ readonly files?: FileContext;
105
139
  }
106
140
  export type RunResult<Output = string> = {
107
141
  readonly status: "completed";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nylorun/harness",
3
- "version": "0.27.1-beta",
3
+ "version": "0.29.0-beta",
4
4
  "description": "Nylorun's TypeScript agent execution engine. See github.com/nylorun/agents.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -69,17 +69,17 @@
69
69
  "eval:compaction": "vitest run test/compaction-eval.test.ts"
70
70
  },
71
71
  "devDependencies": {
72
- "@types/node": "^26.6.4",
72
+ "@types/node": "^26.6.5",
73
73
  "@typescript/native": "npm:typescript@^7.0.2",
74
- "prettier": "^3.9.9",
74
+ "prettier": "^3.9.10",
75
75
  "typescript": "npm:@typescript/typescript6@^6.0.2",
76
- "vitest": "^5.0.2",
77
- "zod": "^4.6.5"
76
+ "vitest": "^5.0.4",
77
+ "zod": "^4.6.7"
78
78
  },
79
79
  "dependencies": {
80
- "@nylorun/core": "0.22.0-beta"
80
+ "@nylorun/core": "0.24.0-beta"
81
81
  },
82
82
  "peerDependencies": {
83
- "zod": "^4.6.5"
83
+ "zod": "^4.6.7"
84
84
  }
85
85
  }