# Context resolution

The `DemandDrivenContextProjector` turns validated requirement paths into the minimal render context for one file. It resolves directly from IR and planning indexes. There's no expanded context-IR copy (decision 0005).

The vocabulary itself (every context type and field) is in [context](/pack-builders/context/overview).

## Envelope

| Root                | Source                                            | When available                                                                                  |
| ------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `file`              | the `PlannedFileArtifact`                         | always                                                                                          |
| `application`       | the active application (bounded metadata)         | always; resolved only when requested                                                            |
| `pack.inputs.<key>` | usage inputs checked against the pack declaration | always                                                                                          |
| subject             | IR, by subject + scope                            | `each` → `schema`, `property.enum`… · `all` / `collects.*` → `schemas[]`, `properties.enums[]`… |
| `group`             | the partition identity                            | `collects.group`, and `collects.feature` (owner)                                                |
| `feature`           | the partition identity                            | `collects.feature`                                                                              |

The collector always comes from the partition, never from the first item.

## Algorithm

For every requirement:

1. take the root from the envelope;
2. walk only the requested segments, projecting while traversing (never materialise and then prune);
3. resolve `$ref`s only when traversal reaches them, delegating to the authoritative resolver for that context type;
4. compute name variants and derived values only when requested;
5. take `file` facts only from planning;
6. apply absence rules, memoise safe lookups, and merge the result into one output tree;
7. discard the per-file context after rendering.

Requirements are merged into one request tree. A parent path never implies its children unless it's a subtree request.

## Absence

| Case                                                                                 | Result      |
| ------------------------------------------------------------------------------------ | ----------- |
| Not requested                                                                        | key omitted |
| Requested, optional object or scalar absent                                          | `null`      |
| Requested, collection empty                                                          | `[]`        |
| Requested inactive variant branch (e.g. `value.property` when `value.kind = schema`) | `null`      |

## Ordering

Each collection uses the order its contract defines: tuple and ordered-enum order, canonical order for unordered enums, effective schema field order, planning order for `file.dependencies`, and export-stream order for `file.exports`. Dictionary iteration order is never used.

## Bounds and cycles

Subtree expansion stops at context-reference boundaries. Semantic cycles (`User.manager → User`) are safe because request paths are finite. A guard keyed on context type + canonical identity + request node turns any recursive attempt into a bounded diagnostic.

## Errors

| Code                                          | Meaning                                                                                         |
| --------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `context.request.invalid`                     | The path isn't in the vocabulary                                                                |
| `context.root.unavailable`                    | Valid path, but not available for this invocation                                               |
| `context.reference.invalid`                   | Broken or incompatible canonical reference                                                      |
| `context.resolution.bounded`                  | A forbidden recursive expansion was attempted                                                   |
| `context.projection.planned_artifact_missing` | Expected generated representation not planned. Names the consumer, target and allowed producers |

Projection-specific failures use the `context.projection.*` family, e.g. `root_invalid`, `pack_input_undeclared`, `application_ambiguous`, `port_missing`.

## Evidence

`project_with_evidence()` returns the context plus the requested, materialised and unmaterialised paths, and typed provider provenance (no values). This feeds [tracing](/pack-builders/context/tracing).
