@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,466 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// ── Module mocks (hoisted) ─────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
mockLoadAndMergeConfigs,
|
|
7
|
+
mockLoadUnifiedConfig,
|
|
8
|
+
mockSyncPermissionSystemStatus,
|
|
9
|
+
mockBuildResolvedConfigLogEntry,
|
|
10
|
+
mockExistsSync,
|
|
11
|
+
mockMkdirSync,
|
|
12
|
+
mockWriteFileSync,
|
|
13
|
+
mockRenameSync,
|
|
14
|
+
mockUnlinkSync,
|
|
15
|
+
} = vi.hoisted(() => ({
|
|
16
|
+
mockLoadAndMergeConfigs: vi.fn(),
|
|
17
|
+
mockLoadUnifiedConfig: vi.fn(),
|
|
18
|
+
mockSyncPermissionSystemStatus: vi.fn(),
|
|
19
|
+
mockBuildResolvedConfigLogEntry: vi.fn(),
|
|
20
|
+
mockExistsSync: vi.fn<(path: string) => boolean>(),
|
|
21
|
+
mockMkdirSync: vi.fn(),
|
|
22
|
+
mockWriteFileSync: vi.fn(),
|
|
23
|
+
mockRenameSync: vi.fn(),
|
|
24
|
+
mockUnlinkSync: vi.fn(),
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
vi.mock("../src/config-loader", () => ({
|
|
28
|
+
loadAndMergeConfigs: mockLoadAndMergeConfigs,
|
|
29
|
+
loadUnifiedConfig: mockLoadUnifiedConfig,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
vi.mock("../src/status", () => ({
|
|
33
|
+
syncPermissionSystemStatus: mockSyncPermissionSystemStatus,
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
vi.mock("../src/config-reporter", () => ({
|
|
37
|
+
buildResolvedConfigLogEntry: mockBuildResolvedConfigLogEntry,
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock("node:fs", () => ({
|
|
41
|
+
existsSync: mockExistsSync,
|
|
42
|
+
mkdirSync: mockMkdirSync,
|
|
43
|
+
writeFileSync: mockWriteFileSync,
|
|
44
|
+
renameSync: mockRenameSync,
|
|
45
|
+
unlinkSync: mockUnlinkSync,
|
|
46
|
+
default: {
|
|
47
|
+
existsSync: mockExistsSync,
|
|
48
|
+
mkdirSync: mockMkdirSync,
|
|
49
|
+
writeFileSync: mockWriteFileSync,
|
|
50
|
+
renameSync: mockRenameSync,
|
|
51
|
+
unlinkSync: mockUnlinkSync,
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
// ── Imports ────────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
import type {
|
|
58
|
+
ExtensionCommandContext,
|
|
59
|
+
ExtensionContext,
|
|
60
|
+
} from "@earendil-works/pi-coding-agent";
|
|
61
|
+
import {
|
|
62
|
+
ConfigStore,
|
|
63
|
+
type ConfigStoreDeps,
|
|
64
|
+
type ResolvedPolicyPathProvider,
|
|
65
|
+
} from "#src/config-store";
|
|
66
|
+
import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
|
|
67
|
+
import type { ResolvedPolicyPaths } from "#src/policy-loader";
|
|
68
|
+
|
|
69
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
function makePolicyPathProvider(
|
|
72
|
+
paths?: Partial<ResolvedPolicyPaths>,
|
|
73
|
+
): ResolvedPolicyPathProvider {
|
|
74
|
+
return {
|
|
75
|
+
getResolvedPolicyPaths: vi.fn(
|
|
76
|
+
(): ResolvedPolicyPaths => ({
|
|
77
|
+
globalConfigPath: "/agent/config.json",
|
|
78
|
+
globalConfigExists: false,
|
|
79
|
+
projectConfigPath: null,
|
|
80
|
+
projectConfigExists: false,
|
|
81
|
+
agentsDir: "/agent/agents",
|
|
82
|
+
agentsDirExists: false,
|
|
83
|
+
projectAgentsDir: null,
|
|
84
|
+
projectAgentsDirExists: false,
|
|
85
|
+
...paths,
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function makeLogger() {
|
|
92
|
+
return {
|
|
93
|
+
debug: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
94
|
+
review: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function makeCtx(overrides: Partial<ExtensionContext> = {}): ExtensionContext {
|
|
99
|
+
return {
|
|
100
|
+
cwd: "/test/project",
|
|
101
|
+
hasUI: false,
|
|
102
|
+
ui: { notify: vi.fn(), setStatus: vi.fn() },
|
|
103
|
+
sessionManager: { getEntries: vi.fn(), addEntry: vi.fn() },
|
|
104
|
+
...overrides,
|
|
105
|
+
} as unknown as ExtensionContext;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function makeCommandCtx(
|
|
109
|
+
overrides: Partial<ExtensionCommandContext> = {},
|
|
110
|
+
): ExtensionCommandContext {
|
|
111
|
+
return {
|
|
112
|
+
cwd: "/test/project",
|
|
113
|
+
ui: { notify: vi.fn(), setStatus: vi.fn() },
|
|
114
|
+
...overrides,
|
|
115
|
+
} as unknown as ExtensionCommandContext;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function makeStore(overrides: Partial<ConfigStoreDeps> = {}): {
|
|
119
|
+
store: ConfigStore;
|
|
120
|
+
logger: ReturnType<typeof makeLogger>;
|
|
121
|
+
} {
|
|
122
|
+
const logger = makeLogger();
|
|
123
|
+
const deps: ConfigStoreDeps = {
|
|
124
|
+
agentDir: "/test/agent",
|
|
125
|
+
policyPaths: makePolicyPathProvider(),
|
|
126
|
+
logger,
|
|
127
|
+
...overrides,
|
|
128
|
+
};
|
|
129
|
+
return { store: new ConfigStore(deps), logger };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── Tests ──────────────────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
describe("ConfigStore", () => {
|
|
135
|
+
beforeEach(() => {
|
|
136
|
+
mockLoadAndMergeConfigs.mockReset().mockReturnValue({
|
|
137
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
138
|
+
issues: [],
|
|
139
|
+
});
|
|
140
|
+
mockLoadUnifiedConfig.mockReset().mockReturnValue({ config: {} });
|
|
141
|
+
mockSyncPermissionSystemStatus.mockReset();
|
|
142
|
+
mockBuildResolvedConfigLogEntry
|
|
143
|
+
.mockReset()
|
|
144
|
+
.mockReturnValue({ resolved: true });
|
|
145
|
+
mockExistsSync.mockReset().mockReturnValue(false);
|
|
146
|
+
mockMkdirSync.mockReset();
|
|
147
|
+
mockWriteFileSync.mockReset();
|
|
148
|
+
mockRenameSync.mockReset();
|
|
149
|
+
mockUnlinkSync.mockReset();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ── current() ─────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
describe("current()", () => {
|
|
155
|
+
it("returns DEFAULT_EXTENSION_CONFIG before any refresh", () => {
|
|
156
|
+
const { store } = makeStore();
|
|
157
|
+
expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ── refresh() ─────────────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
describe("refresh()", () => {
|
|
164
|
+
it("uses the passed ctx cwd for loadAndMergeConfigs", () => {
|
|
165
|
+
const { store } = makeStore();
|
|
166
|
+
store.refresh(makeCtx({ cwd: "/my/project" }));
|
|
167
|
+
expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
|
|
168
|
+
"/test/agent",
|
|
169
|
+
"/my/project",
|
|
170
|
+
expect.any(String),
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("uses empty string cwd when no ctx is provided", () => {
|
|
175
|
+
const { store } = makeStore();
|
|
176
|
+
store.refresh();
|
|
177
|
+
expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
|
|
178
|
+
"/test/agent",
|
|
179
|
+
"",
|
|
180
|
+
expect.any(String),
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("updates current() with normalized merged result", () => {
|
|
185
|
+
const { store } = makeStore();
|
|
186
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
187
|
+
merged: { debugLog: true, permissionReviewLog: false, yoloMode: false },
|
|
188
|
+
issues: [],
|
|
189
|
+
});
|
|
190
|
+
store.refresh();
|
|
191
|
+
expect(store.current().debugLog).toBe(true);
|
|
192
|
+
expect(store.current().permissionReviewLog).toBe(false);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("writes config.loaded debug log", () => {
|
|
196
|
+
const { store, logger } = makeStore();
|
|
197
|
+
store.refresh();
|
|
198
|
+
expect(logger.debug).toHaveBeenCalledWith(
|
|
199
|
+
"config.loaded",
|
|
200
|
+
expect.objectContaining({ debugLog: false }),
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("sets warning when issues are present", () => {
|
|
205
|
+
const { store } = makeStore();
|
|
206
|
+
const ctx = makeCtx({ hasUI: false });
|
|
207
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
208
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
209
|
+
issues: ["legacy config detected"],
|
|
210
|
+
});
|
|
211
|
+
store.refresh(ctx);
|
|
212
|
+
// Verify the warning is tracked (next identical call should not re-notify)
|
|
213
|
+
const mockNotify = vi.fn();
|
|
214
|
+
const ctx2 = makeCtx({
|
|
215
|
+
hasUI: true,
|
|
216
|
+
ui: { notify: mockNotify } as never,
|
|
217
|
+
});
|
|
218
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
219
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
220
|
+
issues: ["legacy config detected"],
|
|
221
|
+
});
|
|
222
|
+
store.refresh(ctx2);
|
|
223
|
+
// Same warning — should not re-notify
|
|
224
|
+
expect(mockNotify).not.toHaveBeenCalled();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("notifies UI when a new warning appears and hasUI is true", () => {
|
|
228
|
+
const mockNotify = vi.fn();
|
|
229
|
+
const { store } = makeStore();
|
|
230
|
+
const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
|
|
231
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
232
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
233
|
+
issues: ["new warning"],
|
|
234
|
+
});
|
|
235
|
+
store.refresh(ctx);
|
|
236
|
+
expect(mockNotify).toHaveBeenCalledWith("new warning", "warning");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("does not re-notify the same warning on subsequent calls", () => {
|
|
240
|
+
const mockNotify = vi.fn();
|
|
241
|
+
const { store } = makeStore();
|
|
242
|
+
const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
|
|
243
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
244
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
245
|
+
issues: ["persistent warning"],
|
|
246
|
+
});
|
|
247
|
+
store.refresh(ctx);
|
|
248
|
+
store.refresh(ctx);
|
|
249
|
+
expect(mockNotify).toHaveBeenCalledTimes(1);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("clears warning when no issues on next refresh", () => {
|
|
253
|
+
const mockNotify = vi.fn();
|
|
254
|
+
const { store } = makeStore();
|
|
255
|
+
// First call: set a warning
|
|
256
|
+
const ctxWithUI = makeCtx({
|
|
257
|
+
hasUI: true,
|
|
258
|
+
ui: { notify: mockNotify } as never,
|
|
259
|
+
});
|
|
260
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
261
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
262
|
+
issues: ["warning"],
|
|
263
|
+
});
|
|
264
|
+
store.refresh(ctxWithUI);
|
|
265
|
+
// Second call: no issues — warning should clear
|
|
266
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
267
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
268
|
+
issues: [],
|
|
269
|
+
});
|
|
270
|
+
store.refresh();
|
|
271
|
+
// Third call: same warning reappears — should notify again (dedup cleared)
|
|
272
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
273
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
274
|
+
issues: ["warning"],
|
|
275
|
+
});
|
|
276
|
+
store.refresh(ctxWithUI);
|
|
277
|
+
expect(mockNotify).toHaveBeenCalledTimes(2);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("calls syncPermissionSystemStatus when hasUI is true", () => {
|
|
281
|
+
const { store } = makeStore();
|
|
282
|
+
const ctx = makeCtx({ hasUI: true });
|
|
283
|
+
store.refresh(ctx);
|
|
284
|
+
expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
|
|
285
|
+
ctx,
|
|
286
|
+
expect.any(Object),
|
|
287
|
+
);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("does not call syncPermissionSystemStatus when hasUI is false", () => {
|
|
291
|
+
const { store } = makeStore();
|
|
292
|
+
const ctx = makeCtx({ hasUI: false });
|
|
293
|
+
store.refresh(ctx);
|
|
294
|
+
expect(mockSyncPermissionSystemStatus).not.toHaveBeenCalled();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("carries piInfrastructureReadPaths from merged config into current()", () => {
|
|
298
|
+
const { store } = makeStore();
|
|
299
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
300
|
+
merged: { piInfrastructureReadPaths: ["/extra/path"] },
|
|
301
|
+
issues: [],
|
|
302
|
+
});
|
|
303
|
+
store.refresh();
|
|
304
|
+
expect(store.current().piInfrastructureReadPaths).toEqual([
|
|
305
|
+
"/extra/path",
|
|
306
|
+
]);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
// ── save() ─────────────────────────────────────────────────────────────
|
|
311
|
+
|
|
312
|
+
describe("save()", () => {
|
|
313
|
+
it("writes merged config to the global path", () => {
|
|
314
|
+
const { store } = makeStore();
|
|
315
|
+
mockLoadUnifiedConfig.mockReturnValue({
|
|
316
|
+
config: { permission: { "*": "ask" } },
|
|
317
|
+
});
|
|
318
|
+
const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
|
|
319
|
+
const ctx = makeCommandCtx();
|
|
320
|
+
store.save(next, ctx);
|
|
321
|
+
expect(mockWriteFileSync).toHaveBeenCalledWith(
|
|
322
|
+
expect.stringContaining(".tmp"),
|
|
323
|
+
expect.stringContaining('"debugLog": true'),
|
|
324
|
+
"utf-8",
|
|
325
|
+
);
|
|
326
|
+
expect(mockRenameSync).toHaveBeenCalled();
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("updates current() after a successful save", () => {
|
|
330
|
+
const { store } = makeStore();
|
|
331
|
+
const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
|
|
332
|
+
store.save(next, makeCommandCtx());
|
|
333
|
+
expect(store.current().debugLog).toBe(true);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("calls syncPermissionSystemStatus after a successful save", () => {
|
|
337
|
+
const { store } = makeStore();
|
|
338
|
+
const ctx = makeCommandCtx();
|
|
339
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
340
|
+
expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
|
|
341
|
+
ctx,
|
|
342
|
+
expect.any(Object),
|
|
343
|
+
);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("writes config.saved debug log after a successful save", () => {
|
|
347
|
+
const { store, logger } = makeStore();
|
|
348
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
|
|
349
|
+
expect(logger.debug).toHaveBeenCalledWith(
|
|
350
|
+
"config.saved",
|
|
351
|
+
expect.objectContaining({ debugLog: false }),
|
|
352
|
+
);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("notifies with error and returns early when write fails", () => {
|
|
356
|
+
const mockNotify = vi.fn();
|
|
357
|
+
const ctx = makeCommandCtx({ ui: { notify: mockNotify } as never });
|
|
358
|
+
const { store, logger } = makeStore();
|
|
359
|
+
mockMkdirSync.mockImplementation(() => {
|
|
360
|
+
throw new Error("disk full");
|
|
361
|
+
});
|
|
362
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
363
|
+
expect(mockNotify).toHaveBeenCalledWith(
|
|
364
|
+
expect.stringContaining("Failed to save"),
|
|
365
|
+
"error",
|
|
366
|
+
);
|
|
367
|
+
// current() is not updated on failure
|
|
368
|
+
expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
|
|
369
|
+
// no debug log on failure
|
|
370
|
+
expect(logger.debug).not.toHaveBeenCalledWith(
|
|
371
|
+
"config.saved",
|
|
372
|
+
expect.anything(),
|
|
373
|
+
);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("attempts cleanup of tmp file when write fails and tmp exists", () => {
|
|
377
|
+
const ctx = makeCommandCtx();
|
|
378
|
+
const { store } = makeStore();
|
|
379
|
+
mockMkdirSync.mockImplementation(() => {
|
|
380
|
+
throw new Error("disk full");
|
|
381
|
+
});
|
|
382
|
+
mockExistsSync.mockReturnValue(true);
|
|
383
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
384
|
+
expect(mockUnlinkSync).toHaveBeenCalled();
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("preserves an existing global toolInputPreviewMaxLength on save", () => {
|
|
388
|
+
const { store } = makeStore();
|
|
389
|
+
// Simulate a global config.json that already has the preview-length field.
|
|
390
|
+
mockLoadUnifiedConfig.mockReturnValue({
|
|
391
|
+
config: { toolInputPreviewMaxLength: 800 },
|
|
392
|
+
});
|
|
393
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
|
|
394
|
+
expect(mockWriteFileSync).toHaveBeenCalledWith(
|
|
395
|
+
expect.stringContaining(".tmp"),
|
|
396
|
+
expect.stringContaining('"toolInputPreviewMaxLength": 800'),
|
|
397
|
+
"utf-8",
|
|
398
|
+
);
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it("preserves an existing global piInfrastructureReadPaths on save", () => {
|
|
402
|
+
const { store } = makeStore();
|
|
403
|
+
// Simulate a global config.json that already has the infra-paths field.
|
|
404
|
+
mockLoadUnifiedConfig.mockReturnValue({
|
|
405
|
+
config: { piInfrastructureReadPaths: ["/extra/path"] },
|
|
406
|
+
});
|
|
407
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
|
|
408
|
+
expect(mockWriteFileSync).toHaveBeenCalledWith(
|
|
409
|
+
expect.stringContaining(".tmp"),
|
|
410
|
+
expect.stringContaining('"piInfrastructureReadPaths"'),
|
|
411
|
+
"utf-8",
|
|
412
|
+
);
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
// ── logResolvedPaths() ─────────────────────────────────────────────────
|
|
417
|
+
|
|
418
|
+
describe("logResolvedPaths()", () => {
|
|
419
|
+
it("writes config.resolved to both review and debug logs", () => {
|
|
420
|
+
const { store, logger } = makeStore();
|
|
421
|
+
store.logResolvedPaths();
|
|
422
|
+
expect(logger.review).toHaveBeenCalledWith(
|
|
423
|
+
"config.resolved",
|
|
424
|
+
expect.any(Object),
|
|
425
|
+
);
|
|
426
|
+
expect(logger.debug).toHaveBeenCalledWith(
|
|
427
|
+
"config.resolved",
|
|
428
|
+
expect.any(Object),
|
|
429
|
+
);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("calls getResolvedPolicyPaths from the provider", () => {
|
|
433
|
+
const mockProvider = makePolicyPathProvider();
|
|
434
|
+
const { store } = makeStore({ policyPaths: mockProvider });
|
|
435
|
+
store.logResolvedPaths();
|
|
436
|
+
expect(mockProvider.getResolvedPolicyPaths).toHaveBeenCalled();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("passes legacy detection results to buildResolvedConfigLogEntry", () => {
|
|
440
|
+
const { store } = makeStore();
|
|
441
|
+
// Make one legacy path exist
|
|
442
|
+
mockExistsSync.mockImplementation((p: string) =>
|
|
443
|
+
p.includes("policies.json"),
|
|
444
|
+
);
|
|
445
|
+
store.logResolvedPaths("/some/project");
|
|
446
|
+
expect(mockBuildResolvedConfigLogEntry).toHaveBeenCalledWith(
|
|
447
|
+
expect.objectContaining({
|
|
448
|
+
legacyGlobalPolicyDetected: expect.any(Boolean),
|
|
449
|
+
legacyProjectPolicyDetected: expect.any(Boolean),
|
|
450
|
+
legacyExtensionConfigDetected: expect.any(Boolean),
|
|
451
|
+
}),
|
|
452
|
+
);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("does not check project legacy path when no cwd is provided", () => {
|
|
456
|
+
const { store } = makeStore();
|
|
457
|
+
store.logResolvedPaths(); // no cwd
|
|
458
|
+
// existsSync called for global and ext-config legacy paths only (not project)
|
|
459
|
+
const calls = mockExistsSync.mock.calls.map(([p]: [string]) => p);
|
|
460
|
+
const projectCalls = calls.filter(
|
|
461
|
+
(p) => p.includes("/null/") || p.includes("null"),
|
|
462
|
+
);
|
|
463
|
+
expect(projectCalls).toHaveLength(0);
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { DecisionAudit } from "#src/decision-audit";
|
|
4
|
+
|
|
5
|
+
function makeAuditLogger() {
|
|
6
|
+
return {
|
|
7
|
+
debug: vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
8
|
+
warn: vi.fn<(message: string) => void>(),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe("DecisionAudit", () => {
|
|
13
|
+
it("counts allowed, blocked, and error decisions in the summary", () => {
|
|
14
|
+
const audit = new DecisionAudit();
|
|
15
|
+
audit.recordDecision("allow");
|
|
16
|
+
audit.recordDecision("allow");
|
|
17
|
+
audit.recordDecision("block");
|
|
18
|
+
audit.recordError();
|
|
19
|
+
|
|
20
|
+
const logger = makeAuditLogger();
|
|
21
|
+
audit.writeSummary(logger);
|
|
22
|
+
|
|
23
|
+
expect(logger.debug).toHaveBeenCalledWith("permission.session_summary", {
|
|
24
|
+
toolCalls: 4,
|
|
25
|
+
allowed: 2,
|
|
26
|
+
blocked: 1,
|
|
27
|
+
errors: 1,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("emits a zeroed summary when no calls were recorded", () => {
|
|
32
|
+
const audit = new DecisionAudit();
|
|
33
|
+
const logger = makeAuditLogger();
|
|
34
|
+
|
|
35
|
+
audit.writeSummary(logger);
|
|
36
|
+
|
|
37
|
+
expect(logger.debug).toHaveBeenCalledWith("permission.session_summary", {
|
|
38
|
+
toolCalls: 0,
|
|
39
|
+
allowed: 0,
|
|
40
|
+
blocked: 0,
|
|
41
|
+
errors: 0,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("does not warn when the counts are consistent", () => {
|
|
46
|
+
const audit = new DecisionAudit();
|
|
47
|
+
audit.recordDecision("allow");
|
|
48
|
+
audit.recordError();
|
|
49
|
+
|
|
50
|
+
const logger = makeAuditLogger();
|
|
51
|
+
audit.writeSummary(logger);
|
|
52
|
+
|
|
53
|
+
expect(logger.warn).not.toHaveBeenCalled();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("warns when the per-call invariant is violated", () => {
|
|
57
|
+
const audit = new DecisionAudit();
|
|
58
|
+
audit.recordDecision("allow");
|
|
59
|
+
// Force a re-opened silent path: bump the private total without a matching
|
|
60
|
+
// sub-total, simulating a future regression that resolves a call without
|
|
61
|
+
// recording its terminal decision.
|
|
62
|
+
(audit as unknown as { toolCalls: number }).toolCalls++;
|
|
63
|
+
|
|
64
|
+
const logger = makeAuditLogger();
|
|
65
|
+
audit.writeSummary(logger);
|
|
66
|
+
|
|
67
|
+
expect(logger.warn).toHaveBeenCalledTimes(1);
|
|
68
|
+
expect(logger.warn).toHaveBeenCalledWith(
|
|
69
|
+
expect.stringContaining("invariant violated"),
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type DecisionReporter,
|
|
5
|
+
GateDecisionReporter,
|
|
6
|
+
} from "#src/decision-reporter";
|
|
7
|
+
import {
|
|
8
|
+
PERMISSIONS_DECISION_CHANNEL,
|
|
9
|
+
type PermissionDecisionEvent,
|
|
10
|
+
} from "#src/permission-events";
|
|
11
|
+
import type { SessionLogger } from "#src/session-logger";
|
|
12
|
+
|
|
13
|
+
// ── fixtures ───────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function makeLogger(): SessionLogger {
|
|
16
|
+
return {
|
|
17
|
+
debug: vi.fn(),
|
|
18
|
+
review: vi.fn(),
|
|
19
|
+
warn: vi.fn(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function makeEvents() {
|
|
24
|
+
return {
|
|
25
|
+
emit: vi.fn(),
|
|
26
|
+
on: vi.fn().mockReturnValue(() => undefined),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makeDecisionEvent(
|
|
31
|
+
overrides: Partial<PermissionDecisionEvent> = {},
|
|
32
|
+
): PermissionDecisionEvent {
|
|
33
|
+
return {
|
|
34
|
+
surface: "read",
|
|
35
|
+
value: "read",
|
|
36
|
+
result: "allow",
|
|
37
|
+
resolution: "policy_allow",
|
|
38
|
+
origin: "global",
|
|
39
|
+
agentName: null,
|
|
40
|
+
matchedPattern: null,
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ── tests ──────────────────────────────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
describe("GateDecisionReporter", () => {
|
|
48
|
+
it("satisfies the DecisionReporter interface", () => {
|
|
49
|
+
const reporter: DecisionReporter = new GateDecisionReporter(
|
|
50
|
+
makeLogger(),
|
|
51
|
+
makeEvents(),
|
|
52
|
+
);
|
|
53
|
+
expect(reporter).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("writeReviewLog", () => {
|
|
57
|
+
it("delegates to logger.review with event and details", () => {
|
|
58
|
+
const logger = makeLogger();
|
|
59
|
+
const reporter = new GateDecisionReporter(logger, makeEvents());
|
|
60
|
+
reporter.writeReviewLog("permission_request.blocked", { tool: "bash" });
|
|
61
|
+
expect(logger.review).toHaveBeenCalledWith("permission_request.blocked", {
|
|
62
|
+
tool: "bash",
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("delegates with an empty details object", () => {
|
|
67
|
+
const logger = makeLogger();
|
|
68
|
+
const reporter = new GateDecisionReporter(logger, makeEvents());
|
|
69
|
+
reporter.writeReviewLog("permission_request.session_approved", {});
|
|
70
|
+
expect(logger.review).toHaveBeenCalledWith(
|
|
71
|
+
"permission_request.session_approved",
|
|
72
|
+
{},
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("does not call emitDecision", () => {
|
|
77
|
+
const events = makeEvents();
|
|
78
|
+
const reporter = new GateDecisionReporter(makeLogger(), events);
|
|
79
|
+
reporter.writeReviewLog("some.event", { key: "val" });
|
|
80
|
+
expect(events.emit).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("emitDecision", () => {
|
|
85
|
+
it("emits on the PERMISSIONS_DECISION_CHANNEL with the event", () => {
|
|
86
|
+
const events = makeEvents();
|
|
87
|
+
const reporter = new GateDecisionReporter(makeLogger(), events);
|
|
88
|
+
const event = makeDecisionEvent();
|
|
89
|
+
reporter.emitDecision(event);
|
|
90
|
+
expect(events.emit).toHaveBeenCalledWith(
|
|
91
|
+
PERMISSIONS_DECISION_CHANNEL,
|
|
92
|
+
event,
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("does not call writeReviewLog", () => {
|
|
97
|
+
const logger = makeLogger();
|
|
98
|
+
const reporter = new GateDecisionReporter(logger, makeEvents());
|
|
99
|
+
reporter.emitDecision(makeDecisionEvent());
|
|
100
|
+
expect(logger.review).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("does not propagate a throwing listener", () => {
|
|
104
|
+
const events = makeEvents();
|
|
105
|
+
events.emit.mockImplementation(() => {
|
|
106
|
+
throw new Error("listener boom");
|
|
107
|
+
});
|
|
108
|
+
const reporter = new GateDecisionReporter(makeLogger(), events);
|
|
109
|
+
expect(() => reporter.emitDecision(makeDecisionEvent())).not.toThrow();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|