-
Notifications
You must be signed in to change notification settings - Fork 12
feat(workflow-executor): evaluate deterministic condition steps without AI #1837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scra3
wants to merge
2
commits into
feature/prd-472-deterministic-decision-step
Choose a base branch
from
feature/prd-472-condition-evaluator
base: feature/prd-472-deterministic-decision-step
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
130 changes: 130 additions & 0 deletions
130
packages/workflow-executor/src/executors/deterministic-condition-evaluator.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import type { ConditionOperator } from '../types/validated/step-definition'; | ||
|
|
||
| // Guard against Date.parse's laxity ("5" parses as a year in some engines): only strings that | ||
| // start like an ISO date are treated as dates. | ||
| const ISO_DATE_PREFIX = /^\d{4}-\d{2}-\d{2}/; | ||
| const TIMEZONE_SUFFIX = /(Z|[+-]\d{2}:?\d{2})$/i; | ||
| // Sequelize hands back numeric/decimal/bigint columns as strings although datasource-sequelize | ||
| // maps them to the Number primitive, so the builder's JSON number meets a string at runtime. | ||
| const NUMERIC_STRING = /^-?\d+(\.\d+)?$/; | ||
|
|
||
| function toTimestamp(value: unknown): number | null { | ||
| if (typeof value !== 'string' || !ISO_DATE_PREFIX.test(value)) return null; | ||
|
|
||
| // Date.parse reads an offset-less datetime as host-local (date-only as UTC), which would route | ||
| // the same run differently per machine — pin every offset-less datetime to UTC. | ||
| const absolute = value.includes('T') && !TIMEZONE_SUFFIX.test(value) ? `${value}Z` : value; | ||
| const parsed = Date.parse(absolute); | ||
|
|
||
| return Number.isNaN(parsed) ? null : parsed; | ||
| } | ||
|
|
||
| function toNumber(value: unknown): number | null { | ||
| if (typeof value === 'number') return Number.isNaN(value) ? null : value; | ||
|
|
||
| return typeof value === 'string' && NUMERIC_STRING.test(value) ? Number(value) : null; | ||
| } | ||
|
|
||
| // Coercion only kicks in against a real number (always the build-time side): two numeric-looking | ||
| // strings stay strings, since the contract exposes ordering operators for Number/Date fields only. | ||
| function toNumberPair(actual: unknown, expected: unknown): [number, number] | null { | ||
| if (typeof actual !== 'number' && typeof expected !== 'number') return null; | ||
|
|
||
| const actualNumber = toNumber(actual); | ||
| const expectedNumber = toNumber(expected); | ||
|
|
||
| return actualNumber !== null && expectedNumber !== null ? [actualNumber, expectedNumber] : null; | ||
| } | ||
|
|
||
| function scalarEqual(actual: unknown, expected: unknown): boolean | null { | ||
| if (actual === expected) return true; | ||
|
|
||
| const numbers = toNumberPair(actual, expected); | ||
| if (numbers) return numbers[0] === numbers[1]; | ||
|
|
||
| const actualTs = toTimestamp(actual); | ||
| const expectedTs = toTimestamp(expected); | ||
| if (actualTs !== null && expectedTs !== null) return actualTs === expectedTs; | ||
|
|
||
| return typeof actual === typeof expected ? false : null; | ||
| } | ||
|
|
||
| function isEqual(actual: unknown, expected: unknown): boolean | null { | ||
| if (Array.isArray(actual) && Array.isArray(expected)) { | ||
| return ( | ||
| actual.length === expected.length && | ||
| actual.every((item, index) => scalarEqual(item, expected[index]) === true) | ||
| ); | ||
| } | ||
|
|
||
| if (Array.isArray(actual) || Array.isArray(expected)) return null; | ||
|
|
||
| return scalarEqual(actual, expected); | ||
| } | ||
|
|
||
| function compare(actual: unknown, expected: unknown): number | null { | ||
| const numbers = toNumberPair(actual, expected); | ||
| if (numbers) return numbers[0] - numbers[1]; | ||
|
|
||
| const actualTs = toTimestamp(actual); | ||
| const expectedTs = toTimestamp(expected); | ||
| if (actualTs !== null && expectedTs !== null) return actualTs - expectedTs; | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function isPresent(value: unknown): boolean { | ||
| if (value === null || value === undefined) return false; | ||
| if (typeof value === 'string') return value.length > 0; | ||
| if (Array.isArray(value)) return value.length > 0; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function isMemberOf(list: unknown, candidate: unknown): boolean { | ||
| return Array.isArray(list) && list.some(item => scalarEqual(item, candidate) === true); | ||
| } | ||
|
|
||
| function ordering(satisfies: (diff: number) => boolean) { | ||
| return (actual: unknown, expected: unknown): boolean => { | ||
| const diff = compare(actual, expected); | ||
|
|
||
| return diff !== null && satisfies(diff); | ||
| }; | ||
| } | ||
|
|
||
| const EVALUATORS: Record< | ||
| Exclude<ConditionOperator, 'present' | 'blank'>, | ||
| (actual: unknown, expected: unknown) => boolean | ||
| > = { | ||
| equal: (actual, expected) => isEqual(actual, expected) === true, | ||
| not_equal: (actual, expected) => isEqual(actual, expected) === false, | ||
| greater_than: ordering(diff => diff > 0), | ||
| less_than: ordering(diff => diff < 0), | ||
| greater_than_or_equal: ordering(diff => diff >= 0), | ||
| less_than_or_equal: ordering(diff => diff <= 0), | ||
| in: (actual, expected) => isMemberOf(expected, actual), | ||
| not_in: (actual, expected) => Array.isArray(expected) && !isMemberOf(expected, actual), | ||
| contains: (actual, expected) => | ||
| typeof actual === 'string' && typeof expected === 'string' && actual.includes(expected), | ||
| not_contains: (actual, expected) => | ||
| typeof actual === 'string' && typeof expected === 'string' && !actual.includes(expected), | ||
| }; | ||
|
|
||
| /** | ||
| * Pure evaluation of one deterministic condition. Never throws for data reasons: | ||
| * - `null` = not evaluable (the resolved value is null/missing) — treated as "not met"; | ||
| * - a type-mismatched comparison (including for negated operators) is "not met" (`false`), | ||
| * so a broken config can never accidentally satisfy a condition. | ||
| */ | ||
| export default function evaluateOperator( | ||
| operator: ConditionOperator, | ||
| actual: unknown, | ||
| expected: unknown, | ||
| ): boolean | null { | ||
| if (operator === 'present') return isPresent(actual); | ||
| if (operator === 'blank') return !isPresent(actual); | ||
| if (actual === null || actual === undefined) return null; | ||
|
|
||
| return EVALUATORS[operator](actual, expected); | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
executors/deterministic-condition-evaluator.ts:7toTimestampaccepts invalid calendar dates such as2026-02-30and converts them to normalized timestamps, so malformed values compare equal to2026-03-02and can satisfy equality, membership, or ordering conditions. Validate that the parsed date round-trips to the original calendar date before returning its timestamp.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: