@zenera/neo 1.1.57 → 1.1.59
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/memory/files.d.ts +7 -37
- package/dist/memory/files.js +10 -34
- package/dist/memory/index.d.ts +3 -6
- package/dist/memory/index.js +3 -7
- package/dist/memory/tools.js +19 -14
- package/dist/project/load.d.ts +2 -2
- package/dist/runner.d.ts +2 -2
- package/dist/services.d.ts +3 -3
- package/dist/tools/files.d.ts +2 -61
- package/dist/tools/files.js +16 -246
- package/dist/tools/workspace.d.ts +139 -0
- package/dist/tools/workspace.js +238 -0
- package/package.json +2 -2
package/dist/memory/files.d.ts
CHANGED
|
@@ -4,49 +4,19 @@ import { type MemoryFile } from './types.ts';
|
|
|
4
4
|
* memory directory cannot become a place where build outputs go to hide.
|
|
5
5
|
*/
|
|
6
6
|
export declare const MAX_FILE_BYTES: number;
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
export interface FileSource {
|
|
13
|
-
path: string;
|
|
14
|
-
stat(): Promise<{
|
|
15
|
-
size: number;
|
|
16
|
-
isFile(): boolean;
|
|
17
|
-
}>;
|
|
18
|
-
read(): Promise<Uint8Array>;
|
|
19
|
-
}
|
|
20
|
-
/** A file on this machine's disk, as a source. */
|
|
21
|
-
export declare function hostFile(path: string): FileSource;
|
|
22
|
-
/**
|
|
23
|
-
* The agent's workspace, as memory needs it: a file to remember is read from
|
|
24
|
-
* it, and a remembered file is copied back into it. Both are contained and
|
|
25
|
-
* permission-checked exactly as the file tools are. `Workspace` is one.
|
|
26
|
-
*/
|
|
27
|
-
export interface WorkspaceFiles {
|
|
28
|
-
source(path: string): FileSource;
|
|
29
|
-
/**
|
|
30
|
-
* Writes bytes and answers with the path the model knows the file by.
|
|
31
|
-
* Refuses to replace an existing file unless told to.
|
|
32
|
-
*/
|
|
33
|
-
write(path: string, data: Uint8Array, opts?: {
|
|
34
|
-
overwrite?: boolean;
|
|
35
|
-
}): Promise<string>;
|
|
7
|
+
/** A file to remember: its bytes, and the name its extension is taken from. */
|
|
8
|
+
export interface FileBytes {
|
|
9
|
+
name: string;
|
|
10
|
+
data: Uint8Array;
|
|
36
11
|
}
|
|
37
12
|
export interface RememberOptions {
|
|
38
13
|
maxBytes?: number;
|
|
39
|
-
/** Display path as passed to the tool, for reporting errors without leaking the host filesystem */
|
|
40
|
-
path?: string;
|
|
41
14
|
}
|
|
42
15
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* The bytes are read once and both hashed and written from that one buffer, so
|
|
46
|
-
* the digest provably belongs to the copy that was kept — re-reading the source
|
|
47
|
-
* to hash it would leave a window for it to change in between.
|
|
16
|
+
* Keep `file` in the memory directory under `id`, and describe it. The digest
|
|
17
|
+
* is of these bytes, the ones written, so it provably belongs to the copy kept.
|
|
48
18
|
*/
|
|
49
|
-
export declare function rememberFile(dir: string,
|
|
19
|
+
export declare function rememberFile(dir: string, file: FileBytes, id: string, opts?: RememberOptions): Promise<MemoryFile>;
|
|
50
20
|
/**
|
|
51
21
|
* What a remembered file is called: `<id>.<ext>`. The name the model is shown,
|
|
52
22
|
* and where a copy lands by default. Graphs written while the tree was still
|
package/dist/memory/files.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { mkdir,
|
|
2
|
+
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { basename, extname, join } from 'node:path';
|
|
4
4
|
import { FILES_DIR } from "./store.js";
|
|
5
5
|
import { MemoryError } from "./types.js";
|
|
@@ -26,39 +26,23 @@ import { MemoryError } from "./types.js";
|
|
|
26
26
|
export const MAX_FILE_BYTES = 2 * 1024 * 1024;
|
|
27
27
|
/** Extensions are for `run_command` and syntax hints, not a type system. */
|
|
28
28
|
const MAX_FORMAT_LEN = 16;
|
|
29
|
-
/** A file on this machine's disk, as a source. */
|
|
30
|
-
export function hostFile(path) {
|
|
31
|
-
return { path, stat: () => stat(path), read: () => readFile(path) };
|
|
32
|
-
}
|
|
33
29
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* The bytes are read once and both hashed and written from that one buffer, so
|
|
37
|
-
* the digest provably belongs to the copy that was kept — re-reading the source
|
|
38
|
-
* to hash it would leave a window for it to change in between.
|
|
30
|
+
* Keep `file` in the memory directory under `id`, and describe it. The digest
|
|
31
|
+
* is of these bytes, the ones written, so it provably belongs to the copy kept.
|
|
39
32
|
*/
|
|
40
|
-
export async function rememberFile(dir,
|
|
33
|
+
export async function rememberFile(dir, file, id, opts = {}) {
|
|
41
34
|
const maxBytes = opts.maxBytes ?? MAX_FILE_BYTES;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!info.isFile()) {
|
|
45
|
-
throw new MemoryError(`${name} is not a regular file`, 'only files can be remembered; write a `fact` node describing a directory instead');
|
|
46
|
-
}
|
|
47
|
-
if (info.size > maxBytes) {
|
|
48
|
-
throw new MemoryError(`${name} is ${info.size} bytes, over the ${maxBytes} byte limit for a remembered file`, 'keep the artifact in the workspace and remember a `snippet` of the part that matters');
|
|
49
|
-
}
|
|
50
|
-
const bytes = await source.read();
|
|
51
|
-
if (bytes.byteLength > maxBytes) {
|
|
52
|
-
throw new MemoryError(`${name} grew past the ${maxBytes} byte limit while being read`);
|
|
35
|
+
if (file.data.byteLength > maxBytes) {
|
|
36
|
+
throw new MemoryError(`${file.name} is ${file.data.byteLength} bytes, over the ${maxBytes} byte limit for a remembered file`, 'keep the artifact in the workspace and remember a `snippet` of the part that matters');
|
|
53
37
|
}
|
|
54
|
-
const format = formatOf(
|
|
38
|
+
const format = formatOf(file.name);
|
|
55
39
|
const stored = id + (format ? `.${format}` : '');
|
|
56
40
|
await mkdir(join(dir, FILES_DIR), { recursive: true });
|
|
57
|
-
await writeFile(join(dir, FILES_DIR, stored),
|
|
41
|
+
await writeFile(join(dir, FILES_DIR, stored), file.data);
|
|
58
42
|
return {
|
|
59
43
|
path: stored,
|
|
60
|
-
bytes:
|
|
61
|
-
sha256: createHash('sha256').update(
|
|
44
|
+
bytes: file.data.byteLength,
|
|
45
|
+
sha256: createHash('sha256').update(file.data).digest('hex'),
|
|
62
46
|
format,
|
|
63
47
|
};
|
|
64
48
|
}
|
|
@@ -79,14 +63,6 @@ export function hostPath(dir, file) {
|
|
|
79
63
|
export async function forgetFile(dir, file) {
|
|
80
64
|
await rm(hostPath(dir, file), { force: true });
|
|
81
65
|
}
|
|
82
|
-
async function describe(source, name) {
|
|
83
|
-
try {
|
|
84
|
-
return await source.stat();
|
|
85
|
-
}
|
|
86
|
-
catch {
|
|
87
|
-
throw new MemoryError(`${name} does not exist`, 'remember a file the run actually wrote, by the path the file tools use');
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
66
|
/**
|
|
91
67
|
* The extension drives how the file is run and rendered, so it is kept, but it
|
|
92
68
|
* becomes part of a filename this module chooses — hence the character class.
|
package/dist/memory/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Embedder } from '../embedding.ts';
|
|
2
2
|
import type { IdClock } from '../ids.ts';
|
|
3
|
-
import { type
|
|
3
|
+
import { type FileBytes } from './files.ts';
|
|
4
4
|
import { type MemoryGraph } from './graph.ts';
|
|
5
5
|
import type { MemoryStore } from './store.ts';
|
|
6
6
|
import { type MemoryFile, type MemoryNode, type MemoryQuery, type Recollection } from './types.ts';
|
|
@@ -29,11 +29,8 @@ export interface CommitNode {
|
|
|
29
29
|
text?: string;
|
|
30
30
|
audience?: string[];
|
|
31
31
|
metadata?: Record<string, unknown>;
|
|
32
|
-
/** the file to
|
|
33
|
-
file?:
|
|
34
|
-
source: FileSource;
|
|
35
|
-
path?: string;
|
|
36
|
-
};
|
|
32
|
+
/** the file to keep: its bytes, read by the caller under its own rules */
|
|
33
|
+
file?: FileBytes;
|
|
37
34
|
expectedRevision?: number;
|
|
38
35
|
}
|
|
39
36
|
export interface CommitEdge {
|
package/dist/memory/index.js
CHANGED
|
@@ -161,9 +161,7 @@ export class MemoryIndex {
|
|
|
161
161
|
const written = [];
|
|
162
162
|
try {
|
|
163
163
|
for (const item of plan.files) {
|
|
164
|
-
const file = await rememberFile(this.store.dir, item.
|
|
165
|
-
path: item.path,
|
|
166
|
-
});
|
|
164
|
+
const file = await rememberFile(this.store.dir, item.file, item.id);
|
|
167
165
|
written.push(file);
|
|
168
166
|
item.target.file = file;
|
|
169
167
|
}
|
|
@@ -386,8 +384,7 @@ export class MemoryIndex {
|
|
|
386
384
|
if (node.file) {
|
|
387
385
|
files.push({
|
|
388
386
|
id,
|
|
389
|
-
|
|
390
|
-
path: node.file.path,
|
|
387
|
+
file: node.file,
|
|
391
388
|
target: patch,
|
|
392
389
|
});
|
|
393
390
|
}
|
|
@@ -414,8 +411,7 @@ export class MemoryIndex {
|
|
|
414
411
|
if (node.file) {
|
|
415
412
|
files.push({
|
|
416
413
|
id: newId,
|
|
417
|
-
|
|
418
|
-
path: node.file.path,
|
|
414
|
+
file: node.file,
|
|
419
415
|
target: draft,
|
|
420
416
|
});
|
|
421
417
|
}
|
package/dist/memory/tools.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { hash } from "../payload.js";
|
|
19
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";
|
|
20
|
+
import { MAX_FILE_BYTES, storedName } from "./files.js";
|
|
21
21
|
import { RevisionMismatch } from "./graph.js";
|
|
22
22
|
import { GREP_FIELDS, grepMemory } from "./grep.js";
|
|
23
23
|
import { renderRecollection } from "./render.js";
|
|
@@ -383,28 +383,27 @@ export function memoryTools(opts) {
|
|
|
383
383
|
const opId = memoryOpId(tc.state.runId, tc.callId);
|
|
384
384
|
let nodes;
|
|
385
385
|
try {
|
|
386
|
-
|
|
387
|
-
|
|
386
|
+
// In order, so a failure names the first bad file, as before.
|
|
387
|
+
nodes = [];
|
|
388
|
+
for (const n of args.nodes) {
|
|
389
|
+
let file;
|
|
388
390
|
if (n.file !== undefined) {
|
|
389
391
|
const rawPath = extractFilePath(n.file);
|
|
390
392
|
if (!rawPath.trim()) {
|
|
391
393
|
throw new MemoryError('file path is required', 'pass the path of the file in the workspace to remember, e.g. "report.py"');
|
|
392
394
|
}
|
|
393
|
-
|
|
394
|
-
source: source(tc, rawPath),
|
|
395
|
-
path: rawPath,
|
|
396
|
-
};
|
|
395
|
+
file = { name: rawPath, data: await readFile(tc, rawPath) };
|
|
397
396
|
}
|
|
398
|
-
|
|
397
|
+
nodes.push({
|
|
399
398
|
ref: n.ref,
|
|
400
399
|
id: n.id,
|
|
401
400
|
kind: n.kind,
|
|
402
401
|
text: n.text,
|
|
403
402
|
audience: n.audience,
|
|
404
403
|
expectedRevision: n.expected_revision,
|
|
405
|
-
file
|
|
406
|
-
};
|
|
407
|
-
}
|
|
404
|
+
file,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
408
407
|
}
|
|
409
408
|
catch (err) {
|
|
410
409
|
return refusal(err);
|
|
@@ -521,18 +520,24 @@ function extractFilePath(raw) {
|
|
|
521
520
|
}
|
|
522
521
|
return '';
|
|
523
522
|
}
|
|
524
|
-
|
|
523
|
+
/** A file the agent names, read under its file tools' rules, as memory takes it. */
|
|
524
|
+
async function readFile(tc, path) {
|
|
525
525
|
if (!tc.services.files) {
|
|
526
526
|
throw new MemoryError('this agent cannot remember files', 'commit the node without `file`, or give the agent a workspace');
|
|
527
527
|
}
|
|
528
528
|
try {
|
|
529
|
-
return tc.services.files.
|
|
529
|
+
return (await tc.services.files.read(path, { maxBytes: MAX_FILE_BYTES })).data;
|
|
530
530
|
}
|
|
531
531
|
catch (err) {
|
|
532
532
|
if (err instanceof MemoryError) {
|
|
533
533
|
throw err;
|
|
534
534
|
}
|
|
535
|
-
|
|
535
|
+
const code = err.code;
|
|
536
|
+
throw new MemoryError(err instanceof Error ? err.message : String(err), code === 'EFBIG'
|
|
537
|
+
? 'keep the artifact in the workspace and remember a `snippet` of the part that matters'
|
|
538
|
+
: code === 'EISDIR'
|
|
539
|
+
? 'only files can be remembered; write a `fact` node describing a directory instead'
|
|
540
|
+
: 'remember a file the run actually wrote, by the path the file tools use');
|
|
536
541
|
}
|
|
537
542
|
}
|
|
538
543
|
function changed(index, ids) {
|
package/dist/project/load.d.ts
CHANGED
|
@@ -1,7 +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 {
|
|
4
|
+
import type { AgentFiles } from '../tools/workspace.ts';
|
|
5
5
|
import { MemoryIndex } from '../memory/index.ts';
|
|
6
6
|
import { ModelRegistry, type EmbeddingRef, type ModelRef, type ProviderSpec } from '../models/factory.ts';
|
|
7
7
|
import type { PayloadStore } from '../payload.ts';
|
|
@@ -77,7 +77,7 @@ export interface ProjectOptions<TCtx = unknown> {
|
|
|
77
77
|
*/
|
|
78
78
|
memoryReadOnly?: boolean;
|
|
79
79
|
/** the agent's workspace, so files can be remembered and copied back */
|
|
80
|
-
files?:
|
|
80
|
+
files?: AgentFiles;
|
|
81
81
|
payloads?: PayloadStore;
|
|
82
82
|
/** extra providers merged with the ones the project declares */
|
|
83
83
|
skills?: SkillProvider[];
|
package/dist/runner.d.ts
CHANGED
|
@@ -3,7 +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 {
|
|
6
|
+
import type { AgentFiles } from './tools/workspace.ts';
|
|
7
7
|
import type { MemoryIndex } from './memory/index.ts';
|
|
8
8
|
import type { Model } from './model.ts';
|
|
9
9
|
import { PayloadResolver, type PayloadStore } from './payload.ts';
|
|
@@ -44,7 +44,7 @@ export interface RunnerOptions<TCtx = unknown> {
|
|
|
44
44
|
payloads?: PayloadStore | PayloadResolver;
|
|
45
45
|
memory?: MemoryIndex;
|
|
46
46
|
/** the agent's workspace, so files can be remembered and copied back */
|
|
47
|
-
files?:
|
|
47
|
+
files?: AgentFiles;
|
|
48
48
|
skills?: SkillProvider[];
|
|
49
49
|
handoffPolicy?: HandoffPolicy;
|
|
50
50
|
/** renders the nodes a policy selected; defaults to a structural note */
|
package/dist/services.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PayloadResolver, type PayloadStore } from './payload.ts';
|
|
2
2
|
import { type IdClock } from './ids.ts';
|
|
3
|
-
import type {
|
|
3
|
+
import type { AgentFiles } from './tools/workspace.ts';
|
|
4
4
|
import type { MemoryIndex } from './memory/index.ts';
|
|
5
5
|
import type { SkillProvider } from './skills.ts';
|
|
6
6
|
/**
|
|
@@ -25,7 +25,7 @@ export declare class Services {
|
|
|
25
25
|
* files reach the sandbox. Supplied by whoever built the workspace, because
|
|
26
26
|
* only it knows the mounts. Without it both are refusals, not crashes.
|
|
27
27
|
*/
|
|
28
|
-
readonly files?:
|
|
28
|
+
readonly files?: AgentFiles;
|
|
29
29
|
/** the driver's clock, so a tool that stamps a node stays replayable */
|
|
30
30
|
readonly clock: IdClock;
|
|
31
31
|
constructor(opts?: {
|
|
@@ -33,7 +33,7 @@ export declare class Services {
|
|
|
33
33
|
memory?: MemoryIndex;
|
|
34
34
|
skills?: SkillProvider[];
|
|
35
35
|
clock?: IdClock;
|
|
36
|
-
files?:
|
|
36
|
+
files?: AgentFiles;
|
|
37
37
|
});
|
|
38
38
|
skillProvider(id: string): SkillProvider;
|
|
39
39
|
hasMemory(): boolean;
|
package/dist/tools/files.d.ts
CHANGED
|
@@ -1,65 +1,6 @@
|
|
|
1
|
-
import type { FileSource, WorkspaceFiles } from '../memory/files.ts';
|
|
2
1
|
import { type AnyTool } from '../types.ts';
|
|
3
|
-
import { type
|
|
4
|
-
|
|
5
|
-
export type WorkspaceOptions = FilesView;
|
|
6
|
-
/** A mount as the workspace holds it: resolved, with its permission decided. */
|
|
7
|
-
export type ResolvedMount = Mount & {
|
|
8
|
-
readOnly: boolean;
|
|
9
|
-
};
|
|
10
|
-
export declare class Workspace implements WorkspaceFiles {
|
|
11
|
-
#private;
|
|
12
|
-
readonly root: string;
|
|
13
|
-
readonly readOnly: boolean;
|
|
14
|
-
readonly mount: string | undefined;
|
|
15
|
-
/** the extra trees, resolved — the primary root is not among them */
|
|
16
|
-
readonly mounts: readonly ResolvedMount[];
|
|
17
|
-
/** where the bytes are; every resolved path is in its namespace */
|
|
18
|
-
readonly fs: FsDriver;
|
|
19
|
-
constructor(opts: WorkspaceOptions);
|
|
20
|
-
/**
|
|
21
|
-
* The one gate. Returns an absolute path inside one of the roots, or throws.
|
|
22
|
-
*
|
|
23
|
-
* A path that does not exist yet cannot be realpath'd, so the nearest
|
|
24
|
-
* existing ancestor is resolved instead and the remainder appended — which
|
|
25
|
-
* is exactly what a create needs and closes the same hole a create opens.
|
|
26
|
-
*/
|
|
27
|
-
within(input: string, opts?: {
|
|
28
|
-
write?: boolean;
|
|
29
|
-
}): string;
|
|
30
|
-
/** Where a path sits under its own root, in posix form. `.` is that root. */
|
|
31
|
-
rel(path: string): string;
|
|
32
|
-
/**
|
|
33
|
-
* How a path is written back to the model, and the reason `mount` is not
|
|
34
|
-
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
35
|
-
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
36
|
-
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
37
|
-
* would have to translate between them to feed one tool's output to the
|
|
38
|
-
* other. With a mount configured every path in and every path out is the
|
|
39
|
-
* mounted one, and copying a path from a command's output into read_file
|
|
40
|
-
* is exact. Without one, everything stays relative as before.
|
|
41
|
-
*/
|
|
42
|
-
show(path: string): string;
|
|
43
|
-
/** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
|
|
44
|
-
isRoot(path: string): boolean;
|
|
45
|
-
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
46
|
-
isInside(inner: string, outer: string): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* A file the agent named, in the form memory copies from: contained like
|
|
49
|
-
* any read, and read through this workspace's driver, so remembering works
|
|
50
|
-
* the same whether the bytes are on this machine or in a remote sandbox.
|
|
51
|
-
*/
|
|
52
|
-
source(path: string): FileSource;
|
|
53
|
-
/**
|
|
54
|
-
* Writes bytes the agent did not type — a remembered file being copied
|
|
55
|
-
* back — under the same rules as `write_file`: contained, refused in a
|
|
56
|
-
* read-only tree or run, and never over an existing file unless asked.
|
|
57
|
-
*/
|
|
58
|
-
write(path: string, data: Uint8Array, opts?: {
|
|
59
|
-
overwrite?: boolean;
|
|
60
|
-
}): Promise<string>;
|
|
61
|
-
mutable(): void;
|
|
62
|
-
}
|
|
2
|
+
import { type WorkspaceOptions } from './workspace.ts';
|
|
3
|
+
export { Workspace, type AgentFile, type AgentFiles, type ResolvedMount, type WorkspaceMount, type WorkspaceOptions, } from './workspace.ts';
|
|
63
4
|
export type FileFormat = 'text' | 'binary' | 'image' | 'audio' | 'video' | 'pdf' | 'archive';
|
|
64
5
|
export interface FileInfo {
|
|
65
6
|
name: string;
|
package/dist/tools/files.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { extname } from 'node:path';
|
|
2
2
|
import { tool } from "../types.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Workspace } from "./workspace.js";
|
|
4
|
+
export { Workspace, } from "./workspace.js";
|
|
4
5
|
// ---------------------------------------------------------------------------
|
|
5
6
|
// File tools
|
|
6
7
|
//
|
|
@@ -27,236 +28,6 @@ const SNIFF_BYTES = 8 * 1024;
|
|
|
27
28
|
const MAX_SCAN = 1024 * 1024;
|
|
28
29
|
/** ...and one listing only spends this much on line counting in total. */
|
|
29
30
|
const LIST_SCAN_BUDGET = 8 * 1024 * 1024;
|
|
30
|
-
export class Workspace {
|
|
31
|
-
root;
|
|
32
|
-
readOnly;
|
|
33
|
-
mount;
|
|
34
|
-
/** the extra trees, resolved — the primary root is not among them */
|
|
35
|
-
mounts;
|
|
36
|
-
/** where the bytes are; every resolved path is in its namespace */
|
|
37
|
-
fs;
|
|
38
|
-
/** primary first */
|
|
39
|
-
#roots;
|
|
40
|
-
/** the ones with a name, longest name first */
|
|
41
|
-
#named;
|
|
42
|
-
/** every root, deepest host path first, for attributing a resolved path to one */
|
|
43
|
-
#deep;
|
|
44
|
-
constructor(opts) {
|
|
45
|
-
this.fs = opts.fs ?? localFs;
|
|
46
|
-
// Resolve the root's own symlinks once, so a workspace that *is* a
|
|
47
|
-
// symlink does not fail every containment check against itself.
|
|
48
|
-
this.root = this.fs.realpathSync(this.fs.path.resolve(opts.root));
|
|
49
|
-
this.readOnly = opts.readOnly ?? false;
|
|
50
|
-
this.mount = mountPoint(opts.mount);
|
|
51
|
-
const roots = [{ path: this.root, at: this.mount, readOnly: this.readOnly }];
|
|
52
|
-
const mounts = [];
|
|
53
|
-
for (const m of opts.mounts ?? []) {
|
|
54
|
-
const at = mountPoint(m.at);
|
|
55
|
-
if (at === undefined) {
|
|
56
|
-
throw new Error(`a workspace mount needs an absolute name of its own: ${m.at}`);
|
|
57
|
-
}
|
|
58
|
-
// Overlapping names would make one path mean two directories, and
|
|
59
|
-
// which one won would depend on the order they were passed in.
|
|
60
|
-
for (const taken of roots) {
|
|
61
|
-
if (taken.at && (at === taken.at || under(at, taken.at) || under(taken.at, at))) {
|
|
62
|
-
throw new Error(`workspace mounts overlap: ${at} and ${taken.at}`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const host = directory(this.fs, m.host, at);
|
|
66
|
-
const readOnly = m.readOnly ?? true;
|
|
67
|
-
roots.push({ path: host, at, readOnly });
|
|
68
|
-
mounts.push({ host, at, readOnly, ...(m.source ? { source: m.source } : {}) });
|
|
69
|
-
}
|
|
70
|
-
this.mounts = mounts;
|
|
71
|
-
this.#roots = roots;
|
|
72
|
-
this.#named = roots
|
|
73
|
-
.filter((r) => r.at !== undefined)
|
|
74
|
-
.sort((a, b) => b.at.length - a.at.length);
|
|
75
|
-
// A mount may sit inside the workspace root; the deeper one owns the path.
|
|
76
|
-
this.#deep = [...roots].sort((a, b) => b.path.length - a.path.length);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* The one gate. Returns an absolute path inside one of the roots, or throws.
|
|
80
|
-
*
|
|
81
|
-
* A path that does not exist yet cannot be realpath'd, so the nearest
|
|
82
|
-
* existing ancestor is resolved instead and the remainder appended — which
|
|
83
|
-
* is exactly what a create needs and closes the same hole a create opens.
|
|
84
|
-
*/
|
|
85
|
-
within(input, opts = {}) {
|
|
86
|
-
if (typeof input !== 'string' || input.length === 0) {
|
|
87
|
-
throw new Error('path is required');
|
|
88
|
-
}
|
|
89
|
-
if (input.includes('\0')) {
|
|
90
|
-
throw new Error('path contains a null byte');
|
|
91
|
-
}
|
|
92
|
-
const { root, rest } = this.#locate(input);
|
|
93
|
-
const { resolve, sep } = this.fs.path;
|
|
94
|
-
const real = this.#realish(resolve(root.path, rest));
|
|
95
|
-
if (real !== root.path && !real.startsWith(root.path + sep)) {
|
|
96
|
-
throw new Error(`outside the workspace: ${input}`);
|
|
97
|
-
}
|
|
98
|
-
if (opts.write && root.readOnly) {
|
|
99
|
-
throw new Error(root.path === this.root
|
|
100
|
-
? 'the workspace is read-only for this run'
|
|
101
|
-
: `${root.at} is read-only`);
|
|
102
|
-
}
|
|
103
|
-
return real;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Which tree a path names, and where in it. `/workspace/src/a.ts` and
|
|
107
|
-
* `src/a.ts` are one file under two names, and only the second resolves
|
|
108
|
-
* against the root, so the mounted spelling is rewritten before anything
|
|
109
|
-
* else looks at it.
|
|
110
|
-
*
|
|
111
|
-
* This is renaming, not permission. What comes out is still resolved, still
|
|
112
|
-
* symlink-followed and still checked against its root by the caller:
|
|
113
|
-
* `/workspace/../etc/passwd` loses its prefix and then fails containment
|
|
114
|
-
* exactly as `../etc/passwd` does.
|
|
115
|
-
*/
|
|
116
|
-
#locate(input) {
|
|
117
|
-
const primary = this.#roots[0];
|
|
118
|
-
// A lone `/` is the model's other name for "the top of what I can see".
|
|
119
|
-
// There is nothing above the root for it to mean, so it means the root
|
|
120
|
-
// — refusing it teaches nothing and costs a turn. This is the only
|
|
121
|
-
// absolute path treated as rooted: `/etc/passwd` still names the
|
|
122
|
-
// machine's file and still fails containment.
|
|
123
|
-
if (/^[\\/]+$/.test(input)) {
|
|
124
|
-
return { root: primary, rest: '.' };
|
|
125
|
-
}
|
|
126
|
-
const at = input.replace(/\\/g, '/');
|
|
127
|
-
for (const root of this.#named) {
|
|
128
|
-
if (at === root.at) {
|
|
129
|
-
return { root, rest: '.' };
|
|
130
|
-
}
|
|
131
|
-
if (at.startsWith(root.at + '/')) {
|
|
132
|
-
return { root, rest: at.slice(root.at.length + 1).replace(/^\/+/, '') || '.' };
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return { root: primary, rest: input };
|
|
136
|
-
}
|
|
137
|
-
/** The root a resolved path belongs to; the primary when nothing else claims it. */
|
|
138
|
-
#owner(path) {
|
|
139
|
-
const { sep } = this.fs.path;
|
|
140
|
-
return (this.#deep.find((r) => path === r.path || path.startsWith(r.path + sep)) ??
|
|
141
|
-
this.#roots[0]);
|
|
142
|
-
}
|
|
143
|
-
#realish(path) {
|
|
144
|
-
const { dirname, join } = this.fs.path;
|
|
145
|
-
const missing = [];
|
|
146
|
-
let at = path;
|
|
147
|
-
for (;;) {
|
|
148
|
-
try {
|
|
149
|
-
return join(this.fs.realpathSync(at), ...missing.reverse());
|
|
150
|
-
}
|
|
151
|
-
catch (err) {
|
|
152
|
-
if (err.code !== 'ENOENT') {
|
|
153
|
-
throw err;
|
|
154
|
-
}
|
|
155
|
-
const parent = dirname(at);
|
|
156
|
-
if (parent === at) {
|
|
157
|
-
// Walked to the filesystem root without finding anything
|
|
158
|
-
// real; nothing here can be inside the workspace.
|
|
159
|
-
throw new Error(`cannot resolve: ${path}`);
|
|
160
|
-
}
|
|
161
|
-
missing.push(at.slice(parent.length + 1));
|
|
162
|
-
at = parent;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
/** Where a path sits under its own root, in posix form. `.` is that root. */
|
|
167
|
-
rel(path) {
|
|
168
|
-
const { relative, sep } = this.fs.path;
|
|
169
|
-
return relative(this.#owner(path).path, path).split(sep).join('/') || '.';
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* How a path is written back to the model, and the reason `mount` is not
|
|
173
|
-
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
174
|
-
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
175
|
-
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
176
|
-
* would have to translate between them to feed one tool's output to the
|
|
177
|
-
* other. With a mount configured every path in and every path out is the
|
|
178
|
-
* mounted one, and copying a path from a command's output into read_file
|
|
179
|
-
* is exact. Without one, everything stays relative as before.
|
|
180
|
-
*/
|
|
181
|
-
show(path) {
|
|
182
|
-
const root = this.#owner(path);
|
|
183
|
-
const rel = this.rel(path);
|
|
184
|
-
if (root.at === undefined) {
|
|
185
|
-
return rel;
|
|
186
|
-
}
|
|
187
|
-
return rel === '.' ? root.at : `${root.at}/${rel}`;
|
|
188
|
-
}
|
|
189
|
-
/** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
|
|
190
|
-
isRoot(path) {
|
|
191
|
-
return this.#roots.some((r) => r.path === path);
|
|
192
|
-
}
|
|
193
|
-
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
194
|
-
isInside(inner, outer) {
|
|
195
|
-
return inner.startsWith(outer + this.fs.path.sep);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* A file the agent named, in the form memory copies from: contained like
|
|
199
|
-
* any read, and read through this workspace's driver, so remembering works
|
|
200
|
-
* the same whether the bytes are on this machine or in a remote sandbox.
|
|
201
|
-
*/
|
|
202
|
-
source(path) {
|
|
203
|
-
const at = this.within(path);
|
|
204
|
-
const fs = this.fs;
|
|
205
|
-
return { path: at, stat: () => fs.stat(at), read: () => fs.readFile(at) };
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Writes bytes the agent did not type — a remembered file being copied
|
|
209
|
-
* back — under the same rules as `write_file`: contained, refused in a
|
|
210
|
-
* read-only tree or run, and never over an existing file unless asked.
|
|
211
|
-
*/
|
|
212
|
-
async write(path, data, opts = {}) {
|
|
213
|
-
this.mutable();
|
|
214
|
-
const at = this.within(path, { write: true });
|
|
215
|
-
if (this.isRoot(at)) {
|
|
216
|
-
throw new Error('refusing to overwrite the workspace root');
|
|
217
|
-
}
|
|
218
|
-
const there = await this.fs.stat(at).catch(() => undefined);
|
|
219
|
-
if (there?.isDirectory()) {
|
|
220
|
-
throw new Error(`${this.show(at)} is a directory`);
|
|
221
|
-
}
|
|
222
|
-
if (there && !opts.overwrite) {
|
|
223
|
-
throw new Error(`${this.show(at)} already exists — pass overwrite to replace it`);
|
|
224
|
-
}
|
|
225
|
-
await this.fs.mkdirp(this.fs.path.dirname(at));
|
|
226
|
-
await this.fs.writeFile(at, data);
|
|
227
|
-
return this.show(at);
|
|
228
|
-
}
|
|
229
|
-
mutable() {
|
|
230
|
-
if (this.readOnly) {
|
|
231
|
-
throw new Error('the workspace is read-only for this run');
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* An absolute name a tree is known by, with any trailing slash removed. A mount
|
|
237
|
-
* of `/` would make everything on the machine look like it was inside, so it is
|
|
238
|
-
* not a mount point; nor is a relative one, which would resolve against nothing.
|
|
239
|
-
*/
|
|
240
|
-
function mountPoint(at) {
|
|
241
|
-
const clean = at?.replace(/\\/g, '/').replace(/\/+$/, '');
|
|
242
|
-
return clean && clean.startsWith('/') ? clean : undefined;
|
|
243
|
-
}
|
|
244
|
-
function under(inner, outer) {
|
|
245
|
-
return inner.startsWith(outer + '/');
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* The host side of a mount, resolved. It has to exist: a mount of nothing is a
|
|
249
|
-
* typo. That it is a directory is left to the first listing, which says so in
|
|
250
|
-
* the model's words — a driver on another machine could not answer sooner.
|
|
251
|
-
*/
|
|
252
|
-
function directory(fs, host, at) {
|
|
253
|
-
try {
|
|
254
|
-
return fs.realpathSync(fs.path.resolve(host));
|
|
255
|
-
}
|
|
256
|
-
catch {
|
|
257
|
-
throw new Error(`workspace mount ${at}: no such directory: ${host}`);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
31
|
/** `a`, `a and b`, `a, b and c` — these end up in sentences the model reads. */
|
|
261
32
|
function nameList(items) {
|
|
262
33
|
if (items.length < 2) {
|
|
@@ -845,7 +616,7 @@ export function fileTools(opts) {
|
|
|
845
616
|
additionalProperties: false,
|
|
846
617
|
},
|
|
847
618
|
execute: async ({ path, start_line, end_line }) => {
|
|
848
|
-
const at = ws.within(path);
|
|
619
|
+
const at = await ws.within(path);
|
|
849
620
|
const info = await fs.stat(at);
|
|
850
621
|
if (info.isDirectory()) {
|
|
851
622
|
return { error: `${ws.show(at)} is a directory`, hint: 'use list_dir' };
|
|
@@ -918,7 +689,7 @@ export function fileTools(opts) {
|
|
|
918
689
|
if (trees && (asked === '' || /^[\\/]+$/.test(asked))) {
|
|
919
690
|
return { path: '/', entries: trees };
|
|
920
691
|
}
|
|
921
|
-
const at = root(path);
|
|
692
|
+
const at = await root(path);
|
|
922
693
|
const entries = await fs.readdir(at);
|
|
923
694
|
const listed = [];
|
|
924
695
|
let budget = LIST_SCAN_BUDGET;
|
|
@@ -973,7 +744,7 @@ export function fileTools(opts) {
|
|
|
973
744
|
if (content.length > MAX_WRITE) {
|
|
974
745
|
return { error: `content exceeds ${MAX_WRITE} bytes` };
|
|
975
746
|
}
|
|
976
|
-
const at = ws.within(path, { write: true });
|
|
747
|
+
const at = await ws.within(path, { write: true });
|
|
977
748
|
await fs.mkdirp(dirname(at));
|
|
978
749
|
await fs.writeFile(at, content);
|
|
979
750
|
return {
|
|
@@ -1056,8 +827,8 @@ export function fileTools(opts) {
|
|
|
1056
827
|
},
|
|
1057
828
|
execute: async ({ from, to, overwrite }) => {
|
|
1058
829
|
ws.mutable();
|
|
1059
|
-
const source = ws.within(from, { write: true });
|
|
1060
|
-
const target = ws.within(to, { write: true });
|
|
830
|
+
const source = await ws.within(from, { write: true });
|
|
831
|
+
const target = await ws.within(to, { write: true });
|
|
1061
832
|
if (ws.isRoot(source)) {
|
|
1062
833
|
return { error: 'refusing to move the workspace root' };
|
|
1063
834
|
}
|
|
@@ -1119,8 +890,8 @@ export function fileTools(opts) {
|
|
|
1119
890
|
},
|
|
1120
891
|
execute: async ({ from, to, overwrite, recursive }) => {
|
|
1121
892
|
ws.mutable();
|
|
1122
|
-
const source = ws.within(from);
|
|
1123
|
-
const target = ws.within(to, { write: true });
|
|
893
|
+
const source = await ws.within(from);
|
|
894
|
+
const target = await ws.within(to, { write: true });
|
|
1124
895
|
if (ws.isRoot(target)) {
|
|
1125
896
|
return { error: 'refusing to overwrite the workspace root' };
|
|
1126
897
|
}
|
|
@@ -1174,7 +945,7 @@ export function fileTools(opts) {
|
|
|
1174
945
|
},
|
|
1175
946
|
execute: async ({ path, recursive }) => {
|
|
1176
947
|
ws.mutable();
|
|
1177
|
-
const at = ws.within(path, { write: true });
|
|
948
|
+
const at = await ws.within(path, { write: true });
|
|
1178
949
|
if (ws.isRoot(at)) {
|
|
1179
950
|
return { error: 'refusing to delete the workspace root' };
|
|
1180
951
|
}
|
|
@@ -1217,9 +988,7 @@ export function fileTools(opts) {
|
|
|
1217
988
|
// workspace would leave them behind a path it has to know to ask
|
|
1218
989
|
// for. A tree nested inside the workspace is walked twice, so the
|
|
1219
990
|
// hits are a set: `show` gives one file one name either way.
|
|
1220
|
-
const where = path?.trim()
|
|
1221
|
-
? [ws.within(path)]
|
|
1222
|
-
: [ws.root, ...ws.mounts.map((m) => m.host)];
|
|
991
|
+
const where = path?.trim() ? [await ws.within(path)] : await ws.trees();
|
|
1223
992
|
const needle = pattern.toLowerCase();
|
|
1224
993
|
const hits = new Set();
|
|
1225
994
|
for (const dir of where) {
|
|
@@ -1264,6 +1033,7 @@ const ERRNO = {
|
|
|
1264
1033
|
EEXIST: 'already exists',
|
|
1265
1034
|
ENOTEMPTY: 'is not empty',
|
|
1266
1035
|
EACCES: 'cannot be reached',
|
|
1036
|
+
EROFS: 'is read-only',
|
|
1267
1037
|
EPERM: 'cannot be reached',
|
|
1268
1038
|
ELOOP: 'is a loop of symlinks',
|
|
1269
1039
|
};
|
|
@@ -1311,7 +1081,7 @@ async function runPatch(ws, patch) {
|
|
|
1311
1081
|
let fuzz = 0;
|
|
1312
1082
|
let ignored = 0;
|
|
1313
1083
|
for (const op of ops) {
|
|
1314
|
-
const at = patchPath(ws, op.path);
|
|
1084
|
+
const at = await patchPath(ws, op.path);
|
|
1315
1085
|
if (ws.isRoot(at)) {
|
|
1316
1086
|
throw new PatchError(`${op.path} is the workspace root`);
|
|
1317
1087
|
}
|
|
@@ -1391,7 +1161,7 @@ async function runPatch(ws, patch) {
|
|
|
1391
1161
|
lines: patched.lines.length,
|
|
1392
1162
|
};
|
|
1393
1163
|
if (op.moveTo) {
|
|
1394
|
-
const to = patchPath(ws, op.moveTo);
|
|
1164
|
+
const to = await patchPath(ws, op.moveTo);
|
|
1395
1165
|
if (ws.isRoot(to)) {
|
|
1396
1166
|
throw new PatchError('cannot move a file onto the workspace root');
|
|
1397
1167
|
}
|
|
@@ -1452,9 +1222,9 @@ async function statOrNull(fs, at) {
|
|
|
1452
1222
|
* behind — a read-only file named halfway down is a reason to reject the whole
|
|
1453
1223
|
* patch, not to write the files above it and then throw.
|
|
1454
1224
|
*/
|
|
1455
|
-
function patchPath(ws, path) {
|
|
1225
|
+
async function patchPath(ws, path) {
|
|
1456
1226
|
try {
|
|
1457
|
-
return ws.within(path, { write: true });
|
|
1227
|
+
return await ws.within(path, { write: true });
|
|
1458
1228
|
}
|
|
1459
1229
|
catch (err) {
|
|
1460
1230
|
throw new PatchError(err instanceof Error ? err.message : String(err));
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { type FsDriver, type MountSource } from '@zenera/sandbox';
|
|
2
|
+
/**
|
|
3
|
+
* The agent's files, as something other than its file tools touches them:
|
|
4
|
+
* contained and permission-checked exactly as those tools are. `Workspace` is
|
|
5
|
+
* one.
|
|
6
|
+
*/
|
|
7
|
+
export interface AgentFiles {
|
|
8
|
+
/** a file, whole; refuses a directory, and a file over `maxBytes` before reading it */
|
|
9
|
+
read(path: string, opts?: {
|
|
10
|
+
maxBytes?: number;
|
|
11
|
+
}): Promise<AgentFile>;
|
|
12
|
+
/**
|
|
13
|
+
* Writes bytes and answers with the path the model knows the file by.
|
|
14
|
+
* Refuses to replace an existing file unless told to.
|
|
15
|
+
*/
|
|
16
|
+
write(path: string, data: Uint8Array, opts?: {
|
|
17
|
+
overwrite?: boolean;
|
|
18
|
+
}): Promise<string>;
|
|
19
|
+
}
|
|
20
|
+
export interface AgentFile {
|
|
21
|
+
/** the name the model knows it by */
|
|
22
|
+
path: string;
|
|
23
|
+
data: Uint8Array;
|
|
24
|
+
}
|
|
25
|
+
/** A tree beside the workspace: its name, and — without a sandbox — its directory here. */
|
|
26
|
+
export interface WorkspaceMount {
|
|
27
|
+
/** the absolute name the agent reaches it by, e.g. `/assets` */
|
|
28
|
+
at: string;
|
|
29
|
+
/** the directory on this machine; only read when the workspace builds its own `fs` */
|
|
30
|
+
host?: string;
|
|
31
|
+
/** read-only unless it says otherwise: an extra tree is reference material */
|
|
32
|
+
readOnly?: boolean;
|
|
33
|
+
source?: MountSource;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The trees the file tools reach. Two ways to say it, one result:
|
|
37
|
+
*
|
|
38
|
+
* - with `fs` — a sandbox's own, already contained — `root` is the workspace's
|
|
39
|
+
* name in it (`/workspace`) and each mount is a name in it too;
|
|
40
|
+
* - without, for a host with no sandbox, `root` and each mount's `host` are
|
|
41
|
+
* directories on this machine, and the workspace contains them itself with
|
|
42
|
+
* the same `RootedFs` a podman sandbox uses.
|
|
43
|
+
*
|
|
44
|
+
* `mount` is the name paths are shown by; unset, the workspace's own paths are
|
|
45
|
+
* shown relative to it.
|
|
46
|
+
*/
|
|
47
|
+
export interface WorkspaceOptions {
|
|
48
|
+
root: string;
|
|
49
|
+
mount?: string;
|
|
50
|
+
mounts?: readonly WorkspaceMount[];
|
|
51
|
+
/** refuse every mutating tool; `zen run --read-only` */
|
|
52
|
+
readOnly?: boolean;
|
|
53
|
+
fs?: FsDriver;
|
|
54
|
+
}
|
|
55
|
+
/** One tree the agent can reach, by name, and what it may do there. */
|
|
56
|
+
interface Tree {
|
|
57
|
+
at: string;
|
|
58
|
+
readOnly: boolean;
|
|
59
|
+
/** for saying which directory is missing, when there is one */
|
|
60
|
+
host?: string;
|
|
61
|
+
}
|
|
62
|
+
/** A mount as the workspace holds it: named, with its permission decided. */
|
|
63
|
+
export type ResolvedMount = Required<Pick<WorkspaceMount, 'at' | 'readOnly'>> & Pick<WorkspaceMount, 'source'>;
|
|
64
|
+
/**
|
|
65
|
+
* What an agent may do with its files, on top of a filesystem that already
|
|
66
|
+
* decides what it may reach. Every path here is the agent's own: it comes in as
|
|
67
|
+
* the model spelled it, is made the one name the fs knows, and goes back out in
|
|
68
|
+
* the model's spelling. Containment — symlinks, `..`, read-only trees — is the
|
|
69
|
+
* fs's; what is left is the tools' own rules.
|
|
70
|
+
*/
|
|
71
|
+
export declare class Workspace implements AgentFiles {
|
|
72
|
+
#private;
|
|
73
|
+
/** the workspace's name in `fs`, and where relative paths start */
|
|
74
|
+
readonly root: string;
|
|
75
|
+
readonly readOnly: boolean;
|
|
76
|
+
/** the name paths are shown by; unset, workspace paths are shown relative */
|
|
77
|
+
readonly mount: string | undefined;
|
|
78
|
+
/** the extra trees — the workspace itself is not among them */
|
|
79
|
+
readonly mounts: readonly ResolvedMount[];
|
|
80
|
+
/** the agent's files, contained, in the agent's paths */
|
|
81
|
+
readonly fs: FsDriver;
|
|
82
|
+
constructor(opts: WorkspaceOptions);
|
|
83
|
+
/** Every tree by name: the workspace first. */
|
|
84
|
+
trees(): string[];
|
|
85
|
+
/**
|
|
86
|
+
* The one gate: the name the fs knows a path by, or a refusal in the
|
|
87
|
+
* model's words. What the fs then does with it is contained by the fs.
|
|
88
|
+
*/
|
|
89
|
+
within(input: string, opts?: {
|
|
90
|
+
write?: boolean;
|
|
91
|
+
}): Promise<string>;
|
|
92
|
+
/**
|
|
93
|
+
* Which tree a path names, and its one name there. `/workspace/src/a.ts`
|
|
94
|
+
* and `src/a.ts` are one file under two names; a lone `/` is the model's
|
|
95
|
+
* other name for the top of what it can see, so it means the workspace;
|
|
96
|
+
* `..` that climbs out of a tree, and any other absolute path, names
|
|
97
|
+
* nothing. Lexical: no file is touched.
|
|
98
|
+
*/
|
|
99
|
+
locate(input: string): {
|
|
100
|
+
tree: Tree;
|
|
101
|
+
at: string;
|
|
102
|
+
} | undefined;
|
|
103
|
+
/** Where a path sits under its own tree, in posix form. `.` is that tree. */
|
|
104
|
+
rel(path: string): string;
|
|
105
|
+
/**
|
|
106
|
+
* How a path is written back to the model, and the reason `mount` is not
|
|
107
|
+
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
108
|
+
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
109
|
+
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
110
|
+
* would have to translate between them to feed one tool's output to the
|
|
111
|
+
* other. With a mount configured every path in and every path out is the
|
|
112
|
+
* mounted one, and copying a path from a command's output into read_file
|
|
113
|
+
* is exact. Without one, the workspace's own paths stay relative.
|
|
114
|
+
*/
|
|
115
|
+
show(path: string): string;
|
|
116
|
+
/** Whether a path *is* one of the trees. Nothing may move or delete one. */
|
|
117
|
+
isRoot(path: string): boolean;
|
|
118
|
+
/** Whether `inner` sits strictly below `outer`. */
|
|
119
|
+
isInside(inner: string, outer: string): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* A file the agent named, whole: contained like any read, and read through
|
|
122
|
+
* this workspace's driver, so it is the same whether the bytes are on this
|
|
123
|
+
* machine or in a remote sandbox. Memory copies a file in through this.
|
|
124
|
+
*/
|
|
125
|
+
read(path: string, opts?: {
|
|
126
|
+
maxBytes?: number;
|
|
127
|
+
}): Promise<AgentFile>;
|
|
128
|
+
/**
|
|
129
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
130
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
131
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
132
|
+
*/
|
|
133
|
+
write(path: string, data: Uint8Array, opts?: {
|
|
134
|
+
overwrite?: boolean;
|
|
135
|
+
}): Promise<string>;
|
|
136
|
+
mutable(): void;
|
|
137
|
+
}
|
|
138
|
+
export {};
|
|
139
|
+
//# sourceMappingURL=workspace.d.ts.map
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
import { RootedFs } from '@zenera/sandbox';
|
|
3
|
+
/** Why a read was refused: `ENOENT`, `EISDIR` or `EFBIG`, as `node:fs` would say. */
|
|
4
|
+
function agentFileError(code, message) {
|
|
5
|
+
const err = new Error(message);
|
|
6
|
+
err.code = code;
|
|
7
|
+
return err;
|
|
8
|
+
}
|
|
9
|
+
/** The name the workspace has inside a contained fs when nobody named it. */
|
|
10
|
+
const LOCAL_ROOT = '/workspace';
|
|
11
|
+
/**
|
|
12
|
+
* What an agent may do with its files, on top of a filesystem that already
|
|
13
|
+
* decides what it may reach. Every path here is the agent's own: it comes in as
|
|
14
|
+
* the model spelled it, is made the one name the fs knows, and goes back out in
|
|
15
|
+
* the model's spelling. Containment — symlinks, `..`, read-only trees — is the
|
|
16
|
+
* fs's; what is left is the tools' own rules.
|
|
17
|
+
*/
|
|
18
|
+
export class Workspace {
|
|
19
|
+
/** the workspace's name in `fs`, and where relative paths start */
|
|
20
|
+
root;
|
|
21
|
+
readOnly;
|
|
22
|
+
/** the name paths are shown by; unset, workspace paths are shown relative */
|
|
23
|
+
mount;
|
|
24
|
+
/** the extra trees — the workspace itself is not among them */
|
|
25
|
+
mounts;
|
|
26
|
+
/** the agent's files, contained, in the agent's paths */
|
|
27
|
+
fs;
|
|
28
|
+
/** the workspace first */
|
|
29
|
+
#trees;
|
|
30
|
+
/** longest name first, for finding the tree a path is in */
|
|
31
|
+
#byName;
|
|
32
|
+
#ready;
|
|
33
|
+
constructor(opts) {
|
|
34
|
+
this.readOnly = opts.readOnly ?? false;
|
|
35
|
+
this.mount = mountPoint(opts.mount);
|
|
36
|
+
const local = opts.fs === undefined;
|
|
37
|
+
this.root = local ? (this.mount ?? LOCAL_ROOT) : (mountPoint(opts.root) ?? LOCAL_ROOT);
|
|
38
|
+
const trees = [
|
|
39
|
+
{ at: this.root, readOnly: this.readOnly, ...(local ? { host: opts.root } : {}) },
|
|
40
|
+
];
|
|
41
|
+
const mounts = [];
|
|
42
|
+
for (const m of opts.mounts ?? []) {
|
|
43
|
+
const at = mountPoint(m.at);
|
|
44
|
+
if (at === undefined) {
|
|
45
|
+
throw new Error(`a workspace mount needs an absolute name of its own: ${m.at}`);
|
|
46
|
+
}
|
|
47
|
+
// Overlapping names would make one path mean two directories, and
|
|
48
|
+
// which one won would depend on the order they were passed in.
|
|
49
|
+
for (const taken of trees) {
|
|
50
|
+
if (at === taken.at || under(at, taken.at) || under(taken.at, at)) {
|
|
51
|
+
throw new Error(`workspace mounts overlap: ${at} and ${taken.at}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (local && !m.host) {
|
|
55
|
+
throw new Error(`workspace mount ${at} needs a host directory`);
|
|
56
|
+
}
|
|
57
|
+
const readOnly = m.readOnly ?? true;
|
|
58
|
+
trees.push({ at, readOnly, ...(local ? { host: m.host } : {}) });
|
|
59
|
+
mounts.push({ at, readOnly, ...(m.source ? { source: m.source } : {}) });
|
|
60
|
+
}
|
|
61
|
+
this.mounts = mounts;
|
|
62
|
+
this.#trees = trees;
|
|
63
|
+
this.#byName = [...trees].sort((a, b) => b.at.length - a.at.length);
|
|
64
|
+
this.fs =
|
|
65
|
+
opts.fs ??
|
|
66
|
+
new RootedFs(trees.map((t) => ({ at: t.at, dir: t.host, readOnly: t.readOnly })));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Every tree has to be there. Asked once, on first use rather than at
|
|
70
|
+
* construction: the fs may be on another machine, and asking is a round trip.
|
|
71
|
+
*/
|
|
72
|
+
#settle() {
|
|
73
|
+
this.#ready ??= (async () => {
|
|
74
|
+
for (const [i, tree] of this.#trees.entries()) {
|
|
75
|
+
// There at all; that it is a directory is the first listing's to say.
|
|
76
|
+
const info = await this.fs.stat(tree.at).catch(() => undefined);
|
|
77
|
+
if (!info) {
|
|
78
|
+
const where = tree.host ?? tree.at;
|
|
79
|
+
throw new Error(i === 0
|
|
80
|
+
? `the workspace does not exist: ${where}`
|
|
81
|
+
: `workspace mount ${tree.at}: no such directory: ${where}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
})().catch((err) => {
|
|
85
|
+
this.#ready = undefined;
|
|
86
|
+
throw err;
|
|
87
|
+
});
|
|
88
|
+
return this.#ready;
|
|
89
|
+
}
|
|
90
|
+
/** Every tree by name: the workspace first. */
|
|
91
|
+
trees() {
|
|
92
|
+
return this.#trees.map((t) => t.at);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The one gate: the name the fs knows a path by, or a refusal in the
|
|
96
|
+
* model's words. What the fs then does with it is contained by the fs.
|
|
97
|
+
*/
|
|
98
|
+
async within(input, opts = {}) {
|
|
99
|
+
if (typeof input !== 'string' || input.length === 0) {
|
|
100
|
+
throw new Error('path is required');
|
|
101
|
+
}
|
|
102
|
+
if (input.includes('\0')) {
|
|
103
|
+
throw new Error('path contains a null byte');
|
|
104
|
+
}
|
|
105
|
+
await this.#settle();
|
|
106
|
+
const found = this.locate(input);
|
|
107
|
+
if (!found) {
|
|
108
|
+
throw new Error(`outside the workspace: ${input}`);
|
|
109
|
+
}
|
|
110
|
+
if (opts.write && found.tree.readOnly) {
|
|
111
|
+
throw new Error(found.tree === this.#trees[0]
|
|
112
|
+
? 'the workspace is read-only for this run'
|
|
113
|
+
: `${found.tree.at} is read-only`);
|
|
114
|
+
}
|
|
115
|
+
return found.at;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Which tree a path names, and its one name there. `/workspace/src/a.ts`
|
|
119
|
+
* and `src/a.ts` are one file under two names; a lone `/` is the model's
|
|
120
|
+
* other name for the top of what it can see, so it means the workspace;
|
|
121
|
+
* `..` that climbs out of a tree, and any other absolute path, names
|
|
122
|
+
* nothing. Lexical: no file is touched.
|
|
123
|
+
*/
|
|
124
|
+
locate(input) {
|
|
125
|
+
const primary = this.#trees[0];
|
|
126
|
+
if (/^[\\/]+$/.test(input)) {
|
|
127
|
+
return { tree: primary, at: primary.at };
|
|
128
|
+
}
|
|
129
|
+
const spelled = input.replace(/\\/g, '/');
|
|
130
|
+
const at = trim(posix.normalize(spelled.startsWith('/') ? spelled : posix.join(primary.at, spelled)));
|
|
131
|
+
const tree = this.#owner(at);
|
|
132
|
+
// Unnamed, the workspace is only ever spelled relative: an absolute
|
|
133
|
+
// path is the machine's, and the machine's is not in reach.
|
|
134
|
+
if (tree === primary && spelled.startsWith('/') && this.mount === undefined) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
return tree ? { tree, at } : undefined;
|
|
138
|
+
}
|
|
139
|
+
#owner(at) {
|
|
140
|
+
return this.#byName.find((t) => at === t.at || under(at, t.at));
|
|
141
|
+
}
|
|
142
|
+
/** Where a path sits under its own tree, in posix form. `.` is that tree. */
|
|
143
|
+
rel(path) {
|
|
144
|
+
const tree = this.#owner(path);
|
|
145
|
+
return tree ? posix.relative(tree.at, path) || '.' : path;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* How a path is written back to the model, and the reason `mount` is not
|
|
149
|
+
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
150
|
+
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
151
|
+
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
152
|
+
* would have to translate between them to feed one tool's output to the
|
|
153
|
+
* other. With a mount configured every path in and every path out is the
|
|
154
|
+
* mounted one, and copying a path from a command's output into read_file
|
|
155
|
+
* is exact. Without one, the workspace's own paths stay relative.
|
|
156
|
+
*/
|
|
157
|
+
show(path) {
|
|
158
|
+
if (this.mount === undefined && this.#owner(path) === this.#trees[0]) {
|
|
159
|
+
return this.rel(path);
|
|
160
|
+
}
|
|
161
|
+
return path;
|
|
162
|
+
}
|
|
163
|
+
/** Whether a path *is* one of the trees. Nothing may move or delete one. */
|
|
164
|
+
isRoot(path) {
|
|
165
|
+
return this.#trees.some((t) => t.at === path);
|
|
166
|
+
}
|
|
167
|
+
/** Whether `inner` sits strictly below `outer`. */
|
|
168
|
+
isInside(inner, outer) {
|
|
169
|
+
return under(inner, outer);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A file the agent named, whole: contained like any read, and read through
|
|
173
|
+
* this workspace's driver, so it is the same whether the bytes are on this
|
|
174
|
+
* machine or in a remote sandbox. Memory copies a file in through this.
|
|
175
|
+
*/
|
|
176
|
+
async read(path, opts = {}) {
|
|
177
|
+
const at = await this.within(path);
|
|
178
|
+
const info = await this.fs.stat(at).catch(() => undefined);
|
|
179
|
+
if (!info) {
|
|
180
|
+
throw agentFileError('ENOENT', `${path} does not exist`);
|
|
181
|
+
}
|
|
182
|
+
if (!info.isFile()) {
|
|
183
|
+
throw agentFileError('EISDIR', `${path} is not a regular file`);
|
|
184
|
+
}
|
|
185
|
+
// Asked before reading, so a file far over the cap is never pulled across.
|
|
186
|
+
if (opts.maxBytes !== undefined && info.size > opts.maxBytes) {
|
|
187
|
+
throw agentFileError('EFBIG', `${path} is ${info.size} bytes, over the ${opts.maxBytes} byte limit`);
|
|
188
|
+
}
|
|
189
|
+
const data = await this.fs.readFile(at);
|
|
190
|
+
if (opts.maxBytes !== undefined && data.byteLength > opts.maxBytes) {
|
|
191
|
+
throw agentFileError('EFBIG', `${path} grew past the ${opts.maxBytes} byte limit`);
|
|
192
|
+
}
|
|
193
|
+
return { path: this.show(at), data };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
197
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
198
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
199
|
+
*/
|
|
200
|
+
async write(path, data, opts = {}) {
|
|
201
|
+
this.mutable();
|
|
202
|
+
const at = await this.within(path, { write: true });
|
|
203
|
+
if (this.isRoot(at)) {
|
|
204
|
+
throw new Error('refusing to overwrite the workspace root');
|
|
205
|
+
}
|
|
206
|
+
const there = await this.fs.stat(at).catch(() => undefined);
|
|
207
|
+
if (there?.isDirectory()) {
|
|
208
|
+
throw new Error(`${this.show(at)} is a directory`);
|
|
209
|
+
}
|
|
210
|
+
if (there && !opts.overwrite) {
|
|
211
|
+
throw new Error(`${this.show(at)} already exists — pass overwrite to replace it`);
|
|
212
|
+
}
|
|
213
|
+
await this.fs.mkdirp(this.fs.path.dirname(at));
|
|
214
|
+
await this.fs.writeFile(at, data);
|
|
215
|
+
return this.show(at);
|
|
216
|
+
}
|
|
217
|
+
mutable() {
|
|
218
|
+
if (this.readOnly) {
|
|
219
|
+
throw new Error('the workspace is read-only for this run');
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* An absolute name a tree is known by, with any trailing slash removed. A mount
|
|
225
|
+
* of `/` would make everything on the machine look like it was inside, so it is
|
|
226
|
+
* not a mount point; nor is a relative one, which would resolve against nothing.
|
|
227
|
+
*/
|
|
228
|
+
function mountPoint(at) {
|
|
229
|
+
const clean = at?.replace(/\\/g, '/').replace(/\/+$/, '');
|
|
230
|
+
return clean && clean.startsWith('/') ? posix.normalize(clean) : undefined;
|
|
231
|
+
}
|
|
232
|
+
function trim(at) {
|
|
233
|
+
return at.length > 1 ? at.replace(/\/+$/, '') : at;
|
|
234
|
+
}
|
|
235
|
+
function under(inner, outer) {
|
|
236
|
+
return inner.startsWith(outer + '/');
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=workspace.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenera/neo",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.59",
|
|
4
4
|
"description": "A multi-agent runtime for Node.js: agents, models, tools, skills, memory and an append-only trajectory.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"prepack": "tsc -b"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@zenera/sandbox": "^1.1.
|
|
65
|
+
"@zenera/sandbox": "^1.1.59",
|
|
66
66
|
"graphology": "^0.26.0",
|
|
67
67
|
"yaml": "^2.9.1",
|
|
68
68
|
"zod": "^4.6.7"
|