@zeldrisho/pi-gate 0.7.1 → 0.7.2
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/CHANGELOG.md +9 -1
- package/README.md +1 -1
- package/package.json +4 -2
- package/src/approval-prompt.ts +140 -0
- package/src/index.ts +77 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.7.2] - 2026-10-25
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Show approval prompts in Pi’s normal dialog area, using the terminal background and Pi theme accents.
|
|
15
|
+
- Highlight matched command text and clarify agent guidance for timeouts, denials, and policy blocks.
|
|
16
|
+
|
|
10
17
|
## [0.7.1] - 2026-10-25
|
|
11
18
|
|
|
12
19
|
### Changed
|
|
@@ -105,7 +112,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
105
112
|
- support `prompt`, `block`, and `allow` actions, with longest-pattern-wins resolution
|
|
106
113
|
- block rather than auto-approve in non-UI modes
|
|
107
114
|
|
|
108
|
-
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.7.
|
|
115
|
+
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.7.2...HEAD
|
|
116
|
+
[0.7.2]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.7.1...pi-gate-v0.7.2
|
|
109
117
|
[0.7.1]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.7.0...pi-gate-v0.7.1
|
|
110
118
|
[0.7.0]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.6.1...pi-gate-v0.7.0
|
|
111
119
|
[0.6.1]: https://github.com/zeldrisho/pi-packages/compare/pi-gate-v0.6.0...pi-gate-v0.6.1
|
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ The built-in `bash` tool is gated, and `write`/`edit` calls targeting the gate c
|
|
|
48
48
|
|
|
49
49
|
Approval dialogs are serialized for concurrent tool calls; cancellation, shutdown, reload, session replacement, or a policy change while a dialog is open denies the pending call. Calls queued before shutdown or session replacement are also denied.
|
|
50
50
|
|
|
51
|
-
Long commands are truncated in approval dialogs, but an approved command executes in full.
|
|
51
|
+
The matched command text is highlighted in the TUI approval dialog. Long commands are truncated in approval dialogs, but an approved command executes in full. Denials, policy blocks, dismissals, and timeouts stop the turn; the agent is told to ask before retrying a denial or proceeding past a block, while a timeout asks whether to retry approval. Other allowed calls in a parallel batch may still run. In non-interactive modes (`-p`, JSON), commands requiring approval are denied.
|
|
52
52
|
|
|
53
53
|
In Herdr-managed panes, install Herdr's Pi integration with `herdr integration install pi` to report pending approvals as `blocked`. Herdr is optional.
|
|
54
54
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeldrisho/pi-gate",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Pi extension that gates bash commands and protects its configuration from tool edits",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"command-blocking",
|
|
@@ -33,13 +33,15 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@earendil-works/pi-coding-agent": "^1.4.0",
|
|
36
|
+
"@earendil-works/pi-tui": "^1.4.0",
|
|
36
37
|
"typebox": "^1.4.2",
|
|
37
38
|
"typescript": "^7.0.2",
|
|
38
39
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.3.0",
|
|
39
40
|
"vite-plus": "1.3.0"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
42
|
-
"@earendil-works/pi-coding-agent": "*"
|
|
43
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
44
|
+
"@earendil-works/pi-tui": "*"
|
|
43
45
|
},
|
|
44
46
|
"engines": {
|
|
45
47
|
"node": ">=24"
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
const ACTIONS = ["Allow", "Deny"] as const;
|
|
5
|
+
|
|
6
|
+
type Action = (typeof ACTIONS)[number];
|
|
7
|
+
|
|
8
|
+
type Foreground = "text" | "muted" | "accent" | "toolOutput" | "border";
|
|
9
|
+
|
|
10
|
+
export interface ApprovalPromptTheme {
|
|
11
|
+
style(text: string, options: { fg?: Foreground; bold?: boolean }): string;
|
|
12
|
+
bold(text: string): string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ApprovalPromptKeybindings {
|
|
16
|
+
matches(
|
|
17
|
+
data: string,
|
|
18
|
+
action: "tui.select.up" | "tui.select.down" | "tui.select.confirm" | "tui.select.cancel",
|
|
19
|
+
): boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ApprovalPromptTui {
|
|
23
|
+
requestRender(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Theme-aware approval dialog used by the interactive TUI. */
|
|
27
|
+
export function createApprovalPrompt(
|
|
28
|
+
tui: ApprovalPromptTui,
|
|
29
|
+
theme: ApprovalPromptTheme,
|
|
30
|
+
keybindings: ApprovalPromptKeybindings,
|
|
31
|
+
done: (result: Action | undefined) => void,
|
|
32
|
+
command: string,
|
|
33
|
+
rule: string,
|
|
34
|
+
): Component {
|
|
35
|
+
let selectedIndex = 0;
|
|
36
|
+
|
|
37
|
+
const invalidate = () => {};
|
|
38
|
+
|
|
39
|
+
const innerWidth = (width: number) => Math.max(1, width - 4);
|
|
40
|
+
|
|
41
|
+
const panelLine = (
|
|
42
|
+
text: string,
|
|
43
|
+
width: number,
|
|
44
|
+
color: "text" | "muted" | "accent" | "toolOutput" = "text",
|
|
45
|
+
) => {
|
|
46
|
+
const contentWidth = innerWidth(width);
|
|
47
|
+
const content = truncateToWidth(text, contentWidth, "…");
|
|
48
|
+
const padding = " ".repeat(Math.max(0, contentWidth - visibleWidth(content)));
|
|
49
|
+
|
|
50
|
+
const styled = theme.style(`${content}${padding}`, { fg: color });
|
|
51
|
+
|
|
52
|
+
return ` ${styled} `;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const component: Component = {
|
|
56
|
+
render(width: number): string[] {
|
|
57
|
+
const available = innerWidth(width);
|
|
58
|
+
const border = theme.style("─".repeat(available + 4), { fg: "border" });
|
|
59
|
+
|
|
60
|
+
const lines = [
|
|
61
|
+
border,
|
|
62
|
+
panelLine(theme.bold("pi-gate: allow this command?"), width, "accent"),
|
|
63
|
+
panelLine("Review the requested command or file change:", width, "muted"),
|
|
64
|
+
panelLine("", width),
|
|
65
|
+
...command.split("\n").map((line) => {
|
|
66
|
+
const contentWidth = innerWidth(width);
|
|
67
|
+
const parts = line.split(/([»«])/u);
|
|
68
|
+
let matched = false;
|
|
69
|
+
|
|
70
|
+
const rendered = parts
|
|
71
|
+
.map((part) => {
|
|
72
|
+
if (part === "»") {
|
|
73
|
+
matched = true;
|
|
74
|
+
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (part === "«") {
|
|
79
|
+
matched = false;
|
|
80
|
+
|
|
81
|
+
return "";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return theme.style(part, { fg: matched ? "accent" : "toolOutput", bold: matched });
|
|
85
|
+
})
|
|
86
|
+
.join("");
|
|
87
|
+
|
|
88
|
+
const visible = line.replace(/[»«]/gu, "");
|
|
89
|
+
const content = truncateToWidth(visible, contentWidth, "…");
|
|
90
|
+
const padding = " ".repeat(Math.max(0, contentWidth - visibleWidth(content)));
|
|
91
|
+
|
|
92
|
+
return ` ${rendered}${theme.style(padding, { fg: "toolOutput" })} `;
|
|
93
|
+
}),
|
|
94
|
+
panelLine("", width),
|
|
95
|
+
panelLine(`Matched rule: ${rule}`, width, "muted"),
|
|
96
|
+
panelLine("", width),
|
|
97
|
+
...ACTIONS.map((action, index) => {
|
|
98
|
+
const selected = index === selectedIndex;
|
|
99
|
+
const label = `${selected ? "→ " : " "}${action}`;
|
|
100
|
+
const row = truncateToWidth(label, available);
|
|
101
|
+
const padded = `${row}${" ".repeat(Math.max(0, available - visibleWidth(row)))}`;
|
|
102
|
+
|
|
103
|
+
return theme.style(` ${padded} `, {
|
|
104
|
+
fg: selected ? "accent" : "text",
|
|
105
|
+
});
|
|
106
|
+
}),
|
|
107
|
+
panelLine("", width),
|
|
108
|
+
panelLine("↑↓ move · enter confirm · esc cancel", width, "muted"),
|
|
109
|
+
border,
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
return lines.map((line) => truncateToWidth(line, width));
|
|
113
|
+
},
|
|
114
|
+
handleInput(data: string): void {
|
|
115
|
+
if (keybindings.matches(data, "tui.select.up") || data === "k") {
|
|
116
|
+
selectedIndex = Math.max(0, selectedIndex - 1);
|
|
117
|
+
} else if (keybindings.matches(data, "tui.select.down") || data === "j") {
|
|
118
|
+
selectedIndex = Math.min(ACTIONS.length - 1, selectedIndex + 1);
|
|
119
|
+
} else if (keybindings.matches(data, "tui.select.confirm")) {
|
|
120
|
+
done(ACTIONS[selectedIndex]);
|
|
121
|
+
|
|
122
|
+
return;
|
|
123
|
+
} else if (keybindings.matches(data, "tui.select.cancel")) {
|
|
124
|
+
done(undefined);
|
|
125
|
+
|
|
126
|
+
return;
|
|
127
|
+
} else {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
component.invalidate();
|
|
132
|
+
tui.requestRender();
|
|
133
|
+
},
|
|
134
|
+
invalidate,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return component;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type ApprovalAction = Action;
|
package/src/index.ts
CHANGED
|
@@ -31,8 +31,10 @@
|
|
|
31
31
|
import {
|
|
32
32
|
isToolCallEventType,
|
|
33
33
|
type ExtensionAPI,
|
|
34
|
+
type ExtensionContext,
|
|
34
35
|
type ToolCallEventResult,
|
|
35
36
|
} from "@earendil-works/pi-coding-agent";
|
|
37
|
+
import { createApprovalPrompt, type ApprovalAction } from "./approval-prompt";
|
|
36
38
|
import { configPath, ensureConfig, loadConfigResult } from "./config";
|
|
37
39
|
import {
|
|
38
40
|
bashTouchesConfig,
|
|
@@ -44,6 +46,76 @@ import { formatCommandForDisplay, formatPromptCommand, formatRule } from "./disp
|
|
|
44
46
|
|
|
45
47
|
const HERDR_BLOCKED_LABEL = "pi-gate: approval required";
|
|
46
48
|
|
|
49
|
+
/** Uses a themed terminal component in TUI mode and Pi's standard selector elsewhere. */
|
|
50
|
+
async function selectApproval(
|
|
51
|
+
ctx: ExtensionContext,
|
|
52
|
+
timeoutMs: number,
|
|
53
|
+
signal: AbortSignal,
|
|
54
|
+
command: string,
|
|
55
|
+
rule: string,
|
|
56
|
+
showMatchHint: boolean,
|
|
57
|
+
): Promise<string | undefined> {
|
|
58
|
+
if (ctx.mode !== "tui" || !ctx.ui.custom) {
|
|
59
|
+
return ctx.ui.select(
|
|
60
|
+
`pi-gate: allow this command?\n\n${command}\n\nMatched rule: ${rule}${showMatchHint ? "\nMatched command text is wrapped in »…«" : ""}`,
|
|
61
|
+
["Allow", "Deny"],
|
|
62
|
+
{ timeout: timeoutMs, signal },
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let uiDone: ((value: ApprovalAction | undefined) => void) | undefined;
|
|
67
|
+
let settled = false;
|
|
68
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
69
|
+
let removeAbortListener: (() => void) | undefined;
|
|
70
|
+
|
|
71
|
+
return new Promise<ApprovalAction | undefined>((resolve, reject) => {
|
|
72
|
+
const finish = (value: ApprovalAction | undefined) => {
|
|
73
|
+
if (settled) return;
|
|
74
|
+
settled = true;
|
|
75
|
+
|
|
76
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
77
|
+
removeAbortListener?.();
|
|
78
|
+
resolve(value);
|
|
79
|
+
uiDone?.(value);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const onAbort = () => finish(undefined);
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
timer = setTimeout(() => finish(undefined), timeoutMs);
|
|
86
|
+
|
|
87
|
+
const uiPromise = ctx.ui.custom<ApprovalAction | undefined>(
|
|
88
|
+
(tui, theme, keybindings, done) => {
|
|
89
|
+
uiDone = done;
|
|
90
|
+
|
|
91
|
+
return createApprovalPrompt(tui, theme, keybindings, done, command, rule);
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
uiPromise
|
|
96
|
+
.then(finish)
|
|
97
|
+
.catch(reject)
|
|
98
|
+
.finally(() => {
|
|
99
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
100
|
+
removeAbortListener?.();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (signal) {
|
|
104
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
105
|
+
removeAbortListener = () => signal.removeEventListener("abort", onAbort);
|
|
106
|
+
|
|
107
|
+
if (signal.aborted) onAbort();
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
settled = true;
|
|
111
|
+
|
|
112
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
113
|
+
removeAbortListener?.();
|
|
114
|
+
reject(error);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
47
119
|
const MAX_REASON_RULE_NAME_LENGTH = 27;
|
|
48
120
|
|
|
49
121
|
/** Escapes a rule pattern for terminal display and bounds its length in denial messages. */
|
|
@@ -57,7 +129,7 @@ function reasonRuleName(pattern: string): string {
|
|
|
57
129
|
|
|
58
130
|
/** Formats an approval timeout with the matched rule, elapsed limit in seconds, and retry guidance. */
|
|
59
131
|
function timeoutReason(pattern: string, timeoutMs: number): string {
|
|
60
|
-
return `pi-gate: no response to rule "${reasonRuleName(pattern)}" within ${timeoutMs / 1_000}s; command not run. Ask the user whether to retry.`;
|
|
132
|
+
return `pi-gate: no response to rule "${reasonRuleName(pattern)}" within ${timeoutMs / 1_000}s; command not run. Ask the user whether to retry approval.`;
|
|
61
133
|
}
|
|
62
134
|
|
|
63
135
|
function reportHerdrBlocked(pi: ExtensionAPI, ctx: { mode?: string }, active: boolean): void {
|
|
@@ -218,7 +290,7 @@ export default function piGate(pi: ExtensionAPI): void {
|
|
|
218
290
|
const rule = configChange ? CONFIG_CHANGE_REASON : formatRule(match);
|
|
219
291
|
|
|
220
292
|
if (match.action === "block") {
|
|
221
|
-
const reason = `pi-gate: blocked by rule "${reasonRuleName(match.pattern)}".
|
|
293
|
+
const reason = `pi-gate: command blocked by rule "${reasonRuleName(match.pattern)}". Ask the user before proceeding.`;
|
|
222
294
|
|
|
223
295
|
if (ctx.hasUI) ctx.ui.notify(reason, "warning");
|
|
224
296
|
|
|
@@ -230,7 +302,7 @@ export default function piGate(pi: ExtensionAPI): void {
|
|
|
230
302
|
block: true,
|
|
231
303
|
reason: configChange
|
|
232
304
|
? CONFIG_CHANGE_REASON
|
|
233
|
-
: `pi-gate: rule "${reasonRuleName(match.pattern)}" needs approval but no UI is available.
|
|
305
|
+
: `pi-gate: rule "${reasonRuleName(match.pattern)}" needs approval but no UI is available. Ask the user before proceeding.`,
|
|
234
306
|
terminate: true,
|
|
235
307
|
};
|
|
236
308
|
}
|
|
@@ -248,11 +320,7 @@ export default function piGate(pi: ExtensionAPI): void {
|
|
|
248
320
|
|
|
249
321
|
try {
|
|
250
322
|
const promptStartedAt = Date.now();
|
|
251
|
-
choice = await ctx
|
|
252
|
-
`pi-gate: allow this command?\n\n${promptCommand}\n\nMatched rule: ${rule}${configChange ? "" : "\nMatched command text is wrapped in »…«"}`,
|
|
253
|
-
["Allow", "Deny"],
|
|
254
|
-
{ timeout: timeoutMs, signal },
|
|
255
|
-
);
|
|
323
|
+
choice = await selectApproval(ctx, timeoutMs, signal, promptCommand, rule, !configChange);
|
|
256
324
|
// Pi returns undefined for both dismissal and timeout; elapsed time separates the timeout case.
|
|
257
325
|
timedOut = choice === undefined && Date.now() - promptStartedAt >= timeoutMs;
|
|
258
326
|
} finally {
|
|
@@ -277,7 +345,7 @@ export default function piGate(pi: ExtensionAPI): void {
|
|
|
277
345
|
? timeoutReason(match.pattern, timeoutMs)
|
|
278
346
|
: configChange
|
|
279
347
|
? CONFIG_CHANGE_REASON
|
|
280
|
-
: `pi-gate: denied
|
|
348
|
+
: `pi-gate: command not run because approval was denied. Ask the user before retrying (rule "${reasonRuleName(match.pattern)}").`,
|
|
281
349
|
terminate: true,
|
|
282
350
|
};
|
|
283
351
|
}
|