Files
orangecat/scripts
CatoandClaude Opus 5.5 a70f1ad61f fix(ai, companions): a strict daily cap on the platform's paid vendor calls; a bot runs on the person's own key; a failed send says the way out (#1672)
* fix(companions): a bot the shared AI tier cannot answer says so, with the way out, and the diagnostics can see it

On 2026-11-12 every bot send on the live site answered 502 for hours: one
link of the shared chain had been switched off and the free links behind it
were spent. A person read "That did not go through. Try again.", which led
nowhere, and the Cat diagnostics workflow read "0 failures", because it
counted the Cat's turns and nothing else.

- services/ai/chain-failure-summary: the sentence ("The free AI tier could
  not answer just now. Try again shortly, or add your own API key in
  Settings → AI: your bots then answer on it.") and a summary of the failed
  links as vendor and HTTP status only.
- The bot turn returns AI_ERROR with sharedPool, the route passes it on, and
  the chat shows the server's line with the "Use your own key" button, the
  same block the day's spent free messages already use.
- The turn logs the summary under companions/chat;
  scripts/diagnose-cat-logs.mjs counts those turns and their links, so the
  workflow answers "which link, which status" without printing journal text.

A person's own key is asked first and never reaches the pool, so the way out
holds whatever the pool does.

Tests: __tests__/unit/ai/chain-failure-summary.test.ts,
__tests__/unit/cat/journal-diagnostics.test.ts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(companions): a bot turn runs on the person's own key from any provider they can add

The failed-send sentence says "add your own API key in Settings → AI: your
bots then answer on it". A review found that false for six of the seven
providers the settings page offers: a bot turn read one key only, an
OpenRouter key marked primary. A person who added a Groq, Anthropic, OpenAI,
Together, DeepSeek or xAI key, or whose OpenRouter key was no longer the
primary, still had every bot turn sent to the shared free pool, and the
"Use your own key" button led back to the same failure.

- services/ai/own-key: the Cat's own rule. The person's valid keys in the
  order they saved them, the first one this app can call; OpenRouter keeps
  its routed client, every other provider is called at its own address with
  its default model.
- sendMessage reads that key for both the turn and the work loop's steps; a
  turn on a direct key is an own-key turn (no free allowance spent).

Tests: __tests__/unit/ai/own-key.test.ts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(companions): every bot path reads the person's own key the same way, and the lookup cannot reach a model

After the previous commit a second reviewer found the promise still false on
two of the three bot paths: the group chat and the routine went on reading
only a primary OpenRouter key.

- own-key.ts readOwnKey: one reader for the one-to-one turn, the group chat
  (rooms/respond, which now passes the direct key on), the routine runner
  and the routine's settings row.
- The lookup and the client are two files. The routine's settings row is a
  page load, and no GET may be able to reach a model client
  (__tests__/unit/ci/no-background-ai caught the first version of this
  commit). own-key.ts imports no client; own-key-service.ts builds one, for
  the turn alone.
- The test walks the settings form's own list of providers
  (WIRED_PROVIDER_IDS), so a provider added there without a client here
  fails it.

The whole unit suite: 686 files, 5568 tests.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* test(companions): the own-key test checks the provider's host exactly

CodeQL flagged a pattern on a URL without anchors (it matches other hosts
too). The test now compares the parsed hostname.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* feat(ai): a strict daily cap on every call the platform's own key makes at a paid vendor

The Gemini API was switched off on 2026-11-12 to stop the spend, and every
bot on the shared pool stopped answering with it. The owner's rule: a vendor
may be on only with a strict spending cap.

- supabase/migrations/20261112200000: platform_vendor_daily_calls and
  reserve_vendor_call(vendor, cap), which counts a call BEFORE it is made and
  refuses once the UTC day's cap is reached, atomically. Service role only.
- services/ai/vendor-spend-cap: the platform's Gemini key is capped at
  GEMINI_DAILY_CALL_CAP calls a day (300 when unset; 0 turns it off). A call
  is refused when the count cannot be read: unknown is never room. A refusal
  reads as a 429, so the chain moves to the next link.
- Applied on every path that calls a vendor with the platform key: the chat
  and bot clients (buildPlatformProviders), the Cat's tool loop and the
  health probe. A person's own key is never counted.

Tests: __tests__/unit/ai/vendor-spend-cap.test.ts (includes a check that every
vendor key the chain can dial on the platform's account is capped). The whole
unit suite: 691 files, 5622 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* feat(ai): every platform-paid vendor call is capped per day, and the per-person limit refuses when it cannot read its count

An audit after the Gemini spend found the cap covered Gemini only:

- Groq, OpenRouter and Together are capped too (3000, 1000 and 300 calls a
  day unless set). They are free tiers today, but a free key bills the day
  its account is upgraded, and OpenRouter's key also serves paid models.
- Embeddings on a paid host (5000 a day; a server on this machine is not
  counted) and every web search (1000 a day) are counted before the call.
  A refused embedding falls back to keyword search; a refused search says
  "could not search".
- checkPlatformUsage (10 free messages a day per person) let every message
  through when its database check failed. It refuses now: unknown is never
  room.

Each cap is an env var (<VENDOR>_DAILY_CALL_CAP); 0 turns the vendor off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(auth): "Start instantly" sends the captcha token with the sign-in

A pseudonymous account gets the free daily allowance, so accounts made by a
script multiply it. The page already holds a Turnstile token for the other
sign-ins; the anonymous sign-in now sends it too. It is enforced only where
the auth server requires a captcha.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 19:08:12 +01:00
..
2025-12-10 16:56:40 +01:00

Scripts Directory

This directory contains all automation scripts organized by purpose.

Directory Structure

  • analysis/ - Bundle analysis, documentation checking, and optimization tools
  • auth-fix/ - Scripts to fix authentication and RLS policy issues
  • db/ - Database operations, migrations, and schema management
    • Migration scripts moved from root: apply-migration.js, apply-*-migration.js
  • deployment/ - Deployment, production setup, and monitoring scripts
  • dev/ - Development helpers, local setup, and debugging tools
  • diagnostics/ - Health checks and system diagnostics
  • maintenance/ - Cleanup, optimization, and maintenance scripts
  • monitoring/ - Performance monitoring and bundle size tracking
  • oauth/ - OAuth provider setup and configuration
  • profile-fix/ - Scripts to fix profile-related issues
  • schema-fix/ - Database schema fixes and migrations
  • storage/ - Supabase storage bucket setup and management
  • test/ - Testing utilities and validation scripts
    • Test/debug scripts moved from root: test-*.js, check-*.js, debug-*.js

Usage

Most scripts can be run with:

node scripts/{category}/script-name.js

For deployment scripts:

bash scripts/deployment/script-name.sh

Important Scripts

  • scripts/dev/dev-start.js - Main development server starter
  • scripts/deployment/browser-verify.js - Post-deploy browser smoke test (deploys run from .github/workflows/cd.yml via scripts/deploy-selfhost.sh — see docs/operations/deployment/DEPLOYMENT_PROCESS.md)
  • scripts/apply-migrations.sh - Applies pending supabase/migrations/*.sql on every deploy (run by scripts/deploy-selfhost.sh). scripts/db/apply-migrations.ts is a legacy script, not the deploy path.
  • scripts/maintenance/cleanup-console-logs.ts - Remove console.log statements