fix(background-agent): use session.idle event for completion detection
- Remove broken session.updated handler (Session has no status field) - Add session.idle event handler for proper completion detection - Remove all file persistence (persist/restore methods) - Add comprehensive logging for debugging - Dual detection: event-based (session.idle) + polling (session.status API) 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -7,12 +7,6 @@ import { log } from "../../shared/logger"
|
|||||||
|
|
||||||
type OpencodeClient = PluginInput["client"]
|
type OpencodeClient = PluginInput["client"]
|
||||||
|
|
||||||
interface SessionInfo {
|
|
||||||
id?: string
|
|
||||||
parentID?: string
|
|
||||||
status?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface MessagePartInfo {
|
interface MessagePartInfo {
|
||||||
sessionID?: string
|
sessionID?: string
|
||||||
type?: string
|
type?: string
|
||||||
@@ -20,8 +14,8 @@ interface MessagePartInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface EventProperties {
|
interface EventProperties {
|
||||||
info?: SessionInfo
|
|
||||||
sessionID?: string
|
sessionID?: string
|
||||||
|
info?: { id?: string }
|
||||||
[key: string]: unknown
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +68,8 @@ export class BackgroundManager {
|
|||||||
this.tasks.set(task.id, task)
|
this.tasks.set(task.id, task)
|
||||||
this.startPolling()
|
this.startPolling()
|
||||||
|
|
||||||
|
log("[background-agent] Launching task:", { taskId: task.id, sessionID })
|
||||||
|
|
||||||
this.client.session.promptAsync({
|
this.client.session.promptAsync({
|
||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: {
|
body: {
|
||||||
@@ -141,24 +137,22 @@ export class BackgroundManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "session.updated") {
|
if (event.type === "session.idle") {
|
||||||
const info = props?.info as SessionInfo | undefined
|
const sessionID = props?.sessionID as string | undefined
|
||||||
if (!info || typeof info.id !== "string") return
|
if (!sessionID) return
|
||||||
const sessionID = info.id
|
|
||||||
const status = info?.status
|
|
||||||
|
|
||||||
const task = this.findBySession(sessionID)
|
const task = this.findBySession(sessionID)
|
||||||
if (!task) return
|
if (!task || task.status !== "running") return
|
||||||
|
|
||||||
if (status === "idle" && task.status === "running") {
|
task.status = "completed"
|
||||||
task.status = "completed"
|
task.completedAt = new Date()
|
||||||
task.completedAt = new Date()
|
this.markForNotification(task)
|
||||||
this.markForNotification(task)
|
this.notifyParentSession(task)
|
||||||
}
|
log("[background-agent] Task completed via session.idle event:", task.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "session.deleted") {
|
if (event.type === "session.deleted") {
|
||||||
const info = props?.info as SessionInfo | undefined
|
const info = props?.info
|
||||||
if (!info || typeof info.id !== "string") return
|
if (!info || typeof info.id !== "string") return
|
||||||
const sessionID = info.id
|
const sessionID = info.id
|
||||||
|
|
||||||
@@ -168,7 +162,7 @@ export class BackgroundManager {
|
|||||||
if (task.status === "running") {
|
if (task.status === "running") {
|
||||||
task.status = "cancelled"
|
task.status = "cancelled"
|
||||||
task.completedAt = new Date()
|
task.completedAt = new Date()
|
||||||
task.error = "Session deleted (cascade delete from parent)"
|
task.error = "Session deleted"
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tasks.delete(task.id)
|
this.tasks.delete(task.id)
|
||||||
@@ -275,9 +269,7 @@ Use \`background_result\` tool with taskId="${task.id}" to retrieve the full res
|
|||||||
const sessionStatus = allStatuses[task.sessionID]
|
const sessionStatus = allStatuses[task.sessionID]
|
||||||
|
|
||||||
if (!sessionStatus) {
|
if (!sessionStatus) {
|
||||||
task.status = "error"
|
log("[background-agent] Session not found in status:", task.sessionID)
|
||||||
task.error = "Session not found"
|
|
||||||
task.completedAt = new Date()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +278,7 @@ Use \`background_result\` tool with taskId="${task.id}" to retrieve the full res
|
|||||||
task.completedAt = new Date()
|
task.completedAt = new Date()
|
||||||
this.markForNotification(task)
|
this.markForNotification(task)
|
||||||
this.notifyParentSession(task)
|
this.notifyParentSession(task)
|
||||||
log("[background-agent] Task completed, notifying parent:", task.id)
|
log("[background-agent] Task completed via polling:", task.id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,8 +315,8 @@ Use \`background_result\` tool with taskId="${task.id}" to retrieve the full res
|
|||||||
task.progress.lastTool = lastTool
|
task.progress.lastTool = lastTool
|
||||||
task.progress.lastUpdate = new Date()
|
task.progress.lastUpdate = new Date()
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (error) {
|
||||||
void 0
|
log("[background-agent] Poll error for task:", { taskId: task.id, error })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user