Skip to content

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

Rust

fix(coding-agent): stop blocking startup on private Prime Inference model authorization (#560)

Florian Brandff669cd79990
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index b9040d929f..d7a5a59a86 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -8,6 +8,7 @@
- Changed session search to rank exact name and first-message matches before prefix, substring, and stricter transcript fuzzy matches.
- Fixed daemon startup failing when an interrupted supervisor left an empty ownership directory.
- Fixed `/effort xhigh` and `/effort max` being rejected as unknown levels when no model was active yet.
+- Fixed startup blocking on a private Prime Inference model authorization request; the authorization is now cached next to `models.json` and stale entries refresh in the background.
## [0.3.3] - 2026-07-23
diff --git a/packages/coding-agent/src/core/model-registry.ts b/packages/coding-agent/src/core/model-registry.ts
index 4f88a6e346..8644ff5ad2 100644
--- a/packages/coding-agent/src/core/model-registry.ts
+++ b/packages/coding-agent/src/core/model-registry.ts
@@ -22,8 +22,8 @@ import {
} from "@earendil-works/pi-ai";
import { registerBuiltinMcpOAuthProviders } from "@earendil-works/pi-ai/mcp";
import { registerOAuthProvider, resetOAuthProviders } from "@earendil-works/pi-ai/oauth";
-import { existsSync, readFileSync } from "fs";
-import { join } from "path";
+import { existsSync, readFileSync, renameSync, writeFileSync } from "fs";
+import { dirname, join } from "path";
import { type Static, type TProperties, Type } from "typebox";
import type { Validator } from "typebox/compile";
import type { TLocalizedValidationError } from "typebox/error";
@@ -401,6 +401,26 @@ function readOpenAICodexModelIds(value: unknown): Set<string> {
);
}
+const PRIVATE_PRIME_AUTHORIZATION_CACHE_FILE = "prime-inference-private-models.json";
+const PRIVATE_PRIME_AUTHORIZATION_CACHE_TTL_MS = 5 * 60_000;
+const PRIVATE_PRIME_BACKGROUND_REFRESH_TIMEOUT_MS = 3_000;
+
+interface PrivatePrimeAuthorizationCache {
+ fingerprint: string;
+ modelIds: Set<string>;
+ refreshedAt: number;
+}
+
+function privatePrimeAuthorizationFingerprint(apiKey: string, teamId: string): string {
+ return createHash("sha256").update(apiKey).update("\0").update(teamId).digest("hex");
+}
+
+function isOfflineModeEnabled(): boolean {
+ const value = process.env.PI_OFFLINE;
+ if (!value) return false;
+ return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
+}
+
/**
* Model registry - loads and manages models, resolves API keys via AuthStorage.
*/
@@ -415,6 +435,7 @@ export class ModelRegistry {
private authorizedPrivatePrimeInferenceTeamId: string | undefined;
private explicitPrivatePrimeInferenceModelIds = new Set<string>();
private openAICodexModelsCache: { authFingerprint: string; modelIds: Set<string>; refreshedAt: number } | undefined;
+ private backgroundPrivatePrimeAuthorization: { fingerprint: string; promise: Promise<void> } | undefined;
private loadError: string | undefined = undefined;
/** Re-register dynamic OAuth providers (e.g. user MCP servers) after refresh() resets the registry. */
@@ -778,20 +799,143 @@ export class ModelRegistry {
return;
}
- try {
- this.authorizedPrivatePrimeInferenceModelIds = await fetchAuthorizedPrivatePrimeInferenceModelIds(
- apiKey,
- teamHeaders,
- );
+ const fingerprint = privatePrimeAuthorizationFingerprint(apiKey, teamId);
+ const cached = this.readPrivatePrimeAuthorizationCache();
+ if (cached?.fingerprint === fingerprint) {
+ // Serve the persisted authorization decision so startup and model lists
+ // don't block on the network. A stale cache refreshes in the background
+ // and the updated ids apply to subsequent lookups in this process.
+ this.authorizedPrivatePrimeInferenceModelIds = new Set(cached.modelIds);
this.authorizedPrivatePrimeInferenceTeamId = teamId;
+ const cacheIsFresh = Date.now() - cached.refreshedAt < PRIVATE_PRIME_AUTHORIZATION_CACHE_TTL_MS;
+ if (cacheIsFresh || isOfflineModeEnabled()) {
+ return;
+ }
+ this.startBackgroundPrivatePrimeAuthorizationRefresh(apiKey, teamHeaders, teamId, fingerprint);
+ return;
+ }
+ if (isOfflineModeEnabled()) {
+ this.authorizedPrivatePrimeInferenceModelIds.clear();
+ this.authorizedPrivatePrimeInferenceTeamId = undefined;
+ return;
+ }
+
+ let authorizedIds: Set<string> | undefined;
+ try {
+ authorizedIds = await fetchAuthorizedPrivatePrimeInferenceModelIds(apiKey, teamHeaders);
} catch {
- if (teamId === previousTeamId) {
- this.authorizedPrivatePrimeInferenceModelIds = previousPrivateModelIds;
+ // Fall back to the previous authorization below.
+ }
+ // Leave newer state untouched if the credentials changed while fetching.
+ if ((await this.currentPrivatePrimeAuthorizationFingerprint()) !== fingerprint) {
+ return;
+ }
+ if (authorizedIds) {
+ this.authorizedPrivatePrimeInferenceModelIds = authorizedIds;
+ this.authorizedPrivatePrimeInferenceTeamId = teamId;
+ this.writePrivatePrimeAuthorizationCache({ fingerprint, modelIds: authorizedIds, refreshedAt: Date.now() });
+ } else if (teamId === previousTeamId) {
+ this.authorizedPrivatePrimeInferenceModelIds = previousPrivateModelIds;
+ this.authorizedPrivatePrimeInferenceTeamId = teamId;
+ } else {
+ this.authorizedPrivatePrimeInferenceModelIds.clear();
+ this.authorizedPrivatePrimeInferenceTeamId = undefined;
+ }
+ }
+
+ /**
+ * Stale cache hits refresh in the background; failures keep the cached ids.
+ * Refreshes for the same credentials are deduped, a changed-credentials
+ * refresh is queued after the in-flight one, and a result is only applied
+ * if the credentials it was fetched with are still current.
+ */
+ private startBackgroundPrivatePrimeAuthorizationRefresh(
+ apiKey: string,
+ teamHeaders: Record<string, string>,
+ teamId: string,
+ fingerprint: string,
+ ): void {
+ if (this.backgroundPrivatePrimeAuthorization?.fingerprint === fingerprint) {
+ return;
+ }
+ const run = async () => {
+ try {
+ const authorizedIds = await fetchAuthorizedPrivatePrimeInferenceModelIds(
+ apiKey,
+ teamHeaders,
+ undefined,
+ PRIVATE_PRIME_BACKGROUND_REFRESH_TIMEOUT_MS,
+ );
+ if ((await this.currentPrivatePrimeAuthorizationFingerprint()) !== fingerprint) {
+ return;
+ }
+ this.authorizedPrivatePrimeInferenceModelIds = authorizedIds;
this.authorizedPrivatePrimeInferenceTeamId = teamId;
- } else {
- this.authorizedPrivatePrimeInferenceModelIds.clear();
- this.authorizedPrivatePrimeInferenceTeamId = undefined;
+ this.writePrivatePrimeAuthorizationCache({ fingerprint, modelIds: authorizedIds, refreshedAt: Date.now() });
+ } catch {
+ // Keep the cached ids.
}
+ };
+ const pending = this.backgroundPrivatePrimeAuthorization?.promise;
+ const promise = (pending ?? Promise.resolve()).then(run);
+ this.backgroundPrivatePrimeAuthorization = { fingerprint, promise };
+ void promise.finally(() => {
+ if (this.backgroundPrivatePrimeAuthorization?.promise === promise) {
+ this.backgroundPrivatePrimeAuthorization = undefined;
+ }
+ });
+ }
+
+ private async currentPrivatePrimeAuthorizationFingerprint(): Promise<string | undefined> {
+ const apiKey = await this.authStorage.getApiKey(PRIME_INFERENCE_PROVIDER_ID);
+ const teamId = this.authStorage.getProviderHeaders(PRIME_INFERENCE_PROVIDER_ID)?.["X-Prime-Team-ID"];
+ return apiKey && teamId ? privatePrimeAuthorizationFingerprint(apiKey, teamId) : undefined;
+ }
+
+ private privatePrimeAuthorizationCachePath(): string | undefined {
+ if (!this.modelsJsonPath) {
+ return undefined;
+ }
+ return join(dirname(this.modelsJsonPath), PRIVATE_PRIME_AUTHORIZATION_CACHE_FILE);
+ }
+
+ private readPrivatePrimeAuthorizationCache(): PrivatePrimeAuthorizationCache | undefined {
+ const cachePath = this.privatePrimeAuthorizationCachePath();
+ if (!cachePath) {
+ return undefined;
+ }
+ try {
+ const parsed = JSON.parse(readFileSync(cachePath, "utf8")) as Partial<
+ Omit<PrivatePrimeAuthorizationCache, "modelIds"> & { modelIds: string[] }
+ >;
+ if (
+ typeof parsed.fingerprint !== "string" ||
+ !Array.isArray(parsed.modelIds) ||
+ typeof parsed.refreshedAt !== "number"
+ ) {
+ return undefined;
+ }
+ return {
+ fingerprint: parsed.fingerprint,
+ modelIds: new Set(parsed.modelIds),
+ refreshedAt: parsed.refreshedAt,
+ };
+ } catch {
+ return undefined;
+ }
+ }
+
+ private writePrivatePrimeAuthorizationCache(cache: PrivatePrimeAuthorizationCache): void {
+ const cachePath = this.privatePrimeAuthorizationCachePath();
+ if (!cachePath) {
+ return;
+ }
+ try {
+ const tmpPath = `${cachePath}.${process.pid}.tmp`;
+ writeFileSync(tmpPath, JSON.stringify({ ...cache, modelIds: [...cache.modelIds] }), { mode: 0o600 });
+ renameSync(tmpPath, cachePath);
+ } catch {
+ // Caching is best effort; a failed write just means the next startup refetches.
}
}
diff --git a/packages/coding-agent/src/core/prime-inference-models.ts b/packages/coding-agent/src/core/prime-inference-models.ts
index de2f3789af..c9af39adfa 100644
--- a/packages/coding-agent/src/core/prime-inference-models.ts
+++ b/packages/coding-agent/src/core/prime-inference-models.ts
@@ -40,6 +40,7 @@ export async function fetchAuthorizedPrivatePrimeInferenceModelIds(
apiKey: string,
teamHeaders: Record<string, string>,
fetchFn: typeof fetch = fetch,
+ timeoutMs: number = PRIVATE_MODEL_REFRESH_TIMEOUT_MS,
): Promise<Set<string>> {
if (!teamHeaders["X-Prime-Team-ID"]) {
return new Set();
@@ -50,7 +51,7 @@ export async function fetchAuthorizedPrivatePrimeInferenceModelIds(
Authorization: `Bearer ${apiKey}`,
...teamHeaders,
},
- signal: AbortSignal.timeout(PRIVATE_MODEL_REFRESH_TIMEOUT_MS),
+ signal: AbortSignal.timeout(timeoutMs),
});
if (response.status === 401 || response.status === 403) {
return new Set();