@goodandready/dsh-key-rotation 0.7.26 → 0.7.28
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 +21 -21
- package/README.md +157 -118
- package/cordis.patch.yml +6 -6
- package/lib/agent-budget.js +68 -68
- package/lib/bucket.js +52 -0
- package/lib/canary.js +56 -56
- package/lib/cascade.js +39 -39
- package/lib/client-helpers.js +21 -21
- package/lib/client.js +95 -5
- package/lib/concurrency.js +72 -72
- package/lib/heal.js +35 -35
- package/lib/histogram.js +66 -66
- package/lib/incident.js +76 -76
- package/lib/index.js +1658 -1402
- package/lib/keycheck.js +31 -0
- package/lib/maintenance.js +64 -0
- package/lib/pool.js +237 -227
- package/lib/quota-window.js +45 -45
- package/lib/quota.js +39 -39
- package/lib/region.js +50 -50
- package/lib/sandbox.js +117 -117
- package/lib/shadow.js +81 -81
- package/lib/usage-report.js +49 -0
- package/lib/webhook.js +133 -64
- package/package.json +58 -58
package/lib/histogram.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
// histogram.js — per-ref latency ring buffer + percentile.
|
|
2
|
-
// ponytail: ring buffer of fixed size, sort-on-read for percentile, no libraries.
|
|
3
|
-
|
|
4
|
-
export const LATENCY_DEFAULT_WINDOW = 200;
|
|
5
|
-
|
|
6
|
-
export class LatencyHistogram {
|
|
7
|
-
constructor({ window = LATENCY_DEFAULT_WINDOW } = {}) {
|
|
8
|
-
const w = Number.isFinite(window) && window > 0 ? Math.floor(window) : LATENCY_DEFAULT_WINDOW;
|
|
9
|
-
this._window = w;
|
|
10
|
-
this._buffers = new Map(); // ref -> Float64Array of size w, plus index/count
|
|
11
|
-
this._lastAt = new Map(); // ref -> epochMs of last sample
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
record(ref, ms) {
|
|
15
|
-
if (!ref || !Number.isFinite(ms) || ms < 0) return;
|
|
16
|
-
let entry = this._buffers.get(ref);
|
|
17
|
-
if (!entry) {
|
|
18
|
-
entry = { buf: new Float64Array(this._window), head: 0, count: 0 };
|
|
19
|
-
this._buffers.set(ref, entry);
|
|
20
|
-
}
|
|
21
|
-
entry.buf[entry.head] = ms;
|
|
22
|
-
entry.head = (entry.head + 1) % this._window;
|
|
23
|
-
if (entry.count < this._window) entry.count += 1;
|
|
24
|
-
this._lastAt.set(ref, Date.now());
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Returns p50/p95/p99 in milliseconds, plus count and lastAt. Sorted copy.
|
|
28
|
-
snapshot(ref) {
|
|
29
|
-
const entry = this._buffers.get(ref);
|
|
30
|
-
const lastAt = this._lastAt.get(ref);
|
|
31
|
-
if (!entry || entry.count === 0) {
|
|
32
|
-
return { count: 0, lastAt: lastAt || null };
|
|
33
|
-
}
|
|
34
|
-
const arr = entry.buf.subarray(0, entry.count);
|
|
35
|
-
const sorted = Array.from(arr).sort((a, b) => a - b);
|
|
36
|
-
const n = sorted.length;
|
|
37
|
-
return {
|
|
38
|
-
count: n,
|
|
39
|
-
lastAt: lastAt || null,
|
|
40
|
-
p50: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.5))],
|
|
41
|
-
p95: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.95))],
|
|
42
|
-
p99: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.99))],
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Returns { [ref]: snapshot }
|
|
47
|
-
snapshotAll() {
|
|
48
|
-
const out = {};
|
|
49
|
-
for (const ref of this._buffers.keys()) out[ref] = this.snapshot(ref);
|
|
50
|
-
return out;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
clear(ref) {
|
|
54
|
-
if (ref) {
|
|
55
|
-
this._buffers.delete(ref);
|
|
56
|
-
this._lastAt.delete(ref);
|
|
57
|
-
} else {
|
|
58
|
-
this._buffers.clear();
|
|
59
|
-
this._lastAt.clear();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get size() {
|
|
64
|
-
return this._buffers.size;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
1
|
+
// histogram.js — per-ref latency ring buffer + percentile.
|
|
2
|
+
// ponytail: ring buffer of fixed size, sort-on-read for percentile, no libraries.
|
|
3
|
+
|
|
4
|
+
export const LATENCY_DEFAULT_WINDOW = 200;
|
|
5
|
+
|
|
6
|
+
export class LatencyHistogram {
|
|
7
|
+
constructor({ window = LATENCY_DEFAULT_WINDOW } = {}) {
|
|
8
|
+
const w = Number.isFinite(window) && window > 0 ? Math.floor(window) : LATENCY_DEFAULT_WINDOW;
|
|
9
|
+
this._window = w;
|
|
10
|
+
this._buffers = new Map(); // ref -> Float64Array of size w, plus index/count
|
|
11
|
+
this._lastAt = new Map(); // ref -> epochMs of last sample
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
record(ref, ms) {
|
|
15
|
+
if (!ref || !Number.isFinite(ms) || ms < 0) return;
|
|
16
|
+
let entry = this._buffers.get(ref);
|
|
17
|
+
if (!entry) {
|
|
18
|
+
entry = { buf: new Float64Array(this._window), head: 0, count: 0 };
|
|
19
|
+
this._buffers.set(ref, entry);
|
|
20
|
+
}
|
|
21
|
+
entry.buf[entry.head] = ms;
|
|
22
|
+
entry.head = (entry.head + 1) % this._window;
|
|
23
|
+
if (entry.count < this._window) entry.count += 1;
|
|
24
|
+
this._lastAt.set(ref, Date.now());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Returns p50/p95/p99 in milliseconds, plus count and lastAt. Sorted copy.
|
|
28
|
+
snapshot(ref) {
|
|
29
|
+
const entry = this._buffers.get(ref);
|
|
30
|
+
const lastAt = this._lastAt.get(ref);
|
|
31
|
+
if (!entry || entry.count === 0) {
|
|
32
|
+
return { count: 0, lastAt: lastAt || null };
|
|
33
|
+
}
|
|
34
|
+
const arr = entry.buf.subarray(0, entry.count);
|
|
35
|
+
const sorted = Array.from(arr).sort((a, b) => a - b);
|
|
36
|
+
const n = sorted.length;
|
|
37
|
+
return {
|
|
38
|
+
count: n,
|
|
39
|
+
lastAt: lastAt || null,
|
|
40
|
+
p50: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.5))],
|
|
41
|
+
p95: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.95))],
|
|
42
|
+
p99: sorted[Math.min(n - 1, Math.floor((n - 1) * 0.99))],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Returns { [ref]: snapshot }
|
|
47
|
+
snapshotAll() {
|
|
48
|
+
const out = {};
|
|
49
|
+
for (const ref of this._buffers.keys()) out[ref] = this.snapshot(ref);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
clear(ref) {
|
|
54
|
+
if (ref) {
|
|
55
|
+
this._buffers.delete(ref);
|
|
56
|
+
this._lastAt.delete(ref);
|
|
57
|
+
} else {
|
|
58
|
+
this._buffers.clear();
|
|
59
|
+
this._lastAt.clear();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get size() {
|
|
64
|
+
return this._buffers.size;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/lib/incident.js
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
// lib/incident.js — auto-create Gitea issue when pool exhausted > threshold.
|
|
2
|
-
// ponytail: minimal — caller provides a token + base URL. No retries on rate-limit.
|
|
3
|
-
|
|
4
|
-
export const INCIDENT_DEFAULT_THRESHOLD_MS = 5 * 60 * 1000; // 5 min
|
|
5
|
-
export const INCIDENT_DEFAULT_COOLDOWN_MS = 30 * 60 * 1000; // 30 min between incidents per provider
|
|
6
|
-
export const INCIDENT_TIMEOUT_MS = 5000;
|
|
7
|
-
|
|
8
|
-
export class IncidentReporter {
|
|
9
|
-
constructor({ token, baseUrl, repo, thresholdMs = INCIDENT_DEFAULT_THRESHOLD_MS, cooldownMs = INCIDENT_DEFAULT_COOLDOWN_MS, fetchImpl } = {}) {
|
|
10
|
-
if (!token) throw new Error('incident: token required');
|
|
11
|
-
if (!baseUrl) throw new Error('incident: baseUrl required');
|
|
12
|
-
if (!repo || !repo.includes('/')) throw new Error('incident: repo (owner/name) required');
|
|
13
|
-
this._token = token;
|
|
14
|
-
this._baseUrl = baseUrl.replace(/\/+$/, '');
|
|
15
|
-
this._repo = repo;
|
|
16
|
-
this._thresholdMs = thresholdMs;
|
|
17
|
-
this._cooldownMs = cooldownMs;
|
|
18
|
-
this._lastIncidentAt = new Map(); // provider -> epochMs
|
|
19
|
-
this._fetch = fetchImpl || (typeof fetch !== 'undefined' ? fetch : () => { throw new Error('incident: no fetch available'); });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Should we report now? Pure; does not perform I/O.
|
|
23
|
-
shouldReport(provider, exhaustedSince, now = Date.now()) {
|
|
24
|
-
if (!provider) return false;
|
|
25
|
-
if (!Number.isFinite(exhaustedSince)) return false;
|
|
26
|
-
if (now - exhaustedSince < this._thresholdMs) return false;
|
|
27
|
-
const last = this._lastIncidentAt.get(provider);
|
|
28
|
-
if (Number.isFinite(last) && now - last < this._cooldownMs) return false;
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
markReported(provider, at = Date.now()) {
|
|
33
|
-
this._lastIncidentAt.set(provider, at);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
resetCooldown(provider) {
|
|
37
|
-
if (provider) this._lastIncidentAt.delete(provider);
|
|
38
|
-
else this._lastIncidentAt.clear();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Open a Gitea issue. ponytail: minimal payload, ignore failures.
|
|
42
|
-
async open(provider, exhaustedSince, now = Date.now()) {
|
|
43
|
-
if (!this.shouldReport(provider, exhaustedSince, now)) return { reported: false };
|
|
44
|
-
const url = `${this._baseUrl}/api/v1/repos/${this._repo}/issues`;
|
|
45
|
-
const body = {
|
|
46
|
-
title: `prod-incident: pool ${provider} exhausted since ${new Date(exhaustedSince).toISOString()}`,
|
|
47
|
-
body: [
|
|
48
|
-
'Auto-generated by `dsh-key-rotation`.',
|
|
49
|
-
'',
|
|
50
|
-
`- provider: \`${provider}\``,
|
|
51
|
-
`- exhaustedSince: \`${new Date(exhaustedSince).toISOString()}\``,
|
|
52
|
-
'',
|
|
53
|
-
'All keys in the pool are in cooldown or missing. Check OpenCode provider status and rotate keys.',
|
|
54
|
-
].join('\n'),
|
|
55
|
-
labels: ['prod-incident'],
|
|
56
|
-
};
|
|
57
|
-
const ctrl = new AbortController();
|
|
58
|
-
const timer = setTimeout(() => ctrl.abort(), INCIDENT_TIMEOUT_MS);
|
|
59
|
-
try {
|
|
60
|
-
const res = await this._fetch(url, {
|
|
61
|
-
method: 'POST',
|
|
62
|
-
headers: { authorization: `token ${this._token}`, 'content-type': 'application/json' },
|
|
63
|
-
body: JSON.stringify(body),
|
|
64
|
-
signal: ctrl.signal,
|
|
65
|
-
});
|
|
66
|
-
if (!res.ok) return { reported: false, status: res.status };
|
|
67
|
-
const data = await res.json();
|
|
68
|
-
this.markReported(provider, now);
|
|
69
|
-
return { reported: true, number: data.number, url: data.html_url };
|
|
70
|
-
} catch (_) {
|
|
71
|
-
return { reported: false };
|
|
72
|
-
} finally {
|
|
73
|
-
clearTimeout(timer);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
// lib/incident.js — auto-create Gitea issue when pool exhausted > threshold.
|
|
2
|
+
// ponytail: minimal — caller provides a token + base URL. No retries on rate-limit.
|
|
3
|
+
|
|
4
|
+
export const INCIDENT_DEFAULT_THRESHOLD_MS = 5 * 60 * 1000; // 5 min
|
|
5
|
+
export const INCIDENT_DEFAULT_COOLDOWN_MS = 30 * 60 * 1000; // 30 min between incidents per provider
|
|
6
|
+
export const INCIDENT_TIMEOUT_MS = 5000;
|
|
7
|
+
|
|
8
|
+
export class IncidentReporter {
|
|
9
|
+
constructor({ token, baseUrl, repo, thresholdMs = INCIDENT_DEFAULT_THRESHOLD_MS, cooldownMs = INCIDENT_DEFAULT_COOLDOWN_MS, fetchImpl } = {}) {
|
|
10
|
+
if (!token) throw new Error('incident: token required');
|
|
11
|
+
if (!baseUrl) throw new Error('incident: baseUrl required');
|
|
12
|
+
if (!repo || !repo.includes('/')) throw new Error('incident: repo (owner/name) required');
|
|
13
|
+
this._token = token;
|
|
14
|
+
this._baseUrl = baseUrl.replace(/\/+$/, '');
|
|
15
|
+
this._repo = repo;
|
|
16
|
+
this._thresholdMs = thresholdMs;
|
|
17
|
+
this._cooldownMs = cooldownMs;
|
|
18
|
+
this._lastIncidentAt = new Map(); // provider -> epochMs
|
|
19
|
+
this._fetch = fetchImpl || (typeof fetch !== 'undefined' ? fetch : () => { throw new Error('incident: no fetch available'); });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Should we report now? Pure; does not perform I/O.
|
|
23
|
+
shouldReport(provider, exhaustedSince, now = Date.now()) {
|
|
24
|
+
if (!provider) return false;
|
|
25
|
+
if (!Number.isFinite(exhaustedSince)) return false;
|
|
26
|
+
if (now - exhaustedSince < this._thresholdMs) return false;
|
|
27
|
+
const last = this._lastIncidentAt.get(provider);
|
|
28
|
+
if (Number.isFinite(last) && now - last < this._cooldownMs) return false;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
markReported(provider, at = Date.now()) {
|
|
33
|
+
this._lastIncidentAt.set(provider, at);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
resetCooldown(provider) {
|
|
37
|
+
if (provider) this._lastIncidentAt.delete(provider);
|
|
38
|
+
else this._lastIncidentAt.clear();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Open a Gitea issue. ponytail: minimal payload, ignore failures.
|
|
42
|
+
async open(provider, exhaustedSince, now = Date.now()) {
|
|
43
|
+
if (!this.shouldReport(provider, exhaustedSince, now)) return { reported: false };
|
|
44
|
+
const url = `${this._baseUrl}/api/v1/repos/${this._repo}/issues`;
|
|
45
|
+
const body = {
|
|
46
|
+
title: `prod-incident: pool ${provider} exhausted since ${new Date(exhaustedSince).toISOString()}`,
|
|
47
|
+
body: [
|
|
48
|
+
'Auto-generated by `dsh-key-rotation`.',
|
|
49
|
+
'',
|
|
50
|
+
`- provider: \`${provider}\``,
|
|
51
|
+
`- exhaustedSince: \`${new Date(exhaustedSince).toISOString()}\``,
|
|
52
|
+
'',
|
|
53
|
+
'All keys in the pool are in cooldown or missing. Check OpenCode provider status and rotate keys.',
|
|
54
|
+
].join('\n'),
|
|
55
|
+
labels: ['prod-incident'],
|
|
56
|
+
};
|
|
57
|
+
const ctrl = new AbortController();
|
|
58
|
+
const timer = setTimeout(() => ctrl.abort(), INCIDENT_TIMEOUT_MS);
|
|
59
|
+
try {
|
|
60
|
+
const res = await this._fetch(url, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: { authorization: `token ${this._token}`, 'content-type': 'application/json' },
|
|
63
|
+
body: JSON.stringify(body),
|
|
64
|
+
signal: ctrl.signal,
|
|
65
|
+
});
|
|
66
|
+
if (!res.ok) return { reported: false, status: res.status };
|
|
67
|
+
const data = await res.json();
|
|
68
|
+
this.markReported(provider, now);
|
|
69
|
+
return { reported: true, number: data.number, url: data.html_url };
|
|
70
|
+
} catch (_) {
|
|
71
|
+
return { reported: false };
|
|
72
|
+
} finally {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|