Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix: one owner for the Prime team header, retry failed shell-command credentials (#2497)

Sebastian Müller64a4b84a8b59
diff --git a/packages/ai/.changes/prime-team-header-single-owner.md b/packages/ai/.changes/prime-team-header-single-owner.md
new file mode 100644
index 0000000000..e2a852b3c0
--- /dev/null
+++ b/packages/ai/.changes/prime-team-header-single-owner.md
@@ -0,0 +1 @@
+- Removed the provider-side Prime Inference team lookup: `X-Prime-Team-ID` now comes only from the caller, so a request with no team header no longer picks up `PRIME_TEAM_ID` or `team_id` from the Prime CLI profile (`~/.prime/config.json`).
diff --git a/packages/ai/src/env-api-keys.ts b/packages/ai/src/env-api-keys.ts
index eaeae11a84..19b2f843e5 100644
--- a/packages/ai/src/env-api-keys.ts
+++ b/packages/ai/src/env-api-keys.ts
@@ -5,7 +5,6 @@ import type { KnownProvider } from "./types.js";
// NEVER convert to top-level runtime imports - breaks browser/Vite builds
let _existsSync: typeof existsSync | null = null;
-let _readFileSync: typeof readFileSync | null = null;
let _homedir: typeof homedir | null = null;
let _join: typeof join | null = null;
@@ -19,7 +18,6 @@ const NODE_PATH_SPECIFIER = "node:" + "path";
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
dynamicImport(NODE_FS_SPECIFIER).then((m) => {
_existsSync = (m as { existsSync: typeof existsSync }).existsSync;
- _readFileSync = (m as { readFileSync: typeof readFileSync }).readFileSync;
});
dynamicImport(NODE_OS_SPECIFIER).then((m) => {
_homedir = (m as { homedir: typeof homedir }).homedir;
@@ -200,22 +198,3 @@ export function getEnvApiKey(provider: string): string | undefined {
return undefined;
}
-
-export function getPrimeTeamId(): string | undefined {
- const fromEnv = process.env.PRIME_TEAM_ID || getProcEnv("PRIME_TEAM_ID");
- if (fromEnv?.trim()) return fromEnv.trim();
-
- if (!_existsSync || !_readFileSync || !_homedir || !_join) return undefined;
- const configPath = _join(_homedir(), ".prime", "config.json");
- if (!_existsSync(configPath)) return undefined;
- try {
- const parsed = JSON.parse(_readFileSync(configPath, "utf-8")) as unknown;
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
- const teamId = (parsed as Record<string, unknown>).team_id;
- if (typeof teamId === "string" && teamId.trim()) return teamId.trim();
- }
- } catch {
- // Treat unreadable or malformed config as no configured team.
- }
- return undefined;
-}
diff --git a/packages/ai/src/providers/openai-completions.ts b/packages/ai/src/providers/openai-completions.ts
index 78496a7593..f38ce477ff 100644
--- a/packages/ai/src/providers/openai-completions.ts
+++ b/packages/ai/src/providers/openai-completions.ts
@@ -11,7 +11,7 @@ import type {
ChatCompletionToolMessageParam,
} from "openai/resources/chat/completions.js";
import { getAnthropicCacheWriteCost, hasStandardAnthropicCachePricing } from "../cache-pricing.js";
-import { getEnvApiKey, getPrimeTeamId } from "../env-api-keys.js";
+import { getEnvApiKey } from "../env-api-keys.js";
import { calculateCost, clampThinkingLevel } from "../models.js";
import type {
AssistantMessage,
@@ -547,11 +547,6 @@ function createClient(
Object.assign(headers, copilotHeaders);
}
- if (model.provider === "prime-inference") {
- const teamId = getPrimeTeamId();
- if (teamId) headers["X-Prime-Team-ID"] = teamId;
- }
-
if (cacheSessionId && compat.sendSessionAffinityHeaders) {
headers.session_id = cacheSessionId;
headers["x-client-request-id"] = cacheSessionId;
diff --git a/packages/ai/test/openai-completions-tool-choice.test.ts b/packages/ai/test/openai-completions-tool-choice.test.ts
index 17b0508e83..a4521332af 100644
--- a/packages/ai/test/openai-completions-tool-choice.test.ts
+++ b/packages/ai/test/openai-completions-tool-choice.test.ts
@@ -1266,6 +1266,7 @@ describe("openai-completions tools payload", () => {
afterEach(() => {
delete process.env.CLOUDFLARE_ACCOUNT_ID;
delete process.env.CLOUDFLARE_GATEWAY_ID;
+ delete process.env.PRIME_TEAM_ID;
});
it.each([
@@ -1370,6 +1371,12 @@ describe("openai-completions tools payload", () => {
expect(clientOptions.defaultHeaders?.Authorization).toBe("Bearer upstream-token");
expect(clientOptions.defaultHeaders?.["cf-aig-authorization"]).toBe("Bearer cf-token");
});
+ it("adds no Prime Inference team header the caller did not pass", async () => {
+ process.env.PRIME_TEAM_ID = "cli-profile-team";
+ const context = { messages: [{ role: "user" as const, content: "hi", timestamp: 1 }] };
+ await streamSimple(getModel("prime-inference", "openai/gpt-5")!, context, { apiKey: "k" }).result();
+ expect(mockState.lastClientOptions).not.toHaveProperty(["defaultHeaders", "X-Prime-Team-ID"]);
+ });
});
function openRouterAuto(): Model<"openai-completions"> {
diff --git a/packages/coding-agent/.changes/prime-team-header-single-owner.md b/packages/coding-agent/.changes/prime-team-header-single-owner.md
new file mode 100644
index 0000000000..862876211c
--- /dev/null
+++ b/packages/coding-agent/.changes/prime-team-header-single-owner.md
@@ -0,0 +1,2 @@
+- Fixed Prime Inference team headers having two owners: the provider layer no longer reads `team_id` from the Prime CLI profile (`~/.prime/config.json`), so a runtime or `PRIME_API_KEY` credential that deliberately has no saved Agent team stays team-less instead of inheriting an unrelated CLI team. The agent's auth storage stays the single source for `X-Prime-Team-ID` (saved team or `PRIME_TEAM_ID`).
+- Fixed shell-command credentials (`!command` API keys and header values) caching failures forever: a command that resolved to nothing is now retried on the next lookup, so a locked keychain or a transient failure no longer disables the credential for the lifetime of the process.
diff --git a/packages/coding-agent/src/core/resolve-config-value.ts b/packages/coding-agent/src/core/resolve-config-value.ts
index 13997795b5..1336abc83d 100644
--- a/packages/coding-agent/src/core/resolve-config-value.ts
+++ b/packages/coding-agent/src/core/resolve-config-value.ts
@@ -6,11 +6,11 @@
import { execSyncHidden, spawnSyncHidden } from "../utils/child-process.js";
import { getShellConfig } from "../utils/shell.js";
-const commandResultCache = new Map<string, string | undefined>();
+const commandResultCache = new Map<string, string>();
/**
* Resolve a config value (API key, header value, etc.) to an actual value.
- * - If starts with "!", executes the rest as a shell command and uses stdout (cached)
+ * - If starts with "!", executes the rest as a shell command and uses stdout (successful results are cached)
* - Otherwise checks environment variable first, then treats as literal (not cached)
*/
export function resolveConfigValue(config: string): string | undefined {
@@ -82,12 +82,18 @@ function executeCommandUncached(commandConfig: string): string | undefined {
}
function executeCommand(commandConfig: string): string | undefined {
- if (commandResultCache.has(commandConfig)) {
- return commandResultCache.get(commandConfig);
+ const cached = commandResultCache.get(commandConfig);
+ if (cached !== undefined) {
+ return cached;
}
+ // A command that produced no value is not a resolution: a locked keychain, a
+ // missing network, or a rotated secret must be retried on the next lookup
+ // instead of pinning the failure for the lifetime of the process.
const result = executeCommandUncached(commandConfig);
- commandResultCache.set(commandConfig, result);
+ if (result !== undefined) {
+ commandResultCache.set(commandConfig, result);
+ }
return result;
}
diff --git a/packages/coding-agent/test/auth-storage.test.ts b/packages/coding-agent/test/auth-storage.test.ts
index 10e4705121..193f309b26 100644
--- a/packages/coding-agent/test/auth-storage.test.ts
+++ b/packages/coding-agent/test/auth-storage.test.ts
@@ -415,9 +415,9 @@ describe("AuthStorage", () => {
describe("caching", () => {
test.each([
- { name: "successful command runs once per process and across instances", fails: false },
- { name: "failed command is cached, never retried", fails: true },
- ])("$name", async ({ fails }) => {
+ { name: "successful command runs once per process and across instances", fails: false, runs: 1 },
+ { name: "failed command is retried on every lookup", fails: true, runs: 3 },
+ ])("$name", async ({ fails, runs }) => {
const counterFile = join(tempDir, "counter");
writeFileSync(counterFile, "0");
const counterPath = toShPath(counterFile);
@@ -434,12 +434,12 @@ describe("AuthStorage", () => {
const expected = fails ? undefined : "key-value";
await expect(authStorage.getApiKey("anthropic")).resolves.toBe(expected);
await expect(authStorage.getApiKey("anthropic")).resolves.toBe(expected);
- // A second instance shares the process-wide command cache.
+ // A second instance shares the process-wide cache of successful commands.
await expect(AuthStorage.create(authJsonPath).getApiKey("anthropic")).resolves.toBe(expected);
// Distinct commands are cached under distinct keys.
await expect(authStorage.getApiKey("openai")).resolves.toBe("key-openai");
- expect(parseInt(readFileSync(counterFile, "utf-8").trim(), 10)).toBe(1);
+ expect(parseInt(readFileSync(counterFile, "utf-8").trim(), 10)).toBe(runs);
});
test("environment variables are not cached (changes are picked up)", async () => {