@oisin-ee/pi-guardrails 0.10.2 → 0.11.1
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 +8 -0
- package/dist/shell-parser.js +65 -2
- package/package.json +1 -1
- package/src/pi/index.ts +21 -7
- package/src/shell-parser.ts +74 -2
package/README.md
CHANGED
|
@@ -13,6 +13,14 @@ Agent guardrails for [pi](https://pi.dev), Claude Code, and Codex. One set of po
|
|
|
13
13
|
In pi, it also fills a missing shell timeout (120 s) and blocks one above 10 minutes, naming each tool's unit.
|
|
14
14
|
Shell text is parsed with `shfmt`, which must be on `PATH`. A command that cannot be parsed is blocked.
|
|
15
15
|
|
|
16
|
+
## Planning models
|
|
17
|
+
|
|
18
|
+
The guardrails do not restrict a model by its identity. The planning selections `tier/plan-claude` and
|
|
19
|
+
`tier/plan-gpt` keep the full policy set: a planning model may run Git commands, write and edit files,
|
|
20
|
+
and use the workflow tools.
|
|
21
|
+
|
|
22
|
+
Planning restrictions belong to Plannotator. It owns plan mode, and it blocks writes while it plans.
|
|
23
|
+
|
|
16
24
|
## pi
|
|
17
25
|
|
|
18
26
|
```bash
|
package/dist/shell-parser.js
CHANGED
|
@@ -95,7 +95,7 @@ pipelineStages = (stmt, source) => {
|
|
|
95
95
|
}
|
|
96
96
|
};
|
|
97
97
|
visit(tree);
|
|
98
|
-
return { calls, guards };
|
|
98
|
+
return { tree, calls, guards };
|
|
99
99
|
}, parsedOf = (command) => {
|
|
100
100
|
if (cache.command !== command || cache.parsed === undefined) {
|
|
101
101
|
cache.parsed = parse(command);
|
|
@@ -117,4 +117,67 @@ shellCalls = (command) => parsedOf(command).calls.map((words) => [...words]),
|
|
|
117
117
|
* @returns {(string[][] | null)[]} One guard per call.
|
|
118
118
|
*/
|
|
119
119
|
shellCallGuards = (command) => parsedOf(command).guards.map((stages) => stages?.map((words) => [...words]) ?? null);
|
|
120
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Whether a literal contains a glob character that the shell would expand. An
|
|
122
|
+
* escaped character is literal, so it does not expand.
|
|
123
|
+
* @param {string} value The raw literal text.
|
|
124
|
+
* @returns {boolean} True for an active glob character.
|
|
125
|
+
*/
|
|
126
|
+
function hasActiveGlob(value) {
|
|
127
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
128
|
+
const character = value[index] ?? "";
|
|
129
|
+
if (!"*?[]{}".includes(character)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
let backslashes = 0;
|
|
133
|
+
for (let before = index - 1; before >= 0 && value[before] === "\\"; before -= 1) {
|
|
134
|
+
backslashes += 1;
|
|
135
|
+
}
|
|
136
|
+
if (backslashes % 2 === 0) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns literal calls only when the complete AST has read-only shell syntax.
|
|
144
|
+
* @param {string} command Shell text.
|
|
145
|
+
* @returns {string[][] | null} Literal argv, or null for unsupported syntax.
|
|
146
|
+
*/
|
|
147
|
+
function literalShellCalls(command) {
|
|
148
|
+
const parsed = parsedOf(command), allowed = new Set([
|
|
149
|
+
"File",
|
|
150
|
+
"Stmt",
|
|
151
|
+
"CallExpr",
|
|
152
|
+
"Word",
|
|
153
|
+
"Lit",
|
|
154
|
+
"SglQuoted",
|
|
155
|
+
"DblQuoted",
|
|
156
|
+
"BinaryCmd",
|
|
157
|
+
]), safe = (node, quoted = false) => {
|
|
158
|
+
if (Array.isArray(node)) {
|
|
159
|
+
return node.every((item) => safe(item, quoted));
|
|
160
|
+
}
|
|
161
|
+
if (!isNode(node)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (node.Type !== undefined && !allowed.has(node.Type)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
if (node.Dollar === true ||
|
|
168
|
+
node.Background === true ||
|
|
169
|
+
(node.Redirs ?? []).length > 0 ||
|
|
170
|
+
(node.Assigns ?? []).length > 0 ||
|
|
171
|
+
(node.Type === "CallExpr" && (node.Args ?? []).length === 0) ||
|
|
172
|
+
(node.Type === "BinaryCmd" && !["&&", "||", "|"].includes(node.Op ?? "")) ||
|
|
173
|
+
(node.Type === "Lit" && !quoted && hasActiveGlob(node.Value ?? "")) ||
|
|
174
|
+
(node.Type === "Lit" && !quoted && (node.Value ?? "").startsWith("~"))) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return Object.values(node).every((value) => safe(value, quoted || node.Type === "DblQuoted"));
|
|
178
|
+
};
|
|
179
|
+
return safe(parsed.tree) && parsed.calls.length > 0
|
|
180
|
+
? parsed.calls.map((words) => [...words])
|
|
181
|
+
: null;
|
|
182
|
+
}
|
|
183
|
+
export { literalShellCalls, shellCallGuards, shellCalls };
|
package/package.json
CHANGED
package/src/pi/index.ts
CHANGED
|
@@ -49,15 +49,29 @@ const MAX_NESTING = 8,
|
|
|
49
49
|
),
|
|
50
50
|
editViolation = (call: ToolCall, cwd: string): string | null =>
|
|
51
51
|
classifyEdits(editInputsFromPayload({ tool_input: call.input, tool_name: call.toolName }), cwd),
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
|
+
* The nested calls of a codemode input.
|
|
54
|
+
* @param {Readonly<Record<string, unknown>>} input The tool input.
|
|
55
|
+
* @returns {readonly ToolCall[]} The calls of the envelope.
|
|
56
|
+
*/
|
|
57
|
+
nestedCalls = (input: Readonly<Record<string, unknown>>): readonly ToolCall[] => {
|
|
53
58
|
const uses = input["tool_uses"];
|
|
54
59
|
if (!Array.isArray(uses)) {
|
|
55
60
|
return [];
|
|
56
61
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
const calls: ToolCall[] = [];
|
|
63
|
+
for (const use of uses) {
|
|
64
|
+
if (!isRecord(use)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const toolName = use["recipient_name"];
|
|
68
|
+
const parameters = use["parameters"];
|
|
69
|
+
calls.push({
|
|
70
|
+
input: isRecord(parameters) ? parameters : {},
|
|
71
|
+
toolName: typeof toolName === "string" ? toolName : "",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return calls;
|
|
61
75
|
},
|
|
62
76
|
toolViolation = (event: ToolCall, cwd: string, depth = 0): string | null => {
|
|
63
77
|
if (depth > MAX_NESTING) {
|
|
@@ -72,8 +86,8 @@ const MAX_NESTING = 8,
|
|
|
72
86
|
if (own !== null) {
|
|
73
87
|
return own;
|
|
74
88
|
}
|
|
75
|
-
for (const
|
|
76
|
-
const message = toolViolation(
|
|
89
|
+
for (const child of nestedCalls(call.input)) {
|
|
90
|
+
const message = toolViolation(child, cwd, depth + 1);
|
|
77
91
|
if (message !== null) {
|
|
78
92
|
return message;
|
|
79
93
|
}
|
package/src/shell-parser.ts
CHANGED
|
@@ -32,6 +32,7 @@ interface ShNode {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
interface Parsed {
|
|
35
|
+
readonly tree: unknown;
|
|
35
36
|
readonly calls: readonly Words[];
|
|
36
37
|
readonly guards: readonly Guard[];
|
|
37
38
|
}
|
|
@@ -145,7 +146,7 @@ const MAX_BUFFER = 16 * 1024 * 1024,
|
|
|
145
146
|
}
|
|
146
147
|
};
|
|
147
148
|
visit(tree);
|
|
148
|
-
return { calls, guards };
|
|
149
|
+
return { tree, calls, guards };
|
|
149
150
|
},
|
|
150
151
|
parsedOf = (command: string): Parsed => {
|
|
151
152
|
if (cache.command !== command || cache.parsed === undefined) {
|
|
@@ -170,4 +171,75 @@ const MAX_BUFFER = 16 * 1024 * 1024,
|
|
|
170
171
|
shellCallGuards = (command: string): (string[][] | null)[] =>
|
|
171
172
|
parsedOf(command).guards.map((stages) => stages?.map((words) => [...words]) ?? null);
|
|
172
173
|
|
|
173
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Whether a literal contains a glob character that the shell would expand. An
|
|
176
|
+
* escaped character is literal, so it does not expand.
|
|
177
|
+
* @param {string} value The raw literal text.
|
|
178
|
+
* @returns {boolean} True for an active glob character.
|
|
179
|
+
*/
|
|
180
|
+
function hasActiveGlob(value: string): boolean {
|
|
181
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
182
|
+
const character = value[index] ?? "";
|
|
183
|
+
if (!"*?[]{}".includes(character)) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
let backslashes = 0;
|
|
187
|
+
for (let before = index - 1; before >= 0 && value[before] === "\\"; before -= 1) {
|
|
188
|
+
backslashes += 1;
|
|
189
|
+
}
|
|
190
|
+
if (backslashes % 2 === 0) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Returns literal calls only when the complete AST has read-only shell syntax.
|
|
199
|
+
* @param {string} command Shell text.
|
|
200
|
+
* @returns {string[][] | null} Literal argv, or null for unsupported syntax.
|
|
201
|
+
*/
|
|
202
|
+
function literalShellCalls(command: string): string[][] | null {
|
|
203
|
+
const parsed = parsedOf(command),
|
|
204
|
+
allowed = new Set([
|
|
205
|
+
"File",
|
|
206
|
+
"Stmt",
|
|
207
|
+
"CallExpr",
|
|
208
|
+
"Word",
|
|
209
|
+
"Lit",
|
|
210
|
+
"SglQuoted",
|
|
211
|
+
"DblQuoted",
|
|
212
|
+
"BinaryCmd",
|
|
213
|
+
]),
|
|
214
|
+
safe = (node: unknown, quoted = false): boolean => {
|
|
215
|
+
if (Array.isArray(node)) {
|
|
216
|
+
return node.every((item: unknown) => safe(item, quoted));
|
|
217
|
+
}
|
|
218
|
+
if (!isNode(node)) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
if (node.Type !== undefined && !allowed.has(node.Type)) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
node.Dollar === true ||
|
|
226
|
+
node.Background === true ||
|
|
227
|
+
(node.Redirs ?? []).length > 0 ||
|
|
228
|
+
(node.Assigns ?? []).length > 0 ||
|
|
229
|
+
(node.Type === "CallExpr" && (node.Args ?? []).length === 0) ||
|
|
230
|
+
(node.Type === "BinaryCmd" && !["&&", "||", "|"].includes(node.Op ?? "")) ||
|
|
231
|
+
(node.Type === "Lit" && !quoted && hasActiveGlob(node.Value ?? "")) ||
|
|
232
|
+
(node.Type === "Lit" && !quoted && (node.Value ?? "").startsWith("~"))
|
|
233
|
+
) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
return Object.values(node).every((value: unknown) =>
|
|
237
|
+
safe(value, quoted || node.Type === "DblQuoted"),
|
|
238
|
+
);
|
|
239
|
+
};
|
|
240
|
+
return safe(parsed.tree) && parsed.calls.length > 0
|
|
241
|
+
? parsed.calls.map((words) => [...words])
|
|
242
|
+
: null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { literalShellCalls, shellCallGuards, shellCalls };
|