@zenera/neo 1.1.56 → 1.1.58
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/config.js +9 -1
- 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 +15 -246
- package/dist/tools/identity.d.ts +12 -0
- package/dist/tools/identity.js +14 -0
- package/dist/tools/workspace.d.ts +95 -0
- package/dist/tools/workspace.js +258 -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/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parse as parseYaml } from 'yaml';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { IDENTITY_ENV_PREFIX } from "../tools/identity.js";
|
|
3
|
+
import { IDENTITY_ENV_PREFIX, isHostOnlyEnv } from "../tools/identity.js";
|
|
4
4
|
// ---------------------------------------------------------------------------
|
|
5
5
|
// agents.yaml
|
|
6
6
|
// ---------------------------------------------------------------------------
|
|
@@ -327,6 +327,14 @@ const sandbox = z
|
|
|
327
327
|
`a project cannot forward its own ${IDENTITY_ENV_PREFIX}* variables`,
|
|
328
328
|
});
|
|
329
329
|
}
|
|
330
|
+
else if (isHostOnlyEnv(name)) {
|
|
331
|
+
ctx.addIssue({
|
|
332
|
+
code: 'custom',
|
|
333
|
+
path: ['env', i],
|
|
334
|
+
message: `${name} configures the host — which plugins it loads and which ` +
|
|
335
|
+
'sandbox service it uses — and is never forwarded into a sandbox',
|
|
336
|
+
});
|
|
337
|
+
}
|
|
330
338
|
}
|
|
331
339
|
});
|
|
332
340
|
const agent = z
|
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 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) {
|
|
@@ -1311,7 +1080,7 @@ async function runPatch(ws, patch) {
|
|
|
1311
1080
|
let fuzz = 0;
|
|
1312
1081
|
let ignored = 0;
|
|
1313
1082
|
for (const op of ops) {
|
|
1314
|
-
const at = patchPath(ws, op.path);
|
|
1083
|
+
const at = await patchPath(ws, op.path);
|
|
1315
1084
|
if (ws.isRoot(at)) {
|
|
1316
1085
|
throw new PatchError(`${op.path} is the workspace root`);
|
|
1317
1086
|
}
|
|
@@ -1391,7 +1160,7 @@ async function runPatch(ws, patch) {
|
|
|
1391
1160
|
lines: patched.lines.length,
|
|
1392
1161
|
};
|
|
1393
1162
|
if (op.moveTo) {
|
|
1394
|
-
const to = patchPath(ws, op.moveTo);
|
|
1163
|
+
const to = await patchPath(ws, op.moveTo);
|
|
1395
1164
|
if (ws.isRoot(to)) {
|
|
1396
1165
|
throw new PatchError('cannot move a file onto the workspace root');
|
|
1397
1166
|
}
|
|
@@ -1452,9 +1221,9 @@ async function statOrNull(fs, at) {
|
|
|
1452
1221
|
* behind — a read-only file named halfway down is a reason to reject the whole
|
|
1453
1222
|
* patch, not to write the files above it and then throw.
|
|
1454
1223
|
*/
|
|
1455
|
-
function patchPath(ws, path) {
|
|
1224
|
+
async function patchPath(ws, path) {
|
|
1456
1225
|
try {
|
|
1457
|
-
return ws.within(path, { write: true });
|
|
1226
|
+
return await ws.within(path, { write: true });
|
|
1458
1227
|
}
|
|
1459
1228
|
catch (err) {
|
|
1460
1229
|
throw new PatchError(err instanceof Error ? err.message : String(err));
|
package/dist/tools/identity.d.ts
CHANGED
|
@@ -28,6 +28,18 @@ export declare const IDENTITY_ENV: {
|
|
|
28
28
|
/** Every name under it is the host's to set; a project may not. */
|
|
29
29
|
export declare const IDENTITY_ENV_PREFIX = "ZEN_USER_";
|
|
30
30
|
export declare function isIdentityEnv(name: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Where a sandbox service is and the credential that reaches it — read by the
|
|
33
|
+
* plugin that provides the sandbox, from the machine's environment only.
|
|
34
|
+
*/
|
|
35
|
+
export declare const SANDBOX_SERVICE_ENV_PREFIX = "ZEN_SANDBOX_";
|
|
36
|
+
/**
|
|
37
|
+
* Variables that are the host's alone: who a run acts for, which plugins load
|
|
38
|
+
* into the engine, and which sandbox service it uses. A project that could set
|
|
39
|
+
* one in a committed file — or forward one into the sandbox it runs code in —
|
|
40
|
+
* could be anyone, run anything, or send its users' work anywhere.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isHostOnlyEnv(name: string): boolean;
|
|
31
43
|
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
32
44
|
export declare function identityEnv(identity: RunIdentity | undefined): Record<string, string>;
|
|
33
45
|
/**
|
package/dist/tools/identity.js
CHANGED
|
@@ -27,6 +27,20 @@ export const IDENTITY_ENV_PREFIX = 'ZEN_USER_';
|
|
|
27
27
|
export function isIdentityEnv(name) {
|
|
28
28
|
return name.startsWith(IDENTITY_ENV_PREFIX);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Where a sandbox service is and the credential that reaches it — read by the
|
|
32
|
+
* plugin that provides the sandbox, from the machine's environment only.
|
|
33
|
+
*/
|
|
34
|
+
export const SANDBOX_SERVICE_ENV_PREFIX = 'ZEN_SANDBOX_';
|
|
35
|
+
/**
|
|
36
|
+
* Variables that are the host's alone: who a run acts for, which plugins load
|
|
37
|
+
* into the engine, and which sandbox service it uses. A project that could set
|
|
38
|
+
* one in a committed file — or forward one into the sandbox it runs code in —
|
|
39
|
+
* could be anyone, run anything, or send its users' work anywhere.
|
|
40
|
+
*/
|
|
41
|
+
export function isHostOnlyEnv(name) {
|
|
42
|
+
return (isIdentityEnv(name) || name === 'ZEN_PLUGINS' || name.startsWith(SANDBOX_SERVICE_ENV_PREFIX));
|
|
43
|
+
}
|
|
30
44
|
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
31
45
|
export function identityEnv(identity) {
|
|
32
46
|
if (!identity) {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type FilesView, type FsDriver, type Mount } 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
|
+
/** The trees the file tools reach — `FilesView` from `@zenera/sandbox`, under the name these tools use. */
|
|
26
|
+
export type WorkspaceOptions = FilesView;
|
|
27
|
+
/** A mount as the workspace holds it: resolved, with its permission decided. */
|
|
28
|
+
export type ResolvedMount = Mount & {
|
|
29
|
+
readOnly: boolean;
|
|
30
|
+
};
|
|
31
|
+
export declare class Workspace implements AgentFiles {
|
|
32
|
+
#private;
|
|
33
|
+
/** the workspace as given, made absolute; where it really is is settled on first use */
|
|
34
|
+
readonly root: string;
|
|
35
|
+
readonly readOnly: boolean;
|
|
36
|
+
readonly mount: string | undefined;
|
|
37
|
+
/** the extra trees, resolved — the primary root is not among them */
|
|
38
|
+
readonly mounts: readonly ResolvedMount[];
|
|
39
|
+
/** where the bytes are; every resolved path is in its namespace */
|
|
40
|
+
readonly fs: FsDriver;
|
|
41
|
+
constructor(opts: WorkspaceOptions);
|
|
42
|
+
/** Every tree, where it really is: the workspace first. */
|
|
43
|
+
trees(): Promise<string[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Where a name points on this machine, lexically: no symlink followed, no
|
|
46
|
+
* file touched. For drawing a link to a file, never for deciding access —
|
|
47
|
+
* that is `within`.
|
|
48
|
+
*/
|
|
49
|
+
hostOf(input: string): string | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* The one gate. Returns an absolute path inside one of the roots, or throws.
|
|
52
|
+
*
|
|
53
|
+
* A path that does not exist yet cannot be realpath'd, so the nearest
|
|
54
|
+
* existing ancestor is resolved instead and the remainder appended — which
|
|
55
|
+
* is exactly what a create needs and closes the same hole a create opens.
|
|
56
|
+
*/
|
|
57
|
+
within(input: string, opts?: {
|
|
58
|
+
write?: boolean;
|
|
59
|
+
}): Promise<string>;
|
|
60
|
+
/** Where a path sits under its own root, in posix form. `.` is that root. */
|
|
61
|
+
rel(path: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* How a path is written back to the model, and the reason `mount` is not
|
|
64
|
+
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
65
|
+
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
66
|
+
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
67
|
+
* would have to translate between them to feed one tool's output to the
|
|
68
|
+
* other. With a mount configured every path in and every path out is the
|
|
69
|
+
* mounted one, and copying a path from a command's output into read_file
|
|
70
|
+
* is exact. Without one, everything stays relative as before.
|
|
71
|
+
*/
|
|
72
|
+
show(path: string): string;
|
|
73
|
+
/** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
|
|
74
|
+
isRoot(path: string): boolean;
|
|
75
|
+
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
76
|
+
isInside(inner: string, outer: string): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* A file the agent named, whole: contained like any read, and read through
|
|
79
|
+
* this workspace's driver, so it is the same whether the bytes are on this
|
|
80
|
+
* machine or in a remote sandbox. Memory copies a file in through this.
|
|
81
|
+
*/
|
|
82
|
+
read(path: string, opts?: {
|
|
83
|
+
maxBytes?: number;
|
|
84
|
+
}): Promise<AgentFile>;
|
|
85
|
+
/**
|
|
86
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
87
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
88
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
89
|
+
*/
|
|
90
|
+
write(path: string, data: Uint8Array, opts?: {
|
|
91
|
+
overwrite?: boolean;
|
|
92
|
+
}): Promise<string>;
|
|
93
|
+
mutable(): void;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=workspace.d.ts.map
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { localFs } from '@zenera/sandbox';
|
|
2
|
+
/** Why a read was refused: `ENOENT`, `EISDIR` or `EFBIG`, as `node:fs` would say. */
|
|
3
|
+
function agentFileError(code, message) {
|
|
4
|
+
const err = new Error(message);
|
|
5
|
+
err.code = code;
|
|
6
|
+
return err;
|
|
7
|
+
}
|
|
8
|
+
export class Workspace {
|
|
9
|
+
/** the workspace as given, made absolute; where it really is is settled on first use */
|
|
10
|
+
root;
|
|
11
|
+
readOnly;
|
|
12
|
+
mount;
|
|
13
|
+
/** the extra trees, resolved — the primary root is not among them */
|
|
14
|
+
mounts;
|
|
15
|
+
/** where the bytes are; every resolved path is in its namespace */
|
|
16
|
+
fs;
|
|
17
|
+
/** primary first */
|
|
18
|
+
#roots;
|
|
19
|
+
/** the ones with a name, longest name first */
|
|
20
|
+
#named;
|
|
21
|
+
/** every root, deepest host path first, for attributing a resolved path to one */
|
|
22
|
+
#deep;
|
|
23
|
+
#ready;
|
|
24
|
+
constructor(opts) {
|
|
25
|
+
this.fs = opts.fs ?? localFs;
|
|
26
|
+
this.root = this.fs.path.resolve(opts.root);
|
|
27
|
+
this.readOnly = opts.readOnly ?? false;
|
|
28
|
+
this.mount = mountPoint(opts.mount);
|
|
29
|
+
const roots = [{ path: this.root, at: this.mount, readOnly: this.readOnly }];
|
|
30
|
+
const mounts = [];
|
|
31
|
+
for (const m of opts.mounts ?? []) {
|
|
32
|
+
const at = mountPoint(m.at);
|
|
33
|
+
if (at === undefined) {
|
|
34
|
+
throw new Error(`a workspace mount needs an absolute name of its own: ${m.at}`);
|
|
35
|
+
}
|
|
36
|
+
// Overlapping names would make one path mean two directories, and
|
|
37
|
+
// which one won would depend on the order they were passed in.
|
|
38
|
+
for (const taken of roots) {
|
|
39
|
+
if (taken.at && (at === taken.at || under(at, taken.at) || under(taken.at, at))) {
|
|
40
|
+
throw new Error(`workspace mounts overlap: ${at} and ${taken.at}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const host = this.fs.path.resolve(m.host);
|
|
44
|
+
const readOnly = m.readOnly ?? true;
|
|
45
|
+
roots.push({ path: host, at, readOnly });
|
|
46
|
+
mounts.push({ host, at, readOnly, ...(m.source ? { source: m.source } : {}) });
|
|
47
|
+
}
|
|
48
|
+
this.mounts = mounts;
|
|
49
|
+
this.#roots = roots;
|
|
50
|
+
this.#named = roots
|
|
51
|
+
.filter((r) => r.at !== undefined)
|
|
52
|
+
.sort((a, b) => b.at.length - a.at.length);
|
|
53
|
+
// A mount may sit inside the workspace root; the deeper one owns the path.
|
|
54
|
+
this.#deep = [...roots].sort((a, b) => b.path.length - a.path.length);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Every root's symlinks resolved, once, so a workspace that *is* a symlink
|
|
58
|
+
* does not fail every containment check against itself. On first use rather
|
|
59
|
+
* than at construction: the driver may be on another machine, and asking it
|
|
60
|
+
* is a round trip.
|
|
61
|
+
*/
|
|
62
|
+
#settle() {
|
|
63
|
+
this.#ready ??= (async () => {
|
|
64
|
+
for (const [i, root] of this.#roots.entries()) {
|
|
65
|
+
const real = await this.fs.resolve(root.path);
|
|
66
|
+
const info = await this.fs.stat(real).catch(() => undefined);
|
|
67
|
+
if (!info) {
|
|
68
|
+
throw new Error(i === 0
|
|
69
|
+
? `the workspace does not exist: ${root.path}`
|
|
70
|
+
: `workspace mount ${root.at}: no such directory: ${root.path}`);
|
|
71
|
+
}
|
|
72
|
+
root.path = real;
|
|
73
|
+
}
|
|
74
|
+
this.#deep = [...this.#roots].sort((a, b) => b.path.length - a.path.length);
|
|
75
|
+
})().catch((err) => {
|
|
76
|
+
this.#ready = undefined;
|
|
77
|
+
throw err;
|
|
78
|
+
});
|
|
79
|
+
return this.#ready;
|
|
80
|
+
}
|
|
81
|
+
/** Every tree, where it really is: the workspace first. */
|
|
82
|
+
async trees() {
|
|
83
|
+
await this.#settle();
|
|
84
|
+
return this.#roots.map((r) => r.path);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Where a name points on this machine, lexically: no symlink followed, no
|
|
88
|
+
* file touched. For drawing a link to a file, never for deciding access —
|
|
89
|
+
* that is `within`.
|
|
90
|
+
*/
|
|
91
|
+
hostOf(input) {
|
|
92
|
+
const { root, rest } = this.#locate(input);
|
|
93
|
+
const { resolve, sep } = this.fs.path;
|
|
94
|
+
const at = resolve(root.path, rest);
|
|
95
|
+
return at === root.path || at.startsWith(root.path + sep) ? at : undefined;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The one gate. Returns an absolute path inside one of the roots, or throws.
|
|
99
|
+
*
|
|
100
|
+
* A path that does not exist yet cannot be realpath'd, so the nearest
|
|
101
|
+
* existing ancestor is resolved instead and the remainder appended — which
|
|
102
|
+
* is exactly what a create needs and closes the same hole a create opens.
|
|
103
|
+
*/
|
|
104
|
+
async within(input, opts = {}) {
|
|
105
|
+
if (typeof input !== 'string' || input.length === 0) {
|
|
106
|
+
throw new Error('path is required');
|
|
107
|
+
}
|
|
108
|
+
if (input.includes('\0')) {
|
|
109
|
+
throw new Error('path contains a null byte');
|
|
110
|
+
}
|
|
111
|
+
await this.#settle();
|
|
112
|
+
const { root, rest } = this.#locate(input);
|
|
113
|
+
const { resolve, sep } = this.fs.path;
|
|
114
|
+
const real = await this.fs.resolve(resolve(root.path, rest));
|
|
115
|
+
if (real !== root.path && !real.startsWith(root.path + sep)) {
|
|
116
|
+
throw new Error(`outside the workspace: ${input}`);
|
|
117
|
+
}
|
|
118
|
+
if (opts.write && root.readOnly) {
|
|
119
|
+
throw new Error(root === this.#roots[0]
|
|
120
|
+
? 'the workspace is read-only for this run'
|
|
121
|
+
: `${root.at} is read-only`);
|
|
122
|
+
}
|
|
123
|
+
return real;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Which tree a path names, and where in it. `/workspace/src/a.ts` and
|
|
127
|
+
* `src/a.ts` are one file under two names, and only the second resolves
|
|
128
|
+
* against the root, so the mounted spelling is rewritten before anything
|
|
129
|
+
* else looks at it.
|
|
130
|
+
*
|
|
131
|
+
* This is renaming, not permission. What comes out is still resolved, still
|
|
132
|
+
* symlink-followed and still checked against its root by the caller:
|
|
133
|
+
* `/workspace/../etc/passwd` loses its prefix and then fails containment
|
|
134
|
+
* exactly as `../etc/passwd` does.
|
|
135
|
+
*/
|
|
136
|
+
#locate(input) {
|
|
137
|
+
const primary = this.#roots[0];
|
|
138
|
+
// A lone `/` is the model's other name for "the top of what I can see".
|
|
139
|
+
// There is nothing above the root for it to mean, so it means the root
|
|
140
|
+
// — refusing it teaches nothing and costs a turn. This is the only
|
|
141
|
+
// absolute path treated as rooted: `/etc/passwd` still names the
|
|
142
|
+
// machine's file and still fails containment.
|
|
143
|
+
if (/^[\\/]+$/.test(input)) {
|
|
144
|
+
return { root: primary, rest: '.' };
|
|
145
|
+
}
|
|
146
|
+
const at = input.replace(/\\/g, '/');
|
|
147
|
+
for (const root of this.#named) {
|
|
148
|
+
if (at === root.at) {
|
|
149
|
+
return { root, rest: '.' };
|
|
150
|
+
}
|
|
151
|
+
if (at.startsWith(root.at + '/')) {
|
|
152
|
+
return { root, rest: at.slice(root.at.length + 1).replace(/^\/+/, '') || '.' };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { root: primary, rest: input };
|
|
156
|
+
}
|
|
157
|
+
/** The root a resolved path belongs to; the primary when nothing else claims it. */
|
|
158
|
+
#owner(path) {
|
|
159
|
+
const { sep } = this.fs.path;
|
|
160
|
+
return (this.#deep.find((r) => path === r.path || path.startsWith(r.path + sep)) ??
|
|
161
|
+
this.#roots[0]);
|
|
162
|
+
}
|
|
163
|
+
/** Where a path sits under its own root, in posix form. `.` is that root. */
|
|
164
|
+
rel(path) {
|
|
165
|
+
const { relative, sep } = this.fs.path;
|
|
166
|
+
return relative(this.#owner(path).path, path).split(sep).join('/') || '.';
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* How a path is written back to the model, and the reason `mount` is not
|
|
170
|
+
* only about what comes in. A shell in the sandbox prints the mounted name
|
|
171
|
+
* — `find` returns /workspace/src/a.ts — so if the file tools answered with
|
|
172
|
+
* `src/a.ts` the model would be holding two vocabularies for one tree and
|
|
173
|
+
* would have to translate between them to feed one tool's output to the
|
|
174
|
+
* other. With a mount configured every path in and every path out is the
|
|
175
|
+
* mounted one, and copying a path from a command's output into read_file
|
|
176
|
+
* is exact. Without one, everything stays relative as before.
|
|
177
|
+
*/
|
|
178
|
+
show(path) {
|
|
179
|
+
const root = this.#owner(path);
|
|
180
|
+
const rel = this.rel(path);
|
|
181
|
+
if (root.at === undefined) {
|
|
182
|
+
return rel;
|
|
183
|
+
}
|
|
184
|
+
return rel === '.' ? root.at : `${root.at}/${rel}`;
|
|
185
|
+
}
|
|
186
|
+
/** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
|
|
187
|
+
isRoot(path) {
|
|
188
|
+
return this.#roots.some((r) => r.path === path);
|
|
189
|
+
}
|
|
190
|
+
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
191
|
+
isInside(inner, outer) {
|
|
192
|
+
return inner.startsWith(outer + this.fs.path.sep);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A file the agent named, whole: contained like any read, and read through
|
|
196
|
+
* this workspace's driver, so it is the same whether the bytes are on this
|
|
197
|
+
* machine or in a remote sandbox. Memory copies a file in through this.
|
|
198
|
+
*/
|
|
199
|
+
async read(path, opts = {}) {
|
|
200
|
+
const at = await this.within(path);
|
|
201
|
+
const info = await this.fs.stat(at).catch(() => undefined);
|
|
202
|
+
if (!info) {
|
|
203
|
+
throw agentFileError('ENOENT', `${path} does not exist`);
|
|
204
|
+
}
|
|
205
|
+
if (!info.isFile()) {
|
|
206
|
+
throw agentFileError('EISDIR', `${path} is not a regular file`);
|
|
207
|
+
}
|
|
208
|
+
// Asked before reading, so a file far over the cap is never pulled across.
|
|
209
|
+
if (opts.maxBytes !== undefined && info.size > opts.maxBytes) {
|
|
210
|
+
throw agentFileError('EFBIG', `${path} is ${info.size} bytes, over the ${opts.maxBytes} byte limit`);
|
|
211
|
+
}
|
|
212
|
+
const data = await this.fs.readFile(at);
|
|
213
|
+
if (opts.maxBytes !== undefined && data.byteLength > opts.maxBytes) {
|
|
214
|
+
throw agentFileError('EFBIG', `${path} grew past the ${opts.maxBytes} byte limit`);
|
|
215
|
+
}
|
|
216
|
+
return { path: this.show(at), data };
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
220
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
221
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
222
|
+
*/
|
|
223
|
+
async write(path, data, opts = {}) {
|
|
224
|
+
this.mutable();
|
|
225
|
+
const at = await this.within(path, { write: true });
|
|
226
|
+
if (this.isRoot(at)) {
|
|
227
|
+
throw new Error('refusing to overwrite the workspace root');
|
|
228
|
+
}
|
|
229
|
+
const there = await this.fs.stat(at).catch(() => undefined);
|
|
230
|
+
if (there?.isDirectory()) {
|
|
231
|
+
throw new Error(`${this.show(at)} is a directory`);
|
|
232
|
+
}
|
|
233
|
+
if (there && !opts.overwrite) {
|
|
234
|
+
throw new Error(`${this.show(at)} already exists — pass overwrite to replace it`);
|
|
235
|
+
}
|
|
236
|
+
await this.fs.mkdirp(this.fs.path.dirname(at));
|
|
237
|
+
await this.fs.writeFile(at, data);
|
|
238
|
+
return this.show(at);
|
|
239
|
+
}
|
|
240
|
+
mutable() {
|
|
241
|
+
if (this.readOnly) {
|
|
242
|
+
throw new Error('the workspace is read-only for this run');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* An absolute name a tree is known by, with any trailing slash removed. A mount
|
|
248
|
+
* of `/` would make everything on the machine look like it was inside, so it is
|
|
249
|
+
* not a mount point; nor is a relative one, which would resolve against nothing.
|
|
250
|
+
*/
|
|
251
|
+
function mountPoint(at) {
|
|
252
|
+
const clean = at?.replace(/\\/g, '/').replace(/\/+$/, '');
|
|
253
|
+
return clean && clean.startsWith('/') ? clean : undefined;
|
|
254
|
+
}
|
|
255
|
+
function under(inner, outer) {
|
|
256
|
+
return inner.startsWith(outer + '/');
|
|
257
|
+
}
|
|
258
|
+
//# 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.58",
|
|
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.58",
|
|
66
66
|
"graphology": "^0.26.0",
|
|
67
67
|
"yaml": "^2.9.1",
|
|
68
68
|
"zod": "^4.6.7"
|