Add agent rehearsal harness, findings, and per-run evidence for split-loop-with-lambda

Six scripted pi sub-agent runs (3 tool regimes × 2 models) rehearsed the
exercise; deterministic gate = solution test + approved file. Key results:
mandated CLI triples cost on the strong model (4:06 vs 0:39, PASS both);
weak model fails only under the mandate (A+Q), passes when free or optional.
Drift documented with code: scorer.updateSelection left inside the extracted
method (A+Q, C+Q). Harness in tools/rehearse; summary in
rehearsal-findings.md; per-run evidence in rehearsal-runs/; cheat sheet in
rehearse-with-agent.md corrected (lambda-typed parameters unsupported).
This commit is contained in:
Your Name
2026-09-27 17:36:19 +00:00
parent a27e32688a
commit b993f25c53
51 changed files with 2281 additions and 2 deletions
@@ -0,0 +1,27 @@
# Rehearsal workspace — refactoring task
You are refactoring `src/main/java/codingdojo/LongRangeForecasts.java` in this
Maven project. Run `mvn -q test` after each change (mvn is on your PATH). It may
fail — that is expected: the dependency on `Scorer` makes the method untestable —
but the code must still *compile*.
## Goal
In `LongRangeForecasts.longRangeForecast()`, the loop over `interestingDates`
collects weather by calling `scorer` (an unwanted external dependency).
1. Introduce a lambda for the scorer-dependent code and move its definition to
the top of the method, leaving the invocation where it is. (No refactoring
tool supports this — do it as a minimal manual edit.)
2. Extract the weather-collection loop into a new method `collectExpectedWeather`
that takes the lambda as a parameter (suggested type `LongFunction<Boolean>`).
3. Add the lambda as a parameter to `longRangeForecast` itself: the signature
must become `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`,
with a public no-arg overload `longRangeForecast()` that delegates and wraps
the `scorer` field in a lambda. Do not keep the `String quarter` parameter.
(`java-refactor change-method-signature` or `java-refactor introduce-param`
may help; fall back to a minimal manual edit if no subcommand can do it.)
4. Stop. Report the final diff.
## Tool rules (variant B)
You may change code with your normal file-editing tools.
@@ -0,0 +1,103 @@
Only in /tmp/rehearsal/runs/B/workspace: AGENTS.md
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java /tmp/rehearsal/runs/B/workspace/src/main/java/codingdojo/LongRangeForecasts.java
--- /tmp/rehearsal/pristine/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:39:01.304467310 +0000
+++ /tmp/rehearsal/runs/B/workspace/src/main/java/codingdojo/LongRangeForecasts.java 2026-09-27 14:53:31.157357707 +0000
@@ -3,36 +3,25 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
+import java.util.function.LongFunction;
public class LongRangeForecasts {
private final Scorer scorer = new Scorer();
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
+ public Map<IceCream, Integer> longRangeForecast() {
+ return longRangeForecast(daysForward -> {
+ scorer.updateSelection();
+ return scorer.lookupWeather(daysForward);
+ });
+ }
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
var result = new HashMap<IceCream, Integer>();
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
- for (IceCream flavour : IceCream.values()) {
- result.put(flavour, 5);
- }
- }
for (IceCream flavour : IceCream.values()) {
result.put(flavour, 0);
}
- List<Instant> interestingDates = List.of(
- Instant.parse("2023-05-01T14:00:00.000-07:00"),
- Instant.parse("2023-05-18T14:00:00.000-07:00"),
- Instant.parse("2023-06-05T14:00:00.000-07:00"),
- Instant.parse("2023-06-23T14:00:00.000-07:00")
- );
- List<Boolean> expectedWeather = new ArrayList<>();
- for (Instant interestingDate : interestingDates) {
- scorer.updateSelection();
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
- expectedWeather.add(lookupWeather);
- }
+ List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
for (IceCream flavour : result.keySet()) {
result.put(flavour, result.get(flavour) + 10);
@@ -46,4 +35,21 @@
}
return result;
}
+
+ private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
+ List<Instant> interestingDates = List.of(
+ Instant.parse("2023-05-01T14:00:00.000-07:00"),
+ Instant.parse("2023-05-18T14:00:00.000-07:00"),
+ Instant.parse("2023-06-05T14:00:00.000-07:00"),
+ Instant.parse("2023-06-23T14:00:00.000-07:00")
+ );
+ List<Boolean> expectedWeather = new ArrayList<>();
+ for (Instant interestingDate : interestingDates) {
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
+ Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
+ expectedWeather.add(lookupWeather);
+ }
+ return expectedWeather;
+ }
}
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:39:03.626410227 +0000
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/DailyForecastsTest.salesForecast.received.txt 2026-09-27 14:53:35.478277153 +0000
@@ -1,4 +1,4 @@
Forecast at: 2026-09-27
Steve score: 9
-Julie score: 9
-Francis score: 9
+Julie score: 3
+Francis score: 11
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:39:01.304467310 +0000
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.java 2026-09-27 14:53:33.005323250 +0000
@@ -13,8 +13,7 @@
@Test
void longRangeForecast() {
- var quarter = "Q3";
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
+ var actual = new LongRangeForecasts().longRangeForecast();
StringBuilder printedResult = printIceCreamForecast(actual);
Approvals.verify(printedResult);
diff -ru '--exclude=target' /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt
--- /tmp/rehearsal/pristine/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:39:03.582411308 +0000
+++ /tmp/rehearsal/runs/B/workspace/src/test/java/codingdojo/LongRangeForecastsTest.longRangeForecast.received.txt 2026-09-27 14:53:35.437277917 +0000
@@ -1 +1 @@
-{Strawberry:12, Chocolate:12, Vanilla:12, }
\ No newline at end of file
+{Strawberry:12, Chocolate:12, Vanilla:17, }
\ No newline at end of file
@@ -0,0 +1,35 @@
--- Run B ---
session file: /tmp/rehearsal/runs/B/session/2026-09-27T14-53-00-499Z_01a0e35a-e6d3-70ca-b02c-6d99534cf397.jsonl
model: z-ai/glm-5.3-flash
session span: 39.2 s
assistant messages: 10
tool calls: 10 total
bash 5
edit 4
read 1
tokens input: 21722
tokens output: 4361
tokens cacheRead: 33984
tokens cacheWrite: 0
tokens reasoning: 2129
tokens TOTAL (sum): 62196
--- /usr/bin/time -v (wall clock + max RSS) ---
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:39.35
Maximum resident set size (kbytes): 249312
Exit status: 0
--- pi stdout (last 15 lines) ---
Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
expectedWeather.add(lookupWeather);
}
return expectedWeather;
}
}
```
**Diff summary** (relative to the original):
1. Added import `java.util.function.LongFunction`.
2. The scorer-dependent code (`scorer.updateSelection()` + `scorer.lookupWeather(daysForward)`) was extracted into a lambda, now passed in as `dayOffsetToWeather` (`LongFunction<Boolean>`); the invocation site (`dayOffsetToWeather.apply(daysForward)`) stayed in the loop.
3. The weather-collection loop (with `interestingDates`) moved into new private method `collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning `List<Boolean>`.
4. Signature changed from `longRangeForecast(String quarter)` to `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`; the `String quarter` parameter and its `Q1`/`Q4` block were removed, and a public no-arg overload was added that delegates and wraps the `scorer` field in a lambda.
**Note:** the test call site in `src/test/java/codingdojo/LongRangeForecastsTest.java` was updated (`longRangeForecast(quarter)` → `longRangeForecast()`) so the project still compiles after the signature change.
@@ -0,0 +1,69 @@
Done. All three refactoring steps are complete; after each one `mvn -q test` compiled successfully (the remaining test failures are the pre-existing ones caused by `Scorer`'s use of `new Random().nextBoolean()` — the expected untestability, not a compile problem). Stopping here as instructed.
There's no git repo in the workspace, so here is the final state and a manual diff summary of `LongRangeForecasts.java`:
```java
package codingdojo;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.LongFunction;
public class LongRangeForecasts {
private final Scorer scorer = new Scorer();
public Map<IceCream, Integer> longRangeForecast() {
return longRangeForecast(daysForward -> {
scorer.updateSelection();
return scorer.lookupWeather(daysForward);
});
}
public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
var result = new HashMap<IceCream, Integer>();
for (IceCream flavour : IceCream.values()) {
result.put(flavour, 0);
}
List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
for (IceCream flavour : result.keySet()) {
result.put(flavour, result.get(flavour) + 10);
var sunnyHolidays = expectedWeather.stream().filter(s -> s).count();
if (sunnyHolidays > 2 && flavour == IceCream.Vanilla) {
result.put(flavour, result.get(flavour) + 5);
}
if (sunnyHolidays > 1 && expectedWeather.get(1)) {
result.put(flavour, result.get(flavour) + 2);
}
}
return result;
}
private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
List<Instant> interestingDates = List.of(
Instant.parse("2023-05-01T14:00:00.000-07:00"),
Instant.parse("2023-05-18T14:00:00.000-07:00"),
Instant.parse("2023-06-05T14:00:00.000-07:00"),
Instant.parse("2023-06-23T14:00:00.000-07:00")
);
List<Boolean> expectedWeather = new ArrayList<>();
for (Instant interestingDate : interestingDates) {
var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
expectedWeather.add(lookupWeather);
}
return expectedWeather;
}
}
```
**Diff summary** (relative to the original):
1. Added import `java.util.function.LongFunction`.
2. The scorer-dependent code (`scorer.updateSelection()` + `scorer.lookupWeather(daysForward)`) was extracted into a lambda, now passed in as `dayOffsetToWeather` (`LongFunction<Boolean>`); the invocation site (`dayOffsetToWeather.apply(daysForward)`) stayed in the loop.
3. The weather-collection loop (with `interestingDates`) moved into new private method `collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning `List<Boolean>`.
4. Signature changed from `longRangeForecast(String quarter)` to `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`; the `String quarter` parameter and its `Q1`/`Q4` block were removed, and a public no-arg overload was added that delegates and wraps the `scorer` field in a lambda.
**Note:** the test call site in `src/test/java/codingdojo/LongRangeForecastsTest.java` was updated (`longRangeForecast(quarter)` → `longRangeForecast()`) so the project still compiles after the signature change.
@@ -0,0 +1 @@
Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java.
@@ -0,0 +1,23 @@
Command being timed: "timeout -k 15 600 pi -p -a --session-dir /tmp/rehearsal/runs/B/session --name rehearsal-B Read AGENTS.md in this directory and execute the refactoring task it describes. Run mvn -q test after each change. When done, stop and report the final diff of LongRangeForecasts.java."
User time (seconds): 15.30
System time (seconds): 1.08
Percent of CPU this job got: 41%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:39.35
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 249312
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 332026
Voluntary context switches: 28517
Involuntary context switches: 935
Swaps: 0
File system inputs: 0
File system outputs: 1624
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
@@ -0,0 +1,10 @@
--- Run B ---
final diff vs pristine: 54 changed lines -> /tmp/rehearsal/runs/B/diff.patch
mvn test with solution's test + approved file:
RESULT: PASS (deterministic)
structural checks:
[x] method collectExpectedWeather exists
[x] takes LongFunction<Boolean> parameter
[i] note: 'scorer' still referenced in LongRangeForecasts.java
[x] lambda syntax present
[i] main class differs from reference solution (may still be a valid variant)