GeezerVison is showing github.com — fix(server): queued messages land after a resume that ran before them · Alb11747-Labs/t3code@fa179ed
File treeExpand file treeCollapse file tree Open diff view settings apps/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 +22-5Lines changed: 22 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -372,8 +372,9 @@ export class OrchestratorV2 extends Context.Service<OrchestratorV2, Orchestrator | 372 | 372 | "t3/orchestration-v2/Orchestrator/OrchestratorV2", | 373 | 373 | ) {} | 374 | 374 |
| | 375 | +/** Run ordinals can skip values: a queued run moves past newer runs when it starts. */ | 375 | 376 | 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; | 377 | 378 | } | 378 | 379 |
| 379 | 380 | /** | @@ -1329,6 +1330,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1329 | 1330 | ); | 1330 | 1331 | return; | 1331 | 1332 | } | | 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; | 1332 | 1348 | const rootNodeId = queuedRun.rootNodeId; | 1333 | 1349 | const attemptId = queuedRun.activeAttemptId; | 1334 | 1350 | const providerThreadId = queuedRun.providerThreadId; | @@ -1602,8 +1618,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1602 | 1618 | ...deliveryProviderThread, | 1603 | 1619 | providerSessionId, | 1604 | 1620 | status: "not_loaded", | 1605 | | - firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? queuedRun.ordinal, | 1606 | | - lastRunOrdinal: queuedRun.ordinal, | | 1621 | + firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? ordinal, | | 1622 | + lastRunOrdinal: ordinal, | 1607 | 1623 | handoffIds: appendContextHandoffId( | 1608 | 1624 | appendContextHandoffId(queuedProviderThread.handoffIds, handoff?.id ?? null), | 1609 | 1625 | legacyImportRecoveryHandoff?.id ?? null, | @@ -1612,6 +1628,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1612 | 1628 | }; | 1613 | 1629 | const startingRun: OrchestrationV2Run = { | 1614 | 1630 | ...queuedRun, | | 1631 | + ordinal, | 1615 | 1632 | status: "starting", | 1616 | 1633 | queuePosition: null, | 1617 | 1634 | startedAt: null, | @@ -1632,7 +1649,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1632 | 1649 | providerTurnId: null, | 1633 | 1650 | nativeItemRef: null, | 1634 | 1651 | parentItemId: null, | 1635 | | - ordinal: queuedRun.ordinal * 100, | | 1652 | + ordinal: ordinal * 100, | 1636 | 1653 | status: "completed", | 1637 | 1654 | title: null, | 1638 | 1655 | type: "user_message", | @@ -1669,7 +1686,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1669 | 1686 | providerTurnId: null, | 1670 | 1687 | nativeItemRef: null, | 1671 | 1688 | parentItemId: null, | 1672 | | - ordinal: queuedRun.ordinal * 100 - 1, | | 1689 | + ordinal: ordinal * 100 - 1, | 1673 | 1690 | status: "completed", | 1674 | 1691 | title: handoff === null ? "Imported context" : "Provider handoff", | 1675 | 1692 | startedAt: now, | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -466,8 +466,9 @@ it.effect.each(["thread.stop", "run.interrupt"] as const)( | 466 | 466 | creationSource: "server", | 467 | 467 | restartContinuationOfRunId: source.id, | 468 | 468 | }); | | 469 | + // The held run resumed after the cancelled source ran, so it sorts after it. | 469 | 470 | assert.deepEqual(yield* threadState(threadId), { | 470 | | - runs: ["completed", "interrupted", "cancelled"], | | 471 | + runs: ["completed", "cancelled", "interrupted"], | 471 | 472 | watched: [], | 472 | 473 | }); | 473 | 474 | }).pipe(Effect.provide(layerTest.pipe(Layer.provideMerge(TestClock.layer())))), | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -3994,6 +3994,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 3994 | 3994 | yield* settle("queued", queuedStartedAt); | 3995 | 3995 | assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("late-wake")).id); | 3996 | 3996 | 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 | + ); | 3997 | 4007 | }), | 3998 | 4008 | ); | 3999 | 4009 |
| @@ -4186,6 +4196,115 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 4186 | 4196 | }), | 4187 | 4197 | ); | 4188 | 4198 |
| | 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 | + | 4189 | 4308 | it.effect.each(["startup", "shutdown"] as const)( | 4190 | 4309 | "preserves and holds queued messages across %s until explicitly resumed", | 4191 | 4310 | (trigger) => | @@ -5425,8 +5544,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => { | 5425 | 5544 | : 0), | 5426 | 5545 | ); | 5427 | 5546 | 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); | 5430 | 5550 | const continuation = after.runs[2]!; | 5431 | 5551 | const completedAt = yield* DateTime.now; | 5432 | 5552 | yield* events.write({ | @@ -5447,8 +5567,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => { | 5447 | 5567 | threadId, | 5448 | 5568 | }); | 5449 | 5569 | 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); | 5452 | 5577 | } | 5453 | 5578 | }), | 5454 | 5579 | ); | |
|