@zenera/neo 1.1.58 → 1.2.0

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.
@@ -1,6 +1,6 @@
1
1
  import { type AnyTool } from '../types.ts';
2
2
  import { type WorkspaceOptions } from './workspace.ts';
3
- export { Workspace, type AgentFile, type AgentFiles, type ResolvedMount, type WorkspaceOptions, } from './workspace.ts';
3
+ export { Workspace, type AgentFile, type AgentFiles, type ResolvedMount, type WorkspaceMount, type WorkspaceOptions, } from './workspace.ts';
4
4
  export type FileFormat = 'text' | 'binary' | 'image' | 'audio' | 'video' | 'pdf' | 'archive';
5
5
  export interface FileInfo {
6
6
  name: string;
@@ -1033,6 +1033,7 @@ const ERRNO = {
1033
1033
  EEXIST: 'already exists',
1034
1034
  ENOTEMPTY: 'is not empty',
1035
1035
  EACCES: 'cannot be reached',
1036
+ EROFS: 'is read-only',
1036
1037
  EPERM: 'cannot be reached',
1037
1038
  ELOOP: 'is a loop of symlinks',
1038
1039
  };
@@ -1,4 +1,4 @@
1
- import { type FilesView, type FsDriver, type Mount } from '@zenera/sandbox';
1
+ import { type FsDriver, type MountSource } from '@zenera/sandbox';
2
2
  /**
3
3
  * The agent's files, as something other than its file tools touches them:
4
4
  * contained and permission-checked exactly as those tools are. `Workspace` is
@@ -22,42 +22,85 @@ export interface AgentFile {
22
22
  path: string;
23
23
  data: Uint8Array;
24
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 & {
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;
29
58
  readOnly: boolean;
30
- };
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
+ */
31
71
  export declare class Workspace implements AgentFiles {
32
72
  #private;
33
- /** the workspace as given, made absolute; where it really is is settled on first use */
73
+ /** the workspace's name in `fs`, and where relative paths start */
34
74
  readonly root: string;
35
75
  readonly readOnly: boolean;
76
+ /** the name paths are shown by; unset, workspace paths are shown relative */
36
77
  readonly mount: string | undefined;
37
- /** the extra trees, resolved — the primary root is not among them */
78
+ /** the extra trees — the workspace itself is not among them */
38
79
  readonly mounts: readonly ResolvedMount[];
39
- /** where the bytes are; every resolved path is in its namespace */
80
+ /** the agent's files, contained, in the agent's paths */
40
81
  readonly fs: FsDriver;
41
82
  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;
83
+ /** Every tree by name: the workspace first. */
84
+ trees(): string[];
50
85
  /**
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.
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.
56
88
  */
57
89
  within(input: string, opts?: {
58
90
  write?: boolean;
59
91
  }): Promise<string>;
60
- /** Where a path sits under its own root, in posix form. `.` is that root. */
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. */
61
104
  rel(path: string): string;
62
105
  /**
63
106
  * How a path is written back to the model, and the reason `mount` is not
@@ -67,12 +110,12 @@ export declare class Workspace implements AgentFiles {
67
110
  * would have to translate between them to feed one tool's output to the
68
111
  * other. With a mount configured every path in and every path out is the
69
112
  * mounted one, and copying a path from a command's output into read_file
70
- * is exact. Without one, everything stays relative as before.
113
+ * is exact. Without one, the workspace's own paths stay relative.
71
114
  */
72
115
  show(path: string): string;
73
- /** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
116
+ /** Whether a path *is* one of the trees. Nothing may move or delete one. */
74
117
  isRoot(path: string): boolean;
75
- /** Whether resolved `inner` sits strictly below resolved `outer`. */
118
+ /** Whether `inner` sits strictly below `outer`. */
76
119
  isInside(inner: string, outer: string): boolean;
77
120
  /**
78
121
  * A file the agent named, whole: contained like any read, and read through
@@ -92,4 +135,5 @@ export declare class Workspace implements AgentFiles {
92
135
  }): Promise<string>;
93
136
  mutable(): void;
94
137
  }
138
+ export {};
95
139
  //# sourceMappingURL=workspace.d.ts.map
@@ -1,32 +1,43 @@
1
- import { localFs } from '@zenera/sandbox';
1
+ import { posix } from 'node:path';
2
+ import { RootedFs } from '@zenera/sandbox';
2
3
  /** Why a read was refused: `ENOENT`, `EISDIR` or `EFBIG`, as `node:fs` would say. */
3
4
  function agentFileError(code, message) {
4
5
  const err = new Error(message);
5
6
  err.code = code;
6
7
  return err;
7
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
+ */
8
18
  export class Workspace {
9
- /** the workspace as given, made absolute; where it really is is settled on first use */
19
+ /** the workspace's name in `fs`, and where relative paths start */
10
20
  root;
11
21
  readOnly;
22
+ /** the name paths are shown by; unset, workspace paths are shown relative */
12
23
  mount;
13
- /** the extra trees, resolved — the primary root is not among them */
24
+ /** the extra trees — the workspace itself is not among them */
14
25
  mounts;
15
- /** where the bytes are; every resolved path is in its namespace */
26
+ /** the agent's files, contained, in the agent's paths */
16
27
  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;
28
+ /** the workspace first */
29
+ #trees;
30
+ /** longest name first, for finding the tree a path is in */
31
+ #byName;
23
32
  #ready;
24
33
  constructor(opts) {
25
- this.fs = opts.fs ?? localFs;
26
- this.root = this.fs.path.resolve(opts.root);
27
34
  this.readOnly = opts.readOnly ?? false;
28
35
  this.mount = mountPoint(opts.mount);
29
- const roots = [{ path: this.root, at: this.mount, readOnly: this.readOnly }];
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
+ ];
30
41
  const mounts = [];
31
42
  for (const m of opts.mounts ?? []) {
32
43
  const at = mountPoint(m.at);
@@ -35,71 +46,54 @@ export class Workspace {
35
46
  }
36
47
  // Overlapping names would make one path mean two directories, and
37
48
  // 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))) {
49
+ for (const taken of trees) {
50
+ if (at === taken.at || under(at, taken.at) || under(taken.at, at)) {
40
51
  throw new Error(`workspace mounts overlap: ${at} and ${taken.at}`);
41
52
  }
42
53
  }
43
- const host = this.fs.path.resolve(m.host);
54
+ if (local && !m.host) {
55
+ throw new Error(`workspace mount ${at} needs a host directory`);
56
+ }
44
57
  const readOnly = m.readOnly ?? true;
45
- roots.push({ path: host, at, readOnly });
46
- mounts.push({ host, at, readOnly, ...(m.source ? { source: m.source } : {}) });
58
+ trees.push({ at, readOnly, ...(local ? { host: m.host } : {}) });
59
+ mounts.push({ at, readOnly, ...(m.source ? { source: m.source } : {}) });
47
60
  }
48
61
  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);
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 })));
55
67
  }
56
68
  /**
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.
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.
61
71
  */
62
72
  #settle() {
63
73
  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);
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);
67
77
  if (!info) {
78
+ const where = tree.host ?? tree.at;
68
79
  throw new Error(i === 0
69
- ? `the workspace does not exist: ${root.path}`
70
- : `workspace mount ${root.at}: no such directory: ${root.path}`);
80
+ ? `the workspace does not exist: ${where}`
81
+ : `workspace mount ${tree.at}: no such directory: ${where}`);
71
82
  }
72
- root.path = real;
73
83
  }
74
- this.#deep = [...this.#roots].sort((a, b) => b.path.length - a.path.length);
75
84
  })().catch((err) => {
76
85
  this.#ready = undefined;
77
86
  throw err;
78
87
  });
79
88
  return this.#ready;
80
89
  }
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;
90
+ /** Every tree by name: the workspace first. */
91
+ trees() {
92
+ return this.#trees.map((t) => t.at);
96
93
  }
97
94
  /**
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.
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.
103
97
  */
104
98
  async within(input, opts = {}) {
105
99
  if (typeof input !== 'string' || input.length === 0) {
@@ -109,61 +103,46 @@ export class Workspace {
109
103
  throw new Error('path contains a null byte');
110
104
  }
111
105
  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)) {
106
+ const found = this.locate(input);
107
+ if (!found) {
116
108
  throw new Error(`outside the workspace: ${input}`);
117
109
  }
118
- if (opts.write && root.readOnly) {
119
- throw new Error(root === this.#roots[0]
110
+ if (opts.write && found.tree.readOnly) {
111
+ throw new Error(found.tree === this.#trees[0]
120
112
  ? 'the workspace is read-only for this run'
121
- : `${root.at} is read-only`);
113
+ : `${found.tree.at} is read-only`);
122
114
  }
123
- return real;
115
+ return found.at;
124
116
  }
125
117
  /**
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.
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.
135
123
  */
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.
124
+ locate(input) {
125
+ const primary = this.#trees[0];
143
126
  if (/^[\\/]+$/.test(input)) {
144
- return { root: primary, rest: '.' };
127
+ return { tree: primary, at: primary.at };
145
128
  }
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
- }
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;
154
136
  }
155
- return { root: primary, rest: input };
137
+ return tree ? { tree, at } : undefined;
156
138
  }
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]);
139
+ #owner(at) {
140
+ return this.#byName.find((t) => at === t.at || under(at, t.at));
162
141
  }
163
- /** Where a path sits under its own root, in posix form. `.` is that root. */
142
+ /** Where a path sits under its own tree, in posix form. `.` is that tree. */
164
143
  rel(path) {
165
- const { relative, sep } = this.fs.path;
166
- return relative(this.#owner(path).path, path).split(sep).join('/') || '.';
144
+ const tree = this.#owner(path);
145
+ return tree ? posix.relative(tree.at, path) || '.' : path;
167
146
  }
168
147
  /**
169
148
  * How a path is written back to the model, and the reason `mount` is not
@@ -173,23 +152,21 @@ export class Workspace {
173
152
  * would have to translate between them to feed one tool's output to the
174
153
  * other. With a mount configured every path in and every path out is the
175
154
  * mounted one, and copying a path from a command's output into read_file
176
- * is exact. Without one, everything stays relative as before.
155
+ * is exact. Without one, the workspace's own paths stay relative.
177
156
  */
178
157
  show(path) {
179
- const root = this.#owner(path);
180
- const rel = this.rel(path);
181
- if (root.at === undefined) {
182
- return rel;
158
+ if (this.mount === undefined && this.#owner(path) === this.#trees[0]) {
159
+ return this.rel(path);
183
160
  }
184
- return rel === '.' ? root.at : `${root.at}/${rel}`;
161
+ return path;
185
162
  }
186
- /** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
163
+ /** Whether a path *is* one of the trees. Nothing may move or delete one. */
187
164
  isRoot(path) {
188
- return this.#roots.some((r) => r.path === path);
165
+ return this.#trees.some((t) => t.at === path);
189
166
  }
190
- /** Whether resolved `inner` sits strictly below resolved `outer`. */
167
+ /** Whether `inner` sits strictly below `outer`. */
191
168
  isInside(inner, outer) {
192
- return inner.startsWith(outer + this.fs.path.sep);
169
+ return under(inner, outer);
193
170
  }
194
171
  /**
195
172
  * A file the agent named, whole: contained like any read, and read through
@@ -250,7 +227,10 @@ export class Workspace {
250
227
  */
251
228
  function mountPoint(at) {
252
229
  const clean = at?.replace(/\\/g, '/').replace(/\/+$/, '');
253
- return clean && clean.startsWith('/') ? clean : undefined;
230
+ return clean && clean.startsWith('/') ? posix.normalize(clean) : undefined;
231
+ }
232
+ function trim(at) {
233
+ return at.length > 1 ? at.replace(/\/+$/, '') : at;
254
234
  }
255
235
  function under(inner, outer) {
256
236
  return inner.startsWith(outer + '/');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenera/neo",
3
- "version": "1.1.58",
3
+ "version": "1.2.0",
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.58",
65
+ "@zenera/sandbox": "^1.2.0",
66
66
  "graphology": "^0.26.0",
67
67
  "yaml": "^2.9.1",
68
68
  "zod": "^4.6.7"