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(pi): reset diagnostic clocks at native turn boundaries

Dashca9fd69e19ae
diff --git a/apps/desktop/src/main/usage/__tests__/organizationQuotaApi.test.ts b/apps/desktop/src/main/usage/__tests__/organizationQuotaApi.test.ts
index f84adeb451..3f50577be9 100644
--- a/apps/desktop/src/main/usage/__tests__/organizationQuotaApi.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/organizationQuotaApi.test.ts
@@ -1,8 +1,8 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-const mocks = vi.hoisted(() => ({ fetch: vi.fn(), error: vi.fn(), warn: vi.fn() }));
+const mocks = vi.hoisted(() => ({ fetch: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }));
vi.mock('electron', () => ({ net: { fetch: mocks.fetch } }));
vi.mock('../../authManager', () => ({ getAccessToken: () => 'fixture-token', refresh: async () => false, invalidateSession: vi.fn() }));
-vi.mock('../../logger', () => ({ createLogger: () => ({ error: mocks.error, warn: mocks.warn }) }));
+vi.mock('../../logger', () => ({ createLogger: () => ({ error: mocks.error, warn: mocks.warn, debug: mocks.debug }) }));
vi.mock('../../i18n.js', () => ({ getResolvedMainLocale: () => 'en' }));
vi.mock('../../clientEndpointsService.js', () => ({ getClientEndpoint: () => 'https://models.example.test' }));
import { readOrganizationAccountUsage } from '../organizationAccountUsage';
@@ -11,7 +11,7 @@ describe('quota through real server API client', () => {
it('preserves the explicit Nova unsupported signal through redaction', async () => {
mocks.fetch.mockResolvedValue({ ok: false, status: 501, json: async () => ({ error: { code: 'MODEL_ACCESS_QUOTA_NOT_SUPPORTED', message: 'private-upstream-detail' } }) });
expect(await readOrganizationAccountUsage()).toBeUndefined();
- expect(JSON.stringify([mocks.error.mock.calls, mocks.warn.mock.calls])).not.toContain('private-upstream-detail');
+ expect(JSON.stringify([mocks.error.mock.calls, mocks.warn.mock.calls, mocks.debug.mock.calls])).not.toContain('private-upstream-detail');
});
it('does not allow an unrelated 501 to activate legacy credentials', async () => {
mocks.fetch.mockResolvedValue({ ok: false, status: 501, json: async () => ({ error: { code: 'OTHER_UNSUPPORTED', message: 'private-upstream-detail' } }) });
diff --git a/docs/dev-rules/pi-harness.md b/docs/dev-rules/pi-harness.md
index 0fc2684367..7c651df892 100644
--- a/docs/dev-rules/pi-harness.md
+++ b/docs/dev-rules/pi-harness.md
@@ -108,6 +108,8 @@ Cindy 以 `pi --mode rpc` spawn pi 二进制(JSONL/stdio),`translator.ts` 把 pi
(首个文本/推理/工具增量)、`agent-settled`(原生整轮终态,不能用 `agent_end` 替代)。
请求阶段时长用宿主单调时钟,`elapsedMs` 从最近一次请求摘要到达起算;终态另有
`turnElapsedMs`,从本轮第一次请求摘要到达起算,包含重试/工具等后续处理。
+原生 `agent_start` 清空上一轮遗留计时,即使上一轮因中止/重试耗尽缺少终态,
+下一轮也不继承旧起点;同轮请求重试保留本轮起点。
这是请求构造钩子的观测时间,不是 socket 发出时间、上游接收时间或插件收到答复时间;
后加载的用户扩展仍可能改写请求。没有测点的 SSH/旧运行时不编造该阶段。
只记录有界计数和已知档位名,无法识别的请求字段省略,不把未知工具数当成零。
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
index 924d4911ba..c9d6e1561f 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
@@ -3389,6 +3389,31 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {
expect(last).toEqual({ startupTraceId: resolverOptions?.startupTraceId, requestSequence: 2,
stage: 'provider-request-prepared', wireEffort: null });
expect(JSON.stringify(debug.mock.calls.filter(([message]) => message === 'pi request stage'))).not.toContain('private');
+
+ // An aborted/retry-exhausted turn can omit agent_settled. A new native
+ // turn must discard its clock, while repeated requests in that turn retain it.
+ const clock = vi.spyOn(performance, 'now').mockReturnValue(5_000);
+ try {
+ knobs.onEvent?.({ type: 'agent_start' });
+ const beforeRequest = debug.mock.calls.filter(([message]) => message === 'pi request stage').length;
+ knobs.onEvent?.(delta);
+ expect(debug.mock.calls.filter(([message]) => message === 'pi request stage')).toHaveLength(beforeRequest);
+ notify(`${token}:{"wireEffort":"low"}`);
+ knobs.onEvent?.(delta);
+ clock.mockReturnValue(5_010);
+ knobs.onEvent?.({ type: 'auto_retry_start', attempt: 1 });
+ notify(`${token}:{"wireEffort":"low"}`);
+ knobs.onEvent?.(delta);
+ clock.mockReturnValue(5_025);
+ knobs.onEvent?.({ type: 'agent_settled' });
+ const retryStages = debug.mock.calls.filter(([message]) => message === 'pi request stage').slice(beforeRequest).map(([, fields]) => fields);
+ expect(retryStages.map(row => row.stage)).toEqual([
+ 'provider-request-prepared', 'first-output', 'provider-request-prepared', 'first-output', 'agent-settled',
+ ]);
+ expect(retryStages.at(-1)).toMatchObject({ requestSequence: 4, elapsedMs: 15, turnElapsedMs: 25 });
+ } finally {
+ clock.mockRestore();
+ }
} finally {
await handle.close();
}
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 63d7b00a75..c28ff6999b 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -5759,6 +5759,14 @@ export class PiAgent extends BaseAgent {
transport,
logger: this.deps.logger,
onEvent: (event: PiRpcEvent) => {
+ // Native retries remain within one agent_start/settled lifecycle.
+ // A new start also recovers diagnostics if the previous turn omitted
+ // settled (abort/retry exhaustion), without changing turn execution.
+ if (event.type === 'agent_start') {
+ providerRequestObservedAt = undefined;
+ providerTurnObservedAt = undefined;
+ providerFirstOutputLogged = false;
+ }
if (!remote && event.type === 'extension_ui_request' && event.method === 'notify' &&
typeof event.message === 'string' && event.message.startsWith(CINDY_PI_REQUEST_DIAGNOSTICS_PREFIX)) {
const prefix = `${CINDY_PI_REQUEST_DIAGNOSTICS_PREFIX}${runtimeInstanceId}:`;