@komarspn/pi-permission-system 16.0.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 +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { buildInputForSurface } from "#src/input-normalizer";
|
|
3
|
+
import type { PermissionsService } from "#src/service";
|
|
4
|
+
import {
|
|
5
|
+
getPermissionsService,
|
|
6
|
+
publishPermissionsService,
|
|
7
|
+
unpublishPermissionsService,
|
|
8
|
+
} from "#src/service";
|
|
9
|
+
import { ToolAccessExtractorRegistry } from "#src/tool-access-extractor-registry";
|
|
10
|
+
import { ToolInputFormatterRegistry } from "#src/tool-input-formatter-registry";
|
|
11
|
+
import type { PermissionCheckResult } from "#src/types";
|
|
12
|
+
|
|
13
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function makeService(
|
|
16
|
+
overrides: Partial<PermissionsService> = {},
|
|
17
|
+
): PermissionsService {
|
|
18
|
+
return {
|
|
19
|
+
checkPermission: vi.fn(),
|
|
20
|
+
getToolPermission: vi.fn(),
|
|
21
|
+
registerToolInputFormatter: vi.fn(),
|
|
22
|
+
registerToolAccessExtractor: vi.fn(),
|
|
23
|
+
...overrides,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ── globalThis accessor ────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
describe("globalThis accessor", () => {
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
const current = getPermissionsService();
|
|
32
|
+
if (current) {
|
|
33
|
+
unpublishPermissionsService(current);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("returns undefined when nothing has been published", () => {
|
|
38
|
+
expect(getPermissionsService()).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns the published service", () => {
|
|
42
|
+
const service = makeService();
|
|
43
|
+
publishPermissionsService(service);
|
|
44
|
+
expect(getPermissionsService()).toBe(service);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("overwrites a previously published service", () => {
|
|
48
|
+
const first = makeService();
|
|
49
|
+
const second = makeService();
|
|
50
|
+
publishPermissionsService(first);
|
|
51
|
+
publishPermissionsService(second);
|
|
52
|
+
expect(getPermissionsService()).toBe(second);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("removes the slot when it still holds the given service", () => {
|
|
56
|
+
const service = makeService();
|
|
57
|
+
publishPermissionsService(service);
|
|
58
|
+
unpublishPermissionsService(service);
|
|
59
|
+
expect(getPermissionsService()).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("does not remove the slot when a different service occupies it", () => {
|
|
63
|
+
const parent = makeService();
|
|
64
|
+
const child = makeService();
|
|
65
|
+
publishPermissionsService(parent);
|
|
66
|
+
// A child instance never published `parent`; unpublishing its own service
|
|
67
|
+
// must be a no-op that leaves the parent's slot intact.
|
|
68
|
+
unpublishPermissionsService(child);
|
|
69
|
+
expect(getPermissionsService()).toBe(parent);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("unpublish is safe to call when nothing was published", () => {
|
|
73
|
+
expect(() => unpublishPermissionsService(makeService())).not.toThrow();
|
|
74
|
+
expect(getPermissionsService()).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// ── service adapter delegation ─────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
describe("service adapter delegation", () => {
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
const current = getPermissionsService();
|
|
83
|
+
if (current) {
|
|
84
|
+
unpublishPermissionsService(current);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const fakeResult: PermissionCheckResult = {
|
|
89
|
+
toolName: "bash",
|
|
90
|
+
state: "allow",
|
|
91
|
+
matchedPattern: "git *",
|
|
92
|
+
source: "bash",
|
|
93
|
+
origin: "global",
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
it("checkPermission delegates surface and value through buildInputForSurface", () => {
|
|
97
|
+
const checkPermission = vi.fn().mockReturnValue(fakeResult);
|
|
98
|
+
const sessionRules = [
|
|
99
|
+
{
|
|
100
|
+
surface: "bash",
|
|
101
|
+
pattern: "*",
|
|
102
|
+
action: "allow" as const,
|
|
103
|
+
layer: "session" as const,
|
|
104
|
+
origin: "session" as const,
|
|
105
|
+
},
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
// Build the adapter the same way index.ts will
|
|
109
|
+
const service = makeService({
|
|
110
|
+
checkPermission(surface, value, agentName) {
|
|
111
|
+
const input = buildInputForSurface(surface, value);
|
|
112
|
+
return checkPermission(surface, input, agentName, sessionRules);
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
publishPermissionsService(service);
|
|
117
|
+
const retrieved = getPermissionsService()!;
|
|
118
|
+
const result = retrieved.checkPermission("bash", "git push");
|
|
119
|
+
|
|
120
|
+
expect(result).toBe(fakeResult);
|
|
121
|
+
expect(checkPermission).toHaveBeenCalledWith(
|
|
122
|
+
"bash",
|
|
123
|
+
{ command: "git push" },
|
|
124
|
+
undefined,
|
|
125
|
+
sessionRules,
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("checkPermission passes agentName through", () => {
|
|
130
|
+
const checkPermission = vi.fn().mockReturnValue(fakeResult);
|
|
131
|
+
|
|
132
|
+
const service = makeService({
|
|
133
|
+
checkPermission(surface, value, agentName) {
|
|
134
|
+
const input = buildInputForSurface(surface, value);
|
|
135
|
+
return checkPermission(surface, input, agentName, []);
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
publishPermissionsService(service);
|
|
140
|
+
getPermissionsService()!.checkPermission("skill", "my-skill", "Explore");
|
|
141
|
+
|
|
142
|
+
expect(checkPermission).toHaveBeenCalledWith(
|
|
143
|
+
"skill",
|
|
144
|
+
{ name: "my-skill" },
|
|
145
|
+
"Explore",
|
|
146
|
+
[],
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("getToolPermission delegates to the permission manager", () => {
|
|
151
|
+
const getToolPermissionFn = vi.fn(
|
|
152
|
+
(_t: string, _a?: string): "deny" => "deny",
|
|
153
|
+
);
|
|
154
|
+
const service: PermissionsService = {
|
|
155
|
+
checkPermission: vi.fn(),
|
|
156
|
+
getToolPermission(toolName, agentName) {
|
|
157
|
+
return getToolPermissionFn(toolName, agentName);
|
|
158
|
+
},
|
|
159
|
+
registerToolInputFormatter: vi.fn(),
|
|
160
|
+
registerToolAccessExtractor: vi.fn(),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
publishPermissionsService(service);
|
|
164
|
+
const result = getPermissionsService()!.getToolPermission(
|
|
165
|
+
"bash",
|
|
166
|
+
"Explore",
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect(result).toBe("deny");
|
|
170
|
+
expect(getToolPermissionFn).toHaveBeenCalledWith("bash", "Explore");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("getToolPermission works without agentName", () => {
|
|
174
|
+
const getToolPermissionFn = vi.fn(
|
|
175
|
+
(_t: string, _a?: string): "ask" => "ask",
|
|
176
|
+
);
|
|
177
|
+
const service: PermissionsService = {
|
|
178
|
+
checkPermission: vi.fn(),
|
|
179
|
+
getToolPermission(toolName, agentName) {
|
|
180
|
+
return getToolPermissionFn(toolName, agentName);
|
|
181
|
+
},
|
|
182
|
+
registerToolInputFormatter: vi.fn(),
|
|
183
|
+
registerToolAccessExtractor: vi.fn(),
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
publishPermissionsService(service);
|
|
187
|
+
const result = getPermissionsService()!.getToolPermission("write");
|
|
188
|
+
|
|
189
|
+
expect(result).toBe("ask");
|
|
190
|
+
expect(getToolPermissionFn).toHaveBeenCalledWith("write", undefined);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("checkPermission uses empty object for unknown surfaces", () => {
|
|
194
|
+
const checkPermission = vi.fn().mockReturnValue(fakeResult);
|
|
195
|
+
|
|
196
|
+
const service = makeService({
|
|
197
|
+
checkPermission(surface, value, agentName) {
|
|
198
|
+
const input = buildInputForSurface(surface, value);
|
|
199
|
+
return checkPermission(surface, input, agentName, []);
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
publishPermissionsService(service);
|
|
204
|
+
getPermissionsService()!.checkPermission("read", "/tmp/file");
|
|
205
|
+
|
|
206
|
+
expect(checkPermission).toHaveBeenCalledWith("read", {}, undefined, []);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// ── registerToolInputFormatter delegation ─────────────────────────────────
|
|
211
|
+
|
|
212
|
+
describe("registerToolInputFormatter delegation", () => {
|
|
213
|
+
afterEach(() => {
|
|
214
|
+
const current = getPermissionsService();
|
|
215
|
+
if (current) {
|
|
216
|
+
unpublishPermissionsService(current);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("delegates to the registry and returns its disposer", () => {
|
|
221
|
+
const registry = new ToolInputFormatterRegistry();
|
|
222
|
+
const formatter = () => "preview";
|
|
223
|
+
|
|
224
|
+
const service = makeService({
|
|
225
|
+
registerToolInputFormatter(toolName, fmt) {
|
|
226
|
+
return registry.register(toolName, fmt);
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
publishPermissionsService(service);
|
|
231
|
+
const dispose = getPermissionsService()!.registerToolInputFormatter(
|
|
232
|
+
"my-tool",
|
|
233
|
+
formatter,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// Registry received the registration
|
|
237
|
+
expect(registry.get("my-tool")).toBe(formatter);
|
|
238
|
+
|
|
239
|
+
// Disposer returned from service removes it from the registry
|
|
240
|
+
dispose();
|
|
241
|
+
expect(registry.get("my-tool")).toBeUndefined();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("throws when a formatter is already registered for the tool name", () => {
|
|
245
|
+
const registry = new ToolInputFormatterRegistry();
|
|
246
|
+
registry.register("my-tool", () => undefined);
|
|
247
|
+
|
|
248
|
+
const service = makeService({
|
|
249
|
+
registerToolInputFormatter(toolName, fmt) {
|
|
250
|
+
return registry.register(toolName, fmt);
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
publishPermissionsService(service);
|
|
255
|
+
expect(() =>
|
|
256
|
+
getPermissionsService()!.registerToolInputFormatter("my-tool", () => ""),
|
|
257
|
+
).toThrow("my-tool");
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// ── registerToolAccessExtractor delegation (#352) ────────────────────────
|
|
262
|
+
|
|
263
|
+
describe("registerToolAccessExtractor delegation", () => {
|
|
264
|
+
afterEach(() => {
|
|
265
|
+
const current = getPermissionsService();
|
|
266
|
+
if (current) {
|
|
267
|
+
unpublishPermissionsService(current);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("delegates to the registry and returns its disposer", () => {
|
|
272
|
+
const registry = new ToolAccessExtractorRegistry();
|
|
273
|
+
const extractor = () => "/etc/hosts";
|
|
274
|
+
|
|
275
|
+
const service = makeService({
|
|
276
|
+
registerToolAccessExtractor(toolName, ext) {
|
|
277
|
+
return registry.register(toolName, ext);
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
publishPermissionsService(service);
|
|
282
|
+
const dispose = getPermissionsService()!.registerToolAccessExtractor(
|
|
283
|
+
"ffgrep",
|
|
284
|
+
extractor,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
expect(registry.get("ffgrep")).toBe(extractor);
|
|
288
|
+
|
|
289
|
+
dispose();
|
|
290
|
+
expect(registry.get("ffgrep")).toBeUndefined();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("throws when an extractor is already registered for the tool name", () => {
|
|
294
|
+
const registry = new ToolAccessExtractorRegistry();
|
|
295
|
+
registry.register("ffgrep", () => undefined);
|
|
296
|
+
|
|
297
|
+
const service = makeService({
|
|
298
|
+
registerToolAccessExtractor(toolName, ext) {
|
|
299
|
+
return registry.register(toolName, ext);
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
publishPermissionsService(service);
|
|
304
|
+
expect(() =>
|
|
305
|
+
getPermissionsService()!.registerToolAccessExtractor("ffgrep", () => ""),
|
|
306
|
+
).toThrow("ffgrep");
|
|
307
|
+
});
|
|
308
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
});
|