Walkthrough: Approval task agent
samples/agents/05-PersistentTaskManager is the cleanest starting point for a stateful PiSharp.Agent application. It separates read-only tools (always allowed) from mutating tools (blocked unless --approve is passed) and persists state cleanly across runs.
- Runtime:
PiSharp.Agent+PiSharp.AI(stable, no experimental diagnostics) - Patterns demonstrated: Approvals & human-in-the-loop, Offline testing & live validation
Run it in 30 seconds
Section titled “Run it in 30 seconds”# 1. Offline read-only demo (inspects fixture without mutating disk)dotnet run --project samples/agents/05-PersistentTaskManager -- --workspace artifacts/task-demo
# 2. Persist a state change across runs with explicit --approvedotnet run --project samples/agents/05-PersistentTaskManager -- --persist-demo --approve --workspace artifacts/task-board
# 3. Deterministic self-test suitedotnet run --project samples/agents/05-PersistentTaskManager -- --self-test
# 4. Live model run with explicit approval enableddotnet run --project samples/agents/05-PersistentTaskManager -- --live --approve --model gpt-6.2-sol --api openai-responses --workspace artifacts/task-liveTools and approval boundary
Section titled “Tools and approval boundary”| Tool | Purpose | Guard |
|---|---|---|
list_tasks |
Reads persisted tasks.json in the workspace, falling back to fixture.json seed when no file exists yet. |
Read-only |
inspect_task |
Fetches one task by id and reports whether the board is already persisted (persisted: true/false). |
Read-only |
add_task |
Appends a unique task (id, title) and writes tasks.json. |
RequiresApproval: true |
complete_task |
Marks an existing task done and writes tasks.json. |
RequiresApproval: true |
Key code walkthrough
Section titled “Key code walkthrough”Inside Workflow.cs, the WorkflowSpec declares all four tools with explicit JSON Schemas and marks only the mutating tools with RequiresApproval: true:
public static WorkflowSpec Create(SampleContext context) => new("persistent-task-manager", "Persistent task manager", "Read the durable task board. Before changing task state, ask for approval. Never claim a change without the tool result.", [ new("list_tasks", "Read tasks.json or the fixture seed when no persisted board exists.", SampleJson.Schema(), (_, ct) => Load(context, ct)), new("inspect_task", "Read one task by ID from the durable board.", SampleJson.Schema("{\"id\":{\"type\":\"string\"}}", "id"), (a, ct) => Inspect(context, a, ct)), new("add_task", "Add a task and persist tasks.json.", SampleJson.Schema("{\"id\":{\"type\":\"string\"},\"title\":{\"type\":\"string\"}}", "id", "title"), (a, ct) => Add(context, a, ct), RequiresApproval: true), new("complete_task", "Mark a task done and persist tasks.json.", SampleJson.Schema("{\"id\":{\"type\":\"string\"}}", "id"), (a, ct) => Complete(context, a, ct), RequiresApproval: true) ], [new("list_tasks", new JsonObject()), new("inspect_task", SampleJson.Object("{\"id\":\"task-501\"}"))]);Notice the defense-in-depth rule inside Complete:
static async Task<JsonNode?> Complete(SampleContext c, JsonObject a, CancellationToken ct){ if (!c.Approved) throw new UnauthorizedAccessException("Approval required to modify tasks.json"); var board = (await Load(c, ct))!.AsObject(); var task = board["tasks"]?.AsArray().FirstOrDefault(x => x?["id"]?.GetValue<string>() == a.Text("id")) ?? throw new ArgumentException("Unknown task"); task["status"] = "done"; await c.WriteJsonAsync("tasks.json", board, ct); return new JsonObject { ["id"] = a.Text("id"), ["status"] = "done", ["path"] = "tasks.json" };}Two safeguards work together:
- The shared
SampleHostchecksRequiresApprovalbefore executing the tool and tells the model whether approval is active. - The tool handler itself re-verifies
c.Approvedand throwsUnauthorizedAccessExceptionif invoked directly without approval — so even custom entry points like--persist-democannot bypass the gate.
Try breaking it
Section titled “Try breaking it”- Omit
--approveon--persist-demo: rundotnet run --project samples/agents/05-PersistentTaskManager -- --persist-demoand verify it exits with code2(--persist-demo requires --approve) without touching disk. - Run
--persist-demo --approvetwice against the same--workspace: openartifacts/task-board/tasks.jsonafter the first run, then run the regular--demo --workspace artifacts/task-boardand confirminspect_tasknow returns"persisted": truewith"status": "done". - Supply a custom fixture: copy
samples/agents/05-PersistentTaskManager/fixture.json, add a new open task, and pass--input my-tasks.json --live --model gpt-6.2-sol --api openai-responses --prompt "Mark my new task complete"with and without--approve.