Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): name the field that forces a runtime rebuild

muzimu2170d51d4bf0199
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index b5303a758..0541a93a3 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -1291,6 +1291,29 @@ function runtimeMatches(
});
}
+function runtimeFirstMismatch(
+ runtime: DesktopAgentRuntime,
+ overrides: Partial<RuntimeMatchConfig> = {},
+): string | null {
+ return runtime.firstMismatch({
+ mode: runtime.getMode(),
+ provider: (runtime as any).provider,
+ thinkingLevel: (runtime as any).thinkingLevel,
+ pluginTools: (runtime as any).pluginTools,
+ pluginSkills: (runtime as any).pluginSkills,
+ projectInstructions: (runtime as any).baseProjectInstructions,
+ customSystemPrompt: (runtime as any).customSystemPrompt,
+ projectMemory: (runtime as any).projectMemory,
+ projectPath: (runtime as any).projectPath,
+ workspaceRoot: (runtime as any).workspaceRoot,
+ commandShell: (runtime as any).commandShell,
+ subagents: (runtime as any).subagents,
+ subagentProviders: (runtime as any).subagentProviders,
+ subagentModelKeys: [...(runtime as any).subagentModelKeys],
+ ...overrides,
+ });
+}
+
describe("system transcript reconstruction", () => {
it("keeps provider usage valid when rebuilding an unchanged prefix", async () => {
const runtime = createRuntime();
@@ -1474,6 +1497,45 @@ describe("custom system prompt files (issue #542)", () => {
});
});
+describe("DesktopAgentRuntime configuration matching", () => {
+ // #1948: a bool leaves "why did my cache reset" unanswerable. The field name
+ // is the diagnostic.
+ it("firstMismatch names the field that changed", async () => {
+ const runtime = createRuntime({ projectMemory: "Use the staging database." });
+ expect(runtimeFirstMismatch(runtime)).toBeNull();
+ expect(
+ runtimeFirstMismatch(runtime, { projectMemory: "Use the production database." }),
+ ).toBe("projectMemory");
+ expect(runtimeFirstMismatch(runtime, { mode: "plan" })).toBe("mode");
+ await runtime.dispose();
+ });
+
+ it("firstMismatch names the provider field, including a refreshed key", async () => {
+ const runtime = createRuntime();
+ const refreshedProvider = {
+ ...(runtime as any).provider,
+ apiKey: "sk-refreshed",
+ };
+ expect(runtimeFirstMismatch(runtime, { provider: refreshedProvider })).toBe(
+ "provider.apiKey",
+ );
+ const otherModel = {
+ ...(runtime as any).provider,
+ modelId: "other-model",
+ };
+ expect(runtimeFirstMismatch(runtime, { provider: otherModel })).toBe(
+ "provider.modelId",
+ );
+ await runtime.dispose();
+ });
+
+ it("a disposed runtime reports itself instead of a field", async () => {
+ const runtime = createRuntime();
+ await runtime.dispose();
+ expect(runtimeFirstMismatch(runtime)).toBe("disposed");
+ });
+});
+
describe("DesktopAgentRuntime configuration matching", () => {
it("retires an idle runtime when project memory changes", async () => {
const runtime = createRuntime({ projectMemory: "Use the staging database." });
diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.ts
index d31d1199d..6c57f354d 100644
--- a/packages/agent-runtime/src/runtime.ts
+++ b/packages/agent-runtime/src/runtime.ts
@@ -2686,8 +2686,13 @@ Do not invent objections or turn speculative risks into blockers. Stop when the
this.emit({ type: "planning_state", state, ...details });
}
- /** True when this runtime can be reused for a prompt with the given config. */
- matches(config: RuntimeMatchConfig): boolean {
+ /**
+ * First reason this runtime cannot be reused for the given config, or null
+ * when it matches. Field names are diagnostics, not API: they name the
+ * comparison that failed so a rebuild can be attributed (#1948) instead of
+ * leaving "why did my cache reset" unanswerable.
+ */
+ firstMismatch(config: RuntimeMatchConfig): string | null {
const requestedPluginTools = config.pluginTools ?? [];
const current = safeJson([...this.pluginTools].sort((a, b) => a.name.localeCompare(b.name)));
const next = safeJson([...requestedPluginTools].sort((a, b) => a.name.localeCompare(b.name)));
@@ -2705,49 +2710,59 @@ Do not invent objections or turn speculative risks into blockers. Stop when the
Boolean(this.provider.extensionAgentKey) &&
this.provider.extensionAgentKey === config.provider.extensionAgentKey &&
this.provider.modelId === config.provider.modelId;
- const providerMatches = extensionAgentMatches || (
- this.provider.id === config.provider.id &&
- this.provider.modelId === config.provider.modelId &&
- (this.provider.baseUrl ?? "") === (config.provider.baseUrl ?? "") &&
- this.provider.apiKey === config.provider.apiKey &&
- this.provider.authKind === config.provider.authKind &&
- (this.provider.apiStyle ?? "") === (config.provider.apiStyle ?? "") &&
- providerHeadersEqual(this.provider.headers, config.provider.headers) &&
- this.provider.supportsReasoning === config.provider.supportsReasoning &&
- currentThinkingLevels === nextThinkingLevels &&
- safeJson(this.provider.modelConfig ?? null) === safeJson(config.provider.modelConfig ?? null)
- );
- return (
- !this.disposed &&
- providerMatches &&
- this.mode === config.mode &&
- this.jevApiKey === config.jevApiKey &&
- this.thinkingLevel ===
- clampThinkingLevel(config.provider, config.thinkingLevel) &&
- current === next &&
- safeJson(this.commandShell) === safeJson(config.commandShell) &&
- safeJson(this.baseProjectInstructions ?? null) ===
- safeJson(config.projectInstructions ?? null) &&
+ // An extension agent is identified by its key and model alone; the other
+ // provider fields belong to the plugin that declared it.
+ if (!extensionAgentMatches) {
+ const providerChecks: Array<[string, boolean]> = [
+ ["provider.id", this.provider.id === config.provider.id],
+ ["provider.modelId", this.provider.modelId === config.provider.modelId],
+ ["provider.baseUrl", (this.provider.baseUrl ?? "") === (config.provider.baseUrl ?? "")],
+ ["provider.apiKey", this.provider.apiKey === config.provider.apiKey],
+ ["provider.authKind", this.provider.authKind === config.provider.authKind],
+ ["provider.apiStyle", (this.provider.apiStyle ?? "") === (config.provider.apiStyle ?? "")],
+ ["provider.headers", providerHeadersEqual(this.provider.headers, config.provider.headers)],
+ ["provider.supportsReasoning", this.provider.supportsReasoning === config.provider.supportsReasoning],
+ ["provider.supportedThinkingLevels", currentThinkingLevels === nextThinkingLevels],
+ ["provider.modelConfig", safeJson(this.provider.modelConfig ?? null) === safeJson(config.provider.modelConfig ?? null)],
+ ];
+ for (const [field, ok] of providerChecks) {
+ if (!ok) return field;
+ }
+ }
+ const checks: Array<[string, boolean]> = [
+ ["disposed", !this.disposed],
+ ["mode", this.mode === config.mode],
+ ["jevApiKey", this.jevApiKey === config.jevApiKey],
+ ["thinkingLevel", this.thinkingLevel === clampThinkingLevel(config.provider, config.thinkingLevel)],
+ ["pluginTools", current === next],
+ ["commandShell", safeJson(this.commandShell) === safeJson(config.commandShell)],
+ ["projectInstructions", safeJson(this.baseProjectInstructions ?? null) === safeJson(config.projectInstructions ?? null)],
// Editing SYSTEM.md / APPEND_SYSTEM.md retires the runtime so the next
// prompt recomposes from the fresh content.
- safeJson(this.customSystemPrompt ?? null) ===
- safeJson(config.customSystemPrompt ?? null) &&
- (this.projectMemory ?? "") === (config.projectMemory?.trim() ?? "") &&
- (this.projectPath ?? "") === (config.remoteWorkspace?.path ?? config.projectPath?.trim() ?? "") &&
- safeJson(this.remoteWorkspace ?? null) === safeJson(config.remoteWorkspace ?? null) &&
- (this.workspaceRoot ?? "") === (config.workspaceRoot?.trim() ?? "") &&
+ ["customSystemPrompt", safeJson(this.customSystemPrompt ?? null) === safeJson(config.customSystemPrompt ?? null)],
+ ["projectMemory", (this.projectMemory ?? "") === (config.projectMemory?.trim() ?? "")],
+ ["projectPath", (this.projectPath ?? "") === (config.remoteWorkspace?.path ?? config.projectPath?.trim() ?? "")],
+ ["remoteWorkspace", safeJson(this.remoteWorkspace ?? null) === safeJson(config.remoteWorkspace ?? null)],
+ ["workspaceRoot", (this.workspaceRoot ?? "") === (config.workspaceRoot?.trim() ?? "")],
// Editing `~/.agents/subagents/*.md` must reach the next prompt. Definition
// bodies are part of the `Task` tool's behavior, so unlike skills they
// are compared in full.
- safeJson(this.subagents) === safeJson(config.subagents ?? []) &&
- safeJson(this.subagentProviders) === safeJson(config.subagentProviders ?? {}) &&
- safeJson([...this.subagentModelKeys].sort()) ===
- safeJson([...new Set(config.subagentModelKeys ?? [])].sort()) &&
+ ["subagents", safeJson(this.subagents) === safeJson(config.subagents ?? [])],
+ ["subagentProviders", safeJson(this.subagentProviders) === safeJson(config.subagentProviders ?? {})],
+ ["subagentModelKeys", safeJson([...this.subagentModelKeys].sort()) === safeJson([...new Set(config.subagentModelKeys ?? [])].sort())],
// Enabling or disabling a trusted extension retires the runtime so the
// next prompt reloads the set (spec 16 §4.3).
- trustedExtensionIds(this.trustedExtensionSpecs) ===
- trustedExtensionIds(config.trustedExtensions ?? [])
- );
+ ["trustedExtensions", trustedExtensionIds(this.trustedExtensionSpecs) === trustedExtensionIds(config.trustedExtensions ?? [])],
+ ];
+ for (const [field, ok] of checks) {
+ if (!ok) return field;
+ }
+ return null;
+ }
+
+ /** True when this runtime can be reused for a prompt with the given config. */
+ matches(config: RuntimeMatchConfig): boolean {
+ return this.firstMismatch(config) === null;
}
/**
diff --git a/packages/agent-runtime/src/sidecar.ts b/packages/agent-runtime/src/sidecar.ts
index 0af3765c7..28eac1f98 100644
--- a/packages/agent-runtime/src/sidecar.ts
+++ b/packages/agent-runtime/src/sidecar.ts
@@ -233,7 +233,7 @@ async function runtimeFor(
errorCode: "AGENT_BUSY",
});
}
- const reusable = existing?.matches({
+ const config = {
mode,
provider,
thinkingLevel,
@@ -251,10 +251,15 @@ async function runtimeFor(
remoteWorkspace: params.remoteWorkspace,
workspaceRoot: params.workspaceRoot,
commandShell: params.commandShell,
- })
- ? existing
- : undefined;
+ };
+ // Name the first field that disagrees so a cache-invalidating rebuild can be
+ // attributed to its cause (#1948) instead of surfacing only as a reset.
+ const mismatch = existing?.firstMismatch(config) ?? null;
+ const reusable = existing && mismatch === null ? existing : undefined;
if (existing && !reusable) {
+ console.error(
+ `[runtime] rebuilding ${sessionId}: runtime config mismatch (${mismatch ?? "unknown"})`,
+ );
await existing.dispose();
runtimes.delete(sessionId);
}