Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {useMemo, useState} from 'react';
import {MessageRow, ThinkingBlock} from '@sentry/scraps/chat';

import {type AutofixSection} from 'sentry/components/events/autofix/useExplorerAutofix';
import {defined} from 'sentry/utils/defined';
import {
groupTranscript,
ResponseGroup,
Expand All @@ -27,7 +28,7 @@ export function ArtifactLoadingDetails({
return (
<ArtifactDetails>
{transcript.responses.length ? (
<StepResponses {...transcript} />
<StepResponses {...transcript} blocks={blocks} />
) : (
<MessageRow from="assistant" density="compact">
<ThinkingBlock title={loadingMessage} startTime={appearedAt} />
Expand Down Expand Up @@ -55,7 +56,7 @@ export function ArtifactPartialDetails({blocks}: {blocks: AutofixSection['blocks

return (
<ArtifactDetails>
<StepResponses {...transcript} />
<StepResponses {...transcript} blocks={blocks} />
</ArtifactDetails>
);
}
Expand All @@ -73,17 +74,31 @@ function useStepTranscript(blocks: AutofixSection['blocks']) {
}

/**
* A step's responses, the way the Seer Agent chat draws them.
* A step's responses, the way the Seer Agent chat draws them. `blocks` is the
* step's full list, including the error blocks left out of `responses`.
*/
function StepResponses({responses, latestTodos}: ReturnType<typeof useStepTranscript>) {
return responses.map(segment => (
<ResponseGroup
key={segment.indices[0]}
group={segment.blocks}
blockIndex={segment.indices[0]!}
latestTodos={latestTodos}
readOnly
showThinking
/>
));
function StepResponses({
blocks,
responses,
latestTodos,
}: ReturnType<typeof useStepTranscript> & {blocks: AutofixSection['blocks']}) {
return responses.map(segment => {
const lastBlock = segment.blocks.at(-1);
const nextBlock = defined(lastBlock)
? blocks[blocks.indexOf(lastBlock) + 1]
: undefined;
return (
<ResponseGroup
key={segment.indices[0]}
group={segment.blocks}
blockIndex={segment.indices[0]!}
latestTodos={latestTodos}
readOnly
showThinking
// The timestamp of the block after the segment: the user's next message or, in a
// stopped step, Seer's error block. None when nothing follows, as while it runs.
endedAt={nextBlock?.timestamp}
/>
);
});
}
252 changes: 252 additions & 0 deletions static/app/components/events/autofix/v3/autofixCards.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ function makePrIterationBlock(
};
}

function makeToolUseBlock(
id: string,
content: string,
timestamp: string,
metadata?: Record<string, string>
): AutofixSection['blocks'][number] {
return {
id,
timestamp,
message: {
role: 'tool_use',
content,
tool_calls: [{id: `${id}-call`, function: 'telemetry_live_search', args: '{}'}],
metadata,
},
};
}

function makeUserBlock(
id: string,
content: string,
timestamp: string,
metadata?: Record<string, string>
): AutofixSection['blocks'][number] {
return {id, timestamp, message: {role: 'user', content, metadata}};
}

function makePatch(repoName: string, path: string): ExplorerFilePatch {
return {
repo_name: repoName,
Expand Down Expand Up @@ -341,6 +368,57 @@ describe('ArtifactCard', () => {
expect(screen.queryByText('Thinking...')).not.toBeInTheDocument();
});

it("ends a response at the user's message while a single run's root cause is still running", () => {
render(
<RootCauseCard
autofix={{
...mockAutofix,
runState: {
run_id: 123,
blocks: [],
status: 'processing',
updated_at: '2026-01-01T00:00:00Z',
feature_id: 'autofix_single_run',
},
}}
groupId="1"
section={makeSection(
'root_cause',
'processing',
[],
[
makeToolUseBlock(
'block-1',
'Reading the cache module.',
'2026-01-01T00:00:00Z',
{step: 'single_run'}
),
makeUserBlock(
'user-1',
'Also check the retry path.',
'2026-01-01T00:00:05Z',
{step: 'single_run'}
),
{
...makeToolUseBlock(
'block-2',
'Reading the retry module.',
'2026-01-01T00:00:10Z'
),
loading: true,
},
]
)}
/>
);

expect(screen.getByText('5.0s')).toBeInTheDocument();
// Only the replied-to response collapses to its summary.
expect(
screen.getAllByRole('button', {name: /See thinking and tool calls/})
).toHaveLength(1);
});

it('starts a new single run from Re-run', async () => {
const startStepMock = jest.fn().mockResolvedValue({runId: 777, isSingleRun: true});
render(
Expand Down Expand Up @@ -2766,6 +2844,9 @@ describe('out of credits', () => {
updated_at: '2026-01-01T00:00:00Z',
};

// Only single runs take replies inside a step.
const singleRunStoppedState = {...stoppedRunState, feature_id: 'autofix_single_run'};

const rootCauseSection = makeOutOfCreditsSection('root_cause', [
makeBlock('block-1', 'The cache key omits the org slug.', {step: 'root_cause'}),
seerErrorBlock,
Expand Down Expand Up @@ -2832,6 +2913,177 @@ describe('out of credits', () => {
});
});

describe("a stopped step's transcript", () => {
it("ends the timer at Seer's error block when a PR iteration stopped", () => {
mockCreditCheck('autofix_pr_iteration', false);
render(
<CodeChangesCard
autofix={{
...mockAutofix,
runState: {...stoppedRunState, repo_pr_states: {'org/repo': makePR()}},
}}
groupId="1"
section={makeOutOfCreditsSection(
'code_changes',
[
makeBlock('block-1', 'Applied the fix.', {step: 'code_changes'}),
{
...makePrIterationBlock(0, {text: 'Please add a test'}),
timestamp: '2026-01-01T00:00:05Z',
},
makeToolUseBlock('block-2', 'Adding the test.', '2026-01-01T00:00:10Z'),
{...seerErrorBlock, timestamp: '2026-01-01T00:00:15Z'},
],
[[makePatch('org/repo', 'src/cart.py')]]
)}
/>,
{organization}
);

expect(screen.getByText('5.0s')).toBeInTheDocument();
expect(
screen.getByRole('button', {name: /See thinking and tool calls/})
).toHaveAttribute('aria-expanded', 'false');
});

it("ends the timer at Seer's error block when the plan stopped", () => {
mockCreditCheck('autofix_solution', false);
render(
<SolutionCard
autofix={{...mockAutofix, runState: stoppedRunState}}
section={makeOutOfCreditsSection('solution', [
makeToolUseBlock(
'block-1',
'Reading the cache module.',
'2026-01-01T00:00:00Z',
{step: 'solution'}
),
{...seerErrorBlock, timestamp: '2026-01-01T00:00:05Z'},
])}
/>,
{organization}
);

expect(screen.getByText('5.0s')).toBeInTheDocument();
});

it("ends the timer at the user's message when a single run's root cause stopped right after it", () => {
mockCreditCheck('autofix_root_cause', false);
render(
<RootCauseCard
autofix={{...mockAutofix, runState: singleRunStoppedState}}
groupId="1"
section={makeOutOfCreditsSection('root_cause', [
makeToolUseBlock(
'block-1',
'Reading the cache module.',
'2026-01-01T00:00:00Z',
{step: 'single_run'}
),
makeUserBlock(
'user-1',
'Also check the retry path.',
'2026-01-01T00:00:05Z',
{step: 'single_run'}
),
{...seerErrorBlock, timestamp: '2026-01-01T00:00:10Z'},
])}
/>,
{organization}
);

expect(screen.getByText('5.0s')).toBeInTheDocument();
});

it("ends each response at the block after it when a single run's root cause stopped", () => {
mockCreditCheck('autofix_root_cause', false);
render(
<RootCauseCard
autofix={{...mockAutofix, runState: singleRunStoppedState}}
groupId="1"
section={makeOutOfCreditsSection('root_cause', [
makeToolUseBlock(
'block-1',
'Reading the cache module.',
'2026-01-01T00:00:00Z',
{step: 'single_run'}
),
makeUserBlock(
'user-1',
'Also check the retry path.',
'2026-01-01T00:00:05Z',
{step: 'single_run'}
),
makeToolUseBlock(
'block-2',
'Searching for the cache key.',
'2026-01-01T00:00:10Z'
),
makeToolUseBlock(
'block-3',
'Reading the retry module.',
'2026-01-01T00:00:15Z'
),
{...seerErrorBlock, timestamp: '2026-01-01T00:00:20Z'},
])}
/>,
{organization}
);

// The earlier response ends at the user's message, the last one at Seer's error block.
expect(screen.getByText('5.0s')).toBeInTheDocument();
expect(screen.getByText('10.0s')).toBeInTheDocument();
for (const button of screen.getAllByRole('button', {
name: /See thinking and tool calls/,
})) {
expect(button).toHaveAttribute('aria-expanded', 'false');
}
});

it("ends a response at a hidden earlier error block when a single run's root cause stopped", () => {
mockCreditCheck('autofix_root_cause', false);
render(
<RootCauseCard
autofix={{...mockAutofix, runState: singleRunStoppedState}}
groupId="1"
section={makeOutOfCreditsSection('root_cause', [
makeToolUseBlock(
'block-1',
'Reading the cache module.',
'2026-01-01T00:00:00Z',
{step: 'single_run'}
),
{
...makeBlock('error-0', 'Sorry, I encountered an unexpected error.'),
timestamp: '2026-01-01T00:00:05Z',
},
makeUserBlock('user-1', 'Try again.', '2026-01-01T00:00:10Z', {
step: 'single_run',
}),
makeToolUseBlock(
'block-2',
'Searching for the cache key.',
'2026-01-01T00:00:15Z'
),
makeToolUseBlock(
'block-3',
'Reading the retry module.',
'2026-01-01T00:00:20Z'
),
{...seerErrorBlock, timestamp: '2026-01-01T00:00:25Z'},
])}
/>,
{organization}
);

expect(
screen.queryByText('Sorry, I encountered an unexpected error.')
).not.toBeInTheDocument();
// The earlier response ends at its error, not at the user's message after it.
expect(screen.getByText('5.0s')).toBeInTheDocument();
});
});

it('keeps what the plan step wrote and disables Re-run while out of credits', async () => {
mockCreditCheck('autofix_solution', false);
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,26 @@ describe('ResponseGroup', () => {
);
});

it('ends the timer at the answer when given a later end time', () => {
const group = [
{...toolUseBlock('t1'), timestamp: '2024-01-01T00:00:00Z'},
{...assistantBlock('a1', 'Done'), timestamp: '2024-01-01T00:00:05Z'},
];

render(
<ResponseGroup
group={group}
blockIndex={1}
latestTodos={findLatestTodos(group)}
showThinking
endedAt="2024-01-01T00:00:10Z"
/>,
{organization}
);

expect(screen.getByText('5.0s')).toBeInTheDocument();
});

it('spins inside the box while a tool works', () => {
const group = [toolUseBlock('t1', {loading: true})];

Expand Down
Loading
Loading