Skip to content

Commit ed32b28

Browse files
committed
fix(session): skip synthetic delegation nudge in subagent sessions
When a user message contains an @agent part, createUserMessage injects a synthetic text part that tells the model to call the task tool with that subagent. In a child/subagent session the orchestrator has already written an explicit task prompt, so injecting the nudge again causes recursive nesting bias: the subagent re-reads the @agent mention in its own context and tries to spawn another layer of delegation. Guard the injection behind a parentID check so only root sessions receive the synthetic delegation instruction. Fixes #17202
1 parent dce7ece commit ed32b28

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

packages/opencode/src/session/prompt.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,15 +1267,24 @@ export namespace SessionPrompt {
12671267
}
12681268

12691269
if (part.type === "agent") {
1270+
const agentPart = {
1271+
...part,
1272+
messageID: info.id,
1273+
sessionID: input.sessionID,
1274+
}
1275+
// Only inject the delegation nudge in root sessions. Subagent sessions
1276+
// already have an explicit task prompt; injecting it there causes
1277+
// recursive nesting bias when the agent sees @agent mentions in its
1278+
// own context.
1279+
const currentSession = await Session.get(input.sessionID)
1280+
if (currentSession.parentID) {
1281+
return [agentPart]
1282+
}
12701283
// Check if this agent would be denied by task permission
12711284
const perm = PermissionNext.evaluate("task", part.name, agent.permission)
12721285
const hint = perm.action === "deny" ? " . Invoked by user; guaranteed to exist." : ""
12731286
return [
1274-
{
1275-
...part,
1276-
messageID: info.id,
1277-
sessionID: input.sessionID,
1278-
},
1287+
agentPart,
12791288
{
12801289
messageID: info.id,
12811290
sessionID: input.sessionID,

0 commit comments

Comments
 (0)