Skip to content

feat(bun): introduce ScriptTrigger & CommandsTrigger for the Bun plugin - #433

Merged
jymaire merged 3 commits into
kestra-io:mainfrom
InnoxCodes:feature/bun-script-commands-triggers
Sep 21, 2026
Merged

jymaire merged 3 commits into
kestra-io:mainfrom
InnoxCodes:feature/bun-script-commands-triggers

Conversation

@InnoxCodes

@InnoxCodes InnoxCodes commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #422, part of #313. plugin-scripts wants every submodule to expose a ScriptTrigger and a CommandsTrigger, so a flow can be triggered whenever the underlying script/commands execution completes, with an exit condition deciding whether the trigger fires. plugin-script-bun was missing both.

Implementation

Ports the pattern already established in plugin-script-go and plugin-script-node (checked both as reference implementations, per the issue), with the edge state kept in the namespace KV store instead of an in-memory field. A polling trigger that:

  • Runs the sub-plugin's Script/Commands task once per poll
  • Matches the rendered exitCondition against either the exit code (exit N) or a regex/substring against the emitted vars. A failed run has no vars, so only exit N can match a failure
  • Supports edge mode (default on) to emit only on a not-matching-to-matching transition, so the trigger doesn't fire repeatedly on every poll while the condition stays true. The previous result is stored in the namespace KV store under a length prefixed key built from the flow and trigger ids, and it is updated on every poll, so it survives the trigger being rebuilt between polls
  • Compiles the condition regex once and keeps it in a small bounded static cache, since the trigger itself is rebuilt on every poll

ScriptTrigger wraps io.kestra.plugin.scripts.bun.Script; CommandsTrigger wraps io.kestra.plugin.scripts.bun.Commands. Both default containerImage to oven/bun, matching the existing Script/Commands tasks' own default. The module doc has a Triggers section and AGENTS.md lists both classes.

An image that cannot be pulled (or any other error thrown before the task returns) is logged as a warning and does not count as a run: nothing is emitted and the edge state is left as it was.

Testing

Ran locally with Docker, 31 tests across these five classes, all passing:

  • ScriptTriggerTest (13): pure condition-matching unit tests that call the real package-private matchesCondition, no Docker needed
  • EdgeStateTest (8): edge state in the KV store, no Docker needed. Each poll runs on a JSON round-tripped copy of the trigger, and there are cases for edge: false, for state being scoped per flow and per trigger, and for the key format. Removing the kvStore.put makes 4 of them fail
  • ConditionPatternTest (3): the regex cache returns the same compiled pattern and stays correct past its size limit
  • ScriptTriggerEvaluateTest (3): runs the real ScriptTrigger#evaluate in a container (fires on exit 1, stays quiet on exit 0, suppresses the second poll in edge mode)
  • CommandsTriggerTest (4): same for CommandsTrigger. Its edge test now runs the second poll on a serialized copy and also fails without the KV write
  • ./gradlew :plugin-script-bun:spotlessCheck passes

🤖 AI-assisted: I used Claude Code to help draft and verify this fix, and cross-checked it against the existing Go/Node reference implementations before opening this PR.

@github-project-automation github-project-automation Bot moved this to To review in Pull Requests Sep 16, 2026
@fdelbrayelle fdelbrayelle added area/plugin Plugin-related issue or feature request kind/external Pull requests raised by community contributors labels Sep 16, 2026
@fdelbrayelle
fdelbrayelle requested review from a team and jymaire September 16, 2026 10:17

@jymaire jymaire left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated correctness review (6 findings).

@InnoxCodes

Copy link
Copy Markdown
Contributor Author

Acknowledging this review. Findings 1, 2 and 6 (lastMatched not surviving the scheduler's per-poll reconstruction, and the test masking it by reusing one instance) are the exact same systemic defect a human reviewer independently caught on the sibling #436 (PowerShell) PR - confirmed real there, not fixed in place since it's copied from the already-merged go/node/python/ruby/shell plugins and needs a real cross-poll state mechanism, not a narrow per-plugin patch. I documented the limitation explicitly (code comment + corrected schema description + a note on the test that only proves same-instance dedup) rather than leave it silently broken, and I'll bring the same documentation fix here plus to #434/#435 in a follow-up pass, together with whatever the tracked AbstractScriptTrigger fix ends up being.

Finding 5 (regex recompiled every poll) is a fair, easy catch I'll fold into that same follow-up.

Finding 4 (exit N not matching when no TaskException is in the cause chain, e.g. an infra failure like an image-pull error) is a real gap distinct from the edge-mode issue - noted, needs its own look at what extractFailure should do when the cause chain doesn't contain a TaskException at all.

Finding 3 (vars=null on failure, so non-exit N conditions can never match a failing run) - same root cause as the "failure logs" doc overclaim on #436, which I corrected there by documenting the limitation rather than changing behavior. Will bring the same correction here.

🤖 AI-assisted - I read and independently verified this review's reasoning against the code rather than taking it at face value.

@InnoxCodes

InnoxCodes commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Pushed 050d2fa, as promised in my earlier note. Going through the six findings:

  1. edge state does not persist across polls: documented, not fixed. Code comment on lastMatched, corrected edge schema description in both triggers ("intended to", and that a distributed deployment still emits on every matching poll). A real fix needs a cross-poll state mechanism shared by the whole plugin family, which the maintainer is tracking separately.
  2. same defect in CommandsTrigger: same treatment.
  3. vars is null on failure, so only exit N can match a failed run: fixed the docs to say exactly that in both triggers. Behaviour unchanged.
  4. exit N not matching when no TaskException is in the cause chain: not changed. Whether a container that never started should count as exit 1 is a semantic choice that is identical across all the copies, so I'd rather see it decided once for the family.
  5. regex recompiled every poll: left as is. It is negligible next to a container run per poll, and a static cache keyed on the rendered condition would need a bound because templated conditions can vary. Happy to add a bounded cache if wanted.
  6. CommandsTriggerTest edge test reuses one instance: added a comment saying it only proves same-instance dedup. ScriptTriggerTest also stopped asserting against a private copy of the logic: matchesCondition is package-private and the tests call it on a real trigger.

Also @EqualsAndHashCode.Exclude on lastMatched and example intervals changed to PT60S.

13/13 unit tests pass locally, and breaking the exit-code branch of matchesCondition makes 4 of them fail. No Docker here, so the container-backed tests are unrun on my side.

🤖 AI-assisted, as noted in the description.

Update: findings 1, 2, 5 and 6 are fixed for real in dada7bf (edge state in the namespace KV store, cached regex, edge tests on a serialized copy), and I have since run the container-backed tests locally (31 across the five trigger test classes, all passing). Findings 3 and 4 are still documented, see the review threads.

@jymaire

jymaire commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Hello @InnoxCodes , it will be difficult for me to merge this PR if all the bugs found during review are only documented and not fixed.

Part of kestra-io#313. plugin-scripts wants every submodule to expose a
ScriptTrigger and a CommandsTrigger so a flow can be triggered whenever
the underlying script/commands execution completes, with an exit
condition deciding whether the trigger fires. plugin-script-bun was
missing both.

Ports the pattern already established in plugin-script-go and
plugin-script-node (the two existing reference implementations checked
against): a polling trigger that runs the sub-plugin's Script/Commands
task once per poll, matches the rendered exitCondition against either
the exit code ('exit N') or a regex/substring against the emitted vars
and failure logs, and supports edge mode (default on) to emit only on
a not-matching-to-matching transition.

- ScriptTrigger wraps io.kestra.plugin.scripts.bun.Script
- CommandsTrigger wraps io.kestra.plugin.scripts.bun.Commands
- Both default containerImage to 'oven/bun', matching the existing
  Script/Commands tasks' default

Fixes kestra-io#422
Ports the review fixes from the PowerShell PR.

- ScriptTriggerTest no longer asserts against a private copy of the
  matching logic. matchesCondition is package-private and the tests
  call it on a real ScriptTrigger (CsvSource table for exit codes).
- Documented the edge mode limitation instead of leaving it silently
  broken: lastMatched is a plain field that does not survive the
  worker's serialize/deserialize round trip, so in a distributed
  deployment edge mode fires on every matching poll. Code comment,
  corrected schema descriptions, and a note on the edge test that only
  proves same-instance dedup. The real fix needs a cross-poll state
  mechanism shared by the whole plugin family and is tracked
  separately.
- Removed the "failure logs" claim from the docs: a failed run passes
  null vars, so only 'exit N' can match a failure.
- @EqualsAndHashCode.Exclude on lastMatched, with a comment that says
  only what the exclusion does (equals already falls back to object
  identity via AbstractTrigger and callSuper=call).
- Example intervals PT10S -> PT60S, matching the coded default.
The edge flag lived in an AtomicBoolean on the trigger, but a polling
trigger is rebuilt on every poll, so with the default edge=true a
condition that stays true fired on every interval. The previous result
is now stored under a length prefixed key built from the flow and
trigger ids in the namespace KV store and updated on every poll.

Also from the review:
- the condition regex is compiled once and cached in a small bounded
  static map, since the trigger itself is rebuilt on every poll
- an empty exitCondition now fails with a clear message instead of
  silently never matching
- class level @Schema descriptions are text blocks
- the edge tests run the second poll on a serialized copy of the trigger
- add a Docker backed ScriptTrigger#evaluate test
- add KV state tests that run without Docker
- document the triggers in the module doc and list them in AGENTS.md
@InnoxCodes
InnoxCodes force-pushed the feature/bun-script-commands-triggers branch from 050d2fa to dada7bf Compare September 21, 2026 13:06
@InnoxCodes

Copy link
Copy Markdown
Contributor Author

Thanks @jymaire, fair point. I pushed dada7bf, which fixes what can be fixed in this PR: the edge state is now kept in the namespace KV store (the bug QA reproduced), the condition regex is cached, and the edge tests run every poll on a serialized copy of the trigger. It is all in the PR description too.

Two things are still documented rather than changed, and I explained why on the threads: a failed run has no vars, so only exit N can match it, and an image that cannot be pulled is logged as a warning instead of counting as a run. I can change either one if you tell me the behaviour you want.

@jymaire jymaire changed the title Introduce ScriptTrigger & CommandsTrigger for the Bun plugin feat(bun): introduce ScriptTrigger & CommandsTrigger for the Bun plugin Sep 21, 2026

@jymaire jymaire left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for bug fixing, I just renamed your PR to follow conventional commits (as described in doc

@jymaire
jymaire merged commit a03cd85 into kestra-io:main Sep 21, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from To review to Done in Pull Requests Sep 21, 2026
Abhishek84313 added a commit to Abhishek84313/plugin-scripts that referenced this pull request Sep 24, 2026
Aligns the R triggers with the pattern merged for Bun in kestra-io#433.

- Persist edge state in the namespace KV store instead of an in-memory
  AtomicBoolean. This drops the field the review flagged for breaking
  Lombok equals/hashCode (AtomicBoolean has identity equality), and also
  fixes kestra-io#449: a rebuilt or rehydrated trigger no longer re-fires.
- Replace the CompletableFuture regex timeout with a bounded LRU cache of
  compiled patterns. The abandoned future leaked a ForkJoinPool.commonPool()
  worker on timeout, since Matcher#find() is not interruptible.
- Fail loudly when the required exitCondition renders empty, instead of
  silently coercing it to "" and never matching.
- Remove the TODO comments from both trigger classes.
- Document both triggers in the plugin how-to doc.
- Replace the edge-mode tests that asserted AtomicBoolean arithmetic with
  tests that drive shouldEmit and evaluate() for real, and randomize
  trigger ids now that edge state persists between runs.
- Revert the scheduler/worker test dependencies, which are not needed.

Co-Authored-By: Claude Opus 5 <[email protected]>
fdelbrayelle added a commit that referenced this pull request Sep 25, 2026
* feat/r-script-commands-triggers

* fix(r): address review feedback on R triggers

Aligns the R triggers with the pattern merged for Bun in #433.

- Persist edge state in the namespace KV store instead of an in-memory
  AtomicBoolean. This drops the field the review flagged for breaking
  Lombok equals/hashCode (AtomicBoolean has identity equality), and also
  fixes #449: a rebuilt or rehydrated trigger no longer re-fires.
- Replace the CompletableFuture regex timeout with a bounded LRU cache of
  compiled patterns. The abandoned future leaked a ForkJoinPool.commonPool()
  worker on timeout, since Matcher#find() is not interruptible.
- Fail loudly when the required exitCondition renders empty, instead of
  silently coercing it to "" and never matching.
- Remove the TODO comments from both trigger classes.
- Document both triggers in the plugin how-to doc.
- Replace the edge-mode tests that asserted AtomicBoolean arithmetic with
  tests that drive shouldEmit and evaluate() for real, and randomize
  trigger ids now that edge state persists between runs.
- Revert the scheduler/worker test dependencies, which are not needed.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: François Delbrayelle <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/plugin Plugin-related issue or feature request kind/external Pull requests raised by community contributors

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Introduce ScriptTrigger & CommandsTrigger for the Bun plugin

3 participants