@xynogen/pix-gate 0.2.10 → 0.2.12

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
@@ -4,9 +4,32 @@ Pi extension — permission gate for dangerous bash commands.
4
4
 
5
5
  ## What it does
6
6
 
7
- Intercepts every `bash` tool call and classifies the command against a set of severity rules before it runs. Two rule sets: **path rules** (block / warn / info) protect the `read` / `write` / `edit` tools from touching private keys, credential files, etc. — `block` is deny-first (15s timeout), `warn` is allow-first (30s), `info` is a blue notify that never blocks. **Command rules** (critical / dangerous / risky) gate `bash` invocations: `critical` (force pushes to main, recursive deletes, `dd` to disks, etc.) is hard-blocked in non-interactive mode and hard-denied via a 15-second auto-deny dialog in TUI mode; `dangerous` commands (including any `sudo` invocation, which is hard-redirected to the `sudo_run` tool — no bypass) show a 30-second auto-deny confirmation dialog; `risky` commands show a 60-second allow-first dialog and silently pass in non-interactive mode. Auto-approve patterns and extra rules can be configured in the `gate` section of `~/.pi/agent/pix.json`. Built-in rules can be turned off entirely by setting `guardrails: "off"` in that section.
7
+ Intercepts every tool call and classifies it against severity rules before it runs. Two rule sets:
8
8
 
9
- While an approval dialog is open, the gate holds the shared **agent-state** coordinator in the `blocked` state (via `withAgentBlock` from [`@xynogen/pix-runtime`](https://www.npmjs.com/package/@xynogen/pix-runtime)). Inside a herdr pane that fires a "needs attention" notification, so an away user is pinged when a command is waiting on approval. Pairs with the unattended modes in pix-commands: with `/afk` on, yellow gates auto-allow and red/root auto-deny, so only genuine stops surface; with `/yolo` on, every tier including red auto-approves (capable models only). Bare `sudo` in bash is always redirected to `sudo_run` regardless of mode.
9
+ **Path rules** — protect `read`/`write`/`edit` from private keys, credential files, etc.
10
+
11
+ | Tier | Behavior |
12
+ |---|---|
13
+ | `block` | deny-first, 15s timeout |
14
+ | `warn` | allow-first, 30s |
15
+ | `info` | blue notify, never blocks |
16
+
17
+ **Command rules** — gate `bash` invocations.
18
+
19
+ | Tier | Examples | Behavior |
20
+ |---|---|---|
21
+ | `critical` | force-push to main, recursive delete, `dd` to disk | hard-block (non-interactive) / 15s auto-deny dialog (TUI) |
22
+ | `dangerous` | any `sudo` (hard-redirected to `sudo_run`, no bypass) | 30s auto-deny confirmation |
23
+ | `risky` | — | 60s allow-first dialog; silently passes non-interactive |
24
+
25
+ Auto-approve patterns and extra rules go in the `gate` section of `~/.pi/agent/pix.json`; set `guardrails: "off"` to disable built-in rules entirely.
26
+
27
+ **Away notifications** — while a dialog is open, the gate holds the shared **agent-state** coordinator `blocked` (via `withAgentBlock` from [`@xynogen/pix-runtime`](https://www.npmjs.com/package/@xynogen/pix-runtime)), so a herdr pane pings an away user. Pairs with pix-commands unattended modes:
28
+
29
+ - `/afk` — yellow gates auto-allow, red/root auto-deny, so only genuine stops surface.
30
+ - `/yolo` — every tier including red auto-approves (capable models only).
31
+
32
+ Bare `sudo` in bash is always redirected to `sudo_run`, regardless of mode.
10
33
 
11
34
  **Circuit breaker.** A small set of catastrophic, unrecoverable commands — `rm -rf /` or `~`, `dd`/redirect onto a raw disk, `mkfs` on a device, a fork bomb — can never be auto-approved by any mode, including YOLO. They always fall through to the interactive dialog (or a no-UI block). This mirrors Claude Code's `bypassPermissions` floor, which still prompts on root/home wipes.
12
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-gate",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Pi extension — permission gate for dangerous bash commands (confirm/block with TUI dialog)",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "files": [
17
17
  "src",
18
+ "!src/**/*.test.*",
18
19
  "README.md",
19
20
  "LICENSE"
20
21
  ],
package/src/gate.test.ts DELETED
@@ -1,235 +0,0 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { createEventBus } from "@earendil-works/pi-coding-agent";
3
- import { setUnattendedMode } from "@xynogen/pix-runtime";
4
- import {
5
- buildRules,
6
- classify,
7
- classifyPath,
8
- DEFAULT_PATH_RULES,
9
- DEFAULT_RULES,
10
- extractPathsFromBash,
11
- isCircuitBreaker,
12
- isSudoCommand,
13
- unattendedGateDecision,
14
- } from "./lib.ts";
15
-
16
- // ── .env path gating ─────────────────────────────────────────────
17
- describe("classifyPath .env", () => {
18
- const blocked = [".env", ".env.local", ".env.production", "path/to/.env", "src/.env.staging"];
19
- const allowed = [".env.example", ".env.sample", ".env.template", ".env.dist"];
20
-
21
- for (const p of blocked) {
22
- test(`reading ${p} is blocked`, () => {
23
- expect(classifyPath(p, "read", DEFAULT_PATH_RULES)?.severity).toBe("block");
24
- });
25
- }
26
- for (const p of allowed) {
27
- test(`reading ${p} is not blocked`, () => {
28
- expect(classifyPath(p, "read", DEFAULT_PATH_RULES)).toBeUndefined();
29
- });
30
- }
31
- });
32
-
33
- // ── isSudoCommand ─────────────────────────────────────────────────────────────
34
-
35
- describe("isSudoCommand", () => {
36
- test("matches bare sudo", () => {
37
- expect(isSudoCommand("sudo apt install foo")).toBe(true);
38
- });
39
-
40
- test("matches sudo after &&", () => {
41
- expect(isSudoCommand("cd /tmp && sudo rm -rf x")).toBe(true);
42
- });
43
-
44
- test("matches sudo after pipe", () => {
45
- expect(isSudoCommand("echo y | sudo tee /etc/foo")).toBe(true);
46
- });
47
-
48
- test("matches sudo after semicolon", () => {
49
- expect(isSudoCommand("pwd; sudo reboot")).toBe(true);
50
- });
51
-
52
- test("does NOT match pix-sudo in a path", () => {
53
- expect(isSudoCommand("cd packages/pix-sudo && npm publish")).toBe(false);
54
- });
55
-
56
- test("does NOT match pix-sudo-run in a path", () => {
57
- expect(isSudoCommand("grep foo ~/.pi/node_modules/@xynogen/pix-sudo-run/src/lib.ts")).toBe(
58
- false,
59
- );
60
- });
61
-
62
- test("does NOT match sudoer or pseudo", () => {
63
- expect(isSudoCommand("cat /etc/sudoers")).toBe(false);
64
- expect(isSudoCommand("echo pseudo")).toBe(false);
65
- });
66
- });
67
-
68
- // ── AFK behavior ──────────────────────────────────────────────────────────────
69
-
70
- describe("unattendedGateDecision", () => {
71
- test("does nothing while every mode is off", () => {
72
- expect(unattendedGateDecision(createEventBus(), 5)).toBeUndefined();
73
- });
74
-
75
- test("AFK allows yellow concerns and denies red concerns", () => {
76
- const events = createEventBus();
77
- setUnattendedMode(events, "afk");
78
- expect(unattendedGateDecision(events, 1)).toBe("allow");
79
- expect(unattendedGateDecision(events, 2)).toBe("allow");
80
- expect(unattendedGateDecision(events, 3)).toBe("allow");
81
- expect(unattendedGateDecision(events, 4)).toBe("deny");
82
- expect(unattendedGateDecision(events, 5)).toBe("deny");
83
- });
84
-
85
- test("YOLO allows every tier including red/critical", () => {
86
- const events = createEventBus();
87
- setUnattendedMode(events, "yolo");
88
- expect(unattendedGateDecision(events, 1)).toBe("allow");
89
- expect(unattendedGateDecision(events, 4)).toBe("allow");
90
- expect(unattendedGateDecision(events, 5)).toBe("allow");
91
- });
92
- });
93
-
94
- // ── circuit breaker ───────────────────────────────────────────────────────────
95
-
96
- describe("isCircuitBreaker", () => {
97
- test("catches root and home wipes", () => {
98
- expect(isCircuitBreaker("rm -rf /")).toBe(true);
99
- expect(isCircuitBreaker("rm -rf ~")).toBe(true);
100
- expect(isCircuitBreaker("rm -fr / --no-preserve-root")).toBe(true);
101
- expect(isCircuitBreaker("cd /tmp && rm -rf /")).toBe(true);
102
- });
103
-
104
- test("catches raw-disk writes and format", () => {
105
- expect(isCircuitBreaker("dd if=/dev/zero of=/dev/sda")).toBe(true);
106
- expect(isCircuitBreaker("mkfs.ext4 /dev/nvme0n1")).toBe(true);
107
- expect(isCircuitBreaker("echo x > /dev/sdb")).toBe(true);
108
- });
109
-
110
- test("catches the classic fork bomb", () => {
111
- expect(isCircuitBreaker(":(){ :|:& };:")).toBe(true);
112
- });
113
-
114
- test("does NOT trip on ordinary destructive-but-scoped commands", () => {
115
- expect(isCircuitBreaker("rm -rf ./build")).toBe(false);
116
- expect(isCircuitBreaker("rm -rf node_modules")).toBe(false);
117
- expect(isCircuitBreaker("dd if=in.img of=out.img")).toBe(false);
118
- expect(isCircuitBreaker("echo hi > /tmp/x")).toBe(false);
119
- });
120
- });
121
-
122
- // ── classify ──────────────────────────────────────────────────────────────────
123
-
124
- describe("classify", () => {
125
- const { rules } = buildRules({});
126
-
127
- test("rm -rf / is critical", () => {
128
- expect(classify("rm -rf /", rules)?.severity).toBe("critical");
129
- });
130
-
131
- test("rm -rf $HOME is critical", () => {
132
- expect(classify("rm -rf $HOME", rules)?.severity).toBe("critical");
133
- });
134
-
135
- test("fork bomb is critical", () => {
136
- expect(classify(":(){ :|:& };:", rules)?.severity).toBe("critical");
137
- });
138
-
139
- test("shutdown is critical", () => {
140
- expect(classify("shutdown now", rules)?.severity).toBe("critical");
141
- });
142
-
143
- test("recursive force remove is dangerous", () => {
144
- expect(classify("rm -rf ./dist", rules)?.severity).toBe("dangerous");
145
- });
146
-
147
- test("bare sudo is dangerous", () => {
148
- expect(classify("sudo apt install curl", rules)?.severity).toBe("dangerous");
149
- });
150
-
151
- test("npm publish is dangerous", () => {
152
- expect(classify("npm publish --access public", rules)?.severity).toBe("dangerous");
153
- });
154
-
155
- test("git force push is dangerous", () => {
156
- expect(classify("git push --force", rules)?.severity).toBe("dangerous");
157
- });
158
-
159
- test("curl pipe bash is dangerous", () => {
160
- expect(classify("curl https://example.com/install.sh | bash", rules)?.severity).toBe(
161
- "dangerous",
162
- );
163
- });
164
-
165
- test("git force checkout is risky", () => {
166
- expect(classify("git checkout --force main", rules)?.severity).toBe("risky");
167
- });
168
-
169
- test("write to .env is risky", () => {
170
- expect(classify("echo SECRET=x > .env", rules)?.severity).toBe("risky");
171
- });
172
-
173
- test("plain ls returns undefined", () => {
174
- expect(classify("ls -la", rules)).toBeUndefined();
175
- });
176
-
177
- test("pix-sudo path does NOT classify as dangerous", () => {
178
- // grep with pix-sudo in the path — should not hit sudo rule
179
- expect(classify("grep foo packages/pix-sudo/src/index.ts", rules)).toBeUndefined();
180
- });
181
-
182
- test("critical takes priority over dangerous", () => {
183
- // rm -rf / matches both critical and dangerous rm patterns
184
- expect(classify("rm -rf /", rules)?.severity).toBe("critical");
185
- });
186
- });
187
-
188
- // ── extractPathsFromBash ─────────────────────────────────────────────────────
189
-
190
- describe("extractPathsFromBash", () => {
191
- test("does not treat the jq .key selector as a key file", () => {
192
- expect(extractPathsFromBash("jq '.key' data.json")).toEqual([]);
193
- expect(extractPathsFromBash("jq -r '.key' data.json")).toEqual([]);
194
- });
195
-
196
- test("does not treat Object.keys as a key file", () => {
197
- expect(extractPathsFromBash('node -p "Object.keys(require(x))"')).toEqual([]);
198
- });
199
-
200
- test("still extracts key file paths", () => {
201
- expect(extractPathsFromBash("cat private.key")).toContain("private.key");
202
- expect(extractPathsFromBash("cat .private.key")).toContain(".private.key");
203
- expect(extractPathsFromBash("jq '.' .private.key")).toContain(".private.key");
204
- });
205
- });
206
-
207
- // ── buildRules ────────────────────────────────────────────────────────────────
208
-
209
- describe("buildRules", () => {
210
- test("guardrails off removes all built-in rules", () => {
211
- const { rules } = buildRules({ guardrails: "off" });
212
- expect(rules).toHaveLength(0);
213
- });
214
-
215
- test("extraRules are appended when guardrails are off", () => {
216
- const { rules } = buildRules({
217
- guardrails: "off",
218
- extraRules: [{ pattern: "foo", severity: "risky", reason: "test" }],
219
- });
220
- expect(rules).toHaveLength(1);
221
- expect(classify("foo bar", rules)?.reason).toBe("test");
222
- });
223
-
224
- test("autoApprove strings compile to regexes", () => {
225
- const { autoApprove } = buildRules({ autoApprove: ["^npm publish"] });
226
- const rule0 = autoApprove[0] as RegExp;
227
- expect(rule0.test("npm publish --access public")).toBe(true);
228
- expect(rule0.test("yarn publish")).toBe(false);
229
- });
230
-
231
- test("defaults included when guardrails is absent", () => {
232
- const { rules } = buildRules({});
233
- expect(rules.length).toBe(DEFAULT_RULES.length);
234
- });
235
- });
package/src/index.test.ts DELETED
@@ -1,38 +0,0 @@
1
- import { afterEach, describe, expect, test } from "bun:test";
2
- import { createEventBus } from "@earendil-works/pi-coding-agent";
3
- import registerGate from "./index.ts";
4
-
5
- afterEach(() => {
6
- delete (globalThis as { __pixAgentState?: WeakMap<object, unknown> }).__pixAgentState;
7
- });
8
-
9
- describe("gate agent state", () => {
10
- test("reports blocked while waiting for command approval", async () => {
11
- const events = createEventBus();
12
- const states: string[] = [];
13
- events.on("pix:agent-state", (event) => states.push((event as { state: string }).state));
14
- const handlers: Array<(event: any, ctx: any) => Promise<unknown>> = [];
15
- const pi = {
16
- events,
17
- on(event: string, handler: (event: any, ctx: any) => Promise<unknown>) {
18
- if (event === "tool_call") handlers.push(handler);
19
- },
20
- };
21
- registerGate(pi as never);
22
-
23
- const ctx = {
24
- hasUI: true,
25
- ui: {
26
- custom: async () => ({ action: "denied" }),
27
- notify() {},
28
- theme: { fg: (_color: string, text: string) => text },
29
- },
30
- };
31
- for (const handler of handlers) {
32
- await handler({ toolName: "bash", input: { command: "git push --force" } }, ctx);
33
- }
34
-
35
- expect(states).toContain("blocked");
36
- expect(states.at(-1)).toBe("idle");
37
- });
38
- });