Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions apps/extension/src/tools/__tests__/human-loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,52 @@ describe("handleRequestHelp", () => {
}
});

it("does not keep the RPC pending when cleanup hangs after content finishes", async () => {
vi.useFakeTimers();
const chromeEvents = installHelpLifecycleChrome();
const sendToTab = vi.fn(async (_tabId: number, message: { type?: string }) => {
if (message.type === "bsk-help-request") return { type: "bsk-help-ack", ok: true };
return new Promise<never>(() => {});
});
const deps = baseDeps({ autoAttachLifecycle: undefined, sendToTab });

try {
const pending = handleRequestHelp(
fakeManager("abcd", 99, 5),
baseParams({ tab_id: 5, timeout_ms: 60_000 }),
deps,
);
await vi.waitFor(() =>
expect(sendToTab).toHaveBeenCalledWith(
5,
expect.objectContaining({ type: "bsk-help-request" }),
),
);

const request = sendToTab.mock.calls[0]?.[1] as { requestId: string };
chromeEvents.runtimeOnMessage.emit(
{
type: "bsk-help-finish",
requestId: request.requestId,
outcome: "continued",
},
{ tab: { id: 5 } as chrome.tabs.Tab } as chrome.runtime.MessageSender,
vi.fn(),
);
await vi.waitFor(() =>
expect(sendToTab).toHaveBeenCalledWith(
5,
expect.objectContaining({ type: "bsk-help-cancel" }),
),
);

await vi.advanceTimersByTimeAsync(1_100);
await expect(pending).resolves.toMatchObject({ outcome: "continued", tab_id: 5 });
} finally {
vi.useRealTimers();
}
});

it("returns completed when explicit completion criteria match", async () => {
const deps = baseDeps({
sendToTab: vi.fn(async () => ({ type: "bsk-help-ack", ok: true })),
Expand Down
18 changes: 17 additions & 1 deletion apps/extension/src/tools/human-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const HELP_SEND_RETRY_DELAY_MS = 350;
const HELP_REARM_DEBOUNCE_MS = 150;
const HELP_REARM_MAX_ATTEMPTS = 12;
const HELP_REARM_RETRY_DELAY_MS = 400;
const HELP_CLEANUP_TIMEOUT_MS = 1_000;
const DEFAULT_COMPLETION_STABLE_MS = 1_000;
const COMPLETION_POLL_MS = 500;

Expand Down Expand Up @@ -299,6 +300,21 @@ async function cleanupHelp(help: ActiveHelpRequest): Promise<void> {
);
}

/** Never let best-effort UI cleanup hold the daemon RPC open indefinitely. */
async function awaitCleanupWithinBudget(cleanup: Promise<void>): Promise<void> {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
await Promise.race([
cleanup,
new Promise<void>((resolve) => {
timer = setTimeout(resolve, HELP_CLEANUP_TIMEOUT_MS);
}),
]);
} finally {
if (timer !== undefined) clearTimeout(timer);
}
}

async function finishHelp(
help: ActiveHelpRequest,
value: RequestHelpResult | RpcError,
Expand All @@ -316,7 +332,7 @@ async function finishHelp(
for (const tabId of help.overlayTabIds) clearRearmTimer(tabId);
if (notifyContent) {
const cleanup = cleanupHelp(help);
if (waitForCleanup) await cleanup;
if (waitForCleanup) await awaitCleanupWithinBudget(cleanup);
else void cleanup;
}
help.resolve(value);
Expand Down