Files
orangecat/scripts/eval-rate-limit.mjs
CatoandClaude Fable 5.1 e2f8779837 feat(cat): ask for permission by name, check its own record, and select sections by turn (#967)
ADR-0006 D5, D6, and the section-selection mechanism — the three items left
open after #964 — plus the fix that lets the eval that gates them actually run.

D5, a first run that can do something. Most of it had already shipped as
grant-on-confirm (20260910203000_confirming_can_also_allow.sql): a denial for a
non-payment, non-high-risk action becomes a consent card reading "Allow and
confirm". What was missing was the other half of the conversation. The model
was told a plain "waiting for confirmation", so its reply announced a done deal
while the card asked for a permission the reply never mentioned. summariseForModel
now says NOT YET ALLOWED, names the category, and says one tap allows it; a hard
denial names the category to allow instead of "tell the user what to grant"
with nothing to say.

Two measured corrections with it. update_profile sat in the default-on 'context'
category with requiresConfirmation: false — a brand-new user's Cat could rewrite
their public @handle and bio on a stray sentence. It is now 'entities' and
confirmed, and reaches a new user through the consent card. And 'settings' was a
permission category with zero actions in it: a switch in the UI governing
nothing. Removed from the app; the Postgres enum value is left orphaned (cannot
be dropped in place, no row references it). The class is closed by a test —
every listed category must govern at least one enabled action.

D6, Cat can check its own work. track-record.ts already derived proposed →
published → funded plus setbacks from cat_action_log; it was ambient context
only. It is now also the read tool check_my_track_record, answered as prose by
formatTrackRecordForModel, which states Cat's own pattern as a fact to act on:
"3 things drafted, none published — offer to finish ONE before creating
anything new"; "2 published but nothing funded — the gap is reach or pricing";
"2 proposals never confirmed — ask what held them back". A null record says
"could not be read — do not reconstruct from memory", never "nothing yet".
Scoped to Cat's own actions in the router; the user's own numbers stay with
query_my_data.

Section selection, mechanism built, flag off. Two things were found by trying
to enable it. Nothing anywhere produced a turnDescriptor — the env flag was a
double gate that changed nothing when set. services/cat/turn-descriptor.ts now
builds one in chat-prepare from facts the caller has: the message, whether
history is empty (fetched before the brief now, so first-message is read not
guessed), the page, the entity. And selection ran BEFORE the capability strip,
so flag-on + actionsVia 'none' + a greeting threw: selection had already removed
an instruction section the strip then demanded. Strip runs first now. Measured
on the tools path: unselected 32,019 chars; a first-message greeting 23,097; a
pricing question 18,824.

The eval that is supposed to validate the flip has not run since 2026-09-10. It
logged "free-model budget 0/0 remaining — SKIPPED" every night. From the box:
OpenRouter answers HTTP 200 on the free model and sends NO x-ratelimit-*
headers; Number(null) is 0 and 0 is finite, so a missing header read as an
exhausted budget and the eval stood down to protect a reserve that was never in
danger — the exact thing the comment above that code says it must not do.
Header parsing is now scripts/eval-rate-limit.mjs, absent means unknown means
proceed, and the run logs when the budget is unknown so silence cannot read as
"checked". Unit-tested.

Gates proved by mutation: 16 mutants, 16 killed, sources sha256-verified
restored. Three survived the first pass and were diagnosed rather than
excused: two had matched update_entity's identical four-line block before
update_profile's (re-anchored on a unique comment), one kept both phrases the
test looked for (test now asserts a null record never reads "nothing yet").

Not built, stated in the ADR: the loop does not resume after confirmation; the
card confirms, the action runs, a system message reports it.


Claude-Session: https://claude.ai/code/session_01MWgvHV9KSt3nLn812C2fxm

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 23:57:17 -07:00

37 lines
1.6 KiB
JavaScript

/**
* Read OpenRouter's rate-limit headers into a budget, or admit you cannot.
*
* Extracted from scripts/eval-cat.mjs because it hid a bug that silenced the
* nightly Cat eval for days: `Number(headers.get('x-ratelimit-remaining'))`
* turns a MISSING header into 0, and 0 is finite, so "OpenRouter sent no
* rate-limit headers" was read as "0 of 0 requests remaining" and the eval
* stood down every night to protect a reserve that was not in danger.
* Measured 2026-09-11 from the box: HTTP 200, the free model answering, and no
* x-ratelimit-* header on the response at all.
*
* Unknown is not the same as empty. Absent headers -> null -> the run proceeds.
* A pure function so it can be tested without the script's env preconditions.
*
* @param {{ get(name: string): string | null }} headers
* @returns {{ limit: number | null, remaining: number, reset: number | null } | null}
*/
export function readRateLimitHeaders(headers) {
const rawRemaining = headers.get('x-ratelimit-remaining');
if (rawRemaining === null || rawRemaining === undefined || rawRemaining === '') {
return null;
}
const remaining = Number(rawRemaining);
if (!Number.isFinite(remaining)) {
return null;
}
const rawLimit = headers.get('x-ratelimit-limit');
const rawReset = headers.get('x-ratelimit-reset');
const limit = rawLimit === null || rawLimit === '' ? null : Number(rawLimit);
const reset = rawReset === null || rawReset === '' ? null : Number(rawReset);
return {
limit: Number.isFinite(limit) ? limit : null,
remaining,
reset: Number.isFinite(reset) ? reset : null,
};
}