@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,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which agents the plugin serves: every agent, or only those whose session
|
|
3
|
+
* runs one of the configured agent presets.
|
|
4
|
+
*
|
|
5
|
+
* A bundle that mounts this plugin for its own preset sets `presets`, so the
|
|
6
|
+
* tools and prompt texts stay out of every other preset's sessions. A child
|
|
7
|
+
* records its parent's preset unless it was started with one of its own, so a
|
|
8
|
+
* served agent's children are served too.
|
|
9
|
+
* @module complete-subagent/host/scope
|
|
10
|
+
*/
|
|
11
|
+
import type { AgentPresetsLike, SessionLike, SessionProjectionsLike } from './types.ts';
|
|
12
|
+
/** Where a session's preset can be read, each looked up at call time because both are optional. */
|
|
13
|
+
export interface PresetSources {
|
|
14
|
+
readonly agentPresets: () => AgentPresetsLike | undefined;
|
|
15
|
+
readonly sessionProjections: () => SessionProjectionsLike | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The preset a session runs. The registry's method comes first because newer
|
|
19
|
+
* hosts deprecate reading its projection directly; 0.2.1-rc.5 has no such
|
|
20
|
+
* method, and the projection is how that host reads it too.
|
|
21
|
+
* @param session - the session.
|
|
22
|
+
* @param sources - the host services to read.
|
|
23
|
+
* @returns the preset id, or undefined when none can be read.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sessionPreset(session: SessionLike, sources: PresetSources): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Build the "does the plugin serve this session" check.
|
|
28
|
+
* @param presets - the configured preset ids; empty serves every session.
|
|
29
|
+
* @param sources - the host services a session's preset is read from.
|
|
30
|
+
* @returns the check. With presets configured, a session whose preset cannot be
|
|
31
|
+
* read (no service, none recorded, a failure) is not served.
|
|
32
|
+
*/
|
|
33
|
+
export declare function presetScope(presets: readonly string[], sources: PresetSources): (session: SessionLike) => boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which agents the plugin serves: every agent, or only those whose session
|
|
3
|
+
* runs one of the configured agent presets.
|
|
4
|
+
*
|
|
5
|
+
* A bundle that mounts this plugin for its own preset sets `presets`, so the
|
|
6
|
+
* tools and prompt texts stay out of every other preset's sessions. A child
|
|
7
|
+
* records its parent's preset unless it was started with one of its own, so a
|
|
8
|
+
* served agent's children are served too.
|
|
9
|
+
* @module complete-subagent/host/scope
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* The preset a session runs. The registry's method comes first because newer
|
|
13
|
+
* hosts deprecate reading its projection directly; 0.2.1-rc.5 has no such
|
|
14
|
+
* method, and the projection is how that host reads it too.
|
|
15
|
+
* @param session - the session.
|
|
16
|
+
* @param sources - the host services to read.
|
|
17
|
+
* @returns the preset id, or undefined when none can be read.
|
|
18
|
+
*/
|
|
19
|
+
export function sessionPreset(session, sources) {
|
|
20
|
+
const registry = sources.agentPresets();
|
|
21
|
+
const preset = typeof registry?.presetForSession === 'function'
|
|
22
|
+
? registry.presetForSession(session)
|
|
23
|
+
: sources.sessionProjections()?.stateOf(session, 'agentPreset');
|
|
24
|
+
return typeof preset === 'string' ? preset : undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Build the "does the plugin serve this session" check.
|
|
28
|
+
* @param presets - the configured preset ids; empty serves every session.
|
|
29
|
+
* @param sources - the host services a session's preset is read from.
|
|
30
|
+
* @returns the check. With presets configured, a session whose preset cannot be
|
|
31
|
+
* read (no service, none recorded, a failure) is not served.
|
|
32
|
+
*/
|
|
33
|
+
export function presetScope(presets, sources) {
|
|
34
|
+
if (presets.length === 0)
|
|
35
|
+
return () => true;
|
|
36
|
+
const allowed = new Set(presets);
|
|
37
|
+
return (session) => {
|
|
38
|
+
try {
|
|
39
|
+
const preset = sessionPreset(session, sources);
|
|
40
|
+
return preset !== undefined && allowed.has(preset);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable bindings: which sub-workspace a child owns and what its parent granted.
|
|
3
|
+
*
|
|
4
|
+
* One JSON file per child session under
|
|
5
|
+
* `$DSH_HOME/plugin-data/complete-subagent/children/`. A child keeps its session
|
|
6
|
+
* id when it is cold-resumed (a finished child woken by `send_message`, or any
|
|
7
|
+
* child after a restart), so the same file applies again. Reads are synchronous
|
|
8
|
+
* and cached, because the sandbox policy is resolved synchronously on every
|
|
9
|
+
* capability call; writes replace the file atomically.
|
|
10
|
+
* @module complete-subagent/host/store
|
|
11
|
+
*/
|
|
12
|
+
/** One path a parent let its child modify. */
|
|
13
|
+
export interface Grant {
|
|
14
|
+
/** Canonical absolute path. */
|
|
15
|
+
readonly path: string;
|
|
16
|
+
/** A directory grant covers its subtree for every tool; a file grant covers the file for `write`/`edit`. */
|
|
17
|
+
readonly kind: 'dir' | 'file';
|
|
18
|
+
/** The request that produced it. */
|
|
19
|
+
readonly requestId: string;
|
|
20
|
+
/** Unix epoch milliseconds. */
|
|
21
|
+
readonly at: number;
|
|
22
|
+
}
|
|
23
|
+
/** Everything the fence needs to know about one child. */
|
|
24
|
+
export interface Binding {
|
|
25
|
+
readonly version: 1;
|
|
26
|
+
/** Child session id. */
|
|
27
|
+
readonly child: string;
|
|
28
|
+
/** Parent session id. */
|
|
29
|
+
readonly parent: string;
|
|
30
|
+
/** Canonical sub-workspace, or null when the child has none. */
|
|
31
|
+
readonly workspace: string | null;
|
|
32
|
+
readonly grants: readonly Grant[];
|
|
33
|
+
/**
|
|
34
|
+
* Set for a child that a confined agent started some other way (the official
|
|
35
|
+
* subagent tools, a workflow): it holds whatever access its parent holds now,
|
|
36
|
+
* and `workspace` / `grants` stay empty.
|
|
37
|
+
*/
|
|
38
|
+
readonly inherited?: true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The default store directory, following the host's home rule: `DSH_HOME`, then
|
|
42
|
+
* `~/.dsh`.
|
|
43
|
+
* @param env - environment to read.
|
|
44
|
+
* @returns the absolute directory holding one file per child.
|
|
45
|
+
*/
|
|
46
|
+
export declare function defaultStoreDirectory(env?: NodeJS.ProcessEnv): string;
|
|
47
|
+
/** File-backed binding store with a synchronous read cache. */
|
|
48
|
+
export declare class BindingStore {
|
|
49
|
+
readonly directory: string;
|
|
50
|
+
private readonly cache;
|
|
51
|
+
/** @param directory - where the per-child files live; created on first write. */
|
|
52
|
+
constructor(directory: string);
|
|
53
|
+
/**
|
|
54
|
+
* The binding of one child, or undefined when it was not bound by this plugin.
|
|
55
|
+
* @param child - child session id.
|
|
56
|
+
* @returns the stored binding.
|
|
57
|
+
*/
|
|
58
|
+
get(child: string): Binding | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Store a binding, replacing any earlier one for the same child. The cache is
|
|
61
|
+
* updated first, so a failed write still fences the child for the life of
|
|
62
|
+
* this process; the failure is rethrown for the caller to report.
|
|
63
|
+
* @param binding - the complete binding.
|
|
64
|
+
*/
|
|
65
|
+
set(binding: Binding): void;
|
|
66
|
+
/**
|
|
67
|
+
* Forget a child, for a delegation that failed to start.
|
|
68
|
+
* @param child - child session id.
|
|
69
|
+
*/
|
|
70
|
+
delete(child: string): void;
|
|
71
|
+
/**
|
|
72
|
+
* Add grants to a bound child, skipping ones it already holds.
|
|
73
|
+
* @param child - child session id.
|
|
74
|
+
* @param grants - new grants.
|
|
75
|
+
* @returns the updated binding.
|
|
76
|
+
* @throws when the child is not bound.
|
|
77
|
+
*/
|
|
78
|
+
addGrants(child: string, grants: readonly Grant[]): Binding;
|
|
79
|
+
/**
|
|
80
|
+
* Remove a bound child's grants on the given paths, of either kind.
|
|
81
|
+
* @param child - child session id.
|
|
82
|
+
* @param paths - canonical paths to take back.
|
|
83
|
+
* @returns the removed grants.
|
|
84
|
+
* @throws when the child is not bound.
|
|
85
|
+
*/
|
|
86
|
+
removeGrants(child: string, paths: readonly string[]): Grant[];
|
|
87
|
+
private fileOf;
|
|
88
|
+
private load;
|
|
89
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable bindings: which sub-workspace a child owns and what its parent granted.
|
|
3
|
+
*
|
|
4
|
+
* One JSON file per child session under
|
|
5
|
+
* `$DSH_HOME/plugin-data/complete-subagent/children/`. A child keeps its session
|
|
6
|
+
* id when it is cold-resumed (a finished child woken by `send_message`, or any
|
|
7
|
+
* child after a restart), so the same file applies again. Reads are synchronous
|
|
8
|
+
* and cached, because the sandbox policy is resolved synchronously on every
|
|
9
|
+
* capability call; writes replace the file atomically.
|
|
10
|
+
* @module complete-subagent/host/store
|
|
11
|
+
*/
|
|
12
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { homedir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
/**
|
|
16
|
+
* The default store directory, following the host's home rule: `DSH_HOME`, then
|
|
17
|
+
* `~/.dsh`.
|
|
18
|
+
* @param env - environment to read.
|
|
19
|
+
* @returns the absolute directory holding one file per child.
|
|
20
|
+
*/
|
|
21
|
+
export function defaultStoreDirectory(env = process.env) {
|
|
22
|
+
const home = env.DSH_HOME !== undefined && env.DSH_HOME !== '' ? env.DSH_HOME : join(homedir(), '.dsh');
|
|
23
|
+
return join(home, 'plugin-data', 'complete-subagent', 'children');
|
|
24
|
+
}
|
|
25
|
+
/** File-backed binding store with a synchronous read cache. */
|
|
26
|
+
export class BindingStore {
|
|
27
|
+
directory;
|
|
28
|
+
cache = new Map();
|
|
29
|
+
/** @param directory - where the per-child files live; created on first write. */
|
|
30
|
+
constructor(directory) {
|
|
31
|
+
this.directory = directory;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The binding of one child, or undefined when it was not bound by this plugin.
|
|
35
|
+
* @param child - child session id.
|
|
36
|
+
* @returns the stored binding.
|
|
37
|
+
*/
|
|
38
|
+
get(child) {
|
|
39
|
+
const cached = this.cache.get(child);
|
|
40
|
+
if (cached !== undefined)
|
|
41
|
+
return cached ?? undefined;
|
|
42
|
+
const loaded = this.load(child);
|
|
43
|
+
this.cache.set(child, loaded ?? null);
|
|
44
|
+
return loaded;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Store a binding, replacing any earlier one for the same child. The cache is
|
|
48
|
+
* updated first, so a failed write still fences the child for the life of
|
|
49
|
+
* this process; the failure is rethrown for the caller to report.
|
|
50
|
+
* @param binding - the complete binding.
|
|
51
|
+
*/
|
|
52
|
+
set(binding) {
|
|
53
|
+
this.cache.set(binding.child, binding);
|
|
54
|
+
mkdirSync(this.directory, { recursive: true });
|
|
55
|
+
const file = this.fileOf(binding.child);
|
|
56
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
57
|
+
writeFileSync(temp, `${JSON.stringify(binding, null, 2)}\n`);
|
|
58
|
+
renameSync(temp, file);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Forget a child, for a delegation that failed to start.
|
|
62
|
+
* @param child - child session id.
|
|
63
|
+
*/
|
|
64
|
+
delete(child) {
|
|
65
|
+
this.cache.set(child, null);
|
|
66
|
+
rmSync(this.fileOf(child), { force: true });
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Add grants to a bound child, skipping ones it already holds.
|
|
70
|
+
* @param child - child session id.
|
|
71
|
+
* @param grants - new grants.
|
|
72
|
+
* @returns the updated binding.
|
|
73
|
+
* @throws when the child is not bound.
|
|
74
|
+
*/
|
|
75
|
+
addGrants(child, grants) {
|
|
76
|
+
const binding = this.get(child);
|
|
77
|
+
if (binding === undefined)
|
|
78
|
+
throw new Error(`child ${child} has no binding`);
|
|
79
|
+
const held = new Set(binding.grants.map(grant => `${grant.kind}:${grant.path}`));
|
|
80
|
+
const added = grants.filter(grant => !held.has(`${grant.kind}:${grant.path}`));
|
|
81
|
+
const next = { ...binding, grants: [...binding.grants, ...added] };
|
|
82
|
+
this.set(next);
|
|
83
|
+
return next;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Remove a bound child's grants on the given paths, of either kind.
|
|
87
|
+
* @param child - child session id.
|
|
88
|
+
* @param paths - canonical paths to take back.
|
|
89
|
+
* @returns the removed grants.
|
|
90
|
+
* @throws when the child is not bound.
|
|
91
|
+
*/
|
|
92
|
+
removeGrants(child, paths) {
|
|
93
|
+
const binding = this.get(child);
|
|
94
|
+
if (binding === undefined)
|
|
95
|
+
throw new Error(`child ${child} has no binding`);
|
|
96
|
+
const targets = new Set(paths);
|
|
97
|
+
const removed = binding.grants.filter(grant => targets.has(grant.path));
|
|
98
|
+
if (removed.length > 0)
|
|
99
|
+
this.set({ ...binding, grants: binding.grants.filter(grant => !targets.has(grant.path)) });
|
|
100
|
+
return removed;
|
|
101
|
+
}
|
|
102
|
+
fileOf(child) {
|
|
103
|
+
return join(this.directory, `${encodeURIComponent(child)}.json`);
|
|
104
|
+
}
|
|
105
|
+
load(child) {
|
|
106
|
+
let text;
|
|
107
|
+
try {
|
|
108
|
+
text = readFileSync(this.fileOf(child), 'utf8');
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const value = JSON.parse(text);
|
|
115
|
+
if (value.version !== 1 || value.child !== child || typeof value.parent !== 'string')
|
|
116
|
+
return undefined;
|
|
117
|
+
if (value.workspace !== null && typeof value.workspace !== 'string')
|
|
118
|
+
return undefined;
|
|
119
|
+
const grants = Array.isArray(value.grants)
|
|
120
|
+
? value.grants.filter((grant) => typeof grant?.path === 'string' && (grant.kind === 'dir' || grant.kind === 'file'))
|
|
121
|
+
: [];
|
|
122
|
+
return { version: 1, child, parent: value.parent, workspace: value.workspace, grants, ...value.inherited === true ? { inherited: true } : {} };
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model-facing write-access tools: a bound child's `request_write_access`,
|
|
3
|
+
* and its parent's `answer_write_request`, `grant_write_access`, and
|
|
4
|
+
* `revoke_write_access`.
|
|
5
|
+
*
|
|
6
|
+
* A request reaches the parent through the host's adjacent-agent channel
|
|
7
|
+
* (`ctx.subagents.sendMessage`): a running parent sees it at its next step, an
|
|
8
|
+
* idle parent starts a turn. The child's call then waits for the answer, so a
|
|
9
|
+
* granted child continues in the same turn. A parent can also grant or revoke
|
|
10
|
+
* without a request; the child reads its current access in its runtime context
|
|
11
|
+
* at every step, and the fence applies it to the next capability call.
|
|
12
|
+
* @module complete-subagent/host/tools
|
|
13
|
+
*/
|
|
14
|
+
import type { ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
15
|
+
import type { Fence } from './policy.ts';
|
|
16
|
+
import type { RequestBroker, WriteRequest } from './requests.ts';
|
|
17
|
+
import type { BindingStore } from './store.ts';
|
|
18
|
+
import { ANSWER_TOOL, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL } from './types.ts';
|
|
19
|
+
import type { SandboxPolicy, SessionLike, SubagentsLike, WorkingDirectoryLike } from './types.ts';
|
|
20
|
+
export { ANSWER_TOOL, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL };
|
|
21
|
+
/** What the tools read. */
|
|
22
|
+
export interface ToolDeps {
|
|
23
|
+
readonly store: BindingStore;
|
|
24
|
+
readonly fence: Fence;
|
|
25
|
+
readonly broker: RequestBroker;
|
|
26
|
+
readonly subagents: SubagentsLike;
|
|
27
|
+
readonly workingDirectory: WorkingDirectoryLike;
|
|
28
|
+
/** The session's enforced (already narrowed) sandbox policy. */
|
|
29
|
+
readonly resolvePolicy: (session: SessionLike) => SandboxPolicy;
|
|
30
|
+
/** How long a parent has to answer. */
|
|
31
|
+
readonly timeoutMs: () => number;
|
|
32
|
+
}
|
|
33
|
+
/** The message the parent receives. */
|
|
34
|
+
export declare function requestMessage(request: WriteRequest, timeoutMs: number): string;
|
|
35
|
+
/**
|
|
36
|
+
* The child-side tool.
|
|
37
|
+
* @param deps - shared state and services.
|
|
38
|
+
* @returns a registry-ready definition.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createRequestTool(deps: ToolDeps): ToolDefinition;
|
|
41
|
+
/**
|
|
42
|
+
* The parent-side answer.
|
|
43
|
+
* @param deps - shared state and services.
|
|
44
|
+
* @returns a registry-ready definition.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createAnswerTool(deps: ToolDeps): ToolDefinition;
|
|
47
|
+
/**
|
|
48
|
+
* The parent-side grant without a request.
|
|
49
|
+
* @param deps - shared state and services.
|
|
50
|
+
* @returns a registry-ready definition.
|
|
51
|
+
*/
|
|
52
|
+
export declare function createGrantTool(deps: ToolDeps): ToolDefinition;
|
|
53
|
+
/**
|
|
54
|
+
* The parent-side revocation.
|
|
55
|
+
* @param deps - shared state and services.
|
|
56
|
+
* @returns a registry-ready definition.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createRevokeTool(deps: ToolDeps): ToolDefinition;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model-facing write-access tools: a bound child's `request_write_access`,
|
|
3
|
+
* and its parent's `answer_write_request`, `grant_write_access`, and
|
|
4
|
+
* `revoke_write_access`.
|
|
5
|
+
*
|
|
6
|
+
* A request reaches the parent through the host's adjacent-agent channel
|
|
7
|
+
* (`ctx.subagents.sendMessage`): a running parent sees it at its next step, an
|
|
8
|
+
* idle parent starts a turn. The child's call then waits for the answer, so a
|
|
9
|
+
* granted child continues in the same turn. A parent can also grant or revoke
|
|
10
|
+
* without a request; the child reads its current access in its runtime context
|
|
11
|
+
* at every step, and the fence applies it to the next capability call.
|
|
12
|
+
* @module complete-subagent/host/tools
|
|
13
|
+
*/
|
|
14
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
15
|
+
import { grantLine, issueGrants, requestedPaths } from "./grants.js";
|
|
16
|
+
import { ANSWER_TOOL, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL } from "./types.js";
|
|
17
|
+
export { ANSWER_TOOL, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL };
|
|
18
|
+
const GRANTED_SCHEMA = {
|
|
19
|
+
type: 'array',
|
|
20
|
+
required: true,
|
|
21
|
+
items: {
|
|
22
|
+
type: 'object',
|
|
23
|
+
additionalProperties: false,
|
|
24
|
+
properties: {
|
|
25
|
+
path: { type: 'string', required: true },
|
|
26
|
+
kind: { type: 'string', required: true, enum: ['dir', 'file'] },
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
const PATHS_DESCRIPTION = 'Relative to your current directory or absolute. End a path with "/" for a whole directory '
|
|
31
|
+
+ '(every tool, including shell commands); a single file is writable through the write and edit tools only.';
|
|
32
|
+
/** The message the parent receives. */
|
|
33
|
+
export function requestMessage(request, timeoutMs) {
|
|
34
|
+
const where = request.workspace === null ? 'it has no sub-workspace' : `its sub-workspace is ${request.workspace}`;
|
|
35
|
+
return [
|
|
36
|
+
`[write-access request ${request.id}] Subagent ${request.child} asks to modify paths outside its sub-workspace (${where}):`,
|
|
37
|
+
...request.paths.map(item => item.directory ? `- ${item.path}/ (directory)` : `- ${item.path}`),
|
|
38
|
+
`Reason: ${request.reason}`,
|
|
39
|
+
`It is paused until you answer with \`${ANSWER_TOOL}\` (request_id "${request.id}", decision "allow" or "deny"; `
|
|
40
|
+
+ `optional \`paths\` to grant a narrower set, optional \`message\` for the subagent). `
|
|
41
|
+
+ `Unanswered, it is denied after ${Math.round(timeoutMs / 60_000)} minutes.`,
|
|
42
|
+
].join('\n');
|
|
43
|
+
}
|
|
44
|
+
/** Render an answer for the child. */
|
|
45
|
+
function renderAnswer(answer) {
|
|
46
|
+
const lines = answer.decision === 'allow'
|
|
47
|
+
? ['Allowed. You may now modify:', ...answer.granted.map(grantLine)]
|
|
48
|
+
: ['Denied. Do not modify these paths; continue within your sub-workspace or report back to your parent.'];
|
|
49
|
+
if (answer.message !== undefined && answer.message !== '')
|
|
50
|
+
lines.push(`Parent's note: ${answer.message}`);
|
|
51
|
+
return lines.join('\n');
|
|
52
|
+
}
|
|
53
|
+
/** The calling agent, which every tool here needs. */
|
|
54
|
+
function callerOf(exec, tool) {
|
|
55
|
+
const agent = exec.agent;
|
|
56
|
+
if (agent === undefined)
|
|
57
|
+
throw new Error(`${tool} requires a calling agent`);
|
|
58
|
+
return agent;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The caller's own bound child.
|
|
62
|
+
* @returns the child session id.
|
|
63
|
+
* @throws when the id is not a child the caller started through the delegation tools.
|
|
64
|
+
*/
|
|
65
|
+
function ownChild(deps, parent, agentId) {
|
|
66
|
+
const id = agentId.trim();
|
|
67
|
+
const binding = deps.store.get(id);
|
|
68
|
+
if (binding?.parent !== parent.id || binding.inherited === true) {
|
|
69
|
+
throw new Error(`"${id}" is not a subagent you started with \`delegate\` or \`delegate_fork\`.`);
|
|
70
|
+
}
|
|
71
|
+
return id;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The child-side tool.
|
|
75
|
+
* @param deps - shared state and services.
|
|
76
|
+
* @returns a registry-ready definition.
|
|
77
|
+
*/
|
|
78
|
+
export function createRequestTool(deps) {
|
|
79
|
+
return defineTool({
|
|
80
|
+
name: REQUEST_TOOL,
|
|
81
|
+
description: 'Ask your parent agent for permission to modify paths outside your sub-workspace. '
|
|
82
|
+
+ 'You are paused until it answers; on "allow" the listed paths become writable for the rest of your work. '
|
|
83
|
+
+ 'End a path with "/" to ask for a whole directory (every tool, including shell commands); '
|
|
84
|
+
+ 'a single file is granted to the write and edit tools only.',
|
|
85
|
+
parameters: {
|
|
86
|
+
paths: {
|
|
87
|
+
type: 'array',
|
|
88
|
+
required: true,
|
|
89
|
+
items: { type: 'string' },
|
|
90
|
+
description: 'Paths to modify, relative to your current directory or absolute. Ask only for what the task needs.',
|
|
91
|
+
},
|
|
92
|
+
reason: {
|
|
93
|
+
type: 'string',
|
|
94
|
+
required: true,
|
|
95
|
+
description: 'One or two sentences: what you will change there and why.',
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
output: {
|
|
99
|
+
schema: {
|
|
100
|
+
type: 'object',
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
properties: {
|
|
103
|
+
decision: { type: 'string', required: true, enum: ['allow', 'deny'] },
|
|
104
|
+
granted: GRANTED_SCHEMA,
|
|
105
|
+
message: { type: 'string' },
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
render: (_args, value) => [{ type: 'text', text: renderAnswer(value) }],
|
|
109
|
+
},
|
|
110
|
+
isConcurrencySafe: () => true,
|
|
111
|
+
async execute(args, exec) {
|
|
112
|
+
const child = callerOf(exec, REQUEST_TOOL);
|
|
113
|
+
const binding = deps.store.get(child.id);
|
|
114
|
+
if (binding === undefined || binding.inherited === true) {
|
|
115
|
+
throw new Error(`${REQUEST_TOOL} is only for subagents started with \`delegate\` or \`delegate_fork\``);
|
|
116
|
+
}
|
|
117
|
+
if (args.paths.length === 0)
|
|
118
|
+
throw new Error('List at least one path');
|
|
119
|
+
const base = await deps.workingDirectory.ensure(child, exec.signal);
|
|
120
|
+
const request = deps.broker.open({
|
|
121
|
+
child: child.id,
|
|
122
|
+
parent: binding.parent,
|
|
123
|
+
workspace: binding.workspace,
|
|
124
|
+
paths: requestedPaths(base, args.paths),
|
|
125
|
+
reason: args.reason.trim(),
|
|
126
|
+
});
|
|
127
|
+
try {
|
|
128
|
+
await deps.subagents.sendMessage(child, binding.parent, [{ type: 'text', text: requestMessage(request, deps.timeoutMs()) }], { signal: exec.signal });
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
deps.broker.close(request.id);
|
|
132
|
+
if (exec.signal.aborted)
|
|
133
|
+
throw error;
|
|
134
|
+
return { decision: 'deny', granted: [], message: `Your parent agent could not be reached (${error instanceof Error ? error.message : String(error)}); nothing was granted.` };
|
|
135
|
+
}
|
|
136
|
+
return await deps.broker.wait(request.id, deps.timeoutMs(), exec.signal);
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The parent-side answer.
|
|
142
|
+
* @param deps - shared state and services.
|
|
143
|
+
* @returns a registry-ready definition.
|
|
144
|
+
*/
|
|
145
|
+
export function createAnswerTool(deps) {
|
|
146
|
+
return defineTool({
|
|
147
|
+
name: ANSWER_TOOL,
|
|
148
|
+
description: 'Answer a write-access request from one of your subagents (a message tagged "[write-access request wr-…]"). '
|
|
149
|
+
+ 'The subagent is paused until you answer. You can grant only paths you can modify yourself.',
|
|
150
|
+
parameters: {
|
|
151
|
+
request_id: { type: 'string', required: true, description: 'The id from the request, like "wr-1a2b3c".' },
|
|
152
|
+
decision: { type: 'string', required: true, enum: ['allow', 'deny'] },
|
|
153
|
+
paths: {
|
|
154
|
+
type: 'array',
|
|
155
|
+
items: { type: 'string' },
|
|
156
|
+
description: `Allow only: the paths to grant instead of the requested ones. ${PATHS_DESCRIPTION} Omit to grant exactly what was requested.`,
|
|
157
|
+
},
|
|
158
|
+
message: { type: 'string', description: 'Optional note for the subagent, such as why you denied or narrowed the request.' },
|
|
159
|
+
},
|
|
160
|
+
output: {
|
|
161
|
+
schema: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
additionalProperties: false,
|
|
164
|
+
properties: {
|
|
165
|
+
request_id: { type: 'string', required: true },
|
|
166
|
+
decision: { type: 'string', required: true, enum: ['allow', 'deny'] },
|
|
167
|
+
granted: GRANTED_SCHEMA,
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
render: (_args, value) => [{
|
|
171
|
+
type: 'text',
|
|
172
|
+
text: value.decision === 'allow'
|
|
173
|
+
? [`Allowed ${value.request_id}. The subagent may now modify:`, ...value.granted.map(grant => grantLine(grant))].join('\n')
|
|
174
|
+
: `Denied ${value.request_id}.`,
|
|
175
|
+
}],
|
|
176
|
+
},
|
|
177
|
+
isConcurrencySafe: () => true,
|
|
178
|
+
async execute(args, exec) {
|
|
179
|
+
const parent = callerOf(exec, ANSWER_TOOL);
|
|
180
|
+
const id = args.request_id.trim();
|
|
181
|
+
const open = () => {
|
|
182
|
+
const request = deps.broker.get(id);
|
|
183
|
+
if (request === undefined)
|
|
184
|
+
throw new Error(`No open write-access request "${id}": it was already answered, timed out, or was cancelled.`);
|
|
185
|
+
if (request.parent !== parent.id)
|
|
186
|
+
throw new Error(`Request "${id}" was sent to another agent.`);
|
|
187
|
+
return request;
|
|
188
|
+
};
|
|
189
|
+
const request = open();
|
|
190
|
+
const message = args.message?.trim();
|
|
191
|
+
const note = message === undefined || message === '' ? {} : { message };
|
|
192
|
+
if (args.decision === 'deny') {
|
|
193
|
+
deps.broker.settle(id, { decision: 'deny', granted: [], ...note });
|
|
194
|
+
return { request_id: id, decision: 'deny', granted: [] };
|
|
195
|
+
}
|
|
196
|
+
const targets = args.paths === undefined
|
|
197
|
+
? request.paths
|
|
198
|
+
: requestedPaths(await deps.workingDirectory.ensure(parent, exec.signal), args.paths);
|
|
199
|
+
if (targets.length === 0)
|
|
200
|
+
throw new Error('Grant at least one path, or deny the request');
|
|
201
|
+
open();
|
|
202
|
+
const grants = issueGrants({ fence: deps.fence, store: deps.store, grantor: parent, policy: deps.resolvePolicy(parent.session), targets, requestId: id });
|
|
203
|
+
deps.store.addGrants(request.child, grants);
|
|
204
|
+
const granted = grants.map(grant => ({ path: grant.path, kind: grant.kind }));
|
|
205
|
+
deps.broker.settle(id, { decision: 'allow', granted, ...note });
|
|
206
|
+
return { request_id: id, decision: 'allow', granted };
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The parent-side grant without a request.
|
|
212
|
+
* @param deps - shared state and services.
|
|
213
|
+
* @returns a registry-ready definition.
|
|
214
|
+
*/
|
|
215
|
+
export function createGrantTool(deps) {
|
|
216
|
+
return defineTool({
|
|
217
|
+
name: GRANT_TOOL,
|
|
218
|
+
description: 'Let one of your subagents modify more paths without waiting for it to ask, for example when you give it follow-up work. '
|
|
219
|
+
+ 'A running subagent sees the new access at its next step. You can grant only paths you can modify yourself.',
|
|
220
|
+
parameters: {
|
|
221
|
+
agent_id: { type: 'string', required: true, description: 'The subagent id returned when you started it.' },
|
|
222
|
+
paths: { type: 'array', required: true, items: { type: 'string' }, description: `Paths to grant. ${PATHS_DESCRIPTION}` },
|
|
223
|
+
},
|
|
224
|
+
output: {
|
|
225
|
+
schema: {
|
|
226
|
+
type: 'object',
|
|
227
|
+
additionalProperties: false,
|
|
228
|
+
properties: {
|
|
229
|
+
agent_id: { type: 'string', required: true },
|
|
230
|
+
granted: GRANTED_SCHEMA,
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
render: (_args, value) => [{
|
|
234
|
+
type: 'text',
|
|
235
|
+
text: [`Granted to ${value.agent_id}:`, ...value.granted.map(grant => grantLine(grant))].join('\n'),
|
|
236
|
+
}],
|
|
237
|
+
},
|
|
238
|
+
isConcurrencySafe: () => true,
|
|
239
|
+
async execute(args, exec) {
|
|
240
|
+
const parent = callerOf(exec, GRANT_TOOL);
|
|
241
|
+
const child = ownChild(deps, parent, args.agent_id);
|
|
242
|
+
if (args.paths.length === 0)
|
|
243
|
+
throw new Error('List at least one path');
|
|
244
|
+
const targets = requestedPaths(await deps.workingDirectory.ensure(parent, exec.signal), args.paths);
|
|
245
|
+
const grants = issueGrants({ fence: deps.fence, store: deps.store, grantor: parent, policy: deps.resolvePolicy(parent.session), targets, requestId: 'grant' });
|
|
246
|
+
deps.store.addGrants(child, grants);
|
|
247
|
+
return { agent_id: child, granted: grants.map(grant => ({ path: grant.path, kind: grant.kind })) };
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The parent-side revocation.
|
|
253
|
+
* @param deps - shared state and services.
|
|
254
|
+
* @returns a registry-ready definition.
|
|
255
|
+
*/
|
|
256
|
+
export function createRevokeTool(deps) {
|
|
257
|
+
return defineTool({
|
|
258
|
+
name: REVOKE_TOOL,
|
|
259
|
+
description: 'Take back paths you granted to one of your subagents. It takes effect at once, also for a running subagent. '
|
|
260
|
+
+ 'The sub-workspace itself cannot be revoked.',
|
|
261
|
+
parameters: {
|
|
262
|
+
agent_id: { type: 'string', required: true, description: 'The subagent id returned when you started it.' },
|
|
263
|
+
paths: { type: 'array', required: true, items: { type: 'string' }, description: 'Granted paths to take back, relative to your current directory or absolute.' },
|
|
264
|
+
},
|
|
265
|
+
output: {
|
|
266
|
+
schema: {
|
|
267
|
+
type: 'object',
|
|
268
|
+
additionalProperties: false,
|
|
269
|
+
properties: {
|
|
270
|
+
agent_id: { type: 'string', required: true },
|
|
271
|
+
revoked: GRANTED_SCHEMA,
|
|
272
|
+
not_granted: { type: 'array', required: true, items: { type: 'string' } },
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
render: (_args, value) => {
|
|
276
|
+
const lines = value.revoked.length === 0
|
|
277
|
+
? [`Nothing revoked from ${value.agent_id}.`]
|
|
278
|
+
: [`Revoked from ${value.agent_id}:`, ...value.revoked.map(grant => grantLine(grant))];
|
|
279
|
+
if (value.not_granted.length > 0)
|
|
280
|
+
lines.push(`Not granted, so nothing to revoke: ${value.not_granted.join(', ')}`);
|
|
281
|
+
return [{ type: 'text', text: lines.join('\n') }];
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
isConcurrencySafe: () => true,
|
|
285
|
+
async execute(args, exec) {
|
|
286
|
+
const parent = callerOf(exec, REVOKE_TOOL);
|
|
287
|
+
const child = ownChild(deps, parent, args.agent_id);
|
|
288
|
+
const paths = requestedPaths(await deps.workingDirectory.ensure(parent, exec.signal), args.paths).map(target => target.path);
|
|
289
|
+
const removed = deps.store.removeGrants(child, paths);
|
|
290
|
+
const gone = new Set(removed.map(grant => grant.path));
|
|
291
|
+
return {
|
|
292
|
+
agent_id: child,
|
|
293
|
+
revoked: removed.map(grant => ({ path: grant.path, kind: grant.kind })),
|
|
294
|
+
not_granted: paths.filter(path => !gone.has(path)),
|
|
295
|
+
};
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
}
|