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): simplify bounded cache recovery and sync main

xuzini-xzn855bc0719097
diff --cc packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts
index 07810274f3,0000000000..f4b9fe2141
mode 100644,000000..100644
--- a/packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts
@@@ -1,415 -1,0 +1,443 @@@
+import { runInNewContext } from 'node:vm';
+import { createHash, randomUUID } from 'node:crypto';
+import path from 'node:path';
+import { describe, expect, it, vi } from 'vitest';
+import { PI_CACHE_RECOVERY_SOURCE } from '../cache-recovery-source.js';
+
+// Unit: real generated provider wrapper, not a duplicate classifier. Protects
+// opaque-400 recovery, one input budget, final hook comparison and TTL storage.
+const model = { provider: 'fixture', id: 'wire', api: 'openai-completions', baseUrl: 'http://127.0.0.1/v1' };
+const routeKey = 'a'.repeat(64);
+const key = JSON.stringify(['connection', 'wire', routeKey]);
+const body = { model: 'wire', messages: [{ role: 'user', content: 'OK' }], prompt_cache_key: 'cache', prompt_cache_retention: '24h' };
+const failure = (text = '400: {"error":{"message":"opaque refusal","param":null,"type":"invalid_request_error"}}') => ({
+ type: 'error', error: { role: 'assistant', content: [], stopReason: 'error', errorMessage: text, usage: { output: 0 } },
+});
+const success = { type: 'done', message: { stopReason: 'stop', content: [{ type: 'text', text: 'OK' }], usage: { input: 3, output: 1, cacheRead: 2 } } };
- function fixture() {
++const modernRegistry = { getProvider() {}, getRegisteredNativeProvider() {} };
++function fixture(legacy = false) {
+ let now = 1000000000;
+ const files = new Map<string, string>();
+ const handlers = new Map<string, (...args: any[]) => any>();
+ const rules = [{ ...model, providerId: 'connection', routeKey }];
+ files.set('/agent/pi-cache-compatibility.json', JSON.stringify(rules));
+ let writeFails = false;
+ const fs = {
+ statSync: (name: string) => {
+ if (!files.has(name)) throw Object.assign(new Error('missing'), { code: 'ENOENT' });
+ return { size: Buffer.byteLength(files.get(name)!), mtimeMs: 1000 };
+ },
+ readFileSync: (name: string) => { fs.statSync(name); return files.get(name); },
+ writeFileSync: (name: string, text: string) => { if (writeFails) throw new Error('read-only'); files.set(name, text); },
+ renameSync: (from: string, to: string) => { files.set(to, files.get(from)!); files.delete(from); },
+ rmSync: (name: string) => { files.delete(name); },
+ };
+ const api = runInNewContext(PI_CACHE_RECOVERY_SOURCE + '\ninstallCindyCacheRecovery(pi)', {
+ pi: { on: (name: string, fn: (...args: any[]) => void) => handlers.set(name, fn) },
+ process: { env: { PI_CODING_AGENT_DIR: '/agent', CINDY_PI_CACHE_COMPAT_FILE: '/compat.json' } },
+ // The in-memory filesystem uses POSIX keys on every host, including Windows.
+ ...fs, path: path.posix, createHash, randomUUID, Buffer, setTimeout, clearTimeout, Date: { now: () => now }, homedir: () => '/home/fixture',
+ });
+ const startUser = (direct = true, followUp = false) => {
+ if (direct) handlers.get('before_agent_start')?.({});
+ handlers.get('message_start')?.({ message: { role: 'user', content: [{ type: 'text', text: 'OK' }] } }, {
- mode: 'rpc', ui: { input: async (_title: string, raw: string) => JSON.stringify({ key: JSON.parse(raw).key, followUp }) },
++ mode: 'rpc', modelRegistry: legacy ? {} : modernRegistry,
++ ui: { input: async (_title: string, raw: string) => JSON.stringify({ key: JSON.parse(raw).key, followUp }) },
+ });
+ };
+ const fresh = () => { handlers.get('input')!({ source: 'rpc' }, { isIdle: () => true }); startUser(); };
+ const interrupt = () => handlers.get('input')!({ source: 'rpc', streamingBehavior: 'steer' }, { isIdle: () => false });
+ const sent: any[] = [];
+ let invocation = 0;
+ const request = async (events: any[] = [failure(), success], options: any = {}, prepare?: (n: number) => void, selectedModel = model) => {
+ const invoke = async function* (opts: any) {
+ const n = invocation++;
+ prepare?.(n);
+ try {
+ const payload = await opts.onPayload(structuredClone(body), selectedModel);
+ sent.push(payload);
+ } catch (error) {
+ yield failure(String(error));
+ return;
+ }
+ const event = events[n] ?? events.at(-1);
+ if (Array.isArray(event)) yield* event; else yield event;
+ };
+ const output = [];
+ for await (const event of api.wrap(selectedModel, options, invoke)) output.push(event);
+ return output;
+ };
+ return { api, handlers, fresh, startUser, interrupt, request, files, sent, clock: (value: number) => { now = value; },
+ failWrites: () => { writeFails = true; }, record: () => JSON.parse(files.get('/compat.json') ?? '{"routes":{}}').routes[key] };
+}
+
+describe('Pi native cache recovery', () => {
+ it.each(['openai-completions', 'openai-responses'])('keeps learned payload recovery without taking provider ownership on an older %s registry', async api => {
- const h = fixture();
++ const h = fixture(true);
+ const selected = { ...model, api };
+ const rules = [{ ...selected, providerId: 'connection', routeKey }];
+ h.files.set('/agent/pi-cache-compatibility.json', JSON.stringify(rules));
+ h.fresh();
+ const record = { supportsLongCacheRetention: false, learnedAt: 1000000000,
+ expiresAt: 1086400000, evidence: 'opaque' };
+ // Another session can learn after the last RPC input. Native continuation
+ // has no new RPC input event, but its real user message starts a new input.
+ h.files.set('/compat.json', JSON.stringify({ routes: { [key]: record } }));
+ h.startUser();
+ const ctx = { model: selected, modelRegistry: { getProvider: vi.fn() } };
+ const hook = h.handlers.get('before_provider_request')!;
+ const payload = { ...body, service_tier: 'priority' };
+ expect((await hook?.({ payload }, ctx)) ?? payload).toEqual({ model: 'wire', messages: body.messages, service_tier: 'priority' });
+ expect(ctx.modelRegistry.getProvider).not.toHaveBeenCalled();
+ expect(payload).toHaveProperty('prompt_cache_key', 'cache');
+ // A newly loaded bridge reads the same durable record, not process memory.
- const restarted = fixture();
++ const restarted = fixture(true);
+ restarted.files.set('/agent/pi-cache-compatibility.json', JSON.stringify(rules));
+ restarted.files.set('/compat.json', h.files.get('/compat.json')!);
+ restarted.fresh();
+ const recovered = restarted.handlers.get('before_provider_request')!;
+ expect(await recovered({ payload }, ctx)).not.toHaveProperty('prompt_cache_key');
+ expect(await recovered({ payload }, { ...ctx, model: { ...selected, baseUrl: selected.baseUrl + '/other' } })).toBeUndefined();
+ expect(await recovered({ payload }, { ...ctx, modelRegistry: { ...ctx.modelRegistry, getRegisteredNativeProvider: vi.fn() } })).toBeUndefined();
+ restarted.clock(1086400001);
+ // Expiry cannot change a tool continuation in the already-started input.
+ restarted.handlers.get('tool_execution_start')!();
+ expect(await recovered({ payload }, ctx)).not.toHaveProperty('prompt_cache_key');
+ restarted.startUser();
+ expect(await recovered({ payload }, ctx)).toBeUndefined();
+ expect(restarted.record()).toEqual(record);
+ });
+
+ it('preserves normal caching and bounds legacy records in the older registry payload hook', async () => {
- const h = fixture();
++ const h = fixture(true);
+ const ctx = { model, modelRegistry: {} };
+ const hook = () => h.handlers.get('before_provider_request')?.({ payload: body }, ctx);
+ h.fresh();
+ expect(await hook()).toBeUndefined();
+ h.files.set('/compat.json', JSON.stringify({ routes: { [key]: { supportsLongCacheRetention: false } } }));
+ h.clock(1001); h.fresh();
+ expect(await hook()).toEqual({ model: 'wire', messages: body.messages });
+ h.clock(1000 + 7 * 86400000 + 1); h.fresh();
+ expect(await hook()).toBeUndefined();
+ });
+
+ it('does one final-payload comparison, learns only after generation, and preserves usage', async () => {
+ const h = fixture(); h.fresh();
+ const hook = vi.fn((payload: any) => ({ ...payload, third_party: 'preserved', service_tier: 'priority' }));
+ const result = await h.request(undefined, { onPayload: hook });
+ expect(h.sent).toHaveLength(2);
+ expect(h.sent[0]).toHaveProperty('prompt_cache_retention', '24h');
+ expect(h.sent[1]).toEqual({ model: 'wire', messages: body.messages, third_party: 'preserved', service_tier: 'priority' });
+ expect(result).toEqual([success]);
+ expect(hook).toHaveBeenCalledTimes(2);
+ expect(h.record()).toMatchObject({ evidence: 'opaque', learnedAt: 1000000000, expiresAt: 1086400000 });
+ h.fresh(); await h.request([success]);
+ expect(h.sent[2]).not.toHaveProperty('prompt_cache_key');
+ h.clock(1086400001); h.fresh(); await h.request([success]);
+ expect(h.sent[3]).toHaveProperty('prompt_cache_key');
+ });
+
+ it('stops after two 400s without learning; a new input carries cache again', async () => {
+ const h = fixture(); h.fresh();
+ expect(await h.request([failure(), failure()])).toEqual([expect.objectContaining({ error: expect.objectContaining({ cindyCacheRecoveryHandled: true }) })]);
+ expect(h.sent).toHaveLength(2); expect(h.record()).toBeUndefined();
+ await h.request([failure()]); // Native retry in the same input cannot refresh the budget.
+ expect(h.sent).toHaveLength(3);
+ expect(h.sent[2]).not.toHaveProperty('prompt_cache_key');
+ h.fresh(); await h.request([success]);
+ expect(h.sent[3]).toHaveProperty('prompt_cache_key');
+ });
+
+ // PR #7561 P1: input queuing is not execution; Pi emits user message_start
+ // (without agent_start) when followUp actually begins. Unit production wrapper.
+ it('arms one budget only when a queued user input actually starts', async () => {
+ const h = fixture(); h.fresh();
+ await h.request([failure()], {}, () => {
+ h.handlers.get('input')!({ source: 'rpc', streamingBehavior: 'followUp' }, { isIdle: () => false });
+ });
+ expect(h.sent).toHaveLength(1); expect(h.record()).toBeUndefined();
+ h.startUser(false, true);
+ await h.request([failure()]);
+ expect(h.sent).toHaveLength(3);
+ expect(h.sent[1]).toHaveProperty('prompt_cache_key');
+ expect(h.sent[2]).not.toHaveProperty('prompt_cache_key');
+ for (const role of ['assistant', 'toolResult', 'system']) h.handlers.get('message_start')?.({ message: { role } });
+ h.handlers.get('turn_start')?.({});
+ await h.request([failure()]);
+ expect(h.sent).toHaveLength(4);
+ expect(h.sent[3]).not.toHaveProperty('prompt_cache_key');
+ expect(h.record()).toBeUndefined();
+ h.startUser(); await h.request([failure(), failure(), failure(), failure(), failure(), success]);
+ expect(h.sent).toHaveLength(6);
+ expect(h.record()).toMatchObject({ evidence: 'opaque' });
+ });
+
+ it('does not reopen recovery when an actual steer user message is consumed', async () => {
+ const h = fixture(); h.fresh();
+ h.handlers.get('tool_execution_start')!();
+ h.interrupt(); h.startUser(false);
+ await h.request([failure(), success]);
+ expect(h.sent).toHaveLength(1);
+ expect(h.sent[0]).toHaveProperty('prompt_cache_key');
+ expect(h.record()).toBeUndefined();
+ // A later independently consumed followUp still owns its budget.
+ h.startUser(false, true);
+ await h.request([failure(), failure(), success]);
+ expect(h.sent).toHaveLength(3);
+ expect(h.record()).toMatchObject({ evidence: 'opaque' });
+ });
+
+ it('keeps the continuing snapshot when steer is consumed across its TTL', async () => {
+ const h = fixture();
+ const record = { supportsLongCacheRetention: false, learnedAt: 1000000000, expiresAt: 1086400000, evidence: 'opaque' };
+ h.files.set('/compat.json', JSON.stringify({ routes: { [key]: record } }));
+ h.fresh();
+ h.handlers.get('tool_execution_start')!();
+ h.clock(1086400001); h.interrupt(); h.startUser(false);
+ await h.request([success]);
+ expect(h.sent[0]).not.toHaveProperty('prompt_cache_key');
+ const hook = h.handlers.get('before_provider_request')!;
+ expect(await hook({ payload: body }, { model, modelRegistry: {} })).not.toHaveProperty('prompt_cache_key');
+ h.startUser(false, true); await h.request([success]);
+ expect(h.sent[1]).toHaveProperty('prompt_cache_key');
+ });
+
+ it('does not arm from a stale receipt or a followUp cancelled while its receipt is pending', async () => {
+ for (const cancel of [false, true]) {
+ const h = fixture(); h.fresh(); h.interrupt();
+ h.handlers.get('message_start')!({ message: { role: 'user', content: [] } }, { mode: 'rpc', ui: {
+ input: async (_title: string, raw: string) => {
+ if (cancel) h.handlers.get('tool_execution_start')!();
+ return JSON.stringify({ key: cancel ? JSON.parse(raw).key : 'stale', followUp: true });
+ },
+ } });
+ await h.request([failure(), success]);
+ expect(h.sent).toHaveLength(1);
+ expect(h.record()).toBeUndefined();
+ }
+ });
+
+ it.each([true, false].flatMap(direct => [{ direct, mode: 'rpc' }, { direct, mode: undefined }]))(
+ 'binds $direct input receipts before message_end replaces the message (mode: $mode)', async ({ direct, mode }) => {
+ const h = fixture(); h.fresh(); h.interrupt();
+ if (direct) h.handlers.get('before_agent_start')!({});
+ const message = { role: 'user', content: [{ type: 'text', text: 'accepted input' }], timestamp: 123 };
+ const key = createHash('sha256').update(JSON.stringify(message)).digest('hex');
+ const query = vi.fn(async () => JSON.stringify({ key, followUp: !direct }));
- h.handlers.get('message_start')!({ message }, { mode, ui: { input: query } });
++ h.handlers.get('message_start')!({ message }, { mode, modelRegistry: modernRegistry, ui: { input: query } });
+ // Pi's message_end replacement mutates the same message object in place.
+ Object.assign(message, { content: [{ type: 'text', text: 'extension replacement' }], timestamp: 456 });
+ await h.request([failure(), success]);
- expect(query).toHaveBeenCalledWith('cindy:cache-input', JSON.stringify({ key, direct }));
++ if (direct) expect(query).not.toHaveBeenCalled();
++ else expect(query).toHaveBeenCalledWith('cindy:cache-input', JSON.stringify({ key, direct: false }));
+ expect(h.sent).toHaveLength(2);
+ expect(h.record()).toMatchObject({ evidence: 'opaque' });
+ },
+ );
+
++ it('keeps modern ordinary input recovery independent of the queue receipt service', async () => {
++ const h = fixture(); h.fresh();
++ const query = vi.fn(() => { throw new Error('queue service unavailable'); });
++ h.handlers.get('before_agent_start')!({});
++ h.handlers.get('message_start')!({ message: { role: 'user', content: 'direct' } }, { mode: 'rpc', modelRegistry: modernRegistry, ui: { input: query } });
++ expect(await h.request([failure(), success])).toEqual([success]);
++ expect(query).not.toHaveBeenCalled();
++ expect(h.sent).toHaveLength(2);
++ expect(h.record()).toMatchObject({ evidence: 'opaque' });
++ });
++
++ it('retains exact direct-input acknowledgement only for older host recovery', async () => {
++ const h = fixture(true);
++ const message = { role: 'user', content: 'legacy direct' };
++ const key = createHash('sha256').update(JSON.stringify(message)).digest('hex');
++ const query = vi.fn(async () => JSON.stringify({ key, followUp: false }));
++ h.handlers.get('before_agent_start')!({});
++ h.handlers.get('message_start')!({ message }, { mode: 'rpc', modelRegistry: {}, ui: { input: query } });
++ await h.handlers.get('before_provider_request')!({ payload: body }, { model, modelRegistry: {} });
++ expect(query).toHaveBeenCalledWith('cindy:cache-input', JSON.stringify({ key, direct: true }));
++ expect(await h.request([failure(), success])).toEqual([success]);
++ expect(query).toHaveBeenCalledTimes(1);
++ expect(h.sent).toHaveLength(2);
++ });
++
+ it('does not ask an interactive runtime to confirm queued provenance', async () => {
+ const h = fixture(); h.fresh(); h.interrupt();
+ const query = vi.fn();
+ h.handlers.get('message_start')!({ message: { role: 'user', content: [] } }, { mode: 'interactive', ui: { input: query } });
+ await h.request([failure(), success]);
+ expect(query).not.toHaveBeenCalled();
+ expect(h.sent).toHaveLength(1);
+ expect(h.record()).toBeUndefined();
+ });
+
+ // PR #7561 P1: freeze learned state across tool calls, including the case
+ // where the TTL passes after input start but before the first provider call.
+ it('freezes learned cache removal until the next real user input', async () => {
+ const h = fixture();
+ h.files.set('/compat.json', JSON.stringify({ routes: { [key]: { supportsLongCacheRetention: false,
+ learnedAt: 1000000000, expiresAt: 1086400000, evidence: 'opaque' } } }));
+ h.fresh(); h.clock(1086400001);
+ await h.request([success]);
+ h.handlers.get('tool_execution_start')!();
+ await h.request([success]);
+ expect(h.sent).toHaveLength(2);
+ for (const payload of h.sent) expect(payload).toEqual({ model: 'wire', messages: body.messages });
+ h.startUser(); await h.request([success]);
+ expect(h.sent[2]).toEqual(body);
+ });
+
+ it('treats an unfamiliar gateway error type as opaque without a vendor allowlist', async () => {
+ const h = fixture(); h.fresh();
+ await h.request([failure('400: {"error":{"type":"new_gateway_validation_shape","message":"请求不满足要求"}}'), success]);
+ expect(h.sent).toHaveLength(2);
+ expect(h.record()).toMatchObject({ evidence: 'opaque', expiresAt: 1086400000 });
+ });
+
+ it.each(['401: unauthorized', '429: limited', '500: broken', 'text says 400: failure',
+ '400: {"error":{"param":"temperature","message":"invalid"}}',
+ '400: {"error":{"code":"context_length_exceeded","message":"opaque"}}',
+ '400: {"error":{"type":"authentication_error","message":"opaque"}}',
+ '400: Invalid value for prompt_cache_retention', '400: 模型不存在'])('leaves known unrelated failures alone: %s', async text => {
+ const h = fixture(); h.fresh(); await h.request([failure(text)]);
+ expect(h.sent).toHaveLength(1); expect(h.record()).toBeUndefined();
+ });
+
+ it.each([
+ { code: 'missing_required_parameter', message: 'Request validation failed' },
+ { type: 'invalid_type', message: 'Request validation failed' },
+ { code: 'invalid_type', param: null, message: 'Request validation failed' },
+ { message: "Missing required parameter: 'messages'." },
+ { message: 'messages: Field required' },
+ { message: 'Required field messages is missing' },
+ { message: 'Invalid type for messages' },
+ { message: 'Invalid parameter: temperature' },
+ { code: 'invalid_parameter', param: null, message: 'Invalid parameter: future_option' },
+ { message: "Parameter 'temperature' is invalid" },
+ { message: 'Invalid parameters: prompt_cache_key, temperature' },
+ ])('does not trial explicit required/type/other-field validation: %j', async error => {
+ const h = fixture(); h.fresh();
+ const event = failure('400: ' + JSON.stringify({ error }));
+ expect(await h.request([event, success])).toEqual([expect.objectContaining({ error: expect.objectContaining({ errorMessage: event.error.errorMessage }) })]);
+ expect(h.sent).toHaveLength(1); expect(h.record()).toBeUndefined();
+ });
+
+ it.each([
+ { code: 'invalid_parameter', message: 'Request validation failed' },
+ { type: 'invalid_parameter', param: null, message: 'Request validation failed' },
+ { message: 'Invalid parameter: prompt_cache_key' },
+ ])('keeps a non-specific validation label eligible for bounded contrast: %j', async error => {
+ const h = fixture(); h.fresh();
+ await h.request([failure('400: ' + JSON.stringify({ error })), success]);
+ expect(h.sent).toHaveLength(2);
+ expect(h.record()).toMatchObject({ evidence: 'opaque', expiresAt: 1086400000 });
+ });
+
+ it.each([
+ 'Validation: Unsupported parameter(s): temperature',
+ 'Unknown field: future_vendor_option',
+ "Unsupported parameter(s): ['temperature']",
+ "Unsupported parameter(s): ['prompt_cache_key', 'temperature']",
+ 'Unsupported parameters: prompt_cache_key, temperature',
+ "Unsupported parameter(s): 'prompt_cache_key', 'future_vendor_option'",
+ '未知请求字段:prompt_cache_key,temperature',
+ "The parameter 'temperature' is not supported",
+ '未知请求字段:temperature',
+ '参数 temperature 不支持',
+ 'Unsupported parameter: prompt_cache_key; unsupported parameter: temperature',
+ ])('does not trial an explicitly rejected other field without structured param: %s', async message => {
+ const h = fixture(); h.fresh();
+ await h.request([failure('400: ' + JSON.stringify({ error: { type: 'invalid_request_error', param: null, message } })), success]);
+ expect(h.sent).toHaveLength(1);
+ expect(h.record()).toBeUndefined();
+ });
+
+ it.each(['Validation: Unsupported parameter(s): prompt_cache_key',
+ 'Unsupported parameter: prompt_cache_retention.',
+ "The parameter 'prompt_cache_key' is not supported",
+ '未知请求字段:prompt_cache_retention',
+ "Unsupported parameter(s): ['prompt_cache_key', 'prompt_cache_retention']",
+ 'Unsupported parameters: prompt_cache_key, prompt_cache_retention',
+ "Unsupported parameter(s): 'prompt_cache_key', 'prompt_cache_retention'",
+ 'Unsupported parameter: prompt_cache_key, use prompt_cache_options instead',
+ 'Unsupported parameter: prompt_cache_key, please use a supported option',
+ '未知请求字段:prompt_cache_key,请改用 prompt_cache_options',
+ ])('still learns explicit cache rejection: %s', async message => {
+ const h = fixture(); h.fresh();
+ await h.request([failure('400: ' + JSON.stringify({ error: { param: null, message } })), success]);
+ expect(h.sent).toHaveLength(2);
+ expect(h.record()).toMatchObject({ evidence: 'explicit', expiresAt: 1604800000 });
+ });
+
+ it.each(['start', 'thinking_delta', 'toolcall_start'])('never retries after %s or a prior tool', async type => {
+ const h = fixture(); h.fresh(); await h.request([[{ type }, failure()]]);
+ expect(h.sent).toHaveLength(1); expect(h.record()).toBeUndefined();
+ h.fresh(); h.handlers.get('tool_execution_start')!(); await h.request([failure()]);
+ expect(h.sent).toHaveLength(2);
+ });
+
+ it('does not send a changed second request, even when the later payload hook changes it', async () => {
+ const h = fixture(); h.fresh(); let n = 0;
+ const result = await h.request(undefined, { onPayload: (p: any) => ({ ...p, temperature: n++ }) });
+ expect(h.sent).toHaveLength(1);
+ expect(result[0].error.errorMessage).toContain('opaque refusal');
+ expect(h.record()).toBeUndefined();
+ });
+
+ it('respects stop and queued input while a payload hook is awaiting', async () => {
+ for (const cancel of [true, false]) {
+ const h = fixture(); h.fresh(); const controller = new AbortController(); let n = 0;
+ await h.request(undefined, { signal: controller.signal, onPayload: async (p: any) => {
+ if (++n === 2) { if (cancel) controller.abort(); else h.interrupt(); }
+ return p;
+ } });
+ expect(h.sent).toHaveLength(1); expect(h.record()).toBeUndefined();
+ }
+ });
+
+ it('does not retry when a later hook changes authentication even if the body is identical', async () => {
+ const h = fixture(); h.fresh(); let n = 0;
+ const headers = { authorization: 'fixture-a' };
+ await h.request(undefined, { headers, onPayload: (payload: any) => {
+ if (++n === 2) headers.authorization = 'fixture-b';
+ return payload;
+ } });
+ expect(h.sent).toHaveLength(1);
+ expect(h.record()).toBeUndefined();
+ });
+
+ it('does not learn from a stream failure or let a storage error undo a good response', async () => {
+ const h = fixture(); h.fresh(); await h.request([failure(), [{ type: 'start' }, failure('stream error')]]);
+ expect(h.record()).toBeUndefined();
+ const good = fixture(); good.failWrites(); good.fresh();
+ expect(await good.request()).toEqual([success]); expect(good.record()).toBeUndefined();
+ good.fresh(); await good.request([success]);
+ expect(good.sent[2]).not.toHaveProperty('prompt_cache_key');
+ });
+
+ it('gives explicit rejection a finite seven-day record and isolates protocols', async () => {
+ const h = fixture(); h.fresh();
+ await h.request([failure('400: {"code":"UNKNOWN_FIELD","data":{"field":"prompt_cache_key"}}'), success]);
+ expect(h.record()).toMatchObject({ evidence: 'explicit', expiresAt: 1604800000 });
+ expect(h.api.accepts({ ...model, api: 'openai-responses' })).toBe(false);
+ expect(h.api.accepts({ ...model, baseUrl: model.baseUrl + '/changed' })).toBe(false);
+ });
+
+ it('preserves malformed files and bounds the lifetime of legacy records', async () => {
+ const h = fixture(); h.files.set('/compat.json', '{bad'); h.fresh(); await h.request();
+ expect(h.files.get('/compat.json')).toBe('{bad');
+ const legacy = fixture(); legacy.files.set('/compat.json', JSON.stringify({ routes: { [key]: { supportsLongCacheRetention: false } } }));
+ legacy.clock(1001); legacy.fresh(); await legacy.request([success]);
+ expect(legacy.sent[0]).not.toHaveProperty('prompt_cache_key');
+ legacy.clock(1000 + 7 * 86400000 + 1); legacy.fresh(); await legacy.request([success]);
+ expect(legacy.sent[1]).toHaveProperty('prompt_cache_key');
+ });
+
+ it('ignores malformed route entries and invalid expiry instead of disabling cache forever', async () => {
+ const h = fixture();
+ const rules = JSON.parse(h.files.get('/agent/pi-cache-compatibility.json')!);
+ h.files.set('/agent/pi-cache-compatibility.json', JSON.stringify([null, false, ...rules]));
+ h.files.set('/compat.json', JSON.stringify({ routes: { [key]: { supportsLongCacheRetention: false, learnedAt: 1, evidence: 'opaque' } } }));
+ h.fresh(); await h.request([success]);
+ expect(h.sent[0]).toHaveProperty('prompt_cache_key');
+ });
+});
diff --cc packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
index c6f4abbf24,e67b608d3f..e86ae22016
--- a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
@@@ -1387,56 -1387,6 +1387,61 @@@ describe("Pi provider-aware model routi
await handle.close();
});
+ it.each(['send', 'native', 'queued'])('freezes per-input cache learning across expiry and relearning: %s', async (mode) => {
+ let active = true;
+ const deps = byomDeps(async () => ({ providers: [{
+ id: 'native-a', name: 'Native A', baseUrl: 'https://cache.example/v1', api: 'openai-completions',
+ models: [{ id: 'local-model' }],
+ }], env: {} }));
- deps.resolvePiNativeCompatOverride = () => active ? { supportsLongCacheRetention: false } : undefined;
++ deps.resolvePiNativeCompatOverride = vi.fn(() => active ? { supportsLongCacheRetention: false } : undefined);
+ const handle = await new PiAgent(deps).startSession({
+ sessionId: 'cache-expiry-receipt', workingDir: cwd, model: 'local-model', providerId: 'native-a',
+ });
+ const events = handle.events()[Symbol.asyncIterator]();
+ try {
++ expect(deps.resolvePiNativeCompatOverride).not.toHaveBeenCalled();
+ for (const expected of [true, false, true]) {
+ active = expected;
+ if (mode === 'send') await handle.send({ type: 'user', content: 'try cache' });
+ if (mode !== 'queued') captured.onEvent!({ type: 'agent_start' });
+ if (mode === 'queued') {
+ captured.onEvent!({ type: 'queue_update', steering: [], followUp: ['try cache'] });
+ captured.onEvent!({ type: 'queue_update', steering: [], followUp: [] });
+ }
+ const message = { role: 'user', content: [{ type: 'text', text: 'try cache' }] };
+ captured.onEvent!({ type: 'message_start', message });
+ const key = createHash('sha256').update(JSON.stringify(message)).digest('hex');
++ // Older registries still acknowledge the exact direct-input snapshot.
+ captured.onEvent!({ type: 'extension_ui_request', method: 'input', title: 'cindy:cache-input',
+ id: 'cache-input', placeholder: JSON.stringify({ key, direct: mode !== 'queued' }) });
+ // Expiry or another session's learning during the request must not
+ // change whether this input actually started with the override.
+ active = !expected;
++ captured.onEvent!({ type: 'agent_start' }); // Pi's native retry, no new user.
++ captured.onEvent!({ type: 'message_start', message: { role: 'assistant', content: [] } });
+ // Consumed steer is another user message in the same execution. It
+ // must neither refresh the receipt nor treat an auto-retry as input.
++ captured.onEvent!({ type: 'agent_start' }); // continue() may flush steering as its first message.
+ captured.onEvent!({ type: 'queue_update', steering: ['steer'], followUp: [] });
+ captured.onEvent!({ type: 'queue_update', steering: [], followUp: [] });
+ const steer = { role: 'user', content: [{ type: 'text', text: 'steer' }] };
+ captured.onEvent!({ type: 'message_start', message: steer });
+ captured.onEvent!({ type: 'extension_ui_request', method: 'input', title: 'cindy:cache-input',
+ id: 'cache-steer', placeholder: JSON.stringify({ key: createHash('sha256').update(JSON.stringify(steer)).digest('hex'), direct: false }) });
+ captured.onEvent!({ type: 'agent_start' });
+ captured.onEvent!({ type: 'message_start', message: { role: 'toolResult', content: [] } });
+ captured.onEvent!({ type: 'auto_retry_end', success: false,
+ finalError: '400: unsupported parameter: prompt_cache_retention' });
+ let event;
+ do { event = await events.next(); } while (!event.done && event.value.type !== 'error');
+ expect(event.value?.data).toMatchObject({ piCacheCompatibility: { applied: expected,
+ providerId: 'native-a', wireModelId: 'local-model',
+ routeKey: piCacheRouteKey('https://cache.example/v1', 'openai-completions') } });
+ captured.onEvent!({ type: 'agent_end', messages: [] });
+ }
+ } finally { await handle.close(); }
+ });
+
it("routes a persisted BYOM id through its namespaced PI runtime provider", async () => {
const authProviderIds: Array<string | null | undefined> = [];
const deps: AgentDeps = {
diff --cc packages/maker-core/src/agents/pi/cache-recovery-source.ts
index 5ccd427898,0000000000..9d2c39e280
mode 100644,000000..100644
--- a/packages/maker-core/src/agents/pi/cache-recovery-source.ts
+++ b/packages/maker-core/src/agents/pi/cache-recovery-source.ts
@@@ -1,326 -1,0 +1,328 @@@
+/** Runs inside unmodified Pi, after every extension payload hook. */
+export const PI_CACHE_RECOVERY_SOURCE = String.raw`
+function installCindyCacheRecovery(pi) {
+ const supported = (api) => api === 'openai-completions' || api === 'openai-responses';
+ const fields = ['prompt_cache_key', 'prompt_cache_retention'];
++ const nativeRegistry = ctx => typeof ctx?.modelRegistry?.getProvider === 'function'
++ && typeof ctx.modelRegistry.getRegisteredNativeProvider === 'function';
+ const day = 24 * 60 * 60 * 1000;
+ const home = process.env.CINDY_PI_CACHE_COMPAT_FILE;
+ const file = home && home.replace(/^\$HOME(?=\/)/, () => homedir());
+ let turn;
+ let pendingInput;
+ let directInput = false;
- let revision = 0;
- let sequence = 0;
+ let memory = Object.create(null);
+ let routeRules = new Map();
+ function rules() {
+ try {
+ const name = path.join(process.env.PI_CODING_AGENT_DIR || '', 'pi-cache-compatibility.json');
+ if (statSync(name).size > 256 * 1024) return [];
+ const value = JSON.parse(readFileSync(name, 'utf8'));
+ return Array.isArray(value) ? value : [];
+ } catch { return []; }
+ }
+ function readRules() {
+ return new Map(rules().flatMap(rule => {
+ if (!rule || typeof rule !== 'object' || typeof rule.providerId !== 'string'
+ || !/^[a-f0-9]{64}$/.test(rule.routeKey)) return [];
+ return [[JSON.stringify([rule.provider, rule.id, rule.baseUrl, rule.api]),
+ JSON.stringify([rule.providerId, rule.id, rule.routeKey])]];
+ }));
+ }
+ function refreshRules() { routeRules = readRules(); }
+ refreshRules();
+ function route(model) {
+ if (!supported(model?.api)) return;
+ return routeRules.get(JSON.stringify([model.provider, model.id, model.baseUrl, model.api]));
+ }
+ function load() {
+ if (!file) return { routes: Object.create(null) };
+ try {
+ const info = statSync(file);
+ if (info.size > 256 * 1024) return;
+ const value = JSON.parse(readFileSync(file, 'utf8'));
+ if (!value || typeof value !== 'object' || !value.routes || typeof value.routes !== 'object' || Array.isArray(value.routes)) return;
+ // Stable legacy grace, not a fresh lifetime on every app launch.
+ for (const record of Object.values(value.routes)) {
+ if (record && record.supportsLongCacheRetention === false && record.expiresAt === undefined
+ && record.learnedAt === undefined && record.evidence === undefined) {
+ record.learnedAt = info.mtimeMs;
+ record.expiresAt = info.mtimeMs + 7 * day;
+ record.evidence = 'explicit';
+ }
+ }
+ return value;
+ } catch (error) { return error?.code === 'ENOENT' ? { routes: Object.create(null) } : undefined; }
+ }
+ function active(record, now) {
+ return record?.supportsLongCacheRetention === false && Number.isFinite(record.learnedAt) && record.learnedAt <= now
+ && Number.isFinite(record.expiresAt) && record.expiresAt > record.learnedAt
+ && ['explicit', 'opaque'].includes(record.evidence)
+ && record.expiresAt <= record.learnedAt + (record.evidence === 'explicit' ? 7 : 1) * day && record.expiresAt > now;
+ }
+ function remember(key, evidence, startedAt) {
+ const record = { supportsLongCacheRetention: false, evidence, learnedAt: startedAt,
+ expiresAt: startedAt + (evidence === 'explicit' ? 7 : 1) * day };
+ if ((memory[key]?.learnedAt ?? 0) > startedAt) return;
+ memory[key] = record;
+ if (!file) return record;
+ let temporary;
+ try {
+ const current = load();
+ if (!current || (current.routes[key]?.learnedAt ?? 0) > startedAt) return;
+ for (const [otherKey, otherRecord] of Object.entries(current.routes)) {
+ if (Number.isFinite(otherRecord?.expiresAt) && otherRecord.expiresAt <= startedAt) delete current.routes[otherKey];
+ }
+ current.routes[key] = record;
+ const data = JSON.stringify(current);
+ if (Buffer.byteLength(data) > 256 * 1024) return;
+ temporary = file + '.' + randomUUID() + '.tmp';
+ writeFileSync(temporary, data, { mode: 0o600, flag: 'wx' });
+ renameSync(temporary, file);
+ delete memory[key];
+ } catch { /* Storage failure must never undo a successful response. */ }
+ finally { if (temporary) { try { rmSync(temporary, { force: true }); } catch {} } }
+ return record;
+ }
+ function withoutCache(payload) {
+ const next = { ...payload };
+ for (const field of fields) delete next[field];
+ return next;
+ }
+ function fingerprint(payload) {
+ return createHash('sha256').update(JSON.stringify(withoutCache(payload), (_key, value) =>
+ value && typeof value === 'object' && !Array.isArray(value)
+ ? Object.fromEntries(Object.keys(value).sort().map(key => [key, value[key]])) : value)).digest('hex');
+ }
+ function requestIdentity(model, options) {
+ // Request-only digest stays in memory, including any credential/header state.
+ return fingerprint({ api: model?.api, provider: model?.provider, id: model?.id, baseUrl: model?.baseUrl,
+ modelHeaders: model?.headers, headers: options?.headers, apiKey: options?.apiKey });
+ }
+ function rejectedFieldList(text) {
+ let rest = text.trimStart().replace(/^\[\s*/, '');
+ const result = [];
+ while (rest) {
+ const match = /^(?:(['"\x60])([a-z_][\w.-]*)\1|([a-z_][\w.-]*))/i.exec(rest);
+ if (!match) break;
+ const tail = rest.slice(match[0].length);
+ // A comma may introduce prose ("use another option instead"). Bare
+ // continuation fields must end at a list/sentence boundary; quoted fields
+ // are explicit. This parses the list without naming vendors or options.
+ if (result.length && !match[1] && !/^\s*(?:[,,;\].!?]|$)/.test(tail)) break;
+ result.push((match[2] ?? match[3]).replace(/\.+$/, '').toLowerCase());
+ const separator = /^\s*[,,]\s*/.exec(tail);
+ if (!separator) break;
+ rest = tail.slice(separator[0].length);
+ }
+ return result;
+ }
+ function failureKind(message, provider) {
+ const text = message?.errorMessage;
+ if (typeof text !== 'string' || text.length > 64 * 1024 || message.stopReason !== 'error'
+ || message.content?.length || (message.usage?.output ?? 0) > 0) return;
+ const prefix = text.startsWith('400:') ? '400:' : provider + ' API error (400):';
+ if (!text.startsWith(prefix)) return;
+ let body = text.slice(prefix.length).trim();
+ let data;
+ try { data = JSON.parse(body); } catch { /* Opaque 400 bodies remain eligible. */ }
+ for (let depth = 0; data && typeof data === 'object' && depth < 4; depth++) {
+ if (data.param != null && !fields.includes(data.param)) return;
+ if (data.code === 'UNKNOWN_FIELD' && data.data?.field != null) {
+ return fields.includes(data.data.field) ? 'explicit' : undefined;
+ }
+ if (typeof data.message === 'string') body = data.message;
+ // Exclude known unrelated categories; unfamiliar gateway types stay opaque.
+ if ([data.code, data.type].some(value => typeof value === 'string'
+ && /auth|permission|forbidden|quota|billing|rate.?limit|model.?not.?found|context|length|content.?filter|policy|invalid.?(?:value|type)|missing.?required/i.test(value))) return;
+ data = data.error;
+ }
+ if (/api.?key|unauthori[sz]ed|authentication|permission denied|quota|rate.?limit|model.{0,30}(?:not found|does not exist)|context.{0,25}(?:length|limit)|content.?filter|content.?policy|invalid.{0,15}value|鉴权|认证失败|额度|限流|模型不存在|上下文.{0,8}(?:超|长)|内容审核|参数值/i.test(body)) return;
+ // Removing optional cache fields cannot supply a required value or repair
+ // an explicitly invalid type. A generic invalid_parameter code alone does
+ // not establish either condition and must retain its bounded contrast.
+ if (/missing\s+required|(?:field|parameter|property|argument)\s+(?:is\s+)?required|required\s+(?:field|parameter|property|argument).{0,80}missing|invalid\s+type/i.test(body)) return;
+ for (const pattern of [
+ /invalid\s+(?:parameters?|fields?|arguments?|properties|property|options?)(?:\(s\))?[\s:=]*/gi,
+ /(?:parameter|field|argument|property|option)\s*[:=]?\s*['"\x60]?([a-z_][\w.-]*)['"\x60]?\s+(?:is\s+)?invalid/gi,
+ ]) {
+ for (const match of body.matchAll(pattern)) {
+ const invalid = match[1] ? [match[1].toLowerCase()]
+ : rejectedFieldList(body.slice(match.index + match[0].length));
+ if (invalid.some(field => !fields.includes(field))) return;
+ }
+ }
+ // A named rejection is evidence even when a gateway omits structured param.
+ // Parse the field relationship, never a list of vendor or parameter names.
+ let cacheRejected = false;
+ for (const pattern of [
+ /(?:unsupported|unknown|unrecognized|unexpected)\s+(?:parameters?|fields?|arguments?|properties|property|options?)(?:\(s\))?[\s:=]*/gi,
+ /(?:parameter|field|argument|property|option)\s*[:=]?\s*['"\x60]?([a-z_][\w.-]*)['"\x60]?\s+(?:is\s+)?(?:unsupported|unknown|unrecognized|unexpected|not\s+(?:supported|allowed|permitted))/gi,
+ /(?:未知|不支持|不接受|无法识别)(?:的)?(?:请求)?(?:参数|字段|属性|选项)[\s::=]*/gi,
+ /(?:参数|字段|属性|选项)\s*[::=]?\s*['"\x60]?([a-z_][\w.-]*)['"\x60]?\s*(?:不支持|不被支持|未知|无法识别)/gi,
+ ]) {
+ for (const match of body.matchAll(pattern)) {
+ const rejected = match[1] ? [match[1].replace(/\.+$/, '').toLowerCase()]
+ : rejectedFieldList(body.slice(match.index + match[0].length));
+ for (const field of rejected) {
+ if (!fields.includes(field)) return;
+ cacheRejected = true;
+ }
+ }
+ }
+ return cacheRejected || /(?:unsupported|not supported|unknown|未知|不支持).{0,80}prompt_cache_(?:key|retention)|prompt_cache_(?:key|retention).{0,80}(?:unsupported|not supported|unknown|未知|不支持)/i.test(body) ? 'explicit' : 'opaque';
+ }
+ function invalidate() {
- revision++;
++ pendingInput = undefined;
+ if (turn) { turn.safe = false; turn.valid = false; }
+ }
+ pi.on('input', () => {
+ // Queuing/steering invalidates the old trial immediately. It cannot arm a
+ // budget for the new input until Pi actually starts that user message.
+ directInput = false;
+ invalidate();
+ });
+ pi.on('before_agent_start', () => { directInput = true; });
+ pi.on('message_start', (event, ctx) => {
+ if (event.message?.role !== 'user') return;
+ // Pi represents both steer and followUp as user messages. Snapshot now,
+ // but decide ownership only after the supervisor sees the public event.
+ invalidate();
+ const inputRules = readRules();
+ const records = load()?.routes ?? Object.create(null);
+ for (const [key, record] of Object.entries(memory)) {
+ if ((records[key]?.learnedAt ?? 0) < record.learnedAt) records[key] = record;
+ }
+ const startedAt = Date.now();
+ const learned = new Set(Object.keys(records).filter(key => active(records[key], startedAt)));
- // Pi's later message_end hooks may replace this object in place. Match
- // the supervisor's message_start receipt, not a subsequently edited object.
- const key = createHash('sha256').update(JSON.stringify(event.message)).digest('hex');
- pendingInput = { key, ctx, direct: directInput, revision, learned, inputRules };
+ if (directInput) {
+ routeRules = inputRules;
- turn = { sequence: ++sequence, safe: true, valid: true, attempted: false, learned };
++ turn = { safe: true, valid: true, attempted: false, learned };
++ }
++ if (!directInput || !nativeRegistry(ctx)) {
++ // Pi's message_end hooks may replace the message in place. Capture the
++ // receipt now. Modern direct inputs need no query; older host recovery
++ // still needs this exact input's snapshot, never an agent_start guess.
++ const key = createHash('sha256').update(JSON.stringify(event.message)).digest('hex');
++ pendingInput = { key, ctx, direct: directInput, learned, inputRules };
+ }
+ directInput = false;
+ });
+ async function settleInput() {
+ const input = pendingInput;
+ if (!input) return;
+ if (!input.promise) input.promise = (async () => {
+ let followUp = false;
+ let timer;
+ try {
+ // Older RPC ExtensionContext versions do not expose mode.
+ if (input.ctx?.mode && input.ctx.mode !== 'rpc') throw new Error('not RPC');
+ const key = input.key;
+ const raw = await Promise.race([
+ input.ctx.ui.input('cindy:cache-input', JSON.stringify({ key, direct: input.direct })),
+ new Promise(resolve => { timer = setTimeout(() => resolve(undefined), 5000); }),
+ ]);
+ if (typeof raw === 'string') {
+ const receipt = JSON.parse(raw);
+ followUp = receipt?.key === key && receipt.followUp === true;
+ }
+ } catch { /* Unconfirmed queued provenance must not reopen recovery. */ }
+ finally { if (timer) clearTimeout(timer); }
- if (pendingInput !== input || input.revision !== revision || input.ctx?.signal?.aborted) return;
++ if (pendingInput !== input || input.ctx?.signal?.aborted) return;
+ if (!input.direct && followUp) {
+ routeRules = input.inputRules;
- turn = { sequence: ++sequence, safe: true, valid: true, attempted: false, learned: input.learned };
++ turn = { safe: true, valid: true, attempted: false, learned: input.learned };
+ }
+ pendingInput = undefined;
+ })();
+ await input.promise;
+ }
+ for (const name of ['tool_call', 'tool_execution_start', 'model_select', 'session_switch', 'session_shutdown']) {
+ pi.on(name, invalidate);
+ }
+ pi.on('before_provider_request', async (event, ctx) => {
+ // Older registries cannot safely restore an extension provider's ownership.
+ // Keep their existing host recovery usable without registering a provider;
+ // modern registries leave all payload handling to the final stream wrapper.
- if (typeof ctx.modelRegistry?.getProvider === 'function'
- && typeof ctx.modelRegistry?.getRegisteredNativeProvider === 'function') return;
++ if (nativeRegistry(ctx)) return;
+ await settleInput();
+ const key = route(ctx.model);
+ // Use the same input snapshot as the modern wrapper, including tool rounds
+ // across expiry. Before any input exists, preserve native preparation calls.
+ if (!key || !(turn ? turn.learned.has(key) : active(load()?.routes[key], Date.now()))) return;
+ const payload = event.payload;
+ if (payload && typeof payload === 'object' && !Array.isArray(payload)) return withoutCache(payload);
+ });
+ return {
+ refreshRules,
+ accepts: model => Boolean(route(model)),
+ wrap(model, options, invoke, provider = model.provider) {
+ return (async function* () {
+ await settleInput();
+ const key = route(model);
+ if (!key || !turn) { yield* invoke(options); return; }
+ const owner = turn;
+ const startedAt = Date.now();
+ const learned = owner.learned.has(key);
+ let originalFingerprint;
+ let originalIdentity;
+ let hasCache = false;
+ let skipped = false;
+ const sameInput = () => turn === owner && owner.valid && owner.safe && !options?.signal?.aborted;
+ const requestOptions = (trial) => ({ ...options,
+ onPayload: async (payload, requestModel) => {
+ const replacement = await options?.onPayload?.(payload, requestModel);
+ const finalPayload = replacement === undefined ? payload : replacement;
+ if (!finalPayload || typeof finalPayload !== 'object' || Array.isArray(finalPayload)) return finalPayload;
+ if (trial) {
+ if (!sameInput() || fingerprint(finalPayload) !== originalFingerprint
+ || requestIdentity(requestModel, options) !== originalIdentity) {
+ skipped = true;
+ throw new Error('CINDY_CACHE_RETRY_CONTEXT_CHANGED');
+ }
+ return withoutCache(finalPayload);
+ }
+ hasCache = fields.some(field => Object.hasOwn(finalPayload, field));
+ if (hasCache && sameInput() && !owner.attempted && requestModel?.id === model.id
+ && requestModel?.api === model.api && requestModel?.baseUrl === model.baseUrl) {
+ originalFingerprint = fingerprint(finalPayload);
+ originalIdentity = requestIdentity(requestModel, options);
+ }
+ return learned || owner.trialKey === key ? withoutCache(finalPayload) : finalPayload;
+ },
+ });
+ for await (const event of invoke(requestOptions(false))) {
+ if (event.type !== 'error') {
+ // A successful response start rules out retrying an in-stream failure.
+ owner.safe = false;
+ yield event;
+ continue;
+ }
+ const evidence = failureKind(event.error, provider);
+ if (!evidence || learned || !hasCache || !originalFingerprint || !sameInput() || owner.attempted) {
+ yield { ...event, error: { ...event.error, cindyCacheRecoveryHandled: true } };
+ continue;
+ }
+ owner.attempted = true;
+ owner.trialKey = key;
+ for await (const retried of invoke(requestOptions(true))) {
+ if (skipped) {
+ yield options?.signal?.aborted ? retried : { ...event, error: { ...event.error, cindyCacheRecoveryHandled: true } };
+ return;
+ }
+ if (retried.type === 'done' && turn === owner && owner.valid && !options?.signal?.aborted
+ && ['stop', 'toolUse'].includes(retried.message?.stopReason)) {
+ const record = remember(key, evidence, startedAt);
+ if (record) owner.learned.add(key);
+ }
+ if (retried.type !== 'error') owner.safe = false;
+ yield retried.type === 'error'
+ ? { ...retried, error: { ...retried.error, cindyCacheRecoveryHandled: true } } : retried;
+ }
+ return;
+ }
+ })();
+ },
+ };
+}
+`;
diff --cc packages/maker-core/src/agents/pi/cindy-bridge-source.ts
index b505534a0b,00f17acd00..14d3bd532e
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@@ -4317,9 -4350,38 +4355,25 @@@ export default async function cindyBrid
},
});
if (!currentPermissionState().reviewOnly) registerCindyQuestionTool(pi);
- function learnedCachePayload(payload, model) {
- if (model?.api !== 'openai-completions' || !payload || typeof payload !== 'object' || Array.isArray(payload)) return payload;
- try {
- const file = path.join(process.env.PI_CODING_AGENT_DIR || '', 'pi-cache-compatibility.json');
- if (!process.env.PI_CODING_AGENT_DIR || statSync(file).size > 256 * 1024) return payload;
- const rules = JSON.parse(readFileSync(file, 'utf8'));
- if (!Array.isArray(rules) || !rules.some((rule) => rule.provider === model.provider && rule.id === model.id && rule.baseUrl === model.baseUrl)) return payload;
- const next = { ...payload };
- delete next.prompt_cache_key;
- delete next.prompt_cache_retention;
- return next;
- } catch { return payload; }
- }
pi.on('before_provider_request', async (event, ctx) => {
const payload = astraResponsesPayload(event.payload, ctx.model) ?? event.payload;
- return (await nativeFastPayload(payload, ctx.model, ctx)) ?? payload;
- const next = learnedCachePayload((await nativeFastPayload(payload, ctx.model, ctx)) ?? payload, ctx.model);
++ const next = (await nativeFastPayload(payload, ctx.model, ctx)) ?? payload;
+ // A one-way native notification adds no host round trip. This is the payload
+ // preparation hook, not proof that HTTP was sent or accepted upstream.
+ const token = process.env.CINDY_PI_TURN_TOOL_POLICY;
+ if (token) {
+ try {
+ const messages = next?.messages ?? next?.input ?? next?.contents;
+ ctx.ui.notify(${JSON.stringify(CINDY_PI_REQUEST_DIAGNOSTICS_PREFIX)} + token + ':' + JSON.stringify(
+ projectPiRequestDiagnostics({
+ toolCount: Array.isArray(next?.tools) ? next.tools.length : undefined,
+ messageCount: Array.isArray(messages) ? messages.length : undefined,
+ wireEffort: next?.reasoning_effort ?? next?.reasoning?.effort ?? next?.output_config?.effort,
+ }),
+ ), 'info');
+ } catch { /* Diagnostics must never block a model request. */ }
+ }
+ return next;
});
const mcpGateway = new CindyMcpGateway();
// bash 隔离 home 经 resolveBashPackageHome 解析(首次加载读删 + 防篡改 stash,
diff --cc packages/maker-core/src/agents/pi/index.ts
index 8546df04b7,96e7e44577..1420d01c21
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@@ -1,6 -1,4 +1,5 @@@
import { getPiExtensionUiCapability } from './extension-ui-capabilities.js';
+import { createPiCacheInputTracker } from './cache-input.js';
- import { createHarnessHookBridge } from '../shared/harness-hooks.js';
import { parsePiManagementArgs, parsePiManagementText } from './managed-command.js';
import { canonicalSkillPath, snapshotDisabledSkillLaunch, currentDisabledSkillLaunchPaths, extendDisabledSkillLaunchPaths, type DisabledSkillLaunchSnapshot } from '../shared/skill-activation.js';
/**
@@@ -2327,24 -2327,22 +2328,19 @@@ export class PiAgent extends BaseAgent
const gatewayImageInputByModel = new Map<string, boolean>();
const gatewayApiByModel = new Map<string, PiGatewayApi>();
const cacheRoutes = new Map<string, PiCacheCompatibility>();
- const cacheRequestRules: Array<{ provider: string; id: string; baseUrl: string }> = [];
- const learnedCompat = (providerId: string, modelId: string, wireModelId: string,
+ const cacheRequestRules: Array<{ provider: string; id: string; baseUrl: string; api: string; providerId: string; routeKey: string }> = [];
- const learnedCompat = (providerId: string, modelId: string, wireModelId: string,
++ const registerCacheRoute = (providerId: string, modelId: string, wireModelId: string,
runtimeProvider: string, baseUrl: string, api: string, runtimeBaseUrl = baseUrl) => {
-- // Unknown identities and SSH must not consume local learning records.
- if (api !== 'openai-completions' && api !== 'openai-responses') return undefined;
- if (opts.remote || api !== 'openai-completions') return undefined;
++ if (api !== 'openai-completions' && api !== 'openai-responses') return;
const routeKey = piCacheRouteKey(baseUrl, api);
-- if (!routeKey) return undefined;
- const compat = opts.remote ? undefined : this.deps.resolvePiNativeCompatOverride?.(providerId, wireModelId, routeKey);
- const compat = this.deps.resolvePiNativeCompatOverride?.(providerId, wireModelId, routeKey);
++ if (!routeKey) return;
cacheRoutes.set(JSON.stringify([runtimeProvider, wireModelId]), {
providerId, modelId, wireModelId, routeKey,
-- applied: compat?.supportsLongCacheRetention === false,
++ applied: false, // Sample the selected route's learning only when its input starts.
});
- if (compat?.supportsLongCacheRetention === false && !cacheRequestRules.some((rule) => rule.provider === runtimeProvider && rule.id === wireModelId)) {
- cacheRequestRules.push({ provider: runtimeProvider, id: wireModelId, baseUrl: runtimeBaseUrl });
+ if (!cacheRequestRules.some((rule) => rule.provider === runtimeProvider && rule.id === wireModelId && rule.api === api)) {
+ cacheRequestRules.push({ provider: runtimeProvider, id: wireModelId, baseUrl: runtimeBaseUrl, api, providerId, routeKey });
}
- // Keep native serialization enabled; the final payload wrapper applies
- // learning dynamically, so expiry needs no model registry rebuild.
- return undefined;
- return compat;
};
const models = runtimeModels.flatMap((publicModel: ModelDescriptor) => {
// availableModels 为跨 provider 拍平的公开能力;BYOM 同 id 冲突时 effort
@@@ -2388,12 -2386,12 +2384,11 @@@
return [];
}
const modelBaseUrl = endpoint ? piGatewayModelBaseUrl(endpoint, api) : undefined;
-- const gatewayCompat = {
-- ...(resolvedSpec?.compat ?? {}),
- ...((api === 'openai-completions' || api === 'openai-responses') ?
- ...(!opts.remote && api === 'openai-completions' ?
-- learnedCompat(gatewayProviderId ?? 'xd', m.id, m.id, PI_PROVIDER_ID,
- this.deps.runtimeConfig.remoteEndpoint ?? modelBaseUrl ?? '', api, modelBaseUrl ?? '') ?? {} : {}),
- this.deps.runtimeConfig.remoteEndpoint ?? '', api, modelBaseUrl ?? '') ?? {} : {}),
-- };
++ if (api === 'openai-completions' || api === 'openai-responses') {
++ registerCacheRoute(gatewayProviderId ?? 'xd', m.id, m.id, PI_PROVIDER_ID,
++ this.deps.runtimeConfig.remoteEndpoint ?? modelBaseUrl ?? '', api, modelBaseUrl ?? '');
++ }
++ const gatewayCompat = resolvedSpec?.compat ?? {};
gatewayApiByModel.set(m.id, api);
// Missing capability metadata must not disable newly discovered vision models.
// Explicit text-only declarations still take precedence over this default.
@@@ -2461,8 -2459,8 +2456,8 @@@
this.deps.logger.warn('pi: native provider id collides with gateway provider "cindy" — skipped', { id: np.id });
continue;
}
-- // Inherited bundled models need the same learning in modelOverrides.
-- for (const model of np.models) learnedCompat(np.sourceProviderId ?? np.id, model.id,
++ // Include inherited models; dynamic learning stays out of models.json.
++ for (const model of np.models) registerCacheRoute(np.sourceProviderId ?? np.id, model.id,
model.wireId ?? model.id, np.id, model.cacheIdentityBaseUrl ?? np.cacheIdentityBaseUrl ?? model.baseUrl ?? np.baseUrl,
model.api ?? np.api ?? '', model.baseUrl ?? np.baseUrl);
const nativeModels = (
@@@ -2472,14 -2470,16 +2467,9 @@@
m.contextWindow && m.contextWindow > 0 ? m.contextWindow : 128_000,
this.deps.resolveModelContextLimit?.(np.sourceProviderId ?? np.id, m.id) ?? 0,
);
- // 自愈写回的 per-model compat 覆盖(如上游拒收长缓存提示后关掉
- // supportsLongCacheRetention)与目录/bundled compat 合并;覆盖字段最少,
- // 不改路由与凭证。
+ // Explicit catalog compat stays native; learned cache hints expire in the payload wrapper.
const wireModelId = m.wireId ?? m.id;
-- const compat = {
-- ...(m.compat ?? {}),
-- ...(learnedCompat(np.sourceProviderId ?? np.id, m.id, wireModelId, np.id,
-- m.cacheIdentityBaseUrl ?? np.cacheIdentityBaseUrl ?? m.baseUrl ?? np.baseUrl,
-- m.api ?? np.api ?? '', m.baseUrl ?? np.baseUrl) ?? {}),
-- };
++ const compat = m.compat ?? {};
return {
id: wireModelId,
name: m.name ?? m.id,
@@@ -5752,19 -5759,52 +5767,65 @@@
transport,
logger: this.deps.logger,
onEvent: (event: PiRpcEvent) => {
+ const cacheInput = cacheInputTracker.observe(event);
+ if (cacheInput) pendingCacheCompatibility = { key: cacheInput.key, value: readCacheCompatibility() };
+ if (event.type === 'extension_ui_request' && event.method === 'input' && event.title === 'cindy:cache-input') {
+ let request: { key?: unknown; direct?: unknown } = {};
+ try { request = JSON.parse(typeof event.placeholder === 'string' ? event.placeholder : '{}'); } catch {}
+ const receipt = !closed && !proc.isClosed ? cacheInputTracker.read(request?.key) : undefined;
+ if (receipt && pendingCacheCompatibility?.key === receipt.key && (request.direct === true || receipt.followUp)) {
+ inputCacheCompatibility = pendingCacheCompatibility.value;
+ pendingCacheCompatibility = undefined;
+ }
+ proc.send({ type: 'extension_ui_response', id: event.id, value: JSON.stringify(receipt ?? {}) });
+ return;
+ }
+ // 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}:`;
+ if (event.message.startsWith(prefix) && event.message.length <= prefix.length + 1024) {
+ try {
+ const metadata = projectPiRequestDiagnostics(JSON.parse(event.message.slice(prefix.length)));
+ providerRequestObservedAt = performance.now();
+ providerTurnObservedAt ??= providerRequestObservedAt;
+ providerFirstOutputLogged = false;
+ providerRequestSequence++;
+ this.deps.logger.debug('pi request stage', {
+ startupTraceId, requestSequence: providerRequestSequence,
+ stage: 'provider-request-prepared', ...metadata,
+ });
+ } catch { /* Invalid diagnostic frames never enter logs or user UI. */ }
+ }
+ return;
+ }
+ if (providerRequestObservedAt !== undefined) {
+ const update = event.assistantMessageEvent as { type?: unknown } | undefined;
+ if (!providerFirstOutputLogged && event.type === 'message_update' &&
+ ['text_delta', 'thinking_delta', 'toolcall_start', 'toolcall_delta'].includes(String(update?.type))) {
+ providerFirstOutputLogged = true;
+ this.deps.logger.debug('pi request stage', {
+ startupTraceId, requestSequence: providerRequestSequence, stage: 'first-output',
+ elapsedMs: Math.max(0, performance.now() - providerRequestObservedAt),
+ });
+ }
+ if (event.type === 'agent_settled') {
+ this.deps.logger.debug('pi request stage', {
+ startupTraceId, requestSequence: providerRequestSequence, stage: 'agent-settled',
+ elapsedMs: Math.max(0, performance.now() - providerRequestObservedAt),
+ turnElapsedMs: Math.max(0, performance.now() - providerTurnObservedAt!),
+ });
+ providerRequestObservedAt = undefined;
+ providerTurnObservedAt = undefined;
+ }
+ }
if (event.type === 'agent_start' && !ctx.pendingHostTurnStartToken) {
syncNativePlanMode?.();
}