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): avoid inherited model rebuild loops (#5387) (#5532)

Tsai Yuane462fcc126a4
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 114b05d512..1e19efc887 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
@@ -3957,7 +3957,7 @@ describe("Pi provider-aware model routing", () => {
it.each([
{ label: 'different live window', window: 400_000, action: 'rebuild' },
- { label: 'matching live window', window: 128_000, action: 'hot' },
+ { label: 'matching live window', window: 128_000, action: 'rebuild' },
{ label: 'missing live window', window: undefined, action: 'rebuild' },
{ label: 'failed live catalog read', window: null, action: 'rebuild' },
] as const)('checks an inherited model before switching: $label', async ({ window, action }) => {
@@ -3980,11 +3980,16 @@ describe("Pi provider-aware model routing", () => {
? { sessionFile: '/mock/s.jsonl', model: { contextWindow: captured.runtimeProvider === 'pi-native' ? window : 200_000 } }
: {} };
};
- const handle = await new PiAgent(deps).startSession({
+ const agent = new PiAgent(deps);
+ const handle = await agent.startSession({
sessionId: 'inherited-window', workingDir: cwd, model: 'local-model', providerId: 'native-a',
});
const config = captured.env.PI_CODING_AGENT_DIR!;
const before = ['models.json', 'settings.json'].map(file => readFileSync(path.join(config, file), 'utf8'));
+ const startupSettings = JSON.parse(before[1]) as {
+ compaction?: { modelOverrides?: Record<string, { reserveTokens?: number }> };
+ };
+ expect(startupSettings.compaction?.modelOverrides).not.toHaveProperty('pi-native/wire-model');
captured.requests.length = 0;
const preview = await handle.previewModelSwitch?.('catalog-alias', { providerId: 'native-account' });
expect(preview).toMatchObject({ action, targetContextWindow: window ?? null, windowVerified: false });
@@ -4002,6 +4007,69 @@ describe("Pi provider-aware model routing", () => {
expect(captured.closes).toBe(0);
}
await handle.close();
+ if (typeof window === 'number') {
+ const rebuilt = await agent.startSession({
+ sessionId: 'inherited-window', workingDir: cwd,
+ model: 'catalog-alias', providerId: 'native-account',
+ });
+ const rebuiltSettings = JSON.parse(readFileSync(
+ path.join(captured.env.PI_CODING_AGENT_DIR!, 'settings.json'), 'utf8',
+ )) as { compaction?: { modelOverrides?: Record<string, { reserveTokens?: number }> } };
+ expect(rebuiltSettings.compaction?.modelOverrides?.['pi-native/wire-model']?.reserveTokens)
+ .toBe(window * 0.25);
+ await rebuilt.close();
+ }
+ });
+
+ it('keeps a learned inherited window below a later Cindy working-window override', async () => {
+ let workingWindow: number | undefined;
+ const deps = byomDeps(async () => ({ providers: [
+ { id: 'native-a', name: 'Current', baseUrl: 'http://a.test', api: 'openai-completions',
+ models: [{ id: 'local-model', contextWindow: 200_000 }] },
+ { id: 'pi-native', sourceProviderId: 'native-account', name: 'Inherited', baseUrl: 'http://b.test',
+ inheritModels: true, modelIdAliases: { 'catalog-alias': 'catalog-model' },
+ models: [{ id: 'catalog-model', wireId: 'wire-model' }] },
+ ], env: {} }));
+ deps.runtimeConfig = { ...deps.runtimeConfig, piAutoCompactThresholdPct: 75 };
+ deps.resolveModelContextLimit = (provider, model) =>
+ provider === 'native-account' && (model === 'catalog-model' || model === 'catalog-alias')
+ ? workingWindow
+ : undefined;
+ captured.requestHandler = async (command) => {
+ if (command.type === 'get_available_models') return { success: true, data: { models: [
+ { provider: 'pi-native', id: 'wire-model', contextWindow: 400_000 },
+ ] } };
+ return { success: true, data: command.type === 'get_state'
+ ? { sessionFile: '/mock/s.jsonl', model: { contextWindow: captured.runtimeProvider === 'pi-native' ? 400_000 : 200_000 } }
+ : {} };
+ };
+
+ const agent = new PiAgent(deps);
+ const initial = await agent.startSession({
+ sessionId: 'learned-working-window', workingDir: cwd,
+ model: 'local-model', providerId: 'native-a',
+ });
+ expect(await initial.previewModelSwitch?.('catalog-alias', { providerId: 'native-account' }))
+ .toMatchObject({ action: 'rebuild', targetContextWindow: 400_000 });
+ await initial.close();
+
+ workingWindow = 150_000;
+ captured.initialProvider = undefined;
+ captured.initialModel = undefined;
+ captured.runtimeProvider = undefined;
+ captured.runtimeModel = undefined;
+ const rebuilt = await agent.startSession({
+ sessionId: 'learned-working-window', workingDir: cwd,
+ model: 'catalog-alias', providerId: 'native-account',
+ });
+ const rebuiltSettings = JSON.parse(readFileSync(
+ path.join(captured.env.PI_CODING_AGENT_DIR!, 'settings.json'), 'utf8',
+ )) as { compaction?: { modelOverrides?: Record<string, { reserveTokens?: number }> } };
+ expect(rebuiltSettings.compaction?.modelOverrides?.['pi-native/wire-model']?.reserveTokens)
+ .toBe(37_500);
+ expect(await rebuilt.previewModelSwitch?.('catalog-alias', { providerId: 'native-account' }))
+ .toMatchObject({ action: 'hot', targetContextWindow: 150_000 });
+ await rebuilt.close();
});
it('refreshes a managed adapter when the same model ID gets a new descriptor', async () => {
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 7cbada1f30..c5d0930223 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -1745,6 +1745,8 @@ export class PiAgent extends BaseAgent {
private readonly inFlightStartups = new Set<Promise<AgentSessionHandle>>();
private readonly subagentRunners = new Map<string, PiSubagentRunnerProcess>();
private readonly subagentRunnerExitErrors = new Map<string, string>();
+ /** Local Pi-bundled windows learned from its runtime catalog for the next process rebuild. */
+ private readonly inheritedNativeContextWindows = new Map<string, number>();
private disposeStarted = false;
constructor(deps: AgentDeps) {
@@ -1814,6 +1816,10 @@ export class PiAgent extends BaseAgent {
});
}
+ private inheritedNativeContextWindowKey(provider: string, model: string): string {
+ return `${provider}\0${model}`;
+ }
+
private requestSubagentRunnerExit(
runner: PiSubagentRunnerProcess,
runId: string,
@@ -2443,7 +2449,27 @@ export class PiAgent extends BaseAgent {
if (provider.id === PI_PROVIDER_ID) continue;
for (const model of provider.models) {
const workingWindow = this.deps.resolveModelContextLimit?.(provider.sourceProviderId ?? provider.id, model.id) ?? undefined;
- addBudget(provider.id, model.wireId ?? model.id, Math.max(model.contextWindow ?? 128_000, workingWindow ?? 0), workingWindow);
+ const inheritsNativeWindow = provider.inheritModels === true && model.api === undefined
+ && model.catalogAddition !== true && model.contextWindow === undefined;
+ const learnedWindow = !opts.remote && inheritsNativeWindow
+ ? this.inheritedNativeContextWindows.get(
+ this.inheritedNativeContextWindowKey(provider.id, model.wireId ?? model.id),
+ )
+ : undefined;
+ // An inherited entry without a Cindy-owned window keeps Pi's native
+ // compaction budget until Pi reports its real catalog value. Cache that
+ // value across the required local process rebuild; never synthesize a
+ // 128K override for a model whose bundled window is still unknown.
+ if (inheritsNativeWindow && !(workingWindow && workingWindow > 0) && !learnedWindow) continue;
+ // Preview treats a Cindy working-window override as the effective
+ // inherited window. Keep startup on the same precedence: a previously
+ // learned larger native capacity must not inflate reserveTokens and
+ // cause every subsequent preview to request another rebuild.
+ const budgetWindow = inheritsNativeWindow && workingWindow && workingWindow > 0
+ ? workingWindow
+ : Math.max(model.contextWindow ?? learnedWindow ?? 128_000, workingWindow ?? 0);
+ addBudget(provider.id, model.wireId ?? model.id,
+ budgetWindow, workingWindow);
}
}
const settingsJsonContent = await this.buildSettingsJsonPreservingUserKeys(
@@ -2777,8 +2803,14 @@ export class PiAgent extends BaseAgent {
const startupWorkingContextWindow = this.deps.resolveModelContextLimit?.(authProviderId, opts.model) ?? undefined;
const nativeModel = nativeProviders.find((provider) => provider.id === initialProvider)?.models
.find((model) => (model.wireId ?? model.id) === initialWireModel);
+ const learnedNativeContextWindow = !remote && nativeModel?.contextWindow === undefined
+ ? this.inheritedNativeContextWindows.get(
+ this.inheritedNativeContextWindowKey(initialProvider, initialWireModel),
+ )
+ : undefined;
const startupContextWindow = Math.max(
- nativeModel?.contextWindow ?? selectedRuntimeModel?.contextWindow ?? publicRuntimeModel?.contextWindow ?? 128_000,
+ nativeModel?.contextWindow ?? learnedNativeContextWindow
+ ?? selectedRuntimeModel?.contextWindow ?? publicRuntimeModel?.contextWindow ?? 128_000,
startupWorkingContextWindow ?? 0,
);
@@ -6535,6 +6567,12 @@ export class PiAgent extends BaseAgent {
? entries?.find((entry) => entry.provider === provider && entry.id === wireId)?.contextWindow : undefined;
if (typeof actualWindow === 'number' && Number.isSafeInteger(actualWindow) && actualWindow > 0) {
configuredWindow = actualWindow;
+ if (!remote && provider && wireId) {
+ this.inheritedNativeContextWindows.set(
+ this.inheritedNativeContextWindowKey(provider, wireId),
+ actualWindow,
+ );
+ }
}
} catch { /* No mutation: leave window resolution to the existing recovery path. */ }
if (!configuredWindow) {
@@ -6984,7 +7022,8 @@ export class PiAgent extends BaseAgent {
}
const verifiedWindow = verifiedModel.contextWindow;
const verifiedReserve = reserveForWindow(verifiedWindow, effectiveProviderId, model);
- if (!liveRuntimeSettings || resolvePiNativeReserve(liveRuntimeSettings, provider, wireModel) !== verifiedReserve) {
+ if (!liveRuntimeSettings ||
+ resolvePiNativeReserve(liveRuntimeSettings, provider, wireModel) !== verifiedReserve) {
throw new Error('Pi selected a model whose native compaction settings require a fresh runtime');
}
await this.writePiRuntimeSettings(configHome, {