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
36 changes: 35 additions & 1 deletion src/workbench/extensions/agent/schemas/agentApiSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { assert, describe, expect, it } from 'vitest'

import {
AGENT_WS_EVENT_TYPES,
Expand Down Expand Up @@ -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({
Expand Down
21 changes: 19 additions & 2 deletions src/workbench/extensions/agent/schemas/agentApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ export const zAgentRunMode = zGeneratedAgentRunMode.superRefine(
)
export type AgentRunModeValue = AgentRunModePreference['mode']

const SKILL_NAME_MAX = 256
Comment thread
christian-byrne marked this conversation as resolved.
Resolved

/**
* 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) =>
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
typeof skill === 'string'
? Array.from(skill.slice(0, SKILL_NAME_MAX * 2))
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
.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
Expand All @@ -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
})

/**
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ describe('normalizeAgentTranscript', () => {
)
})

it('clamps an over-long persisted skill while restoring the transcript', () => {
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
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 = {
Expand Down
Loading