GeezerVison is showing github.com — fix(server): linked workflow steps recover replies from older pages (… · patroza/t3code@d2a9b77
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/workflows
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/workflows/WorkflowAgentLinks.test.ts +109Lines changed: 109 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,42 @@ const input = { | 59 | 59 | const run = (status: string) => ({ recentRuns: [{ runId: "turn-1", status }], items: [] }); | 60 | 60 | const waited = { threadId: "thread-1", runId: "turn-1", status: "idle" }; | 61 | 61 |
| | 62 | +const assistant = (position: number, runId: string, text: string) => ({ | | 63 | + type: "assistant_message", | | 64 | + itemId: `item-${position}`, | | 65 | + position, | | 66 | + runId, | | 67 | + text, | | 68 | + textTruncated: false, | | 69 | +}); | | 70 | +const page = ( | | 71 | + items: ReadonlyArray<ReturnType<typeof assistant>>, | | 72 | + earlierPosition?: number | null, | | 73 | + hasMore = false, | | 74 | +) => ({ | | 75 | + recentRuns: [], | | 76 | + items, | | 77 | + earlierPosition, | | 78 | + hasMore, | | 79 | + nextPosition: items.at(-1)?.position ?? null, | | 80 | +}); | | 81 | +const readReply = (...pages: ReadonlyArray<unknown>) => { | | 82 | + const there = peer({ | | 83 | + t3_thread_wait: [{ ...waited, timedOut: false }], | | 84 | + t3_thread_read: [run("completed"), ...pages], | | 85 | + }); | | 86 | + return Effect.gen(function* () { | | 87 | + const result = yield* Effect.provide( | | 88 | + Effect.flatMap(WorkflowAgentLinks.make, (links) => | | 89 | + links.result({ threadId: "thread-1", turnId: "turn-1" }, "env-b"), | | 90 | + ), | | 91 | + there.layer, | | 92 | + ); | | 93 | + return { result, reads: there.calls.slice(2).map((call) => call.arguments) }; | | 94 | + }); | | 95 | +}; | | 96 | +const messageRead = { threadId: "thread-1", view: "messages", limit: 100 }; | | 97 | + | 62 | 98 | it.effect("starts a thread there by the project's folder, with the step's key as the request", () => | 63 | 99 | Effect.gen(function* () { | 64 | 100 | const there = peer({ | @@ -224,6 +260,79 @@ it.effect("waits in pieces, rides out a dropped route, and returns the output an | 224 | 260 | }), | 225 | 261 | ); | 226 | 262 |
| | 263 | +it.effect("returns the whole last reply from an older page of a linked thread", () => | | 264 | + Effect.gen(function* () { | | 265 | + const later = { | | 266 | + ...assistant(4, "turn-1", "Latest "), | | 267 | + textTruncated: true, | | 268 | + nextTextOffset: 7, | | 269 | + }; | | 270 | + const { result, reads } = yield* readReply( | | 271 | + page([assistant(9, "turn-3", "newest turn")], 9), | | 272 | + page([assistant(6, "turn-2", "another turn")], 6, true), | | 273 | + page([assistant(3, "turn-1", "earlier draft"), later], 3, true), | | 274 | + page([{ ...later, text: "complete reply", textTruncated: false }]), | | 275 | + ); | | 276 | + assert.deepStrictEqual(result, { | | 277 | + ending: "completed", | | 278 | + detail: "", | | 279 | + reply: "Latest complete reply", | | 280 | + }); | | 281 | + assert.deepStrictEqual(reads, [ | | 282 | + messageRead, | | 283 | + { ...messageRead, beforePosition: 9 }, | | 284 | + { ...messageRead, beforePosition: 6 }, | | 285 | + { threadId: "thread-1", itemId: "item-4", textOffset: 7, maxCharsPerItem: 50_000 }, | | 286 | + ]); | | 287 | + }), | | 288 | +); | | 289 | + | | 290 | +it.effect("keeps the latest matching reply when paging a legacy peer forward", () => | | 291 | + Effect.gen(function* () { | | 292 | + for (const earlierPosition of [undefined, null]) { | | 293 | + const { result, reads } = yield* readReply( | | 294 | + page([assistant(2, "turn-1", "draft")], earlierPosition, true), | | 295 | + page([assistant(4, "turn-1", "final")], earlierPosition, true), | | 296 | + page([assistant(6, "turn-2", "other turn")], earlierPosition), | | 297 | + ); | | 298 | + assert.strictEqual(result.reply, "final"); | | 299 | + assert.deepStrictEqual(reads, [ | | 300 | + messageRead, | | 301 | + { ...messageRead, afterPosition: 2 }, | | 302 | + { ...messageRead, afterPosition: 4 }, | | 303 | + ]); | | 304 | + } | | 305 | + }), | | 306 | +); | | 307 | + | | 308 | +it.effect("keeps the reply when a legacy peer's forward cursor stops advancing", () => | | 309 | + Effect.gen(function* () { | | 310 | + for (const position of [2, 1]) { | | 311 | + const { result, reads } = yield* readReply( | | 312 | + page([assistant(2, "turn-1", "Done.")], undefined, true), | | 313 | + page([assistant(position, "turn-2", "other turn")], undefined, true), | | 314 | + ); | | 315 | + assert.strictEqual(result.reply, "Done."); | | 316 | + assert.deepStrictEqual(reads, [messageRead, { ...messageRead, afterPosition: 2 }]); | | 317 | + } | | 318 | + }), | | 319 | +); | | 320 | + | | 321 | +it.effect( | | 322 | + "returns an empty reply when older pages are exhausted or their cursor stops advancing", | | 323 | + () => | | 324 | + Effect.gen(function* () { | | 325 | + for (const earlierPosition of [null, 3, 4]) { | | 326 | + const { result, reads } = yield* readReply( | | 327 | + page([assistant(3, "turn-2", "other turn")], 3), | | 328 | + page([assistant(1, "turn-0", "another turn")], earlierPosition, true), | | 329 | + ); | | 330 | + assert.deepStrictEqual(result, { ending: "completed", detail: "", reply: "" }); | | 331 | + assert.deepStrictEqual(reads, [messageRead, { ...messageRead, beforePosition: 3 }]); | | 332 | + } | | 333 | + }), | | 334 | +); | | 335 | + | 227 | 336 | it.effect("a turn someone stopped there is stopped, and a refused link fails at once", () => | 228 | 337 | Effect.gen(function* () { | 229 | 338 | const stopped = peer({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/workflows/WorkflowAgentLinks.ts +17-4Lines changed: 17 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -131,20 +131,33 @@ export const make = Effect.gen(function* () { | 131 | 131 | environment: string, | 132 | 132 | ) { | 133 | 133 | let last: OrchestratorMcpThreadReadResult["items"][number] | undefined; | 134 | | - let afterPosition: number | undefined; | | 134 | + let cursor: { beforePosition?: number; afterPosition?: number } = {}; | 135 | 135 | while (true) { | 136 | 136 | const read = yield* call(environment, "t3_thread_read", OrchestratorMcpThreadReadResult, { | 137 | 137 | threadId: turn.threadId, | 138 | 138 | view: "messages", | 139 | 139 | limit: 100, | 140 | | - ...(afterPosition === undefined ? {} : { afterPosition }), | | 140 | + ...cursor, | 141 | 141 | }); | 142 | 142 | last = | 143 | 143 | read.items.findLast( | 144 | 144 | (item) => item.type === "assistant_message" && item.runId === turn.turnId, | 145 | 145 | ) ?? last; | 146 | | - if (!read.hasMore || read.nextPosition === null) break; | 147 | | - afterPosition = read.nextPosition; | | 146 | + const { earlierPosition, nextPosition, hasMore } = read; | | 147 | + // Backward pages are chronological too, and hasMore points toward newer items. | | 148 | + if (cursor.afterPosition === undefined && earlierPosition != null) { | | 149 | + if (last !== undefined || earlierPosition >= (cursor.beforePosition ?? Infinity)) break; | | 150 | + cursor = { beforePosition: earlierPosition }; | | 151 | + continue; | | 152 | + } | | 153 | + if ( | | 154 | + cursor.beforePosition !== undefined || | | 155 | + !hasMore || | | 156 | + nextPosition === null || | | 157 | + nextPosition <= (cursor.afterPosition ?? -1) | | 158 | + ) | | 159 | + break; | | 160 | + cursor = { afterPosition: nextPosition }; | 148 | 161 | } | 149 | 162 | if (last === undefined) return ""; | 150 | 163 | let text = last.text ?? ""; | |
|