refactor: carry catalog tier on the model
Move the catalog tier from the x-nous-catalog-tier request header to a catalogTier property on each model. pi forwards only model.headers to the provider and copies them into the request options, so a header needed stripping on both the model and the options before every request; that adapter leaked for two audit rounds. A property never reaches the wire, survives pi's model store, and needs no wrapper, so TIER_HEADER, untier and withoutTierHeader are deleted and pi's stream passes through untouched. Models stored by the previous version come back without the property until the next interactive start rewrites the store; the CHANGELOG tells the user to start pi once interactively after upgrading.
This commit is contained in:
1 parent
a1735049b7
commit
cb5aecf815
5 files changed
+26
-73
No files matched your search
+1
-1
@@ -15,7 +15,7 @@ Offline unit tests for `index.ts`. They pin the pure logic that has real branchi
|
||||
- `index.test.mjs` loads `index.ts` through jiti, the same TS loader pi uses, because the host packages are not installed in `node_modules`.
|
||||
- Host imports are aliased to `stubs/pi-ai.mjs`. Alias keys are matched longest-prefix first: `@earendil-works/pi-ai/compat` must be listed before `@earendil-works/pi-ai`, or the bare key swallows the subpath and resolution fails.
|
||||
- `stubs/pi-ai.mjs` exports only what `index.ts` imports: `envApiKeyAuth`, `createProvider`, `openAICompletionsApi`, `getAgentDir` (plus `agentDir` so tests can find the flag file). `getAgentDir` is called inside the default export, so it is reached only by tests that drive the commands; it returns a private `mkdtemp` dir per run, shared through `globalThis` because jiti loads the stub as a separate instance from a native import. It is a type-less stub; correctness of those functions is pi's concern and is verified live, not here.
|
||||
- Exported functions under test: `toModel`, `toCredential`, `tokenRequest`, `fetchModels`, `tierOf`, `withoutTierHeader`, and the default export via the `drive()` harness. `drive(flag)` returns `run(name, model, available, refresh)` — pass a deferred promise as `refresh` to simulate overlapping command invocations, and a Nous `model` when the `setModel` half of a contract is under test — plus the registered `provider`, which is how the provider-level `fetchModels` last-good retention is reached. Anything else is not unit-tested (notably the device-code poll loop inside `login`, which would need timer control and extraction).
|
||||
- Exported functions under test: `toModel`, `toCredential`, `tokenRequest`, `fetchModels`, `tierOf`, and the default export via the `drive()` harness. `drive(flag)` returns `run(name, model, available, refresh)` — pass a deferred promise as `refresh` to simulate overlapping command invocations, and a Nous `model` when the `setModel` half of a contract is under test — plus the registered `provider`, which is how the provider-level `fetchModels` last-good retention is reached. Anything else is not unit-tested (notably the device-code poll loop inside `login`, which would need timer control and extraction).
|
||||
- Network calls are replaced with `t.mock.method(globalThis, "fetch", …)` from `node:test`, which restores itself when the test ends. Do not hand-roll a `globalThis.fetch` swap.
|
||||
- `fetchModels` reads a key only from the `credential` argument. Env resolution of `$NOUS_API_KEY` is `envApiKeyAuth`'s job upstream. The one test that touches `process.env` sets it deliberately, to pin that `fetchModels` ignores it; it restores the previous value in `t.after`.
|
||||
|
||||
|
||||
+8
-40
@@ -17,7 +17,7 @@ const jiti = createJiti(import.meta.url, {
|
||||
},
|
||||
});
|
||||
const extension = await jiti.import("../index.ts");
|
||||
const { toModel, toCredential, tokenRequest, fetchModels, tierOf, withoutTierHeader } = extension;
|
||||
const { toModel, toCredential, tokenRequest, fetchModels, tierOf } = extension;
|
||||
// The stub shares its private agent dir through globalThis (see stubs/pi-ai.mjs); the catalog flag lives there.
|
||||
const { agentDir } = await import("./stubs/pi-ai.mjs");
|
||||
const flagPath = join(agentDir, "nous-portal-catalog");
|
||||
@@ -43,49 +43,17 @@ test("tierOf: free by synthesizedFreeVariant or :free suffix, promo only when pr
|
||||
assert.equal(tierOf({ id: "x" }), undefined);
|
||||
});
|
||||
|
||||
test("toModel: the catalog tier rides on the model as the tier header (pi persists and restores model objects)", () => {
|
||||
test("toModel: the catalog tier is a model property, never a header, so nothing needs stripping before a request", () => {
|
||||
const free = toModel({ id: "upstage/solar-mini4:free" });
|
||||
assert.equal(free.headers["x-nous-catalog-tier"], "free");
|
||||
assert.equal(free.catalogTier, "free");
|
||||
assert.equal(free.headers, undefined);
|
||||
assert.deepEqual(free.input, ["text"]);
|
||||
assert.equal(free.reasoning, false);
|
||||
assert.equal(free.cost.input, 0);
|
||||
assert.equal(free.contextWindow, 131072);
|
||||
});
|
||||
|
||||
test("withoutTierHeader: strips the tier header from model AND options on both stream entry points; everything else untouched", async () => {
|
||||
const calls = [];
|
||||
const api = {
|
||||
stream: (m, c, o) => (calls.push(["stream", m, c, o]), "s"),
|
||||
streamSimple: (m, c, o) => (calls.push(["streamSimple", m, c, o]), "ss"),
|
||||
};
|
||||
const wrapped = withoutTierHeader(api);
|
||||
const tiered = { id: "m", headers: { "x-nous-catalog-tier": "free", "x-keep": "1" } };
|
||||
const ctx = { messages: [] };
|
||||
// pi copies model.headers into options.headers before calling the provider, so options carry the tier too.
|
||||
const opts = { apiKey: "k", headers: { "x-nous-catalog-tier": "free", "x-keep": "1" } };
|
||||
assert.equal(await wrapped.stream(tiered, ctx, opts), "s");
|
||||
assert.equal(await wrapped.streamSimple(tiered, ctx, opts), "ss");
|
||||
for (const [entry, model, , options] of calls) {
|
||||
assert.equal(model.headers["x-nous-catalog-tier"], undefined, entry + " model");
|
||||
assert.equal(model.headers["x-keep"], "1", "other model headers survive");
|
||||
assert.notEqual(model, tiered, "original model object is not mutated");
|
||||
assert.equal(options.headers["x-nous-catalog-tier"], undefined, entry + " options");
|
||||
assert.equal(options.headers["x-keep"], "1", "other option headers survive");
|
||||
assert.equal(options.apiKey, "k");
|
||||
assert.notEqual(options, opts, "stripped options are a copy");
|
||||
assert.equal(tiered.headers["x-nous-catalog-tier"], "free", "caller's model is not mutated");
|
||||
assert.equal(opts.headers["x-nous-catalog-tier"], "free", "caller's options are not mutated");
|
||||
}
|
||||
// An untiered model is passed through by identity; tiered options are still stripped.
|
||||
const plain = { id: "p" };
|
||||
wrapped.stream(plain, ctx, opts);
|
||||
assert.equal(calls[2][1], plain);
|
||||
assert.equal(calls[2][3].headers["x-nous-catalog-tier"], undefined);
|
||||
// Nothing to strip on either side → both by identity.
|
||||
const bare = { apiKey: "k" };
|
||||
wrapped.stream(plain, ctx, bare);
|
||||
assert.equal(calls[3][1], plain);
|
||||
assert.equal(calls[3][3], bare);
|
||||
const promo = toModel({ id: "google/gemini-4-carbon", pricing: { prompt: "0.000002", original: { prompt: "0.000004" } } });
|
||||
assert.equal(promo.catalogTier, "promo");
|
||||
assert.equal(toModel({ id: "openai/gpt-5.4-nano" }).catalogTier, undefined, "untiered models carry no tier");
|
||||
});
|
||||
|
||||
test("toCredential: keeps the previous refresh token when the server rotates only the access token", () => {
|
||||
@@ -176,7 +144,7 @@ const drive = (flag) => {
|
||||
});
|
||||
return { run, provider, switched, notes };
|
||||
};
|
||||
const nous = (id, tier) => ({ id, provider: "nous-portal", ...(tier && { headers: { "x-nous-catalog-tier": tier } }) });
|
||||
const nous = (id, tier) => ({ id, provider: "nous-portal", ...(tier && { catalogTier: tier }) });
|
||||
const other = (id) => ({ id, provider: "anthropic" });
|
||||
|
||||
test("/nous-free: a session whose model survives the filter is left alone, and the notice names that model", async () => {
|
||||
|
||||
Reference in new issue
Block a user