fix(prompt-gate): harden internal prompt dispatch

This commit is contained in:
YeonGyu-Kim
2026-05-19 16:02:18 +09:00
parent 6c63372ef9
commit 1492bffd20
49 changed files with 1488 additions and 141 deletions
@@ -285,7 +285,7 @@ describe("createTeamSendMessageTool", () => {
expect(calls[0]?.directory).toBe(resolveBaseDir(fixture.config))
})
test("#given recipient OpenCode session is busy #when team_send_message attempts live delivery #then it leaves the message unread without starting another reply", async () => {
test("#given recipient OpenCode session is busy #when team_send_message attempts live delivery #then it reserves the message in the central prompt queue", async () => {
// given
const fixture = await createTeamFixture()
let promptCalls = 0
@@ -309,11 +309,10 @@ describe("createTeamSendMessageTool", () => {
// then
expect(promptCalls).toBe(0)
const unread = await listUnreadMessages(fixture.teamRunId, "m2", fixture.config)
expect(unread).toHaveLength(1)
expect(unread[0]?.body).toBe("ping while busy")
expect(unread).toHaveLength(0)
})
test("#given rapid live deliveries to one recipient #when the first prompt just dispatched #then the next message stays unread instead of starting another reply", async () => {
test("#given rapid live deliveries to one recipient #when the first prompt just dispatched #then the next message is queued instead of starting another reply", async () => {
// given
const fixture = await createTeamFixture()
const { client, calls } = createRecordingClient()
@@ -335,11 +334,10 @@ describe("createTeamSendMessageTool", () => {
expect(calls).toHaveLength(1)
expect(calls[0]?.parts[0]?.text).toContain("first ping")
const unread = await listUnreadMessages(fixture.teamRunId, "m2", fixture.config)
expect(unread).toHaveLength(1)
expect(unread[0]?.body).toBe("second ping")
expect(unread).toHaveLength(0)
})
test("#given live delivery left a rapid message unread #when recipient idle wake fires immediately #then the wake hint does not start a second reply", async () => {
test("#given live delivery queued a rapid message #when recipient idle wake fires immediately #then the wake hint does not start a second reply", async () => {
// given
const fixture = await createTeamFixture()
const { client, calls } = createRecordingClient()
@@ -373,8 +371,7 @@ describe("createTeamSendMessageTool", () => {
expect(calls).toHaveLength(1)
expect(calls[0]?.parts[0]?.text).toContain("first ping")
const unread = await listUnreadMessages(fixture.teamRunId, "m2", fixture.config)
expect(unread).toHaveLength(1)
expect(unread[0]?.body).toBe("second ping")
expect(unread).toHaveLength(0)
})
test("live delivery pins the recipient's resolved subagent_type and model on promptAsync", async () => {
+4 -23
View File
@@ -4,8 +4,9 @@ import { type ToolDefinition, tool } from "@opencode-ai/plugin/tool"
import { z } from "zod"
import type { TeamModeConfig } from "../../../config/schema/team-mode"
import { dispatchInternalPrompt } from "../../../hooks/shared/prompt-async-gate"
import { dispatchInternalPrompt, isInternalPromptDispatchAccepted } from "../../../hooks/shared/prompt-async-gate"
import { log } from "../../../shared/logger"
import { isAmbiguousPromptDispatchFailure } from "../../../shared/prompt-failure-classifier"
import { applyMemberSessionRouting, buildMemberPromptBody } from "../member-session-routing"
import { buildEnvelope } from "../team-mailbox/poll"
import {
@@ -68,26 +69,6 @@ const TeamSendMessageArgsSchema = z.object({
type DeliveryReservation = Awaited<ReturnType<typeof reserveMessageForDelivery>>
function extractPromptFailureMessage(error: unknown): string {
if (typeof error === "string") return error
if (error instanceof Error) return error.message
if (typeof error === "object" && error !== null) {
const record = error as Record<string, unknown>
if (typeof record.message === "string") return record.message
try {
return JSON.stringify(error)
} catch {
return ""
}
}
return String(error)
}
function shouldKeepReservationAfterFailedLivePrompt(error: unknown): boolean {
const message = extractPromptFailureMessage(error)
return message.includes("Unexpected EOF") || message.includes("timed out")
}
async function resolveTeamRuntimeDetails(
teamRunId: string,
sessionID: string,
@@ -230,7 +211,7 @@ async function deliverLive(
query: { directory: recipientMember.worktreePath ?? directory },
},
})
if (promptResult.status === "failed" && shouldKeepReservationAfterFailedLivePrompt(promptResult.error)) {
if (promptResult.status === "failed" && isAmbiguousPromptDispatchFailure(promptResult.error)) {
await markLiveDeliveryPending(teamRunId, recipientName, message.messageId, config)
log("[team-mailbox] live delivery prompt failed after dispatch attempt, keeping reservation pending", {
teamRunId,
@@ -241,7 +222,7 @@ async function deliverLive(
})
continue
}
if (promptResult.status !== "dispatched") {
if (!isInternalPromptDispatchAccepted(promptResult)) {
log("[team-mailbox] live delivery skipped by promptAsync gate, falling back to inbox injection", {
status: promptResult.status,
teamRunId,