Skip to content

fix(core): a static resolve of a user seeds its host as parent, so host-aspects projects - #744

Merged
sini merged 1 commit into
mainfrom
fix/742-root-user-parent
Nov 6, 2026
Merged

sini merged 1 commit into
mainfrom
fix/742-root-user-parent

Conversation

@sini

@sini sini commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #742, the case #743 left open. The issue's reproduction resolves the user as the root:

den.lib.aspects.resolveImports "homeManager"
  (den.lib.resolveEntity "user" { inherit host user; })

#743 made fxResolveImports materialize policy.spawn requests, and seeded the root's own kind, which covers a host resolved as the root. A user root has no scopeParent entry, though, so materializeSpawns finds no parent record, and the host-aspects projection is still skipped.

When the root's context carries the record of its schema parent (den.schema.user.parent = "host", and ctx.host), the static resolve now adds that parent as a scope for spawn materialization only:

  • scopeParent.<root> = mkScopeId { host = … }, the id a full resolve gives the host;
  • scopeContexts.<that id> = { host = … };
  • scopeEntityKind.<that id> = "host".

It does nothing when the root already has a parent, or when its context has no parent record. Pipe assembly and delivery still get the unseeded maps.

Tests

templates/ci/modules/deadbugs/issue-742-subtree-route-complex-forward.nix:

  • test-static-resolve-of-root-user-projects-host-aspects is the issue's own reproduction: a user root, with the host in its context. Before this change it reads projected = "<missing>".

Validation

Every top-level suite was run under a 12G memory cap:

suite result
c1 109/109
c2 139/139
c3 248/248
c4 44/44 (also 44 on main)
c5 226/226
deadbugs 275/275
deprecated 5/5
internal 283/283
new 1/1

…st-aspects projects

resolveImports on a user root (resolveEntity "user" { host; user; }) had no
parent scope, so the spawn host-aspects requests had no host aspect to
project and the projection was skipped. When the root's context carries its
schema parent's record, the static resolve now adds that parent as a scope,
keyed as a full resolve keys it, for spawn materialization only.
@github-actions github-actions Bot added the allow-ci allow all CI integration tests label Nov 5, 2026
@sini
sini merged commit 2f1ae4a into main Nov 6, 2026
17 checks passed
@sini
sini deleted the fix/742-root-user-parent branch November 6, 2026 00:20
sini added a commit that referenced this pull request Nov 10, 2026
## Summary

PR 1 of 3 for #694 (spec:
https://gist.github.com/sini/de16b6cb510067d6d31bf5f365db832a, §7 item
1). The old subtree merge is still on, so for every den channel the
output is unchanged. What this adds is the mechanism PR 3's flip depends
on: a delivery says which entity receives it.

`route`, `forward` and `provide` accept `receiver = <entity record>`,
e.g. `receiver = host`. The delivery lands in the first scope, walking
up from its source (inclusive), whose own entity has that record's
`id_hash`. A scope's own entity is the record `push-scope` links in
`scopeByEntity`, or the resolved root's record. A receiver that isn't on
the chain is an error naming the record. Without `receiver` an edge
stays where it is today.

### Mechanism

- **One target function.** `targetScopeOf` (`edges/route.nix`) replaces
`appendScopeIdOf` and is used at every site that hard-coded the source
scope: simple routes, complex forwards (`applyComplexRouteEdge`),
provides (`applyOneProvide`), the toposort producer key, and the edge
trace. `sourceScopeId` keeps meaning the source, which
`suppressionVerdicts` keys on.
- **Readers of a delivered bucket.** A simple route still reads the
frozen seed unless a delivery writes into a bucket it reads. Then it
reads that bucket after the fold, pinned after every producer into it,
and producers are keyed by target, so a `receiver` delivery counts where
it lands. This generalises #742's `forwarded` treatment from complex
forwards to every delivery. `wsl-to-host` reading primary-user's
delivered `wsl.defaultUser` depends on it.
- **Provide dedup.** A provide that names a receiver also keys on its
source scope's own entity and on its receiver's target scope. One
battery policy firing at several user scopes then keeps each user's
delivery: two users' shells and unfree lists, for example. `id_hash` is
parent-blind (kind and name), so the target is part of the key: tux on
igloo and tux in igloo's guest are two deliveries, and both get their
account.
- **`push-scope`.** A push that lands on its parent's scope id
overwrites none of that scope's kind, entity link or isolation mark.
That covers a routing kind (`flake-system`, the microvm template's
`microvm-host`) and a re-entry of a record the parent already binds. On
main the microvm host scope read `scopeEntityKind = "microvm-host"`. An
`isEntity` push of a *new* record that adds no nameable key would have
to share its parent's node, so it is refused by name. It occurs in no
suite and no template.
- **A receiver without an id_hash is matched by value.** A receiver with
an id_hash keeps the hash walk. One without, such as a guest whose
ad-hoc record binds `host` to itself (so its users' account and shell
deliveries name it), is compared by value with the own record of each
entity scope that has no id_hash of its own. A scope with an id_hash
can't be that receiver, so it is skipped, and a guest named like one of
its users is not an error. A receiver that matches nothing still ends in
the "not an entity on the chain" error, and one that is not a record
keeps the "not an entity record" error. The comparison is #752's loud
helpers (an error for two records that differ only in function values),
moved from `policy/schema.nix` into `policy/entity-identity.nix`. #752's
own late-dispatch checks keep their quiet comparison, so the move
changes no #752 outcome.
- **Root kind on the full resolve.** `resolveWithPaths` of a directly
resolved entity now seeds the root scope's kind, as the static resolve
has since #743.

### Channels moved onto `receiver` (spec §4)

- `user-to-host` (`users.users.<n>`), the home-manager `userForward`,
and the user-scope arm of the home-manager host-module import (now a
keyed `provide`, so fleet-demo's policy-resolved users keep it).
- `define-user`, `primary-user` and `user-shell`. Their account settings
are delivered as `provide { receiver = host; path = [ "users" "users"
<name> ]; }`. Their host-wide settings (`programs.<shell>.enable`,
`environment.shells`, `system.primaryUser`, `wsl.defaultUser`) are
provides to the host. `isNormalUser` and `extraGroups` are written only
on nixos hosts (spec §9).
- The `unfree` and `insecure` host fallback: a keyed `provide`, so the
same list from several users lands once.
- The microvm template's guest `osProvide`, `microvmProvide` and
`sharedNixStore`.

Every user-scope channel is also gated on `home == null`. A standalone
home `tux@igloo` binds the real `igloo` record, which isn't on the
home's chain. The content was dropped there before too.

### Deliberate changes (for review)

- **Account settings use a provide, not the `user` class.** The spec
says the `user` class. That loses the settings whenever the battery is
included on the host instead of the user: the host-scope fan-out emits
`user` content into the host's bucket, which `user-to-host` doesn't
read. diagram-demo's `devbox` does exactly this. Cell:
`test-host-included-primary-user`.
- **Routes now read locally delivered content.** For example, a guest's
delivery route now carries its users' `user` class. This follows from
the reader rule above and was approved for this PR.
- **Root-kind seeding is a fix with a visible effect.** On main a
host-aspects spawn under a directly resolved host was skipped.
`internal.delivery-edges.test-topology-home-extraction` goes from 6 to
12 edges, measured on both. Flake-built hosts are unaffected.
- **`internal.delivery-edges` expectations** for host-users, darwin and
home-extraction are rebuilt from the file's helpers (`userForwardTail`,
`edge`, `synthesize`), compared as sets. The user channels now target
the host and carry the `receiver` annotation, and the user-scope
home-manager host-module import is a provide edge. In home-extraction
the forward appears twice: once from the host's fold and once from the
host-aspects spawn's surfaced fold, which now materializes because of
the root-kind seeding above. The trace concatenates both without dedup.

- **Isolated users' channels cross.** If you set `isolated` on users as
a #694 workaround, their home-manager, account and shell channels now
reach the host. To keep a channel off, exclude its policy. On main,
`isolated` blocked them, because they rode the merge. Here they are
deliveries that name the host, and `isolated` only withholds the merge
(spec C13, ruled by the owner).
- **Deliveries only to OS hosts.** `define-user`, `primary-user` and
`user-shell` deliver only to `nixos` and `darwin` hosts, which are the
classes main wrote. `primary-user`'s account settings go only to nixos
hosts and `system.primaryUser` only to darwin hosts.
- **Provide dedup follows the entity and the receiver.** A provide that
names a receiver dedups per source entity and target scope. A scope with
no entity of its own (a microvm guest, a routing scope) belongs to its
nearest entity ancestor.

### Not in this PR

- The `os-user-class-fwd` hook stays on the old merge. It moves in PR 2
with `mutual-provider`, both as `provide { receiver = host; }` carrying
the aspect resolved for `host.class`.
- The read-only oracle (`edge-trace.nix` route and provides lists)
renders `receiver` edges at their source. Production doesn't read those
lists.

## Tests

`templates/ci/modules/deadbugs/issue-694-receiver.nix`, 16 cells. Each
was driven red on main or by reverting the mechanism it names:

- §3 topologies: a user route reaches its host, the user scope isolated
so the old merge can't carry it; a microvm guest delivers to the real
host through two scopes with no own entity.
- C1, the nix-config-shaped guest: its delivery names the parent record
and lands at `microvm.vms.<name>.config`; a guest user's account reaches
the guest, not the parent; a home-manager user inside the guest is
carried.
- C4: the host scope keeps its own kind under a microvm-host push.
- C6: two users' shells, unfree lists and one same-policy provide all
arrive.
- C7: a policy-resolved user still gets the home-manager NixOS module.
- A WSL host evaluates with an unkeyed `den.default` `{ host }`
function.
- The refused collapsed entity push and the unknown-receiver error.
- From the option oracle below: a host-side `primary-user` include, and
identical unfree lists from two users.
- Review additions: a darwin host gets `system.primaryUser` and no
nixos-only account options; a non-OS host gets no account or shell
settings; a user-rooted static resolve that binds its host but builds no
host scope drops the delivery and still resolves the user's own content.

Six of the 16 cells pass on main as well. They are preservation
controls; this is what each one pins:

| cell | revert that turns it red |
|---|---|
| `test-host-included-primary-user` | account settings back in the
`user` class (measured red during development) |
| `test-identical-unfree-lists-once` | the unfree host module unkeyed
(measured red during development) |
| `test-policy-resolved-user-gets-hm-module` | the user-scope
home-manager host-module provide removed (measured red) |
| `test-non-os-host-gets-no-account-settings` | none. Reverting the
OS-class gate leaves it green; it pins the outcome |
| `test-darwin-primary-user-no-nixos-account` | none. It pins
`system.primaryUser` delivered and no nixos-only option |
| `test-wsl-host-no-duplicate-default` | none. It pins that the
`wsl-host` routing push applies a `den.default` `{ host }` function once
|

## Validation

**Bisect evidence for the stack.** `main` gets 7 squash commits, one per
PR (#749, #751, #752, #746, #747, #753, #756). Each PR head is verified:
the five rewritten heads (#752, #746, #747, #753, #756) with the full
local oracle (every suite, templates and `main`'s own templates against
`main`, policy firings, cost), and #749 and #751 published, unchanged
and green in CI. The to-users-nested unfree fixture equals `main` at
every head.

All runs under a 12G memory cap, one top-level suite at a time, on this
branch rebased onto `2f1ae4a` (#744):

| suite | this branch | main (`2f1ae4a`) |
|---|---|---|
| c1 | 109 | 109 |
| c2 | 139 | 139 |
| c3 | 248 | 248 |
| c4 | 46 | 46 |
| c5 | 226 | 226 |
| deadbugs | 291 | 275 |
| deprecated | 5 | 5 |
| internal | 283¹ | 283 |
| new | 1 | 1 |

¹ The full internal run read 281/283. The two reds were
`internal.fx-spawn-edges`, which calls `assembleSpawnSubtree` directly
and now has to pass `ownEntityOf` (the defaults were removed so that a
missed site fails loudly). After the fixture fix the suite reran green,
3/3.

Spec §7 oracle, over every template host, re-run against main `2f1ae4a`
after the review fixes (results unchanged):

- **Toplevel `.drv`:** 9 hosts build on main, and all 9 are identical on
this branch. The other 9 (fleet-demo 4, diagram-demo 3, terranix-demo 2)
fail a NixOS assertion on main too.
- **Option values** the moved channels write (users, home-manager users,
shells, primary user, WSL user, unfree and insecure lists, microvm VMs),
with lists sorted: 16 hosts on both sides, with one difference. On
diagram-demo `devbox`, alice's and bob's `extraGroups` read
`networkmanager networkmanager wheel wheel` on main and `networkmanager
wheel` here. `primary-user` is included both on the host and on each
user, and is now delivered once per user. Two real regressions this
oracle found during development are fixed and have cells.

**Against `main`, scaling.** `diagram-demo`'s `server` with N users
added (and, for two hosts, the same N on `server2`), `NIX_COUNT_CALLS`
counters (deterministic per eval), `main` `bf2504f` against this head:

| | N = 0 | 1 | 4 | 16 |
|---|---|---|---|---|
| one host, main | 8,883,284 | 10,835,092 | 16,695,410 | 40,226,007 |
| one host, this PR | 8,838,694 | 10,781,254 | 16,608,264 | 39,927,237 |
| two hosts, main | 9,065,523 | 11,127,953 | 17,337,954 | 42,533,335 |
| two hosts, this PR | 9,004,862 | 11,043,773 | 17,161,849 | 41,675,366
|

This PR is cheaper than `main` at every point, and the difference grows
with N. Fitted per function through N = 0/1/4, the only positive N²
terms against `main` are den-diagram's graph rendering (`graph.nix`
`instOf`, `fullName`; it renders more edges, since deliveries are edges
now) and `trace.nix`, at most 3.3 calls per N². Every drv is unchanged.
`producerScopes` is a keyed set, not `lib.unique`, because the
flake-root fold spans the fleet.

**The by-value receiver path, bounded.** A receiver with an `id_hash`
costs nothing new: it takes the hash walk, as before. A receiver without
one costs one comparison per scope on its chain whose own record has no
`id_hash` either. Each comparison checks `name` first and stops on a
mismatch, and it leaves out pipeline products (`hasAspect`, and
`__`-prefixed attributes such as `__resolveResult`), so it never
resolves an entity again. No template has such a receiver; the
`main`-relative scaling above covers every other path this PR touches.

**Cells:** `deadbugs.issue-694-receiver-identity` (6): the same user
name on a host and in its guest both get their account (red with the
receiver missing from the dedup key), with a distinct-name control and a
home-manager control; a guest without an `id_hash` receives its users'
deliveries; a guest named like its user (red with hashed scopes
compared); a non-record receiver is an error.

**A late-included `{ host }` policy fires at its scope (moved here from
#753; reported in GitHub #754).** This PR nests unfree's host half in a
`{ host }` policy. Delivered to a user through `provides.to-users`, that
policy registers at the user after the user's own dispatch ran, and late
dispatch's entity-kind filter (which keeps an ancestor's policies from
re-firing at deeper kinds) dropped it, so the package never reached the
host's unfree list. Author policies of that shape were dropped on `main`
too. A scope's own late registrations are now exempt from that filter,
as its own dispatch would have fired them. Cells:
`issue-754-to-users-nested-host-policy` (unfree's host half and an
author `{ host }` policy, both red without the fix) and
`issue-754-ancestor-host-policy-stays-at-host` (an ancestor's own host
policy still doesn't fire at the user). With it, this PR alone keeps
main's unfree list for a to-users delivery (the nested to-users fixture
equals `main`).

**Suites** at this head (on #752): c1 109, c2 139, c3 248, c4 44, c5
226, deadbugs 334, deprecated 5, internal 284, new 1. Templates: of 14
configurations, the 9 that build on `main` give the same drv, and the
same 5 fail on both sides; option values equal on 11 of 12 hosts, except
the `extraGroups` difference above. `main`'s own templates with this
den: 14/14 equal.

**Policy firings** against this PR's previous head: 79 fewer across
every suite and template configuration, every one a removed duplicate
firing of the same instance (#752's fix); per policy, scope and bound
arguments, no instance is lost and none added; every drv is identical.

**Cost:** measured against `main` above. The owner has given the
go-ahead, and it runs after the PR 3 suite. This row will be filled with
wall time and peak memory at the head sha.
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.

resolveImports drops policy.spawn requests, so host-aspects is silently inert on a static resolve

1 participant