@goodandready/dsh-key-rotation 0.7.26 → 0.7.27
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 +140 -118
- package/cordis.patch.yml +6 -6
- package/lib/agent-budget.js +68 -68
- package/lib/bucket.js +41 -0
- package/lib/canary.js +56 -56
- package/lib/cascade.js +39 -39
- package/lib/client-helpers.js +21 -21
- package/lib/client.js +52 -1
- 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 +1536 -1402
- package/lib/keycheck.js +31 -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/webhook.js +133 -64
- package/package.json +58 -58
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
|
+
}
|