@llm4ts/flow 4.10.0 → 4.11.1

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.
package/src/Activity.ts CHANGED
@@ -133,15 +133,7 @@ interface OpenTool {
133
133
  }
134
134
 
135
135
  export type ToolCategory =
136
- | "explore"
137
- | "edit"
138
- | "test"
139
- | "build"
140
- | "install"
141
- | "git"
142
- | "script"
143
- | "delegate"
144
- | "other"
136
+ "explore" | "edit" | "test" | "build" | "install" | "git" | "script" | "delegate" | "other"
145
137
 
146
138
  /**
147
139
  * A harness handing work to a sub-agent of its own: Claude's Agent (Task
@@ -161,16 +161,14 @@ const claimsOf = (epicId: string, brief: EpicBrief): ReadonlyArray<Located> => {
161
161
  }))
162
162
  return [
163
163
  ...brief.scope.flatMap((item) =>
164
- item.citations.map(
165
- (citation): Located => ({
166
- epicId,
167
- approved,
168
- disposition: "in-scope",
169
- note: item.title,
170
- program: citation.program,
171
- scenario: citation.scenario
172
- })
173
- )
164
+ item.citations.map((citation): Located => ({
165
+ epicId,
166
+ approved,
167
+ disposition: "in-scope",
168
+ note: item.title,
169
+ program: citation.program,
170
+ scenario: citation.scenario
171
+ }))
174
172
  ),
175
173
  ...disposed("dropped", brief.dropped),
176
174
  ...disposed("provided", brief.provided),
@@ -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
package/src/FlowEvents.ts CHANGED
@@ -442,11 +442,7 @@ export class ExecutorHandedOver extends Schema.TaggedClass<ExecutorHandedOver>()
442
442
  ) {}
443
443
 
444
444
  export type RosterEvent =
445
- | ExecutorLeased
446
- | ExecutorReleased
447
- | ExecutorExcluded
448
- | ExecutorResumed
449
- | ExecutorHandedOver
445
+ ExecutorLeased | ExecutorReleased | ExecutorExcluded | ExecutorResumed | ExecutorHandedOver
450
446
 
451
447
  const resumedWords: Readonly<Record<ExecutorResumed["why"], string>> = {
452
448
  expired: "back in the round",
package/src/Pack.ts CHANGED
@@ -141,8 +141,7 @@ export interface Pack {
141
141
  /** `## Audit` (ADR 0028): `- dimensions: a, b, c` the rulebook auditors take; default list in the flow. */
142
142
  readonly audit: ReadonlyArray<string> | undefined
143
143
  readonly diagnostics:
144
- | { readonly command: ReadonlyArray<string>; readonly format: DiagnosticsFormat }
145
- | undefined
144
+ { readonly command: ReadonlyArray<string>; readonly format: DiagnosticsFormat } | undefined
146
145
  readonly dir: string
147
146
  readonly gate: (name: string) => ReadonlyArray<string> | undefined
148
147
  readonly prompt: (name: string) => string | undefined
package/src/Provenance.ts CHANGED
@@ -53,22 +53,20 @@ export const makeProvenanceStore = (files: PlainFileStoreShape): ProvenanceStore
53
53
  )
54
54
 
55
55
  const load = (path: string): Effect.Effect<Provenance, FlowError> =>
56
- Effect.flatMap(
57
- files.read(path),
58
- (contents): Effect.Effect<Provenance, FlowError> =>
59
- contents === undefined
60
- ? Effect.fail(
61
- PersistenceError.make({
62
- message: `provenance manifest does not exist: ${path}`
56
+ Effect.flatMap(files.read(path), (contents): Effect.Effect<Provenance, FlowError> =>
57
+ contents === undefined
58
+ ? Effect.fail(
59
+ PersistenceError.make({
60
+ message: `provenance manifest does not exist: ${path}`
61
+ })
62
+ )
63
+ : Schema.decodeUnknownEffect(Schema.fromJsonString(Provenance))(contents).pipe(
64
+ Effect.mapError((error) =>
65
+ PlanParseError.make({
66
+ message: `malformed provenance manifest ${path}: ${String(error)}`
63
67
  })
64
68
  )
65
- : Schema.decodeUnknownEffect(Schema.fromJsonString(Provenance))(contents).pipe(
66
- Effect.mapError((error) =>
67
- PlanParseError.make({
68
- message: `malformed provenance manifest ${path}: ${String(error)}`
69
- })
70
- )
71
- )
69
+ )
72
70
  )
73
71
 
74
72
  const extend = (
@@ -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,26 @@ 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
+
529
549
  /**
530
550
  * What the task planner is told after a red phase (ADR 0053): the tests are
531
551
  * written and frozen, so the tasks write the production code that makes
@@ -850,6 +870,14 @@ export interface StoriesOptions {
850
870
  * verdict judged under one bar is never reused under another.
851
871
  */
852
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
853
881
  /**
854
882
  * Second opinion on a BLOCKED_ON claim the plan cannot settle (a path no
855
883
  * story owns, or a parallel story's). `real: false` sends the coder back
@@ -2185,6 +2213,66 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2185
2213
  return { verdict, empty }
2186
2214
  })
2187
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
+
2188
2276
  /**
2189
2277
  * The story review (ADR 0044): every lens and the judge on the story's
2190
2278
  * diff, one fix turn per round, re-checks after the first. Returns the
@@ -2308,9 +2396,29 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2308
2396
  }
2309
2397
  })
2310
2398
  const rounds = yield* Ref.get(lastRound)
2311
- const left = result.issues
2399
+ const reviewed = result.issues
2312
2400
  // Lenses and the judge always name themselves: a finding with no
2313
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")
2314
2422
  const gateLeft = left.filter((issue) => issue.lens === undefined)
2315
2423
  if (gateLeft.length > 0) {
2316
2424
  return yield* failed(
@@ -2343,11 +2451,14 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2343
2451
  ReviewResult.make({ issues: carried })
2344
2452
  )
2345
2453
  }
2346
- return judge === undefined
2347
- ? undefined
2348
- : `judge cleared (story review round ${rounds}${
2349
- waiting.length === 0 ? "" : `, ${waiting.length} finding(s) deferred`
2350
- })`
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
+ )
2351
2462
  })
2352
2463
 
2353
2464
  let judgeNote: string | undefined
@@ -2374,6 +2485,17 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
2374
2485
  }
2375
2486
  if (round >= judgeRounds) {
2376
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
+ }
2377
2499
  return yield* failed(
2378
2500
  story,
2379
2501
  empty
@@ -353,6 +353,13 @@ 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
+ }),
356
363
  LLM4TS_TEST_FIRST: setting({
357
364
  group: "review",
358
365
  scope: "project",