pi-harness-runtime 1.1.506 → 1.1.507
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/CHANGELOG.md +82 -0
- package/harness/provider-quota-reset.ts +53 -19
- package/harness/provider-recovery-runtime.ts +20 -0
- package/harness/quota-capture.ts +150 -0
- package/harness/quota-low-remaining.ts +75 -0
- package/harness/quota-reset-duration.ts +87 -0
- package/harness/quota-threshold.ts +117 -0
- package/harness/resume-scope.ts +85 -0
- package/harness/scope-expansion.ts +438 -0
- package/index.ts +212 -7
- package/mirror.ts +4 -0
- package/package.json +1 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/notification/dist/adapters/detail-format.d.ts +5 -2
- package/packages/notification/dist/adapters/detail-format.d.ts.map +1 -1
- package/packages/notification/dist/adapters/detail-format.js +17 -0
- package/packages/notification/dist/adapters/detail-format.js.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.js +1 -0
- package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.js +1 -0
- package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.js +1 -0
- package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.js +1 -0
- package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
- package/packages/notification/dist/notification-center.d.ts.map +1 -1
- package/packages/notification/dist/notification-center.js +10 -0
- package/packages/notification/dist/notification-center.js.map +1 -1
- package/packages/notification/dist/types.d.ts +14 -1
- package/packages/notification/dist/types.d.ts.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/adapters/detail-format.ts +29 -2
- package/packages/notification/src/adapters/email-adapter.ts +1 -0
- package/packages/notification/src/adapters/line-adapter.ts +1 -0
- package/packages/notification/src/adapters/ntfy-adapter.ts +1 -0
- package/packages/notification/src/adapters/telegram-adapter.ts +1 -0
- package/packages/notification/src/notification-center.ts +14 -0
- package/packages/notification/src/types.ts +14 -0
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,88 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [1.1.507](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.507) (2026-11-11)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **actions:** register the job resume, the GLM countdown and the queued continuations ([177c964](https://github.com/ManotLuijiu/pi-harness-runtime/commit/177c964555b0057e6bc7d293dafab10c95eabbf9))
|
|
11
|
+
* **delegation:** a child is only a child when it really ran ([ac4e678](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ac4e6782d7d4bc4941e458425c8a9f38a51fc9d4))
|
|
12
|
+
* **delegation:** the scope of a request is measured before it is judged ([684b6a5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/684b6a55184f63d3be24c48dcecfb0b13410dc15))
|
|
13
|
+
* **harness:** the startup tells the user that the session keeps its issues with bd ([81d63c5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/81d63c5dc2452b9f08001756866db80046a2532a))
|
|
14
|
+
* **honcho:** the memory service speaks the native MCP of Pi ([f9f740c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f9f740c12869bae4c4380afbe8c76e12388bb744))
|
|
15
|
+
* **jev:** one decision per question, bound to the question it was evaluated for ([03448aa](https://github.com/ManotLuijiu/pi-harness-runtime/commit/03448aaa17f2d0c3bcf644b0b1d5949727dfff8c))
|
|
16
|
+
* **jev:** the evaluation of a choice sees what the options mean ([f0c207e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f0c207e074701e6b7dab0965170ec39453d9d502))
|
|
17
|
+
* **jev:** the pending question carries the ask and the answer ([a792133](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a792133e517fb8c12d3363e98942f3af30270f27))
|
|
18
|
+
* **jev:** the pending question counts down to the decision ([d0cc171](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0cc171223986b2d32c670b6c1c74712fe0a4ad8))
|
|
19
|
+
* **keys:** tell the user what to do about an exposed key file ([7031003](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7031003574307351b7a8f28ee14159b58e17b793))
|
|
20
|
+
* **keys:** the key files are repaired when their permissions are unsafe ([e9c2c26](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e9c2c26b287665b56ed749de269737e8403bb8af))
|
|
21
|
+
* **mcp:** the harness connects the registered servers when nobody else does ([7fef951](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7fef95137ad4664df57f045cd91b7d233315b0bc))
|
|
22
|
+
* **merge:** a manual run can leave the merge to the pull request event ([b3d1950](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b3d1950b5941c78ccd56951acdafd1d4afbe1bff))
|
|
23
|
+
* **phase:** the agent owns the todo list and the harness says so ([c489370](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c489370bd7926aece8cb1a77aa20ca2d2672f1fb))
|
|
24
|
+
* **providers:** a shared registry of the pending automatic actions and a bounded recovery ([c5f955b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c5f955bc82adcb6999a89d5bc278fa87fccee022))
|
|
25
|
+
* **providers:** one failure has one executor, and the retry bridge is checked ([e73c4c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e73c4c76ff20a9708083659bdcefe74870a84837))
|
|
26
|
+
* **quota:** a fresh capture and a low allowance decide the MiniMax recovery ([06d7b8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/06d7b8e971e0da58a344f75778c98edb442562bd))
|
|
27
|
+
* **recovery:** a MiniMax quota exhaustion waits for the captured reset plus ten minutes ([5af08a0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5af08a0e86c8c93fa5b7ff64a7b08e4514bc68f3))
|
|
28
|
+
* **review:** the review starts on its own when the implementation is done ([04f51f5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/04f51f599cd3222b6c74dd8fadb1373bd72d9bd7))
|
|
29
|
+
* **status:** the status line names the phase, the progress and the children ([0167bf3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0167bf396a0b987c1d44f3b869351976a951dd65))
|
|
30
|
+
* **test:** the task waits for the final tier of the validation plan ([fa147db](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa147dbf7d50d56e6ea48c6d8fbb4fcb190caf8a))
|
|
31
|
+
* **validation:** a large job starts with a measured pilot ([425964b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/425964b04a3d4fa02adfabed47ff62c5ec2b9c28))
|
|
32
|
+
* **validation:** the size of a job is a fact, not a phrase ([8aa6c11](https://github.com/ManotLuijiu/pi-harness-runtime/commit/8aa6c119565d0f8412dc8aeafa6b295d47d3f093))
|
|
33
|
+
* **workspace:** a Frappe bench names the mode of the workspace ([9d58517](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d585172063a330992fee933316e268ae9985e3c))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Bug Fixes
|
|
37
|
+
|
|
38
|
+
* **ci:** every run of a required check counts, and the worst one decides ([da9e0da](https://github.com/ManotLuijiu/pi-harness-runtime/commit/da9e0da6b0ca29cb320764fd5fc1b8a8dee8b1e0))
|
|
39
|
+
* **ci:** the newest attempt of a run decides, not the attempt that failed ([9d7adcc](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d7adcc31cbacc517c3c1a9bcd3956be94c908ef))
|
|
40
|
+
* **harness:** a raw control byte hid the request scope from the review ([852b1ff](https://github.com/ManotLuijiu/pi-harness-runtime/commit/852b1ff3afc21adb71e92b85d21ff1c88e14816a))
|
|
41
|
+
* **honcho:** a handshake that never finishes is named instead of hanging the startup ([d66005f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d66005f928a4f8df5de5b8a0579cbd4f8df37ff9))
|
|
42
|
+
* **honcho:** a probe that the end of the session cuts off is not a failure ([493fd93](https://github.com/ManotLuijiu/pi-harness-runtime/commit/493fd938ba09fdcf57bed4aaf259d81c4040909e))
|
|
43
|
+
* **honcho:** a slow connector is not a failed one, the row is corrected when the tools appear ([b62ac5b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b62ac5b16ad93e2bfb10cbec589b80a09b7e20b3))
|
|
44
|
+
* **honcho:** a tools listing that the end of the session cuts off is not a failure ([62cc8c3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/62cc8c376aade01c953ce2a1c6501ed07b74a1e9))
|
|
45
|
+
* **honcho:** the report names the state of the MCP server, not the registration ([762f4d5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/762f4d59e4eabcab9a1c21a61aa522c3267621b3))
|
|
46
|
+
* **hooks:** a tag is checked through the commit behind it ([c823e8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c823e8e1f6973c62b5449a97c101759a4d5cbba5))
|
|
47
|
+
* **jev:** a completion question, a stale check and a pending validation ([827120b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/827120b4138b6f5ae253e69154de190085703de8))
|
|
48
|
+
* **jev:** a new request does not inherit the completion of the old one ([0f863c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0f863c7fcaf35e261beabebef15b8ab634132fdc))
|
|
49
|
+
* **jev:** a prohibition must not hide a destructive action ([78a6cc1](https://github.com/ManotLuijiu/pi-harness-runtime/commit/78a6cc199e647f812e145483f7e94991358d8f34))
|
|
50
|
+
* **jev:** a refusal without punctuation, and only a decline of an action ([ef3dbf8](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ef3dbf87a2a1725373281613a0d48881ed5d2239))
|
|
51
|
+
* **jev:** a rejection during the countdown pauses the work ([9b018ce](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9b018ce1152b6724dee73cdc901b8a360ecc91ab))
|
|
52
|
+
* **jev:** a session switch ends the wait of the old session ([ca03d39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ca03d399c0d4a1b2dd428ac39836ab53c0e51ab6))
|
|
53
|
+
* **jev:** an open question is not a permission to answer it ([e20eca2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e20eca24bb88d385198bb12ee3ebf573af9aea86))
|
|
54
|
+
* **jev:** keep a comparative caveat with the option it constrains ([7be306b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7be306b0d0872713ebca9386d613ae5a8da9d5b6))
|
|
55
|
+
* **jev:** the action of the question on its own text, and a reply is not an authorization ([7c3fdd6](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7c3fdd6822b58bd6ccfa3bcdf1c5282cb3db52dd))
|
|
56
|
+
* **jev:** the action of the question, and an open question blocks the work ([1f9ff39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/1f9ff39efc4ee17464c54c2ca9b583245093dbac))
|
|
57
|
+
* **jev:** the alternatives of a question are parsed without a quadratic pattern ([63016a3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/63016a3f0488acb801990dd46770c00d382b5858))
|
|
58
|
+
* **jev:** the countdown of an obsolete question does not outlive the work ([fe78b29](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fe78b29e6fc513b11a6993bc68fd9418dc77f79b))
|
|
59
|
+
* **jev:** the facts of an option survive the wording of the agent ([5a6250c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5a6250c07aa8d965bc6ae242cccbf519477598f8))
|
|
60
|
+
* **jev:** the scope decides the question, the identity guards the awaits ([ed348b2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ed348b2f94460a3b3ac11c01afd4420ee4ec1020))
|
|
61
|
+
* **jev:** the scope of a request on every human path, and the todos it touched ([cec8f36](https://github.com/ManotLuijiu/pi-harness-runtime/commit/cec8f3644333b1d13a203340df80a30fef5c1ac0))
|
|
62
|
+
* **jev:** the subject of the scope decides, a clarification is preserved ([faf6a0f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/faf6a0fe7d0dce94d4fa48d0d7a57d126556ba6d))
|
|
63
|
+
* **keys:** the key directory is audited as a tree ([081cc75](https://github.com/ManotLuijiu/pi-harness-runtime/commit/081cc75ac8ceff36b0d619c3f76371dc08459d3c))
|
|
64
|
+
* **logging:** the sanitizer of a message writes its control characters as escapes ([f4bbdb9](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f4bbdb94bc1801d4228142bc1a1b31505c42a575))
|
|
65
|
+
* **mcp:** the connector and the watcher stop with the session ([4d1b87d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/4d1b87d14d015749025b62bf54d6d0b6137295f7))
|
|
66
|
+
* **mcp:** the connector survives a runtime that cannot list the servers ([d0f555f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0f555fa14f51de33591e572a41ded5b59a3e4dd))
|
|
67
|
+
* **merge:** the boolean input is read as a boolean ([971b716](https://github.com/ManotLuijiu/pi-harness-runtime/commit/971b7169a43db382dbdeb6bb9bab02ab56e0351c))
|
|
68
|
+
* **merge:** the manual job needs the right of the merge ([7ba685e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7ba685ec43d815f90aa35b742579e87e76c69209))
|
|
69
|
+
* **merge:** the merge of the pull request is the GraphQL mutation ([972bab4](https://github.com/ManotLuijiu/pi-harness-runtime/commit/972bab4b0f7faf1b0047f3bb767ef6db3de4b2fa))
|
|
70
|
+
* **merge:** the workflow merges the pull request the gates cleared ([d7639ec](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d7639ec9b83723510acb4a5c4e23d5d6167c3af6))
|
|
71
|
+
* **providers:** the escape of the user stops the recovery, and a permanent failure clears it ([cfe5797](https://github.com/ManotLuijiu/pi-harness-runtime/commit/cfe57976bf463c327e45b874fd297c3e942246b8))
|
|
72
|
+
* **providers:** the pause wins over a retry, and the retry starts when the run settles ([e016dd2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e016dd2a5add5b0f3c848a225a28a59e0f5fc7f3))
|
|
73
|
+
* **recovery:** a cancelled countdown does not own the next attempt ([7b79f30](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7b79f301389fae48201e696d3086d257ed722fb9))
|
|
74
|
+
* **recovery:** a cancelled retry of Pi is a cancelled decision, a spent budget is not ([7e13f15](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7e13f156a279cded2b2d4b0c99358100b3d5e038))
|
|
75
|
+
* **recovery:** a manual request takes the work back from the countdown ([fa81d8f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa81d8fd9d6160166a095e07bdf17049bbafa32d))
|
|
76
|
+
* **recovery:** the predicate of Pi decides the retry, the provider identity is normalized ([6c8c17b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/6c8c17b53302cea1602d192251eacd0ebbd3e8c9))
|
|
77
|
+
* **release:** a manual release keeps the version of its manifest ([122e661](https://github.com/ManotLuijiu/pi-harness-runtime/commit/122e661ea48cb8aff14c71dbd0221a4cf0ba4c8e))
|
|
78
|
+
* **release:** the alignment names the reason of a failure ([5814dd0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5814dd0b780f5839cf8011ed0cbbbb31e7eb0418))
|
|
79
|
+
* **release:** the channels of the package name the release ([a7a273f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a7a273ffe8398e2b5b07ef1c321777de41792c6e))
|
|
80
|
+
* **release:** the release publishes and leaves the channels alone ([5871746](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5871746589c1281d855dbeb591b02419fa43f22e))
|
|
81
|
+
* **release:** the release pull request names the version of its branch ([a846e83](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a846e83e6de6e529f4bca05e869db0b5eebb37c5))
|
|
82
|
+
* **release:** the release scans the tree of the commit it publishes ([7d47a0b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7d47a0b5406480a1e1d3a52623a76aabd1e2d0ba))
|
|
83
|
+
* **workspace:** a strong bench signal decides, and the question is not a fact of an option ([303a828](https://github.com/ManotLuijiu/pi-harness-runtime/commit/303a828f55aa7c4e0a611231f482489b530ff5a3))
|
|
84
|
+
* **workspace:** keep a client name out of the comment about the search depth ([b572e0a](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b572e0af16ff562203bda2b5c47b41807cff5a95))
|
|
85
|
+
* **workspace:** the repository names the mode before the mode stays unknown ([188a32c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/188a32c7a6af1465dd24714278fccc73d06f6477))
|
|
86
|
+
|
|
5
87
|
### [1.1.506](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.506) (2026-11-11)
|
|
6
88
|
|
|
7
89
|
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
normalizeProviderIdentity,
|
|
6
6
|
type RecoveryResetEvidence,
|
|
7
7
|
} from "./provider-recovery.ts";
|
|
8
|
+
import { decideQuotaReset, quotaWindowState } from "./quota-threshold.ts";
|
|
8
9
|
|
|
9
10
|
/** Give the quota service time to settle before probing it again. */
|
|
10
11
|
export const QUOTA_RESET_BUFFER_MS = 10 * 60_000;
|
|
@@ -13,8 +14,18 @@ export const QUOTA_RESET_BUFFER_MS = 10 * 60_000;
|
|
|
13
14
|
type QuotaWindowName = "h5" | "weekly";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
|
-
* A failed request plus fresh
|
|
17
|
-
*
|
|
17
|
+
* A failed request plus fresh quota can identify a reset even when the
|
|
18
|
+
* provider's generic error does not name its window.
|
|
19
|
+
*
|
|
20
|
+
* A window that is empty decides on its own: the account is blocked until
|
|
21
|
+
* it resets, and the bounded cycle cannot change that. A window that is
|
|
22
|
+
* nearly empty - below ten percent of the allowance - decides as well,
|
|
23
|
+
* when its reset is more than an hour away. A reset that arrives sooner is
|
|
24
|
+
* reached by the bounded cycle, and the cycle keeps the failure.
|
|
25
|
+
*
|
|
26
|
+
* The percentages of the mirror are the used percentages; the policy is
|
|
27
|
+
* stated in the remaining percentage, and the conversion is in
|
|
28
|
+
* `quota-threshold.ts`.
|
|
18
29
|
*
|
|
19
30
|
* The selector reads the capture that the harness already has. It never
|
|
20
31
|
* scrapes again and never parses the text of the console: a capture that is
|
|
@@ -60,31 +71,54 @@ export function confirmedQuotaReset(
|
|
|
60
71
|
resetAt: mirror.weekly_resets_at_epoch,
|
|
61
72
|
},
|
|
62
73
|
];
|
|
74
|
+
// An empty window blocks the account until it resets.
|
|
63
75
|
const exhausted = windows.filter(
|
|
64
|
-
({ used }) =>
|
|
65
|
-
typeof used === "number" && Number.isFinite(used) && used >= 100,
|
|
76
|
+
({ used }) => quotaWindowState(used) === "exhausted",
|
|
66
77
|
);
|
|
67
|
-
if (exhausted.length
|
|
78
|
+
if (exhausted.length > 0) {
|
|
79
|
+
// If an exhausted window has no usable reset, another window cannot
|
|
80
|
+
// establish when the account becomes usable. Keep bounded recovery.
|
|
81
|
+
let latest: { window: QuotaWindowName; resetAt: number } | undefined;
|
|
82
|
+
for (const { window, resetAt } of exhausted) {
|
|
83
|
+
if (
|
|
84
|
+
typeof resetAt !== "number" ||
|
|
85
|
+
!Number.isFinite(resetAt) ||
|
|
86
|
+
resetAt <= now
|
|
87
|
+
) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
if (!latest || resetAt > latest.resetAt) latest = { window, resetAt };
|
|
91
|
+
}
|
|
92
|
+
if (!latest) return undefined;
|
|
93
|
+
return {
|
|
94
|
+
provider: normalizeProviderIdentity(provider),
|
|
95
|
+
window: latest.window,
|
|
96
|
+
resetAt: latest.resetAt,
|
|
97
|
+
observedAt,
|
|
98
|
+
source: mirror.source,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
68
101
|
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
102
|
+
// No window is empty. A window that is nearly empty decides, when its
|
|
103
|
+
// reset is far enough away: a reset that arrives within the hour is
|
|
104
|
+
// reached by the bounded cycle of the failure.
|
|
105
|
+
const low = windows.filter(({ used }) => quotaWindowState(used) === "low");
|
|
106
|
+
let latestLow: { window: QuotaWindowName; resetAt: number } | undefined;
|
|
107
|
+
for (const { window, used, resetAt } of low) {
|
|
108
|
+
if (decideQuotaReset({ usedPct: used, resetAt, now }) !== "reset") {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (typeof resetAt !== "number" || !Number.isFinite(resetAt)) continue;
|
|
112
|
+
if (!latestLow || resetAt > latestLow.resetAt) {
|
|
113
|
+
latestLow = { window, resetAt };
|
|
79
114
|
}
|
|
80
|
-
if (!latest || resetAt > latest.resetAt) latest = { window, resetAt };
|
|
81
115
|
}
|
|
82
|
-
if (!
|
|
116
|
+
if (!latestLow) return undefined;
|
|
83
117
|
|
|
84
118
|
return {
|
|
85
119
|
provider: normalizeProviderIdentity(provider),
|
|
86
|
-
window:
|
|
87
|
-
resetAt:
|
|
120
|
+
window: latestLow.window,
|
|
121
|
+
resetAt: latestLow.resetAt,
|
|
88
122
|
observedAt,
|
|
89
123
|
source: mirror.source,
|
|
90
124
|
};
|
|
@@ -226,6 +226,26 @@ export class ProviderRecoveryRuntime {
|
|
|
226
226
|
};
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
+
/**
|
|
230
|
+
* The failure that waits for the settled boundary.
|
|
231
|
+
*
|
|
232
|
+
* The caller reads it to prepare what the recovery needs before it
|
|
233
|
+
* picks a deadline - a fresh quota capture, for example. Reading it
|
|
234
|
+
* changes nothing: the candidate stays where it is until the run
|
|
235
|
+
* settles.
|
|
236
|
+
*/
|
|
237
|
+
pendingQuotaFailure(): { provider: string; kind: string } | null {
|
|
238
|
+
const candidate = this.candidate;
|
|
239
|
+
if (!candidate) return null;
|
|
240
|
+
if (
|
|
241
|
+
candidate.failure.kind !== "usage-exhausted" &&
|
|
242
|
+
candidate.failure.kind !== "rate-limit"
|
|
243
|
+
) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return { provider: candidate.provider, kind: candidate.failure.kind };
|
|
247
|
+
}
|
|
248
|
+
|
|
229
249
|
/**
|
|
230
250
|
* The run settled.
|
|
231
251
|
*
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The quota capture that a provider recovery needs.
|
|
3
|
+
*
|
|
4
|
+
* The recovery decides when to continue from a capture of the quota, and
|
|
5
|
+
* the row of the mirror can be hours old. A capture that is forced before
|
|
6
|
+
* the decision refreshes that row.
|
|
7
|
+
*
|
|
8
|
+
* The capture is the one of the harness: it refreshes the mirror and
|
|
9
|
+
* nothing else. It never charges an attempt of the recovery and it never
|
|
10
|
+
* schedules anything.
|
|
11
|
+
*
|
|
12
|
+
* A capture that failed is remembered as a failure: the row of the mirror
|
|
13
|
+
* is the old one then, and an old row cannot prove a deadline.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The time a capture of the same failure may be reused. */
|
|
17
|
+
export const QUOTA_CAPTURE_REUSE_MS = 2 * 60_000;
|
|
18
|
+
|
|
19
|
+
/** The time the recovery waits for the capture. */
|
|
20
|
+
export const QUOTA_CAPTURE_WAIT_MS = 15_000;
|
|
21
|
+
|
|
22
|
+
/** What the last capture did. */
|
|
23
|
+
export interface QuotaCaptureOutcome {
|
|
24
|
+
/** The provider the capture was about. */
|
|
25
|
+
provider: string;
|
|
26
|
+
/** Did the capture work? */
|
|
27
|
+
ok: boolean;
|
|
28
|
+
/** When the capture ended. */
|
|
29
|
+
at: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The dependencies of the controller. */
|
|
33
|
+
export interface QuotaCaptureOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Capture the quota of a provider.
|
|
36
|
+
*
|
|
37
|
+
* The result says whether the capture worked. A capture that throws is
|
|
38
|
+
* a failed capture, not a crash of the harness.
|
|
39
|
+
*/
|
|
40
|
+
capture(provider: string): Promise<boolean>;
|
|
41
|
+
/** The clock of the controller. */
|
|
42
|
+
now?(): number;
|
|
43
|
+
/** The log of the controller. */
|
|
44
|
+
log?(message: string): void;
|
|
45
|
+
/** The time the recovery waits for the capture. */
|
|
46
|
+
waitMs?: number;
|
|
47
|
+
/** The time a capture of the same failure may be reused. */
|
|
48
|
+
reuseMs?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The capture of the quota of a provider failure.
|
|
53
|
+
*
|
|
54
|
+
* One capture at a time: the browser of the harness is not a resource that
|
|
55
|
+
* is spent twice for one failure.
|
|
56
|
+
*/
|
|
57
|
+
export class QuotaCaptureController {
|
|
58
|
+
private readonly options: QuotaCaptureOptions;
|
|
59
|
+
private readonly now: () => number;
|
|
60
|
+
private readonly waitMs: number;
|
|
61
|
+
private readonly reuseMs: number;
|
|
62
|
+
private pending: {
|
|
63
|
+
provider: string;
|
|
64
|
+
promise: Promise<boolean>;
|
|
65
|
+
startedAt: number;
|
|
66
|
+
} | null = null;
|
|
67
|
+
private last: QuotaCaptureOutcome | null = null;
|
|
68
|
+
|
|
69
|
+
constructor(options: QuotaCaptureOptions) {
|
|
70
|
+
this.options = options;
|
|
71
|
+
this.now = options.now ?? (() => Date.now());
|
|
72
|
+
this.waitMs = options.waitMs ?? QUOTA_CAPTURE_WAIT_MS;
|
|
73
|
+
this.reuseMs = options.reuseMs ?? QUOTA_CAPTURE_REUSE_MS;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The outcome of the last capture. */
|
|
77
|
+
get outcome(): QuotaCaptureOutcome | null {
|
|
78
|
+
return this.last;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Did the last capture of this provider fail?
|
|
83
|
+
*
|
|
84
|
+
* A failed capture means that the row of the mirror is the old one, so
|
|
85
|
+
* it cannot prove a deadline.
|
|
86
|
+
*/
|
|
87
|
+
failed(provider: string): boolean {
|
|
88
|
+
const outcome = this.last;
|
|
89
|
+
if (!outcome) return false;
|
|
90
|
+
return outcome.provider === provider.toLowerCase() && !outcome.ok;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Start the capture of a quota failure.
|
|
95
|
+
*
|
|
96
|
+
* A capture that is running for the same provider is not started again,
|
|
97
|
+
* and a capture that ended recently is not repeated: its row is fresh
|
|
98
|
+
* enough for the decision.
|
|
99
|
+
*/
|
|
100
|
+
begin(provider: string): void {
|
|
101
|
+
const key = provider.toLowerCase();
|
|
102
|
+
const current = this.pending;
|
|
103
|
+
if (
|
|
104
|
+
current &&
|
|
105
|
+
current.provider === key &&
|
|
106
|
+
current.startedAt > this.now() - this.reuseMs
|
|
107
|
+
) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const promise = (async () => {
|
|
111
|
+
try {
|
|
112
|
+
const ok = await this.options.capture(key);
|
|
113
|
+
this.last = { provider: key, ok, at: this.now() };
|
|
114
|
+
return ok;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
this.options.log?.(
|
|
117
|
+
`The forced quota capture of ${key} failed: ${
|
|
118
|
+
error instanceof Error ? error.message : String(error)
|
|
119
|
+
}`,
|
|
120
|
+
);
|
|
121
|
+
this.last = { provider: key, ok: false, at: this.now() };
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
})();
|
|
125
|
+
this.pending = { provider: key, promise, startedAt: this.now() };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Wait for the capture, within a bound.
|
|
130
|
+
*
|
|
131
|
+
* A capture that takes too long is given up: the recovery keeps the
|
|
132
|
+
* bounded cycle, and the capture that finishes later only refreshes
|
|
133
|
+
* the mirror.
|
|
134
|
+
*/
|
|
135
|
+
async wait(): Promise<void> {
|
|
136
|
+
const current = this.pending;
|
|
137
|
+
if (!current) return;
|
|
138
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
139
|
+
try {
|
|
140
|
+
await Promise.race([
|
|
141
|
+
current.promise,
|
|
142
|
+
new Promise<void>((resolve) => {
|
|
143
|
+
timer = setTimeout(resolve, this.waitMs);
|
|
144
|
+
}),
|
|
145
|
+
]);
|
|
146
|
+
} finally {
|
|
147
|
+
if (timer) clearTimeout(timer);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The alert of a low allowance.
|
|
3
|
+
*
|
|
4
|
+
* An allowance that falls below ten percent is worth a message: the human
|
|
5
|
+
* can act before the window is empty. The message is sent once per
|
|
6
|
+
* crossing: a capture of the same state again is not a new crossing, and
|
|
7
|
+
* an allowance that recovers arms the alert again.
|
|
8
|
+
*
|
|
9
|
+
* The alert is not a decision and not a recovery: it needs no approval
|
|
10
|
+
* and it costs no attempt of the recovery cycle.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
LOW_REMAINING_THRESHOLD_PCT,
|
|
15
|
+
quotaWindowState,
|
|
16
|
+
} from "./quota-threshold.ts";
|
|
17
|
+
|
|
18
|
+
/** What the tracker says about an observation. */
|
|
19
|
+
export type QuotaAlertDecision =
|
|
20
|
+
/** The allowance is below the threshold and the alert was not sent yet. */
|
|
21
|
+
| "notify"
|
|
22
|
+
/** The allowance is below the threshold and the alert was sent already. */
|
|
23
|
+
| "silent"
|
|
24
|
+
/** The allowance recovered; the alert is armed again. */
|
|
25
|
+
| "rearmed"
|
|
26
|
+
/** Nothing is known about the allowance. */
|
|
27
|
+
| "none";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The state of the alert, per provider.
|
|
31
|
+
*
|
|
32
|
+
* The tracker remembers the providers whose alert was sent. The state is
|
|
33
|
+
* the state of the allowance, not of the capture: a capture that says
|
|
34
|
+
* nothing does not arm the alert again.
|
|
35
|
+
*/
|
|
36
|
+
export class QuotaLowRemainingTracker {
|
|
37
|
+
private readonly thresholdPct: number;
|
|
38
|
+
private readonly alerted = new Set<string>();
|
|
39
|
+
|
|
40
|
+
constructor(thresholdPct: number = LOW_REMAINING_THRESHOLD_PCT) {
|
|
41
|
+
this.thresholdPct = thresholdPct;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Observe the used percentage of the five-hour window of a provider.
|
|
46
|
+
*
|
|
47
|
+
* The percentage is the used percentage of the mirror: the allowance
|
|
48
|
+
* that is left is its complement.
|
|
49
|
+
*/
|
|
50
|
+
observe(
|
|
51
|
+
provider: string,
|
|
52
|
+
usedPct: number | undefined | null,
|
|
53
|
+
): QuotaAlertDecision {
|
|
54
|
+
const key = provider.toLowerCase();
|
|
55
|
+
const state = quotaWindowState(usedPct);
|
|
56
|
+
if (state === "unknown") return "none";
|
|
57
|
+
if (state === "available") {
|
|
58
|
+
// The allowance recovered: the next crossing alerts again.
|
|
59
|
+
return this.alerted.delete(key) ? "rearmed" : "none";
|
|
60
|
+
}
|
|
61
|
+
if (this.alerted.has(key)) return "silent";
|
|
62
|
+
this.alerted.add(key);
|
|
63
|
+
return "notify";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Was the alert sent for this provider? */
|
|
67
|
+
isAlerted(provider: string): boolean {
|
|
68
|
+
return this.alerted.has(provider.toLowerCase());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The threshold of the alert, in percent of the remaining allowance. */
|
|
72
|
+
get threshold(): number {
|
|
73
|
+
return this.thresholdPct;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The duration of a quota reset, as the console of a provider writes it.
|
|
3
|
+
*
|
|
4
|
+
* The console of MiniMax shows the remaining time of a window as text
|
|
5
|
+
* ("Resets in 1 hr 42 min"). The mirror of the harness needs an absolute
|
|
6
|
+
* moment, because the recovery compares it with the clock when it decides
|
|
7
|
+
* when to continue.
|
|
8
|
+
*
|
|
9
|
+
* The conversion is anchored at the moment of the capture: the text
|
|
10
|
+
* describes the moment of the capture, so the epoch of the reset is the
|
|
11
|
+
* capture plus the duration. A text that is not a duration - a date, an
|
|
12
|
+
* empty field, a number without a unit - proves nothing and yields no
|
|
13
|
+
* epoch.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The milliseconds of every unit the console writes. */
|
|
17
|
+
const UNIT_MS: Record<string, number> = {
|
|
18
|
+
s: 1000,
|
|
19
|
+
sec: 1000,
|
|
20
|
+
secs: 1000,
|
|
21
|
+
second: 1000,
|
|
22
|
+
seconds: 1000,
|
|
23
|
+
m: 60_000,
|
|
24
|
+
min: 60_000,
|
|
25
|
+
mins: 60_000,
|
|
26
|
+
minute: 60_000,
|
|
27
|
+
minutes: 60_000,
|
|
28
|
+
h: 3_600_000,
|
|
29
|
+
hr: 3_600_000,
|
|
30
|
+
hrs: 3_600_000,
|
|
31
|
+
hour: 3_600_000,
|
|
32
|
+
hours: 3_600_000,
|
|
33
|
+
d: 86_400_000,
|
|
34
|
+
day: 86_400_000,
|
|
35
|
+
days: 86_400_000,
|
|
36
|
+
w: 604_800_000,
|
|
37
|
+
week: 604_800_000,
|
|
38
|
+
weeks: 604_800_000,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const DURATION_PATTERN =
|
|
42
|
+
/(\d+(?:\.\d+)?)\s*(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w)\b/gi;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The duration of a reset text, in milliseconds.
|
|
46
|
+
*
|
|
47
|
+
* The text may carry a prefix ("Resets in ...") and several units
|
|
48
|
+
* ("4 days 20 hr 42 min"). The units are summed. A text without a
|
|
49
|
+
* duration yields nothing.
|
|
50
|
+
*/
|
|
51
|
+
export function parseResetDurationMs(
|
|
52
|
+
text: string | undefined | null,
|
|
53
|
+
): number | undefined {
|
|
54
|
+
if (typeof text !== "string") return undefined;
|
|
55
|
+
const trimmed = text.trim();
|
|
56
|
+
if (!trimmed) return undefined;
|
|
57
|
+
let total = 0;
|
|
58
|
+
let found = false;
|
|
59
|
+
for (const match of trimmed.matchAll(DURATION_PATTERN)) {
|
|
60
|
+
const amount = Number.parseFloat(match[1]);
|
|
61
|
+
const unit = match[2].toLowerCase();
|
|
62
|
+
const unitMs = UNIT_MS[unit];
|
|
63
|
+
if (!Number.isFinite(amount) || unitMs === undefined) continue;
|
|
64
|
+
total += amount * unitMs;
|
|
65
|
+
found = true;
|
|
66
|
+
}
|
|
67
|
+
return found ? Math.round(total) : undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The absolute epoch of a reset, read from the text of the console.
|
|
72
|
+
*
|
|
73
|
+
* The observation is the anchor: the text describes the state of the
|
|
74
|
+
* window at the moment of the capture.
|
|
75
|
+
*/
|
|
76
|
+
export function resetEpochFromDuration(
|
|
77
|
+
text: string | undefined | null,
|
|
78
|
+
observedAtMs: number | undefined | null,
|
|
79
|
+
): number | undefined {
|
|
80
|
+
if (typeof observedAtMs !== "number" || !Number.isFinite(observedAtMs)) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const duration = parseResetDurationMs(text);
|
|
84
|
+
if (duration === undefined) return undefined;
|
|
85
|
+
const epoch = observedAtMs + duration;
|
|
86
|
+
return Number.isFinite(epoch) ? epoch : undefined;
|
|
87
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The threshold of the low-allowance policy.
|
|
3
|
+
*
|
|
4
|
+
* The console of a provider shows the percentage that is left ("5h: 0%
|
|
5
|
+
* left"), the mirror of the harness stores the percentage that is used.
|
|
6
|
+
* The two are complements, and the policy is stated in the remaining
|
|
7
|
+
* percentage, so the conversion and the thresholds live here and are used
|
|
8
|
+
* by name.
|
|
9
|
+
*
|
|
10
|
+
* The module is pure: it decides, it does not observe. The capture of the
|
|
11
|
+
* quota, the freshness of the capture and the scheduling of the recovery
|
|
12
|
+
* belong to the modules around it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The remaining percentage below which the allowance of a window is low. */
|
|
16
|
+
export const LOW_REMAINING_THRESHOLD_PCT = 10;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A reset that is nearer than this is left to the bounded recovery cycle.
|
|
20
|
+
*
|
|
21
|
+
* The cycle of a failure waits 1, 4, 9 and 16 minutes. A reset that
|
|
22
|
+
* arrives within the hour is reached by that cycle, and spending the
|
|
23
|
+
* budget of the cycle on a reset that is about to happen would leave
|
|
24
|
+
* nothing for the failure that comes next.
|
|
25
|
+
*/
|
|
26
|
+
export const RESET_HORIZON_MS = 60 * 60_000;
|
|
27
|
+
|
|
28
|
+
/** The remaining percentage of a used percentage. */
|
|
29
|
+
export function remainingPctFromUsedPct(usedPct: number): number {
|
|
30
|
+
return 100 - usedPct;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The used percentage of a remaining percentage. */
|
|
34
|
+
export function usedPctFromRemainingPct(remainingPct: number): number {
|
|
35
|
+
return 100 - remainingPct;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The state of one quota window. */
|
|
39
|
+
export type QuotaWindowState =
|
|
40
|
+
/** Nothing is known about the window. */
|
|
41
|
+
| "unknown"
|
|
42
|
+
/** The window still has its allowance. */
|
|
43
|
+
| "available"
|
|
44
|
+
/** The window is below the threshold, but not empty. */
|
|
45
|
+
| "low"
|
|
46
|
+
/** The window is empty. */
|
|
47
|
+
| "exhausted";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The state of a window, read from its used percentage.
|
|
51
|
+
*
|
|
52
|
+
* An unknown percentage is unknown and nothing else: it is not an empty
|
|
53
|
+
* window and it is not a window with an allowance.
|
|
54
|
+
*/
|
|
55
|
+
export function quotaWindowState(
|
|
56
|
+
usedPct: number | undefined | null,
|
|
57
|
+
): QuotaWindowState {
|
|
58
|
+
if (typeof usedPct !== "number" || !Number.isFinite(usedPct)) {
|
|
59
|
+
return "unknown";
|
|
60
|
+
}
|
|
61
|
+
const remaining = remainingPctFromUsedPct(usedPct);
|
|
62
|
+
if (remaining <= 0) return "exhausted";
|
|
63
|
+
if (remaining < LOW_REMAINING_THRESHOLD_PCT) return "low";
|
|
64
|
+
return "available";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Is the window below the threshold (nearly empty or empty)? */
|
|
68
|
+
export function isBelowLowThreshold(
|
|
69
|
+
usedPct: number | undefined | null,
|
|
70
|
+
): boolean {
|
|
71
|
+
const state = quotaWindowState(usedPct);
|
|
72
|
+
return state === "low" || state === "exhausted";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Is the window completely empty? */
|
|
76
|
+
export function isFullyExhausted(
|
|
77
|
+
usedPct: number | undefined | null,
|
|
78
|
+
): boolean {
|
|
79
|
+
return quotaWindowState(usedPct) === "exhausted";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The decision of the policy for one window. */
|
|
83
|
+
export type QuotaResetDecision =
|
|
84
|
+
/** Wait for the reset of the window. */
|
|
85
|
+
| "reset"
|
|
86
|
+
/** Keep the bounded recovery cycle. */
|
|
87
|
+
| "bounded";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Decide what one window means for a failed request.
|
|
91
|
+
*
|
|
92
|
+
* An empty window blocks the account until it resets, whatever the
|
|
93
|
+
* distance of the reset is: the bounded cycle cannot help, because every
|
|
94
|
+
* attempt of the cycle fails the same way.
|
|
95
|
+
*
|
|
96
|
+
* A window that is nearly empty blocks the account as well, but a reset
|
|
97
|
+
* that arrives within the hour is reached by the cycle, so the cycle
|
|
98
|
+
* keeps the failure.
|
|
99
|
+
*/
|
|
100
|
+
export function decideQuotaReset(input: {
|
|
101
|
+
usedPct: number | undefined | null;
|
|
102
|
+
resetAt: number | undefined | null;
|
|
103
|
+
now: number;
|
|
104
|
+
}): QuotaResetDecision {
|
|
105
|
+
const state = quotaWindowState(input.usedPct);
|
|
106
|
+
if (state !== "exhausted" && state !== "low") return "bounded";
|
|
107
|
+
const resetAt = input.resetAt;
|
|
108
|
+
if (
|
|
109
|
+
typeof resetAt !== "number" ||
|
|
110
|
+
!Number.isFinite(resetAt) ||
|
|
111
|
+
resetAt <= input.now
|
|
112
|
+
) {
|
|
113
|
+
return "bounded";
|
|
114
|
+
}
|
|
115
|
+
if (state === "exhausted") return "reset";
|
|
116
|
+
return resetAt - input.now > RESET_HORIZON_MS ? "reset" : "bounded";
|
|
117
|
+
}
|