Skip to content

fix: a schema-included parametric aspect keeps its own identity, so excludes match it - #728

Merged
sini merged 4 commits into
mainfrom
fix/exclude-parametric-schema-include
Oct 15, 2026
Merged

sini merged 4 commits into
mainfrom
fix/exclude-parametric-schema-include

Conversation

@sini

@sini sini commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

@theutz reported this in Matrix: den.aspects.<host>.excludes = [ den.aspects.kuma.observed ] had no effect when kuma.observed was a parametric nested aspect ({ host, ... }: …) included through den.schema.host.includes. Its content still reached the excluded host. That included nixos modules, and content routed to a flake-scope class.

I measured on main 631b034 an aspect excluded on one host of two, by inclusion route and by aspect shape:

included via parametric static
den.schema.host.includes not excluded excluded
den.default.includes excluded excluded
host aspect includes excluded excluded

den.hosts.<sys>.<host>.excludes failed the same way for the schema/parametric cell.

Root cause

A parametric nested aspect reaches wrapChild (nix/lib/aspects/fx/aspect/normalize.nix) as a functor-bearing wrapper. It carries __aspectChain but no name.

  • Function branch: because the wrapper has a __functor, lib.isFunction is true and it goes to wrapFunctorChild. That function never derived a name.
  • Content-wrapper branch: this branch does derive name + meta.aspect-chain from __aspectChain. It existed for exactly this reason, so that an aspect keeps its own identity whatever path includes it. But the function branch caught these wrappers first, so it was unreachable for them.
  • Resulting identity: the aspect resolved as an anonymous host/<anon>:N.

Traced at check-constraint: for the schema/parametric case, kuma/observed was never checked. Only host/<anon>:N nodes were.

Two symptoms followed from that:

  • Excludes keyed on kuma/observed never matched.
  • The same aspect, included by both the schema and the host aspect, got two identities and was included twice.

Fix

withChainIdentity derives name + meta.aspect-chain from __aspectChain when the child has no name. It is applied in the functor branch, before wrapFunctorChild, and reused by the content-wrapper branch, so both paths assign identity one way.

Tests

New file templates/ci/modules/deadbugs/schema-included-parametric-exclude.nix, with users on both hosts:

  • test-host-aspect-excludes
  • test-host-instance-excludes
  • test-excluded-host-adds-nothing-to-flake-scope-class: a flake-scope instantiate of a custom class gets only the observed host.
  • test-schema-and-host-include-dedup: the same aspect included by the schema and by the host aspect is applied once.

Red control: 0/4 against main's normalize.nix. On main the dedup test reads [ "igloo" "igloo" ].
Full suite: with the fix, the full templates/ci suite passes locally, 1301/1301.

Not changed

With den.default.includes, an exclude on the host aspect does not remove the aspect's contribution from the host's user scopes. den.default also applies at user scope, and an aspect-level exclude covers the declaring aspect's subtree only. The instance-level den.hosts.<sys>.<host>.excludes does cover it. I left this alone because it looks like the intended scoping. Flag it if not.

sini added a commit that referenced this pull request Oct 15, 2026
…ts in includes (#729)

## Summary

This is the second excludes gap: `den.lib.policy.exclude <aspect>`,
returned by a policy that sits in an aspect's `includes`, was ignored.
It was traced from a nix-config host: a microVM guest whose `tailscale`
aspect, pulled in through a role, still declared its age secret.

```nix
den.aspects.role.includes = [ den.aspects.feat ];
den.policies.drop-feat = { host, ... }: [ (den.lib.policy.exclude den.aspects.feat) ];
den.aspects.igloo.includes = [ den.policies.drop-feat den.aspects.role ];
# feat's nixos content and quirk emits still applied
```

The aspect-level form, `den.aspects.igloo.excludes = [ den.aspects.feat
]`, already worked through the role.

#728 is the other gap: a schema-included parametric aspect losing its
identity. That's a different cause in a different file. The two PRs are
independent and don't conflict.

## Root cause

Two problems, both in how a policy's exclude was registered.

1. **Timing.** A policy in `includes` is only *registered* during the
include walk (`register-aspect-policy`). It *fires* at the scope's
`dispatch-policies`, after the aspect's static includes have already
passed `gate` → `check-constraint`. The trace shows `check feat` with no
entry, then `register-constraint owner=policy identity=feat`.
2. **Reach.** Even if it registered in time, the entry records the
policy's own include chain as `ownerChain`, and `filterByScope` applies
a subtree exclude only under that chain. It would cover only what the
policy itself includes. (`issue-540-exclude-guard` passes only because
its include and exclude come from one policy.)

## Fix

`registerConstraints` (`nix/lib/aspects/fx/aspect/children.nix`) already
registers an aspect's own `excludes` before its includes are walked. It
now does the same for the policies in that aspect's `includes`:
- It evaluates each one against the scope's context: `scopeContexts`
plus the aspect's scope handlers, and `self`, as dispatch binds it.
- It registers their `exclude` effects as subtree constraints alongside
the aspect's own.

The result is the same timing and the same reach as `excludes = [ … ]`.

A policy whose required arguments the scope doesn't supply yet is
skipped here and left to its dispatch, which behaves as before. The
later dispatch re-registers the same exclude, which is harmless.

## Tests

New file
`templates/ci/modules/deadbugs/policy-exclude-reaches-includer-subtree.nix`:
- **Exclusion cases:** a direct include; through a role with the policy
first; through a role with the policy last.
- **Quirk emits:** dropped too.
- **Controls:**
  - the aspect-level `excludes` through a role;
- a sibling host that includes the role without the policy keeps the
aspect, so the exclude stays the includer's.

| check | main | this PR |
|---|---|---|
| new test file | 2/6 (the 4 exclusion cases fail) | 6/6 |
| nix-config's independent file `features/exclude-nested-role.nix`
(branch `test/policy-exclude-reach`, 658f9a8) | not run | 6/6 |
| full `templates/ci` suite (local) | not run | 1303/1303 |

## Cost

Each policy listed in an aspect's `includes` is now evaluated once more,
at constraint registration, in addition to its dispatch. Only its
`exclude` effects are used. I didn't measure the cost.
sini added 3 commits October 15, 2026 09:20
…xcludes match it

A parametric nested aspect (den.aspects.a.b = { host, ... }: …) reaches the
pipeline as a functor-bearing wrapper carrying __aspectChain but no name.
wrapChild sent it down the function branch, which never derived the name from
__aspectChain; the content-wrapper branch that did was unreachable for it. So
an include of it through den.schema.<kind>.includes resolved as an anonymous
<parent>/<anon>:N: excludes naming it (on the host aspect or the host instance)
never matched, and it was not deduplicated against the same aspect included
by the host aspect.

withChainIdentity now derives name + meta.aspect-chain from __aspectChain for
both branches.
…ontent stays per host

The first fix named the functor-bearing wrapper from __aspectChain but left
it on the function branch. Its functor only re-applies the merged aspect, so
the parametric functions inside __contentValues were resolved under the
wrapper's identity with no entity arguments recorded. The emitted class
modules were then keyed by the aspect alone (class@kuma/observed), and a
flake-scope instantiate that merges several hosts kept only the first host's
module.

Content wrappers now take the content-wrapper branch first: chain identity
plus the parametric functions as includes, resolved with the scope's entity
arguments. Adds a test that a flake-scope class keeps every host's content.
… aspect's chain

den.aspects.<host> = { host, ... }: { excludes = [ … ]; } is coerced to
{ includes = [ fn ]; }, so its body is walked as an anonymous child one chain
segment below the aspect. Its excludes registered at that deeper chain and so
reached only the body's own subtree, missing everything the host scope
includes beside it (a schema include, for one), while the static form of the
same aspect excluded it.

The coercion sites mark the function as the aspect's own body (ownBody), and
registerConstraints registers that body's constraints at the owning aspect's
chain.
@sini
sini force-pushed the fix/exclude-parametric-schema-include branch from 322c9d3 to a887c8a Compare October 15, 2026 16:27
@sini
sini enabled auto-merge (squash) October 15, 2026 16:28
@sini
sini merged commit c01ef97 into main Oct 15, 2026
@sini
sini deleted the fix/exclude-parametric-schema-include branch October 15, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

allow-ci allow all CI integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants