Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions .pi/extensions/fm-codex-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Firstmate's always-visible Codex credential-store indicator for the Pi footer.
//
// Verified against Pi 0.84.4, which renders every extension status registered through
// ctx.ui.setStatus() on its own footer line, sorted by key and joined with a space,
// directly beneath the token/cost/context stats. That line is on screen for as long as
// the built-in footer is, which is also for as long as the captain's quota display is,
// so the indicator needs no widget of its own. A widget would be the wrong surface
// anyway: Pi orders widgets by extension load order, which is unsorted directory order,
// so a second belowEditor widget could land above or below the quota block from one run
// to the next and would compete with it for Pi's ten-line widget budget.
//
// This file never reads a credential. Two non-secret Pi APIs answer the whole question:
// getAgentDir() resolves the agent directory this process actually authenticates from
// (PI_CODING_AGENT_DIR, otherwise ~/.pi/agent), and modelRegistry.getProviderAuthStatus
// reports whether a credential for the provider is configured and which surface it came
// from. No access token, refresh token, stored credential, account id, or email address
// is read, derived, or displayed. A store is named only when Pi reports the credential
// as the stored one; every other case renders an explicit unverified state rather than
// implying an account.
//
// docs/pi-account-indicator.md owns the captain-facing behavior and its limits.
import { homedir } from "node:os";
import { relative, resolve, sep } from "node:path";
import { getAgentDir } from "@earendil-works/pi-coding-agent";
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";

// Pi's provider id for the ChatGPT/Codex subscription family.
const PROVIDER = "openai-codex";
// Sorted with the other extension statuses on Pi's footer status line.
const STATUS_KEY = "firstmate-codex-account";

// A store label is a path fragment and nothing else. Pi's own footer sanitizer only
// collapses whitespace, so an escape sequence reaching this text would repaint the
// captain's screen, and a mangled label could read as a different store than the one
// in use. Anything that is not plainly path-shaped is therefore reported as unknown
// rather than cleaned up into something that looks like an answer.
const SAFE_LABEL = /^[A-Za-z0-9._\-/]{1,64}$/;
// The AuthStatus.source values Pi 0.84.4 reports. Only "stored" means the credential
// came from the agent directory this indicator names.
const KNOWN_SOURCES = new Set([
"stored",
"runtime",
"environment",
"fallback",
"models_json_key",
"models_json_command",
]);

/**
* The credential store this Pi process authenticates from, as the captain reads it:
* `.pi` and `.pi-geris` rather than two long paths that differ late. The trailing
* `agent` segment is dropped because it is identical for every home and carries none
* of the distinction. A directory outside the home keeps its absolute path.
*/
function codexStoreLabel(agentDir: string, home: string): string {
const suffix = `${sep}agent`;
// Normalized first so a trailing separator cannot leave the `agent` segment in
// the label and make one store read as two.
const normalized = resolve(agentDir);
const store = normalized.endsWith(suffix) ? normalized.slice(0, -suffix.length) : normalized;
const fromHome = relative(home, store);
const label = fromHome && !fromHome.startsWith("..") ? fromHome : store;
return SAFE_LABEL.test(label) ? label : "unknown";
}

/**
* What the footer says, from the resolved store and Pi's own non-secret auth status.
* `configured` alone is not enough to name a store: an environment or runtime
* credential authenticates some other account entirely, and naming the store then
* would be exactly the false claim this indicator exists to prevent.
*/
function codexAccountStatus(
agentDir: string,
home: string,
auth: { configured: boolean; source?: string } | undefined,
): string {
if (!auth?.configured) return "codex@unverified(no-login)";
if (auth.source !== "stored") {
const source = auth.source && KNOWN_SOURCES.has(auth.source) ? auth.source : "other";
return `codex@unverified(${source})`;
}
return `codex@${codexStoreLabel(agentDir, home)}`;
}

export default function (pi: ExtensionAPI) {
const publish = (ctx: ExtensionContext): void => {
if (!ctx.hasUI) return;
let auth: { configured: boolean; source?: string } | undefined;
try {
auth = ctx.modelRegistry.getProviderAuthStatus(PROVIDER);
} catch {
// An unavailable auth status is missing evidence, never a signed-in account.
auth = undefined;
}
ctx.ui.setStatus(STATUS_KEY, codexAccountStatus(getAgentDir(), homedir(), auth));
};

// Pi exposes no login or logout event, so the status is refreshed on the events that
// bracket every way the credential can change under a running session: a new or
// reloaded session, a model or provider switch, and the start of each turn.
pi.on("session_start", (_event, ctx) => publish(ctx));
pi.on("model_select", (_event, ctx) => publish(ctx));
pi.on("turn_start", (_event, ctx) => publish(ctx));
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Firstmate's skills live in two separate places with different audiences:
- [docs/configuration.md](docs/configuration.md) - environment variables, `FM_HOME`, runtime backend selection, optional Relay and its X and Discord setup steps, the files you set, and harness support.
- [docs/remote-secondmates.md](docs/remote-secondmates.md) - current setup, routing, transfer, recovery, and safety behavior for whole-home remote second mates.
- [docs/calm.md](docs/calm.md) - current Pi `/calm` behavior and supported presentation limits.
- [docs/pi-account-indicator.md](docs/pi-account-indicator.md) - the Pi footer indicator that names the Codex credential store the running session authenticates from.
- [docs/voice-relay.md](docs/voice-relay.md) - the optional spoken interface: setup on both machines, measured round-trip cost, what a spoken answer may read, and what this build does not do yet.
- [docs/wedge-alarm.md](docs/wedge-alarm.md) - configure the active alert for an away-mode escalation delivery that gets stuck.
- [docs/tmux-backend.md](docs/tmux-backend.md) - current setup and limits for the tmux reference backend.
Expand Down
1 change: 1 addition & 0 deletions bin/fm-test-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ family_for_basename() {
fm-bearings-board.test.sh|\
fm-brief.test.sh|fm-vendor-auth-probe.test.sh|\
fm-calm-pi-extension.test.sh|fm-cd-pretool-check.test.sh|\
fm-codex-account-pi-extension.test.sh|\
fm-classify-decision-key.test.sh|\
fm-composer-ghost.test.sh|fm-composer-lib.test.sh|\
fm-crew-state.test.sh|fm-captain-hold-lifecycle.test.sh|\
Expand Down
12 changes: 12 additions & 0 deletions docs/documentation-audiences.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
"source": "docs/codex-app-backend.md",
"target": "docs/verification/runtime-backends.md"
},
{
"source": "docs/pi-account-indicator.md",
"target": "docs/verification/pi-account-indicator.md"
},
{
"source": "docs/trace-context.md",
"target": "docs/verification/trace-context.md"
Expand Down Expand Up @@ -296,6 +300,10 @@
"path": "docs/orca-backend.md",
"audience": "operator-current"
},
{
"path": "docs/pi-account-indicator.md",
"audience": "operator-current"
},
{
"path": "docs/remote-secondmates.md",
"audience": "operator-current"
Expand Down Expand Up @@ -364,6 +372,10 @@
"path": "docs/verification/muse.md",
"audience": "maintainer-verification"
},
{
"path": "docs/verification/pi-account-indicator.md",
"audience": "maintainer-verification"
},
{
"path": "docs/verification/process-event-sources.md",
"audience": "maintainer-verification"
Expand Down
32 changes: 32 additions & 0 deletions docs/pi-account-indicator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Pi Codex account indicator

Firstmate's Pi interface names the Codex credential store the running session authenticates from, so the quota figures on screen can be read against the account that actually supplies them.
The indicator is always on, needs no configuration, and adds nothing to the quota display's own numbers or layout.

Pi renders it on the footer's extension-status line, directly beneath the token, cost, context, and model row, for as long as the footer itself is on screen.

It reads one of three forms:

- `codex@<store>` names the credential store in use, written the way the captain reads it: `codex@.pi` for the personal Pi home and `codex@.pi-geris` for the Geris one.
A store outside the home directory is named by its absolute path instead.
- `codex@unverified(<reason>)` states that no account can be named.
`no-login` means the store holds no Codex credential, and a source name such as `environment` or `runtime` means the credential in use came from somewhere other than the store, so naming that store would describe the wrong account.
- `codex@unknown` means the store's own path cannot be stated safely, so it is reported as unknown rather than cleaned up into something that reads like a real store.

The store is whichever agent directory this Pi process authenticates from, which is `PI_CODING_AGENT_DIR` when set and `~/.pi/agent` otherwise.
That is the same directory Pi reads its credential from, so the label is the account bucket itself rather than an inference from the model, the provider, or the shell.

The indicator names a store, never a person and never an account address.
It reads no access token, no refresh token, no stored credential, and no account id, and it displays none of those, so a store that holds an unexpected login is reported as that store rather than as the account behind it.

Pi emits no login or logout event, so the indicator refreshes when a session starts or reloads, when the model or provider changes, and at the start of each turn.
A `/login` performed mid-turn is therefore reflected from the next turn onward.

`.pi/extensions/fm-codex-account.ts` owns the behavior, and [`verification/pi-account-indicator.md`](verification/pi-account-indicator.md) owns the dated Pi evidence it rests on.

Regression entry points:

```sh
tests/fm-codex-account-pi-extension.test.sh
tests/fm-pi-primary-types.test.sh
```
89 changes: 89 additions & 0 deletions docs/verification/pi-account-indicator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Pi Codex account indicator verification

Audience: maintainer verification.

This record supports [`pi-account-indicator.md`](../pi-account-indicator.md) and the extension at `.pi/extensions/fm-codex-account.ts`.
It records only the vendor facts that must be re-established when Pi changes.

## Surfaces and APIs the indicator depends on

Verified 2026-09-08 on Pi 0.84.4.

```sh
pi --version
```

```text
0.84.4
```

Three package-level facts carry the implementation, each read from the installed package's own declarations:

- `getAgentDir()` is re-exported from the package root (`dist/index.d.ts`) and resolves `PI_CODING_AGENT_DIR`, falling back to `~/.pi/agent` (`dist/config.js`).
`getAuthPath()` is `join(getAgentDir(), "auth.json")` in the same module, so the resolved agent directory is the credential store, not a directory that merely sits beside one.
- `ExtensionUIContext.setStatus(key, text)` is declared as footer/status-bar text (`dist/core/extensions/types.d.ts`), and `FooterComponent` renders every extension status on one footer line, sorted by key and joined with a space, below the token, cost, context, and model row (`dist/modes/interactive/components/footer.js`).
Pi's own footer sanitizer only collapses whitespace, so escape sequences in status text survive; the extension therefore refuses any store label that is not plainly path-shaped rather than passing it through.
- `ModelRegistry.getProviderAuthStatus(provider)` returns `{ configured, source?, label? }` (`dist/core/model-registry.d.ts`, `dist/core/provider-composer.d.ts`), where `source` is one of `stored`, `runtime`, `environment`, `fallback`, `models_json_key`, or `models_json_command`.
It reports that a credential exists and which surface it came from, and never which account it belongs to.

A `belowEditor` widget was rejected as the surface: Pi orders widgets by extension load order, which is unsorted directory order (`dist/core/extensions/loader.js`), and caps the widget area at ten lines, so a second widget could render above or below an existing quota widget from one run to the next.

Pi exposes no login, logout, or auth-changed event in `ExtensionAPI`, which is why the indicator refreshes on `session_start`, `model_select`, and `turn_start` instead.

## Non-secret readiness evidence

Verified 2026-09-08 on Pi 0.84.4, against the personal agent directory.

```sh
pi auth check --provider openai-codex --json --no-refresh
```

```json
{"status":"ready","provider":"openai-codex","authType":"oauth"}
```

This confirms `openai-codex` as Pi's provider id for the ChatGPT/Codex subscription family and confirms that a store-backed OAuth credential is what the personal home holds.
The indicator does not call this command; the fact is recorded because it is the supported way to re-establish provider readiness by hand without emitting a credential, which `--credentials` would.

## Live rendering evidence

Verified 2026-09-08 on Pi 0.84.4 under tmux at 200 columns, with the extension loaded through `-e` and `--no-session` so no session file was written.

Personal store, against the real agent directory:

```sh
pi --no-session -e .pi/extensions/fm-codex-account.ts
```

The captured footer tail, with the quota display above it unchanged:

```text
/tmp/claude-1000
$0.000 (sub) 0.0%/272k (auto) (openai-codex) gpt-5.6-sol • medium
codex@.pi
```

Second store, against a scratch home holding a synthetic API-key credential, so the real Geris home was neither read nor written:

```sh
HOME=<scratch> PI_CODING_AGENT_DIR=<scratch>/.pi-geris/agent pi --offline --no-session -e .pi/extensions/fm-codex-account.ts
```

```text
/tmp/claude-1000
0.0%/0 (auto) unknown
codex@.pi-geris
```

The two homes render as distinct labels, and the second run also shows that the label follows the credential store rather than the model: Pi reported no model at all under `--offline` with an empty catalog cache, and the store was still named correctly.

An agent directory holding no credential cannot be shown this way, because Pi exits rather than starting an interactive session with no provider available.
`tests/fm-codex-account-pi-extension.test.sh` covers that state, along with the foreign-source, unrecognized-source, unavailable-status, and unnameable-store states.

## Regression coverage

`tests/fm-codex-account-pi-extension.test.sh` loads the tracked extension against the installed Pi package, so `PI_CODING_AGENT_DIR` is resolved by Pi's own `getAgentDir()` rather than by a copy of that logic in the test.
It pins the two distinct store labels and asserts the divergence itself, the four unverified states, the unnameable-store refusal, the refresh events, footer silence without a UI, and secret safety.
Secret safety is asserted two ways that fail for different reasons: the fake registry raises from every credential-bearing Pi API, and the resolved store holds an unreadable credential file, so a run that reached for either would fail rather than pass quietly.

`tests/fm-pi-primary-types.test.sh` type-checks the extension against the installed Pi declarations under `strict`.
Loading
Loading