Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agentic-ide): harden replay curtain races

rubenluetke10-beep529b42770adf
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
index 4963d2c8d2..584aab061b 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.test.tsx
@@ -437,6 +437,92 @@ describe("AgenticTerminal layout", () => {
expect(terminalHarness.scrollToBottom).toHaveBeenCalled();
expect(region?.className).not.toContain("invisible");
});
+
+ it("does not let an older replay reveal a newer replay", () => {
+ vi.useFakeTimers();
+ render(
+ <AgenticTerminal
+ name="Dana"
+ displayName="Codex"
+ appearance="dark"
+ fontSize={13}
+ active
+ />,
+ );
+ const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
+ act(() => vi.advanceTimersByTime(20));
+
+ terminalHarness.deferWrite = true;
+ act(() => {
+ terminalHarness.handlers.current?.onReplay?.("older replay" as never);
+ terminalHarness.handlers.current?.onReplay?.("newer replay" as never);
+ });
+ expect(region?.className).toContain("invisible");
+ expect(terminalHarness.writeCallbacks).toHaveLength(2);
+
+ act(() => {
+ terminalHarness.writeCallbacks.shift()?.();
+ vi.advanceTimersByTime(20);
+ });
+ expect(region?.className).toContain("invisible");
+
+ act(() => {
+ terminalHarness.writeCallbacks.shift()?.();
+ vi.advanceTimersByTime(20);
+ });
+ expect(region?.className).not.toContain("invisible");
+ });
+
+ it("does not let a stale replay frame bypass a new stage barrier", () => {
+ vi.useFakeTimers();
+ const view = render(
+ <AgenticTerminal
+ name="Dana"
+ displayName="Codex"
+ appearance="dark"
+ fontSize={13}
+ active
+ />,
+ );
+ const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
+ act(() => vi.advanceTimersByTime(20));
+
+ terminalHarness.deferWrite = true;
+ act(() => {
+ terminalHarness.handlers.current?.onReplay?.("recorded session" as never);
+ terminalHarness.writeCallbacks.shift()?.();
+ });
+ // The replay completion has scheduled its reveal frame, but a stage switch
+ // supersedes it before that frame gets to paint.
+ view.rerender(
+ <AgenticTerminal
+ name="Dana"
+ displayName="Codex"
+ appearance="dark"
+ fontSize={13}
+ active={false}
+ />,
+ );
+ view.rerender(
+ <AgenticTerminal
+ name="Dana"
+ displayName="Codex"
+ appearance="dark"
+ fontSize={13}
+ active
+ />,
+ );
+ expect(terminalHarness.writeCallbacks).toHaveLength(1);
+
+ act(() => vi.advanceTimersByTime(20));
+ expect(region?.className).toContain("invisible");
+
+ act(() => {
+ terminalHarness.writeCallbacks.shift()?.();
+ vi.advanceTimersByTime(20);
+ });
+ expect(region?.className).not.toContain("invisible");
+ });
});
describe("pane keyboard", () => {
diff --git a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
index ae6a9c8d78..1d95b5bf49 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/AgenticTerminal.tsx
@@ -424,6 +424,11 @@ export function AgenticTerminal({
* completion may reveal the pane.
*/
const replayCurtainRef = useRef(false);
+ // Every replay and active-stage transition invalidates callbacks/frames from
+ // the one before it. A boolean alone cannot distinguish "A finished" from
+ // "B is still rebuilding", so an old callback could otherwise reveal B.
+ const replayGenerationRef = useRef(0);
+ const replayRevealFrameRef = useRef<number | undefined>(undefined);
/**
* The delivery this pane is currently showing a receipt for, if any.
*
@@ -494,7 +499,12 @@ export function AgenticTerminal({
// down between a replay dropping the curtain and its write callback (see
// `replayToPane`), which would leave the new terminal invisible with
// nothing left to lift it.
+ replayGenerationRef.current += 1;
replayCurtainRef.current = false;
+ if (replayRevealFrameRef.current !== undefined) {
+ cancelAnimationFrame(replayRevealFrameRef.current);
+ replayRevealFrameRef.current = undefined;
+ }
setTailReady(activeRef.current);
const linkOptions = {
@@ -860,6 +870,12 @@ export function AgenticTerminal({
*/
const replayToPane = (text: string) => {
if (!text) return;
+ const generation = replayGenerationRef.current + 1;
+ replayGenerationRef.current = generation;
+ if (replayRevealFrameRef.current !== undefined) {
+ cancelAnimationFrame(replayRevealFrameRef.current);
+ replayRevealFrameRef.current = undefined;
+ }
// Parked output belongs to the screen this replay REPLACES, and it was
// captured before it. Written afterwards it would paint the older screen
// over the newer one; written before, it would be reset away regardless.
@@ -888,6 +904,7 @@ export function AgenticTerminal({
// is parked and un-parked by the same rules as anything else — and so it
// counts as the pane having painted.
writeToPane(text, () => {
+ if (generation !== replayGenerationRef.current) return;
// Cleared unconditionally: a callback that fired while the pane was
// away must not leave the stage-switch settle step refusing to lift
// the curtain forever.
@@ -897,8 +914,15 @@ export function AgenticTerminal({
// prompt, never several screens above it.
term.scrollToBottom?.();
if (!curtain) return;
- requestAnimationFrame(() => {
- if (disposed || !activeRef.current) return;
+ replayRevealFrameRef.current = requestAnimationFrame(() => {
+ replayRevealFrameRef.current = undefined;
+ if (
+ disposed ||
+ !activeRef.current ||
+ generation !== replayGenerationRef.current
+ ) {
+ return;
+ }
term.scrollToBottom?.();
setTailReady(true);
});
@@ -1204,6 +1228,12 @@ export function AgenticTerminal({
return () => {
disposed = true;
+ replayGenerationRef.current += 1;
+ replayCurtainRef.current = false;
+ if (replayRevealFrameRef.current !== undefined) {
+ cancelAnimationFrame(replayRevealFrameRef.current);
+ replayRevealFrameRef.current = undefined;
+ }
// Before the terminal is disposed below: a deadline flush one tick later
// would write into it after it is gone.
cancelHoldTimer();
@@ -1246,6 +1276,15 @@ export function AgenticTerminal({
* hidden siblings stay parked even when a prompt is addressed to them.
*/
useLayoutEffect(() => {
+ // A stage change supersedes every reveal owned by the stage before it. If
+ // a replay is still parsing, the active branch's queue barrier below waits
+ // for it and becomes the only path allowed to lift the new curtain.
+ replayGenerationRef.current += 1;
+ replayCurtainRef.current = false;
+ if (replayRevealFrameRef.current !== undefined) {
+ cancelAnimationFrame(replayRevealFrameRef.current);
+ replayRevealFrameRef.current = undefined;
+ }
if (!active) {
setTailReady(false);
visibilityRef.current?.park();