@intentic/sandbox-contract 1.403.0 → 1.404.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/dist/contracts/agent.contract.d.ts +8 -0
- package/dist/contracts/agent.contract.d.ts.map +1 -1
- package/dist/contracts/agents.contract.d.ts +40 -0
- package/dist/contracts/agents.contract.d.ts.map +1 -1
- package/dist/contracts/runner.contract.d.ts +246 -246
- package/dist/contracts/seal.contract.d.ts +10 -0
- package/dist/contracts/seal.contract.d.ts.map +1 -1
- package/dist/contracts/system.contract.d.ts +30 -18
- package/dist/contracts/system.contract.d.ts.map +1 -1
- package/dist/contracts/translator.contract.d.ts +16 -0
- package/dist/contracts/translator.contract.d.ts.map +1 -1
- package/dist/events/system-events.d.ts +8 -0
- package/dist/events/system-events.d.ts.map +1 -1
- package/dist/models/plan-pools.d.ts +5 -34
- package/dist/models/plan-pools.d.ts.map +1 -1
- package/dist/models/plan-pools.js +4 -95
- package/dist/models/plan-pools.js.map +1 -1
- package/dist/models/verdict/pool-gates.d.ts +17 -0
- package/dist/models/verdict/pool-gates.d.ts.map +1 -0
- package/dist/models/verdict/pool-gates.js +41 -0
- package/dist/models/verdict/pool-gates.js.map +1 -0
- package/dist/models/verdict/refusal-verdict.d.ts +21 -0
- package/dist/models/verdict/refusal-verdict.d.ts.map +1 -0
- package/dist/models/verdict/refusal-verdict.js +68 -0
- package/dist/models/verdict/refusal-verdict.js.map +1 -0
- package/dist/protocol/raw/raw-json-routes.d.ts +196 -0
- package/dist/protocol/raw/raw-json-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-json-routes.js +5 -0
- package/dist/protocol/raw/raw-json-routes.js.map +1 -1
- package/dist/protocol/raw/raw-routes.d.ts +31 -0
- package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-routes.js +7 -0
- package/dist/protocol/raw/raw-routes.js.map +1 -1
- package/dist/schemas/agent-next.d.ts +1 -0
- package/dist/schemas/agent-next.d.ts.map +1 -1
- package/dist/schemas/agent-next.js +4 -0
- package/dist/schemas/agent-next.js.map +1 -1
- package/dist/schemas/agent.d.ts +126 -0
- package/dist/schemas/agent.d.ts.map +1 -1
- package/dist/schemas/agent.js +15 -4
- package/dist/schemas/agent.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/diagnostics.d.ts +248 -0
- package/dist/schemas/diagnostics.d.ts.map +1 -0
- package/dist/schemas/diagnostics.js +114 -0
- package/dist/schemas/diagnostics.js.map +1 -0
- package/dist/schemas/hosts.d.ts +1 -0
- package/dist/schemas/hosts.d.ts.map +1 -1
- package/dist/schemas/hosts.js +2 -1
- package/dist/schemas/hosts.js.map +1 -1
- package/dist/schemas/links.d.ts +6 -0
- package/dist/schemas/links.d.ts.map +1 -1
- package/dist/schemas/links.js +6 -0
- package/dist/schemas/links.js.map +1 -1
- package/dist/schemas/providers/plan-limits.d.ts +22 -1
- package/dist/schemas/providers/plan-limits.d.ts.map +1 -1
- package/dist/schemas/providers/plan-limits.js +10 -1
- package/dist/schemas/providers/plan-limits.js.map +1 -1
- package/dist/schemas/providers/refusals.d.ts +5 -0
- package/dist/schemas/providers/refusals.d.ts.map +1 -1
- package/dist/schemas/providers/refusals.js +4 -0
- package/dist/schemas/providers/refusals.js.map +1 -1
- package/package.json +5 -5
- package/src/models/plan-pools.test.ts +96 -5
- package/src/models/plan-pools.ts +22 -212
- package/src/models/verdict/pool-gates.ts +94 -0
- package/src/models/verdict/refusal-verdict.ts +148 -0
- package/src/protocol/raw/raw-json-routes.ts +7 -0
- package/src/protocol/raw/raw-routes.ts +17 -0
- package/src/schemas/agent-next.ts +6 -0
- package/src/schemas/agent.ts +50 -29
- package/src/schemas/diagnostics.ts +166 -0
- package/src/schemas/hosts.ts +8 -2
- package/src/schemas/links.ts +9 -0
- package/src/schemas/providers/plan-limits.ts +16 -1
- package/src/schemas/providers/refusals.ts +12 -3
package/src/models/plan-pools.ts
CHANGED
|
@@ -1,98 +1,26 @@
|
|
|
1
|
-
import type { AccountState, AccountUsage, ProviderRefusal, UsageWindow
|
|
1
|
+
import type { AccountState, AccountUsage, ProviderRefusal, UsageWindow } from "../schemas/providers/plan-limits.js";
|
|
2
2
|
import { humanizeModelId } from "./model-label.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export interface ModelRef {
|
|
25
|
-
// Wire model id.
|
|
26
|
-
readonly id: string;
|
|
27
|
-
// Display name, when available; matched too, since the tier word may be in either id or label.
|
|
28
|
-
readonly label?: string | undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Splits into lowercase alnum words for whole-word matching: a substring test would match "opus" inside a word that
|
|
32
|
-
// merely contains it.
|
|
33
|
-
export const wordsOf = (text: string): readonly string[] =>
|
|
34
|
-
text
|
|
35
|
-
.toLowerCase()
|
|
36
|
-
.split(/[^a-z0-9]+/)
|
|
37
|
-
.filter(Boolean);
|
|
38
|
-
|
|
39
|
-
// Whether `needle` appears as a contiguous run of whole words within `words`.
|
|
40
|
-
export const runOfWords = (words: readonly string[], needle: readonly string[]): boolean =>
|
|
41
|
-
needle.length > 0 && words.some((_, at) => needle.every((word, index) => words[at + index] === word));
|
|
42
|
-
|
|
43
|
-
const nameMatches = (name: string, model: ModelRef): boolean => {
|
|
44
|
-
const needle = wordsOf(name);
|
|
45
|
-
return runOfWords(wordsOf(model.id), needle) || (model.label !== undefined && runOfWords(wordsOf(model.label), needle));
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export const gatesModel = (gates: WindowGates, model: ModelRef): boolean =>
|
|
49
|
-
gates === "all" ? true : gates === "none" ? false : gates.models.some((name) => nameMatches(name, model));
|
|
50
|
-
|
|
51
|
-
// Served models no pool in `windows` gates: what a reading cannot say anything about. A pool gated `none` covers
|
|
52
|
-
// nothing, and one gated `all` covers every model.
|
|
53
|
-
export const uncoveredModels = (windows: readonly UsageWindow[], served: readonly ModelRef[]): string[] =>
|
|
54
|
-
served.filter((model) => !windows.some((window) => gatesModel(window.gates, model))).map((model) => model.id);
|
|
55
|
-
|
|
56
|
-
// Pools that gate this model, or, with no model given, every pool that gates anything.
|
|
57
|
-
export const gatingWindows = (usage: AccountUsage | undefined, model?: ModelRef): readonly UsageWindow[] =>
|
|
58
|
-
(usage?.windows ?? []).filter((window) => (model === undefined ? window.gates !== "none" : gatesModel(window.gates, model)));
|
|
59
|
-
|
|
60
|
-
const fullest = (windows: readonly UsageWindow[]): UsageWindow | undefined =>
|
|
61
|
-
windows.reduce<UsageWindow | undefined>((worst, window) => (worst === undefined || window.utilization > worst.utilization ? window : worst), undefined);
|
|
62
|
-
|
|
63
|
-
// The pool that gates the next turn: the fullest of the windows this model spends. Undefined means nothing gates it (or
|
|
64
|
-
// nothing was measured) — distinct from a measured 0%.
|
|
65
|
-
export const bindingWindow = (usage: AccountUsage | undefined, model?: ModelRef): UsageWindow | undefined => fullest(gatingWindows(usage, model));
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Whether a turn answered on `served` proves `other` had room on this account too: every pool `other` spends gates
|
|
69
|
-
* `served` as well, so the answer went through each of them. False where nothing is read, and where `other` spends a pool
|
|
70
|
-
* of its own (Claude's Opus slice, a model the plan has no entitlement to), which another model's answer says nothing
|
|
71
|
-
* about. Codex meters every model from one allowance, so a Luna turn going through proves Sol's allowance open as well.
|
|
72
|
-
*/
|
|
73
|
-
export const answerCovers = (usage: AccountUsage | undefined, served: ModelRef, other: ModelRef): boolean => {
|
|
74
|
-
const pools = gatingWindows(usage, other);
|
|
75
|
-
return pools.length > 0 && pools.every((window) => gatesModel(window.gates, served));
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
// The model's own metered pool, by most specific name match; distinct from bindingWindow, which may answer with an
|
|
79
|
-
// all-models pool. Two matches tied in specificity return undefined rather than guess.
|
|
80
|
-
export const scopedWindow = (usage: AccountUsage | undefined, model: ModelRef): UsageWindow | undefined => {
|
|
81
|
-
const matched = (usage?.windows ?? [])
|
|
82
|
-
.flatMap((window) => {
|
|
83
|
-
if (window.gates === "all" || window.gates === "none") {
|
|
84
|
-
return [];
|
|
85
|
-
}
|
|
86
|
-
const specificity = Math.max(0, ...window.gates.models.filter((name) => nameMatches(name, model)).map((name) => wordsOf(name).length));
|
|
87
|
-
return specificity === 0 ? [] : [{ window, specificity }];
|
|
88
|
-
})
|
|
89
|
-
.toSorted((left, right) => right.specificity - left.specificity);
|
|
90
|
-
const best = matched[0];
|
|
91
|
-
if (best === undefined || matched[1]?.specificity === best.specificity) {
|
|
92
|
-
return undefined;
|
|
93
|
-
}
|
|
94
|
-
return best.window;
|
|
95
|
-
};
|
|
3
|
+
import { bindingWindow, gatesModel, gatingWindows, type ModelRef, pinnedFull, provenFull, SPENT_UTILIZATION } from "./verdict/pool-gates.js";
|
|
4
|
+
import { type RefusalsOnFile, type ServiceFacts, standingOver } from "./verdict/refusal-verdict.js";
|
|
5
|
+
|
|
6
|
+
// Which pool gates a model (verdict/pool-gates.ts) and how each refusal on file stands over each account
|
|
7
|
+
// (verdict/refusal-verdict.ts), kept beside this module and served from it, so every caller reads the one rule through
|
|
8
|
+
// one door.
|
|
9
|
+
export {
|
|
10
|
+
answerCovers,
|
|
11
|
+
bindingWindow,
|
|
12
|
+
gatesModel,
|
|
13
|
+
gatingWindows,
|
|
14
|
+
type ModelRef,
|
|
15
|
+
pinnedFull,
|
|
16
|
+
provenFull,
|
|
17
|
+
runOfWords,
|
|
18
|
+
scopedWindow,
|
|
19
|
+
SPENT_UTILIZATION,
|
|
20
|
+
uncoveredModels,
|
|
21
|
+
wordsOf,
|
|
22
|
+
} from "./verdict/pool-gates.js";
|
|
23
|
+
export { refusalVerdict, refusedPoolReset, type RefusalsOnFile, type ServiceFacts, standingOver } from "./verdict/refusal-verdict.js";
|
|
96
24
|
|
|
97
25
|
// How long a pool's window runs, read off the provider's own key and whatever name it published. One implementation,
|
|
98
26
|
// because two things need it and must agree: a narrow column names the window by `short`, and a reading with no
|
|
@@ -196,124 +124,6 @@ export const currentWindows = (usage: AccountUsage, now: number): UsageWindow[]
|
|
|
196
124
|
// gate, listing and surface reads. The daemon and the editor run it on the same facts (the account rows and every
|
|
197
125
|
// refusal on file); neither keeps a judged copy, so the two cannot disagree.
|
|
198
126
|
|
|
199
|
-
/** Everything the serviceability rule reads of one account, as the account rows already carry it. */
|
|
200
|
-
export interface ServiceFacts {
|
|
201
|
-
// The daemon's key: a native account's id, a routed credential's auth-file name.
|
|
202
|
-
readonly account: string;
|
|
203
|
-
readonly needsReauth?: boolean | undefined;
|
|
204
|
-
// Why the sign-in needs renewing, where the provider said.
|
|
205
|
-
readonly detail?: string | undefined;
|
|
206
|
-
readonly seatRefusal?: string | undefined;
|
|
207
|
-
readonly cooling?: { readonly until?: number | undefined; readonly reason?: string | undefined; readonly verify?: string | undefined } | undefined;
|
|
208
|
-
readonly usage?: AccountUsage | undefined;
|
|
209
|
-
// How the account is named to a person (its label, a routed credential's address); a surface that quotes a figure
|
|
210
|
-
// says whose it is.
|
|
211
|
-
readonly label?: string | undefined;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Every refusal on file for one provider: what its accounts are judged against, each standing until something answers it. */
|
|
215
|
-
export type RefusalsOnFile = readonly ProviderRefusal[];
|
|
216
|
-
|
|
217
|
-
// A spent allowance is over once a reading with room lands after it; a refused credential once any reading does (the
|
|
218
|
-
// token worked). Nothing a reading says answers an entitlement refusal: a seatless account authenticates and reads fine.
|
|
219
|
-
// Nor does the refused account's own reading answer an allowance it does not measure (`unmetered`: an admin's credit).
|
|
220
|
-
// `measured` says whether this reading can speak to the allowance at all: false for that account's own, true for any
|
|
221
|
-
// account's where nothing says which one refused (refusalVerdict).
|
|
222
|
-
const answers = (refusal: ProviderRefusal, facts: ServiceFacts, measured: boolean = refusal.unmetered !== true): boolean => {
|
|
223
|
-
const measuredAt = facts.usage?.measuredAt;
|
|
224
|
-
if (refusal.kind === "entitlement" || !measured || measuredAt === undefined || measuredAt <= refusal.at) {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
if (refusal.kind === "auth") {
|
|
228
|
-
return facts.needsReauth !== true;
|
|
229
|
-
}
|
|
230
|
-
const binding = bindingWindow(facts.usage);
|
|
231
|
-
return binding !== undefined && binding.utilization < SPENT_UTILIZATION;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* When the pool a spent allowance that named no instant was refused in reopens, in epoch seconds: the soonest reset of
|
|
236
|
-
* the binding pool for the model it named (the pool judgedReading pins full) on the accounts it may have refused, its own
|
|
237
|
-
* where it named one, any where it named none, as any account's room answers a nameless one. The daemon's ledger stamps
|
|
238
|
-
* it on the refusal as it files it (`poolResetsAt`), while the readings still show that pool open: once it reopens the
|
|
239
|
-
* reading lapses it to an empty pool and a different one binds, so it cannot be read back off the reading later.
|
|
240
|
-
* Undefined for a refusal that named its instant, one no reading measures (`unmetered`), a person's mark (lifted by a
|
|
241
|
-
* person or a turn, not a window), and where no reading publishes a reset still ahead of the refusal.
|
|
242
|
-
*/
|
|
243
|
-
export const refusedPoolReset = (refusal: ProviderRefusal, accounts: readonly ServiceFacts[]): number | undefined => {
|
|
244
|
-
if (refusal.kind !== "limit" || refusal.resetsAt !== undefined || refusal.unmetered === true || refusal.byHand === true) {
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
const model = refusal.model === undefined ? undefined : { id: refusal.model };
|
|
248
|
-
const resets = accounts
|
|
249
|
-
.filter((facts) => refusal.account === undefined || facts.account === refusal.account)
|
|
250
|
-
.flatMap((facts) => {
|
|
251
|
-
const reset = bindingWindow(facts.usage, model)?.resetsAt;
|
|
252
|
-
return reset !== undefined && reset * 1000 > refusal.at ? [reset] : [];
|
|
253
|
-
});
|
|
254
|
-
return resets.length === 0 ? undefined : Math.min(...resets);
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* How a refusal on file stands against everything read since, over every account the provider holds: `gone` once the
|
|
259
|
-
* account it named is disconnected, `answered` once the instant it named has passed (or, where it named none, the
|
|
260
|
-
* reset of the pool it was refused in, stamped as it was filed: refusedPoolReset) or once that account (or, for a refusal
|
|
261
|
-
* naming none, any account) has a later reading that contradicts it, else `standing`. An empty list is one not loaded
|
|
262
|
-
* yet, so the refusal stands. Judged by the daemon and every open window at the same instant from what the refusal
|
|
263
|
-
* carries: nothing waits for a fresh reading or a ledger write to flip it. 2026-10-20: a refusal naming no instant stood
|
|
264
|
-
* past its pool's reset until the next reading landed, up to the 15-minute idle sweep, while the picker already said
|
|
265
|
-
* "back at" that reset. A stamp is this sandbox's guess, not the provider's word, so a reading taken after it outranks
|
|
266
|
-
* it: 2026-10-21, a weekly pool read at a rounded 100 once the five-hour one it was stamped with reopened, and the
|
|
267
|
-
* account read ready with no room, so every turn let through was refused again.
|
|
268
|
-
*/
|
|
269
|
-
export const refusalVerdict = (
|
|
270
|
-
refusal: ProviderRefusal,
|
|
271
|
-
accounts: readonly ServiceFacts[],
|
|
272
|
-
now: number = Date.now(),
|
|
273
|
-
): "standing" | "answered" | "gone" => {
|
|
274
|
-
const named = accounts.filter((facts) => facts.account === refusal.account);
|
|
275
|
-
if (refusal.account !== undefined && named.length === 0) {
|
|
276
|
-
return accounts.length === 0 ? "standing" : "gone";
|
|
277
|
-
}
|
|
278
|
-
const whose = refusal.account === undefined ? accounts : named;
|
|
279
|
-
// The provider's own "try again at" is over at that instant, whichever account it refused; so, where it named none,
|
|
280
|
-
// is the refused pool's reset the ledger stamped, unless a reading since says otherwise (read below).
|
|
281
|
-
const over = refusal.resetsAt ?? refusal.poolResetsAt;
|
|
282
|
-
const readSince = (instant: number): boolean => whose.some((facts) => (facts.usage?.measuredAt ?? 0) > instant * 1000);
|
|
283
|
-
if (refusal.kind === "limit" && over !== undefined && over * 1000 <= now && (refusal.resetsAt !== undefined || !readSince(over))) {
|
|
284
|
-
return "answered";
|
|
285
|
-
}
|
|
286
|
-
// Where it is known which account it refused (it named one, or there is only one), the provider's own instant
|
|
287
|
-
// outranks a usage endpoint that kept showing room while it refused: over at that instant, and not before.
|
|
288
|
-
const known = refusal.account !== undefined || accounts.length <= 1;
|
|
289
|
-
if (refusal.kind === "limit" && refusal.resetsAt !== undefined && known) {
|
|
290
|
-
return "standing";
|
|
291
|
-
}
|
|
292
|
-
if (known) {
|
|
293
|
-
return whose.some((facts) => answers(refusal, facts)) ? "answered" : "standing";
|
|
294
|
-
}
|
|
295
|
-
/* A nameless refusal over several accounts is one of them saying no, and nothing says which: the translator benches
|
|
296
|
-
the one it refused and routes on, so any of them reading room since answers it, whatever kind of allowance it was
|
|
297
|
-
and whatever instant it named. Held to its instant, or to an allowance no reading measures, one team account's
|
|
298
|
-
refusal held every Codex account spent for a day, the two free ones reading 0% used included (2026-10-19). */
|
|
299
|
-
return accounts.some((facts) => answers(refusal, facts, true)) ? "answered" : "standing";
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
// A stamp a reading has outlived (refusalVerdict) says nothing of when the pool now holding the refusal reopens: that
|
|
303
|
-
// pool's own reset does.
|
|
304
|
-
const unstampedPast = (refusal: ProviderRefusal, now: number): ProviderRefusal => {
|
|
305
|
-
if (refusal.poolResetsAt === undefined || refusal.poolResetsAt * 1000 > now) {
|
|
306
|
-
return refusal;
|
|
307
|
-
}
|
|
308
|
-
const { poolResetsAt: _outlived, ...standing } = refusal;
|
|
309
|
-
return standing;
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
/** The refusals still standing over each of a provider's accounts: those naming it, and those naming none. */
|
|
313
|
-
export const standingOver = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile, now: number = Date.now()): ((account: string) => RefusalsOnFile) => {
|
|
314
|
-
const standing = refusals.filter((refusal) => refusalVerdict(refusal, accounts, now) === "standing").map((refusal) => unstampedPast(refusal, now));
|
|
315
|
-
return (account) => standing.filter((refusal) => (refusal.account ?? account) === account);
|
|
316
|
-
};
|
|
317
127
|
|
|
318
128
|
// The account reopens for this question when the pools holding it shut do: all of them for a model's own question (or
|
|
319
129
|
// the pools gating every model), the first to reopen when only per-model slices are full. Unknown if any is unpublished.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { AccountUsage, UsageWindow, WindowGates } from "../../schemas/providers/plan-limits.js";
|
|
2
|
+
|
|
3
|
+
// Single reading of which account pool gates a given model (or, with none named, the account's tightest pool), shared
|
|
4
|
+
// by the daemon's picker/refusal logic and the browser's rings and rail. A pool gated `none` is excluded from the
|
|
5
|
+
// no-model case: visible to the account, never a gate on a turn.
|
|
6
|
+
|
|
7
|
+
// 100 = exhaustion (the call will be refused). The one spent line: the daemon's pickers and every surface read it, and
|
|
8
|
+
// anything short of it still has room.
|
|
9
|
+
export const SPENT_UTILIZATION = 100;
|
|
10
|
+
|
|
11
|
+
// Whether a reading proves its pool empty. A pool read at the line by a provider that rounds to whole percents
|
|
12
|
+
// (`rounded`) does not: Anthropic reports 99.5% as 100 and keeps serving turns on it, so treating that as spent strands
|
|
13
|
+
// the last of an allowance (and turns a plain send into one scheduled for the reset). Such a pool is at the line, with
|
|
14
|
+
// no room to rank by; only the provider's refusal (pinnedFull) proves it empty.
|
|
15
|
+
export const provenFull = (window: UsageWindow): boolean => window.utilization >= SPENT_UTILIZATION && window.rounded !== true;
|
|
16
|
+
|
|
17
|
+
// A pool a standing limit refusal shows full: at the line, and proven so whatever the provider's rounding.
|
|
18
|
+
export const pinnedFull = (window: UsageWindow): UsageWindow => {
|
|
19
|
+
const { rounded: _rounded, ...pool } = window;
|
|
20
|
+
return { ...pool, utilization: Math.max(window.utilization, SPENT_UTILIZATION) };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface ModelRef {
|
|
24
|
+
// Wire model id.
|
|
25
|
+
readonly id: string;
|
|
26
|
+
// Display name, when available; matched too, since the tier word may be in either id or label.
|
|
27
|
+
readonly label?: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Splits into lowercase alnum words for whole-word matching: a substring test would match "opus" inside a word that
|
|
31
|
+
// merely contains it.
|
|
32
|
+
export const wordsOf = (text: string): readonly string[] =>
|
|
33
|
+
text
|
|
34
|
+
.toLowerCase()
|
|
35
|
+
.split(/[^a-z0-9]+/)
|
|
36
|
+
.filter(Boolean);
|
|
37
|
+
|
|
38
|
+
// Whether `needle` appears as a contiguous run of whole words within `words`.
|
|
39
|
+
export const runOfWords = (words: readonly string[], needle: readonly string[]): boolean =>
|
|
40
|
+
needle.length > 0 && words.some((_, at) => needle.every((word, index) => words[at + index] === word));
|
|
41
|
+
|
|
42
|
+
const nameMatches = (name: string, model: ModelRef): boolean => {
|
|
43
|
+
const needle = wordsOf(name);
|
|
44
|
+
return runOfWords(wordsOf(model.id), needle) || (model.label !== undefined && runOfWords(wordsOf(model.label), needle));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const gatesModel = (gates: WindowGates, model: ModelRef): boolean =>
|
|
48
|
+
gates === "all" ? true : gates === "none" ? false : gates.models.some((name) => nameMatches(name, model));
|
|
49
|
+
|
|
50
|
+
// Served models no pool in `windows` gates: what a reading cannot say anything about. A pool gated `none` covers
|
|
51
|
+
// nothing, and one gated `all` covers every model.
|
|
52
|
+
export const uncoveredModels = (windows: readonly UsageWindow[], served: readonly ModelRef[]): string[] =>
|
|
53
|
+
served.filter((model) => !windows.some((window) => gatesModel(window.gates, model))).map((model) => model.id);
|
|
54
|
+
|
|
55
|
+
// Pools that gate this model, or, with no model given, every pool that gates anything.
|
|
56
|
+
export const gatingWindows = (usage: AccountUsage | undefined, model?: ModelRef): readonly UsageWindow[] =>
|
|
57
|
+
(usage?.windows ?? []).filter((window) => (model === undefined ? window.gates !== "none" : gatesModel(window.gates, model)));
|
|
58
|
+
|
|
59
|
+
const fullest = (windows: readonly UsageWindow[]): UsageWindow | undefined =>
|
|
60
|
+
windows.reduce<UsageWindow | undefined>((worst, window) => (worst === undefined || window.utilization > worst.utilization ? window : worst), undefined);
|
|
61
|
+
|
|
62
|
+
// The pool that gates the next turn: the fullest of the windows this model spends. Undefined means nothing gates it (or
|
|
63
|
+
// nothing was measured) — distinct from a measured 0%.
|
|
64
|
+
export const bindingWindow = (usage: AccountUsage | undefined, model?: ModelRef): UsageWindow | undefined => fullest(gatingWindows(usage, model));
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether a turn answered on `served` proves `other` had room on this account too: every pool `other` spends gates
|
|
68
|
+
* `served` as well, so the answer went through each of them. False where nothing is read, and where `other` spends a pool
|
|
69
|
+
* of its own (Claude's Opus slice, a model the plan has no entitlement to), which another model's answer says nothing
|
|
70
|
+
* about. Codex meters every model from one allowance, so a Luna turn going through proves Sol's allowance open as well.
|
|
71
|
+
*/
|
|
72
|
+
export const answerCovers = (usage: AccountUsage | undefined, served: ModelRef, other: ModelRef): boolean => {
|
|
73
|
+
const pools = gatingWindows(usage, other);
|
|
74
|
+
return pools.length > 0 && pools.every((window) => gatesModel(window.gates, served));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// The model's own metered pool, by most specific name match; distinct from bindingWindow, which may answer with an
|
|
78
|
+
// all-models pool. Two matches tied in specificity return undefined rather than guess.
|
|
79
|
+
export const scopedWindow = (usage: AccountUsage | undefined, model: ModelRef): UsageWindow | undefined => {
|
|
80
|
+
const matched = (usage?.windows ?? [])
|
|
81
|
+
.flatMap((window) => {
|
|
82
|
+
if (window.gates === "all" || window.gates === "none") {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
const specificity = Math.max(0, ...window.gates.models.filter((name) => nameMatches(name, model)).map((name) => wordsOf(name).length));
|
|
86
|
+
return specificity === 0 ? [] : [{ window, specificity }];
|
|
87
|
+
})
|
|
88
|
+
.toSorted((left, right) => right.specificity - left.specificity);
|
|
89
|
+
const best = matched[0];
|
|
90
|
+
if (best === undefined || matched[1]?.specificity === best.specificity) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
return best.window;
|
|
94
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { AccountUsage, ModelCooldown, ProviderRefusal } from "../../schemas/providers/plan-limits.js";
|
|
2
|
+
import { bindingWindow, SPENT_UTILIZATION } from "./pool-gates.js";
|
|
3
|
+
|
|
4
|
+
// Which refusals on file still speak, and over which account: the half of the serviceability rule plan-pools.ts runs
|
|
5
|
+
// before it lays them on the readings (judgedReading). Every account is judged on its own facts, never another's.
|
|
6
|
+
|
|
7
|
+
/** Everything the serviceability rule reads of one account, as the account rows already carry it. */
|
|
8
|
+
export interface ServiceFacts {
|
|
9
|
+
// The daemon's key: a native account's id, a routed credential's auth-file name.
|
|
10
|
+
readonly account: string;
|
|
11
|
+
readonly needsReauth?: boolean | undefined;
|
|
12
|
+
// Why the sign-in needs renewing, where the provider said.
|
|
13
|
+
readonly detail?: string | undefined;
|
|
14
|
+
readonly seatRefusal?: string | undefined;
|
|
15
|
+
readonly cooling?: { readonly until?: number | undefined; readonly reason?: string | undefined; readonly verify?: string | undefined } | undefined;
|
|
16
|
+
// A routed account's per-model benches as the translator holds them (TranslatorAccount.cooldowns).
|
|
17
|
+
readonly cooldowns?: readonly ModelCooldown[] | undefined;
|
|
18
|
+
readonly usage?: AccountUsage | undefined;
|
|
19
|
+
// How the account is named to a person (its label, a routed credential's address); a surface that quotes a figure
|
|
20
|
+
// says whose it is.
|
|
21
|
+
readonly label?: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Every refusal on file for one provider: what its accounts are judged against, each standing until something answers it. */
|
|
25
|
+
export type RefusalsOnFile = readonly ProviderRefusal[];
|
|
26
|
+
|
|
27
|
+
// A spent allowance is over once a reading with room in the pool its model spends lands after it; a refused credential
|
|
28
|
+
// once any reading does (the token worked). Nothing a reading says answers an entitlement refusal: a seatless account
|
|
29
|
+
// authenticates and reads fine. Nor does the refused account's own reading answer an allowance it does not measure
|
|
30
|
+
// (`unmetered`: an admin's credit). `measured` says whether this reading can speak to the allowance at all: false for
|
|
31
|
+
// that account's own, true for another account's where nothing says which one refused (verdictOn).
|
|
32
|
+
// The refused model's pool, not the account's fullest: 2026-11-08, a Google account with its Claude pool at 0% answered
|
|
33
|
+
// a Gemini refusal, and one with Claude at 100% could never answer a Gemini one with Gemini's pool open.
|
|
34
|
+
const answers = (refusal: ProviderRefusal, facts: ServiceFacts, measured: boolean = refusal.unmetered !== true): boolean => {
|
|
35
|
+
const measuredAt = facts.usage?.measuredAt;
|
|
36
|
+
if (refusal.kind === "entitlement" || !measured || measuredAt === undefined || measuredAt <= refusal.at) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (refusal.kind === "auth") {
|
|
40
|
+
return facts.needsReauth !== true;
|
|
41
|
+
}
|
|
42
|
+
const binding = bindingWindow(facts.usage, refusal.model === undefined ? undefined : { id: refusal.model });
|
|
43
|
+
return binding !== undefined && binding.utilization < SPENT_UTILIZATION;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* When the pool a spent allowance that named no instant was refused in reopens, in epoch seconds: the soonest reset of
|
|
48
|
+
* the binding pool for the model it named (the pool judgedReading pins full) on the accounts it may have refused, its own
|
|
49
|
+
* where it named one, any where it named none, as any account's room answers a nameless one. The daemon's ledger stamps
|
|
50
|
+
* it on the refusal as it files it (`poolResetsAt`), while the readings still show that pool open: once it reopens the
|
|
51
|
+
* reading lapses it to an empty pool and a different one binds, so it cannot be read back off the reading later.
|
|
52
|
+
* Undefined for a refusal that named its instant, one no reading measures (`unmetered`), a person's mark (lifted by a
|
|
53
|
+
* person or a turn, not a window), and where no reading publishes a reset still ahead of the refusal.
|
|
54
|
+
*/
|
|
55
|
+
export const refusedPoolReset = (refusal: ProviderRefusal, accounts: readonly ServiceFacts[]): number | undefined => {
|
|
56
|
+
if (refusal.kind !== "limit" || refusal.resetsAt !== undefined || refusal.unmetered === true || refusal.byHand === true) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const model = refusal.model === undefined ? undefined : { id: refusal.model };
|
|
60
|
+
const resets = accounts
|
|
61
|
+
.filter((facts) => refusal.account === undefined || facts.account === refusal.account)
|
|
62
|
+
.flatMap((facts) => {
|
|
63
|
+
const reset = bindingWindow(facts.usage, model)?.resetsAt;
|
|
64
|
+
return reset !== undefined && reset * 1000 > refusal.at ? [reset] : [];
|
|
65
|
+
});
|
|
66
|
+
return resets.length === 0 ? undefined : Math.min(...resets);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* How one refusal stands over ONE account, judged from that account's facts alone: nothing read of another account
|
|
71
|
+
* answers it there (2026-11-08: one Google account reading room answered every nameless refusal for all 31, within
|
|
72
|
+
* seconds of each, so a refused model read 0% and then 100% again while every turn on it was refused). Over once the
|
|
73
|
+
* instant it named has passed (or, where it named none, the reset of the pool it was refused in, stamped as it was filed:
|
|
74
|
+
* refusedPoolReset, unless this account was read since and a reading outranks a stamp: 2026-10-21). Where it is known to
|
|
75
|
+
* be this account's (it named it, or the provider has no other), the provider's own instant outranks a usage endpoint
|
|
76
|
+
* that kept showing room while it refused, so it stands until then; else this account's reading of the refused pool
|
|
77
|
+
* since it answers it. A nameless one is one account saying no and nothing saying which, so on each account it is only
|
|
78
|
+
* that account's own reading that answers it, whatever kind of allowance or instant it named: held to its instant, one
|
|
79
|
+
* team account's refusal held every Codex account spent for a day, the free ones reading 0% used (2026-10-19).
|
|
80
|
+
*/
|
|
81
|
+
const verdictOn = (refusal: ProviderRefusal, facts: ServiceFacts, accounts: number, now: number): "standing" | "answered" => {
|
|
82
|
+
const over = refusal.resetsAt ?? refusal.poolResetsAt;
|
|
83
|
+
const readSince = over !== undefined && (facts.usage?.measuredAt ?? 0) > over * 1000;
|
|
84
|
+
if (refusal.kind === "limit" && over !== undefined && over * 1000 <= now && (refusal.resetsAt !== undefined || !readSince)) {
|
|
85
|
+
return "answered";
|
|
86
|
+
}
|
|
87
|
+
const known = refusal.account !== undefined || accounts <= 1;
|
|
88
|
+
if (refusal.kind === "limit" && refusal.resetsAt !== undefined && known) {
|
|
89
|
+
return "standing";
|
|
90
|
+
}
|
|
91
|
+
return answers(refusal, facts, known ? undefined : true) ? "answered" : "standing";
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* How a refusal on file stands over a provider's accounts: `gone` once the account it named is disconnected, `standing`
|
|
96
|
+
* while it stands over any account it covers (verdictOn), else `answered`. An empty list is one not loaded yet, so the
|
|
97
|
+
* refusal stands. Judged by the daemon and every open window at the same instant from what the refusal carries.
|
|
98
|
+
*/
|
|
99
|
+
export const refusalVerdict = (
|
|
100
|
+
refusal: ProviderRefusal,
|
|
101
|
+
accounts: readonly ServiceFacts[],
|
|
102
|
+
now: number = Date.now(),
|
|
103
|
+
): "standing" | "answered" | "gone" => {
|
|
104
|
+
const covered = accounts.filter((facts) => (refusal.account ?? facts.account) === facts.account);
|
|
105
|
+
if (accounts.length === 0) {
|
|
106
|
+
return "standing";
|
|
107
|
+
}
|
|
108
|
+
if (covered.length === 0) {
|
|
109
|
+
return "gone";
|
|
110
|
+
}
|
|
111
|
+
return covered.some((facts) => verdictOn(refusal, facts, accounts.length, now) === "standing") ? "standing" : "answered";
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// A stamp a reading has outlived (verdictOn) says nothing of when the pool now holding the refusal reopens: that pool's
|
|
115
|
+
// own reset does.
|
|
116
|
+
const unstampedPast = (refusal: ProviderRefusal, now: number): ProviderRefusal => {
|
|
117
|
+
if (refusal.poolResetsAt === undefined || refusal.poolResetsAt * 1000 > now) {
|
|
118
|
+
return refusal;
|
|
119
|
+
}
|
|
120
|
+
const { poolResetsAt: _outlived, ...standing } = refusal;
|
|
121
|
+
return standing;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// What a translator's per-model bench says, as a refusal filed for its account: a spent allowance on that model that
|
|
125
|
+
// stands until the instant the provider named, and no reading answers it before then (verdictOn: named, with its
|
|
126
|
+
// instant). Dated before anything on file, since the translator keeps no instant it began, so a refusal filed since
|
|
127
|
+
// (a revoked sign-in) is the account's newer word. One past its instant is over.
|
|
128
|
+
export const cooldownRefusals = (facts: ServiceFacts, now: number = Date.now()): ProviderRefusal[] =>
|
|
129
|
+
(facts.cooldowns ?? []).flatMap((cooldown) =>
|
|
130
|
+
cooldown.until * 1000 > now
|
|
131
|
+
? [{ at: 0, kind: "limit", account: facts.account, model: cooldown.model, resetsAt: cooldown.until, message: `Out of allowance for ${cooldown.model} on this account.` }]
|
|
132
|
+
: [],
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The refusals still standing over each of a provider's accounts (those naming it, those naming none, and the
|
|
137
|
+
* translator's per-model benches of it), each judged on that account's own facts.
|
|
138
|
+
*/
|
|
139
|
+
export const standingOver = (accounts: readonly ServiceFacts[], refusals: RefusalsOnFile, now: number = Date.now()): ((account: string) => RefusalsOnFile) => {
|
|
140
|
+
const live = refusals.filter((refusal) => refusalVerdict(refusal, accounts, now) !== "gone");
|
|
141
|
+
return (account) => {
|
|
142
|
+
const facts = accounts.find((entry) => entry.account === account) ?? { account };
|
|
143
|
+
const filed = live
|
|
144
|
+
.filter((refusal) => (refusal.account ?? account) === account && verdictOn(refusal, facts, accounts.length, now) === "standing")
|
|
145
|
+
.map((refusal) => unstampedPast(refusal, now));
|
|
146
|
+
return [...cooldownRefusals(facts, now), ...filed];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
EnvironmentSchema,
|
|
15
15
|
} from "../../schemas/environment.js";
|
|
16
16
|
import { SyncStatusSchema } from "../../schemas/devices.js";
|
|
17
|
+
import { ConversationBundleSchema, DiagnosticsAnswerSchema, DiagnosticsAskSchema, DiagnosticsQuerySchema } from "../../schemas/diagnostics.js";
|
|
17
18
|
import { HostsListSchema } from "../../schemas/hosts.js";
|
|
18
19
|
import { VoicesViewSchema } from "../../schemas/voice.js";
|
|
19
20
|
import {
|
|
@@ -191,6 +192,12 @@ export const RAW_JSON_ROUTES = {
|
|
|
191
192
|
"POST /link/conversations/{id}/messages": { input: LinkMessageSchema, output: LinkMessageReceiptSchema },
|
|
192
193
|
"POST /link/conversations/{id}/answer": { input: LinkAnswerSchema, output: OkSchema },
|
|
193
194
|
"POST /link/conversations/{id}/stop": { output: LinkTaskStatusSchema },
|
|
195
|
+
// A link's records: the diagnostics answer and a conversation's bundle. The fleet reads answer as the `agents`
|
|
196
|
+
// command's own routes do, which have no shared schema.
|
|
197
|
+
"POST /link/records/diagnostics": { input: DiagnosticsQuerySchema, output: DiagnosticsAnswerSchema },
|
|
198
|
+
"GET /link/records/fleet/{handle}/bundle": { output: ConversationBundleSchema },
|
|
199
|
+
"POST /diagnostics": { input: DiagnosticsAskSchema, output: DiagnosticsAnswerSchema },
|
|
200
|
+
"GET /fleet/{handle}/bundle": { output: ConversationBundleSchema },
|
|
194
201
|
// An accounts link: what it may use, one static key's copy, and a lent sign-in's per-turn credential.
|
|
195
202
|
"GET /link/accounts": { output: LinkAccountsSchema },
|
|
196
203
|
"POST /link/accounts/copy": { input: SharedAccountRefSchema, output: LinkAccountCopySchema },
|
|
@@ -168,6 +168,11 @@ export const RAW_ROUTES = {
|
|
|
168
168
|
"POST /fleet/message": { agent: true },
|
|
169
169
|
"POST /fleet/resume": { agent: true },
|
|
170
170
|
"GET /fleet/{handle}": { agent: true },
|
|
171
|
+
// One conversation's debug bundle (schemas/diagnostics.ts): `agents bundle`, and the editor's download of it. It
|
|
172
|
+
// carries the daemon's log lines about it, so a person reads it at the floor the logs are read at.
|
|
173
|
+
"GET /fleet/{handle}/bundle": { agent: true, floor: "maintainer", control: "never" },
|
|
174
|
+
// The `diagnostics` command: one read-only question of the daemon's records, or of a linked sandbox's with `on`.
|
|
175
|
+
"POST /diagnostics": { agent: true, floor: "maintainer", control: "never" },
|
|
171
176
|
// The `devices push` CLI: a program the agent built, carried to one of the owner's computers in chunks; held to the
|
|
172
177
|
// devices the calling conversation's turn mounts, and refused by the machine itself unless "Run programs" is on.
|
|
173
178
|
"POST /devices/{name}/artifacts": { agent: true, control: "never" },
|
|
@@ -269,6 +274,11 @@ export const RAW_ROUTES = {
|
|
|
269
274
|
// Its sibling for MCP: a hooked runtime's (Cursor's) calls to its turn's MCP servers, on a session signed the same
|
|
270
275
|
// way, naming the provider and the one server it may forward to. Held open while a server streams its answer.
|
|
271
276
|
"ALL /privacy/mcp/{session}": { auth: "door", control: "never", stream: true },
|
|
277
|
+
// The translator's gateway: every routed loop's model requests, on the signed session its base URL carries (the
|
|
278
|
+
// daemon's agent/providers/translator-gateway-session.ts), which names the provider, and on the translator's bearer
|
|
279
|
+
// too, both checked in the handler. Each request naming a model is placed on one of the provider's accounts before
|
|
280
|
+
// the translator sees it, so a refusal names the account that gave it. Held open while a model streams its answer.
|
|
281
|
+
"ALL /translator-gateway/{session}/*": { auth: "door", control: "never", stream: true },
|
|
272
282
|
// Inbound links: another of the owner's sandboxes handing this one tasks (schemas/links.ts). Minted, re-termed and
|
|
273
283
|
// revoked by the owner alone, checked in the handler like a control token; a waiting task is the owner's to allow.
|
|
274
284
|
"GET /system/links": { control: "never" },
|
|
@@ -284,6 +294,13 @@ export const RAW_ROUTES = {
|
|
|
284
294
|
"POST /link/conversations/{id}/messages": { auth: "door", control: "never" },
|
|
285
295
|
"POST /link/conversations/{id}/answer": { auth: "door", control: "never" },
|
|
286
296
|
"POST /link/conversations/{id}/stop": { auth: "door", control: "never" },
|
|
297
|
+
// A link's records doors (schemas/links.ts `records`): the asking sandbox reading this one's conversations, logs and
|
|
298
|
+
// diagnostics without starting a turn. Each checks the link token itself and answers only while the link's terms
|
|
299
|
+
// share records; none can change anything.
|
|
300
|
+
"GET /link/records/fleet": { auth: "door", control: "never" },
|
|
301
|
+
"GET /link/records/fleet/{handle}": { auth: "door", control: "never" },
|
|
302
|
+
"GET /link/records/fleet/{handle}/bundle": { auth: "door", control: "never" },
|
|
303
|
+
"POST /link/records/diagnostics": { auth: "door", control: "never" },
|
|
287
304
|
// An accounts link's doors (schemas/providers/account-sharing.ts): the accounts its terms name, a static key's copy,
|
|
288
305
|
// and a lent sign-in's access token per turn, re-minted mid-turn. The refresh token never leaves; each door checks the
|
|
289
306
|
// link token itself and answers only for what the terms name.
|
|
@@ -25,6 +25,12 @@ export const AgentNextSchema = z.discriminatedUnion("kind", [
|
|
|
25
25
|
z.object({
|
|
26
26
|
kind: z.literal("land"),
|
|
27
27
|
after: z.string().describe("The conversation whose landed work the first booked message waits for."),
|
|
28
|
+
// A modifier rather than a kind of its own, so a window older than it still reads the wait (as a land, which it
|
|
29
|
+
// says a little wrong) rather than failing the whole card.
|
|
30
|
+
turn: z
|
|
31
|
+
.literal(true)
|
|
32
|
+
.optional()
|
|
33
|
+
.describe("Set when that message waits for the conversation's turn to end cleanly with work to look at, not for its work to land (AgentTurn `sendAfterTurn`)."),
|
|
28
34
|
count: z.number().int().min(1).describe("How many messages are booked to go by themselves."),
|
|
29
35
|
}),
|
|
30
36
|
]);
|