Harness Engineering Guide: Building an Auditable Agent Runtime Environment

    This guide explains how to build an auditable runtime environment for AI agents using Harness, the supporting layer outside the model. It covers rules, tools, permissions, checkpoints, audit logging, rollback, cost and quality monitoring, and practical safeguards for repeatable code tasks.

    1. Conceptual Boundaries

    Harness refers to all supporting infrastructure outside the model. It includes rules files, tool integration, task decomposition order, checkpoints, and permission boundaries. A common simplified formula is: agent ≈ model + Harness. The model handles reasoning and generation, while Harness maps reasoning results to the file system, terminal, browser, and external tools, and restricts the scope of those operations.

    Component Role Example
    Rules file Declares goals, constraints, output format, and acceptance criteria harness-rules.yaml
    Tools Interfaces through which the model calls external capabilities Code search, test runs
    Permissions Limits what can be read, written, and executed Command allowlist
    Checkpoints Verifies results after each step File existence checks, unit tests
    Audit Records operations and supports rollback Operation logs, task snapshots

    Harness is suitable for repetitive code tasks, such as generating structure descriptions, adding tests, and organizing documentation. It is also suitable for team processes that require traceability. Harness cannot replace human judgment. For large-scale refactoring without test coverage, or operations that affect production data, keep human confirmation in the loop.

    2. Environment Setup and Credential Configuration

    Before running, prepare Python 3.10 or later, Git, and a terminal with network access. First confirm the versions:

    1. python3 --version
    2. git --version

    Create a virtual environment outside the project to avoid polluting the system environment with dependencies:

    1. python3 -m venv .venv
    2. source .venv/bin/activate
    3. pip install <framework-package-name>

    Replace <framework-package-name> with the package name given in the framework documentation. The parameters for starting the local console also follow the framework documentation; run --help first to see available options. The startup command will look similar to the following:

    1. <framework-command> console --workspace ./demo-project --mode sandbox

    Apply for credentials from the official platform and grant only the call quota required for the current project. Do not write credentials into code, configuration files, or logs. Inject them through environment variables instead:

    1. export HARNESS_API_KEY="key copied from the official platform"

    If you use a .env file to store local configuration, add it to .gitignore:

    1. .env
    2. logs/

    Credentials should be rotated regularly. The rotation cycle is decided by the team; a common practice is every 90 days. If credentials are leaked, revoke them on the platform immediately and apply for new ones.

    For the first run, do not use a real project directly. Copy a sandbox directory first and confirm that the workspace is clean:

    1. cp -r ./my-project ./demo-project
    2. cd ./demo-project
    3. git status --porcelain

    If git status --porcelain produces no output, the workspace has no uncommitted changes. Then run a read-only task in the sandbox and execute the command above again. If the output is not empty, the agent has modified files; inspect the rules and permission configuration first.

    3. Rules, Tools, and Permission Configuration

    3.1 Minimal Rules File

    The following rules file describes a read-only task. The field names are illustrative; actual fields follow the framework schema, but all four types of information must be present: goal, constraints, output format, and acceptance criteria.

    1. # harness-rules.yaml
    2. task:
    3. goal: Scan the src/ directory and generate a structure description
    4. constraints:
    5. - The src/ directory is read-only; do not modify any source code
    6. - Do not execute delete operations
    7. - Do not access the external network
    8. output:
    9. format: markdown
    10. path: docs/structure.md
    11. acceptance:
    12. - docs/structure.md exists
    13. - Each top-level directory has a description
    14. - The file does not contain secret-like strings
    15. checkpoints:
    16. on_fail: pause_and_ask
    17. max_auto_retry: 1

    on_fail: pause_and_ask means that when a check fails, the task pauses and requests human confirmation. The automatic retry limit is set to 1 to prevent the model from looping on the same error.

    3.2 Permission Allowlist

    Permissions are closed by default and opened item by item only when needed. The following configuration applies to the read-only task above:

    1. permissions:
    2. filesystem:
    3. read: ["./src", "./docs", "./tests"]
    4. write: ["./docs/structure.md"]
    5. delete: []
    6. terminal:
    7. mode: allowlist
    8. commands:
    9. - "git status"
    10. - "git diff"
    11. - "pytest tests/ -q"
    12. timeout_seconds: 120
    13. network:
    14. default: deny
    15. allow_domains: []
    16. secrets:
    17. source: env

    The command allowlist should contain only explicitly listed commands. High-risk commands such as rm and curl should not appear in the allowlist. Network access is denied by default; add domains only when truly needed.

    Temporary privilege escalation must follow these rules:

    • State the task ID and reason in the request.
    • Another team member must approve it; the requester cannot approve their own request.
    • It is valid for no more than 15 minutes and is automatically revoked on expiry.
    • Every operation during the escalation period is written to the audit log.
    • After the task ends, compare snapshots to confirm that no changes exceed the requested scope.

    Teams can set different permission templates for different roles. Junior developers default to read-only; senior developers can obtain temporary write permissions after approval.

    3.3 Tool Contracts

    Every tool must declare its input, output, scope, and timeout. A tool should not provide capabilities that are not stated in its contract.

    Field Description Example
    name Tool name, globally unique code_search
    input Type definitions for parameters {"query": string, "path": string}
    output Format of returned content File paths and line numbers; do not return full code text
    scope Directory range that can be accessed ./src
    timeout_seconds Timeout for a single call 10
    redact Filtering rules for sensitive information Replace values matching key, token, or password with ***

    Plugins must record their version number and compatible Harness version range. After a version upgrade, old plugins should be marked deprecated and removed after an agreed period. This prevents rules files from referencing tools that are no longer valid.

    4. Task Orchestration, Audit, and Rollback

    4.1 Split Tasks and Set Checkpoints

    Break a large goal into small steps that can be verified independently. The structure-description task above can be split into three steps: scan directories, analyze dependencies, and generate documentation. Run a checkpoint after each step. A checkpoint can be a file existence check, a unit test, or a static scan. If a check fails, pause the task and wait for human confirmation instead of retrying immediately.

    Before the task starts, the workspace must be clean. Record the current commit hash as a snapshot:

    1. git rev-parse HEAD

    Write this hash to the audit log so that rollback can locate the starting point.

    4.2 Audit Fields

    Every file read/write, command execution, and network access must be logged. At minimum, include the following fields:

    Field Description
    task_id Task ID
    timestamp Operation time in UTC ISO 8601 format
    actor Requester or agent identifier
    action read, write, delete, exec, or network
    target File path, command, or domain
    diff_sha256 Digest of content before and after the change, used for comparison
    command Original command, redacted
    cwd Directory in which the command was executed
    exit_code Command exit code
    duration_ms Execution duration
    snapshot_id Snapshot ID at task start
    approval_id Required for privilege escalation; records the approval ID

    Logs must not contain plaintext secrets. For production data scenarios, validate against a redacted copy first, and consider connecting real data only after confirming correctness.

    4.3 Rollback Checklist

    When an incident occurs, proceed in the following order:

    1. Pause the task and stop initiating new tool calls.
    2. Find the first anomalous operation in the audit log and record its time and snapshot_id.
    3. Run git status and git diff to list all changes.
    4. Check out the snapshot version in a new branch or temporary directory and verify files one by one.
    5. Have a human confirm which changes should be kept, then merge those changes separately.
    6. Rerun acceptance checks to confirm that the result is correct.
    7. Record the cause, impact scope, and rule items that need to be modified.

    Do not run a forced reset directly on the main branch. Verify all recovery operations in an isolated location before merging back to the main branch.

    4.4 Common Failures and Handling

    Symptom Possible cause Handling
    The same step repeatedly fails No retry limit Set max_auto_retry and pause after it is exceeded
    Files are unexpectedly modified Write permission scope is too broad Narrow the write list to output files only
    The task is unresponsive for a long time No timeout configured for the tool Configure timeout_seconds for each tool
    Documentation is inconsistent with code Missing acceptance criteria Add acceptance items that correspond to the code

    5. Cost and Quality Monitoring

    Metrics need clear definitions; otherwise, numbers cannot be compared. Record the following four items:

    • Task success rate: the number of tasks that pass all acceptance items divided by the total number of tasks in the same task set. A fixed set of 20 tasks is recommended as a benchmark set.
    • Average steps: the average number of tool calls per task.
    • Token consumption: read the usage field returned by the platform directly, and count input and output separately.
    • Human interventions: the number of times a paused task requires human handling.

    For latency, record both P50 and P95, measured end-to-end from task start to acceptance completion.

    When comparing the effects of different configurations, keep the model, reasoning depth, task set, and Harness version consistent, and record the test date. After modifying rules, rerun the same task set to re-evaluate.