$ fssg build --deterministic --validate |

How to Reuse FACET Prompt Fragments with Anchors and Aliases

FACET

Repeated instructions look harmless until they diverge. One copy gains a safety rule, another keeps the old wording, and a third is edited for a single workflow. The model then receives different policy depending on which branch assembled the prompt.

FACET anchors and aliases solve a narrow version of this problem: reuse one already-declared value inside one compiled document. They do not create a package system, fetch remote templates, or mutate shared state at runtime. That limited scope is exactly why they are useful.

The smallest useful example

Suppose two examples need the same review policy:

@examples
  policies:
    - &review_policy { mode: "read-only", require_evidence: true, max_files: 20 }
    - *review_policy
  fallback: *review_policy

The anchor &review_policy names the value on its line. Each *review_policy is replaced with that value during canonization. Anchor metadata disappears from the emitted representation; consumers receive ordinary JSON.

With the reference CLI installed from the repository, the basic check is:

facet lint contract.facet
facet canon contract.facet > contract.canonical.json

The important artifact is not the pretty source alone. It is contract.canonical.json: the exact structure handed to the next layer. This follows the broader FACET language model and its separation between authoring syntax and canonical execution input.

Why copy-and-paste is a contract risk

Duplication creates several identities for what engineers still describe as “the same rule.” Text diffs can detect changed bytes, but they cannot tell whether divergence was intentional. An alias removes that ambiguity inside the document: there is one definition and multiple resolved uses.

That gives a reviewer three concrete checks:

  1. Where is the value defined?
  2. Which nodes reuse it?
  3. What did the compiler emit after resolution?

This is different from asking the model to remember a previous paragraph. Reuse happens before inference. The compiler, not the model, resolves identity.

It also differs from FACET imports. Imports compose files and have explicit merge or replacement semantics. Anchors reuse a value after imports, variable resolution, and conditional filtering. If a rule belongs to a shared library, use an import. If a value is repeated within the assembled document, an anchor is often the smaller mechanism. The distinction matters because deterministic import merging has a wider blast radius than local alias substitution.

Resolution order is part of the behavior

FACET v1.1 Draft r3 defines this relevant sequence:

imports
→ variables
→ variable type checks
→ conditional filtering
→ anchors and aliases
→ lenses
→ canonical JSON

An alias therefore points into the document that survived earlier compile-time decisions. A conditionally removed node should not leave a magical runtime reference behind. A lens receives the resolved value, not unresolved anchor syntax.

This ordering prevents a common configuration-language failure: two implementations applying individually reasonable transformations in different orders and producing different contracts. The pipeline is part of the language semantics, not an implementation detail.

Fail closed on identity errors

A reuse mechanism is trustworthy only if ambiguous identity is rejected. The specification assigns explicit failures:

Situation Required result
Alias names no anchor F201
Alias cycle F201
Anchor name is defined twice F202

Do not “helpfully” choose the nearest definition or leave an unresolved string for the runtime. Both behaviors turn a compiler error into hidden policy drift.

A minimal negative test belongs beside every important shared fragment:

@examples
  first: *missing_policy
if facet lint invalid.facet; then
  echo "expected an undefined-alias failure" >&2
  exit 1
fi

The current Python reference implementation collects anchors first and then substitutes aliases. Its tests check that anchor markers disappear and that aliases resolve to equal JSON values. The repository is still explicitly marked as a draft/alpha implementation, so this is evidence of current semantics—not a claim of production maturity.

A practical review pattern

I would integrate anchors with three files, not with trust in source readability alone:

contracts/
├── agent.facet
├── agent.canonical.json
└── test_agent_contract.py

agent.facet is the human-maintained source. agent.canonical.json is a review artifact regenerated in CI. The test asserts properties that matter after compilation:

import json

contract = json.load(open("contracts/agent.canonical.json", encoding="utf-8"))
policy = contract["examples"]["policies"][0]

assert contract["examples"]["fallback"] == policy
assert "&" not in json.dumps(contract)
assert "*" not in json.dumps(contract)

The last two assertions are deliberately crude but useful: execution artifacts should not leak authoring metadata. Stronger tests should inspect the exact paths and values the runtime consumes.

In CI, regenerate the canonical file, compare it with the committed artifact, and fail on a diff:

facet canon contracts/agent.facet > /tmp/agent.canonical.json
diff -u contracts/agent.canonical.json /tmp/agent.canonical.json

That turns a local convenience into a reviewable contract change. It also complements the FACET CI contract gate: linting proves the document is accepted, while the canonical diff shows what acceptance means for downstream code.

Where anchors should stop

Anchors are not inheritance. There is no implicit override chain, no late binding, and no runtime lookup. Trying to turn them into an object model would reintroduce the ambiguity they remove.

I use this boundary:

  • Anchor when the same complete value is reused locally.
  • Variable when a scalar or structured input is supplied explicitly.
  • Import when a reusable module belongs in another file.
  • Lens when a value needs a pure transformation.
  • Host code when behavior depends on I/O, authorization, time, or external state.

If two policies are almost the same, an alias may be the wrong abstraction. Their difference could be the important part. Name both values or move the shared invariant into a smaller explicit structure rather than hiding overrides.

The deeper point: reuse must preserve inspectability

Most prompt reuse systems optimize authoring effort. That is not enough. The compiled result must remain inspectable, diffable, and testable without asking a model what the source “probably means.”

FACET anchors are valuable because they disappear. They reduce duplication in source while producing plain canonical data for execution. The runtime does not need an alias resolver, and the audit trail does not depend on source-level shorthand.

That is the pattern I want from an AI contract language: expressive authoring on one side, reduced semantics at the execution boundary on the other. Canonical JSON as an intermediate representation is what makes that reduction operational rather than rhetorical.

Use anchors for local identity, test the emitted artifact, and reject every unresolved or duplicate name. Reuse should make a contract smaller to author—not harder to prove.

Sources

FACET AI CLI Terminal
facet@ai:~$