fix: address round-3 review findings
- Strip the catalog-tier header from request options too. pi copies model.headers into options.headers before calling the provider (pi-ai getAuth -> prepareRequest) and the client applies options.headers last, so the header still reached Nous on every free/promo request; the old test pinned the leak. Both sides pinned. - Drop the 'Press Enter' prompt in OAuth login: it cleared pi's device-code confirm action (login-dialog showPrompt), so Enter opened nothing. pi's own dialog shows the code and opens the browser. - Poll interval rejects non-finite values (Node clamps an overflowing setTimeout to 1 ms) and each sleep is capped at the deadline, matching pi-ai's poller. - Tests: 17 (was 14). /nous-free back to full, /nous-catalog cycling, and startup restore of a saved catalog are covered. - Docs: README says the live list overlays the baseline by id (baseline ids that vanish stay listed); AGENTS.md states both strips.
This commit is contained in:
1 parent
b9065d409c
commit
8d91094022
4 files changed
+75
-24
No files matched your search
@@ -128,12 +128,16 @@ async function login(interaction: ProviderAuthInteraction): Promise<OAuthCredent
|
||||
expiresInSeconds: device.expires_in,
|
||||
};
|
||||
interaction.notify(event);
|
||||
// pi shows the code and opens the browser on Enter; the prompt resolves once the user confirms.
|
||||
await interaction.prompt({ type: "text", message: "Press Enter to open the Nous Portal sign-in page." });
|
||||
// pi's dialog shows the code and opens the browser on Enter. Do NOT prompt here: a prompt clears that
|
||||
// confirm action (login-dialog.js showPrompt → clearDeviceCodeAction), so Enter would open nothing.
|
||||
|
||||
const deadline = Date.now() + device.expires_in * 1000;
|
||||
// Floor at 1 s like pi-ai's own poller: a missing, zero or non-numeric interval must not become a tight loop.
|
||||
let interval = Math.max(Number(device.interval) || 5, 1);
|
||||
// Floor at 1 s and reject non-finite values like pi-ai's own poller: a missing, zero, non-numeric or
|
||||
// overflowing interval must not become a tight loop (Node clamps an overflowing setTimeout to 1 ms).
|
||||
const requested = Number(device.interval);
|
||||
let interval = Number.isFinite(requested) && requested > 0 ? Math.max(requested, 1) : 5;
|
||||
// Never sleep past the deadline.
|
||||
const nap = () => sleep(Math.min(interval * 1000, deadline - Date.now()), undefined, { signal });
|
||||
while (Date.now() < deadline) {
|
||||
try {
|
||||
return toCredential(
|
||||
@@ -143,10 +147,10 @@ async function login(interaction: ProviderAuthInteraction): Promise<OAuthCredent
|
||||
if (!(error instanceof OAuthError)) throw error;
|
||||
// RFC 8628: pending → wait the server's interval; slow_down → wait longer. Never poll again without sleeping,
|
||||
// or a repeat slow_down turns into a tight loop against the token endpoint. The deadline bounds the total.
|
||||
if (error.code === "authorization_pending") await sleep(interval * 1000, undefined, { signal });
|
||||
if (error.code === "authorization_pending") await nap();
|
||||
else if (error.code === "slow_down") {
|
||||
interval += 5;
|
||||
await sleep(interval * 1000, undefined, { signal });
|
||||
await nap();
|
||||
} else throw error;
|
||||
}
|
||||
}
|
||||
@@ -235,20 +239,22 @@ export async function fetchModels(credential: Credential | undefined, signal: Ab
|
||||
return (payload.data ?? []).filter((m) => m.id && !/hermes/i.test(m.id)).map(toModel);
|
||||
}
|
||||
|
||||
// The tier header is metadata only; strip it before the request so it never reaches Nous.
|
||||
// The tier header is metadata only; strip it before the request so it never reaches Nous. pi copies
|
||||
// model.headers into options.headers before calling the provider (pi-ai getAuth → prepareRequest) and the
|
||||
// client applies options.headers last, so BOTH the model and the options must be stripped.
|
||||
// (Nous free/discounted models 500 on an injected `tags.user` body field as of 2026-11-25, so no body
|
||||
// rewriting happens here — if that changes, wrap stream/streamSimple and chain options.onPayload.)
|
||||
const untier = (model: Model<Api>): Model<Api> => {
|
||||
if (!model.headers?.[TIER_HEADER]) return model;
|
||||
const headers = { ...model.headers };
|
||||
// rewriting happens here — if that changes, chain options.onPayload.)
|
||||
const untier = <T extends { headers?: Record<string, string | null> } | undefined>(value: T): T => {
|
||||
if (!value?.headers?.[TIER_HEADER]) return value;
|
||||
const headers = { ...value.headers };
|
||||
delete headers[TIER_HEADER];
|
||||
return { ...model, headers };
|
||||
return { ...value, headers };
|
||||
};
|
||||
|
||||
export function withoutTierHeader(api: ProviderStreams): ProviderStreams {
|
||||
return {
|
||||
stream: (model, context, options) => api.stream(untier(model), context, options),
|
||||
streamSimple: (model, context, options) => api.streamSimple(untier(model), context, options),
|
||||
stream: (model, context, options) => api.stream(untier(model), context, untier(options)),
|
||||
streamSimple: (model, context, options) => api.streamSimple(untier(model), context, untier(options)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user