@zenera/neo 1.1.54 → 1.1.56
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/kernel.d.ts +11 -1
- package/dist/kernel.js +24 -1
- package/dist/memory/files.d.ts +37 -3
- package/dist/memory/files.js +26 -14
- package/dist/memory/graph.d.ts +22 -0
- package/dist/memory/graph.js +72 -7
- package/dist/memory/grep.js +3 -2
- package/dist/memory/index.d.ts +17 -4
- package/dist/memory/index.js +112 -31
- package/dist/memory/merge.js +29 -17
- package/dist/memory/migrate.d.ts +14 -0
- package/dist/memory/migrate.js +112 -0
- package/dist/memory/render.js +2 -1
- package/dist/memory/store.d.ts +36 -27
- package/dist/memory/store.js +409 -126
- package/dist/memory/tools.js +70 -9
- package/dist/memory/types.d.ts +1 -1
- package/dist/memory/types.js +1 -1
- package/dist/project/config.d.ts +1 -24
- package/dist/project/config.js +9 -4
- package/dist/project/load.d.ts +6 -8
- package/dist/project/load.js +3 -4
- package/dist/runner.d.ts +23 -6
- package/dist/runner.js +72 -8
- package/dist/services.d.ts +8 -6
- package/dist/services.js +7 -6
- package/dist/state.d.ts +8 -2
- package/dist/tools/files.d.ts +28 -37
- package/dist/tools/files.js +106 -84
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/sandbox.d.ts +2 -269
- package/dist/tools/sandbox.js +10 -741
- package/dist/types.d.ts +42 -0
- package/dist/types.js +22 -0
- package/package.json +2 -1
- package/dist/tools/workspace.d.ts +0 -79
- package/dist/tools/workspace.js +0 -1470
package/dist/memory/tools.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
2
|
+
* The six tools an agent gets. `search` and `load` are split because finding
|
|
3
3
|
* is not reading — search returns stitched context the model never asked for,
|
|
4
4
|
* and counting that as use would poison recency. `grep` is separate from
|
|
5
5
|
* `search` because exactness is not a tuning of nearness: a ranking returns the
|
|
@@ -8,14 +8,17 @@
|
|
|
8
8
|
* building it with three calls leaves the graph half-formed if the model stops
|
|
9
9
|
* early. `forget` is separate because deleting rarely happens in the same
|
|
10
10
|
* breath as creating, and a `forget` buried inside a truncated commit preview
|
|
11
|
-
* would be both irreversible and invisible.
|
|
11
|
+
* would be both irreversible and invisible. `copy_file` is the only way a
|
|
12
|
+
* remembered file reaches the sandbox, which never mounts the memory.
|
|
12
13
|
*
|
|
13
14
|
* `audience` reaches the schema only when the binding grants more than one
|
|
14
15
|
* label, and `sees` never does: an agent that could name its own audience
|
|
15
16
|
* could read another agent's slice just by asking for it.
|
|
16
17
|
*/
|
|
17
18
|
import { hash } from "../payload.js";
|
|
18
|
-
import { MEMORY_COMMIT_TOOL, MEMORY_FORGET_TOOL, MEMORY_GREP_TOOL, MEMORY_LOAD_TOOL, MEMORY_SEARCH_TOOL, tool, withEffects, } from "../types.js";
|
|
19
|
+
import { MEMORY_COMMIT_TOOL, MEMORY_COPY_FILE_TOOL, MEMORY_FORGET_TOOL, MEMORY_GREP_TOOL, MEMORY_LOAD_TOOL, MEMORY_SEARCH_TOOL, tool, withEffects, } from "../types.js";
|
|
20
|
+
import { storedName } from "./files.js";
|
|
21
|
+
import { RevisionMismatch } from "./graph.js";
|
|
19
22
|
import { GREP_FIELDS, grepMemory } from "./grep.js";
|
|
20
23
|
import { renderRecollection } from "./render.js";
|
|
21
24
|
import { canForget, canWrite, KIND_HELP, MemoryError, RELATION_HELP, } from "./types.js";
|
|
@@ -96,7 +99,7 @@ export function memoryTools(opts) {
|
|
|
96
99
|
name: MEMORY_LOAD_TOOL,
|
|
97
100
|
description: 'Read memories in full by id, using the ids from a ' +
|
|
98
101
|
`${MEMORY_SEARCH_TOOL} legend. A remembered file comes back inline when it ` +
|
|
99
|
-
|
|
102
|
+
`is small; to open or run it, copy it into the workspace with ${MEMORY_COPY_FILE_TOOL}.`,
|
|
100
103
|
parameters: {
|
|
101
104
|
type: 'object',
|
|
102
105
|
properties: {
|
|
@@ -130,7 +133,7 @@ export function memoryTools(opts) {
|
|
|
130
133
|
revision: l.node.revision,
|
|
131
134
|
metadata: l.node.metadata,
|
|
132
135
|
file: l.node.file
|
|
133
|
-
? {
|
|
136
|
+
? { name: storedName(l.node.file), bytes: l.node.file.bytes }
|
|
134
137
|
: undefined,
|
|
135
138
|
content: l.content,
|
|
136
139
|
})), { kind: 'memory_op', spec });
|
|
@@ -143,7 +146,7 @@ export function memoryTools(opts) {
|
|
|
143
146
|
`instead of ${MEMORY_SEARCH_TOOL} when the answer has to be complete rather ` +
|
|
144
147
|
'than close — whether something was already recorded, or where a name, path, ' +
|
|
145
148
|
'id or command appears. This is the only correct way to search the memory ' +
|
|
146
|
-
'directly
|
|
149
|
+
'directly. Read a whole node with ' +
|
|
147
150
|
`${MEMORY_LOAD_TOOL}.`,
|
|
148
151
|
parameters: {
|
|
149
152
|
type: 'object',
|
|
@@ -217,7 +220,7 @@ export function memoryTools(opts) {
|
|
|
217
220
|
id: m.node.id,
|
|
218
221
|
kind: m.node.kind,
|
|
219
222
|
stale: m.stale || undefined,
|
|
220
|
-
file: m.node.file
|
|
223
|
+
file: m.node.file && storedName(m.node.file),
|
|
221
224
|
hits: m.hits.map((h) => ({ in: h.where, line: h.line, text: h.text })),
|
|
222
225
|
more: m.more || undefined,
|
|
223
226
|
})),
|
|
@@ -227,6 +230,57 @@ export function memoryTools(opts) {
|
|
|
227
230
|
}, { kind: 'memory_op', spec });
|
|
228
231
|
},
|
|
229
232
|
}),
|
|
233
|
+
// The one way a remembered file reaches the sandbox. Memory is never
|
|
234
|
+
// mounted, so the box only ever holds the workspace and the project's
|
|
235
|
+
// read-only trees, and the files stay with the graph that explains them.
|
|
236
|
+
// Reading memory is enough to copy from it: the write is the workspace's.
|
|
237
|
+
tool({
|
|
238
|
+
name: MEMORY_COPY_FILE_TOOL,
|
|
239
|
+
description: 'Copies a remembered file into the workspace so a command can open or run it. ' +
|
|
240
|
+
`Give the id of a node that holds a file, from ${MEMORY_SEARCH_TOOL} or ` +
|
|
241
|
+
`${MEMORY_LOAD_TOOL}. The copy is yours to edit; remembering the edited ` +
|
|
242
|
+
`version is a new ${MEMORY_COMMIT_TOOL}.`,
|
|
243
|
+
parameters: {
|
|
244
|
+
type: 'object',
|
|
245
|
+
properties: {
|
|
246
|
+
id: { type: 'string', description: 'the node that holds the file' },
|
|
247
|
+
to: {
|
|
248
|
+
type: 'string',
|
|
249
|
+
description: 'where in the workspace to put it; the file’s own name ' +
|
|
250
|
+
'(<id>.<ext>) at the workspace root when omitted',
|
|
251
|
+
},
|
|
252
|
+
overwrite: {
|
|
253
|
+
type: 'boolean',
|
|
254
|
+
description: 'replace a file already at that path',
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
required: ['id'],
|
|
258
|
+
additionalProperties: false,
|
|
259
|
+
},
|
|
260
|
+
execute: async (args, tc) => {
|
|
261
|
+
if (!tc.services.files) {
|
|
262
|
+
return {
|
|
263
|
+
error: 'this agent has no workspace to copy into',
|
|
264
|
+
hint: `read the file inline with ${MEMORY_LOAD_TOOL} instead`,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
const { file, bytes } = await index.fileOf(args.id, binding.sees);
|
|
269
|
+
const path = await tc.services.files.write(args.to?.trim() || storedName(file), bytes, {
|
|
270
|
+
overwrite: args.overwrite,
|
|
271
|
+
});
|
|
272
|
+
return { id: args.id, path, bytes: bytes.byteLength, copied: true };
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
if (err instanceof MemoryError) {
|
|
276
|
+
return refusal(err);
|
|
277
|
+
}
|
|
278
|
+
// The workspace's refusals — read-only, outside, already there —
|
|
279
|
+
// are things the model can fix by choosing another `to`.
|
|
280
|
+
return { error: err instanceof Error ? err.message : String(err) };
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
}),
|
|
230
284
|
];
|
|
231
285
|
if (!canWrite(binding)) {
|
|
232
286
|
return tools;
|
|
@@ -408,6 +462,7 @@ export function memoryTools(opts) {
|
|
|
408
462
|
},
|
|
409
463
|
execute: async (args, tc) => {
|
|
410
464
|
const opId = memoryOpId(tc.state.runId, tc.callId);
|
|
465
|
+
index.refresh();
|
|
411
466
|
const before = args.ids.flatMap((id) => {
|
|
412
467
|
const node = index.graph.get(id);
|
|
413
468
|
return node ? [{ id, kind: node.kind, revision: node.revision }] : [];
|
|
@@ -446,6 +501,12 @@ function refusal(err) {
|
|
|
446
501
|
if (err instanceof MemoryError) {
|
|
447
502
|
return { error: err.message, hint: err.hint };
|
|
448
503
|
}
|
|
504
|
+
if (err instanceof RevisionMismatch) {
|
|
505
|
+
return {
|
|
506
|
+
error: err.message,
|
|
507
|
+
hint: `another run changed it; ${MEMORY_LOAD_TOOL} it again and commit against the new revision`,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
449
510
|
throw err;
|
|
450
511
|
}
|
|
451
512
|
function extractFilePath(raw) {
|
|
@@ -461,11 +522,11 @@ function extractFilePath(raw) {
|
|
|
461
522
|
return '';
|
|
462
523
|
}
|
|
463
524
|
function source(tc, path) {
|
|
464
|
-
if (!tc.services.
|
|
525
|
+
if (!tc.services.files) {
|
|
465
526
|
throw new MemoryError('this agent cannot remember files', 'commit the node without `file`, or give the agent a workspace');
|
|
466
527
|
}
|
|
467
528
|
try {
|
|
468
|
-
return tc.services.
|
|
529
|
+
return tc.services.files.source(path);
|
|
469
530
|
}
|
|
470
531
|
catch (err) {
|
|
471
532
|
if (err instanceof MemoryError) {
|
package/dist/memory/types.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export declare class MemoryError extends Error {
|
|
|
47
47
|
constructor(message: string, hint?: string);
|
|
48
48
|
}
|
|
49
49
|
export interface MemoryFile {
|
|
50
|
-
/**
|
|
50
|
+
/** `<id>.<ext>`; older graphs hold `/memory/<id>.<ext>` — read it through `storedName` */
|
|
51
51
|
path: string;
|
|
52
52
|
bytes: number;
|
|
53
53
|
sha256: string;
|
package/dist/memory/types.js
CHANGED
|
@@ -46,7 +46,7 @@ export const KIND_HELP = {
|
|
|
46
46
|
plan: 'an approach that worked, worth reusing on a similar request',
|
|
47
47
|
fact: 'a durable truth about the environment or the domain',
|
|
48
48
|
snippet: 'a short piece of code or configuration, too small to be a file',
|
|
49
|
-
file: 'an artifact kept whole
|
|
49
|
+
file: 'an artifact kept whole, to copy back into the workspace and run again',
|
|
50
50
|
operation: 'an external call that was made — an endpoint, command or query',
|
|
51
51
|
preference: 'a standing instruction from the user that applies to every run',
|
|
52
52
|
};
|
package/dist/project/config.d.ts
CHANGED
|
@@ -348,30 +348,7 @@ export declare const projectSchema: z.ZodObject<{
|
|
|
348
348
|
limit: z.ZodDefault<z.ZodInt>;
|
|
349
349
|
}, z.core.$strict>]>>;
|
|
350
350
|
}, z.core.$strict>>, unknown>>;
|
|
351
|
-
sandbox: z.ZodOptional<z.
|
|
352
|
-
image: z.ZodOptional<z.ZodString>;
|
|
353
|
-
build: z.ZodOptional<z.ZodObject<{
|
|
354
|
-
dockerfile: z.ZodString;
|
|
355
|
-
context: z.ZodOptional<z.ZodString>;
|
|
356
|
-
}, z.core.$strict>>;
|
|
357
|
-
cpus: z.ZodOptional<z.ZodNumber>;
|
|
358
|
-
memory: z.ZodOptional<z.ZodInt>;
|
|
359
|
-
network: z.ZodOptional<z.ZodEnum<{
|
|
360
|
-
none: "none";
|
|
361
|
-
host: "host";
|
|
362
|
-
bridge: "bridge";
|
|
363
|
-
}>>;
|
|
364
|
-
workdir: z.ZodOptional<z.ZodString>;
|
|
365
|
-
timeout: z.ZodOptional<z.ZodInt>;
|
|
366
|
-
hardening: z.ZodOptional<z.ZodEnum<{
|
|
367
|
-
strict: "strict";
|
|
368
|
-
standard: "standard";
|
|
369
|
-
}>>;
|
|
370
|
-
user: z.ZodOptional<z.ZodString>;
|
|
371
|
-
persist: z.ZodOptional<z.ZodBoolean>;
|
|
372
|
-
env: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
373
|
-
keys: z.ZodOptional<z.ZodBoolean>;
|
|
374
|
-
}, z.core.$strict>>;
|
|
351
|
+
sandbox: z.ZodOptional<z.ZodNever>;
|
|
375
352
|
default: z.ZodOptional<z.ZodBoolean>;
|
|
376
353
|
}, z.core.$strict>>;
|
|
377
354
|
}, z.core.$strict>;
|
package/dist/project/config.js
CHANGED
|
@@ -360,11 +360,16 @@ const agent = z
|
|
|
360
360
|
.preprocess((v) => (typeof v === 'boolean' ? (v ? {} : undefined) : v), memoryBinding.optional())
|
|
361
361
|
.optional(),
|
|
362
362
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
363
|
+
* Gone: a session has one sandbox, the project's, shared by every
|
|
364
|
+
* agent. Refused rather than ignored, so a project that relied on it
|
|
365
|
+
* hears where its settings have to go instead.
|
|
366
366
|
*/
|
|
367
|
-
sandbox:
|
|
367
|
+
sandbox: z
|
|
368
|
+
.never({
|
|
369
|
+
error: 'per-agent sandboxes were removed — a session has one sandbox, shared by ' +
|
|
370
|
+
"every agent; move these settings to the project's top-level `sandbox:`",
|
|
371
|
+
})
|
|
372
|
+
.optional(),
|
|
368
373
|
/** entrypoint, when no top-level `default` is given */
|
|
369
374
|
default: z.boolean().optional(),
|
|
370
375
|
})
|
package/dist/project/load.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Agent, AgentRegistry } from '../agent.ts';
|
|
2
2
|
import type { Embedder } from '../embedding.ts';
|
|
3
3
|
import { RunStream } from '../events.ts';
|
|
4
|
+
import type { WorkspaceFiles } from '../memory/files.ts';
|
|
4
5
|
import { MemoryIndex } from '../memory/index.ts';
|
|
5
6
|
import { ModelRegistry, type EmbeddingRef, type ModelRef, type ProviderSpec } from '../models/factory.ts';
|
|
6
7
|
import type { PayloadStore } from '../payload.ts';
|
|
@@ -65,9 +66,7 @@ export interface ProjectOptions<TCtx = unknown> {
|
|
|
65
66
|
memory?: MemoryIndex;
|
|
66
67
|
/**
|
|
67
68
|
* Another directory to keep the memory graph in, relative to the project
|
|
68
|
-
* root when it is not absolute.
|
|
69
|
-
* as `/memory` too, or the agent reads its remembered files from one graph
|
|
70
|
-
* and writes to another.
|
|
69
|
+
* root when it is not absolute.
|
|
71
70
|
*/
|
|
72
71
|
memoryDir?: string;
|
|
73
72
|
/**
|
|
@@ -77,8 +76,8 @@ export interface ProjectOptions<TCtx = unknown> {
|
|
|
77
76
|
* warmed memory at once, which the directory lock otherwise forbids.
|
|
78
77
|
*/
|
|
79
78
|
memoryReadOnly?: boolean;
|
|
80
|
-
/**
|
|
81
|
-
|
|
79
|
+
/** the agent's workspace, so files can be remembered and copied back */
|
|
80
|
+
files?: WorkspaceFiles;
|
|
82
81
|
payloads?: PayloadStore;
|
|
83
82
|
/** extra providers merged with the ones the project declares */
|
|
84
83
|
skills?: SkillProvider[];
|
|
@@ -184,9 +183,8 @@ export declare class AgentProject<TCtx = unknown> {
|
|
|
184
183
|
export declare function projectRegistry<TCtx>(config: ProjectConfig, opts?: ProjectOptions<TCtx>): ModelRegistry;
|
|
185
184
|
/**
|
|
186
185
|
* Where this project's memory lives, or `undefined` when it has none. Shared
|
|
187
|
-
* with the front end, which
|
|
188
|
-
*
|
|
189
|
-
* graph is not.
|
|
186
|
+
* with the front end, which reports and exports the same directory: two
|
|
187
|
+
* answers to this question would mean two graphs.
|
|
190
188
|
*
|
|
191
189
|
* `override` is the host pointing the run at another graph — `zen run
|
|
192
190
|
* --memory`. It answers for a project that declares no memory at all, because
|
package/dist/project/load.js
CHANGED
|
@@ -173,7 +173,7 @@ export class AgentProject {
|
|
|
173
173
|
const runner = new AgentRunner({
|
|
174
174
|
payloads: this.#options.payloads,
|
|
175
175
|
memory: this.memory,
|
|
176
|
-
|
|
176
|
+
files: this.#options.files,
|
|
177
177
|
skills: [...this.skillProviders],
|
|
178
178
|
...overrides,
|
|
179
179
|
});
|
|
@@ -530,9 +530,8 @@ function memoryFor(spec) {
|
|
|
530
530
|
}
|
|
531
531
|
/**
|
|
532
532
|
* Where this project's memory lives, or `undefined` when it has none. Shared
|
|
533
|
-
* with the front end, which
|
|
534
|
-
*
|
|
535
|
-
* graph is not.
|
|
533
|
+
* with the front end, which reports and exports the same directory: two
|
|
534
|
+
* answers to this question would mean two graphs.
|
|
536
535
|
*
|
|
537
536
|
* `override` is the host pointing the run at another graph — `zen run
|
|
538
537
|
* --memory`. It answers for a project that declares no memory at all, because
|
package/dist/runner.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Agent, AgentRegistry, type AgentOptions } from './agent.ts';
|
|
|
3
3
|
import { type Architecture } from './architecture.ts';
|
|
4
4
|
import { RunStream } from './events.ts';
|
|
5
5
|
import { type IdClock } from './ids.ts';
|
|
6
|
+
import type { WorkspaceFiles } from './memory/files.ts';
|
|
6
7
|
import type { MemoryIndex } from './memory/index.ts';
|
|
7
8
|
import type { Model } from './model.ts';
|
|
8
9
|
import { PayloadResolver, type PayloadStore } from './payload.ts';
|
|
@@ -10,7 +11,7 @@ import { Services } from './services.ts';
|
|
|
10
11
|
import { type SkillProvider } from './skills.ts';
|
|
11
12
|
import { type AgentState } from './state.ts';
|
|
12
13
|
import { type HandoffNode, type TrajectoryNode } from './trajectory.ts';
|
|
13
|
-
import { type Input, type TokenUsage } from './types.ts';
|
|
14
|
+
import { type Input, type ResumeAnswer, type TokenUsage } from './types.ts';
|
|
14
15
|
/**
|
|
15
16
|
* Collapsing history is two decisions, and they belong on different sides of
|
|
16
17
|
* the I/O line: *which* nodes go (pure, so the kernel can stay pure) and *how*
|
|
@@ -42,8 +43,8 @@ export interface RunnerOptions<TCtx = unknown> {
|
|
|
42
43
|
services?: Services;
|
|
43
44
|
payloads?: PayloadStore | PayloadResolver;
|
|
44
45
|
memory?: MemoryIndex;
|
|
45
|
-
/**
|
|
46
|
-
|
|
46
|
+
/** the agent's workspace, so files can be remembered and copied back */
|
|
47
|
+
files?: WorkspaceFiles;
|
|
47
48
|
skills?: SkillProvider[];
|
|
48
49
|
handoffPolicy?: HandoffPolicy;
|
|
49
50
|
/** renders the nodes a policy selected; defaults to a structural note */
|
|
@@ -67,6 +68,15 @@ export interface RunOptions<T = string, TCtx = unknown> {
|
|
|
67
68
|
signal?: AbortSignal;
|
|
68
69
|
maxForkDepth?: number;
|
|
69
70
|
}
|
|
71
|
+
export interface ResumeOptions<T = string, TCtx = unknown> extends RunOptions<T, TCtx> {
|
|
72
|
+
/**
|
|
73
|
+
* Answers to the calls the run is suspended on, by call id. A suspended call
|
|
74
|
+
* without an answer stays suspended: the run stops again at once.
|
|
75
|
+
*/
|
|
76
|
+
answers?: Record<string, ResumeAnswer>;
|
|
77
|
+
}
|
|
78
|
+
/** What a new message says to a call still waiting for a person. */
|
|
79
|
+
export declare const SUPERSEDED_MESSAGE = "cancelled: the user sent a new message instead of answering this; continue without it";
|
|
70
80
|
/**
|
|
71
81
|
* The driver: it owns all nondeterminism and all I/O, and calls the kernel to
|
|
72
82
|
* advance the state. Every checkpoint event carries a complete snapshot, so a
|
|
@@ -97,9 +107,16 @@ export declare class AgentRunner<TCtx = unknown> {
|
|
|
97
107
|
* consumer sees (`run_created`) already carries a persistable snapshot.
|
|
98
108
|
*/
|
|
99
109
|
run<T = string>(agent: Agent<TCtx> | string, input?: Input, opts?: RunOptions<T, TCtx>): RunStream<T>;
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Continues a persisted run from its own snapshot. A run that stopped
|
|
112
|
+
* `suspended` continues once `answers` answer its suspended calls.
|
|
113
|
+
*/
|
|
114
|
+
resume<T = string>(state: AgentState, opts?: ResumeOptions<T, TCtx>): RunStream<T>;
|
|
115
|
+
/**
|
|
116
|
+
* Adds a follow-up message to a finished (or paused) run and continues it.
|
|
117
|
+
* Calls still suspended are cancelled first: the person chose to say
|
|
118
|
+
* something else rather than answer them.
|
|
119
|
+
*/
|
|
103
120
|
send<T = string>(state: AgentState, input: Input, opts?: RunOptions<T, TCtx>): RunStream<T>;
|
|
104
121
|
}
|
|
105
122
|
//# sourceMappingURL=runner.d.ts.map
|
package/dist/runner.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Services } from "./services.js";
|
|
|
9
9
|
import { activeSkillNames } from "./skills.js";
|
|
10
10
|
import { lastText } from "./state.js";
|
|
11
11
|
import { lastOfType, projectMessages, } from "./trajectory.js";
|
|
12
|
-
import { isToolReturn, stringify, zeroUsage, } from "./types.js";
|
|
12
|
+
import { isToolReturn, isToolSuspend, stringify, zeroUsage, } from "./types.js";
|
|
13
13
|
const defaultJoinPolicy = {
|
|
14
14
|
summarize: (child, services) => lastText(child, services.payloads),
|
|
15
15
|
onBranchError: 'continue',
|
|
@@ -51,6 +51,8 @@ export function modelSummarizer(model, instructions = SUMMARY_INSTRUCTIONS) {
|
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
|
+
/** What a new message says to a call still waiting for a person. */
|
|
55
|
+
export const SUPERSEDED_MESSAGE = 'cancelled: the user sent a new message instead of answering this; continue without it';
|
|
54
56
|
// ---------------------------------------------------------------------------
|
|
55
57
|
// Runner
|
|
56
58
|
// ---------------------------------------------------------------------------
|
|
@@ -82,7 +84,7 @@ export class AgentRunner {
|
|
|
82
84
|
new Services({
|
|
83
85
|
payloads: opts.payloads,
|
|
84
86
|
memory: opts.memory,
|
|
85
|
-
|
|
87
|
+
files: opts.files,
|
|
86
88
|
skills: opts.skills,
|
|
87
89
|
clock: this.#clock,
|
|
88
90
|
});
|
|
@@ -126,18 +128,59 @@ export class AgentRunner {
|
|
|
126
128
|
// awaited, so building a RunStream costs nothing.
|
|
127
129
|
return new RunStream(this.#start(agent, input, opts));
|
|
128
130
|
}
|
|
129
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Continues a persisted run from its own snapshot. A run that stopped
|
|
133
|
+
* `suspended` continues once `answers` answer its suspended calls.
|
|
134
|
+
*/
|
|
130
135
|
resume(state, opts = {}) {
|
|
131
|
-
return new RunStream(this.#
|
|
136
|
+
return new RunStream(this.#resumeAndDrive(state, opts));
|
|
132
137
|
}
|
|
133
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Adds a follow-up message to a finished (or paused) run and continues it.
|
|
140
|
+
* Calls still suspended are cancelled first: the person chose to say
|
|
141
|
+
* something else rather than answer them.
|
|
142
|
+
*/
|
|
134
143
|
send(state, input, opts = {}) {
|
|
135
144
|
return new RunStream(this.#sendAndDrive(state, input, opts));
|
|
136
145
|
}
|
|
146
|
+
async *#resumeAndDrive(state, opts) {
|
|
147
|
+
const answered = yield* this.#answer(state, opts.answers ?? {}, opts);
|
|
148
|
+
return yield* this.#drive(answered, opts);
|
|
149
|
+
}
|
|
137
150
|
async *#sendAndDrive(state, input, opts) {
|
|
138
|
-
const
|
|
151
|
+
const cancel = {};
|
|
152
|
+
for (const s of state.suspended ?? []) {
|
|
153
|
+
cancel[s.callId] = { status: 'cancelled', message: SUPERSEDED_MESSAGE };
|
|
154
|
+
}
|
|
155
|
+
const answered = yield* this.#answer(state, cancel, opts);
|
|
156
|
+
const next = await Kernel.applyUserInput(answered, input, this.#env(opts));
|
|
139
157
|
return yield* this.#drive(next, opts);
|
|
140
158
|
}
|
|
159
|
+
/** Applies answers to suspended calls; answers to anything else are ignored. */
|
|
160
|
+
async *#answer(initial, answers, opts) {
|
|
161
|
+
const env = this.#env(opts);
|
|
162
|
+
let state = initial;
|
|
163
|
+
for (const s of initial.suspended ?? []) {
|
|
164
|
+
const answer = answers[s.callId];
|
|
165
|
+
if (!answer) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (answer.status === 'resolved' && 'rerun' in answer) {
|
|
169
|
+
state = Kernel.releaseSuspension(state, s.callId);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const outcome = answer.status === 'resolved'
|
|
173
|
+
? { output: stringify(answer.payload), isError: false }
|
|
174
|
+
: { output: answer.message ?? 'cancelled: nobody answered', isError: true };
|
|
175
|
+
const was = state;
|
|
176
|
+
state = await Kernel.applyToolResult(state, s.callId, outcome, env);
|
|
177
|
+
const node = appended(was, state).find((n) => n.type === 'tool_result');
|
|
178
|
+
if (node) {
|
|
179
|
+
yield tag(state, undefined, { type: 'after_tool_call', state, node });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return state;
|
|
183
|
+
}
|
|
141
184
|
#env(opts) {
|
|
142
185
|
return {
|
|
143
186
|
services: this.services,
|
|
@@ -207,11 +250,23 @@ export class AgentRunner {
|
|
|
207
250
|
break;
|
|
208
251
|
}
|
|
209
252
|
case 'tools': {
|
|
253
|
+
// A suspended call waits for `resume`; when nothing else is left
|
|
254
|
+
// to run, the run stops here and says what it waits for.
|
|
255
|
+
const waiting = new Set((state.suspended ?? []).map((x) => x.callId));
|
|
256
|
+
const runnable = action.calls.filter((c) => !waiting.has(c.callId));
|
|
257
|
+
if (!runnable.length) {
|
|
258
|
+
return yield* this.#finish(state, branch, 'suspended');
|
|
259
|
+
}
|
|
210
260
|
const tools = await Kernel.resolveTools(state, this.registry, env);
|
|
211
|
-
for (const call of
|
|
261
|
+
for (const call of runnable) {
|
|
212
262
|
yield tag(state, branch, { type: 'before_tool_call', state, call });
|
|
213
263
|
const was = state;
|
|
214
|
-
const
|
|
264
|
+
const ran = await this.#runTool(state, tools, call, opts);
|
|
265
|
+
if ('suspend' in ran) {
|
|
266
|
+
state = Kernel.applySuspension(state, call, ran.suspend, this.#clock.now());
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const outcome = ran;
|
|
215
270
|
state = await Kernel.applyToolResult(state, call.callId, outcome, env);
|
|
216
271
|
const added = appended(was, state);
|
|
217
272
|
const node = added.find((n) => n.type === 'tool_result');
|
|
@@ -465,6 +520,15 @@ export class AgentRunner {
|
|
|
465
520
|
services: this.services,
|
|
466
521
|
signal: opts.signal,
|
|
467
522
|
});
|
|
523
|
+
if (isToolSuspend(out)) {
|
|
524
|
+
// A branch runs alongside its siblings and is joined when all end:
|
|
525
|
+
// there is no run of its own to stop and resume.
|
|
526
|
+
// Its message still reaches the model, which can pass it on.
|
|
527
|
+
return opts.branch
|
|
528
|
+
? fail(`error: ${def.name} needs a person and cannot wait inside a fork branch` +
|
|
529
|
+
(out.spec.message ? `: ${out.spec.message}` : ''))
|
|
530
|
+
: { suspend: out.spec };
|
|
531
|
+
}
|
|
468
532
|
const effects = isToolReturn(out) ? out.effects : [];
|
|
469
533
|
const value = isToolReturn(out) ? out.output : out;
|
|
470
534
|
return {
|
package/dist/services.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PayloadResolver, type PayloadStore } from './payload.ts';
|
|
2
2
|
import { type IdClock } from './ids.ts';
|
|
3
|
+
import type { WorkspaceFiles } from './memory/files.ts';
|
|
3
4
|
import type { MemoryIndex } from './memory/index.ts';
|
|
4
5
|
import type { SkillProvider } from './skills.ts';
|
|
5
6
|
/**
|
|
@@ -18,12 +19,13 @@ export declare class Services {
|
|
|
18
19
|
/** the one memory graph, when the project has one */
|
|
19
20
|
readonly memory?: MemoryIndex;
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* The agent's workspace, for the two memory tools that cross into it:
|
|
23
|
+
* remembering a file reads from it, and copying a remembered file back
|
|
24
|
+
* writes to it — memory is never mounted, so that copy is the only way its
|
|
25
|
+
* files reach the sandbox. Supplied by whoever built the workspace, because
|
|
26
|
+
* only it knows the mounts. Without it both are refusals, not crashes.
|
|
25
27
|
*/
|
|
26
|
-
readonly
|
|
28
|
+
readonly files?: WorkspaceFiles;
|
|
27
29
|
/** the driver's clock, so a tool that stamps a node stays replayable */
|
|
28
30
|
readonly clock: IdClock;
|
|
29
31
|
constructor(opts?: {
|
|
@@ -31,7 +33,7 @@ export declare class Services {
|
|
|
31
33
|
memory?: MemoryIndex;
|
|
32
34
|
skills?: SkillProvider[];
|
|
33
35
|
clock?: IdClock;
|
|
34
|
-
|
|
36
|
+
files?: WorkspaceFiles;
|
|
35
37
|
});
|
|
36
38
|
skillProvider(id: string): SkillProvider;
|
|
37
39
|
hasMemory(): boolean;
|
package/dist/services.js
CHANGED
|
@@ -18,12 +18,13 @@ export class Services {
|
|
|
18
18
|
/** the one memory graph, when the project has one */
|
|
19
19
|
memory;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
21
|
+
* The agent's workspace, for the two memory tools that cross into it:
|
|
22
|
+
* remembering a file reads from it, and copying a remembered file back
|
|
23
|
+
* writes to it — memory is never mounted, so that copy is the only way its
|
|
24
|
+
* files reach the sandbox. Supplied by whoever built the workspace, because
|
|
25
|
+
* only it knows the mounts. Without it both are refusals, not crashes.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
+
files;
|
|
27
28
|
/** the driver's clock, so a tool that stamps a node stays replayable */
|
|
28
29
|
clock;
|
|
29
30
|
#skills = new Map();
|
|
@@ -31,7 +32,7 @@ export class Services {
|
|
|
31
32
|
const p = opts.payloads ?? new InMemoryPayloadStore();
|
|
32
33
|
this.payloads = p instanceof PayloadResolver ? p : new PayloadResolver(p);
|
|
33
34
|
this.memory = opts.memory;
|
|
34
|
-
this.
|
|
35
|
+
this.files = opts.files;
|
|
35
36
|
this.clock = opts.clock ?? systemClock;
|
|
36
37
|
for (const s of opts.skills ?? []) {
|
|
37
38
|
this.#skills.set(s.id, s);
|
package/dist/state.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PayloadResolver } from './payload.ts';
|
|
2
2
|
import { type TrajectoryNode } from './trajectory.ts';
|
|
3
|
-
import type { JsonSchema, TokenUsage } from './types.ts';
|
|
3
|
+
import type { JsonSchema, Suspension, TokenUsage } from './types.ts';
|
|
4
4
|
export type RunPhase =
|
|
5
5
|
/** initial context built, nothing executed */
|
|
6
6
|
'created'
|
|
@@ -56,6 +56,11 @@ export interface AgentState {
|
|
|
56
56
|
callId: string;
|
|
57
57
|
branches: string[];
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Pending tool calls the run is suspended on, waiting for a person (see
|
|
61
|
+
* `suspend`). Each is also in `pendingToolCalls`; `runner.resume` answers them.
|
|
62
|
+
*/
|
|
63
|
+
suspended?: Suspension[];
|
|
59
64
|
/** derived cache; always recomputable from the trajectory */
|
|
60
65
|
usage: TokenUsage;
|
|
61
66
|
/** serialized user context (must be JSON) */
|
|
@@ -69,7 +74,8 @@ export interface RunResult<T = string> {
|
|
|
69
74
|
agent: string;
|
|
70
75
|
state: AgentState;
|
|
71
76
|
usage: TokenUsage;
|
|
72
|
-
|
|
77
|
+
/** suspended: waiting on `state.suspended`; `runner.resume` continues it */
|
|
78
|
+
stopReason: 'final' | 'aborted' | 'failed' | 'suspended';
|
|
73
79
|
}
|
|
74
80
|
/**
|
|
75
81
|
* Structural check for a rehydrated state. There is no prototype to restore —
|