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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions browser_tests/fixtures/agentPromptHistoryFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import type {
} from '@comfyorg/ingest-types'
import { zAgentPostMessageRequest } from '@comfyorg/ingest-types/zod'

import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema'
import type {
AgentWsEvent,
PersistedToolCallSummary
} from '@/workbench/extensions/agent/schemas/agentApiSchema'

import { agentTest } from '@e2e/fixtures/agentPanelFixture'
import { workflowSelectionTest } from '@e2e/fixtures/agentWorkflowSelectionFixture'
Expand All @@ -18,13 +21,26 @@ import { webSocketFixture } from '@e2e/fixtures/ws'

const base = mergeTests(agentTest, workflowSelectionTest, webSocketFixture)

// A mocked history row models what the server actually persists, which is
// the relaxed `PersistedToolCallSummary` shape (see agentApiSchema.ts), not
// the stricter wire `ToolCallSummary` generated type.
type MockAgentMessageContent = Omit<
NonNullable<AgentMessage['content']>,
'tool_calls'
> & {
tool_calls?: PersistedToolCallSummary[]
}
type MockAgentMessage = Omit<AgentMessage, 'content'> & {
content?: MockAgentMessageContent
}

export const promptHistoryTest = base.extend<{
agentPanel: AgentPanel
promptHistory: {
requests: AgentPostMessageRequest[]
historyReads: () => number
historyRequestThreadIds: string[]
completeLatestTurn: (content: AgentMessage['content']) => void
completeLatestTurn: (content: MockAgentMessageContent) => void
}
}>({
agentPanel: async ({ page }, use) => {
Expand All @@ -34,7 +50,7 @@ export const promptHistoryTest = base.extend<{
// Workflow selection boots the app before these agent-specific routes.
void workflowSelection
const requests: AgentPostMessageRequest[] = []
const messages: AgentMessage[] = []
const messages: MockAgentMessage[] = []
const historyRequestThreadIds: string[] = []
let threadId = ''
let historyReads = 0
Expand Down
77 changes: 65 additions & 12 deletions src/workbench/extensions/agent/schemas/agentApiSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
zAgentMessage,
zAgentMessages,
zAgentTurnAccepted,
zAgentWsEvent
zAgentWsEvent,
zPersistedToolCallSummary
} from './agentApiSchema'
import type { ZodTypeAny } from 'zod'

Expand Down Expand Up @@ -148,7 +149,67 @@ describe('agentApiSchema contract subtleties', () => {
expect(parsed.data.skill).toBe(`${'a'.repeat(255)}😀`)
})

it('keeps a whole transcript readable when one persisted skill is over-long', () => {
it('clamps an over-long skill on a persisted tool-call row', () => {
const parsed = zPersistedToolCallSummary.parse({
id: 'audit-row-uuid-1',
tool_call_id: 'call-1',
tool_name: 'load_skill',
status: 'success',
skill: 'a'.repeat(300)
})

expect(parsed.skill).toBe('a'.repeat(256))
})

it('accepts an explicit null tool_call_id and duration_ms on a persisted tool-call row', () => {
const parsed = zPersistedToolCallSummary.safeParse({
id: 'audit-row-uuid-1',
tool_call_id: null,
tool_name: 'search_nodes',
status: 'success',
duration_ms: null
})

expect(parsed.success).toBe(true)
})

it('accepts a persisted tool-call row with no status key or an explicit null status', () => {
const noStatusKey = zPersistedToolCallSummary.safeParse({
id: 'audit-row-uuid-1',
tool_call_id: 'call-1',
tool_name: 'search_nodes'
})
const nullStatus = zPersistedToolCallSummary.safeParse({
id: 'audit-row-uuid-1',
tool_call_id: 'call-1',
tool_name: 'search_nodes',
status: null
})

expect(noStatusKey.success).toBe(true)
expect(nullStatus.success).toBe(true)
})

it.for([null, 'not-an-array', 42, {}])(
'does not throw zAgentMessages.parse() when content.tool_calls is %s instead of an array',
(toolCalls) => {
expect(() =>
zAgentMessages.parse([
{
id: 'row-1',
thread_id: 't1',
turn_id: 'turn-a',
seq: 1,
role: 'assistant',
status: 'complete',
content: { text: 'Done', tool_calls: toolCalls }
}
])
).not.toThrow()
}
)

it('keeps a whole transcript readable when a persisted tool-call row is malformed', () => {
const parsed = zAgentMessages.parse([
{
id: 'row-1',
Expand All @@ -159,20 +220,12 @@ describe('agentApiSchema contract subtleties', () => {
status: 'complete',
content: {
text: 'Done',
tool_calls: [
{
id: 'audit-row-uuid-1',
tool_call_id: 'call-1',
tool_name: 'load_skill',
status: 'success',
skill: 'a'.repeat(300)
}
]
tool_calls: [{ unexpected: 'shape' }]
}
}
])

expect(parsed[0].content?.tool_calls?.[0].skill).toBe('a'.repeat(256))
expect(parsed[0].content?.tool_calls).toEqual([{ unexpected: 'shape' }])
})

it('accepts agent_message_done with usage null (cancelled turn)', () => {
Expand Down
64 changes: 48 additions & 16 deletions src/workbench/extensions/agent/schemas/agentApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,66 @@ const zSkillName = z
/**
* 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.
*
* This is also the element type of `zAgentMessageContent.tool_calls` below,
* which is what lets a persisted `skill` reach `parseToolCallEntry` at all —
* the generated `zToolCallSummary` has no `skill` key and no `.passthrough()`,
* so using it there stripped the field before the transcript parser saw it.
* 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-finite/negative
* `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.extend({
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
id: z.string(),
// `.nullish()`, matching `zSkillName` above: a Go pointer field serialized
// without `omitempty` sends an explicit `null` rather than omitting the
// key, and `.optional()` alone rejects that, failing this entry's
// `safeParse` and dropping the whole tool-call row.
tool_call_id: z.string().nullish(),
status: z.string().nullish(),
// `z.custom`, not `z.number()`: Zod 4's `z.number()` also rejects
// `Infinity`/`-Infinity` (Zod 3 only rejected `NaN`), which would fail
// this entry's `safeParse` and drop the whole tool-call row over a bad
// `duration_ms` alone. `NaN` stays rejected here, matching the
// pre-existing Zod 3 behavior; `parseDurationMs` in `agentTranscript.ts`
// already does the real finite/non-negative check and omits the field
// for `Infinity`/`-Infinity` instead of failing the whole entry.
duration_ms: z
.custom<number>((val) => typeof val === 'number' && !Number.isNaN(val))
.nullish(),
skill: zSkillName
})
export type PersistedToolCallSummary = z.infer<typeof zPersistedToolCallSummary>

/**
* 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(zPersistedToolCallSummary).optional()
// `z.unknown()`, not `z.array(z.unknown())`: a Go nil slice persisted
// without `omitempty` sends `tool_calls: null`, and any other historical
// row could carry a non-array value here too. `zAgentMessages.parse()`
// throws on the whole history for a schema mismatch, so this field must
// accept anything; `agentTranscript.ts`'s `parseToolCalls` already
// guards with `Array.isArray` and degrades just that one row's tool
// calls instead.
tool_calls: z.unknown().optional()
})
.passthrough()

Expand Down
Loading
Loading