fix(sisyphus_task): use promptAsync for sync mode to preserve main session

- session.prompt() changes the active session, causing UI model switch
- Switch to promptAsync + polling to avoid main session state change
- Matches background-agent pattern for consistency
This commit is contained in:
YeonGyu-Kim 2026-01-08 15:19:10 +09:00
parent 869efbe9ad
commit 2d3894a860

View File

@ -384,31 +384,52 @@ System notifies on completion. Use \`background_output\` with task_id="${task.id
metadata: { sessionId: sessionID, category: args.category, sync: true }, metadata: { sessionId: sessionID, category: args.category, sync: true },
}) })
try { // Use promptAsync to avoid changing main session's active state
await client.session.prompt({ let promptError: Error | undefined
path: { id: sessionID }, await client.session.promptAsync({
body: { path: { id: sessionID },
agent: agentToUse, body: {
model: categoryModel, agent: agentToUse,
system: systemContent, model: categoryModel,
tools: { system: systemContent,
task: false, tools: {
sisyphus_task: false, task: false,
}, sisyphus_task: false,
parts: [{ type: "text", text: args.prompt }],
}, },
}) parts: [{ type: "text", text: args.prompt }],
} catch (promptError) { },
}).catch((error) => {
promptError = error instanceof Error ? error : new Error(String(error))
})
if (promptError) {
if (toastManager && taskId !== undefined) { if (toastManager && taskId !== undefined) {
toastManager.removeTask(taskId) toastManager.removeTask(taskId)
} }
const errorMessage = promptError instanceof Error ? promptError.message : String(promptError) const errorMessage = promptError.message
if (errorMessage.includes("agent.name") || errorMessage.includes("undefined")) { if (errorMessage.includes("agent.name") || errorMessage.includes("undefined")) {
return `❌ Agent "${agentToUse}" not found. Make sure the agent is registered in your opencode.json or provided by a plugin.\n\nSession ID: ${sessionID}` return `❌ Agent "${agentToUse}" not found. Make sure the agent is registered in your opencode.json or provided by a plugin.\n\nSession ID: ${sessionID}`
} }
return `❌ Failed to send prompt: ${errorMessage}\n\nSession ID: ${sessionID}` return `❌ Failed to send prompt: ${errorMessage}\n\nSession ID: ${sessionID}`
} }
// Poll for session completion
const POLL_INTERVAL_MS = 500
const MAX_POLL_TIME_MS = 10 * 60 * 1000
const pollStart = Date.now()
while (Date.now() - pollStart < MAX_POLL_TIME_MS) {
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS))
const statusResult = await client.session.status()
const allStatuses = (statusResult.data ?? {}) as Record<string, { type: string }>
const sessionStatus = allStatuses[sessionID]
if (sessionStatus?.type === "idle") {
break
}
}
const messagesResult = await client.session.messages({ const messagesResult = await client.session.messages({
path: { id: sessionID }, path: { id: sessionID },
}) })