# Context overview

This directory documents the renderer-facing context that pack templates can request.

Use this folder in this order:

| Need                                                                                                         | File                                              |
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------- |
| Find a concrete template path such as `schema.fields[].name.snake`                                           | [catalog.md](/pack-builders/context/catalog)      |
| Understand derived helper facts such as `is`, `has_*`, `*_json`, path segments and generated representations | [derived.md](/pack-builders/context/derived)      |
| Understand a selectable semantic subject such as `schema` or `operation`                                     | the subject files below                           |
| Understand reusable nested contracts such as `NameContext`                                                   | [shared/](/pack-builders/context/shared-overview) |

Context discovery through `dryv context list|show` is documented in [derived.md](/pack-builders/context/derived).

## Subjects

| Subject              | File                                                               |
| -------------------- | ------------------------------------------------------------------ |
| `property.primitive` | [property.primitive.md](/pack-builders/context/property-primitive) |
| `property.enum`      | [property.enum.md](/pack-builders/context/property-enum)           |
| `property.composite` | [property.composite.md](/pack-builders/context/property-composite) |
| `property.literal`   | [property.literal.md](/pack-builders/context/property-literal)     |
| `property.union`     | [property.union.md](/pack-builders/context/property-union)         |
| `property.record`    | [property.record.md](/pack-builders/context/property-record)       |
| `property.tuple`     | [property.tuple.md](/pack-builders/context/property-tuple)         |
| `property.generic`   | [property.generic.md](/pack-builders/context/property-generic)     |
| `property.temporal`  | [property.temporal.md](/pack-builders/context/property-temporal)   |
| `schema`             | [schema.md](/pack-builders/context/schema)                         |
| `expression`         | [expression.md](/pack-builders/context/expression)                 |
| `policy`             | [policy.md](/pack-builders/context/policy)                         |
| `failure`            | [failure.md](/pack-builders/context/failure)                       |
| `event`              | [event.md](/pack-builders/context/event)                           |
| `operation`          | [operation.md](/pack-builders/context/operation)                   |
| `view`               | [view.md](/pack-builders/context/view)                             |
| `view.part`          | [view.part.md](/pack-builders/context/view-part)                   |
| `workflow`           | [workflow.md](/pack-builders/context/workflow)                     |
| `surface`            | [surface.md](/pack-builders/context/surface)                       |

## Pack configuration metadata

| Context | File                                            | Availability                                                          |
| ------- | ----------------------------------------------- | --------------------------------------------------------------------- |
| `pack`  | [metadata/pack.md](/pack-builders/context/pack) | Bounded values declared by the active pack's typed `inputs` contract. |

## Generation metadata context

`file` is separate from semantic/structural metadata.

| Context | File                                            | Availability                                                                                                                                         |
| ------- | ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `file`  | [metadata/file.md](/pack-builders/context/file) | One concrete rendered `PlannedFileArtifact`; carries planned symbols, paths, generated dependencies, exports, and renderer-useful artifact metadata. |

A symbol-bearing template should use `file.symbol` or `file.symbols.<key>` for the exported declaration name. Semantic names remain semantic identity and must not be recomposed to guess the generated symbol.

## Structural metadata contexts

These are **not selectable subjects**.

| Context       | File                                                          | Availability                                                                               |
| ------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| `application` | [metadata/application.md](/pack-builders/context/application) | Global bounded metadata for every invocation.                                              |
| `group`       | [metadata/group.md](/pack-builders/context/group)             | Collector metadata for `collects.group`; also owner metadata alongside `collects.feature`. |
| `feature`     | [metadata/feature.md](/pack-builders/context/feature)         | Collector metadata for `collects.feature`.                                                 |

They deliberately omit declaration-membership arrays such as all schemas, operations, features, groups, or views.

## Core rule

```text
generation metadata + global metadata + selected subject + scope
→ renderer context
```

Every rendered concrete file may request:

```text
file: FileContext
application: ApplicationContext
```

`file` is defined in [metadata/file.md](/pack-builders/context/file) and is derived from artifact planning planning, not Runtime IR.

Every invocation may additionally request:

The application root is deliberately bounded metadata, not an aggregate semantic graph.

Filters only change membership. They never change the item context contract.

Examples:

```text
schema + each
→ schema: SchemaContext

schema + all
→ schemas: SchemaContext[]

property.enum + each
→ property.enum: EnumPropertyContext

property.enum + all
→ properties.enums: EnumPropertyContext[]

view.part + each
→ view.part: ViewPartContext

view.part + all
→ views.parts: ViewPartContext[]
```

## Context composition rule

Each context contract has exactly one authoritative definition.

A subject file documents only:

- the context fields unique to that subject;
- relationships to shared contexts;
- relationships to another subject's full context;
- any local constraints on how that borrowed context is used.

Do **not** copy an existing context tree into another subject file.

Use references such as:

```text
NameContext
    → shared/name.md

SemanticLinkContext
    → shared/semantic-link.md

SemanticValueContext
    → shared/semantic-value.md

ExplanationContext
    → shared/explanation.md

LifecycleContext
    → shared/lifecycle.md

SemanticPathContext
    → shared/semantic-path.md

schema.fields[].value.property.primitive
    → full PrimitivePropertyContext from property.primitive.md

schema.fields[].value.property.enum
    → full EnumPropertyContext from property.enum.md

schema.fields[].value.schema
    → full SchemaContext from schema.md
```

"Full context" means the exact same typed contract. It does **not** mean Dryv eagerly materializes the entire subtree. Template analysis and context resolution still request/materialize only paths actually used by the template.

This rule is important because one semantic concept must not slowly acquire several almost-identical template contracts.

## Proposed template-normalization rules

### Absence

When a requested semantic value is absent:

- optional scalar/object → `null`;
- collection → `[]`.

Template analysis and context resolution still control whether a path is materialized at all.

### Named maps become arrays

Canonical IR may store dictionaries for efficient lookup, but template context should expose named members as deterministic arrays:

```text
IR map key
→ item.name + deterministic array entry
```

This avoids renderer-specific map-key tricks such as Handlebars `@key` and makes the same context usable from Jinja, Liquid, Mustache, etc.

### Raw JSON pointers are not the template API

Templates should not parse:

```text
#/schemas/users.User
#/operations/users.CreateUser
```

References resolve to bounded semantic links.

## Shared contexts

Shared nested contracts live in [shared/](/pack-builders/context/shared-overview). They are not selectable roots, but they appear inside many subject and metadata contexts.

Every first-class semantic subject context exposes `id: string`. `id` is semantic identity and is deliberately separate from [NameContext](/pack-builders/context/shared-name).

### TypeContext

`TypeContext` remains the renderer-neutral recursive type-expression contract used where Dryv meaning is itself a type expression, for example union members, record keys/values, tuple items, and generic bodies/arguments.

Schema/composite fields do **not** use an ambiguous generic `TypeContext` for their primary value. They use [SemanticValueContext](/pack-builders/context/shared-semantic-value), because a field specifically distinguishes:

```text
property
schema
unknown
dynamic
```

This preserves the canonical difference between "field uses another schema" and "field uses a reusable property with its complete constraints/meaning."

Target-language spellings never belong in either contract.

### ValuePortContext

| Path    | Type                                                | Meaning                                                                                                                         |
| ------- | --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `index` | `integer`                                           | Stable order/binding index.                                                                                                     |
| `name`  | [`NameContext`](/pack-builders/context/shared-name) | Semantic parameter/result name.                                                                                                 |
| `value` | `SemanticValueContext`                              | Full reusable semantic value carried by the port; see [shared/semantic-value.md](/pack-builders/context/shared-semantic-value). |

Ports carry an optional canonical `name`; unnamed ports project as `input_0`, `output_0` (decision 0012).

## First-class metadata consistency

The pack contract allows `tags` on every selectable subject. Therefore every selectable subject context should consistently expose `id`, `name`, `description`, `tags`, and `explanations` where meaningful.

Owned semantic items expose at most one canonical `feature: SemanticLinkContext|null`. Cross-feature usage is represented through normal semantic references, not `features[]`.

Every selectable subject inherits `SemanticMetadata` (decision 0012).

## Audit standard

For every subject file ask:

1. Can a real pack generate useful code without parsing raw refs or map keys?
2. Is the context renderer-neutral?
3. Are relationships resolved but bounded?
4. Are target-language details kept out of semantic context?
5. Does the example prove the context is actually usable?
6. What Runtime IR changes does this imply?
7. Does any nested unit independently cause artifacts and deserve a subject?

## Collector metadata rule

Structural scope metadata never carries full membership trees.

For `collects.group`:

```text
application: ApplicationContext
group: GroupContext
schemas: SchemaContext[]
```

For `collects.feature`:

```text
application: ApplicationContext
group: GroupContext
feature: FeatureContext
operations: OperationContext[]
```

A selected item may still contain bounded owner links such as `schema.group` or `schema.feature`, but those links do not recursively expand into the metadata roots.

For `each` and `all`, Dryv does not invent a singular `group` or `feature` root from collection contents. Scope controls those roots explicitly.
