@cortexkit/opencode-openai-auth 1.23.1 → 1.23.2
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-by5wfvqm.js} +2 -2
- package/dist/index-nd2ks7er.js +2 -0
- package/dist/index-t876xhkr.js +60 -0
- package/dist/{index-p81hxfe6.js → index-v8w8j1wx.js} +1 -1
- package/dist/{index-3ya2s179.js → index-v8wrmhyp.js} +1 -1
- package/dist/{index-bd18sfjj.js → index-x00sxt2w.js} +10 -10
- 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,424 @@
|
|
|
1
|
+
import { types } from "node:util";
|
|
2
|
+
import { redactStrings } from "./d669d72c40e9e116-logger.js";
|
|
3
|
+
const escapedErrorField = /"(?:e|\\u0065)(?:r|\\u0072){2}(?:o|\\u006f)(?:r|\\u0072)"\s*:/i, escapedErrorType = /"(?:t|\\u0074)(?:y|\\u0079)(?:p|\\u0070)(?:e|\\u0065)"\s*:\s*"(?:e|\\u0065)(?:r|\\u0072){2}(?:o|\\u006f)(?:r|\\u0072)"/i;
|
|
4
|
+
function decodeEscapes(text) {
|
|
5
|
+
return text.replace(/\\(?:u([a-f\d]{4})|(["\\/bfnrt]))/gi, (sequence, hex, short) => {
|
|
6
|
+
if (hex)
|
|
7
|
+
return String.fromCharCode(Number.parseInt(hex, 16));
|
|
8
|
+
return { b: "\b", f: "\f", n: `
|
|
9
|
+
`, r: "\r", t: "\t" }[short] ?? short ?? sequence;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function maskKnownCredential(text, credential) {
|
|
13
|
+
if (!credential)
|
|
14
|
+
return text;
|
|
15
|
+
const literal = text.split(credential).join("[credential]"), needle = credential.replace(/\s/g, "");
|
|
16
|
+
if (!needle)
|
|
17
|
+
return literal;
|
|
18
|
+
const normalized = literal.replace(/\s/g, "");
|
|
19
|
+
if (!normalized.includes(needle))
|
|
20
|
+
return literal;
|
|
21
|
+
const positions = [];
|
|
22
|
+
for (let index = 0;index < literal.length; index++) {
|
|
23
|
+
const character = literal[index] ?? "";
|
|
24
|
+
if (!/\s/.test(character))
|
|
25
|
+
positions.push(index);
|
|
26
|
+
}
|
|
27
|
+
const parts = [];
|
|
28
|
+
let copied = 0;
|
|
29
|
+
for (let from = normalized.indexOf(needle);from >= 0; from = normalized.indexOf(needle, from + needle.length)) {
|
|
30
|
+
const start = positions[from] ?? copied, end = (positions[from + needle.length - 1] ?? start) + 1;
|
|
31
|
+
parts.push(literal.slice(copied, start), "[credential]");
|
|
32
|
+
copied = end;
|
|
33
|
+
}
|
|
34
|
+
return parts.length ? parts.join("") + literal.slice(copied) : literal;
|
|
35
|
+
}
|
|
36
|
+
function redactRawText(text, credential) {
|
|
37
|
+
const masked = maskKnownCredential(text, credential);
|
|
38
|
+
return redactStrings(masked).replaceAll("***REDACTED***", "[credential]");
|
|
39
|
+
}
|
|
40
|
+
export function redactCredentialText(text, credential) {
|
|
41
|
+
const raw = redactRawText(text, credential), decoded = decodeEscapes(raw), safe = redactRawText(decoded, credential);
|
|
42
|
+
return safe === decoded ? raw : safe;
|
|
43
|
+
}
|
|
44
|
+
function canonicalNumber(text) {
|
|
45
|
+
const parts = /^(-?)(0|[1-9]\d*)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
46
|
+
if (!parts)
|
|
47
|
+
return;
|
|
48
|
+
const fraction = parts[3] ?? "", digits = (parts[2] ?? "") + fraction;
|
|
49
|
+
let first = 0;
|
|
50
|
+
while (digits[first] === "0")
|
|
51
|
+
first++;
|
|
52
|
+
if (first === digits.length)
|
|
53
|
+
return "0";
|
|
54
|
+
let end = digits.length;
|
|
55
|
+
while (digits[end - 1] === "0")
|
|
56
|
+
end--;
|
|
57
|
+
const exponent = parts[4] ?? "0";
|
|
58
|
+
let start = exponent.startsWith("-") || exponent.startsWith("+") ? 1 : 0;
|
|
59
|
+
while (exponent[start] === "0")
|
|
60
|
+
start++;
|
|
61
|
+
const significant = exponent.slice(start) || "0";
|
|
62
|
+
if (significant.length > 16)
|
|
63
|
+
return;
|
|
64
|
+
const scale = BigInt((exponent.startsWith("-") ? "-" : "") + significant) - BigInt(fraction.length) + BigInt(digits.length - end);
|
|
65
|
+
return `${parts[1]}${digits.slice(first, end)}e${scale}`;
|
|
66
|
+
}
|
|
67
|
+
function redactCredentialJsonText(text, credential) {
|
|
68
|
+
const numericCredential = credential ? canonicalNumber(credential) : void 0;
|
|
69
|
+
return text.replace(/"(?:\\.|[^"\\])*"|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null/g, (literal) => {
|
|
70
|
+
if (!literal.startsWith('"'))
|
|
71
|
+
return credential && (literal === credential || numericCredential !== void 0 && canonicalNumber(literal) === numericCredential || (credential === "Infinity" || credential === "-Infinity") && /^[-\d]/.test(literal) && String(Number(literal)) === credential) ? '"[credential]"' : literal;
|
|
72
|
+
const decoded = decodeEscapes(literal.slice(1, -1)), redacted = redactCredentialText(decoded, credential);
|
|
73
|
+
return redacted === decoded ? literal : JSON.stringify(redacted);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function redactDiagnosticBody(body, credential) {
|
|
77
|
+
const lexical = redactCredentialJsonText(body, credential);
|
|
78
|
+
try {
|
|
79
|
+
JSON.parse(body);
|
|
80
|
+
return lexical;
|
|
81
|
+
} catch {
|
|
82
|
+
return redactCredentialText(lexical, credential);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const diagnosticLimit = 16384, responseBodyLimit = 65536, INCOMPLETE_PROVIDER_REFUSAL = "No diagnostic was received.";
|
|
86
|
+
async function readBoundedRefusalBody(body) {
|
|
87
|
+
if (!body)
|
|
88
|
+
return "";
|
|
89
|
+
const reader = body.getReader(), buffer = new Uint8Array(responseBodyLimit), decoder = new TextDecoder, deadline = performance.now() + 2000;
|
|
90
|
+
let size = 0, timer;
|
|
91
|
+
try {
|
|
92
|
+
const read = async () => {
|
|
93
|
+
while (performance.now() < deadline) {
|
|
94
|
+
const { done, value } = await reader.read();
|
|
95
|
+
if (done)
|
|
96
|
+
return decoder.decode(buffer.subarray(0, size));
|
|
97
|
+
if (size + value.byteLength > buffer.byteLength)
|
|
98
|
+
return;
|
|
99
|
+
buffer.set(value, size);
|
|
100
|
+
size += value.byteLength;
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
};
|
|
104
|
+
return await Promise.race([
|
|
105
|
+
read(),
|
|
106
|
+
new Promise((resolve) => {
|
|
107
|
+
timer = setTimeout(resolve, 2000, void 0);
|
|
108
|
+
})
|
|
109
|
+
]);
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
} finally {
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
reader.cancel().catch(() => {});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const retryBodyPatterns = [
|
|
118
|
+
/429|500|502|503|504|524/i,
|
|
119
|
+
/rate increased too quickly|rate limit|rate-limit|rate_limit|too many requests/i,
|
|
120
|
+
/overloaded|service unavailable|service_unavailable|service-unavailable|internal error|internal_error|internal server error|server error|server_error|server-error|provider returned error|provider_returned_error|provider-returned-error/i,
|
|
121
|
+
/terminated|fetch failed|failed to fetch|network[-_\s]error|upstream connect|connection error|connection refused|connection lost|socket connection was closed|socket hang up|reset before headers|getaddrinfo|enotfound|eai_again|econnrefused|econnreset|etimedout/i,
|
|
122
|
+
/^timeout$|\b(?:request|response|connection|network|stream|read) (?:timeout|timed out|time out)\b/i,
|
|
123
|
+
/try your request again|retry your request|resource exhausted|resource_exhausted/i,
|
|
124
|
+
/\btry again (?:later|in\b)|\b(?:currently|temporarily) at capacity\b/i
|
|
125
|
+
];
|
|
126
|
+
function requestIdFromBody(body) {
|
|
127
|
+
try {
|
|
128
|
+
const value = ownData(JSON.parse(body), "request_id");
|
|
129
|
+
if (typeof value === "string")
|
|
130
|
+
return value;
|
|
131
|
+
if (typeof value !== "number")
|
|
132
|
+
return;
|
|
133
|
+
let depth = 0, key, atKey = !0, atValue = !1, requestId;
|
|
134
|
+
for (const match of body.matchAll(/"(?:\\.|[^"\\])*"|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|[{}[\]:,]/g)) {
|
|
135
|
+
const token = match[0];
|
|
136
|
+
if (token === "{" || token === "[") {
|
|
137
|
+
depth++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (token === "}" || token === "]") {
|
|
141
|
+
depth--;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (depth !== 1)
|
|
145
|
+
continue;
|
|
146
|
+
if (token === ",") {
|
|
147
|
+
atKey = !0;
|
|
148
|
+
atValue = !1;
|
|
149
|
+
key = void 0;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (token === ":") {
|
|
153
|
+
atValue = !0;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (atKey && token.startsWith('"')) {
|
|
157
|
+
key = JSON.parse(token);
|
|
158
|
+
atKey = !1;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (atValue && key === "request_id" && !token.startsWith('"'))
|
|
162
|
+
requestId = token;
|
|
163
|
+
atValue = !1;
|
|
164
|
+
}
|
|
165
|
+
return requestId;
|
|
166
|
+
} catch {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function projectResponseBody(body, credential) {
|
|
171
|
+
const safe = redactDiagnosticBody(body, credential);
|
|
172
|
+
if (Buffer.byteLength(safe, "utf8") <= responseBodyLimit)
|
|
173
|
+
return safe;
|
|
174
|
+
const markers = retryBodyPatterns.flatMap((pattern) => pattern.exec(safe)?.[0] ?? []);
|
|
175
|
+
for (const marker of ["FreeUsageLimitError", "GoUsageLimitError"])
|
|
176
|
+
if (safe.includes(marker))
|
|
177
|
+
markers.push(marker);
|
|
178
|
+
const compact = { retry: markers };
|
|
179
|
+
try {
|
|
180
|
+
const parsed = JSON.parse(safe);
|
|
181
|
+
if (ownData(parsed, "type") === "error")
|
|
182
|
+
compact.type = "error";
|
|
183
|
+
const source = ownData(parsed, "error"), error = {};
|
|
184
|
+
for (const key of ["code", "type"]) {
|
|
185
|
+
const value = ownData(source, key);
|
|
186
|
+
if (typeof value === "string")
|
|
187
|
+
error[key] = value.slice(0, 256);
|
|
188
|
+
}
|
|
189
|
+
if (Object.keys(error).length)
|
|
190
|
+
compact.error = error;
|
|
191
|
+
const metadata = {};
|
|
192
|
+
for (const key of ["workspace", "limitName"]) {
|
|
193
|
+
const value = ownData(ownData(parsed, "metadata"), key);
|
|
194
|
+
if (typeof value === "string")
|
|
195
|
+
metadata[key] = value.slice(0, 256);
|
|
196
|
+
}
|
|
197
|
+
if (Object.keys(metadata).length)
|
|
198
|
+
compact.metadata = metadata;
|
|
199
|
+
} catch {}
|
|
200
|
+
return JSON.stringify(compact);
|
|
201
|
+
}
|
|
202
|
+
function safeObject(value) {
|
|
203
|
+
if (!value || typeof value !== "object")
|
|
204
|
+
return !1;
|
|
205
|
+
let prototype = value;
|
|
206
|
+
for (let depth = 0;prototype && depth < 16; depth++) {
|
|
207
|
+
if (types.isProxy(prototype))
|
|
208
|
+
return !1;
|
|
209
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
210
|
+
}
|
|
211
|
+
return prototype === null;
|
|
212
|
+
}
|
|
213
|
+
function ownData(value, key) {
|
|
214
|
+
if (!safeObject(value))
|
|
215
|
+
return;
|
|
216
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
217
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
218
|
+
}
|
|
219
|
+
function boundedText(text, credential) {
|
|
220
|
+
return redactCredentialText(text, credential).slice(0, diagnosticLimit);
|
|
221
|
+
}
|
|
222
|
+
function nativeGetter(prototype, key) {
|
|
223
|
+
const getter = Object.getOwnPropertyDescriptor(prototype, key)?.get;
|
|
224
|
+
if (!getter)
|
|
225
|
+
throw Error(`Missing native ${key} accessor`);
|
|
226
|
+
return getter;
|
|
227
|
+
}
|
|
228
|
+
const domMessage = nativeGetter(DOMException.prototype, "message"), domName = nativeGetter(DOMException.prototype, "name");
|
|
229
|
+
export function redactCredentialError(error, credential) {
|
|
230
|
+
let message = ownData(error, "message"), name = ownData(error, "name");
|
|
231
|
+
if (safeObject(error) && error instanceof DOMException)
|
|
232
|
+
try {
|
|
233
|
+
message = domMessage.call(error);
|
|
234
|
+
name = domName.call(error);
|
|
235
|
+
} catch {}
|
|
236
|
+
const result = Error(boundedText(typeof message === "string" ? message : typeof error === "string" ? error : "Provider request failed", credential));
|
|
237
|
+
for (const key of Object.getOwnPropertyNames(result))
|
|
238
|
+
if (key !== "message")
|
|
239
|
+
Reflect.deleteProperty(result, key);
|
|
240
|
+
Object.defineProperty(result, "name", {
|
|
241
|
+
value: typeof name === "string" ? boundedText(name, credential) : "Error",
|
|
242
|
+
configurable: !0,
|
|
243
|
+
writable: !0
|
|
244
|
+
});
|
|
245
|
+
const copy = (source, target, key) => {
|
|
246
|
+
const value = ownData(source, key);
|
|
247
|
+
if (typeof value === "string" || typeof value === "number" && Number.isFinite(value) || typeof value === "boolean")
|
|
248
|
+
Object.defineProperty(target, key, {
|
|
249
|
+
value: typeof value === "string" ? boundedText(value, credential) : value,
|
|
250
|
+
enumerable: !0,
|
|
251
|
+
configurable: !0,
|
|
252
|
+
writable: !0
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
for (const key of [
|
|
256
|
+
"status",
|
|
257
|
+
"statusCode",
|
|
258
|
+
"status_code",
|
|
259
|
+
"resets_at",
|
|
260
|
+
"resets_in_seconds"
|
|
261
|
+
]) {
|
|
262
|
+
const value = ownData(error, key);
|
|
263
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
264
|
+
copy(error, result, key);
|
|
265
|
+
}
|
|
266
|
+
for (const key of ["code", "type", "isRetryable"])
|
|
267
|
+
copy(error, result, key);
|
|
268
|
+
for (const key of ["error", "data"]) {
|
|
269
|
+
const source = ownData(error, key), fields = Object.create(null);
|
|
270
|
+
for (const field of [
|
|
271
|
+
"type",
|
|
272
|
+
"code",
|
|
273
|
+
"message",
|
|
274
|
+
"resets_at",
|
|
275
|
+
"resets_in_seconds"
|
|
276
|
+
])
|
|
277
|
+
copy(source, fields, field);
|
|
278
|
+
const nested = ownData(source, "error");
|
|
279
|
+
if (safeObject(nested)) {
|
|
280
|
+
const classification = Object.create(null);
|
|
281
|
+
for (const field of [
|
|
282
|
+
"type",
|
|
283
|
+
"code",
|
|
284
|
+
"message",
|
|
285
|
+
"resets_at",
|
|
286
|
+
"resets_in_seconds"
|
|
287
|
+
])
|
|
288
|
+
copy(nested, classification, field);
|
|
289
|
+
fields.error = classification;
|
|
290
|
+
}
|
|
291
|
+
if (Object.keys(fields).length)
|
|
292
|
+
Object.defineProperty(result, key, { value: fields, enumerable: !0 });
|
|
293
|
+
}
|
|
294
|
+
const headers = ownData(error, "responseHeaders");
|
|
295
|
+
if (headers && typeof headers === "object") {
|
|
296
|
+
const ownedHeaders = {};
|
|
297
|
+
for (const key of ["retry-after-ms", "retry-after"]) {
|
|
298
|
+
const value = ownData(headers, key);
|
|
299
|
+
if (typeof value === "string")
|
|
300
|
+
ownedHeaders[key] = boundedText(value, credential);
|
|
301
|
+
}
|
|
302
|
+
Object.defineProperty(result, "responseHeaders", {
|
|
303
|
+
value: ownedHeaders,
|
|
304
|
+
enumerable: !0
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
const body = ownData(error, "responseBody");
|
|
308
|
+
if (typeof body === "string")
|
|
309
|
+
Object.defineProperty(result, "responseBody", {
|
|
310
|
+
value: projectResponseBody(body, credential),
|
|
311
|
+
enumerable: !0
|
|
312
|
+
});
|
|
313
|
+
const directId = ownData(error, "request_id"), headerId = ownData(headers, "x-request-id"), requestId = typeof directId === "string" ? directId : typeof headerId === "string" ? headerId : typeof body === "string" ? requestIdFromBody(redactDiagnosticBody(body, credential)) : void 0;
|
|
314
|
+
if (typeof requestId === "string")
|
|
315
|
+
Object.defineProperty(result, "request_id", {
|
|
316
|
+
value: boundedText(requestId, credential),
|
|
317
|
+
enumerable: !0
|
|
318
|
+
});
|
|
319
|
+
for (const marker of ["vercel.ai.error", "vercel.ai.error.AI_APICallError"]) {
|
|
320
|
+
const key = Symbol.for(marker);
|
|
321
|
+
if (ownData(error, key) === !0)
|
|
322
|
+
Object.defineProperty(result, key, { value: !0 });
|
|
323
|
+
}
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
export function redactCredentialFrame(frame, credential) {
|
|
327
|
+
if (!frame.includes("error") && (!frame.includes("\\u00") || !escapedErrorField.test(frame) && !escapedErrorType.test(frame)))
|
|
328
|
+
return frame;
|
|
329
|
+
let value;
|
|
330
|
+
try {
|
|
331
|
+
value = JSON.parse(frame);
|
|
332
|
+
} catch {
|
|
333
|
+
return frame;
|
|
334
|
+
}
|
|
335
|
+
if (value?.type === "error" || value?.error || value?.response?.error)
|
|
336
|
+
return redactCredentialJsonText(frame, credential);
|
|
337
|
+
return frame;
|
|
338
|
+
}
|
|
339
|
+
const responseStatus = nativeGetter(Response.prototype, "status"), responseStatusText = nativeGetter(Response.prototype, "statusText"), responseHeaders = nativeGetter(Response.prototype, "headers"), responseBody = nativeGetter(Response.prototype, "body"), responseText = Response.prototype.text, headerEntries = Headers.prototype.entries;
|
|
340
|
+
export async function redactCredentialResponse(response, credential, mode = "http", options) {
|
|
341
|
+
try {
|
|
342
|
+
let scrub = function(event) {
|
|
343
|
+
const bom = atStart && event.startsWith("\uFEFF") ? "\uFEFF" : "";
|
|
344
|
+
atStart = !1;
|
|
345
|
+
return bom + event.slice(bom.length).replace(/^data: ?([^\r\n]*)/gm, (line, data) => line.slice(0, line.length - data.length) + redactCredentialFrame(data, credential));
|
|
346
|
+
};
|
|
347
|
+
if (!safeObject(response))
|
|
348
|
+
throw Error("Invalid provider response");
|
|
349
|
+
const status = responseStatus.call(response), headers = responseHeaders.call(response), bodyStream = responseBody.call(response), ownedHeaders = new Headers;
|
|
350
|
+
for (const [name, value] of headerEntries.call(headers))
|
|
351
|
+
ownedHeaders.append(name, value);
|
|
352
|
+
const init = {
|
|
353
|
+
status,
|
|
354
|
+
statusText: redactCredentialText(responseStatusText.call(response), credential),
|
|
355
|
+
headers: ownedHeaders
|
|
356
|
+
};
|
|
357
|
+
if (status < 200 || status > 299) {
|
|
358
|
+
for (const [name, value] of headerEntries.call(headers)) {
|
|
359
|
+
if (redactCredentialText(name, credential?.toLowerCase()) !== name) {
|
|
360
|
+
init.headers.delete(name);
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
try {
|
|
364
|
+
init.headers.set(name, redactCredentialText(value, credential));
|
|
365
|
+
} catch {
|
|
366
|
+
init.headers.delete(name);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
init.headers.delete("content-length");
|
|
370
|
+
if (options?.boundRefusal && (status === 403 || status === 429)) {
|
|
371
|
+
const body = await readBoundedRefusalBody(bodyStream);
|
|
372
|
+
return new Response(body === void 0 ? INCOMPLETE_PROVIDER_REFUSAL : redactDiagnosticBody(body, credential), init);
|
|
373
|
+
}
|
|
374
|
+
const body = await responseText.call(response);
|
|
375
|
+
return new Response(redactDiagnosticBody(body, credential), init);
|
|
376
|
+
}
|
|
377
|
+
if (!bodyStream || mode === "http")
|
|
378
|
+
return response;
|
|
379
|
+
init.headers.delete("content-length");
|
|
380
|
+
const reader = ReadableStream.prototype.getReader.call(bodyStream), decoder = new TextDecoder("utf-8", { ignoreBOM: !0 }), encoder = new TextEncoder;
|
|
381
|
+
let pending = "", atStart = !0;
|
|
382
|
+
return new Response(new ReadableStream({
|
|
383
|
+
async pull(controller) {
|
|
384
|
+
try {
|
|
385
|
+
for (;; ) {
|
|
386
|
+
const chunk = await reader.read();
|
|
387
|
+
pending += decoder.decode(chunk.value, { stream: !chunk.done });
|
|
388
|
+
let emitted = !1;
|
|
389
|
+
for (;; ) {
|
|
390
|
+
const boundary = /\r?\n\r?\n/.exec(pending);
|
|
391
|
+
if (!boundary)
|
|
392
|
+
break;
|
|
393
|
+
const end = boundary.index + boundary[0].length;
|
|
394
|
+
controller.enqueue(encoder.encode(scrub(pending.slice(0, end))));
|
|
395
|
+
pending = pending.slice(end);
|
|
396
|
+
emitted = !0;
|
|
397
|
+
}
|
|
398
|
+
if (chunk.done) {
|
|
399
|
+
if (pending)
|
|
400
|
+
controller.enqueue(encoder.encode(scrub(pending)));
|
|
401
|
+
controller.close();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (emitted)
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
} catch (error) {
|
|
408
|
+
controller.error(redactCredentialError(error, credential));
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
async cancel(reason) {
|
|
412
|
+
try {
|
|
413
|
+
await reader.cancel(reason);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
throw redactCredentialError(error, credential);
|
|
416
|
+
} finally {
|
|
417
|
+
reader.releaseLock();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}), init);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
throw redactCredentialError(error, credential);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { validateIdentityFence } from "./f317a4740f9d76f8-identity.js";
|
|
2
|
+
import { mergeAccessState, mergeTimedMarkEntries } from "./f1012265b3a768c7-merge-laws.js";
|
|
3
|
+
import { sameAccount } from "./f6d728f5e601f98b-request-history.js";
|
|
4
|
+
export const LIMITS_HISTORY_BOUND = 1024;
|
|
5
|
+
function storedCause(cause) {
|
|
6
|
+
return cause === 'rate-limit' || cause === 'payment-balance'
|
|
7
|
+
? cause
|
|
8
|
+
: undefined;
|
|
9
|
+
}
|
|
10
|
+
/** An absent cause is the conservative all-cause fence used by older writers. */
|
|
11
|
+
export function tombstoneCoversCause(clear, cause) {
|
|
12
|
+
return storedCause(clear.cause) === undefined || clear.cause === cause;
|
|
13
|
+
}
|
|
14
|
+
export function clearTombstoneId(clear) {
|
|
15
|
+
return JSON.stringify([timedMarkKeyId(clear.key), storedCause(clear.cause)]);
|
|
16
|
+
}
|
|
17
|
+
export function createLimitsNamespace(namespace, generation) {
|
|
18
|
+
return {
|
|
19
|
+
schemaVersion: 1,
|
|
20
|
+
namespace,
|
|
21
|
+
generation,
|
|
22
|
+
revision: 0,
|
|
23
|
+
lowWatermark: 0,
|
|
24
|
+
marks: [],
|
|
25
|
+
tombstones: [],
|
|
26
|
+
operationIds: [],
|
|
27
|
+
access: [],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function accountId(account) {
|
|
31
|
+
return JSON.stringify([
|
|
32
|
+
account.namespace,
|
|
33
|
+
account.accountId,
|
|
34
|
+
account.incarnation,
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
export function timedMarkKeyId(key) {
|
|
38
|
+
const scope = key.scope;
|
|
39
|
+
return JSON.stringify([
|
|
40
|
+
accountId(key.account),
|
|
41
|
+
scope.kind,
|
|
42
|
+
scope.kind === 'model'
|
|
43
|
+
? scope.model
|
|
44
|
+
: scope.kind === 'family'
|
|
45
|
+
? scope.family
|
|
46
|
+
: null,
|
|
47
|
+
key.window,
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
function operationId(op) {
|
|
51
|
+
return JSON.stringify([op.producerId, op.operationId]);
|
|
52
|
+
}
|
|
53
|
+
/** Apply tombstones before compacting them; the watermark then fences delayed commands. */
|
|
54
|
+
export function pruneLimitsNamespace(snapshot, now) {
|
|
55
|
+
const clears = new Map();
|
|
56
|
+
for (const clear of snapshot.tombstones) {
|
|
57
|
+
const id = timedMarkKeyId(clear.key);
|
|
58
|
+
const list = clears.get(id);
|
|
59
|
+
if (list)
|
|
60
|
+
list.push(clear);
|
|
61
|
+
else
|
|
62
|
+
clears.set(id, [clear]);
|
|
63
|
+
}
|
|
64
|
+
const marks = snapshot.marks.flatMap((mark) => {
|
|
65
|
+
const fences = clears.get(timedMarkKeyId(mark.key));
|
|
66
|
+
const entries = mergeTimedMarkEntries(mark.mergeLaw, fences
|
|
67
|
+
? mark.entries.filter((entry) => !fences.some((clear) => tombstoneCoversCause(clear, entry.cause) &&
|
|
68
|
+
entry.baseRevision < clear.revision))
|
|
69
|
+
: mark.entries, now);
|
|
70
|
+
return entries.length ? [{ ...mark, entries }] : [];
|
|
71
|
+
});
|
|
72
|
+
let lowWatermark = snapshot.lowWatermark;
|
|
73
|
+
const compact = (entries) => {
|
|
74
|
+
const ordered = [...entries].sort((a, b) => a.revision - b.revision);
|
|
75
|
+
const count = Math.max(0, ordered.length - LIMITS_HISTORY_BOUND);
|
|
76
|
+
for (const entry of ordered.slice(0, count)) {
|
|
77
|
+
lowWatermark = Math.max(lowWatermark, entry.revision + 1);
|
|
78
|
+
}
|
|
79
|
+
return ordered.slice(count);
|
|
80
|
+
};
|
|
81
|
+
const tombstones = compact(snapshot.tombstones);
|
|
82
|
+
const operationIds = compact(snapshot.operationIds);
|
|
83
|
+
return {
|
|
84
|
+
...snapshot,
|
|
85
|
+
revision: Math.max(snapshot.revision, lowWatermark),
|
|
86
|
+
marks,
|
|
87
|
+
tombstones,
|
|
88
|
+
operationIds,
|
|
89
|
+
lowWatermark,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** Pure transaction body; load namespace state and identity bindings under one lock. */
|
|
93
|
+
export function applyLimitsCommands(initial, commands, context) {
|
|
94
|
+
let snapshot = pruneLimitsNamespace(initial, context.now);
|
|
95
|
+
const outcomes = [];
|
|
96
|
+
for (const command of commands) {
|
|
97
|
+
const reject = (reason) => {
|
|
98
|
+
outcomes.push({
|
|
99
|
+
producerId: command.producerId,
|
|
100
|
+
operationId: command.operationId,
|
|
101
|
+
status: 'rejected',
|
|
102
|
+
reason,
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
if (command.binding.account.namespace !== snapshot.namespace) {
|
|
106
|
+
reject('wrong-namespace');
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (command.baseRevision < snapshot.lowWatermark) {
|
|
110
|
+
reject('below-watermark');
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (command.baseRevision > snapshot.revision) {
|
|
114
|
+
reject('future-revision');
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const authoritative = context.bindings.find((b) => sameAccount(b.account, command.binding.account));
|
|
118
|
+
if (!authoritative) {
|
|
119
|
+
reject('unknown-account');
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const fence = validateIdentityFence(command.binding, authoritative, context.authorizeEpoch);
|
|
123
|
+
if (fence) {
|
|
124
|
+
reject(fence);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const duplicate = snapshot.operationIds.find((op) => operationId(op) === operationId(command));
|
|
128
|
+
if (duplicate) {
|
|
129
|
+
outcomes.push({ ...duplicate, status: 'duplicate' });
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const account = authoritative.account;
|
|
133
|
+
const revision = snapshot.revision + 1;
|
|
134
|
+
let marks = [...snapshot.marks];
|
|
135
|
+
let tombstones = [...snapshot.tombstones];
|
|
136
|
+
let access = [...snapshot.access];
|
|
137
|
+
if (command.type === 'mark') {
|
|
138
|
+
const key = { account, scope: command.scope, window: command.window };
|
|
139
|
+
const id = timedMarkKeyId(key);
|
|
140
|
+
const previous = marks.find((m) => timedMarkKeyId(m.key) === id);
|
|
141
|
+
if (tombstones.some((clear) => command.baseRevision < clear.revision &&
|
|
142
|
+
tombstoneCoversCause(clear, command.cause) &&
|
|
143
|
+
timedMarkKeyId(clear.key) === id)) {
|
|
144
|
+
reject('cleared');
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (previous && previous.mergeLaw !== command.mergeLaw) {
|
|
148
|
+
reject('merge-law-conflict');
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const entries = mergeTimedMarkEntries(command.mergeLaw, [
|
|
152
|
+
...(previous?.entries ?? []),
|
|
153
|
+
{
|
|
154
|
+
until: command.until,
|
|
155
|
+
evidenceAt: command.evidenceAt,
|
|
156
|
+
baseRevision: command.baseRevision,
|
|
157
|
+
committedRevision: revision,
|
|
158
|
+
...(command.cause === 'rate-limit' ||
|
|
159
|
+
command.cause === 'payment-balance'
|
|
160
|
+
? { cause: command.cause }
|
|
161
|
+
: {}),
|
|
162
|
+
},
|
|
163
|
+
], context.now);
|
|
164
|
+
marks = marks.filter((m) => timedMarkKeyId(m.key) !== id);
|
|
165
|
+
if (entries.length)
|
|
166
|
+
marks.push({ key, mergeLaw: command.mergeLaw, entries });
|
|
167
|
+
}
|
|
168
|
+
else if (command.type === 'clear') {
|
|
169
|
+
const cause = storedCause(command.cause);
|
|
170
|
+
const keys = command.keys
|
|
171
|
+
.map((key) => ({ ...key, account }))
|
|
172
|
+
.filter((key) => command.cause === undefined ||
|
|
173
|
+
!marks.some((mark) => timedMarkKeyId(mark.key) === timedMarkKeyId(key) &&
|
|
174
|
+
mark.entries.some((entry) => entry.cause !== command.cause)));
|
|
175
|
+
const ids = new Set(keys.map(timedMarkKeyId));
|
|
176
|
+
const evidenceAt = command.evidenceAt;
|
|
177
|
+
if (evidenceAt !== undefined &&
|
|
178
|
+
marks.some((m) => ids.has(timedMarkKeyId(m.key)) &&
|
|
179
|
+
m.entries.some((e) => e.evidenceAt > evidenceAt))) {
|
|
180
|
+
reject('newer-evidence');
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
marks = marks.filter((m) => !ids.has(timedMarkKeyId(m.key)));
|
|
184
|
+
// Keep earlier fences for other causes, including an earlier all-cause clear.
|
|
185
|
+
// Replacing them would either lose a fence or extend it to unrelated evidence.
|
|
186
|
+
tombstones = tombstones.filter((t) => !ids.has(timedMarkKeyId(t.key)) ||
|
|
187
|
+
(cause !== undefined && t.cause !== cause));
|
|
188
|
+
// A clear enumerates covered keys, not all scopes on the account.
|
|
189
|
+
for (const key of new Map(keys.map((k) => [timedMarkKeyId(k), k])).values()) {
|
|
190
|
+
tombstones.push({
|
|
191
|
+
key,
|
|
192
|
+
revision,
|
|
193
|
+
...(cause === undefined ? {} : { cause }),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
const incoming = {
|
|
199
|
+
account,
|
|
200
|
+
state: command.state,
|
|
201
|
+
eligibilityStateUpdatedAt: command.eligibilityStateUpdatedAt,
|
|
202
|
+
committedRevision: revision,
|
|
203
|
+
};
|
|
204
|
+
const previous = access.find((a) => sameAccount(a.account, account));
|
|
205
|
+
access = access.filter((a) => !sameAccount(a.account, account));
|
|
206
|
+
access.push(previous ? mergeAccessState(previous, incoming) : incoming);
|
|
207
|
+
}
|
|
208
|
+
const op = {
|
|
209
|
+
producerId: command.producerId,
|
|
210
|
+
operationId: command.operationId,
|
|
211
|
+
revision,
|
|
212
|
+
};
|
|
213
|
+
snapshot = pruneLimitsNamespace({
|
|
214
|
+
...snapshot,
|
|
215
|
+
revision,
|
|
216
|
+
marks,
|
|
217
|
+
tombstones,
|
|
218
|
+
access,
|
|
219
|
+
operationIds: [...snapshot.operationIds, op],
|
|
220
|
+
}, context.now);
|
|
221
|
+
outcomes.push({ ...op, status: 'committed' });
|
|
222
|
+
}
|
|
223
|
+
return { snapshot, outcomes };
|
|
224
|
+
}
|