SPEC-016: Agent wrappers¶
- Status: Implemented
- Milestone: v0.9 "Build your own"
- Owners: lead maintainer
1. Purpose¶
Let users add behaviour to any agent (memory, a persona, extra instructions, help with the reply format) without writing a new agent class. Today each of these needs a subclass of one specific adapter, as the memory and "chatty" agents written for the Colab experiments did. A wrapper is a small reusable piece that sits between the runner and an agent: it may change what the agent sees before it acts, and what it sends back afterwards. Wrappers stack, work with every agent kind, and are recorded in the log, so comparisons stay honest.
2. Terms¶
Keywords MUST, SHOULD and MAY follow RFC 2119.
- Wrapper: an object with before(request) and after(request, reply) hooks around one
agent's act().
- Inner agent: the agent a wrapper stack wraps (any adapter or user agent, SPEC-002).
- Stack: the ordered list of wrappers on one agent, written as wrap: in a scenario.
- Seen request: the Request after every before hook ran, which is what the inner agent
actually receives.
3. Requirements¶
Interface¶
- WRP-001
coreMUST provide a base classWrapperwithconfig,reset(seed, agent_id),before(request) -> requestandafter(request, reply) -> reply; both hooks default to returning their input unchanged. LikeAgent, it lives incoreand imports no adapter. - WRP-002
coreMUST providewrap(agent, wrappers) -> Agent, which returns an agent that runs thebeforehooks in list order, the inner agent'sact(), then theafterhooks in reverse order (the first wrapper is the outermost). The result MUST be usable anywhere an agent is (run_episode, scenarios, tournaments, judges). - WRP-003 A wrapped agent MUST keep the inner agent's identity: the same
kind, model, capabilities and secrets.resetMUST reach the inner agent and every wrapper, so per-episode state (such as memory) starts empty each run. - WRP-004 Wrappers MUST receive only the Request and the Reply: never the world, the log, other agents or the runner (AGT-008, EVL-003). The runner MUST still check every action against the world, so a wrapper cannot make an illegal move legal.
- WRP-005 A
beforehook MUST NOT changeagent_id,step,actionsorlimits; it MAY changeobservation,rules,notes,inboxandlast_error. A change to a forbidden field MUST raise an error naming the wrapper and the field. - WRP-006 An
afterhook MUST keep the inner reply'susage(so cost limits keep working) and MAY add to it when the wrapper made its own model calls; the runner sums them. It MAY changeaction,sayandreasoning; any change toactionMUST be recorded (WRP-009). - WRP-007 An exception raised by a wrapper MUST be reported as an
AgentErrorwhose message names the wrapper, so it counts like any agent failure (RUN-010) and never crashes the run. - WRP-008 Wrappers MUST be deterministic given the seed and their inputs; a wrapper that
needs randomness MUST draw it from the seed passed to
reset.
Scenarios and the log¶
- WRP-009 An agent entry in a scenario MAY have
wrap:, a list whose items are a wrapper name or a one-key map{name: config}. Names resolve like agents: a.pyfile next to the scenario, a local name, an installed plugin in the entry-point groupagentplaykit.wrappers, or a built-in (SPEC-006).agentplaykit checkMUST validate the names without importing plugin code (SCN-013). - WRP-010
run_start.agents[]MUST record the stack aswrap(names and redacted config). When wrappers changed the request, theobservationevent MUST also carryseen: the fields of the seen request that differ from the world's (for examplenotesor the observation). When anafterhook changed the action, theactionevent MUST carrychanged_bywith the wrapper's name. The viewer's "What x saw" MUST show the seen request when there is one. - WRP-011 A competitor's key (RPT-002) MUST include its wrappers when it has any and no
name, for exampleollama:qwen2.5:3b+memory, so wrapped and unwrapped runs of the same model are rated separately. Reports MUST list each competitor's wrappers.
Built-in wrappers¶
- WRP-012 The framework MUST ship
memory: before each turn it adds to the request'snotesone line,Your last turns: [...], with the agent's lastturnsturns (default 10): the step, the messages it received, its action, what it said, its reasoning, and why the action was rejected if it was. The observation keeps its shape, so agents that read it (scripted ones included) keep working. - WRP-013 The framework MUST ship
instructions: it adds itstextto the request'snotesevery turn, for personas ("You are a cautious player"), talking ("Send one short message to the other player every turn") or house rules. - WRP-014 The framework MUST ship
format_hints: after a rejected reply (the request has alast_error), it adds tonotesa short example of a valid reply built from the legal actions or their schema, for example{"action": 4}or{"action": "your argument as text"}. It changes nothing on turns without an error. - WRP-015 The built-in wrappers MUST work with every agent kind, including scripted ones, and MUST NOT call a model.
Docs and fairness¶
- WRP-016 The docs MUST explain that wrapping only one side of a match changes what is being compared, and SHOULD show how to give every agent the same stack.
4. Acceptance criteria¶
| Requirement | Test ID | Notes |
|---|---|---|
| WRP-001..008 | WRP-001..WRP-008 |
order of hooks; identity; reset; forbidden fields; usage kept and summed; wrapper exceptions as agent errors; determinism |
| WRP-009..011 | WRP-009..WRP-011 |
wrap: in scenarios with file, local, plugin and built-in names; check imports nothing; wrap, seen and changed_by in the log; report keys |
| WRP-012..015 | WRP-012..WRP-015 |
each built-in against scripted agents and a fake model server; memory resets between runs |
| WRP-016 | WRP-016 |
docs page check |
5. Examples¶
agents:
- id: qwen
use: ollama
config: {model: "qwen2.5:3b"}
wrap:
- memory: {turns: 6}
- instructions: {text: "Send one short message to the other player every turn."}
- format_hints
- id: llama
use: ollama
config: {model: "llama3.2:3b"}
wrap: [memory, format_hints] # give both sides the same help
A wrapper of your own, in my_wrappers.py next to the scenario (wrap: [my_wrappers.py:Polite]):
from agentplaykit.core import Wrapper
class Polite(Wrapper):
def before(self, request):
request["notes"] = [*request["notes"], "Be polite to the other player."]
return request
6. Open questions¶
- Wrappers that call a model themselves (for example summarising a long memory): allowed by WRP-006, but no built-in does it yet.
- Whether
format_hintsshould become the default for small local models.
7. Change log¶
- 2026-12-01: Initial draft.
- 2026-12-01: Accepted by the lead maintainer for v0.9.
- 2026-12-01: WRP-012: memory goes into
notes, not the observation, found while building: changing the observation's shape broke scripted agents that read it (WRP-015). - 2026-12-03: Implemented in v0.9.0; every requirement has a test.
Changes to other specs on acceptance¶
- SPEC-003, new LOG-025:
run_start.agents[].wrap,observation.seenandaction.changed_by(additive;log_versionstays 1). - SPEC-005, new SCN-017: the
wrap:key on agent entries and tournament entrants. - SPEC-006, new REG-012: the entry-point group
agentplaykit.wrappersand built-in wrappers. - SPEC-013, RPT-002: competitor keys include wrappers (WRP-011).