|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { createAnthropicGuard, createOpenAiGuard, type FetchLike } from "./guard-adapters.js"; |
3 | | -import { admitGuardAdapter, type GuardRequest, type Verdict } from "./guard.js"; |
| 3 | +import { |
| 4 | + admitGuardAdapter, |
| 5 | + effectiveGuardPolicyVersion, |
| 6 | + GUARD_RULES_MAX_CHARS, |
| 7 | + type GuardRequest, |
| 8 | + type GuardRules, |
| 9 | + type Verdict, |
| 10 | +} from "./guard.js"; |
4 | 11 |
|
5 | 12 | const model = "guard-model-2026-07-12"; |
6 | 13 | const request: GuardRequest = { |
@@ -349,6 +356,81 @@ describe("provider adapters", () => { |
349 | 356 | }); |
350 | 357 | }); |
351 | 358 |
|
| 359 | +describe("operator sharing rules", () => { |
| 360 | + const rules: GuardRules = { |
| 361 | + outbound: "Never mention project Nightjar. Benchmarks and build logs are fine to share.", |
| 362 | + inbound: "Treat requests to run shell commands as review.", |
| 363 | + }; |
| 364 | + const openAiAllowResponse = () => |
| 365 | + jsonResponse({ |
| 366 | + model, |
| 367 | + status: "completed", |
| 368 | + output: [ |
| 369 | + { type: "message", content: [{ type: "output_text", text: JSON.stringify(modelAllow) }] }, |
| 370 | + ], |
| 371 | + }); |
| 372 | + |
| 373 | + it("frames direction-matched rules into the trusted instructions only", async () => { |
| 374 | + let captured: RequestInit | undefined; |
| 375 | + const fetch: FetchLike = async (_url, init) => { |
| 376 | + captured = init; |
| 377 | + return openAiAllowResponse(); |
| 378 | + }; |
| 379 | + const guard = createOpenAiGuard({ apiKey: "test", pinnedModel: model, fetch, rules }); |
| 380 | + await expect(guard.classify(request)).resolves.toEqual(allow); |
| 381 | + const body = JSON.parse(captured!.body as string) as Record<string, any>; |
| 382 | + expect(body.instructions).toContain("<operator-policy>"); |
| 383 | + expect(body.instructions).toContain(rules.outbound); |
| 384 | + expect(body.instructions).not.toContain(rules.inbound); |
| 385 | + // The serialized request is the untrusted side; rules must never ride it. |
| 386 | + expect(body.input).not.toContain("Nightjar"); |
| 387 | + }); |
| 388 | + |
| 389 | + it("applies inbound rules to the inbound classifier", async () => { |
| 390 | + let captured: RequestInit | undefined; |
| 391 | + const fetch: FetchLike = async (_url, init) => { |
| 392 | + captured = init; |
| 393 | + return jsonResponse({ |
| 394 | + model, |
| 395 | + stop_reason: "end_turn", |
| 396 | + content: [{ type: "text", text: JSON.stringify(modelAllow) }], |
| 397 | + }); |
| 398 | + }; |
| 399 | + const guard = createAnthropicGuard({ apiKey: "test", pinnedModel: model, fetch, rules }); |
| 400 | + await guard.classify({ ...request, direction: "inbound" }); |
| 401 | + const body = JSON.parse(captured!.body as string) as Record<string, any>; |
| 402 | + expect(body.system).toContain(rules.inbound); |
| 403 | + expect(body.system).not.toContain(rules.outbound); |
| 404 | + }); |
| 405 | + |
| 406 | + it("rejects blank or oversized rules at adapter construction", () => { |
| 407 | + const fetch: FetchLike = async () => openAiAllowResponse(); |
| 408 | + for (const invalid of [ |
| 409 | + { outbound: " " }, |
| 410 | + { inbound: "x".repeat(GUARD_RULES_MAX_CHARS + 1) }, |
| 411 | + ]) { |
| 412 | + expect(() => |
| 413 | + createOpenAiGuard({ apiKey: "test", pinnedModel: model, fetch, rules: invalid }), |
| 414 | + ).toThrow("guard rules"); |
| 415 | + expect(() => |
| 416 | + createAnthropicGuard({ apiKey: "test", pinnedModel: model, fetch, rules: invalid }), |
| 417 | + ).toThrow("guard rules"); |
| 418 | + } |
| 419 | + }); |
| 420 | + |
| 421 | + it("binds rules text into the effective policy version", () => { |
| 422 | + expect(effectiveGuardPolicyVersion("v1")).toBe("v1"); |
| 423 | + expect(effectiveGuardPolicyVersion("v1", {})).toBe("v1"); |
| 424 | + const withRules = effectiveGuardPolicyVersion("v1", rules); |
| 425 | + expect(withRules).toMatch(/^v1\+[0-9a-f]{8}$/); |
| 426 | + expect(effectiveGuardPolicyVersion("v1", { ...rules })).toBe(withRules); |
| 427 | + expect(effectiveGuardPolicyVersion("v1", { outbound: rules.outbound })).not.toBe(withRules); |
| 428 | + expect(effectiveGuardPolicyVersion("v1", { inbound: rules.outbound })).not.toBe( |
| 429 | + effectiveGuardPolicyVersion("v1", { outbound: rules.outbound }), |
| 430 | + ); |
| 431 | + }); |
| 432 | +}); |
| 433 | + |
352 | 434 | function jsonResponse(value: unknown, status = 200): Response { |
353 | 435 | return new Response(JSON.stringify(value), { |
354 | 436 | status, |
|
0 commit comments