fix(background-agent): omit model field to use session's lastModel

Previously, background task completion notifications passed parentModel when defined, causing OpenCode to use default Sonnet model when parentModel was undefined. Now model field is always omitted, letting OpenCode use the session's existing lastModel (like todo-continuation hook).

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
justsisyphus 2026-01-14 18:02:20 +09:00
parent 45d660176e
commit 6ded689d08
2 changed files with 14 additions and 16 deletions

View File

@ -675,9 +675,9 @@ describe("LaunchInput.skillContent", () => {
}) })
}) })
describe("BackgroundManager.notifyParentSession - model/agent context preservation", () => { describe("BackgroundManager.notifyParentSession - agent context preservation", () => {
test("should include model field in session.prompt when parentModel is defined", async () => { test("should never pass model field - let OpenCode use session's lastModel", async () => {
// #given // #given - task with parentModel defined
const task: BackgroundTask = { const task: BackgroundTask = {
id: "task-with-model", id: "task-with-model",
sessionID: "session-child", sessionID: "session-child",
@ -696,8 +696,8 @@ describe("BackgroundManager.notifyParentSession - model/agent context preservati
// #when // #when
const promptBody = buildNotificationPromptBody(task) const promptBody = buildNotificationPromptBody(task)
// #then - model MUST be included when parentModel is defined // #then - model MUST NOT be passed (OpenCode uses session's lastModel)
expect(promptBody.model).toEqual({ providerID: "anthropic", modelID: "claude-opus-4-5" }) expect("model" in promptBody).toBe(false)
expect(promptBody.agent).toBe("Sisyphus") expect(promptBody.agent).toBe("Sisyphus")
}) })
@ -721,9 +721,9 @@ describe("BackgroundManager.notifyParentSession - model/agent context preservati
// #when // #when
const promptBody = buildNotificationPromptBody(task) const promptBody = buildNotificationPromptBody(task)
// #then // #then - no agent, no model (let OpenCode handle)
expect("agent" in promptBody).toBe(false) expect("agent" in promptBody).toBe(false)
expect(promptBody.model).toEqual({ providerID: "anthropic", modelID: "claude-opus" }) expect("model" in promptBody).toBe(false)
}) })
test("should include agent field when parentAgent is defined", async () => { test("should include agent field when parentAgent is defined", async () => {
@ -748,9 +748,10 @@ describe("BackgroundManager.notifyParentSession - model/agent context preservati
// #then // #then
expect(promptBody.agent).toBe("Sisyphus") expect(promptBody.agent).toBe("Sisyphus")
expect("model" in promptBody).toBe(false)
}) })
test("should not pass model field when parentModel is undefined", async () => { test("should not pass model field even when parentModel is undefined", async () => {
// #given // #given
const task: BackgroundTask = { const task: BackgroundTask = {
id: "task-no-model", id: "task-no-model",
@ -770,7 +771,7 @@ describe("BackgroundManager.notifyParentSession - model/agent context preservati
// #when // #when
const promptBody = buildNotificationPromptBody(task) const promptBody = buildNotificationPromptBody(task)
// #then // #then - model never passed regardless of parentModel
expect("model" in promptBody).toBe(false) expect("model" in promptBody).toBe(false)
expect(promptBody.agent).toBe("Sisyphus") expect(promptBody.agent).toBe("Sisyphus")
}) })
@ -785,9 +786,8 @@ function buildNotificationPromptBody(task: BackgroundTask): Record<string, unkno
body.agent = task.parentAgent body.agent = task.parentAgent
} }
if (task.parentModel?.providerID && task.parentModel?.modelID) { // Don't pass model - let OpenCode use session's existing lastModel
body.model = { providerID: task.parentModel.providerID, modelID: task.parentModel.modelID } // This prevents model switching when parentModel is undefined or different
}
return body return body
} }

View File

@ -639,16 +639,14 @@ Use \`background_output(task_id="${task.id}")\` to retrieve this result when rea
} }
// Inject notification via session.prompt with noReply // Inject notification via session.prompt with noReply
// Preserve parent session's model/agent context to prevent model switching // Don't pass model - let OpenCode use session's existing lastModel (like todo-continuation)
// This prevents model switching when parentModel is undefined
try { try {
await this.client.session.prompt({ await this.client.session.prompt({
path: { id: task.parentSessionID }, path: { id: task.parentSessionID },
body: { body: {
noReply: !allComplete, noReply: !allComplete,
...(task.parentAgent !== undefined ? { agent: task.parentAgent } : {}), ...(task.parentAgent !== undefined ? { agent: task.parentAgent } : {}),
...(task.parentModel?.providerID && task.parentModel?.modelID
? { model: { providerID: task.parentModel.providerID, modelID: task.parentModel.modelID } }
: {}),
parts: [{ type: "text", text: notification }], parts: [{ type: "text", text: notification }],
}, },
}) })