diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts index 16fb2a57d55..51d00a40dba 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { assert, describe, expect, it } from 'vitest' import { AGENT_WS_EVENT_TYPES, @@ -114,6 +114,40 @@ describe('agentApiSchema contract subtleties', () => { expect(parsed).toMatchObject({ data: { skill: null } }) }) + it('clamps an over-long skill instead of dropping the tool-call frame', () => { + const parsed = zAgentWsEvent.parse({ + type: 'agent_tool_call', + data: { + tool_call_id: 'call-1', + tool_name: 'load_skill', + status: 'running', + skill: 'a'.repeat(300), + message_id: 'm1', + thread_id: 't1' + } + }) + + assert(parsed.type === 'agent_tool_call') + expect(parsed.data.skill).toBe('a'.repeat(256)) + }) + + it('does not split a Unicode code point when clamping a skill', () => { + const parsed = zAgentWsEvent.parse({ + type: 'agent_tool_call', + data: { + tool_call_id: 'call-1', + tool_name: 'load_skill', + status: 'running', + skill: `${'a'.repeat(255)}😀tail`, + message_id: 'm1', + thread_id: 't1' + } + }) + + assert(parsed.type === 'agent_tool_call') + expect(parsed.data.skill).toBe(`${'a'.repeat(255)}😀`) + }) + it('accepts agent_message_done with usage null (cancelled turn)', () => { expect( zAgentWsEvent.safeParse({ diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 627801b3649..8ab748ac722 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -95,6 +95,23 @@ export const zAgentRunMode = zGeneratedAgentRunMode.superRefine( ) export type AgentRunModeValue = AgentRunModePreference['mode'] +const SKILL_NAME_MAX = 256 + +/** + * Display-only, so an over-long name is clamped. Rejecting it would drop + * the live frame or the persisted tool call. + */ +const zSkillName = z + .string() + .nullish() + .transform((skill) => + typeof skill === 'string' + ? Array.from(skill.slice(0, SKILL_NAME_MAX * 2)) + .slice(0, SKILL_NAME_MAX) + .join('') + : skill + ) + /** * One entry of a persisted assistant row's `content.tool_calls` (see * `agentTranscript.ts`'s `parseToolCallEntry`), the reload-path counterpart @@ -115,7 +132,7 @@ export const zPersistedToolCallSummary = zToolCallSummary.extend({ tool_call_id: z.string().optional(), status: z.string(), duration_ms: z.number().optional(), - skill: z.string().max(256).nullish() + skill: zSkillName }) /** @@ -187,7 +204,7 @@ const zAgentToolCallData = z tool_call_id: z.string(), tool_name: z.string(), status: z.enum(['running', 'success', 'error']), - skill: z.string().max(256).nullish(), + skill: zSkillName, args: z.never().optional(), duration_ms: z.number().optional(), message_id: z.string(), diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts index 81832efc009..91ab651aa44 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.test.ts @@ -276,6 +276,29 @@ describe('normalizeAgentTranscript', () => { ) }) + it('clamps an over-long persisted skill while restoring the transcript', () => { + const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') + message.content = { + tool_calls: [ + { + id: 'audit-row-uuid-1', + tool_call_id: 'call-1', + tool_name: 'load_skill', + status: 'success', + skill: 'a'.repeat(300) + } + ] + } + + const parsed = zAgentMessages.parse([message]) + + expect(normalizeAgentTranscript(parsed).messages[0].parts[0]).toMatchObject( + { + skill: 'a'.repeat(256) + } + ) + }) + it('retains a persisted tool call with a null skill', () => { const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1') message.content = {