Skip to content

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

Rust

restore prime api key precedence (#385)

kt9a96ce20b12b
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index c41d04d2d7..8a1dd7f493 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -2,6 +2,8 @@
## [Unreleased]
+- Fixed Prime Inference credential and team-header precedence to prefer `PRIME_API_KEY`, then the Prime CLI config, then `auth.json`.
+
## [0.2.9] - 2026-07-13
- Changed tool call groups to use one blank row above and below without blank rows between consecutive calls.
diff --git a/packages/coding-agent/src/core/auth-storage.ts b/packages/coding-agent/src/core/auth-storage.ts
index a60d61bcff..493c9c964d 100644
--- a/packages/coding-agent/src/core/auth-storage.ts
+++ b/packages/coding-agent/src/core/auth-storage.ts
@@ -481,13 +481,23 @@ export class AuthStorage {
}
private getAuthSourceCandidates(provider: string, options?: { includeFallback?: boolean }): AuthSourceCandidate[] {
- const candidates = [
- this.getRuntimeAuthCandidate(provider),
- provider === PRIME_INFERENCE_PROVIDER_ID ? this.getPrimeCliAuthCandidate(provider) : undefined,
- this.getStoredAuthCandidate(provider),
- this.getEnvironmentAuthCandidate(provider),
- options?.includeFallback === false ? undefined : this.getFallbackAuthCandidate(provider),
- ];
+ const fallbackCandidate =
+ options?.includeFallback === false ? undefined : this.getFallbackAuthCandidate(provider);
+ const candidates =
+ provider === PRIME_INFERENCE_PROVIDER_ID
+ ? [
+ this.getRuntimeAuthCandidate(provider),
+ this.getEnvironmentAuthCandidate(provider),
+ this.getPrimeCliAuthCandidate(provider),
+ this.getStoredAuthCandidate(provider),
+ fallbackCandidate,
+ ]
+ : [
+ this.getRuntimeAuthCandidate(provider),
+ this.getStoredAuthCandidate(provider),
+ this.getEnvironmentAuthCandidate(provider),
+ fallbackCandidate,
+ ];
return candidates.filter((candidate): candidate is AuthSourceCandidate => candidate !== undefined);
}
@@ -801,10 +811,9 @@ export class AuthStorage {
* Get API key for a provider.
* Priority:
* 1. Runtime override (CLI --api-key)
- * 2. API key from auth.json
- * 3. OAuth token from auth.json (auto-refreshed with locking)
- * 4. Environment variable
- * 5. Fallback resolver (models.json custom providers)
+ * 2. Prime Inference: environment variable, Prime CLI config, auth.json
+ * 3. Other providers: auth.json, environment variable
+ * 4. Fallback resolver (models.json custom providers)
*/
async getApiKeyWithSourceToken(
providerId: string,
@@ -820,6 +829,20 @@ export class AuthStorage {
};
}
+ const envCandidate = this.getEnvironmentAuthCandidate(providerId);
+ const envKey = getEnvApiKey(providerId);
+ if (
+ providerId === PRIME_INFERENCE_PROVIDER_ID &&
+ envKey &&
+ envCandidate &&
+ !this.isAuthSourceStale(providerId, envCandidate)
+ ) {
+ return {
+ apiKey: envKey,
+ sourceToken: this.getAuthSourceTokenForCandidate(providerId, envCandidate),
+ };
+ }
+
if (providerId === PRIME_INFERENCE_PROVIDER_ID) {
const primeCliCandidate = this.getPrimeCliAuthCandidate(providerId);
const primeCliKey = this.getPrimeCliApiKey(providerId);
@@ -911,10 +934,13 @@ export class AuthStorage {
}
}
- // Fall back to environment variable
- const envCandidate = this.getEnvironmentAuthCandidate(providerId);
- const envKey = getEnvApiKey(providerId);
- if (envKey && envCandidate && !this.isAuthSourceStale(providerId, envCandidate)) {
+ // Other providers preserve auth.json priority over environment variables.
+ if (
+ providerId !== PRIME_INFERENCE_PROVIDER_ID &&
+ envKey &&
+ envCandidate &&
+ !this.isAuthSourceStale(providerId, envCandidate)
+ ) {
return {
apiKey: envKey,
sourceToken: this.getAuthSourceTokenForCandidate(providerId, envCandidate),
@@ -1007,30 +1033,33 @@ export class AuthStorage {
if (config?.teamIdFromEnv) {
return undefined;
}
- const credential = this.data[PRIME_INFERENCE_PROVIDER_ID];
- if (config?.apiKey) {
- if (credential?.type === "api_key" && credential.primeTeam === null) {
- return null;
- }
- if (config.teamId) {
- return this.toPrimeTeamCredential({
- teamId: config.teamId,
- name: config.teamName ?? "Prime CLI team",
- ...(config.teamRole ? { role: config.teamRole } : {}),
- });
- }
- if (credential?.type === "api_key" && credential.primeTeam) {
- return credential.primeTeam;
- }
- return null;
- }
}
const credential = this.data[PRIME_INFERENCE_PROVIDER_ID];
+ const authSource = this.getAuthStatus(PRIME_INFERENCE_PROVIDER_ID).source;
+ if (authSource === "runtime" || authSource === "environment") {
+ return undefined;
+ }
+ if (authSource === "prime_cli") {
+ if (credential?.type === "api_key" && credential.primeTeam === null) {
+ return null;
+ }
+ if (config?.teamId) {
+ return this.toPrimeTeamCredential({
+ teamId: config.teamId,
+ name: config.teamName ?? "Prime CLI team",
+ ...(config.teamRole ? { role: config.teamRole } : {}),
+ });
+ }
+ if (credential?.type === "api_key" && credential.primeTeam) {
+ return credential.primeTeam;
+ }
+ return null;
+ }
if (credential?.type === "api_key" && credential.primeTeam !== undefined) {
return credential.primeTeam;
}
- if (config?.teamId) {
+ if (!config?.apiKey && config?.teamId) {
return this.toPrimeTeamCredential({
teamId: config.teamId,
name: config.teamName ?? "Prime CLI team",
@@ -1050,30 +1079,7 @@ export class AuthStorage {
return primeCliConfig.teamId ? { "X-Prime-Team-ID": primeCliConfig.teamId } : undefined;
}
- const credential = this.data[providerId];
- if (primeCliConfig?.apiKey) {
- if (credential?.type === "api_key" && credential.primeTeam === null) {
- return undefined;
- }
- if (primeCliConfig.teamId) {
- return { "X-Prime-Team-ID": primeCliConfig.teamId };
- }
- return credential?.type === "api_key" && credential.primeTeam?.teamId
- ? { "X-Prime-Team-ID": credential.primeTeam.teamId }
- : undefined;
- }
-
- if (credential?.type === "api_key") {
- if (credential.primeTeam === null) {
- return undefined;
- }
- const selectedTeamId = credential.primeTeam?.teamId;
- if (selectedTeamId) {
- return { "X-Prime-Team-ID": selectedTeamId };
- }
- }
-
- const teamId = primeCliConfig?.teamId;
+ const teamId = this.getPrimeInferenceTeamSelection()?.teamId;
return teamId ? { "X-Prime-Team-ID": teamId } : undefined;
}
diff --git a/packages/coding-agent/test/auth-storage.test.ts b/packages/coding-agent/test/auth-storage.test.ts
index d8d2b10eec..c2f368a833 100644
--- a/packages/coding-agent/test/auth-storage.test.ts
+++ b/packages/coding-agent/test/auth-storage.test.ts
@@ -334,7 +334,7 @@ describe("AuthStorage", () => {
expect(authStorage.getAuthStatus("anthropic")).toEqual({ configured: true, source: "stored" });
});
- test("prime inference uses Prime CLI auth over legacy Prime Agent auth", async () => {
+ test("prime inference uses Prime CLI auth over stored auth", async () => {
const primeConfigPath = join(tempDir, "prime-config.json");
writeFileSync(primeConfigPath, JSON.stringify({ api_key: "prime-cli-key" }));
writeAuthJson({
@@ -357,31 +357,49 @@ describe("AuthStorage", () => {
});
});
- test("prime inference uses Prime CLI auth over environment auth", async () => {
+ test("prime inference uses environment auth over Prime CLI and stored auth", async () => {
const originalPrimeApiKey = process.env.PRIME_API_KEY;
+ const originalPrimeTeamId = process.env.PRIME_TEAM_ID;
process.env.PRIME_API_KEY = "env-prime-key";
+ delete process.env.PRIME_TEAM_ID;
try {
const primeConfigPath = join(tempDir, "prime-config.json");
- writeFileSync(primeConfigPath, JSON.stringify({ api_key: "prime-cli-key" }));
- writeAuthJson({});
+ writeFileSync(
+ primeConfigPath,
+ JSON.stringify({ api_key: "prime-cli-key", team_id: "cli-team", team_name: "CLI Research" }),
+ );
+ writeAuthJson({
+ "prime-inference": {
+ type: "api_key",
+ key: "agent-key",
+ primeTeam: { teamId: "stored-team", name: "Stored Research" },
+ },
+ });
authStorage = AuthStorage.create(authJsonPath, {
primeCliConfigPath: primeConfigPath,
usePrimeCliConfig: true,
});
- await expect(authStorage.getApiKey("prime-inference")).resolves.toBe("prime-cli-key");
+ await expect(authStorage.getApiKey("prime-inference")).resolves.toBe("env-prime-key");
expect(authStorage.getAuthStatus("prime-inference")).toEqual({
configured: false,
- source: "prime_cli",
- label: "Prime CLI",
+ source: "environment",
+ label: "PRIME_API_KEY",
});
+ expect(authStorage.getProviderHeaders("prime-inference")).toBeUndefined();
+ expect(authStorage.getPrimeInferenceTeamSelection()).toBeUndefined();
} finally {
if (originalPrimeApiKey === undefined) {
delete process.env.PRIME_API_KEY;
} else {
process.env.PRIME_API_KEY = originalPrimeApiKey;
}
+ if (originalPrimeTeamId === undefined) {
+ delete process.env.PRIME_TEAM_ID;
+ } else {
+ process.env.PRIME_TEAM_ID = originalPrimeTeamId;
+ }
}
});