Count the tool calls an agent's policy allows

Agent buildMedium25 min

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]. effect is "allow" or "deny". tool is a tool's name or "*", and resource is 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]]) -> int
Example 1
Inputallowed_calls(3, 4, [["deny", "shell", "*"], ["allow", "*", "docs"], ["allow", "search", "*"]])
Output5
Example 2
Inputallowed_calls(1000000, 1000000, [["allow", "*", "*"]])
Output1000000000000
Example 3
Inputallowed_calls(5, 5, [["deny", "*", "*"], ["allow", "*", "*"]])
Output0