@intutic/gate 2.4.0 → 2.5.0

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/README.md CHANGED
@@ -42,6 +42,18 @@ implementation runs — this package's throw-based refusal contract, the same on
42
42
  `packages/mcp-proxy/src/policy.ts` use elsewhere in this repo's gate
43
43
  vocabulary.
44
44
 
45
+ ## Fail mode
46
+
47
+ `GateClient` fails closed by default (`failClosed: true`). When the control
48
+ plane's hook gate cannot be reached, answers non-2xx, or answers
49
+ `GOVERNANCE_UNAVAILABLE` because a check it runs (the SOP register, the SSO
50
+ group policy, the MCP server registry, image provenance, promoted findings)
51
+ could not complete, there is no verdict, and the call is refused with
52
+ `HOOK_GATE`. Pass `failClosed: false` to let calls through during such an
53
+ outage. A request the hook gate cannot read (HTTP 400, `INVALID_REQUEST`) is
54
+ refused whatever `failClosed` says. intutic-clawde's `GateClient` behaves the
55
+ same way, held to it by `packages/shared-types/fixtures/hook-gate-response-vectors.json`.
56
+
45
57
  ## Public API surface
46
58
 
47
59
  | Export | From | What it is |
@@ -59,6 +71,7 @@ vocabulary.
59
71
  | `McpRegistryRecord`, `McpAllowlistRecord` | `mcpRegistryRecord.ts` | Tier M: the snapshot's MCP server registry and allowlist (`Snapshot.mcpRegistry`, `Snapshot.mcpAllowlist`), decided as the hook gates and the MCP proxy decide them; a byte-identical copy of `@intutic/shared-types`' module. |
60
72
  | `classify`, `isDeploy`, `isTest`, `touchesInfra` | `actions.ts` | Command classifier shared by A2's deploy trigger. |
61
73
  | `canonicalJson`, `holdKey`, `holdMessage` | `hold.ts` | The hold helpers: key-sorted JSON, the tool name and argument hash an approved hold is matched on, and the message a held call carries. |
74
+ | (internal) | `chaosGate.ts` | A chaos run's plan, counters and faults, which `Gate.guard` applies when `INTUTIC_CHAOS_PLAN` is set (`CHAOS_FAULT`, or `IntuticGateHold` for a held call); a byte-identical copy of `@intutic/shared-types`' module, as the hook gates emit it. |
62
75
  | `COMMAND_SIZE_LIMIT`, `ARGUMENTS_SIZE_LIMIT`, `GATE_DEADLINE_MS` | `limits.ts` | A command over 256 KiB or arguments over 1 MiB are refused with `COMMAND_TOO_LARGE` before any tier runs. `GATE_DEADLINE_MS` (9000) is how long the hook script `intuticSandboxBootstrap()` writes may evaluate, from process start, before it refuses with `GATE_DEADLINE`: one second under that hook entry's 10-second timeout, so a slow rule is refused rather than left to the harness. |
63
76
 
64
77
  The snapshot must carry the `#digest <hash>` line the sync daemon writes (the
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Fault injection in the gates: the part of a chaos run (`chaos.ts`) a hook
3
+ * gate or an in-process gate SDK carries.
4
+ *
5
+ * A hook gate is a new process per tool call, and parallel gates share one
6
+ * run, so nothing here keeps state in memory: the plan comes from {@link CHAOS_PLAN_ENV}-style text each call,
7
+ * and every count lives in files beside the events file.
8
+ *
9
+ * - `<events>.gate-plan-loaded` — created once, with an exclusive create; the
10
+ * gate that creates it reports `plan_loaded`, so separate gate processes do
11
+ * not repeat it.
12
+ * - `<events>.gate-counters/<fault index>.seen.<n>` and `.hit.<n>` — one
13
+ * empty file per matching call a fault saw and per call it hit. A call
14
+ * claims the next number with an exclusive create (`open` with `wx`,
15
+ * `O_CREAT | O_EXCL`), which the file system grants to exactly one process,
16
+ * so two gates deciding at once can never both take the same number; the
17
+ * claimed number is the call's 1-based index. A count is the number of
18
+ * files, read once to start the search, so a claim costs one directory
19
+ * read and usually one create. That is `FaultCounter`'s `after_calls` and
20
+ * `times` across processes.
21
+ * - `<events>.gate-counters/<fault index>.held.<key>.<g>` — a call a
22
+ * `permission.deny_or_delay` fault in `delay` or `hold_then_deny` mode
23
+ * held; its modification time is when. `<key>` is the call's digest (the
24
+ * tool and its input, as the gate keys an approval hold), `<g>` counts the
25
+ * holds of that call, claimed like a call number. `.released.<key>.<g>`,
26
+ * created with the same exclusive create, ends that hold: one retry, and
27
+ * only one, is the one the hold lets through or denies.
28
+ *
29
+ * A permission fault never makes a gate wait. It holds the call the way an
30
+ * approval hold does: the call is refused as held, and the identical retry
31
+ * (same tool, same input) is refused as held again until `delay_s` seconds
32
+ * have passed since the first refusal. After that the retry is allowed
33
+ * (`delay`) or denied (`hold_then_deny`), and the hold is over: the next
34
+ * identical call is a new matching call. A held call's retries are the same
35
+ * call, so no fault counts them as matching calls. A test moves the clock by
36
+ * moving the `held` files' modification times back.
37
+ *
38
+ * Only a call governance allowed counts: a fault never applies to a call the
39
+ * gate refused, held or asked about, so such a call is not one of the
40
+ * fault's matching calls either. Every fault whose `match` covers an allowed
41
+ * call counts it as seen; the first one in plan order that is due injects,
42
+ * and the faults after it do not spend a hit on the same call. A `tool.error`
43
+ * on an `mcp__<server>__<tool>` call is the MCP proxy's to inject, so a gate
44
+ * leaves it alone; `permission.deny_or_delay` is the gate's on any tool.
45
+ *
46
+ * The functions are self-contained on purpose — no imports, no module state —
47
+ * because the JavaScript hook gates emit them as source
48
+ * ({@link CHAOS_GATE_JS_SOURCE}) and run them outside this package, and
49
+ * `@intutic/gate` carries a byte-identical copy of this file
50
+ * (`packages/gate-js/src/chaosGate.ts`). The bash gates and
51
+ * `intutic_clawde.gate` run the Python transliteration,
52
+ * `intutic_clawde/gate/chaos.py`, which the sync daemon emits verbatim. All
53
+ * of them run `fixtures/chaos-gate-vectors.json`. File access goes through
54
+ * the `fs` argument (Node's `fs` in a gate).
55
+ *
56
+ * @module
57
+ */
58
+ /** One fault a gate injects, with its defaults applied. */
59
+ export interface ChaosGateFault {
60
+ /** The fault's position in the plan: its counters are keyed on it. */
61
+ index: number;
62
+ id: 'tool.error' | 'permission.deny_or_delay';
63
+ match: {
64
+ tool?: string;
65
+ host?: string;
66
+ model?: string;
67
+ } | null;
68
+ afterCalls: number;
69
+ times: number | null;
70
+ /** `tool.error` only. */
71
+ message: string;
72
+ /** `permission.deny_or_delay` only. */
73
+ mode: 'deny' | 'delay' | 'hold_then_deny';
74
+ /** `permission.deny_or_delay` only: how long a held call stays held, in seconds. */
75
+ delayS: number;
76
+ }
77
+ /** The fields of a plan a gate uses. */
78
+ export interface ChaosGatePlan {
79
+ runId: string;
80
+ canary: string;
81
+ /** The faults a gate injects; the plan's others are another component's. */
82
+ faults: ChaosGateFault[];
83
+ }
84
+ /** One call, as the gate decided it before any fault. */
85
+ export interface ChaosGateCall {
86
+ tool: string;
87
+ decision: 'allow' | 'deny' | 'ask' | 'hold';
88
+ ruleId?: string | null;
89
+ destructive: boolean;
90
+ policyHit: boolean;
91
+ /**
92
+ * The call's digest in hex: its tool and input, as the gate keys an
93
+ * approval hold, so an identical retry has the same key. Only a held
94
+ * call's retry reads it.
95
+ */
96
+ key?: string;
97
+ }
98
+ /** A fault to apply to an allowed call. */
99
+ export interface ChaosGateFaultOutcome {
100
+ faultId: string;
101
+ /** `deny` refuses the call; `hold` refuses it as held, as an approval hold does. */
102
+ kind: 'deny' | 'hold';
103
+ /** The refusal text, carrying the plan's canary. */
104
+ reason: string;
105
+ }
106
+ /** The file operations the functions use: Node's `fs` provides them. */
107
+ export interface ChaosGateFs {
108
+ appendFileSync(path: string, data: string): void;
109
+ mkdirSync(path: string): unknown;
110
+ readdirSync(path: string): string[];
111
+ openSync(path: string, flags: string): number;
112
+ closeSync(fd: number): void;
113
+ statSync(path: string): {
114
+ mtimeMs: number;
115
+ };
116
+ }
117
+ /**
118
+ * Decodes and checks a plan: base64url JSON, version 1, with valid values in
119
+ * every field a gate reads. Returns the plan, or why it is not one.
120
+ */
121
+ export declare function chaosGateDecode(raw: string): {
122
+ plan: ChaosGatePlan;
123
+ error: null;
124
+ } | {
125
+ plan: null;
126
+ error: string;
127
+ };
128
+ /**
129
+ * Whether a fault's `match` covers a gate call. A gate call has a tool and
130
+ * nothing else, so a fault that names a host or a model never matches one.
131
+ * `tool` matches exactly, or by suffix when it starts with `*`.
132
+ */
133
+ export declare function chaosGateMatches(match: ChaosGateFault['match'], tool: string): boolean;
134
+ /** One events-file line: `ts`, `runId`, `component`, `type`, then the fields that are set. */
135
+ export declare function chaosGateEvent(plan: ChaosGatePlan, type: string, fields: Record<string, unknown>, now: string): string;
136
+ /** Appends whole lines in one write. An I/O error is reported, never thrown: it must not decide the call. */
137
+ export declare function chaosGateAppend(fs: ChaosGateFs, eventsPath: string, lines: string): void;
138
+ /**
139
+ * Claims the next numbered file `<dir>/<prefix><n>` with an exclusive create
140
+ * and returns n, or 0 when every number up to `limit` is taken. -1 on an I/O
141
+ * error, after saying so.
142
+ */
143
+ export declare function chaosGateClaim(fs: ChaosGateFs, dir: string, prefix: string, limit: number | null): number;
144
+ /**
145
+ * The open hold of one fault on one call: the number of the newest
146
+ * `<fault>.held.<key>.<g>` file that has no `.released.` twin, and when it
147
+ * was created. Null when there is none or it cannot be read.
148
+ */
149
+ export declare function chaosGateOpenHold(fs: ChaosGateFs, dir: string, fault: number, key: string): {
150
+ g: number;
151
+ sinceMs: number;
152
+ } | null;
153
+ /** Ends hold g with an exclusive create: true for the one caller that does. */
154
+ export declare function chaosGateRelease(fs: ChaosGateFs, dir: string, fault: number, key: string, g: number): boolean;
155
+ /**
156
+ * Reports one call: `plan_loaded` the first time any gate reports to this
157
+ * events file, then `gate_decision`; for an allowed call, the fault that is
158
+ * due (`fault_injected`, once per held call however often it is retried),
159
+ * and `tool_call`: ok when the call goes ahead, not ok when it is held.
160
+ * Returns the fault the gate must apply, or null. `now` is the ISO-8601 time,
161
+ * and the clock a hold is timed by.
162
+ */
163
+ export declare function chaosGateCall(fs: ChaosGateFs, plan: ChaosGatePlan, eventsPath: string, call: ChaosGateCall, now: string): ChaosGateFaultOutcome | null;
164
+ /** Every function above as JavaScript source, for a hook gate to emit. */
165
+ export declare const CHAOS_GATE_JS_SOURCE: string;
166
+ //# sourceMappingURL=chaosGate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chaosGate.d.ts","sourceRoot":"","sources":["../src/chaosGate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwD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AAW,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAiBjI;AAED,+EAA+E;AAC/E,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAQ7G;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,EAAE,EAAE,WAAW,EACf,IAAI,EAAE,aAAa,EACnB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,aAAa,EACnB,GAAG,EAAE,MAAM,GACV,qBAAqB,GAAG,IAAI,CAyE9B;AAED,0EAA0E;AAC1E,eAAO,MAAM,oBAAoB,QAWpB,CAAA"}
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Fault injection in the gates: the part of a chaos run (`chaos.ts`) a hook
3
+ * gate or an in-process gate SDK carries.
4
+ *
5
+ * A hook gate is a new process per tool call, and parallel gates share one
6
+ * run, so nothing here keeps state in memory: the plan comes from {@link CHAOS_PLAN_ENV}-style text each call,
7
+ * and every count lives in files beside the events file.
8
+ *
9
+ * - `<events>.gate-plan-loaded` — created once, with an exclusive create; the
10
+ * gate that creates it reports `plan_loaded`, so separate gate processes do
11
+ * not repeat it.
12
+ * - `<events>.gate-counters/<fault index>.seen.<n>` and `.hit.<n>` — one
13
+ * empty file per matching call a fault saw and per call it hit. A call
14
+ * claims the next number with an exclusive create (`open` with `wx`,
15
+ * `O_CREAT | O_EXCL`), which the file system grants to exactly one process,
16
+ * so two gates deciding at once can never both take the same number; the
17
+ * claimed number is the call's 1-based index. A count is the number of
18
+ * files, read once to start the search, so a claim costs one directory
19
+ * read and usually one create. That is `FaultCounter`'s `after_calls` and
20
+ * `times` across processes.
21
+ * - `<events>.gate-counters/<fault index>.held.<key>.<g>` — a call a
22
+ * `permission.deny_or_delay` fault in `delay` or `hold_then_deny` mode
23
+ * held; its modification time is when. `<key>` is the call's digest (the
24
+ * tool and its input, as the gate keys an approval hold), `<g>` counts the
25
+ * holds of that call, claimed like a call number. `.released.<key>.<g>`,
26
+ * created with the same exclusive create, ends that hold: one retry, and
27
+ * only one, is the one the hold lets through or denies.
28
+ *
29
+ * A permission fault never makes a gate wait. It holds the call the way an
30
+ * approval hold does: the call is refused as held, and the identical retry
31
+ * (same tool, same input) is refused as held again until `delay_s` seconds
32
+ * have passed since the first refusal. After that the retry is allowed
33
+ * (`delay`) or denied (`hold_then_deny`), and the hold is over: the next
34
+ * identical call is a new matching call. A held call's retries are the same
35
+ * call, so no fault counts them as matching calls. A test moves the clock by
36
+ * moving the `held` files' modification times back.
37
+ *
38
+ * Only a call governance allowed counts: a fault never applies to a call the
39
+ * gate refused, held or asked about, so such a call is not one of the
40
+ * fault's matching calls either. Every fault whose `match` covers an allowed
41
+ * call counts it as seen; the first one in plan order that is due injects,
42
+ * and the faults after it do not spend a hit on the same call. A `tool.error`
43
+ * on an `mcp__<server>__<tool>` call is the MCP proxy's to inject, so a gate
44
+ * leaves it alone; `permission.deny_or_delay` is the gate's on any tool.
45
+ *
46
+ * The functions are self-contained on purpose — no imports, no module state —
47
+ * because the JavaScript hook gates emit them as source
48
+ * ({@link CHAOS_GATE_JS_SOURCE}) and run them outside this package, and
49
+ * `@intutic/gate` carries a byte-identical copy of this file
50
+ * (`packages/gate-js/src/chaosGate.ts`). The bash gates and
51
+ * `intutic_clawde.gate` run the Python transliteration,
52
+ * `intutic_clawde/gate/chaos.py`, which the sync daemon emits verbatim. All
53
+ * of them run `fixtures/chaos-gate-vectors.json`. File access goes through
54
+ * the `fs` argument (Node's `fs` in a gate).
55
+ *
56
+ * @module
57
+ */
58
+ /**
59
+ * Decodes and checks a plan: base64url JSON, version 1, with valid values in
60
+ * every field a gate reads. Returns the plan, or why it is not one.
61
+ */
62
+ export function chaosGateDecode(raw) {
63
+ const ids = [
64
+ 'llm.rate_limit', 'llm.upstream_5xx', 'llm.context_trim', 'llm.model_degrade', 'tool.error', 'tool.stale_data',
65
+ 'mcp.toolset_change', 'permission.deny_or_delay', 'inject.tool_result', 'egress.dns_fail', 'egress.credential_401',
66
+ ];
67
+ const invalid = (why) => ({ plan: null, error: 'INTUTIC_CHAOS_PLAN is not a valid plan: ' + why });
68
+ const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
69
+ const isInt = (v, min, max) => typeof v === 'number' && Number.isInteger(v) && v >= min && v <= max;
70
+ let json;
71
+ try {
72
+ if (typeof raw !== 'string' || !/^[A-Za-z0-9_-]+={0,2}$/.test(raw))
73
+ throw new Error('alphabet');
74
+ const b64 = raw.replace(/=+$/, '').replace(/-/g, '+').replace(/_/g, '/');
75
+ const binary = atob(b64 + '='.repeat((4 - (b64.length % 4)) % 4));
76
+ const bytes = new Uint8Array(binary.length);
77
+ for (let i = 0; i < binary.length; i++)
78
+ bytes[i] = binary.charCodeAt(i);
79
+ json = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
80
+ }
81
+ catch {
82
+ return { plan: null, error: 'INTUTIC_CHAOS_PLAN is not base64url-encoded JSON' };
83
+ }
84
+ if (!isObject(json))
85
+ return invalid('not a JSON object');
86
+ if (json.v !== 1)
87
+ return invalid('version ' + JSON.stringify(json.v) + ', and a gate reads only version 1');
88
+ if (typeof json.runId !== 'string' || !/^[A-Za-z0-9_-]{6,64}$/.test(json.runId))
89
+ return invalid('runId');
90
+ if (typeof json.canary !== 'string' || !/^E-INTUTIC-[0-9A-F]{4,16}$/.test(json.canary))
91
+ return invalid('canary');
92
+ if (!Array.isArray(json.faults) || json.faults.length > 50)
93
+ return invalid('faults');
94
+ const faults = [];
95
+ for (let i = 0; i < json.faults.length; i++) {
96
+ const f = json.faults[i];
97
+ const at = 'faults[' + i + ']';
98
+ if (!isObject(f) || typeof f.id !== 'string' || ids.indexOf(f.id) === -1)
99
+ return invalid(at + '.id');
100
+ if (f.id !== 'tool.error' && f.id !== 'permission.deny_or_delay')
101
+ continue;
102
+ let match = null;
103
+ if (f.match !== undefined) {
104
+ if (!isObject(f.match))
105
+ return invalid(at + '.match');
106
+ for (const k of Object.keys(f.match)) {
107
+ const v = f.match[k];
108
+ if ((k !== 'tool' && k !== 'host' && k !== 'model') || typeof v !== 'string' || v.length === 0) {
109
+ return invalid(at + '.match.' + k);
110
+ }
111
+ }
112
+ match = f.match;
113
+ }
114
+ if (f.after_calls !== undefined && !isInt(f.after_calls, 0, 10000))
115
+ return invalid(at + '.after_calls');
116
+ if (f.times !== undefined && !isInt(f.times, 1, 10000))
117
+ return invalid(at + '.times');
118
+ const fault = {
119
+ index: i,
120
+ id: f.id,
121
+ match: match,
122
+ afterCalls: f.after_calls === undefined ? 0 : f.after_calls,
123
+ times: f.times === undefined ? null : f.times,
124
+ message: 'tool failed',
125
+ mode: 'deny',
126
+ delayS: 0,
127
+ };
128
+ if (f.id === 'tool.error' && f.message !== undefined) {
129
+ if (typeof f.message !== 'string' || f.message.length < 1 || f.message.length > 2000)
130
+ return invalid(at + '.message');
131
+ fault.message = f.message;
132
+ }
133
+ if (f.id === 'permission.deny_or_delay') {
134
+ if (f.mode !== undefined) {
135
+ if (f.mode !== 'deny' && f.mode !== 'delay' && f.mode !== 'hold_then_deny')
136
+ return invalid(at + '.mode');
137
+ fault.mode = f.mode;
138
+ }
139
+ if (f.delay_s !== undefined) {
140
+ if (!isInt(f.delay_s, 0, 600))
141
+ return invalid(at + '.delay_s');
142
+ fault.delayS = f.delay_s;
143
+ }
144
+ }
145
+ faults.push(fault);
146
+ }
147
+ return { plan: { runId: json.runId, canary: json.canary, faults: faults }, error: null };
148
+ }
149
+ /**
150
+ * Whether a fault's `match` covers a gate call. A gate call has a tool and
151
+ * nothing else, so a fault that names a host or a model never matches one.
152
+ * `tool` matches exactly, or by suffix when it starts with `*`.
153
+ */
154
+ export function chaosGateMatches(match, tool) {
155
+ if (!match)
156
+ return true;
157
+ if (match.host !== undefined || match.model !== undefined)
158
+ return false;
159
+ const p = match.tool;
160
+ if (p === undefined)
161
+ return true;
162
+ return p.charAt(0) === '*' ? tool.endsWith(p.slice(1)) : tool === p;
163
+ }
164
+ /** One events-file line: `ts`, `runId`, `component`, `type`, then the fields that are set. */
165
+ export function chaosGateEvent(plan, type, fields, now) {
166
+ const event = { ts: now, runId: plan.runId, component: 'gate', type: type };
167
+ for (const k of Object.keys(fields))
168
+ if (fields[k] !== undefined && fields[k] !== null)
169
+ event[k] = fields[k];
170
+ return JSON.stringify(event) + '\n';
171
+ }
172
+ /** Appends whole lines in one write. An I/O error is reported, never thrown: it must not decide the call. */
173
+ export function chaosGateAppend(fs, eventsPath, lines) {
174
+ try {
175
+ fs.appendFileSync(eventsPath, lines);
176
+ }
177
+ catch (e) {
178
+ try {
179
+ console.error('[Intutic Governance] chaos: could not append to ' + eventsPath + ': ' + String((e && e.message) || e));
180
+ }
181
+ catch {
182
+ /* stderr gone too */
183
+ }
184
+ }
185
+ }
186
+ /**
187
+ * Claims the next numbered file `<dir>/<prefix><n>` with an exclusive create
188
+ * and returns n, or 0 when every number up to `limit` is taken. -1 on an I/O
189
+ * error, after saying so.
190
+ */
191
+ export function chaosGateClaim(fs, dir, prefix, limit) {
192
+ try {
193
+ // Beside the events file, never creating its directory: the runner did.
194
+ try {
195
+ fs.mkdirSync(dir);
196
+ }
197
+ catch (e) {
198
+ if (!e || e.code !== 'EEXIST')
199
+ throw e;
200
+ }
201
+ const names = fs.readdirSync(dir);
202
+ let n = 0;
203
+ for (let i = 0; i < names.length; i++)
204
+ if (names[i].indexOf(prefix) === 0)
205
+ n++;
206
+ for (n = n + 1; limit === null || n <= limit; n++) {
207
+ try {
208
+ fs.closeSync(fs.openSync(dir + '/' + prefix + n, 'wx'));
209
+ return n;
210
+ }
211
+ catch (e) {
212
+ if (!e || e.code !== 'EEXIST')
213
+ throw e;
214
+ }
215
+ }
216
+ return 0;
217
+ }
218
+ catch (e) {
219
+ try {
220
+ console.error('[Intutic Governance] chaos: could not count calls in ' + dir + ': ' + String((e && e.message) || e));
221
+ }
222
+ catch {
223
+ /* stderr gone too */
224
+ }
225
+ return -1;
226
+ }
227
+ }
228
+ /**
229
+ * The open hold of one fault on one call: the number of the newest
230
+ * `<fault>.held.<key>.<g>` file that has no `.released.` twin, and when it
231
+ * was created. Null when there is none or it cannot be read.
232
+ */
233
+ export function chaosGateOpenHold(fs, dir, fault, key) {
234
+ try {
235
+ const held = fault + '.held.' + key + '.';
236
+ const names = fs.readdirSync(dir);
237
+ let g = 0;
238
+ for (let i = 0; i < names.length; i++) {
239
+ if (names[i].indexOf(held) !== 0)
240
+ continue;
241
+ const n = Number(names[i].slice(held.length));
242
+ if (n > g)
243
+ g = n;
244
+ }
245
+ if (g === 0 || names.indexOf(fault + '.released.' + key + '.' + g) !== -1)
246
+ return null;
247
+ // In whole milliseconds, as now is: a hold is never younger than the call that reads it.
248
+ return { g: g, sinceMs: Math.floor(fs.statSync(dir + '/' + held + g).mtimeMs) };
249
+ }
250
+ catch {
251
+ // No counters yet, or none readable: no hold to continue.
252
+ return null;
253
+ }
254
+ }
255
+ /** Ends hold g with an exclusive create: true for the one caller that does. */
256
+ export function chaosGateRelease(fs, dir, fault, key, g) {
257
+ try {
258
+ fs.closeSync(fs.openSync(dir + '/' + fault + '.released.' + key + '.' + g, 'wx'));
259
+ return true;
260
+ }
261
+ catch {
262
+ // Another gate released it first, or it cannot be written.
263
+ return false;
264
+ }
265
+ }
266
+ /**
267
+ * Reports one call: `plan_loaded` the first time any gate reports to this
268
+ * events file, then `gate_decision`; for an allowed call, the fault that is
269
+ * due (`fault_injected`, once per held call however often it is retried),
270
+ * and `tool_call`: ok when the call goes ahead, not ok when it is held.
271
+ * Returns the fault the gate must apply, or null. `now` is the ISO-8601 time,
272
+ * and the clock a hold is timed by.
273
+ */
274
+ export function chaosGateCall(fs, plan, eventsPath, call, now) {
275
+ let lines = '';
276
+ let first = true;
277
+ try {
278
+ fs.closeSync(fs.openSync(eventsPath + '.gate-plan-loaded', 'wx'));
279
+ }
280
+ catch (e) {
281
+ // Already reported. Any other error: report again rather than not at all.
282
+ if (e && e.code === 'EEXIST')
283
+ first = false;
284
+ }
285
+ if (first)
286
+ lines += chaosGateEvent(plan, 'plan_loaded', { faults: plan.faults.length }, now);
287
+ lines += chaosGateEvent(plan, 'gate_decision', {
288
+ tool: call.tool,
289
+ decision: call.decision,
290
+ ruleId: call.ruleId || null,
291
+ destructive: !!call.destructive,
292
+ policyHit: !!call.policyHit,
293
+ }, now);
294
+ let fault = null;
295
+ if (call.decision === 'allow') {
296
+ const dir = eventsPath + '.gate-counters';
297
+ const key = String(call.key || '').replace(/[^0-9A-Za-z]/g, '').slice(0, 128) || 'call';
298
+ let nowMs = Date.parse(now);
299
+ if (isNaN(nowMs))
300
+ nowMs = Date.now();
301
+ const tag = ' [' + plan.canary + ']';
302
+ const held = 'Permission held for approval; retrying this exact call passes once it is approved' + tag;
303
+ // The retry of a held call: still held until the delay has passed, then
304
+ // let through or denied by the one retry that releases it.
305
+ let retry = false;
306
+ for (let i = 0; i < plan.faults.length && !retry; i++) {
307
+ const f = plan.faults[i];
308
+ if (f.id !== 'permission.deny_or_delay' || f.mode === 'deny' || !chaosGateMatches(f.match, call.tool))
309
+ continue;
310
+ const open = chaosGateOpenHold(fs, dir, f.index, key);
311
+ if (!open)
312
+ continue;
313
+ retry = true;
314
+ if (f.delayS > 0 && nowMs - open.sinceMs < f.delayS * 1000) {
315
+ fault = { faultId: f.id, kind: 'hold', reason: held };
316
+ }
317
+ else if (chaosGateRelease(fs, dir, f.index, key, open.g)) {
318
+ if (f.mode === 'hold_then_deny') {
319
+ fault = { faultId: f.id, kind: 'deny', reason: 'Permission held for approval, then denied by approver' + tag };
320
+ }
321
+ }
322
+ else {
323
+ // Another gate released it first: this call is a new one.
324
+ retry = false;
325
+ }
326
+ }
327
+ for (let i = 0; i < plan.faults.length && !retry; i++) {
328
+ const f = plan.faults[i];
329
+ if (!chaosGateMatches(f.match, call.tool))
330
+ continue;
331
+ // An MCP tool's error is the MCP proxy's to inject; a gate injects the built-in side.
332
+ if (f.id === 'tool.error' && call.tool.indexOf('mcp__') === 0)
333
+ continue;
334
+ const seen = chaosGateClaim(fs, dir, f.index + '.seen.', null);
335
+ if (fault || seen < 1 || seen <= f.afterCalls)
336
+ continue;
337
+ if (f.times !== null && chaosGateClaim(fs, dir, f.index + '.hit.', f.times) < 1)
338
+ continue;
339
+ if (f.id === 'tool.error') {
340
+ fault = { faultId: f.id, kind: 'deny', reason: f.message + tag };
341
+ }
342
+ else if (f.mode === 'deny') {
343
+ fault = { faultId: f.id, kind: 'deny', reason: 'Permission denied by approver' + tag };
344
+ }
345
+ else {
346
+ // Held from now: the hold's file is what its retries find.
347
+ if (chaosGateClaim(fs, dir, f.index + '.held.' + key + '.', null) < 1)
348
+ continue;
349
+ fault = { faultId: f.id, kind: 'hold', reason: held };
350
+ }
351
+ lines += chaosGateEvent(plan, 'fault_injected', { faultId: f.id, call: seen, target: call.tool }, now);
352
+ }
353
+ if (!fault || fault.kind === 'hold')
354
+ lines += chaosGateEvent(plan, 'tool_call', { tool: call.tool, ok: !fault }, now);
355
+ }
356
+ chaosGateAppend(fs, eventsPath, lines);
357
+ return fault;
358
+ }
359
+ /** Every function above as JavaScript source, for a hook gate to emit. */
360
+ export const CHAOS_GATE_JS_SOURCE = [
361
+ chaosGateDecode,
362
+ chaosGateMatches,
363
+ chaosGateEvent,
364
+ chaosGateAppend,
365
+ chaosGateClaim,
366
+ chaosGateOpenHold,
367
+ chaosGateRelease,
368
+ chaosGateCall,
369
+ ]
370
+ .map((f) => f.toString())
371
+ .join('\n');
372
+ //# sourceMappingURL=chaosGate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chaosGate.js","sourceRoot":"","sources":["../src/chaosGate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;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