Let `FunctionToolset.instructions` and instruction functions opt into `on_change='append'`
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.instructionstook onlyfunc. It now takesname=andon_change=, mirroring@agent.instructionsand@capability.instructionsexactly. A named function is keyedtoolset:<toolset id>:<name>. A named part on a toolset without anidwarns and rewrites, and an unnamed function gets the toolset's own key, just like an unnamed@capability.instructionsfunction.- 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=...)orFunctionToolset(instructions=...), and callables returned fromAbstractCapability.get_instructions(). Any instruction function may now return anInstructionPart, and itsnameandon_changeapply 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 onlyon_change. Returning a part with a different name raises aUserError. - A returned part always counts as
dynamic, whatever it says, because its text is computed. - Returning
Nonewithdraws 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.
- The name is qualified against the source the function belongs to. An unnamed agent callable becomes addressable as
Implementation:
SystemPromptRunneris now generic over its result (PromptT, which defaults tostr), so instruction functions can returnstr | InstructionPartand system prompt functions staystr.AgentInstruction's callable member isInstructionFunc(in_instructions, not exported). The three decorators gain overloads for functions returningInstructionPart; the existing overloads come first, so the types ofstrfunctions don't change.SourcedInstructiongainssource, 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