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): bound cache parameter recovery and remember successful routes

xuzini-xznd5e094d6d0a8
diff --git a/apps/desktop/src/main/maker-host/__tests__/pi-native-compat-overrides-store.test.ts b/apps/desktop/src/main/maker-host/__tests__/pi-native-compat-overrides-store.test.ts
index 23e16bf386..d389d2560a 100644
--- a/apps/desktop/src/main/maker-host/__tests__/pi-native-compat-overrides-store.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/pi-native-compat-overrides-store.test.ts
@@ -22,11 +22,28 @@ describe('learned cache compatibility', () => {
expect(read('a', 'model')).toBeUndefined();
expect(fs.readFileSync(file, 'utf8')).not.toContain('supportsStore');
});
+ it('preserves native trial expiry metadata when another route is recorded', async () => {
+ const learnedAt = Date.now();
+ const timed = { ...fix, learnedAt, expiresAt: learnedAt + 86400000, evidence: 'opaque' };
+ const key = JSON.stringify(['a', 'model', a]);
+ fs.writeFileSync(file, JSON.stringify({ routes: { [key]: timed } }));
+ await record('b', 'other', fix, b);
+ expect(JSON.parse(fs.readFileSync(file, 'utf8')).routes[key]).toEqual(timed);
+ fs.writeFileSync(file, JSON.stringify({ routes: { [key]: { ...timed, expiresAt: learnedAt - 1, learnedAt: learnedAt - 86400000 } } }));
+ expect(read('a', 'model', a)).toBeUndefined();
+ });
it('merges concurrent updates without losing another model', async () => {
expect(await Promise.all([record('a', 'one', fix, a), record('a', 'two', fix, a)])).toEqual([true, true]);
expect(read('a', 'one', a)).toEqual(fix);
expect(read('a', 'two', a)).toEqual(fix);
});
+ it('does not turn invalid timed metadata into a permanent legacy override', async () => {
+ const key = JSON.stringify(['a', 'model', a]);
+ fs.writeFileSync(file, JSON.stringify({ routes: { [key]: { ...fix, learnedAt: 1, evidence: 'opaque' } } }));
+ expect(read('a', 'model', a)).toBeUndefined();
+ await record('b', 'other', fix, b);
+ expect(JSON.parse(fs.readFileSync(file, 'utf8')).routes[key]).toBeUndefined();
+ });
it('does not overwrite malformed files or report an unpersisted success', async () => {
fs.writeFileSync(file, '{bad-json');
expect(await record('a', 'model', fix, a)).toBe(false);
diff --git a/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts b/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
index b020a0d990..53bd9d2ed9 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
@@ -34,6 +34,8 @@ vi.mock('../../agent-binaries/index.js', () => ({
vi.mock('../runtime-configs.js', () => ({
getRipgrepBinaryPath: () => state.ripgrepPath,
+ // Cache route identities now resolve the real upstream even for local Pi.
+ claudeUpstreamEndpoint: () => 'https://upstream.test/v1',
}));
vi.mock('../../mcp-integrations/piEnvironment.js', () => ({
diff --git a/apps/desktop/src/main/maker-host/pi-host.ts b/apps/desktop/src/main/maker-host/pi-host.ts
index 2dbc006a4e..847286f746 100644
--- a/apps/desktop/src/main/maker-host/pi-host.ts
+++ b/apps/desktop/src/main/maker-host/pi-host.ts
@@ -1956,8 +1956,10 @@ export function buildPiAgent(opts: BuildPiAgentOpts): PiAgent | null {
return new PiAgent({
resolveSessionEnvironment: resolveCompanionRuntimeEnvironment,
getDisabledSkillPaths: readDisabledSkillPaths,
- // 上游拒收可选请求字段(如 prompt_cache_retention / prompt_cache_key)后学到的 per-model
- // compat 修正,写 models.json 时合并;只改 compat,不动路由与凭证。
+ // 执行端动态学习;SSH 独立存储,避免把本机线路结论带到远端。
+ resolvePiNativeCompatStorePath: remoteHostId => remoteHostId
+ ? '$HOME/.xdt-server/v1/pi-agent-home/pi-native-compat-overrides.json'
+ : path.join(app.getPath('userData'), 'pi-native-compat-overrides.json'),
resolvePiNativeCompatOverride: (providerId, modelId, routeKey) =>
readPiNativeCompatOverride(providerId, modelId, routeKey),
getManagedSkills: listCindyManagedSkills,
diff --git a/apps/desktop/src/main/maker-host/pi-native-compat-overrides-store.ts b/apps/desktop/src/main/maker-host/pi-native-compat-overrides-store.ts
index fa8efb9d39..648047b305 100644
--- a/apps/desktop/src/main/maker-host/pi-native-compat-overrides-store.ts
+++ b/apps/desktop/src/main/maker-host/pi-native-compat-overrides-store.ts
@@ -7,7 +7,7 @@ import path from 'node:path';
import { desktopMakerLogger } from './logger-adapter.js';
import { createOverrideSettingsFile } from './override-settings-file.js';
-type CacheOverride = { supportsLongCacheRetention: false };
+type CacheOverride = { supportsLongCacheRetention: false; learnedAt?: number; expiresAt?: number; evidence?: 'explicit' | 'opaque' };
interface PiNativeCompatOverrides { routes: Record<string, CacheOverride> }
const log = desktopMakerLogger.child('pi-native-compat-overrides-store');
const DEFAULTS: PiNativeCompatOverrides = { routes: {} };
@@ -27,7 +27,14 @@ function normalize(raw: unknown): PiNativeCompatOverrides {
if (!Array.isArray(parts) || parts.length !== 3 || !parts.every((v) => typeof v === 'string')
|| identity(parts[0], parts[1], parts[2]) !== key) continue;
if (value && typeof value === 'object' && (value as CacheOverride).supportsLongCacheRetention === false) {
- routes[key] = { supportsLongCacheRetention: false };
+ const record = value as CacheOverride;
+ const timed = Number.isFinite(record.learnedAt) && Number.isFinite(record.expiresAt)
+ && record.expiresAt! > record.learnedAt!
+ && record.expiresAt! <= record.learnedAt! + (record.evidence === 'opaque' ? 1 : 7) * 86400000
+ && (record.evidence === 'explicit' || record.evidence === 'opaque');
+ if (!timed && (record.learnedAt !== undefined || record.expiresAt !== undefined || record.evidence !== undefined)) continue;
+ routes[key] = { supportsLongCacheRetention: false,
+ ...(timed ? { learnedAt: record.learnedAt, expiresAt: record.expiresAt, evidence: record.evidence } : {}) };
}
} catch { /* Invalid identities are not executable configuration. */ }
}
@@ -43,7 +50,9 @@ export function readPiNativeCompatOverride(providerId: string, modelId: string,
const key = identity(providerId, modelId, routeKey);
if (!key) return undefined;
store.invalidateIfChanged();
- return store.read().routes[key] ? { supportsLongCacheRetention: false } : undefined;
+ const record = store.read().routes[key];
+ return record && (record.expiresAt === undefined || (record.expiresAt > Date.now() && record.learnedAt! <= Date.now()))
+ ? { supportsLongCacheRetention: false } : undefined;
}
export async function recordPiNativeCompatOverride(providerId: string, modelId: string,
compat: Record<string, unknown>, routeKey?: string): Promise<boolean> {
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
index cc486d992c..2cf1800bdf 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
@@ -1180,6 +1180,17 @@ describe('createContextOverflowRollover', () => {
return deps;
}
+ it('does not stack legacy rebuild or learning on a native cache recovery decision', async () => {
+ const deps = makePiDeps([msg('user', 'Reply OK', 'u1', 1)]);
+ const rollover = createContextOverflowRollover(deps);
+ rollover.claim('s1');
+ expect(await rollover.tryRecover('s1', { ...receipt, reason: 'unsupported-request-option', message: rejection,
+ piCacheRecoveryHandled: true })).toBe(false);
+ expect(deps.recordPiNativeCompatOverride).not.toHaveBeenCalled();
+ expect(deps.closeSession).not.toHaveBeenCalled();
+ expect(deps.replayUserMessage).not.toHaveBeenCalled();
+ });
+
it.each([
rejection,
...['prompt_cache_key', 'prompt_cache_retention'].map(field => '400: ' + JSON.stringify({
diff --git a/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts b/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
index 7a1f8b40c6..fc66b44b57 100644
--- a/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
+++ b/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
@@ -669,6 +669,9 @@ export function createContextOverflowRollover(deps: ContextOverflowRolloverDeps)
// 不再带该字段),并重放同一轮用户消息。只做一次:已学过的 (provider, model)
// 再失败就交给常规错误面,绝不循环重试。
if (unsupportedOption) {
+ // Native provider recovery owns this failure, including the decision
+ // not to retry. Never stack a legacy process rebuild on top of it.
+ if ((errorData as { piCacheRecoveryHandled?: boolean }).piCacheRecoveryHandled) return false;
if (!live && turnIdentity?.instanceId) return false;
const compat = unsupportedRequestOptionCompatOverride(
unsupportedRequestOptionErrorText(errorData),
diff --git a/packages/maker-core/src/agents/base-agent.ts b/packages/maker-core/src/agents/base-agent.ts
index d3fc5b0e65..ba5ed41633 100644
--- a/packages/maker-core/src/agents/base-agent.ts
+++ b/packages/maker-core/src/agents/base-agent.ts
@@ -919,11 +919,10 @@ export interface AgentDeps {
context?: { remote: boolean; runtimeBinaryPath?: string },
) => PiGatewayModelSpec | null | undefined;
- /**
- * Pi-only:host 已学到(自愈写入)的 per-model compat 覆盖。写 models.json 时
- * 合并进该原生 provider 模型的 compat;undefined = 无覆盖。不改变路由与凭证,
- * 只关掉上游已明确拒收的可选字段(如 supportsLongCacheRetention: false)。
- */
+ /** Execution-local cache compatibility store; never copy local records to SSH. */
+ resolvePiNativeCompatStorePath?: (remoteHostId?: string | null) => string;
+
+ /** Legacy host recovery snapshot; native Pi applies learning at final payload time. */
resolvePiNativeCompatOverride?: (
providerId: string,
modelId: string,
diff --git a/packages/maker-core/src/agents/pi/__tests__/cache-compatibility.integration.test.ts b/packages/maker-core/src/agents/pi/__tests__/cache-compatibility.integration.test.ts
index 7135e269b8..ab33519d53 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cache-compatibility.integration.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cache-compatibility.integration.test.ts
@@ -1,127 +1,120 @@
import { createServer } from 'node:http';
-import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { PiAgent } from '../index.js';
-import { PI_CACHE_RECOVERY_EXPECTATION, type AgentDeps, type AgentSessionHandle } from '../../base-agent.js';
+import type { AgentDeps, AgentSessionHandle } from '../../base-agent.js';
import type { AgentEvent } from '../../../types/events.js';
import type { Logger } from '../../../interfaces/logger.js';
-import type { PiCacheCompatibility } from '../cache-compatibility.js';
const binary = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../../../apps/pi-bin', process.platform + '-' + process.arch, process.platform === 'win32' ? 'pi.exe' : 'pi');
const logger: Logger = { trace() {}, debug() {}, info() {}, warn() {}, error() {}, fatal() {}, child: () => logger };
+function responses() {
+ const item = { id: 'msg_fixture', type: 'message', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text: 'OK', annotations: [], logprobs: [] }] };
+ const response = { id: 'resp_fixture', object: 'response', status: 'completed', model: 'fixture', output: [item], usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 } };
+ return [
+ { type: 'response.created', response: { ...response, status: 'in_progress', output: [] } },
+ { type: 'response.output_item.added', output_index: 0, item: { ...item, status: 'in_progress', content: [] } },
+ { type: 'response.content_part.added', item_id: item.id, output_index: 0, content_index: 0, part: { type: 'output_text', text: '', annotations: [], logprobs: [] } },
+ { type: 'response.output_text.delta', item_id: item.id, output_index: 0, content_index: 0, delta: 'OK' },
+ { type: 'response.output_item.done', output_index: 0, item },
+ { type: 'response.completed', response },
+ ].map((event, sequence_number) => 'event: ' + event.type + '\ndata: ' + JSON.stringify({ ...event, sequence_number }) + '\n\n').join('');
+}
-describe.skipIf(!existsSync(binary))('Pi cache learning over real HTTP and native RPC', () => {
- it.each(['normal', 'api.openai.com', 'retry-failure'].flatMap(suffix => [
- { suffix, errorFormat: 'omitted param', rejection: { message: 'Validation: Unsupported parameter(s): prompt_cache_key', type: 'invalid_request_error' } },
- ...['prompt_cache_key', 'prompt_cache_retention'].map(field => ({
- suffix, errorFormat: `null param (${field})`,
- rejection: { error: { code: null, message: `Validation: Unsupported parameter(s): \`${field}\``, param: null, type: 'invalid_request_error' } },
- })),
- ]))('rebuilds with a learned fix and preserves one active input: $suffix / $errorFormat', async ({ suffix, rejection }) => {
+// Integration tier: public PiAgent -> generated bridge -> official Pi -> loopback
+// HTTP, covering #5878/#6570/#6649, both adapters, one transcript input and TTL.
+describe.skipIf(!existsSync(binary))('Pi cache recovery over real native RPC/HTTP', () => {
+ it.each(['openai-completions', 'openai-responses'].flatMap(api => [
+ { api, mode: 'opaque', rejection: { error: { message: 'opaque refusal', type: 'invalid_request_error', param: null } } },
+ { api, mode: 'explicit', rejection: { message: '未知请求字段:prompt_cache_retention', type: 'invalid_request_error' } },
+ { api, mode: 'always-fails', rejection: { message: 'Validation: Unsupported parameter(s): prompt_cache_key', type: 'invalid_request_error' } },
+ { api, mode: 'supported', rejection: {} },
+ ]))('preserves bounded recovery and native caching: $api / $mode', async ({ api, mode, rejection }) => {
const root = mkdtempSync(path.join(os.tmpdir(), 'cindy-cache-integration-'));
const received: Array<Record<string, unknown>> = [];
- let rejectCacheFields = true;
+ let rejectCache = mode !== 'supported';
+ let persistentFailure = mode === 'always-fails';
const gateway = createServer(async (req, res) => {
- let body = '';
- for await (const chunk of req) body += chunk;
- const data = JSON.parse(body);
- received.push(data);
- if (rejectCacheFields && (data.prompt_cache_key !== undefined || data.prompt_cache_retention !== undefined)) {
- res.writeHead(400, { 'content-type': 'application/json' });
- res.end(JSON.stringify(rejection));
- return;
- }
- if (suffix === 'retry-failure' && received.length === 2) {
- res.writeHead(400, { 'content-type': 'application/json' });
- res.end(JSON.stringify({ message: 'Non-cache provider validation failure', type: 'invalid_request_error' }));
- return;
+ expect(req.headers.authorization).toBe('Bearer fixture-key');
+ let body = ''; for await (const chunk of req) body += chunk;
+ const data = JSON.parse(body); received.push(data);
+ if (persistentFailure || rejectCache && ('prompt_cache_key' in data || 'prompt_cache_retention' in data)) {
+ res.writeHead(400, { 'content-type': 'application/json' }); res.end(JSON.stringify(rejection)); return;
}
res.writeHead(200, { 'content-type': 'text/event-stream' });
- res.end('data: ' + JSON.stringify({ id: 'one', choices: [{ index: 0, delta: { role: 'assistant', content: 'OK' }, finish_reason: null }] }) + '\n\n'
- + 'data: ' + JSON.stringify({ id: 'one', choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 } }) + '\n\n'
- + 'data: [DONE]\n\n');
+ res.end(api === 'openai-responses' ? responses() : 'data: ' + JSON.stringify({ id: 'one', choices: [{ index: 0, delta: { role: 'assistant', content: 'OK' }, finish_reason: null }] }) + '\n\n'
+ + 'data: ' + JSON.stringify({ id: 'one', choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 } }) + '\n\n' + 'data: [DONE]\n\n');
});
let handle: AgentSessionHandle | undefined;
try {
- await new Promise<void>((resolve) => gateway.listen(0, '127.0.0.1', resolve));
- const address = gateway.address();
- if (!address || typeof address === 'string') throw new Error('missing loopback listener');
- let endpoint = 'http://127.0.0.1:' + address.port + '/' + suffix + '/v1';
- let learned: PiCacheCompatibility | undefined;
+ await new Promise<void>(resolve => gateway.listen(0, '127.0.0.1', resolve));
+ const address = gateway.address(); if (!address || typeof address === 'string') throw new Error('missing loopback listener');
+ let endpoint = 'http://127.0.0.1:' + address.port + '/api.openai.com/v1';
+ const storeFile = path.join(root, 'compat.json');
const deps: AgentDeps = {
binaryPath: binary, logger,
auth: { getState: async () => ({ authenticated: true, identity: 'fixture', authSource: 'api-key' }), triggerLogin: async () => ({ authenticated: true }), logout: async () => {}, getAuthEnv: async () => ({}) },
runtimeConfig: { endpoint: 'http://127.0.0.1:0' },
resolvePiAgentHome: () => path.join(root, 'agent'),
- resolvePiNativeProviders: async () => ({ providers: [{ id: 'strict', sourceProviderId: 'strict', name: 'Strict fixture', baseUrl: endpoint, api: 'openai-completions', modelIdAliases: { 'fixture-alias': 'fixture-public' }, models: [{ id: 'fixture-public', wireId: 'fixture', api: 'openai-completions', contextWindow: 32768, maxTokens: 100 }] }], env: {} }),
- resolvePiNativeCompatOverride: (provider, model, route) => learned && provider === learned.providerId && model === learned.wireModelId && route === learned.routeKey ? { supportsLongCacheRetention: false } : undefined,
+ resolvePiNativeCompatStorePath: () => storeFile,
+ resolvePiNativeProviders: async () => ({ providers: [{ id: 'strict', sourceProviderId: 'strict', name: 'Strict fixture', apiKeyEnvVar: 'CINDY_PI_KEY_FIXTURE', baseUrl: endpoint, api: api as 'openai-completions' | 'openai-responses', modelIdAliases: { 'fixture-alias': 'fixture-public' }, models: [{ id: 'fixture-public', wireId: 'fixture', api: api as 'openai-completions' | 'openai-responses', contextWindow: 32768, maxTokens: 100 }] }], env: { CINDY_PI_KEY_FIXTURE: 'fixture-key' } }),
preparePiExtraSpawnConfig: async () => ({ mcpEnv: { HOME: root, USERPROFILE: root, XDG_CONFIG_HOME: path.join(root, 'xdg') } }),
};
const workingDir = path.join(root, 'work'); mkdirSync(workingDir);
const opts = { workingDir, model: 'fixture-alias', providerId: 'strict', permissionMode: 'bypassPermissions' as const };
- const run = async (options: Parameters<AgentSessionHandle['send']>[1]) => {
+ const run = async () => {
const events: AgentEvent[] = [];
const finish = (async () => { for await (const event of handle!.events()) { events.push(event); if (event.type === 'done') break; } })();
- await handle!.send({ type: 'user', content: 'Reply OK' }, options);
- await finish;
- return events;
+ await handle!.send({ type: 'user', content: 'Reply OK' }); await finish; return events;
};
const agent = new PiAgent(deps);
handle = await agent.startSession({ ...opts, sessionId: 'first' });
- let userEntry = '';
- const failed = await run({ onTranscriptUserEntry: async (id) => { userEntry = id; } });
- const error = failed.find((event) => event.type === 'error')?.data as { reason: string; piCacheCompatibility: PiCacheCompatibility; piCacheRecoverySafe: boolean };
- expect(error).toMatchObject({ reason: 'unsupported-request-option', piCacheRecoverySafe: true, piCacheCompatibility: { applied: false, providerId: 'strict', modelId: 'fixture-alias', wireModelId: 'fixture' } });
- expect(userEntry).not.toBe('');
- expect(received).toHaveLength(1);
- learned = error.piCacheCompatibility;
- const resumeSessionId = handle.id;
- await handle.close();
- handle = await agent.startSession({ ...opts, sessionId: 'rebuilt', resumeSessionId });
- await expect(handle.send({ type: 'user', content: 'Reply OK' }, { [PI_CACHE_RECOVERY_EXPECTATION]: { ...learned, routeKey: '0'.repeat(64) }, retryTranscriptUserEntryId: userEntry })).rejects.toThrow(/not applied to the expected route/);
- expect(received).toHaveLength(1);
- const originalUserEntry = userEntry;
- const succeeded = await run({
- [PI_CACHE_RECOVERY_EXPECTATION]: learned, retryTranscriptUserEntryId: userEntry,
- // Desktop relinks the existing user row to this replacement entry.
- onTranscriptUserEntry: async id => { userEntry = id; },
- });
- expect(userEntry).not.toBe(originalUserEntry);
- expect(received).toHaveLength(2);
- expect(received[1]).not.toHaveProperty('prompt_cache_key');
- expect(received[1]).not.toHaveProperty('prompt_cache_retention');
- expect((received[1].messages as Array<{ role: string }>).filter((m) => m.role === 'user')).toHaveLength(1);
- if (suffix === 'retry-failure') {
- expect(succeeded.some(event => event.type === 'error')).toBe(true);
- const manual = await run({ retryTranscriptUserEntryId: userEntry });
- expect(manual.some(event => event.type === 'error')).toBe(false);
- expect(received).toHaveLength(3);
- expect((received[2].messages as Array<{ role: string }>).filter(m => m.role === 'user')).toHaveLength(1);
+ const first = await run();
+ expect(received).toHaveLength(mode === 'supported' ? 1 : 2);
+ expect(received[0]).toHaveProperty('prompt_cache_key');
+ expect(received[0]).toHaveProperty('prompt_cache_retention', '24h');
+ if (mode !== 'supported') {
+ const { prompt_cache_key: _key, prompt_cache_retention: _retention, ...without } = received[0];
+ expect(received[1]).toEqual(without);
+ }
+ expect(first.filter(e => e.type === 'done')).toHaveLength(1);
+ const sessions = path.join(root, 'agent', 'sessions');
+ const transcripts = readdirSync(sessions, { recursive: true }).map(name => path.join(sessions, name.toString())).filter(name => name.endsWith('.jsonl'));
+ const entries = transcripts.flatMap(name => readFileSync(name, 'utf8').trim().split('\n').map(line => JSON.parse(line)));
+ expect(entries.filter(entry => entry.type === 'message' && entry.message?.role === 'user')).toHaveLength(1);
+ expect(entries.filter(entry => entry.type === 'message' && entry.message?.role === 'assistant')).toHaveLength(1);
+ if (mode === 'always-fails') {
+ expect(first.filter(e => e.type === 'error')).toHaveLength(1);
+ expect(first.find(e => e.type === 'error')?.data).toMatchObject({ piCacheRecoveryHandled: true });
+ expect(existsSync(storeFile)).toBe(false);
+ persistentFailure = false; rejectCache = false;
+ await run(); expect(received).toHaveLength(3);
+ expect(received[2]).toHaveProperty('prompt_cache_key');
return;
}
- expect(succeeded.some((event) => event.type === 'error')).toBe(false);
- expect(succeeded.find((event) => event.type === 'done')?.data).toMatchObject({ result: 'OK' });
- await handle.close();
- handle = await agent.startSession({ ...opts, sessionId: 'new-task' });
- expect((await run({})).some((event) => event.type === 'error')).toBe(false);
- expect(received).toHaveLength(3);
- expect(received[2]).not.toHaveProperty('prompt_cache_key');
- await handle.close();
- // A different endpoint under the same connection retains native caching.
- endpoint += '/different';
- rejectCacheFields = false;
- handle = await agent.startSession({ ...opts, sessionId: 'working-gateway' });
- expect((await run({})).some((event) => event.type === 'error')).toBe(false);
- expect(received).toHaveLength(4);
- expect(received[3]).toHaveProperty('prompt_cache_key');
- expect(received[3]).toHaveProperty('prompt_cache_retention', '24h');
+ expect(first.some(e => e.type === 'error')).toBe(false);
+ expect(first.find(e => e.type === 'done')?.data).toMatchObject({ result: 'OK' });
+ if (mode === 'supported') { expect(existsSync(storeFile)).toBe(false); return; }
+ const store = JSON.parse(readFileSync(storeFile, 'utf8'));
+ const records = Object.values(store.routes) as Array<{ learnedAt: number; expiresAt: number; evidence: string }>;
+ expect(records).toHaveLength(1);
+ expect(records[0].expiresAt - records[0].learnedAt).toBe((mode === 'explicit' ? 7 : 1) * 86400000);
+ expect(readFileSync(storeFile, 'utf8')).not.toContain(endpoint);
+ await handle.close(); handle = await agent.startSession({ ...opts, sessionId: 'learned' });
+ await run(); expect(received).toHaveLength(3); expect(received[2]).not.toHaveProperty('prompt_cache_key');
+ // The same running Pi must restore real fields when its rule expires.
+ records[0].expiresAt = Date.now() - 1;
+ writeFileSync(storeFile, JSON.stringify(store)); rejectCache = false;
+ await run(); expect(received).toHaveLength(4); expect(received[3]).toHaveProperty('prompt_cache_key');
+ await handle.close(); endpoint += '/other';
+ handle = await agent.startSession({ ...opts, sessionId: 'other-route' });
+ await run(); expect(received[4]).toHaveProperty('prompt_cache_retention', '24h');
} finally {
- await handle?.close();
- gateway.closeAllConnections();
- await new Promise<void>((resolve) => gateway.close(() => resolve()));
+ await handle?.close(); gateway.closeAllConnections();
+ await new Promise<void>(resolve => gateway.close(() => resolve()));
rmSync(root, { recursive: true, force: true });
}
- }, 60_000);
+ }, 60000);
});
diff --git a/packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts b/packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts
new file mode 100644
index 0000000000..21f4ffa649
--- /dev/null
+++ b/packages/maker-core/src/agents/pi/__tests__/cache-recovery.test.ts
@@ -0,0 +1,181 @@
+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() {
+ let now = 1000000000;
+ const files = new Map<string, string>();
+ const handlers = new Map<string, (...args: any[]) => void>();
+ 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' } },
+ ...fs, path, createHash, randomUUID, Buffer, Date: { now: () => now }, homedir: () => '/home/fixture',
+ });
+ const fresh = () => handlers.get('input')!({ source: 'rpc' }, { isIdle: () => true });
+ 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, 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('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');
+ });
+
+ 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(['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 --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
index f8f04fbb43..b8a9b6c518 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
@@ -2738,21 +2738,3 @@ it('leaves a direct tool alone when its required name is not the Cindy envelope'
expect(requests).toHaveLength(1);
expect(requests[0].params).toEqual({ name: 'write_file', arguments: { content: 'text' } });
});
-
-it('removes cache fields only for an exact learned Chat Completions route and preserves Fast', () => {
- const start = CINDY_BRIDGE_EXTENSION_SOURCE.indexOf(' function learnedCachePayload(');
- const end = CINDY_BRIDGE_EXTENSION_SOURCE.indexOf(" pi.on('before_provider_request'", start);
- const model = { provider: 'connection-a', id: 'wire', baseUrl: 'http://localhost/api.openai.com/v1', api: 'openai-completions' };
- let rules = JSON.stringify([model]);
- const adapt = new Function('path', 'process', 'statSync', 'readFileSync', CINDY_BRIDGE_EXTENSION_SOURCE.slice(start, end) + '; return learnedCachePayload;')(
- { join: (...v: string[]) => v.join('/') }, { env: { PI_CODING_AGENT_DIR: '/isolated' } }, () => ({ size: rules.length }), () => rules,
- );
- const original = { messages: [], prompt_cache_key: 'key', prompt_cache_retention: '24h', service_tier: 'priority' };
- expect(adapt(original, model)).toEqual({ messages: [], service_tier: 'priority' });
- expect(original.prompt_cache_key).toBe('key');
- expect(adapt(original, { ...model, provider: 'connection-b' })).toBe(original);
- expect(adapt(original, { ...model, baseUrl: 'http://other/v1' })).toBe(original);
- expect(adapt(original, { ...model, api: 'openai-responses' })).toBe(original);
- rules = '{bad-json';
- expect(adapt(original, model)).toBe(original);
-});
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
index fa83b716a7..b9c2267050 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
@@ -919,6 +919,24 @@ describe('Cindy durable PI Subagent runner', () => {
};
}
+ it('freezes cache route rules into a resumed generation and rejects oversized artifacts', async () => {
+ const { fixture, first } = await resumableFixture();
+ const sourceFile = path.join(fixture.root, first.runId, 'pi-home', 'pi-cache-compatibility.json');
+ const rules = JSON.stringify([{ provider: 'fixture', id: 'wire', routeKey: 'a'.repeat(64) }]);
+ await writeFile(sourceFile, rules);
+ const launchRunner = vi.fn(async () => {});
+ const id = await resumePiSubagentRun(fixture.root, first.runId, 'continue', { ...resumeLaunch(fixture), launchRunner });
+ if (!id) throw new Error('Expected a resumed generation');
+ expect(await readFile(path.join(fixture.root, id, 'pi-home', 'pi-cache-compatibility.json'), 'utf8')).toBe(rules);
+ expect(launchRunner).toHaveBeenCalledOnce();
+ // This unlaunched queued generation would otherwise block the second attempt.
+ await rm(path.join(fixture.root, id), { recursive: true, force: true });
+ await writeFile(sourceFile, 'x'.repeat(256 * 1024 + 1));
+ await expect(resumePiSubagentRun(fixture.root, first.runId, 'continue', { ...resumeLaunch(fixture), launchRunner }))
+ .rejects.toThrow('cache rules escaped');
+ expect(launchRunner).toHaveBeenCalledOnce();
+ });
+
it('refuses when a live instance already holds the claim', async () => {
const { fixture, first } = await resumableFixture();
// process.ppid is certainly alive and is not us.
diff --git a/packages/maker-core/src/agents/pi/__tests__/nativeProviderAdapterRefresh.test.ts b/packages/maker-core/src/agents/pi/__tests__/nativeProviderAdapterRefresh.test.ts
index 877816aac5..8340a149b5 100644
--- a/packages/maker-core/src/agents/pi/__tests__/nativeProviderAdapterRefresh.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/nativeProviderAdapterRefresh.test.ts
@@ -5,7 +5,7 @@ import { CINDY_BRIDGE_EXTENSION_SOURCE } from '../cindy-bridge-source.js';
import { nativeProviderAdapterAliases, PI_NATIVE_PROVIDER_ADAPTER_SOURCE } from '../native-provider-adapter-source.js';
type RegisteredProvider = { id: string; auth: { apiKey?: unknown; oauth?: unknown }; getModels: () => unknown[];
- streamSimple(model: unknown, context: unknown, options: unknown): Promise<unknown> };
+ streamSimple(model: unknown, context: unknown, options: unknown): Promise<unknown> | AsyncIterable<unknown> };
type Alias = { id: string; name: string; provider: string; keyEnv?: string; remoteProviderId?: string; models?: Array<{ id: string; grant: string }> };
@@ -35,6 +35,10 @@ function loadAdapter(initialAliases: Alias[] = [], network?: { fetch: unknown; s
const ctx = {
modelRegistry: {
getAll: () => models,
+ getRegisteredNativeProvider: (id: string) => providers.get(id),
+ getProvider: (id: string) => providers.get(id) ?? ({ id, name: id, auth: { apiKey: 'preserved-auth' },
+ getModels: () => models.filter(model => model.provider === id),
+ filterModels: () => [], stream: network?.stream, streamSimple: network?.stream }),
async refresh(options: { allowNetwork?: boolean }) {
expect(options).toEqual({ allowNetwork: false });
actions.push('refresh');
@@ -61,7 +65,7 @@ function loadAdapter(initialAliases: Alias[] = [], network?: { fetch: unknown; s
parseCindyProviderRefreshSnapshot: (raw: unknown, nonce: string) => {
nonce: string; env: Record<string, string>; aliases: Alias[];
} | undefined;
- registerCindyNativeProviderAdapters: (pi: unknown) => Promise<{
+ registerCindyNativeProviderAdapters: (pi: unknown, cacheRecovery?: unknown) => Promise<{
refresh: (snapshot: { nonce: string; env: Record<string, string>; aliases: Alias[] }, ctx: unknown, secrets: Set<string>) => Promise<void>;
}>;
};
@@ -69,6 +73,62 @@ function loadAdapter(initialAliases: Alias[] = [], network?: { fetch: unknown; s
}
describe('Cindy native provider refresh bridge', () => {
+ it('wraps only managed cache routes and keeps the native serializer and payload hook', async () => {
+ const stream = vi.fn(async function* () {});
+ const h = loadAdapter([], { fetch: vi.fn(), stream });
+ h.setModels([{ provider: 'managed', id: 'wire' }, { provider: 'extension-owned', id: 'other' }]);
+ await h.ctx.modelRegistry.refresh({ allowNetwork: false });
+ const wrap = vi.fn((model, options, invoke) => invoke(options));
+ await h.adapter.registerCindyNativeProviderAdapters(h.pi, { accepts: (model: any) => model.provider === 'managed', wrap });
+ await h.handlers.get('session_start')!({}, h.ctx);
+ expect([...h.providers.keys()]).toEqual(['managed']);
+ expect(h.providers.get('managed')).toMatchObject({ auth: { apiKey: 'preserved-auth' }, filterModels: expect.any(Function) });
+ const model = { provider: 'managed', id: 'wire', api: 'openai-responses' };
+ const options = { apiKey: 'fixture-key', onPayload: vi.fn() };
+ await h.providers.get('managed')!.streamSimple(model, { messages: [] }, options);
+ expect(wrap).toHaveBeenCalledWith(model, options, expect.any(Function), 'managed');
+ expect(stream).toHaveBeenCalledWith(model, { messages: [] }, options);
+ });
+ it.each(['original', 'newer', 'removed'])('preserves extension provider ownership across catalog refresh: %s', async owner => {
+ const fallbackStream = vi.fn(async function* () {});
+ const h = loadAdapter([], { fetch: vi.fn(), stream: fallbackStream });
+ const models = [{ provider: 'managed', id: 'wire' }];
+ h.setModels(models);
+ await h.ctx.modelRegistry.refresh({ allowNetwork: false });
+ const provider = (name: string) => ({ id: 'managed', name, auth: { apiKey: name },
+ getModels: () => models, filterModels: vi.fn(),
+ stream: vi.fn(async function* () {}), streamSimple: vi.fn(async function* () {}) });
+ const original = provider('original-extension');
+ const replacement = provider('newer-extension');
+ h.pi.registerProvider(original);
+ const adapter = await h.adapter.registerCindyNativeProviderAdapters(h.pi, { accepts: () => true,
+ wrap: (_model: unknown, options: unknown, invoke: any) => invoke(options) });
+ await h.handlers.get('session_start')!({}, h.ctx);
+ if (owner === 'newer') h.pi.registerProvider(replacement);
+ if (owner === 'removed') h.pi.unregisterProvider('managed');
+ await adapter.refresh({ nonce: 'refresh', env: {}, aliases: [] }, h.ctx, new Set());
+ const current = h.providers.get('managed')!;
+ if (owner === 'removed') {
+ expect(current.auth).toEqual({ apiKey: 'preserved-auth' });
+ expect(original.streamSimple).not.toHaveBeenCalled();
+ return;
+ }
+ const expected = owner === 'newer' ? replacement : original;
+ expect(current.auth).toBe(expected.auth);
+ expect(current).toMatchObject({ filterModels: expected.filterModels });
+ await current.streamSimple({ provider: 'managed', id: 'wire', api: 'openai-responses' }, { messages: [] }, {});
+ expect(expected.streamSimple).toHaveBeenCalledOnce();
+ expect(fallbackStream).not.toHaveBeenCalled();
+ });
+ it.each(['getProvider', 'getRegisteredNativeProvider'])('leaves an older runtime provider intact without %s', async missing => {
+ const h = loadAdapter();
+ h.setModels([{ provider: 'managed', id: 'wire' }]);
+ await h.ctx.modelRegistry.refresh({ allowNetwork: false });
+ (h.ctx.modelRegistry as any)[missing] = undefined;
+ await h.adapter.registerCindyNativeProviderAdapters(h.pi, { accepts: () => true });
+ await h.handlers.get('session_start')!({}, h.ctx);
+ expect(h.providers.size).toBe(0);
+ });
// Review 4333949156 / sharing §12, unit: existing native-stream coverage only
// used guest=true. An owner-hosted BYOM provider must also query its real source
// before the SDK, including after refresh; revoked/failed checks cannot stream.
diff --git 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
index e67b608d3f..b53111e7f2 100644
--- 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
@@ -1366,24 +1366,22 @@ describe("Pi provider-aware model routing", () => {
supportsStore: false,
supportsDeveloperRole: false,
maxTokensField: "max_tokens",
- supportsLongCacheRetention: false,
}),
}),
// The learned wire model is distinct from the public model alias.
expect.objectContaining({
id: "glm-5.3-wire",
- compat: { supportsLongCacheRetention: false },
}),
]);
// BYOM 命名空间 provider 用 sourceProviderId 命中同一条学习结果。
expect(models.providers["cindy-byom-opencode-go"]?.models).toEqual([
expect.objectContaining({
id: "glm-5.3-flash",
- compat: { supportsLongCacheRetention: false },
}),
]);
const rules = JSON.parse(readFileSync(path.join(captured.env.PI_CODING_AGENT_DIR as string, 'pi-cache-compatibility.json'), 'utf8'));
- expect(rules).toContainEqual({ provider: 'opencode-go', id: 'glm-5.3-wire', baseUrl: proxied ? 'http://127.0.0.1:19988/v1' : 'https://opencode.ai/zen/go/v1' });
+ expect(rules).toContainEqual(expect.objectContaining({ provider: 'opencode-go', id: 'glm-5.3-wire', api: 'openai-completions', providerId: 'opencode-go', routeKey: piCacheRouteKey('https://opencode.ai/zen/go/v1', 'openai-completions'), baseUrl: proxied ? 'http://127.0.0.1:19988/v1' : 'https://opencode.ai/zen/go/v1' }));
+ for (const model of models.providers['opencode-go']!.models!) expect(model.compat ?? {}).not.toHaveProperty('supportsLongCacheRetention', false);
await handle.close();
});
@@ -2634,7 +2632,7 @@ describe("Pi provider-aware model routing", () => {
});
it("does not live-refresh xAI when the caller pins the gateway with providerId null", async () => {
- let resolves = 0;
+ const resolvedProviders: Array<string | null | undefined> = [];
captured.requestHandler = async (command) =>
command.type === "get_state"
? {
@@ -2682,8 +2680,8 @@ describe("Pi provider-aware model routing", () => {
},
resolvePiGatewayModelApi: () => "openai-responses",
resolvePiAgentHome: () => agentHome,
- resolvePiNativeProviders: async () => {
- resolves += 1;
+ resolvePiNativeProviders: async (selection) => {
+ resolvedProviders.push(selection?.providerId);
return {
providers: [
{
@@ -2704,10 +2702,11 @@ describe("Pi provider-aware model routing", () => {
model: "local-model",
providerId: "native-a",
});
- expect(resolves).toBe(1);
+ expect(resolvedProviders).toEqual(["native-a"]);
captured.requests.length = 0;
await handle.setModel!("xai/grok-4.6", { providerId: null });
- expect(resolves).toBe(2);
+ // Changing the gateway connection identity may refresh its cache route rules.
+ expect(resolvedProviders.slice(1).every(id => id === null)).toBe(true);
expect(captured.requests).toContainEqual({
type: "set_model",
provider: "cindy",
@@ -3362,7 +3361,7 @@ describe("Pi provider-aware model routing", () => {
runtimeConfig: {
endpoint: "http://127.0.0.1:9988/",
get remoteEndpoint() {
- if (api !== 'openai-completions') throw new Error('Unrelated protocol must not read cache identity');
+ if (api !== 'openai-completions' && api !== 'openai-responses') throw new Error('Unrelated protocol must not read cache identity');
return 'https://gateway.example.test/v1';
},
},
@@ -3565,7 +3564,7 @@ describe("Pi provider-aware model routing", () => {
await handle.close();
});
- it('merges a learned compat override into the gateway models.json block', async () => {
+ it('keeps gateway caching native and emits exact dynamic recovery rules', async () => {
const deps: AgentDeps = {
auth: {
getState: async () => ({ authenticated: true, identity: 'test', authSource: 'api-key' as const }),
@@ -3613,10 +3612,12 @@ describe("Pi provider-aware model routing", () => {
id: 'z-ai/glm-5.3-flash',
compat: expect.objectContaining({
supportsStore: false,
- supportsLongCacheRetention: false,
}),
}),
]);
+ expect(models.providers.cindy?.models?.[0].compat).not.toHaveProperty('supportsLongCacheRetention', false);
+ const rules = JSON.parse(readFileSync(path.join(captured.env.PI_CODING_AGENT_DIR as string, 'pi-cache-compatibility.json'), 'utf8'));
+ expect(rules).toContainEqual(expect.objectContaining({ provider: 'cindy', id: 'z-ai/glm-5.3-flash', providerId: 'xd', api: 'openai-completions' }));
await handle.close();
});
@@ -8318,6 +8319,10 @@ describe("Pi provider-aware model routing", () => {
)?.[1];
expect(modelsJson).toBeTruthy();
expect(settingsJson).toBeTruthy();
+ const cacheRules = [...written.entries()].find(([filePath]) =>
+ filePath.endsWith("pi-cache-compatibility.json"),
+ )?.[1];
+ expect(cacheRules).toBeTruthy();
const modelsOnlyHash = createHash("sha256")
.update(modelsJson!)
.digest("hex");
@@ -8325,6 +8330,8 @@ describe("Pi provider-aware model routing", () => {
.update(modelsJson!)
.update("\n")
.update(settingsJson!)
+ .update("\n")
+ .update(cacheRules!)
.digest("hex");
expect(launchHash).not.toBe(modelsOnlyHash);
expect(capturedRemoteEnvs).toHaveLength(1);
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
index 9d7f568079..9b3fcc3c71 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
@@ -2463,3 +2463,16 @@ it('retains tool image bytes and a video resource alongside text', () => {
});
disposePiTranslateContext(ctx);
});
+
+// A second 400 must remain terminal; Desktop may not add the old process replay.
+it('projects native cache recovery ownership onto the terminal error', () => {
+ const ctx = createPiTranslateContext(noopLogger);
+ const { queue, events } = makeQueue();
+ translatePiEvent(ev({ type: 'agent_start' }), queue, ctx);
+ translatePiEvent(ev({ type: 'message_end', message: { role: 'assistant', content: [], stopReason: 'error',
+ errorMessage: '400: {"message":"Unsupported parameter(s): prompt_cache_key"}', cindyCacheRecoveryHandled: true } }), queue, ctx);
+ translatePiEvent(ev({ type: 'agent_settled' }), queue, ctx);
+ expect(events.filter(event => event.type === 'error')).toHaveLength(1);
+ expect(events.find(event => event.type === 'error')?.data).toMatchObject({ piCacheRecoveryHandled: true, isTerminal: true });
+ disposePiTranslateContext(ctx);
+});
diff --git a/packages/maker-core/src/agents/pi/cache-recovery-source.ts b/packages/maker-core/src/agents/pi/cache-recovery-source.ts
new file mode 100644
index 0000000000..2eed2547c6
--- /dev/null
+++ b/packages/maker-core/src/agents/pi/cache-recovery-source.ts
@@ -0,0 +1,205 @@
+/** 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 day = 24 * 60 * 60 * 1000;
+ const home = process.env.CINDY_PI_CACHE_COMPAT_FILE;
+ const file = home && home.replace(/^\$HOME(?=\/)/, () => homedir());
+ let turn;
+ 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 refreshRules() {
+ routeRules = 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])]];
+ }));
+ }
+ 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 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/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;
+ return /(?: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';
+ }
+ pi.on('input', (event, ctx) => {
+ if (ctx.isIdle() && !event.streamingBehavior && event.source === 'rpc') {
+ refreshRules();
+ 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;
+ }
+ turn = { sequence: ++sequence, safe: true, valid: true, attempted: false, records };
+ } else if (turn) { turn.safe = false; turn.valid = false; }
+ });
+ for (const name of ['tool_call', 'tool_execution_start', 'model_select', 'session_switch', 'session_shutdown']) {
+ pi.on(name, () => { if (turn) { turn.safe = false; turn.valid = false; } });
+ }
+ return {
+ refreshRules,
+ accepts: model => Boolean(route(model)),
+ wrap(model, options, invoke, provider = model.provider) {
+ const key = route(model);
+ if (!key || !turn) return invoke(options);
+ const owner = turn;
+ const startedAt = Date.now();
+ const learned = active(owner.records[key], startedAt);
+ 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;
+ },
+ });
+ return (async function* () {
+ 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.records[key] = record;
+ }
+ if (retried.type !== 'error') owner.safe = false;
+ yield retried.type === 'error'
+ ? { ...retried, error: { ...retried.error, cindyCacheRecoveryHandled: true } } : retried;
+ }
+ return;
+ }
+ })();
+ },
+ };
+}
+`;
diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
index f914131e7a..b505534a0b 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -1,3 +1,4 @@
+import { PI_CACHE_RECOVERY_SOURCE } from './cache-recovery-source.js';
import { PI_NATIVE_PROVIDER_ADAPTER_SOURCE } from './native-provider-adapter-source.js';
/**
* cindy-bridge —— 写进 pi agentHome/extensions/ 的扩展源码(字符串常量)。
@@ -69,7 +70,8 @@ import {
closeSync, globSync, lstatSync, openSync, opendirSync, readFileSync, realpathSync,
renameSync, rmSync, statSync, writeFileSync,
} from 'node:fs';
-import { createHash } from 'node:crypto';
+import { createHash, randomUUID } from 'node:crypto';
+import { homedir } from 'node:os';
import { stat } from 'node:fs/promises';
import { spawn } from 'node:child_process';
import { createInterface } from 'node:readline';
@@ -4068,6 +4070,8 @@ async function nativeFastPayload(payload, model, ctx) {
return out;
}
+${PI_CACHE_RECOVERY_SOURCE}
+
${PI_NATIVE_PROVIDER_ADAPTER_SOURCE}
// ─── 设备托管:执行器操作 ───────────────────────────────────────────
@@ -4261,7 +4265,8 @@ export default async function cindyBridge(piApi: any) {
});
}
installTextOnlyTurnPolicy(pi);
- const nativeProviderAdapters = await registerCindyNativeProviderAdapters(pi);
+ const cacheRecovery = installCindyCacheRecovery(pi);
+ const nativeProviderAdapters = await registerCindyNativeProviderAdapters(pi, cacheRecovery);
const initialNativeSettings = typeof pi.getSettings === 'function' ? undefined
: (() => {
try {
@@ -4312,22 +4317,9 @@ export default async function cindyBridge(piApi: any) {
},
});
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 learnedCachePayload((await nativeFastPayload(payload, ctx.model, ctx)) ?? payload, ctx.model);
+ return (await nativeFastPayload(payload, ctx.model, ctx)) ?? payload;
});
const mcpGateway = new CindyMcpGateway();
// bash 隔离 home 经 resolveBashPackageHome 解析(首次加载读删 + 防篡改 stash,
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index a04d9ac3eb..9ff1675547 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -2292,9 +2292,9 @@ export class PiAgent extends BaseAgent {
cacheRoutes: Map<string, PiCacheCompatibility>;
gatewayImageInputByModel: Map<string, boolean>;
gatewayApiByModel: Map<string, PiGatewayApi>;
- /** models.json + settings.json 内容 sha256 —— 远端 daemon 启动身份的一部分。 */
+ /** Models, settings and cache route rules; remote daemon launch identity. */
modelsJsonHash: string;
- /** Models alone, excluding the independently mutable compaction settings. */
+ /** Models and cache routes, excluding independently mutable compaction settings. */
modelConfigHash: string;
}> {
// 远端:baseUrl 用 host 注入的 upstream endpoint(remoteEndpoint,gateway key 同源),
@@ -2326,22 +2326,24 @@ 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 cacheRequestRules: Array<{ provider: string; id: string; baseUrl: string; api: string; providerId: string; routeKey: string }> = [];
const learnedCompat = (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 (opts.remote || api !== 'openai-completions') return undefined;
+ if (api !== 'openai-completions' && api !== 'openai-responses') return undefined;
const routeKey = piCacheRouteKey(baseUrl, api);
if (!routeKey) return undefined;
- const compat = this.deps.resolvePiNativeCompatOverride?.(providerId, wireModelId, routeKey);
+ const compat = opts.remote ? undefined : this.deps.resolvePiNativeCompatOverride?.(providerId, wireModelId, routeKey);
cacheRoutes.set(JSON.stringify([runtimeProvider, wireModelId]), {
providerId, modelId, wireModelId, routeKey,
applied: compat?.supportsLongCacheRetention === false,
});
- 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 });
}
- return compat;
+ // Keep native serialization enabled; the final payload wrapper applies
+ // learning dynamically, so expiry needs no model registry rebuild.
+ return undefined;
};
const models = runtimeModels.flatMap((publicModel: ModelDescriptor) => {
// availableModels 为跨 provider 拍平的公开能力;BYOM 同 id 冲突时 effort
@@ -2387,9 +2389,9 @@ export class PiAgent extends BaseAgent {
const modelBaseUrl = endpoint ? piGatewayModelBaseUrl(endpoint, api) : undefined;
const gatewayCompat = {
...(resolvedSpec?.compat ?? {}),
- ...(!opts.remote && api === 'openai-completions' ?
+ ...((api === 'openai-completions' || api === 'openai-responses') ?
learnedCompat(gatewayProviderId ?? 'xd', m.id, m.id, PI_PROVIDER_ID,
- this.deps.runtimeConfig.remoteEndpoint ?? '', api, modelBaseUrl ?? '') ?? {} : {}),
+ this.deps.runtimeConfig.remoteEndpoint ?? modelBaseUrl ?? '', api, modelBaseUrl ?? '') ?? {} : {}),
};
gatewayApiByModel.set(m.id, api);
// Missing capability metadata must not disable newly discovered vision models.
@@ -2469,9 +2471,7 @@ export class PiAgent extends BaseAgent {
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 ?? {}),
@@ -2511,8 +2511,7 @@ export class PiAgent extends BaseAgent {
const window = Math.max(model.contextWindow ?? 0,
this.deps.resolveModelContextLimit?.(np.sourceProviderId ?? np.id, model.id) ?? 0);
const contextWindow = Number.isSafeInteger(window) && window > 0 ? window : undefined;
- const compat = cacheRoutes.get(JSON.stringify([np.id, model.wireId ?? model.id]))?.applied
- ? { ...(model.compat ?? {}), supportsLongCacheRetention: false } : undefined;
+ const compat = model.compat;
return contextWindow || compat ? [[model.wireId ?? model.id, {
...(contextWindow ? { contextWindow } : {}), ...(compat ? { compat } : {}),
}]] : [];
@@ -2576,12 +2575,16 @@ export class PiAgent extends BaseAgent {
);
// 远端 launch identity 必须覆盖进程启动才读的快照。只 hash models.json 时,
// retry/transport/compaction 改写会落到旧 configHome,daemon 纯 attach。
+ const cacheRequestRulesContent = JSON.stringify(cacheRequestRules);
+ const modelConfigHash = createHash('sha256').update(modelsJsonContent)
+ .update('\n').update(cacheRequestRulesContent).digest('hex');
const modelsJsonHash = createHash('sha256')
.update(modelsJsonContent)
.update('\n')
.update(settingsJsonContent)
+ .update('\n')
+ .update(cacheRequestRulesContent)
.digest('hex');
- const modelConfigHash = createHash('sha256').update(modelsJsonContent).digest('hex');
if (!opts.preview) {
// 诊断(排查 LAZY_CREATE_FAILED):远端写前留痕 —— 确认 writeModelsJson 是否
// 执行、endpoint 是否有值、路径形态。
@@ -2596,6 +2599,7 @@ export class PiAgent extends BaseAgent {
await opts.fileOps.mkdirp(agentHome);
// fileOps 远端写入内部已 umask 077(创建即 600,无 TOCTOU —— R5 H-1)。
await opts.fileOps.writeFile(modelsJsonPath, modelsJsonContent);
+ await opts.fileOps.writeFile(joinRemotePosixPath(agentHome, 'pi-cache-compatibility.json'), cacheRequestRulesContent);
await opts.fileOps.writeFile(settingsJsonPath, settingsJsonContent);
this.deps.logger.info?.('pi writeModelsJson done', { modelsJsonPath });
} else {
@@ -2603,9 +2607,7 @@ export class PiAgent extends BaseAgent {
// 控制面文件:显式 600,防同机其他用户读 BYOM baseUrl / provider 路由
// (R5 安全审计 H-5)。
await fs.writeFile(modelsJsonPath, modelsJsonContent, { mode: 0o600 });
- await fs.writeFile(path.join(agentHome, 'pi-cache-compatibility.json'), JSON.stringify(
- cacheRequestRules,
- ), { mode: 0o600 });
+ await fs.writeFile(path.join(agentHome, 'pi-cache-compatibility.json'), cacheRequestRulesContent, { mode: 0o600 });
await fs.writeFile(settingsJsonPath, settingsJsonContent, {
mode: 0o600,
});
@@ -5620,6 +5622,7 @@ export class PiAgent extends BaseAgent {
...(proxyEnv ?? {}),
// BYOM 原生 provider 的 api keys(键名对应 spec.apiKeyEnvVar,models.json 用 $ENV 引用)。
...nativeEnv,
+ CINDY_PI_CACHE_COMPAT_FILE: this.deps.resolvePiNativeCompatStorePath?.(opts.remoteHostId) ?? joinRemotePosixPath(agentHome, 'pi-native-compat-overrides.json'),
CINDY_PI_NATIVE_PROVIDER_ADAPTERS: JSON.stringify(nativeProviderAdapterAliases(nativeProviders, hostedGuest ? hosted?.guestProvider?.modelIds : undefined, Boolean(hosted))),
// 外部 MCP header 真值只经 env 交给 bridge extension;host 生成独立名字,
// 且这些键已进入 piSecretEnvNames,LLM 可调用的 bash 子进程拿不到。
@@ -6979,8 +6982,8 @@ export class PiAgent extends BaseAgent {
const previousGatewayModel = retainedLiveGatewayModel;
const modelsPath = joinRemotePosixPath(configHome, 'models.json');
const settingsPath = joinRemotePosixPath(configHome, 'settings.json');
- const cacheRulesPath = path.join(configHome, 'pi-cache-compatibility.json');
- const oldCacheRules = fileOps ? undefined : await fs.readFile(cacheRulesPath, 'utf8');
+ const cacheRulesPath = joinRemotePosixPath(configHome, 'pi-cache-compatibility.json');
+ const oldCacheRules = fileOps ? await fileOps.readFile(cacheRulesPath, 256 * 1024) : await fs.readFile(cacheRulesPath, 'utf8');
// Capture exact private bytes. Rebuilding from a live catalog would not be
// a rollback if the catalog changed after the session started.
const [oldModelsContent, oldSettingsContent] = await Promise.all([
@@ -7001,6 +7004,7 @@ export class PiAgent extends BaseAgent {
if (fileOps) {
await fileOps.writeFile(modelsPath, oldModelsContent);
await fileOps.writeFile(settingsPath, oldSettingsContent);
+ await fileOps.writeFile(cacheRulesPath, oldCacheRules);
} else {
await fs.writeFile(modelsPath, oldModelsContent, { mode: 0o600 });
await fs.writeFile(settingsPath, oldSettingsContent, { mode: 0o600 });
diff --git a/packages/maker-core/src/agents/pi/native-provider-adapter-source.ts b/packages/maker-core/src/agents/pi/native-provider-adapter-source.ts
index 60c0d9ba47..72ac8b545a 100644
--- a/packages/maker-core/src/agents/pi/native-provider-adapter-source.ts
+++ b/packages/maker-core/src/agents/pi/native-provider-adapter-source.ts
@@ -87,13 +87,14 @@ function parseCindyProviderRefreshSnapshot(raw: unknown, nonce: string): CindyPr
return { nonce, env, aliases };
}
-async function registerCindyNativeProviderAdapters(pi: any) {
+async function registerCindyNativeProviderAdapters(pi: any, cacheRecovery?: any) {
let aliases: CindyAdapterAlias[] = [];
try {
const initial = JSON.parse(process.env.CINDY_PI_NATIVE_PROVIDER_ADAPTERS ?? '[]');
if (Array.isArray(initial)) aliases = initial;
} catch { /* A malformed startup hint cannot affect other Pi extensions. */ }
const registered = new Set<string>();
+ const cacheRegistrations = new Map<string, { original: any; installed: any }>();
let apis: { lazyStream: any; envApiKeyAuth: any; getApiProvider: any } | undefined;
async function loadApis() {
if (!apis) {
@@ -105,10 +106,23 @@ async function registerCindyNativeProviderAdapters(pi: any) {
}
return apis;
}
- function registerAliases(models: any[], currentAliases: CindyAdapterAlias[]) {
+ function registerAliases(models: any[], currentAliases: CindyAdapterAlias[], ctx: any) {
if (!apis) throw new Error('Native provider adapter is unavailable');
const { lazyStream, envApiKeyAuth, getApiProvider } = apis;
- for (const alias of currentAliases) {
+ const preserved = new Map<string, any>();
+ const automatic = cacheRecovery ? [...new Set(models.filter((model: any) => cacheRecovery.accepts(model)).map((model: any) => model.provider))]
+ .filter(id => !currentAliases.some(alias => alias.id === id))
+ .flatMap(id => {
+ if (typeof ctx.modelRegistry.getRegisteredNativeProvider !== 'function') return [];
+ const original = ctx.modelRegistry.getProvider?.(id);
+ // Older runtimes without this API keep their original provider intact.
+ if (!original || typeof original.stream !== 'function' || typeof original.streamSimple !== 'function') return [];
+ preserved.set(id, original);
+ return [{ id, name: original.name ?? id, provider: id }];
+ }) : [];
+ for (const alias of [...currentAliases, ...automatic]) {
+ const original = preserved.get(alias.id);
+ const nativeOriginal = original ? ctx.modelRegistry.getRegisteredNativeProvider(alias.id) : undefined;
const providerModels = models.filter((model: any) => model.provider === alias.id
&& (!alias.models || alias.models.some(allowed => allowed.id === model.id)));
if (providerModels.length === 0 && !alias.models) continue;
@@ -123,7 +137,7 @@ async function registerCindyNativeProviderAdapters(pi: any) {
});
if (!response.ok || (await response.json()).allowed !== true) throw new Error('The model is not authorized for this remote task');
}
- const api = getApiProvider(model.api);
+ const api = original ?? getApiProvider(model.api);
if (!api) throw new Error('The selected native provider API is unavailable');
const nativeModel = { ...model, provider: alias.provider };
const nativeContext = { ...context, messages: context.messages.map((message: any) =>
@@ -148,7 +162,8 @@ async function registerCindyNativeProviderAdapters(pi: any) {
},
};
}
- const events = simple ? api.streamSimple(nativeModel, nativeContext, nativeOptions) : api.stream(nativeModel, nativeContext, nativeOptions);
+ const invoke = (requestOptions: any) => simple ? api.streamSimple(nativeModel, nativeContext, requestOptions) : api.stream(nativeModel, nativeContext, requestOptions);
+ const events = cacheRecovery ? cacheRecovery.wrap(model, nativeOptions, invoke, nativeModel.provider) : invoke(nativeOptions);
return (async function* () {
for await (const event of events) yield { ...event,
...(event.partial ? { partial: { ...event.partial, provider: alias.id } } : {}),
@@ -157,19 +172,25 @@ async function registerCindyNativeProviderAdapters(pi: any) {
};
})();
});
- pi.registerProvider({ id: alias.id, name: alias.name,
+ pi.registerProvider({ ...original, id: alias.id, name: alias.name,
// Pi's native Provider contract requires auth even when Cindy's proxy handles OAuth.
- auth: alias.keyEnv ? { apiKey: envApiKeyAuth('API key', [alias.keyEnv]) } : {},
- getModels: () => providerModels,
+ auth: original?.auth ?? (alias.keyEnv ? { apiKey: envApiKeyAuth('API key', [alias.keyEnv]) } : {}),
+ getModels: original ? () => original.getModels() : () => providerModels,
stream: (model: any, context: any, options: any) => stream(model, context, options, false),
streamSimple: (model: any, context: any, options: any) => stream(model, context, options, true),
});
+ if (original) cacheRegistrations.set(alias.id, { original: nativeOriginal,
+ installed: ctx.modelRegistry.getRegisteredNativeProvider(alias.id) });
registered.add(alias.id);
}
}
if (aliases.length) await loadApis();
- pi.on('session_start', (_event: any, ctx: any) => {
- if (aliases.length) registerAliases(ctx.modelRegistry.getAll(), aliases);
+ pi.on('session_start', async (_event: any, ctx: any) => {
+ const models = ctx.modelRegistry.getAll();
+ if (aliases.length || cacheRecovery && models.some((model: any) => cacheRecovery.accepts(model))) {
+ if (!apis) await loadApis();
+ registerAliases(models, aliases, ctx);
+ }
});
return {
async refresh(snapshot: CindyProviderRefreshSnapshot, ctx: any, secretEnvNames: Set<string>) {
@@ -186,12 +207,26 @@ async function registerCindyNativeProviderAdapters(pi: any) {
process.env[name] = value;
secretEnvNames.add(name);
}
- for (const id of registered) pi.unregisterProvider(id);
+ for (const id of registered) {
+ const registration = cacheRegistrations.get(id);
+ if (!registration) pi.unregisterProvider(id);
+ else if (ctx.modelRegistry.getRegisteredNativeProvider(id) === registration.installed) {
+ // Unregister restores built-ins, not a prior extension registration.
+ // Restore that registration only while this wrapper still owns it.
+ if (registration.original) pi.registerProvider(registration.original);
+ else pi.unregisterProvider(id);
+ }
+ }
registered.clear();
+ cacheRegistrations.clear();
const result = await ctx.modelRegistry.refresh({ allowNetwork: false });
if (result?.aborted || result?.errors?.size) throw new Error('Native provider catalog refresh failed');
aliases = snapshot.aliases;
- if (aliases.length) registerAliases(ctx.modelRegistry.getAll(), aliases);
+ cacheRecovery?.refreshRules?.();
+ if (aliases.length || cacheRecovery) {
+ await loadApis();
+ registerAliases(ctx.modelRegistry.getAll(), aliases, ctx);
+ }
// Registering providers schedules an internal refresh in Pi. Await one
// public refresh so the receipt reflects the final provider snapshot.
const final = await ctx.modelRegistry.refresh({ allowNetwork: false });
diff --git a/packages/maker-core/src/agents/pi/pi-subagent-runs.ts b/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
index 098d4f43d8..b9bf20dc5f 100644
--- a/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
+++ b/packages/maker-core/src/agents/pi/pi-subagent-runs.ts
@@ -2724,6 +2724,19 @@ async function resumeClaimedPiSubagentRun(
fs.readFile(selectedRunnerFile),
]);
JSON.parse(modelsJson.toString('utf8'));
+ // Optional for older durable snapshots; apply the same artifact boundary as models.json.
+ let cacheRules: Buffer | undefined;
+ try {
+ const sourceCacheRules = path.join(sourceConfigHome, 'pi-cache-compatibility.json');
+ const [info, canonical] = await Promise.all([fs.lstat(sourceCacheRules), fs.realpath(sourceCacheRules)]);
+ if (info.isSymbolicLink() || !info.isFile() || info.size > 256 * 1024
+ || path.dirname(canonical) !== canonicalConfigHome) {
+ throw new Error('PI Subagent resume cache rules escaped their source run');
+ }
+ cacheRules = await fs.readFile(sourceCacheRules);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
+ }
const runId = randomUUID();
const runDir = path.join(root, runId);
const childConfigHome = path.join(runDir, 'pi-home');
@@ -2793,6 +2806,7 @@ async function resumeClaimedPiSubagentRun(
}
await fs.mkdir(childConfigHome, { recursive: true, mode: 0o700 });
await fs.writeFile(path.join(childConfigHome, 'models.json'), modelsJson, { mode: 0o600, flag: 'wx' });
+ if (cacheRules) await fs.writeFile(path.join(childConfigHome, 'pi-cache-compatibility.json'), cacheRules, { mode: 0o600, flag: 'wx' });
await copyStagedRipgrep(
sourceConfigHome,
childConfigHome,
diff --git a/packages/maker-core/src/agents/pi/translator.ts b/packages/maker-core/src/agents/pi/translator.ts
index ff00ad3b6a..c54a5a2b63 100644
--- a/packages/maker-core/src/agents/pi/translator.ts
+++ b/packages/maker-core/src/agents/pi/translator.ts
@@ -72,6 +72,7 @@ interface PiUsage {
}
interface PiAssistantMessage {
+ cindyCacheRecoveryHandled?: boolean;
role: 'assistant';
content?: Array<Record<string, unknown>>;
usage?: PiUsage;
@@ -102,6 +103,7 @@ interface PiThinkingBlock {
export interface PiTranslateContext {
getCacheCompatibility?: () => import('./cache-compatibility.js').PiCacheCompatibility | undefined;
cacheRecoveryHasOutput?: boolean;
+ cacheRecoveryHandled?: boolean;
logger: Logger;
/** Host-owned live tariff selector fallback when the provider response has no accepted tier. */
getPriceVariant?: () => 'standard' | 'priority';
@@ -685,6 +687,7 @@ export function translatePiEvent(
case 'agent_start': {
ctx.turnGeneration += 1;
ctx.cacheRecoveryHasOutput = false;
+ ctx.cacheRecoveryHandled = false;
ctx.pendingHostTurnStartToken = null;
if (ctx.hostAbortRequestGeneration !== ctx.turnGeneration) {
clearPiHostAbortRequests(ctx);
@@ -752,6 +755,7 @@ export function translatePiEvent(
case 'message_end': {
const message = event.message as PiAssistantMessage | undefined;
if (!message || message.role !== 'assistant') return;
+ if (message.cindyCacheRecoveryHandled === true) ctx.cacheRecoveryHandled = true;
const pendingPriceVariant = ctx.pendingPriceVariants.shift();
const reportedPriceVariant = priceVariantFromServiceTier(message.usage?.service_tier);
const executionPriceVariant = message.usage && ctx.resolveUsagePriceVariant?.({
@@ -1037,6 +1041,7 @@ export function translatePiEvent(
data: {
...terminalError,
piCacheRecoverySafe: ctx.cacheRecoveryHasOutput === false,
+ ...(ctx.cacheRecoveryHandled ? { piCacheRecoveryHandled: true } : {}),
isTerminal: true,
},
source: 'pi',
@@ -1166,6 +1171,7 @@ export function translatePiEvent(
...(finalError.reason === UNSUPPORTED_REQUEST_OPTION_REASON ? {
piCacheCompatibility: ctx.getCacheCompatibility?.(),
piCacheRecoverySafe: ctx.cacheRecoveryHasOutput === false,
+ ...(ctx.cacheRecoveryHandled ? { piCacheRecoveryHandled: true } : {}),
} : {}),
isTerminal: true,
...(exhaustedReason ? { reason: exhaustedReason } : {}),