Add `DenyUnanswered` harness capability to answer deferred calls

Merged
PY
17d ago
+710-58

Posted by Claude Code on Douwe's behalf.

Harness runs could pause forever on a held tool approval or an unanswered question. clai2 solved this locally, but harness users had no equivalent.

Changes:

  • Add pydantic_ai_harness.DenyUnanswered: it denies every held approval with ToolDenied(message) and answers deferred ask_user_question calls with question_message ("No one is available to answer in this run; continue without the answer."), so the run finishes instead of pausing on DeferredToolRequests. Other deferred calls stay deferred. Exported lazily from the package root.
  • SubAgents and DynamicWorkflow forward the parent run's DenyUnanswered to child runs by default, including durable children run inside the parent's workflow (DenyUnanswered is marked safe to add at runtime). This per-capability forwarding is interim: the general "inherit into child runs" hook in #13135 will replace it.
  • A delegation that a durable engine runs as a tool activity (a named sub-agent under TemporalDurability) gets no forwarded instance, because the activity's run context does not carry the parent's capabilities. find_deny_unanswered returns None there, so such delegations behave exactly as without the capability. A remote sub-agent run by a SubAgentWorker (TaskControl(remote_agents=...)) likewise gets no forwarded instance, since the worker runs the child with its own capabilities. Both limitations are documented; closing them belongs to #13135.
  • clai2 session permissions use the harness capability (through a small subclass that keeps the /permissions full hint for calls Permissions held) and drop their local class (no re-export shim).
  • Docs: README.md next to the code, docs/harness/deny-unanswered.md (its examples run in test_examples), capability tables, navigation, and the harness skill reference.

Headless clai2 behaviour change

clai2 sessions where nobody can be asked (no terminal picker or web approval cards bound, such as clai2 -p) now:

  • deny calls Permissions held with only the clai2 text ("No one could be asked to approve it here; /permissions full lets such calls run."), instead of "The user denied this command, so it did not run." followed by that text;
  • also deny held approvals Permissions did not hold (for example MCP tools or requires_approval=True tools), with the harness default message instead of leaving them deferred, since /permissions doesn't change them;
  • answer AskUser questions with the "no one is available to answer" text instead of leaving them deferred.

Attended terminal and web sessions don't bind DenyUnanswered and behave as on main.

Decisions on the open review questions

  • clai2 import path: no re-export shim for the old class.
  • Child forwarding: SubAgents and DynamicWorkflow forward the parent's DenyUnanswered by default, as interim until #13135. No root_capability fallback for durable activities.
  • AskUser plumbing: unanswered questions get their own configurable question_message instead of AskUser's "The user declined to answer" text.
  • Wording: approvals say no one was available to approve; questions say no one is available to answer.

Takeover

Taken over to finish: merged the latest main into the branch (no force pushes) and resolved the conflicts by keeping both sides (capability lists, imports, docs-example lists). Review follow-ups:

  • DenyUnanswered now finds AskUser questions by requests.kind(...) == 'question' plus AskUser's ownership metadata instead of the tool name, so a PrefixTools-renamed AskUser is answered and an unrelated tool that happens to be called ask_user_question and defers as an external call stays deferred. Both have regression tests.

No core pydantic_ai code changes.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code