Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(local-agent): 失败轮次的用量先于终态投影

david7e35ec1897ac
diff --git a/apps/desktop/src/main/local-agent/__tests__/runtime.test.ts b/apps/desktop/src/main/local-agent/__tests__/runtime.test.ts
index 02b8c76605..f929233ce5 100644
--- a/apps/desktop/src/main/local-agent/__tests__/runtime.test.ts
+++ b/apps/desktop/src/main/local-agent/__tests__/runtime.test.ts
@@ -232,4 +232,32 @@ describe('native event projection', () => {
expect(emit.mock.calls.map(([event]) => event.type)).toEqual(['usage', 'turn_end']);
expect(emit.mock.calls[0][0].data).toEqual({ input: 7, cached: 0, output: 2 });
});
+ // Claude's failed result: a usage-less terminal error, then the paired done with usage.
+ it('reports a failed turn usage before its end, and still ends when no done follows', () => {
+ vi.useFakeTimers();
+ try {
+ const emit = vi.fn(),
+ p = createEventProjection(emit);
+ p.begin();
+ p.accept({ type: 'error', data: { message: 'failed', isTerminal: true } });
+ p.accept({
+ type: 'done',
+ data: { is_error: true, usageSegments: [{ inputTokens: 4, outputTokens: 1 }] },
+ });
+ expect(emit.mock.calls.map(([event]) => event)).toEqual([
+ { type: 'usage', data: { input: 4, cached: 0, output: 1 } },
+ { type: 'turn_end', data: { status: 'failed', message: 'failed' } },
+ ]);
+ p.begin();
+ emit.mockClear();
+ p.accept({ type: 'error', data: { message: 'stopped', isTerminal: true } });
+ expect(emit).not.toHaveBeenCalled();
+ vi.runAllTimers();
+ expect(emit.mock.calls.map(([event]) => event)).toEqual([
+ { type: 'turn_end', data: { status: 'failed', message: 'stopped' } },
+ ]);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
});
diff --git a/apps/desktop/src/main/local-agent/runtime.ts b/apps/desktop/src/main/local-agent/runtime.ts
index 4fbf388755..680ad377b2 100644
--- a/apps/desktop/src/main/local-agent/runtime.ts
+++ b/apps/desktop/src/main/local-agent/runtime.ts
@@ -18,6 +18,9 @@ const object = (v: unknown): Record<string, unknown> =>
v && typeof v === 'object' ? (v as Record<string, unknown>) : {};
const string = (v: unknown) => (typeof v === 'string' ? v : '');
+/** How long a usage-less terminal error waits for its paired done before ending the turn. */
+const ERROR_USAGE_GRACE_MS = 250;
+
/** Final snapshots must not repeat already-streamed text/thinking. */
export function createEventProjection(
emit: (event: unknown) => void,
@@ -59,8 +62,19 @@ export function createEventProjection(
const ignoredTools = new Set<string>();
const streamed = new Map<string, string>();
let failure: string | undefined;
+ let pendingError: string | undefined;
+ let errorGrace: ReturnType<typeof setTimeout> | undefined;
+ const settleError = () => {
+ clearTimeout(errorGrace);
+ const failed = pendingError;
+ pendingError = undefined;
+ if (failed === undefined || ended) return;
+ ended = true;
+ emit({ type: 'turn_end', data: { status: 'failed', message: failed } });
+ };
return {
begin() {
+ settleError();
textSeen = false;
failure = undefined;
streamed.clear();
@@ -124,18 +138,24 @@ export function createEventProjection(
(typeof d.isTerminal === 'boolean' ? d.isTerminal : d.willRetry !== true) &&
!ended
) {
+ const failed = string(d.message) || message('EXECUTION_FAILED', 'Cindy 执行未完成。');
if (d.usage) {
addEventUsage(d);
reportUsage();
+ } else if (pendingError === undefined) {
+ // Claude reports a failed result as error then a paired done carrying the usage;
+ // other engines may stop at the error, so the wait is bounded.
+ pendingError = failed;
+ errorGrace = setTimeout(settleError, ERROR_USAGE_GRACE_MS);
+ errorGrace.unref?.();
+ return;
}
ended = true;
- emit({
- type: 'turn_end',
- data: {
- status: 'failed',
- message: string(d.message) || message('EXECUTION_FAILED', 'Cindy 执行未完成。'),
- },
- });
+ emit({ type: 'turn_end', data: { status: 'failed', message: failed } });
+ } else if (event.type === 'done' && pendingError !== undefined) {
+ addEventUsage(d);
+ reportUsage();
+ settleError();
} else if (event.type === 'done' && !ended) {
if (!textSeen && string(d.result)) emit({ type: 'text', data: d.result });
addEventUsage(d);
@@ -168,6 +188,7 @@ export function createEventProjection(
if (!ended) failure ??= message;
},
fail(message: string, status: 'failed' | 'interrupted' = 'failed') {
+ settleError();
if (!ended) {
ended = true;
emit({ type: 'turn_end', data: { status, message } });