Skip to content

Refresh Pi's model registry before serving the catalog - #4727

Merged
SawyerHood merged 1 commit into
get-bb:mainfrom
dandaka:fix/pi-catalog-refresh-models
Oct 2, 2026
Merged

SawyerHood merged 1 commit into
get-bb:mainfrom
dandaka:fix/pi-catalog-refresh-models

Conversation

@dandaka

@dandaka dandaka commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

The Pi bridge keeps one pi --mode rpc --no-session catalog child per cwd. The child stays alive until it has gone BB_PI_CATALOG_IDLE_MS (5 minutes) without a fetch (catalog.ts). Pi answers get_available_models from session.modelRuntime.getAvailableSnapshot(), which it builds at startup, and its RPC protocol has no refresh command. While a child was alive, bb could not see models added to models.json or credentials added to auth.json until that child was replaced. Root-cause report; additional evidence is in this comment.

What changed

  • plugins/provider-pi/src/bridge/bb-pi-extension.ts: the embedded extension handles a new refresh-models bridge-channel request. It calls ctx.modelRegistry.refresh({ allowNetwork: false }), which reloads models.json, rebuilds providers, and reruns Pi's availability pass (re-reading auth.json). When the session context has no registry it returns { refreshed: false, reason: "unsupported" }. A rejection comes back as the reply's error.
  • plugins/provider-pi/src/bridge/catalog.ts:
    • Each catalog child generation now correlates bridge-channel requests with their replies, using catalog-<n> IDs, a timeout, and rejection when the child exits.
    • Before every get_available_models except a generation's first, the catalog sends refresh-models. Concurrent reads share one in-flight refresh.
    • If the refresh is unsupported, rejects, or does not answer within 5 s, the child is killed, and the existing PiRpcChildExitedError retry in fetchGeneration() serves the list from a new child.
    • A generation's first read is never refreshed, since it serves the snapshot Pi just built. This also bounds the fallback to one respawn per read.
    • probe(), touch(), and the catalog-model-scope handshake are unchanged.
  • plugins/provider-pi/src/bridge/fake-pi-rpc.mjs: the fake now serves a boot-time snapshot, read from FAKE_PI_MODELS_FILE when set. Its extension context has a modelRegistry.refresh that reloads the snapshot and logs to FAKE_PI_REFRESH_LOG. New knobs: FAKE_PI_NO_MODEL_REGISTRY=1 and FAKE_PI_REFRESH_FAILURE=hang|reject. Existing knobs are unchanged.

There are no server, daemon wire, HOST_DAEMON_PROTOCOL_VERSION, CLI, environment-variable, or Pi version changes.

This deviates from the report's proposal. Instead of fingerprinting models.json, the bridge asks Pi to reload. A fingerprint would miss credentials added to auth.json, which is the case in the follow-up evidence on the issue.

Compatibility: ModelRegistry.refresh exists in Pi 0.84.0 (the declared minimum and the pinned devDependency) and in 1.0.0. The unsupported path covers a missing session context, or an older or custom build launched through BB_PI_BRIDGE_COMMAND.

Scope limit: every bridge fetch is now fresh, but the server still memoizes the catalog for 10 minutes and serves stale data while it revalidates (FRESH_MS / evaluate()). The model picker and bb provider models pi can therefore still lag by up to that window. That layer is a separate follow-up, and the design question for it is in the issue comment. resolvePiModel() reads the warm catalog through the same rawModels() path, so bridge-side model resolution sees new models right away.

How you verified

  • New tests in catalog.test.ts. All five fail on main with the stale [ 'fake-provider/fake-model' ] list and pass with this change:
    • A model added after the child started is served without a respawn. The spawn counter stays at 1, and there is one {"allowNetwork":false} refresh.
    • Concurrent listModels() and rawModels() calls share one refresh.
    • With no model registry, the second read comes from a new child (spawn counter 2).
    • When the refresh hangs or rejects, the child is replaced and the first child has exited (2 cases).
    • The existing restart test (FAKE_PI_SCOPE_BY_SPAWN / FAKE_PI_EXIT_AFTER_FIRST_AVAILABLE) passes, unchanged.
  • pnpm exec turbo run test --filter=bb-plugin-provider-pi: 27 files, 170 tests passed. turbo run typecheck and turbo run lint (0 warnings, 0 errors) pass for the same filter. git diff --check is clean, and oxfmt --check is clean on the four changed files.
  • Real Pi, no bb server. I called getPiCatalog() with the real pi binary and an isolated PI_CODING_AGENT_DIR: read, add a model to models.json, read, write a deepseek key to auth.json, read.
    • main, Pi 1.0.0: ['probe/alpha'] on all three reads.
    • This branch, Pi 1.0.0: ['probe/alpha'] → ['probe/alpha', 'probe/beta'] → ['deepseek/deepseek-flash', 'deepseek/deepseek-v4-pro', 'probe/alpha', 'probe/beta'], with the same pi PID throughout and 2–4 ms per read.
    • This branch, Pi 0.84.0: the same result (deepseek/deepseek-v4-flash, deepseek/deepseek-v4-pro), with the same PID.
  • The open fix: resolve Pi selections against the host model catalog #4710 also edits fake-pi-rpc.mjs. A test merge with it applies cleanly.

Fixes #2289

🤖 Generated with Claude Code

AGENT GENERATED

The bridge keeps one `pi --mode rpc` child per cwd for up to five idle
minutes, and Pi answers `get_available_models` from a snapshot it builds
at startup. Models or credentials added while the child was alive stayed
invisible until the child was replaced.

Before every catalog read after a generation's first, ask the bb
extension inside Pi to call `ctx.modelRegistry.refresh({ allowNetwork:
false })` over the bridge channel. Concurrent reads share one refresh.
When the registry is missing, the refresh rejects, or it does not answer
within five seconds, kill the child and let the existing exit retry serve
the list from a newly started one.

Refs get-bb#2289

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Thanks @dandaka for the PR. This repo needs approval before a PR. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ) and explain what you want to open a PR for and why. A maintainer then adds you to the allow list. This PR now closes on its own. Reopen it or open a new one after approval.

@github-actions github-actions Bot closed this Oct 2, 2026
SawyerHood added a commit that referenced this pull request Oct 2, 2026
## Human comments

## What was wrong

@dandaka's PR #4727 was closed by the contributor gate because the
account was absent from the approved contributor list.

## What changed

Add `dandaka` to `.github/APPROVED_CONTRIBUTORS` so they can open pull
requests. This grants PR approval only.

## How you verified

Verified the canonical GitHub login, confirmed it was absent from main,
checked that the diff adds only this login, and confirmed it appears
exactly once case-insensitively. `git diff --check` passed. CI and
SlopCop are skipped for this data-only change.

> AGENT GENERATED
@SawyerHood SawyerHood reopened this Oct 2, 2026

@SawyerHood SawyerHood left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit e0838a7. No blocking findings.

The refresh uses the same ModelRuntime that serves get_available_models in the supported Pi 0.84.0 dependency. Reply correlation, shared in-flight refresh, and bounded child-replacement retry cover the changed behavior. The tests load the actual embedded extension and exercise freshness and unsupported, rejected, and timed-out refresh paths.

Independent verification:

  • pnpm exec turbo run test typecheck lint --filter=bb-plugin-provider-pi passed: 169 tests passed, one Bun runtime test skipped; typecheck and lint passed.
  • Real Pi 0.84.0 with an isolated PI_CODING_AGENT_DIR: adding and removing models in models.json and adding a placeholder provider key in auth.json were reflected by successive reads through the same catalog object.
  • git diff --check passed.

The server's existing 10-minute catalog cache remains a documented scope limitation: this fixes bridge freshness, while model-picker freshness still needs the separate follow-up described in the PR. I did not verify Pi 1.0.0 independently.

AGENT GENERATED

@SawyerHood
SawyerHood merged commit 2d03788 into get-bb:main Oct 2, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pi provider: stale model catalog after models.json change; plugin reload never retires superseded bridge workers

2 participants