@intentic/sandbox-contract 1.427.0 → 1.429.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -5
- package/dist/contracts/accounts.contract.d.ts +4 -0
- package/dist/contracts/accounts.contract.d.ts.map +1 -1
- package/dist/contracts/agent.contract.d.ts +60 -0
- package/dist/contracts/agent.contract.d.ts.map +1 -1
- package/dist/contracts/agents.contract.d.ts +617 -0
- package/dist/contracts/agents.contract.d.ts.map +1 -1
- package/dist/contracts/agents.contract.js +11 -0
- package/dist/contracts/agents.contract.js.map +1 -1
- package/dist/contracts/bases.contract.d.ts +43 -0
- package/dist/contracts/bases.contract.d.ts.map +1 -0
- package/dist/contracts/bases.contract.js +33 -0
- package/dist/contracts/bases.contract.js.map +1 -0
- package/dist/contracts/ci.contract.d.ts +17 -0
- package/dist/contracts/ci.contract.d.ts.map +1 -1
- package/dist/contracts/loops.contract.d.ts +2 -0
- package/dist/contracts/loops.contract.d.ts.map +1 -1
- package/dist/contracts/needs.contract.d.ts +52 -0
- package/dist/contracts/needs.contract.d.ts.map +1 -1
- package/dist/contracts/offload.contract.d.ts +14 -14
- package/dist/contracts/runner.contract.d.ts +62 -14
- package/dist/contracts/runner.contract.d.ts.map +1 -1
- package/dist/contracts/seal.contract.d.ts +10 -0
- package/dist/contracts/seal.contract.d.ts.map +1 -1
- package/dist/contracts/sessions.contract.d.ts +6 -0
- package/dist/contracts/sessions.contract.d.ts.map +1 -1
- package/dist/contracts/settings.contract.d.ts +4 -0
- package/dist/contracts/settings.contract.d.ts.map +1 -1
- package/dist/contracts/system.contract.d.ts +370 -4
- package/dist/contracts/system.contract.d.ts.map +1 -1
- package/dist/contracts/system.contract.js +45 -0
- package/dist/contracts/system.contract.js.map +1 -1
- package/dist/contracts/translator.contract.d.ts +8 -0
- package/dist/contracts/translator.contract.d.ts.map +1 -1
- package/dist/contracts/usage.contract.d.ts +110 -0
- package/dist/contracts/usage.contract.d.ts.map +1 -1
- package/dist/contracts/usage.contract.js +18 -0
- package/dist/contracts/usage.contract.js.map +1 -1
- package/dist/contracts/workflows.contract.d.ts +3 -0
- package/dist/contracts/workflows.contract.d.ts.map +1 -1
- package/dist/events/agent-events.d.ts +26 -0
- package/dist/events/agent-events.d.ts.map +1 -1
- package/dist/events/agent-events.js +4 -0
- package/dist/events/agent-events.js.map +1 -1
- package/dist/events/agent-transcript.d.ts +6 -0
- package/dist/events/agent-transcript.d.ts.map +1 -1
- package/dist/events/system-events.d.ts +222 -0
- package/dist/events/system-events.d.ts.map +1 -1
- package/dist/events/system-events.js +6 -0
- package/dist/events/system-events.js.map +1 -1
- package/dist/events/transcript.d.ts +30 -0
- package/dist/events/transcript.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/models/plan-pools.d.ts +8 -7
- package/dist/models/plan-pools.d.ts.map +1 -1
- package/dist/models/plan-pools.js +63 -40
- package/dist/models/plan-pools.js.map +1 -1
- package/dist/models/verdict/current-windows.d.ts +9 -0
- package/dist/models/verdict/current-windows.d.ts.map +1 -0
- package/dist/models/verdict/current-windows.js +60 -0
- package/dist/models/verdict/current-windows.js.map +1 -0
- package/dist/models/verdict/refusal-verdict.d.ts +6 -3
- package/dist/models/verdict/refusal-verdict.d.ts.map +1 -1
- package/dist/models/verdict/refusal-verdict.js +71 -18
- package/dist/models/verdict/refusal-verdict.js.map +1 -1
- package/dist/models/verdict/window-period.d.ts +7 -1
- package/dist/models/verdict/window-period.d.ts.map +1 -1
- package/dist/models/verdict/window-period.js +21 -2
- package/dist/models/verdict/window-period.js.map +1 -1
- package/dist/protocol/raw/raw-routes.d.ts +13 -0
- package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-routes.js +4 -0
- package/dist/protocol/raw/raw-routes.js.map +1 -1
- package/dist/schemas/agents-list.d.ts +4 -0
- package/dist/schemas/agents-list.d.ts.map +1 -1
- package/dist/schemas/agents.d.ts +6 -0
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/agents.js +2 -0
- package/dist/schemas/agents.js.map +1 -1
- package/dist/schemas/bases.d.ts +52 -0
- package/dist/schemas/bases.d.ts.map +1 -0
- package/dist/schemas/bases.js +31 -0
- package/dist/schemas/bases.js.map +1 -0
- package/dist/schemas/ci.d.ts +35 -0
- package/dist/schemas/ci.d.ts.map +1 -1
- package/dist/schemas/ci.js +25 -0
- package/dist/schemas/ci.js.map +1 -1
- package/dist/schemas/context-continues.d.ts +5 -0
- package/dist/schemas/context-continues.d.ts.map +1 -0
- package/dist/schemas/context-continues.js +17 -0
- package/dist/schemas/context-continues.js.map +1 -0
- package/dist/schemas/device-stacks.d.ts +161 -0
- package/dist/schemas/device-stacks.d.ts.map +1 -0
- package/dist/schemas/device-stacks.js +65 -0
- package/dist/schemas/device-stacks.js.map +1 -0
- package/dist/schemas/hosts.d.ts +2 -0
- package/dist/schemas/hosts.d.ts.map +1 -1
- package/dist/schemas/hosts.js +2 -0
- package/dist/schemas/hosts.js.map +1 -1
- package/dist/schemas/loops.d.ts +4 -0
- package/dist/schemas/loops.d.ts.map +1 -1
- package/dist/schemas/loops.js +7 -0
- package/dist/schemas/loops.js.map +1 -1
- package/dist/schemas/needs.d.ts +50 -1
- package/dist/schemas/needs.d.ts.map +1 -1
- package/dist/schemas/needs.js +16 -2
- package/dist/schemas/needs.js.map +1 -1
- package/dist/schemas/proceed-without-asking.d.ts +9 -0
- package/dist/schemas/proceed-without-asking.d.ts.map +1 -0
- package/dist/schemas/proceed-without-asking.js +17 -0
- package/dist/schemas/proceed-without-asking.js.map +1 -0
- package/dist/schemas/providers/capacity.d.ts +291 -0
- package/dist/schemas/providers/capacity.d.ts.map +1 -0
- package/dist/schemas/providers/capacity.js +64 -0
- package/dist/schemas/providers/capacity.js.map +1 -0
- package/dist/schemas/providers/plan-limits.d.ts +14 -0
- package/dist/schemas/providers/plan-limits.d.ts.map +1 -1
- package/dist/schemas/providers/plan-limits.js +9 -2
- package/dist/schemas/providers/plan-limits.js.map +1 -1
- package/dist/schemas/providers/provider-oauth.d.ts +4 -0
- package/dist/schemas/providers/provider-oauth.d.ts.map +1 -1
- package/dist/schemas/providers/refusals.d.ts +8 -0
- package/dist/schemas/providers/refusals.d.ts.map +1 -1
- package/dist/schemas/providers/refusals.js +8 -0
- package/dist/schemas/providers/refusals.js.map +1 -1
- package/dist/schemas/run-targets.d.ts +1 -0
- package/dist/schemas/run-targets.d.ts.map +1 -1
- package/dist/schemas/run-targets.js +2 -1
- package/dist/schemas/run-targets.js.map +1 -1
- package/dist/schemas/settings.d.ts +4 -0
- package/dist/schemas/settings.d.ts.map +1 -1
- package/dist/schemas/settings.js +6 -8
- package/dist/schemas/settings.js.map +1 -1
- package/dist/schemas/workflows.d.ts +5 -0
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/state/definition.d.ts +8 -0
- package/dist/state/definition.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/contracts/agents.contract.ts +13 -0
- package/src/contracts/bases.contract.ts +38 -0
- package/src/contracts/system.contract.ts +66 -0
- package/src/contracts/usage.contract.ts +21 -0
- package/src/events/agent-events.ts +8 -0
- package/src/events/system-events.ts +11 -0
- package/src/index.ts +3 -0
- package/src/models/plan-pools.test.ts +271 -39
- package/src/models/plan-pools.ts +132 -70
- package/src/models/verdict/current-windows.ts +95 -0
- package/src/models/verdict/refusal-verdict.ts +124 -30
- package/src/models/verdict/window-period.ts +53 -6
- package/src/protocol/raw/raw-routes.ts +9 -0
- package/src/schemas/agents.ts +2 -0
- package/src/schemas/bases.ts +48 -0
- package/src/schemas/ci.ts +35 -0
- package/src/schemas/context-continues.ts +40 -0
- package/src/schemas/device-stacks.ts +126 -0
- package/src/schemas/hosts.ts +4 -0
- package/src/schemas/loops.ts +10 -0
- package/src/schemas/needs.ts +25 -2
- package/src/schemas/proceed-without-asking.ts +37 -0
- package/src/schemas/providers/capacity.ts +91 -0
- package/src/schemas/providers/plan-limits.ts +20 -3
- package/src/schemas/providers/refusals.ts +17 -0
- package/src/schemas/run-targets.ts +4 -3
- package/src/schemas/settings.ts +6 -14
- package/src/state/daemon-floor.test.ts +2 -0
- package/src/state/workspace-state.test.ts +3 -3
- package/src/state/workspace-state.ts +1 -1
package/src/models/plan-pools.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AccountState, AccountUsage, ProviderRefusal, UsageWindow } from "../schemas/providers/plan-limits.js";
|
|
2
2
|
import { humanizeModelId } from "./model-label.js";
|
|
3
3
|
import { bindingWindow, gatesModel, gatingWindows, type ModelRef, pinnedFull, provenFull, SPENT_UTILIZATION } from "./verdict/pool-gates.js";
|
|
4
|
+
import { factsAt, usageLapsesAt } from "./verdict/current-windows.js";
|
|
4
5
|
import { type RefusalsOnFile, refusedInstant, type ServiceFacts, standingOver } from "./verdict/refusal-verdict.js";
|
|
5
6
|
import { windowPeriod } from "./verdict/window-period.js";
|
|
6
7
|
|
|
@@ -21,52 +22,24 @@ export {
|
|
|
21
22
|
uncoveredModels,
|
|
22
23
|
wordsOf,
|
|
23
24
|
} from "./verdict/pool-gates.js";
|
|
24
|
-
export {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// A pool whose reading belongs to a window that has since closed: still the account's pool, at the floor any new window
|
|
39
|
-
// starts from, and marked with when the old window closed so a surface can say it has not been re-read since.
|
|
40
|
-
const lapsedWindow = (window: UsageWindow, measuredAt: number): UsageWindow => {
|
|
41
|
-
const { resetsAt, rounded: _rounded, utilization: _utilization, ...pool } = window;
|
|
42
|
-
// Only reached for a window windowLive retired, so with no published reset its length is known.
|
|
43
|
-
const closedAt = resetsAt ?? Math.floor((measuredAt + (windowPeriod(window)?.seconds ?? 0) * 1000) / 1000);
|
|
44
|
-
return { ...pool, utilization: 0, lapsedAt: closedAt };
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// What a reading says about each of the account's pools now. A window past its reset is kept rather than dropped, as a
|
|
48
|
-
// lapsed pool (lapsedWindow): dropping it made a reading whose re-reads were failing lose its five-hour row within hours
|
|
49
|
-
// while the weekly rows stayed, a partial card that reads as complete. The floor of 0 is what dropping it already meant
|
|
50
|
-
// to every verdict (an absent pool binds nothing). A reading with no window still current says nothing about now and
|
|
51
|
-
// answers none, so the account reads as unmeasured. Idempotent: a pool already lapsed stays as it is.
|
|
52
|
-
// A pool the provider stopped reporting (`missingSince`) is dropped instead once its window closes: nothing will read it
|
|
53
|
-
// again, so its floor of 0 would be a pool that is not there.
|
|
54
|
-
export const currentWindows = (usage: AccountUsage, now: number): UsageWindow[] => {
|
|
55
|
-
const live = (window: UsageWindow): boolean => window.lapsedAt === undefined && windowLive(window, usage.measuredAt, now);
|
|
56
|
-
if (!usage.windows.some(live)) {
|
|
57
|
-
return [];
|
|
58
|
-
}
|
|
59
|
-
return usage.windows.flatMap((window) => {
|
|
60
|
-
if (window.lapsedAt !== undefined || live(window)) {
|
|
61
|
-
return [window];
|
|
62
|
-
}
|
|
63
|
-
return window.missingSince === undefined ? [lapsedWindow(window, usage.measuredAt)] : [];
|
|
64
|
-
});
|
|
65
|
-
};
|
|
25
|
+
export {
|
|
26
|
+
type RefusedInstant,
|
|
27
|
+
refusedInstant,
|
|
28
|
+
refusalVerdict,
|
|
29
|
+
refusedPoolOf,
|
|
30
|
+
refusedPoolReset,
|
|
31
|
+
type RefusalsOnFile,
|
|
32
|
+
type ServiceFacts,
|
|
33
|
+
stampedRefusal,
|
|
34
|
+
standingOver,
|
|
35
|
+
} from "./verdict/refusal-verdict.js";
|
|
36
|
+
export { namedPeriod, type PeriodSource, periodWords, type WindowPeriod, windowPeriod } from "./verdict/window-period.js";
|
|
37
|
+
export { currentWindows, factsAt, usageAt, usageLapsesAt, windowLive } from "./verdict/current-windows.js";
|
|
66
38
|
|
|
67
39
|
// Whether an account can serve a turn, and what one provider's accounts can serve together: the one rule every picker,
|
|
68
40
|
// gate, listing and surface reads. The daemon and the editor run it on the same facts (the account rows and every
|
|
69
|
-
// refusal on file)
|
|
41
|
+
// refusal on file) at the instant each passes, the sandbox's own clock: every reading is first taken as it stands then
|
|
42
|
+
// (factsAt), so a window past its reset reads lapsed on both sides whoever re-read it last.
|
|
70
43
|
|
|
71
44
|
|
|
72
45
|
// The account reopens for this question when the pools holding it shut do: all of them for a model's own question (or
|
|
@@ -177,24 +150,42 @@ const refusedPool = (refusal: ProviderRefusal): UsageWindow => {
|
|
|
177
150
|
return pool;
|
|
178
151
|
};
|
|
179
152
|
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
|
|
153
|
+
// The pool one limit refusal is laid on, or `own` where it is a pool of its own (refusedPool). The pool the ledger
|
|
154
|
+
// resolved it to as it filed it (refusal-verdict.ts refusedPoolOf), where it did and the reading still names that pool:
|
|
155
|
+
// exactly that one, whatever reset the reading now shows for it or which pool now binds; resolved to none the reading
|
|
156
|
+
// published, its own. One filed unresolved (before 2026-11-19, a translator's bench, a reading that could not say, an
|
|
157
|
+
// instant never named) is worked out here as it always was: the pool the refused turn's model spends (else the one asked
|
|
158
|
+
// about, else the tightest), unless its instant is the reset of no pool a reading `polled` since publishes
|
|
159
|
+
// (refusedInstant `unpublished`): a Codex refusal on GPT 6.1 Sol naming a reset neither window had pinned the weekly
|
|
160
|
+
// window, which gates every model, so every Codex model read spent for four days while both windows showed most of the
|
|
161
|
+
// allowance free (2026-11-14).
|
|
162
|
+
const refusedIn = (usage: AccountUsage | undefined, refusal: ProviderRefusal, model: ModelRef | undefined, polled: AccountUsage | undefined): UsageWindow | "own" => {
|
|
163
|
+
if (refusal.poolUnpublished === true) {
|
|
164
|
+
return "own";
|
|
165
|
+
}
|
|
166
|
+
const resolved = refusal.pool === undefined ? undefined : usage?.windows.find((window) => window.kind === refusal.pool);
|
|
167
|
+
if (resolved !== undefined) {
|
|
168
|
+
return resolved;
|
|
169
|
+
}
|
|
187
170
|
const binding = bindingWindow(usage, refusal.model === undefined ? model : { id: refusal.model });
|
|
188
|
-
|
|
171
|
+
return binding === undefined || refusedInstant(refusal, polled) === "unpublished" ? "own" : binding;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// One limit refusal laid on a reading: the pool it was refused in (refusedIn) read full, since a polled reading freezes
|
|
175
|
+
// the moment a pool actually empties, and reopening when the provider said to try again, where it said, over whatever
|
|
176
|
+
// the polled pool last said. Never another model's allowance.
|
|
177
|
+
const pinRefusal = (usage: AccountUsage | undefined, refusal: ProviderRefusal, model: ModelRef | undefined, polled: AccountUsage | undefined): AccountUsage => {
|
|
178
|
+
const refused = refusedIn(usage, refusal, model, polled);
|
|
179
|
+
if (usage === undefined || refused === "own") {
|
|
189
180
|
return { ...(usage ?? { measuredAt: refusal.at }), windows: [...(usage?.windows ?? []), refusedPool(refusal)] };
|
|
190
181
|
}
|
|
191
|
-
const pinned = pinnedFull(
|
|
182
|
+
const pinned = pinnedFull(refused);
|
|
192
183
|
// Reopening when the refusal ends (refusalVerdict), so the "back at" every surface draws is when it flips.
|
|
193
184
|
const over = refusal.resetsAt ?? refusal.poolResetsAt;
|
|
194
185
|
if (over !== undefined) {
|
|
195
186
|
pinned.resetsAt = over;
|
|
196
187
|
}
|
|
197
|
-
return { ...usage, windows: usage.windows.map((window) => (window ===
|
|
188
|
+
return { ...usage, windows: usage.windows.map((window) => (window === refused ? pinned : window)) };
|
|
198
189
|
};
|
|
199
190
|
|
|
200
191
|
/**
|
|
@@ -216,9 +207,9 @@ export const newestRefusal = (refusals: RefusalsOnFile): ProviderRefusal | undef
|
|
|
216
207
|
* One account's verdict on everything on file over it. `standing` is the refusals still standing over it (standingOver).
|
|
217
208
|
* Precedence: a revoked sign-in outranks a lost seat (reconnecting fixes one, nothing here fixes the other), both outrank
|
|
218
209
|
* the newest standing refusal where it refused the account itself, then a translator bench, then the plan limits on the
|
|
219
|
-
* reading the refusals leave (judgedReading).
|
|
210
|
+
* reading the refusals leave (judgedReading), on the account's reading as it stands at `now` (factsAt).
|
|
220
211
|
*/
|
|
221
|
-
export const serviceState = (facts: ServiceFacts, standing: RefusalsOnFile
|
|
212
|
+
export const serviceState = (facts: ServiceFacts, standing: RefusalsOnFile, model: ModelRef | undefined, now: number): AccountState => {
|
|
222
213
|
if (facts.needsReauth === true) {
|
|
223
214
|
return { kind: "blocked", fix: "reconnect", reason: facts.detail ?? "sign-in expired" };
|
|
224
215
|
}
|
|
@@ -234,7 +225,7 @@ export const serviceState = (facts: ServiceFacts, standing: RefusalsOnFile = [],
|
|
|
234
225
|
if (newest?.kind === "entitlement") {
|
|
235
226
|
return { kind: "blocked", fix: "admin", reason: newest.message };
|
|
236
227
|
}
|
|
237
|
-
const plan = readingHeadroom(judgedReading(facts.usage, standing, model), model);
|
|
228
|
+
const plan = readingHeadroom(judgedReading(factsAt(facts, now).usage, standing, model), model);
|
|
238
229
|
return facts.cooling === undefined ? plan : benched(facts.cooling, plan, now);
|
|
239
230
|
};
|
|
240
231
|
|
|
@@ -258,15 +249,50 @@ const benched = (cooling: NonNullable<ServiceFacts["cooling"]>, plan: AccountSta
|
|
|
258
249
|
return { kind: "blocked", fix: "wait", reason: cooling.reason ?? "cooling down", until };
|
|
259
250
|
};
|
|
260
251
|
|
|
261
|
-
|
|
252
|
+
/* A MODEL AN ACCOUNT'S PLAN DOES NOT INCLUDE IS NOT ITS TO SERVE, HOWEVER MUCH ROOM IT HAS. Two ChatGPT Free sign-ins
|
|
253
|
+
back from their monthly reset read 100% left while the translator routed GPT-6.2-Sol only to the Team one, spent for
|
|
254
|
+
two hours: every Sol request skipped both Free ones and met the Team one's bench, the turn was refused, the re-read
|
|
255
|
+
showed both Free ones full of room, the held turn was re-timed to now, and it went round again every two minutes
|
|
256
|
+
(2026-11-19). So an account the source lists models for, without this one, is `blocked` with the `plan` fix for it,
|
|
257
|
+
and for no other model. Judged only where some account of the provider lists the model under exactly this id: a
|
|
258
|
+
listing that names it otherwise (an alias, a thinking suffix) or none at all says nothing about any account. */
|
|
259
|
+
const offPlan = (accounts: readonly ServiceFacts[], model: ModelRef | undefined): ((facts: ServiceFacts) => boolean) => {
|
|
260
|
+
const listed = (facts: ServiceFacts): facts is ServiceFacts & { readonly models: readonly string[] } => (facts.models?.length ?? 0) > 0;
|
|
261
|
+
if (model === undefined || !accounts.some((facts) => listed(facts) && facts.models.includes(model.id))) {
|
|
262
|
+
return () => false;
|
|
263
|
+
}
|
|
264
|
+
return (facts) => listed(facts) && !facts.models.includes(model.id);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const offPlanState = (model: ModelRef): AccountState => ({
|
|
268
|
+
kind: "blocked",
|
|
269
|
+
fix: "plan",
|
|
270
|
+
reason: `${model.label ?? humanizeModelId(model.id)} is not on this account's plan`,
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
/** Whether a verdict says the account's plan does not include the model it was judged on: outside that model's fleet. */
|
|
274
|
+
export const offPlanVerdict = (state: AccountState): boolean => state.kind === "blocked" && state.fix === "plan";
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Every account of one provider, judged together, since a refusal naming no account is answered by any of them. `now` is
|
|
278
|
+
* the sandbox's clock (epoch ms), never defaulted: a window's reset, a refusal's instant and a bench's end are all judged
|
|
279
|
+
* against it, so a caller holding another clock (a browser's) passes the sandbox's reading of it.
|
|
280
|
+
*/
|
|
262
281
|
export const serviceStates = (
|
|
263
282
|
accounts: readonly ServiceFacts[],
|
|
264
|
-
refusals: RefusalsOnFile
|
|
265
|
-
model
|
|
266
|
-
now: number
|
|
283
|
+
refusals: RefusalsOnFile | undefined,
|
|
284
|
+
model: ModelRef | undefined,
|
|
285
|
+
now: number,
|
|
267
286
|
): ReadonlyMap<string, AccountState> => {
|
|
268
|
-
const
|
|
269
|
-
|
|
287
|
+
const current = accounts.map((facts) => factsAt(facts, now));
|
|
288
|
+
const standing = standingOver(current, refusals ?? [], now);
|
|
289
|
+
const excluded = offPlan(current, model);
|
|
290
|
+
return new Map(
|
|
291
|
+
current.map((facts) => [
|
|
292
|
+
facts.account,
|
|
293
|
+
model !== undefined && excluded(facts) ? offPlanState(model) : serviceState(facts, standing(facts.account), model, now),
|
|
294
|
+
]),
|
|
295
|
+
);
|
|
270
296
|
};
|
|
271
297
|
|
|
272
298
|
/** When an account that cannot serve says it can again, in epoch seconds; undefined where its verdict names no instant. */
|
|
@@ -291,16 +317,21 @@ export type FleetCapacity =
|
|
|
291
317
|
// says when, and `pool` what holds that one.
|
|
292
318
|
| { readonly kind: "spent"; readonly accounts: number; readonly reopensAt?: number | undefined; readonly pool?: string | undefined };
|
|
293
319
|
|
|
294
|
-
/**
|
|
320
|
+
/**
|
|
321
|
+
* A provider's capacity for one model from its accounts as already judged on that model (serviceStates). An account whose
|
|
322
|
+
* plan does not include the model is no part of its fleet: never counted, never the reopen.
|
|
323
|
+
*/
|
|
295
324
|
export const fleetOf = (accounts: readonly ServiceFacts[], states: ReadonlyMap<string, AccountState>): FleetCapacity => {
|
|
296
|
-
|
|
325
|
+
const judged = accounts
|
|
326
|
+
.map((facts) => ({ facts, state: states.get(facts.account) ?? ({ kind: "unknown" } as const) }))
|
|
327
|
+
.filter(({ state }) => !offPlanVerdict(state));
|
|
328
|
+
if (judged.length === 0) {
|
|
297
329
|
return { kind: "unknown" };
|
|
298
330
|
}
|
|
299
|
-
const judged = accounts.map((facts) => ({ facts, state: states.get(facts.account) ?? ({ kind: "unknown" } as const) }));
|
|
300
331
|
const serving = judged.filter(({ state }) => state.kind === "ready" || state.kind === "unknown");
|
|
301
332
|
if (serving.length > 0) {
|
|
302
333
|
const room = serving.flatMap(({ facts, state }) => (state.kind === "ready" && facts.usage !== undefined ? [facts.usage.measuredAt] : []));
|
|
303
|
-
return { kind: "serving", serving: serving.length, accounts:
|
|
334
|
+
return { kind: "serving", serving: serving.length, accounts: judged.length, roomMeasuredAt: room.length === 0 ? undefined : Math.max(...room) };
|
|
304
335
|
}
|
|
305
336
|
// The soonest instant any account names; one with none only decides the pool where nothing names an instant.
|
|
306
337
|
const soonest = judged.reduce<AccountState | undefined>((best, { state }) => {
|
|
@@ -310,15 +341,18 @@ export const fleetOf = (accounts: readonly ServiceFacts[], states: ReadonlyMap<s
|
|
|
310
341
|
}, undefined);
|
|
311
342
|
return {
|
|
312
343
|
kind: "spent",
|
|
313
|
-
accounts:
|
|
344
|
+
accounts: judged.length,
|
|
314
345
|
reopensAt: soonest === undefined ? undefined : reopensAtOf(soonest),
|
|
315
346
|
pool: soonest?.kind === "spent" ? soonest.pool : undefined,
|
|
316
347
|
};
|
|
317
348
|
};
|
|
318
349
|
|
|
319
|
-
/** A provider's capacity for one model, judged on its accounts and every refusal on file. */
|
|
320
|
-
export const fleetCapacity = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile
|
|
321
|
-
fleetOf(
|
|
350
|
+
/** A provider's capacity for one model, judged on its accounts and every refusal on file, at `now` (serviceStates). */
|
|
351
|
+
export const fleetCapacity = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile | undefined, model: ModelRef | undefined, now: number): FleetCapacity =>
|
|
352
|
+
fleetOf(
|
|
353
|
+
accounts.map((facts) => factsAt(facts, now)),
|
|
354
|
+
serviceStates(accounts, refusals, model, now),
|
|
355
|
+
);
|
|
322
356
|
|
|
323
357
|
// Worst last: proven room, no reading either way, a pool at the line (it may hold a sliver, or nothing), known spent,
|
|
324
358
|
// and nothing a turn can run on.
|
|
@@ -418,3 +452,31 @@ export const preferredAccount = <T extends RankedAccount>(entries: readonly T[])
|
|
|
418
452
|
*/
|
|
419
453
|
export const bestWithRoom = <T extends RankedAccount>(entries: readonly T[]): T | undefined =>
|
|
420
454
|
preferredAccount(entries.filter((entry) => entry.state.kind === "ready" && entry.state.room > 0));
|
|
455
|
+
|
|
456
|
+
/* WHEN THE CLOCK ALONE NEXT CHANGES A PROVIDER'S PICTURE, so whoever holds it judges again then rather than on its next
|
|
457
|
+
read: a window resetting (or outliving the length it is trusted for), an account's reopening, a translator bench ending,
|
|
458
|
+
a refusal's own instant or the stamped reset of the pool it was refused in, and a switch-off ageing out of the week
|
|
459
|
+
that ranks an account as flapping. A refusal's horizon is not here: the ledger forgets it on read and says so as it
|
|
460
|
+
does (provider-refusals.ts horizonWatch). Every instant at or before `now` is already judged. */
|
|
461
|
+
|
|
462
|
+
/** The soonest instant after `now` at which the clock alone changes any verdict on these facts, in epoch ms. */
|
|
463
|
+
export const capacityValidUntil = (
|
|
464
|
+
accounts: readonly ServiceFacts[],
|
|
465
|
+
states: ReadonlyMap<string, AccountState>,
|
|
466
|
+
refusals: RefusalsOnFile,
|
|
467
|
+
switchOffs: SwitchOffs | undefined,
|
|
468
|
+
now: number,
|
|
469
|
+
): number | undefined => {
|
|
470
|
+
const seconds = (at: number | undefined): number | undefined => (at === undefined ? undefined : at * 1000);
|
|
471
|
+
const instants = [
|
|
472
|
+
...accounts.flatMap((facts) => [
|
|
473
|
+
usageLapsesAt(facts.usage, now),
|
|
474
|
+
seconds(facts.cooling?.until),
|
|
475
|
+
...(facts.cooldowns ?? []).map((cooldown) => cooldown.until * 1000),
|
|
476
|
+
]),
|
|
477
|
+
...[...states.values()].map((state) => seconds(reopensAtOf(state))),
|
|
478
|
+
...refusals.flatMap((refusal) => [seconds(refusal.resetsAt), seconds(refusal.poolResetsAt)]),
|
|
479
|
+
...Object.values(switchOffs ?? {}).flatMap((offs) => offs.map((at) => at + FLAPPING_WINDOW_MS)),
|
|
480
|
+
].filter((at): at is number => at !== undefined && at > now);
|
|
481
|
+
return instants.length === 0 ? undefined : Math.min(...instants);
|
|
482
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { AccountUsage, UsageWindow } from "../../schemas/providers/plan-limits.js";
|
|
2
|
+
import { windowPeriod } from "./window-period.js";
|
|
3
|
+
|
|
4
|
+
// What a reading still says at a given instant: the first thing the serviceability rule does with every account's facts,
|
|
5
|
+
// so the daemon and every open window judge the same reading at the same instant, whoever last re-read it. Before
|
|
6
|
+
// 2026-11-19 the daemon lapsed a window past its reset as it read its store, and the rule did not: an editor holding the
|
|
7
|
+
// facts it was sent judged a Codex five-hour pool at 100% spent, "back at 12:00", at 12:05, while the daemon judged the
|
|
8
|
+
// same account ready with 60% left, until something made the editor read the accounts again.
|
|
9
|
+
|
|
10
|
+
// Whether a reading of this window can still be true. A published reset instant is the authority; with none, the
|
|
11
|
+
// window's own length is, since a pool read as empty says nothing about the window that opened after it. A window whose
|
|
12
|
+
// length nothing names keeps the old rule: only its reset instant can retire it.
|
|
13
|
+
export const windowLive = (window: UsageWindow, measuredAt: number, now: number): boolean => {
|
|
14
|
+
if (window.resetsAt !== undefined) {
|
|
15
|
+
return window.resetsAt * 1000 > now;
|
|
16
|
+
}
|
|
17
|
+
const period = windowPeriod(window);
|
|
18
|
+
return period === undefined || measuredAt + period.seconds * 1000 > now;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// A pool whose reading belongs to a window that has since closed: still the account's pool, at the floor any new window
|
|
22
|
+
// starts from, and marked with when the old window closed so a surface can say it has not been re-read since.
|
|
23
|
+
const lapsedWindow = (window: UsageWindow, measuredAt: number): UsageWindow => {
|
|
24
|
+
const { resetsAt, rounded: _rounded, utilization: _utilization, ...pool } = window;
|
|
25
|
+
// Only reached for a window windowLive retired, so with no published reset its length is known.
|
|
26
|
+
const closedAt = resetsAt ?? Math.floor((measuredAt + (windowPeriod(window)?.seconds ?? 0) * 1000) / 1000);
|
|
27
|
+
return { ...pool, utilization: 0, lapsedAt: closedAt };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// What a reading says about each of the account's pools now. A window past its reset is kept rather than dropped, as a
|
|
31
|
+
// lapsed pool (lapsedWindow): dropping it made a reading whose re-reads were failing lose its five-hour row within hours
|
|
32
|
+
// while the weekly rows stayed, a partial card that reads as complete. The floor of 0 is what dropping it already meant
|
|
33
|
+
// to every verdict (an absent pool binds nothing). A reading with no window still current says nothing about now and
|
|
34
|
+
// answers none, so the account reads as unmeasured. Idempotent: a pool already lapsed stays as it is.
|
|
35
|
+
// A pool the provider stopped reporting (`missingSince`) is dropped instead once its window closes: nothing will read it
|
|
36
|
+
// again, so its floor of 0 would be a pool that is not there.
|
|
37
|
+
export const currentWindows = (usage: AccountUsage, now: number): UsageWindow[] => {
|
|
38
|
+
const live = (window: UsageWindow): boolean => window.lapsedAt === undefined && windowLive(window, usage.measuredAt, now);
|
|
39
|
+
if (!usage.windows.some(live)) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
return usage.windows.flatMap((window) => {
|
|
43
|
+
if (window.lapsedAt !== undefined || live(window)) {
|
|
44
|
+
return [window];
|
|
45
|
+
}
|
|
46
|
+
return window.missingSince === undefined ? [lapsedWindow(window, usage.measuredAt)] : [];
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A reading as it stands at `now` (currentWindows), or undefined where no window of it is still current. Idempotent, and
|
|
52
|
+
* the very reading it was given where nothing in it has lapsed, so a surface comparing readings by identity sees no change.
|
|
53
|
+
*/
|
|
54
|
+
export const usageAt = (usage: AccountUsage | undefined, now: number): AccountUsage | undefined => {
|
|
55
|
+
if (usage === undefined) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const windows = currentWindows(usage, now);
|
|
59
|
+
if (windows.length === 0) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return windows.length === usage.windows.length && windows.every((window, index) => window === usage.windows[index]) ? usage : { ...usage, windows };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** One account's facts with its reading as it stands at `now` (usageAt): what every verdict judges. */
|
|
66
|
+
export const factsAt = <T extends { readonly usage?: AccountUsage | undefined }>(facts: T, now: number): T => {
|
|
67
|
+
if (facts.usage === undefined) {
|
|
68
|
+
return facts;
|
|
69
|
+
}
|
|
70
|
+
const usage = usageAt(facts.usage, now);
|
|
71
|
+
if (usage === facts.usage) {
|
|
72
|
+
return facts;
|
|
73
|
+
}
|
|
74
|
+
// An absent reading is spelled as `usage: undefined`, which every reader of the facts treats as not measured.
|
|
75
|
+
return { ...facts, usage };
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The soonest instant after `now` at which a reading changes by the clock alone: a current window's reset, or the end of
|
|
80
|
+
* the length a window with no published reset is trusted for. Epoch ms; undefined where nothing in it is due to lapse.
|
|
81
|
+
*/
|
|
82
|
+
export const usageLapsesAt = (usage: AccountUsage | undefined, now: number): number | undefined => {
|
|
83
|
+
if (usage === undefined) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const ends = usage.windows.flatMap((window) => {
|
|
87
|
+
if (window.lapsedAt !== undefined) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
const period = windowPeriod(window);
|
|
91
|
+
const at = window.resetsAt !== undefined ? window.resetsAt * 1000 : period === undefined ? undefined : usage.measuredAt + period.seconds * 1000;
|
|
92
|
+
return at !== undefined && at > now ? [at] : [];
|
|
93
|
+
});
|
|
94
|
+
return ends.length === 0 ? undefined : Math.min(...ends);
|
|
95
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { AccountUsage, ModelCooldown, ProviderRefusal } from "../../schemas/providers/plan-limits.js";
|
|
1
|
+
import type { AccountUsage, ModelCooldown, ProviderRefusal, UsageWindow } from "../../schemas/providers/plan-limits.js";
|
|
2
|
+
import { factsAt } from "./current-windows.js";
|
|
2
3
|
import { bindingWindow, gatingWindows, SPENT_UTILIZATION } from "./pool-gates.js";
|
|
3
4
|
import { windowPeriod } from "./window-period.js";
|
|
4
5
|
|
|
@@ -20,6 +21,9 @@ export interface ServiceFacts {
|
|
|
20
21
|
// How the account is named to a person (its label, a routed credential's address); a surface that quotes a figure
|
|
21
22
|
// says whose it is.
|
|
22
23
|
readonly label?: string | undefined;
|
|
24
|
+
// The model ids the account runs, where its source can say (TranslatorAccount.models). Absent or empty: unknown,
|
|
25
|
+
// never none.
|
|
26
|
+
readonly models?: readonly string[] | undefined;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
/** Every refusal on file for one provider: what its accounts are judged against, each standing until something answers it. */
|
|
@@ -71,9 +75,39 @@ export const refusedPoolReset = (refusal: ProviderRefusal, accounts: readonly Se
|
|
|
71
75
|
// from a countdown at the moment it was said.
|
|
72
76
|
const SAME_RESET_SECONDS = 30 * 60;
|
|
73
77
|
|
|
78
|
+
// A pool the instant rule can judge: its reset and its window's length both known.
|
|
79
|
+
interface JudgedPool {
|
|
80
|
+
readonly window: UsageWindow;
|
|
81
|
+
readonly reset: number;
|
|
82
|
+
readonly period: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The current pools the refused model spends, each with its reset and length; undefined where there are none, or where
|
|
86
|
+
// one lacks either, since then the refusal's instant may be that pool's.
|
|
87
|
+
const judgedPools = (refusal: ProviderRefusal, usage: AccountUsage): readonly JudgedPool[] | undefined => {
|
|
88
|
+
const current = gatingWindows(usage, refusal.model === undefined ? undefined : { id: refusal.model }).filter((window) => window.lapsedAt === undefined);
|
|
89
|
+
const judged = current.flatMap((window) => {
|
|
90
|
+
const period = windowPeriod(window)?.seconds;
|
|
91
|
+
return window.resetsAt === undefined || period === undefined ? [] : [{ window, reset: window.resetsAt, period }];
|
|
92
|
+
});
|
|
93
|
+
return current.length > 0 && judged.length === current.length ? judged : undefined;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// A pool whose window opened after the refusal was filed, with room: the provider renewed it early.
|
|
97
|
+
const renewedAfter = (refusal: ProviderRefusal, { window, reset, period }: JudgedPool): boolean =>
|
|
98
|
+
reset - period > refusal.at / 1000 + SAME_RESET_SECONDS && window.utilization < SPENT_UTILIZATION;
|
|
99
|
+
|
|
100
|
+
// Whether every pool that could have held the refusal renewed since: a pool held it only if its window is at least as
|
|
101
|
+
// long as the wait the provider named.
|
|
102
|
+
const holdersRenewed = (refusal: ProviderRefusal, pools: readonly JudgedPool[], resetsAt: number): boolean => {
|
|
103
|
+
const holders = pools.filter(({ period }) => period + SAME_RESET_SECONDS >= resetsAt - refusal.at / 1000);
|
|
104
|
+
return holders.length > 0 && holders.every((pool) => renewedAfter(refusal, pool));
|
|
105
|
+
};
|
|
106
|
+
|
|
74
107
|
/**
|
|
75
108
|
* What a reading of the refused account taken since says about the instant a spent allowance named, judged on the pools
|
|
76
|
-
* the refused model spends:
|
|
109
|
+
* the refused model spends. Only for a refusal the ledger could not resolve as it filed it (refusedPoolOf): one filed
|
|
110
|
+
* before 2026-11-19, a translator's bench, or one whose account's reading could not say then.
|
|
77
111
|
* - `published`: one of them resets at that instant, so the refusal is that pool's, and the instant outranks the room
|
|
78
112
|
* the reading shows (verdictOn).
|
|
79
113
|
* - `renewed`: none resets there, and every pool long enough to have held the refusal opened after it was filed, with
|
|
@@ -91,23 +125,85 @@ export const refusedInstant = (refusal: ProviderRefusal, usage: AccountUsage | u
|
|
|
91
125
|
if (refusal.kind !== "limit" || resetsAt === undefined || usage === undefined || usage.measuredAt <= refusal.at) {
|
|
92
126
|
return "unjudged";
|
|
93
127
|
}
|
|
94
|
-
const pools =
|
|
95
|
-
|
|
96
|
-
return window.lapsedAt === undefined ? [{ window, reset: window.resetsAt, period }] : [];
|
|
97
|
-
});
|
|
98
|
-
if (pools.length === 0 || pools.some(({ reset, period }) => reset === undefined || period === undefined)) {
|
|
128
|
+
const pools = judgedPools(refusal, usage);
|
|
129
|
+
if (pools === undefined) {
|
|
99
130
|
return "unjudged";
|
|
100
131
|
}
|
|
101
|
-
if (pools.some(({ reset }) => Math.abs(
|
|
132
|
+
if (pools.some(({ reset }) => Math.abs(reset - resetsAt) <= SAME_RESET_SECONDS)) {
|
|
102
133
|
return "published";
|
|
103
134
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
135
|
+
return holdersRenewed(refusal, pools, resetsAt) ? "renewed" : "unpublished";
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/* WHICH POOL A SPENT ALLOWANCE THAT NAMED ITS INSTANT WAS REFUSED IN, resolved once, as the daemon's ledger files it
|
|
139
|
+
(provider-refusals.ts), from the refused account's reading as it stood when the provider refused (factsAt): the pool
|
|
140
|
+
the refused model spends whose reset is that instant (the fullest, where several reset then), stamped `pool`; or, where
|
|
141
|
+
every such pool is current and publishes its reset and its length and none resets then, none of them
|
|
142
|
+
(`poolUnpublished`: a pool the reading does not name, such as one model's own cap). Until 2026-11-19 every judgement
|
|
143
|
+
worked this out again, in the daemon and in every open window, from whatever reading it held, and a reading taken once
|
|
144
|
+
the pool had moved said something else: a Codex refusal on GPT 6.1 Sol was pinned on the weekly pool, which gates
|
|
145
|
+
every model, for four days (2026-11-14). Re-inferring it on each judgement was rejected for that: the reading that can
|
|
146
|
+
say is the one beside the refusal, and only the ledger holds both at once.
|
|
147
|
+
|
|
148
|
+
Nothing is stamped where the reading cannot say, and the rule then still works it out as it judges (refusedInstant):
|
|
149
|
+
a refusal naming no instant (its pool is the one a reading since shows full, refusedPoolReset), one no reading
|
|
150
|
+
measures, a person's mark, one naming no account over several (each account's own pool may differ), an account with
|
|
151
|
+
no reading, and a reading in which a pool the model spends has closed since it was read or publishes no reset or no
|
|
152
|
+
length, since the instant may be that pool's. */
|
|
153
|
+
export const refusedPoolOf = (refusal: ProviderRefusal, accounts: readonly ServiceFacts[]): Pick<ProviderRefusal, "pool" | "poolUnpublished"> => {
|
|
154
|
+
const { resetsAt } = refusal;
|
|
155
|
+
if (refusal.kind !== "limit" || resetsAt === undefined || refusal.unmetered === true || refusal.byHand === true) {
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
const refused = refusal.account === undefined ? (accounts.length === 1 ? accounts[0] : undefined) : accounts.find((facts) => facts.account === refusal.account);
|
|
159
|
+
const usage = refused === undefined ? undefined : factsAt(refused, refusal.at).usage;
|
|
160
|
+
const model = refusal.model === undefined ? undefined : { id: refusal.model };
|
|
161
|
+
const pools = gatingWindows(usage, model);
|
|
162
|
+
if (usage === undefined || pools.some((window) => window.lapsedAt !== undefined)) {
|
|
163
|
+
return {};
|
|
164
|
+
}
|
|
165
|
+
const matched = pools
|
|
166
|
+
.filter((window) => window.resetsAt !== undefined && Math.abs(window.resetsAt - resetsAt) <= SAME_RESET_SECONDS)
|
|
167
|
+
.reduce<UsageWindow | undefined>((fullest, window) => (fullest === undefined || window.utilization > fullest.utilization ? window : fullest), undefined);
|
|
168
|
+
if (matched !== undefined) {
|
|
169
|
+
return { pool: matched.kind };
|
|
170
|
+
}
|
|
171
|
+
return judgedPools(refusal, usage) === undefined ? {} : { poolUnpublished: true };
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A refusal as the daemon's ledger files it: a spent allowance stamped with what the accounts' readings say of it now and
|
|
176
|
+
* will not say once its pool has moved on, the reset of the pool it was refused in where it named no instant
|
|
177
|
+
* (refusedPoolReset) and the pool its instant is the reset of where it named one (refusedPoolOf). Anything else as it came.
|
|
178
|
+
*/
|
|
179
|
+
export const stampedRefusal = (refusal: ProviderRefusal, accounts: readonly ServiceFacts[]): ProviderRefusal => {
|
|
180
|
+
const stamped: ProviderRefusal = { ...refusal, ...refusedPoolOf(refusal, accounts) };
|
|
181
|
+
const poolResetsAt = refusedPoolReset(refusal, accounts);
|
|
182
|
+
if (poolResetsAt !== undefined) {
|
|
183
|
+
stamped.poolResetsAt = poolResetsAt;
|
|
184
|
+
}
|
|
185
|
+
return stamped;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Whether a reading since shows the allowance a refusal named its instant for renewed early, so it is over before then.
|
|
189
|
+
// Resolved as it was filed: the very pool it was refused in opened since with room (a pool the reading no longer names,
|
|
190
|
+
// or one with no reset or length, says nothing, and the instant stands). Resolved to no published pool, or filed before
|
|
191
|
+
// refusals were resolved: every pool long enough to have held it did (refusedInstant `renewed`).
|
|
192
|
+
const renewedSince = (refusal: ProviderRefusal, usage: AccountUsage | undefined): boolean => {
|
|
193
|
+
const { resetsAt } = refusal;
|
|
194
|
+
if (refusal.pool === undefined && refusal.poolUnpublished !== true) {
|
|
195
|
+
return refusedInstant(refusal, usage) === "renewed";
|
|
196
|
+
}
|
|
197
|
+
if (resetsAt === undefined || usage === undefined || usage.measuredAt <= refusal.at) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
if (refusal.poolUnpublished === true) {
|
|
201
|
+
const pools = judgedPools(refusal, usage);
|
|
202
|
+
return pools !== undefined && holdersRenewed(refusal, pools, resetsAt);
|
|
203
|
+
}
|
|
204
|
+
const window = usage.windows.find((entry) => entry.kind === refusal.pool && entry.lapsedAt === undefined);
|
|
205
|
+
const period = window === undefined ? undefined : windowPeriod(window)?.seconds;
|
|
206
|
+
return window?.resetsAt !== undefined && period !== undefined && renewedAfter(refusal, { window, reset: window.resetsAt, period });
|
|
111
207
|
};
|
|
112
208
|
|
|
113
209
|
/*
|
|
@@ -118,7 +214,7 @@ export const refusedInstant = (refusal: ProviderRefusal, usage: AccountUsage | u
|
|
|
118
214
|
* refusedPoolReset, unless this account was read since and a reading outranks a stamp: 2026-10-21). Where it is known to
|
|
119
215
|
* be this account's (it named it, or the provider has no other), the provider's own instant outranks a usage endpoint
|
|
120
216
|
* that kept showing room while it refused, so it stands until then, unless a reading since shows that pool renewed
|
|
121
|
-
* early (
|
|
217
|
+
* early (renewedSince); else this account's reading of the refused pool
|
|
122
218
|
* since it answers it. A nameless one is one account saying no and nothing saying which, so on each account it is only
|
|
123
219
|
* that account's own reading that answers it, whatever kind of allowance or instant it named: held to its instant, one
|
|
124
220
|
* team account's refusal held every Codex account spent for a day, the free ones reading 0% used (2026-10-19).
|
|
@@ -131,7 +227,7 @@ const verdictOn = (refusal: ProviderRefusal, facts: ServiceFacts, accounts: numb
|
|
|
131
227
|
}
|
|
132
228
|
const known = refusal.account !== undefined || accounts <= 1;
|
|
133
229
|
if (refusal.kind === "limit" && refusal.resetsAt !== undefined && known) {
|
|
134
|
-
return
|
|
230
|
+
return renewedSince(refusal, facts.usage) ? "answered" : "standing";
|
|
135
231
|
}
|
|
136
232
|
return answers(refusal, facts, known ? undefined : true) ? "answered" : "standing";
|
|
137
233
|
};
|
|
@@ -139,14 +235,11 @@ const verdictOn = (refusal: ProviderRefusal, facts: ServiceFacts, accounts: numb
|
|
|
139
235
|
/**
|
|
140
236
|
* How a refusal on file stands over a provider's accounts: `gone` once the account it named is disconnected, `standing`
|
|
141
237
|
* while it stands over any account it covers (verdictOn), else `answered`. An empty list is one not loaded yet, so the
|
|
142
|
-
* refusal stands. Judged by the daemon and every open window at the same instant
|
|
238
|
+
* refusal stands. Judged by the daemon and every open window at the same instant (`now`, the sandbox's clock in epoch ms)
|
|
239
|
+
* from what the refusal carries and each account's reading as it stands then (factsAt).
|
|
143
240
|
*/
|
|
144
|
-
export const refusalVerdict = (
|
|
145
|
-
refusal
|
|
146
|
-
accounts: readonly ServiceFacts[],
|
|
147
|
-
now: number = Date.now(),
|
|
148
|
-
): "standing" | "answered" | "gone" => {
|
|
149
|
-
const covered = accounts.filter((facts) => (refusal.account ?? facts.account) === facts.account);
|
|
241
|
+
export const refusalVerdict = (refusal: ProviderRefusal, accounts: readonly ServiceFacts[], now: number): "standing" | "answered" | "gone" => {
|
|
242
|
+
const covered = accounts.map((facts) => factsAt(facts, now)).filter((facts) => (refusal.account ?? facts.account) === facts.account);
|
|
150
243
|
if (accounts.length === 0) {
|
|
151
244
|
return "standing";
|
|
152
245
|
}
|
|
@@ -170,7 +263,7 @@ const unstampedPast = (refusal: ProviderRefusal, now: number): ProviderRefusal =
|
|
|
170
263
|
// stands until the instant the provider named, and no reading answers it before then (verdictOn: named, with its
|
|
171
264
|
// instant). Dated before anything on file, since the translator keeps no instant it began, so a refusal filed since
|
|
172
265
|
// (a revoked sign-in) is the account's newer word. One past its instant is over.
|
|
173
|
-
export const cooldownRefusals = (facts: ServiceFacts, now: number
|
|
266
|
+
export const cooldownRefusals = (facts: ServiceFacts, now: number): ProviderRefusal[] =>
|
|
174
267
|
(facts.cooldowns ?? []).flatMap((cooldown) =>
|
|
175
268
|
cooldown.until * 1000 > now
|
|
176
269
|
? [{ at: 0, kind: "limit", account: facts.account, model: cooldown.model, resetsAt: cooldown.until, message: `Out of allowance for ${cooldown.model} on this account.` }]
|
|
@@ -179,14 +272,15 @@ export const cooldownRefusals = (facts: ServiceFacts, now: number = Date.now()):
|
|
|
179
272
|
|
|
180
273
|
/**
|
|
181
274
|
* The refusals still standing over each of a provider's accounts (those naming it, those naming none, and the
|
|
182
|
-
* translator's per-model benches of it), each judged on that account's own facts.
|
|
275
|
+
* translator's per-model benches of it), each judged on that account's own facts as they stand at `now` (factsAt).
|
|
183
276
|
*/
|
|
184
|
-
export const standingOver = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile, now: number
|
|
185
|
-
const
|
|
277
|
+
export const standingOver = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile, now: number): ((account: string) => RefusalsOnFile) => {
|
|
278
|
+
const current = accounts.map((facts) => factsAt(facts, now));
|
|
279
|
+
const live = refusals.filter((refusal) => refusalVerdict(refusal, current, now) !== "gone");
|
|
186
280
|
return (account) => {
|
|
187
|
-
const facts =
|
|
281
|
+
const facts = current.find((entry) => entry.account === account) ?? { account };
|
|
188
282
|
const filed = live
|
|
189
|
-
.filter((refusal) => (refusal.account ?? account) === account && verdictOn(refusal, facts,
|
|
283
|
+
.filter((refusal) => (refusal.account ?? account) === account && verdictOn(refusal, facts, current.length, now) === "standing")
|
|
190
284
|
.map((refusal) => unstampedPast(refusal, now));
|
|
191
285
|
return [...cooldownRefusals(facts, now), ...filed];
|
|
192
286
|
};
|