Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Stamp tool durations from execution start, not model arg-streaming start

rcourtman1ba8aa0249b0
diff --git a/frontend-modern/src/components/AI/Chat/MessageItem.tsx b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
index 06b86c4dce..43e1b2c784 100644
--- a/frontend-modern/src/components/AI/Chat/MessageItem.tsx
+++ b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
@@ -740,6 +740,7 @@ export const MessageItem: Component<MessageItemProps> = (props) => {
{(tool) => (
<ToolExecutionBlock
startedAt={evt()?.startedAt}
+ runningAt={evt()?.runningAt}
completedAt={evt()?.updatedAt}
live={props.message.isStreaming}
settleUntil={evt()?.settleUntil}
diff --git a/frontend-modern/src/components/AI/Chat/ToolExecutionBlock.tsx b/frontend-modern/src/components/AI/Chat/ToolExecutionBlock.tsx
index 03b8b4a939..4dd23087c9 100644
--- a/frontend-modern/src/components/AI/Chat/ToolExecutionBlock.tsx
+++ b/frontend-modern/src/components/AI/Chat/ToolExecutionBlock.tsx
@@ -32,6 +32,10 @@ import { getAssistantFastToolCompletionSettleUntil } from './streamActivityTimin
interface ToolExecutionBlockProps {
tool: ToolExecution;
startedAt?: number;
+ // When execution actually began. startedAt marks the model starting to
+ // stream tool arguments; on slow routes that can run minutes before the
+ // tool runs, and the duration stamp must not bill it to the tool.
+ runningAt?: number;
completedAt?: number;
live?: boolean;
settleUntil?: number;
@@ -384,7 +388,9 @@ export const ToolExecutionBlock: Component<ToolExecutionBlockProps> = (props) =>
});
const durationLabel = createMemo(() =>
- settlingFastCompletion() ? '' : formatCompletedToolDuration(props.startedAt, props.completedAt),
+ settlingFastCompletion()
+ ? ''
+ : formatCompletedToolDuration(props.runningAt ?? props.startedAt, props.completedAt),
);
const statusLabel = () =>
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/ToolExecutionBlock.test.tsx b/frontend-modern/src/components/AI/Chat/__tests__/ToolExecutionBlock.test.tsx
index dc75777027..8feac4ba37 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/ToolExecutionBlock.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/ToolExecutionBlock.test.tsx
@@ -132,6 +132,22 @@ describe('ToolExecutionBlock', () => {
expect(screen.getByLabelText('Tool duration <1s')).toHaveTextContent('<1s');
});
+ it('stamps duration from execution start, not model arg-streaming start', () => {
+ // The model spent ~2 minutes streaming arguments (startedAt) before the
+ // tool actually ran for 2s (runningAt → completedAt). The visible stamp
+ // must bill the tool only for its execution.
+ render(() => (
+ <ToolExecutionBlock
+ tool={makeTool()}
+ startedAt={1_000}
+ runningAt={129_000}
+ completedAt={131_000}
+ />
+ ));
+
+ expect(screen.getByLabelText('Tool duration 2s')).toHaveTextContent('2s');
+ });
+
it('briefly presents fresh live fast completions as running before settling', async () => {
vi.useFakeTimers();
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
index 190849deb1..33d1a12733 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
@@ -2081,6 +2081,7 @@ describe('useChat', () => {
status: 'running',
progress: undefined,
startedAt: expect.any(Number),
+ runningAt: expect.any(Number),
updatedAt: expect.any(Number),
});
expect(assistant.streamEvents).toContainEqual(
@@ -2094,6 +2095,40 @@ describe('useChat', () => {
dispose();
});
+ it('anchors execution start on the running phase and carries it onto the completed tool row', async () => {
+ const { getFireEvent } = setupWithEventCapture();
+ const { value: chat, dispose } = withRoot(() => useChat({ sessionId: 's' }));
+
+ await chat.sendMessage('hi');
+ const fire = getFireEvent();
+
+ // Model is still streaming arguments: no execution start yet.
+ fire({
+ type: 'tool_start',
+ data: { id: 'tool-1', name: 'pulse_query', input: '', phase: 'pending' },
+ });
+ let assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ expect(assistant.pendingTools![0].runningAt).toBeUndefined();
+
+ // Execution begins.
+ fire({
+ type: 'tool_progress',
+ data: { id: 'tool-1', name: 'pulse_query', input: '{}', phase: 'running' },
+ });
+ assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ const runningAt = assistant.pendingTools![0].runningAt;
+ expect(runningAt).toEqual(expect.any(Number));
+
+ fire({
+ type: 'tool_end',
+ data: { id: 'tool-1', name: 'pulse_query', input: '{}', output: 'ok', success: true },
+ });
+ assistant = chat.messages().find((m) => m.role === 'assistant')!;
+ const completedEvent = (assistant.streamEvents || []).find((evt) => evt.type === 'tool');
+ expect(completedEvent?.runningAt).toBe(runningAt);
+ dispose();
+ });
+
it('uses tool_start status and progress as immediate live activity', async () => {
const { getFireEvent } = setupWithEventCapture();
const { value: chat, dispose } = withRoot(() => useChat({ sessionId: 's' }));
diff --git a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
index 046918ebaf..511adda839 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -834,15 +834,17 @@ export function useChat(options: UseChatOptions = {}) {
continue;
}
replacedTool = true;
+ const mergedStatus = data.phase ? status : tool.status || status;
resolvedTool = {
...tool,
id: data.id || tool.id,
name: data.name || tool.name,
input: data.input || tool.input,
rawInput: data.raw_input || tool.rawInput,
- status: data.phase ? status : tool.status || status,
+ status: mergedStatus,
progress: progress || tool.progress,
startedAt: tool.startedAt || now,
+ runningAt: tool.runningAt || (mergedStatus === 'running' ? now : undefined),
updatedAt: now,
};
updatedPendingTools.push(resolvedTool);
@@ -857,6 +859,7 @@ export function useChat(options: UseChatOptions = {}) {
status,
progress,
startedAt: now,
+ runningAt: status === 'running' ? now : undefined,
updatedAt: now,
};
updatedPendingTools.push(resolvedTool);
@@ -908,6 +911,7 @@ export function useChat(options: UseChatOptions = {}) {
status,
progress: progress || tool.progress,
startedAt: tool.startedAt || now,
+ runningAt: tool.runningAt || (status === 'running' ? now : undefined),
updatedAt: now,
});
@@ -1588,6 +1592,7 @@ export function useChat(options: UseChatOptions = {}) {
tool: newToolCall,
toolId: completedToolId,
startedAt: resolvedPendingTool?.startedAt,
+ runningAt: resolvedPendingTool?.runningAt,
updatedAt: completedAt,
settleUntil,
});
@@ -1616,6 +1621,7 @@ export function useChat(options: UseChatOptions = {}) {
tool: newToolCall,
toolId: completedToolId,
startedAt,
+ runningAt: evt.pendingTool?.runningAt || evt.runningAt,
updatedAt: completedAt,
settleUntil,
};
diff --git a/frontend-modern/src/components/AI/Chat/types.ts b/frontend-modern/src/components/AI/Chat/types.ts
index 87b89c7774..a7c993ae58 100644
--- a/frontend-modern/src/components/AI/Chat/types.ts
+++ b/frontend-modern/src/components/AI/Chat/types.ts
@@ -22,6 +22,10 @@ export interface PendingTool {
status?: 'pending' | 'running' | 'waiting';
progress?: string;
startedAt?: number;
+ // When execution actually began (first 'running' phase). startedAt marks
+ // the model starting to stream tool arguments, which can run minutes ahead
+ // of execution on slow routes — durations must not bill that to the tool.
+ runningAt?: number;
updatedAt?: number;
}
@@ -122,6 +126,9 @@ export interface StreamDisplayEvent {
tool?: ToolExecution;
pendingTool?: PendingTool;
toolCancel?: ToolCancellation;
+ // Execution start carried from the pending tool onto the completed row so
+ // the duration stamp reflects execution, not model arg-streaming.
+ runningAt?: number;
content?: string;
model?: string;
failedModel?: string;