From 5733291a0fcaf7fd81de270c736ea3925a8f3fe6 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Thu, 11 Dec 2025 17:15:02 +0900 Subject: [PATCH] fix(background-agent): notify parent session when task completes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add notifyParentSession() to send message to parent session via prompt() - Agent receives completion notification immediately, not waiting for next chat.message - Includes task ID, description, duration, tool calls in notification 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode) --- src/features/background-agent/manager.ts | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts index a4094b7..25c9ee3 100644 --- a/src/features/background-agent/manager.ts +++ b/src/features/background-agent/manager.ts @@ -224,6 +224,43 @@ export class BackgroundManager { } } + private notifyParentSession(task: BackgroundTask): void { + const duration = this.formatDuration(task.startedAt, task.completedAt) + const toolCalls = task.progress?.toolCalls ?? 0 + + const message = `✅ **Background task completed!** + +**Task ID:** ${task.id} +**Description:** ${task.description} +**Duration:** ${duration} +**Tool calls:** ${toolCalls} + +Use \`background_result\` tool with taskId="${task.id}" to retrieve the full result.` + + this.client.session.prompt({ + path: { id: task.parentSessionID }, + body: { + parts: [{ type: "text", text: message }], + }, + }).catch(() => { + void 0 + }) + } + + private formatDuration(start: Date, end?: Date): string { + const duration = (end ?? new Date()).getTime() - start.getTime() + const seconds = Math.floor(duration / 1000) + const minutes = Math.floor(seconds / 60) + const hours = Math.floor(minutes / 60) + + if (hours > 0) { + return `${hours}h ${minutes % 60}m ${seconds % 60}s` + } else if (minutes > 0) { + return `${minutes}m ${seconds % 60}s` + } + return `${seconds}s` + } + private hasRunningTasks(): boolean { for (const task of this.tasks.values()) { if (task.status === "running") return true @@ -255,6 +292,7 @@ export class BackgroundManager { task.completedAt = new Date() this.markForNotification(task) this.persist() + this.notifyParentSession(task) continue }