Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(chat): show running state for turns this tab didn't start

Yuting Zhongda6d1c7f6d7b
diff --git a/notfair/src/components/live-transcript.tsx b/notfair/src/components/live-transcript.tsx
index 03e4d6a6..ea10b25e 100644
--- a/notfair/src/components/live-transcript.tsx
+++ b/notfair/src/components/live-transcript.tsx
@@ -37,6 +37,7 @@ import {
import { Markdown } from "@/components/markdown";
import { brandDomain } from "@/components/mcp-icon";
import { RunningDot } from "@/components/running-dot";
+import { findOpenTurn, isOpenTurnLive } from "@/lib/turn-state";
import {
WorkingIndicator,
type WorkingMood,
@@ -646,6 +647,22 @@ export function LiveTranscript({
const transcriptEnded = events.some(
(e) => e.kind === "lifecycle" && e.phase === "done",
);
+ // A turn can be running that THIS tab didn't start — another tab, a
+ // reload mid-turn, or a dropped SSE stream (the backend keeps going;
+ // disconnect ≠ cancel). Without this, the UI showed a Send button and
+ // no indicator while the agent was mid-work: everything read as
+ // "finished". Derive in-flight state from the committed events, with a
+ // staleness cutoff so a server crash can't strand a forever-spinner.
+ const openTurn = useMemo(() => findOpenTurn(events), [events]);
+ const [staleNow, setStaleNow] = useState(() => Date.now());
+ useEffect(() => {
+ if (!openTurn || sendingChat) return;
+ setStaleNow(Date.now());
+ const id = setInterval(() => setStaleNow(Date.now()), 30_000);
+ return () => clearInterval(id);
+ }, [openTurn, sendingChat]);
+ const remoteTurnActive = !sendingChat && isOpenTurnLive(openTurn, staleNow);
+ const composerBusy = sendingChat || remoteTurnActive;
// Keep the indicator at the bottom of the transcript throughout a live
// turn. Read-only logs can opt into a static completed status, but merely
// disabling the composer must never manufacture a forever-running state.
@@ -656,6 +673,7 @@ export function LiveTranscript({
// working.")
const showThinking =
sendingChat ||
+ remoteTurnActive ||
Boolean(blockedReason) ||
(showCompletedStatus && transcriptEnded);
@@ -711,7 +729,7 @@ export function LiveTranscript({
<LiveWorkingIndicator
agentDisplayName={agentDisplayName}
events={events}
- turnStartedAt={turnStartedAt}
+ turnStartedAt={turnStartedAt ?? openTurn?.startedAt ?? null}
lifecyclePhase={pendingLifecycle}
pendingTools={pendingTools}
hasPendingAssistant={pendingAssistant.length > 0}
@@ -833,7 +851,7 @@ export function LiveTranscript({
</DropdownMenuContent>
</DropdownMenu>
)}
- {sendingChat ? (
+ {composerBusy ? (
<Button
type="button"
variant="outline"
@@ -857,12 +875,14 @@ export function LiveTranscript({
)}
</div>
</form>
- {(sendingChat || !composerDisabled) && (
+ {(composerBusy || !composerDisabled) && (
<p className="pt-1.5 text-center text-[10px] text-muted-foreground">
- {sendingChat ? (
+ {composerBusy ? (
<span className="inline-flex items-center gap-1.5">
<RunningDot size="sm" aria-label="" />
- Streaming — click stop to abort
+ {sendingChat
+ ? "Streaming — click stop to abort"
+ : "Agent is working — click stop to interrupt"}
</span>
) : (
<>Enter to send · Shift+Enter for newline</>
@@ -1406,8 +1426,15 @@ function LiveWorkingIndicator({
hasPendingAssistant?: boolean;
}) {
const [now, setNow] = useState<number>(() => Date.now());
+ // Scope the "done" check to the current turn — an earlier completed
+ // turn's done event must not freeze the clock on a live later turn.
const visiblyEnded =
- events.some((e) => e.kind === "lifecycle" && e.phase === "done") &&
+ events.some(
+ (e) =>
+ e.kind === "lifecycle" &&
+ e.phase === "done" &&
+ (turnStartedAt == null || e.ts >= turnStartedAt),
+ ) &&
!pendingTools?.some((t) => !t.done) &&
!hasPendingAssistant;
useEffect(() => {
diff --git a/notfair/src/lib/turn-state.test.ts b/notfair/src/lib/turn-state.test.ts
new file mode 100644
index 00000000..65743159
--- /dev/null
+++ b/notfair/src/lib/turn-state.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+
+import { findOpenTurn, isOpenTurnLive, OPEN_TURN_STALE_MS } from "./turn-state";
+
+const t0 = 1_800_000_000_000;
+const lc = (phase: string, ts: number) => ({ kind: "lifecycle", ts, phase });
+const tool = (ts: number) => ({ kind: "tool_call", ts });
+
+describe("findOpenTurn", () => {
+ it("returns null for an empty log or a completed last turn", () => {
+ expect(findOpenTurn([])).toBeNull();
+ expect(findOpenTurn([lc("start", t0), tool(t0 + 1), lc("done", t0 + 2)])).toBeNull();
+ });
+
+ it("detects a trailing start with no done as an open turn", () => {
+ const turn = findOpenTurn([lc("start", t0), tool(t0 + 5_000)]);
+ expect(turn).toEqual({ startedAt: t0, lastEventTs: t0 + 5_000 });
+ });
+
+ it("is not fooled by earlier completed turns in the same thread", () => {
+ const events = [
+ lc("start", t0),
+ lc("done", t0 + 1),
+ lc("start", t0 + 10),
+ tool(t0 + 20),
+ ];
+ expect(findOpenTurn(events)).toEqual({ startedAt: t0 + 10, lastEventTs: t0 + 20 });
+ });
+
+ it("skips non-boundary lifecycle phases (warming etc.)", () => {
+ const events = [lc("start", t0), lc("warming", t0 + 1), tool(t0 + 2)];
+ expect(findOpenTurn(events)).toEqual({ startedAt: t0, lastEventTs: t0 + 2 });
+ });
+});
+
+describe("isOpenTurnLive", () => {
+ const turn = { startedAt: t0, lastEventTs: t0 };
+ it("is live while events are fresh, stale after the cutoff", () => {
+ expect(isOpenTurnLive(turn, t0 + 1_000)).toBe(true);
+ expect(isOpenTurnLive(turn, t0 + OPEN_TURN_STALE_MS + 1)).toBe(false);
+ expect(isOpenTurnLive(null, t0)).toBe(false);
+ });
+});
diff --git a/notfair/src/lib/turn-state.ts b/notfair/src/lib/turn-state.ts
new file mode 100644
index 00000000..59fc0908
--- /dev/null
+++ b/notfair/src/lib/turn-state.ts
@@ -0,0 +1,48 @@
+/**
+ * Turn-state derivation from committed transcript events.
+ *
+ * A turn can be running that the viewing tab didn't start — another tab,
+ * a page reload mid-turn, or a dropped SSE stream (the backend keeps
+ * going; disconnect ≠ cancel). The transcript itself is the source of
+ * truth: the harness writes a lifecycle "start" when a turn begins and
+ * transcript-tail surfaces its final as lifecycle "done", so a trailing
+ * "start" with no "done" after it means the agent is still working.
+ */
+
+type LifecycleLike = { kind: string; ts: number; phase?: string };
+
+export type OpenTurn = {
+ /** ts of the lifecycle "start" that opened the still-running turn. */
+ startedAt: number;
+ /** ts of the newest committed event — the staleness anchor. */
+ lastEventTs: number;
+};
+
+/**
+ * The still-open turn implied by the event log, or null when the last
+ * turn completed. Scans backward to the nearest turn boundary so earlier
+ * completed turns in the same thread can't mask a live one (or fake one).
+ */
+export function findOpenTurn(events: readonly LifecycleLike[]): OpenTurn | null {
+ for (let i = events.length - 1; i >= 0; i--) {
+ const e = events[i]!;
+ if (e.kind !== "lifecycle") continue;
+ if (e.phase === "done") return null;
+ if (e.phase === "start") {
+ return { startedAt: e.ts, lastEventTs: events[events.length - 1]!.ts };
+ }
+ }
+ return null;
+}
+
+/**
+ * No committed event for this long means the run is treated as orphaned
+ * (server restart mid-turn never writes a final). Live turns emit tool
+ * and delta events well inside this window, so a healthy turn can't go
+ * stale; the indicator simply returns when events resume.
+ */
+export const OPEN_TURN_STALE_MS = 10 * 60_000;
+
+export function isOpenTurnLive(turn: OpenTurn | null, now: number): boolean {
+ return turn !== null && now - turn.lastEventTs < OPEN_TURN_STALE_MS;
+}