(
toolId: string,
sortedPolicies: readonly (Pick<ToolPolicy, "pattern" | "action" | "id"> &
Partial<Pick<ToolPolicy, "owner">>)[],
defaultRequiresApproval?: boolean,
)
| 255 | }; |
| 256 | |
| 257 | export const effectivePolicyFromSorted = ( |
| 258 | toolId: string, |
| 259 | sortedPolicies: readonly (Pick<ToolPolicy, "pattern" | "action" | "id"> & |
| 260 | Partial<Pick<ToolPolicy, "owner">>)[], |
| 261 | defaultRequiresApproval?: boolean, |
| 262 | ): EffectivePolicy => { |
| 263 | const firstMatchByOwner = new Map<string, EffectivePolicy>(); |
| 264 | for (const p of sortedPolicies) { |
| 265 | const ownerKey = "owner" in p && p.owner ? String(p.owner) : "__flat__"; |
| 266 | if (firstMatchByOwner.has(ownerKey)) continue; |
| 267 | if (matchPattern(p.pattern, toolId)) { |
| 268 | firstMatchByOwner.set(ownerKey, { |
| 269 | action: p.action, |
| 270 | source: "user", |
| 271 | pattern: p.pattern, |
| 272 | policyId: p.id, |
| 273 | }); |
| 274 | } |
| 275 | } |
| 276 | let selected: EffectivePolicy | undefined; |
| 277 | for (const match of firstMatchByOwner.values()) { |
| 278 | selected = moreRestrictive(selected, match); |
| 279 | } |
| 280 | return selected ?? liftPlugin(defaultRequiresApproval); |
| 281 | }; |
| 282 | |
| 283 | // --------------------------------------------------------------------------- |
| 284 | // Row → public projection. |
no test coverage detected