Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/cli/ai/sessions/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export function replaySessionHistory( ui: AiChatUI, entries: SessionEntry[] ): v
}

if ( isStudioCustomEntryOfType( entry, 'studio.tool_progress' ) ) {
if ( entry.data ) ui.setLoaderMessage( entry.data.message );
// Tool progress is ephemeral UI state (loader text) with no value
// when rehydrating history: `finishReplay()` clears the loader at
// the end anyway. Replaying it one entry at a time is the bottleneck
// behind the "Resuming session…" hang on sessions that persisted
// tens of thousands of progress ticks, so skip it during replay.
// See https://github.com/Automattic/studio/issues/3865
continue;
}

Expand Down
84 changes: 84 additions & 0 deletions apps/cli/ai/sessions/tests/replay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, it, vi } from 'vitest';
import { replaySessionHistory } from '../replay';
import type { SessionEntry } from '@earendil-works/pi-coding-agent';
import type { AiChatUI } from 'cli/ai/ui';

function createUiSpy() {
return {
prepareForReplay: vi.fn(),
setReplayTimestamp: vi.fn(),
setActiveSite: vi.fn(),
beginAgentTurn: vi.fn(),
addUserMessage: vi.fn(),
endAgentTurn: vi.fn(),
setLoaderMessage: vi.fn(),
showAgentQuestion: vi.fn(),
renderToolResults: vi.fn(),
handleEvent: vi.fn(),
finishReplay: vi.fn(),
};
}

function toolProgress( message: string, timestamp: string ): SessionEntry {
return {
type: 'custom',
customType: 'studio.tool_progress',
timestamp,
data: { message },
} as SessionEntry;
}

describe( 'replaySessionHistory', () => {
it( 'does not replay persisted studio.tool_progress entries', () => {
const ui = createUiSpy();
const entries: SessionEntry[] = Array.from( { length: 5000 }, ( _, i ) =>
toolProgress(
`progress ${ i }`,
`2026-06-01T00:00:${ String( i % 60 ).padStart( 2, '0' ) }.000Z`
)
);

replaySessionHistory( ui as unknown as AiChatUI, entries );

expect( ui.setLoaderMessage ).not.toHaveBeenCalled();
expect( ui.prepareForReplay ).toHaveBeenCalledTimes( 1 );
expect( ui.finishReplay ).toHaveBeenCalledTimes( 1 );
} );

it( 'replays the conversation while ignoring interleaved progress', () => {
const ui = createUiSpy();
const entries: SessionEntry[] = [
{
type: 'custom',
customType: 'studio.user_prompt',
timestamp: '2026-06-01T00:00:00.000Z',
data: { source: 'prompt', text: 'Create a landing page' },
} as SessionEntry,
toolProgress( 'Reading files…', '2026-06-01T00:00:01.000Z' ),
toolProgress( 'Running WP-CLI…', '2026-06-01T00:00:02.000Z' ),
{
type: 'message',
timestamp: '2026-06-01T00:00:03.000Z',
message: {
role: 'assistant',
content: [ { type: 'text', text: 'Done.' } ],
},
} as SessionEntry,
{
type: 'custom',
customType: 'studio.turn_closed',
timestamp: '2026-06-01T00:00:04.000Z',
data: { status: 'success' },
} as SessionEntry,
];

replaySessionHistory( ui as unknown as AiChatUI, entries );

expect( ui.setLoaderMessage ).not.toHaveBeenCalled();
expect( ui.addUserMessage ).toHaveBeenCalledWith( 'Create a landing page' );
expect( ui.beginAgentTurn ).toHaveBeenCalledTimes( 1 );
expect( ui.handleEvent ).toHaveBeenCalledTimes( 1 );
expect( ui.endAgentTurn ).toHaveBeenCalledTimes( 1 );
expect( ui.finishReplay ).toHaveBeenCalledTimes( 1 );
} );
} );
Loading