Skip to content
Closed
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
10 changes: 2 additions & 8 deletions apps/web/src/components/BackgroundThreadAnnouncer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
import { isShownUnderParent } from "@t3tools/client-runtime/state/threads";
import { isDelegatedAppThread } from "@t3tools/contracts";
import type { AnyRouter } from "@tanstack/react-router";
import type { AtomRegistry } from "effect/reactivity";

import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import { useComposerDraftStore } from "../composerDraftStore";
import { createLiveRegion } from "../hooks/useLiveRegion";
import { liveShellEnvironmentIdsAtom } from "../state/shell";
Expand All @@ -16,6 +14,7 @@ import {
type BackgroundThreadSnapshot,
nextBackgroundThreadAnnouncement,
resolveBackgroundThreadRequest,
resolveBackgroundThreadRunStatus,
} from "./backgroundThreadAnnouncement";

const backgroundThreadLiveRegion = createLiveRegion();
Expand Down Expand Up @@ -59,12 +58,7 @@ export function startBackgroundThreadAnnouncements(
title: thread.title,
...resolveBackgroundThreadRequest(thread),
runId: thread.latestRun?.runId ?? null,
runStatus:
thread.latestRun?.status === "completed" &&
(isDelegatedAppThread(thread.source) ||
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks))
? null
: (thread.latestRun?.status ?? null),
runStatus: resolveBackgroundThreadRunStatus(thread),
activeRunId: thread.runtime?.activeRunId ?? null,
snoozed: effectiveSnoozed(thread, { now }),
});
Expand Down
58 changes: 58 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 @@ -266,3 +267,60 @@ describe("resolveBackgroundThreadRequest", () => {
).toEqual(["Needs approval: Fix login redirect"]);
});
});

describe("resolveBackgroundThreadRunStatus", () => {
const rootLineage = {
rootThreadId: ThreadId.make("root"),
parentThreadId: null,
relationshipToParent: null,
} as const;
const completedRun = { runId: firstRun, status: "completed" as const };
const base = {
latestRun: completedRun,
pendingBackgroundTasks: [],
source: {
lineage: rootLineage,
pendingBackgroundTasks: [],
},
};

it("suppresses completed status while a monitor holds a wake or when stop was requested", () => {
const monitorTask = { taskId: "mon-1", kind: "monitor" as const, description: "tail -f" };
expect(resolveBackgroundThreadRunStatus(base)).toBe("completed");
expect(
resolveBackgroundThreadRunStatus({
...base,
source: { ...base.source, latestRunStopRequested: true },
}),
).toBeNull();
expect(
resolveBackgroundThreadRunStatus({
...base,
pendingBackgroundTasks: [monitorTask],
source: {
...base.source,
latestRunBackgroundWake: true,
pendingBackgroundTasks: [monitorTask],
},
}),
).toBeNull();
expect(
resolveBackgroundThreadRunStatus({
...base,
pendingBackgroundTasks: [monitorTask],
source: {
...base.source,
latestRunBackgroundWake: false,
pendingBackgroundTasks: [monitorTask],
},
}),
).toBe("completed");
expect(
resolveBackgroundThreadRunStatus({
...base,
latestRun: { status: "failed" as const },
source: { ...base.source, latestRunStopRequested: true },
}),
).toBe("failed");
});
});
29 changes: 29 additions & 0 deletions apps/web/src/components/backgroundThreadAnnouncement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
type OrchestrationV2RunStatus,
type RunId,
} from "@t3tools/contracts";
import {
backgroundWorkHoldsCompletion,
monitorHoldsWakeCompletion,
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";

import { msg } from "gt-react";

Expand Down Expand Up @@ -51,6 +55,31 @@ export function resolveBackgroundThreadRequest(
return { request: null, requestId: null };
}

export function resolveBackgroundThreadRunStatus(thread: {
readonly latestRun: { readonly status: OrchestrationV2RunStatus } | null;
readonly pendingBackgroundTasks: SidebarThreadSummary["pendingBackgroundTasks"];
readonly source: Pick<
SidebarThreadSummary["source"],
| "lineage"
| "delegatedFrom"
| "latestRunBackgroundWake"
| "latestRunStopRequested"
| "pendingBackgroundTasks"
>;
}): OrchestrationV2RunStatus | null {
const status = thread.latestRun?.status ?? null;
if (
status === "completed" &&
(isDelegatedAppThread(thread.source) ||
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