GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): retry unstarted runs after recovery (#30560) · incognitojam/styal@569cf21

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps
    • server/src/orchestration-v2
      • Orchestrator.ts
      • ProviderRuntimeRecoveryService.test.ts
      • ProviderRuntimeRecoveryService.ts
      • runtimeLayer.test.ts
    • web/src/components/chat
      • QueuedRunsControl.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
26062606
},
26072607
})),
26082608
{
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 })}`,
26102611
commandId,
26112612
threadId,
26122613
request: { type: "provider-turn.start", runId: queuedRun.id },
Collapse file

‎apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts‎

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 numberDiff line numberDiff line change
@@ -532,7 +532,19 @@ it.effect("recovery closes a secret card's form but leaves a link card open", ()
532532
}).pipe(Effect.provide(layer));
533533
});
534534

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";
536548
const threadId = ThreadId.make("thread_queued_restart");
537549
const runId = RunId.make("run_queued_restart");
538550
const attemptId = RunAttemptId.make("attempt_queued_restart");
@@ -541,14 +553,28 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart
541553
null;
542554
const projection = {
543555
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+
: [],
545568
providerSessions: [],
546569
providerThreads: [],
547570
providerTurns: [],
548571
runs: [
549572
{
550573
id: runId,
551574
status,
575+
startedAt: null,
576+
activeAttemptId: attemptId,
577+
userMessageId: MessageId.make("message_queued_restart"),
552578
queuePosition: 1,
553579
providerInstanceId: ProviderInstanceId.make("codex"),
554580
...(status === "preparing"
@@ -571,14 +597,32 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart
571597
},
572598
],
573599
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+
: []),
574609
{
575610
id: rootNodeId,
576611
runId,
577612
status: "pending",
578613
},
579614
],
580615
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+
: [],
582626
turnItems: [],
583627
} as unknown as OrchestrationV2ThreadProjection;
584628
const layer = ProviderRuntimeRecovery.layer.pipe(
@@ -610,7 +654,7 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart
610654
return Effect.gen(function* () {
611655
const summary =
612656
yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
613-
assert.equal(summary.terminalizedRuns, status === "preparing" ? 1 : 0);
657+
assert.equal(summary.terminalizedRuns, terminalized ? 1 : 0);
614658
const command = committedInput;
615659
assert.isNotNull(command);
616660
if (command === null) return;
@@ -619,17 +663,17 @@ it.effect.each(["queued", "preparing"] as const)("recovers $0 work after restart
619663
const nodeEvent = command.events.find((event) => event.type === "node.updated");
620664
assert.equal(
621665
runEvent?.type === "run.updated" ? runEvent.payload.status : null,
622-
status === "preparing" ? "cancelled" : "queued",
666+
terminalized ? "cancelled" : "queued",
623667
);
624668
assert.isEmpty(command.effects);
625-
if (status === "preparing") {
669+
if (terminalized) {
626670
assert.equal(
627671
attemptEvent?.type === "run-attempt.updated" ? attemptEvent.payload.status : null,
628672
"cancelled",
629673
);
630674
assert.equal(
631675
nodeEvent?.type === "node.updated" ? nodeEvent.payload.status : null,
632-
"cancelled",
676+
scenario === "starting-own-request" ? null : "cancelled",
633677
);
634678
} else {
635679
assert.equal(runEvent?.type === "run.updated" ? runEvent.payload.queuePosition : null, 1);
Collapse file

‎apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts
+81-6Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
restartContinuationRun,
3333
} from "./RestartContinuation.ts";
3434
import { isBufferedProviderNotification } from "./NotificationMailbox.ts";
35+
import { queuedRunsInDeliveryOrder } from "./QueuedRunOrder.ts";
3536
import {
3637
endOrphanedNativeSubagent,
3738
endRunlessRootTurns,
@@ -267,7 +268,7 @@ export const make = Effect.gen(function* () {
267268
stoppedThisPass: ReadonlyMap<string, RecoverySession> = new Map(),
268269
) {
269270
const now = yield* DateTime.now;
270-
const runs = [] as Array<OrchestrationV2ThreadProjection["runs"][number]>;
271+
let runs = [] as Array<OrchestrationV2ThreadProjection["runs"][number]>;
271272
for (const run of nonterminalRuns(projection)) {
272273
if (run.status === "waiting") {
273274
const checkpointEffects = yield* outbox
@@ -382,13 +383,25 @@ export const make = Effect.gen(function* () {
382383
cancelledBackgroundNativeIds.add(item.nativeItemRef.nativeId);
383384
}
384385
};
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+
);
385397
const queuedRuns = projection.runs.filter((run) => run.status === "queued");
398+
const pendingRuns = [...unstartedRuns, ...queuedRuns];
386399
const queuedMessages =
387-
queuedRuns.length === 0
400+
pendingRuns.length === 0
388401
? []
389402
: (yield* projections
390403
.getThreadRecords(projection.thread.id, ["messages"], {
391-
messageIds: queuedRuns.map((run) => run.userMessageId),
404+
messageIds: pendingRuns.map((run) => run.userMessageId),
392405
})
393406
.pipe(
394407
Effect.mapError(
@@ -403,20 +416,82 @@ export const make = Effect.gen(function* () {
403416
const bufferedMessageIds = new Set(
404417
queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id),
405418
);
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()) {
407477
if (bufferedMessageIds.has(run.userMessageId)) {
408478
runs.push(run);
409479
continue;
410480
}
411-
if (run.queueHeld === true) continue;
481+
if (run.queueHeld === true && retryableRuns.length === 0) continue;
412482
events.push({
413483
id: yield* allocateEventId(),
414484
type: "run.updated",
415485
threadId: projection.thread.id,
416486
runId: run.id,
417487
providerInstanceId: run.providerInstanceId,
418488
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+
},
420495
});
421496
}
422497
for (const question of questions) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.