Skip to content
Merged
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
3 changes: 2 additions & 1 deletion apps/web/src/components/BackgroundThreadAnnouncer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type BackgroundThreadSnapshot,
nextBackgroundThreadAnnouncement,
resolveBackgroundThreadRequest,
resolveBackgroundThreadRunStatus,
} from "./backgroundThreadAnnouncement";

const backgroundThreadLiveRegion = createLiveRegion();
Expand Down Expand Up @@ -51,7 +52,7 @@ export function startBackgroundThreadAnnouncements(
title: thread.title,
...resolveBackgroundThreadRequest(thread),
runId: thread.latestRun?.runId ?? null,
runStatus: thread.latestRun?.status ?? null,
runStatus: resolveBackgroundThreadRunStatus(thread),
activeRunId: thread.runtime?.activeRunId ?? null,
snoozed: effectiveSnoozed(thread, { now }),
});
Expand Down
70 changes: 70 additions & 0 deletions apps/web/src/components/backgroundThreadAnnouncement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type BackgroundThreadSnapshot,
nextBackgroundThreadAnnouncement,
resolveBackgroundThreadRequest,
resolveBackgroundThreadRunStatus,
} from "./backgroundThreadAnnouncement";

const firstRun = RunId.make("run-1");
Expand Down Expand Up @@ -187,3 +188,72 @@ describe("resolveBackgroundThreadRequest", () => {
).toEqual(["Needs approval: Fix login redirect"]);
});
});

describe("resolveBackgroundThreadRunStatus", () => {
const threadStatus = (overrides?: {
readonly status?: "completed" | "failed" | "interrupted";
readonly pendingBackgroundTasks?: ReadonlyArray<{
readonly taskId: string;
readonly kind: "command" | "monitor" | "subagent" | "background_task";
readonly awaited?: true;
}>;
readonly latestRunBackgroundWake?: boolean;
readonly latestRunStopRequested?: boolean;
}) => {
const pendingBackgroundTasks = overrides?.pendingBackgroundTasks ?? [];
return resolveBackgroundThreadRunStatus({
latestRun: { status: overrides?.status ?? "completed" },
pendingBackgroundTasks,
source: {
pendingBackgroundTasks,
latestRunBackgroundWake: overrides?.latestRunBackgroundWake ?? false,
latestRunStopRequested: overrides?.latestRunStopRequested ?? false,
},
});
};

it("suppresses Done while awaited background work or a monitor wake holds completion, or when stop was requested", () => {
const heldCases = [
{ latestRunStopRequested: true },
{ pendingBackgroundTasks: [{ taskId: "sub-1", kind: "subagent" as const }] },
{
pendingBackgroundTasks: [
{ taskId: "cmd-1", kind: "command" as const, awaited: true as const },
],
},
{
latestRunBackgroundWake: true,
pendingBackgroundTasks: [{ taskId: "mon-1", kind: "monitor" as const }],
},
];
for (const input of heldCases) {
expect(threadStatus(input)).toBeNull();
}
});

it("keeps Done for unawaited background work and keeps non-completed outcomes", () => {
expect(
threadStatus({
pendingBackgroundTasks: [{ taskId: "cmd-1", kind: "command" }],
}),
).toBe("completed");
expect(
threadStatus({
latestRunBackgroundWake: false,
pendingBackgroundTasks: [{ taskId: "mon-1", kind: "monitor" }],
}),
).toBe("completed");
expect(
threadStatus({
status: "failed",
pendingBackgroundTasks: [{ taskId: "sub-1", kind: "subagent" }],
}),
).toBe("failed");
expect(
threadStatus({
status: "interrupted",
latestRunStopRequested: true,
}),
).toBe("interrupted");
});
});
25 changes: 25 additions & 0 deletions apps/web/src/components/backgroundThreadAnnouncement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { OrchestrationV2RunStatus, RunId } from "@t3tools/contracts";
import {
backgroundWorkHoldsCompletion,
monitorHoldsWakeCompletion,
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";

import { msg } from "gt-react";

Expand Down Expand Up @@ -40,6 +44,27 @@ export function resolveBackgroundThreadRequest(
return { request: null, requestId: null };
}

/** Suppresses Done announcements for stopped runs, awaited background work, and monitor wakes. */
export function resolveBackgroundThreadRunStatus(thread: {
readonly latestRun: { readonly status: OrchestrationV2RunStatus } | null;
readonly pendingBackgroundTasks: SidebarThreadSummary["pendingBackgroundTasks"];
readonly source: Pick<
SidebarThreadSummary["source"],
"latestRunBackgroundWake" | "latestRunStopRequested" | "pendingBackgroundTasks"
>;
}): OrchestrationV2RunStatus | null {
const status = thread.latestRun?.status ?? null;
if (
status === "completed" &&
(thread.source.latestRunStopRequested === true ||
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks) ||
monitorHoldsWakeCompletion(thread.source))
) {
return null;
}
return status;
}

interface BackgroundThreadMemoryEntry {
readonly request: BackgroundThreadSnapshot["request"];
readonly requestId: string | null;
Expand Down
Loading