|
| 1 | +// How a wildcard grant row and a bare `tool:<name>` row each fare against a |
| 2 | +// PINNED tool's runtime gate. |
| 3 | +// |
| 4 | +// A pinned tool package is loaded through the namespacing loader, which |
| 5 | +// rewrites every definition name to `<bundleId>:<name>`. The workflow child |
| 6 | +// gates each call on `tool:<call.name>`, so the query it issues for a pinned |
| 7 | +// tool is `tool:<bundleId>:<name>`. The deploy-time capability walk, by |
| 8 | +// contrast, reads inline `agent.toolFactories`, which carry no bundle context, |
| 9 | +// so the rows it contributes are bare `tool:<name>`. |
| 10 | +// |
| 11 | +// That asymmetry is what makes the namespaced form a step-scoping mechanism: |
| 12 | +// a bare row cannot address a pinned tool's gate, and the authority a pinned |
| 13 | +// tool actually needs is supplied per step by the sidecar's tool-mark floor, |
| 14 | +// derived from the factories that step loaded. These cases pin what a wildcard |
| 15 | +// row does to that scoping. |
| 16 | + |
| 17 | +import { describe, test, expect } from "bun:test"; |
| 18 | + |
| 19 | +import { evaluateGrants } from "./evaluate"; |
| 20 | +import type { GrantRule } from "./types"; |
| 21 | + |
| 22 | +const BUNDLE_ID = "tp_pinned_bundle"; |
| 23 | +const TOOL_NAME = "search"; |
| 24 | +const NAMESPACED_GATE = `tool:${BUNDLE_ID}:${TOOL_NAME}`; |
| 25 | +const BARE_GATE = `tool:${TOOL_NAME}`; |
| 26 | +const INVOKE = "invoke"; |
| 27 | + |
| 28 | +function grant( |
| 29 | + overrides: Partial<GrantRule> & |
| 30 | + Pick<GrantRule, "resource" | "action" | "effect">, |
| 31 | +): GrantRule { |
| 32 | + return { |
| 33 | + id: `grt_${Math.random().toString(36).slice(2, 10)}`, |
| 34 | + origin: "creator", |
| 35 | + conditions: null, |
| 36 | + expiresAt: null, |
| 37 | + roleId: null, |
| 38 | + principalId: null, |
| 39 | + ...overrides, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("a wildcard row reaches a pinned tool's namespaced gate", () => { |
| 44 | + test("*/* resolves to allow for tool:<bundleId>:<name>/invoke", async () => { |
| 45 | + const result = await evaluateGrants( |
| 46 | + [grant({ resource: "*", action: "*", effect: "allow" })], |
| 47 | + NAMESPACED_GATE, |
| 48 | + INVOKE, |
| 49 | + ); |
| 50 | + expect(result.effect).toBe("allow"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("the walk's bare tool:<name> row does NOT reach that gate", async () => { |
| 54 | + const result = await evaluateGrants( |
| 55 | + [grant({ resource: BARE_GATE, action: INVOKE, effect: "allow" })], |
| 56 | + NAMESPACED_GATE, |
| 57 | + INVOKE, |
| 58 | + ); |
| 59 | + expect(result.effect).toBe(null); |
| 60 | + }); |
| 61 | + |
| 62 | + test("the bare row is operative against a bare, unpinned gate", async () => { |
| 63 | + const result = await evaluateGrants( |
| 64 | + [grant({ resource: BARE_GATE, action: INVOKE, effect: "allow" })], |
| 65 | + BARE_GATE, |
| 66 | + INVOKE, |
| 67 | + ); |
| 68 | + expect(result.effect).toBe("allow"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("a wildcard row reaches a second bundle's tool the step never loaded", async () => { |
| 72 | + const result = await evaluateGrants( |
| 73 | + [grant({ resource: "*", action: "*", effect: "allow" })], |
| 74 | + "tool:tp_other_bundle:transfer", |
| 75 | + INVOKE, |
| 76 | + ); |
| 77 | + expect(result.effect).toBe("allow"); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("the tool-mark floor still outranks a wildcard row", () => { |
| 82 | + test("an ask floor beats */* allow at higher specificity", async () => { |
| 83 | + const floor = grant({ |
| 84 | + resource: NAMESPACED_GATE, |
| 85 | + action: INVOKE, |
| 86 | + effect: "ask", |
| 87 | + }); |
| 88 | + const wildcard = grant({ resource: "*", action: "*", effect: "allow" }); |
| 89 | + const result = await evaluateGrants( |
| 90 | + [wildcard, floor], |
| 91 | + NAMESPACED_GATE, |
| 92 | + INVOKE, |
| 93 | + ); |
| 94 | + expect(result.effect).toBe("ask"); |
| 95 | + }); |
| 96 | +}); |
0 commit comments