@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,116 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import type { MergedScopes } from "#src/scope-merge";
3
- import { mergeScopesWithOrigins } from "#src/scope-merge";
4
-
5
- describe("mergeScopesWithOrigins", () => {
6
- it("returns empty result for empty scopes array", () => {
7
- const result: MergedScopes = mergeScopesWithOrigins([]);
8
- expect(result.mergedPermission).toEqual({});
9
- expect(result.origins.size).toBe(0);
10
- });
11
-
12
- it("attributes a string surface value to the contributing scope via the '*' pattern", () => {
13
- const result = mergeScopesWithOrigins([
14
- ["global", { permission: { bash: "allow" } }],
15
- ]);
16
- expect(result.mergedPermission).toEqual({ bash: "allow" });
17
- expect(result.origins.get("bash")?.get("*")).toBe("global");
18
- });
19
-
20
- it("attributes each pattern of an object surface value to the contributing scope", () => {
21
- const result = mergeScopesWithOrigins([
22
- [
23
- "project",
24
- { permission: { bash: { "git *": "allow", "npm *": "deny" } } },
25
- ],
26
- ]);
27
- expect(result.mergedPermission).toEqual({
28
- bash: { "git *": "allow", "npm *": "deny" },
29
- });
30
- expect(result.origins.get("bash")?.get("git *")).toBe("project");
31
- expect(result.origins.get("bash")?.get("npm *")).toBe("project");
32
- });
33
-
34
- it(
35
- "shallow-merge: patterns not redefined by the higher scope keep their lower-scope origin;" +
36
- " patterns the higher scope defines switch to the higher scope",
37
- () => {
38
- const result = mergeScopesWithOrigins([
39
- [
40
- "global",
41
- { permission: { bash: { "ls *": "allow", "git *": "allow" } } },
42
- ],
43
- ["project", { permission: { bash: { "git *": "deny" } } }],
44
- ]);
45
- expect(result.mergedPermission).toEqual({
46
- bash: { "ls *": "allow", "git *": "deny" },
47
- });
48
- // "ls *" was not touched by project — retains global attribution
49
- expect(result.origins.get("bash")?.get("ls *")).toBe("global");
50
- // "git *" was overridden by project — switches to project attribution
51
- expect(result.origins.get("bash")?.get("git *")).toBe("project");
52
- },
53
- );
54
-
55
- it("full replacement (string over object): higher scope re-attributes the entire surface to its own origin", () => {
56
- const result = mergeScopesWithOrigins([
57
- ["global", { permission: { bash: { "ls *": "allow" } } }],
58
- ["project", { permission: { bash: "deny" } }],
59
- ]);
60
- expect(result.mergedPermission).toEqual({ bash: "deny" });
61
- // The string value produces a single "*" pattern for the replacing scope
62
- expect(result.origins.get("bash")?.get("*")).toBe("project");
63
- // The former "ls *" pattern from global is gone — origins are replaced, not merged
64
- expect(result.origins.get("bash")?.has("ls *")).toBe(false);
65
- });
66
-
67
- it("full replacement (object over string): higher scope re-attributes the entire surface to its own origin", () => {
68
- const result = mergeScopesWithOrigins([
69
- ["global", { permission: { bash: "ask" } }],
70
- ["project", { permission: { bash: { "git *": "deny" } } }],
71
- ]);
72
- expect(result.mergedPermission).toEqual({ bash: { "git *": "deny" } });
73
- // The object value attributes each pattern to the replacing scope
74
- expect(result.origins.get("bash")?.get("git *")).toBe("project");
75
- // The former "*" attribution from global is gone
76
- expect(result.origins.get("bash")?.has("*")).toBe(false);
77
- });
78
-
79
- it("applies four-scope precedence in lowest→highest order (global → project → agent → project-agent)", () => {
80
- const result = mergeScopesWithOrigins([
81
- ["global", { permission: { read: "ask" } }],
82
- ["project", { permission: { write: "deny" } }],
83
- ["agent", { permission: { bash: "deny" } }],
84
- ["project-agent", { permission: { mcp: "allow" } }],
85
- ]);
86
- expect(result.mergedPermission).toEqual({
87
- read: "ask",
88
- write: "deny",
89
- bash: "deny",
90
- mcp: "allow",
91
- });
92
- expect(result.origins.get("read")?.get("*")).toBe("global");
93
- expect(result.origins.get("write")?.get("*")).toBe("project");
94
- expect(result.origins.get("bash")?.get("*")).toBe("agent");
95
- expect(result.origins.get("mcp")?.get("*")).toBe("project-agent");
96
- });
97
-
98
- it("skips scopes with no permission key, contributing nothing to either map", () => {
99
- const result = mergeScopesWithOrigins([
100
- ["global", {}],
101
- ["project", { permission: { bash: "allow" } }],
102
- ]);
103
- expect(result.mergedPermission).toEqual({ bash: "allow" });
104
- expect(result.origins.get("bash")?.get("*")).toBe("project");
105
- });
106
-
107
- it("attributes the universal '*' surface like any other (downstream reads origins.get('*')?.get('*') for universalFallbackOrigin)", () => {
108
- const result = mergeScopesWithOrigins([
109
- ["global", { permission: { "*": "deny" } }],
110
- ["project", { permission: { "*": "allow" } }],
111
- ]);
112
- expect(result.mergedPermission).toEqual({ "*": "allow" });
113
- // Both scopes write a string — each is a full replacement; project wins last
114
- expect(result.origins.get("*")?.get("*")).toBe("project");
115
- });
116
- });
@@ -1,163 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { PermissionsService } from "#src/service";
3
- import {
4
- PermissionServiceLifecycle,
5
- type ServiceLifecycle,
6
- } from "#src/service-lifecycle";
7
- import type { SubagentSessionRegistry } from "#src/subagent-registry";
8
-
9
- import { makeCtx } from "#test/helpers/handler-fixtures";
10
-
11
- // ── module stubs ───────────────────────────────────────────────────────────
12
-
13
- const mockIsRegisteredSubagentChild = vi.hoisted(() =>
14
- vi.fn<(ctx: unknown, registry: unknown) => boolean>().mockReturnValue(false),
15
- );
16
- const mockPublishPermissionsService = vi.hoisted(() => vi.fn<() => void>());
17
- const mockUnpublishPermissionsService = vi.hoisted(() => vi.fn<() => void>());
18
- const mockEmitReadyEvent = vi.hoisted(() => vi.fn<() => void>());
19
-
20
- vi.mock("#src/subagent-context", () => ({
21
- isRegisteredSubagentChild: mockIsRegisteredSubagentChild,
22
- }));
23
- vi.mock("#src/service", () => ({
24
- publishPermissionsService: mockPublishPermissionsService,
25
- unpublishPermissionsService: mockUnpublishPermissionsService,
26
- }));
27
- vi.mock("#src/permission-events", () => ({
28
- emitReadyEvent: mockEmitReadyEvent,
29
- }));
30
-
31
- // ── helpers ────────────────────────────────────────────────────────────────
32
-
33
- function makeService(): PermissionsService {
34
- return {
35
- checkPermission: vi.fn(),
36
- getToolPermission: vi.fn(),
37
- registerToolInputFormatter: vi.fn(),
38
- registerToolAccessExtractor: vi.fn(),
39
- };
40
- }
41
-
42
- function makeRegistry(): SubagentSessionRegistry {
43
- return {
44
- has: vi.fn().mockReturnValue(false),
45
- } as unknown as SubagentSessionRegistry;
46
- }
47
-
48
- function makeLifecycle(overrides?: { subscriptions?: (() => void)[] }) {
49
- const service = makeService();
50
- const registry = makeRegistry();
51
- const events = { emit: vi.fn(), on: vi.fn() };
52
- const subscriptions = overrides?.subscriptions ?? [];
53
- const lifecycle = new PermissionServiceLifecycle(
54
- service,
55
- registry,
56
- events,
57
- subscriptions,
58
- );
59
- return { lifecycle, service, registry, events, subscriptions };
60
- }
61
-
62
- beforeEach(() => {
63
- mockIsRegisteredSubagentChild.mockReset();
64
- mockIsRegisteredSubagentChild.mockReturnValue(false);
65
- mockPublishPermissionsService.mockReset();
66
- mockUnpublishPermissionsService.mockReset();
67
- mockEmitReadyEvent.mockReset();
68
- });
69
-
70
- // ── ServiceLifecycle interface shape ──────────────────────────────────────
71
-
72
- it("PermissionServiceLifecycle satisfies ServiceLifecycle", () => {
73
- const { lifecycle } = makeLifecycle();
74
- const _: ServiceLifecycle = lifecycle;
75
- expect(_).toBeDefined();
76
- });
77
-
78
- // ── activate ──────────────────────────────────────────────────────────────
79
-
80
- describe("activate", () => {
81
- it("publishes the service for a non-child session", () => {
82
- const ctx = makeCtx();
83
- const { lifecycle, service } = makeLifecycle();
84
- mockIsRegisteredSubagentChild.mockReturnValue(false);
85
- lifecycle.activate(ctx);
86
- expect(mockPublishPermissionsService).toHaveBeenCalledWith(service);
87
- });
88
-
89
- it("skips publishing for a registered child session", () => {
90
- const ctx = makeCtx();
91
- const { lifecycle } = makeLifecycle();
92
- mockIsRegisteredSubagentChild.mockReturnValue(true);
93
- lifecycle.activate(ctx);
94
- expect(mockPublishPermissionsService).not.toHaveBeenCalled();
95
- });
96
-
97
- it("always emits the ready event, even for a child session", () => {
98
- const ctx = makeCtx();
99
- const { lifecycle, events } = makeLifecycle();
100
- mockIsRegisteredSubagentChild.mockReturnValue(true);
101
- lifecycle.activate(ctx);
102
- expect(mockEmitReadyEvent).toHaveBeenCalledWith(events);
103
- });
104
-
105
- it("emits ready after publishing the service", () => {
106
- const ctx = makeCtx();
107
- const order: string[] = [];
108
- mockPublishPermissionsService.mockImplementation(() =>
109
- order.push("publish"),
110
- );
111
- mockEmitReadyEvent.mockImplementation(() => order.push("ready"));
112
- const { lifecycle } = makeLifecycle();
113
- lifecycle.activate(ctx);
114
- expect(order).toEqual(["publish", "ready"]);
115
- });
116
-
117
- it("passes ctx and registry to isRegisteredSubagentChild", () => {
118
- const ctx = makeCtx();
119
- const { lifecycle, registry } = makeLifecycle();
120
- lifecycle.activate(ctx);
121
- expect(mockIsRegisteredSubagentChild).toHaveBeenCalledWith(ctx, registry);
122
- });
123
- });
124
-
125
- // ── teardown ──────────────────────────────────────────────────────────────
126
-
127
- describe("teardown", () => {
128
- it("calls each subscription unsubscribe function", () => {
129
- const unsub1 = vi.fn();
130
- const unsub2 = vi.fn();
131
- const unsub3 = vi.fn();
132
- const { lifecycle } = makeLifecycle({
133
- subscriptions: [unsub1, unsub2, unsub3],
134
- });
135
- lifecycle.teardown();
136
- expect(unsub1).toHaveBeenCalledOnce();
137
- expect(unsub2).toHaveBeenCalledOnce();
138
- expect(unsub3).toHaveBeenCalledOnce();
139
- });
140
-
141
- it("unpublishes the service after running subscriptions", () => {
142
- const order: string[] = [];
143
- const unsub = vi.fn(() => order.push("unsub"));
144
- mockUnpublishPermissionsService.mockImplementation(() =>
145
- order.push("unpublish"),
146
- );
147
- const { lifecycle } = makeLifecycle({ subscriptions: [unsub] });
148
- lifecycle.teardown();
149
- expect(order).toEqual(["unsub", "unpublish"]);
150
- });
151
-
152
- it("passes the service to unpublishPermissionsService", () => {
153
- const { lifecycle, service } = makeLifecycle();
154
- lifecycle.teardown();
155
- expect(mockUnpublishPermissionsService).toHaveBeenCalledWith(service);
156
- });
157
-
158
- it("works with no subscriptions", () => {
159
- const { lifecycle } = makeLifecycle({ subscriptions: [] });
160
- expect(() => lifecycle.teardown()).not.toThrow();
161
- expect(mockUnpublishPermissionsService).toHaveBeenCalledOnce();
162
- });
163
- });
@@ -1,261 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
- import type { AccessIntent } from "#src/access-intent/access-intent";
3
- import { PathNormalizer } from "#src/path-normalizer";
4
- import { LocalPermissionsService } from "#src/permissions-service";
5
- import type { PermissionsService } from "#src/service";
6
- import {
7
- getPermissionsService,
8
- publishPermissionsService,
9
- unpublishPermissionsService,
10
- } from "#src/service";
11
- import { ToolAccessExtractorRegistry } from "#src/tool-access-extractor-registry";
12
- import { ToolInputFormatterRegistry } from "#src/tool-input-formatter-registry";
13
- import type { PermissionCheckResult, PermissionState } from "#src/types";
14
-
15
- // ── helpers ────────────────────────────────────────────────────────────────
16
-
17
- function makeService(
18
- overrides: Partial<PermissionsService> = {},
19
- ): PermissionsService {
20
- return {
21
- checkPermission: vi.fn(),
22
- getToolPermission: vi.fn(),
23
- registerToolInputFormatter: vi.fn(),
24
- registerToolAccessExtractor: vi.fn(),
25
- ...overrides,
26
- };
27
- }
28
-
29
- // ── globalThis accessor ────────────────────────────────────────────────────
30
-
31
- describe("globalThis accessor", () => {
32
- afterEach(() => {
33
- const current = getPermissionsService();
34
- if (current) {
35
- unpublishPermissionsService(current);
36
- }
37
- });
38
-
39
- it("returns undefined when nothing has been published", () => {
40
- expect(getPermissionsService()).toBeUndefined();
41
- });
42
-
43
- it("returns the published service", () => {
44
- const service = makeService();
45
- publishPermissionsService(service);
46
- expect(getPermissionsService()).toBe(service);
47
- });
48
-
49
- it("overwrites a previously published service", () => {
50
- const first = makeService();
51
- const second = makeService();
52
- publishPermissionsService(first);
53
- publishPermissionsService(second);
54
- expect(getPermissionsService()).toBe(second);
55
- });
56
-
57
- it("removes the slot when it still holds the given service", () => {
58
- const service = makeService();
59
- publishPermissionsService(service);
60
- unpublishPermissionsService(service);
61
- expect(getPermissionsService()).toBeUndefined();
62
- });
63
-
64
- it("does not remove the slot when a different service occupies it", () => {
65
- const parent = makeService();
66
- const child = makeService();
67
- publishPermissionsService(parent);
68
- // A child instance never published `parent`; unpublishing its own service
69
- // must be a no-op that leaves the parent's slot intact.
70
- unpublishPermissionsService(child);
71
- expect(getPermissionsService()).toBe(parent);
72
- });
73
-
74
- it("unpublish is safe to call when nothing was published", () => {
75
- expect(() => unpublishPermissionsService(makeService())).not.toThrow();
76
- expect(getPermissionsService()).toBeUndefined();
77
- });
78
- });
79
-
80
- // ── service adapter delegation ─────────────────────────────────────────────
81
-
82
- describe("service round-trip through the global slot", () => {
83
- afterEach(() => {
84
- const current = getPermissionsService();
85
- if (current) {
86
- unpublishPermissionsService(current);
87
- }
88
- });
89
-
90
- const fakeResult: PermissionCheckResult = {
91
- toolName: "bash",
92
- state: "allow",
93
- matchedPattern: "git *",
94
- source: "bash",
95
- origin: "global",
96
- };
97
-
98
- function makeResolver() {
99
- return {
100
- resolve: vi
101
- .fn<(intent: AccessIntent) => PermissionCheckResult>()
102
- .mockReturnValue(fakeResult),
103
- getToolPermission: vi
104
- .fn<(toolName: string, agentName?: string) => PermissionState>()
105
- .mockReturnValue("ask"),
106
- };
107
- }
108
-
109
- function publishLocalService(resolver: ReturnType<typeof makeResolver>) {
110
- publishPermissionsService(
111
- new LocalPermissionsService(
112
- resolver,
113
- {
114
- getPathNormalizer: () => new PathNormalizer("linux", "/test/project"),
115
- },
116
- new ToolInputFormatterRegistry(),
117
- new ToolAccessExtractorRegistry(),
118
- ),
119
- );
120
- }
121
-
122
- it("resolves a non-path query via a tool intent", () => {
123
- const resolver = makeResolver();
124
- publishLocalService(resolver);
125
- const result = getPermissionsService()!.checkPermission(
126
- "bash",
127
- "git push",
128
- "Explore",
129
- );
130
- expect(result).toBe(fakeResult);
131
- expect(resolver.resolve).toHaveBeenCalledWith({
132
- kind: "tool",
133
- surface: "bash",
134
- input: { command: "git push" },
135
- agentName: "Explore",
136
- });
137
- });
138
-
139
- it("resolves a path-surface query via an access-path intent", () => {
140
- const resolver = makeResolver();
141
- publishLocalService(resolver);
142
- getPermissionsService()!.checkPermission("read", "/test/project/.env");
143
- const intent = resolver.resolve.mock.calls[0][0];
144
- expect(intent.kind).toBe("access-path");
145
- if (intent.kind === "access-path") {
146
- expect(intent.surface).toBe("read");
147
- }
148
- });
149
-
150
- it("delegates getToolPermission through the resolver", () => {
151
- const resolver = makeResolver();
152
- resolver.getToolPermission.mockReturnValue("deny");
153
- publishLocalService(resolver);
154
- const result = getPermissionsService()!.getToolPermission(
155
- "write",
156
- "Explore",
157
- );
158
- expect(result).toBe("deny");
159
- expect(resolver.getToolPermission).toHaveBeenCalledWith("write", "Explore");
160
- });
161
- });
162
-
163
- // ── registerToolInputFormatter delegation ─────────────────────────────────
164
-
165
- describe("registerToolInputFormatter delegation", () => {
166
- afterEach(() => {
167
- const current = getPermissionsService();
168
- if (current) {
169
- unpublishPermissionsService(current);
170
- }
171
- });
172
-
173
- it("delegates to the registry and returns its disposer", () => {
174
- const registry = new ToolInputFormatterRegistry();
175
- const formatter = () => "preview";
176
-
177
- const service = makeService({
178
- registerToolInputFormatter(toolName, fmt) {
179
- return registry.register(toolName, fmt);
180
- },
181
- });
182
-
183
- publishPermissionsService(service);
184
- const dispose = getPermissionsService()!.registerToolInputFormatter(
185
- "my-tool",
186
- formatter,
187
- );
188
-
189
- // Registry received the registration
190
- expect(registry.get("my-tool")).toBe(formatter);
191
-
192
- // Disposer returned from service removes it from the registry
193
- dispose();
194
- expect(registry.get("my-tool")).toBeUndefined();
195
- });
196
-
197
- it("throws when a formatter is already registered for the tool name", () => {
198
- const registry = new ToolInputFormatterRegistry();
199
- registry.register("my-tool", () => undefined);
200
-
201
- const service = makeService({
202
- registerToolInputFormatter(toolName, fmt) {
203
- return registry.register(toolName, fmt);
204
- },
205
- });
206
-
207
- publishPermissionsService(service);
208
- expect(() =>
209
- getPermissionsService()!.registerToolInputFormatter("my-tool", () => ""),
210
- ).toThrow("my-tool");
211
- });
212
- });
213
-
214
- // ── registerToolAccessExtractor delegation (#352) ────────────────────────
215
-
216
- describe("registerToolAccessExtractor delegation", () => {
217
- afterEach(() => {
218
- const current = getPermissionsService();
219
- if (current) {
220
- unpublishPermissionsService(current);
221
- }
222
- });
223
-
224
- it("delegates to the registry and returns its disposer", () => {
225
- const registry = new ToolAccessExtractorRegistry();
226
- const extractor = () => "/etc/hosts";
227
-
228
- const service = makeService({
229
- registerToolAccessExtractor(toolName, ext) {
230
- return registry.register(toolName, ext);
231
- },
232
- });
233
-
234
- publishPermissionsService(service);
235
- const dispose = getPermissionsService()!.registerToolAccessExtractor(
236
- "ffgrep",
237
- extractor,
238
- );
239
-
240
- expect(registry.get("ffgrep")).toBe(extractor);
241
-
242
- dispose();
243
- expect(registry.get("ffgrep")).toBeUndefined();
244
- });
245
-
246
- it("throws when an extractor is already registered for the tool name", () => {
247
- const registry = new ToolAccessExtractorRegistry();
248
- registry.register("ffgrep", () => undefined);
249
-
250
- const service = makeService({
251
- registerToolAccessExtractor(toolName, ext) {
252
- return registry.register(toolName, ext);
253
- },
254
- });
255
-
256
- publishPermissionsService(service);
257
- expect(() =>
258
- getPermissionsService()!.registerToolAccessExtractor("ffgrep", () => ""),
259
- ).toThrow("ffgrep");
260
- });
261
- });
@@ -1,75 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { SessionApproval } from "#src/session-approval";
4
-
5
- describe("SessionApproval", () => {
6
- describe("single", () => {
7
- it("stores surface and one pattern", () => {
8
- const approval = SessionApproval.single("bash", "git *");
9
- expect(approval.surface).toBe("bash");
10
- expect(approval.patterns).toEqual(["git *"]);
11
- });
12
-
13
- it("representativePattern returns the pattern", () => {
14
- const approval = SessionApproval.single("bash", "git *");
15
- expect(approval.representativePattern).toBe("git *");
16
- });
17
-
18
- it("toGateApproval returns { surface, pattern }", () => {
19
- const approval = SessionApproval.single("bash", "git *");
20
- expect(approval.toGateApproval()).toEqual({
21
- surface: "bash",
22
- pattern: "git *",
23
- });
24
- });
25
- });
26
-
27
- describe("multiple", () => {
28
- it("stores surface and all patterns", () => {
29
- const approval = SessionApproval.multiple("external_directory", [
30
- "/outside/a/*",
31
- "/outside/b/*",
32
- ]);
33
- expect(approval.surface).toBe("external_directory");
34
- expect(approval.patterns).toEqual(["/outside/a/*", "/outside/b/*"]);
35
- });
36
-
37
- it("representativePattern returns the first pattern", () => {
38
- const approval = SessionApproval.multiple("external_directory", [
39
- "/outside/a/*",
40
- "/outside/b/*",
41
- ]);
42
- expect(approval.representativePattern).toBe("/outside/a/*");
43
- });
44
-
45
- it("toGateApproval returns { surface, pattern } using the first pattern", () => {
46
- const approval = SessionApproval.multiple("external_directory", [
47
- "/outside/a/*",
48
- "/outside/b/*",
49
- ]);
50
- expect(approval.toGateApproval()).toEqual({
51
- surface: "external_directory",
52
- pattern: "/outside/a/*",
53
- });
54
- });
55
-
56
- it("defensive copy — mutating the source array does not affect patterns", () => {
57
- const source = ["/outside/a/*", "/outside/b/*"];
58
- const approval = SessionApproval.multiple("external_directory", source);
59
- source.push("/outside/c/*");
60
- expect(approval.patterns).toEqual(["/outside/a/*", "/outside/b/*"]);
61
- });
62
- });
63
-
64
- describe("empty patterns (degenerate case)", () => {
65
- it("representativePattern returns undefined", () => {
66
- const approval = SessionApproval.multiple("external_directory", []);
67
- expect(approval.representativePattern).toBeUndefined();
68
- });
69
-
70
- it("toGateApproval returns undefined", () => {
71
- const approval = SessionApproval.multiple("external_directory", []);
72
- expect(approval.toGateApproval()).toBeUndefined();
73
- });
74
- });
75
- });