# Core templates

> This section covers the template and how an instance is raised from it.

Page: PAG · Templates
Canonical: https://banes-lab.com/pag/templates#templates-core

This section is stop 87 of 102 in the learning route. Previous: [06 - Orchestration invariants](https://banes-lab.com/pag/orchestration/orchestration-invariants.md). Next: [02 - Coordination templates](https://banes-lab.com/pag/templates/templates-coordination.md). It builds on [02 - Document structure](https://banes-lab.com/pag/guide/document-structure.md), [11 - The drop-in](https://banes-lab.com/disciplined-methodology/start/onboarding.md).

This section covers the template and how an instance is raised from it. A template record has four parts, a declared document type, the slots an instance fills, the constraints every instance must satisfy, and a body in which every slot appears by name, as shown in [A1·a template record](https://banes-lab.com/pag/templates#templates-core-panel-a). [A1·d template and sibling](https://banes-lab.com/pag/templates#templates-core-panel-d) shows how a template differs from a sibling instance. Raising an instance is a resolution in which each slot is substituted, a slot left unresolved is reported rather than guessed, and a value outside a slot's declared set is a violation. [A1·c resolution](https://banes-lab.com/pag/templates#templates-core-panel-c) lists the three outcomes. The record and [A1·b workflow body](https://banes-lab.com/pag/templates#templates-core-panel-b) are the grammar's own workflow template, read from its records rather than restated here.

### Contract, not content

An instance derived from a sibling inherits that sibling's accidents as a contract. Four parties produce four formats for one surface, each derived from a different sibling, and the check that later reads them derives its schema from a fifth. A sibling carries one instance's choices and a template carries the constraint, and a reader copying a sibling cannot tell which is which.

For this reason a template carries the contract, an instance resolves its slots, and a check reads the template. The second instance is raised from a template written for it, rather than from the first instance. In practice, a template is written the second time a shape occurs, before the second instance is written, in the order shown in [A1·e second instance](https://banes-lab.com/pag/templates#templates-core-panel-e). It declares its type, names every slot with whether it is required and, where the values form a closed set, that set, and it names the constraints every instance must satisfy so a check can read them. The body keeps only what every instance shares, with every varying value as a slot. Each new instance is raised by resolving the slots, and an instance whose resolution reports an unresolved slot or a violation is refused.

To check this, take a template and find a value in it that would be wrong for the next instance. That value is content rather than contract, and a slot is the repair. Then resolve the template with one slot missing, and a resolution that raises the instance anyway has guessed. A shape seen once has no template, because one instance cannot show which of its parts are invariant. A template raised from one instance is [premature abstraction](https://banes-lab.com/records/arch/premature-abstraction.md).

The slots fall into two kinds by who supplies the value. An instance slot is what this document is for, supplied when it is raised. A host slot is a fact about the tree the document will be walked in, namespaced by what it is a fact about, and resolved by the adapter rather than typed into an instance, so one template can be raised in any tree.

The constraints are the family's acceptance criteria, and a check over an instance reads them from the template, as [the drop-in](https://banes-lab.com/disciplined-methodology/start/onboarding.md) describes. What the template excludes is as deliberate as what it carries, so it names no model, no path and no tool, for the reasons described in [semantic operations](https://banes-lab.com/pag/guide/tool-invocation.md). A correction lands in the template and reaches every later instance, never in the instance where only its author would see it.

A1·a template record

```text
template:
type:        WORKFLOW                       # a declared document type · nothing else
title:       <what the family is for>
slots:
- name:        {WORKFLOW_NAME}             # the value an instance supplies
description: <what the slot holds>
required:    true
kind:        string
- name:        {project.governance_policy}   # a host fact · resolved by the adapter, never typed
required:    true
- name:        {limits.max_lines}            # a bound · resolved from the host's limits
required:    false
- name:        {scope}
required:    true
enum:        [<investigate>, <action>]     # a closed set · a value outside it is a violation
constraints:                                  # what every instance must satisfy · named, checkable
- declaration_required
- gate_per_node
- contract_reads_prior_output
- population_declared
- refusal_before_write
- invariant_has_objector
- no_autonomous_spawn
- single_source_of_truth
body: |
<the document, with every slot as {name}>
```

A1·b workflow body

```pag
---
name: {WORKFLOW_NAME}
type: WORKFLOW
version: 1.0.0
---

THIS WORKFLOW EXECUTES {WORKFLOW_PURPOSE}

%% META %%:
intent: "{WORKFLOW_INTENT}"
objective: "{OBJECTIVE}"
jurisdiction: {INPUT_SOURCE} and {OUTPUT_TARGET} | external: every other surface
recursion_limit: 2

# NODE 1 — {NODE_ONE_TITLE}   [epistemic · analysis · set-theory · yields: set]
@purpose: "read the input and see it through the analysis the workflow is for"
@genesis: existence
CONTRACT:
input:     {INPUT_SOURCE}
transform: READ_RESOURCE {INPUT_SOURCE} INTO input; ANALYZE_CONTENT input AGAINST {ANALYSIS_TARGET} INTO analysis
output:    analysis
HANDOFF GATE (evidence-bearing):
[check] input read from {INPUT_SOURCE} (evidence: the read returned content) over: {INPUT_SOURCE} measured: <read> / <declared>
[check] analysis produced (evidence: a count above zero)
[check] every entry of analysis names its source in input (evidence: no entry with an empty source)
result: pass → NODE 2 | empty → REPAIR (owner: NODE 1) | unknown → BLOCKED

# NODE 2 — {NODE_TWO_TITLE}   [epistemic · formalisation · computation · yields: procedure]
@purpose: "transform every item by one rule, preserving what the next node needs"
@genesis: transformation
CONTRACT:
input:     analysis from NODE 1
transform: FOR EACH item IN analysis: COMPOSE_ARTIFACT result FROM item USING {TRANSFORM_RULE}; APPEND result TO results
preserves: the source of every item
output:    results
HANDOFF GATE:
[check] one result per item (evidence: the two counts match) over: analysis measured: <transformed> / <items>
[check] every result conforms to {TRANSFORM_RULE} (evidence: VALIDATE_ARTIFACT passed on each)
[check] analysis unchanged (evidence: a witness read)
result: pass → NODE 3 | mismatch → REPAIR (owner: NODE 2) | unknown → BLOCKED

# NODE 3 — FINALISATION   [evaluative · representation · information-theory · yields: artifact]
@purpose: "persist the results once, refuse a stale destination, and report to the parties whose next work they create"
@genesis: constraint
CONTRACT:
input:     results from NODE 2
transform: PERSIST_ARTIFACT results TO {OUTPUT_TARGET}; REPORT_RESULT completion TO <the parties whose next work it creates>
output:    {OUTPUT_TARGET}
freshness: fingerprint(results) + fingerprint(this document)
HANDOFF GATE:
[check] {OUTPUT_TARGET} persisted (evidence: a read returns it) over: results measured: <persisted> / <results>
[check] completion reported (evidence: the report)
[check] entry count of {OUTPUT_TARGET} matches results (evidence: the two numbers)
refuse: {OUTPUT_TARGET} changed since it was read before PERSIST_ARTIFACT
standing: moved-set none
result: pass → TERMINATE | loss → REPAIR (owner: NODE 3) | unknown → BLOCKED

# CROSS-NODE INVARIANTS
INVARIANT prior-output-only: a node reads only the prior node's output over: every node binds: the workflow objector: [check] input names NODE n-1 or a slot
INVARIANT one-truth: one fact has one home across the nodes over: every artifact binds: the workflow objector: [check] entry count of the output matches results
INVARIANT no-spawn: no autonomous party is spawned over: every node binds: the workflow objector: none

REPORT:
subject: NODE 3
verdict: pass | fail | unknown
domain: declared <results> measured <persisted>
completion: saturated <bool> complete <bool> verified <bool>

```

A1·c resolution

```text
resolve <template> WITH <the values an instance supplies>

substituted   every {name} the instance supplied, replaced in the body
unresolved    [{OUTPUT_TARGET}]                        # still in the body · the instance is not ready
violations    [missing_required_slot:{OBJECTIVE},       # a required slot with no value
enum_violation:{scope}]                  # a value outside the slot's closed set

# an instance with a non-empty unresolved or violations list is not raised · nothing guesses a value
```

A1·d template and sibling

```mermaid
flowchart TB
template["A template record · type, slots, constraints, body"]
raised["An instance · raised by resolving every slot"]
check["A check · reads the constraints and the slot declarations"]
sibling["A sibling instance"]
copied["An instance copied from the sibling · inherits its accidents"]
template --> raised
template --> check
sibling -. the tempting path .-> copied
```

A1·e second instance

```mermaid
flowchart LR
one["One instance · an artifact"]
two["A second · the invariant half is now visible"]
template["The template is written before the second is"]
later["Every later instance is raised from it"]
one --> two --> template --> later
```

## Links to

- [Premature Abstraction](https://banes-lab.com/records/arch/premature-abstraction.md)
- [The drop-in](https://banes-lab.com/disciplined-methodology/start/onboarding.md)
- [Semantic operations](https://banes-lab.com/pag/guide/tool-invocation.md)

## Linked from

- [Integrating algorithms](https://banes-lab.com/pag/patterns/algorithm-integration.md)
- [Three encodings](https://banes-lab.com/disciplined-methodology/start/three-encodings.md)
- [Execute the template](https://banes-lab.com/disciplined-methodology/plan/execute-the-template.md)
