(
toolId: string,
sortedPolicies: readonly (Pick<ToolPolicy, "pattern" | "action" | "id"> &
Partial<Pick<ToolPolicy, "owner">>)[],
defaultRequiresApproval?: boolean,
)
| 214 | }; |
| 215 | |
| 216 | export const effectivePolicyFromSorted = ( |
| 217 | toolId: string, |
| 218 | sortedPolicies: readonly (Pick<ToolPolicy, "pattern" | "action" | "id"> & |
| 219 | Partial<Pick<ToolPolicy, "owner">>)[], |
| 220 | defaultRequiresApproval?: boolean, |
| 221 | ): EffectivePolicy => { |
| 222 | const firstMatchByOwner = new Map<string, EffectivePolicy>(); |
| 223 | for (const p of sortedPolicies) { |
| 224 | const ownerKey = "owner" in p && p.owner ? String(p.owner) : "__flat__"; |
| 225 | if (firstMatchByOwner.has(ownerKey)) continue; |
| 226 | if (matchPattern(p.pattern, toolId)) { |
| 227 | firstMatchByOwner.set(ownerKey, { |
| 228 | action: p.action, |
| 229 | source: "user", |
| 230 | pattern: p.pattern, |
| 231 | policyId: p.id, |
| 232 | }); |
| 233 | } |
| 234 | } |
| 235 | let selected: EffectivePolicy | undefined; |
| 236 | for (const match of firstMatchByOwner.values()) { |
| 237 | selected = moreRestrictive(selected, match); |
| 238 | } |
| 239 | return selected ?? liftPlugin(defaultRequiresApproval); |
| 240 | }; |
| 241 | |
| 242 | // --------------------------------------------------------------------------- |
| 243 | // Row → public projection. |
no test coverage detected