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 — Merge upstream PR #17773 into stark/main · AdEx-Partners-DE/t3code@e345a1c

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
@@ -379,8 +379,9 @@ export class OrchestratorV2 extends Context.Service<OrchestratorV2, Orchestrator
379379
"t3/orchestration-v2/Orchestrator/OrchestratorV2",
380380
) {}
381381

382+
/** Run ordinals can skip values: a queued run moves past newer runs when it starts. */
382383
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;
384385
}
385386

386387
/**
@@ -1359,6 +1360,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13591360
);
13601361
return;
13611362
}
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;
13621378
const rootNodeId = queuedRun.rootNodeId;
13631379
const attemptId = queuedRun.activeAttemptId;
13641380
const providerThreadId = queuedRun.providerThreadId;
@@ -1632,8 +1648,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16321648
...deliveryProviderThread,
16331649
providerSessionId,
16341650
status: "not_loaded",
1635-
firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? queuedRun.ordinal,
1636-
lastRunOrdinal: queuedRun.ordinal,
1651+
firstRunOrdinal: queuedProviderThread.firstRunOrdinal ?? ordinal,
1652+
lastRunOrdinal: ordinal,
16371653
handoffIds: appendContextHandoffId(
16381654
appendContextHandoffId(queuedProviderThread.handoffIds, handoff?.id ?? null),
16391655
legacyImportRecoveryHandoff?.id ?? null,
@@ -1642,6 +1658,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16421658
};
16431659
const startingRun: OrchestrationV2Run = {
16441660
...queuedRun,
1661+
ordinal,
16451662
status: "starting",
16461663
queuePosition: null,
16471664
startedAt: null,
@@ -1662,7 +1679,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16621679
providerTurnId: null,
16631680
nativeItemRef: null,
16641681
parentItemId: null,
1665-
ordinal: queuedRun.ordinal * 100,
1682+
ordinal: ordinal * 100,
16661683
status: "completed",
16671684
title: null,
16681685
type: "user_message",
@@ -1699,7 +1716,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16991716
providerTurnId: null,
17001717
nativeItemRef: null,
17011718
parentItemId: null,
1702-
ordinal: queuedRun.ordinal * 100 - 1,
1719+
ordinal: ordinal * 100 - 1,
17031720
status: "completed",
17041721
title: handoff === null ? "Imported context" : "Provider handoff",
17051722
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
@@ -4147,6 +4147,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
41474147
yield* settle("queued", queuedStartedAt);
41484148
assert.equal(yield* Queue.take(startedRunIds), (yield* runFor("late-wake")).id);
41494149
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+
);
41504160
}),
41514161
);
41524162

@@ -4339,6 +4349,115 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
43394349
}),
43404350
);
43414351

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+
43424461
it.effect.each(["startup", "shutdown"] as const)(
43434462
"preserves and holds queued messages across %s until explicitly resumed",
43444463
(trigger) =>
@@ -5636,8 +5755,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
56365755
: 0),
56375756
);
56385757
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);
56415761
const continuation = after.runs[2]!;
56425762
const completedAt = yield* DateTime.now;
56435763
yield* events.write({
@@ -5658,8 +5778,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
56585778
threadId,
56595779
});
56605780
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);
56635788
}
56645789
}),
56655790
);

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.