fix(parent-wake): bound assistant-text defer to escape stuck sessions
shouldDeferParentWakeForSessionHistory previously had only one escape path from the defer state: stale pending tool call. If the assistant had unfinished text but no pending tool call (session crashed mid-stream, model errored after partial text, network died), the escape never fired and parent-wake deferred forever. Background-agent completions never woke the parent. Add a second escape: when the assistant text blocks but no tool wait is pending, dispatch the wake after toolCallDeferMaxMs anyway. The prompt-async-gate still defends if the assistant text turns out to be live; we just stop deferring indefinitely. Closes pre-publish blocker V11.
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
import { describe, expect, test } from "bun:test"
|
import { describe, expect, test } from "bun:test"
|
||||||
import { releaseAllPromptAsyncReservationsForTesting } from "../../hooks/shared/prompt-async-gate"
|
import { releaseAllPromptAsyncReservationsForTesting } from "../../hooks/shared/prompt-async-gate"
|
||||||
|
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||||
import { ParentWakeNotifier } from "./parent-wake-notifier"
|
import { ParentWakeNotifier } from "./parent-wake-notifier"
|
||||||
|
|
||||||
type PromptAsyncCall = {
|
type PromptAsyncCall = {
|
||||||
@@ -16,12 +19,11 @@ type PromptAsyncCall = {
|
|||||||
type ParentWakeClient = ConstructorParameters<typeof ParentWakeNotifier>[0]["client"]
|
type ParentWakeClient = ConstructorParameters<typeof ParentWakeNotifier>[0]["client"]
|
||||||
|
|
||||||
describe("ParentWakeNotifier — assistant turn blocking", () => {
|
describe("ParentWakeNotifier — assistant turn blocking", () => {
|
||||||
test("#given stale unfinished assistant text turn blocks the parent #when flushing pending wake #then stale tool escape does not dispatch", async () => {
|
test("#given stale unfinished assistant text has no pending tool call #when checking parent wake history #then parent wake dispatches after defer max", async () => {
|
||||||
// given
|
// given
|
||||||
const originalDateNow = Date.now
|
const originalDateNow = Date.now
|
||||||
Date.now = () => 100_000
|
Date.now = () => 100_000
|
||||||
const promptAsyncCalls: PromptAsyncCall[] = []
|
const client = unsafeTestValue<ParentWakeClient>({
|
||||||
const client: ParentWakeClient = {
|
|
||||||
session: {
|
session: {
|
||||||
messages: async () => ({
|
messages: async () => ({
|
||||||
data: [
|
data: [
|
||||||
@@ -31,17 +33,16 @@ describe("ParentWakeNotifier — assistant turn blocking", () => {
|
|||||||
finish: "unknown",
|
finish: "unknown",
|
||||||
time: { created: 90_000 },
|
time: { created: 90_000 },
|
||||||
},
|
},
|
||||||
parts: [{ type: "reasoning", text: "still streaming" }],
|
parts: [{ type: "text", text: "still streaming" }],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
status: async () => ({ data: { "parent-unfinished-text": { type: "idle" } } }),
|
status: async () => ({ data: { "parent-stale-text": { type: "idle" } } }),
|
||||||
promptAsync: async (call: PromptAsyncCall) => {
|
promptAsync: async () => {
|
||||||
promptAsyncCalls.push(call)
|
|
||||||
return { data: {} }
|
return { data: {} }
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
const notifier = new ParentWakeNotifier(
|
const notifier = new ParentWakeNotifier(
|
||||||
{
|
{
|
||||||
client,
|
client,
|
||||||
@@ -59,12 +60,12 @@ describe("ParentWakeNotifier — assistant turn blocking", () => {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
notifier.queuePendingParentWake(
|
notifier.queuePendingParentWake(
|
||||||
"parent-unfinished-text",
|
"parent-stale-text",
|
||||||
"task complete",
|
"task complete",
|
||||||
{ agent: "sisyphus" },
|
{ agent: "sisyphus" },
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
const pendingWake = notifier.getPendingParentWakes().get("parent-unfinished-text")
|
const pendingWake = notifier.getPendingParentWakes().get("parent-stale-text")
|
||||||
expect(pendingWake).toBeDefined()
|
expect(pendingWake).toBeDefined()
|
||||||
if (!pendingWake) {
|
if (!pendingWake) {
|
||||||
throw new Error("Missing pending parent wake")
|
throw new Error("Missing pending parent wake")
|
||||||
@@ -73,11 +74,76 @@ describe("ParentWakeNotifier — assistant turn blocking", () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// when
|
// when
|
||||||
await notifier.flushPendingParentWake("parent-unfinished-text")
|
const decision = await notifier["shouldDeferParentWakeForSessionHistory"]("parent-stale-text", pendingWake)
|
||||||
|
|
||||||
// then
|
// then
|
||||||
expect(promptAsyncCalls).toHaveLength(0)
|
expect(decision).toEqual({ defer: false, skipPromptGateToolStateCheck: false })
|
||||||
expect(notifier.getPendingParentWakes().has("parent-unfinished-text")).toBe(true)
|
} finally {
|
||||||
|
Date.now = originalDateNow
|
||||||
|
notifier.shutdown()
|
||||||
|
releaseAllPromptAsyncReservationsForTesting()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given fresh unfinished assistant text has no pending tool call #when checking parent wake history #then parent wake continues deferring", async () => {
|
||||||
|
// given
|
||||||
|
const originalDateNow = Date.now
|
||||||
|
Date.now = () => 100_000
|
||||||
|
const client = unsafeTestValue<ParentWakeClient>({
|
||||||
|
session: {
|
||||||
|
messages: async () => ({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
info: {
|
||||||
|
role: "assistant",
|
||||||
|
finish: "unknown",
|
||||||
|
time: { created: 99_000 },
|
||||||
|
},
|
||||||
|
parts: [{ type: "text", text: "still streaming" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
status: async () => ({ data: { "parent-fresh-text": { type: "idle" } } }),
|
||||||
|
promptAsync: async () => {
|
||||||
|
return { data: {} }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const notifier = new ParentWakeNotifier(
|
||||||
|
{
|
||||||
|
client,
|
||||||
|
directory: "/tmp/test-omo",
|
||||||
|
enqueueNotificationForParent: async (_sessionID, operation) => {
|
||||||
|
await operation()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pendingRetryMs: 1_000,
|
||||||
|
acceptedMessageSkewMs: 5_000,
|
||||||
|
toolCallDeferMaxMs: 5_000,
|
||||||
|
failureRequeueWindowMs: 5_000,
|
||||||
|
userMessageInProgressWindowMs: 2_000,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
notifier.queuePendingParentWake(
|
||||||
|
"parent-fresh-text",
|
||||||
|
"task complete",
|
||||||
|
{ agent: "sisyphus" },
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
const pendingWake = notifier.getPendingParentWakes().get("parent-fresh-text")
|
||||||
|
expect(pendingWake).toBeDefined()
|
||||||
|
if (!pendingWake) {
|
||||||
|
throw new Error("Missing pending parent wake")
|
||||||
|
}
|
||||||
|
pendingWake.toolCallDeferralStartedAt = 98_000
|
||||||
|
|
||||||
|
try {
|
||||||
|
// when
|
||||||
|
const decision = await notifier["shouldDeferParentWakeForSessionHistory"]("parent-fresh-text", pendingWake)
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(decision).toEqual({ defer: true, skipPromptGateToolStateCheck: false })
|
||||||
} finally {
|
} finally {
|
||||||
Date.now = originalDateNow
|
Date.now = originalDateNow
|
||||||
notifier.shutdown()
|
notifier.shutdown()
|
||||||
@@ -89,7 +155,7 @@ describe("ParentWakeNotifier — assistant turn blocking", () => {
|
|||||||
// given
|
// given
|
||||||
const promptAsyncCalls: PromptAsyncCall[] = []
|
const promptAsyncCalls: PromptAsyncCall[] = []
|
||||||
let messageReads = 0
|
let messageReads = 0
|
||||||
const client: ParentWakeClient = {
|
const client = unsafeTestValue<ParentWakeClient>({
|
||||||
session: {
|
session: {
|
||||||
messages: async () => {
|
messages: async () => {
|
||||||
messageReads += 1
|
messageReads += 1
|
||||||
@@ -115,7 +181,7 @@ describe("ParentWakeNotifier — assistant turn blocking", () => {
|
|||||||
return { data: {} }
|
return { data: {} }
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
const notifier = new ParentWakeNotifier(
|
const notifier = new ParentWakeNotifier(
|
||||||
{
|
{
|
||||||
client,
|
client,
|
||||||
|
|||||||
@@ -560,10 +560,11 @@ export class ParentWakeNotifier {
|
|||||||
const latestToolWaitAgeMs = toolWaitState.createdAt === undefined
|
const latestToolWaitAgeMs = toolWaitState.createdAt === undefined
|
||||||
? 0
|
? 0
|
||||||
: now - toolWaitState.createdAt
|
: now - toolWaitState.createdAt
|
||||||
|
const deferAge = now - wake.toolCallDeferralStartedAt
|
||||||
if (
|
if (
|
||||||
wake.shouldReply
|
wake.shouldReply
|
||||||
&& toolWaitState.waiting
|
&& toolWaitState.waiting
|
||||||
&& now - wake.toolCallDeferralStartedAt >= this.options.toolCallDeferMaxMs
|
&& deferAge >= this.options.toolCallDeferMaxMs
|
||||||
&& latestToolWaitAgeMs >= this.options.toolCallDeferMaxMs
|
&& latestToolWaitAgeMs >= this.options.toolCallDeferMaxMs
|
||||||
) {
|
) {
|
||||||
log("[background-agent] Sending parent wake after stale tool-call deferral window:", {
|
log("[background-agent] Sending parent wake after stale tool-call deferral window:", {
|
||||||
@@ -571,6 +572,13 @@ export class ParentWakeNotifier {
|
|||||||
})
|
})
|
||||||
return { defer: false, skipPromptGateToolStateCheck: true }
|
return { defer: false, skipPromptGateToolStateCheck: true }
|
||||||
}
|
}
|
||||||
|
if (!toolWaitState.waiting && deferAge >= this.options.toolCallDeferMaxMs) {
|
||||||
|
log("[background-agent] Sending parent wake after stale assistant-text deferral window:", {
|
||||||
|
sessionID,
|
||||||
|
deferAgeMs: deferAge,
|
||||||
|
})
|
||||||
|
return { defer: false, skipPromptGateToolStateCheck: false }
|
||||||
|
}
|
||||||
log("[background-agent] Deferred parent wake because latest assistant turn blocks internal prompts:", {
|
log("[background-agent] Deferred parent wake because latest assistant turn blocks internal prompts:", {
|
||||||
sessionID,
|
sessionID,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user