Dev.to AI 🤖 Ai 👁 0 📖 7 min read

How I approve AI agent commands without staying at the terminal

How I approve AI agent commands without staying at the terminal I let AI agents do a growing share of my infrastructure work: investigate alerts, prepare changes and propose fixes. Eventually, a fix involves running a

How I approve AI agent commands without staying at the terminal

I let AI agents do a growing share of my infrastructure work: investigate alerts, prepare changes and propose fixes. Eventually, a fix involves running a command that changes something. I want to review that command, but I don't want to sit at a terminal waiting for the permission prompt.

So I moved the approval conversation to a Slack DM. The agent prepares a command, a small Bash script posts it, and I reply A42 ok in the thread. The script checks the answer and hands the command back to the agent.

The gate is about 120 lines of Bash. I currently use it only for non-production changes in sessions I started myself. Production changes still need me in the session, and scheduled agents only write proposals and exit.

That scope matters. This is a convenient way to collect approval from a human. Its security still depends on the permissions around the agent.

Why move the prompt?

A terminal permission prompt works while I'm watching the session. If I walk away during a long task, the agent stops at the first command that needs an answer. The investigation may be finished, the change prepared, and everything sits there waiting for me to return to the right terminal.

A DM lets me review the proposal from my phone. The agent can keep working on unrelated steps while the blocked action waits. The same approach could support scheduled sessions later, but I haven't enabled that yet.

I also want the approval request to show the actual command. "Restart the service" leaves too much interpretation between what I approve and what the agent eventually runs.

Freeze, post, reply, release

The flow is deliberately small:

  1. The agent writes the proposed command to a file. The approval script copies it into its state directory, makes that copy read-only, and records its SHA-256 hash and expiry.
  2. The script sends a DM containing a summary, the full command, a hash prefix, a proposal ID and the expiry time.
  3. I reply in that message's thread. A correction needs a new proposal.
  4. The script polls the thread. When it accepts an approval, it marks the proposal as used under a file lock and returns the frozen command. The agent is instructed to execute that command unchanged.

A synthetic example looks like this:

[approval A42] staging: scale demo-api to 3 replicas to absorb the queue backlog
    kubectl scale deployment/demo-api --replicas=3 -n demo --kubeconfig ~/.kube/staging
sha256 <hash-prefix>. Reply in this thread `A42 ok` or `A42 no`.
Expires 2026-11-13 14:30 UTC.

One proposal holds one operational action. If the agent needs three unrelated changes, it sends three proposals. I don't want to approve the first line and accidentally agree to everything below it.

The hash binds the proposal to the saved command text. It doesn't freeze the environment in which that command runs. In this example, the kubeconfig could change while I'm deciding. A command that invokes a script also depends on whatever that script contains when it executes.

What counts as approval

The script checks the configured approver's Slack user ID and reads replies only from the proposal's thread. The approver's identity comes from configuration, not from something the agent writes in its request.

It accepts a small vocabulary: ok, yes, approve, no or reject, optionally preceded by the proposal ID. "ok, but use 2 replicas" is not approval. The agent needs to prepare a new command and ask again.

The saved command is hashed again before approval is released. A mismatch produces CHANGED. Making the copy read-only helps prevent accidental edits; it doesn't make the file tamper-proof against a process that controls the same account or can replace the saved metadata.

A proposal expires after four hours. An approval posted before the deadline can be collected during a ten-minute grace period afterwards, to allow for polling delay. A reply posted after the deadline doesn't revive it. That grace period also means execution can happen after the original expiry, so the expiry isn't a guarantee that the command still makes sense against current infrastructure.

The last valid reply observed before the proposal is consumed wins. I can send no after ok while it is still pending. Once the script has released the command, a later message isn't a cancellation mechanism.

A detail worth getting right: edited replies

Slack distinguishes a message's original timestamp from its edit timestamp. Filtering out message subtypes alone doesn't reject edited replies. An old message changed to ok after the deadline could otherwise look like an approval posted in time. Slack documents the separate edited field here.

For this workflow, I would reject edited replies and require a fresh message. This is a revised example of the reply filter, not the complete approval script:

jq -r --arg u "$APPROVER_ID" --arg p "$PROPOSAL_TS" \
      --arg id "$ID" --argjson exp "$EXPIRES" '
  if .ok != true then error("Slack API request failed") else . end
  | [ .messages[]
      | select(.user == $u
               and (.bot_id | not)
               and (.subtype | not)
               and (.edited | not)
               and ((.ts | tonumber) > ($p | tonumber))
               and ((.ts | tonumber) <= $exp))
      | (.text | ascii_downcase
               | gsub("^\\s+|[\\s.!]+$"; "")) as $text
      | (($id | ascii_downcase) + " ") as $prefix
      | (if ($text | startswith($prefix))
         then ($text | ltrimstr($prefix)) else $text end) as $vote
      | select(["ok", "yes", "approve", "no", "reject"]
               | index($vote))
      | {ts: (.ts | tonumber), vote: $vote}
    ]
  | sort_by(.ts) | last | .vote // ""'

The surrounding code still needs to fetch the correct DM and thread, handle pagination, validate the local state and expiry, and consume the approval under a lock. A failed API call or malformed response must stop approval processing. It cannot become permission to run.

One approval can be consumed once. Execution is a separate question.

The script reports its decision through exit codes:

Exit code Result Meaning for the agent
0 APPROVED Receives the frozen command for execution
3 REJECTED Drops the proposed action
4 EXPIRED Drops it; a new proposal needs fresh evidence
5 CHANGED Stops and reports that the saved command changed
6 USED Does not reuse this approval
10 PENDING Waits or works on an independent step

USED means the approval was consumed. It doesn't mean the command ran successfully, or even started.

If the script marks a proposal used and the session crashes before execution, the action may never happen. If a command changes the system but its result gets lost, retrying it could repeat the change. The lock prevents two checks from consuming the same proposal; it doesn't control what the agent does with the returned command.

That's why I wouldn't describe this as exactly-once execution. After an ambiguous result, inspect the target before retrying. Prefer idempotent operations where possible, and get a new approval if another attempt is needed.

Why polling instead of buttons?

Buttons would be nicer. They also need a running handler, using either an HTTP endpoint or a Socket Mode connection. Socket Mode avoids exposing a public HTTP endpoint, but it still needs a process to receive interactions.

The polling version uses a Slack bot token, curl, jq, hashing tools and flock on the Linux host. It runs only while a proposal is open. That was enough for my current volume without adding another service to operate.

I poll every 30 seconds. That interval needs to fit the app's API limits, especially if several proposals are waiting. Slack's conversations.replies documentation gives internal customer-built apps Tier 3 limits, while commercially distributed apps outside the Marketplace can face a one-request-per-minute limit. Handle rate limits and Retry-After; don't assume this interval works for every installation.

A crash while waiting should leave the action blocked. A crash around approval consumption needs the more careful recovery described above.

The boundaries I still rely on

The script checks an environment label against a non-production allowlist. It doesn't parse the command and prove what infrastructure it targets. A proposal labelled "staging" could still contain a command pointing at production. Reading the command helps, but the enforceable boundary is a credential that cannot write to production.

The agent can also bypass this workflow if its tools already let it execute arbitrary commands with write access. Instructions to ask first are useful, but they don't remove that capability. A stronger design would put write credentials behind a separate executor that validates approval and runs the approved artifact itself.

Commands go to Slack as written, so they must not contain inline secrets. References to protected files or environment variables can keep secret values out of the message, but those values remain part of the execution context. They aren't covered by the command hash.

Finally, the Slack account is part of the approval chain. Someone controlling that session can answer as me. I'm willing to use that arrangement for the limited non-production workflow described here. I haven't extended it to production.

What this changes for me

The useful part is being able to leave a session without turning a routine approval into a reason to stay at my desk. The agent prepares the action, I review it where I can respond, and the blocked step can continue.

I can see this fitting between two workflows I already understand: agents that only investigate and prepare changes, and narrowly scoped automation that can run known operations without asking each time. Moving between them depends on the action, credentials and recovery path, not on whether the approval arrives in a terminal or a chat window.

For now, the small script solves the interruption problem. The permissions around it still decide what the agent can actually change.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.