Let `FunctionToolset.instructions` and instruction functions opt into `on_change='append'`

Merged
DO
1mo ago
+445-66

Follow-up to #8188, and the code half of the audit behind #10756. Before this PR, two kinds of dynamic instructions couldn't use on_change='append':

  • @FunctionToolset.instructions took only func. It now takes name= and on_change=, mirroring @agent.instructions and @capability.instructions exactly. A named function is keyed toolset:<toolset id>:<name>. A named part on a toolset without an id warns and rewrites, and an unnamed function gets the toolset's own key, just like an unnamed @capability.instructions function.
  • Instruction functions that aren't registered with a decorator had no way to declare a policy. These are callables passed to Agent(instructions=...), Capability(instructions=...) or FunctionToolset(instructions=...), and callables returned from AbstractCapability.get_instructions(). Any instruction function may now return an InstructionPart, and its name and on_change apply as if the function had been registered with them:
    • The name is qualified against the source the function belongs to. An unnamed agent callable becomes addressable as agent:<name> once the part it returns is named.
    • Either side asking for 'append' wins over the 'rewrite' default.
    • A function registered with name= can return an unnamed part to set only on_change. Returning a part with a different name raises a UserError.
    • A returned part always counts as dynamic, whatever it says, because its text is computed.
    • Returning None withdraws the block, like an empty string does. The policy is only known after evaluation, but withdrawal works because recorded append-mode blocks missing from the current instructions are withdrawn. The round-trip test covers it.

Implementation:

  • SystemPromptRunner is now generic over its result (PromptT, which defaults to str), so instruction functions can return str | InstructionPart and system prompt functions stay str.
  • AgentInstruction's callable member is InstructionFunc (in _instructions, not exported). The three decorators gain overloads for functions returning InstructionPart; the existing overloads come first, so the types of str functions don't change.
  • SourcedInstruction gains source, the source a name declared at evaluation is qualified against.

Tests: test_instruction_updates_survive_fresh_agent_and_serialized_history gains three source cases: toolset-function, agent-returned-part and capability-returned-part. Each runs seven turns (A, B, B, A, None, None, C) through serialized history, including the withdrawal-then-reappear path, with a byte-stable prefix. There are also targeted tests for the merge rules, the rename error, the unaddressable warnings, name validation, and a FunctionToolset callable returning a part.

Docs: docs/agent.md (instruction names, and how each way of registering opts in, with a runnable example), docs/toolsets.md, the docstrings, and the agent skill.

Not in this PR: run-level instructions= and MCP server instructions.

Checklist

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

šŸ¤– Generated with Claude Code