Skip to content

fix: policy.when with hasAspect no longer cycles, policy.for accepts effect descriptors - #530

Merged
sini merged 2 commits into
mainfrom
fix/policy-include-not-callable
May 13, 2026
Merged

sini merged 2 commits into
mainfrom
fix/policy-include-not-callable

Conversation

@sini

@sini sini commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • policy.for and policy.when now accept policy effect descriptors (e.g. policy.include) and inline aspect attrsets as arguments, not just raw functions or existing policies
  • Fixes crash: policy.for entity (policy.include aspects.foo) → "attempt to call something which is not a function but a set"
  • Fixes Infinite recursion with den.lib.policy.when and <entity>.hasAspect #525: policy.when ({host,...}: host.hasAspect ref) { ... } → infinite recursion
  • Guards are walk-order independent: deferred on first failure, re-evaluated against final pathSet

How it works

policy.for/policy.when accept effect descriptors — toInnerPolicy helper coerces effect descriptors and inline attrsets into callable policies. Four-branch dispatch: existing policy → passthrough, effect descriptor → constant function, inline attrset → include effect, raw function → direct use.

policy.when with non-function values emits conditional aspects — instead of registering a policy (which fires during dispatch, causing the hasAspect → config.resolved cycle), inline aspects and effect descriptors become conditional aspects with meta.guard. These route through compile-conditional, which evaluates guards against the in-flight pathSet — no dependency on config.resolved.

Entity-shaped guard context — compile-conditional enriches the guard context with entity stubs (host = { hasAspect }, user = { hasAspect }) derived from scope handler keys. This lets predicates written as ({ host, ... }: host.hasAspect ref) work without touching entity config.

Deferred guard evaluation — when a guard fails (aspect not yet in pathSet), the conditional is deferred via defer-conditional and re-evaluated at resolve-children boundary when the pathSet is complete. Still-failing guards produce tombstones. This makes guards walk-order independent.

Changes

  • nix/lib/policy-effects.nix — toInnerPolicy helper, policy.when conditional path, throws on non-include effect descriptors
  • nix/lib/aspects/fx/handlers/compile-conditional.nix — mkGuardCtx, mkPipelineHasAspect, deferConditional, deferConditionalHandler, drainConditionalsHandler
  • nix/lib/aspects/fx/handlers/resolve-children.nix — drains conditionals after children resolve
  • nix/lib/aspects/fx/handlers/state-util.nix — defensive or in all three scoped helpers
  • nix/lib/aspects/fx/pipeline.nix — new state field, handler registrations
  • nix/lib/schema-util.nix — extracted schemaEntityKindsSet (was duplicated in 4 files)
  • 4 files updated to use shared schemaEntityKindsSet

Test plan

  • test-for-include-fires — policy.for entity (policy.include ...) delivers content
  • test-for-include-suppressed — non-matching entity suppresses
  • test-when-include-fires — policy.when pred (policy.include ...) works
  • test-when-inline-aspect — policy.when pred { nixos... } works
  • test-hasaspect-guard-fires — host.hasAspect in guard fires correctly
  • test-hasaspect-guard-suppresses — guard suppresses when aspect absent
  • test-hasaspect-from-user-scope — works from user aspect includes
  • test-deferred-guard-fires-after-walk — walk-order independence via deferral
  • test-hasaspect-foranyclass — forAnyClass variant works
  • Full CI: 795/795 passing

@sini
sini requested a review from theutz May 13, 2026 18:50

@theutz theutz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@sini sini added the allow-ci allow all CI integration tests label May 13, 2026
@sini
sini requested a review from vic as a code owner May 13, 2026 20:56
@sini
sini force-pushed the fix/policy-include-not-callable branch 9 times, most recently from 8d12eef to 38aa87a Compare May 13, 2026 21:42
@sini sini changed the title fix: policy.for/when accept effect descriptors like policy.include fix: policy.when with hasAspect no longer cycles, policy.for accepts effect descriptors May 13, 2026
@sini
sini enabled auto-merge (squash) May 13, 2026 21:50
sini added 2 commits May 13, 2026 15:37
policy.for and policy.when only handled two input types: existing
policies (__isPolicy) and raw functions. Passing a policy effect
descriptor like policy.include returned { __policyEffect, value }
which was stored as fn and crashed when called.

Add toInnerPolicy helper that recognizes effect descriptors and wraps
them as constant-returning policies, enabling the natural composition
pattern: policy.for entity (policy.include aspects.foo)
Three changes that together resolve the infinite recursion when
policy.when predicates access entity.hasAspect:

1. policy.when wrapping inline aspects or effect descriptors now emits
   a conditional aspect (meta.guard) instead of registering a policy.
   Conditional aspects route through compile-conditional, which reads
   the in-flight pathSet — no dependency on config.resolved.

2. compile-conditional's guard context is enriched with entity-shaped
   stubs (host = { hasAspect }, user = { hasAspect }, ...) derived
   from scope handler keys. This allows predicates written as
   ({ host, ... }: host.hasAspect ref) to work without touching the
   real entity config.

3. policy.for/when accept policy effect descriptors (policy.include,
   policy.exclude, etc.) and inline aspect attrsets as arguments,
   not just raw functions or existing policies. Enables the natural
   composition: policy.for entity (policy.include aspects.foo)
@sini
sini force-pushed the fix/policy-include-not-callable branch from 38aa87a to b96bc79 Compare May 13, 2026 22:37
@sini
sini merged commit 501b2da into main May 13, 2026
15 checks passed
@sini
sini deleted the fix/policy-include-not-callable branch May 13, 2026 22:43
sini added a commit that referenced this pull request May 14, 2026
…541)

## Summary

- Fixes #540: `policy.when` with `hasAspect` failed when the conditional
was the first include and its dependency was a later sibling
- `drain-conditionals` was firing inside every `resolve-children` call,
including nested aspects — the guard re-evaluated before siblings were
walked
- Now drains only at entity-level aspects (`__entityKind`) or root scope
aspects (`currentScope == rootScopeId`)

## Root cause

The conditional's `resolve-children` drained deferred guards before the
parent finished walking all siblings. The pathSet was incomplete, so the
guard failed and tombstoned permanently.

## Test plan

- [x] `test-conditional-before-dependency` — conditional first,
dependency second in aspect includes
- [x] `test-conditional-same-level-schema` — same pattern via schema
includes
- [x] Full CI: 805/805 passing (including all prior #525/#530 tests)

Co-Authored-by: @fmway who provided amazing tests
charlesfire pushed a commit to charlesfire/den that referenced this pull request May 16, 2026
…effect descriptors (denful#530)

## Summary

- `policy.for` and `policy.when` now accept policy effect descriptors
(e.g. `policy.include`) and inline aspect attrsets as arguments, not
just raw functions or existing policies
- Fixes crash: `policy.for entity (policy.include aspects.foo)` →
"attempt to call something which is not a function but a set"
- Fixes denful#525: `policy.when ({host,...}: host.hasAspect ref) { ... }` →
infinite recursion
- Guards are walk-order independent: deferred on first failure,
re-evaluated against final pathSet

## How it works

**`policy.for`/`policy.when` accept effect descriptors** —
`toInnerPolicy` helper coerces effect descriptors and inline attrsets
into callable policies. Four-branch dispatch: existing policy →
passthrough, effect descriptor → constant function, inline attrset →
include effect, raw function → direct use.

**`policy.when` with non-function values emits conditional aspects** —
instead of registering a policy (which fires during dispatch, causing
the `hasAspect` → `config.resolved` cycle), inline aspects and effect
descriptors become conditional aspects with `meta.guard`. These route
through `compile-conditional`, which evaluates guards against the
in-flight pathSet — no dependency on `config.resolved`.

**Entity-shaped guard context** — `compile-conditional` enriches the
guard context with entity stubs (`host = { hasAspect }`, `user = {
hasAspect }`) derived from scope handler keys. This lets predicates
written as `({ host, ... }: host.hasAspect ref)` work without touching
entity config.

**Deferred guard evaluation** — when a guard fails (aspect not yet in
pathSet), the conditional is deferred via `defer-conditional` and
re-evaluated at `resolve-children` boundary when the pathSet is
complete. Still-failing guards produce tombstones. This makes guards
walk-order independent.

## Changes

- `nix/lib/policy-effects.nix` — `toInnerPolicy` helper, `policy.when`
conditional path, throws on non-include effect descriptors
- `nix/lib/aspects/fx/handlers/compile-conditional.nix` — `mkGuardCtx`,
`mkPipelineHasAspect`, `deferConditional`, `deferConditionalHandler`,
`drainConditionalsHandler`
- `nix/lib/aspects/fx/handlers/resolve-children.nix` — drains
conditionals after children resolve
- `nix/lib/aspects/fx/handlers/state-util.nix` — defensive `or` in all
three scoped helpers
- `nix/lib/aspects/fx/pipeline.nix` — new state field, handler
registrations
- `nix/lib/schema-util.nix` — extracted `schemaEntityKindsSet` (was
duplicated in 4 files)
- 4 files updated to use shared `schemaEntityKindsSet`

## Test plan

- [x] `test-for-include-fires` — `policy.for entity (policy.include
...)` delivers content
- [x] `test-for-include-suppressed` — non-matching entity suppresses
- [x] `test-when-include-fires` — `policy.when pred (policy.include
...)` works
- [x] `test-when-inline-aspect` — `policy.when pred { nixos... }` works
- [x] `test-hasaspect-guard-fires` — `host.hasAspect` in guard fires
correctly
- [x] `test-hasaspect-guard-suppresses` — guard suppresses when aspect
absent
- [x] `test-hasaspect-from-user-scope` — works from user aspect includes
- [x] `test-deferred-guard-fires-after-walk` — walk-order independence
via deferral
- [x] `test-hasaspect-foranyclass` — `forAnyClass` variant works
- [x] Full CI: 795/795 passing
charlesfire pushed a commit to charlesfire/den that referenced this pull request May 16, 2026
…#540) (denful#541)

## Summary

- Fixes denful#540: `policy.when` with `hasAspect` failed when the conditional
was the first include and its dependency was a later sibling
- `drain-conditionals` was firing inside every `resolve-children` call,
including nested aspects — the guard re-evaluated before siblings were
walked
- Now drains only at entity-level aspects (`__entityKind`) or root scope
aspects (`currentScope == rootScopeId`)

## Root cause

The conditional's `resolve-children` drained deferred guards before the
parent finished walking all siblings. The pathSet was incomplete, so the
guard failed and tombstoned permanently.

## Test plan

- [x] `test-conditional-before-dependency` — conditional first,
dependency second in aspect includes
- [x] `test-conditional-same-level-schema` — same pattern via schema
includes
- [x] Full CI: 805/805 passing (including all prior denful#525/denful#530 tests)

Co-Authored-by: @fmway who provided amazing tests
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.

Infinite recursion with den.lib.policy.when and <entity>.hasAspect

2 participants