@aliou/pi-guardrails 0.11.2 → 0.12.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.
Files changed (94) hide show
  1. package/README.md +72 -167
  2. package/extensions/guardrails/commands/examples/index.ts +520 -0
  3. package/extensions/guardrails/commands/onboarding/config.ts +54 -0
  4. package/{src/commands/onboarding-command.ts → extensions/guardrails/commands/onboarding/index.ts} +5 -31
  5. package/extensions/guardrails/commands/settings/add-rule-wizard.ts +267 -0
  6. package/extensions/guardrails/commands/settings/examples.ts +399 -0
  7. package/extensions/guardrails/commands/settings/index.ts +596 -0
  8. package/extensions/guardrails/commands/settings/path-list-editor.ts +158 -0
  9. package/extensions/guardrails/commands/settings/scope-picker-submenu.ts +69 -0
  10. package/extensions/guardrails/commands/settings/utils.ts +108 -0
  11. package/extensions/guardrails/components/onboarding-choice-step.ts +140 -0
  12. package/extensions/guardrails/components/onboarding-finish-step.ts +50 -0
  13. package/extensions/guardrails/components/onboarding-intro-step.ts +30 -0
  14. package/extensions/guardrails/components/onboarding-types.ts +10 -0
  15. package/extensions/guardrails/components/onboarding-wizard.ts +116 -0
  16. package/{src → extensions/guardrails}/components/pattern-editor.ts +11 -10
  17. package/extensions/guardrails/index.ts +106 -0
  18. package/extensions/guardrails/rules.test.ts +107 -0
  19. package/extensions/guardrails/rules.ts +119 -0
  20. package/extensions/guardrails/targets.test.ts +44 -0
  21. package/extensions/guardrails/targets.ts +66 -0
  22. package/extensions/path-access/grants.test.ts +47 -0
  23. package/extensions/path-access/grants.ts +68 -0
  24. package/extensions/path-access/index.ts +143 -0
  25. package/extensions/path-access/prompt.ts +196 -0
  26. package/extensions/path-access/rules.test.ts +46 -0
  27. package/extensions/path-access/rules.ts +37 -0
  28. package/extensions/path-access/targets.test.ts +40 -0
  29. package/extensions/path-access/targets.ts +19 -0
  30. package/extensions/permission-gate/grants.ts +21 -0
  31. package/extensions/permission-gate/index.ts +122 -0
  32. package/extensions/permission-gate/prompt.ts +222 -0
  33. package/extensions/permission-gate/rules.test.ts +132 -0
  34. package/extensions/permission-gate/rules.ts +72 -0
  35. package/package.json +18 -20
  36. package/schema.json +286 -0
  37. package/src/core/check.test.ts +169 -0
  38. package/src/core/check.ts +38 -0
  39. package/src/{hooks/permission-gate/dangerous-commands.test.ts → core/commands/dangerous.test.ts} +134 -2
  40. package/src/{hooks/permission-gate/dangerous-commands.ts → core/commands/dangerous.ts} +119 -1
  41. package/src/core/commands/index.ts +15 -0
  42. package/src/core/index.ts +13 -0
  43. package/src/{utils/path-access.test.ts → core/paths/access.test.ts} +1 -5
  44. package/src/core/paths/index.ts +14 -0
  45. package/src/{utils → core/shell}/command-args.test.ts +31 -20
  46. package/src/core/shell/index.ts +2 -0
  47. package/src/core/types.ts +55 -0
  48. package/src/shared/config/defaults.ts +118 -0
  49. package/src/shared/config/index.ts +17 -0
  50. package/src/shared/config/loader.ts +64 -0
  51. package/src/shared/config/migration/001-v0-format-upgrade.ts +107 -0
  52. package/src/shared/config/migration/002-strip-toolchain-fields.ts +39 -0
  53. package/src/shared/config/migration/003-strip-command-explainer-fields.ts +42 -0
  54. package/src/shared/config/migration/004-env-files-to-policies.ts +87 -0
  55. package/src/shared/config/migration/005-normalize-allowed-paths.ts +43 -0
  56. package/src/shared/config/migration/006-apply-builtin-defaults.ts +19 -0
  57. package/src/shared/config/migration/007-mark-onboarding-done.ts +25 -0
  58. package/src/shared/config/migration/index.ts +44 -0
  59. package/src/shared/config/migration/version.ts +7 -0
  60. package/src/shared/config/types.ts +141 -0
  61. package/src/shared/events.ts +100 -0
  62. package/src/shared/index.ts +6 -0
  63. package/src/shared/matching.test.ts +86 -0
  64. package/src/{utils → shared}/matching.ts +4 -4
  65. package/src/{utils → shared/paths}/bash-paths.test.ts +11 -2
  66. package/src/{utils → shared/paths}/bash-paths.ts +4 -4
  67. package/src/shared/paths/index.ts +1 -0
  68. package/src/shared/warnings.ts +17 -0
  69. package/docs/defaults.md +0 -140
  70. package/docs/examples.md +0 -170
  71. package/src/commands/onboarding.ts +0 -390
  72. package/src/commands/settings-command.ts +0 -1616
  73. package/src/config.ts +0 -392
  74. package/src/hooks/index.ts +0 -11
  75. package/src/hooks/path-access.ts +0 -395
  76. package/src/hooks/permission-gate/index.test.ts +0 -332
  77. package/src/hooks/permission-gate/index.ts +0 -595
  78. package/src/hooks/policies.ts +0 -322
  79. package/src/index.ts +0 -96
  80. package/src/lib/executor.ts +0 -280
  81. package/src/lib/index.ts +0 -16
  82. package/src/lib/model-resolver.ts +0 -47
  83. package/src/lib/timing.ts +0 -42
  84. package/src/lib/types.ts +0 -115
  85. package/src/utils/events.ts +0 -32
  86. package/src/utils/migration.test.ts +0 -58
  87. package/src/utils/migration.ts +0 -340
  88. package/src/utils/warnings.ts +0 -7
  89. /package/src/{utils/path-access.ts → core/paths/access.ts} +0 -0
  90. /package/src/{utils → core/paths}/path.test.ts +0 -0
  91. /package/src/{utils → core/paths}/path.ts +0 -0
  92. /package/src/{utils/shell-utils.ts → core/shell/ast.ts} +0 -0
  93. /package/src/{utils → core/shell}/command-args.ts +0 -0
  94. /package/src/{utils/glob-expander.ts → shared/glob.ts} +0 -0
package/README.md CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  # Guardrails
4
4
 
5
- Security hooks for Pi to reduce accidental destructive actions and secret-file access.
5
+ Guardrails adds safety checks to Pi so agents are less likely to read secrets, write protected files, access paths outside the workspace, or run dangerous shell commands by accident.
6
6
 
7
- ## Demo
7
+ This package installs three Pi extensions:
8
8
 
9
- <video src="https://assets.aliou.me/pi-extensions/demos/pi-guardrails.mp4" controls playsinline muted></video>
9
+ - **guardrails** for file protection policies, settings, onboarding, and examples.
10
+ - **path-access** for controlling access outside the current workspace.
11
+ - **permission-gate** for confirming or blocking risky shell commands.
10
12
 
11
13
  ## Install
12
14
 
@@ -14,185 +16,112 @@ Security hooks for Pi to reduce accidental destructive actions and secret-file a
14
16
  pi install npm:@aliou/pi-guardrails
15
17
  ```
16
18
 
17
- Or from git:
19
+ ## First run
18
20
 
19
- ```bash
20
- pi install git:github.com/aliou/pi-guardrails
21
+ After installing, run the onboarding command to choose a starting setup:
22
+
23
+ ```text
24
+ /guardrails:onboarding
21
25
  ```
22
26
 
23
- ## Documentation
27
+ ![Guardrails onboarding walkthrough](https://assets.aliou.me/pi-extensions/demos/guardrails/v0.12.0/onboarding.gif)
24
28
 
25
- - [Default configuration](docs/defaults.md) built-in policy rules and permission gate patterns
26
- - [Example presets](docs/examples.md) — pre-configured presets available in settings
29
+ You can change everything later with:
27
30
 
28
- ## What it does
31
+ ```text
32
+ /guardrails:settings
33
+ ```
29
34
 
30
- - **policies**: named file-protection rules with per-rule protection levels.
31
- - **permission-gate**: detects dangerous bash commands and asks for confirmation.
32
- - **path-access**: restricts tool access to the current working directory with allow/ask/block modes.
33
- - **optional command explainer**: can call a small LLM to explain a dangerous command inline in the confirmation dialog.
35
+ ## Included extensions
34
36
 
35
- ## Config locations
37
+ ### guardrails
36
38
 
37
- Guardrails reads and merges config from:
39
+ The `guardrails` extension owns file protection policies and the user-facing commands.
38
40
 
39
- - Global: `~/.pi/agent/extensions/guardrails.json`
40
- - Project: `.pi/extensions/guardrails.json`
41
- - Memory (session): internal temporary scope used by settings/commands
42
-
43
- Priority: `memory > local > global > defaults`.
44
-
45
- Use `/guardrails:settings` to edit config interactively.
46
-
47
- ## Current schema
48
-
49
- ```json
50
- {
51
- "enabled": true,
52
- "features": {
53
- "policies": true,
54
- "permissionGate": true,
55
- "pathAccess": false
56
- },
57
- "pathAccess": {
58
- "mode": "ask",
59
- "allowedPaths": []
60
- },
61
- "policies": {
62
- "rules": [
63
- {
64
- "id": "secret-files",
65
- "description": "Files containing secrets",
66
- "patterns": [
67
- { "pattern": ".env" },
68
- { "pattern": ".env.local" },
69
- { "pattern": ".env.production" },
70
- { "pattern": ".env.prod" },
71
- { "pattern": ".dev.vars" }
72
- ],
73
- "allowedPatterns": [
74
- { "pattern": ".env.example" },
75
- { "pattern": ".env.sample" },
76
- { "pattern": ".env.test" },
77
- { "pattern": "*.example.env" },
78
- { "pattern": "*.sample.env" },
79
- { "pattern": "*.test.env" }
80
- ],
81
- "protection": "noAccess",
82
- "onlyIfExists": true
83
- }
84
- ]
85
- },
86
- "permissionGate": {
87
- "patterns": [
88
- { "pattern": "rm -rf", "description": "recursive force delete" },
89
- { "pattern": "sudo", "description": "superuser command" }
90
- ],
91
- "customPatterns": [],
92
- "requireConfirmation": true,
93
- "allowedPatterns": [],
94
- "autoDenyPatterns": [],
95
- "explainCommands": false,
96
- "explainModel": null,
97
- "explainTimeout": 5000
98
- }
99
- }
100
- ```
41
+ Use it to protect files like `.env`, private keys, local credentials, generated logs, database dumps, or any project-specific path you do not want Pi to read or modify without clear intent.
42
+
43
+ ![Guardrails policies and settings walkthrough](https://assets.aliou.me/pi-extensions/demos/guardrails/v0.12.0/policies.gif)
101
44
 
102
- All fields optional. Missing fields use defaults.
45
+ Useful commands:
103
46
 
104
- ## Policies
47
+ ```text
48
+ /guardrails:settings
49
+ /guardrails:onboarding
50
+ /guardrails:examples
51
+ ```
105
52
 
106
- Each rule has:
53
+ ### path-access
107
54
 
108
- - `id`: stable identifier used for overrides across scopes.
109
- - `patterns`: files to match (glob by default, regex if `regex: true`). Glob semantics: patterns containing `/` match the full relative path; patterns without `/` match basename only.
110
- - `allowedPatterns`: exceptions.
111
- - `protection`:
112
- - `noAccess`: block `read`, `write`, `edit`, `bash`, `grep`, `find`, `ls`
113
- - `readOnly`: block `write`, `edit`, `bash`
114
- - `none`: explicit no protection
115
- - `onlyIfExists` (default true)
116
- - `blockMessage` with `{file}` placeholder
117
- - `enabled` (default true)
55
+ The `path-access` extension checks tool calls that target paths outside the current working directory.
118
56
 
119
- When multiple rules match the same file, strongest protection wins:
120
- `noAccess > readOnly > none`.
57
+ It can allow, block, or ask before Pi accesses files elsewhere on your machine. In ask mode, you can allow one file or a directory once, for the session, or always.
121
58
 
122
- ### Add rule with AI
59
+ ![Guardrails path access prompt walkthrough](https://assets.aliou.me/pi-extensions/demos/guardrails/v0.12.0/path-access.gif)
123
60
 
124
- Use:
61
+ ### permission-gate
125
62
 
126
- ```text
127
- /guardrails:add-policy
128
- ```
63
+ The `permission-gate` extension detects dangerous bash commands before they run.
129
64
 
130
- This starts a subagent that helps build and save one policy rule.
65
+ It catches built-in risky patterns like recursive deletes, privileged commands, disk formatting, broad permission changes, and configured custom patterns. You can allow once, allow for the session, deny, or configure auto-deny rules.
131
66
 
132
- ## Path access
67
+ ![Guardrails permission gate walkthrough](https://assets.aliou.me/pi-extensions/demos/guardrails/v0.12.0/permission-gate.gif)
133
68
 
134
- Restrict tool access to the current working directory. When enabled, any tool call targeting a path outside `cwd` is checked against the configured mode:
69
+ ## Configuration
135
70
 
136
- - **allow**: no restrictions
137
- - **ask**: prompt with options to grant access (file or directory, for session or always)
138
- - **block**: deny all outside access
71
+ Most configuration should happen through the interactive settings UI:
139
72
 
140
- ```jsonc
141
- {
142
- "features": { "pathAccess": true },
143
- "pathAccess": {
144
- "mode": "ask",
145
- "allowedPaths": ["~/code/shared-libs/", "~/.config/myapp"]
146
- }
147
- }
73
+ ```text
74
+ /guardrails:settings
148
75
  ```
149
76
 
150
- Grants are stored in project config (always) or session memory (session). The `allowedPaths` array is merged across all config scopes.
77
+ Advanced users can edit the settings file directly:
151
78
 
152
- Limitations:
153
- - Symlinks are not resolved (lexical path comparison only).
154
- - Bash path extraction is best-effort (AST-based heuristics).
155
- - In non-interactive mode, `ask` mode degrades to `block`.
79
+ - Global: `~/.pi/agent/extensions/guardrails.json`
80
+ - Project: `.pi/extensions/guardrails.json`
156
81
 
157
- ## Permission gate
82
+ Guardrails writes a `$schema` field to saved settings files, so modern editors provide autocomplete and validation. The generated schema is committed at [`schema.json`](schema.json).
158
83
 
159
- Detects dangerous bash commands and prompts user confirmation.
84
+ ## Examples
160
85
 
161
- Built-in dangerous patterns are matched structurally (AST-based) for better accuracy:
86
+ Use the examples command to add common policy and command presets without replacing your existing config:
162
87
 
163
- - `rm -rf`
164
- - `sudo`
165
- - `dd if=`
166
- - `mkfs.`
167
- - `chmod -R 777`
168
- - `chown -R`
88
+ ```text
89
+ /guardrails:examples
90
+ ```
169
91
 
170
- You can also add custom dangerous patterns.
92
+ ![Guardrails examples command walkthrough](https://assets.aliou.me/pi-extensions/demos/guardrails/v0.12.0/examples.gif)
171
93
 
172
- ### Explain commands (opt-in)
94
+ The available presets live in [`extensions/guardrails/commands/settings/examples.ts`](extensions/guardrails/commands/settings/examples.ts).
173
95
 
174
- If enabled, guardrails calls an LLM before showing the confirmation dialog and displays a short explanation.
96
+ ## Similar but different
175
97
 
176
- Config fields:
98
+ Pi is designed to make agent safety extensible. Guardrails focuses on deterministic, configurable file policies, outside-workspace path access, and dangerous-command prompts. Other packages tend to fall into two useful groups.
177
99
 
178
- - `permissionGate.explainCommands` (boolean)
179
- - `permissionGate.explainModel` (`provider/model-id`)
180
- - `permissionGate.explainTimeout` (ms)
100
+ ### Make one yourself!
181
101
 
182
- Failures/timeouts degrade gracefully: dialog still shows without explanation.
102
+ If Guardrails or the alternatives below do not fit your needs, you can also make your own. Start from the [Pi permission gate example](https://github.com/earendil-works/pi/blob/main/packages/coding-agent/examples/extensions/permission-gate.ts), then ask Pi to customize it for your workflow.
183
103
 
184
- ## Migration notes
104
+ ### Permission and policy gates
185
105
 
186
- Legacy fields are auto-migrated:
106
+ These packages add checks around tool calls before they run. They are closest to Guardrails when you want policy enforcement without changing where Pi executes.
187
107
 
188
- - `features.protectEnvFiles` -> `features.policies`
189
- - `envFiles` -> `policies.rules` (migrated into `secret-files`)
108
+ - [@gotgenes/pi-permission-system](https://pi.dev/packages/%40gotgenes/pi-permission-system): broad permission enforcement for Pi tool calls.
109
+ - [@vtstech/pi-security](https://pi.dev/packages/%40vtstech/pi-security): command, path, network, mode, and audit controls.
110
+ - [pi-control](https://github.com/mcowger/pi-control/blob/main/README.md): location-scoped, action-based policies for tool calls, with allow, log, ask, and deny outcomes before execution.
111
+ - [@casualjim/pi-heimdall](https://pi.dev/packages/%40casualjim/pi-heimdall): secret exposure guards, command policies, protected `.env` files, and a sandbox guard.
112
+ - [pi-file-permissions](https://pi.dev/packages/pi-file-permissions): file-level permissions for read, write, edit, find, grep, and ls tools.
113
+ - [pi-secret-guard](https://pi.dev/packages/pi-secret-guard): focused protection against committing or pushing secrets to git.
190
114
 
191
- `config.version` is a schema marker, not npm package version.
115
+ ### Sandboxes and containment
192
116
 
193
- Also note:
117
+ These packages reduce blast radius by running Pi, subagents, or tool calls inside a constrained environment. They can be a better fit when you want isolation first and prompts second.
194
118
 
195
- - `preventBrew`, `preventPython`, `enforcePackageManager`, `packageManager` were removed from guardrails and moved to `@aliou/pi-toolchain`.
119
+ - [Pi + Gondolin sandbox example](https://github.com/earendil-works/gondolin/blob/main/host/examples/pi-gondolin.ts): upstream example that runs Pi tools inside a Gondolin micro-VM.
120
+ - [pi-sandbox](https://pi.dev/packages/pi-sandbox): OS-level sandboxing for bash, with allow/deny checks and prompts for file tools.
121
+ - [pi-container-sandbox](https://pi.dev/packages/pi-container-sandbox): runs read, write, edit, bash, and user bash operations inside a Docker or Apple container session.
122
+ - [@alexanderfortin/pi-freestyle-sandbox](https://pi.dev/packages/%40alexanderfortin/pi-freestyle-sandbox): runs sandboxed subagents in Freestyle cloud VMs.
123
+ - [@the-agency/vmpi](https://pi.dev/packages/%40the-agency/vmpi): runs Pi inside a QEMU microVM with limited filesystem and network access.
124
+ - [pi-claude-sandbox](https://pi.dev/packages/pi-claude-sandbox): Claude-style OS sandboxing with interactive permission prompts.
196
125
 
197
126
  ## Development
198
127
 
@@ -202,30 +131,6 @@ pnpm test:watch # Run tests in watch mode
202
131
  pnpm typecheck # Type check
203
132
  pnpm lint # Lint
204
133
  pnpm format # Format
134
+ pnpm gen:schema # Regenerate schema.json
135
+ pnpm check:schema # Verify schema.json is current
205
136
  ```
206
-
207
- ## Events
208
-
209
- Guardrails emits events for other extensions:
210
-
211
- ### `guardrails:blocked`
212
-
213
- ```ts
214
- interface GuardrailsBlockedEvent {
215
- feature: "policies" | "permissionGate" | "pathAccess";
216
- toolName: string;
217
- input: Record<string, unknown>;
218
- reason: string;
219
- userDenied?: boolean;
220
- }
221
- ```
222
-
223
- ### `guardrails:dangerous`
224
-
225
- ```ts
226
- interface GuardrailsDangerousEvent {
227
- command: string;
228
- description: string;
229
- pattern: string;
230
- }
231
- ```