Terminal-Based AI Agents: A Controlled Workflow for AI-Assisted Development
Build a controlled, auditable workflow for terminal-based AI agents, from research and drafting to design adaptation, testing, and staging. This guide covers MCP integration, least-privilege policies, approval gates, audit logs, rollback procedures, and a phased approach to automation.
1. Why the Terminal Works as an Agent Workspace
The terminal gives AI agents a straightforward working environment: they can read and write files, run commands, and invoke scripts without navigating a complex interface. Developers describe a goal in natural language, and the agent breaks it into a sequence of commands to execute step by step. Each step leaves a text record that can be reviewed when something goes wrong.
The workflow in this guide has five stages: research collection, draft generation, design adaptation, validation, and staging with experiments. Each stage needs explicit inputs, outputs, and acceptance criteria. Without these boundaries, agents struggle to complete tasks reliably, and failures become harder to trace.
2. Connect Tools and Data Sources with MCP
MCP, or Model Context Protocol, is an open protocol that lets agent clients access external capabilities through a consistent interface. MCP servers can expose three types of capabilities:
- Tools: executable operations.
- Resources: readable data.
- Prompts: reusable prompt templates.
The client reads its configuration at startup to discover available servers.
Configuration locations and field names vary by client. The following example uses the common mcpServers format. Check your client’s documentation before using it.
{"mcpServers": {"web-fetch": {"command": "uvx","args": ["mcp-server-fetch"]},"design-library": {"command": "node","args": ["./tools/design-library/index.js"],"env": { "DESIGN_LIB_PATH": "./design/refs" }}}}
The first server fetches web pages; the second reads a local library of design references. Both run as local processes.
For services that require credentials, store secrets in environment variables or a secrets management service. Do not put them in configuration files or commit them to the repository.
3. Establish a Project Directory Structure
The pipeline relies on consistent directory conventions. Agents read and write only in designated locations, while reviewers can quickly find the artifacts produced at each stage.
site/├── content/│ ├── drafts/ # Stages 1 and 2: source excerpts and drafts│ └── posts/ # Approved articles├── design/│ ├── tokens.json # Design tokens: colors, type sizes, radii, shadows, motion│ └── refs/ # Approved reference screenshots and source snippets├── src/ # Page and component code├── tests/│ ├── e2e/│ └── visual/├── experiments/ # A/B experiment configuration└── agent/├── policy.yaml # Permissions, approval gates, and resource limits└── audit/ # Operation logs
4. Apply a Least-Privilege Policy
By default, the agent should only read source material, write drafts, and run tests. Publishing, changing experiment traffic, and modifying infrastructure require approval. The following policy describes these boundaries.
agent:allowed_paths:- content/- src/pages/- tests/- experiments/denied_paths:- .env- infra/- .github/workflows/network:allow_domains:- data.example-research.orgcommands:allow:- npm run build- npm test- npx playwright testdeny:- "git push --force*"- "rm -rf*"approval_required:- action: deploy_productionapprover: content-lead- action: change_experiment_trafficapprover: growth-ownerlimits:max_run_minutes: 30max_changed_files: 40
This file is a declaration, not an enforcement mechanism. A sandbox or CI system must enforce the actual restrictions. The sandbox limits filesystem and network access; CI checks branch and approval status. Asking an agent to follow rules in a prompt does not guarantee security.
5. Define Inputs, Outputs, and Failure Handling
| Stage | Input | Output | Acceptance criteria | Failure handling |
|---|---|---|---|---|
| 1. Research collection | Topic and allowlisted sources | Raw excerpts under content/drafts/<run-id>/ |
Every claim includes a source URL, and every source is allowlisted | Retry the same source twice with increasing delays; if it still fails, skip it and record the failure |
| 2. Draft generation | Raw excerpts and article template | Markdown draft with sources listed in frontmatter | Every numerical claim in the draft is traceable to the excerpts | Preserve the draft, mark it needs-review, and wait for human review |
| 3. Design adaptation | Draft and design/tokens.json |
New page under src/pages/ and screenshots |
Screenshot differences from the reference page stay below the team’s threshold | Discard the new page if it exceeds the threshold and retain the previous version |
| 4. Validation | Build artifacts | Test reports | Successful build, passing end-to-end tests, and no broken links | Stop on any failure; do not proceed to staging |
| 5. Staging and experiments | Validated build | Staging URL and production version identifier | Human approval | If approval times out, retain only the staging version |
6. Run the Pipeline
The following script connects the five stages. The agent run command is illustrative; replace it with the actual invocation for your agent tool.
#!/usr/bin/env bashset -euo pipefailTOPIC="${1:?Usage: pipeline.sh 'topic'}"RUN_ID="r-$(date +%Y%m%d-%H%M%S)"BRANCH="agent/${RUN_ID}"DRAFT_DIR="content/drafts/${RUN_ID}"git switch -c "$BRANCH"# Stage 1: Collect sources; write only to the draft directoryagent run --policy agent/policy.yaml --task collect --topic "$TOPIC" --out "$DRAFT_DIR"# Stage 2: Generate the draftagent run --policy agent/policy.yaml --task draft --in "$DRAFT_DIR" --out "$DRAFT_DIR"# Stage 3: Adapt the design; only src/pages/ is writableagent run --policy agent/policy.yaml --task design --tokens design/tokens.json --out src/pages/# Stage 4: Validatenpm run buildnpx playwright testnpx linkinator ./dist --recurse# Stage 5: Deploy to staging only. Production requires approval and is not deployed here.npm run deploy:staginggit add content/ src/pages/ tests/git commit -m "agent: ${RUN_ID} ${TOPIC}"echo "Branch ${BRANCH} is ready. Open a merge request and wait for approval."
The runtime environment needs Node.js, Python for the MCP server launched through uvx, Playwright browsers, and the required secrets configured as environment variables. Consult each tool’s documentation for version requirements.
7. Maintain Audit Logs
Record each tool call as one JSON line to support troubleshooting and compliance reviews.
{"ts":"2026-11-23T08:30:12Z","run_id":"r-20261123-083000","actor":"pipeline-agent","action":"write_file","path":"content/drafts/r-20261123-083000/a.md","policy_rule":"allowed_paths","result":"ok","approver":null}
Every entry must include:
- Timestamp.
- Run identifier.
- Actor.
- Action.
- Target path or command.
- Applicable policy rule.
- Execution result.
- Approver.
Store logs separately. The agent must not have write access to the audit directory.
8. Plan for Rollback and Circuit Breakers
- Isolate each run. Work on a dedicated branch. The agent must not push directly to the main branch.
- Revert faulty releases. If a problem appears after a merge, use
git revertto undo the corresponding merge commit, then redeploy the previous version tag. - Stop when limits are exceeded. Trigger a circuit breaker if execution exceeds
max_run_minutes, changed files exceedmax_changed_files, or the same batch fails validation twice consecutively. Stop immediately and notify the owner. - Keep experiments reversible. Start A/B experiments with a small share of traffic and retain a one-click off switch. Disabling the experiment should restore the original page.
9. Turn Design Preferences into Testable Rules
Instructions such as “keep it subtle” cannot be reliably evaluated by a script or agent. Replace them with measurable constraints before adding them to an automated workflow.
The following is a starting template. Adjust the values to match your team’s brand.
{"typography": { "h1": "32px/40px", "h2": "24px/32px", "body": "16px/24px" },"radius": { "max": "8px" },"shadow": { "levels": 2 },"motion": { "duration": "200-250ms", "easing": "ease-out" },"review": { "screenshotDiffThreshold": "Set by the team during initial acceptance review" }}
After each successful acceptance review, add the page screenshots and corresponding token changes to design/refs/. Keep the version history in Git so the team can review how its design patterns evolve.
10. Decide Which Tasks to Delegate
| Task | Suitable for automatic execution? | Approver | Notes |
|---|---|---|---|
| Collect public source material | Yes | None | Allowlisted sources only |
| Generate drafts | Yes | Editor | Drafts must receive human review |
| Change experiment traffic | Not without approval | Growth owner | Approval is mandatory |
| Deploy to production | Not without approval | Content lead | Approval is mandatory |
| Process personal user data | No, not by default | Data owner | Classify the data before deciding whether to grant access |
| Modify infrastructure configuration | No | Operations lead | Not permitted by the policy |
11. Start with Research and Drafting
Begin with stages one and two only, and have editors review drafts daily. Once source accuracy and draft quality are consistent, enable design adaptation. Consider automated staging and A/B experiments only after those earlier stages are stable.
For every additional stage, run the complete workflow in staging for at least one release cycle before moving on to the next stage.
