# expression

## Intent

A named renderer-neutral evaluable expression used by policies, defaults, invariants, and semantic conditions.

Exact selection subject:

```yaml
subject: expression
```

The current Runtime IR is evidence, not authority. Target improvements needed for a usable template contract are called out explicitly.

## Scope envelope

| Scope                 | Context                              |
| --------------------- | ------------------------------------ |
| `each`                | `expression: ExpressionContext`      |
| `all`                 | `expressions: ExpressionContext[]`   |
| collection candidates | `collects.group`, `collects.feature` |

For `collects.<dimension>`, the singular collector is added beside the plural selected-subject array. A collector is valid only when ownership is semantically provable.

## Context tree

| Context path                 | Type                                                                                      | Meaning / template use                               |
| ---------------------------- | ----------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| `expression.id`              | `string`                                                                                  | Opaque stable semantic identity.                     |
| `expression.name`            | `NameContext`                                                                             | Stable semantic identity and finite naming variants. |
| `expression.description`     | `string\|null`                                                                            | Human semantic description.                          |
| `expression.tags`            | `string[]`                                                                                | Semantic classifiers; empty when none.               |
| `expression.explanations`    | `ExplanationContext[]`                                                                    | Advisory usage/security/implementation notes.        |
| `expression.group`           | `SemanticLinkContext\|null`                                                               | Owning semantic group.                               |
| `expression.feature`         | `SemanticLinkContext\|null`                                                               | Canonical owning feature, when present.              |
| `expression.node`            | `ExpressionNodeContext`                                                                   | Normalized recursive AST root.                       |
| `expression.node.kind`       | `literal\|context\|duration\|comparison\|arithmetic\|logical\|unary\|function\|reference` | Explicit node discriminator.                         |
| `expression.node.value`      | `CanonicalValue\|ExpressionNodeContext\|null`                                             | Literal/unary/function input.                        |
| `expression.node.context`    | `string\|null`                                                                            | Named evaluation context.                            |
| `expression.node.path`       | `SemanticPathContext\|null`                                                               | Path inside named context.                           |
| `expression.node.operator`   | `string\|null`                                                                            | Comparison/arithmetic/logical/unary operator.        |
| `expression.node.values`     | `ExpressionNodeContext[]`                                                                 | N-ary operands.                                      |
| `expression.node.left/right` | `ExpressionNodeContext\|null`                                                             | Binary comparison operands.                          |
| `expression.node.function`   | `string\|null`                                                                            | Temporal function.                                   |
| `expression.node.time_zone`  | `string\|null`                                                                            | Timezone argument.                                   |
| `expression.node.duration`   | `DurationContext\|null`                                                                   | Duration literal.                                    |
| `expression.node.target`     | `SemanticLinkContext\|null`                                                               | Reusable expression link.                            |

## Usage rules

- Normalize structurally different current nodes into an explicit `kind` tree.
- Recursive expression refs remain bounded unless a future request protocol explicitly expands them.

## Runtime IR implications to audit

- Reusable expressions should gain consistent metadata/group ownership.
- Provide a normalized inspectable node discriminator.

## Template example — Jinja / predicate

```jinja
def {{ expression.name.snake }}(ctx):
{% if expression.node.kind == "comparison" %}
    return {{ render_expression(expression.node.left) }} {{ render_operator(expression.node.operator) }} {{ render_expression(expression.node.right) }}
{% endif %}
```

The example uses semantic context only. Any `renderType`/`render_type` helper shown is a pack-owned macro/partial consuming the documented `TypeContext`, not hidden Dryv context.

## Audit questions

- Can a real expression pack generate code without parsing raw IR storage conventions?
- Is any field here present only because the current IR happens to store it?
- Does any nested item need first-class selection of its own?
