@llm4ts/flow 4.9.0 → 4.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,12 @@ export interface FlowContextShape {
37
37
  * on a context built by hand; judges then fall back to `reasoning`.
38
38
  */
39
39
  readonly judge?: LlmServiceShape
40
+ /**
41
+ * The judge's agent with write access, for the one rescue turn a story
42
+ * gets when its last round would fail it (ADR 0054): over a roster, a
43
+ * judge executor seated as a coder. Absent unless the flow asks for it.
44
+ */
45
+ readonly rescuer?: LlmServiceShape
40
46
  readonly coderCapabilities: ConnectorCapabilities
41
47
  readonly userPrompt: string
42
48
  readonly workDir: string
@@ -118,6 +118,13 @@ export interface RosterSeatOptions {
118
118
  readonly borrow?: Effect.Effect<ExecutorSpec | undefined>
119
119
  /** Who is asking, for the events. */
120
120
  readonly label?: string
121
+ /**
122
+ * The role the leased executor is seated as, when not `role` itself: a
123
+ * judge slot seated as a coder writes (the rescuer, ADR 0054).
124
+ */
125
+ readonly seatAs?: Role
126
+ /** Told which executor took each call. */
127
+ readonly onLease?: (executor: string) => Effect.Effect<void>
121
128
  }
122
129
 
123
130
  /**
@@ -155,7 +162,23 @@ export const rosterSeat = (
155
162
  })
156
163
 
157
164
  const seatOf = (executor: ExecutorSpec): Effect.Effect<RosterSeat, LlmError> =>
158
- source.seatFor(executor, role, workDir).pipe(Effect.mapError(asLlmError))
165
+ // Seated as another role only when the roster lists it for that role too:
166
+ // a judge-only executor is never given a coder's write access (ADR 0054).
167
+ options.seatAs !== undefined && !hasRole(executor, options.seatAs)
168
+ ? Effect.fail(
169
+ asLlmError(
170
+ RosterExhausted.make({
171
+ role: options.seatAs,
172
+ reasons: [`executor '${executor.id}' is not listed as a ${options.seatAs}`]
173
+ })
174
+ )
175
+ )
176
+ : Effect.andThen(
177
+ options.onLease === undefined ? Effect.void : options.onLease(executor.id),
178
+ source
179
+ .seatFor(executor, options.seatAs ?? role, workDir)
180
+ .pipe(Effect.mapError(asLlmError))
181
+ )
159
182
 
160
183
  /**
161
184
  * After a failure that took `executor` out of the round, whether another
package/src/Stories.ts CHANGED
@@ -526,6 +526,42 @@ export const storyTaskPlanInstructions = (story: Story): string =>
526
526
  '[{"title":"...","description":"...","completed":false}]}'
527
527
  ].join("\n")
528
528
 
529
+ /**
530
+ * The rescuer's one turn (ADR 0054): the story, what is still open, and that
531
+ * the coder's rounds are spent, so the fix is the rescuer's to make now.
532
+ */
533
+ export const rescuePrompt = (story: Story, why: string, standing: ReviewResult): string =>
534
+ [
535
+ `You are taking over story ${story.id} ("${story.title}") from its coder: ${why}.`,
536
+ "Fix what is still open below yourself, in this working tree, inside the story's owned",
537
+ "paths. Change production code; never change, skip or delete a frozen red test. Run the",
538
+ "checks you need, then stop: the gates and the judge decide whether the story merges.",
539
+ "",
540
+ story.description.trim(),
541
+ ...(story.acceptance.length === 0
542
+ ? []
543
+ : ["", "Done when:", ...story.acceptance.map((line) => `- ${line}`)]),
544
+ "",
545
+ "Still open:",
546
+ issueLines(standing)
547
+ ].join("\n")
548
+
549
+ /**
550
+ * What the task planner is told after a red phase (ADR 0053): the tests are
551
+ * written and frozen, so the tasks write the production code that makes
552
+ * them pass and never plan the tests again.
553
+ */
554
+ export const redTestsNote = (frozen: ReadonlyArray<string>): ReadonlyArray<string> =>
555
+ frozen.length === 0
556
+ ? []
557
+ : [
558
+ "",
559
+ "The story's tests are already written, committed and frozen (test-first):",
560
+ bullets(frozen),
561
+ "Plan the tasks that make them pass by writing production code. Do not plan a task that",
562
+ "writes, changes, skips or deletes these tests; a task may add further tests of its own."
563
+ ]
564
+
529
565
  /** The coder plans its own tasks from the story — the orchestrator split the epic, not the story. */
530
566
  export const defaultPlanTasks = (
531
567
  seats: StorySeats,
@@ -834,6 +870,14 @@ export interface StoriesOptions {
834
870
  * verdict judged under one bar is never reused under another.
835
871
  */
836
872
  readonly judgeBar?: string
873
+ /**
874
+ * The rescue (ADR 0054): when a story's last round would fail it, the
875
+ * context's `rescuer` (the judge's agent, with write access) takes the
876
+ * coder's seat for one turn on what is still open; the gates and one more
877
+ * judge verdict then decide. Off, or without a rescuer, the story fails as
878
+ * before.
879
+ */
880
+ readonly rescue?: boolean
837
881
  /**
838
882
  * Second opinion on a BLOCKED_ON claim the plan cannot settle (a path no
839
883
  * story owns, or a parallel story's). `real: false` sends the coder back
@@ -1927,7 +1971,7 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
1927
1971
  `story ${story.id}: plan tasks`,
1928
1972
  planTasks(watchedSeats, story, text)
1929
1973
  ),
1930
- prompt,
1974
+ [prompt, ...redTestsNote(redTests?.frozen ?? [])].join("\n"),
1931
1975
  laneOf(story)
1932
1976
  ),
1933
1977
  system: taskSystem,
@@ -2169,6 +2213,66 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2169
2213
  return { verdict, empty }
2170
2214
  })
2171
2215
 
2216
+ /**
2217
+ * The rescue (ADR 0054): the story's last round left `standing` open, so
2218
+ * the judge's agent takes the coder's seat for one turn, then the gates
2219
+ * and one judge verdict decide. Undefined when no rescue is configured;
2220
+ * the story's note when it cleared; the story fails otherwise.
2221
+ */
2222
+ const rescue = (
2223
+ standing: ReviewResult,
2224
+ why: string,
2225
+ round: number
2226
+ ): Effect.Effect<string | undefined, FlowError> =>
2227
+ Effect.gen(function* () {
2228
+ const rescuer = storyContext.rescuer
2229
+ if (options.rescue !== true || rescuer === undefined) {
2230
+ return undefined
2231
+ }
2232
+ yield* laneOf(story).publish(
2233
+ Info.make({
2234
+ lane: story.id,
2235
+ message: `story ${story.id}: ${why}; the judge's agent takes the coder's seat for one rescue turn`
2236
+ })
2237
+ )
2238
+ yield* stage(
2239
+ laneOf(story),
2240
+ `story ${story.id}: rescue`,
2241
+ Effect.flatMap(makeChat(rescuer, { system, events, agent: "rescuer" }), (chat) =>
2242
+ chat.ask(rescuePrompt(story, why, standing))
2243
+ )
2244
+ )
2245
+ if ((yield* git.uncommittedFiles).length > 0) {
2246
+ yield* git.commitAll(`${story.id}: rescue`)
2247
+ }
2248
+ yield* repairPerimeter
2249
+ const reported = yield* Ref.make<ReadonlySet<string>>(new Set())
2250
+ const regated = yield* applyTriage(yield* gates, triage, laneOf(story), reported)
2251
+ if (!regated.isClean) {
2252
+ return yield* failed(
2253
+ story,
2254
+ `${why}; the rescue left its gates red:\n${issueLines(regated)}`
2255
+ )
2256
+ }
2257
+ if (options.judge === undefined) {
2258
+ return "gates green after a rescue"
2259
+ }
2260
+ const { verdict } = yield* judgeOnce(options.judge, round)
2261
+ if (!verdict.isClean && !(deferring && !verdict.issues.some(isBlocking))) {
2262
+ return yield* failed(
2263
+ story,
2264
+ `${why}; judge not cleared after a rescue:\n${issueLines(verdict)}`
2265
+ )
2266
+ }
2267
+ const waiting = nonBlockingIssues(verdict)
2268
+ for (const issue of waiting) {
2269
+ deferred.push(deferredLine(`judge after rescue`, issue))
2270
+ }
2271
+ return `judge cleared after a rescue (round ${round}${
2272
+ waiting.length === 0 ? "" : `, ${waiting.length} finding(s) deferred`
2273
+ })`
2274
+ })
2275
+
2172
2276
  /**
2173
2277
  * The story review (ADR 0044): every lens and the judge on the story's
2174
2278
  * diff, one fix turn per round, re-checks after the first. Returns the
@@ -2292,9 +2396,29 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2292
2396
  }
2293
2397
  })
2294
2398
  const rounds = yield* Ref.get(lastRound)
2295
- const left = result.issues
2399
+ const reviewed = result.issues
2296
2400
  // Lenses and the judge always name themselves: a finding with no
2297
2401
  // lens is a gate's (target gates, oracle, perimeter, frozen tests).
2402
+ const gateOpen = reviewed.filter((issue) => issue.lens === undefined)
2403
+ const judgeOpen = reviewed.filter(
2404
+ (issue) => issue.lens === "judge" && (!deferring || isBlocking(issue))
2405
+ )
2406
+ const roundsRan = `${rounds} story review round${rounds === 1 ? "" : "s"}`
2407
+ const rescued =
2408
+ gateOpen.length === 0 && judgeOpen.length === 0
2409
+ ? undefined
2410
+ : yield* rescue(
2411
+ ReviewResult.make({ issues: [...gateOpen, ...judgeOpen] }),
2412
+ gateOpen.length > 0
2413
+ ? `its gates are still red after ${roundsRan}`
2414
+ : `judge not cleared after ${roundsRan}`,
2415
+ (yield* Ref.get(judgeRuns)) + 1
2416
+ )
2417
+ // A rescue that cleared the story answered the gates and the judge.
2418
+ const left =
2419
+ rescued === undefined
2420
+ ? reviewed
2421
+ : reviewed.filter((issue) => issue.lens !== undefined && issue.lens !== "judge")
2298
2422
  const gateLeft = left.filter((issue) => issue.lens === undefined)
2299
2423
  if (gateLeft.length > 0) {
2300
2424
  return yield* failed(
@@ -2327,11 +2451,14 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2327
2451
  ReviewResult.make({ issues: carried })
2328
2452
  )
2329
2453
  }
2330
- return judge === undefined
2331
- ? undefined
2332
- : `judge cleared (story review round ${rounds}${
2333
- waiting.length === 0 ? "" : `, ${waiting.length} finding(s) deferred`
2334
- })`
2454
+ return (
2455
+ rescued ??
2456
+ (judge === undefined
2457
+ ? undefined
2458
+ : `judge cleared (story review round ${rounds}${
2459
+ waiting.length === 0 ? "" : `, ${waiting.length} finding(s) deferred`
2460
+ })`)
2461
+ )
2335
2462
  })
2336
2463
 
2337
2464
  let judgeNote: string | undefined
@@ -2358,6 +2485,17 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2358
2485
  }
2359
2486
  if (round >= judgeRounds) {
2360
2487
  const revisions = `${round - 1} revision${round === 2 ? "" : "s"}`
2488
+ const rescued = yield* rescue(
2489
+ verdict,
2490
+ empty
2491
+ ? `the story's code is not in place after ${revisions}`
2492
+ : `judge not cleared after ${revisions}`,
2493
+ round + 1
2494
+ )
2495
+ if (rescued !== undefined) {
2496
+ judgeNote = rescued
2497
+ break
2498
+ }
2361
2499
  return yield* failed(
2362
2500
  story,
2363
2501
  empty
@@ -353,6 +353,20 @@ export const settings = {
353
353
  fallback: fallback.value(false, "off"),
354
354
  doc: "Records the judge's tests score as debt instead of failing the story; --defer-tests sets it too."
355
355
  }),
356
+ LLM4TS_RESCUE: setting({
357
+ group: "review",
358
+ scope: "project",
359
+ value: Value.toggle,
360
+ fallback: fallback.value(true, "on"),
361
+ doc: "epic-stories gives a story its last round would fail one rescue turn by the judge's agent, with write access, before failing it; off (or --no-rescue) fails it at once."
362
+ }),
363
+ LLM4TS_TEST_FIRST: setting({
364
+ group: "review",
365
+ scope: "project",
366
+ value: Value.toggle,
367
+ fallback: fallback.value(true, "on"),
368
+ doc: "epic-stories writes each story's tests first and proves them red before its tasks are planned; off (or --no-test-first) plans tasks straight from the story."
369
+ }),
356
370
  LLM4TS_STALL_REPEATS: setting({
357
371
  group: "review",
358
372
  scope: "project",