@zenera/neo 1.1.47 → 1.1.49
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/project/config.js +11 -0
- package/dist/project/load.d.ts +7 -0
- package/dist/project/load.js +18 -9
- package/dist/tools/identity.d.ts +48 -0
- package/dist/tools/identity.js +97 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/sandbox.d.ts +21 -0
- package/dist/tools/sandbox.js +38 -2
- package/package.json +1 -1
package/dist/project/config.js
CHANGED
|
@@ -1,5 +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
4
|
// ---------------------------------------------------------------------------
|
|
4
5
|
// agents.yaml
|
|
5
6
|
// ---------------------------------------------------------------------------
|
|
@@ -317,6 +318,16 @@ const sandbox = z
|
|
|
317
318
|
// });
|
|
318
319
|
// }
|
|
319
320
|
// }
|
|
321
|
+
for (const [i, name] of (value.env ?? []).entries()) {
|
|
322
|
+
if (name.startsWith(IDENTITY_ENV_PREFIX)) {
|
|
323
|
+
ctx.addIssue({
|
|
324
|
+
code: 'custom',
|
|
325
|
+
path: ['env', i],
|
|
326
|
+
message: `${name} is set by the host for the user a run acts for — ` +
|
|
327
|
+
`a project cannot forward its own ${IDENTITY_ENV_PREFIX}* variables`,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
320
331
|
});
|
|
321
332
|
const agent = z
|
|
322
333
|
.object({
|
package/dist/project/load.d.ts
CHANGED
|
@@ -34,6 +34,13 @@ export interface ProjectOptions<TCtx = unknown> {
|
|
|
34
34
|
* against it.
|
|
35
35
|
*/
|
|
36
36
|
tools?: AnyTool<TCtx>[];
|
|
37
|
+
/**
|
|
38
|
+
* Tools every agent gets without naming them — `current_user`, from the engine. Config
|
|
39
|
+
* can still select one by name, and take it away with `-name`; what it
|
|
40
|
+
* cannot do is forget it, which is the point for a tool whose whole value
|
|
41
|
+
* is that the model can always ask.
|
|
42
|
+
*/
|
|
43
|
+
alwaysTools?: AnyTool<TCtx>[];
|
|
37
44
|
/**
|
|
38
45
|
* Credentials for the names the config declares — or names it does not, so
|
|
39
46
|
* a deployment can add a provider the repository never mentions. Merged
|
package/dist/project/load.js
CHANGED
|
@@ -65,7 +65,7 @@ export async function loadProject(dir, opts = {}) {
|
|
|
65
65
|
description: spec.description,
|
|
66
66
|
instructions: instructionsFor(root, spec, houseRules),
|
|
67
67
|
model: resolve(spec.model ?? config.model, `agents.${spec.name}.model`),
|
|
68
|
-
tools: toolsFor(spec, opts.tools ?? []),
|
|
68
|
+
tools: toolsFor(spec, opts.tools ?? [], opts.alwaysTools ?? []),
|
|
69
69
|
handoffs: spec.handoffs,
|
|
70
70
|
skills: bindingFor(spec, providers),
|
|
71
71
|
fork: forkFor(spec),
|
|
@@ -460,14 +460,23 @@ function embedderResolver(config, opts, models) {
|
|
|
460
460
|
}
|
|
461
461
|
};
|
|
462
462
|
}
|
|
463
|
-
function toolsFor(spec, available) {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
463
|
+
function toolsFor(spec, available, always) {
|
|
464
|
+
// One namespace, so `current_user` and `identity:*` resolve, and `-current_user` is a removal
|
|
465
|
+
// rather than an unknown tool.
|
|
466
|
+
const names = new Set(available.map((t) => t.name));
|
|
467
|
+
const all = [...available, ...always.filter((t) => !names.has(t.name))];
|
|
468
|
+
const where = { where: `agents.${spec.name}.tools`, hint: 'pass it in ProjectOptions.tools' };
|
|
469
|
+
const selectors = spec.tools ?? [];
|
|
470
|
+
const chosen = selectors.length ? selectTools(all, selectors, where) : [];
|
|
471
|
+
if (!always.length) {
|
|
472
|
+
return chosen;
|
|
473
|
+
}
|
|
474
|
+
// What `*` and the removals leave of everything is what the author did
|
|
475
|
+
// not take away; an always-tool survives unless it was among the taken.
|
|
476
|
+
const drops = selectors.filter((s) => s.trim().startsWith('-'));
|
|
477
|
+
const kept = new Set(selectTools(all, ['*', ...drops], where).map((t) => t.name));
|
|
478
|
+
const have = new Set(chosen.map((t) => t.name));
|
|
479
|
+
return [...chosen, ...always.filter((t) => kept.has(t.name) && !have.has(t.name))];
|
|
471
480
|
}
|
|
472
481
|
function bindingFor(spec, providers) {
|
|
473
482
|
if (!spec.skills) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type AnyTool } from '../types.ts';
|
|
2
|
+
/** The family name config selects the set by: `tools: [identity:*]`. */
|
|
3
|
+
export declare const IDENTITY_GROUP = "identity";
|
|
4
|
+
/**
|
|
5
|
+
* Where an identity came from. Only `host` is vouched for: the process that
|
|
6
|
+
* opened the run authenticated the user itself. `local` is whatever this
|
|
7
|
+
* machine was told — `zen run --as`, or variables already in the environment —
|
|
8
|
+
* which is fine for testing a project and means nothing as proof.
|
|
9
|
+
*/
|
|
10
|
+
export type IdentitySource = 'host' | 'local';
|
|
11
|
+
export interface RunIdentity {
|
|
12
|
+
/** stable id of the user; what to key anything per-user on */
|
|
13
|
+
id: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
/** the user's role in this project, in the host's own words */
|
|
17
|
+
role?: string;
|
|
18
|
+
source: IdentitySource;
|
|
19
|
+
}
|
|
20
|
+
/** The variables the identity is exported as, inside the sandbox. */
|
|
21
|
+
export declare const IDENTITY_ENV: {
|
|
22
|
+
readonly id: "ZEN_USER_ID";
|
|
23
|
+
readonly email: "ZEN_USER_EMAIL";
|
|
24
|
+
readonly name: "ZEN_USER_NAME";
|
|
25
|
+
readonly role: "ZEN_USER_ROLE";
|
|
26
|
+
readonly source: "ZEN_USER_SOURCE";
|
|
27
|
+
};
|
|
28
|
+
/** Every name under it is the host's to set; a project may not. */
|
|
29
|
+
export declare const IDENTITY_ENV_PREFIX = "ZEN_USER_";
|
|
30
|
+
export declare function isIdentityEnv(name: string): boolean;
|
|
31
|
+
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
32
|
+
export declare function identityEnv(identity: RunIdentity | undefined): Record<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
* Reads an identity back out of an environment — a `zen run` started inside a
|
|
35
|
+
* sandbox inherits its parent's, which is what keeps a nested run acting for
|
|
36
|
+
* the same person. Always `local`: a variable says what someone set, not who
|
|
37
|
+
* checked it.
|
|
38
|
+
*/
|
|
39
|
+
export declare function identityFromEnv(env: NodeJS.ProcessEnv): RunIdentity | undefined;
|
|
40
|
+
/** Drops the fields that say nothing, so `current_user` and the report show only what is known. */
|
|
41
|
+
export declare function compactIdentity(identity: RunIdentity): RunIdentity;
|
|
42
|
+
/**
|
|
43
|
+
* `current_user`. The description is the same whoever is signed in, so every chat on a
|
|
44
|
+
* shared project keeps sending byte-identical tool schemas; who it is arrives
|
|
45
|
+
* only in the result.
|
|
46
|
+
*/
|
|
47
|
+
export declare function identityTools<TCtx = unknown>(identity?: RunIdentity): AnyTool<TCtx>[];
|
|
48
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { tool } from "../types.js";
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Who a run is for
|
|
4
|
+
//
|
|
5
|
+
// Not who wrote the project and not whose keys pay for it: the person the host
|
|
6
|
+
// authenticated and is running this turn on behalf of. The host knows that and
|
|
7
|
+
// the model does not, so it travels the one way that cannot be argued with — as
|
|
8
|
+
// a value the host hands in, read back by a tool that takes no arguments. There
|
|
9
|
+
// is nothing for the model to pass, so there is nothing for it to make up.
|
|
10
|
+
//
|
|
11
|
+
// The same values reach code in the sandbox as `ZEN_USER_*` variables, set on
|
|
12
|
+
// every command rather than on the container: a container outlives a run, and
|
|
13
|
+
// one kept between sessions must not answer for whoever used it last.
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
/** The family name config selects the set by: `tools: [identity:*]`. */
|
|
16
|
+
export const IDENTITY_GROUP = 'identity';
|
|
17
|
+
/** The variables the identity is exported as, inside the sandbox. */
|
|
18
|
+
export const IDENTITY_ENV = {
|
|
19
|
+
id: 'ZEN_USER_ID',
|
|
20
|
+
email: 'ZEN_USER_EMAIL',
|
|
21
|
+
name: 'ZEN_USER_NAME',
|
|
22
|
+
role: 'ZEN_USER_ROLE',
|
|
23
|
+
source: 'ZEN_USER_SOURCE',
|
|
24
|
+
};
|
|
25
|
+
/** Every name under it is the host's to set; a project may not. */
|
|
26
|
+
export const IDENTITY_ENV_PREFIX = 'ZEN_USER_';
|
|
27
|
+
export function isIdentityEnv(name) {
|
|
28
|
+
return name.startsWith(IDENTITY_ENV_PREFIX);
|
|
29
|
+
}
|
|
30
|
+
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
31
|
+
export function identityEnv(identity) {
|
|
32
|
+
if (!identity) {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
const env = {
|
|
36
|
+
[IDENTITY_ENV.id]: identity.id,
|
|
37
|
+
[IDENTITY_ENV.source]: identity.source,
|
|
38
|
+
};
|
|
39
|
+
for (const key of ['email', 'name', 'role']) {
|
|
40
|
+
const value = identity[key];
|
|
41
|
+
if (value) {
|
|
42
|
+
env[IDENTITY_ENV[key]] = value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return env;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reads an identity back out of an environment — a `zen run` started inside a
|
|
49
|
+
* sandbox inherits its parent's, which is what keeps a nested run acting for
|
|
50
|
+
* the same person. Always `local`: a variable says what someone set, not who
|
|
51
|
+
* checked it.
|
|
52
|
+
*/
|
|
53
|
+
export function identityFromEnv(env) {
|
|
54
|
+
const id = env[IDENTITY_ENV.id]?.trim();
|
|
55
|
+
if (!id) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const pick = (name) => env[name]?.trim() || undefined;
|
|
59
|
+
return compactIdentity({
|
|
60
|
+
id,
|
|
61
|
+
email: pick(IDENTITY_ENV.email),
|
|
62
|
+
name: pick(IDENTITY_ENV.name),
|
|
63
|
+
role: pick(IDENTITY_ENV.role),
|
|
64
|
+
source: 'local',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Drops the fields that say nothing, so `current_user` and the report show only what is known. */
|
|
68
|
+
export function compactIdentity(identity) {
|
|
69
|
+
return Object.fromEntries(Object.entries(identity).filter(([, v]) => v !== undefined && v !== ''));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* `current_user`. The description is the same whoever is signed in, so every chat on a
|
|
73
|
+
* shared project keeps sending byte-identical tool schemas; who it is arrives
|
|
74
|
+
* only in the result.
|
|
75
|
+
*/
|
|
76
|
+
export function identityTools(identity) {
|
|
77
|
+
const currentUser = tool({
|
|
78
|
+
name: 'current_user',
|
|
79
|
+
group: IDENTITY_GROUP,
|
|
80
|
+
description: 'Returns the signed-in user this run is acting for: id, and email, name and role ' +
|
|
81
|
+
'when known. Use it when an answer depends on who is asking, instead of asking ' +
|
|
82
|
+
'them or guessing. `source: "host"` means the application authenticated them; ' +
|
|
83
|
+
'`"local"` means it was only stated on this machine. Code you run in the sandbox ' +
|
|
84
|
+
`sees the same as $${IDENTITY_ENV.id}, $${IDENTITY_ENV.email}, $${IDENTITY_ENV.name} ` +
|
|
85
|
+
`and $${IDENTITY_ENV.role}, or through the \`zenera\` library: ` +
|
|
86
|
+
'`import zenera; zenera.identity()` in Python, `require("zenera").identity()` in Node.',
|
|
87
|
+
parameters: { type: 'object', properties: {}, additionalProperties: false },
|
|
88
|
+
execute: () => identity
|
|
89
|
+
? { authenticated: true, ...identity }
|
|
90
|
+
: {
|
|
91
|
+
authenticated: false,
|
|
92
|
+
hint: 'this run has no signed-in user; do not assume one',
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
return [currentUser];
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=identity.js.map
|
package/dist/tools/index.d.ts
CHANGED
package/dist/tools/index.js
CHANGED
package/dist/tools/sandbox.d.ts
CHANGED
|
@@ -85,6 +85,25 @@ export interface SandboxOptions extends SandboxSpec {
|
|
|
85
85
|
readOnly?: boolean;
|
|
86
86
|
/** extra bind mounts; the host side must already exist */
|
|
87
87
|
mounts?: readonly SandboxMount[];
|
|
88
|
+
/**
|
|
89
|
+
* Environment set on every command rather than on the container: values
|
|
90
|
+
* that belong to the run, not to the box — the user it is acting for.
|
|
91
|
+
*
|
|
92
|
+
* `env` is fixed when the container is created, and a container is kept
|
|
93
|
+
* between sessions and shared between agents; a run's values written there
|
|
94
|
+
* would still be answering for whoever came before. Not part of the
|
|
95
|
+
* container's name either, for the same reason in reverse: a new user is
|
|
96
|
+
* not a new machine. Plain values on podman's command line, so nothing
|
|
97
|
+
* secret goes here — that is what `secrets` is for.
|
|
98
|
+
*/
|
|
99
|
+
execEnv?: Record<string, string>;
|
|
100
|
+
/**
|
|
101
|
+
* Directories put in front of a path-like variable for every command —
|
|
102
|
+
* `PYTHONPATH`, say — keeping whatever the image already set behind them.
|
|
103
|
+
* `--env` could only replace the image's value, and an image that relies on
|
|
104
|
+
* its own `PYTHONPATH` would break on the first `python`.
|
|
105
|
+
*/
|
|
106
|
+
execPaths?: Record<string, readonly string[]>;
|
|
88
107
|
/** the container engine binary */
|
|
89
108
|
engine?: string;
|
|
90
109
|
/** how processes are spawned — the seam tests replace */
|
|
@@ -136,6 +155,8 @@ interface Resolved extends Required<Omit<SandboxSpec, 'user'>> {
|
|
|
136
155
|
readOnly: boolean;
|
|
137
156
|
mounts: readonly SandboxMount[];
|
|
138
157
|
engine: string;
|
|
158
|
+
execEnv: Record<string, string>;
|
|
159
|
+
execPaths: Record<string, readonly string[]>;
|
|
139
160
|
explicitCpus?: boolean;
|
|
140
161
|
explicitMemory?: boolean;
|
|
141
162
|
}
|
package/dist/tools/sandbox.js
CHANGED
|
@@ -314,6 +314,28 @@ export class Sandbox {
|
|
|
314
314
|
args.push(s.image, 'sleep', 'infinity');
|
|
315
315
|
return args;
|
|
316
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* `podman exec` for a command the agent asked for, with the run's own
|
|
319
|
+
* environment. The script still arrives on stdin; when a path has to be
|
|
320
|
+
* extended, the shell that reads it is started by a fixed `-c` line that
|
|
321
|
+
* exports the variable and `exec`s it — built from host-chosen names and
|
|
322
|
+
* paths only, so no text of the agent's is ever part of a parse here.
|
|
323
|
+
*/
|
|
324
|
+
#exec(options = []) {
|
|
325
|
+
const env = Object.entries(this.spec.execEnv).flatMap(([k, v]) => ['--env', `${k}=${v}`]);
|
|
326
|
+
const paths = Object.entries(this.spec.execPaths).filter(([, dirs]) => dirs.length);
|
|
327
|
+
const shell = paths.length
|
|
328
|
+
? [
|
|
329
|
+
'/bin/sh',
|
|
330
|
+
'-c',
|
|
331
|
+
[
|
|
332
|
+
...paths.map(([name, dirs]) => `export ${name}=${quote(dirs.join(':'))}"\${${name}:+:\$${name}}"`),
|
|
333
|
+
'exec /bin/sh -s',
|
|
334
|
+
].join('; '),
|
|
335
|
+
]
|
|
336
|
+
: ['/bin/sh', '-s'];
|
|
337
|
+
return ['exec', '--interactive', ...env, ...options, this.name, ...shell];
|
|
338
|
+
}
|
|
317
339
|
/**
|
|
318
340
|
* Runs one command and waits for it. The script goes down stdin, so no part
|
|
319
341
|
* of it is ever parsed by a shell on this side of the container.
|
|
@@ -323,7 +345,11 @@ export class Sandbox {
|
|
|
323
345
|
const cwd = this.inside(opts.cwd);
|
|
324
346
|
const timeoutMs = this.#timeout(opts.timeout);
|
|
325
347
|
const startedAt = Date.now();
|
|
326
|
-
const once = () => this.#run(this.spec.engine, ['
|
|
348
|
+
const once = () => this.#run(this.spec.engine, this.#exec(['--workdir', cwd]), {
|
|
349
|
+
input: command,
|
|
350
|
+
timeoutMs,
|
|
351
|
+
signal: opts.signal,
|
|
352
|
+
});
|
|
327
353
|
let res = await once();
|
|
328
354
|
if (gone(res)) {
|
|
329
355
|
// podman refused before running anything, so the retry cannot run the command twice.
|
|
@@ -386,7 +412,10 @@ export class Sandbox {
|
|
|
386
412
|
// the job missing.
|
|
387
413
|
`i=0; while [ ! -s ${JOBS}/${id}.pid ] && [ $i -lt 50 ]; do i=$((i+1)); sleep 0.1; done`,
|
|
388
414
|
].join('\n');
|
|
389
|
-
const started = await this.#run(this.spec.engine,
|
|
415
|
+
const started = await this.#run(this.spec.engine, this.#exec(), {
|
|
416
|
+
input: launch,
|
|
417
|
+
timeoutMs: 30_000,
|
|
418
|
+
});
|
|
390
419
|
if (started.code !== 0) {
|
|
391
420
|
throw new SandboxError(`could not start the job: ${message(started)}`);
|
|
392
421
|
}
|
|
@@ -593,6 +622,11 @@ function resolveSpec(opts) {
|
|
|
593
622
|
}
|
|
594
623
|
const hardening = opts.hardening ?? 'standard';
|
|
595
624
|
const strict = hardening === 'strict';
|
|
625
|
+
for (const name of [...Object.keys(opts.execEnv ?? {}), ...Object.keys(opts.execPaths ?? {})]) {
|
|
626
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
627
|
+
throw new SandboxError(`not a variable name: ${JSON.stringify(name)}`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
596
630
|
return {
|
|
597
631
|
root: opts.root,
|
|
598
632
|
key: opts.key,
|
|
@@ -612,6 +646,8 @@ function resolveSpec(opts) {
|
|
|
612
646
|
readOnly: opts.readOnly ?? false,
|
|
613
647
|
mounts: opts.mounts ?? [],
|
|
614
648
|
engine: opts.engine ?? 'podman',
|
|
649
|
+
execEnv: opts.execEnv ?? {},
|
|
650
|
+
execPaths: opts.execPaths ?? {},
|
|
615
651
|
};
|
|
616
652
|
}
|
|
617
653
|
/**
|