Extension-registered providers: fresh sessions intermittently ignore defaultProvider/defaultModel and start on another provider's default
What happened?
A fresh pi session intermittently does not start with the configured
defaultProvider/defaultModel from settings.json when that provider is
registered by an extension via pi.registerProvider(name, config). Instead, pi
silently falls back to another provider's default model
(defaultModelPerProvider) — in our case google/gemini-3.1-pro-preview,
which our Google credential (free tier, quota limit: 0) cannot serve, so the
first request fails with a 429.
Measured over repeated fresh launches: ~4 of 10 sessions start on the wrong
model; the rest start correctly on the configured default. Instrumenting
findInitialModel (step 3) shows the outcome correlates 1:1 with the
availability snapshot state at resolution time:
step3 <default-provider>/<default-model> -> HIT auth=false → session starts on the fallback
step3 <default-provider>/<default-model> -> HIT auth=true → session starts on the configured defaultRoot cause: createAgentSession resolves the initial model
(restoreModelFromSession / findInitialModel) before name+config provider
registrations from extensions are applied to the model registry.
pi.registerProvider(name, config)queues intoruntime.pendingProviderRegistrationswhile extensions are loading (loader.js, "Pre-bind: queue registrations…"); the queue is flushed inbindCore, which runs when theAgentSessionis constructed — after initial model resolution.findInitialModelstep 3 (model-resolver.js) requiresgetModel(provider, id)andhasConfiguredAuth(provider); the latter readssnapshot.configuredProviders, which is rebuilt asynchronously byrunAvailabilityRefreshand can lag the registration.- When step 3 misses, step 4 returns the first
defaultModelPerProvidermatch from the available snapshot — silently, with no warning.
Note that pi.registerNativeProvider(provider) (object form) is flushed in
createAgentSessionServices with an awaited
modelRuntime.refresh({ allowNetwork: false }) before model resolution — the
name+config form has no equivalent before findInitialModel.
Possibly a residual of #6948 / #6922 (async provider model refresh vs startup
resolution, addressed for the built-in llama.cpp provider): model lists now
hydrate from the store at startup, but the configured/auth snapshot can still
lag, and the fallback is silent instead of an error. #2291 is an earlier
instance of the same ordering problem from a session_start hook.
Suggested direction: after extension loading completes, flush both pending
registration queues (name+config and native) and await
modelRuntime.refresh({ allowNetwork: false }) before initial-model
resolution — mirroring what createAgentSessionServices already does after
extension load. Happy to send a PR if the direction looks right.
Steps to reproduce
- Register a provider from an extension using the name+config form:
export default function (pi) {
pi.registerProvider("acme", {
name: "acme",
baseUrl: "http://127.0.0.1:9/v1", // never called in this repro
api: "openai-completions",
models: [
{
id: "acme-model",
name: "Acme Model",
contextWindow: 128000,
maxTokens: 8192,
},
],
auth: {
apiKey: {
name: "acme",
check: async () => ({ type: "api_key" }),
resolve: async () => ({ auth: { apiKey: "demo" }, source: "acme" }),
},
},
fetchModels: async () => [],
});
}- Add a stored credential for it in auth.json:
"acme": { "type": "api_key", "key": "demo" }- settings.json:
{ "defaultProvider": "acme", "defaultModel": "acme-model" }- Launch several fresh sessions back-to-back and inspect each session
journal's first
model_changeentry:
for i in $(seq 1 10); do
d=/tmp/repro-$i; mkdir -p "$d"; (cd "$d" && pi -p "hi" >/dev/null 2>&1)
done
# first model_change per journal: acme-model @ acme on good launches,
# gemini-3.1-pro-preview @ google on fallback launchesMeasured: 4 of 10 fresh sessions started on the fallback provider; 10 of 10 with the refresh applied before resolution started on the configured default.
Expected behavior
Every fresh session starts with defaultProvider/defaultModel from
settings.json — regardless of how long extension loading takes. If the
configured provider cannot be resolved yet, pi should say so explicitly
instead of silently starting on an unrelated provider's default model.
Version
0.84.4
Source: earendil-works/pi