@cortexkit/opencode-openai-auth 1.23.1 → 1.23.3
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/bin.js +7 -7
- package/dist/{index-7hy72nfy.js → index-9kges8jy.js} +2 -2
- package/dist/index-ekt6aq17.js +60 -0
- package/dist/index-nd2ks7er.js +2 -0
- package/dist/{index-p81hxfe6.js → index-vf4nn20q.js} +1 -1
- package/dist/{index-bd18sfjj.js → index-x00sxt2w.js} +10 -10
- package/dist/{index-3ya2s179.js → index-xkngfyvc.js} +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/{runtime-layout-B07ApQZL.d.ts → internal-DacVgEWs.d.ts} +86 -663
- package/dist/rpc/port-file.js +1 -1
- package/dist/runtime-layout-Dcbj_Rq3.d.ts +650 -0
- package/dist/{sidebar-state--OTpRzFd.d.ts → sidebar-state-L--CdbLi.d.ts} +4 -15
- package/dist/sidebar-state.d.ts +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/tui.d.ts +5 -2
- package/dist/v2/server.d.ts +2 -1
- package/dist/v2/server.js +4 -4
- package/package.json +1 -1
- package/src/tui-compiled/raw/shared/054118b2705fdc20-ansi.js +98 -0
- package/src/tui-compiled/raw/shared/0895e780489c494d-window.js +33 -0
- package/src/tui-compiled/raw/shared/0b7d24474e2f33e9-credential-redaction.js +424 -0
- package/src/tui-compiled/raw/shared/0c9533222cb4380c-namespace.js +224 -0
- package/src/tui-compiled/raw/shared/112607e46a5b5f58-accounts.js +393 -0
- package/src/tui-compiled/raw/shared/136fa4abe48ad69d-cachekeep.js +411 -0
- package/src/tui-compiled/raw/shared/1547bd74f4214d2d-index.js +28 -0
- package/src/tui-compiled/raw/shared/1567207f323dc733-codex-http.js +44 -0
- package/src/tui-compiled/raw/shared/15f402885cbb2fb3-envelope-loss.js +151 -0
- package/src/tui-compiled/raw/shared/18df43cab4563acf-families.js +149 -0
- package/src/tui-compiled/raw/shared/1a4c289eec261e04-codex-instructions.js +59 -0
- package/src/tui-compiled/raw/shared/1afd446e190ef85c-vault-messages.js +12 -0
- package/src/tui-compiled/raw/shared/1b06ec96856ff8b9-codex-endpoint.js +17 -0
- package/src/tui-compiled/raw/shared/1e2f1a89bb34a773-vault-adapter.js +110 -0
- package/src/tui-compiled/raw/shared/2194fb8e8b42d7d5-insula-quota.js +73 -0
- package/src/tui-compiled/raw/shared/22b2bce42f2d6806-handle.js +10 -0
- package/src/tui-compiled/raw/shared/2577ec69f9973ae9-quota-normalize.js +13 -14
- package/src/tui-compiled/raw/shared/26055d4e5ad5e71f-codex-request.js +198 -0
- package/src/tui-compiled/raw/shared/26b57ac4c3b0c3f6-unconfirmed-retain-counter.js +41 -0
- package/src/tui-compiled/raw/shared/2bb681e99c2721d7-registry.js +538 -0
- package/src/tui-compiled/raw/shared/2dce6e9613993329-quota-manager.js +13 -20
- package/src/tui-compiled/raw/shared/2ec33b3e2b8ac38a-terminal.js +168 -0
- package/src/tui-compiled/raw/shared/30bcca1d1b4240a0-pool-session-pins.js +32 -0
- package/src/tui-compiled/raw/shared/3af9a210aa2ccdf2-codex-call-status.js +21 -0
- package/src/tui-compiled/raw/shared/3b31b36bf0877602-wording.js +120 -0
- package/src/tui-compiled/raw/shared/3c4c1a2d3fc92726-failure.js +59 -0
- package/src/tui-compiled/raw/shared/3dfe71eec6e0487d-presets.js +240 -0
- package/src/tui-compiled/raw/shared/43de3a5c4845cfc1-pool-adapter.js +149 -0
- package/src/tui-compiled/raw/shared/49353b63a833c8fb-session-cache-policy.js +60 -0
- package/src/tui-compiled/raw/shared/4adf46e07e025363-axes.js +7 -0
- package/src/tui-compiled/raw/shared/4ba847b77580b1eb-confirm.js +21 -0
- package/src/tui-compiled/raw/shared/4d05c2d7a531aece-oauth.js +48 -36
- package/src/tui-compiled/raw/shared/5018d4665e969588-limit-policy.js +159 -0
- package/src/tui-compiled/raw/shared/595bdf3fff13ba79-limit-identity.js +49 -0
- package/src/tui-compiled/raw/shared/59def9e82b61204a-session-cache-activity.js +35 -0
- package/src/tui-compiled/raw/shared/5d806e1a305d14f5-commands.js +879 -0
- package/src/tui-compiled/raw/shared/5fc39320fa8bd3a0-limit-registry.js +473 -0
- package/src/tui-compiled/raw/shared/63c1ee7e70e85e8d-vault.js +1 -0
- package/src/tui-compiled/raw/shared/6545ab7f757314b7-refresh-all-quota.js +237 -0
- package/src/tui-compiled/raw/shared/66937534c260d20b-stable-json.js +11 -0
- package/src/tui-compiled/raw/shared/7056acef1b021f19-bridge.js +10 -0
- package/src/tui-compiled/raw/shared/72d2ca42883809b4-marker.js +107 -0
- package/src/tui-compiled/raw/shared/775431e21f469203-record.js +3 -0
- package/src/tui-compiled/raw/shared/799f3ade4efbd1ea-opencode-v1.js +47 -0
- package/src/tui-compiled/raw/shared/7b2a0471b29e4f5e-wake.js +47 -0
- package/src/tui-compiled/raw/shared/7b5d6afff4ff32b9-select.js +159 -0
- package/src/tui-compiled/raw/shared/7bf96bf4d9249b3d-vault-billing.js +108 -0
- package/src/tui-compiled/raw/shared/7c98bde36aad4819-input-transformations.js +34 -0
- package/src/tui-compiled/raw/shared/7fb0418303f20a10-index.js +3 -0
- package/src/tui-compiled/raw/shared/806e12ccbad34388-pi-wait.js +51 -0
- package/src/tui-compiled/raw/shared/81ae201b724cac14-menu.js +73 -0
- package/src/tui-compiled/raw/shared/8349ed25dc629e97-scope.js +10 -0
- package/src/tui-compiled/raw/shared/86da3379973780b9-limit-provenance.js +60 -0
- package/src/tui-compiled/raw/shared/8aba66ed486d30a7-decide.js +425 -0
- package/src/tui-compiled/raw/shared/8c0c8df25a5dca1f-manager.js +735 -0
- package/src/tui-compiled/raw/shared/8c42bbec02f59636-vault-routing.js +212 -0
- package/src/tui-compiled/raw/shared/8ca6175bfae53e13-envelope.js +123 -0
- package/src/tui-compiled/raw/shared/8d0245e41049d83f-header-classifier.js +121 -0
- package/src/tui-compiled/raw/shared/9c162809179c7560-login.js +89 -0
- package/src/tui-compiled/raw/shared/a1f495296a4b756f-frame-turn.js +72 -0
- package/src/tui-compiled/raw/shared/a37138dcbd893f55-hold-bounds.js +5 -0
- package/src/tui-compiled/raw/shared/aa7fb10d77a2cccd-vault-account-menu.js +133 -0
- package/src/tui-compiled/raw/shared/adc5eeb5a8d28280-persistence.js +9 -0
- package/src/tui-compiled/raw/shared/b301c79e6378d1fb-vault.js +678 -0
- package/src/tui-compiled/raw/shared/b3c11de5f36e617a-reset.js +44 -0
- package/src/tui-compiled/raw/shared/ba6535c610d1edb3-pool-source-state.js +100 -0
- package/src/tui-compiled/raw/shared/bd4aef685681048c-duration.js +20 -0
- package/src/tui-compiled/raw/shared/bd5977eca210d2b8-index.js +662 -0
- package/src/tui-compiled/raw/shared/bfc46d34f4c9db33-limit-recovery.js +127 -0
- package/src/tui-compiled/raw/shared/c66495424ad84aee-hold.js +25 -0
- package/src/tui-compiled/raw/shared/c6a67b836bd5afcd-vault-command-menu.js +300 -0
- package/src/tui-compiled/raw/shared/c9340fb545c4e5de-internal.js +73 -0
- package/src/tui-compiled/raw/shared/c954c6447b7b9b64-activity.js +52 -0
- package/src/tui-compiled/raw/shared/cba8b0aee46b8526-codex-lite.js +85 -0
- package/src/tui-compiled/raw/shared/cec39ccddab4fe9c-labels.js +11 -0
- package/src/tui-compiled/raw/shared/cef0f2346957e83c-index.js +552 -0
- package/src/tui-compiled/raw/shared/cf7dedf24f902d68-dump-size.js +41 -0
- package/src/tui-compiled/raw/shared/d422d6dbc4e35199-background-quota-refresh.js +107 -0
- package/src/tui-compiled/raw/shared/d5b268767d330507-doctor.js +81 -0
- package/src/tui-compiled/raw/shared/d7ac5ca41f49121b-index.js +12 -0
- package/src/tui-compiled/raw/shared/d7fa77560855dee2-jitter.js +9 -0
- package/src/tui-compiled/raw/shared/de7f7d8803624369-path.js +31 -0
- package/src/tui-compiled/raw/shared/df0bdc74fb0c0db0-limit-mark.js +27 -0
- package/src/tui-compiled/raw/shared/e26c2bb5722ab047-routing-hold.js +23 -0
- package/src/tui-compiled/raw/shared/e2ae081583adfce3-dump.js +80 -0
- package/src/tui-compiled/raw/shared/e44ba1b3a9903f5d-reset-credits.js +483 -0
- package/src/tui-compiled/raw/shared/e5229952fc12f647-codex-models.js +34 -0
- package/src/tui-compiled/raw/shared/e8fda84e90176436-uuid-v7.js +13 -0
- package/src/tui-compiled/raw/shared/ea56007991dbf068-terminal.js +19 -0
- package/src/tui-compiled/raw/shared/ec6f1d96e7d70174-quota-refusal.js +145 -0
- package/src/tui-compiled/raw/shared/f03d7b43a214a4a1-canonical-input.js +42 -0
- package/src/tui-compiled/raw/shared/f1012265b3a768c7-merge-laws.js +31 -0
- package/src/tui-compiled/raw/shared/f2ca864fe967415a-cache-policy.js +198 -0
- package/src/tui-compiled/raw/shared/f317a4740f9d76f8-identity.js +21 -0
- package/src/tui-compiled/raw/shared/f607353c4126272a-routing-wait.js +19 -0
- package/src/tui-compiled/raw/shared/f6189f5c9aee31eb-decision.js +55 -0
- package/src/tui-compiled/raw/shared/f6d728f5e601f98b-request-history.js +120 -0
- package/src/tui-compiled/raw/shared/f981e6b15fb76cdb-codex-body.js +28 -0
- package/src/tui-compiled/raw/shared/fda579e5a0a54165-codex-effort.js +60 -0
- package/src/tui-compiled/raw/sidebar-state.js +54 -4
- package/src/tui-compiled/raw/tui/v2-dialog.js +1 -1
- package/src/tui-compiled/raw/tui.tsx +27 -5
- package/src/tui-compiled/runtime/shared/054118b2705fdc20-ansi.js +98 -0
- package/src/tui-compiled/runtime/shared/0895e780489c494d-window.js +33 -0
- package/src/tui-compiled/runtime/shared/0b7d24474e2f33e9-credential-redaction.js +424 -0
- package/src/tui-compiled/runtime/shared/0c9533222cb4380c-namespace.js +224 -0
- package/src/tui-compiled/runtime/shared/112607e46a5b5f58-accounts.js +393 -0
- package/src/tui-compiled/runtime/shared/136fa4abe48ad69d-cachekeep.js +411 -0
- package/src/tui-compiled/runtime/shared/1547bd74f4214d2d-index.js +28 -0
- package/src/tui-compiled/runtime/shared/1567207f323dc733-codex-http.js +44 -0
- package/src/tui-compiled/runtime/shared/15f402885cbb2fb3-envelope-loss.js +151 -0
- package/src/tui-compiled/runtime/shared/18df43cab4563acf-families.js +149 -0
- package/src/tui-compiled/runtime/shared/1a4c289eec261e04-codex-instructions.js +59 -0
- package/src/tui-compiled/runtime/shared/1afd446e190ef85c-vault-messages.js +12 -0
- package/src/tui-compiled/runtime/shared/1b06ec96856ff8b9-codex-endpoint.js +17 -0
- package/src/tui-compiled/runtime/shared/1e2f1a89bb34a773-vault-adapter.js +110 -0
- package/src/tui-compiled/runtime/shared/2194fb8e8b42d7d5-insula-quota.js +73 -0
- package/src/tui-compiled/runtime/shared/22b2bce42f2d6806-handle.js +10 -0
- package/src/tui-compiled/runtime/shared/2577ec69f9973ae9-quota-normalize.js +13 -14
- package/src/tui-compiled/runtime/shared/26055d4e5ad5e71f-codex-request.js +198 -0
- package/src/tui-compiled/runtime/shared/26b57ac4c3b0c3f6-unconfirmed-retain-counter.js +41 -0
- package/src/tui-compiled/runtime/shared/2bb681e99c2721d7-registry.js +538 -0
- package/src/tui-compiled/runtime/shared/2dce6e9613993329-quota-manager.js +13 -20
- package/src/tui-compiled/runtime/shared/2ec33b3e2b8ac38a-terminal.js +168 -0
- package/src/tui-compiled/runtime/shared/30bcca1d1b4240a0-pool-session-pins.js +32 -0
- package/src/tui-compiled/runtime/shared/3af9a210aa2ccdf2-codex-call-status.js +21 -0
- package/src/tui-compiled/runtime/shared/3b31b36bf0877602-wording.js +120 -0
- package/src/tui-compiled/runtime/shared/3c4c1a2d3fc92726-failure.js +59 -0
- package/src/tui-compiled/runtime/shared/3dfe71eec6e0487d-presets.js +240 -0
- package/src/tui-compiled/runtime/shared/43de3a5c4845cfc1-pool-adapter.js +149 -0
- package/src/tui-compiled/runtime/shared/49353b63a833c8fb-session-cache-policy.js +60 -0
- package/src/tui-compiled/runtime/shared/4adf46e07e025363-axes.js +7 -0
- package/src/tui-compiled/runtime/shared/4ba847b77580b1eb-confirm.js +21 -0
- package/src/tui-compiled/runtime/shared/4d05c2d7a531aece-oauth.js +48 -36
- package/src/tui-compiled/runtime/shared/5018d4665e969588-limit-policy.js +159 -0
- package/src/tui-compiled/runtime/shared/595bdf3fff13ba79-limit-identity.js +49 -0
- package/src/tui-compiled/runtime/shared/59def9e82b61204a-session-cache-activity.js +35 -0
- package/src/tui-compiled/runtime/shared/5d806e1a305d14f5-commands.js +879 -0
- package/src/tui-compiled/runtime/shared/5fc39320fa8bd3a0-limit-registry.js +473 -0
- package/src/tui-compiled/runtime/shared/63c1ee7e70e85e8d-vault.js +1 -0
- package/src/tui-compiled/runtime/shared/6545ab7f757314b7-refresh-all-quota.js +237 -0
- package/src/tui-compiled/runtime/shared/66937534c260d20b-stable-json.js +11 -0
- package/src/tui-compiled/runtime/shared/7056acef1b021f19-bridge.js +10 -0
- package/src/tui-compiled/runtime/shared/72d2ca42883809b4-marker.js +107 -0
- package/src/tui-compiled/runtime/shared/775431e21f469203-record.js +3 -0
- package/src/tui-compiled/runtime/shared/799f3ade4efbd1ea-opencode-v1.js +47 -0
- package/src/tui-compiled/runtime/shared/7b2a0471b29e4f5e-wake.js +47 -0
- package/src/tui-compiled/runtime/shared/7b5d6afff4ff32b9-select.js +159 -0
- package/src/tui-compiled/runtime/shared/7bf96bf4d9249b3d-vault-billing.js +108 -0
- package/src/tui-compiled/runtime/shared/7c98bde36aad4819-input-transformations.js +34 -0
- package/src/tui-compiled/runtime/shared/7fb0418303f20a10-index.js +3 -0
- package/src/tui-compiled/runtime/shared/806e12ccbad34388-pi-wait.js +51 -0
- package/src/tui-compiled/runtime/shared/81ae201b724cac14-menu.js +73 -0
- package/src/tui-compiled/runtime/shared/8349ed25dc629e97-scope.js +10 -0
- package/src/tui-compiled/runtime/shared/86da3379973780b9-limit-provenance.js +60 -0
- package/src/tui-compiled/runtime/shared/8aba66ed486d30a7-decide.js +425 -0
- package/src/tui-compiled/runtime/shared/8c0c8df25a5dca1f-manager.js +735 -0
- package/src/tui-compiled/runtime/shared/8c42bbec02f59636-vault-routing.js +212 -0
- package/src/tui-compiled/runtime/shared/8ca6175bfae53e13-envelope.js +123 -0
- package/src/tui-compiled/runtime/shared/8d0245e41049d83f-header-classifier.js +121 -0
- package/src/tui-compiled/runtime/shared/9c162809179c7560-login.js +89 -0
- package/src/tui-compiled/runtime/shared/a1f495296a4b756f-frame-turn.js +72 -0
- package/src/tui-compiled/runtime/shared/a37138dcbd893f55-hold-bounds.js +5 -0
- package/src/tui-compiled/runtime/shared/aa7fb10d77a2cccd-vault-account-menu.js +133 -0
- package/src/tui-compiled/runtime/shared/adc5eeb5a8d28280-persistence.js +9 -0
- package/src/tui-compiled/runtime/shared/b301c79e6378d1fb-vault.js +678 -0
- package/src/tui-compiled/runtime/shared/b3c11de5f36e617a-reset.js +44 -0
- package/src/tui-compiled/runtime/shared/ba6535c610d1edb3-pool-source-state.js +100 -0
- package/src/tui-compiled/runtime/shared/bd4aef685681048c-duration.js +20 -0
- package/src/tui-compiled/runtime/shared/bd5977eca210d2b8-index.js +662 -0
- package/src/tui-compiled/runtime/shared/bfc46d34f4c9db33-limit-recovery.js +127 -0
- package/src/tui-compiled/runtime/shared/c66495424ad84aee-hold.js +25 -0
- package/src/tui-compiled/runtime/shared/c6a67b836bd5afcd-vault-command-menu.js +300 -0
- package/src/tui-compiled/runtime/shared/c9340fb545c4e5de-internal.js +73 -0
- package/src/tui-compiled/runtime/shared/c954c6447b7b9b64-activity.js +52 -0
- package/src/tui-compiled/runtime/shared/cba8b0aee46b8526-codex-lite.js +85 -0
- package/src/tui-compiled/runtime/shared/cec39ccddab4fe9c-labels.js +11 -0
- package/src/tui-compiled/runtime/shared/cef0f2346957e83c-index.js +552 -0
- package/src/tui-compiled/runtime/shared/cf7dedf24f902d68-dump-size.js +41 -0
- package/src/tui-compiled/runtime/shared/d422d6dbc4e35199-background-quota-refresh.js +107 -0
- package/src/tui-compiled/runtime/shared/d5b268767d330507-doctor.js +81 -0
- package/src/tui-compiled/runtime/shared/d7ac5ca41f49121b-index.js +12 -0
- package/src/tui-compiled/runtime/shared/d7fa77560855dee2-jitter.js +9 -0
- package/src/tui-compiled/runtime/shared/de7f7d8803624369-path.js +31 -0
- package/src/tui-compiled/runtime/shared/df0bdc74fb0c0db0-limit-mark.js +27 -0
- package/src/tui-compiled/runtime/shared/e26c2bb5722ab047-routing-hold.js +23 -0
- package/src/tui-compiled/runtime/shared/e2ae081583adfce3-dump.js +80 -0
- package/src/tui-compiled/runtime/shared/e44ba1b3a9903f5d-reset-credits.js +483 -0
- package/src/tui-compiled/runtime/shared/e5229952fc12f647-codex-models.js +34 -0
- package/src/tui-compiled/runtime/shared/e8fda84e90176436-uuid-v7.js +13 -0
- package/src/tui-compiled/runtime/shared/ea56007991dbf068-terminal.js +19 -0
- package/src/tui-compiled/runtime/shared/ec6f1d96e7d70174-quota-refusal.js +145 -0
- package/src/tui-compiled/runtime/shared/f03d7b43a214a4a1-canonical-input.js +42 -0
- package/src/tui-compiled/runtime/shared/f1012265b3a768c7-merge-laws.js +31 -0
- package/src/tui-compiled/runtime/shared/f2ca864fe967415a-cache-policy.js +198 -0
- package/src/tui-compiled/runtime/shared/f317a4740f9d76f8-identity.js +21 -0
- package/src/tui-compiled/runtime/shared/f607353c4126272a-routing-wait.js +19 -0
- package/src/tui-compiled/runtime/shared/f6189f5c9aee31eb-decision.js +55 -0
- package/src/tui-compiled/runtime/shared/f6d728f5e601f98b-request-history.js +120 -0
- package/src/tui-compiled/runtime/shared/f981e6b15fb76cdb-codex-body.js +28 -0
- package/src/tui-compiled/runtime/shared/fda579e5a0a54165-codex-effort.js +60 -0
- package/src/tui-compiled/runtime/sidebar-state.js +54 -4
- package/src/tui-compiled/runtime/tui/v2-dialog.js +1 -1
- package/src/tui-compiled/runtime/tui.js +11 -4
- package/dist/index-0p8r7cpy.js +0 -60
- package/dist/index-px1t650b.js +0 -2
- package/dist/map-D5PHhv4z.d.ts +0 -72
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { sameAccount } from "./f6d728f5e601f98b-request-history.js";
|
|
2
|
+
/** Plugin-owned turn budgets, independent of individual logical request histories. */
|
|
3
|
+
export class UnconfirmedRetainCounter {
|
|
4
|
+
maxSessions;
|
|
5
|
+
sessions = new Map();
|
|
6
|
+
constructor(maxSessions = 4096) {
|
|
7
|
+
this.maxSessions = maxSessions;
|
|
8
|
+
if (!Number.isSafeInteger(maxSessions) || maxSessions < 1)
|
|
9
|
+
throw new RangeError('maxSessions must be a positive safe integer');
|
|
10
|
+
}
|
|
11
|
+
count(sessionId, turnId, account) {
|
|
12
|
+
const entry = this.sessions.get(sessionId);
|
|
13
|
+
return entry?.turnId === turnId && sameAccount(entry.account, account)
|
|
14
|
+
? entry.count
|
|
15
|
+
: 0;
|
|
16
|
+
}
|
|
17
|
+
/** Call once after accepting a decision with incrementUnconfirmedRetain. */
|
|
18
|
+
increment(sessionId, turnId, account) {
|
|
19
|
+
const count = this.count(sessionId, turnId, account);
|
|
20
|
+
if (!count) {
|
|
21
|
+
this.sessions.delete(sessionId);
|
|
22
|
+
if (this.sessions.size >= this.maxSessions) {
|
|
23
|
+
const oldest = this.sessions.keys().next().value;
|
|
24
|
+
if (oldest !== undefined)
|
|
25
|
+
this.sessions.delete(oldest);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
this.sessions.set(sessionId, {
|
|
29
|
+
turnId,
|
|
30
|
+
account: { ...account },
|
|
31
|
+
count: count + 1,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Clear on moves, API fallback and session lifecycle boundaries. */
|
|
35
|
+
clear(sessionId) {
|
|
36
|
+
this.sessions.delete(sessionId);
|
|
37
|
+
}
|
|
38
|
+
clearAll() {
|
|
39
|
+
this.sessions.clear();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { AsyncResource } from "node:async_hooks";
|
|
2
|
+
import { createSessionActivityStore } from "./c954c6447b7b9b64-activity.js";
|
|
3
|
+
import { limitsEnvelopeAbandonReason, } from "./8ca6175bfae53e13-envelope.js";
|
|
4
|
+
import { isIdentityCompatible, retainVerifiedIdentity } from "./f317a4740f9d76f8-identity.js";
|
|
5
|
+
import { applyLimitsCommands, createLimitsNamespace, pruneLimitsNamespace, } from "./0c9533222cb4380c-namespace.js";
|
|
6
|
+
import { LimitsPersistenceRefusal } from "./adc5eeb5a8d28280-persistence.js";
|
|
7
|
+
import { sameAccount } from "./f6d728f5e601f98b-request-history.js";
|
|
8
|
+
export const LIMITS_REGISTRY_KEY = Symbol.for('@cortexkit/common-auth/limits-registry/schema-v1');
|
|
9
|
+
const PREFIX = '@cortexkit/common-auth/limits-registry/schema-v';
|
|
10
|
+
const RELOAD_MS = 1000;
|
|
11
|
+
const IDLE_MS = 5000;
|
|
12
|
+
// Run timers and I/O in the module's async context rather than inheriting a
|
|
13
|
+
// caller's lock-holding callback context, which can prohibit store re-entry.
|
|
14
|
+
const detached = new AsyncResource('common-auth-limits');
|
|
15
|
+
const clock = {
|
|
16
|
+
now: () => Date.now(),
|
|
17
|
+
setTimeout(callback, delay) {
|
|
18
|
+
const timer = setTimeout(callback, delay);
|
|
19
|
+
timer.unref?.();
|
|
20
|
+
return timer;
|
|
21
|
+
},
|
|
22
|
+
clearTimeout: (timer) => clearTimeout(timer),
|
|
23
|
+
};
|
|
24
|
+
/** Publish a recovery fence even when expiry already removed the observed timer.
|
|
25
|
+
* Capture binding/baseRevision with observe before the probe; dispatch to that
|
|
26
|
+
* account joins the clear acknowledgment through beforeDispatch, not here.
|
|
27
|
+
*/
|
|
28
|
+
export function clearLimitsAfterProbe(registry, input) {
|
|
29
|
+
registry.enqueue({ ...input, type: 'clear' }, 'success');
|
|
30
|
+
}
|
|
31
|
+
function schemaRegistry() {
|
|
32
|
+
for (const symbol of Object.getOwnPropertySymbols(globalThis)) {
|
|
33
|
+
const key = Symbol.keyFor(symbol);
|
|
34
|
+
if (key?.startsWith(PREFIX) && symbol !== LIMITS_REGISTRY_KEY) {
|
|
35
|
+
throw new Error('limits-schema-mismatch');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const current = Reflect.get(globalThis, LIMITS_REGISTRY_KEY);
|
|
39
|
+
if (current) {
|
|
40
|
+
if (current.schemaVersion !== 1 || !(current.namespaces instanceof Map))
|
|
41
|
+
throw new Error('limits-schema-mismatch');
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
const created = { schemaVersion: 1, namespaces: new Map() };
|
|
45
|
+
Reflect.set(globalThis, LIMITS_REGISTRY_KEY, created);
|
|
46
|
+
return created;
|
|
47
|
+
}
|
|
48
|
+
function opId(command) {
|
|
49
|
+
return JSON.stringify([command.producerId, command.operationId]);
|
|
50
|
+
}
|
|
51
|
+
class Runtime {
|
|
52
|
+
adapter;
|
|
53
|
+
clock;
|
|
54
|
+
restrictive;
|
|
55
|
+
listeners = new Set();
|
|
56
|
+
owners = 0;
|
|
57
|
+
activity = createSessionActivityStore();
|
|
58
|
+
journal = new Map();
|
|
59
|
+
failed = new Set();
|
|
60
|
+
restrictions = new Map();
|
|
61
|
+
snapshot;
|
|
62
|
+
recoveryView;
|
|
63
|
+
bindings = [];
|
|
64
|
+
lastReloadAt = Number.NEGATIVE_INFINITY;
|
|
65
|
+
lastActivity = Number.NEGATIVE_INFINITY;
|
|
66
|
+
lastReadAttempt = Number.NEGATIVE_INFINITY;
|
|
67
|
+
generation = 0;
|
|
68
|
+
degradedAgreement = false;
|
|
69
|
+
abandoned;
|
|
70
|
+
lastFailure;
|
|
71
|
+
dirty = true;
|
|
72
|
+
live = false;
|
|
73
|
+
publicationTimer;
|
|
74
|
+
reloadTimer;
|
|
75
|
+
unsubscribe;
|
|
76
|
+
reading;
|
|
77
|
+
writing;
|
|
78
|
+
handoff = Promise.resolve();
|
|
79
|
+
resolveAbandonment;
|
|
80
|
+
abandonment = new Promise((resolve) => {
|
|
81
|
+
this.resolveAbandonment = resolve;
|
|
82
|
+
});
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.adapter = options.adapter;
|
|
85
|
+
this.clock = options.clock ?? clock;
|
|
86
|
+
this.restrictive = options.isAccessRestrictive;
|
|
87
|
+
}
|
|
88
|
+
fail(error) {
|
|
89
|
+
if (this.abandoned)
|
|
90
|
+
return;
|
|
91
|
+
const reason = limitsEnvelopeAbandonReason(error);
|
|
92
|
+
if (reason) {
|
|
93
|
+
this.abandoned = reason;
|
|
94
|
+
this.lastFailure = error;
|
|
95
|
+
this.live = false;
|
|
96
|
+
this.generation++;
|
|
97
|
+
this.cancelTimers();
|
|
98
|
+
this.unsubscribe?.();
|
|
99
|
+
this.unsubscribe = undefined;
|
|
100
|
+
this.journal.clear();
|
|
101
|
+
this.failed.clear();
|
|
102
|
+
this.restrictions.clear();
|
|
103
|
+
this.recoveryView = undefined;
|
|
104
|
+
this.resolveAbandonment();
|
|
105
|
+
for (const listener of this.listeners) {
|
|
106
|
+
try {
|
|
107
|
+
listener.onAbandon?.(reason);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Owners cannot prevent terminal cleanup for other owners.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.degradedAgreement = true;
|
|
116
|
+
this.lastFailure = error;
|
|
117
|
+
}
|
|
118
|
+
reconcile(view) {
|
|
119
|
+
if (this.abandoned)
|
|
120
|
+
return false;
|
|
121
|
+
if (view.snapshot.schemaVersion !== 1 ||
|
|
122
|
+
view.snapshot.namespace !== this.adapter.namespace)
|
|
123
|
+
throw new Error('limits-schema-mismatch');
|
|
124
|
+
if (!view.authoritative) {
|
|
125
|
+
// Use repaired restrictions for dispatch, but never expose an unwritten
|
|
126
|
+
// recovery revision as a durable basis for new commands.
|
|
127
|
+
if (view.degradedAgreement &&
|
|
128
|
+
this.snapshot &&
|
|
129
|
+
view.snapshot.revision > this.snapshot.revision)
|
|
130
|
+
this.recoveryView = view;
|
|
131
|
+
this.fail(new Error('limits-unverifiable-snapshot'));
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
// A read started before a commit may finish after it. Never roll that commit back.
|
|
135
|
+
if (this.snapshot &&
|
|
136
|
+
view.snapshot.generation === this.snapshot.generation &&
|
|
137
|
+
view.snapshot.revision < this.snapshot.revision)
|
|
138
|
+
return false;
|
|
139
|
+
this.snapshot = view.snapshot;
|
|
140
|
+
this.recoveryView = undefined;
|
|
141
|
+
this.bindings = view.bindings.map((binding) => {
|
|
142
|
+
const previous = this.bindings.find((b) => sameAccount(b.account, binding.account));
|
|
143
|
+
return previous ? retainVerifiedIdentity(previous, binding) : binding;
|
|
144
|
+
});
|
|
145
|
+
for (const [id, restriction] of this.restrictions) {
|
|
146
|
+
const command = restriction.command;
|
|
147
|
+
const binding = this.bindings.find((b) => sameAccount(b.account, command.binding.account));
|
|
148
|
+
const access = this.snapshot.access.find((a) => sameAccount(a.account, command.binding.account));
|
|
149
|
+
if (!binding ||
|
|
150
|
+
!isIdentityCompatible(binding, command.binding) ||
|
|
151
|
+
(command.type === 'access' &&
|
|
152
|
+
access &&
|
|
153
|
+
access.committedRevision > restriction.revision &&
|
|
154
|
+
access.eligibilityStateUpdatedAt >= command.eligibilityStateUpdatedAt)) {
|
|
155
|
+
this.restrictions.delete(id);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (view.degradedAgreement)
|
|
159
|
+
this.fail(new Error('limits-envelope-recovered'));
|
|
160
|
+
return !view.degradedAgreement;
|
|
161
|
+
}
|
|
162
|
+
touch() {
|
|
163
|
+
this.lastActivity = this.clock.now();
|
|
164
|
+
this.armReload();
|
|
165
|
+
}
|
|
166
|
+
cancelTimers() {
|
|
167
|
+
if (this.publicationTimer !== undefined)
|
|
168
|
+
this.clock.clearTimeout(this.publicationTimer);
|
|
169
|
+
if (this.reloadTimer !== undefined)
|
|
170
|
+
this.clock.clearTimeout(this.reloadTimer);
|
|
171
|
+
this.publicationTimer = this.reloadTimer = undefined;
|
|
172
|
+
}
|
|
173
|
+
schedulePublish(delay) {
|
|
174
|
+
if (!this.live)
|
|
175
|
+
return;
|
|
176
|
+
if (this.publicationTimer !== undefined)
|
|
177
|
+
this.clock.clearTimeout(this.publicationTimer);
|
|
178
|
+
detached.runInAsyncScope(() => {
|
|
179
|
+
this.publicationTimer = this.clock.setTimeout(() => {
|
|
180
|
+
this.publicationTimer = undefined;
|
|
181
|
+
detached.runInAsyncScope(() => {
|
|
182
|
+
void this.flush();
|
|
183
|
+
});
|
|
184
|
+
}, delay);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
armReload() {
|
|
188
|
+
if (!this.live ||
|
|
189
|
+
this.reading ||
|
|
190
|
+
this.reloadTimer !== undefined ||
|
|
191
|
+
this.clock.now() - this.lastActivity >= IDLE_MS)
|
|
192
|
+
return;
|
|
193
|
+
detached.runInAsyncScope(() => {
|
|
194
|
+
this.reloadTimer = this.clock.setTimeout(() => {
|
|
195
|
+
this.reloadTimer = undefined;
|
|
196
|
+
if (!this.live || this.clock.now() - this.lastActivity >= IDLE_MS)
|
|
197
|
+
return;
|
|
198
|
+
detached.runInAsyncScope(() => {
|
|
199
|
+
void this.reload();
|
|
200
|
+
});
|
|
201
|
+
}, Math.max(0, this.lastReadAttempt + RELOAD_MS - this.clock.now()));
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async start(adapter) {
|
|
205
|
+
if (this.abandoned)
|
|
206
|
+
throw this.lastFailure;
|
|
207
|
+
this.adapter = adapter;
|
|
208
|
+
this.live = true;
|
|
209
|
+
const generation = ++this.generation;
|
|
210
|
+
await this.reload();
|
|
211
|
+
if (this.abandoned || !this.snapshot)
|
|
212
|
+
throw this.lastFailure ?? new Error('limits-hydration-failed');
|
|
213
|
+
this.unsubscribe = adapter.subscribe?.(() => {
|
|
214
|
+
if (!this.live || this.generation !== generation)
|
|
215
|
+
return;
|
|
216
|
+
this.dirty = true;
|
|
217
|
+
detached.runInAsyncScope(() => {
|
|
218
|
+
void this.reload();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
if (this.journal.size)
|
|
222
|
+
this.schedulePublish(0);
|
|
223
|
+
}
|
|
224
|
+
reload() {
|
|
225
|
+
if (this.abandoned)
|
|
226
|
+
return Promise.resolve();
|
|
227
|
+
if (this.reading)
|
|
228
|
+
return this.reading;
|
|
229
|
+
const started = this.clock.now();
|
|
230
|
+
this.lastReadAttempt = started;
|
|
231
|
+
this.reading = detached
|
|
232
|
+
.runInAsyncScope(async () => {
|
|
233
|
+
try {
|
|
234
|
+
this.dirty = false;
|
|
235
|
+
const clean = this.reconcile(await this.adapter.read(this.snapshot));
|
|
236
|
+
if (clean) {
|
|
237
|
+
this.lastReloadAt = started;
|
|
238
|
+
if (!this.journal.size &&
|
|
239
|
+
!this.failed.size &&
|
|
240
|
+
!this.restrictions.size) {
|
|
241
|
+
this.degradedAgreement = false;
|
|
242
|
+
this.lastFailure = undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
this.dirty = true;
|
|
248
|
+
this.fail(error);
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
.finally(() => {
|
|
252
|
+
this.reading = undefined;
|
|
253
|
+
this.armReload();
|
|
254
|
+
});
|
|
255
|
+
return this.reading;
|
|
256
|
+
}
|
|
257
|
+
provisional() {
|
|
258
|
+
if (this.abandoned)
|
|
259
|
+
return createLimitsNamespace(this.adapter.namespace, this.snapshot?.generation ?? '');
|
|
260
|
+
const snapshot = this.recoveryView?.snapshot ?? this.snapshot;
|
|
261
|
+
const pending = [...this.journal.values()]
|
|
262
|
+
.map((p) => p.command)
|
|
263
|
+
.filter((c) => c.type === 'mark');
|
|
264
|
+
const provisional = applyLimitsCommands(pruneLimitsNamespace(snapshot, this.clock.now()), pending, {
|
|
265
|
+
now: this.clock.now(),
|
|
266
|
+
bindings: this.bindings,
|
|
267
|
+
authorizeEpoch: (a, b) => a.credentialEpoch === b.credentialEpoch,
|
|
268
|
+
}).snapshot;
|
|
269
|
+
// Pending evidence may restrict routing, but cannot invent durable revisions
|
|
270
|
+
// or acknowledged operation ids for subsequent observations.
|
|
271
|
+
return {
|
|
272
|
+
...this.snapshot,
|
|
273
|
+
marks: provisional.marks,
|
|
274
|
+
tombstones: snapshot.tombstones,
|
|
275
|
+
access: snapshot.access,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
enqueue(command, event) {
|
|
279
|
+
if (this.abandoned)
|
|
280
|
+
return;
|
|
281
|
+
if (command.binding.account.namespace !== this.adapter.namespace)
|
|
282
|
+
throw new Error('wrong-namespace');
|
|
283
|
+
const id = opId(command);
|
|
284
|
+
if (this.journal.has(id) ||
|
|
285
|
+
this.snapshot.operationIds.some((op) => opId(op) === id))
|
|
286
|
+
return;
|
|
287
|
+
this.journal.set(id, { command: structuredClone(command), event });
|
|
288
|
+
this.touch();
|
|
289
|
+
// A control must include all earlier evidence, even if the mark debounce has not fired.
|
|
290
|
+
const hasControl = [...this.journal.values()].some((p) => p.command.type !== 'mark');
|
|
291
|
+
this.schedulePublish(hasControl ? 0 : RELOAD_MS);
|
|
292
|
+
}
|
|
293
|
+
flush() {
|
|
294
|
+
if (this.abandoned)
|
|
295
|
+
return this.writing ?? Promise.resolve();
|
|
296
|
+
if (this.writing)
|
|
297
|
+
return this.writing;
|
|
298
|
+
if (!this.journal.size)
|
|
299
|
+
return Promise.resolve();
|
|
300
|
+
this.writing = detached
|
|
301
|
+
.runInAsyncScope(async () => {
|
|
302
|
+
let batch = [...this.journal.values()];
|
|
303
|
+
while (batch.length && !this.abandoned) {
|
|
304
|
+
try {
|
|
305
|
+
const result = await this.adapter.commit(batch.map((p) => p.command), this.snapshot);
|
|
306
|
+
if (this.abandoned)
|
|
307
|
+
return;
|
|
308
|
+
this.reconcile(result);
|
|
309
|
+
if (!result.authoritative)
|
|
310
|
+
throw new Error('limits-unverifiable-commit');
|
|
311
|
+
for (const pending of batch) {
|
|
312
|
+
const outcome = result.outcomes.find((o) => opId(o) === opId(pending.command));
|
|
313
|
+
if (!outcome)
|
|
314
|
+
throw new Error('limits-missing-acknowledgement');
|
|
315
|
+
this.settle(pending, outcome);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
if (limitsEnvelopeAbandonReason(error)) {
|
|
320
|
+
this.fail(error);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (this.abandoned)
|
|
324
|
+
return;
|
|
325
|
+
if (error instanceof LimitsPersistenceRefusal &&
|
|
326
|
+
error.code === 'pool-not-ready') {
|
|
327
|
+
for (const pending of batch)
|
|
328
|
+
this.settle(pending, {
|
|
329
|
+
...pending.command,
|
|
330
|
+
status: 'rejected',
|
|
331
|
+
reason: 'pool-not-ready',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
for (const pending of batch)
|
|
336
|
+
if (this.journal.has(opId(pending.command)))
|
|
337
|
+
this.failed.add(opId(pending.command));
|
|
338
|
+
this.fail(error);
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const remaining = [...this.journal.values()];
|
|
343
|
+
const lastControl = remaining.findLastIndex((p) => p.command.type !== 'mark');
|
|
344
|
+
// Marks arriving while adapter.commit() runs retain their own debounce.
|
|
345
|
+
// Only a later control needs earlier marks in its acknowledgement.
|
|
346
|
+
batch = remaining.slice(0, lastControl + 1);
|
|
347
|
+
}
|
|
348
|
+
})
|
|
349
|
+
.finally(() => {
|
|
350
|
+
this.writing = undefined;
|
|
351
|
+
if (this.journal.size && this.live)
|
|
352
|
+
this.schedulePublish(RELOAD_MS);
|
|
353
|
+
if (this.degradedAgreement && this.live)
|
|
354
|
+
this.armReload();
|
|
355
|
+
});
|
|
356
|
+
return this.writing;
|
|
357
|
+
}
|
|
358
|
+
settle(pending, outcome) {
|
|
359
|
+
const command = pending.command;
|
|
360
|
+
const id = opId(command);
|
|
361
|
+
this.journal.delete(id);
|
|
362
|
+
this.failed.delete(id);
|
|
363
|
+
if (outcome.status === 'rejected') {
|
|
364
|
+
this.fail(new Error(outcome.reason ?? 'limits-command-rejected'));
|
|
365
|
+
if (command.type === 'access' && this.restrictive(command.state)) {
|
|
366
|
+
this.restrictions.set(id, { command, revision: this.snapshot.revision });
|
|
367
|
+
// Recovery rejects pre-barrier observations. Restrictive access alone
|
|
368
|
+
// can be resubmitted at that barrier without granting availability.
|
|
369
|
+
// Repeated competing recoveries must not keep a dispatch waiting forever;
|
|
370
|
+
// after three retries, keep the account blocked and agreement degraded
|
|
371
|
+
// instead of retrying again.
|
|
372
|
+
if (outcome.reason === 'below-watermark' &&
|
|
373
|
+
command.baseRevision < this.snapshot.lowWatermark &&
|
|
374
|
+
(pending.recoveryRetries ?? 0) < 3) {
|
|
375
|
+
this.journal.set(id, {
|
|
376
|
+
...pending,
|
|
377
|
+
recoveryRetries: (pending.recoveryRetries ?? 0) + 1,
|
|
378
|
+
command: { ...command, baseRevision: this.snapshot.revision },
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
for (const listener of this.listeners) {
|
|
383
|
+
try {
|
|
384
|
+
listener.onRejected?.(command, outcome);
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
/* Observers cannot change settlement. */
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if (outcome.revision !== undefined &&
|
|
392
|
+
(command.type === 'mark' || pending.event === 'success')) {
|
|
393
|
+
for (const listener of this.listeners) {
|
|
394
|
+
try {
|
|
395
|
+
listener.onEvent?.({
|
|
396
|
+
type: command.type === 'mark' ? 'mark' : 'success',
|
|
397
|
+
command,
|
|
398
|
+
revision: outcome.revision,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
catch {
|
|
402
|
+
/* Observers cannot change settlement. */
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
async beforeDispatch(binding) {
|
|
408
|
+
if (this.abandoned)
|
|
409
|
+
return { snapshot: this.provisional(), blocked: false };
|
|
410
|
+
this.touch();
|
|
411
|
+
const controls = () => [...this.journal.values()].some((p) => p.command.type !== 'mark' &&
|
|
412
|
+
sameAccount(p.command.binding.account, binding.account));
|
|
413
|
+
if (controls())
|
|
414
|
+
await Promise.race([this.flush(), this.abandonment]);
|
|
415
|
+
if (this.dirty || this.clock.now() - this.lastReloadAt >= RELOAD_MS)
|
|
416
|
+
await Promise.race([this.reload(), this.abandonment]);
|
|
417
|
+
if (this.abandoned)
|
|
418
|
+
return { snapshot: this.provisional(), blocked: false };
|
|
419
|
+
const current = (this.recoveryView?.bindings ?? this.bindings).find((b) => sameAccount(b.account, binding.account));
|
|
420
|
+
const snapshot = this.provisional();
|
|
421
|
+
return {
|
|
422
|
+
snapshot,
|
|
423
|
+
blocked: controls() ||
|
|
424
|
+
!current ||
|
|
425
|
+
!isIdentityCompatible(current, binding) ||
|
|
426
|
+
[...this.restrictions.values()].some((r) => sameAccount(r.command.binding.account, binding.account)) ||
|
|
427
|
+
[...this.snapshot.access, ...snapshot.access].some((a) => sameAccount(a.account, binding.account) &&
|
|
428
|
+
this.restrictive(a.state)),
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
async stop() {
|
|
432
|
+
this.live = false;
|
|
433
|
+
this.generation++;
|
|
434
|
+
this.cancelTimers();
|
|
435
|
+
await this.flush();
|
|
436
|
+
await this.reading;
|
|
437
|
+
this.unsubscribe?.();
|
|
438
|
+
this.unsubscribe = undefined;
|
|
439
|
+
this.cancelTimers();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/** Shared owners use one journal/listener; a last-owner replacement waits for handoff. */
|
|
443
|
+
export async function acquireLimitsRegistry(options) {
|
|
444
|
+
const registry = schemaRegistry();
|
|
445
|
+
let runtime = registry.namespaces.get(options.adapter.namespace);
|
|
446
|
+
if (!runtime) {
|
|
447
|
+
runtime = new Runtime(options);
|
|
448
|
+
registry.namespaces.set(options.adapter.namespace, runtime);
|
|
449
|
+
}
|
|
450
|
+
const state = runtime;
|
|
451
|
+
const listener = {
|
|
452
|
+
onAbandon: options.onAbandon,
|
|
453
|
+
onEvent: options.onEvent,
|
|
454
|
+
onRejected: options.onRejected,
|
|
455
|
+
};
|
|
456
|
+
// Reserve the owner synchronously so concurrent acquisitions cannot start two listeners.
|
|
457
|
+
const first = state.owners++ === 0;
|
|
458
|
+
state.listeners.add(listener);
|
|
459
|
+
if (first)
|
|
460
|
+
state.handoff = state.handoff
|
|
461
|
+
.catch(() => { })
|
|
462
|
+
.then(() => state.start(options.adapter));
|
|
463
|
+
try {
|
|
464
|
+
await state.handoff;
|
|
465
|
+
}
|
|
466
|
+
catch (error) {
|
|
467
|
+
state.listeners.delete(listener);
|
|
468
|
+
state.owners--;
|
|
469
|
+
state.live = false;
|
|
470
|
+
state.cancelTimers();
|
|
471
|
+
throw error;
|
|
472
|
+
}
|
|
473
|
+
let disposed = false;
|
|
474
|
+
const active = () => {
|
|
475
|
+
if (disposed)
|
|
476
|
+
throw new Error('limits-registry-disposed');
|
|
477
|
+
};
|
|
478
|
+
return {
|
|
479
|
+
get abandoned() {
|
|
480
|
+
return state.abandoned;
|
|
481
|
+
},
|
|
482
|
+
get snapshot() {
|
|
483
|
+
active();
|
|
484
|
+
return state.provisional();
|
|
485
|
+
},
|
|
486
|
+
get lastReloadAt() {
|
|
487
|
+
return state.lastReloadAt;
|
|
488
|
+
},
|
|
489
|
+
get degradedAgreement() {
|
|
490
|
+
return state.degradedAgreement;
|
|
491
|
+
},
|
|
492
|
+
get lastFailure() {
|
|
493
|
+
return state.lastFailure;
|
|
494
|
+
},
|
|
495
|
+
get pendingCommands() {
|
|
496
|
+
return state.journal.size;
|
|
497
|
+
},
|
|
498
|
+
activity: state.activity,
|
|
499
|
+
observe(binding) {
|
|
500
|
+
active();
|
|
501
|
+
return {
|
|
502
|
+
binding: structuredClone(binding),
|
|
503
|
+
baseRevision: state.snapshot.revision,
|
|
504
|
+
};
|
|
505
|
+
},
|
|
506
|
+
enqueue(command, event) {
|
|
507
|
+
active();
|
|
508
|
+
state.enqueue(command, event);
|
|
509
|
+
},
|
|
510
|
+
beforeDispatch(binding) {
|
|
511
|
+
active();
|
|
512
|
+
return state.beforeDispatch(binding);
|
|
513
|
+
},
|
|
514
|
+
flush() {
|
|
515
|
+
active();
|
|
516
|
+
return state.flush();
|
|
517
|
+
},
|
|
518
|
+
reload() {
|
|
519
|
+
active();
|
|
520
|
+
return state.reload();
|
|
521
|
+
},
|
|
522
|
+
dispose() {
|
|
523
|
+
if (disposed)
|
|
524
|
+
return state.handoff;
|
|
525
|
+
disposed = true;
|
|
526
|
+
state.owners--;
|
|
527
|
+
if (!state.owners) {
|
|
528
|
+
state.handoff = state.handoff
|
|
529
|
+
.then(() => state.stop())
|
|
530
|
+
.finally(() => state.listeners.delete(listener));
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
state.listeners.delete(listener);
|
|
534
|
+
}
|
|
535
|
+
return state.handoff;
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
}
|
|
@@ -131,8 +131,7 @@ export class QuotaManager {
|
|
|
131
131
|
if (accountId)
|
|
132
132
|
this.mainQuotaAccountId = accountId;
|
|
133
133
|
this.main = entry;
|
|
134
|
-
|
|
135
|
-
this.clearRateLimited("main");
|
|
134
|
+
this.clearRecoveredRateLimit("main", entry);
|
|
136
135
|
}
|
|
137
136
|
peekMainForPolicy(accountId) {
|
|
138
137
|
if (accountId && this.mainQuotaAccountId && this.mainQuotaAccountId !== accountId)
|
|
@@ -157,8 +156,7 @@ export class QuotaManager {
|
|
|
157
156
|
this.fallbackTokenFps.set(accountId, tokenFingerprint(accessToken));
|
|
158
157
|
else
|
|
159
158
|
this.fallbackTokenFps.delete(accountId);
|
|
160
|
-
|
|
161
|
-
this.clearRateLimited(accountId);
|
|
159
|
+
this.clearRecoveredRateLimit(accountId, entry);
|
|
162
160
|
}
|
|
163
161
|
markRateLimited(accountId, resetAtMs) {
|
|
164
162
|
const existing = this.rateLimitedUntilMap.get(accountId);
|
|
@@ -176,6 +174,14 @@ export class QuotaManager {
|
|
|
176
174
|
clearRateLimited(accountId) {
|
|
177
175
|
this.rateLimitedUntilMap.delete(accountId);
|
|
178
176
|
}
|
|
177
|
+
clearRecoveredRateLimit(accountId, entry) {
|
|
178
|
+
const until = this.rateLimitedUntilMap.get(accountId);
|
|
179
|
+
if (until === void 0 || !Number.isFinite(entry.checkedAt))
|
|
180
|
+
return;
|
|
181
|
+
const short = until - entry.checkedAt <= 18000000, { primary, secondary } = entry.quota, windowMinutes = short ? 300 : 10080, window = primary?.windowMinutes !== void 0 || secondary?.windowMinutes !== void 0 ? [primary, secondary].find((reading) => reading?.windowMinutes === windowMinutes) : short ? primary : secondary;
|
|
182
|
+
if (window && Number.isFinite(window.usedPercent) && window.usedPercent >= 0 && window.usedPercent < 100 && Number.isFinite(window.remainingPercent) && window.remainingPercent > 0)
|
|
183
|
+
this.clearRateLimited(accountId);
|
|
184
|
+
}
|
|
179
185
|
async refreshMain(accessToken) {
|
|
180
186
|
const fp = tokenFingerprint(accessToken);
|
|
181
187
|
if (this.mainTokenFp && this.mainTokenFp !== fp) {
|
|
@@ -358,16 +364,13 @@ export class QuotaManager {
|
|
|
358
364
|
}
|
|
359
365
|
try {
|
|
360
366
|
const fetchStartedAt = this.now(), quota = await this._fetchQuota(accessToken), now = this.now();
|
|
361
|
-
this.
|
|
362
|
-
this.main = {
|
|
367
|
+
this.setMain(accessToken, {
|
|
363
368
|
quota,
|
|
364
369
|
refreshAfter: getQuotaNextRefreshAt(quota, this.storage, now),
|
|
365
370
|
checkedAt: now
|
|
366
|
-
};
|
|
367
|
-
if (quotaLooksHealthy(quota))
|
|
368
|
-
this.clearRateLimited("main");
|
|
371
|
+
}, void 0, !0);
|
|
369
372
|
this.mainLastApiError = void 0;
|
|
370
|
-
this.onMainQuotaFetched?.(quota, now,
|
|
373
|
+
this.onMainQuotaFetched?.(quota, now, thisFetchFp, fetchStartedAt);
|
|
371
374
|
return quota;
|
|
372
375
|
} catch (error) {
|
|
373
376
|
this._handleMainFetchError(error);
|
|
@@ -461,13 +464,3 @@ export class QuotaManager {
|
|
|
461
464
|
function hasAnyQuotaWindow(quota) {
|
|
462
465
|
return Boolean(quota.primary || quota.secondary);
|
|
463
466
|
}
|
|
464
|
-
function quotaLooksHealthy(quota) {
|
|
465
|
-
const { primary, secondary } = quota;
|
|
466
|
-
if (!primary && !secondary)
|
|
467
|
-
return !1;
|
|
468
|
-
if (primary && primary.usedPercent >= 100)
|
|
469
|
-
return !1;
|
|
470
|
-
if (secondary && secondary.usedPercent >= 100)
|
|
471
|
-
return !1;
|
|
472
|
-
return !0;
|
|
473
|
-
}
|