Personal Telegram developer console for GitHub
feat(hardening): add fail-closed kill switch core
davoodmehraban89 committed
Oct 5, 2026 at 13:21 UTC
07e9b0946a5aeacc3694125d7cbb32a5cdab9f43
1 file changed
+44
src/core/kill-switch.js
new
+44
@@ -0,0 +1,44 @@
1
+export class KillSwitch {
2
+ constructor({ store } = {}) {
3
+ if (!store || typeof store.get !== 'function' || typeof store.set !== 'function') throw new Error('Kill switch store is required');
4
+ this.store = store;
5
+ }
6
+
7
+ async status() {
8
+ const state = await this.store.get();
9
+ if (!state || !['NORMAL', 'LOCKED'].includes(state.mode)) throw new Error('Kill switch state is unavailable');
10
+ return state;
11
+ }
12
+
13
+ async blocks(risk) {
14
+ if (!['WRITE', 'DESTRUCTIVE'].includes(String(risk || ''))) return false;
15
+ try {
16
+ return (await this.status()).mode !== 'NORMAL';
17
+ } catch {
18
+ return true;
19
+ }
20
+ }
21
+
22
+ async automationBlocked() {
23
+ try {
24
+ return (await this.status()).mode !== 'NORMAL';
25
+ } catch {
26
+ return true;
27
+ }
28
+ }
29
+
30
+ async lock(context = {}) {
31
+ this.#assertControl(context);
32
+ return this.store.set({ mode: 'LOCKED', changedAt: Date.now() });
33
+ }
34
+
35
+ async unlock(context = {}) {
36
+ this.#assertControl(context);
37
+ return this.store.set({ mode: 'NORMAL', changedAt: Date.now() });
38
+ }
39
+
40
+ #assertControl(context) {
41
+ if (context.isOwner !== true) throw new Error('Kill switch control requires owner');
42
+ if (context.approved !== true) throw new Error('Kill switch control requires approval');
43
+ }
44
+}