From 85dfe26a0028bfa3faff7036baa884e1b176da20 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2026 08:11:48 +0000 Subject: [PATCH 1/4] feat: use generated ToolCallSummary type for persisted tool-call schema (#19585) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Replaces the interim hand-written `zPersistedToolCallSummary` zod schema in `agentApiSchema.ts` with the generated `zToolCallSummary` from `@comfyorg/ingest-types/zod`, now that [Comfy-Org/cloud#10360](https://github.com/Comfy-Org/cloud/pull/10360) added the `ToolCallSummary` OpenAPI schema for persisted tool-call history. ## Base branch note `main` does not yet have `ToolCallSummary` in `packages/ingest-types` — the automated type-update PR that adds it, #19526, is still open (unmerged) at the time of this PR. Its single commit (`c819ad46`) is a pure, isolated `packages/ingest-types` regen with no conflicts against `main`'s tip, so I cherry-picked just that commit onto a fresh branch off `main` rather than basing on #19526's own branch, to keep this PR's history clean and independent of that bot-managed branch's future churn. If #19526 merges first, this PR's first commit becomes a no-op on rebase; if this merges first, #19526 will need a trivial rebase. ## Changes - **What**: - `zPersistedToolCallSummary` now aliases the generated `zToolCallSummary` directly instead of a hand-rolled permissive schema. - Fixed a codegen gap this surfaced: `AgentMessage.content`'s generated **TS type** keeps a `[key: string]: unknown` index signature, but its generated **zod schema** dropped `z.record(z.unknown())` for a bare `{ tool_calls }` object with no `.passthrough()` — which would otherwise reject every other `content` field (`text`, `attachments`, `workflow_references`, ...) this file relies on. Added a local `zAgentMessageContent` (tool_calls typed via `zToolCallSummary`, `.passthrough()` for the rest) to bridge it. - Updated `agentTranscript.ts`'s `parseToolCallEntry`/`toolCallPartState`/`toolCallOk` now that `tool_call_id` is guaranteed present and `status` is closed to `'success' | 'error'` (previously `z.string().optional()` with an `id`-fallback for pre-`tool_call_id` rows). - Updated test fixtures in `agentTranscript.test.ts`, `agentEventTransport.test.ts`, and `agentConversationStore.test.ts` to match the tightened contract (valid fixtures now include `tool_call_id` + `'success'/'error'` status; fixtures deliberately testing malformed/legacy shapes now assert the entry is **dropped** rather than rendered with a broadened status vocabulary, matching the generated schema's own docstring: "a row a dead turn left in pending/running ... is dropped rather than shown as a perpetual-progress chip"). - **Breaking**: None externally — this is FE-internal parsing of already-persisted rows. Behavior only changes for the never-before-possible edge cases the interim schema was defensively permissive about (a persisted row missing `tool_call_id`, or carrying a non-terminal/unrecognized `status`): those are now dropped instead of rendered, matching what the finalized backend contract guarantees can no longer occur. - **Dependencies**: None new — `@comfyorg/ingest-types` is an existing workspace dependency; only its generated content changed (via cherry-picked commit). ## Before / After **Before** (`agentApiSchema.ts`): ```ts export const zPersistedToolCallSummary = z .object({ id: z.string(), tool_call_id: z.string().optional(), tool_name: z.string(), status: z.string().optional(), duration_ms: z.unknown().optional() }) .passthrough() ``` **After**: ```ts import { zToolCallSummary } from '@comfyorg/ingest-types/zod' // ... export const zPersistedToolCallSummary = zToolCallSummary ``` ## Review Focus - The codegen-gap fix (`zAgentMessageContent`) is the part most worth a second look — it's necessary for anything to compile/parse once `content`'s zod schema tightened, but it's patching a mismatch between the OpenAPI-generated TS type and its zod counterpart rather than something this PR set out to change. Cloud/codegen maintainers may want to fix this upstream (route `additionalProperties`/index-signature cases through `.passthrough()` in the zod plugin) so future consumers don't need this kind of local patch. - `tool_call_id` is now required and `status` is a closed `'success' | 'error'` enum — I removed the old `id`-fallback and broadened-status-vocabulary handling in `agentTranscript.ts` accordingly, trusting the backend's own docstring that only terminal, `tool_call_id`-bearing rows are ever persisted. Worth a sanity check against how `services/agent/internal/persist.ToolCallSummary` actually writes these rows. - Verification: `pnpm typecheck` (repo-wide, clean) and `pnpm test:unit` for every changed/affected file — `agentApiSchema.test.ts`, `agentTranscript.test.ts` (34 tests), `agentEventTransport.test.ts`, `agentConversationStore.test.ts` — all green. Did not run the full unit suite; a large, pre-existing and unrelated set of `.vue`-component test files fail in this environment with `Cannot find package '@vue/compiler-sfc'` even on unmodified `main` (verified via `git stash`), so that failure is not from this change. --- _Requested by **Christian Byrne** · #comfy-agent-user-feedback_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RuB3pFMNyS3AE7JYNFNesb --- _Generated by [Claude Code](https://claude.ai/code/session_01RuB3pFMNyS3AE7JYNFNesb)_ --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Christian Byrne --- .../agentToolCallHistoryPersistence.spec.ts | 5 +- packages/ingest-types/src/index.ts | 1 + packages/ingest-types/src/types.gen.ts | 34 +++- packages/ingest-types/src/zod.gen.ts | 22 +- .../agent/schemas/agentApiSchema.ts | 44 ++-- .../agent/agentEventTransport.test.ts | 2 +- .../services/agent/agentTranscript.test.ts | 191 ++++++++++++------ .../agent/services/agent/agentTranscript.ts | 38 ++-- .../agent/agentConversationStore.test.ts | 18 +- 9 files changed, 233 insertions(+), 122 deletions(-) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index ade70062713..934240e9c5a 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -92,13 +92,14 @@ test( tool_calls: [ { id: 'call-1', + tool_call_id: 'call-1', tool_name: 'search_nodes', - status: 'ok', + status: 'success', duration_ms: 420 }, { - // Deliberately 'success' (not 'ok', like call-1) to cover both terminal vocabularies. id: 'call-2', + tool_call_id: 'call-2', tool_name: 'add_node', status: 'success', duration_ms: 180 diff --git a/packages/ingest-types/src/index.ts b/packages/ingest-types/src/index.ts index 0c7f39a9fc7..439d8673aa1 100644 --- a/packages/ingest-types/src/index.ts +++ b/packages/ingest-types/src/index.ts @@ -1074,6 +1074,7 @@ export type { TeamCreditStopPrice, TeamCreditStops, TeamCreditStopSummary, + ToolCallSummary, UpdateAssetData, UpdateAssetError, UpdateAssetErrors, diff --git a/packages/ingest-types/src/types.gen.ts b/packages/ingest-types/src/types.gen.ts index 3f953b7126c..0ef7039b507 100644 --- a/packages/ingest-types/src/types.gen.ts +++ b/packages/ingest-types/src/types.gen.ts @@ -504,6 +504,35 @@ export type UpdateHubProfileRequest = { website_urls?: Array } +/** + * One persisted tool call attached to an assistant message's content.tool_calls (services/agent/internal/persist.ToolCallSummary), so a chat reload can render the tool-call history a turn produced. Display data only — raw arguments/results are never projected here. Only terminal rows (status ok/error) are ever surfaced; a row a dead turn left in pending/running has no wire-status mapping and is dropped rather than shown as a perpetual-progress chip. + */ +export type ToolCallSummary = { + /** + * Omitted when zero. + */ + duration_ms?: number + /** + * Present only when status is error. + */ + error_code?: string + finished_at?: string + /** + * This tool-call row's own primary key. + */ + id: string + started_at?: string + /** + * The WIRE status vocabulary (api/agent_events.schema.json's agent_tool_call), translated from the audit-row vocabulary (pending/running/ok/error) via ToolCallWireStatus — the same function the live agent_tool_call broadcast uses, so reloaded history and a live frame for the same call never disagree on the vocabulary. "running" never appears here: only terminal rows are queried, so a call still in flight when its turn died is omitted rather than shown as a perpetually in-progress chip. + */ + status: 'success' | 'error' + /** + * The provider tool-use id, the same id a live agent_tool_call WebSocket frame carries as tool_call_id — the frontend keys its tool chip on this value so a reload and a resumed live frame for the same call render as one chip, not two. Rows recorded before this id existed have none; id (this row's own primary key) is emitted instead, so the field is never empty, though it then cannot match any live frame. + */ + tool_call_id: string + tool_name: string +} + /** * Credit-stop ladder for the pricing slider (BE-1254). Returned by GET /api/billing/plans for every workspace regardless of the caller's token or workspace type (the personal/team distinction was removed); omitted only when the catalog defines no stops. */ @@ -1204,7 +1233,7 @@ export type ResubscribeRequest = { } /** - * The newest open renewal invoice of the workspace's Stripe subscription (active, or canceled but not yet ended). Returned only to workspace owners on the stripe billing rail while billing_status is payment_failed, and not while a payment for it is processing. hosted_invoice_url is a bearer payment link. + * The newest open renewal invoice of the workspace's Stripe subscription (active, or canceled but not yet ended). Returned only to workspace owners on the stripe billing rail while billing_status is payment_failed or paused, and not while a payment for it is processing. hosted_invoice_url is a bearer payment link. */ export type RenewalInvoice = { /** @@ -4740,9 +4769,10 @@ export type AgentPendingAsk = { */ export type AgentMessage = { /** - * Message payload. User turns carry {text, attachments?, attachment_refs?, workflow_references?}. Attachments are the input-image filenames from the request. attachment_refs is the server's own resolution of those same filenames to library assets, as {name, id?, kind?} objects, and exists so a later turn in the thread can reach an earlier turn's file — clients should keep reading attachments. workflow_references is an optional array of explicit non-target references, each with workflow_id and name (an empty string when no name was supplied). An optional unavailable: true records that the reference could not be authorized at turn start, without distinguishing unknown IDs, inaccessible workflows, or lookup failures. These entries preserve the user's reference intent without exposing workflow content; the frontend restores reference chips from this metadata. The field is omitted when there are no references. Assistant turns carry {text} — the final answer text (or error copy on a failed turn). Omitted when empty (e.g. an assistant message still streaming). Per-turn token accounting is NOT included here; it is surfaced on the agent_message_done WebSocket broadcast. + * Message payload. User turns carry {text, attachments?, attachment_refs?, workflow_references?}. Attachments are the input-image filenames from the request. attachment_refs is the server's own resolution of those same filenames to library assets, as {name, id?, kind?} objects, and exists so a later turn in the thread can reach an earlier turn's file — clients should keep reading attachments. workflow_references is an optional array of explicit non-target references, each with workflow_id and name (an empty string when no name was supplied). An optional unavailable: true records that the reference could not be authorized at turn start, without distinguishing unknown IDs, inaccessible workflows, or lookup failures. These entries preserve the user's reference intent without exposing workflow content; the frontend restores reference chips from this metadata. The field is omitted when there are no references. Assistant turns carry {text} — the final answer text (or error copy on a failed turn). Omitted when empty (e.g. an assistant message still streaming). Per-turn token accounting is NOT included here; it is surfaced on the agent_message_done WebSocket broadcast. tool_calls is an optional array of ToolCallSummary, attached to an assistant message that has persisted terminal (ok/error) tool-call rows — it lets a chat reload render the tool history a turn produced instead of showing nothing until the next live turn. Omitted when the message has no such rows. */ content?: { + tool_calls?: Array [key: string]: unknown } id: string diff --git a/packages/ingest-types/src/zod.gen.ts b/packages/ingest-types/src/zod.gen.ts index 8520f88cf3f..39875cbf3f1 100644 --- a/packages/ingest-types/src/zod.gen.ts +++ b/packages/ingest-types/src/zod.gen.ts @@ -334,6 +334,20 @@ export const zUpdateHubProfileRequest = z.object({ website_urls: z.array(z.string()).optional() }) +/** + * One persisted tool call attached to an assistant message's content.tool_calls (services/agent/internal/persist.ToolCallSummary), so a chat reload can render the tool-call history a turn produced. Display data only — raw arguments/results are never projected here. Only terminal rows (status ok/error) are ever surfaced; a row a dead turn left in pending/running has no wire-status mapping and is dropped rather than shown as a perpetual-progress chip. + */ +export const zToolCallSummary = z.object({ + duration_ms: z.number().int().optional(), + error_code: z.string().optional(), + finished_at: z.string().datetime().optional(), + id: z.string(), + started_at: z.string().datetime().optional(), + status: z.enum(['success', 'error']), + tool_call_id: z.string(), + tool_name: z.string() +}) + /** * Pre/post-discount price for a team credit stop, in cents. */ @@ -767,7 +781,7 @@ export const zResubscribeRequest = z.object({ }) /** - * The newest open renewal invoice of the workspace's Stripe subscription (active, or canceled but not yet ended). Returned only to workspace owners on the stripe billing rail while billing_status is payment_failed, and not while a payment for it is processing. hosted_invoice_url is a bearer payment link. + * The newest open renewal invoice of the workspace's Stripe subscription (active, or canceled but not yet ended). Returned only to workspace owners on the stripe billing rail while billing_status is payment_failed or paused, and not while a payment for it is processing. hosted_invoice_url is a bearer payment link. */ export const zRenewalInvoice = z.object({ amount_due: z.coerce @@ -2829,7 +2843,11 @@ export const zAgentPendingAsk = z.object({ * A persisted message in an agent thread. */ export const zAgentMessage = z.object({ - content: z.record(z.unknown()).optional(), + content: z + .object({ + tool_calls: z.array(zToolCallSummary).optional() + }) + .optional(), id: z.string(), pending_ask: zAgentPendingAsk.optional(), role: z.enum(['user', 'assistant', 'tool', 'system']), diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 54e43286d05..34c20121aee 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -7,6 +7,7 @@ import { zAgentRunMode as zGeneratedAgentRunMode, zAgentThreadListResponse as zGeneratedAgentThreadListResponse, zAgentTurnAccepted as zGeneratedAgentTurnAccepted, + zToolCallSummary, zWorkflowListResponse } from '@comfyorg/ingest-types/zod' import type { @@ -97,37 +98,32 @@ export type AgentRunModeValue = AgentRunModePreference['mode'] /** * One entry of a persisted assistant row's `content.tool_calls` (see * `agentTranscript.ts`'s `parseToolCallEntry`), the reload-path counterpart - * to the live WebSocket's `zAgentToolCallData` above. `status` is - * deliberately `z.string()` rather than a closed enum: an unrecognized value - * must still surface as a failed `ToolPart` (`toolCallOk` treats anything - * other than `pending`/`running`/`ok`/`success` as failure), so schema - * validation should reject a malformed *entry* (missing `id`/`tool_name`), - * not an unfamiliar *status* string or a bad `duration_ms` — `duration_ms` is - * `z.unknown().optional()` so a NaN/Infinity/negative value there doesn't - * sink the whole entry; `parseToolCallEntry` narrows it separately and just - * omits it. `status` is likewise `.optional()`: an entry that omits it - * entirely must still survive validation (`toolCallPartState`/`toolCallOk` - * already treat `undefined` as terminal-and-failed, matching the old - * parser's behavior for a status-less call). + * to the live WebSocket's `zAgentToolCallData` above. Sourced directly from + * the generated `ToolCallSummary` schema (Comfy-Org/cloud#10360) — the + * backend only ever persists terminal rows (`status: 'success' | 'error'`); + * a row a dead turn left in `pending`/`running` has no wire-status mapping + * and is dropped server-side rather than reaching this parser. */ -export const zPersistedToolCallSummary = z +export const zPersistedToolCallSummary = zToolCallSummary + +/** + * The generated `AgentMessage.content` schema narrows to just `tool_calls` + * (typed via `zToolCallSummary`), but the OpenAPI-generated TS type still + * carries a `[key: string]: unknown` index signature for it — the zod + * plugin's output didn't get a matching `.passthrough()`. Re-widened here so + * a persisted row's other `content` fields (`text`, `attachments`, + * `attachment_refs`, `workflow_references`, ...; see `agentTranscript.ts`) + * keep parsing. + */ +const zAgentMessageContent = z .object({ - id: z.string(), - // The provider tool-use id a LIVE `agent_tool_call` frame carries as - // `tool_call_id` (see `zAgentToolCallData` above). `parseToolCallEntry` - // prefers this over `id` when building `callId` so a restored `ToolPart` - // is keyed the same way a live frame for the same call will be, and can - // be updated in place rather than rendered as an unmatched duplicate. - // Optional: rows recorded before `tool_call_id` existed have none. - tool_call_id: z.string().optional(), - tool_name: z.string(), - status: z.string().optional(), - duration_ms: z.unknown().optional() + tool_calls: z.array(zToolCallSummary).optional() }) .passthrough() export const zAgentMessage = zGeneratedAgentMessage .extend({ + content: zAgentMessageContent.optional(), pending_ask: zAgentPendingAsk.optional() }) .passthrough() diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index 0ab64d12dad..e478a8140ec 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -433,7 +433,7 @@ describe('agentEventTransport text and tool parts', () => { id: 'audit-row-uuid-1', tool_call_id: 'call-1', tool_name: 'run', - status: 'running' + status: 'error' } ] } diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 649e263652e..3867f7c95fa 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -209,9 +209,10 @@ describe('normalizeAgentTranscript', () => { text: 'Done', tool_calls: [ { - id: 'call-1', + id: 'audit-row-uuid-1', + tool_call_id: 'call-1', tool_name: 'search_nodes', - status: 'ok', + status: 'success', duration_ms: 420 } ] @@ -232,7 +233,7 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('prefers tool_call_id over id when deriving callId, matching what live frames key on', () => { + it('keys callId on tool_call_id, matching what live frames key on', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', @@ -241,7 +242,7 @@ describe('normalizeAgentTranscript', () => { id: 'audit-row-uuid-1', tool_call_id: 'provider-call-1', tool_name: 'search_nodes', - status: 'ok' + status: 'success' } ] } @@ -260,20 +261,20 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('falls back to id for callId when a row was recorded before tool_call_id existed', () => { + it('drops a tool-call entry with no tool_call_id, matching the generated ToolCallSummary contract', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', tool_calls: [ - { id: 'audit-row-uuid-1', tool_name: 'search_nodes', status: 'ok' } + { id: 'audit-row-uuid-1', tool_name: 'search_nodes', status: 'success' } ] - } + } as unknown as AgentMessages[number]['content'] const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toContainEqual( - expect.objectContaining({ callId: 'audit-row-uuid-1' }) - ) + expect(transcript.messages[0].parts).toEqual([ + { type: 'text', text: 'Done', state: 'done' } + ]) }) it('omits tool parts entirely when a message carries no tool_calls', () => { @@ -291,8 +292,18 @@ describe('normalizeAgentTranscript', () => { message.content = { text: 'Done', tool_calls: [ - { id: 'call-1', tool_name: 'search_nodes', status: 'ok' }, - { id: 'call-2', tool_name: 'add_node', status: 'error' } + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success' + }, + { + id: 'audit-2', + tool_call_id: 'call-2', + tool_name: 'add_node', + status: 'error' + } ] } @@ -317,35 +328,37 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('clamps a restored pending/running tool call to done+ok:false with no live transport', () => { + it('drops a persisted tool call left pending/running by a dead turn, matching the backend contract that only terminal rows are ever persisted', () => { const message = row(1, 'assistant', 'turn-a', '', 'row-1') message.content = { tool_calls: [ - { id: 'call-1', tool_name: 'search_nodes', status: 'running' } + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'running' + } ] - } + } as unknown as AgentMessages[number]['content'] const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toEqual([ - { - type: 'tool', - callId: 'call-1', - name: 'search_nodes', - state: 'done', - ok: false - } - ]) + expect(transcript.messages[0].parts).toEqual([]) }) - it('keeps a pending/running tool call streaming when its row is the live run_approval ask', () => { + it('keeps the runApproval part and pending state for a live run_approval ask even though its stale pending/running tool call is dropped', () => { const message = row(1, 'assistant', 'turn-a', '', 'row-1') message.status = 'streaming' message.content = { tool_calls: [ - { id: 'call-1', tool_name: 'search_nodes', status: 'running' } + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'running' + } ] - } + } as unknown as AgentMessages[number]['content'] message.pending_ask = { message_id: 'row-1', ask_id: 'ask-1', @@ -359,22 +372,26 @@ describe('normalizeAgentTranscript', () => { const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toContainEqual({ - type: 'tool', - callId: 'call-1', - name: 'search_nodes', - state: 'streaming' - }) + expect(transcript.messages[0].parts).toEqual([ + { type: 'runApproval', askId: 'ask-1' } + ]) expect(transcript.pending?.messageId).toBe('row-1') }) - it.for(['success', 'failed', 'cancelled', 'timeout', 'unrecognized'])( - 'maps terminal tool-call status %s through the broadened vocabulary', + it.for(['success', 'error'] as const)( + 'maps terminal tool-call status %s to ok matching status === success', (status) => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', - tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status }] + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status + } + ] } const transcript = normalizeAgentTranscript([message]) @@ -389,17 +406,44 @@ describe('normalizeAgentTranscript', () => { } ) - it.for([NaN, Infinity, -Infinity, -1])( - 'rejects a non-finite or negative durationMs (%s)', + it('omits durationMs for a negative but otherwise valid duration_ms', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success', + duration_ms: -1 + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }) + }) + + it.for([NaN, Infinity, -Infinity])( + 'drops the whole tool-call entry when duration_ms is non-finite (%s), since the generated schema requires an integer', (durationMs) => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', tool_calls: [ { - id: 'call-1', + id: 'audit-1', + tool_call_id: 'call-1', tool_name: 'search_nodes', - status: 'ok', + status: 'success', duration_ms: durationMs } ] @@ -407,13 +451,9 @@ describe('normalizeAgentTranscript', () => { const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toContainEqual({ - type: 'tool', - callId: 'call-1', - name: 'search_nodes', - state: 'done', - ok: true - }) + expect(transcript.messages[0].parts).toEqual([ + { type: 'text', text: 'Done', state: 'done' } + ]) } ) @@ -422,12 +462,23 @@ describe('normalizeAgentTranscript', () => { message.content = { text: 'Done', tool_calls: [ - { id: 'call-1', tool_name: 'search_nodes', status: 'running' }, - { id: 'call-2', tool_name: 'add_node', status: 'ok' }, { - id: 'call-1', + id: 'audit-1', + tool_call_id: 'call-1', tool_name: 'search_nodes', - status: 'ok', + status: 'success' + }, + { + id: 'audit-2', + tool_call_id: 'call-2', + tool_name: 'add_node', + status: 'success' + }, + { + id: 'audit-3', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success', duration_ms: 420 } ] @@ -458,12 +509,17 @@ describe('normalizeAgentTranscript', () => { it.for([ { tool_calls: 'not-an-array' }, { tool_calls: [null, 17, 'a-string'] }, - { tool_calls: [{ tool_name: 'no_id', status: 'ok' }] }, - { tool_calls: [{ id: 'call-1', status: 'ok' }] }, + { tool_calls: [{ tool_name: 'no_id', status: 'success' }] }, + { + tool_calls: [{ id: 'call-1', tool_call_id: 'call-1', status: 'success' }] + }, { tool_calls: [] } ])('ignores malformed tool_calls metadata: $tool_calls', (content) => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') - message.content = { text: 'Done', ...content } + message.content = { + text: 'Done', + ...content + } as unknown as AgentMessages[number]['content'] const transcript = normalizeAgentTranscript([message]) @@ -475,14 +531,22 @@ describe('normalizeAgentTranscript', () => { it('dedupes a repeated callId across two assistant rows of the same turn', () => { const first = row(1, 'assistant', 'turn-a', '', 'row-1') first.content = { - tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status: 'ok' }] + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success' + } + ] } const second = row(2, 'assistant', 'turn-a', 'Done', 'row-2') second.content = { text: 'Done', tool_calls: [ { - id: 'call-1', + id: 'audit-2', + tool_call_id: 'call-1', tool_name: 'search_nodes', status: 'error', duration_ms: 900 @@ -504,23 +568,18 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('renders a persisted tool call with no status field as a failure, not a dropped entry', () => { + it('drops a persisted tool call entry with no status field, since status is required by the generated contract', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', - tool_calls: [{ id: 'call-1', tool_name: 'search_nodes' }] - } + tool_calls: [ + { id: 'audit-1', tool_call_id: 'call-1', tool_name: 'search_nodes' } + ] + } as unknown as AgentMessages[number]['content'] const transcript = normalizeAgentTranscript([message]) expect(transcript.messages[0].parts).toEqual([ - { - type: 'tool', - callId: 'call-1', - name: 'search_nodes', - state: 'done', - ok: false - }, { type: 'text', text: 'Done', state: 'done' } ]) }) diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index dc2e266fec8..c0062a07ccd 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -102,17 +102,13 @@ function parseUserWorkflowReferences( } /** - * A persisted tool-call status is `pending`/`running` while it was still in - * flight when the turn ended, and some terminal string otherwise (`ok`, - * `success`, `error`, `failed`, `cancelled`, `timeout`, ...) — the exact - * terminal vocabulary is not yet settled between the backend's persisted and - * live wire formats, so anything other than `pending`/`running` is treated as - * terminal here. - * - * A restored (non-live) row has no transport left to ever settle its tool - * parts, so a `pending`/`running` status there would otherwise spin forever; - * only `isLive` (the row is the one actively backed by a live transport — - * the run_approval mid-ask case) keeps it in `streaming` state. + * `zPersistedToolCallSummary` only ever validates a terminal + * (`success`/`error`) row — the backend drops a row a dead turn left in + * `pending`/`running` before persisting it. `isLive` (the row is the one + * actively backed by a live transport — the run_approval mid-ask case) is + * kept only for a call that arrives through the live WebSocket path with a + * status this schema doesn't cover; a restored (non-live) row is always + * `done`. */ function toolCallPartState( status: unknown, @@ -125,17 +121,15 @@ function toolCallPartState( /** * `undefined` while the call is still genuinely in progress (matching the * live path, which omits `ok` until a terminal status arrives); once the - * part is in a `done` state, only `ok`/`success` counts as success — every - * other terminal string, including a restored `pending`/`running` call that - * had no live transport to finish it, reads as failure rather than being - * rendered as if it succeeded. + * part is in a `done` state, only `success` counts as success — `error` + * reads as failure, matching `ToolCallSummary.status`. */ function toolCallOk( status: unknown, state: ToolPart['state'] ): boolean | undefined { if (state === 'streaming') return undefined - return status === 'ok' || status === 'success' + return status === 'success' } /** @@ -143,7 +137,7 @@ function toolCallOk( * and maps it onto the same `ToolPart` the live WebSocket path builds from * `agent_tool_call` events, so a reloaded transcript renders through the * identical work-summary UI as a live turn. `undefined` for anything that - * doesn't validate (missing `id`/`tool_name`, wrong types, ...). + * doesn't validate (missing `id`/`tool_call_id`/`tool_name`, wrong types, ...). */ function parseToolCallEntry( entry: unknown, @@ -152,7 +146,6 @@ function parseToolCallEntry( const parsed = zPersistedToolCallSummary.safeParse(entry) if (!parsed.success) return undefined const { - id, tool_call_id: toolCallId, tool_name: toolName, status, @@ -168,11 +161,10 @@ function parseToolCallEntry( : undefined return { type: 'tool', - // A live `agent_tool_call` frame keys its update on `tool_call_id`, not - // this row's own `id` — prefer it so a restored part matches a live - // frame that arrives for it later. Falls back to `id` only for rows - // recorded before `tool_call_id` existed. - callId: toolCallId ?? id, + // A live `agent_tool_call` frame keys its update on `tool_call_id`, so a + // restored part uses the same id to match a live frame that arrives for + // it later. + callId: toolCallId, name: toolName, state, ...(ok !== undefined ? { ok } : {}), diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 3f2facdd3b2..02f3471de91 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -728,7 +728,14 @@ describe('useAgentConversationStore', () => { const assistant = historyRow(2, 'assistant', 'turn-a', 'Done') assistant.content = { text: 'Done', - tool_calls: [{ id: 'call-1', tool_name: 'search_nodes', status: 'ok' }] + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success' + } + ] } const store = useAgentConversationStore() @@ -748,7 +755,14 @@ describe('useAgentConversationStore', () => { const threadAAssistant = historyRow(2, 'assistant', 'turn-a', 'Done A') threadAAssistant.content = { text: 'Done A', - tool_calls: [{ id: 'call-a', tool_name: 'search_nodes', status: 'ok' }] + tool_calls: [ + { + id: 'audit-a', + tool_call_id: 'call-a', + tool_name: 'search_nodes', + status: 'success' + } + ] } const threadA = [ historyRow(1, 'user', 'turn-a', 'Find a node'), From f3957b60f26bac44463fe34565e25916cb627743 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2026 06:36:38 +0000 Subject: [PATCH 2/4] fix(website): bump router catalog model count to 213 (#19574) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _Requested by **Robert Paige** · [Slack thread](https://comfy-organization.slack.com/archives/C0B61J7QCJW/p1790743770365959?thread_ts=1790743770.365959&cid=C0B61J7QCJW)_ ## Summary `website-unit` / `website-unit-gate` is red on `main` and on every PR because the live docs catalog has drifted past the hardcoded model count. ## Changes - **What**: `router-providers.test.ts` › "counts the models the docs catalog lists" fetches `docs.comfy.org/development/comfy-router/models.md` and asserts the count of distinct model paths equals `ROUTER_CATALOG_MODEL_COUNT`. The live catalog now lists 213 models while the constant was still pinned at 212, so the assertion fails on every run. Bumped `ROUTER_CATALOG_MODEL_COUNT` 212 → 213 and refreshed `ROUTER_PROVIDER_COVERAGE_VERIFIED_AT` to today, matching the shape of #18843 (which did the same 209 → 212 bump for the same reason on Sep 25). Only the count assertion was failing in CI, not any per-provider row assertion, so the provider rows themselves were left untouched. ## Before / After - **Before**: `website-unit` fails with `expected 213 to be 212`; blocks unrelated PRs, including #19565. - **After**: count matches the live catalog; `website-unit` / `website-unit-gate` pass again. ## Review Focus Minimal, scoped change: only the two constants in `apps/website/src/config/router-providers.ts` were touched, following the same pattern as #18843. --- _Generated by [Claude Code](https://claude.ai/code/session_01QByuWtiNix7Q4BY6kvhgPR)_ --- apps/website/src/config/router-providers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/website/src/config/router-providers.ts b/apps/website/src/config/router-providers.ts index fa77973d28f..0e6a84f08ed 100644 --- a/apps/website/src/config/router-providers.ts +++ b/apps/website/src/config/router-providers.ts @@ -36,10 +36,10 @@ export interface RouterProviderCoverageRow { const DOCS_MODELS_URL = 'https://docs.comfy.org/development/comfy-router/models' /** The day the rows and the catalog size were last checked against the docs. */ -export const ROUTER_PROVIDER_COVERAGE_VERIFIED_AT = '2026-09-25' +export const ROUTER_PROVIDER_COVERAGE_VERIFIED_AT = '2026-09-30' /** How many models the Router catalog lists, all served by Comfy. */ -export const ROUTER_CATALOG_MODEL_COUNT = 212 +export const ROUTER_CATALOG_MODEL_COUNT = 213 /** In the order the page shows them: alphabetical by `name`. */ export const ROUTER_PROVIDER_COVERAGE: readonly RouterProviderCoverageRow[] = [ From 5d44c38cc881c82332cec8b7ace3858bc862192f Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Tue, 29 Sep 2026 23:57:19 +0000 Subject: [PATCH 3/4] fix(billing-web): re-quote on QUOTE_STALE in the full-page checkout (#19499) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes the `full-page-promo.spec.ts` "a code that lapses before Pay" failure that has been red on main since #19126. The full-page checkout now re-quotes when a subscribe comes back as `QUOTE_STALE`. ## Changes - **What**: #19253 made `payVerdictOf` re-quote only when a raw `SUBSCRIPTION_QUOTE_STALE` `serverCode` came back. #19126 then changed account-core to turn a 4xx with that code into `{ code: 'QUOTE_STALE' }` with no `serverCode`. The two PRs were written in parallel and merged about 40 minutes apart. Since then, `payVerdictOf` falls through to `failure`. The page shows "Your quote changed…" instead of re-pricing and showing the expired-promo card. `payVerdictOf` now maps `QUOTE_STALE` to `requote`. - The `payVerdict` and `FullPageCheckoutView` unit tests built the pre-#19126 result shape by hand, so they missed this. They now use the shape account-core actually returns. ## Review Focus `SUBSCRIPTION_QUOTE_STALE` is gone from `STALE_QUOTE_SERVER_CODES`. Only `subscribe` emits it, and account-core now maps it for 4xx responses. A 5xx that echoes the code arrives as `REQUEST_FAILED`, which account-core treats as an upstream failure, not a stale quote. With this change, that 5xx no longer triggers a re-quote. Red/green: both updated unit tests fail without the `payVerdict.ts` change and pass with it. `pnpm test:e2e:local e2e/full-page-promo.spec.ts` passes 6/6. --- apps/billing-web/src/checkout/payVerdict.test.ts | 2 +- apps/billing-web/src/checkout/payVerdict.ts | 7 +++---- apps/billing-web/src/views/FullPageCheckoutView.test.ts | 9 +-------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/apps/billing-web/src/checkout/payVerdict.test.ts b/apps/billing-web/src/checkout/payVerdict.test.ts index e3b3eba3185..b33e1f9e59c 100644 --- a/apps/billing-web/src/checkout/payVerdict.test.ts +++ b/apps/billing-web/src/checkout/payVerdict.test.ts @@ -124,7 +124,7 @@ describe('payVerdictOf', () => { }, { name: 'a stale subscription quote', - result: refusedWith('REQUEST_FAILED', 'SUBSCRIPTION_QUOTE_STALE'), + result: { status: 'error', code: 'QUOTE_STALE' }, expected: { kind: 'requote', because: 'quote_expired' } }, { diff --git a/apps/billing-web/src/checkout/payVerdict.ts b/apps/billing-web/src/checkout/payVerdict.ts index 85565b866b4..909b7f470f8 100644 --- a/apps/billing-web/src/checkout/payVerdict.ts +++ b/apps/billing-web/src/checkout/payVerdict.ts @@ -12,10 +12,7 @@ import { import type { InlineOutcome, OperationOutcome } from '@/checkout/checkoutPage' /** The server refused the quote the customer consented to; re-price before asking again. */ -const STALE_QUOTE_SERVER_CODES = [ - 'PRORATION_QUOTE_EXPIRED', - 'SUBSCRIPTION_QUOTE_STALE' -] as const +const STALE_QUOTE_SERVER_CODES = ['PRORATION_QUOTE_EXPIRED'] as const /** * What a Pay's result asks of the page. `settled` needs nothing from @@ -39,6 +36,8 @@ export function payVerdictOf(result: SubscriptionCommandResult): PayVerdict { ) return { kind: 'requote', because: 'quote_expired' } switch (result.code) { + case 'QUOTE_STALE': + return { kind: 'requote', because: 'quote_expired' } case 'REACTIVATION_CONFIRMATION_REQUIRED': return { kind: 'requote', because: 'reactivation_required' } case 'OPERATION_ALREADY_PENDING': diff --git a/apps/billing-web/src/views/FullPageCheckoutView.test.ts b/apps/billing-web/src/views/FullPageCheckoutView.test.ts index 2c8615542d4..da75d1ee4b5 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.test.ts +++ b/apps/billing-web/src/views/FullPageCheckoutView.test.ts @@ -2169,14 +2169,7 @@ describe('FullPageCheckoutView promo codes', () => { it('a code that lapsed before Pay: back to capture, chip gone, the expired card instead of a decline', async () => { const fake = await renderCheckout( { - subscribe: { - status: 'error', - code: 'REQUEST_FAILED', - serverCode: readBillingErrorCode({ - code: 'SUBSCRIPTION_QUOTE_STALE', - message: 'stale' - }) - } + subscribe: { status: 'error', code: 'QUOTE_STALE' } }, quotesByCode ) From 30c177df4dbc0403932d9b1d276a8b360afbb7ee Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 30 Sep 2026 11:38:24 +0000 Subject: [PATCH 4/4] fix: don't let one malformed tool-call row fail the whole agent history load `agentRestClient.ts`'s `getMessages()` calls `zAgentMessages.parse()` (throws, not safeParse). Since `zAgentMessage.content.tool_calls` was typed as `z.array(zToolCallSummary)` -- the generated schema, which requires `tool_call_id`, closes `status` to `'success' | 'error'`, and requires an integer `duration_ms` -- a single non-conforming historical tool-call row threw during `.parse()` and failed the entire array/message/response parse, surfacing as `agent_history_load_failed` instead of gracefully degrading just that one entry. - agentApiSchema.ts: the message-parse boundary's `content.tool_calls` is now `z.array(z.unknown())`, so a malformed entry can never throw at `zAgentMessages.parse()`. `zPersistedToolCallSummary` (used only by `parseToolCallEntry`'s own `safeParse`) relaxes the generated schema: `tool_call_id` optional, `status` a bare string, `duration_ms` not required to be an integer. - agentTranscript.ts: `parseToolCallEntry` validates only the fields it reads (`.pick()`), restores the `tool_call_id ?? id` fallback for rows predating `tool_call_id`, and `toolCallOk` treats the older `'ok'` status alias the same as `'success'`. - agentTranscript.test.ts: restores the pre-tightening fixtures/tests this broke, and adds a regression test that exercises the actual `zAgentMessages.parse()` boundary with a malformed row, confirming the whole history still loads and only that one tool-call entry degrades. Claude-Session: https://claude.ai/code/session_01RuB3pFMNyS3AE7JYNFNesb --- .../agent/schemas/agentApiSchema.ts | 42 +++- .../services/agent/agentTranscript.test.ts | 211 ++++++++++++++---- .../agent/services/agent/agentTranscript.ts | 56 +++-- 3 files changed, 239 insertions(+), 70 deletions(-) diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 34c20121aee..59e4601e661 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -98,26 +98,44 @@ export type AgentRunModeValue = AgentRunModePreference['mode'] /** * One entry of a persisted assistant row's `content.tool_calls` (see * `agentTranscript.ts`'s `parseToolCallEntry`), the reload-path counterpart - * to the live WebSocket's `zAgentToolCallData` above. Sourced directly from - * the generated `ToolCallSummary` schema (Comfy-Org/cloud#10360) — the - * backend only ever persists terminal rows (`status: 'success' | 'error'`); - * a row a dead turn left in `pending`/`running` has no wire-status mapping - * and is dropped server-side rather than reaching this parser. + * to the live WebSocket's `zAgentToolCallData` above. Sourced from the + * generated `ToolCallSummary` schema (Comfy-Org/cloud#10360), but relaxed: + * the generated schema requires `tool_call_id`, narrows `status` to the + * closed `'success' | 'error'` wire vocabulary, and requires `duration_ms` + * to be an integer, which is only true for rows persisted after that + * backend change landed. Real historical rows can still carry a bare `id` + * with no `tool_call_id`, a `status` of `'ok'` (an older success alias — + * see `toolCallOk` in `agentTranscript.ts`), or a non-integer `duration_ms`. + * `parseToolCallEntry` picks only the fields it actually reads from this, + * so an unrelated field (e.g. a strict `started_at`/`finished_at` datetime) + * never gates the entry. */ -export const zPersistedToolCallSummary = zToolCallSummary +export const zPersistedToolCallSummary = zToolCallSummary.extend({ + id: z.string(), + tool_call_id: z.string().optional(), + status: z.string(), + duration_ms: z.number().optional() +}) /** - * The generated `AgentMessage.content` schema narrows to just `tool_calls` - * (typed via `zToolCallSummary`), but the OpenAPI-generated TS type still - * carries a `[key: string]: unknown` index signature for it — the zod - * plugin's output didn't get a matching `.passthrough()`. Re-widened here so - * a persisted row's other `content` fields (`text`, `attachments`, + * The message-parse boundary's `content.tool_calls` field is deliberately + * `z.array(z.unknown())`, not the stricter `zPersistedToolCallSummary` + * above: `agentRestClient.ts`'s `request()` calls `.parse()` (throws) on + * the whole `AgentMessages` array, so a single non-conforming historical + * tool-call row must never fail the entire conversation-history load. + * `agentTranscript.ts`'s `parseToolCallEntry` runs its own `safeParse` + * per entry and degrades just that one row instead. + * + * The generated `AgentMessage.content` schema also keeps a + * `[key: string]: unknown` index signature on its TS type but no matching + * `.passthrough()` on its zod schema — the `.passthrough()` below re-widens + * it so a persisted row's other `content` fields (`text`, `attachments`, * `attachment_refs`, `workflow_references`, ...; see `agentTranscript.ts`) * keep parsing. */ const zAgentMessageContent = z .object({ - tool_calls: z.array(zToolCallSummary).optional() + tool_calls: z.array(z.unknown()).optional() }) .passthrough() diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 3867f7c95fa..1c1aa36fa69 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import type { AgentMessages } from '../../schemas/agentApiSchema' -import { toTurnId } from '../../schemas/agentApiSchema' +import { toTurnId, zAgentMessages } from '../../schemas/agentApiSchema' import { normalizeAgentTranscript } from './agentTranscript' const row = ( @@ -261,20 +261,20 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('drops a tool-call entry with no tool_call_id, matching the generated ToolCallSummary contract', () => { + it('falls back to id for callId when a row was recorded before tool_call_id existed', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', tool_calls: [ { id: 'audit-row-uuid-1', tool_name: 'search_nodes', status: 'success' } ] - } as unknown as AgentMessages[number]['content'] + } const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toEqual([ - { type: 'text', text: 'Done', state: 'done' } - ]) + expect(transcript.messages[0].parts).toContainEqual( + expect.objectContaining({ callId: 'audit-row-uuid-1' }) + ) }) it('omits tool parts entirely when a message carries no tool_calls', () => { @@ -328,7 +328,7 @@ describe('normalizeAgentTranscript', () => { ]) }) - it('drops a persisted tool call left pending/running by a dead turn, matching the backend contract that only terminal rows are ever persisted', () => { + it('clamps a restored pending/running tool call to done+ok:false with no live transport', () => { const message = row(1, 'assistant', 'turn-a', '', 'row-1') message.content = { tool_calls: [ @@ -339,14 +339,22 @@ describe('normalizeAgentTranscript', () => { status: 'running' } ] - } as unknown as AgentMessages[number]['content'] + } const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toEqual([]) + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: false + } + ]) }) - it('keeps the runApproval part and pending state for a live run_approval ask even though its stale pending/running tool call is dropped', () => { + it('keeps a pending/running tool call streaming when its row is the live run_approval ask', () => { const message = row(1, 'assistant', 'turn-a', '', 'row-1') message.status = 'streaming' message.content = { @@ -358,7 +366,7 @@ describe('normalizeAgentTranscript', () => { status: 'running' } ] - } as unknown as AgentMessages[number]['content'] + } message.pending_ask = { message_id: 'row-1', ask_id: 'ask-1', @@ -372,9 +380,12 @@ describe('normalizeAgentTranscript', () => { const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toEqual([ - { type: 'runApproval', askId: 'ask-1' } - ]) + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'streaming' + }) expect(transcript.pending?.messageId).toBe('row-1') }) @@ -406,7 +417,58 @@ describe('normalizeAgentTranscript', () => { } ) - it('omits durationMs for a negative but otherwise valid duration_ms', () => { + it.for([Infinity, -Infinity, -1])( + 'omits durationMs for a non-finite or negative duration_ms (%s) without dropping the entry', + (durationMs) => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success', + duration_ms: durationMs + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }) + } + ) + + it('drops the whole tool-call entry when duration_ms is NaN, matching the live schema (z.number()) rejecting it', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'success', + duration_ms: NaN + } + ] + } + + const transcript = normalizeAgentTranscript([message]) + + expect(transcript.messages[0].parts).toEqual([ + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + + it('accepts a non-integer duration_ms, matching the live path leniency', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = { text: 'Done', @@ -416,7 +478,7 @@ describe('normalizeAgentTranscript', () => { tool_call_id: 'call-1', tool_name: 'search_nodes', status: 'success', - duration_ms: -1 + duration_ms: 420.5 } ] } @@ -428,34 +490,35 @@ describe('normalizeAgentTranscript', () => { callId: 'call-1', name: 'search_nodes', state: 'done', - ok: true + ok: true, + durationMs: 420.5 }) }) - it.for([NaN, Infinity, -Infinity])( - 'drops the whole tool-call entry when duration_ms is non-finite (%s), since the generated schema requires an integer', - (durationMs) => { - const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') - message.content = { - text: 'Done', - tool_calls: [ - { - id: 'audit-1', - tool_call_id: 'call-1', - tool_name: 'search_nodes', - status: 'success', - duration_ms: durationMs - } - ] - } + it('treats the older ok status alias the same as success', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + text: 'Done', + tool_calls: [ + { + id: 'audit-1', + tool_call_id: 'call-1', + tool_name: 'search_nodes', + status: 'ok' + } + ] + } - const transcript = normalizeAgentTranscript([message]) + const transcript = normalizeAgentTranscript([message]) - expect(transcript.messages[0].parts).toEqual([ - { type: 'text', text: 'Done', state: 'done' } - ]) - } - ) + expect(transcript.messages[0].parts).toContainEqual({ + type: 'tool', + callId: 'call-1', + name: 'search_nodes', + state: 'done', + ok: true + }) + }) it('dedupes a repeated callId within one row, keeping the last entry at the first position', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') @@ -575,7 +638,7 @@ describe('normalizeAgentTranscript', () => { tool_calls: [ { id: 'audit-1', tool_call_id: 'call-1', tool_name: 'search_nodes' } ] - } as unknown as AgentMessages[number]['content'] + } const transcript = normalizeAgentTranscript([message]) @@ -584,6 +647,74 @@ describe('normalizeAgentTranscript', () => { ]) }) + it('does not fail the whole history load through zAgentMessages.parse() when one tool-call entry is malformed (regression for agent_history_load_failed)', () => { + // A raw wire payload, not the pre-typed `row()` fixture: this goes + // through the real `zAgentMessages.parse()` boundary that + // `agentRestClient.ts`'s `getMessages()` calls (and which throws, not + // safeParse). Before the fix, `content.tool_calls` was typed as + // `z.array(zToolCallSummary)`, so the single malformed entry below + // (missing `tool_name`) would have thrown during `.parse()` and failed + // the ENTIRE array -- every row, not just this one entry. + const rawHistory: unknown = [ + { + id: 'row-1', + thread_id: 'thread-1', + seq: 1, + role: 'user', + status: 'complete', + turn_id: 'turn-a', + content: { text: 'Search for upscalers' } + }, + { + id: 'row-2', + thread_id: 'thread-1', + seq: 2, + role: 'assistant', + status: 'complete', + turn_id: 'turn-a', + content: { + text: 'Done', + tool_calls: [ + // Malformed: no tool_name, so it can never become a ToolPart -- + // this one entry should degrade, not the whole load. + { id: 'audit-bad', tool_call_id: 'call-bad', status: 'success' }, + { + id: 'audit-good', + tool_call_id: 'call-good', + tool_name: 'search_nodes', + status: 'success' + } + ] + } + } + ] + + let parsed: AgentMessages | undefined + expect(() => { + parsed = zAgentMessages.parse(rawHistory) + }).not.toThrow() + expect(parsed).toHaveLength(2) + + const transcript = normalizeAgentTranscript(parsed!) + + // The whole history loaded: both turns' content is present. + expect(transcript.userTexts.get(toTurnId('turn-a'))).toBe( + 'Search for upscalers' + ) + // Only the malformed entry degraded -- the valid sibling tool call and + // the reply text still rendered. + expect(transcript.messages[0].parts).toEqual([ + { + type: 'tool', + callId: 'call-good', + name: 'search_nodes', + state: 'done', + ok: true + }, + { type: 'text', text: 'Done', state: 'done' } + ]) + }) + it('restores available and unavailable references without changing the latest workflow target', () => { const first = row(1, 'user', 'turn-a', 'Compare these', 'row-1') first.workflow_id = 'wf-target-a' diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index c0062a07ccd..adf2135e1c7 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -102,13 +102,15 @@ function parseUserWorkflowReferences( } /** - * `zPersistedToolCallSummary` only ever validates a terminal - * (`success`/`error`) row — the backend drops a row a dead turn left in - * `pending`/`running` before persisting it. `isLive` (the row is the one - * actively backed by a live transport — the run_approval mid-ask case) is - * kept only for a call that arrives through the live WebSocket path with a - * status this schema doesn't cover; a restored (non-live) row is always - * `done`. + * A persisted tool-call status is `pending`/`running` while it was still in + * flight when a dead turn ended, and terminal (`success`/`error`, or the + * older `ok` success alias — see `toolCallOk`) otherwise. + * + * A restored (non-live) row has no transport left to ever settle its tool + * parts, so a `pending`/`running` status there is clamped straight to + * `done` (never rendered as a perpetual-progress chip) rather than left + * spinning forever; only `isLive` (the row is the one actively backed by a + * live transport — the run_approval mid-ask case) keeps it `streaming`. */ function toolCallPartState( status: unknown, @@ -121,31 +123,48 @@ function toolCallPartState( /** * `undefined` while the call is still genuinely in progress (matching the * live path, which omits `ok` until a terminal status arrives); once the - * part is in a `done` state, only `success` counts as success — `error` - * reads as failure, matching `ToolCallSummary.status`. + * part is in a `done` state, `success` and the older `ok` success alias + * both count as success, matching how the live path (`toolCallOk`'s + * counterpart in `agentEventTransport.ts`) resolves a terminal frame — + * everything else, including a restored `pending`/`running` call that had + * no live transport to finish it, reads as failure rather than being + * rendered as if it succeeded. */ function toolCallOk( status: unknown, state: ToolPart['state'] ): boolean | undefined { if (state === 'streaming') return undefined - return status === 'success' + return status === 'success' || status === 'ok' } /** - * Validates one `content.tool_calls` entry against `zPersistedToolCallSummary` - * and maps it onto the same `ToolPart` the live WebSocket path builds from + * Validates one `content.tool_calls` entry against `zPersistedToolCallSummary`, + * picking only the fields this function actually reads (`id`/`tool_call_id`, + * `tool_name`, `status`, `duration_ms`) so an unrelated field a real + * historical row gets wrong or omits (e.g. `started_at`/`finished_at`, + * which are never read) can't sink an otherwise-usable entry. Maps a valid + * entry onto the same `ToolPart` the live WebSocket path builds from * `agent_tool_call` events, so a reloaded transcript renders through the * identical work-summary UI as a live turn. `undefined` for anything that - * doesn't validate (missing `id`/`tool_call_id`/`tool_name`, wrong types, ...). + * doesn't validate (missing `id`/`tool_name`, wrong types, ...). */ function parseToolCallEntry( entry: unknown, isLive: boolean ): ToolPart | undefined { - const parsed = zPersistedToolCallSummary.safeParse(entry) + const parsed = zPersistedToolCallSummary + .pick({ + id: true, + tool_call_id: true, + tool_name: true, + status: true, + duration_ms: true + }) + .safeParse(entry) if (!parsed.success) return undefined const { + id, tool_call_id: toolCallId, tool_name: toolName, status, @@ -161,10 +180,11 @@ function parseToolCallEntry( : undefined return { type: 'tool', - // A live `agent_tool_call` frame keys its update on `tool_call_id`, so a - // restored part uses the same id to match a live frame that arrives for - // it later. - callId: toolCallId, + // A live `agent_tool_call` frame keys its update on `tool_call_id`, not + // this row's own `id` — prefer it so a restored part matches a live + // frame that arrives for it later. Falls back to `id` only for rows + // recorded before `tool_call_id` existed. + callId: toolCallId ?? id, name: toolName, state, ...(ok !== undefined ? { ok } : {}),