Skip to content

fix(xargs): evaluate sub-expressions once per item (yetibot/yetibot#995) - #350

Open
joncraven wants to merge 1 commit into
yetibot:masterfrom
joncraven:fix/995-late-subexpr-xargs
Open

joncraven wants to merge 1 commit into
yetibot:masterfrom
joncraven:fix/995-late-subexpr-xargs

Conversation

@joncraven

Copy link
Copy Markdown

Problem

Closes the evaluation-order half of yetibot/yetibot#995 — Sub expressions in xargs are expanded too early (the interpreter/xargs code lives in this repo; the app repo only carries the issue).

Because the evaluator walks the parse tree depth-first, a sub-expression inside an xargs stage — e.g. `random` or `render {{score}}` — was evaluated once, before the piped collection reached xargs:

!list 1 2 3 | xargs echo `random`     ;=> same value on all three lines
!karma | xargs echo score of `render {{score}}`
;=> score of clojure.lang.LazySeq@782c3717 @foo: 7   (render saw the whole collection)

The old xargs code even documented the dead-end: the item-loop binding of *contextual-data* "achieves nothing" because the sub-commands "would have been evaluated too early… see #995". This PR makes that binding achieve exactly what it was planted for.

Approach

  1. Interpreter (src/yetibot/core/interpreter.clj): when a pipeline stage is structurally an xargs command whose AST contains at least one :sub-expr node, skip the depth-first evaluation of that stage and dispatch it with its unparsed text (sub-expressions normalized to $(…) form via the existing unparse-transformer). Detection is structural, so quotes/fences/literals can't fool it.
  2. xargs (src/yetibot/core/commands/collections.clj): new late-eval-sub-exprs helper scans the args for $(…) groups (quote-aware, nesting-aware) and evaluates each group, once per item, via parser/parse-and-eval — inside the *contextual-data* binding that already rebinds to the current item's data. The surrounding command text is untouched, so per-item dispatch works as before.

Semantics chosen (per the issue's own framing and xargs(1) analogy): evaluate once per item. "Evaluate once total" remains expressible by hoisting the sub-expression ahead of the pipe (`random` | xargs …).

Stages without sub-expressions — the overwhelmingly common xargs usage — are dispatched exactly as before, byte for byte.

Verification

  • Live end-to-end (REPL against a real DB):
    • repeat 3 hello | xargs echo tick2` + backtick → counter command fired 3× (once per item; pre-fix: exactly 1×)
    • repeat 3 hello | xargs echo random + backtick → three **distinct** values (distinct?` → true)
    • repeat 2 hello | xargs echo tick2 tick2+ backtick →("2 3 …" "1 4 …")` — two fresh evaluations per item
    • $(…) syntax handled identically; echo a b c | words | xargs echo item regression unchanged: ("item a" "item b" "item c"); echo there | echo echo hi+ backtick →"hi there"` (early evaluation preserved outside xargs stages)
  • New Midje facts (late-eval-sub-exprs (issue #995)): pass-through (no sub-exprs / blank / nil), quoted $(…) literals untouched, single/multiple/nested group substitution with call-count assertions, error-text substitution, exception fallback.
  • Full suite (lein with-profile +test midje :all + Postgres 16): 2730 succeeded (baseline 2717 → +13 = the new checks), and the same 31 pre-existing environment-dependent failures (history/veo specs) as the unmodified master baseline run — no regressions introduced.

Notes

  • Deliberately minimal: no parser change, no new vars/config; reuses unparse-transformer, parse-and-eval, and the existing contextual-data binding.
  • Landing this in the app requires the usual Bump yetibot/core PR (e.g. #1339-style) after a core release.

…o early

Sub-expressions (backtick or $(..)) inside an xargs stage were evaluated
depth-first while preparing the stage, before the piped collection existed,
so every iteration shared a single early result and commands like
`render {{score}}` saw the whole upstream collection instead of the
current item.

The interpreter now recognizes an xargs stage containing sub-expressions
and dispatches it unparsed; xargs late-evaluates each $(..) group per
item, inside the *contextual-data* binding that already rebinds to each
item's data. Args with no sub-expressions are dispatched exactly as
before.

See yetibot/yetibot#995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant