Skip to content

API Reference parrot_tools.tool_optimizations.writer

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.tool_optimizations.writer

View source

Targeted patch generation for already-decided TASK work (FEAT-543).

:class:TargetedWriterToolkit delegates implementation, never design. It accepts a TASK file whose ## Delegation Contract proves the design is already complete, builds a bounded prompt from that packet alone, asks a configured :class:~parrot.clients.base.AbstractClient for a unified diff, validates the diff against the approved scope and the expected source bytes, and stores it as an artifact.

Safety properties:

  • No repository exploration and no design mode. The delegate sees only the packet, its approved reference slices and its decided implementation blocks — never whole files and never host conversation history.
  • An incomplete or stale contract costs zero model calls. Validation runs first; a ContractError returns to the thinking model unchanged.
  • No silent model switching. A client whose fallback model is enabled is refused at construction, and a response reporting a substituted model aborts the operation with no repair attempt.
  • Generation never touches a target file. The result is an artifact id plus a patch path; the thinking model reviews the hunks with the bounded reader and only then calls writer_apply.

Provider satellites stay lazy: importing this module pulls in no AWS SDK.

Attribute SYSTEM_PROMPT

Class TargetedWriterToolkit

Generate and apply validated patches for already-decided TASK work.

Parameter Type Default Description
repo_root str | Path The repository checkout this toolkit is confined to.
llm_client Optional['AbstractClient'] None The configured client, injected by MCP config.
limits Optional[WriterLimits] None Budgets for one generation.
expected_model_ids Sequence[str] () Additional model identities the provider may legitimately report (e.g. Bedrock's translated model id).
owns_client bool True When True, this toolkit opens and closes the client.
**kwargs Any {} Policy overrides and toolkit keyword arguments.

Attributes:

Attribute Type Description
arg_models dict[str, type[BaseModel]]
llm_dependent_tools frozenset
confirming_tools frozenset
auto_open bool

Method TargetedWriterToolkit.writer_generate

Generate a reviewed-patch artifact for an eligible TASK file.

Validates the TASK's delegation contract, calls the configured model with no tools and no history, validates the returned unified diff against the approved scope and current source bytes, and stores it. No target file is modified. The result carries an artifact id and a patch path for the thinking model to review before applying.

writer_generate(task_path: str) -> OperationResult

Parameter Type Default Description
task_path str Repository-relative path of the TASK file.

Returns: OperationResult — An operation result whose data carries artifact_id, patch_path, patch_sha256, targets, usage, repairs, configured_model and actual_model.

Method TargetedWriterToolkit.writer_apply

Apply a reviewed patch artifact to the worktree.

Every precondition is re-checked before anything is written: the reviewed patch hash, the packet identity, the delegation contract, the absence of staged targets, and each target's current revision. Files are then replaced one at a time through adjacent temporary files, with a recovery journal recording what was intended.

Multi-file mutation is not globally atomic. On failure, only files that still match exactly what this operation wrote are restored; a file a concurrent editor has touched is never overwritten, and the operation reports recovery_required instead.

Applying never stages, commits or pushes anything.

The confirm argument added over MCP records that a human approved this mutation; it is not authorization a model can grant itself.

writer_apply(artifact_id: str, reviewed_sha256: str) -> OperationResult

Parameter Type Default Description
artifact_id str The artifact returned by writer_generate.
reviewed_sha256 str The hash of the patch the caller actually read.

Returns: OperationResult — An operation result whose data carries applied, artifact_id and journal_path, or already_applied.

Function build_prompt

Build the delegation prompt from approved content only.

build_prompt(contract: ValidatedContract) -> str

Parameter Type Default Description
contract ValidatedContract The validated contract.

Returns: str — The prompt text: packet, references, implementation blocks and acceptance criteria, and nothing else.

Function build_repair_prompt

Build the single permitted repair prompt.

The delegate gets the same approved context plus a bounded diagnostic — never additional repository content, and never a second chance at a contract that was itself invalid.

The diagnostic is redacted (see :func:redact_local_content): the rejected patch is the model's own output and may be echoed back, but the local source it failed to match may not.

build_repair_prompt(contract: ValidatedContract, previous_patch: str, error: PatchError) -> str

Parameter Type Default Description
contract ValidatedContract The validated contract.
previous_patch str The rejected patch text.
error PatchError The rejection.

Returns: str — The repair prompt text.

Function redact_local_content

Strip local file content from a diagnostic before it leaves the machine.

The thinking model sees the full, unredacted diagnostic — it runs locally. The delegate does not: only the approved packet slices may be sent to the provider, so a mismatch diagnostic reports that a line differed, never what the local line said.

redact_local_content(details: dict[str, Any]) -> dict[str, Any]

Parameter Type Default Description
details dict[str, Any] The raw error details.

Returns: dict[str, Any] — A copy safe to place in a provider prompt.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally