In the previous Command Semantics & Interpretation chapter, we learned how to translate the confusing results of a command after it runs.
But wait! Before we even run a command, shouldn't we check if it is safe?
Welcome to Permission Orchestration. This is the "Brain" of the tool. It decides whether a command gets a "Green Light" (Allow), a "Red Light" (Deny), or if it needs to check with you first (Ask).
Imagine a high-security building. At the entrance, there is a head security guard.
If a visitor arrives, the guard doesn't just glance at them and say "Go ahead."
Crucially, the order matters. If the person is on the Blacklist, the guard sends them away immediatelyβthey don't waste time checking their bag.
The Problem: If we check rules in the wrong order, we might accidentally allow a dangerous command.
git. We see the command is git status. We allow it. BUT, we missed that the command was actually cd /secret; git status.The Solution: We need an Orchestrator that coordinates all these checks in a strict hierarchy.
In early versions of permission tools, the logic was often: "Check Rule A. If safe, return Allow. If not, Check Rule B."
This is dangerous. Why? Because Rule A might say "Safe" while Rule B screams "DANGER!"
In PowerShellTool, we use a strategy called Collect-Then-Reduce.
decisions).When looking at our pile of decisions, the priority is strict:
We cannot trust a command string just by looking at it. PowerShell is tricky.
Get-Process looks safe.Get-Process; Remove-Item / looks safe at the start, but is destructive at the end.To handle this, the Orchestrator works in two phases:
rm, we block rm immediately.
Here is how the Orchestrator handles a request like rm -Recurse ./src.
The logic lives in powershellPermissions.ts. Let's walk through the powershellToolHasPermission function.
Before we do anything expensive, check if the user strictly banned this command.
// powershellPermissions.ts
export async function powershellToolHasPermission(input, context) {
const command = input.command.trim()
// 1. Check strict rules on the raw text
const exactMatch = powershellToolCheckExactMatchPermission(input, context)
// If the user explicitly banned this, STOP immediately.
if (exactMatch.behavior === 'deny') {
return exactMatch
}
Explanation: This is the bouncer checking the "Banned List" at the door. If you are banned, you don't get in, even if you are just here to deliver pizza.
If the command passes the first check, we parse it and prepare to collect opinions.
// 2. Parse the command to understand its structure
const parsed = await parsePowerShellCommand(command)
// 3. Create a bucket to hold all our safety checks
const decisions: PermissionResult[] = []
Explanation: parsed is now a map of the command structure. decisions is an empty array where we will throw every "Allow", "Deny", or "Ask" we generate.
Now we call our specialist modules. (We will learn about these in the next chapters).
// Check A: Is the command fundamentally dangerous? (Chapter 6)
const safetyResult = powershellCommandIsSafe(command, parsed)
if (safetyResult.behavior !== 'passthrough') {
decisions.push(safetyResult)
}
// Check B: Are the file paths safe? (Chapter 4)
const pathResult = checkPathConstraints(input, parsed, ...);
if (pathResult.behavior !== 'passthrough') {
decisions.push(pathResult)
}
Explanation: We ask Security & Threat Detection and Path & Filesystem Validation for their opinions. We push their answers into the decisions bucket.
Finally, we look at everything in the bucket and apply the hierarchy.
// 4. REDUCE: Deny > Ask > Allow
// Did ANY check say Deny?
const denied = decisions.find(d => d.behavior === 'deny')
if (denied) return denied
// Did ANY check say Ask?
const asked = decisions.find(d => d.behavior === 'ask')
if (asked) return asked
// Did explicit Allow rules match?
const allowed = decisions.find(d => d.behavior === 'allow')
if (allowed) return allowed
Explanation: This is the core logic. Even if 5 checks say "Allow", if one check says "Deny", the result is Deny. This ensures that a loophole in one check doesn't compromise the whole system.
In this chapter, we explored the Permission Orchestrator, the central brain of the PowerShell tool.
Now that the Orchestrator is running, it needs to consult its specialists. The most important specialist is the one that prevents the AI from writing over your important files.
Next Chapter: Path & Filesystem Validation
Generated by Code IQ