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): queued messages land after a resume that ran before them · Alb11747-Labs/t3code@fa179ed

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2
    • Orchestrator.ts
    • ThreadStop.test.ts
    • runtimeLayer.test.ts
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
+22-5Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,9 @@ export class OrchestratorV2 extends Context.Service<OrchestratorV2, Orchestrator
372372
"t3/orchestration-v2/Orchestrator/OrchestratorV2",
373373
) {}
374374

375+
/** Run ordinals can skip values: a queued run moves past newer runs when it starts. */
375376
function nextRunOrdinal(projection: Pick<OrchestrationV2ThreadProjection, "runs">): number {
376-
return projection.runs.length + 1;
377+
return projection.runs.reduce((max, run) => Math.max(max, run.ordinal), 0) + 1;
377378
}
378379

379380
/**
@@ -1329,6 +1330,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13291330
);
13301331
return;
13311332
}
1333+
// Run ordinals order the transcript, checkpoints, and the latest run. A
1334+
// run that started while this one waited (a resume of a held queue, a
1335+
// reorder, a delivery that jumps the queue) has a newer ordinal, so this
1336+
// run moves after it. A started run has its prompt in the transcript (a
1337+
// wake's prompt is a notification); a message cancelled from the queue has none.
1338+
const newerRunIds = projection.runs
1339+
.filter((run) => run.status !== "queued" && run.ordinal > queuedRun.ordinal)
1340+
.map((run) => run.id);
1341+
const startedAhead =
1342+
newerRunIds.length > 0 &&
1343+
(yield* projectionStore.getThreadRecords(threadId, ["turnItems"], {
1344+
turnItemTypes: ["user_message", "notification"],
1345+
turnItemRunIds: newerRunIds,
1346+
})).turnItems.length > 0;
1347+
const ordinal = startedAhead ? nextRunOrdinal(projection) : queuedRun.ordinal;
13321348
const rootNodeId = queuedRun.rootNodeId;
13331349
const attemptId = queuedRun.activeAttemptId;
13341350
const providerThreadId = queuedRun.providerThreadId;
@@ -1602,8 +1618,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16021618
...deliveryProviderThread,
16031619
providerSessionId,
16041620
status: "not_loaded",
1605-
firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? queuedRun.ordinal,
1606-
lastRunOrdinal: queuedRun.ordinal,
1621+
firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? ordinal,
1622+
lastRunOrdinal: ordinal,
16071623
handoffIds: appendContextHandoffId(
16081624
appendContextHandoffId(queuedProviderThread.handoffIds, handoff?.id ?? null),
16091625
legacyImportRecoveryHandoff?.id ?? null,
@@ -1612,6 +1628,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16121628
};
16131629
const startingRun: OrchestrationV2Run = {
16141630
...queuedRun,
1631+
ordinal,
16151632
status: "starting",
16161633
queuePosition: null,
16171634
startedAt: null,
@@ -1632,7 +1649,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16321649
providerTurnId: null,
16331650
nativeItemRef: null,
16341651
parentItemId: null,
1635-
ordinal: queuedRun.ordinal * 100,
1652+
ordinal: ordinal * 100,
16361653
status: "completed",
16371654
title: null,
16381655
type: "user_message",
@@ -1669,7 +1686,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16691686
providerTurnId: null,
16701687
nativeItemRef: null,
16711688
parentItemId: null,
1672-
ordinal: queuedRun.ordinal * 100 - 1,
1689+
ordinal: ordinal * 100 - 1,
16731690
status: "completed",
16741691
title: handoff === null ? "Imported context" : "Provider handoff",
16751692
startedAt: now,
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadStop.test.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,9 @@ it.effect.each(["thread.stop", "run.interrupt"] as const)(
466466
creationSource: "server",
467467
restartContinuationOfRunId: source.id,
468468
});
469+
// The held run resumed after the cancelled source ran, so it sorts after it.
469470
assert.deepEqual(yield* threadState(threadId), {
470-
runs: ["completed", "interrupted", "cancelled"],
471+
runs: ["completed", "cancelled", "interrupted"],
471472
watched: [],
472473
});
473474
}).pipe(Effect.provide(layerTest.pipe(Layer.provideMerge(TestClock.layer())))),
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/runtimeLayer.test.ts
+129-4Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3994,6 +3994,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
39943994
yield* settle("queued", queuedStartedAt);
39953995
assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("late-wake")).id);
39963996
assert.equal(millis((yield* runFor("late-wake")).workStartedAt), millis(queuedStartedAt));
3997+
3998+
// Runs sort in the order they started, including after a wake that ran ahead.
3999+
const ordinals = yield* Effect.forEach(
4000+
["prompt", "early-wake", "queued", "late-wake"],
4001+
(key) => Effect.map(runFor(key), (run) => run.ordinal),
4002+
);
4003+
assert.deepEqual(
4004+
ordinals,
4005+
ordinals.toSorted((left, right) => left - right),
4006+
);
39974007
}),
39984008
);
39994009

@@ -4186,6 +4196,115 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
41864196
}),
41874197
);
41884198

4199+
it.effect("places held queued messages after a continuation that started first", () =>
4200+
Effect.gen(function* () {
4201+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4202+
const eventSink = yield* EventSink.EventSinkV2;
4203+
const threadId = ThreadId.make("runtime-layer-continuation-before-queue");
4204+
const messageId = (key: string) => MessageId.make(`${threadId}:${key}`);
4205+
yield* orchestrator.dispatch({
4206+
type: "thread.create",
4207+
createdBy: "user",
4208+
creationSource: "web",
4209+
commandId: CommandId.make(`${threadId}:create`),
4210+
threadId,
4211+
projectId: ProjectId.make(`${threadId}:project`),
4212+
title: "Continuation before queue",
4213+
modelSelection,
4214+
runtimeMode: "full-access",
4215+
interactionMode: "default",
4216+
branch: null,
4217+
worktreePath: process.cwd(),
4218+
});
4219+
const dispatch = (key: string, start: boolean) =>
4220+
orchestrator.dispatch({
4221+
type: "message.dispatch",
4222+
createdBy: "user",
4223+
creationSource: "web",
4224+
commandId: CommandId.make(`${threadId}:message:${key}`),
4225+
threadId,
4226+
messageId: messageId(key),
4227+
text: key,
4228+
attachments: [],
4229+
modelSelection,
4230+
dispatchMode: { type: start ? "start_immediately" : "queue_after_active" },
4231+
});
4232+
const runFor = (key: string) =>
4233+
Effect.map(orchestrator.getThreadProjection(threadId), ({ runs }) => {
4234+
const run = runs.find((candidate) => candidate.userMessageId === messageId(key));
4235+
assert.isDefined(run);
4236+
return run;
4237+
});
4238+
yield* dispatch("active", true);
4239+
yield* dispatch("first-queued", false);
4240+
yield* dispatch("second-queued", false);
4241+
yield* orchestrator.dispatch({
4242+
type: "run.interrupt",
4243+
commandId: CommandId.make(`${threadId}:interrupt`),
4244+
threadId,
4245+
runId: (yield* runFor("active")).id,
4246+
holdQueue: true,
4247+
});
4248+
// The continuation starts while the older messages wait in the held queue.
4249+
yield* dispatch("continue", true);
4250+
yield* orchestrator.dispatch({
4251+
type: "queue.resume",
4252+
commandId: CommandId.make(`${threadId}:resume`),
4253+
threadId,
4254+
});
4255+
4256+
const startedRunIds = yield* Queue.unbounded<RunId>();
4257+
const afterSequence = yield* orchestrator.getThreadEventSequence(threadId);
4258+
yield* eventSink.stream({ threadId, afterSequence }).pipe(
4259+
Stream.runForEach((stored) =>
4260+
stored.event.type === "run.updated" && stored.event.payload.status === "starting"
4261+
? Queue.offer(startedRunIds, stored.event.payload.id)
4262+
: Effect.void,
4263+
),
4264+
Effect.forkScoped,
4265+
);
4266+
yield* Effect.yieldNow;
4267+
const complete = (key: string) =>
4268+
Effect.gen(function* () {
4269+
const run = yield* runFor(key);
4270+
const now = yield* DateTime.now;
4271+
yield* eventSink.write({
4272+
events: [
4273+
{
4274+
id: EventId.make(`${threadId}:${key}:completed`),
4275+
type: "run.updated",
4276+
threadId,
4277+
runId: run.id,
4278+
...(run.rootNodeId === null ? {} : { nodeId: run.rootNodeId }),
4279+
providerInstanceId: run.providerInstanceId,
4280+
occurredAt: now,
4281+
payload: { ...run, status: "completed", startedAt: now, completedAt: now },
4282+
},
4283+
],
4284+
});
4285+
});
4286+
4287+
yield* complete("continue");
4288+
assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("first-queued")).id);
4289+
yield* complete("first-queued");
4290+
assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("second-queued")).id);
4291+
4292+
const keys = ["active", "continue", "first-queued", "second-queued"];
4293+
const runs = yield* Effect.forEach(keys, runFor);
4294+
const ordinals = runs.map((run) => run.ordinal);
4295+
assert.deepEqual(
4296+
ordinals,
4297+
ordinals.toSorted((left, right) => left - right),
4298+
);
4299+
const { turnItems } = yield* orchestrator.getThreadProjection(threadId);
4300+
const userItems = turnItems.filter((item) => item.type === "user_message");
4301+
assert.deepEqual(
4302+
userItems.map((item) => item.messageId),
4303+
keys.map(messageId),
4304+
);
4305+
}),
4306+
);
4307+
41894308
it.effect.each(["startup", "shutdown"] as const)(
41904309
"preserves and holds queued messages across %s until explicitly resumed",
41914310
(trigger) =>
@@ -5425,8 +5544,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
54255544
: 0),
54265545
);
54275546
if (scenario === "queued-resume") {
5428-
assert.equal(after.runs[1]?.status, "queued");
5429-
assert.isTrue(after.runs[1]?.queueHeld);
5547+
const held = after.runs[1]!;
5548+
assert.equal(held.status, "queued");
5549+
assert.isTrue(held.queueHeld);
54305550
const continuation = after.runs[2]!;
54315551
const completedAt = yield* DateTime.now;
54325552
yield* events.write({
@@ -5447,8 +5567,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
54475567
threadId,
54485568
});
54495569
const resumed = yield* orchestrator.getThreadProjection(threadId);
5450-
assert.equal(resumed.runs[1]?.status, "starting");
5451-
assert.isFalse(resumed.runs[1]?.queueHeld);
5570+
// The held message ran after the continuation, so it now sorts after it.
5571+
assert.deepEqual(
5572+
resumed.runs.map((run) => run.id),
5573+
[run.id, continuation.id, held.id],
5574+
);
5575+
assert.equal(resumed.runs[2]?.status, "starting");
5576+
assert.isFalse(resumed.runs[2]?.queueHeld);
54525577
}
54535578
}),
54545579
);

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.