GeezerVison is showing github.com — Merge upstream PR #17773 into stark/main · AdEx-Partners-DE/t3code@e345a1c
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 |
|---|
@@ -379,8 +379,9 @@ export class OrchestratorV2 extends Context.Service<OrchestratorV2, Orchestrator | 379 | 379 | "t3/orchestration-v2/Orchestrator/OrchestratorV2", | 380 | 380 | ) {} | 381 | 381 |
| | 382 | +/** Run ordinals can skip values: a queued run moves past newer runs when it starts. */ | 382 | 383 | function nextRunOrdinal(projection: Pick<OrchestrationV2ThreadProjection, "runs">): number { | 383 | | - return projection.runs.length + 1; | | 384 | + return projection.runs.reduce((max, run) => Math.max(max, run.ordinal), 0) + 1; | 384 | 385 | } | 385 | 386 |
| 386 | 387 | /** | @@ -1359,6 +1360,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1359 | 1360 | ); | 1360 | 1361 | return; | 1361 | 1362 | } | | 1363 | + // Run ordinals order the transcript, checkpoints, and the latest run. A | | 1364 | + // run that started while this one waited (a resume of a held queue, a | | 1365 | + // reorder, a delivery that jumps the queue) has a newer ordinal, so this | | 1366 | + // run moves after it. A started run has its prompt in the transcript (a | | 1367 | + // wake's prompt is a notification); a message cancelled from the queue has none. | | 1368 | + const newerRunIds = projection.runs | | 1369 | + .filter((run) => run.status !== "queued" && run.ordinal > queuedRun.ordinal) | | 1370 | + .map((run) => run.id); | | 1371 | + const startedAhead = | | 1372 | + newerRunIds.length > 0 && | | 1373 | + (yield* projectionStore.getThreadRecords(threadId, ["turnItems"], { | | 1374 | + turnItemTypes: ["user_message", "notification"], | | 1375 | + turnItemRunIds: newerRunIds, | | 1376 | + })).turnItems.length > 0; | | 1377 | + const ordinal = startedAhead ? nextRunOrdinal(projection) : queuedRun.ordinal; | 1362 | 1378 | const rootNodeId = queuedRun.rootNodeId; | 1363 | 1379 | const attemptId = queuedRun.activeAttemptId; | 1364 | 1380 | const providerThreadId = queuedRun.providerThreadId; | @@ -1632,8 +1648,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1632 | 1648 | ...deliveryProviderThread, | 1633 | 1649 | providerSessionId, | 1634 | 1650 | status: "not_loaded", | 1635 | | - firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? queuedRun.ordinal, | 1636 | | - lastRunOrdinal: queuedRun.ordinal, | | 1651 | + firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? ordinal, | | 1652 | + lastRunOrdinal: ordinal, | 1637 | 1653 | handoffIds: appendContextHandoffId( | 1638 | 1654 | appendContextHandoffId(queuedProviderThread.handoffIds, handoff?.id ?? null), | 1639 | 1655 | legacyImportRecoveryHandoff?.id ?? null, | @@ -1642,6 +1658,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1642 | 1658 | }; | 1643 | 1659 | const startingRun: OrchestrationV2Run = { | 1644 | 1660 | ...queuedRun, | | 1661 | + ordinal, | 1645 | 1662 | status: "starting", | 1646 | 1663 | queuePosition: null, | 1647 | 1664 | startedAt: null, | @@ -1662,7 +1679,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1662 | 1679 | providerTurnId: null, | 1663 | 1680 | nativeItemRef: null, | 1664 | 1681 | parentItemId: null, | 1665 | | - ordinal: queuedRun.ordinal * 100, | | 1682 | + ordinal: ordinal * 100, | 1666 | 1683 | status: "completed", | 1667 | 1684 | title: null, | 1668 | 1685 | type: "user_message", | @@ -1699,7 +1716,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 1699 | 1716 | providerTurnId: null, | 1700 | 1717 | nativeItemRef: null, | 1701 | 1718 | parentItemId: null, | 1702 | | - ordinal: queuedRun.ordinal * 100 - 1, | | 1719 | + ordinal: ordinal * 100 - 1, | 1703 | 1720 | status: "completed", | 1704 | 1721 | title: handoff === null ? "Imported context" : "Provider handoff", | 1705 | 1722 | 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 |
|---|
@@ -4147,6 +4147,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 4147 | 4147 | yield* settle("queued", queuedStartedAt); | 4148 | 4148 | assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("late-wake")).id); | 4149 | 4149 | assert.equal(millis((yield* runFor("late-wake")).workStartedAt), millis(queuedStartedAt)); | | 4150 | + | | 4151 | + // Runs sort in the order they started, including after a wake that ran ahead. | | 4152 | + const ordinals = yield* Effect.forEach( | | 4153 | + ["prompt", "early-wake", "queued", "late-wake"], | | 4154 | + (key) => Effect.map(runFor(key), (run) => run.ordinal), | | 4155 | + ); | | 4156 | + assert.deepEqual( | | 4157 | + ordinals, | | 4158 | + ordinals.toSorted((left, right) => left - right), | | 4159 | + ); | 4150 | 4160 | }), | 4151 | 4161 | ); | 4152 | 4162 |
| @@ -4339,6 +4349,115 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 4339 | 4349 | }), | 4340 | 4350 | ); | 4341 | 4351 |
| | 4352 | + it.effect("places held queued messages after a continuation that started first", () => | | 4353 | + Effect.gen(function* () { | | 4354 | + const orchestrator = yield* Orchestrator.OrchestratorV2; | | 4355 | + const eventSink = yield* EventSink.EventSinkV2; | | 4356 | + const threadId = ThreadId.make("runtime-layer-continuation-before-queue"); | | 4357 | + const messageId = (key: string) => MessageId.make(`${threadId}:${key}`); | | 4358 | + yield* orchestrator.dispatch({ | | 4359 | + type: "thread.create", | | 4360 | + createdBy: "user", | | 4361 | + creationSource: "web", | | 4362 | + commandId: CommandId.make(`${threadId}:create`), | | 4363 | + threadId, | | 4364 | + projectId: ProjectId.make(`${threadId}:project`), | | 4365 | + title: "Continuation before queue", | | 4366 | + modelSelection, | | 4367 | + runtimeMode: "full-access", | | 4368 | + interactionMode: "default", | | 4369 | + branch: null, | | 4370 | + worktreePath: process.cwd(), | | 4371 | + }); | | 4372 | + const dispatch = (key: string, start: boolean) => | | 4373 | + orchestrator.dispatch({ | | 4374 | + type: "message.dispatch", | | 4375 | + createdBy: "user", | | 4376 | + creationSource: "web", | | 4377 | + commandId: CommandId.make(`${threadId}:message:${key}`), | | 4378 | + threadId, | | 4379 | + messageId: messageId(key), | | 4380 | + text: key, | | 4381 | + attachments: [], | | 4382 | + modelSelection, | | 4383 | + dispatchMode: { type: start ? "start_immediately" : "queue_after_active" }, | | 4384 | + }); | | 4385 | + const runFor = (key: string) => | | 4386 | + Effect.map(orchestrator.getThreadProjection(threadId), ({ runs }) => { | | 4387 | + const run = runs.find((candidate) => candidate.userMessageId === messageId(key)); | | 4388 | + assert.isDefined(run); | | 4389 | + return run; | | 4390 | + }); | | 4391 | + yield* dispatch("active", true); | | 4392 | + yield* dispatch("first-queued", false); | | 4393 | + yield* dispatch("second-queued", false); | | 4394 | + yield* orchestrator.dispatch({ | | 4395 | + type: "run.interrupt", | | 4396 | + commandId: CommandId.make(`${threadId}:interrupt`), | | 4397 | + threadId, | | 4398 | + runId: (yield* runFor("active")).id, | | 4399 | + holdQueue: true, | | 4400 | + }); | | 4401 | + // The continuation starts while the older messages wait in the held queue. | | 4402 | + yield* dispatch("continue", true); | | 4403 | + yield* orchestrator.dispatch({ | | 4404 | + type: "queue.resume", | | 4405 | + commandId: CommandId.make(`${threadId}:resume`), | | 4406 | + threadId, | | 4407 | + }); | | 4408 | + | | 4409 | + const startedRunIds = yield* Queue.unbounded<RunId>(); | | 4410 | + const afterSequence = yield* orchestrator.getThreadEventSequence(threadId); | | 4411 | + yield* eventSink.stream({ threadId, afterSequence }).pipe( | | 4412 | + Stream.runForEach((stored) => | | 4413 | + stored.event.type === "run.updated" && stored.event.payload.status === "starting" | | 4414 | + ? Queue.offer(startedRunIds, stored.event.payload.id) | | 4415 | + : Effect.void, | | 4416 | + ), | | 4417 | + Effect.forkScoped, | | 4418 | + ); | | 4419 | + yield* Effect.yieldNow; | | 4420 | + const complete = (key: string) => | | 4421 | + Effect.gen(function* () { | | 4422 | + const run = yield* runFor(key); | | 4423 | + const now = yield* DateTime.now; | | 4424 | + yield* eventSink.write({ | | 4425 | + events: [ | | 4426 | + { | | 4427 | + id: EventId.make(`${threadId}:${key}:completed`), | | 4428 | + type: "run.updated", | | 4429 | + threadId, | | 4430 | + runId: run.id, | | 4431 | + ...(run.rootNodeId === null ? {} : { nodeId: run.rootNodeId }), | | 4432 | + providerInstanceId: run.providerInstanceId, | | 4433 | + occurredAt: now, | | 4434 | + payload: { ...run, status: "completed", startedAt: now, completedAt: now }, | | 4435 | + }, | | 4436 | + ], | | 4437 | + }); | | 4438 | + }); | | 4439 | + | | 4440 | + yield* complete("continue"); | | 4441 | + assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("first-queued")).id); | | 4442 | + yield* complete("first-queued"); | | 4443 | + assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("second-queued")).id); | | 4444 | + | | 4445 | + const keys = ["active", "continue", "first-queued", "second-queued"]; | | 4446 | + const runs = yield* Effect.forEach(keys, runFor); | | 4447 | + const ordinals = runs.map((run) => run.ordinal); | | 4448 | + assert.deepEqual( | | 4449 | + ordinals, | | 4450 | + ordinals.toSorted((left, right) => left - right), | | 4451 | + ); | | 4452 | + const { turnItems } = yield* orchestrator.getThreadProjection(threadId); | | 4453 | + const userItems = turnItems.filter((item) => item.type === "user_message"); | | 4454 | + assert.deepEqual( | | 4455 | + userItems.map((item) => item.messageId), | | 4456 | + keys.map(messageId), | | 4457 | + ); | | 4458 | + }), | | 4459 | + ); | | 4460 | + | 4342 | 4461 | it.effect.each(["startup", "shutdown"] as const)( | 4343 | 4462 | "preserves and holds queued messages across %s until explicitly resumed", | 4344 | 4463 | (trigger) => | @@ -5636,8 +5755,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => { | 5636 | 5755 | : 0), | 5637 | 5756 | ); | 5638 | 5757 | if (scenario === "queued-resume") { | 5639 | | - assert.equal(after.runs[1]?.status, "queued"); | 5640 | | - assert.isTrue(after.runs[1]?.queueHeld); | | 5758 | + const held = after.runs[1]!; | | 5759 | + assert.equal(held.status, "queued"); | | 5760 | + assert.isTrue(held.queueHeld); | 5641 | 5761 | const continuation = after.runs[2]!; | 5642 | 5762 | const completedAt = yield* DateTime.now; | 5643 | 5763 | yield* events.write({ | @@ -5658,8 +5778,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => { | 5658 | 5778 | threadId, | 5659 | 5779 | }); | 5660 | 5780 | const resumed = yield* orchestrator.getThreadProjection(threadId); | 5661 | | - assert.equal(resumed.runs[1]?.status, "starting"); | 5662 | | - assert.isFalse(resumed.runs[1]?.queueHeld); | | 5781 | + // The held message ran after the continuation, so it now sorts after it. | | 5782 | + assert.deepEqual( | | 5783 | + resumed.runs.map((run) => run.id), | | 5784 | + [run.id, continuation.id, held.id], | | 5785 | + ); | | 5786 | + assert.equal(resumed.runs[2]?.status, "starting"); | | 5787 | + assert.isFalse(resumed.runs[2]?.queueHeld); | 5663 | 5788 | } | 5664 | 5789 | }), | 5665 | 5790 | ); | |
|