Skip to content

Replay your coding agent sessions as a map and a timeline

fix(viewer): keep partial playback progress across a speed change

adityajha2005df4882ce5f67
diff --git a/viewer/src/core/playback.test.ts b/viewer/src/core/playback.test.ts
index ccc4bb7..68c264d 100644
--- a/viewer/src/core/playback.test.ts
+++ b/viewer/src/core/playback.test.ts
@@ -125,6 +125,19 @@ describe('playbackClock', () => {
expect(SPEEDS[s.speedIdx]).toBe(16);
expect(clocked(s).frames(1000, 1000 + MS_PER_EVENT).state.p).toBe(26);
});
+
+ it('keeps partial progress across a speed change when given the same carry', () => {
+ const carry = { last: 0, acc: 0 };
+ const actions: PlaybackAction[] = [];
+ // Half an event at 1x, then a 4x clock picks up where it left off.
+ const slow = playbackClock(0, (a) => actions.push(a), carry);
+ slow(1000);
+ slow(1000 + MS_PER_EVENT / 2);
+ expect(actions).toEqual([]);
+ const fast = playbackClock(1, (a) => actions.push(a), carry);
+ fast(1000 + MS_PER_EVENT / 2 + event4x / 2);
+ expect(actions).toEqual([{ type: 'advance', by: 1 }]);
+ });
});
describe('replayKeyAction', () => {
diff --git a/viewer/src/core/playback.ts b/viewer/src/core/playback.ts
index e8efc9e..b6d5a92 100644
--- a/viewer/src/core/playback.ts
+++ b/viewer/src/core/playback.ts
@@ -76,19 +76,24 @@ export function playbackReducer(
}
}
+/** The clock's progress; pass the same one to a new clock to keep it across a speed change. */
+export interface ClockCarry {
+ last: number;
+ acc: number;
+}
+
export function playbackClock(
speedIdx: number,
- dispatch: (a: PlaybackAction) => void
+ dispatch: (a: PlaybackAction) => void,
+ carry: ClockCarry = { last: 0, acc: 0 }
) {
- let last = 0;
- let acc = 0;
return (t: number) => {
- const dt = last ? Math.min(t - last, MAX_FRAME_MS) : 0;
- last = t;
- acc += (dt * SPEEDS[speedIdx]) / MS_PER_EVENT;
- const by = Math.floor(acc);
+ const dt = carry.last ? Math.min(t - carry.last, MAX_FRAME_MS) : 0;
+ carry.last = t;
+ carry.acc += (dt * SPEEDS[speedIdx]) / MS_PER_EVENT;
+ const by = Math.floor(carry.acc);
if (!by) return;
- acc -= by;
+ carry.acc -= by;
dispatch({ type: 'advance', by });
};
}
diff --git a/viewer/src/explorer/usePlaybackWindow.ts b/viewer/src/explorer/usePlaybackWindow.ts
index e9072aa..2978a9a 100644
--- a/viewer/src/explorer/usePlaybackWindow.ts
+++ b/viewer/src/explorer/usePlaybackWindow.ts
@@ -3,6 +3,7 @@ import {
useEffect,
useMemo,
useReducer,
+ useRef,
type KeyboardEvent,
} from 'react';
import type { SessionMap, SessionMapEvent } from '@/core/schema';
@@ -64,9 +65,15 @@ export function usePlaybackWindow(map: SessionMap) {
})
);
+ // Progress survives a speed change but not a pause, seek or step (all of
+ // which stop playback), so this resets only when playing flips.
+ const carry = useRef({ last: 0, acc: 0 });
+ useEffect(() => {
+ carry.current = { last: 0, acc: 0 };
+ }, [pb.playing]);
useEffect(() => {
if (!pb.playing) return;
- const tick = playbackClock(pb.speedIdx, dispatch);
+ const tick = playbackClock(pb.speedIdx, dispatch, carry.current);
let raf = 0;
const frame = (t: number) => {
tick(t);