Skip to content

API Reference parrot_tools.tool_optimizations.contracts

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

parrot_tools.tool_optimizations.contracts

View source

Delegation-contract parsing and validation (FEAT-543).

This module decides — entirely offline, before any model call — whether a TASK file is eligible for delegation. A stale or incomplete contract is returned to the thinking model with a precise, actionable error; the delegate never explores the repository and never resolves a design gap.

A TASK file is eligible when it contains exactly one ## Delegation Contract section holding exactly one json fenced block that validates as a :class:DelegationPacket, and every implementation block that packet references exists in the same file, is free of placeholders, and matches the recorded revisions on disk.

design_complete: true is an eligibility declaration, not proof of correctness: this module checks structural completeness, while the thinking model verifies semantics.

Attribute ALLOWED_VALIDATION_PROGRAMS

Class CodeBlock

A labelled fenced code block inside a TASK file.

Fields:

Field Type Default Description
block_id str The id= attribute from the fence info string.
language str '' The fence's language token, if any.
text str '' The block's literal content.
start_line int The 1-based line of the opening fence.
path Optional[str] None The path= attribute, when the block is a file's content.

Class ContractError

A delegation contract is missing, malformed, stale or underspecified.

Parameter Type Default Description
code str The stable failure code.
message str '' A human-readable explanation.
details Optional[dict[str, Any]] None Structured context (block ids, paths, expected vs current).

Attributes:

Attribute Type Description
code A stable snake_case code identifying the failure.
details Structured context telling the thinking model what to fix.

Class ResolvedReference

A reference slice loaded from disk and checked for freshness.

Fields:

Field Type Default Description
slice ReferenceSlice The packet's declared reference.
content str The bounded excerpt actually read.
current_sha256 str The referenced file's current digest.

Class ValidatedContract

A TASK file proven eligible for delegation.

Fields:

Field Type Default Description
task_path str The repo-relative TASK file path.
packet DelegationPacket The validated delegation packet.
packet_sha256 str The packet's canonical identity.
blocks dict[str, CodeBlock] Only the implementation blocks the packet references.
references list[ResolvedReference] The loaded, freshness-checked reference slices.
targets_state dict[str, Optional[str]] Target path -> current digest, or None when absent.
context_bytes int The measured size of everything sent to the delegate.

Function extract_packet

Parse and validate the packet JSON block.

extract_packet(json_text: str) -> DelegationPacket

Parameter Type Default Description
json_text str The raw contents of the packet fenced block.

Returns: DelegationPacket — The validated packet.

Raises:

  • ContractError — The JSON is malformed or the packet is invalid.

Function find_placeholders

Find unresolved placeholder code inside an implementation block.

find_placeholders(block: CodeBlock) -> list[tuple[int, str]]

Parameter Type Default Description
block CodeBlock The block to scan.

Returns: list[tuple[int, str]] — A list of (line_number, pattern_name) tuples, relative to the block's own first line.

Function parse_task_file

Extract the packet JSON and every labelled code block from a TASK file.

The fence scanner is hand-written on purpose (no markdown dependency) and tracks fence length, so a shorter fence nested inside a block is preserved literally.

parse_task_file(text: str) -> tuple[str, dict[str, CodeBlock]]

Parameter Type Default Description
text str The TASK file's full text.

Returns: tuple[str, dict[str, CodeBlock]] — A (packet_json, blocks) tuple keyed by block id.

Raises:

  • ContractError — The delegation section or packet block is missing, duplicated, or a block id is duplicated or malformed.

Function render_prompt_sections

Render the deterministic prompt sections for a validated contract.

The output contains only the approved packet and its approved excerpts — never whole repository files and never host conversation history.

render_prompt_sections(contract: ValidatedContract) -> dict[str, str]

Parameter Type Default Description
contract ValidatedContract The validated contract.

Returns: dict[str, str] — A mapping with packet, references, blocks and acceptance sections, each a Markdown heading plus fenced content.

Function validate_contract

Prove a TASK file is eligible for delegation, or explain why it is not.

Everything here is deterministic and offline: no network, no model, and no command is ever executed.

validate_contract(policy: OptimizationPolicy, task_path: str, *, limits_override: Optional[WriterLimits] = None) -> ValidatedContract

Parameter Type Default Description
policy OptimizationPolicy The active policy, supplying the repository root and limits.
task_path str The repo-relative TASK file path.
limits_override Optional[WriterLimits] None Limits replacing the packet's own, when supplied.

Returns: ValidatedContract — The validated contract, ready for prompt rendering.

Raises:

  • ContractError — Any eligibility rule failed.

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