feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Seat usage: the pure functions against reference vectors, numbered in
// order, so "check 17" always names the same vector.
feat(seats): an API seat's cycle starts on its own grant day
The ledger reset every API seat's spend on one
REVIEW_GATES_API_CYCLE_DAY and cleared every credit-out, an OpenRouter
key's too, at that reset. Each API seat's cycle starts on its
ANTHROPIC_API_KEY<n>_GRANT_DAY now (1..31, a day past a month's end its
last), else the cycle day, also 1..31. A credit-out carries across the
rollover, and the run's probe tries a key held out on credit again, as
`seats` does an API key: an ok lifts the mark - an API key's is new
credits, its cycle restarting from the grant day, or from today when
that day is over a week back; an OpenRouter key keeps its cycle's spend,
and a key that answers with its cycle's budget spent stays out on that
budget. A probe that finds a key out of credit records it, unless the
ledger already has it out. Both are decided in one locked update on the
file's entry, a mark another driver made since the probe started
standing, and the ledger's stamps carry their UTC offset so marks are
ordered by instant.
`seats` shows the probe's reading beside the driver's verdict (the
`probe` and `driver` columns replace `state` and `ledger: out`), each
seat's grant day and when its next credits are due, and notes on what
the probe changed in the ledger or why the driver's verdict stands; the
run logs the days.
2026-11-02 13:58:29 +01:00
# include <stdlib.h>
# include <time.h>
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
import std ;
import Crafter . Review ;
namespace fs = std : : filesystem ;
using namespace Crafter : : Review ;
using namespace Crafter : : Review : : Seats ;
namespace {
std : : int32_t Failures = 0 ;
std : : int32_t N = 0 ;
void Eq ( const std : : string & got , const std : : string & want ) {
+ + N ;
if ( got ! = want ) {
std : : println ( std : : cerr , " check {}: \n got: {} \n want: {} " , N , Dyn : : Repr ( got ) , Dyn : : Repr ( want ) ) ;
+ + Failures ;
}
}
void Eq ( bool got , bool want ) {
Eq ( std : : string ( got ? " True " : " False " ) , std : : string ( want ? " True " : " False " ) ) ;
}
void EqJson ( const Json & got , const Json & want ) {
+ + N ;
if ( ! Dyn : : Equal ( got , want ) ) {
std : : println ( std : : cerr , " check {}: \n got: {} \n want: {} " , N , DumpCompactSorted ( got ) , DumpCompactSorted ( want ) ) ;
+ + Failures ;
}
}
std : : vector < std : : string > Lines ( const std : : string & text ) {
return Dyn : : Split ( text , " \n " ) ;
}
Json With ( Json value , const Json & changes ) {
for ( auto it = changes . begin ( ) ; it ! = changes . end ( ) ; + + it ) value [ it . key ( ) ] = it . value ( ) ;
return value ;
}
}
int main ( ) {
feat(seats): an API seat's cycle starts on its own grant day
The ledger reset every API seat's spend on one
REVIEW_GATES_API_CYCLE_DAY and cleared every credit-out, an OpenRouter
key's too, at that reset. Each API seat's cycle starts on its
ANTHROPIC_API_KEY<n>_GRANT_DAY now (1..31, a day past a month's end its
last), else the cycle day, also 1..31. A credit-out carries across the
rollover, and the run's probe tries a key held out on credit again, as
`seats` does an API key: an ok lifts the mark - an API key's is new
credits, its cycle restarting from the grant day, or from today when
that day is over a week back; an OpenRouter key keeps its cycle's spend,
and a key that answers with its cycle's budget spent stays out on that
budget. A probe that finds a key out of credit records it, unless the
ledger already has it out. Both are decided in one locked update on the
file's entry, a mark another driver made since the probe started
standing, and the ledger's stamps carry their UTC offset so marks are
ordered by instant.
`seats` shows the probe's reading beside the driver's verdict (the
`probe` and `driver` columns replace `state` and `ledger: out`), each
seat's grant day and when its next credits are due, and notes on what
the probe changed in the ledger or why the driver's verdict stands; the
run logs the days.
2026-11-02 13:58:29 +01:00
// The API report's dates (NextGrant, its "today") are the process zone's,
// as for dt.date.fromtimestamp: the vectors below are UTC's.
setenv ( " TZ " , " UTC " , 1 ) ;
tzset ( ) ;
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
const Zone utc = ZoneNamed ( " UTC " ) ;
const Zone ams = ZoneNamed ( " Europe/Amsterdam " ) ;
// 2026-09-07 10:00 UTC
constexpr double now = 1788775200.0 ;
const auto nowS = static_cast < std : : int64_t > ( now ) ;
const Json cfg = DefaultConfig ( ) ;
auto seat = [ & ] ( std : : int32_t k , double five , double seven = 0 , const Json & over = Json : : object ( ) ) {
Json windows = { { " five_hour " , { { " utilization " , five } , { " resetsAt " , nowS + 300 * k } } } , { " seven_day " , { { " utilization " , seven } , { " resetsAt " , nowS + 3 * 86400 } } } } ;
// An integer stays an integer.
for ( auto & w : windows ) {
const double u = w . at ( " utilization " ) . get < double > ( ) ;
if ( u = = std : : floor ( u ) ) w [ " utilization " ] = static_cast < std : : int64_t > ( u ) ;
}
const auto [ util , binding ] = SeatUtil ( windows ) ;
Json s = { { " seat " , k } , { " state " , " ok " } , { " windows " , windows } , { " util " , util } , { " binding " , binding } , { " resetsAt " , windows . at ( " five_hour " ) . at ( " resetsAt " ) } } ;
return With ( s , over ) ;
} ;
const std : : string fable ( FableWindow ) ;
// format_when: the clock in the report zone and a countdown; a reset over
// a day out carries its date.
Eq ( FormatWhen ( nowS + 45 * 60 , now , utc ) , " 10:45 (in 45 min) " ) ;
Eq ( FormatWhen ( nowS + 3 * 3600 + 5 * 60 , now , ams ) , " 15:05 (in 3h 05m) " ) ;
Eq ( FormatWhen ( nowS + 3 * 86400 , now , utc ) , " Thu 10 Sept, 10:00 (in 3d 0h) " ) ;
Eq ( FormatWhen ( nowS - 60 , now , utc ) , " 09:59 (now) " ) ;
// The report: a row per seat, columns padded to line up; who is eligible
// and where the next lens lands.
const Json lim = seat ( 1 , 116 , 21 , { { " state " , " limited " } , { " util " , 115 } , { " error " , " five_hour rejected, overage out_of_credits " } } ) ;
Json three = seat ( 3 , 62 , 16 ) ;
three [ " windows " ] [ fable ] = { { " utilization " , 22 } , { " resetsAt " , 0 } } ;
std : : vector < std : : string > lines = Lines ( RenderStatusReport ( { lim , seat ( 2 , 61 , 18 ) , three } , cfg , now , utc ) ) ;
Eq ( lines . at ( 0 ) , " | seat | 5h | resets | 7d | resets | fable 7d | " ) ;
Eq ( lines . at ( 1 ) , " |------|------|-------------------|-----|-------------------------------|----------| " ) ;
Eq ( lines . at ( 2 ) , " | 1 | 116% | 10:05 (in 5 min) | 21% | Thu 10 Sept, 10:00 (in 3d 0h) | — | " ) ;
Eq ( lines . at ( 3 ) , " | 2 | 61% | 10:10 (in 10 min) | 18% | Thu 10 Sept, 10:00 (in 3d 0h) | — | " ) ;
Eq ( lines . at ( 4 ) , " | 3 | 62% | 10:15 (in 15 min) | 16% | Thu 10 Sept, 10:00 (in 3d 0h) | 22% | " ) ;
Eq ( lines . at ( 5 ) , " " ) ;
2026-10-31 21:13:55 +01:00
Eq ( lines . at ( 6 ) , " policy spread · ceiling 85% · handover at 90% · a lens is placed at +10% · 7d cutoff 99% " ) ;
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
Eq ( lines . at ( 7 ) , " eligible now: seat 2 (25% headroom), seat 3 (25% headroom) → next lens: seat 2 " ) ;
Eq ( lines . at ( 8 ) , " fable lenses: seat 3 (63% fable headroom) " ) ;
Eq ( lines . at ( 9 ) , " limited: seat 1 until 10:05 (in 5 min) " ) ;
// A seat the probe could not read: an empty row, the reason under the table.
const std : : string dead = RenderStatusReport ( { seat ( 2 , 10 ) , { { " seat " , 4 } , { " state " , " unknown " } , { " windows " , Json : : object ( ) } , { " error " , " probe timeout after 20000 ms " } } } , cfg , now , utc ) ;
Eq ( dead . contains ( " | 4 | — | — | — | — | — | " ) , true ) ;
// The board: the passes every driver on this machine has in flight on an
// account, a column only when there are any.
const std : : vector < std : : string > held = Lines ( RenderStatusReport ( { seat ( 2 , 61 , 18 ) , three } , cfg , now , utc , { { " seats " , { { " k3 " , { { " inflight " , 4 } } } } } } , { { 3 , " k3 " } } ) ) ;
Eq ( held . at ( 0 ) , " | seat | 5h | resets | 7d | resets | fable 7d | in flight | " ) ;
Eq ( held . at ( 3 ) , " | 3 | 62% | 10:15 (in 15 min) | 16% | Thu 10 Sept, 10:00 (in 3d 0h) | 22% | 4 | " ) ;
Eq ( dead . contains ( " not usable: seat 4 unknown (probe timeout after 20000 ms) " ) , true ) ;
// A Fable probe refused for the Fable window needs the Haiku stand-in,
// which it then lands on: its window, and 'limited' for that window alone.
Json fableWindows = seat ( 2 , 10 ) . at ( " windows " ) ;
fableWindows [ fable ] = { { " utilization " , 100 } , { " resetsAt " , nowS + 86400 } } ;
const Json fableLimited = With ( seat ( 2 , 10 ) , { { " state " , " limited " } , { " util " , 100 } , { " binding " , fable } , { " error " , std : : format ( " {} rejected " , fable ) } , { " windows " , fableWindows } } ) ;
Eq ( NeedsHaiku ( fableLimited ) , true ) ;
const Json merged = MergeFableProbe ( seat ( 2 , 10 ) , fableLimited ) ;
EqJson ( merged . at ( " fable " ) , { { " state " , " limited " } } ) ;
EqJson ( merged . at ( " windows " ) . at ( fable ) , { { " utilization " , 100 } , { " resetsAt " , nowS + 86400 } } ) ;
std : : string r = RenderStatusReport ( { merged } , cfg , now , utc ) ;
Eq ( Lines ( r ) . at ( 2 ) . contains ( " | limited (100%) | " ) , true ) ;
Eq ( r . contains ( " fable lenses: no seat with room on its Fable window — Fable lenses run on their fallback model " ) , true ) ;
Eq ( r . contains ( " eligible now: seat 2 (75% headroom) " ) , true ) ;
// A readable Fable probe is the seat itself.
Json fullWindows = seat ( 3 , 20 , 30 ) . at ( " windows " ) ;
fullWindows [ fable ] = { { " utilization " , 44 } , { " resetsAt " , nowS + 86400 } } ;
const Json full = With ( seat ( 3 , 20 , 30 ) , { { " windows " , fullWindows } } ) ;
Eq ( NeedsHaiku ( full ) , false ) ;
EqJson ( MergeFableProbe ( full , full ) . at ( " fable " ) , { { " state " , " ok " } } ) ;
// A drained Fable window makes an Opus seat, never an idle one.
Json drainedWindows = seat ( 4 , 10 , 20 ) . at ( " windows " ) ;
drainedWindows [ fable ] = { { " utilization " , 98 } , { " resetsAt " , nowS + 86400 } } ;
Json drained = With ( seat ( 4 , 10 , 20 ) , { { " util " , 100 } , { " binding " , fable } , { " windows " , drainedWindows } } ) ;
drained = MergeFableProbe ( drained , drained ) ;
EqJson ( Json : : array ( { drained . at ( " util " ) , drained . at ( " binding " ) , drained . at ( " fable " ) } ) , Json : : array ( { 20 , " seven_day " , { { " state " , " ok " } } } ) ) ;
r = RenderStatusReport ( { drained , full } , cfg , now , utc ) ;
Eq ( r . contains ( " eligible now: seat 4 (65% headroom), seat 3 (55% headroom) → next lens: seat 4 " ) , true ) ;
Eq ( r . contains ( " fable lenses: seat 3 (41% fable headroom) " ) , true ) ;
// Reported under the CLI's other name, it lands under the canonical key.
Json altWindows = seat ( 5 , 10 ) . at ( " windows " ) ;
altWindows [ " seven_day_fable " ] = { { " utilization " , 44 } , { " resetsAt " , nowS + 86400 } } ;
Json alt = With ( seat ( 5 , 10 ) , { { " windows " , altWindows } } ) ;
alt = MergeFableProbe ( alt , alt ) ;
EqJson ( alt . at ( " windows " ) . at ( fable ) , { { " utilization " , 44 } , { " resetsAt " , nowS + 86400 } } ) ;
Eq ( alt . at ( " windows " ) . contains ( " seven_day_fable " ) , false ) ;
Eq ( RenderStatusReport ( { alt } , cfg , now , utc ) . contains ( " fable lenses: seat 5 (41% fable headroom) " ) , true ) ;
// A Fable probe refused for the 5-hour window: the seat is limited for
// everything, no stand-in, the Fable window still read.
Json fiveWindows = seat ( 2 , 110 ) . at ( " windows " ) ;
fiveWindows [ fable ] = { { " utilization " , 40 } , { " resetsAt " , nowS + 86400 } } ;
const Json fiveLimited = With ( seat ( 2 , 110 ) , { { " state " , " limited " } , { " util " , 110 } , { " binding " , " five_hour " } , { " error " , " five_hour rejected " } , { " windows " , fiveWindows } } ) ;
Eq ( NeedsHaiku ( fiveLimited ) , false ) ;
r = RenderStatusReport ( { MergeFableProbe ( fiveLimited , fiveLimited ) } , cfg , now , utc ) ;
Eq ( Lines ( r ) . at ( 2 ) . contains ( " | 110% | 10:10 (in 10 min) | 0% " ) & & Lines ( r ) . at ( 2 ) . ends_with ( " | 40% | " ) , true ) ;
Eq ( r . contains ( " limited: seat 2 until 10:10 (in 10 min) " ) , true ) ;
Eq ( r . contains ( " fable lenses: seat 2 " ) , false ) ;
Eq ( NeedsHaiku ( { { " seat " , 1 } , { " state " , " unknown " } , { " windows " , Json : : object ( ) } , { " error " , " probe timeout " } } ) , true ) ;
Eq ( NeedsHaiku ( seat ( 1 , 10 ) ) , false ) ;
// A Fable probe that could not be read: its state in the cell, the reason
// under the table.
const Json deadFable = MergeFableProbe ( seat ( 2 , 10 ) , { { " seat " , 2 } , { " state " , " unknown " } , { " windows " , Json : : object ( ) } , { " error " , " probe timeout after 20000 ms " } } ) ;
r = RenderStatusReport ( { deadFable } , cfg , now , utc ) ;
Eq ( Lines ( r ) . at ( 2 ) . contains ( " | unknown | " ) , true ) ;
Eq ( r . contains ( " fable probe: seat 2 unknown (probe timeout after 20000 ms) " ) , true ) ;
const Json revoked = { { " seat " , 3 } , { " state " , " invalid " } , { " windows " , Json : : object ( ) } , { " error " , " token revoked " } } ;
r = RenderStatusReport ( { MergeFableProbe ( revoked , revoked ) } , cfg , now , utc ) ;
Eq ( r . contains ( " not usable: seat 3 invalid (token revoked) " ) , true ) ;
Eq ( r . contains ( " fable probe: " ) , false ) ;
feat(seats): accounts without a 5-hour limit
Anthropic is removing the 5-hour limit in stages (from 2026-10-31), and
an account without it reports the weekly window alone. Until now the pool
took such an account for an unprobed seat: always eligible, ranked after
every other, never held to the weekly cutoff, with no cap on its passes.
- The burst window is the 5-hour one, else the weekly one
(Seats::BurstWindow). An account whose burst window is the weekly one is
ranked by it and admitted under the 95 % cutoff with the passes in
flight projected on it, at the measured share per pass (0.04 at xhigh,
0.125 otherwise; REVIEW_GATES_LENS_COST_7D_PCT), and waits for the weekly
reset when over it.
- Every OAuth account and the local login takes at most 128 passes at once,
every driver's counted (REVIEW_GATES_OAUTH_CONCURRENCY): every run
measured with 149 or more in flight on one account drew usage-limit
rejections, none of the 119 with 139 or fewer.
- A 'concurrents' rejection pauses the seat for 20 s or its retry-after,
not until a window's reset (a seat sat two hours at 10 % on one).
- Board entries, the folded foreign record and the board's view carry
cost7d, so the drivers see each other's weekly load.
- `seats` and the log table say `no limit` in such an account's 5-hour
column; the board's others-line names its weekly projection.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 17:07:17 +01:00
// 2026-10-31: an account whose 5-hour limit is removed reports the weekly
// window alone - `no limit` in its 5h cell, the weekly window its burst
// window, ranked by it.
const Json nf = { { " seat " , 3 } , { " state " , " ok " } , { " windows " , { { " seven_day " , { { " utilization " , 3 } , { " resetsAt " , nowS + 6 * 86400 } } } } } , { " util " , 5 } , { " binding " , " seven_day " } , { " resetsAt " , nowS + 6 * 86400 } } ;
r = RenderStatusReport ( { seat ( 2 , 61 , 18 ) , nf } , cfg , now , utc ) ;
Eq ( Lines ( r ) . at ( 3 ) , std : : string ( " | 3 | no limit | — | 3% | Sun 13 Sept, 10:00 (in 6d 0h) | — | " ) ) ;
Eq ( Lines ( r ) . at ( 6 ) , std : : string ( " eligible now: seat 3 (80% headroom), seat 2 (25% headroom) → next lens: seat 3 " ) ) ;
Eq ( BurstWindow ( nf . at ( " windows " ) ) = = " seven_day " & & BurstWindow ( seat ( 2 , 61 ) . at ( " windows " ) ) = = " five_hour " & & ! BurstWindow ( Json : : object ( ) ) , true ) ;
Eq ( std : : format ( " {}|{}|{} " , WindowCell ( nf . at ( " windows " ) , " five_hour " ) , WindowCell ( Json : : object ( ) , " five_hour " ) , WindowCell ( nf . at ( " windows " ) , " seven_day " ) ) , std : : string ( " no limit|—|3% " ) ) ;
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
// The local login is seat 0, named `local`.
r = RenderStatusReport ( { seat ( 0 , 30 ) , seat ( 2 , 50 ) } , cfg , now , utc ) ;
Eq ( Lines ( r ) . at ( 2 ) . starts_with ( " | local | 30% " ) , true ) ;
Eq ( r . contains ( " eligible now: local (55% headroom), seat 2 (35% headroom) → next lens: local " ) , true ) ;
// The pick: the pipeline's one-lens wave.
auto unknown = [ ] ( std : : int32_t k ) { return Json { { " seat " , k } , { " state " , " unknown " } , { " windows " , Json : : object ( ) } , { " error " , " probe timeout " } } ; } ;
auto invalid = [ ] ( std : : int32_t k ) { return Json { { " seat " , k } , { " state " , " invalid " } , { " windows " , Json : : object ( ) } , { " error " , " authentication failed " } } ; } ;
EqJson ( PickNext ( { } , cfg ) , { { " kind " , " stop " } , { " status " , " error " } , { " reason " , " no seat token configured " } } ) ;
EqJson ( PickNext ( { invalid ( 1 ) , invalid ( 2 ) } , cfg ) . at ( " reason " ) , " no valid seat token (seats 1, 2 rejected) " ) ;
EqJson ( PickNext ( { unknown ( 3 ) , unknown ( 1 ) } , cfg ) , { { " kind " , " seat " } , { " seat " , 1 } , { " source " , " round-robin " } } ) ;
EqJson ( PickNext ( { seat ( 1 , 5 ) , seat ( 2 , 1 ) , seat ( 3 , 0 ) } , cfg ) , { { " kind " , " seat " } , { " seat " , 2 } , { " source " , " probe " } } ) ;
EqJson ( PickNext ( { seat ( 1 , 5 ) , seat ( 2 , 1 ) , seat ( 3 , 0 ) } , With ( cfg , { { " policy " , " pack " } } ) ) . at ( " seat " ) , 1 ) ;
EqJson ( PickNext ( { seat ( 1 , 90 , 10 ) , seat ( 2 , 80 , 96 ) , seat ( 3 , 86 ) } , cfg ) . at ( " seat " ) , 3 ) ;
EqJson ( PickNext ( { seat ( 1 , 50 ) , unknown ( 2 ) } , cfg ) . at ( " seat " ) , 1 ) ;
// Tokens: TOKEN1..6 by fixed name, the legacy name as seat 1, a dotenv
// file's token lines only.
const std : : string text = Dyn : : Join ( std : : vector < std : : string > { " MAILOSAUR_API_KEY=abc def " , " export CLAUDE_CODE_OAUTH_TOKEN1= \" one \" " , " CLAUDE_CODE_OAUTH_TOKEN2='two' " ,
" CLAUDE_CODE_OAUTH_TOKEN3 = three " , " CLAUDE_CODE_OAUTH_TOKEN7=seven " , " CLAUDE_CODE_OAUTH_TOKEN=legacy " ,
" # CLAUDE_CODE_OAUTH_TOKEN4=commented " } , " \n " ) ;
const std : : map < std : : string , std : : string > parsed = SeatTokensFromEnvFile ( text ) ;
const std : : map < std : : string , std : : string > wantParsed = { { " CLAUDE_CODE_OAUTH_TOKEN1 " , " one " } , { " CLAUDE_CODE_OAUTH_TOKEN2 " , " two " } , { " CLAUDE_CODE_OAUTH_TOKEN3 " , " three " } , { " CLAUDE_CODE_OAUTH_TOKEN " , " legacy " } } ;
Eq ( parsed = = wantParsed , true ) ;
std : : vector < std : : int32_t > ids ;
for ( const auto & [ n , token ] : SeatTokensFromEnv ( parsed ) ) ids . push_back ( n ) ;
Eq ( ids = = std : : vector < std : : int32_t > { 1 , 2 , 3 } , true ) ;
using Tokens = std : : vector < std : : pair < std : : int32_t , std : : string > > ;
Eq ( SeatTokensFromEnv ( { { " CLAUDE_CODE_OAUTH_TOKEN1 " , " a " } , { " CLAUDE_CODE_OAUTH_TOKEN2 " , " " } , { " CLAUDE_CODE_OAUTH_TOKEN3 " , " c " } , { " CLAUDE_CODE_OAUTH_TOKEN7 " , " x " } ,
{ " CLAUDE_CODE_OAUTH_TOKENX " , " y " } , { " CLAUDE_CODE_OAUTH_TOKEN " , " legacy " } } ) = = Tokens { { 1 , " a " } , { 3 , " c " } } , true ) ;
Eq ( SeatTokensFromEnv ( { { " CLAUDE_CODE_OAUTH_TOKEN " , " legacy " } , { " CLAUDE_CODE_OAUTH_TOKEN2 " , " b " } } ) = = Tokens { { 1 , " legacy " } , { 2 , " b " } } , true ) ;
Eq ( SeatTokensFromEnv ( { } ) . empty ( ) , true ) ;
// Config: repo variables override the defaults, garbage does not.
EqJson ( ConfigFromEnv ( { } ) , { { " maxUtil " , 85 } , { " lensCostPct " , 10 } , { " waitMaxMin " , 15 } , { " policy " , " spread " } , { " handoverPct " , 90 } } ) ;
EqJson ( ConfigFromEnv ( { { " REVIEW_GATES_SEAT_MAX_UTIL " , " 70 " } , { " REVIEW_GATES_LENS_COST_PCT " , " abc " } , { " REVIEW_GATES_SEAT_WAIT_MIN " , " 0 " } , { " REVIEW_GATES_SEAT_HANDOVER_PCT " , " 80 " } ,
{ " REVIEW_GATES_SEAT_POLICY " , " pack " } } ) ,
{ { " maxUtil " , 70 } , { " lensCostPct " , 10 } , { " waitMaxMin " , 0 } , { " policy " , " pack " } , { " handoverPct " , 80 } } ) ;
EqJson ( ConfigFromEnv ( { { " REVIEW_GATES_SEAT_POLICY " , " weird " } } ) . at ( " policy " ) , " spread " ) ;
// The stream: captured from claude 2.1.263, values shortened.
const std : : string init = R " ({ " type " : " system " , " subtype " : " init " , " model " : " claude - haiku - 4 - 5 - 20251001 " , " claude_code_version " : " 2.1 .263 " }) " ;
const std : : string event = R " ({ " type " : " rate_limit_event " , " rate_limit_info " :{ " status " : " allowed " , " resetsAt " :1788789600, " rateLimitType " : " five_hour " ,) "
R " ( " overageStatus " : " rejected " , " overageDisabledReason " : " out_of_credits " , " isUsingOverage " :false, " unifiedWindows " :{) "
R " ( " five_hour " :{ " utilization " :0.05, " resetsAt " :1788789600}, " seven_day " :{ " utilization " :0.01, " resetsAt " :1789038000}}},) "
R " ( " uuid " : " u " , " session_id " : " 400f 3 eb9 - 4732 - 42f 1 - a257 - b84d1da95164 " }) " ;
const std : : string assistant = R " ({ " type " : " assistant " , " message " :{ " role " : " assistant " , " content " :[{ " type " : " text " , " text " : " ok " }]}}) " ;
const std : : string result = R " ({ " type " : " result " , " subtype " : " success " , " is_error " :false, " structured_output " :{ " ok " :true}, " result " : " ok " , " modelUsage " :{ " claude - haiku - 4 - 5 - 20251001 " :{ " inputTokens " :134}}}) " ;
for ( const std : : vector < std : : string > & order : { std : : vector < std : : string > { init , event , assistant , result } , std : : vector < std : : string > { init , assistant , event , result } ,
std : : vector < std : : string > { init , assistant , result , event } } ) {
const Json p = ParseStream ( std : : format ( " {} \n " , Dyn : : Join ( order , " \n " ) ) ) ;
EqJson ( p . at ( " result " ) . at ( " structured_output " ) , { { " ok " , true } } ) ;
EqJson ( p . at ( " sessionId " ) , " 400f3eb9-4732-42f1-a257-b84d1da95164 " ) ;
const Json & rl = p . at ( " rateLimit " ) ;
EqJson ( Json : : array ( { rl . at ( " status " ) , rl . at ( " rateLimitType " ) , rl . at ( " resetsAt " ) , rl . at ( " overageReason " ) } ) , Json : : array ( { " allowed " , " five_hour " , 1788789600 , " out_of_credits " } ) ) ;
EqJson ( rl . at ( " windows " ) . at ( " five_hour " ) , { { " utilization " , 5 } , { " resetsAt " , 1788789600 } } ) ;
EqJson ( rl . at ( " windows " ) . at ( " seven_day " ) , { { " utilization " , 1 } , { " resetsAt " , 1789038000 } } ) ;
}
EqJson ( ParseStream ( Dyn : : Join ( std : : vector < std : : string > { " garbage " , event , " more garbage " , result } , " \n " ) ) . at ( " result " ) . at ( " structured_output " ) , { { " ok " , true } } ) ;
EqJson ( ParseStream ( " { \n \" type \" : \" result \" , \n \" subtype \" : \" success \" \n } " ) . at ( " result " ) , nullptr ) ;
EqJson ( ParseStream ( " " ) . at ( " rateLimit " ) , nullptr ) ;
EqJson ( Json : : array ( { Pct ( 1.16 ) , Pct ( 0.05 ) , Pct ( 0.625 ) , Pct ( " x " ) , Pct ( - 1 ) } ) , Json : : array ( { 116 , 5 , 62.5 , 0 , 0 } ) ) ;
auto pair = [ ] ( const std : : pair < Json , std : : string > & p ) { return Json : : array ( { p . first , p . second } ) ; } ;
EqJson ( pair ( SeatUtil ( { { " five_hour " , { { " utilization " , 61 } , { " resetsAt " , 1 } } } , { " seven_day " , { { " utilization " , 18 } , { " resetsAt " , 2 } } } } ) ) , Json : : array ( { 60 , " five_hour " } ) ) ;
EqJson ( pair ( SeatUtil ( { { " five_hour " , { { " utilization " , 12 } , { " resetsAt " , 1 } } } , { " seven_day_fable " , { { " utilization " , 97 } , { " resetsAt " , 2 } } } } ) ) , Json : : array ( { 95 , " seven_day_fable " } ) ) ;
2026-10-31 21:13:55 +01:00
Eq ( IsEligible ( seat ( 1 , 10 , 99 ) , With ( cfg , { { " maxUtil " , 100 } } ) ) , false ) ;
Eq ( IsEligible ( seat ( 1 , 10 , 98 ) , With ( cfg , { { " maxUtil " , 100 } } ) ) , true ) ;
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
Eq ( IsEligible ( seat ( 1 , 10 , 94 ) , cfg ) , false ) ;
// Redaction: the seat's own token, token shapes and addresses; auth
// failures by status or by text.
const std : : vector < std : : string > secret = { " lit-secret " } ;
Eq ( ClipReason ( " token sk-ant-oat01-abcdefghijklmnopqrstuvwxyz0123 and jorijn@example.com x \n y " , secret ) , " token [redacted:token] and [redacted:email] x y " ) ;
Eq ( ClipReason ( " bad lit-secret here " , secret ) , " bad [redacted:token] here " ) ;
Eq ( LooksLikeAuthFailure ( { { " api_error_status " , 401 } } , " " ) , true ) ;
feat(seats): an API seat's cycle starts on its own grant day
The ledger reset every API seat's spend on one
REVIEW_GATES_API_CYCLE_DAY and cleared every credit-out, an OpenRouter
key's too, at that reset. Each API seat's cycle starts on its
ANTHROPIC_API_KEY<n>_GRANT_DAY now (1..31, a day past a month's end its
last), else the cycle day, also 1..31. A credit-out carries across the
rollover, and the run's probe tries a key held out on credit again, as
`seats` does an API key: an ok lifts the mark - an API key's is new
credits, its cycle restarting from the grant day, or from today when
that day is over a week back; an OpenRouter key keeps its cycle's spend,
and a key that answers with its cycle's budget spent stays out on that
budget. A probe that finds a key out of credit records it, unless the
ledger already has it out. Both are decided in one locked update on the
file's entry, a mark another driver made since the probe started
standing, and the ledger's stamps carry their UTC offset so marks are
ordered by instant.
`seats` shows the probe's reading beside the driver's verdict (the
`probe` and `driver` columns replace `state` and `ledger: out`), each
seat's grant day and when its next credits are due, and notes on what
the probe changed in the ledger or why the driver's verdict stands; the
run logs the days.
2026-11-02 13:58:29 +01:00
// The API report (2026-10-17, ported 2026-11-02): the probe beside the
// driver's verdict, the cycle, the grant day and the next credits (today,
// a date, overdue) - the Python driver's vectors.
{
const Json acfg = SeatPool : : ApiConfig ( { } ) ;
auto api = [ & ] ( std : : int32_t n , const std : : string & state , const Json & error , std : : int32_t day , const Json & ledger , const Json & event = nullptr ) {
Json s = { { " seat " , n } , { " state " , state } , { " error " , error } , { " grantDay " , day } , { " ledger " , ledger } , { " ledgerOut " , SeatPool : : LedgerOut ( ledger , acfg . at ( " budgetUsd " ) . get < double > ( ) ) } ,
{ " nextCredits " , SeatPool : : NextGrant ( now , day , Dyn : : Get ( ledger , " outSince " ) ) } } ;
if ( ! event . is_null ( ) ) s [ " event " ] = event ;
return s ;
} ;
auto entry = [ ] ( const std : : string & cycle , double spent , std : : int32_t procs , const Json & outSince ) {
return Json { { " cycle " , cycle } , { " spentUsd " , spent } , { " procs " , procs } , { " outSince " , outSince } , { " outReason " , outSince . is_null ( ) ? Json ( ) : Json ( " credit balance too low " ) } } ;
} ;
const std : : string report = RenderApiReport ( { api ( 1 , " out " , " Your credit balance is too low " , 7 , entry ( " 2026-09-07 " , 0.0 , 0 , " 2026-08-20T20:19:06 " ) ) ,
api ( 3 , " ok " , nullptr , 7 , entry ( " 2026-09-07 " , 0.0 , 0 , nullptr ) , " new-grant " ) , api ( 4 , " ok " , nullptr , 5 , entry ( " 2026-09-05 " , 200.0 , 100 , nullptr ) ) ,
api ( 2 , " unknown " , " probe timeout after 30000 ms " , 17 , entry ( " 2026-08-17 " , 12.5 , 9 , " 2026-09-01T10:00:00 " ) ) ,
api ( 5 , " out " , " Your credit balance is too low " , 3 , entry ( " 2026-09-03 " , 0.0 , 0 , " 2026-08-10T10:00:00 " ) ) } ,
acfg , now ) ;
std : : string flat ;
for ( const char c : report ) {
if ( c ! = ' ' | | ! flat . ends_with ( ' ' ) ) flat + = c ;
}
Eq ( flat . contains ( " | seat | probe | driver | driver spend this cycle | procs | cycle from | grant day | next credits | " ) , true ) ;
Eq ( flat . contains ( " | api1 | out (Your credit balance is too low) | out (credit since 2026-08-20 20:19) | $0.00 of $200 | 0 | 2026-09-07 | 7 | today | " ) , true ) ;
Eq ( flat . contains ( " | api2 | unknown (probe timeout after 30000 ms) | out (credit since 2026-09-01 10:00) | $12.50 of $200 | 9 | 2026-08-17 | 17 | 17 Sept | " ) , true ) ;
Eq ( flat . contains ( " | api3 | ok | ok | $0.00 of $200 | 0 | 2026-09-07 | 7 | 7 Oct | " ) , true ) ;
Eq ( flat . contains ( " | api4 | ok | out (budget spent) | $200.00 of $200 | 100 | 2026-09-05 | 5 | 5 Oct | " ) , true ) ;
Eq ( flat . contains ( " | api5 | out (Your credit balance is too low) | out (credit since 2026-08-10 10:00) | $0.00 of $200 | 0 | 2026-09-03 | 3 | overdue, 3 Sept | " ) , true ) ;
std : : vector < std : : string > notes ;
for ( const std : : string & line : Lines ( flat ) ) {
if ( line . starts_with ( " api " ) & & notes . size ( ) < 4 ) notes . push_back ( line ) ;
}
EqJson ( Json ( notes ) , Json : : array ( { " api2: probe unknown - the ledger's verdict stands " , " api3: the key answers again - new credits; the ledger cycle restarted (from 2026-09-07) " ,
" api4: the key answers, but the driver's budget for this cycle is spent (REVIEW_GATES_API_BUDGET_USD) " ,
" api seats run first (REVIEW_GATES_SEAT_ORDER=api-first) · budget $200 per seat per cycle · a cycle starts on the seat's grant day "
" (ANTHROPIC_API_KEY<n>_GRANT_DAY, else REVIEW_GATES_API_CYCLE_DAY=1) · 8 processes per seat · placed at +$1 each " } ) ) ;
}
feat(seats): memory, the Playwright MCP config, and the seats library
:Memory: MemAvailable and the reserve policy, the sampler of a pass's peak
tree PSS (one sweep, a pause of three sweeps at least, stopped with the
driver), and the calibration file every driver on the machine shares,
re-read and written under its lock.
:Mcp: the pinned @playwright/mcp 0.0.83 arguments and the --mcp-config of
one pass, compact and in a fixed key order.
:Seats: the dotenv loader (token lines only, TOKEN7 inert), the one-word
probe (run under the supervisor, a missing binary reported with its errno
text), the window maths, the ranking, the Fable merge, the editor-bundle
lookup of the claude binary, the redaction of anything token-shaped, and
the report. This libc++ has no tzdb, so a zone by name converts through
the C library with TZ set, reading the system's zoneinfo.
tests/Seats: 92 checks against reference vectors, numbered in order, all
green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 07:32:14 +01:00
Eq ( LooksLikeAuthFailure ( nullptr , " Invalid API key · Please run /login " ) , true ) ;
Eq ( LooksLikeAuthFailure ( { { " result " , " ok " } } , " " ) , false ) ;
// find_claude_bin: CLAUDE_BIN, then PATH, then the newest editor-bundled
// binary (numerically), else the bare name.
const fs : : path home = fs : : temp_directory_path ( ) / std : : format ( " seats-home-{} " , Platform : : PublishedPid ( ) ) ;
fs : : remove_all ( home ) ;
fs : : create_directories ( home ) ;
Eq ( FindClaudeBin ( { { " CLAUDE_BIN " , " /x/claude " } , { " PATH " , " " } } , home . string ( ) ) , " /x/claude " ) ;
const fs : : path binDir = home / " bin " ;
fs : : create_directories ( binDir ) ;
const fs : : path fake = binDir / " claude " ;
std : : ofstream ( fake ) . close ( ) ;
fs : : permissions ( fake , fs : : perms : : owner_all | fs : : perms : : group_read | fs : : perms : : group_exec | fs : : perms : : others_read | fs : : perms : : others_exec ) ;
Eq ( FindClaudeBin ( { { " PATH " , std : : format ( " /nowhere:{} " , binDir . string ( ) ) } } , home . string ( ) ) , fake . string ( ) ) ;
for ( const std : : string_view v : { " 2.1.100-linux-x64 " , " 2.1.99-linux-x64 " , " 2.1.263-linux-x64 " } ) {
const fs : : path d = home / " .vscode-oss/extensions " / std : : format ( " anthropic.claude-code-{} " , v ) / " resources/native-binary " ;
fs : : create_directories ( d ) ;
std : : ofstream ( d / " claude " ) . close ( ) ;
}
Eq ( FindClaudeBin ( { { " PATH " , " /nowhere " } } , home . string ( ) ) , ( home / " .vscode-oss/extensions/anthropic.claude-code-2.1.263-linux-x64/resources/native-binary/claude " ) . string ( ) ) ;
Eq ( FindClaudeBin ( { { " PATH " , " /nowhere " } } , ( home / " empty " ) . string ( ) ) , " claude " ) ;
fs : : remove_all ( home ) ;
std : : println ( " seats: {} checks{} " , N , Failures ? std : : format ( " , {} failed " , Failures ) : std : : string ( " ok " ) ) ;
return Failures ? 1 : 0 ;
}