Skip to content

feat(pi): add persistent supervision branch model selection - #3028

Merged
kunchenguid merged 7 commits into
mainfrom
fm/fm-supervision-branch-model-r1
Aug 25, 2026
Merged

kunchenguid merged 7 commits into
mainfrom
fm/fm-supervision-branch-model-r1

Conversation

@kunchenguid

Copy link
Copy Markdown
Owner

Intent

Add a Pi slash command that lets an operator pick and persist a cheaper model for the supervision-branch session, stored in this Firstmate home's gitignored config/ directory.

WHY: people are adopting the Pi main+supervision-branch design, and supervision is an easier job than main, so running the branch on a cheaper model saves cost. The captain wants operators to configure a different model for the supervision session.

ACCEPTED REQUIREMENTS, all deliberate:

  • The command must let the captain choose a model IN-SESSION, ideally through Pi's own model picker. Investigation of the Pi 0.81.1 SDK found Pi's built-in interactive commands (/model, /settings) are explicitly NOT exposed to extensions - the SDK docs state they are handled only in interactive mode and are excluded from pi.getCommands(). So the smallest supported alternative was used rather than forcing it: ctx.ui.select() (Pi's own selector dialog) over Pi's own catalog. This keeps the intended UX and was judged not to change it materially, so no escalation was raised on that point. The picker limitation is noted in docs/configuration.md.
  • Explicit constraint: do NOT invent a parallel Firstmate model catalog. Firstmate keeps no model catalog of its own; the offered list is what Pi reports when the picker opens.
  • Persist the pick under config/ as a firstmate-owned, gitignored single-value file matching the existing convention (config/calm, config/crew-harness). Chosen name config/supervision-branch-model, one / line plus a newline, split at the FIRST slash so a provider-qualified id such as openrouter/anthropic/claude-sonnet-4-5 survives. Written atomically via tmp+rename at mode 0600, copying the config/calm precedent so a failed write leaves the current choice unchanged. config/ is already gitignored wholesale, so no .gitignore change was needed.
  • The supervision branch must READ that choice when it creates or reopens its session so it survives session start, /new, /resume, /fork, and reload. All those paths funnel through the single createBranch() in .pi/extensions/fm-branch-supervision.ts, so resolution happens there, before any session-file or prompt work, and is passed as createAgentSession's model option.
  • Scope constraint: keep the change to supervision-branch model selection only. Do not alter main's model, or the branch's actor-identity, lease, generation, or lock-ownership logic.

CAPTAIN DECISIONS MADE DURING REVIEW OF AN EARLIER ROUND, all now implemented and all binding:

  1. CREDENTIALS - do NOT install, convert, derive, or overwrite credentials at the branch-session boundary. An earlier attempt registered the main registry's provider and installed an API key into a fresh branch runtime; that route was ruled out and removed. Key fact: the supervision branch runs in the SAME home and SAME OS user as main, so its runtime already holds the user's stored credentials and each keeps its native type - OAuth stays OAuth, an API key stays an API key. This matters for OAuth-only families such as openai-codex/*, which broke when an OAuth-derived token was installed as a runtime API key. The branch now relies entirely on the stored credentials its own runtime already holds.
  2. PICKER RESTRICTION - to keep the invariant that every offered model actually works, the picker is the intersection of what Pi reports as available and what a fresh isolated branch runtime can itself resolve with those existing stored credentials. A provider that exists only because a main-session extension registered it dynamically is deliberately not offered. This was chosen over weakening the branch's isolation.
  3. ISOLATION IS A HARD CONSTRAINT - the branch loader keeps noExtensions, noSkills, noPromptTemplates, noThemes, and noContextFiles all true, and never loads main-session extensions. That isolation is deliberate: the branch must never be able to spawn its own branch, and its system prompt must stay byte-stable for prompt caching.
  4. LIVE REBIND RACE - a pick landing while a branch build is in flight must not be silently lost. A model-selection revision is tracked through branch creation, so a session built against a stale read is disposed and rebuilt under the newest choice rather than adopted.
  5. PIN IS AUTHORITATIVE ON EVERY REOPEN - this was a real correctness bug, not a docs issue. Clearing the pin with 'Follow main' removed the file, but the next build reopened the persistent branch session with no explicit override, so Pi restored the model that session had recorded (the old pinned model) while the command reported that the branch follows main. The current state of the pin file now decides the model on every branch create AND reopen and overrides Pi's session-state restore. With a pin, that model. With no pin, main's own current model is applied EXPLICITLY, tracked from the contexts Pi already hands the extension plus its model_select event, because createBranch runs at wake time with no context of its own.
  6. NEVER LOSE A WAKE OVER MODEL CHOICE - only when main's model is genuinely unknown, or the isolated runtime cannot run it, does a build fall back to passing no model override at all, which is the pre-feature behavior. The branch is never refused over model choice alone.
  7. NOTIFICATION HONESTY - the command reports the model actually applied, and says plainly when clearing the pin could not apply main's model, rather than claiming a change that did not take effect.
  8. UNUSABLE VS UNPARSEABLE PIN - a pin naming a model the isolated branch runtime cannot hand back is never silently downgraded onto main's model: the branch refuses to build and the wake falls back to the captain-facing main path naming the unusable pin, which is this extension's own documented failure direction. An unparseable file is deliberately treated as simply no pin, matching the config/calm precedent, so the branch follows main.
  9. INHERITANCE - the pin is home-local and NOT part of secondmate inherited configuration. It is not added to FM_INHERITABLE_CONFIG. This matches config/calm, likewise a Pi-extension-read, FM_CONFIG_OVERRIDE-resolved, per-operator preference documented as not inherited.
  • docs/configuration.md owns the operator-facing schema and documents the file: name, format, what the picker offers, the authoritative-on-every-build semantics, absent-means-follow-main, the fallback, atomic replacement, and the non-inheritance choice.

REPO CONVENTIONS required and followed (this edits Firstmate's shared tracked material): the firstmate-coding-guidelines skill was loaded first; one full sentence per line in tracked Markdown; plain dash never an em dash; no agent name as a commit co-author; colocated tests extended in tests/ rather than a new runner; tests exercise executable interfaces and observable behavior rather than implementation source text; harness-dependent behavior is proven against the real vendor SDK with the dated result recorded in docs/verification/runtime-backends.md.

TESTS, all behavioral, in tests/fm-pi-branch-extension.test.sh: the current pin state binding every create and reopen; clearing the pin returning a reopened branch to main's model and specifically NOT the old pinned one; an unparseable pin behaving as no pin; the unknown-main-model fallback to no override; the picker's offered contents including the exclusion of a main-session-only provider and the preservation of OAuth credential type; persisted bytes and 0600 mode; live-branch rebind; the in-flight build race; cancellation leaving the pin untouched; a runtime load failure leaving the old pin and emitting no success notice; and unusable-pin fallback to main. The follow-main wiring was mutation-checked to confirm the tests fail without it. The opt-in real-SDK guard tests/fm-pi-branch-live-e2e.test.sh gained two probes against the real installed pi-coding-agent: the extension reading an unresolvable pin through a real ModelRegistry, and a credential-free precedence probe using a local never-contacted fake provider proving an explicit model is applied on create, wins over the model a reopened session recorded, and is absent-pin-restorable.

While updating the dated verification record I also corrected its stale claim that the installed signed pi CLI was 0.84.1; this machine has 0.82.0, and the npm package pinned by the guard and typecheck is 0.81.1.

README.md gained one pointer sentence next to the existing /calm sentence for discoverability, with docs/configuration.md remaining the single owner of the schema per the repo's one-owner rule. AGENTS.md gained one line in its config inventory, and docs/pi-supervision-branch.md gained one-line cross-references in its components, cost-model, and verification sections.

What Changed

  • Add /supervision-model to select an isolated-runtime-compatible model from Pi’s catalog and persist the pin atomically with private permissions.
  • Apply the current pin, or explicitly follow main, on every supervision branch create and reopen while safely rebinding live and in-flight sessions.
  • Document the configuration and expand behavioral and real-SDK coverage for model selection, persistence, fallback, credentials, and session precedence.

Risk Assessment

✅ Low: The model-selection changes are scoped, preserve branch isolation and credential semantics, and address live and in-flight rebinding without introducing a substantiated correctness issue.

Testing

Inspected the model-selection change, ran focused behavioral and strict type tests, and verified against the real Pi 0.81.1 SDK that explicit models override reopened session state. Pin persistence, Follow main, live-follow rebinding, pinned-model stability, picker filtering, OAuth preservation, race handling, and honest failure notifications all worked. No screenshot was captured because the Pi selector was exercised through its headless command interface.

Evidence: Focused supervision-branch behavioral test transcript

Source: Focused supervision-branch behavioral test transcript

ok - branch owns accepted wakes with a stable prefix contract and verdict-driven merge delivery
ok - scopeForUnreadWake excludes every main-only class without vetoing eligible task-local rows, and writes the eligible snapshot
ok - branch prompt_cache_key is stable per home across sessions and distinct between homes
ok - branch default-on eligibility (task-scoped, heartbeat, afk) binds and a broken branch falls back to main
ok - pre-drain eligibility re-check defers a newly main-owned row
ok - pre-drain eligibility re-check excludes a newly main-owned row without deferring eligible work
ok - a settled branch turn releases an unacknowledged grant for main replay
ok - a stale main claim cannot silently suppress later wake delivery
ok - pre-drain eligibility re-check no-ops an already-drained wake
ok - dialog mirror filters tool and operational traffic, lands before wakes, and keeps a durable cursor
ok - branch session persists across process restarts through the recorded pointer
ok - the current pin state binds every branch create and reopen, and clearing it returns the branch to main's model
ok - unpinned branches follow main model changes live while pinned branches stay fixed
ok - supervision-model command persists the captain's pick and rebinds the live branch
ok - an unusable model pin falls back to main and an unparseable one is treated as no pin
ok - replacement activation cleans old branch leases and retries failed cleanup
ok - branch activates on a cold start once the lock is acquired, never before
ok - queued wakes and mirrors stop mutating branch state after lock ownership is lost
ok - stale reports, shells, mirrors, cursors, leases, and prompts perform no side effects
ok - a Pi session that does not own the lock accepts nothing and mutates no branch state
ok - an extension rebind re-mirrors undelivered dialog instead of dropping it
Evidence: Real Pi 0.81.1 SDK end-to-end transcript

Source: Real Pi 0.81.1 SDK end-to-end transcript

ok - real Pi SDK 0.81.1 accepts the branch session construction and preserves an unpromptable wake
ok - real Pi SDK 0.81.1 applies an explicit branch model on create and over a reopened session's recorded model
Evidence: Strict Pi extension typecheck transcript

Source: Strict Pi extension typecheck transcript

ok - tracked Pi extensions pass strict no-emit typecheck against Pi 0.81.1

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 2 issues found → auto-fixed ✅
  • 🚨 .pi/extensions/fm-branch-supervision.ts:859 - The required absent-pin “follow main” behavior is not maintained when main changes models. model_select only updates mainModel; it neither invalidates an in-flight build nor releases an existing unpinned branch. Concrete path: build an unpinned branch on model A, switch main to B, then dispatch another wake; the persistent branch still runs A indefinitely. This contradicts the criterion that “with no pin, main's own current model is applied EXPLICITLY.” When no valid pin exists, bump the model-selection revision and release the branch at this event boundary so an in-flight or live A session cannot survive the switch.
  • 🚨 .pi/extensions/fm-branch-supervision.ts:933 - The post-clear model resolution is outside error handling. If ModelRuntime.create() succeeds for the picker but fails after the operator selects Follow main, the pin has already been removed and the branch released, then the command rejects without the required honest warning. This contradicts “the command ... says plainly when clearing the pin could not apply main's model.” Catch this resolution failure and emit the same warning path with its reason.

🔧 Fix: Make unpinned supervision follow main model changes
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • git status --short and target diff inspection
  • bash tests/fm-pi-branch-extension.test.sh
  • bash tests/fm-pi-primary-types.test.sh
  • FM_PI_BRANCH_LIVE_E2E=1 bash tests/fm-pi-branch-live-e2e.test.sh
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Supervision is an easier job than the captain's own conversation, so the
Pi supervision branch does not need main's model. A new /supervision-model
command opens Pi's own selector over Pi's own catalog of credentialed
models, plus a "Follow main" entry, and persists the pick as one
<provider>/<model-id> line in this home's gitignored
config/supervision-branch-model. Firstmate keeps no model catalog of its
own.

The branch resolves the pin at every branch build - the first wake of a
cold start and the reopen after /new, /resume, /fork, or reload - so the
choice survives all of them, and picking also releases the live branch so
the next wake reopens the same persistent branch conversation under the
new model. An absent, unreadable, or unparseable file means no pin and
keeps today's behavior byte for byte: no model option is passed and Pi
picks the branch's model exactly as before.

A pin naming a model Pi cannot hand back is never silently downgraded
onto main's model: the branch refuses to build and the wake falls back to
the captain-facing main path naming the unusable pin, which is the
extension's existing failure direction.

The choice is home-local and not part of secondmate inherited
configuration, matching the Pi Calm preference precedent.

docs/configuration.md owns the operator-facing schema. Portable
regressions cover pin-present on create and reopen, pin-absent default,
the command's persistence, cancellation, and live rebind, and both
unusable and unparseable pins. The opt-in real-SDK guard proves the
vendor surface the pin reads and that an explicit model wins over the
model a reopened session recorded.
Clearing the pin with "Follow main" removed the file but the next branch
build reopened the persistent branch session with no explicit model
override, so Pi restored the model that session had recorded - the old
pinned model - while the command reported that the branch now follows
main. The same gap meant an absent pin did not reliably mean
same-model-as-main once a home had pinned once.

The pin file's current state now decides the model on every branch build,
create and reopen alike, overriding Pi's session-state restore. With a
pin, that model. With no pin, main's own current model is applied
explicitly, tracked from the contexts Pi already hands the extension plus
its model_select event, since the branch is built at wake time with no
context of its own. Only when main's model is unknown, or this home's
stored credentials cannot run it in the isolated branch runtime, does a
build fall back to passing no override at all, which is the behavior from
before the pin existed; the branch is never refused over model choice.

The command's notification now reports the model actually applied, and
says plainly when clearing the pin could not apply main's model instead
of claiming a change that did not take effect.

No credential handling changes: the branch still relies entirely on the
stored credentials its own runtime already holds, and the picker stays
restricted to models that runtime can resolve.

Colocated regressions cover pin present on create and reopen, clearing
the pin returning a reopened branch to main's model and specifically not
the old pinned one, an unparseable pin behaving as no pin, and the
unknown-main-model fallback to no override.
@kunchenguid
kunchenguid force-pushed the fm/fm-supervision-branch-model-r1 branch from 688cf90 to 730ab67 Compare August 25, 2026 07:26
@greptile-apps

greptile-apps Bot commented Aug 25, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge, with no concrete blocking or independently actionable non-blocking issue identified.

The model pin is applied through the shared branch creation path, stale builds are rejected through revision tracking, live replacement is serialized behind active work, and failure paths preserve wake fallback and provide accurate operator notifications.

Reviews (1): Last reviewed commit: "no-mistakes(document): Correct supervisi..." | Re-trigger Greptile

@kunchenguid
kunchenguid merged commit 9a01dea into main Aug 25, 2026
25 of 26 checks passed
@kunchenguid
kunchenguid deleted the fm/fm-supervision-branch-model-r1 branch August 25, 2026 22:15
akrcdev added a commit to akrcdev/firstmate that referenced this pull request Aug 29, 2026