Skip to content

Commit c910264

Browse files
committed
fix(agents): declare visible-session constraints upfront in sessions_spawn
1 parent 40d31f3 commit c910264

11 files changed

Lines changed: 146 additions & 60 deletions

‎docs/tools/subagents.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Per-agent override: `agents.list[].subagents.delegationMode`.
221221
<ParamField path="mode" type='"run" | "session"' default="run">
222222
If `thread: true` and `mode` is omitted, default becomes `session`. `mode: "session"` requires `thread: true`.
223223
If thread binding is unavailable for the requester channel, use `mode: "run"` instead.
224+
With `visible: true`, omit `mode`; visible sessions are persistent and do not support `mode: "run"`.
224225
</ParamField>
225226
<ParamField path="cleanup" type='"delete" | "keep"' default="keep">
226227
`"delete"` archives the session immediately after announce (still keeps the transcript via rename).
@@ -251,7 +252,7 @@ their latest assistant turn back to the requester; external delivery stays with
251252
the parent/requester agent.
252253
</Warning>
253254

254-
With `visible: true`, `model`, `cwd`, and a same-agent `context: "fork"` are supported. A sandboxed target restricts `cwd` to that agent's workspace. Thread binding, `mode`, thinking overrides, light bootstrap context, and attachment staging are unavailable on this path because visible sessions are persistent dashboard sessions created through `sessions.create`. Visible spawning is also rejected when inherited tool restrictions cannot be carried into the dashboard session. See [Managed worktrees](/concepts/managed-worktrees) for checkout naming, setup, cleanup, and restore behavior.
255+
With `visible: true`, `model`, `cwd`, and a same-agent `context: "fork"` are supported. A sandboxed target restricts `cwd` to that agent's workspace. Thread binding, `mode`, thinking overrides, `lightContext`, `attachments`, and `attachAs` are unavailable on this path because visible sessions are persistent dashboard sessions created through `sessions.create`. Visible spawning is rejected when the requester was itself spawned with an inherited tool allowlist or denylist; that restriction is fixed at spawn time and has no config override. Session listing and addressing obey `tools.sessions.visibility`; the default `tree` scope covers the current session and its own spawn subtree. See [Managed worktrees](/concepts/managed-worktrees) for checkout naming, setup, cleanup, and restore behavior.
255256

256257
### Task names and targeting
257258

‎src/agents/tool-description-presets.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export function describeSessionsSpawnTool(options?: {
7272
options?.threadAvailable
7373
? '`mode="run"` one-shot; `mode="session"` persistent/thread-bound only on supporting requester channel.'
7474
: '`mode="run"` one-shot background.',
75+
'`visible=true`: persistent dashboard session; subagent only; omit `mode` (no `mode="run"`), `thread`, `thinking`, `lightContext`, `attachments`, `attachAs`; inherited tool allow/denylist blocks it at spawn with no config override.',
76+
"Session listing/addressing obeys `tools.sessions.visibility` (`tree` default: current + own spawn subtree).",
7577
"Inherits parent workspace. Native task arrives as first `[Subagent Task]`.",
7678
'Native transcript needed: `context="fork"`; else omit/isolated.',
7779
"Use fresh child for sidecar/parallel batch reads, multi-step search, data collection; avoid quick lookup/single read unless policy prefers.",
@@ -81,9 +83,9 @@ export function describeSessionsSpawnTool(options?: {
8183
return baseDescription.join(" ");
8284
}
8385
return [
84-
...baseDescription.slice(0, 3),
86+
...baseDescription.slice(0, 5),
8587
'`runtime="acp"` ids: codex, claude, gemini, opencode, or configured ACP.',
86-
...baseDescription.slice(3),
88+
...baseDescription.slice(5),
8789
].join(" ");
8890
}
8991

‎src/agents/tools/sessions-spawn-tool.test.ts‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,26 @@ describe("sessions_spawn tool", () => {
382382
it("advertises visible sessions with terse UI guidance", () => {
383383
const tool = createSessionsSpawnTool();
384384
const schema = tool.parameters as {
385-
properties?: { visible?: { description?: string }; worktree?: unknown };
385+
properties?: Record<
386+
string,
387+
{ anyOf?: unknown; description?: string; enum?: string[] } | undefined
388+
>;
386389
};
387390

388391
expect(schema.properties?.visible?.description).toBe(
389-
"visible: user sees session in UI. Use when user asked or talks via web/app.",
392+
"Persistent UI session; subagent only; omit mode/thread/thinking/lightContext/attachments/attachAs; unavailable with inherited tool allow/denylist.",
390393
);
394+
expect(tool.description).toContain("`visible=true`: persistent dashboard session");
395+
expect(tool.description).toContain('no `mode="run"`');
396+
expect(tool.description).toContain("inherited tool allow/denylist");
397+
expect(tool.description).toContain("`tools.sessions.visibility`");
398+
expect(schema.properties?.runtime?.description).toContain("visible=true");
399+
expect(schema.properties?.mode?.description).toContain("Omit with visible=true");
400+
expect(schema.properties?.lightContext?.description).toContain("unavailable with visible=true");
401+
expect(schema.properties?.attachments?.description).toContain("unavailable with visible=true");
402+
expect(schema.properties?.attachAs?.description).toContain("unavailable with visible=true");
403+
expect(schema.properties?.mode?.enum).toEqual(["run"]);
404+
expect(schema.properties?.mode?.anyOf).toBeUndefined();
391405
expect(schema.properties?.worktree).toBeDefined();
392406
});
393407

@@ -477,7 +491,7 @@ describe("sessions_spawn tool", () => {
477491

478492
await expect(
479493
tool.execute("hidden-worktree", { task: "inspect", worktree: true }),
480-
).rejects.toThrow("worktree options require visible=true");
494+
).rejects.toThrow("Parameters require visible=true: worktree");
481495
expect(hoisted.spawnSubagentDirectMock).not.toHaveBeenCalled();
482496
});
483497

@@ -616,32 +630,32 @@ describe("sessions_spawn tool", () => {
616630
[
617631
"thinking",
618632
{ thinking: "high" },
619-
"thinking unavailable with visible=true: thinking overrides are not wired to the sessions.create path",
633+
"Parameters unavailable with visible=true: thinking: thinking overrides are not wired to the sessions.create path",
620634
],
621635
[
622636
"thread",
623637
{ thread: true },
624-
"thread unavailable with visible=true: visible sessions route to the dashboard, not a channel thread",
638+
"Parameters unavailable with visible=true: thread: visible sessions route to the dashboard, not a channel thread",
625639
],
626640
[
627641
"mode",
628642
{ mode: "session" },
629-
"mode unavailable with visible=true: visible sessions are persistent dashboard sessions",
643+
"Parameters unavailable with visible=true: mode: visible sessions are persistent dashboard sessions",
630644
],
631645
[
632646
"lightContext",
633647
{ lightContext: true },
634-
"lightContext unavailable with visible=true: bootstrap staging is not wired to the sessions.create path",
648+
"Parameters unavailable with visible=true: lightContext: bootstrap staging is not wired to the sessions.create path",
635649
],
636650
[
637651
"attachments",
638652
{ attachments: [{ name: "note.txt", content: "hello" }] },
639-
"attachments unavailable with visible=true: attachment staging is not wired to the sessions.create path",
653+
"Parameters unavailable with visible=true: attachments: attachment staging is not wired to the sessions.create path",
640654
],
641655
[
642656
"attachAs",
643657
{ attachAs: { mountPath: "inputs" } },
644-
"attachAs unavailable with visible=true: attachment staging is not wired to the sessions.create path",
658+
"Parameters unavailable with visible=true: attachAs: attachment staging is not wired to the sessions.create path",
645659
],
646660
] as const)("rejects visible %s overrides with a reason", async (_name, override, message) => {
647661
const tool = createSessionsSpawnTool({ agentSessionKey: "agent:main:main" });
@@ -651,6 +665,26 @@ describe("sessions_spawn tool", () => {
651665
).rejects.toThrow(message);
652666
});
653667

668+
it("reports every unsupported visible parameter in one error", async () => {
669+
const tool = createSessionsSpawnTool({ agentSessionKey: "agent:main:main" });
670+
671+
await expect(
672+
tool.execute("visible-unsupported-many", {
673+
task: "inspect",
674+
runtime: "acp",
675+
thinking: "high",
676+
thread: true,
677+
mode: "run",
678+
lightContext: true,
679+
attachments: [{ name: "note.txt", content: "hello" }],
680+
attachAs: { mountPath: "inputs" },
681+
visible: true,
682+
}),
683+
).rejects.toThrow(
684+
'Parameters unavailable with visible=true: runtime: supports runtime="subagent" only; thinking: thinking overrides are not wired to the sessions.create path; thread: visible sessions route to the dashboard, not a channel thread; mode: visible sessions are persistent dashboard sessions; lightContext: bootstrap staging is not wired to the sessions.create path; attachments: attachment staging is not wired to the sessions.create path; attachAs: attachment staging is not wired to the sessions.create path',
685+
);
686+
});
687+
654688
it("denies visible sessions when tool restrictions cannot carry forward", async () => {
655689
const callGateway = vi.fn();
656690
const tool = createSessionsSpawnTool({
@@ -667,7 +701,8 @@ describe("sessions_spawn tool", () => {
667701

668702
expect(result.details).toMatchObject({
669703
status: "forbidden",
670-
error: "Visible sessions unavailable with inherited tool restrictions.",
704+
error:
705+
"Visible sessions unavailable with inherited tool restrictions. This session was spawned with a tool allow/denylist; visible sessions require an unrestricted session.",
671706
});
672707
expect(callGateway).not.toHaveBeenCalled();
673708
});

‎src/agents/tools/sessions-spawn-tool.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,40 @@ function createSessionsSpawnToolSchema(params: {
143143
label: Type.Optional(Type.String()),
144144
runtime: optionalStringEnum(
145145
params.acpAvailable ? SESSIONS_SPAWN_RUNTIMES : (["subagent"] as const),
146+
{ description: 'Runtime; visible=true requires "subagent".' },
146147
),
147148
agentId: Type.Optional(Type.String()),
148149
model: Type.Optional(Type.String()),
149-
thinking: Type.Optional(Type.String()),
150+
thinking: Type.Optional(
151+
Type.String({ description: "Thinking override; unavailable with visible=true." }),
152+
),
150153
cwd: Type.Optional(Type.String()),
151154
...(params.threadAvailable
152155
? {
153156
thread: Type.Optional(
154157
Type.Boolean({
155-
description: 'Bind new chat thread when supported; true defaults mode="session".',
158+
description:
159+
'Bind new chat thread when supported; true defaults mode="session"; unavailable with visible=true.',
156160
}),
157161
),
158162
}
159163
: {}),
160-
mode: optionalStringEnum(spawnModes),
161-
cleanup: optionalStringEnum(["delete", "keep"] as const),
164+
mode: optionalStringEnum(spawnModes, {
165+
description: params.threadAvailable
166+
? '"run" one-shot; "session" persistent/thread-bound. Omit with visible=true.'
167+
: '"run" one-shot. Omit with visible=true; visible sessions are persistent.',
168+
}),
169+
cleanup: optionalStringEnum(["delete", "keep"] as const, {
170+
description: "Hidden session cleanup; visible=true always keeps the session.",
171+
}),
162172
sandbox: optionalStringEnum(SESSIONS_SPAWN_SANDBOX_MODES),
163173
context: optionalStringEnum(SUBAGENT_SPAWN_CONTEXT_MODES, {
164-
description: "Native: omit/isolated clean; fork only needing requester transcript.",
174+
description:
175+
"Native: omit/isolated clean; fork only needing requester transcript; visible fork requires same agent.",
165176
}),
166177
lightContext: Type.Optional(
167178
Type.Boolean({
168-
description: "Light bootstrap; subagent only.",
179+
description: "Light bootstrap; subagent only; unavailable with visible=true.",
169180
}),
170181
),
171182
...(params.swarmEnabled
@@ -187,15 +198,18 @@ function createSessionsSpawnToolSchema(params: {
187198
encoding: Type.Optional(optionalStringEnum(["utf8", "base64"] as const)),
188199
mimeType: Type.Optional(Type.String()),
189200
}),
190-
{ maxItems: 50 },
201+
{ maxItems: 50, description: "Inline snapshots; unavailable with visible=true." },
191202
),
192203
),
193204
attachAs: Type.Optional(
194-
Type.Object({
195-
// Where the spawned agent should look for attachments.
196-
// Kept as a hint; implementation materializes into the child workspace.
197-
mountPath: Type.Optional(Type.String()),
198-
}),
205+
Type.Object(
206+
{
207+
// Where the spawned agent should look for attachments.
208+
// Kept as a hint; implementation materializes into the child workspace.
209+
mountPath: Type.Optional(Type.String()),
210+
},
211+
{ description: "Attachment mount hint; unavailable with visible=true." },
212+
),
199213
),
200214
...(params.acpAvailable
201215
? {

‎src/agents/tools/sessions-spawn-visible.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import { reserveVisibleChildSlot } from "./sessions-spawn-visible-admission.js";
3030
export const VISIBLE_SESSIONS_SPAWN_SCHEMA = {
3131
visible: Type.Optional(
3232
Type.Boolean({
33-
description: "visible: user sees session in UI. Use when user asked or talks via web/app.",
33+
description:
34+
"Persistent UI session; subagent only; omit mode/thread/thinking/lightContext/attachments/attachAs; unavailable with inherited tool allow/denylist.",
3435
}),
3536
),
3637
worktree: Type.Optional(Type.Boolean({ description: "Visible session worktree" })),
@@ -94,18 +95,30 @@ export async function maybeSpawnVisibleSession(params: {
9495
const worktreeName = readStringParam(params.raw, "worktreeName");
9596
const worktreeBaseRef = readStringParam(params.raw, "worktreeBaseRef");
9697
if (params.raw.visible !== true) {
97-
if (worktree || worktreeName || worktreeBaseRef) {
98-
throw new ToolInputError("worktree options require visible=true");
98+
const visibleOnlyParams = [
99+
["worktree", worktree],
100+
["worktreeName", worktreeName],
101+
["worktreeBaseRef", worktreeBaseRef],
102+
] as const;
103+
const providedVisibleOnlyParams = visibleOnlyParams
104+
.filter(([, value]) => value !== undefined && value !== false)
105+
.map(([name]) => name);
106+
if (providedVisibleOnlyParams.length > 0) {
107+
throw new ToolInputError(
108+
`Parameters require visible=true: ${providedVisibleOnlyParams.join(", ")}`,
109+
);
99110
}
100111
return undefined;
101112
}
102-
if (params.runtime !== "subagent") {
103-
throw new ToolInputError('visible=true supports runtime="subagent" only');
104-
}
105113
const modelOverride = normalizeToolModelOverride(readStringParam(params.raw, "model"));
106114
const requestedCwd = readStringParam(params.raw, "cwd");
107115
const spawnedCwd = requestedCwd ? resolveUserPath(requestedCwd) : undefined;
108116
const unsupported = [
117+
[
118+
"runtime",
119+
params.runtime === "subagent" ? undefined : params.runtime,
120+
'supports runtime="subagent" only',
121+
],
109122
[
110123
"thinking",
111124
readStringParam(params.raw, "thinking"),
@@ -133,10 +146,12 @@ export async function maybeSpawnVisibleSession(params: {
133146
"attachment staging is not wired to the sessions.create path",
134147
],
135148
] as const;
136-
const unsupportedEntry = unsupported.find(([, value]) => value !== undefined);
137-
if (unsupportedEntry) {
149+
const unsupportedEntries = unsupported.filter(([, value]) => value !== undefined);
150+
if (unsupportedEntries.length > 0) {
138151
throw new ToolInputError(
139-
`${unsupportedEntry[0]} unavailable with visible=true: ${unsupportedEntry[2]}`,
152+
`Parameters unavailable with visible=true: ${unsupportedEntries
153+
.map(([name, , reason]) => `${name}: ${reason}`)
154+
.join("; ")}`,
140155
);
141156
}
142157

@@ -147,7 +162,8 @@ export async function maybeSpawnVisibleSession(params: {
147162
) {
148163
return {
149164
status: "forbidden",
150-
error: "Visible sessions unavailable with inherited tool restrictions.",
165+
error:
166+
"Visible sessions unavailable with inherited tool restrictions. This session was spawned with a tool allow/denylist; visible sessions require an unrestricted session.",
151167
};
152168
}
153169
const ownership = resolveSubagentSpawnOwnership({

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/codex-dynamic-tools.discord-group.json‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@
138138
"type": "function"
139139
},
140140
{
141-
"description": "Spawn clean child; default `runtime=\"subagent\"`. `mode=\"run\"` one-shot; `mode=\"session\"` persistent/thread-bound only on supporting requester channel. Inherits parent workspace. Native task arrives as first `[Subagent Task]`. Native transcript needed: `context=\"fork\"`; else omit/isolated. Use fresh child for sidecar/parallel batch reads, multi-step search, data collection; avoid quick lookup/single read unless policy prefers. After spawn, do non-overlap work. Run result returns; session output stays thread.",
141+
"description": "Spawn clean child; default `runtime=\"subagent\"`. `mode=\"run\"` one-shot; `mode=\"session\"` persistent/thread-bound only on supporting requester channel. `visible=true`: persistent dashboard session; subagent only; omit `mode` (no `mode=\"run\"`), `thread`, `thinking`, `lightContext`, `attachments`, `attachAs`; inherited tool allow/denylist blocks it at spawn with no config override. Session listing/addressing obeys `tools.sessions.visibility` (`tree` default: current + own spawn subtree). Inherits parent workspace. Native task arrives as first `[Subagent Task]`. Native transcript needed: `context=\"fork\"`; else omit/isolated. Use fresh child for sidecar/parallel batch reads, multi-step search, data collection; avoid quick lookup/single read unless policy prefers. After spawn, do non-overlap work. Run result returns; session output stays thread.",
142142
"inputSchema": {
143143
"properties": {
144144
"agentId": {
145145
"type": "string"
146146
},
147147
"attachAs": {
148+
"description": "Attachment mount hint; unavailable with visible=true.",
148149
"properties": {
149150
"mountPath": {
150151
"type": "string"
@@ -153,6 +154,7 @@
153154
"type": "object"
154155
},
155156
"attachments": {
157+
"description": "Inline snapshots; unavailable with visible=true.",
156158
"items": {
157159
"properties": {
158160
"content": {
@@ -176,11 +178,12 @@
176178
"type": "array"
177179
},
178180
"cleanup": {
181+
"description": "Hidden session cleanup; visible=true always keeps the session.",
179182
"enum": ["delete", "keep"],
180183
"type": "string"
181184
},
182185
"context": {
183-
"description": "Native: omit/isolated clean; fork only needing requester transcript.",
186+
"description": "Native: omit/isolated clean; fork only needing requester transcript; visible fork requires same agent.",
184187
"enum": ["isolated", "fork"],
185188
"type": "string"
186189
},
@@ -191,17 +194,19 @@
191194
"type": "string"
192195
},
193196
"lightContext": {
194-
"description": "Light bootstrap; subagent only.",
197+
"description": "Light bootstrap; subagent only; unavailable with visible=true.",
195198
"type": "boolean"
196199
},
197200
"mode": {
201+
"description": "\"run\" one-shot; \"session\" persistent/thread-bound. Omit with visible=true.",
198202
"enum": ["run", "session"],
199203
"type": "string"
200204
},
201205
"model": {
202206
"type": "string"
203207
},
204208
"runtime": {
209+
"description": "Runtime; visible=true requires \"subagent\".",
205210
"enum": ["subagent"],
206211
"type": "string"
207212
},
@@ -217,14 +222,15 @@
217222
"type": "string"
218223
},
219224
"thinking": {
225+
"description": "Thinking override; unavailable with visible=true.",
220226
"type": "string"
221227
},
222228
"thread": {
223-
"description": "Bind new chat thread when supported; true defaults mode=\"session\".",
229+
"description": "Bind new chat thread when supported; true defaults mode=\"session\"; unavailable with visible=true.",
224230
"type": "boolean"
225231
},
226232
"visible": {
227-
"description": "visible: user sees session in UI. Use when user asked or talks via web/app.",
233+
"description": "Persistent UI session; subagent only; omit mode/thread/thinking/lightContext/attachments/attachAs; unavailable with inherited tool allow/denylist.",
228234
"type": "boolean"
229235
},
230236
"worktree": {

0 commit comments

Comments
 (0)