|
1 | | -import { afterEach, describe, expect, mock, spyOn, test } from "bun:test"; |
| 1 | +import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"; |
2 | 2 | import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
| 5 | +import { getLogger } from "@intx/log"; |
| 6 | +import type { ToolCall } from "@intx/types/runtime"; |
5 | 7 |
|
| 8 | +import { LOG_NAMESPACE_ROOT } from "../branding.js"; |
6 | 9 | import * as permissionStore from "../permission/store.js"; |
| 10 | +import { createPermissionGate } from "../permission/gate.js"; |
| 11 | +import type { GrantScope } from "../permission/types.js"; |
7 | 12 | import { |
| 13 | + APPROVAL_PERSIST_FAILURE_NOTICE, |
8 | 14 | buildSubAgentProvider, |
9 | 15 | createApprovalPersist, |
10 | 16 | createLiveSubAgentSources, |
@@ -160,6 +166,12 @@ describe("loadSeededApprovals merge order", () => { |
160 | 166 | }); |
161 | 167 |
|
162 | 168 | describe("createApprovalPersist", () => { |
| 169 | + const persistLogger = getLogger([LOG_NAMESPACE_ROOT, "session", "approvals"]); |
| 170 | + |
| 171 | + beforeEach(() => { |
| 172 | + spyOn(persistLogger, "warn"); |
| 173 | + }); |
| 174 | + |
163 | 175 | afterEach(() => { |
164 | 176 | mock.restore(); |
165 | 177 | }); |
@@ -203,6 +215,120 @@ describe("createApprovalPersist", () => { |
203 | 215 | expect(providerModel).toHaveBeenNthCalledWith(1, "openai:gpt-5", approval); |
204 | 216 | expect(providerModel).toHaveBeenNthCalledWith(2, "anthropic:claude-opus", approval); |
205 | 217 | }); |
| 218 | + |
| 219 | + const persistedScopes: { |
| 220 | + scope: Exclude<GrantScope, "session">; |
| 221 | + reject: (message: string) => void; |
| 222 | + }[] = [ |
| 223 | + { |
| 224 | + scope: "project", |
| 225 | + reject: (message) => { |
| 226 | + spyOn(permissionStore, "saveProjectApproval").mockRejectedValue(new Error(message)); |
| 227 | + }, |
| 228 | + }, |
| 229 | + { |
| 230 | + scope: "global", |
| 231 | + reject: (message) => { |
| 232 | + spyOn(permissionStore, "saveGlobalApproval").mockRejectedValue(new Error(message)); |
| 233 | + }, |
| 234 | + }, |
| 235 | + { |
| 236 | + scope: "provider-model", |
| 237 | + reject: (message) => { |
| 238 | + spyOn(permissionStore, "saveProviderModelApproval").mockRejectedValue(new Error(message)); |
| 239 | + }, |
| 240 | + }, |
| 241 | + ]; |
| 242 | + |
| 243 | + const shellCall = (command: string): ToolCall => ({ |
| 244 | + id: "c", |
| 245 | + name: "run_shell", |
| 246 | + arguments: { command }, |
| 247 | + }); |
| 248 | + |
| 249 | + async function flushUnhandledRejections(): Promise<unknown> { |
| 250 | + let unhandled: unknown = null; |
| 251 | + const onUnhandled = (reason: unknown): void => { |
| 252 | + unhandled = reason; |
| 253 | + }; |
| 254 | + process.on("unhandledRejection", onUnhandled); |
| 255 | + try { |
| 256 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 257 | + } finally { |
| 258 | + process.off("unhandledRejection", onUnhandled); |
| 259 | + } |
| 260 | + return unhandled; |
| 261 | + } |
| 262 | + |
| 263 | + for (const { scope, reject } of persistedScopes) { |
| 264 | + test(`a rejected ${scope} write is contained, logged, noticed, and never becomes an unhandled rejection`, async () => { |
| 265 | + const message = `${scope} disk full`; |
| 266 | + reject(message); |
| 267 | + const notices: string[] = []; |
| 268 | + |
| 269 | + const persist = createApprovalPersist( |
| 270 | + "/tmp/proj", |
| 271 | + () => "openai:gpt-5", |
| 272 | + (text) => { |
| 273 | + notices.push(text); |
| 274 | + }, |
| 275 | + ); |
| 276 | + persist({ tool: "run_shell", pattern: "npm *" }, scope); |
| 277 | + |
| 278 | + expect(await flushUnhandledRejections()).toBeNull(); |
| 279 | + expect(persistLogger.warn).toHaveBeenCalledTimes(1); |
| 280 | + expect(persistLogger.warn).toHaveBeenCalledWith( |
| 281 | + "Failed to persist {scope} approval: {error}", |
| 282 | + { |
| 283 | + scope, |
| 284 | + error: message, |
| 285 | + }, |
| 286 | + ); |
| 287 | + expect(notices).toEqual([APPROVAL_PERSIST_FAILURE_NOTICE]); |
| 288 | + }); |
| 289 | + |
| 290 | + test(`a throwing ${scope} persist notice is contained and never becomes an unhandled rejection`, async () => { |
| 291 | + reject(`${scope} EIO`); |
| 292 | + |
| 293 | + const persist = createApprovalPersist( |
| 294 | + "/tmp/proj", |
| 295 | + () => "openai:gpt-5", |
| 296 | + () => { |
| 297 | + throw new Error("notice exploded"); |
| 298 | + }, |
| 299 | + ); |
| 300 | + persist({ tool: "run_shell", pattern: "npm *" }, scope); |
| 301 | + |
| 302 | + expect(await flushUnhandledRejections()).toBeNull(); |
| 303 | + }); |
| 304 | + |
| 305 | + test(`an approved call still completes and the in-memory ${scope} grant still applies when persist rejects`, async () => { |
| 306 | + reject(`${scope} EACCES`); |
| 307 | + const persist = createApprovalPersist("/tmp/proj", () => "openai:gpt-5"); |
| 308 | + let asked = 0; |
| 309 | + const gate = createPermissionGate({ |
| 310 | + approvals: [], |
| 311 | + requestApproval: async () => { |
| 312 | + asked++; |
| 313 | + return { |
| 314 | + allow: true, |
| 315 | + persist: { id: scope, label: "", pattern: "npm *", grant: scope }, |
| 316 | + }; |
| 317 | + }, |
| 318 | + persist, |
| 319 | + interactive: true, |
| 320 | + skipPermissions: false, |
| 321 | + providerName: "openai", |
| 322 | + model: "gpt-5", |
| 323 | + }); |
| 324 | + |
| 325 | + expect((await gate.evaluate(shellCall("npm test"))).allowed).toBe(true); |
| 326 | + expect(asked).toBe(1); |
| 327 | + expect(await flushUnhandledRejections()).toBeNull(); |
| 328 | + expect((await gate.evaluate(shellCall("npm run build"))).allowed).toBe(true); |
| 329 | + expect(asked).toBe(1); |
| 330 | + }); |
| 331 | + } |
206 | 332 | }); |
207 | 333 |
|
208 | 334 | describe("skillDirsFromEnabledPlugins", () => { |
|
0 commit comments