@llm4ts/flow 4.11.5 → 4.12.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.
@@ -4,6 +4,7 @@ import * as Fiber from "effect/Fiber"
4
4
  import * as Queue from "effect/Queue"
5
5
  import * as Semaphore from "effect/Semaphore"
6
6
  import {
7
+ Info,
7
8
  StageCompleted,
8
9
  StageFailed,
9
10
  StageStarted,
@@ -15,7 +16,7 @@ import {
15
16
  import { withKindSpan, type SpanOptions } from "./Spans.ts"
16
17
  import { FlowAborted, describeFlowError, type FlowError } from "./FlowError.ts"
17
18
  import { taskGraph, type Plan, type Task, type TaskNode } from "./Plan.ts"
18
- import type { PlanStoreShape } from "./Persistence.ts"
19
+ import type { PlanStoreShape, TaskAttemptsShape } from "./Persistence.ts"
19
20
  import { pathWithin } from "./StoryPlan.ts"
20
21
 
21
22
  const errorMessage = describeFlowError
@@ -68,15 +69,53 @@ const withSatisfies = (description: string): { readonly satisfies?: ReadonlyArra
68
69
  return satisfies === undefined ? {} : { satisfies }
69
70
  }
70
71
 
72
+ /** What a task's run is told about the runs before it (ADR 0056). */
73
+ export interface TaskAttempt {
74
+ /** Earlier runs of this task that a dead process cut off; 0 on a first run. */
75
+ readonly cutOff: number
76
+ }
77
+
78
+ /**
79
+ * Runs a task under the plan's cut-off journal: marked before, unmarked when
80
+ * it ends while the process lives. `lock` keeps tasks that run at once from
81
+ * writing the journal over each other.
82
+ */
83
+ const journaled = <A, E, R>(
84
+ attempts: TaskAttemptsShape | undefined,
85
+ lock: Semaphore.Semaphore,
86
+ events: FlowEventsShape,
87
+ task: Task,
88
+ run: (attempt: TaskAttempt) => Effect.Effect<A, E, R>
89
+ ): Effect.Effect<A, E | FlowError, R> => {
90
+ if (attempts === undefined) {
91
+ return run({ cutOff: 0 })
92
+ }
93
+ return Effect.gen(function* () {
94
+ const cutOff = yield* lock.withPermit(attempts.begin(task.title))
95
+ if (cutOff > 0) {
96
+ yield* events.publish(
97
+ Info.make({
98
+ message: `task "${task.title}": ${cutOff} earlier run(s) were cut off before they finished`
99
+ })
100
+ )
101
+ }
102
+ return yield* Effect.onExit(run({ cutOff }), (exit) =>
103
+ Effect.ignore(lock.withPermit(attempts.end(task.title, Exit.isSuccess(exit))))
104
+ )
105
+ })
106
+ }
107
+
71
108
  export const implementTaskLoop = Effect.fn("@llm4ts/flow/PlanExecution.implementTaskLoop")(
72
109
  function* <E, R>(
73
110
  store: PlanStoreShape,
74
111
  events: FlowEventsShape,
75
112
  planPath: string,
76
113
  plan: Plan,
77
- perTask: (task: Task, planSoFar: Plan) => Effect.Effect<void, E, R>
114
+ perTask: (task: Task, planSoFar: Plan, attempt: TaskAttempt) => Effect.Effect<void, E, R>
78
115
  ): Effect.fn.Return<Plan, E | FlowError, R> {
79
116
  let current = plan
117
+ const attempts = store.attempts?.(planPath)
118
+ const lock = yield* Semaphore.make(1)
80
119
  const count = plan.tasks.length
81
120
  yield* events.publish(
82
121
  TasksPlanned.make({
@@ -93,7 +132,12 @@ export const implementTaskLoop = Effect.fn("@llm4ts/flow/PlanExecution.implement
93
132
  yield* events.publish(
94
133
  TaskStarted.make({ index, count, title: task.title, ...withSatisfies(task.description) })
95
134
  )
96
- yield* stage(events, task.title, perTask(task, current))
135
+ const planSoFar = current
136
+ yield* stage(
137
+ events,
138
+ task.title,
139
+ journaled(attempts, lock, events, task, (attempt) => perTask(task, planSoFar, attempt))
140
+ )
97
141
  current = current.complete(task.title)
98
142
  yield* store.save(planPath, current)
99
143
  yield* events.publish(TaskCompleted.make({ index, count, title: task.title }))
@@ -134,6 +178,8 @@ export interface AsideRequest {
134
178
  /** The plan's size, for `task n/m`. */
135
179
  readonly count: number
136
180
  readonly planSoFar: Plan
181
+ /** What the task's run is told about the runs before it (ADR 0056). */
182
+ readonly attempt: TaskAttempt
137
183
  /** Runs `effect` while no task runs at home: the merge back into the story. */
138
184
  readonly exclusive: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
139
185
  /** Says the task started, on the coder it got; call it once that coder is held. */
@@ -170,7 +216,7 @@ export const implementTaskGraph = Effect.fn("@llm4ts/flow/PlanExecution.implemen
170
216
  events: FlowEventsShape,
171
217
  planPath: string,
172
218
  plan: Plan,
173
- perTask: (task: Task, planSoFar: Plan) => Effect.Effect<void, E, R>,
219
+ perTask: (task: Task, planSoFar: Plan, attempt: TaskAttempt) => Effect.Effect<void, E, R>,
174
220
  parallel: ParallelTasks<E2, R2>
175
221
  ): Effect.fn.Return<Plan, E | E2 | FlowError, R | R2> {
176
222
  const graph = taskGraph(plan)
@@ -191,8 +237,10 @@ export const implementTaskGraph = Effect.fn("@llm4ts/flow/PlanExecution.implemen
191
237
  const last = new Set<number>()
192
238
  const running = new Map<number, "home" | "aside">()
193
239
  const fibers: Array<Fiber.Fiber<unknown, unknown>> = []
194
- const finished = yield* Queue.unbounded<Finished<E | E2>>()
240
+ const finished = yield* Queue.unbounded<Finished<E | E2 | FlowError>>()
195
241
  const home = yield* Semaphore.make(1)
242
+ const attempts = store.attempts?.(planPath)
243
+ const journal = yield* Semaphore.make(1)
196
244
  const exclusive = <A, EX, RX>(effect: Effect.Effect<A, EX, RX>) => home.withPermit(effect)
197
245
 
198
246
  const started = (
@@ -235,7 +283,13 @@ export const implementTaskGraph = Effect.fn("@llm4ts/flow/PlanExecution.implemen
235
283
  exclusive(
236
284
  Effect.andThen(
237
285
  started(pick),
238
- stage(events, pick.task.title, perTask(pick.task, snapshot))
286
+ stage(
287
+ events,
288
+ pick.task.title,
289
+ journaled(attempts, journal, events, pick.task, (attempt) =>
290
+ perTask(pick.task, snapshot, attempt)
291
+ )
292
+ )
239
293
  )
240
294
  ).pipe(
241
295
  Effect.as(undefined),
@@ -258,10 +312,11 @@ export const implementTaskGraph = Effect.fn("@llm4ts/flow/PlanExecution.implemen
258
312
  continue
259
313
  }
260
314
  running.set(node.index, "aside")
261
- const request: AsideRequest = {
315
+ const request = (attempt: TaskAttempt): AsideRequest => ({
262
316
  node,
263
317
  count,
264
318
  planSoFar: current,
319
+ attempt,
265
320
  exclusive,
266
321
  announce: (coder) =>
267
322
  started(node, {
@@ -269,10 +324,12 @@ export const implementTaskGraph = Effect.fn("@llm4ts/flow/PlanExecution.implemen
269
324
  ...(coder.executor === undefined ? {} : { executor: coder.executor }),
270
325
  ...(coder.clone === undefined ? {} : { clone: coder.clone })
271
326
  })
272
- }
327
+ })
273
328
  fibers.push(
274
329
  yield* Effect.forkChild(
275
- parallel.aside(request).pipe(
330
+ journaled(attempts, journal, events, node.task, (attempt) =>
331
+ parallel.aside(request(attempt))
332
+ ).pipe(
276
333
  Effect.exit,
277
334
  Effect.flatMap((exit) => Queue.offer(finished, { kind: "aside", node, exit }))
278
335
  )
package/src/Stories.ts CHANGED
@@ -62,6 +62,7 @@ import {
62
62
  } from "./Perimeter.ts"
63
63
  import {
64
64
  loadVersioned,
65
+ attemptsPath,
65
66
  makePlanStore,
66
67
  saveVersioned,
67
68
  type PlainFileStoreShape
@@ -1298,6 +1299,7 @@ export const implementStoriesFlow = Effect.fn("@llm4ts/flow/Stories.implement")(
1298
1299
  // fresh branch would inherit "every task complete" and skip the coder.
1299
1300
  // The notes its tasks carried describe the old definition: gone too.
1300
1301
  yield* files.remove(planPath(story))
1302
+ yield* files.remove(attemptsPath(planPath(story)))
1301
1303
  yield* files.remove(notesPath(story))
1302
1304
  yield* files.remove(redTestsPath(options.stateDir, story.id))
1303
1305
  stored = undefined