Skip to content

Template Synthesis

Template-based synthesis — Phase A.

Cluster: Uncategorised | Type: component | MCP Tools: 8

Overview

Template-based program synthesis using a three-stage matching pipeline with Thompson bandit selection. Given I/O samples and type signatures, it searches a registry of parameterized template families (numeric and agentic), fits parameters via subgraph matching, and verifies results via empirical functor checking. A coverage ledger tracks all synthesis attempts for analysis and arm performance tuning.

Experimental status: this is the Phase A advanced synthesis path. It is not enabled by default in the production-facing goal engine or the normal hyperdistillation capture path. Hyperdistillation only routes through this component when G6_ADVANCED_TEMPLATE_SYNTHESIS is enabled or a caller explicitly passes advanced_template_synthesis=True.

Production warning: do not enable this flag globally until replay and longitudinal benchmarks validate the workload-specific benefit. For launch users, keep template synthesis hidden behind hyperdistillation or explicit developer configuration; it is a reliability primitive, not a first-run product surface for non-technical users. If enabled, monitor the coverage ledger for template_a, template_d, and claude_headless attempts and compare outcomes against the default deterministic synthesis path.

Verification boundary: successful template synthesis means the fitted template matched the supplied examples under empirical checks. It requires at least three I/O samples and does not prove correctness for unseen inputs, distribution shifts, or high-stakes workflows. Treat generated artifacts as candidates that still need domain tests, holdout examples, and human review where consequences are material.

Headless fallback: Claude Code headless is controlled separately by the coordinator's enable_headless option and should remain disabled unless live cost and safety gates are in place.

Caveats and known limitations:

  • Template registry is static — new template families require code changes, not runtime registration
  • Thompson bandit selection has no decay — arms with early poor performance may be underexplored long-term
  • Empirical functor checking verifies examples only — it does not prove correctness for unseen inputs
  • Requests with fewer than three I/O samples are rejected because underspecified synthesis can otherwise produce misleading confidence
  • Coverage ledger is append-only with no pruning — can grow unbounded over long-running sessions
  • Headless Claude Code fallback incurs real API costs and has no built-in budget cap
  • Runtime-dependent Family-D templates declare requires_executor: true and raise [TEMPLATE_EXECUTOR_MISSING] when called without their required callable; they must not be treated as fully executed workflows from structural fit alone

Works well with: solver, goal_engine, formal_methods

Public API

TemplateRankingError(ValueError)

The runtime did not produce a usable template-ranking decision.

TemplateRankingDecision

Outcome of a template-ranking decision.

Field Type Default
ranked_template_ids list[str] field(default_factory=list)
rationale str ''
signals list[str] field(default_factory=list)
confidence float 0.0
completion_state str 'qualified-draft'
degraded bool False
diverged_from_floor bool False
raw_response str ''

TemplateSynthesisRuntime(Protocol)

The grounded template-ranking surface template_synthesis gains.

Methods:

rank_templates(io_types: TypeSignature, io_samples: list[tuple[Any, Any]], goal_context: str, survivor_ids: list[str]) -> TemplateRankingDecision

LLMTemplateSynthesisRuntime

Provider-neutral ranking runtime backed by G6's LLM caller interface.

Constructor:

Parameter Type Default
llm LLMCaller \| None None

Methods:

rank_templates(io_types: TypeSignature, io_samples: list[tuple[Any, Any]], goal_context: str, survivor_ids: list[str]) -> TemplateRankingDecision

TemplateSynthesisPlanner

Runtime-first template-ranking facade with a real deterministic fallback.

Constructor:

Parameter Type Default
runtime TemplateSynthesisRuntime \| None None

Methods:

rank(io_types: TypeSignature, io_samples: list[tuple[Any, Any]], goal_context: str, registry: dict[str, Template]) -> tuple[TemplateRankingDecision, list[dict[str, Any]]]

TemplateSynthesisCoordinator(AIBlock[TemplateRequest, SynthesizedArtifact, dict])

3-stage template synthesis with Thompson bandit selection.

Constructor:

Parameter Type Default
ledger CoverageLedger \| None None
seed int \| None None
enable_headless bool False
promotion_store PromotionStore \| None None
max_cost_usd float \| None None
global_cost_cap_usd float \| None None
library Any None

Methods:

infer(request: TemplateRequest) -> Result[SynthesizedArtifact]

TemplateSynthesisPatternRuntime

Load-bearing context-minimization mechanism for the ranking runtime.

Field Type Default
sample_limit int 32
goal_char_limit int 600

Methods:

minimize_io(io_samples: list[Any]) -> list[Any]

Bound the I/O-sample payload sent to the LLM (context minimization).

TypeSignature

Field Type Default
input_type str required
output_type str required

TemplateSpec

Field Type Default
template_id str required
signature TypeSignature required
dag_nodes tuple[str, ...] required
dag_edges tuple[tuple[str, str], ...] required
sample_io tuple[tuple[Any, Any], ...] required
param_schema dict[str, str] required
requires_executor bool False

TemplateRequest

Field Type Default
problem_signature str required
io_samples list[tuple[Any, Any]] required
io_types TypeSignature required
goal_context str required
budget_usd float required
holdout_samples list[tuple[Any, Any]] field(default_factory=list)

SynthesizedArtifact

Field Type Default
template_id str required
params dict[str, Any] required
callable_ref str required
verification str required
attempt SynthesisAttempt required
confidence float 1.0
verification_scope str 'empirical_fit_only'
agentic_evidence dict[str, Any] \| None None
completion_state str 'qualified-draft'
evidence dict[str, Any] field(default_factory=dict)

Methods:

is_promotable() -> bool

True only when verification went beyond training fit on supplied I/O.

TemplateSynthesisSkill

Field Type Default
name str required
pattern_slug str required
description str required
capabilities tuple[str, ...] required
triggers tuple[str, ...] required
risk_notes tuple[str, ...] required
executable bool False

Methods:

compact() -> dict[str, Any]

TemplateSynthesisSkillCatalog

Methods:

list_skills() -> list[TemplateSynthesisSkill]

get(slug: str) -> TemplateSynthesisSkill | None

Functions

summarize_template_synthesis_agentic_evidence(decisions: list[dict[str, Any]]) -> dict[str, Any]

Summarise the runtime-vs-fallback decision trace for template ranking.

agentic_planner_enabled(default_enabled: bool) -> bool

Decide whether the agentic template-ranking planner should be used.

deterministic_template_ranking(io_types: TypeSignature, io_samples: list[tuple[Any, Any]], registry: dict[str, Template], reason: str = 'deterministic fallback') -> TemplateRankingDecision

Real demoted fallback: Stage-1 type survivors ranked by Stage-2 structure.

validate_template_ranking(decision: TemplateRankingDecision, survivor_ids: set[str]) -> None

Reject malformed rankings (output-verification-loop).

applied_agentic_patterns() -> list[dict[str, Any]]

Return compact metadata for the template_synthesis-applied patterns.

get_skill_catalog() -> TemplateSynthesisSkillCatalog

MCP Tools

Operation Source
ops template_synthesis_mcp
help template_synthesis_mcp
synthesize template_synthesis_mcp
rank_templates template_synthesis_mcp
list_patterns template_synthesis_mcp
get_info template_synthesis_mcp
list_templates template_synthesis_mcp
source_health template_synthesis_mcp