# Template filesystem

This document applies the locked rules from [00-locked-decisions.md](/pack-builders/design/rules).

## Global resources

Any resource under the pack source root that is not inside a `{template_key}`
marker is generated once per pack activation.

Examples:

```text
templates/
├── package.json.sf.hbs
├── tsconfig.json.sf.hbs
└── src/
    └── main.ts.sf.hbs
```

No manifest `templates.project` entry is required.

Rendered global resources may use roots that are structurally valid for a global
invocation, such as pack inputs and bound-unit facts. They do not receive a
fake semantic subject.

## Selection resources

A selection template has a real manifest entry:

```yaml
selections:
  schemas:
    subject: schema
    scope: each

templates:
  dto:
    $ref: "#/selections/schemas"
    output:
      name: $(subject.name.kebab)
      symbol: $(subject.name.pascal)Dto
```

Physical layout:

```text
templates/
└── src/
    └── generated/
        └── dto/
            └── {dto}/
                └── $(name).dto.ts.hbs
```

Static path structure is visible in the filesystem.

## Dynamic path insertion

The marker is the insertion point for `output.path`.

Example:

```text
templates/src/controllers/{controller}/$(name).controller.ts.hbs
```

and:

```yaml
output:
  name: $(feature.name.kebab)
  path:
    - $(group.name.kebab)
    - $(feature.name.kebab)
```

means:

```text
src/controllers/<group>/<feature>/<name>.controller.ts
```

The Engine must reject a static segment such as:

```yaml
path:
  - src
```

because `src` belongs physically in the template tree.

## Ownership control suffix

Normal resource:

```text
$(name).dto.ts.hbs
```

is managed.

Scaffold resource:

```text
$(name).screen.dart.sf.hbs
```

is written only if missing and then belongs to the user.

This convention applies equally to:

- global rendered files;
- global raw/static files;
- selection-rendered files;
- files produced by project, package and inject packs.

Ownership is visible from the source resource and must appear explicitly in the
plan/trace.

## Why ownership belongs to files

A single selection may legitimately produce both kinds:

```text
{view}/
├── $(name).generated.dart.hbs
└── $(name).screen.dart.sf.hbs
```

Template-level ownership cannot represent that without splitting an otherwise
coherent selection into artificial registry entries.

Resource-level ownership avoids that duplication.

## Collision identity

Collision detection must operate on final output paths after stripping:

1. renderer suffix;
2. ownership control suffix.

Therefore these two resources conflict:

```text
main.dart.hbs
main.dart.sf.hbs
```

because both target:

```text
main.dart
```

There is never a last-writer-wins rule.

## Staged files and file actions

The `files` action stage operates on rendered/staged generated files before compare/write.

A pack action may restrict the files it contributes to a batched action:

```yaml
actions:
  format:
    stage: files
    extensions: [.ts]
    commands:
      - run: [pnpm, exec, prettier, --write, $(files)]
```

The extension filter is evaluated against each pack's own staged outputs before same-ID actions are batched. Dryv then unions and sorts the filtered file paths.

This means:

```text
Pack A extensions [.ts]
Pack B extensions [.ts, .json]
```

does not cause Pack A's unrelated JSON outputs to enter the merged formatter invocation.

Formatting in staging is part of generated-output normalization. The normalized staged result is what comparison, write and trace should observe.

The staging location must preserve deterministic access to unit configuration used by tools such as formatters, without changing the rule that the Client owns workspace I/O and process execution.
