@starsharryzhu/complete-subagent 0.0.0-stage → 0.1.1
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/AGENTS.md +58 -0
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/cordis.patch.yml +11 -0
- package/lib/client.js +439 -0
- package/lib/client.modules.txt +8 -0
- package/lib/host/access.d.ts +46 -0
- package/lib/host/access.js +54 -0
- package/lib/host/call-scope.d.ts +21 -0
- package/lib/host/call-scope.js +26 -0
- package/lib/host/delegate.d.ts +47 -0
- package/lib/host/delegate.js +133 -0
- package/lib/host/grants.d.ts +51 -0
- package/lib/host/grants.js +62 -0
- package/lib/host/legacy.d.ts +57 -0
- package/lib/host/legacy.js +85 -0
- package/lib/host/models.d.ts +59 -0
- package/lib/host/models.js +139 -0
- package/lib/host/paths.d.ts +51 -0
- package/lib/host/paths.js +100 -0
- package/lib/host/policy.d.ts +72 -0
- package/lib/host/policy.js +169 -0
- package/lib/host/prompt.d.ts +41 -0
- package/lib/host/prompt.js +100 -0
- package/lib/host/requests.d.ts +82 -0
- package/lib/host/requests.js +109 -0
- package/lib/host/scope.d.ts +33 -0
- package/lib/host/scope.js +46 -0
- package/lib/host/store.d.ts +89 -0
- package/lib/host/store.js +128 -0
- package/lib/host/tools.d.ts +58 -0
- package/lib/host/tools.js +298 -0
- package/lib/host/types.d.ts +184 -0
- package/lib/host/types.js +26 -0
- package/lib/index.d.ts +55 -0
- package/lib/index.js +245 -0
- package/lib/shared/settings.d.ts +69 -0
- package/lib/shared/settings.js +86 -0
- package/locale/AGENTS.md +22 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +95 -4
- package/src/AGENTS.md +26 -0
- package/src/client/AGENTS.md +28 -0
- package/src/client/css.d.ts +5 -0
- package/src/client/host.ts +82 -0
- package/src/client/index.ts +81 -0
- package/src/client/locales.ts +61 -0
- package/src/client/settings.tsx +222 -0
- package/src/client/styles.css +44 -0
- package/src/host/AGENTS.md +53 -0
- package/src/host/access.ts +82 -0
- package/src/host/call-scope.ts +31 -0
- package/src/host/delegate.ts +166 -0
- package/src/host/grants.ts +82 -0
- package/src/host/legacy.ts +86 -0
- package/src/host/models.ts +155 -0
- package/src/host/paths.ts +100 -0
- package/src/host/policy.ts +169 -0
- package/src/host/prompt.ts +113 -0
- package/src/host/requests.ts +149 -0
- package/src/host/scope.ts +54 -0
- package/src/host/store.ts +159 -0
- package/src/host/tools.ts +318 -0
- package/src/host/types.ts +192 -0
- package/src/index.ts +316 -0
- package/src/shared/AGENTS.md +20 -0
- package/src/shared/settings.ts +104 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path helpers shared by the fence, the spawn binder, and the request tools.
|
|
3
|
+
*
|
|
4
|
+
* Every path the plugin stores or compares is canonical: absolute, with the
|
|
5
|
+
* longest existing prefix resolved through `realpath` (so `/tmp` is
|
|
6
|
+
* `/private/tmp` on darwin, as in the host's own sandbox roots) and any missing
|
|
7
|
+
* suffix appended as spelled. Comparing canonical paths is what the host's fs
|
|
8
|
+
* fence and Seatbelt profile do, so a decision made here matches the one they
|
|
9
|
+
* enforce.
|
|
10
|
+
* @module complete-subagent/host/paths
|
|
11
|
+
*/
|
|
12
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
13
|
+
import { homedir } from 'node:os';
|
|
14
|
+
import { basename, dirname, isAbsolute, join, resolve, sep } from 'node:path';
|
|
15
|
+
/**
|
|
16
|
+
* Resolve a model-supplied path against a base directory. `~` and `~/…` name the
|
|
17
|
+
* user's home, as the host's file tools accept.
|
|
18
|
+
* @param base - absolute directory relative paths start from.
|
|
19
|
+
* @param path - the path as the model wrote it.
|
|
20
|
+
* @returns an absolute, lexically normalized path.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveFrom(base, path) {
|
|
23
|
+
if (path === '~')
|
|
24
|
+
return homedir();
|
|
25
|
+
if (path.startsWith('~/'))
|
|
26
|
+
return join(homedir(), path.slice(2));
|
|
27
|
+
return isAbsolute(path) ? resolve(path) : resolve(base, path);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The canonical spelling of a path that may not exist yet.
|
|
31
|
+
* @param path - an absolute path.
|
|
32
|
+
* @returns the realpath of its longest existing prefix plus the missing rest.
|
|
33
|
+
*/
|
|
34
|
+
export function canonical(path) {
|
|
35
|
+
const missing = [];
|
|
36
|
+
let current = resolve(path);
|
|
37
|
+
while (true) {
|
|
38
|
+
try {
|
|
39
|
+
const real = realpathSync.native(current);
|
|
40
|
+
return missing.length === 0 ? real : join(real, ...missing.reverse());
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
const parent = dirname(current);
|
|
44
|
+
if (parent === current)
|
|
45
|
+
return resolve(path);
|
|
46
|
+
missing.push(basename(current));
|
|
47
|
+
current = parent;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether `path` is `root` or lies below it. Both must be canonical.
|
|
53
|
+
* @param path - candidate path.
|
|
54
|
+
* @param root - containing directory.
|
|
55
|
+
* @returns true when contained.
|
|
56
|
+
*/
|
|
57
|
+
export function isUnder(path, root) {
|
|
58
|
+
if (path === root)
|
|
59
|
+
return true;
|
|
60
|
+
const prefix = root.endsWith(sep) ? root : root + sep;
|
|
61
|
+
return path.startsWith(prefix);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The physical directory a path names right now, as the host accepts it for an
|
|
65
|
+
* additional writable root: existing, a directory, and spelled without symlinks.
|
|
66
|
+
* @param path - a canonical path.
|
|
67
|
+
* @returns the path when it qualifies, otherwise undefined.
|
|
68
|
+
*/
|
|
69
|
+
export function physicalDirectory(path) {
|
|
70
|
+
try {
|
|
71
|
+
const real = realpathSync.native(path);
|
|
72
|
+
return real === resolve(path) && statSync(real).isDirectory() ? real : undefined;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The deepest existing directory at or above a path.
|
|
80
|
+
* @param path - an absolute path.
|
|
81
|
+
* @returns a physical directory that exists.
|
|
82
|
+
*/
|
|
83
|
+
export function nearestDirectory(path) {
|
|
84
|
+
let current = canonical(path);
|
|
85
|
+
while (physicalDirectory(current) === undefined) {
|
|
86
|
+
const parent = dirname(current);
|
|
87
|
+
if (parent === current)
|
|
88
|
+
return current;
|
|
89
|
+
current = parent;
|
|
90
|
+
}
|
|
91
|
+
return current;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Whether a model-written path asks for a whole directory: it ends in a separator.
|
|
95
|
+
* @param path - the path as written.
|
|
96
|
+
* @returns true for `dir/` spellings.
|
|
97
|
+
*/
|
|
98
|
+
export function namesDirectory(path) {
|
|
99
|
+
return path.endsWith('/') || path.endsWith(sep);
|
|
100
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The write fence: narrows a bound child's sandbox policy to its sub-workspace
|
|
3
|
+
* plus what its parent granted.
|
|
4
|
+
*
|
|
5
|
+
* Every enforcing capability (bash's OS sandbox, the fs write fence, terminals,
|
|
6
|
+
* PTC) asks `ctx.sandboxPolicy.resolve({ session })` for each call, and the
|
|
7
|
+
* host's child agents copy the parent's header cwd, so by default a child may
|
|
8
|
+
* write anywhere in the parent's project. {@link patchResolve} wraps the
|
|
9
|
+
* service's prototype method, so every holder of the service, including ones
|
|
10
|
+
* that captured it at load, sees the narrowed policy; disposing restores it.
|
|
11
|
+
* @module complete-subagent/host/policy
|
|
12
|
+
*/
|
|
13
|
+
import type { Binding, BindingStore } from './store.ts';
|
|
14
|
+
import type { SandboxPolicy, SandboxPolicyRequest, SessionLike, WorkingDirectoryLike } from './types.ts';
|
|
15
|
+
/** What the fence reads besides the store. */
|
|
16
|
+
export interface FenceOptions {
|
|
17
|
+
readonly store: BindingStore;
|
|
18
|
+
/** Effective directories, for relative file-tool targets. */
|
|
19
|
+
readonly workingDirectory?: WorkingDirectoryLike;
|
|
20
|
+
/** Reports a failure that made the fence fall back to read-only. */
|
|
21
|
+
readonly warn?: (message: string) => void;
|
|
22
|
+
}
|
|
23
|
+
/** Narrows policies for bound children and answers "may this agent modify that path". */
|
|
24
|
+
export declare class Fence {
|
|
25
|
+
private readonly options;
|
|
26
|
+
private readonly temp;
|
|
27
|
+
constructor(options: FenceOptions);
|
|
28
|
+
/**
|
|
29
|
+
* The binding in force for a session. An inherited binding takes the access
|
|
30
|
+
* of the nearest ancestor with a binding of its own, as that ancestor holds it
|
|
31
|
+
* now; a broken chain leaves no access (fail closed).
|
|
32
|
+
* @param session - any session.
|
|
33
|
+
* @returns the binding, or undefined for sessions the fence leaves alone.
|
|
34
|
+
*/
|
|
35
|
+
bindingOf(session: SessionLike): Binding | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Narrow one resolved policy. Unbound sessions pass through untouched; a
|
|
38
|
+
* bound child gets `workspace-write` rooted at its sub-workspace (or the temp
|
|
39
|
+
* directory when it has none) and its directory grants as extra roots. The
|
|
40
|
+
* one `write`/`edit` call whose target was granted is rooted at that grant
|
|
41
|
+
* instead, because a host older than 0.2.2-alpha.2 reads only `workspaceRoot`.
|
|
42
|
+
* `read-only` stays read-only. A failure falls back to read-only for subagent
|
|
43
|
+
* sessions rather than widening anything, and stays within that one session.
|
|
44
|
+
* @param policy - the host's resolved policy.
|
|
45
|
+
* @param request - the resolve request (its session decides).
|
|
46
|
+
* @returns the policy to enforce.
|
|
47
|
+
*/
|
|
48
|
+
narrow(policy: SandboxPolicy, request: SandboxPolicyRequest): SandboxPolicy;
|
|
49
|
+
/**
|
|
50
|
+
* Whether a session may itself modify a path, judged by its own (possibly
|
|
51
|
+
* narrowed) policy and, for a file, its own file grants. Temp directories
|
|
52
|
+
* count, as they do for the host's `workspace-write`.
|
|
53
|
+
* @param policy - the session's resolved policy.
|
|
54
|
+
* @param session - the session asking.
|
|
55
|
+
* @param path - canonical target.
|
|
56
|
+
* @param kind - whether the target is a directory subtree or one file.
|
|
57
|
+
* @returns true when the session could write there.
|
|
58
|
+
*/
|
|
59
|
+
canModify(policy: SandboxPolicy, session: SessionLike, path: string, kind: 'dir' | 'file'): boolean;
|
|
60
|
+
/** The root that admits the running file-tool call's target when it lies in a grant: a granted directory, or a granted file's folder. */
|
|
61
|
+
private grantedCallRoot;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Wrap `resolve` on the sandbox-policy service's prototype. Cordis hands
|
|
65
|
+
* consumers a tracing proxy whose property reads fall through to the
|
|
66
|
+
* prototype, so patching the prototype reaches every consumer at once.
|
|
67
|
+
* @param service - `ctx.sandboxPolicy` (the proxy or the instance).
|
|
68
|
+
* @param narrow - turns the host's policy into the enforced one.
|
|
69
|
+
* @returns a disposer that restores the original method if it is still ours.
|
|
70
|
+
* @throws when the service has no prototype `resolve` method (a host change).
|
|
71
|
+
*/
|
|
72
|
+
export declare function patchResolve(service: object, narrow: (policy: SandboxPolicy, request: SandboxPolicyRequest) => SandboxPolicy): () => void;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The write fence: narrows a bound child's sandbox policy to its sub-workspace
|
|
3
|
+
* plus what its parent granted.
|
|
4
|
+
*
|
|
5
|
+
* Every enforcing capability (bash's OS sandbox, the fs write fence, terminals,
|
|
6
|
+
* PTC) asks `ctx.sandboxPolicy.resolve({ session })` for each call, and the
|
|
7
|
+
* host's child agents copy the parent's header cwd, so by default a child may
|
|
8
|
+
* write anywhere in the parent's project. {@link patchResolve} wraps the
|
|
9
|
+
* service's prototype method, so every holder of the service, including ones
|
|
10
|
+
* that captured it at load, sees the narrowed policy; disposing restores it.
|
|
11
|
+
* @module complete-subagent/host/policy
|
|
12
|
+
*/
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { dirname } from 'node:path';
|
|
15
|
+
import { currentCall } from "./call-scope.js";
|
|
16
|
+
import { canonical, isUnder, nearestDirectory, physicalDirectory, resolveFrom } from "./paths.js";
|
|
17
|
+
/** File tools whose single target a grant may admit by moving the call's root, and the argument naming it. */
|
|
18
|
+
const FILE_TOOLS = {
|
|
19
|
+
write: 'file_path',
|
|
20
|
+
edit: 'file_path',
|
|
21
|
+
str_replace_editor: 'path',
|
|
22
|
+
};
|
|
23
|
+
/** How many inherited bindings the fence follows before it gives up and closes. */
|
|
24
|
+
const MAX_INHERITANCE = 32;
|
|
25
|
+
/** Narrows policies for bound children and answers "may this agent modify that path". */
|
|
26
|
+
export class Fence {
|
|
27
|
+
options;
|
|
28
|
+
temp = canonical(tmpdir());
|
|
29
|
+
constructor(options) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The binding in force for a session. An inherited binding takes the access
|
|
34
|
+
* of the nearest ancestor with a binding of its own, as that ancestor holds it
|
|
35
|
+
* now; a broken chain leaves no access (fail closed).
|
|
36
|
+
* @param session - any session.
|
|
37
|
+
* @returns the binding, or undefined for sessions the fence leaves alone.
|
|
38
|
+
*/
|
|
39
|
+
bindingOf(session) {
|
|
40
|
+
const own = this.options.store.get(session.id);
|
|
41
|
+
if (own?.inherited !== true)
|
|
42
|
+
return own;
|
|
43
|
+
let source = own;
|
|
44
|
+
for (let hops = 0; source?.inherited === true && hops < MAX_INHERITANCE; hops++)
|
|
45
|
+
source = this.options.store.get(source.parent);
|
|
46
|
+
const access = source === undefined || source.inherited === true ? { workspace: null, grants: [] } : source;
|
|
47
|
+
return { ...own, workspace: access.workspace, grants: access.grants };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Narrow one resolved policy. Unbound sessions pass through untouched; a
|
|
51
|
+
* bound child gets `workspace-write` rooted at its sub-workspace (or the temp
|
|
52
|
+
* directory when it has none) and its directory grants as extra roots. The
|
|
53
|
+
* one `write`/`edit` call whose target was granted is rooted at that grant
|
|
54
|
+
* instead, because a host older than 0.2.2-alpha.2 reads only `workspaceRoot`.
|
|
55
|
+
* `read-only` stays read-only. A failure falls back to read-only for subagent
|
|
56
|
+
* sessions rather than widening anything, and stays within that one session.
|
|
57
|
+
* @param policy - the host's resolved policy.
|
|
58
|
+
* @param request - the resolve request (its session decides).
|
|
59
|
+
* @returns the policy to enforce.
|
|
60
|
+
*/
|
|
61
|
+
narrow(policy, request) {
|
|
62
|
+
const session = request.session;
|
|
63
|
+
if (session === undefined)
|
|
64
|
+
return policy;
|
|
65
|
+
try {
|
|
66
|
+
const binding = this.bindingOf(session);
|
|
67
|
+
if (binding === undefined || policy.mode === 'read-only')
|
|
68
|
+
return policy;
|
|
69
|
+
const roots = new Set(policy.additionalWritableRoots ?? []);
|
|
70
|
+
for (const grant of binding.grants) {
|
|
71
|
+
if (grant.kind !== 'dir')
|
|
72
|
+
continue;
|
|
73
|
+
const directory = physicalDirectory(grant.path);
|
|
74
|
+
if (directory !== undefined)
|
|
75
|
+
roots.add(directory);
|
|
76
|
+
}
|
|
77
|
+
const callRoot = this.grantedCallRoot(session, binding);
|
|
78
|
+
if (callRoot !== undefined)
|
|
79
|
+
roots.add(callRoot);
|
|
80
|
+
return {
|
|
81
|
+
...policy,
|
|
82
|
+
mode: 'workspace-write',
|
|
83
|
+
workspaceRoot: callRoot ?? binding.workspace ?? this.temp,
|
|
84
|
+
additionalWritableRoots: [...roots],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (session.header.origin !== 'subagent')
|
|
89
|
+
return policy;
|
|
90
|
+
this.options.warn?.(`complete-subagent: fence failed for ${session.id}, using read-only: ${String(error)}`);
|
|
91
|
+
return { ...policy, mode: 'read-only' };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Whether a session may itself modify a path, judged by its own (possibly
|
|
96
|
+
* narrowed) policy and, for a file, its own file grants. Temp directories
|
|
97
|
+
* count, as they do for the host's `workspace-write`.
|
|
98
|
+
* @param policy - the session's resolved policy.
|
|
99
|
+
* @param session - the session asking.
|
|
100
|
+
* @param path - canonical target.
|
|
101
|
+
* @param kind - whether the target is a directory subtree or one file.
|
|
102
|
+
* @returns true when the session could write there.
|
|
103
|
+
*/
|
|
104
|
+
canModify(policy, session, path, kind) {
|
|
105
|
+
if (policy.mode === 'danger-full-access')
|
|
106
|
+
return true;
|
|
107
|
+
if (policy.mode === 'read-only')
|
|
108
|
+
return false;
|
|
109
|
+
const roots = [policy.workspaceRoot, ...policy.additionalWritableRoots ?? [], '/tmp', this.temp].map(canonical);
|
|
110
|
+
if (roots.some(root => isUnder(path, root)))
|
|
111
|
+
return true;
|
|
112
|
+
if (kind !== 'file')
|
|
113
|
+
return false;
|
|
114
|
+
return this.bindingOf(session)?.grants.some(grant => grant.kind === 'file' && grant.path === path) === true;
|
|
115
|
+
}
|
|
116
|
+
/** The root that admits the running file-tool call's target when it lies in a grant: a granted directory, or a granted file's folder. */
|
|
117
|
+
grantedCallRoot(session, binding) {
|
|
118
|
+
if (binding.grants.length === 0)
|
|
119
|
+
return undefined;
|
|
120
|
+
const call = currentCall();
|
|
121
|
+
if (call?.agent?.session.id !== session.id)
|
|
122
|
+
return undefined;
|
|
123
|
+
const key = FILE_TOOLS[call.name];
|
|
124
|
+
if (key === undefined)
|
|
125
|
+
return undefined;
|
|
126
|
+
const raw = call.arguments?.[key];
|
|
127
|
+
if (typeof raw !== 'string' || raw === '')
|
|
128
|
+
return undefined;
|
|
129
|
+
const base = this.options.workingDirectory?.get(session) ?? session.header.cwd ?? process.cwd();
|
|
130
|
+
const target = canonical(resolveFrom(base, raw));
|
|
131
|
+
if (binding.workspace !== null && isUnder(target, binding.workspace))
|
|
132
|
+
return undefined;
|
|
133
|
+
for (const grant of binding.grants) {
|
|
134
|
+
if (grant.kind === 'file' && grant.path === target)
|
|
135
|
+
return nearestDirectory(dirname(target));
|
|
136
|
+
if (grant.kind === 'dir' && isUnder(target, grant.path)) {
|
|
137
|
+
const directory = physicalDirectory(grant.path);
|
|
138
|
+
if (directory !== undefined)
|
|
139
|
+
return directory;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Wrap `resolve` on the sandbox-policy service's prototype. Cordis hands
|
|
147
|
+
* consumers a tracing proxy whose property reads fall through to the
|
|
148
|
+
* prototype, so patching the prototype reaches every consumer at once.
|
|
149
|
+
* @param service - `ctx.sandboxPolicy` (the proxy or the instance).
|
|
150
|
+
* @param narrow - turns the host's policy into the enforced one.
|
|
151
|
+
* @returns a disposer that restores the original method if it is still ours.
|
|
152
|
+
* @throws when the service has no prototype `resolve` method (a host change).
|
|
153
|
+
*/
|
|
154
|
+
export function patchResolve(service, narrow) {
|
|
155
|
+
const prototype = Object.getPrototypeOf(service);
|
|
156
|
+
const original = prototype?.resolve;
|
|
157
|
+
if (prototype === null || typeof original !== 'function') {
|
|
158
|
+
throw new Error('sandboxPolicy.resolve is not a prototype method; this host version is not supported');
|
|
159
|
+
}
|
|
160
|
+
const patched = function resolve(request) {
|
|
161
|
+
const policy = original.call(this, request);
|
|
162
|
+
return narrow(policy, request ?? {});
|
|
163
|
+
};
|
|
164
|
+
prototype.resolve = patched;
|
|
165
|
+
return () => {
|
|
166
|
+
if (prototype.resolve === patched)
|
|
167
|
+
prototype.resolve = original;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing guidance. A parent reads the delegation and sub-workspace rules
|
|
3
|
+
* in the `delegate` / `delegate_fork` descriptions and parameters; a bound
|
|
4
|
+
* child reads its sub-workspace and grants in a runtime context; a parent with
|
|
5
|
+
* open requests reads them in another.
|
|
6
|
+
* @module complete-subagent/host/prompt
|
|
7
|
+
*/
|
|
8
|
+
import type { WriteRequest } from './requests.ts';
|
|
9
|
+
import type { Binding } from './store.ts';
|
|
10
|
+
import type { DelegateTool } from './types.ts';
|
|
11
|
+
/**
|
|
12
|
+
* The description of one delegation tool.
|
|
13
|
+
* @param tool - which tool.
|
|
14
|
+
* @param models - the model-selection paragraph from `describeModels`.
|
|
15
|
+
* @returns the full description.
|
|
16
|
+
*/
|
|
17
|
+
export declare function delegateDescription(tool: DelegateTool, models: string): string;
|
|
18
|
+
/** The `prompt` parameter, by whether the child inherits this conversation. */
|
|
19
|
+
export declare const PROMPT_DESCRIPTION: Readonly<Record<DelegateTool, string>>;
|
|
20
|
+
/** The `cwd` parameter. */
|
|
21
|
+
export declare const CWD_DESCRIPTION: string;
|
|
22
|
+
/** The `writable_paths` parameter. */
|
|
23
|
+
export declare const WRITABLE_PATHS_DESCRIPTION: string;
|
|
24
|
+
/** The `model` parameter. */
|
|
25
|
+
export declare const MODEL_DESCRIPTION = "The child's model as \"provider/model\", one of the listed choices. Omit it to use your own model.";
|
|
26
|
+
/** The `model` parameter when the child cannot run the parent's model. */
|
|
27
|
+
export declare const LISTED_MODEL_DESCRIPTION = "The child's model as \"provider/model\", one of the listed choices. Omit it to use the first.";
|
|
28
|
+
/** The `reasoning_effort` parameter. */
|
|
29
|
+
export declare const EFFORT_DESCRIPTION: string;
|
|
30
|
+
/**
|
|
31
|
+
* Runtime context for a bound child.
|
|
32
|
+
* @param binding - the child's binding in force (inherited access already resolved).
|
|
33
|
+
* @returns the context text.
|
|
34
|
+
*/
|
|
35
|
+
export declare function childContext(binding: Binding, shellReachesGrants?: boolean): string;
|
|
36
|
+
/**
|
|
37
|
+
* Runtime context for a parent with open requests.
|
|
38
|
+
* @param requests - its open requests.
|
|
39
|
+
* @returns the context text, or '' when none are open.
|
|
40
|
+
*/
|
|
41
|
+
export declare function pendingContext(requests: readonly WriteRequest[]): string;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing guidance. A parent reads the delegation and sub-workspace rules
|
|
3
|
+
* in the `delegate` / `delegate_fork` descriptions and parameters; a bound
|
|
4
|
+
* child reads its sub-workspace and grants in a runtime context; a parent with
|
|
5
|
+
* open requests reads them in another.
|
|
6
|
+
* @module complete-subagent/host/prompt
|
|
7
|
+
*/
|
|
8
|
+
import { ANSWER_TOOL, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL } from "./types.js";
|
|
9
|
+
/** What a delegation tool is for, by whether the child inherits this conversation. */
|
|
10
|
+
const PURPOSE = {
|
|
11
|
+
delegate: 'Delegate a self-contained task to a subagent: a separate agent that works in its own context, so the work '
|
|
12
|
+
+ '(research, a scoped implementation, an analysis) does not consume this conversation\'s context.',
|
|
13
|
+
delegate_fork: 'Delegate a task to a subagent that inherits this conversation: a child seeded with all completed turns so far '
|
|
14
|
+
+ '(not the current in-flight turn). Use it when the subtask builds on this conversation\'s context, such as a follow-up '
|
|
15
|
+
+ 'analysis, a review, or a continuation, without consuming this conversation\'s context for the work itself.',
|
|
16
|
+
};
|
|
17
|
+
/** How a started child reports back and how to keep working meanwhile. */
|
|
18
|
+
const LIFECYCLE = 'It returns the subagent\'s id at once; the runtime notifies you with its result when it finishes. '
|
|
19
|
+
+ 'Use `send_message` to steer it while it runs or to continue its conversation afterwards. '
|
|
20
|
+
+ 'Start independent delegations together in one message and keep working; when only waiting remains, end your turn instead of polling.';
|
|
21
|
+
/** The sub-workspace rules every delegation tool states. */
|
|
22
|
+
const SUB_WORKSPACES = [
|
|
23
|
+
'Sub-workspaces: the `cwd` you pass becomes the child\'s sub-workspace, created if missing, and the only place where it may modify files '
|
|
24
|
+
+ 'besides temporary directories and `writable_paths`. Without a `cwd`, or with one outside your own writable area, the child can read but not modify the workspace.',
|
|
25
|
+
`A child that needs more sends you a "[write-access request wr-…]" message and is paused until you answer with \`${ANSWER_TOOL}\`. `
|
|
26
|
+
+ `You can also change a child's access at any time with \`${GRANT_TOOL}\` and \`${REVOKE_TOOL}\`. `
|
|
27
|
+
+ 'You can grant only paths you can modify yourself, and whatever your child starts is confined the same way.',
|
|
28
|
+
].join(' ');
|
|
29
|
+
/**
|
|
30
|
+
* The description of one delegation tool.
|
|
31
|
+
* @param tool - which tool.
|
|
32
|
+
* @param models - the model-selection paragraph from `describeModels`.
|
|
33
|
+
* @returns the full description.
|
|
34
|
+
*/
|
|
35
|
+
export function delegateDescription(tool, models) {
|
|
36
|
+
const cache = tool === 'delegate_fork'
|
|
37
|
+
? ' Choosing another model loses the provider-side reuse of the inherited conversation.'
|
|
38
|
+
: '';
|
|
39
|
+
return `${PURPOSE[tool]} ${LIFECYCLE}\n\n${SUB_WORKSPACES}\n\n${models}${cache}`;
|
|
40
|
+
}
|
|
41
|
+
/** The `prompt` parameter, by whether the child inherits this conversation. */
|
|
42
|
+
export const PROMPT_DESCRIPTION = {
|
|
43
|
+
delegate: 'The complete, self-contained task for the subagent. It does not share this conversation\'s context, so include everything it needs.',
|
|
44
|
+
delegate_fork: 'The task for the subagent. It already sees this conversation\'s completed turns, so build on them and state only what is new.',
|
|
45
|
+
};
|
|
46
|
+
/** The `cwd` parameter. */
|
|
47
|
+
export const CWD_DESCRIPTION = 'The child\'s sub-workspace and initial directory; relative paths use your current directory. '
|
|
48
|
+
+ 'It is created if missing, and the child may modify files only there (plus temporary directories and `writable_paths`). '
|
|
49
|
+
+ 'Omit it, or name a directory outside your own writable area, and the child can read but not modify the workspace. '
|
|
50
|
+
+ 'Later directory changes in either agent are independent.';
|
|
51
|
+
/** The `writable_paths` parameter. */
|
|
52
|
+
export const WRITABLE_PATHS_DESCRIPTION = 'Paths outside the sub-workspace that the child may also modify from the start, '
|
|
53
|
+
+ 'relative to your current directory or absolute. End a path with "/" (or name an existing directory) to grant the whole directory '
|
|
54
|
+
+ 'to every tool, including shell commands; a single file is writable through the write and edit tools only. '
|
|
55
|
+
+ 'You can grant only what you can modify yourself; the child asks you for anything else.';
|
|
56
|
+
/** The `model` parameter. */
|
|
57
|
+
export const MODEL_DESCRIPTION = 'The child\'s model as "provider/model", one of the listed choices. Omit it to use your own model.';
|
|
58
|
+
/** The `model` parameter when the child cannot run the parent's model. */
|
|
59
|
+
export const LISTED_MODEL_DESCRIPTION = 'The child\'s model as "provider/model", one of the listed choices. Omit it to use the first.';
|
|
60
|
+
/** The `reasoning_effort` parameter. */
|
|
61
|
+
export const EFFORT_DESCRIPTION = 'The child\'s reasoning effort id, such as "low", "high", or "max", among those its model allows. '
|
|
62
|
+
+ 'Use lower efforts for simple, mechanical work and higher ones for hard reasoning.';
|
|
63
|
+
/**
|
|
64
|
+
* Runtime context for a bound child.
|
|
65
|
+
* @param binding - the child's binding in force (inherited access already resolved).
|
|
66
|
+
* @returns the context text.
|
|
67
|
+
*/
|
|
68
|
+
export function childContext(binding, shellReachesGrants = true) {
|
|
69
|
+
const lines = [
|
|
70
|
+
binding.workspace === null
|
|
71
|
+
? 'Sub-workspace: none. You can read the workspace, but you may modify files only in temporary directories.'
|
|
72
|
+
: `Sub-workspace: ${binding.workspace}. You may modify files only under this directory and temporary directories.`,
|
|
73
|
+
];
|
|
74
|
+
const directories = binding.grants.filter(grant => grant.kind === 'dir').map(grant => grant.path);
|
|
75
|
+
const files = binding.grants.filter(grant => grant.kind === 'file').map(grant => grant.path);
|
|
76
|
+
const from = binding.inherited === true ? 'to the agent above you' : 'by your parent';
|
|
77
|
+
const fileToolsOnly = '(write and edit tools only, not shell commands)';
|
|
78
|
+
if (directories.length > 0)
|
|
79
|
+
lines.push(`Also granted ${from} ${shellReachesGrants ? '(any tool)' : fileToolsOnly}: ${directories.join(', ')}`);
|
|
80
|
+
if (files.length > 0)
|
|
81
|
+
lines.push(`Also granted ${from} ${fileToolsOnly}: ${files.join(', ')}`);
|
|
82
|
+
lines.push(binding.inherited === true
|
|
83
|
+
? 'Your parent is a confined subagent, so you share its access and cannot ask for more. If the task needs other paths, report that back instead.'
|
|
84
|
+
: `To modify anything else, call \`${REQUEST_TOOL}\` with the exact paths and a short reason, and wait for the answer. `
|
|
85
|
+
+ 'This replaces approval: do not set `sandbox_permissions`.');
|
|
86
|
+
return lines.join('\n');
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Runtime context for a parent with open requests.
|
|
90
|
+
* @param requests - its open requests.
|
|
91
|
+
* @returns the context text, or '' when none are open.
|
|
92
|
+
*/
|
|
93
|
+
export function pendingContext(requests) {
|
|
94
|
+
if (requests.length === 0)
|
|
95
|
+
return '';
|
|
96
|
+
return [
|
|
97
|
+
`Write-access requests waiting for your \`${ANSWER_TOOL}\` answer (the subagents are paused):`,
|
|
98
|
+
...requests.map(request => `- ${request.id} from ${request.child}: ${request.paths.map(item => item.directory ? `${item.path}/` : item.path).join(', ')} — ${request.reason}`),
|
|
99
|
+
].join('\n');
|
|
100
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write-access requests waiting for a parent's answer.
|
|
3
|
+
*
|
|
4
|
+
* A request lives only in memory, from the child's `request_write_access` call
|
|
5
|
+
* until the parent answers, the timeout passes, or the child's call is
|
|
6
|
+
* cancelled. If the process exits, the child's call ends with it, so nothing
|
|
7
|
+
* needs to survive a restart; the grants an answer produces are stored by the
|
|
8
|
+
* caller in the binding store.
|
|
9
|
+
* @module complete-subagent/host/requests
|
|
10
|
+
*/
|
|
11
|
+
/** One requested path. */
|
|
12
|
+
export interface RequestedPath {
|
|
13
|
+
/** Canonical absolute path. */
|
|
14
|
+
readonly path: string;
|
|
15
|
+
/** Whether the whole directory is asked for (an existing directory, or a `dir/` spelling). */
|
|
16
|
+
readonly directory: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** What a child's call returns. */
|
|
19
|
+
export interface WriteAnswer {
|
|
20
|
+
readonly decision: 'allow' | 'deny';
|
|
21
|
+
readonly granted: {
|
|
22
|
+
path: string;
|
|
23
|
+
kind: 'dir' | 'file';
|
|
24
|
+
}[];
|
|
25
|
+
readonly message?: string;
|
|
26
|
+
}
|
|
27
|
+
/** One open request. */
|
|
28
|
+
export interface WriteRequest {
|
|
29
|
+
readonly id: string;
|
|
30
|
+
/** Child session id. */
|
|
31
|
+
readonly child: string;
|
|
32
|
+
/** Parent session id: the only agent that may answer. */
|
|
33
|
+
readonly parent: string;
|
|
34
|
+
/** The child's sub-workspace, for the parent's message. */
|
|
35
|
+
readonly workspace: string | null;
|
|
36
|
+
readonly paths: readonly RequestedPath[];
|
|
37
|
+
readonly reason: string;
|
|
38
|
+
readonly at: number;
|
|
39
|
+
}
|
|
40
|
+
/** The open requests of this process. */
|
|
41
|
+
export declare class RequestBroker {
|
|
42
|
+
private readonly entries;
|
|
43
|
+
/**
|
|
44
|
+
* Open a request.
|
|
45
|
+
* @param fields - everything but the id and time.
|
|
46
|
+
* @returns the open request.
|
|
47
|
+
*/
|
|
48
|
+
open(fields: Omit<WriteRequest, 'id' | 'at'>): WriteRequest;
|
|
49
|
+
/**
|
|
50
|
+
* An open request by id.
|
|
51
|
+
* @param id - request id.
|
|
52
|
+
* @returns the request, or undefined once answered, expired, or cancelled.
|
|
53
|
+
*/
|
|
54
|
+
get(id: string): WriteRequest | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The open requests addressed to one parent, oldest first.
|
|
57
|
+
* @param parent - parent session id.
|
|
58
|
+
* @returns its requests.
|
|
59
|
+
*/
|
|
60
|
+
pendingFor(parent: string): WriteRequest[];
|
|
61
|
+
/**
|
|
62
|
+
* Answer an open request and close it.
|
|
63
|
+
* @param id - request id.
|
|
64
|
+
* @param answer - the decision the child receives.
|
|
65
|
+
* @returns false when the request was no longer open.
|
|
66
|
+
*/
|
|
67
|
+
settle(id: string, answer: WriteAnswer): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Close a request without an answer (the message to the parent failed).
|
|
70
|
+
* @param id - request id.
|
|
71
|
+
*/
|
|
72
|
+
close(id: string): void;
|
|
73
|
+
/**
|
|
74
|
+
* Wait for the answer. A timeout closes the request as denied; cancellation
|
|
75
|
+
* closes it and rejects with the signal's reason.
|
|
76
|
+
* @param id - request id.
|
|
77
|
+
* @param timeoutMs - how long the parent has.
|
|
78
|
+
* @param signal - the child's call cancellation.
|
|
79
|
+
* @returns the parent's answer, or a denial on timeout.
|
|
80
|
+
*/
|
|
81
|
+
wait(id: string, timeoutMs: number, signal: AbortSignal): Promise<WriteAnswer>;
|
|
82
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write-access requests waiting for a parent's answer.
|
|
3
|
+
*
|
|
4
|
+
* A request lives only in memory, from the child's `request_write_access` call
|
|
5
|
+
* until the parent answers, the timeout passes, or the child's call is
|
|
6
|
+
* cancelled. If the process exits, the child's call ends with it, so nothing
|
|
7
|
+
* needs to survive a restart; the grants an answer produces are stored by the
|
|
8
|
+
* caller in the binding store.
|
|
9
|
+
* @module complete-subagent/host/requests
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes } from 'node:crypto';
|
|
12
|
+
/** The open requests of this process. */
|
|
13
|
+
export class RequestBroker {
|
|
14
|
+
entries = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* Open a request.
|
|
17
|
+
* @param fields - everything but the id and time.
|
|
18
|
+
* @returns the open request.
|
|
19
|
+
*/
|
|
20
|
+
open(fields) {
|
|
21
|
+
let id;
|
|
22
|
+
do
|
|
23
|
+
id = `wr-${randomBytes(3).toString('hex')}`;
|
|
24
|
+
while (this.entries.has(id));
|
|
25
|
+
const request = { ...fields, id, at: Date.now() };
|
|
26
|
+
let answer;
|
|
27
|
+
const answered = new Promise((resolve) => {
|
|
28
|
+
answer = resolve;
|
|
29
|
+
});
|
|
30
|
+
this.entries.set(id, { request, answered, answer });
|
|
31
|
+
return request;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* An open request by id.
|
|
35
|
+
* @param id - request id.
|
|
36
|
+
* @returns the request, or undefined once answered, expired, or cancelled.
|
|
37
|
+
*/
|
|
38
|
+
get(id) {
|
|
39
|
+
return this.entries.get(id)?.request;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The open requests addressed to one parent, oldest first.
|
|
43
|
+
* @param parent - parent session id.
|
|
44
|
+
* @returns its requests.
|
|
45
|
+
*/
|
|
46
|
+
pendingFor(parent) {
|
|
47
|
+
return [...this.entries.values()].map(entry => entry.request).filter(request => request.parent === parent);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Answer an open request and close it.
|
|
51
|
+
* @param id - request id.
|
|
52
|
+
* @param answer - the decision the child receives.
|
|
53
|
+
* @returns false when the request was no longer open.
|
|
54
|
+
*/
|
|
55
|
+
settle(id, answer) {
|
|
56
|
+
const entry = this.entries.get(id);
|
|
57
|
+
if (entry === undefined)
|
|
58
|
+
return false;
|
|
59
|
+
this.entries.delete(id);
|
|
60
|
+
entry.answer(answer);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Close a request without an answer (the message to the parent failed).
|
|
65
|
+
* @param id - request id.
|
|
66
|
+
*/
|
|
67
|
+
close(id) {
|
|
68
|
+
this.entries.delete(id);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Wait for the answer. A timeout closes the request as denied; cancellation
|
|
72
|
+
* closes it and rejects with the signal's reason.
|
|
73
|
+
* @param id - request id.
|
|
74
|
+
* @param timeoutMs - how long the parent has.
|
|
75
|
+
* @param signal - the child's call cancellation.
|
|
76
|
+
* @returns the parent's answer, or a denial on timeout.
|
|
77
|
+
*/
|
|
78
|
+
wait(id, timeoutMs, signal) {
|
|
79
|
+
const entry = this.entries.get(id);
|
|
80
|
+
if (entry === undefined)
|
|
81
|
+
return Promise.resolve({ decision: 'deny', granted: [], message: 'The request was closed before it could be answered.' });
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
const finish = () => {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
signal.removeEventListener('abort', onAbort);
|
|
86
|
+
};
|
|
87
|
+
const onAbort = () => {
|
|
88
|
+
finish();
|
|
89
|
+
this.entries.delete(id);
|
|
90
|
+
reject(signal.reason ?? new Error('aborted'));
|
|
91
|
+
};
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
finish();
|
|
94
|
+
this.entries.delete(id);
|
|
95
|
+
resolve({ decision: 'deny', granted: [], message: `Your parent did not answer within ${Math.round(timeoutMs / 1000)} seconds; nothing was granted.` });
|
|
96
|
+
}, timeoutMs);
|
|
97
|
+
timer.unref?.();
|
|
98
|
+
if (signal.aborted) {
|
|
99
|
+
onAbort();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
103
|
+
void entry.answered.then((answer) => {
|
|
104
|
+
finish();
|
|
105
|
+
resolve(answer);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|