feat: add bounded diagnostics and improve UX feedback
Ask and Execute guidance, Runtime state, live announcements, loading states, retry paths, settings focus, and update failures now provide consistent and accessible feedback across the desktop experience. Add bounded redacted Desktop and Agent diagnostic logs, a read-only Agent diagnostics command, and an explicit opt-in for attaching a small Desktop diagnostic summary to in-app feedback. Diagnostics stay local by default and the existing feedback v1 service contract remains compatible. Release note: 新增可选桌面诊断反馈与 Agent 诊断导出,并统一 Ask/Execute 状态、错误恢复和无障碍反馈;诊断默认保留在本地,仅在用户明确勾选时附加桌面摘要。
This commit is contained in:
@@ -106,7 +106,12 @@ describe('AgentRuntimeController', () => {
|
||||
it('fails retired runtime requests and disposes them after exit', async () => {
|
||||
const previous = new TestRuntime(true, true)
|
||||
const next = new TestRuntime()
|
||||
const controller = new AgentRuntimeController(previous)
|
||||
const observeFailure = vi.fn()
|
||||
const controller = new AgentRuntimeController(
|
||||
previous,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
const authorize = vi.fn(async () => 'once' as const)
|
||||
const approvedStream = controller.run(
|
||||
{
|
||||
@@ -137,6 +142,7 @@ describe('AgentRuntimeController', () => {
|
||||
await expect(controller.getStatus()).resolves.toMatchObject({
|
||||
label: 'Test'
|
||||
})
|
||||
expect(observeFailure).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps a retiring runtime alive until its status probe finishes', async () => {
|
||||
@@ -449,6 +455,132 @@ describe('AgentRuntimeController', () => {
|
||||
expect(authorize).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not report cancellation, AbortError, or tool denial', async () => {
|
||||
const observeFailure = vi.fn()
|
||||
const canceledRuntime = new TestRuntime()
|
||||
canceledRuntime.run = async function* (
|
||||
_request,
|
||||
signal
|
||||
): AsyncGenerator<AgentEvent, void, void> {
|
||||
yield* []
|
||||
signal.throwIfAborted()
|
||||
await new Promise<void>((_resolve, reject) => {
|
||||
signal.addEventListener(
|
||||
'abort',
|
||||
() => reject(signal.reason),
|
||||
{ once: true }
|
||||
)
|
||||
})
|
||||
}
|
||||
const canceledController = new AgentRuntimeController(
|
||||
canceledRuntime,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
const abortController = new AbortController()
|
||||
const canceled = canceledController.run(
|
||||
{
|
||||
requestId: '1c608898-ecb7-4081-8174-2b6a52f53b18',
|
||||
conversationId: 'conversation-canceled',
|
||||
prompt: 'test',
|
||||
workMode: 'ask'
|
||||
},
|
||||
abortController.signal
|
||||
)
|
||||
const pending = canceled.next()
|
||||
abortController.abort()
|
||||
await expect(pending).rejects.toMatchObject({
|
||||
name: 'AbortError'
|
||||
})
|
||||
await canceledController.dispose()
|
||||
|
||||
const abortErrorRuntime = new TestRuntime()
|
||||
abortErrorRuntime.run = async function* (): AsyncGenerator<
|
||||
AgentEvent,
|
||||
void,
|
||||
void
|
||||
> {
|
||||
yield* []
|
||||
throw new DOMException('aborted', 'AbortError')
|
||||
}
|
||||
const abortErrorController = new AgentRuntimeController(
|
||||
abortErrorRuntime,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
const aborted = abortErrorController.run(
|
||||
{
|
||||
requestId: '1c608898-ecb7-4081-8174-2b6a52f53b19',
|
||||
conversationId: 'conversation-abort-error',
|
||||
prompt: 'test',
|
||||
workMode: 'ask'
|
||||
},
|
||||
new AbortController().signal
|
||||
)
|
||||
await expect(aborted.next()).rejects.toMatchObject({
|
||||
name: 'AbortError'
|
||||
})
|
||||
await abortErrorController.dispose()
|
||||
|
||||
const deniedRuntime = new TestRuntime(false, false, true)
|
||||
const deniedController = new AgentRuntimeController(
|
||||
deniedRuntime,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
const denied = deniedController.run(
|
||||
{
|
||||
requestId: '1c608898-ecb7-4081-8174-2b6a52f53b20',
|
||||
conversationId: 'conversation-tool-denied',
|
||||
prompt: 'test',
|
||||
workMode: 'execute'
|
||||
},
|
||||
new AbortController().signal,
|
||||
async () => 'deny'
|
||||
)
|
||||
await expect(denied.next()).rejects.toThrow('tool denied')
|
||||
await deniedController.dispose()
|
||||
|
||||
expect(observeFailure).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reports genuine Runtime run failures', async () => {
|
||||
const runtime = new TestRuntime()
|
||||
const failure = new Error('provider failed')
|
||||
runtime.run = async function* (): AsyncGenerator<
|
||||
AgentEvent,
|
||||
void,
|
||||
void
|
||||
> {
|
||||
yield* []
|
||||
throw failure
|
||||
}
|
||||
const observeFailure = vi.fn()
|
||||
const controller = new AgentRuntimeController(
|
||||
runtime,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
const stream = controller.run(
|
||||
{
|
||||
requestId: '1c608898-ecb7-4081-8174-2b6a52f53b21',
|
||||
conversationId: 'conversation-run-failed',
|
||||
prompt: 'test',
|
||||
workMode: 'ask'
|
||||
},
|
||||
new AbortController().signal
|
||||
)
|
||||
|
||||
await expect(stream.next()).rejects.toBe(failure)
|
||||
expect(observeFailure).toHaveBeenCalledWith({
|
||||
component: 'runtime',
|
||||
stage: 'run',
|
||||
code: 'runtime.run.failed',
|
||||
error: failure
|
||||
})
|
||||
await controller.dispose()
|
||||
})
|
||||
|
||||
it('forwards per-tool authorization without adding a whole-run gate', async () => {
|
||||
const runtime = new TestRuntime(false, false, true)
|
||||
const controller = new AgentRuntimeController(runtime)
|
||||
@@ -490,4 +622,26 @@ describe('AgentRuntimeController', () => {
|
||||
'当前 Runtime 不支持工具执行'
|
||||
)
|
||||
})
|
||||
|
||||
it('reports Runtime boundary failures without request content', async () => {
|
||||
const runtime = new TestRuntime()
|
||||
const failure = new Error('provider raw response: secret prompt')
|
||||
runtime.getStatus = vi.fn(async () => {
|
||||
throw failure
|
||||
})
|
||||
const observeFailure = vi.fn()
|
||||
const controller = new AgentRuntimeController(
|
||||
runtime,
|
||||
undefined,
|
||||
observeFailure
|
||||
)
|
||||
|
||||
await expect(controller.getStatus()).rejects.toBe(failure)
|
||||
expect(observeFailure).toHaveBeenCalledWith({
|
||||
component: 'runtime',
|
||||
stage: 'status',
|
||||
code: 'runtime.operation.failed',
|
||||
error: failure
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -11,6 +11,9 @@ import type {
|
||||
RuntimeConversationCompactOutcome,
|
||||
RuntimeEvent
|
||||
} from './runtime'
|
||||
import type {
|
||||
DesktopDiagnosticFailureObserver
|
||||
} from '../desktop-diagnostics'
|
||||
|
||||
type RuntimeSlot = {
|
||||
runtime: AgentRuntime
|
||||
@@ -80,7 +83,9 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
|
||||
constructor(
|
||||
runtime: AgentRuntime,
|
||||
private readonly shutdownGraceMs = 2_000
|
||||
private readonly shutdownGraceMs = 2_000,
|
||||
private readonly observeFailure?:
|
||||
DesktopDiagnosticFailureObserver
|
||||
) {
|
||||
this.current = {
|
||||
runtime,
|
||||
@@ -201,18 +206,22 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
}
|
||||
|
||||
async getStatus(): Promise<AgentRuntimeStatus> {
|
||||
return this.probeStatus((runtime) => runtime.getStatus())
|
||||
return this.probeStatus(
|
||||
'status',
|
||||
(runtime) => runtime.getStatus()
|
||||
)
|
||||
}
|
||||
|
||||
async testConnection(): Promise<AgentRuntimeStatus> {
|
||||
return this.probeStatus(
|
||||
'connection-test',
|
||||
(runtime) =>
|
||||
runtime.testConnection?.() ?? runtime.getStatus()
|
||||
)
|
||||
}
|
||||
|
||||
async getNativeSnapshot(): Promise<RuntimeNativeSnapshot> {
|
||||
return this.invoke((runtime) => {
|
||||
return this.invoke('native-snapshot', (runtime) => {
|
||||
if (!runtime.getNativeSnapshot) {
|
||||
throw new Error('当前 Runtime 不支持原生能力清单')
|
||||
}
|
||||
@@ -225,18 +234,23 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
signal: AbortSignal
|
||||
): Promise<RuntimeConversationCompactOutcome> {
|
||||
this.ownedConversationIds.add(request.conversationId)
|
||||
return this.invoke((runtime) => {
|
||||
if (!runtime.compactConversation) {
|
||||
throw new Error('当前 Runtime 不支持手动压缩')
|
||||
}
|
||||
return runtime.compactConversation(request, signal)
|
||||
})
|
||||
return this.invoke(
|
||||
'compact',
|
||||
(runtime) => {
|
||||
if (!runtime.compactConversation) {
|
||||
throw new Error('当前 Runtime 不支持手动压缩')
|
||||
}
|
||||
return runtime.compactConversation(request, signal)
|
||||
},
|
||||
signal
|
||||
)
|
||||
}
|
||||
|
||||
private async probeStatus(
|
||||
stage: string,
|
||||
operation: (runtime: AgentRuntime) => Promise<AgentRuntimeStatus>
|
||||
): Promise<AgentRuntimeStatus> {
|
||||
const status = await this.invoke(operation)
|
||||
const status = await this.invoke(stage, operation)
|
||||
return {
|
||||
...status,
|
||||
supportsToolExecution: this.current.runtime.supportsToolExecution
|
||||
@@ -244,7 +258,9 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
}
|
||||
|
||||
private async invoke<T>(
|
||||
operation: (runtime: AgentRuntime) => Promise<T>
|
||||
stage: string,
|
||||
operation: (runtime: AgentRuntime) => Promise<T>,
|
||||
signal?: AbortSignal
|
||||
): Promise<T> {
|
||||
if (this.closing) {
|
||||
throw new Error('Agent Runtime 正在关闭')
|
||||
@@ -257,6 +273,15 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
throw new Error('Runtime 已切换,请重试')
|
||||
}
|
||||
return status
|
||||
} catch (error) {
|
||||
if (
|
||||
slot === this.current &&
|
||||
!signal?.aborted &&
|
||||
!isAbortError(error)
|
||||
) {
|
||||
this.reportFailure(stage, 'runtime.operation.failed', error)
|
||||
}
|
||||
throw error
|
||||
} finally {
|
||||
slot.activeRequests -= 1
|
||||
if (
|
||||
@@ -280,9 +305,21 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
const slot = this.current
|
||||
this.ownedConversationIds.add(request.conversationId)
|
||||
const toolsAllowed = request.workMode === 'execute'
|
||||
let toolDenied = false
|
||||
const effectiveAuthorize: RuntimeAuthorizer | undefined = toolsAllowed
|
||||
? authorize
|
||||
: async () => 'deny'
|
||||
? authorize === undefined
|
||||
? undefined
|
||||
: async (authorizationRequest) => {
|
||||
const decision = await authorize(authorizationRequest)
|
||||
if (decision === 'deny') {
|
||||
toolDenied = true
|
||||
}
|
||||
return decision
|
||||
}
|
||||
: async () => {
|
||||
toolDenied = true
|
||||
return 'deny'
|
||||
}
|
||||
slot.activeRequests += 1
|
||||
try {
|
||||
if (toolsAllowed && !slot.runtime.supportsToolExecution) {
|
||||
@@ -304,18 +341,31 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
throw new Error('用户拒绝了 Agent 工具执行')
|
||||
}
|
||||
}
|
||||
for await (const event of slot.runtime.run(
|
||||
request,
|
||||
signal,
|
||||
effectiveAuthorize
|
||||
)) {
|
||||
try {
|
||||
for await (const event of slot.runtime.run(
|
||||
request,
|
||||
signal,
|
||||
effectiveAuthorize
|
||||
)) {
|
||||
if (slot !== this.current && !slot.drainable) {
|
||||
throw new Error('Runtime 已切换,当前请求已中断')
|
||||
}
|
||||
toolDenied = false
|
||||
yield event
|
||||
}
|
||||
if (slot !== this.current && !slot.drainable) {
|
||||
throw new Error('Runtime 已切换,当前请求已中断')
|
||||
}
|
||||
yield event
|
||||
}
|
||||
if (slot !== this.current && !slot.drainable) {
|
||||
throw new Error('Runtime 已切换,当前请求已中断')
|
||||
} catch (error) {
|
||||
if (
|
||||
!signal.aborted &&
|
||||
!toolDenied &&
|
||||
!isAbortError(error) &&
|
||||
(slot === this.current || slot.drainable)
|
||||
) {
|
||||
this.reportFailure('run', 'runtime.run.failed', error)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
} finally {
|
||||
slot.activeRequests -= 1
|
||||
@@ -329,6 +379,23 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
private reportFailure(
|
||||
stage: string,
|
||||
code: string,
|
||||
error: unknown
|
||||
): void {
|
||||
try {
|
||||
this.observeFailure?.({
|
||||
component: 'runtime',
|
||||
stage,
|
||||
code,
|
||||
error
|
||||
})
|
||||
} catch {
|
||||
// Diagnostics must not alter Runtime behavior.
|
||||
}
|
||||
}
|
||||
|
||||
async releaseConversation(conversationId: string): Promise<void> {
|
||||
const results = await Promise.allSettled([
|
||||
this.current.runtime.releaseConversation?.(conversationId),
|
||||
@@ -549,6 +616,17 @@ export class AgentRuntimeController implements AgentRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
function isAbortError(error: unknown): boolean {
|
||||
if (typeof error !== 'object' || error === null) {
|
||||
return false
|
||||
}
|
||||
try {
|
||||
return 'name' in error && error.name === 'AbortError'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function waitWithin(
|
||||
promise: Promise<void>,
|
||||
timeoutMs: number
|
||||
|
||||
@@ -13,6 +13,9 @@ import type {
|
||||
} from './runtime'
|
||||
import { AgentRuntimeController } from './runtime-controller'
|
||||
import type { ExecutionSpaceDescriptor } from '../execution-space'
|
||||
import type {
|
||||
DesktopDiagnosticFailureObserver
|
||||
} from '../desktop-diagnostics'
|
||||
|
||||
export type SelectedRuntimeResolver = {
|
||||
getRuntime(
|
||||
@@ -70,7 +73,9 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
private readonly maximumCachedRuntimes =
|
||||
DEFAULT_MAXIMUM_CACHED_RUNTIMES,
|
||||
private readonly maximumRetainedRuntimes =
|
||||
DEFAULT_MAXIMUM_RETAINED_RUNTIMES
|
||||
DEFAULT_MAXIMUM_RETAINED_RUNTIMES,
|
||||
private readonly observeFailure?:
|
||||
DesktopDiagnosticFailureObserver
|
||||
) {
|
||||
if (
|
||||
!Number.isSafeInteger(maximumCachedRuntimes) ||
|
||||
@@ -129,7 +134,11 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
await runtime.dispose()
|
||||
throw new Error('Runtime 设置已更改,请重新选择')
|
||||
}
|
||||
const controller = new AgentRuntimeController(runtime)
|
||||
const controller = new AgentRuntimeController(
|
||||
runtime,
|
||||
undefined,
|
||||
this.observeFailure
|
||||
)
|
||||
entry.controller = controller
|
||||
return controller
|
||||
}
|
||||
@@ -148,6 +157,7 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
if (this.entries.get(key) === entry) {
|
||||
this.entries.delete(key)
|
||||
}
|
||||
this.reportFailure('create', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -162,6 +172,9 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
this.tests.add(operation)
|
||||
try {
|
||||
return await operation
|
||||
} catch (error) {
|
||||
this.reportFailure('status', error)
|
||||
throw error
|
||||
} finally {
|
||||
this.tests.delete(operation)
|
||||
}
|
||||
@@ -177,6 +190,9 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
this.tests.add(operation)
|
||||
try {
|
||||
return await operation
|
||||
} catch (error) {
|
||||
this.reportFailure('connection-test', error)
|
||||
throw error
|
||||
} finally {
|
||||
this.tests.delete(operation)
|
||||
}
|
||||
@@ -196,6 +212,9 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
this.snapshots.add(operation)
|
||||
try {
|
||||
return await operation
|
||||
} catch (error) {
|
||||
this.reportFailure('native-snapshot', error)
|
||||
throw error
|
||||
} finally {
|
||||
this.snapshots.delete(operation)
|
||||
}
|
||||
@@ -421,4 +440,17 @@ export class SelectedRuntimeManager implements SelectedRuntimeResolver {
|
||||
void this.startRetiring(entry, false)
|
||||
}
|
||||
}
|
||||
|
||||
private reportFailure(stage: string, error: unknown): void {
|
||||
try {
|
||||
this.observeFailure?.({
|
||||
component: 'runtime',
|
||||
stage,
|
||||
code: 'runtime.operation.failed',
|
||||
error
|
||||
})
|
||||
} catch {
|
||||
// Diagnostics must not alter Runtime behavior.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user