Skip to main content
Glama

Apply Contract Proposal Patch

apply_contract_patch
Destructive

Verifies a completed contract's proposed patch against its SHA-256 baseline and atomically applies it via git apply—or dry-runs it—aborting on drift to prevent overwrites.

Instructions

Verify the cryptographic dispatch baseline SHA-256 lock and atomically apply a COMPLETED contract's proposed_patch to workspace_root via git apply (ADR-0007).

Purpose

Completes the zero-write proposal loop (write_mode="proposal") by allowing the requester host to dry-run or atomically land a worker's unified diff only when the local baseline has not drifted.

Strict Preconditions & Safety Gates

  1. Contract must be in COMPLETED status with write_mode="proposal", non-empty proposed_patch, and proposal_applied_at === null (refuses duplicate application with ALREADY_APPLIED).

  2. Baseline Drift Lock: Every existing pointer file touched by proposed_patch must currently match its exact dispatched SHA-256 in pointers_json, and newly created files must not already exist on disk. Any drift immediately aborts with status="PROPOSAL_CONFLICT" (zero fuzzy merging, zero overwrites).

  3. Runs "git apply --check" in workspace_root; when dry_run is false, executes "git apply", computes post-apply SHA-256 hashes into verified_sha256, and records proposal_applied_at.

Return Value

Returns { ok, contract_id, status: 'APPLIED' | 'DRY_RUN_OK' | 'ALREADY_APPLIED' | 'PROPOSAL_CONFLICT' | 'PRECONDITION_FAILED', dry_run, proposed_patch_sha256?, proposal_applied_at?, touched_files?, verified_sha256?, drifted_files?, git_check_error?, remediation?, message }.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "contract_id": "<contract_id>"
    },
    "name": "apply_contract_patch"
  }
}

Input Schema

NameRequiredDescriptionDefault
dry_runNoWhen true, verifies the cryptographic baseline SHA-256 lock and runs `git apply --check` without modifying any workspace files or database state (default: false).
agent_idNoOptional caller agent identifier for audit attribution (defaults to AGENT_NAME env var).
contract_idYesThe COMPLETED contract ID carrying a worker-submitted proposed_patch (write_mode="proposal", ADR-0007).

Schema Changelog

Changes observed during successful MCP inspections.

  1. Addedv0.6.0

TDQS

A4.4/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations declare destructive=true, readOnly=false, idempotent=false, but the description goes far beyond them: it discloses the baseline SHA-256 drift lock, refusal semantics with ALREADY_APPLIED, zero fuzzy merging/zero overwrites, the git apply --check pre-flight, and the post-apply database write of proposal_applied_at. An agent knows exactly what state changes and what aborts a run.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Front-loaded with the core action, then organized under Purpose / Preconditions / Return Value headers, which suits a high-stakes mutation. There is mild redundancy between the Purpose paragraph and the precondition list (baseline drift is mentioned twice), but no filler sentences.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

No output schema exists, yet the description enumerates every returned field and status value in detail, and covers side effects, failure modes, and remediation. For a destructive three-parameter tool, nothing an agent needs to call it safely is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so contract_id, dry_run, and agent_id are already documented in the schema. The description adds only the extra constraint that contract_id must reference a COMPLETED proposal-mode contract and restates dry_run's check-only behavior — useful reinforcement but not new semantic depth beyond the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource — atomically apply a COMPLETED contract's proposed_patch to workspace_root via git apply — and names the exact mechanism (ADR-0007, zero-write proposal loop). This is clearly distinguishable from siblings like verify_contract or fulfill_contract, which do not land patches.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Gives explicit when-to-use conditions: contract must be COMPLETED, write_mode="proposal", non-empty proposed_patch, and proposal_applied_at === null, plus a dry_run path for pre-flight verification. It stops short of naming an alternative tool to use when those preconditions fail (e.g. how to re-dispatch or inspect instead), so it is clear context without explicit exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.