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
25 changes: 15 additions & 10 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
deriveWorkLogEntries,
hasActionableProposedPlan,
hasToolActivityForTurn,
isSessionActivelyRunningTurn,
isLatestTurnSettled,
formatElapsed,
} from "../session-logic";
Expand Down Expand Up @@ -926,7 +927,11 @@ export default function ChatView({ threadId }: ChatViewProps) {
activePendingUserInput: activePendingUserInput?.requestId ?? null,
threadError: activeThread?.error,
});
const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint;
const isTurnRunning = isSessionActivelyRunningTurn(
activeLatestTurn,
activeThread?.session ?? null,
);
const isWorking = isTurnRunning || isSendBusy || isConnecting || isRevertingCheckpoint;
const nowIso = new Date(nowTick).toISOString();
const activeWorkStartedAt = deriveActiveWorkStartedAt(
activeLatestTurn,
Expand All @@ -943,7 +948,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
if (activePendingProgress) {
return `pending:${activePendingProgress.questionIndex}:${activePendingProgress.isLastQuestion}:${activePendingIsResponding}`;
}
if (phase === "running") {
if (isTurnRunning) {
return "running";
}
if (showPlanFollowUpPrompt) {
Expand All @@ -957,7 +962,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
isConnecting,
isPreparingWorktree,
isSendBusy,
phase,
isTurnRunning,
prompt,
showPlanFollowUpPrompt,
]);
Expand Down Expand Up @@ -2143,10 +2148,10 @@ export default function ChatView({ threadId }: ChatViewProps) {
scheduleStickToBottom();
}, [messageCount, scheduleStickToBottom]);
useEffect(() => {
if (phase !== "running") return;
if (!isTurnRunning) return;
if (!shouldAutoScrollRef.current) return;
scheduleStickToBottom();
}, [phase, scheduleStickToBottom, timelineEntries]);
}, [isTurnRunning, scheduleStickToBottom, timelineEntries]);

useEffect(() => {
setExpandedWorkGroups({});
Expand Down Expand Up @@ -2365,14 +2370,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
: "local";

useEffect(() => {
if (phase !== "running") return;
if (!isTurnRunning) return;
const timer = window.setInterval(() => {
setNowTick(Date.now());
}, 1000);
return () => {
window.clearInterval(timer);
};
}, [phase]);
}, [isTurnRunning]);

useEffect(() => {
if (!activeThreadId) return;
Expand Down Expand Up @@ -2592,7 +2597,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const api = readNativeApi();
if (!api || !activeThread || isRevertingCheckpoint) return;

if (phase === "running" || isSendBusy || isConnecting) {
if (isTurnRunning || isSendBusy || isConnecting) {
setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");
return;
}
Expand Down Expand Up @@ -2625,7 +2630,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
}
setIsRevertingCheckpoint(false);
},
[activeThread, isConnecting, isRevertingCheckpoint, isSendBusy, phase, setThreadError],
[activeThread, isConnecting, isRevertingCheckpoint, isSendBusy, isTurnRunning, setThreadError],
);

const onSend = async (e?: { preventDefault: () => void }) => {
Expand Down Expand Up @@ -4214,7 +4219,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
}
: null
}
isRunning={phase === "running"}
isRunning={isTurnRunning}
showPlanFollowUpPrompt={
pendingUserInputs.length === 0 && showPlanFollowUpPrompt
}
Expand Down
88 changes: 85 additions & 3 deletions apps/web/src/session-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
findSidebarProposedPlan,
hasActionableProposedPlan,
hasToolActivityForTurn,
isSessionActivelyRunningTurn,
isLatestTurnSettled,
} from "./session-logic";

Expand Down Expand Up @@ -1072,15 +1073,30 @@ describe("isLatestTurnSettled", () => {
} as const;

it("returns false while the same turn is still active in a running session", () => {
expect(
isLatestTurnSettled(
{
...latestTurn,
completedAt: null,
},
{
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
},
),
).toBe(false);
});

it("returns true when the turn completed but the session status stayed stale-running", () => {
expect(
isLatestTurnSettled(latestTurn, {
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
}),
).toBe(false);
).toBe(true);
});

it("returns false while any turn is running to avoid stale latest-turn banners", () => {
it("returns false while a different turn is still running", () => {
expect(
isLatestTurnSettled(latestTurn, {
orchestrationStatus: "running",
Expand Down Expand Up @@ -1112,6 +1128,56 @@ describe("isLatestTurnSettled", () => {
});
});

describe("isSessionActivelyRunningTurn", () => {
const completedTurn = {
turnId: TurnId.makeUnsafe("turn-1"),
startedAt: "2026-02-27T21:10:00.000Z",
completedAt: "2026-02-27T21:10:06.000Z",
} as const;

it("returns true when the current turn has not completed yet", () => {
expect(
isSessionActivelyRunningTurn(
{
...completedTurn,
completedAt: null,
},
{
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
},
),
).toBe(true);
});

it("returns false when the same turn already completed and only the session is stale", () => {
expect(
isSessionActivelyRunningTurn(completedTurn, {
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
}),
).toBe(false);
});

it("returns true when a different turn is still active", () => {
expect(
isSessionActivelyRunningTurn(completedTurn, {
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-2"),
}),
).toBe(true);
});

it("returns false when the session is not running", () => {
expect(
isSessionActivelyRunningTurn(completedTurn, {
orchestrationStatus: "ready",
activeTurnId: undefined,
}),
).toBe(false);
});
});

describe("deriveActiveWorkStartedAt", () => {
const latestTurn = {
turnId: TurnId.makeUnsafe("turn-1"),
Expand All @@ -1122,7 +1188,10 @@ describe("deriveActiveWorkStartedAt", () => {
it("prefers the in-flight turn start when the latest turn is not settled", () => {
expect(
deriveActiveWorkStartedAt(
latestTurn,
{
...latestTurn,
completedAt: null,
},
{
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
Expand All @@ -1132,6 +1201,19 @@ describe("deriveActiveWorkStartedAt", () => {
).toBe("2026-02-27T21:10:00.000Z");
});

it("falls back to sendStartedAt when only the session status is stale-running", () => {
expect(
deriveActiveWorkStartedAt(
latestTurn,
{
orchestrationStatus: "running",
activeTurnId: TurnId.makeUnsafe("turn-1"),
},
"2026-02-27T21:11:00.000Z",
),
).toBe("2026-02-27T21:11:00.000Z");
});

it("falls back to sendStartedAt once the latest turn is settled", () => {
expect(
deriveActiveWorkStartedAt(
Expand Down
21 changes: 18 additions & 3 deletions apps/web/src/session-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,30 @@ export function formatElapsed(startIso: string, endIso: string | undefined): str
type LatestTurnTiming = Pick<OrchestrationLatestTurn, "turnId" | "startedAt" | "completedAt">;
type SessionActivityState = Pick<ThreadSession, "orchestrationStatus" | "activeTurnId">;

export function isSessionActivelyRunningTurn(
latestTurn: LatestTurnTiming | null,
session: SessionActivityState | null,
): boolean {
if (!session || session.orchestrationStatus !== "running") return false;
if (!latestTurn) return true;

const activeTurnId = session.activeTurnId;
if (activeTurnId === undefined) {
return latestTurn.completedAt === null;
}
if (latestTurn.turnId !== activeTurnId) {
return true;
}
return latestTurn.completedAt === null;
}

export function isLatestTurnSettled(
latestTurn: LatestTurnTiming | null,
session: SessionActivityState | null,
): boolean {
if (!latestTurn?.startedAt) return false;
if (!latestTurn.completedAt) return false;
if (!session) return true;
if (session.orchestrationStatus === "running") return false;
return true;
return !isSessionActivelyRunningTurn(latestTurn, session);
}

export function deriveActiveWorkStartedAt(
Expand Down
Loading