@llm4ts/flow 0.15.0 → 0.16.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.
- package/dist/BenchReport.d.ts +2 -2
- package/dist/BenchReport.d.ts.map +1 -1
- package/dist/CostLedger.d.ts +2 -2
- package/dist/CostLedger.d.ts.map +1 -1
- package/dist/Equiv.d.ts +3 -3
- package/dist/Equiv.d.ts.map +1 -1
- package/dist/Flow.d.ts +1 -1
- package/dist/Flow.d.ts.map +1 -1
- package/dist/FlowContext.d.ts +10 -0
- package/dist/FlowContext.d.ts.map +1 -1
- package/dist/FlowContext.js.map +1 -1
- package/dist/FlowError.d.ts +42 -1
- package/dist/FlowError.d.ts.map +1 -1
- package/dist/FlowError.js +60 -1
- package/dist/FlowError.js.map +1 -1
- package/dist/GitTool.d.ts +15 -1
- package/dist/GitTool.d.ts.map +1 -1
- package/dist/GitTool.js +30 -2
- package/dist/GitTool.js.map +1 -1
- package/dist/Perimeter.d.ts +13 -0
- package/dist/Perimeter.d.ts.map +1 -0
- package/dist/Perimeter.js +34 -0
- package/dist/Perimeter.js.map +1 -0
- package/dist/Persistence.d.ts +2 -2
- package/dist/Persistence.d.ts.map +1 -1
- package/dist/PlanExecution.d.ts +1 -1
- package/dist/PlanExecution.d.ts.map +1 -1
- package/dist/ProgramJudge.d.ts +1 -1
- package/dist/ProgramJudge.d.ts.map +1 -1
- package/dist/Replay.d.ts +2 -2
- package/dist/Replay.d.ts.map +1 -1
- package/dist/Review.d.ts +2 -2
- package/dist/Review.d.ts.map +1 -1
- package/dist/ReviewCache.d.ts +1 -1
- package/dist/ReviewCache.d.ts.map +1 -1
- package/dist/Stories.d.ts +109 -0
- package/dist/Stories.d.ts.map +1 -0
- package/dist/Stories.js +432 -0
- package/dist/Stories.js.map +1 -0
- package/dist/StoryPlan.d.ts +81 -0
- package/dist/StoryPlan.d.ts.map +1 -0
- package/dist/StoryPlan.js +242 -0
- package/dist/StoryPlan.js.map +1 -0
- package/dist/TransientRetry.d.ts +18 -0
- package/dist/TransientRetry.d.ts.map +1 -1
- package/dist/TransientRetry.js +35 -3
- package/dist/TransientRetry.js.map +1 -1
- package/package.json +5 -2
- package/src/FlowContext.ts +10 -0
- package/src/FlowError.ts +74 -1
- package/src/GitTool.ts +74 -4
- package/src/Perimeter.ts +49 -0
- package/src/Stories.ts +670 -0
- package/src/StoryPlan.ts +353 -0
- package/src/TransientRetry.ts +76 -7
package/src/StoryPlan.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// The epic-level plan of the parallel story executor (ADR 0013): stories
|
|
2
|
+
// with a declared dependency graph and declared file ownership. Dependencies
|
|
3
|
+
// are declared here, up front, never discovered by a running coder.
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Schema from "effect/Schema"
|
|
6
|
+
import { PlanParseError, StoryPlanInvalid, type PersistenceError } from "./FlowError.ts"
|
|
7
|
+
import type { PlainFileStoreShape } from "./Persistence.ts"
|
|
8
|
+
import { stableHash } from "./Plan.ts"
|
|
9
|
+
|
|
10
|
+
export const StoryPlanVersion = 1
|
|
11
|
+
|
|
12
|
+
export class Story extends Schema.Class<Story>("Story")({
|
|
13
|
+
/** Kebab-case, unique within the plan; names the branch and the worktree. */
|
|
14
|
+
id: Schema.String,
|
|
15
|
+
title: Schema.String,
|
|
16
|
+
description: Schema.String,
|
|
17
|
+
/** Story ids this one waits for; they are merged before this one starts. */
|
|
18
|
+
dependsOn: Schema.Array(Schema.String).pipe(
|
|
19
|
+
Schema.withConstructorDefault(Effect.succeed([])),
|
|
20
|
+
Schema.withDecodingDefaultKey(Effect.succeed([]))
|
|
21
|
+
),
|
|
22
|
+
/** Repo-relative path prefixes (files or directories) the story may create or change. */
|
|
23
|
+
owned: Schema.Array(Schema.String),
|
|
24
|
+
/** Path prefixes fed as context and forbidden to change. */
|
|
25
|
+
sharedReadOnly: Schema.Array(Schema.String).pipe(
|
|
26
|
+
Schema.withConstructorDefault(Effect.succeed([])),
|
|
27
|
+
Schema.withDecodingDefaultKey(Effect.succeed([]))
|
|
28
|
+
),
|
|
29
|
+
/** What dependents may rely on: routes, exports, contracts. */
|
|
30
|
+
provides: Schema.Array(Schema.String).pipe(
|
|
31
|
+
Schema.withConstructorDefault(Effect.succeed([])),
|
|
32
|
+
Schema.withDecodingDefaultKey(Effect.succeed([]))
|
|
33
|
+
)
|
|
34
|
+
}) {}
|
|
35
|
+
|
|
36
|
+
export class StoryPlan extends Schema.Class<StoryPlan>("StoryPlan")({
|
|
37
|
+
epicId: Schema.String,
|
|
38
|
+
/** The epic as the operator phrased it. */
|
|
39
|
+
epic: Schema.String,
|
|
40
|
+
stories: Schema.Array(Story)
|
|
41
|
+
}) {
|
|
42
|
+
story(id: string): Story | undefined {
|
|
43
|
+
return this.stories.find((story) => story.id === id)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A path prefix in canonical form: no `./`, no trailing slash, forward slashes. */
|
|
48
|
+
export const normalizePath = (path: string): string =>
|
|
49
|
+
path
|
|
50
|
+
.trim()
|
|
51
|
+
.replace(/\\/g, "/")
|
|
52
|
+
.replace(/^(\.\/)+/, "")
|
|
53
|
+
.replace(/\/+$/, "")
|
|
54
|
+
|
|
55
|
+
/** Whether `path` is `prefix` itself or lies under it. */
|
|
56
|
+
export const pathWithin = (path: string, prefix: string): boolean => {
|
|
57
|
+
const target = normalizePath(path)
|
|
58
|
+
const root = normalizePath(prefix)
|
|
59
|
+
return root.length === 0 || target === root || target.startsWith(`${root}/`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const overlapping = (left: string, right: string): boolean =>
|
|
63
|
+
pathWithin(left, right) || pathWithin(right, left)
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Every violation of the plan's invariants, in one pass — an operator fixing
|
|
67
|
+
* a hand-edited plan wants the whole list, not a rerun per finding.
|
|
68
|
+
*/
|
|
69
|
+
export const storyPlanViolations = (plan: StoryPlan): ReadonlyArray<string> => {
|
|
70
|
+
const violations: Array<string> = []
|
|
71
|
+
const ids = new Set<string>()
|
|
72
|
+
for (const story of plan.stories) {
|
|
73
|
+
if (ids.has(story.id)) {
|
|
74
|
+
violations.push(`duplicate story id '${story.id}'`)
|
|
75
|
+
}
|
|
76
|
+
ids.add(story.id)
|
|
77
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(story.id)) {
|
|
78
|
+
violations.push(`story id '${story.id}' is not kebab-case`)
|
|
79
|
+
}
|
|
80
|
+
if (story.owned.length === 0) {
|
|
81
|
+
violations.push(`story '${story.id}' owns no paths`)
|
|
82
|
+
}
|
|
83
|
+
for (const dependency of story.dependsOn) {
|
|
84
|
+
if (dependency === story.id) {
|
|
85
|
+
violations.push(`story '${story.id}' depends on itself`)
|
|
86
|
+
} else if (!plan.stories.some((candidate) => candidate.id === dependency)) {
|
|
87
|
+
violations.push(`story '${story.id}' depends on unknown story '${dependency}'`)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const owned of story.owned) {
|
|
91
|
+
for (const shared of story.sharedReadOnly) {
|
|
92
|
+
if (overlapping(owned, shared)) {
|
|
93
|
+
violations.push(
|
|
94
|
+
`story '${story.id}' both owns '${owned}' and declares '${shared}' shared read-only`
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (let index = 0; index < plan.stories.length; index += 1) {
|
|
101
|
+
const left = plan.stories[index]
|
|
102
|
+
if (left === undefined) {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
for (const right of plan.stories.slice(index + 1)) {
|
|
106
|
+
for (const leftPath of left.owned) {
|
|
107
|
+
for (const rightPath of right.owned) {
|
|
108
|
+
if (overlapping(leftPath, rightPath)) {
|
|
109
|
+
violations.push(
|
|
110
|
+
`stories '${left.id}' and '${right.id}' both own '${normalizePath(leftPath)}' / '${normalizePath(rightPath)}'`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const cycle of cycles(plan)) {
|
|
118
|
+
violations.push(`dependency cycle: ${cycle.join(" -> ")}`)
|
|
119
|
+
}
|
|
120
|
+
return violations
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const cycles = (plan: StoryPlan): ReadonlyArray<ReadonlyArray<string>> => {
|
|
124
|
+
const found: Array<ReadonlyArray<string>> = []
|
|
125
|
+
const state = new Map<string, "visiting" | "done">()
|
|
126
|
+
const visit = (id: string, trail: ReadonlyArray<string>): void => {
|
|
127
|
+
const mark = state.get(id)
|
|
128
|
+
if (mark === "done") {
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
if (mark === "visiting") {
|
|
132
|
+
const start = trail.indexOf(id)
|
|
133
|
+
found.push([...trail.slice(start), id])
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
state.set(id, "visiting")
|
|
137
|
+
for (const dependency of plan.story(id)?.dependsOn ?? []) {
|
|
138
|
+
// A self-edge is already reported as "depends on itself".
|
|
139
|
+
if (dependency !== id && plan.story(dependency) !== undefined) {
|
|
140
|
+
visit(dependency, [...trail, id])
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
state.set(id, "done")
|
|
144
|
+
}
|
|
145
|
+
for (const story of plan.stories) {
|
|
146
|
+
visit(story.id, [])
|
|
147
|
+
}
|
|
148
|
+
return found
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const validateStoryPlan = Effect.fn("@llm4ts/flow/StoryPlan.validate")(function* (
|
|
152
|
+
plan: StoryPlan
|
|
153
|
+
): Effect.fn.Return<StoryPlan, StoryPlanInvalid> {
|
|
154
|
+
const violations = storyPlanViolations(plan)
|
|
155
|
+
return violations.length === 0 ? plan : yield* StoryPlanInvalid.make({ violations })
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Stories grouped by the earliest wave they can run in (all dependencies in
|
|
160
|
+
* earlier waves). Assumes a valid plan; a cycle leaves its members out.
|
|
161
|
+
*/
|
|
162
|
+
export const topologicalWaves = (plan: StoryPlan): ReadonlyArray<ReadonlyArray<string>> => {
|
|
163
|
+
const waves: Array<ReadonlyArray<string>> = []
|
|
164
|
+
const placed = new Set<string>()
|
|
165
|
+
let remaining = plan.stories.map((story) => story.id)
|
|
166
|
+
while (remaining.length > 0) {
|
|
167
|
+
const wave = remaining.filter((id) =>
|
|
168
|
+
(plan.story(id)?.dependsOn ?? []).every((dependency) => placed.has(dependency))
|
|
169
|
+
)
|
|
170
|
+
if (wave.length === 0) {
|
|
171
|
+
break
|
|
172
|
+
}
|
|
173
|
+
waves.push(wave)
|
|
174
|
+
for (const id of wave) {
|
|
175
|
+
placed.add(id)
|
|
176
|
+
}
|
|
177
|
+
remaining = remaining.filter((id) => !placed.has(id))
|
|
178
|
+
}
|
|
179
|
+
return waves
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface StoryProgress {
|
|
183
|
+
readonly done: ReadonlySet<string>
|
|
184
|
+
readonly failed: ReadonlySet<string>
|
|
185
|
+
readonly skipped: ReadonlySet<string>
|
|
186
|
+
readonly running: ReadonlySet<string>
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Stories that may start now: not yet touched, every dependency done. Plan order. */
|
|
190
|
+
export const readyStories = (plan: StoryPlan, progress: StoryProgress): ReadonlyArray<Story> =>
|
|
191
|
+
plan.stories.filter(
|
|
192
|
+
(story) =>
|
|
193
|
+
!progress.done.has(story.id) &&
|
|
194
|
+
!progress.failed.has(story.id) &&
|
|
195
|
+
!progress.skipped.has(story.id) &&
|
|
196
|
+
!progress.running.has(story.id) &&
|
|
197
|
+
story.dependsOn.every((dependency) => progress.done.has(dependency))
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
/** Every story that transitively depends on `id`, in plan order. */
|
|
201
|
+
export const dependentsOf = (plan: StoryPlan, id: string): ReadonlyArray<string> => {
|
|
202
|
+
const blocked = new Set<string>([id])
|
|
203
|
+
let grew = true
|
|
204
|
+
while (grew) {
|
|
205
|
+
grew = false
|
|
206
|
+
for (const story of plan.stories) {
|
|
207
|
+
if (!blocked.has(story.id) && story.dependsOn.some((dependency) => blocked.has(dependency))) {
|
|
208
|
+
blocked.add(story.id)
|
|
209
|
+
grew = true
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return plan.stories
|
|
214
|
+
.map((story) => story.id)
|
|
215
|
+
.filter((candidate) => candidate !== id && blocked.has(candidate))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Stable over the story entry's content — a changed entry means a fresh branch. */
|
|
219
|
+
export const storyHash = (story: Story): string =>
|
|
220
|
+
stableHash(
|
|
221
|
+
JSON.stringify({
|
|
222
|
+
id: story.id,
|
|
223
|
+
title: story.title,
|
|
224
|
+
description: story.description,
|
|
225
|
+
dependsOn: [...story.dependsOn],
|
|
226
|
+
owned: [...story.owned].map(normalizePath),
|
|
227
|
+
sharedReadOnly: [...story.sharedReadOnly].map(normalizePath),
|
|
228
|
+
provides: [...story.provides]
|
|
229
|
+
})
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
export const storyPlanFenceInfo = "json storyplan"
|
|
233
|
+
|
|
234
|
+
const fencePattern = /```json[ \t]+storyplan[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*```/
|
|
235
|
+
|
|
236
|
+
/** The raw JSON of the first ```json storyplan fenced block, if any. */
|
|
237
|
+
export const storyPlanBlock = (markdown: string): string | undefined =>
|
|
238
|
+
fencePattern.exec(markdown)?.[1]
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Decodes the story plan embedded in its markdown file. Decoding only —
|
|
242
|
+
* `validateStoryPlan` checks the graph invariants separately, so a parse
|
|
243
|
+
* failure and an invalid plan are told apart.
|
|
244
|
+
*/
|
|
245
|
+
export const parseStoryPlan = Effect.fn("@llm4ts/flow/StoryPlan.parse")(function* (
|
|
246
|
+
markdown: string
|
|
247
|
+
): Effect.fn.Return<StoryPlan, PlanParseError> {
|
|
248
|
+
const block = storyPlanBlock(markdown)
|
|
249
|
+
if (block === undefined) {
|
|
250
|
+
return yield* PlanParseError.make({
|
|
251
|
+
message: "no ```json storyplan fenced block in the story plan markdown"
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
return yield* Schema.decodeUnknownEffect(Schema.fromJsonString(StoryPlan))(block).pipe(
|
|
255
|
+
Effect.mapError((error) =>
|
|
256
|
+
PlanParseError.make({ message: `invalid story plan block: ${String(error)}` })
|
|
257
|
+
)
|
|
258
|
+
)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
const list = (items: ReadonlyArray<string>): string =>
|
|
262
|
+
items.length === 0 ? "none" : items.join(", ")
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The operator-facing markdown: a readable summary per story plus the
|
|
266
|
+
* fenced block that is the source of truth. Editing the block and rerunning
|
|
267
|
+
* is the approval and the re-plan path.
|
|
268
|
+
*/
|
|
269
|
+
export const renderStoryPlan = Effect.fn("@llm4ts/flow/StoryPlan.render")(function* (
|
|
270
|
+
plan: StoryPlan
|
|
271
|
+
): Effect.fn.Return<string, PlanParseError> {
|
|
272
|
+
const encoded = yield* Schema.encodeEffect(StoryPlan)(plan).pipe(
|
|
273
|
+
Effect.mapError((error) =>
|
|
274
|
+
PlanParseError.make({ message: `story plan not encodable: ${String(error)}` })
|
|
275
|
+
)
|
|
276
|
+
)
|
|
277
|
+
const waves = topologicalWaves(plan)
|
|
278
|
+
const lines: Array<string> = [
|
|
279
|
+
`# Epic: ${plan.epicId}`,
|
|
280
|
+
"",
|
|
281
|
+
plan.epic.trim(),
|
|
282
|
+
"",
|
|
283
|
+
"## Waves",
|
|
284
|
+
"",
|
|
285
|
+
...waves.map((wave, index) => `${index + 1}. ${wave.join(", ")}`),
|
|
286
|
+
"",
|
|
287
|
+
"## Stories",
|
|
288
|
+
""
|
|
289
|
+
]
|
|
290
|
+
for (const story of plan.stories) {
|
|
291
|
+
lines.push(
|
|
292
|
+
`### ${story.id} — ${story.title}`,
|
|
293
|
+
"",
|
|
294
|
+
story.description.trim(),
|
|
295
|
+
"",
|
|
296
|
+
`- depends on: ${list(story.dependsOn)}`,
|
|
297
|
+
`- owned: ${list(story.owned)}`,
|
|
298
|
+
`- shared read-only: ${list(story.sharedReadOnly)}`,
|
|
299
|
+
`- provides: ${list(story.provides)}`,
|
|
300
|
+
""
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
lines.push(
|
|
304
|
+
"## Plan block",
|
|
305
|
+
"",
|
|
306
|
+
"Edit this block to change the plan; the prose above is regenerated from it.",
|
|
307
|
+
"",
|
|
308
|
+
"```" + storyPlanFenceInfo,
|
|
309
|
+
JSON.stringify(encoded, null, 2),
|
|
310
|
+
"```",
|
|
311
|
+
""
|
|
312
|
+
)
|
|
313
|
+
return lines.join("\n")
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
export interface StoryPlanStoreShape {
|
|
317
|
+
readonly save: (
|
|
318
|
+
path: string,
|
|
319
|
+
plan: StoryPlan
|
|
320
|
+
) => Effect.Effect<void, PersistenceError | PlanParseError>
|
|
321
|
+
readonly load: (
|
|
322
|
+
path: string
|
|
323
|
+
) => Effect.Effect<StoryPlan | undefined, PersistenceError | PlanParseError>
|
|
324
|
+
/** An existing file wins over `create`: the operator's edits are the plan. */
|
|
325
|
+
readonly recoverOrCreate: <E, R>(
|
|
326
|
+
path: string,
|
|
327
|
+
create: Effect.Effect<StoryPlan, E, R>
|
|
328
|
+
) => Effect.Effect<StoryPlan, E | PersistenceError | PlanParseError, R>
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export const makeStoryPlanStore = (files: PlainFileStoreShape): StoryPlanStoreShape => {
|
|
332
|
+
const save = (
|
|
333
|
+
path: string,
|
|
334
|
+
plan: StoryPlan
|
|
335
|
+
): Effect.Effect<void, PersistenceError | PlanParseError> =>
|
|
336
|
+
Effect.flatMap(renderStoryPlan(plan), (markdown) => files.writeAtomic(path, markdown))
|
|
337
|
+
const load = (
|
|
338
|
+
path: string
|
|
339
|
+
): Effect.Effect<StoryPlan | undefined, PersistenceError | PlanParseError> =>
|
|
340
|
+
Effect.flatMap(files.read(path), (contents) =>
|
|
341
|
+
contents === undefined ? Effect.succeed(undefined) : parseStoryPlan(contents)
|
|
342
|
+
)
|
|
343
|
+
return {
|
|
344
|
+
save,
|
|
345
|
+
load,
|
|
346
|
+
recoverOrCreate: (path, create) =>
|
|
347
|
+
Effect.flatMap(load(path), (stored) =>
|
|
348
|
+
stored === undefined
|
|
349
|
+
? Effect.tap(create, (plan) => save(path, plan))
|
|
350
|
+
: Effect.succeed(stored)
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
}
|
package/src/TransientRetry.ts
CHANGED
|
@@ -119,6 +119,14 @@ export interface TransientRetryOptions {
|
|
|
119
119
|
readonly baseDelay?: Duration.Input
|
|
120
120
|
readonly flakyRetries?: number
|
|
121
121
|
readonly flakyDelay?: Duration.Input
|
|
122
|
+
/**
|
|
123
|
+
* Re-asks a structured call whose response could not be parsed as the
|
|
124
|
+
* requested JSON, with the parse failure quoted back to the model. The
|
|
125
|
+
* model's output is not deterministic, so a malformed or off-schema reply
|
|
126
|
+
* is usually a one-off; but a schema the model cannot satisfy fails every
|
|
127
|
+
* time, so this budget is small (default 2) and independent of the others.
|
|
128
|
+
*/
|
|
129
|
+
readonly parseRetries?: number
|
|
122
130
|
}
|
|
123
131
|
|
|
124
132
|
interface ResolvedTransientRetryOptions {
|
|
@@ -126,15 +134,43 @@ interface ResolvedTransientRetryOptions {
|
|
|
126
134
|
readonly baseDelay: Duration.Duration
|
|
127
135
|
readonly flakyRetries: number
|
|
128
136
|
readonly flakyDelay: Duration.Duration
|
|
137
|
+
readonly parseRetries: number
|
|
129
138
|
}
|
|
130
139
|
|
|
131
140
|
const resolveOptions = (options: TransientRetryOptions): ResolvedTransientRetryOptions => ({
|
|
132
141
|
maxRetries: options.maxRetries ?? 3,
|
|
133
142
|
baseDelay: Duration.fromInputUnsafe(options.baseDelay ?? "1 second"),
|
|
134
143
|
flakyRetries: options.flakyRetries ?? 6,
|
|
135
|
-
flakyDelay: Duration.fromInputUnsafe(options.flakyDelay ?? "1 second")
|
|
144
|
+
flakyDelay: Duration.fromInputUnsafe(options.flakyDelay ?? "1 second"),
|
|
145
|
+
parseRetries: options.parseRetries ?? 2
|
|
136
146
|
})
|
|
137
147
|
|
|
148
|
+
/** A structured call whose text came back but did not decode as the requested JSON. */
|
|
149
|
+
export const isStructuredParseFailure = (error: LlmError): boolean => error._tag === "ParseError"
|
|
150
|
+
|
|
151
|
+
const repairReasonLimit = 400
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The prompt for a structured re-ask: the original, plus what went wrong
|
|
155
|
+
* with the previous reply. Quoting the failure is what turns a blind retry
|
|
156
|
+
* into a repair — the common causes (prose around the JSON, a missing
|
|
157
|
+
* required field, a string where a number was asked for) are all things the
|
|
158
|
+
* model fixes on sight.
|
|
159
|
+
*/
|
|
160
|
+
export const repairPrompt = (prompt: string, error: LlmError): string => {
|
|
161
|
+
const reason = error.message.trim()
|
|
162
|
+
const shown =
|
|
163
|
+
reason.length <= repairReasonLimit ? reason : `${reason.slice(0, repairReasonLimit - 3)}...`
|
|
164
|
+
return [
|
|
165
|
+
prompt,
|
|
166
|
+
"",
|
|
167
|
+
"Your previous reply could not be parsed as the requested JSON:",
|
|
168
|
+
shown,
|
|
169
|
+
"Reply again with ONLY the JSON object — no prose before or after it, no markdown",
|
|
170
|
+
"fences, every required field present, and values of the requested types."
|
|
171
|
+
].join("\n")
|
|
172
|
+
}
|
|
173
|
+
|
|
138
174
|
const backoff = (baseDelay: Duration.Duration, attempt: number): Duration.Duration =>
|
|
139
175
|
Duration.times(baseDelay, 2 ** attempt)
|
|
140
176
|
|
|
@@ -256,6 +292,39 @@ const retryStream = <R>(
|
|
|
256
292
|
return loop(0, 0)
|
|
257
293
|
}
|
|
258
294
|
|
|
295
|
+
/**
|
|
296
|
+
* A structured call under all three budgets: `run(prompt)` is retried for
|
|
297
|
+
* transient and flaky failures as any effect is, and a parse failure re-asks
|
|
298
|
+
* with `repairPrompt` up to `parseRetries` times before it is surfaced.
|
|
299
|
+
*/
|
|
300
|
+
const retryStructured = <A, R>(
|
|
301
|
+
prompt: string,
|
|
302
|
+
run: (prompt: string) => Effect.Effect<A, LlmError, R>,
|
|
303
|
+
options: ResolvedTransientRetryOptions,
|
|
304
|
+
events: FlowEvents["Service"]
|
|
305
|
+
): Effect.Effect<A, LlmError, R> => {
|
|
306
|
+
const loop = (current: string, parseAttempt: number): Effect.Effect<A, LlmError, R> =>
|
|
307
|
+
retryEffect(run(current), "structured", options, events).pipe(
|
|
308
|
+
Effect.catchIf(
|
|
309
|
+
(error) => isStructuredParseFailure(error) && parseAttempt < options.parseRetries,
|
|
310
|
+
(error) =>
|
|
311
|
+
waitForRetry(
|
|
312
|
+
events,
|
|
313
|
+
"structured output (repair retry)",
|
|
314
|
+
parseAttempt,
|
|
315
|
+
options.parseRetries,
|
|
316
|
+
error,
|
|
317
|
+
options.flakyDelay
|
|
318
|
+
).pipe(
|
|
319
|
+
Effect.andThen(
|
|
320
|
+
Effect.suspend(() => loop(repairPrompt(prompt, error), parseAttempt + 1))
|
|
321
|
+
)
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
)
|
|
325
|
+
return loop(prompt, 0)
|
|
326
|
+
}
|
|
327
|
+
|
|
259
328
|
export const makeTransientRetry = Effect.fn("@llm4ts/flow/TransientRetry.make")(function* (
|
|
260
329
|
underlying: LlmServiceShape,
|
|
261
330
|
retryOptions: TransientRetryOptions = {}
|
|
@@ -271,16 +340,16 @@ export const makeTransientRetry = Effect.fn("@llm4ts/flow/TransientRetry.make")(
|
|
|
271
340
|
executeWithTools: (prompt, tools) =>
|
|
272
341
|
retryEffect(underlying.executeWithTools(prompt, tools), "tools", options, events),
|
|
273
342
|
executeStructured: (prompt, schema, jsonSchema) =>
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
343
|
+
retryStructured(
|
|
344
|
+
prompt,
|
|
345
|
+
(current) => underlying.executeStructured(current, schema, jsonSchema),
|
|
277
346
|
options,
|
|
278
347
|
events
|
|
279
348
|
),
|
|
280
349
|
executeStructuredWithUsage: (prompt, schema, jsonSchema) =>
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
350
|
+
retryStructured(
|
|
351
|
+
prompt,
|
|
352
|
+
(current) => underlying.executeStructuredWithUsage(current, schema, jsonSchema),
|
|
284
353
|
options,
|
|
285
354
|
events
|
|
286
355
|
),
|