GeezerVison is showing github.com — fix(server): retry unstarted runs after recovery (#30560) · incognitojam/styal@569cf21
File treeExpand file treeCollapse file tree Open diff view settings server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2606,7 +2606,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 2606 | 2606 | }, | 2607 | 2607 | })), | 2608 | 2608 | { | 2609 | | - id: `effect:${commandId}:provider-turn.start:${queuedRun.id}`, | | 2609 | + // A restart can return this run to the queue after its previous start effect was cancelled. | | 2610 | + id: `effect:${commandId}:provider-turn.start:${queuedRun.id}:${yield* idAllocator.allocate.event({ threadId, commandId })}`, | 2610 | 2611 | commandId, | 2611 | 2612 | threadId, | 2612 | 2613 | request: { type: "provider-turn.start", runId: queuedRun.id }, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts +51-7Lines changed: 51 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -532,7 +532,19 @@ it.effect("recovery closes a secret card's form but leaves a link card open", () | 532 | 532 | }).pipe(Effect.provide(layer)); | 533 | 533 | }); | 534 | 534 |
| 535 | | -it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart", (status) => { | | 535 | +it.effect.each([ | | 536 | + "queued", | | 537 | + "preparing", | | 538 | + "starting", | | 539 | + "starting-other-request", | | 540 | + "starting-own-request", | | 541 | + "starting-scheduled", | | 542 | +] as const)("recovers $0 work after restart", (scenario) => { | | 543 | + const status = scenario.startsWith("starting") ? "starting" : scenario; | | 544 | + const terminalized = | | 545 | + status === "preparing" || | | 546 | + scenario === "starting-own-request" || | | 547 | + scenario === "starting-scheduled"; | 536 | 548 | const threadId = ThreadId.make("thread_queued_restart"); | 537 | 549 | const runId = RunId.make("run_queued_restart"); | 538 | 550 | const attemptId = RunAttemptId.make("attempt_queued_restart"); | @@ -541,14 +553,28 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart | 541 | 553 | null; | 542 | 554 | const projection = { | 543 | 555 | thread: { id: threadId }, | 544 | | - runtimeRequests: [], | | 556 | + runtimeRequests: scenario.endsWith("request") | | 557 | + ? [ | | 558 | + { | | 559 | + id: RuntimeRequestId.make("pending-recovery-request"), | | 560 | + nodeId: | | 561 | + scenario === "starting-own-request" ? rootNodeId : NodeId.make("other-run-request"), | | 562 | + kind: "user_input", | | 563 | + status: "pending", | | 564 | + responseCapability: { type: "message" }, | | 565 | + }, | | 566 | + ] | | 567 | + : [], | 545 | 568 | providerSessions: [], | 546 | 569 | providerThreads: [], | 547 | 570 | providerTurns: [], | 548 | 571 | runs: [ | 549 | 572 | { | 550 | 573 | id: runId, | 551 | 574 | status, | | 575 | + startedAt: null, | | 576 | + activeAttemptId: attemptId, | | 577 | + userMessageId: MessageId.make("message_queued_restart"), | 552 | 578 | queuePosition: 1, | 553 | 579 | providerInstanceId: ProviderInstanceId.make("codex"), | 554 | 580 | ...(status === "preparing" | @@ -571,14 +597,32 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart | 571 | 597 | }, | 572 | 598 | ], | 573 | 599 | nodes: [ | | 600 | + ...(scenario === "starting-other-request" | | 601 | + ? [ | | 602 | + { | | 603 | + id: NodeId.make("other-run-request"), | | 604 | + runId: RunId.make("earlier-run"), | | 605 | + status: "waiting", | | 606 | + }, | | 607 | + ] | | 608 | + : []), | 574 | 609 | { | 575 | 610 | id: rootNodeId, | 576 | 611 | runId, | 577 | 612 | status: "pending", | 578 | 613 | }, | 579 | 614 | ], | 580 | 615 | subagents: [], | 581 | | - messages: [], | | 616 | + messages: | | 617 | + status === "starting" | | 618 | + ? [ | | 619 | + { | | 620 | + id: MessageId.make("message_queued_restart"), | | 621 | + createdBy: "user", | | 622 | + ...(scenario === "starting-scheduled" ? { scheduledTaskId: "scheduled-task" } : {}), | | 623 | + }, | | 624 | + ] | | 625 | + : [], | 582 | 626 | turnItems: [], | 583 | 627 | } as unknown as OrchestrationV2ThreadProjection; | 584 | 628 | const layer = ProviderRuntimeRecovery.layer.pipe( | @@ -610,7 +654,7 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart | 610 | 654 | return Effect.gen(function* () { | 611 | 655 | const summary = | 612 | 656 | yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup"); | 613 | | - assert.equal(summary.terminalizedRuns, status === "preparing" ? 1 : 0); | | 657 | + assert.equal(summary.terminalizedRuns, terminalized ? 1 : 0); | 614 | 658 | const command = committedInput; | 615 | 659 | assert.isNotNull(command); | 616 | 660 | if (command === null) return; | @@ -619,17 +663,17 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart | 619 | 663 | const nodeEvent = command.events.find((event) => event.type === "node.updated"); | 620 | 664 | assert.equal( | 621 | 665 | runEvent?.type === "run.updated" ? runEvent.payload.status : null, | 622 | | - status === "preparing" ? "cancelled" : "queued", | | 666 | + terminalized ? "cancelled" : "queued", | 623 | 667 | ); | 624 | 668 | assert.isEmpty(command.effects); | 625 | | - if (status === "preparing") { | | 669 | + if (terminalized) { | 626 | 670 | assert.equal( | 627 | 671 | attemptEvent?.type === "run-attempt.updated" ? attemptEvent.payload.status : null, | 628 | 672 | "cancelled", | 629 | 673 | ); | 630 | 674 | assert.equal( | 631 | 675 | nodeEvent?.type === "node.updated" ? nodeEvent.payload.status : null, | 632 | | - "cancelled", | | 676 | + scenario === "starting-own-request" ? null : "cancelled", | 633 | 677 | ); | 634 | 678 | } else { | 635 | 679 | assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.queuePosition : null, 1); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts +81-6Lines changed: 81 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ import { | 32 | 32 | restartContinuationRun, | 33 | 33 | } from "./RestartContinuation.ts"; | 34 | 34 | import { isBufferedProviderNotification } from "./NotificationMailbox.ts"; | | 35 | +import { queuedRunsInDeliveryOrder } from "./QueuedRunOrder.ts"; | 35 | 36 | import { | 36 | 37 | endOrphanedNativeSubagent, | 37 | 38 | endRunlessRootTurns, | @@ -267,7 +268,7 @@ export const make = Effect.gen(function* () { | 267 | 268 | stoppedThisPass: ReadonlyMap<string, RecoverySession> = new Map(), | 268 | 269 | ) { | 269 | 270 | const now = yield* DateTime.now; | 270 | | - const runs = [] as Array<OrchestrationV2ThreadProjection["runs"][number]>; | | 271 | + let runs = [] as Array<OrchestrationV2ThreadProjection["runs"][number]>; | 271 | 272 | for (const run of nonterminalRuns(projection)) { | 272 | 273 | if (run.status === "waiting") { | 273 | 274 | const checkpointEffects = yield* outbox | @@ -382,13 +383,25 @@ export const make = Effect.gen(function* () { | 382 | 383 | cancelledBackgroundNativeIds.add(item.nativeItemRef.nativeId); | 383 | 384 | } | 384 | 385 | }; | | 386 | + const unstartedRuns = runs.filter( | | 387 | + (run) => | | 388 | + run.status === "starting" && | | 389 | + neverStarted(run) && | | 390 | + !projection.providerTurns.some((turn) => turn.runAttemptId === run.activeAttemptId) && | | 391 | + !projection.runtimeRequests.some( | | 392 | + (request) => | | 393 | + request.status === "pending" && | | 394 | + projection.nodes.some((node) => node.id === request.nodeId && node.runId === run.id), | | 395 | + ), | | 396 | + ); | 385 | 397 | const queuedRuns = projection.runs.filter((run) => run.status === "queued"); | | 398 | + const pendingRuns = [...unstartedRuns, ...queuedRuns]; | 386 | 399 | const queuedMessages = | 387 | | - queuedRuns.length === 0 | | 400 | + pendingRuns.length === 0 | 388 | 401 | ? [] | 389 | 402 | : (yield* projections | 390 | 403 | .getThreadRecords(projection.thread.id, ["messages"], { | 391 | | - messageIds: queuedRuns.map((run) => run.userMessageId), | | 404 | + messageIds: pendingRuns.map((run) => run.userMessageId), | 392 | 405 | }) | 393 | 406 | .pipe( | 394 | 407 | Effect.mapError( | @@ -403,20 +416,82 @@ export const make = Effect.gen(function* () { | 403 | 416 | const bufferedMessageIds = new Set( | 404 | 417 | queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id), | 405 | 418 | ); | 406 | | - for (const run of queuedRuns) { | | 419 | + const retryableMessageIds = new Set( | | 420 | + queuedMessages | | 421 | + .filter( | | 422 | + (message) => | | 423 | + message.createdBy === "user" && | | 424 | + message.scheduledTaskId === undefined && | | 425 | + message.notification === undefined && | | 426 | + message.delegatedCompletion === undefined, | | 427 | + ) | | 428 | + .map((message) => message.id), | | 429 | + ); | | 430 | + // A saved user message the provider never accepted stays available through the held queue. | | 431 | + const retryableRuns = unstartedRuns.filter((run) => | | 432 | + retryableMessageIds.has(run.userMessageId), | | 433 | + ); | | 434 | + const retryableRunIds = new Set(retryableRuns.map((run) => run.id)); | | 435 | + const retryableItems = | | 436 | + retryableRuns.length === 0 | | 437 | + ? [] | | 438 | + : (yield* projections | | 439 | + .getThreadRecords(projection.thread.id, ["turnItems"], { | | 440 | + turnItemTypes: ["user_message"], | | 441 | + turnItemRunIds: retryableRuns.map((run) => run.id), | | 442 | + }) | | 443 | + .pipe( | | 444 | + Effect.mapError( | | 445 | + (cause) => | | 446 | + new ProviderRuntimeRecoveryError({ | | 447 | + operation: "read-projections", | | 448 | + threadId: projection.thread.id, | | 449 | + cause, | | 450 | + }), | | 451 | + ), | | 452 | + )).turnItems; | | 453 | + for (const item of retryableItems) { | | 454 | + if (item.type !== "user_message" || !retryableMessageIds.has(item.messageId)) continue; | | 455 | + events.push({ | | 456 | + id: yield* allocateEventId(), | | 457 | + type: "turn-item.updated", | | 458 | + threadId: projection.thread.id, | | 459 | + ...(item.runId === null ? {} : { runId: item.runId }), | | 460 | + occurredAt: now, | | 461 | + payload: { | | 462 | + ...item, | | 463 | + inputIntent: "queued_turn", | | 464 | + status: "pending", | | 465 | + startedAt: null, | | 466 | + completedAt: null, | | 467 | + updatedAt: now, | | 468 | + }, | | 469 | + }); | | 470 | + } | | 471 | + runs = runs.filter((run) => !retryableRunIds.has(run.id)); | | 472 | + const heldRuns = [ | | 473 | + ...retryableRuns, | | 474 | + ...queuedRunsInDeliveryOrder({ runs: queuedRuns, messages: queuedMessages }), | | 475 | + ]; | | 476 | + for (const [index, run] of heldRuns.entries()) { | 407 | 477 | if (bufferedMessageIds.has(run.userMessageId)) { | 408 | 478 | runs.push(run); | 409 | 479 | continue; | 410 | 480 | } | 411 | | - if (run.queueHeld === true) continue; | | 481 | + if (run.queueHeld === true && retryableRuns.length === 0) continue; | 412 | 482 | events.push({ | 413 | 483 | id: yield* allocateEventId(), | 414 | 484 | type: "run.updated", | 415 | 485 | threadId: projection.thread.id, | 416 | 486 | runId: run.id, | 417 | 487 | providerInstanceId: run.providerInstanceId, | 418 | 488 | occurredAt: now, | 419 | | - payload: { ...run, queueHeld: true }, | | 489 | + payload: { | | 490 | + ...run, | | 491 | + status: "queued", | | 492 | + queueHeld: true, | | 493 | + ...(retryableRuns.length === 0 ? {} : { queuePosition: index + 1 }), | | 494 | + }, | 420 | 495 | }); | 421 | 496 | } | 422 | 497 | for (const question of questions) { | |
|