Repository navigation
WG012: flag MutableSideEffect with reference-equality value type #72
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # WG012 — MutableSideEffect with reference-equality value type | ||
|
|
||
| | | | | ||
| |---|---| | ||
| | **Rule ID** | WG012 | | ||
| | **Category** | Determinism ([WG001–WG099](../../CONTRIBUTING.md#rule-numbering)) | | ||
| | **Severity** | WARNING | | ||
| | **Engine** | Temporal Java SDK | | ||
| | **Since** | 1.0.0 | | ||
| | **Auto Fix** | Not Available | | ||
|
|
||
| ## Problem | ||
|
|
||
| A `Workflow.mutableSideEffect(id, valueClass, updateFunction, func)` call's `valueClass` | ||
| argument names a type that relies on inherited, identity-based `Object.equals()` instead of | ||
| a real, value-based one. | ||
|
|
||
| ## Why this matters | ||
|
|
||
| `Workflow.mutableSideEffect()` records a new history event only when its `updateFunction` | ||
| reports the value as changed — that's the entire point of the API over a plain | ||
| `sideEffect()`: skip recording (and skip re-running `func`) when nothing actually changed. | ||
| The common `updateFunction` pattern is `(oldValue, newValue) -> !oldValue.equals(newValue)`, | ||
| which delegates the "did it change" question straight to `equals()`. | ||
|
|
||
| A value type with no `equals()` override falls back to `Object`'s default, which compares | ||
| object identity, not field values. Since `func` constructs a *new* instance on every call, | ||
| two logically-identical values are still two different objects — `equals()` always returns | ||
| `false`, `updateFunction` always reports "changed," and every single call appends a new | ||
| `MarkerRecorded` event to workflow history regardless of whether the underlying value moved | ||
| at all. `mutableSideEffect()` silently degrades into `sideEffect()` with extra bookkeeping | ||
| overhead, and a workflow that calls it frequently accumulates history far faster than its | ||
| author likely expects. | ||
|
|
||
| ## Bad Example | ||
|
|
||
| ```java | ||
| public class PaymentWorkflowImpl implements PaymentWorkflow { | ||
| @Override | ||
| public String fetchCurrentPrice() { | ||
| Price price = Workflow.mutableSideEffect( | ||
| "price", | ||
| Price.class, | ||
| (oldValue, newValue) -> !oldValue.equals(newValue), | ||
| () -> pricingService.currentPrice()); | ||
| return price.toString(); | ||
| } | ||
| } | ||
|
|
||
| class Price { | ||
| // No equals()/hashCode() override — every call is reported as "changed." | ||
| double amount; | ||
| } | ||
| ``` | ||
|
|
||
| ## Good Example | ||
|
|
||
| ```java | ||
| public class PaymentWorkflowImpl implements PaymentWorkflow { | ||
| @Override | ||
| public String fetchCurrentPrice() { | ||
| Price price = Workflow.mutableSideEffect( | ||
| "price", | ||
| Price.class, | ||
| (oldValue, newValue) -> !oldValue.equals(newValue), | ||
| () -> pricingService.currentPrice()); | ||
| return price.toString(); | ||
| } | ||
| } | ||
|
|
||
| record Price(double amount) { | ||
| // A record: the language always synthesizes a value-based equals()/hashCode(), so | ||
| // updateFunction correctly reports "unchanged" once the price stabilizes. | ||
| } | ||
| ``` | ||
|
|
||
| A hand-written `equals()`/`hashCode()` pair (or a primitive/JDK wrapper type like | ||
| `Integer`/`String`) is equally correct — the rule only cares that *some* value-based | ||
| equality exists, not how it's implemented. | ||
|
|
||
| ## Recommended Fix | ||
|
|
||
| Use a primitive, a record, or a type with a proper value-based `equals()`/`hashCode()` for | ||
| the `mutableSideEffect` value type. WG012 checks the value type's resolved `equals()`, not | ||
| the `updateFunction`, so rewriting only the `updateFunction`'s comparison logic (e.g. to | ||
| compare fields directly instead of calling `equals()`) does not suppress this diagnostic — | ||
| the value type itself still needs real equality for WoGu to recognize it as safe. | ||
|
|
||
| ## References | ||
|
|
||
| - [Temporal documentation: Workflow determinism](https://docs.temporal.io/workflows#deterministic-constraints) | ||
| - [Temporal Java SDK: `Workflow.mutableSideEffect()`](https://javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/workflow/Workflow.html) | ||
|
|
||
| ## False Positives | ||
|
|
||
| Unlike WG001–WG011, WG012 isn't a call-graph reachability rule — it's a `Class<T>` | ||
| argument type check. WoGu resolves the value type via its symbol solver: a Java record is | ||
| always treated as safe (the language guarantees a synthesized value-based `equals()`); a | ||
| type declaring its own `equals(Object)`, or inheriting one from a resolvable ancestor other | ||
| than `java.lang.Object`, is also treated as safe. | ||
|
|
||
| A type WoGu cannot resolve — one that lives in a compiled dependency rather than the | ||
| project's own source, since `SourceRootParser` never includes the target project's compiled | ||
| classpath — is treated as "cannot determine," not a violation. This is a deliberate | ||
| false-negative-preferring choice, the same one every other rule in this catalog makes for | ||
| an unresolvable call: WG012 will not flag a well-known JDK type it happens to resolve via | ||
| reflection (`Integer`, `String`, and similar all have real `equals()` and are correctly | ||
| recognized as safe), but a type from a third-party library not on WoGu's own classpath will | ||
| silently not be checked either way. | ||
|
|
||
| WG012 only inspects the `valueClass` argument; it never looks at `updateFunction`. An | ||
| `updateFunction` written to compare fields directly instead of calling `equals()` may well | ||
| be correct at runtime, but WoGu has no way to verify that a given lambda actually implements | ||
| value equality correctly (a trivial or inverted comparator would look the same | ||
| syntactically), so this is not a check WG012 attempts — the diagnostic will still fire | ||
| until the value type itself has real equality. | ||
|
|
||
| If you believe WG012 is flagging or missing something incorrectly, please open an issue | ||
| with a minimal reproduction. | ||
|
|
||
| ## Since Version | ||
|
|
||
| 1.0.0 |
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
13 changes: 13 additions & 0 deletions
13
sample-temporal-project/src/main/java/com/example/wogu/sample/services/CachedBalance.java
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,13 @@ | ||
| package com.example.wogu.sample.services; | ||
|
|
||
| public class CachedBalance { | ||
|
|
||
| // Intentionally no equals()/hashCode() override — this is what WG012 flags on the | ||
| // Workflow.mutableSideEffect() call in PaymentService.refreshCachedBalance(): every call | ||
| // is reported as "changed" regardless of whether the amount actually moved. | ||
| double amount; | ||
|
|
||
| CachedBalance(double amount) { | ||
| this.amount = amount; | ||
| } | ||
| } |
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
91 changes: 91 additions & 0 deletions
91
wogu-temporal/src/main/java/dev/wogu/temporal/MutableSideEffectEqualityRule.java
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,91 @@ | ||
| package dev.wogu.temporal; | ||
|
|
||
| import com.github.javaparser.ast.body.MethodDeclaration; | ||
| import dev.wogu.api.Rule; | ||
| import dev.wogu.api.ValidationContext; | ||
| import dev.wogu.api.Violation; | ||
| import dev.wogu.temporal.callgraph.CallGraphAnalyzer; | ||
| import dev.wogu.temporal.callgraph.CallTarget; | ||
| import dev.wogu.temporal.callgraph.ContextEntryPoint; | ||
| import dev.wogu.temporal.callgraph.ExecutionContext; | ||
| import dev.wogu.temporal.callgraph.ValueBasedEqualityArgumentTarget; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * The {@code mutable-side-effect-equality} declarative rule type: flags a call to the | ||
| * single method named in a {@link RuleDefinition}'s {@code methods} list whose | ||
| * {@code Class<T>} argument at {@code valueTypeArgumentIndex} names a type relying on | ||
| * inherited, identity-based {@code equals()}. | ||
| * | ||
| * <p>This is the generic engine behind WG012 ({@code Workflow.mutableSideEffect()}'s value | ||
| * type) and any future "this argument's resolved type must have real equality" rule of the | ||
| * same shape — none of which need a dedicated Java class. Adding one is purely a matter of | ||
| * adding a YAML definition with {@code type: mutable-side-effect-equality}, | ||
| * {@code methods: [the one method]}, and {@code valueTypeArgumentIndex} under | ||
| * {@code src/main/resources/rules}; this class builds the matching | ||
| * {@link ValueBasedEqualityArgumentTarget} and reuses {@link TemporalRuleSupport} for the | ||
| * actual call-graph traversal, suppression, and violation building, exactly like | ||
| * {@link ForbiddenMethodRule} does for its own shape — including honoring the same | ||
| * {@code suppressedContexts}/{@code requiredContexts} fields, via the same | ||
| * {@link ForbiddenMethodRule#toExecutionContext} conversion. | ||
| */ | ||
| final class MutableSideEffectEqualityRule implements TemporalRule { | ||
|
|
||
| private final Rule metadata; | ||
| private final CallTarget target; | ||
| private final String message; | ||
| private final String suggestedFix; | ||
| private final Set<ExecutionContext> suppressedContexts; | ||
| private final Set<ExecutionContext> requiredContexts; | ||
|
|
||
| MutableSideEffectEqualityRule(RuleDefinition definition) { | ||
| this.metadata = definition.toRule(); | ||
| if (definition.methods().size() != 1) { | ||
| throw new IllegalArgumentException( | ||
| "Rule '" + definition.id() + "' of type 'mutable-side-effect-equality' must declare exactly one " | ||
| + "method, found: " + definition.methods()); | ||
| } | ||
| if (definition.valueTypeArgumentIndex() == null) { | ||
| throw new IllegalArgumentException( | ||
| "Rule '" + definition.id() + "' of type 'mutable-side-effect-equality' requires " | ||
| + "'valueTypeArgumentIndex'"); | ||
| } | ||
| this.target = | ||
| new ValueBasedEqualityArgumentTarget(definition.methods().get(0), definition.valueTypeArgumentIndex()); | ||
| this.message = definition.description(); | ||
| this.suggestedFix = definition.replacement(); | ||
| this.suppressedContexts = | ||
| definition.suppressedContexts().stream().map(ForbiddenMethodRule::toExecutionContext).collect(Collectors.toUnmodifiableSet()); | ||
| this.requiredContexts = | ||
| definition.requiredContexts().stream().map(ForbiddenMethodRule::toExecutionContext).collect(Collectors.toUnmodifiableSet()); | ||
| } | ||
|
|
||
| @Override | ||
| public Rule metadata() { | ||
| return metadata; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Violation> evaluate( | ||
| ValidationContext context, | ||
| List<ScannedWorkflowClass> workflowClasses, | ||
| CallGraphAnalyzer callGraph, | ||
| Predicate<MethodDeclaration> activityBoundary, | ||
| List<ContextEntryPoint> contextEntryPoints) { | ||
| return TemporalRuleSupport.findViolations( | ||
| workflowClasses, | ||
| callGraph, | ||
| target, | ||
| metadata, | ||
| context, | ||
| message, | ||
| suggestedFix, | ||
| activityBoundary, | ||
| contextEntryPoints, | ||
| suppressedContexts, | ||
| requiredContexts); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.