Repository navigation
fix: policy.when with hasAspect no longer cycles, policy.for accepts effect descriptors - #530
Merged
Merged
Conversation
theutz
approved these changes
May 13, 2026
sini
force-pushed
the
fix/policy-include-not-callable
branch
9 times, most recently
from
May 13, 2026 21:42
8d12eef to
38aa87a
Compare
theutz
approved these changes
May 13, 2026
sini
enabled auto-merge (squash)
May 13, 2026 21:50
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
force-pushed
the
fix/policy-include-not-callable
branch
from
May 13, 2026 22:37
38aa87a to
b96bc79
Compare
3 tasks done
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
policy.forandpolicy.whennow accept policy effect descriptors (e.g.policy.include) and inline aspect attrsets as arguments, not just raw functions or existing policiespolicy.for entity (policy.include aspects.foo)→ "attempt to call something which is not a function but a set"den.lib.policy.whenand<entity>.hasAspect#525:policy.when ({host,...}: host.hasAspect ref) { ... }→ infinite recursionHow it works
policy.for/policy.whenaccept effect descriptors —toInnerPolicyhelper 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.whenwith non-function values emits conditional aspects — instead of registering a policy (which fires during dispatch, causing thehasAspect→config.resolvedcycle), inline aspects and effect descriptors become conditional aspects withmeta.guard. These route throughcompile-conditional, which evaluates guards against the in-flight pathSet — no dependency onconfig.resolved.Entity-shaped guard context —
compile-conditionalenriches 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-conditionaland re-evaluated atresolve-childrenboundary when the pathSet is complete. Still-failing guards produce tombstones. This makes guards walk-order independent.Changes
nix/lib/policy-effects.nix—toInnerPolicyhelper,policy.whenconditional path, throws on non-include effect descriptorsnix/lib/aspects/fx/handlers/compile-conditional.nix—mkGuardCtx,mkPipelineHasAspect,deferConditional,deferConditionalHandler,drainConditionalsHandlernix/lib/aspects/fx/handlers/resolve-children.nix— drains conditionals after children resolvenix/lib/aspects/fx/handlers/state-util.nix— defensiveorin all three scoped helpersnix/lib/aspects/fx/pipeline.nix— new state field, handler registrationsnix/lib/schema-util.nix— extractedschemaEntityKindsSet(was duplicated in 4 files)schemaEntityKindsSetTest plan
test-for-include-fires—policy.for entity (policy.include ...)delivers contenttest-for-include-suppressed— non-matching entity suppressestest-when-include-fires—policy.when pred (policy.include ...)workstest-when-inline-aspect—policy.when pred { nixos... }workstest-hasaspect-guard-fires—host.hasAspectin guard fires correctlytest-hasaspect-guard-suppresses— guard suppresses when aspect absenttest-hasaspect-from-user-scope— works from user aspect includestest-deferred-guard-fires-after-walk— walk-order independence via deferraltest-hasaspect-foranyclass—forAnyClassvariant works