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