Problem
Add an eval that teaches the Java Optionals skill to prevent and repair Optional.isPresent() plus orElseThrow() control flow when a Java 9+ side-effecting present/empty branch should use Optional.ifPresentOrElse(...).
This issue covers two related failures:
- Introduction-time failure: the weaker Optional shape was introduced while implementing a new lookup branch, even though the Java Optionals skill was available in the session and its trigger description covers writing or reviewing code that touches
isPresent, orElseThrow, absent/missing values, or Optional control flow.
- Review-time repair: the same weaker shape was later noticed and refactored after the user explicitly asked whether
if (request.lookup().isPresent()) was best practice.
This is eval-worthy because the weaker code is functionally correct, but it uses null-style Optional control flow: isPresent() gates the branch and orElseThrow() retrieves the same Optional value afterward. The skill should learn to trigger before introducing that shape, not only after a user explicitly calls it out.
Area
Evals or scoring
Code before the feature prompt was executed
Before lookup support existed, the method always rendered the full private diagnostics context. There was no present/empty lookup branch yet:
private String renderPrivateContext(DiagnosticsRequest request, Optional<DiagnosticsTokenHasher> sharedTokenHasher)
throws IOException {
DiagnosticsContext context = diagnosticsContext(request, sharedTokenHasher);
StringBuilder body = new StringBuilder();
body.append("# Symphony for Trello Private Context\n\n");
body.append("Private diagnostics context. Do not paste this output into public issues.\n");
body.append("It may include Trello board names, board ids, board URLs, and local paths.\n");
body.append("It does not include credential values or worker log contents.\n");
section(body, "Command");
line(body, "time_utc", now().toString());
line(body, "command", String.join(" ", diagnosticsArguments(request, true)));
line(body, "command_context", commandContext());
appendSelectionMetadata(body, context);
appendDiagnosticsTokenKeyStatus(body);
section(body, "Local Paths");
appendLocalPathIdentifiers(body, context.paths(), context.manifestPath());
section(body, "Connected Board Identifiers");
appendLocalManifestIdentifiers(
body,
context.selectedManifest(),
context.manifestSnapshot().status(),
context.paths().defaultEnvPath());
section(body, "Workflow Identifiers");
appendLocalWorkflowIdentifiers(body, context.selectedWorkflowPaths());
section(body, "Log Identifiers");
appendLocalLogIdentifiers(
body, context.paths().stateHome(), context.selectedWorkflowPaths(), context.selected());
return body.toString();
}
Feature prompt that caused isPresent() to be introduced
The implementation prompt asked for a focused private diagnostics token lookup feature. Relevant requirements included:
Keep the PR as a focused private diagnostics token lookup feature.
Generated lookup commands and executable docs do not quote concrete <path:...> tokens.
Keep existing lookup tests for found token, managed PID token, file-backed secret token, missing token without full private dump, malformed token without arbitrary private search, and --lookup rejected without --show-private-context.
The implementation needed a new request.lookup() branch: when lookup is present, render only the lookup result; when absent, render the full private diagnostics context.
Prompt-produced code that introduced the weaker Optional shape
The feature implementation added the branch using isPresent() and orElseThrow():
private String renderPrivateContext(DiagnosticsRequest request, Optional<DiagnosticsTokenHasher> sharedTokenHasher)
throws IOException {
DiagnosticsContext context = diagnosticsContext(request, sharedTokenHasher);
StringBuilder body = new StringBuilder();
body.append("# Symphony for Trello Private Context\n\n");
body.append("Private diagnostics context. Do not paste this output into public issues.\n");
body.append("It may include Trello board names, board ids, board URLs, and local paths.\n");
body.append("It does not include credential values or worker log contents.\n");
section(body, "Command");
line(body, "time_utc", now().toString());
line(body, "command", String.join(" ", diagnosticsArguments(request, true)));
line(body, "command_context", commandContext());
appendSelectionMetadata(body, context);
appendDiagnosticsTokenKeyStatus(body);
if (request.lookup().isPresent()) {
section(body, "Lookup");
appendPrivateContextLookup(body, context, request.lookup().orElseThrow());
return body.toString();
}
section(body, "Local Paths");
appendLocalPathIdentifiers(body, context.paths(), context.manifestPath());
section(body, "Connected Board Identifiers");
appendLocalManifestIdentifiers(
body,
context.selectedManifest(),
context.manifestSnapshot().status(),
context.paths().defaultEnvPath());
section(body, "Workflow Identifiers");
appendLocalWorkflowIdentifiers(body, context.selectedWorkflowPaths());
appendLocalSecretFileIdentifiers(body, context.selectedWorkflowPaths());
section(body, "Log Identifiers");
appendLocalLogIdentifiers(
body, context.paths().stateHome(), context.selectedWorkflowPaths(), context.selected());
section(body, "Process State Identifiers");
appendLocalProcessStateIdentifiers(
body, context.paths().stateHome(), context.selectedWorkflowPaths(), context.selected());
return body.toString();
}
Why the prompt-produced code is weak
-
It reads the same Optional twice.
request.lookup().isPresent() decides the branch, then request.lookup().orElseThrow() retrieves the value. That is safe in this code, but it is the same null-check-then-get shape Optional is meant to avoid.
-
It uses an exception-oriented accessor for a value already proven present.
orElseThrow() communicates failure handling, but this branch is not a failure path. It is ordinary present-value control flow.
-
It splits one present/empty decision across an early return and the rest of the method.
The method has exactly two side-effecting rendering branches: render a lookup result when the Optional is present, or render the full private context when it is absent. That maps directly to ifPresentOrElse(...).
-
It shows a skill-trigger gap.
The Java Optionals skill description says to use it when writing, reviewing, or refactoring Optional code, and specifically when touching isPresent, orElseThrow, absent/missing values, or nullable control flow. The feature prompt did not explicitly mention Optional style, so an eval should ensure the skill still triggers based on the code being introduced.
Follow-up prompt that caused the repair
The user later asked:
in if (request.lookup().isPresent()) { can you please use the java optionals skill to verify if that is best practice in that part of the code?
The Java baseline for this code was Java 25, so Java 9+ Optional APIs such as ifPresentOrElse(...) were available.
Maintainer-preferred code
private String renderPrivateContext(DiagnosticsRequest request, Optional<DiagnosticsTokenHasher> sharedTokenHasher)
throws IOException {
DiagnosticsContext context = diagnosticsContext(request, sharedTokenHasher);
StringBuilder body = new StringBuilder();
body.append("# Symphony for Trello Private Context\n\n");
body.append("Private diagnostics context. Do not paste this output into public issues.\n");
body.append("It may include Trello board names, board ids, board URLs, and local paths.\n");
body.append("It does not include credential values or worker log contents.\n");
section(body, "Command");
line(body, "time_utc", now().toString());
line(body, "command", String.join(" ", diagnosticsArguments(request, true)));
line(body, "command_context", commandContext());
appendSelectionMetadata(body, context);
appendDiagnosticsTokenKeyStatus(body);
request.lookup()
.ifPresentOrElse(
lookup -> {
section(body, "Lookup");
appendPrivateContextLookup(body, context, lookup);
},
() -> {
section(body, "Local Paths");
appendLocalPathIdentifiers(body, context.paths(), context.manifestPath());
section(body, "Connected Board Identifiers");
appendLocalManifestIdentifiers(
body,
context.selectedManifest(),
context.manifestSnapshot().status(),
context.paths().defaultEnvPath());
section(body, "Workflow Identifiers");
appendLocalWorkflowIdentifiers(body, context.selectedWorkflowPaths());
appendLocalSecretFileIdentifiers(body, context.selectedWorkflowPaths());
section(body, "Log Identifiers");
appendLocalLogIdentifiers(
body,
context.paths().stateHome(),
context.selectedWorkflowPaths(),
context.selected());
section(body, "Process State Identifiers");
appendLocalProcessStateIdentifiers(
body,
context.paths().stateHome(),
context.selectedWorkflowPaths(),
context.selected());
});
return body.toString();
}
Why the replacement is better
The replacement consumes the Optional once and names the present value directly in the present branch. It makes the method's two outcomes explicit: present lookup renders only lookup output; empty lookup renders the full private context.
This is also a better use of Optional because ifPresentOrElse(...) is designed for side-effecting present/empty branches. It avoids a fake transformation chain, avoids orElse(null), avoids a generic helper, and preserves the original output ordering and behavior.
The eval should not blindly require Optional chains everywhere. It should prefer this shape only when the branch is ordinary Optional control flow and the target Java version supports ifPresentOrElse(...).
Desired eval behavior
- In an implementation task, trigger the Java Optionals skill when the proposed Java code introduces an Optional branch,
isPresent, orElseThrow, absent/missing lookup handling, or similar Optional control flow, even when the user did not explicitly ask for Optional advice.
- Reward implementing the new lookup branch with
ifPresentOrElse(...) from the start when both present and empty branches are side-effecting and behavior can be preserved.
- In a review/refactor task, reward recognizing
isPresent() plus get() / orElseThrow() as a weaker Optional style when the value is immediately consumed in a side-effecting branch.
- Reward explaining that the original code is functionally correct but less idiomatic and less direct.
- Reward preserving ordering, side effects, checked exceptions, and return behavior.
- Do not require the refactor when
ifPresentOrElse(...) is unavailable due to Java baseline.
- Do not require the refactor when checked exceptions, complex returns, or clearer imperative flow make an ordinary
if more readable.
Anti-patterns the eval should reject
- Introducing
optional.isPresent() plus optional.get() or optional.orElseThrow() for this simple present/empty side-effecting branch.
- Keeping that shape during review when Java 9+
ifPresentOrElse(...) is available and clearer.
- Replacing the branch with
orElse(null) followed by a null check.
- Building a generic Optional helper only to avoid
ifPresentOrElse(...).
- Using
map(...), orElseGet(...), or stream() in a way that hides side effects or changes execution order.
- Creating an intermediate collection or fake stream pipeline for one Optional value.
- Changing the output by rendering both lookup and full-context sections.
Suggested eval name
side-effecting-ifpresentorelse-lookup
Alternatives considered
A separate issue for the introduction-time trigger would duplicate the same code, preferred replacement, and scoring rule. Keeping both phases in one issue makes the desired eval clearer: the skill should prevent the antipattern during implementation and catch it during review.
A broader eval about all isPresent() usage would be too noisy because some imperative Optional checks are acceptable.
Current workaround
A reviewer can manually ask the agent to revisit isPresent() / orElseThrow() pairs with the Java Optionals skill, but the skill should learn to catch this pattern during the first implementation or review pass.
Additional context
The example is intentionally self-contained. It does not require access to the source repository or pull request to understand the Optional lesson.
AI Disclosure: This issue was prepared with AI assistance. The human contributor requested the eval capture and reviewed the intended behavior.
Problem
Add an eval that teaches the Java Optionals skill to prevent and repair
Optional.isPresent()plusorElseThrow()control flow when a Java 9+ side-effecting present/empty branch should useOptional.ifPresentOrElse(...).This issue covers two related failures:
isPresent,orElseThrow, absent/missing values, or Optional control flow.if (request.lookup().isPresent())was best practice.This is eval-worthy because the weaker code is functionally correct, but it uses null-style Optional control flow:
isPresent()gates the branch andorElseThrow()retrieves the same Optional value afterward. The skill should learn to trigger before introducing that shape, not only after a user explicitly calls it out.Area
Evals or scoring
Code before the feature prompt was executed
Before lookup support existed, the method always rendered the full private diagnostics context. There was no present/empty lookup branch yet:
Feature prompt that caused
isPresent()to be introducedThe implementation prompt asked for a focused private diagnostics token lookup feature. Relevant requirements included:
The implementation needed a new
request.lookup()branch: when lookup is present, render only the lookup result; when absent, render the full private diagnostics context.Prompt-produced code that introduced the weaker Optional shape
The feature implementation added the branch using
isPresent()andorElseThrow():Why the prompt-produced code is weak
It reads the same Optional twice.
request.lookup().isPresent()decides the branch, thenrequest.lookup().orElseThrow()retrieves the value. That is safe in this code, but it is the same null-check-then-get shape Optional is meant to avoid.It uses an exception-oriented accessor for a value already proven present.
orElseThrow()communicates failure handling, but this branch is not a failure path. It is ordinary present-value control flow.It splits one present/empty decision across an early return and the rest of the method.
The method has exactly two side-effecting rendering branches: render a lookup result when the Optional is present, or render the full private context when it is absent. That maps directly to
ifPresentOrElse(...).It shows a skill-trigger gap.
The Java Optionals skill description says to use it when writing, reviewing, or refactoring Optional code, and specifically when touching
isPresent,orElseThrow, absent/missing values, or nullable control flow. The feature prompt did not explicitly mention Optional style, so an eval should ensure the skill still triggers based on the code being introduced.Follow-up prompt that caused the repair
The user later asked:
The Java baseline for this code was Java 25, so Java 9+ Optional APIs such as
ifPresentOrElse(...)were available.Maintainer-preferred code
Why the replacement is better
The replacement consumes the Optional once and names the present value directly in the present branch. It makes the method's two outcomes explicit: present lookup renders only lookup output; empty lookup renders the full private context.
This is also a better use of Optional because
ifPresentOrElse(...)is designed for side-effecting present/empty branches. It avoids a fake transformation chain, avoidsorElse(null), avoids a generic helper, and preserves the original output ordering and behavior.The eval should not blindly require Optional chains everywhere. It should prefer this shape only when the branch is ordinary Optional control flow and the target Java version supports
ifPresentOrElse(...).Desired eval behavior
isPresent,orElseThrow, absent/missing lookup handling, or similar Optional control flow, even when the user did not explicitly ask for Optional advice.ifPresentOrElse(...)from the start when both present and empty branches are side-effecting and behavior can be preserved.isPresent()plusget()/orElseThrow()as a weaker Optional style when the value is immediately consumed in a side-effecting branch.ifPresentOrElse(...)is unavailable due to Java baseline.ifmore readable.Anti-patterns the eval should reject
optional.isPresent()plusoptional.get()oroptional.orElseThrow()for this simple present/empty side-effecting branch.ifPresentOrElse(...)is available and clearer.orElse(null)followed by a null check.ifPresentOrElse(...).map(...),orElseGet(...), orstream()in a way that hides side effects or changes execution order.Suggested eval name
side-effecting-ifpresentorelse-lookupAlternatives considered
A separate issue for the introduction-time trigger would duplicate the same code, preferred replacement, and scoring rule. Keeping both phases in one issue makes the desired eval clearer: the skill should prevent the antipattern during implementation and catch it during review.
A broader eval about all
isPresent()usage would be too noisy because some imperative Optional checks are acceptable.Current workaround
A reviewer can manually ask the agent to revisit
isPresent()/orElseThrow()pairs with the Java Optionals skill, but the skill should learn to catch this pattern during the first implementation or review pass.Additional context
The example is intentionally self-contained. It does not require access to the source repository or pull request to understand the Optional lesson.
AI Disclosure: This issue was prepared with AI assistance. The human contributor requested the eval capture and reviewed the intended behavior.