@gotgenes/pi-permission-system 18.1.0 → 18.1.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.
Files changed (132) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/docs/assets/logo.png +0 -0
  3. package/docs/assets/logo.svg +213 -0
  4. package/docs/configuration.md +700 -0
  5. package/docs/cross-extension-api.md +525 -0
  6. package/docs/guides/permission-frontmatter-for-subagent-extensions.md +201 -0
  7. package/docs/guides/upstream-issue-template.md +113 -0
  8. package/docs/migration/legacy-to-flat.md +365 -0
  9. package/docs/opencode-compatibility.md +213 -0
  10. package/docs/session-approvals.md +68 -0
  11. package/docs/subagent-integration.md +102 -0
  12. package/docs/troubleshooting.md +53 -0
  13. package/package.json +5 -2
  14. package/src/access-intent/access-path.ts +29 -4
  15. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  16. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  17. package/src/path-normalizer.ts +105 -2
  18. package/test/access-intent/access-path.test.ts +0 -277
  19. package/test/access-intent/bash/node-text.test.ts +0 -148
  20. package/test/access-intent/bash/parser.test.ts +0 -19
  21. package/test/access-intent/bash/program.test.ts +0 -673
  22. package/test/access-intent/bash/token-classification.test.ts +0 -363
  23. package/test/access-intent/bash/token-collection.test.ts +0 -300
  24. package/test/active-agent.test.ts +0 -155
  25. package/test/async-cache.test.ts +0 -48
  26. package/test/bash-arity.test.ts +0 -144
  27. package/test/bash-external-directory.test.ts +0 -1022
  28. package/test/builtin-tool-input-formatters.test.ts +0 -109
  29. package/test/canonicalize-path.test.ts +0 -119
  30. package/test/composition-root.test.ts +0 -698
  31. package/test/config-loader.test.ts +0 -740
  32. package/test/config-modal.test.ts +0 -320
  33. package/test/config-paths.test.ts +0 -83
  34. package/test/config-pipeline.test.ts +0 -90
  35. package/test/config-reporter.test.ts +0 -147
  36. package/test/config-store.test.ts +0 -466
  37. package/test/decision-audit.test.ts +0 -72
  38. package/test/decision-reporter.test.ts +0 -112
  39. package/test/denial-messages.test.ts +0 -714
  40. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  41. package/test/expand-home.test.ts +0 -93
  42. package/test/extension-config.test.ts +0 -129
  43. package/test/extension-paths.test.ts +0 -108
  44. package/test/forwarded-permissions/io.test.ts +0 -251
  45. package/test/forwarding-manager.test.ts +0 -200
  46. package/test/handlers/before-agent-start.test.ts +0 -314
  47. package/test/handlers/external-directory-integration.test.ts +0 -515
  48. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  49. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  50. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  51. package/test/handlers/gates/bash-command.test.ts +0 -257
  52. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  53. package/test/handlers/gates/bash-path.test.ts +0 -346
  54. package/test/handlers/gates/candidate-check.test.ts +0 -52
  55. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  56. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  57. package/test/handlers/gates/external-directory.test.ts +0 -267
  58. package/test/handlers/gates/helpers.test.ts +0 -165
  59. package/test/handlers/gates/path.test.ts +0 -369
  60. package/test/handlers/gates/runner.test.ts +0 -408
  61. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  62. package/test/handlers/gates/skill-input.test.ts +0 -128
  63. package/test/handlers/gates/skill-read.test.ts +0 -161
  64. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  65. package/test/handlers/gates/tool.test.ts +0 -244
  66. package/test/handlers/input-events.test.ts +0 -168
  67. package/test/handlers/input.test.ts +0 -199
  68. package/test/handlers/lifecycle.test.ts +0 -221
  69. package/test/handlers/tool-call-boundary.test.ts +0 -145
  70. package/test/handlers/tool-call-events.test.ts +0 -277
  71. package/test/handlers/tool-call.test.ts +0 -395
  72. package/test/handlers/validate-requested-tool.test.ts +0 -92
  73. package/test/helpers/external-directory-fixtures.ts +0 -269
  74. package/test/helpers/gate-fixtures.ts +0 -316
  75. package/test/helpers/handler-fixtures.ts +0 -335
  76. package/test/helpers/make-fake-pi.ts +0 -100
  77. package/test/helpers/manager-harness.ts +0 -112
  78. package/test/helpers/session-fixtures.ts +0 -199
  79. package/test/input-normalizer.test.ts +0 -325
  80. package/test/logging.test.ts +0 -51
  81. package/test/mcp-targets.test.ts +0 -233
  82. package/test/node-modules-discovery.test.ts +0 -97
  83. package/test/normalize.test.ts +0 -247
  84. package/test/path-containment.test.ts +0 -161
  85. package/test/path-normalization.test.ts +0 -233
  86. package/test/path-normalizer.test.ts +0 -196
  87. package/test/path-surfaces.test.ts +0 -55
  88. package/test/pattern-suggest.test.ts +0 -248
  89. package/test/permission-dialog.test.ts +0 -205
  90. package/test/permission-event-rpc.test.ts +0 -560
  91. package/test/permission-events.test.ts +0 -400
  92. package/test/permission-forwarder.test.ts +0 -370
  93. package/test/permission-forwarding.test.ts +0 -315
  94. package/test/permission-gate.test.ts +0 -269
  95. package/test/permission-manager-unified.test.ts +0 -3714
  96. package/test/permission-merge.test.ts +0 -61
  97. package/test/permission-prompter.test.ts +0 -518
  98. package/test/permission-prompts.test.ts +0 -363
  99. package/test/permission-resolver.test.ts +0 -277
  100. package/test/permission-session.test.ts +0 -404
  101. package/test/permission-ui-prompt.test.ts +0 -146
  102. package/test/permissions-service.test.ts +0 -192
  103. package/test/pi-infrastructure-read.test.ts +0 -432
  104. package/test/policy-loader.test.ts +0 -561
  105. package/test/prompting-gateway.test.ts +0 -231
  106. package/test/rule.test.ts +0 -650
  107. package/test/safe-system-paths.test.ts +0 -46
  108. package/test/scope-merge.test.ts +0 -116
  109. package/test/service-lifecycle.test.ts +0 -163
  110. package/test/service.test.ts +0 -261
  111. package/test/session-approval.test.ts +0 -75
  112. package/test/session-logger.test.ts +0 -200
  113. package/test/session-rules.test.ts +0 -321
  114. package/test/session-start.test.ts +0 -112
  115. package/test/skill-prompt-sanitizer.test.ts +0 -418
  116. package/test/status.test.ts +0 -10
  117. package/test/subagent-context.test.ts +0 -372
  118. package/test/subagent-lifecycle-events.test.ts +0 -132
  119. package/test/subagent-registry.test.ts +0 -145
  120. package/test/synthesize.test.ts +0 -302
  121. package/test/system-prompt-sanitizer.test.ts +0 -382
  122. package/test/tool-access-extractor-registry.test.ts +0 -77
  123. package/test/tool-input-formatter-registry.test.ts +0 -75
  124. package/test/tool-input-path.test.ts +0 -84
  125. package/test/tool-input-preview.test.ts +0 -129
  126. package/test/tool-input-prompt-formatters.test.ts +0 -115
  127. package/test/tool-preview-formatter.test.ts +0 -458
  128. package/test/tool-registry.test.ts +0 -197
  129. package/test/value-guards.test.ts +0 -193
  130. package/test/wildcard-matcher.test.ts +0 -424
  131. package/test/yaml-frontmatter.test.ts +0 -91
  132. package/test/yolo-mode.test.ts +0 -188
@@ -1,128 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { describeSkillInputGate } from "#src/handlers/gates/skill-input";
4
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
5
-
6
- // ── helpers ────────────────────────────────────────────────────────────────
7
-
8
- function makeSkillCheck(state: "allow" | "deny" | "ask") {
9
- return makeCheckResult({
10
- state,
11
- toolName: "skill",
12
- source: "skill",
13
- origin: "global",
14
- matchedPattern: "*",
15
- });
16
- }
17
-
18
- // ── describeSkillInputGate ─────────────────────────────────────────────────
19
-
20
- describe("describeSkillInputGate", () => {
21
- it("sets surface to 'skill'", () => {
22
- const descriptor = describeSkillInputGate(
23
- "librarian",
24
- null,
25
- makeSkillCheck("allow"),
26
- );
27
- expect(descriptor.surface).toBe("skill");
28
- });
29
-
30
- it("sets input.name to the skill name", () => {
31
- const descriptor = describeSkillInputGate(
32
- "librarian",
33
- null,
34
- makeSkillCheck("allow"),
35
- );
36
- expect(descriptor.input).toEqual({ name: "librarian" });
37
- });
38
-
39
- it("passes preCheck through verbatim", () => {
40
- const check = makeSkillCheck("deny");
41
- const descriptor = describeSkillInputGate("librarian", null, check);
42
- expect(descriptor.preCheck).toBe(check);
43
- });
44
-
45
- it("sets denialContext with kind skill_input and skill name", () => {
46
- const descriptor = describeSkillInputGate(
47
- "librarian",
48
- null,
49
- makeSkillCheck("allow"),
50
- );
51
- expect(descriptor.denialContext).toEqual({
52
- kind: "skill_input",
53
- skillName: "librarian",
54
- agentName: undefined,
55
- });
56
- });
57
-
58
- it("includes agentName in denialContext when provided", () => {
59
- const descriptor = describeSkillInputGate(
60
- "librarian",
61
- "code-agent",
62
- makeSkillCheck("allow"),
63
- );
64
- expect(descriptor.denialContext).toEqual({
65
- kind: "skill_input",
66
- skillName: "librarian",
67
- agentName: "code-agent",
68
- });
69
- });
70
-
71
- it("sets promptDetails source to 'skill_input' with skill name and agent", () => {
72
- const descriptor = describeSkillInputGate(
73
- "librarian",
74
- "code-agent",
75
- makeSkillCheck("ask"),
76
- );
77
- expect(descriptor.promptDetails).toMatchObject({
78
- source: "skill_input",
79
- agentName: "code-agent",
80
- skillName: "librarian",
81
- });
82
- });
83
-
84
- it("includes a non-empty message in promptDetails", () => {
85
- const descriptor = describeSkillInputGate(
86
- "librarian",
87
- null,
88
- makeSkillCheck("ask"),
89
- );
90
- expect(typeof descriptor.promptDetails.message).toBe("string");
91
- expect(descriptor.promptDetails.message.length).toBeGreaterThan(0);
92
- });
93
-
94
- it("sets logContext source to 'skill_input' with skill name and agent", () => {
95
- const descriptor = describeSkillInputGate(
96
- "librarian",
97
- "code-agent",
98
- makeSkillCheck("allow"),
99
- );
100
- expect(descriptor.logContext).toMatchObject({
101
- source: "skill_input",
102
- skillName: "librarian",
103
- agentName: "code-agent",
104
- });
105
- });
106
-
107
- it("sets decision surface to 'skill' and value to the skill name", () => {
108
- const descriptor = describeSkillInputGate(
109
- "my-skill",
110
- null,
111
- makeSkillCheck("allow"),
112
- );
113
- expect(descriptor.decision).toEqual({
114
- surface: "skill",
115
- value: "my-skill",
116
- });
117
- });
118
-
119
- it("does not set preResolved or sessionApproval", () => {
120
- const descriptor = describeSkillInputGate(
121
- "librarian",
122
- null,
123
- makeSkillCheck("allow"),
124
- );
125
- expect(descriptor.preResolved).toBeUndefined();
126
- expect(descriptor.sessionApproval).toBeUndefined();
127
- });
128
- });
@@ -1,161 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { describeSkillReadGate } from "#src/handlers/gates/skill-read";
3
- import type { ToolCallContext } from "#src/handlers/gates/types";
4
- import { PathNormalizer } from "#src/path-normalizer";
5
- import type { SkillPromptEntry } from "#src/skill-prompt-sanitizer";
6
-
7
- // All test tccs use cwd "/test/project"; one normalizer serves every call.
8
- const normalizer = new PathNormalizer("linux", "/test/project");
9
-
10
- // ── SDK stubs ──────────────────────────────────────────────────────────────
11
- vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
12
- const original =
13
- await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
14
- return { ...original };
15
- });
16
-
17
- // ── helpers ────────────────────────────────────────────────────────────────
18
-
19
- function makeSkillEntry(
20
- overrides: Partial<SkillPromptEntry> = {},
21
- ): SkillPromptEntry {
22
- return {
23
- name: "librarian",
24
- description: "Research skills",
25
- location: "/skills/librarian/SKILL.md",
26
- state: "ask",
27
- normalizedLocation: "/skills/librarian/SKILL.md",
28
- normalizedBaseDir: "/skills/librarian",
29
- ...overrides,
30
- };
31
- }
32
-
33
- function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
34
- return {
35
- toolName: "read",
36
- agentName: null,
37
- input: { path: "/skills/librarian/SKILL.md" },
38
- toolCallId: "tc-1",
39
- cwd: "/test/project",
40
- ...overrides,
41
- };
42
- }
43
-
44
- // ── tests ──────────────────────────────────────────────────────────────────
45
-
46
- describe("describeSkillReadGate", () => {
47
- it("returns null when tool is not read", () => {
48
- const result = describeSkillReadGate(
49
- makeTcc({ toolName: "write" }),
50
- normalizer,
51
- () => [makeSkillEntry()],
52
- );
53
- expect(result).toBeNull();
54
- });
55
-
56
- it("returns null when no active skill entries", () => {
57
- const result = describeSkillReadGate(makeTcc(), normalizer, () => []);
58
- expect(result).toBeNull();
59
- });
60
-
61
- it("returns null when read path does not match any skill", () => {
62
- const result = describeSkillReadGate(
63
- makeTcc({ input: { path: "/test/project/src/index.ts" } }),
64
- normalizer,
65
- () => [makeSkillEntry()],
66
- );
67
- expect(result).toBeNull();
68
- });
69
-
70
- it("returns null when input has no path", () => {
71
- const result = describeSkillReadGate(
72
- makeTcc({ input: {} }),
73
- normalizer,
74
- () => [makeSkillEntry()],
75
- );
76
- expect(result).toBeNull();
77
- });
78
-
79
- it("returns GateDescriptor with preResolved.state matching skill entry state (ask)", () => {
80
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
81
- makeSkillEntry({ state: "ask" }),
82
- ]);
83
- expect(result).not.toBeNull();
84
- const desc = result!;
85
- expect(desc.preResolved).toEqual({ state: "ask" });
86
- });
87
-
88
- it("returns GateDescriptor with preResolved.state matching skill entry state (allow)", () => {
89
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
90
- makeSkillEntry({ state: "allow" }),
91
- ]);
92
- expect(result).not.toBeNull();
93
- const desc = result!;
94
- expect(desc.preResolved).toEqual({ state: "allow" });
95
- });
96
-
97
- it("returns GateDescriptor with preResolved.state matching skill entry state (deny)", () => {
98
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
99
- makeSkillEntry({ state: "deny" }),
100
- ]);
101
- expect(result).not.toBeNull();
102
- const desc = result!;
103
- expect(desc.preResolved).toEqual({ state: "deny" });
104
- });
105
-
106
- it("decision surface is 'skill' and decision value is the skill name", () => {
107
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
108
- makeSkillEntry({ name: "my-skill" }),
109
- ])!;
110
- expect(result.decision.surface).toBe("skill");
111
- expect(result.decision.value).toBe("my-skill");
112
- });
113
-
114
- it("denialContext contains the skill name and read path", () => {
115
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
116
- makeSkillEntry({ name: "librarian" }),
117
- ])!;
118
- expect(result.denialContext).toEqual({
119
- kind: "skill_read",
120
- skillName: "librarian",
121
- readPath: "/skills/librarian/SKILL.md",
122
- agentName: undefined,
123
- });
124
- });
125
-
126
- it("promptDetails includes skill_read source and skillName", () => {
127
- const result = describeSkillReadGate(
128
- makeTcc({ agentName: "test-agent", toolCallId: "tc-42" }),
129
- normalizer,
130
- () => [makeSkillEntry({ name: "my-skill" })],
131
- )!;
132
- expect(result.promptDetails).toMatchObject({
133
- source: "skill_read",
134
- agentName: "test-agent",
135
- toolCallId: "tc-42",
136
- toolName: "read",
137
- skillName: "my-skill",
138
- });
139
- expect(result.promptDetails.message).toBeDefined();
140
- });
141
-
142
- it("logContext includes skill_read source and skillName", () => {
143
- const result = describeSkillReadGate(
144
- makeTcc({ agentName: "agent-1" }),
145
- normalizer,
146
- () => [makeSkillEntry({ name: "librarian" })],
147
- )!;
148
- expect(result.logContext).toMatchObject({
149
- source: "skill_read",
150
- skillName: "librarian",
151
- agentName: "agent-1",
152
- });
153
- });
154
-
155
- it("surface is 'skill' on the descriptor", () => {
156
- const result = describeSkillReadGate(makeTcc(), normalizer, () => [
157
- makeSkillEntry(),
158
- ])!;
159
- expect(result.surface).toBe("skill");
160
- });
161
- });
@@ -1,356 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- import type { AccessPath } from "#src/access-intent/access-path";
4
- import { ToolCallGatePipeline } from "#src/handlers/gates/tool-call-gate-pipeline";
5
- import { PathNormalizer } from "#src/path-normalizer";
6
-
7
- import {
8
- makeGateInputs,
9
- makeGateRunner,
10
- makeResolver,
11
- makeTcc,
12
- } from "#test/helpers/gate-fixtures";
13
- import { makeCheckResult } from "#test/helpers/handler-fixtures";
14
-
15
- // ── BashProgram.parse mock ─────────────────────────────────────────────────
16
-
17
- const { mockBashProgramParse } = vi.hoisted(() => ({
18
- mockBashProgramParse: vi.fn(),
19
- }));
20
-
21
- vi.mock("#src/access-intent/bash/program", () => ({
22
- BashProgram: { parse: mockBashProgramParse },
23
- }));
24
-
25
- // Mock node:fs so realpathSync (used by canonicalizePath) is controllable for
26
- // the per-tool symlink-resolution test. Default implementation is identity.
27
- const realpathSync = vi.hoisted(() =>
28
- vi.fn<(path: string) => string>((p) => p),
29
- );
30
- vi.mock("node:fs", () => ({
31
- realpathSync,
32
- default: { realpathSync },
33
- }));
34
-
35
- function makeMockBashProgram() {
36
- return {
37
- commands: vi.fn<() => []>(() => []),
38
- pathRuleCandidates: vi.fn<() => []>(() => []),
39
- externalPaths: vi.fn<() => AccessPath[]>(() => []),
40
- };
41
- }
42
-
43
- // ── ToolCallGatePipeline ───────────────────────────────────────────────────
44
-
45
- describe("ToolCallGatePipeline", () => {
46
- beforeEach(() => {
47
- mockBashProgramParse.mockReset();
48
- mockBashProgramParse.mockResolvedValue(makeMockBashProgram());
49
- realpathSync.mockReset();
50
- realpathSync.mockImplementation((p: string) => p);
51
- });
52
-
53
- // ── non-bash tools ───────────────────────────────────────────────────────
54
-
55
- describe("evaluate — non-bash tool", () => {
56
- it("returns allow when all gates pass", async () => {
57
- const resolver = makeResolver(makeCheckResult());
58
- const inputs = makeGateInputs();
59
- const { runner } = makeGateRunner();
60
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
61
-
62
- const result = await pipeline.evaluate(
63
- makeTcc({ toolName: "read", input: {} }),
64
- runner,
65
- );
66
-
67
- expect(result).toEqual({ action: "allow" });
68
- });
69
-
70
- it("returns block when the tool gate denies", async () => {
71
- const resolver = makeResolver(
72
- makeCheckResult({ state: "deny", matchedPattern: "*" }),
73
- );
74
- const inputs = makeGateInputs();
75
- const { runner } = makeGateRunner();
76
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
77
-
78
- const result = await pipeline.evaluate(
79
- makeTcc({ toolName: "read", input: {} }),
80
- runner,
81
- );
82
-
83
- expect(result).toMatchObject({ action: "block" });
84
- });
85
-
86
- it("short-circuits after the first blocking gate without evaluating later ones", async () => {
87
- const resolver = makeResolver(makeCheckResult());
88
- const inputs = makeGateInputs();
89
- const { runner } = makeGateRunner();
90
- const runSpy = vi
91
- .spyOn(runner, "run")
92
- .mockResolvedValue({ action: "block", reason: "first gate blocked" });
93
-
94
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
95
- const result = await pipeline.evaluate(
96
- makeTcc({ toolName: "read", input: {} }),
97
- runner,
98
- );
99
-
100
- expect(result).toEqual({ action: "block", reason: "first gate blocked" });
101
- // Pipeline looped to the first gate, got block, and stopped — not all 6 gates.
102
- expect(runSpy).toHaveBeenCalledTimes(1);
103
- });
104
-
105
- it("calls getToolPreviewLimits() during evaluate", async () => {
106
- const getToolPreviewLimits = vi.fn(() => ({
107
- toolInputPreviewMaxLength: 500,
108
- toolTextSummaryMaxLength: 100,
109
- toolInputLogPreviewMaxLength: 200,
110
- }));
111
- const resolver = makeResolver(makeCheckResult());
112
- const inputs = makeGateInputs({ getToolPreviewLimits });
113
- const { runner } = makeGateRunner();
114
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
115
-
116
- await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
117
-
118
- expect(getToolPreviewLimits).toHaveBeenCalled();
119
- });
120
-
121
- it("calls getInfrastructureReadDirs() during evaluate", async () => {
122
- const getInfrastructureReadDirs = vi.fn<() => string[]>(() => []);
123
- const resolver = makeResolver(makeCheckResult());
124
- const inputs = makeGateInputs({ getInfrastructureReadDirs });
125
- const { runner } = makeGateRunner();
126
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
127
-
128
- await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
129
-
130
- expect(getInfrastructureReadDirs).toHaveBeenCalled();
131
- });
132
-
133
- it("calls getActiveSkillEntries() during evaluate", async () => {
134
- const getActiveSkillEntries = vi.fn<() => []>(() => []);
135
- const resolver = makeResolver(makeCheckResult());
136
- const inputs = makeGateInputs({ getActiveSkillEntries });
137
- const { runner } = makeGateRunner();
138
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
139
-
140
- await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
141
-
142
- expect(getActiveSkillEntries).toHaveBeenCalled();
143
- });
144
-
145
- it("does not call BashProgram.parse for non-bash tools", async () => {
146
- const resolver = makeResolver(makeCheckResult());
147
- const inputs = makeGateInputs();
148
- const { runner } = makeGateRunner();
149
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
150
-
151
- await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
152
-
153
- expect(mockBashProgramParse).not.toHaveBeenCalled();
154
- });
155
- });
156
-
157
- // ── bash tool ────────────────────────────────────────────────────────────
158
-
159
- describe("evaluate — bash tool", () => {
160
- it("returns allow when the bash command is permitted", async () => {
161
- const resolver = makeResolver(makeCheckResult());
162
- const inputs = makeGateInputs();
163
- const { runner } = makeGateRunner();
164
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
165
-
166
- const result = await pipeline.evaluate(
167
- makeTcc({ toolName: "bash", input: { command: "echo hello" } }),
168
- runner,
169
- );
170
-
171
- expect(result).toEqual({ action: "allow" });
172
- });
173
-
174
- it("parses BashProgram exactly once per evaluate for bash tools with a command", async () => {
175
- const resolver = makeResolver(makeCheckResult());
176
- const inputs = makeGateInputs();
177
- const { runner } = makeGateRunner();
178
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
179
-
180
- await pipeline.evaluate(
181
- makeTcc({ toolName: "bash", input: { command: "echo hello" } }),
182
- runner,
183
- );
184
-
185
- expect(mockBashProgramParse).toHaveBeenCalledTimes(1);
186
- expect(mockBashProgramParse).toHaveBeenCalledWith(
187
- "echo hello",
188
- expect.any(PathNormalizer),
189
- expect.any(Function),
190
- );
191
- });
192
-
193
- it("does not parse BashProgram when the bash command is empty", async () => {
194
- const resolver = makeResolver(makeCheckResult());
195
- const inputs = makeGateInputs();
196
- const { runner } = makeGateRunner();
197
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
198
-
199
- await pipeline.evaluate(
200
- makeTcc({ toolName: "bash", input: { command: "" } }),
201
- runner,
202
- );
203
-
204
- expect(mockBashProgramParse).not.toHaveBeenCalled();
205
- });
206
-
207
- it("passes the session's promotable path-token matcher into BashProgram.parse (#509)", async () => {
208
- const resolver = makeResolver(makeCheckResult());
209
- const isPromotable = vi.fn((token: string) => token === "id_rsa");
210
- const getPromotablePathTokenMatcher = vi.fn(() => isPromotable);
211
- const inputs = makeGateInputs({ getPromotablePathTokenMatcher });
212
- const { runner } = makeGateRunner();
213
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
214
-
215
- await pipeline.evaluate(
216
- makeTcc({
217
- toolName: "bash",
218
- input: { command: "cat id_rsa" },
219
- agentName: "my-agent",
220
- }),
221
- runner,
222
- );
223
-
224
- expect(getPromotablePathTokenMatcher).toHaveBeenCalledWith("my-agent");
225
- expect(mockBashProgramParse).toHaveBeenCalledWith(
226
- "cat id_rsa",
227
- expect.any(PathNormalizer),
228
- isPromotable,
229
- );
230
- });
231
- });
232
-
233
- // ── customExtractors threading (#352) ────────────────────────────────────
234
-
235
- describe("evaluate — customExtractors threading (#352)", () => {
236
- // Deny only the cross-cutting `path` surface; allow everything else, so a
237
- // block can only come from the path gate seeing the extracted path.
238
- function pathDenyingResolver() {
239
- const resolver = makeResolver();
240
- resolver.resolve.mockImplementation((intent) =>
241
- intent.surface === "path"
242
- ? makeCheckResult({ state: "deny", matchedPattern: "*" })
243
- : makeCheckResult(),
244
- );
245
- return resolver;
246
- }
247
-
248
- const extractors = {
249
- get: (name: string) =>
250
- name === "ffgrep"
251
- ? (input: Record<string, unknown>) =>
252
- typeof input.target === "string" ? input.target : undefined
253
- : undefined,
254
- };
255
-
256
- it("forwards extractors so a custom-shaped tool is path-gated", async () => {
257
- const resolver = pathDenyingResolver();
258
- const inputs = makeGateInputs();
259
- const { runner } = makeGateRunner();
260
- const pipeline = new ToolCallGatePipeline(
261
- resolver,
262
- inputs,
263
- undefined,
264
- extractors,
265
- );
266
-
267
- const result = await pipeline.evaluate(
268
- makeTcc({
269
- toolName: "ffgrep",
270
- input: { target: "/test/project/secret.env" },
271
- }),
272
- runner,
273
- );
274
-
275
- expect(result).toMatchObject({ action: "block" });
276
- });
277
-
278
- it("without extractors the custom-shaped tool is not path-gated", async () => {
279
- const resolver = pathDenyingResolver();
280
- const inputs = makeGateInputs();
281
- const { runner } = makeGateRunner();
282
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
283
-
284
- const result = await pipeline.evaluate(
285
- makeTcc({
286
- toolName: "ffgrep",
287
- input: { target: "/test/project/secret.env" },
288
- }),
289
- runner,
290
- );
291
-
292
- expect(result).toEqual({ action: "allow" });
293
- });
294
- });
295
-
296
- // ── per-tool path-bearing gate (#502) ────────────────────────────────────
297
-
298
- describe("evaluate — per-tool path-bearing gate (#502)", () => {
299
- it("emits an access-path intent on the tool-name surface for a path-bearing tool", async () => {
300
- const resolver = makeResolver(makeCheckResult());
301
- const inputs = makeGateInputs();
302
- const { runner } = makeGateRunner();
303
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
304
-
305
- await pipeline.evaluate(
306
- makeTcc({ toolName: "read", input: { path: "/test/cwd/foo.ts" } }),
307
- runner,
308
- );
309
-
310
- const perTool = resolver.resolve.mock.calls.find(
311
- ([intent]) => intent.surface === "read",
312
- );
313
- expect(perTool?.[0].kind).toBe("access-path");
314
- });
315
-
316
- it("keeps a path-bearing tool with no path on the tool intent", async () => {
317
- const resolver = makeResolver(makeCheckResult());
318
- const inputs = makeGateInputs();
319
- const { runner } = makeGateRunner();
320
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
321
-
322
- await pipeline.evaluate(makeTcc({ toolName: "read", input: {} }), runner);
323
-
324
- const perTool = resolver.resolve.mock.calls.find(
325
- ([intent]) => intent.surface === "read",
326
- );
327
- expect(perTool?.[0].kind).toBe("tool");
328
- });
329
-
330
- it("blocks when a per-tool rule matches the symlink-resolved form", async () => {
331
- // /test/cwd/foo.env is a symlink to /vault/foo.env; the per-tool rule is
332
- // keyed on the resolved target, which is only reachable via matchValues().
333
- realpathSync.mockImplementation((p: string) =>
334
- p === "/test/cwd/foo.env" ? "/vault/foo.env" : p,
335
- );
336
- const resolver = makeResolver();
337
- resolver.resolve.mockImplementation((intent) =>
338
- intent.kind === "access-path" &&
339
- intent.surface === "read" &&
340
- intent.path.matchValues().includes("/vault/foo.env")
341
- ? makeCheckResult({ state: "deny", matchedPattern: "*.env" })
342
- : makeCheckResult(),
343
- );
344
- const inputs = makeGateInputs();
345
- const { runner } = makeGateRunner();
346
- const pipeline = new ToolCallGatePipeline(resolver, inputs);
347
-
348
- const result = await pipeline.evaluate(
349
- makeTcc({ toolName: "read", input: { path: "/test/cwd/foo.env" } }),
350
- runner,
351
- );
352
-
353
- expect(result).toMatchObject({ action: "block" });
354
- });
355
- });
356
- });