@gotgenes/pi-permission-system 13.2.0 → 14.0.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/CHANGELOG.md +24 -0
- package/package.json +1 -1
- package/src/bash-arity.ts +24 -0
- package/src/config-paths.ts +13 -0
- package/src/input-normalizer.ts +7 -1
- package/src/pattern-suggest.ts +8 -4
- package/src/permission-manager.ts +6 -2
- package/test/bash-arity.test.ts +39 -1
- package/test/config-paths.test.ts +5 -0
- package/test/input-normalizer.test.ts +28 -0
- package/test/pattern-suggest.test.ts +24 -0
- package/test/permission-manager-unified.test.ts +52 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [14.0.1](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v14.0.0...pi-permission-system-v14.0.1) (2026-06-19)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **pi-permission-system:** strip shell comment lines from bash commands before matching ([d045591](https://github.com/gotgenes/pi-packages/commit/d0455915d6d4ce50534884639e516a9e1ef38976))
|
|
14
|
+
|
|
15
|
+
## [14.0.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v13.2.0...pi-permission-system-v14.0.0) (2026-06-17)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### ⚠ BREAKING CHANGES
|
|
19
|
+
|
|
20
|
+
* project agents' `permission:` frontmatter at `<cwd>/.pi/agents/<name>.md` is now read and enforced. Previously the wrong directory (`<cwd>/.pi/agent/agents`) was checked and the frontmatter was silently ignored, so a session may become more restrictive on upgrade.
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* correct project agents directory path to <cwd>/.pi/agents ([#428](https://github.com/gotgenes/pi-packages/issues/428)) ([eb5af78](https://github.com/gotgenes/pi-packages/commit/eb5af78193fa3cc574da6b8d80efd643ebce0ef9))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Documentation
|
|
28
|
+
|
|
29
|
+
* correct project agent override path to <cwd>/.pi/agents ([#428](https://github.com/gotgenes/pi-packages/issues/428)) ([d193d6a](https://github.com/gotgenes/pi-packages/commit/d193d6a61155fb4e4a064800509cdbbd84b0ceb9))
|
|
30
|
+
* fix stale project agents path in troubleshooting and ADR-0001 ([#428](https://github.com/gotgenes/pi-packages/issues/428)) ([95effeb](https://github.com/gotgenes/pi-packages/commit/95effebfd0b2e87db4512c91adc134b2470f26a3))
|
|
31
|
+
|
|
8
32
|
## [13.2.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v13.1.2...pi-permission-system-v13.2.0) (2026-06-17)
|
|
9
33
|
|
|
10
34
|
|
package/package.json
CHANGED
package/src/bash-arity.ts
CHANGED
|
@@ -184,3 +184,27 @@ export function prefix(tokens: string[]): string[] {
|
|
|
184
184
|
// Unknown command — default arity 1.
|
|
185
185
|
return [tokens[0]];
|
|
186
186
|
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Remove shell comment lines from a bash command string.
|
|
190
|
+
*
|
|
191
|
+
* A comment line is one whose first non-whitespace character is `#`. Agents
|
|
192
|
+
* frequently prepend descriptive comments before the real command
|
|
193
|
+
* (e.g. `"# Check debug logs\nfind ..."`); such prefixes defeat wildcard
|
|
194
|
+
* pattern matching and session-approval suggestions, which tokenize the
|
|
195
|
+
* leading text. Stripping comment lines lets matching operate on the actual
|
|
196
|
+
* command.
|
|
197
|
+
*
|
|
198
|
+
* The original command is never returned: when every line is a comment (or
|
|
199
|
+
* the input is blank) an empty string is returned, and each caller applies
|
|
200
|
+
* its own fallback.
|
|
201
|
+
*
|
|
202
|
+
* @param command - Raw bash command, possibly multi-line.
|
|
203
|
+
* @returns The command with comment lines removed and surrounding whitespace
|
|
204
|
+
* trimmed, or an empty string when nothing meaningful remains.
|
|
205
|
+
*/
|
|
206
|
+
export function stripBashCommentLines(command: string): string {
|
|
207
|
+
const lines = command.split("\n");
|
|
208
|
+
const meaningful = lines.filter((line) => !/^\s*#/.test(line));
|
|
209
|
+
return meaningful.join("\n").trim();
|
|
210
|
+
}
|
package/src/config-paths.ts
CHANGED
|
@@ -21,6 +21,19 @@ export function getProjectConfigPath(cwd: string): string {
|
|
|
21
21
|
return join(cwd, ".pi", "extensions", EXTENSION_ID, "config.json");
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Directory holding project-scoped custom agent definition files.
|
|
26
|
+
*
|
|
27
|
+
* `<cwd>/.pi/agents` is a Pi platform convention, also encoded by
|
|
28
|
+
* `@gotgenes/pi-subagents`' `loadCustomAgents` (`config/custom-agents.ts`).
|
|
29
|
+
* The two packages encode it independently — pi-permission-system has no
|
|
30
|
+
* dependency on pi-subagents (ADR-0002) — so this is this package's
|
|
31
|
+
* authoritative copy.
|
|
32
|
+
*/
|
|
33
|
+
export function getProjectAgentsDir(cwd: string): string {
|
|
34
|
+
return join(cwd, ".pi", "agents");
|
|
35
|
+
}
|
|
36
|
+
|
|
24
37
|
export function getLegacyGlobalPolicyPath(agentDir: string): string {
|
|
25
38
|
return join(agentDir, "pi-permissions.jsonc");
|
|
26
39
|
}
|
package/src/input-normalizer.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stripBashCommentLines } from "./bash-arity";
|
|
1
2
|
import { getNonEmptyString, toRecord } from "./common";
|
|
2
3
|
import { createMcpPermissionTargets } from "./mcp-targets";
|
|
3
4
|
import { getPathPolicyValues, PATH_BEARING_TOOLS } from "./path-utils";
|
|
@@ -92,9 +93,14 @@ export function normalizeInput(
|
|
|
92
93
|
if (toolName === "bash") {
|
|
93
94
|
const record = toRecord(input);
|
|
94
95
|
const command = typeof record.command === "string" ? record.command : "";
|
|
96
|
+
// Strip leading shell comment lines so pattern matching operates on the
|
|
97
|
+
// actual command, not a `# description` prefix agents often prepend.
|
|
98
|
+
// Fall back to the raw command when stripping leaves nothing, so an
|
|
99
|
+
// all-comment command still evaluates against its literal text.
|
|
100
|
+
const matchValue = stripBashCommentLines(command) || command;
|
|
95
101
|
return {
|
|
96
102
|
surface: "bash",
|
|
97
|
-
values: [
|
|
103
|
+
values: [matchValue],
|
|
98
104
|
resultExtras: { command },
|
|
99
105
|
};
|
|
100
106
|
}
|
package/src/pattern-suggest.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { prefix } from "./bash-arity";
|
|
1
|
+
import { prefix, stripBashCommentLines } from "./bash-arity";
|
|
2
2
|
import { PATH_BEARING_TOOLS } from "./path-utils";
|
|
3
3
|
import { deriveApprovalPattern } from "./session-rules";
|
|
4
4
|
|
|
@@ -26,11 +26,15 @@ export interface SessionApprovalSuggestion {
|
|
|
26
26
|
export function suggestBashPattern(command: string): string {
|
|
27
27
|
const trimmed = command.trim();
|
|
28
28
|
if (!trimmed) return "";
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
// Strip leading shell comment lines so the suggestion is based on the
|
|
30
|
+
// actual command, not a `# description` prefix agents often prepend.
|
|
31
|
+
const stripped = stripBashCommentLines(trimmed);
|
|
32
|
+
if (!stripped) return "";
|
|
33
|
+
const tokens = stripped.split(/\s+/);
|
|
34
|
+
if (tokens.length === 1) return stripped;
|
|
31
35
|
const meaningful = prefix(tokens);
|
|
32
36
|
if (meaningful.length >= tokens.length) {
|
|
33
|
-
return `${
|
|
37
|
+
return `${stripped}*`;
|
|
34
38
|
}
|
|
35
39
|
return `${meaningful.join(" ")} *`;
|
|
36
40
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { isPermissionState } from "./common";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getGlobalConfigPath,
|
|
5
|
+
getProjectAgentsDir,
|
|
6
|
+
getProjectConfigPath,
|
|
7
|
+
} from "./config-paths";
|
|
4
8
|
import { normalizeInput } from "./input-normalizer";
|
|
5
9
|
import { normalizeFlatConfig } from "./normalize";
|
|
6
10
|
import { PATH_SURFACES } from "./path-utils";
|
|
@@ -350,7 +354,7 @@ function derivePolicyLoaderOptions(
|
|
|
350
354
|
globalConfigPath: getGlobalConfigPath(agentDir),
|
|
351
355
|
agentsDir: join(agentDir, "agents"),
|
|
352
356
|
projectGlobalConfigPath: cwd ? getProjectConfigPath(cwd) : undefined,
|
|
353
|
-
projectAgentsDir: cwd ?
|
|
357
|
+
projectAgentsDir: cwd ? getProjectAgentsDir(cwd) : undefined,
|
|
354
358
|
};
|
|
355
359
|
}
|
|
356
360
|
|
package/test/bash-arity.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { ARITY, prefix } from "#src/bash-arity";
|
|
2
|
+
import { ARITY, prefix, stripBashCommentLines } from "#src/bash-arity";
|
|
3
3
|
|
|
4
4
|
describe("ARITY dictionary", () => {
|
|
5
5
|
it("is exported as a plain object", () => {
|
|
@@ -104,3 +104,41 @@ describe("prefix", () => {
|
|
|
104
104
|
expect(prefix(["ls", "-la", "/tmp"])).toEqual(["ls"]);
|
|
105
105
|
});
|
|
106
106
|
});
|
|
107
|
+
|
|
108
|
+
describe("stripBashCommentLines", () => {
|
|
109
|
+
it("removes a single leading comment line", () => {
|
|
110
|
+
expect(
|
|
111
|
+
stripBashCommentLines("# Check debug logs\nfind /home -type f"),
|
|
112
|
+
).toBe("find /home -type f");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("removes multiple leading comment lines", () => {
|
|
116
|
+
expect(
|
|
117
|
+
stripBashCommentLines("# Step 1\n# Step 2\ngit status --short"),
|
|
118
|
+
).toBe("git status --short");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("returns empty string when all lines are comments", () => {
|
|
122
|
+
expect(stripBashCommentLines("# just a comment")).toBe("");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("returns empty string for blank input", () => {
|
|
126
|
+
expect(stripBashCommentLines("")).toBe("");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("returns the command unchanged when no comment lines are present", () => {
|
|
130
|
+
expect(stripBashCommentLines("grep -rn foo src/")).toBe(
|
|
131
|
+
"grep -rn foo src/",
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("trims surrounding whitespace from the result", () => {
|
|
136
|
+
expect(stripBashCommentLines("\n\n ls -la \n")).toBe("ls -la");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("treats indented comment lines as comments", () => {
|
|
140
|
+
expect(stripBashCommentLines(" # indented comment\necho hi")).toBe(
|
|
141
|
+
"echo hi",
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
getLegacyExtensionConfigPath,
|
|
10
10
|
getLegacyGlobalPolicyPath,
|
|
11
11
|
getLegacyProjectPolicyPath,
|
|
12
|
+
getProjectAgentsDir,
|
|
12
13
|
getProjectConfigPath,
|
|
13
14
|
REVIEW_LOG_FILENAME,
|
|
14
15
|
} from "#src/config-paths";
|
|
@@ -42,6 +43,10 @@ describe("config-paths", () => {
|
|
|
42
43
|
join(cwd, ".pi", "extensions", "pi-permission-system", "config.json"),
|
|
43
44
|
);
|
|
44
45
|
});
|
|
46
|
+
|
|
47
|
+
it("getProjectAgentsDir returns .pi/agents under cwd", () => {
|
|
48
|
+
expect(getProjectAgentsDir(cwd)).toBe(join(cwd, ".pi", "agents"));
|
|
49
|
+
});
|
|
45
50
|
});
|
|
46
51
|
|
|
47
52
|
describe("legacy paths", () => {
|
|
@@ -198,6 +198,34 @@ describe("normalizeInput — non-MCP surfaces", () => {
|
|
|
198
198
|
expect(result.values).toEqual([""]);
|
|
199
199
|
expect(result.resultExtras).toEqual({ command: "" });
|
|
200
200
|
});
|
|
201
|
+
|
|
202
|
+
it("strips leading comment lines from values but keeps original in resultExtras", () => {
|
|
203
|
+
const cmd = "# Check debug logs\nfind /home -path '*debug*' -type f";
|
|
204
|
+
const result = normalizeInput("bash", { command: cmd }, []);
|
|
205
|
+
expect(result.values).toEqual(["find /home -path '*debug*' -type f"]);
|
|
206
|
+
expect(result.resultExtras).toEqual({ command: cmd });
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("strips multiple comment lines", () => {
|
|
210
|
+
const cmd = "# Step 1\n# Step 2\ngit status --short";
|
|
211
|
+
const result = normalizeInput("bash", { command: cmd }, []);
|
|
212
|
+
expect(result.values).toEqual(["git status --short"]);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("preserves command when no comment lines present", () => {
|
|
216
|
+
const result = normalizeInput(
|
|
217
|
+
"bash",
|
|
218
|
+
{ command: "grep -rn foo src/" },
|
|
219
|
+
[],
|
|
220
|
+
);
|
|
221
|
+
expect(result.values).toEqual(["grep -rn foo src/"]);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("falls back to original when all lines are comments", () => {
|
|
225
|
+
const cmd = "# just a comment";
|
|
226
|
+
const result = normalizeInput("bash", { command: cmd }, []);
|
|
227
|
+
expect(result.values).toEqual(["# just a comment"]);
|
|
228
|
+
});
|
|
201
229
|
});
|
|
202
230
|
|
|
203
231
|
describe("path-bearing tools (read, write, edit, grep, find, ls)", () => {
|
|
@@ -42,6 +42,30 @@ describe("suggestBashPattern", () => {
|
|
|
42
42
|
"docker compose up *",
|
|
43
43
|
);
|
|
44
44
|
});
|
|
45
|
+
|
|
46
|
+
it("strips leading comment lines and suggests based on the actual command", () => {
|
|
47
|
+
expect(
|
|
48
|
+
suggestBashPattern(
|
|
49
|
+
"# Check debug logs\nfind /home -path '*debug*' -type f",
|
|
50
|
+
),
|
|
51
|
+
).toBe("find *");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("strips multiple leading comment lines", () => {
|
|
55
|
+
expect(suggestBashPattern("# Step 1\n# Step 2\ngit status --short")).toBe(
|
|
56
|
+
"git status *",
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("returns empty for comment-only input", () => {
|
|
61
|
+
expect(suggestBashPattern("# just a comment")).toBe("");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("handles mixed comment and command lines", () => {
|
|
65
|
+
expect(suggestBashPattern("# description\nrm -rf ./build; echo done")).toBe(
|
|
66
|
+
"rm *",
|
|
67
|
+
);
|
|
68
|
+
});
|
|
45
69
|
});
|
|
46
70
|
|
|
47
71
|
describe("suggestMcpPattern", () => {
|
|
@@ -8,7 +8,11 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
|
8
8
|
import { homedir, tmpdir } from "node:os";
|
|
9
9
|
import { dirname, join } from "node:path";
|
|
10
10
|
import { describe, expect, it, test } from "vitest";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
getGlobalConfigPath,
|
|
13
|
+
getProjectAgentsDir,
|
|
14
|
+
getProjectConfigPath,
|
|
15
|
+
} from "#src/config-paths";
|
|
12
16
|
import {
|
|
13
17
|
PermissionManager,
|
|
14
18
|
type ScopedPermissionManager,
|
|
@@ -1669,6 +1673,53 @@ describe("PermissionManager — configureForCwd and agentDir option", () => {
|
|
|
1669
1673
|
cleanup();
|
|
1670
1674
|
}
|
|
1671
1675
|
});
|
|
1676
|
+
|
|
1677
|
+
it("configureForCwd(cwd) derives projectAgentsDir at <cwd>/.pi/agents (regression: #428)", () => {
|
|
1678
|
+
// Bug: old code derived <cwd>/.pi/agent/agents instead of <cwd>/.pi/agents.
|
|
1679
|
+
// This test pins the correct path and verifies agentsDir is unchanged.
|
|
1680
|
+
const { agentDir, cwd, cleanup } = makeAgentDirSetup({
|
|
1681
|
+
globalPermission: { read: "allow" },
|
|
1682
|
+
});
|
|
1683
|
+
try {
|
|
1684
|
+
const manager = new PermissionManager({ agentDir });
|
|
1685
|
+
manager.configureForCwd(cwd);
|
|
1686
|
+
const paths = manager.getResolvedPolicyPaths();
|
|
1687
|
+
expect(paths.projectAgentsDir).toBe(getProjectAgentsDir(cwd));
|
|
1688
|
+
expect(paths.agentsDir).toBe(join(agentDir, "agents"));
|
|
1689
|
+
} finally {
|
|
1690
|
+
cleanup();
|
|
1691
|
+
}
|
|
1692
|
+
});
|
|
1693
|
+
|
|
1694
|
+
it("configureForCwd(cwd) enforces permission: frontmatter from <cwd>/.pi/agents/<agent>.md (regression: #428)", () => {
|
|
1695
|
+
// Bug: wrong directory meant project-agent frontmatter was never loaded.
|
|
1696
|
+
const { agentDir, cwd, cleanup } = makeAgentDirSetup({
|
|
1697
|
+
globalPermission: { read: "allow" },
|
|
1698
|
+
});
|
|
1699
|
+
try {
|
|
1700
|
+
// Write a project agent definition with a deny override.
|
|
1701
|
+
const projectAgentsDir = getProjectAgentsDir(cwd);
|
|
1702
|
+
mkdirSync(projectAgentsDir, { recursive: true });
|
|
1703
|
+
writeFileSync(
|
|
1704
|
+
join(projectAgentsDir, "coder.md"),
|
|
1705
|
+
"---\npermission:\n read: deny\n---\n# Coder\n",
|
|
1706
|
+
);
|
|
1707
|
+
|
|
1708
|
+
const manager = new PermissionManager({ agentDir });
|
|
1709
|
+
manager.configureForCwd(cwd);
|
|
1710
|
+
|
|
1711
|
+
// Without an agent name: global allow applies.
|
|
1712
|
+
expect(manager.checkPermission("read", { path: "foo.txt" }).state).toBe(
|
|
1713
|
+
"allow",
|
|
1714
|
+
);
|
|
1715
|
+
// With the "coder" agent: project-agent deny overrides global allow.
|
|
1716
|
+
expect(
|
|
1717
|
+
manager.checkPermission("read", { path: "foo.txt" }, "coder").state,
|
|
1718
|
+
).toBe("deny");
|
|
1719
|
+
} finally {
|
|
1720
|
+
cleanup();
|
|
1721
|
+
}
|
|
1722
|
+
});
|
|
1672
1723
|
});
|
|
1673
1724
|
|
|
1674
1725
|
// ---------------------------------------------------------------------------
|