viv icon indicating copy to clipboard operation
viv copied to clipboard

Sifting expressions in casting pools don't match chapter 16: no flattening, and a no-match null throws

Open cgorto opened this issue 3 months ago • 0 comments

What the documentation says

Language reference chapter 16 ("Pattern composition") states:

A sift pattern expression in a from directive returns the matched actions as a flat collection of action instances, which the enclosing pattern can then bind to a group action role.

Chapter 7 defines sifting expressions as general expressions, and the compiler (0.12.2) accepts them in the from: directive of an ordinary action role as well.

What the runtime does

evaluateSifting (runtimes/js/src/interpreter/interpreter.ts) returns SiftingMatch | null — an object mapping the pattern's action-variable names to record-ID arrays. Nothing between it and pool assembly flattens that value, so in getCustomPool (runtimes/js/src/role-caster/casting.ts, at the object-keys rule near line 783):

  • A match hits isPlainObject → Object.keys(...), so the casting pool becomes the pattern's variable names (["reckoning", "escapes"]), not record IDs.
  • No match (null) is neither object nor array → VivRoleCastingError: "Bad pool directive: evaluated to a single value, not an array".

Consequences

  1. Pattern composition as documented in chapter 16 (the eye-for-an-eye example) cannot work.
  2. A free action with a sift-fed pool poisons action selection: every targeting pass errors on days with no match, instead of the cast failing gracefully.

Verified live: with a free action whose group action role casts from: sift pattern <p>: over: @teller, every initiator's daily selection failed with the pool error until a match existed.

Repro

action brawl:
    gloss: "@bruiser brawls with @bruised"
    tags: brawl
    roles:
        @bruiser:
            as: initiator
        @bruised:
            as: character, recipient

query brawls:
    tags:
        any: brawl

pattern brawl-story:
    actions:
        @fight:
            from: search query brawls:
                over: inherit

action spin-a-yarn:
    gloss: "@teller tells a tale"
    roles:
        @teller:
            as: initiator
        @tale*:
            as: action
            n: 1-10
            from: sift pattern brawl-story:
                over: @teller

Compiles clean on vivc 0.12.2 (schema 0.10.1). At runtime, @tale*'s pool is ["fight"] after a brawl, and a role-casting error before one.

Suggested fix

In custom-pool evaluation, when the pool expression is a sifting, flatten the match object's values into one deduplicated flat pool (dedup because composed patterns may share a hinge record), and treat null as an empty pool so the cast fails instead of erroring. Empty-on-null also gives free actions the right semantics: a character with no story simply does not tell one.

cgorto avatar Aug 18 '26 18:08 cgorto