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
120 changes: 67 additions & 53 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,71 @@ function agentTabFilename(name: string | undefined): string | undefined {
return cleaned.length === 0 ? undefined : `${cleaned}.json`
}

type AgentTabTarget = NonNullable<ReturnType<typeof boundOrOpenWorkflowFor>>

async function openBoundAgentTab(
data: AgentActiveTabData,
bound: AgentTabTarget,
stale: () => boolean
): Promise<void> {
const opened = await workflowService.openWorkflow(bound)
if (stale()) return
if (!opened) {
warnWorkflowUnavailable()
trackWorkflowOpenFailure('toast')
return
}
// Name resolution does not create the binding consumed downstream.
bindingStore.bind(data.workflow_id, bound.path)
if (status.value !== 'idle') tabActivity.setEditing(bound.path)
bindWorkflow(data.workflow_id)
useTelemetry()?.trackAgentWorkflowApplied({
workflow_id: data.workflow_id,
target: 'active_tab_switch'
})
}

async function openNewAgentTab(
data: AgentActiveTabData,
stale: () => boolean
): Promise<void> {
const creatingStartedAt = Date.now()
tabActivity.setCreating(true)
const remainingCreatingTime =
CREATING_TAB_MIN_DURATION_MS - (Date.now() - creatingStartedAt)
if (remainingCreatingTime > 0)
await new Promise((resolve) => setTimeout(resolve, remainingCreatingTime))
if (stale()) return
Comment thread
christian-byrne marked this conversation as resolved.
Resolved

const tab = workflowStore.createNewTemporary(
agentTabFilename(data.name),
agentTabGraph
)
tabActivity.setCreating(false)
let opened: boolean
try {
opened = await workflowService.openWorkflow(tab)
} catch (error) {
await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
throw error
}
if (stale() || !opened) {
await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
if (!stale()) {
warnWorkflowUnavailable()
trackWorkflowOpenFailure('toast')
}
return
}
if (status.value !== 'idle') tabActivity.setEditing(tab.path)
bindingStore.bind(data.workflow_id, tab.path)
bindWorkflow(data.workflow_id)
useTelemetry()?.trackAgentWorkflowApplied({
workflow_id: data.workflow_id,
target: 'active_tab_open'
})
}

async function onAgentActiveTab(
data: AgentActiveTabData,
generation: number
Expand All @@ -906,59 +971,8 @@ async function onAgentActiveTab(
if (stale()) return
try {
const bound = boundOrOpenWorkflowFor(data.workflow_id)
if (bound) {
const opened = await workflowService.openWorkflow(bound)
if (stale()) return
if (!opened) {
warnWorkflowUnavailable()
trackWorkflowOpenFailure('toast')
return
}
// boundOrOpenWorkflowFor can resolve by cloud name, which leaves no binding behind
// for everything downstream that only reads tabPathFor.
bindingStore.bind(data.workflow_id, bound.path)
if (status.value !== 'idle') tabActivity.setEditing(bound.path)
bindWorkflow(data.workflow_id)
useTelemetry()?.trackAgentWorkflowApplied({
workflow_id: data.workflow_id,
target: 'active_tab_switch'
})
return
}
const creatingStartedAt = Date.now()
tabActivity.setCreating(true)
const remainingCreatingTime =
CREATING_TAB_MIN_DURATION_MS - (Date.now() - creatingStartedAt)
if (remainingCreatingTime > 0)
await new Promise((resolve) => setTimeout(resolve, remainingCreatingTime))
if (stale()) return
const tab = workflowStore.createNewTemporary(
agentTabFilename(data.name),
agentTabGraph
)
tabActivity.setCreating(false)
let opened: boolean
try {
opened = await workflowService.openWorkflow(tab)
} catch (error) {
await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
throw error
}
if (stale() || !opened) {
await workflowService.closeWorkflow(tab, { warnIfUnsaved: false })
if (!stale()) {
warnWorkflowUnavailable()
trackWorkflowOpenFailure('toast')
}
return
}
if (status.value !== 'idle') tabActivity.setEditing(tab.path)
bindingStore.bind(data.workflow_id, tab.path)
bindWorkflow(data.workflow_id)
useTelemetry()?.trackAgentWorkflowApplied({
workflow_id: data.workflow_id,
target: 'active_tab_open'
})
if (bound) await openBoundAgentTab(data, bound, stale)
else await openNewAgentTab(data, stale)
} catch (error) {
if (stale()) return
bindWorkflow(data.workflow_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2784,7 +2784,11 @@ describe('app:agent_error telemetry (TEL-8)', () => {
const [error, options] = vi.mocked(reportError).mock.calls[0]
expect((error as Error).message).toBe('Malformed agent stream event')
expect(options.tags?.event_type).toBe('agent_message_done')
expect(options.context?.issues).toEqual(expect.any(Array))
expect(options.context?.issue_count).toEqual(expect.any(Number))
expect(options.context?.issue_codes).toEqual(
expect.arrayContaining([expect.any(String)])
)
expect(options.context).not.toHaveProperty('issues')
})

it('reports a rejected send to the unified error sinks', async () => {
Expand Down Expand Up @@ -2876,7 +2880,7 @@ describe('app:agent_error telemetry (TEL-8)', () => {

try {
const ok = await session.sendMessage('make me a cat')
expect(ok).toBe(false)
expect(ok).toBe(true)
} finally {
setItem.mockRestore()
}
Expand All @@ -2901,7 +2905,7 @@ describe('app:agent_error telemetry (TEL-8)', () => {
const session = useAgentSession({ rest, events: fakeEvents().source })
session.start()

await session.sendMessage('make me a cat')
expect(await session.sendMessage('make me a cat')).toBe(true)

expect(telemetry.trackAgentError).toHaveBeenCalledWith({
error_class: 'request_failed',
Expand All @@ -2916,15 +2920,14 @@ describe('app:agent_error telemetry (TEL-8)', () => {
const rest = fakeRest({
postMessage: vi.fn(async () => {
throw new AgentResponseUnreadableError(
'/agent/threads/new/messages',
new SyntaxError('Unexpected end of JSON input')
)
})
})
const session = useAgentSession({ rest, events: fakeEvents().source })
session.start()

await session.sendMessage('make me a cat')
expect(await session.sendMessage('make me a cat')).toBe(true)

expect(telemetry.trackAgentError).toHaveBeenCalledWith({
error_class: 'request_failed',
Expand Down Expand Up @@ -3060,6 +3063,18 @@ describe('app:agent_error telemetry (TEL-8)', () => {

// The aborted turn, then the idle session the other nineteen arrived into.
expect(telemetry.trackAgentError).toHaveBeenCalledTimes(2)
expect(session.notices.value).toHaveLength(2)
})

it('does not trust attacker-controlled message ids as telemetry keys', () => {
const { source, emit } = fakeEvents()
const session = useAgentSession({ rest: fakeRest(), events: source })
session.start()

for (let i = 0; i < 100; i++)
emit({ type: 'agent_message_delta', data: { message_id: `spoof-${i}` } })

expect(telemetry.trackAgentError).toHaveBeenCalledTimes(1)
})

it('tracks malformed frames of event types that never reach the user', async () => {
Expand Down
Loading
Loading