# Architecture review: catalog tier as a model property, and a clock seam for the login poll loop > **Date:** 2026-11-25 > **Type:** generic > **Reference:** improve-codebase-architecture review (HTML report in `$TMPDIR`, not committed); grilling round 1–2; supersedes the round-3 decision row in `20261125-fresh-context-audit-rounds-1-5_log.md` ## Goal Run the architecture review on `index.ts`, surface where a module is shallow or a rule has no locality, and implement the candidates the grilling settled, in priority order. ## What Was Done - **Review.** Hot spots taken from `git log` (every fix commit touched `index.ts` and the tests); a fresh-context scout walked the code independently and ranked the same two friction points first. Report: three candidates, a Coherence note, one top recommendation. - **Candidate 2, retire the tier strip.** The catalog tier now rides on the model as the `catalogTier` property (`TieredModel = Model & { catalogTier?: Tier }`, written by `toModel`, read by `tierOfModel`). `TIER_HEADER`, `untier`, `withoutTierHeader` and the adapter around pi's stream are deleted; `api` hands `openAICompletionsApi()` through untouched. The 35-line strip test is gone; the `toModel` test pins that a tiered model has **no** `headers`. - **Candidate 1, poll loop testable through `login`.** `login(interaction, clock = realClock)`; `Clock = { now, sleep }`; `realClock` wraps `node:timers/promises` + `Date.now`; `auth.oauth.login` still passes only the interaction. `login` is exported. Five tests drive it through `driveLogin` with an auto-advancing clock: pending → `slow_down` → success (`[5000, 10000]`, one `device_code` event), missing interval → 5000, `0.5` → 1000 floor, deadline cap (`[5000, 5000, 2000]` then "Nous login timed out."), `expired_token` propagates without sleeping; the sixth test uses the real clock and a bare `fetch` mock: a non-OK device-code response throws with its status. - **Candidate 3, catalog switch owner.** Parked (see Decisions). - **Closeout.** Root `AGENTS.md` Architecture bullets, `tests/AGENTS.md` export list and clock contract, `CHANGELOG.md` `### Changed` with the upgrade note. `npm run check`: `tsc` clean, 25/25 tests (was 20). ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Tier carried as a model property, not as `Model.headers["x-nous-catalog-tier"]` | pi forwards only `model.headers` into requests (pi-ai `dist/models.js:361,450`; coding-agent `dist/core/model-runtime.js:476`) but persists whole entries and restores them as-is: pi-ai `createProvider.refreshModels` (`dist/models.js:594-619`) sets `dynamicModels` from `context.stored` before any fetch, then persists the refreshed list through `context.publish`. The store is `ModelsStore` (`models-store.js:30`, `/models-store.json`). The header carrier needed four strip points and leaked for two audit rounds; a property needs none. A `tsc` probe against the repo's `paths` exited 0, with and without a new type. | | One-line `TieredModel` intersection type | `tsc` then checks the write side: the property is written explicitly in a literal typed `TieredModel`, so a misspelled key is an excess-property error. The read side keeps one cast, where it already had one. | | Accept the one-session edge for models persisted by the old version | Nous ignores the old header and the first interactive start rewrites the store entry (confirmed after the fact: 0 legacy headers in `models-store.json`). A legacy fallback read would need a removal condition and a verification step for no lasting gain. CHANGELOG tells the user to start pi once interactively after upgrading. The premise recorded at decision time was wrong (see Gotchas): whether a legacy-carrier entry existed locally was never established, because the check read `models.json`. | | Optional clock parameter on `login`, over a hand-rolled mock-timer sleep or a pure wait-decision extraction | Zero-production-change paths are closed: `t.mock.timers` does not capture `node:timers/promises` and jiti does not alias `node:` builtins (both probed on Node 26.12.1). A hand-rolled abortable sleep re-adds code round 1 removed; pure extraction leaves the deadline loop and error propagation untested. Two adapters (real timers, fake clock) make it a real seam, and pi's interface is unchanged. | | Auto-advancing fake clock (`sleep` adds its ms to `now`) | The deadline falls out of the recorded sleeps, which pins the round-3 cap exactly. A hand-advanced clock was rejected as ceremony. | | Candidate 3 parked | Deletion test: `apply` already concentrates the rule (two commands, one implementation); the three fixed bugs were ordering and await mistakes a new owner would not have prevented; the `drive()` harness weight is unchanged. Do not re-raise without new evidence. | | No `GLOSSARY.md`, no ADR | "Catalog tier" is already defined in root `AGENTS.md`; the carrier is easy to reverse and ADR-0001 is untouched. The round-3 devlog row is superseded by this log, not rewritten. | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | The whole `index.ts` edit was rejected, nothing written | Two `LINE:HASH` anchors mistyped by hand for lines 310 and 313 | Re-sent with the hashes from the read output; the edit tool rejects atomically, so no partial state | | `login` treated as already exported in the plan | Misread of line 112 (`async function login`, no `export`) | The export is part of the new signature; `tests/AGENTS.md` lists it | | Persisted-model check read the wrong file | `~/.pi/agent/models.json` is the user's overrides (`ModelConfig`); the dynamic catalog that `createProvider` restores and persists is `~/.pi/agent/models-store.json` (`ModelsStore`). The earlier recipe (`grep -c x-nous-catalog-tier ~/.pi/agent/models.json`) therefore proved nothing | After the user's test, read `models-store.json`: 0 `x-nous-catalog-tier`, 45 `catalogTier`. Recipe and root `AGENTS.md` now name the right file | ## Files Changed | File | Change Summary | |------|---------------| | `index.ts` | `Tier`, `TieredModel`; `toModel` writes `catalogTier`; `tierOfModel` reads it; `TIER_HEADER`, `untier`, `withoutTierHeader` and the then-unused `ProviderStreams` import deleted; `api` passes pi's stream through. internal `Clock` and `realClock`; `login(interaction, clock)` exported; `auth.oauth.login` wraps it. Net +20 −36. | | `tests/index.test.mjs` | Strip test deleted; `toModel` tier test retargeted to the property with `headers === undefined`; `drive()` helper uses `catalogTier`; `driveLogin` + six `login` tests (five on the fake clock); the fetch mock throws after 100 token polls so a sleep-less regression fails instead of hanging | | `AGENTS.md` | Architecture: carrier bullet rewritten; `login` clock bullet added; session-log window refreshed | | `tests/AGENTS.md` | Purpose mentions the poll loop and no wall-clock time; export list; `login` clock and `driveLogin` contract; why mock timers and jiti aliasing cannot be used | | `CHANGELOG.md` | `### Changed`: carrier move and the upgrade note | | `docs/devlog/20261125-architecture-review-tier-property-and-login-clock_log.md`, `docs/devlog/INDEX.md` | This log and the index row | ## Open Items & Next Steps - [x] Live check of the carrier: done by the user on 2026-11-25 — toggled full, free and promo, restarted with free active, `/model` listed only free models, then returned to full (which removes the flag file). On disk afterwards: `~/.pi/agent/models-store.json` `nous-portal` entry with 409 models, 45 `catalogTier` (3 free, 42 promo), 0 `x-nous-catalog-tier`, 0 models with `headers`, `checkedAt` 2026-11-25T10:58:50Z. - [x] Mutation check of the six poll-loop tests, run the same day in a scratch copy (temp dir with `index.ts`, `tests/`, `node_modules` symlinked; the repo file was never touched). Six mutations — `slow_down` bump removed, interval fallback 5 → 1, 1 s floor removed, deadline cap removed, non-retryable error swallowed, non-OK device-code check removed — each failed exactly its own test (1 fail / 24 pass, baseline 25/25). - [x] Committed in the closeout as two atomic commits, each gated on its own tree: `cb5aecf` (carrier move, `refactor:`, 19/19) and `544af1e` (clock seam + tests, `test:`, 25/25). - [ ] Delete the review report from `$TMPDIR` when no longer needed. --- *Log written by write-log skill*