Count the tool calls an agent's policy allows
Your task
Before you give an agent a new policy, you want to know how much it lets through. A tool call here is a pair: one tool and one resource. The company has millions of each.
Write allowed_calls(tool_count, resource_count, rules) that returns how many pairs the policy allows.
Input and output:
tool_count: how many tools exist, 1 to 10,000,000.resource_count: how many resources exist, 1 to 10,000,000.rules: up to 100 rules in order, each[effect, tool, resource].effectis"allow"or"deny".toolis a tool's name or"*", andresourceis a resource's name or"*".- Every name in the rules is the name of a tool or resource that exists, compared exactly. No tool or resource is named
"*". - Return an integer.
Rules:
- A rule matches a pair when its tool is
"*"or that pair's tool, and its resource is"*"or that pair's resource. - The first rule that matches a pair decides it, and later rules do not count.
- A pair that no rule matches is denied.
- The tools and resources a rule does not name still exist and still count.
Examples
allowed_calls(tool_count: int, resource_count: int, rules: list[list[str]]) -> intExample 1
Input
allowed_calls(3, 4, [["deny", "shell", "*"], ["allow", "*", "docs"], ["allow", "search", "*"]])Output
5Example 2
Input
allowed_calls(1000000, 1000000, [["allow", "*", "*"]])Output
1000000000000Example 3
Input
allowed_calls(5, 5, [["deny", "*", "*"], ["allow", "*", "*"]])Output
0