crysk 0.0.0 → 0.3.1
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/LICENSE +176 -0
- package/README.md +611 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +945 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +910 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +845 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// D1-08 PR3 (§9.2): rate-limit bucket identity.
|
|
2
|
+
//
|
|
3
|
+
// bucketId = HMAC(localKey, provider | credentialFingerprint | account |
|
|
4
|
+
// project | rateLimitGroup)
|
|
5
|
+
//
|
|
6
|
+
// Two design invariants that make this NON-obvious to implement naively:
|
|
7
|
+
//
|
|
8
|
+
// 1. Unknown scope MUST coarsen, never split (spec: "Bucket Identity
|
|
9
|
+
// Coarsens Unknown Scope"). An omitted `account`/`project`/
|
|
10
|
+
// `rateLimitGroup` enters the HMAC as the literal sentinel "unknown", so
|
|
11
|
+
// distinct-but-unknown scopes collapse onto ONE bucket rather than each
|
|
12
|
+
// silently getting its own ceiling. This is not a fallback for a bug —
|
|
13
|
+
// it is the correct, conservative behavior for every credential kind
|
|
14
|
+
// that has not yet learned to report its scope (today: 100% of
|
|
15
|
+
// claude_subscription_oauth).
|
|
16
|
+
//
|
|
17
|
+
// 2. Fields are length-prefixed BEFORE the `|` join (threat matrix:
|
|
18
|
+
// "Delimiter collision"). Naive concatenation lets a scope field that
|
|
19
|
+
// itself contains `|` shift the join boundary and forge another bucket's
|
|
20
|
+
// identity — e.g. `{account:"a|b"}` naively joining to the same string as
|
|
21
|
+
// `{account:"a", project:"b"}`. `len:value` prefixes make that shift
|
|
22
|
+
// structurally impossible: the decoder (conceptually) always knows where
|
|
23
|
+
// one field ends because its length is stated up front.
|
|
24
|
+
//
|
|
25
|
+
// `deriveBucketId` is pure. `loadOrCreateBucketKey` is the one impure half —
|
|
26
|
+
// injectable for offline tests, defaulting to real fs/crypto in production.
|
|
27
|
+
|
|
28
|
+
import { createHmac, randomBytes as nodeRandomBytes } from "node:crypto";
|
|
29
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
30
|
+
import { prheroLayout } from "../home-preflight";
|
|
31
|
+
|
|
32
|
+
const UNKNOWN_SCOPE_SENTINEL = "unknown";
|
|
33
|
+
const BUCKET_KEY_BYTES = 32;
|
|
34
|
+
|
|
35
|
+
export interface BucketScope {
|
|
36
|
+
readonly account?: string;
|
|
37
|
+
readonly project?: string;
|
|
38
|
+
readonly rateLimitGroup?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface BucketIdentityInput {
|
|
42
|
+
readonly provider: string;
|
|
43
|
+
readonly credentialFingerprint: string;
|
|
44
|
+
readonly scope?: BucketScope;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// `len:value` — a field's stated length is checked before its content is
|
|
48
|
+
// consumed, so a `|` (or any other byte) inside `value` cannot be mistaken
|
|
49
|
+
// for the next field's boundary.
|
|
50
|
+
function encodeField(value: string): string {
|
|
51
|
+
return `${value.length}:${value}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function scopeFieldOrUnknown(value: string | undefined): string {
|
|
55
|
+
return value === undefined || value.length === 0
|
|
56
|
+
? UNKNOWN_SCOPE_SENTINEL
|
|
57
|
+
: value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Exposed for tests that want to assert on the exact pre-HMAC message shape
|
|
61
|
+
// without duplicating the encoding rule; not otherwise a public contract.
|
|
62
|
+
export function bucketIdentityMessage(input: BucketIdentityInput): string {
|
|
63
|
+
const fields = [
|
|
64
|
+
input.provider,
|
|
65
|
+
input.credentialFingerprint,
|
|
66
|
+
scopeFieldOrUnknown(input.scope?.account),
|
|
67
|
+
scopeFieldOrUnknown(input.scope?.project),
|
|
68
|
+
scopeFieldOrUnknown(input.scope?.rateLimitGroup),
|
|
69
|
+
];
|
|
70
|
+
return fields.map(encodeField).join("|");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The output is a non-secret bucket ID (redaction, not authentication —
|
|
74
|
+
// §9.2/Q5): safe to appear in pipeline.json or logs. The localKey is what
|
|
75
|
+
// must never appear there.
|
|
76
|
+
export function deriveBucketId(
|
|
77
|
+
input: BucketIdentityInput,
|
|
78
|
+
localKey: Uint8Array,
|
|
79
|
+
): string {
|
|
80
|
+
return createHmac("sha256", localKey)
|
|
81
|
+
.update(bucketIdentityMessage(input))
|
|
82
|
+
.digest("hex");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface LoadOrCreateBucketKeyDeps {
|
|
86
|
+
readonly existsFn?: (p: string) => boolean;
|
|
87
|
+
readonly readFileFn?: (p: string) => Buffer;
|
|
88
|
+
readonly writeFileFn?: (p: string, data: Buffer) => void;
|
|
89
|
+
readonly mkdirFn?: (p: string) => void;
|
|
90
|
+
readonly randomBytesFn?: (n: number) => Buffer;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// spec: "Persisted HMAC Key Yields Cross-Run-Stable Bucket IDs" — a 32-byte
|
|
94
|
+
// key file at `bucketKeyPath` (Q5: persisted, not per-process, so bucket IDs
|
|
95
|
+
// stay comparable across runs for the deferred durable ledger), mode 0600,
|
|
96
|
+
// created on first use. Rotation is delete-the-file; there is no versioning
|
|
97
|
+
// because the key's job is redaction, not authentication, so an old bucket
|
|
98
|
+
// ID silently going stale after rotation is an accepted, harmless cost.
|
|
99
|
+
export function loadOrCreateBucketKey(
|
|
100
|
+
home: string,
|
|
101
|
+
deps: LoadOrCreateBucketKeyDeps = {},
|
|
102
|
+
): Buffer {
|
|
103
|
+
const exists = deps.existsFn ?? existsSync;
|
|
104
|
+
const readBytes = deps.readFileFn ?? ((p: string) => readFileSync(p));
|
|
105
|
+
const writeBytes =
|
|
106
|
+
deps.writeFileFn ??
|
|
107
|
+
((p: string, data: Buffer) =>
|
|
108
|
+
writeFileSync(p, data, { mode: 0o600, flag: "wx" }));
|
|
109
|
+
const mkdir =
|
|
110
|
+
deps.mkdirFn ?? ((p: string) => mkdirSync(p, { recursive: true }));
|
|
111
|
+
const makeRandomBytes =
|
|
112
|
+
deps.randomBytesFn ?? ((n: number) => nodeRandomBytes(n));
|
|
113
|
+
|
|
114
|
+
const layout = prheroLayout(home);
|
|
115
|
+
if (exists(layout.bucketKeyPath)) {
|
|
116
|
+
return readBytes(layout.bucketKeyPath);
|
|
117
|
+
}
|
|
118
|
+
mkdir(layout.dir);
|
|
119
|
+
const key = makeRandomBytes(BUCKET_KEY_BYTES);
|
|
120
|
+
try {
|
|
121
|
+
writeBytes(layout.bucketKeyPath, key);
|
|
122
|
+
return key;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
const code =
|
|
125
|
+
error !== null &&
|
|
126
|
+
typeof error === "object" &&
|
|
127
|
+
"code" in error &&
|
|
128
|
+
typeof error.code === "string"
|
|
129
|
+
? error.code
|
|
130
|
+
: undefined;
|
|
131
|
+
if (code === "EEXIST") {
|
|
132
|
+
return readBytes(layout.bucketKeyPath);
|
|
133
|
+
}
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// D1-08 PR3 (§9.2): "The limiter enforces global and per-bucket ceilings
|
|
2
|
+
// with abort-aware FIFO queues. A lease is released exactly once in
|
|
3
|
+
// finally. `local_fenced_remote_unconfirmed` releases the local execution
|
|
4
|
+
// slot but trips the bucket circuit breaker to zero new capacity until
|
|
5
|
+
// reconciliation or operator reset."
|
|
6
|
+
//
|
|
7
|
+
// This module is the pure mechanism only — no harness wiring, no
|
|
8
|
+
// AttemptAdmissionGate (that interface, and the lease-acquire/release call
|
|
9
|
+
// sites inside the attempt loop, land in PR5a per the design's own tripwire
|
|
10
|
+
// table: "PR3 | None — pure"). `ConcurrencyLimiter` is deliberately
|
|
11
|
+
// standalone and directly testable so PR5a's wiring work is "wrap this in
|
|
12
|
+
// AttemptAdmissionGate," not "invent FIFO/breaker semantics under time
|
|
13
|
+
// pressure."
|
|
14
|
+
//
|
|
15
|
+
// Design notes worth pinning here because a queue implementation drifts
|
|
16
|
+
// easily:
|
|
17
|
+
// - Admission is FIFO per bucket. A global ceiling is checked ALONGSIDE the
|
|
18
|
+
// per-bucket ceiling — both must have room before a queued request is
|
|
19
|
+
// admitted, so a busy bucket cannot starve capacity another bucket is
|
|
20
|
+
// entitled to, and the global cap still holds when every bucket is under
|
|
21
|
+
// its own ceiling.
|
|
22
|
+
// - `release()` is idempotent (§9.2 "released exactly once in finally" is
|
|
23
|
+
// the CALLER's contract; this module tolerates a caller that calls it
|
|
24
|
+
// twice anyway, cheaply, rather than making a caller bug corrupt the
|
|
25
|
+
// slot count).
|
|
26
|
+
// - Aborting a QUEUED request removes it from the queue and rejects it
|
|
27
|
+
// without ever having consumed a slot — required by "FIFO Abort-Aware
|
|
28
|
+
// Admission."
|
|
29
|
+
// - `tripBreaker` is permanent for the lifetime of this limiter instance
|
|
30
|
+
// (the process, i.e. "the rest of the run" per spec — cross-run
|
|
31
|
+
// persistence is explicitly out of scope, §"In-Memory Ledger With
|
|
32
|
+
// Surfaced Cross-Run Gap"). It rejects every currently-queued request for
|
|
33
|
+
// that bucket immediately, and every future `acquire()` call for it,
|
|
34
|
+
// without ever granting a slot.
|
|
35
|
+
|
|
36
|
+
export class BucketBreakerTrippedError extends Error {
|
|
37
|
+
readonly bucketId: string;
|
|
38
|
+
|
|
39
|
+
constructor(bucketId: string) {
|
|
40
|
+
super(
|
|
41
|
+
`bucket "${bucketId}" circuit breaker is tripped: no new capacity this run`,
|
|
42
|
+
);
|
|
43
|
+
this.name = "BucketBreakerTrippedError";
|
|
44
|
+
this.bucketId = bucketId;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class ConcurrencyAdmissionAbortedError extends Error {
|
|
49
|
+
readonly bucketId: string;
|
|
50
|
+
|
|
51
|
+
constructor(bucketId: string) {
|
|
52
|
+
super(
|
|
53
|
+
`admission to bucket "${bucketId}" was aborted before a slot was granted`,
|
|
54
|
+
);
|
|
55
|
+
this.name = "ConcurrencyAdmissionAbortedError";
|
|
56
|
+
this.bucketId = bucketId;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ConcurrencyLease {
|
|
61
|
+
release(): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ConcurrencyLimiterOptions {
|
|
65
|
+
// Per-bucket ceiling. Uniform across buckets in PR3 — a per-bucket
|
|
66
|
+
// override table is not part of this slice's scope.
|
|
67
|
+
readonly bucketCeiling: number;
|
|
68
|
+
// Optional global ceiling across every bucket combined. Defaults to
|
|
69
|
+
// Number.POSITIVE_INFINITY (no global cap beyond the per-bucket ones).
|
|
70
|
+
readonly globalCeiling?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface QueueEntry {
|
|
74
|
+
readonly resolve: (lease: ConcurrencyLease) => void;
|
|
75
|
+
readonly reject: (error: Error) => void;
|
|
76
|
+
readonly detachAbortListener: () => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export class ConcurrencyLimiter {
|
|
80
|
+
private readonly bucketCeiling: number;
|
|
81
|
+
private readonly globalCeiling: number;
|
|
82
|
+
private readonly bucketInUse = new Map<string, number>();
|
|
83
|
+
private readonly bucketQueues = new Map<string, QueueEntry[]>();
|
|
84
|
+
private readonly trippedBuckets = new Set<string>();
|
|
85
|
+
private globalInUse = 0;
|
|
86
|
+
|
|
87
|
+
constructor(options: ConcurrencyLimiterOptions) {
|
|
88
|
+
this.bucketCeiling = options.bucketCeiling;
|
|
89
|
+
this.globalCeiling = options.globalCeiling ?? Number.POSITIVE_INFINITY;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
acquire(bucketId: string, signal?: AbortSignal): Promise<ConcurrencyLease> {
|
|
93
|
+
if (this.trippedBuckets.has(bucketId)) {
|
|
94
|
+
return Promise.reject(new BucketBreakerTrippedError(bucketId));
|
|
95
|
+
}
|
|
96
|
+
if (signal?.aborted) {
|
|
97
|
+
return Promise.reject(new ConcurrencyAdmissionAbortedError(bucketId));
|
|
98
|
+
}
|
|
99
|
+
return new Promise<ConcurrencyLease>((resolve, reject) => {
|
|
100
|
+
const onAbort = (): void => {
|
|
101
|
+
this.removeQueued(bucketId, entry);
|
|
102
|
+
reject(new ConcurrencyAdmissionAbortedError(bucketId));
|
|
103
|
+
};
|
|
104
|
+
const detachAbortListener = (): void => {
|
|
105
|
+
signal?.removeEventListener("abort", onAbort);
|
|
106
|
+
};
|
|
107
|
+
const entry: QueueEntry = { resolve, reject, detachAbortListener };
|
|
108
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
109
|
+
|
|
110
|
+
const queue = this.bucketQueues.get(bucketId) ?? [];
|
|
111
|
+
queue.push(entry);
|
|
112
|
+
this.bucketQueues.set(bucketId, queue);
|
|
113
|
+
this.pump(bucketId);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Permanent for the life of this limiter instance ("the rest of the
|
|
118
|
+
// run"). Every currently-queued request for the bucket is rejected right
|
|
119
|
+
// away — none of them was ever granted a slot, so there is nothing to
|
|
120
|
+
// release.
|
|
121
|
+
tripBreaker(bucketId: string): void {
|
|
122
|
+
this.trippedBuckets.add(bucketId);
|
|
123
|
+
const queue = this.bucketQueues.get(bucketId);
|
|
124
|
+
if (queue === undefined) return;
|
|
125
|
+
this.bucketQueues.set(bucketId, []);
|
|
126
|
+
for (const entry of queue) {
|
|
127
|
+
entry.detachAbortListener();
|
|
128
|
+
entry.reject(new BucketBreakerTrippedError(bucketId));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private removeQueued(bucketId: string, entry: QueueEntry): void {
|
|
133
|
+
const queue = this.bucketQueues.get(bucketId);
|
|
134
|
+
if (queue === undefined) return;
|
|
135
|
+
const index = queue.indexOf(entry);
|
|
136
|
+
if (index !== -1) queue.splice(index, 1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private pump(bucketId: string): void {
|
|
140
|
+
const queue = this.bucketQueues.get(bucketId);
|
|
141
|
+
if (queue === undefined) return;
|
|
142
|
+
while (
|
|
143
|
+
queue.length > 0 &&
|
|
144
|
+
(this.bucketInUse.get(bucketId) ?? 0) < this.bucketCeiling &&
|
|
145
|
+
this.globalInUse < this.globalCeiling
|
|
146
|
+
) {
|
|
147
|
+
const entry = queue.shift();
|
|
148
|
+
if (entry === undefined) break;
|
|
149
|
+
entry.detachAbortListener();
|
|
150
|
+
this.bucketInUse.set(bucketId, (this.bucketInUse.get(bucketId) ?? 0) + 1);
|
|
151
|
+
this.globalInUse++;
|
|
152
|
+
let released = false;
|
|
153
|
+
entry.resolve({
|
|
154
|
+
release: () => {
|
|
155
|
+
if (released) return;
|
|
156
|
+
released = true;
|
|
157
|
+
this.bucketInUse.set(
|
|
158
|
+
bucketId,
|
|
159
|
+
(this.bucketInUse.get(bucketId) ?? 0) - 1,
|
|
160
|
+
);
|
|
161
|
+
this.globalInUse--;
|
|
162
|
+
// Releasing a slot frees per-bucket capacity for THIS bucket and
|
|
163
|
+
// global capacity for every bucket — a queued request in a
|
|
164
|
+
// DIFFERENT bucket may be waiting on the global ceiling alone, so
|
|
165
|
+
// every bucket with a non-empty queue must get a pump chance, not
|
|
166
|
+
// only the one that just released.
|
|
167
|
+
this.pump(bucketId);
|
|
168
|
+
for (const otherBucketId of this.bucketQueues.keys()) {
|
|
169
|
+
if (otherBucketId !== bucketId) this.pump(otherBucketId);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|