GeezerVison is showing github.com — fix(server): preserve active turns when switching provider instances … · pingdotgg/t3code@94f54a5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/threadAccess.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,8 @@ export const dispatchFailure = ( | 48 | 48 | error: OrchestratorV2Error | ThreadManagement.ThreadManagementProviderInstanceDisabledError, | 49 | 49 | ) => | 50 | 50 | error._tag === "ThreadManagementProviderInstanceDisabledError" || | 51 | | - error._tag === "OrchestratorRuntimeModeAboveCeilingError" | | 51 | + error._tag === "OrchestratorRuntimeModeAboveCeilingError" || | | 52 | + error._tag === "OrchestratorProviderSelectionBusyError" | 52 | 53 | ? new OrchestratorMcpFailure({ code: "invalid_request", message: error.message }) | 53 | 54 | : (error._tag === "OrchestratorDispatchError" || | 54 | 55 | error._tag === "OrchestratorCommandRejectedError") && | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/core.test.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { | 35 | 35 | OrchestratorCommandRejectedError, | 36 | 36 | OrchestratorDispatchError, | 37 | 37 | OrchestratorProjectionError, | | 38 | + OrchestratorProviderSelectionBusyError, | 38 | 39 | } from "../../orchestration-v2/Orchestrator.ts"; | 39 | 40 |
| 40 | 41 | import * as ServerSecretStore from "../../auth/ServerSecretStore.ts"; | @@ -227,6 +228,11 @@ it("bounds public command rejections and redacts internal dispatch causes", () = | 227 | 228 | new OrchestratorCommandRejectedError({ ...command, cause: "Run is not queued." }), | 228 | 229 | ).message, | 229 | 230 | ).toBe("Run is not queued."); | | 231 | + const busy = new OrchestratorProviderSelectionBusyError({ | | 232 | + commandId: command.commandId, | | 233 | + threadId, | | 234 | + }); | | 235 | + expect(dispatchFailure(busy)).toMatchObject({ code: "invalid_request", message: busy.message }); | 230 | 236 | for (const cause of [ | 231 | 237 | undefined, | 232 | 238 | "", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts +188Lines changed: 188 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,6 +62,194 @@ const layerTest = Layer.mergeAll( | 62 | 62 | ), | 63 | 63 | ); | 64 | 64 |
| | 65 | +it.effect.each( | | 66 | + (["provider.switch", "thread.model-selection.set"] as const).flatMap((type) => | | 67 | + [ | | 68 | + { active: true, selectionChange: "instance" }, | | 69 | + { active: false, selectionChange: "instance" }, | | 70 | + { active: true, selectionChange: "next-turn" }, | | 71 | + { active: true, selectionChange: "restart" }, | | 72 | + ].map((scenario) => ({ type, ...scenario })), | | 73 | + ), | | 74 | +)("$type $selectionChange ($active)", ({ type, active, selectionChange }) => | | 75 | + Effect.gen(function* () { | | 76 | + const orchestrator = yield* Orchestrator.OrchestratorV2; | | 77 | + const projections = yield* ProjectionStore.ProjectionStoreV2; | | 78 | + const outbox = yield* EffectOutbox.EffectOutboxV2; | | 79 | + const threadId = ThreadId.make("thread:instance-switch"); | | 80 | + const now = yield* DateTime.now; | | 81 | + const sessionId = ProviderSessionId.make("session:instance-switch"); | | 82 | + const providerThreadId = ProviderThreadId.make("provider-thread:instance-switch"); | | 83 | + const runId = RunId.make("run:instance-switch"); | | 84 | + const nodeId = NodeId.make("node:instance-switch"); | | 85 | + const attemptId = RunAttemptId.make("attempt:instance-switch"); | | 86 | + yield* orchestrator.dispatch({ | | 87 | + type: "thread.create", | | 88 | + commandId: CommandId.make("create-instance-switch"), | | 89 | + threadId, | | 90 | + projectId: ProjectId.make("project:instance-switch"), | | 91 | + title: "Instance switch", | | 92 | + modelSelection, | | 93 | + runtimeMode: "full-access", | | 94 | + interactionMode: "default", | | 95 | + branch: null, | | 96 | + worktreePath: null, | | 97 | + createdBy: "user", | | 98 | + creationSource: "web", | | 99 | + }); | | 100 | + yield* projections.apply({ | | 101 | + id: EventId.make("instance-switch:session"), | | 102 | + type: "provider-session.attached", | | 103 | + threadId, | | 104 | + occurredAt: now, | | 105 | + payload: { | | 106 | + id: sessionId, | | 107 | + driver: adapter.driver, | | 108 | + providerInstanceId: instanceId, | | 109 | + status: "ready", | | 110 | + cwd: "/repo", | | 111 | + model: modelSelection.model, | | 112 | + capabilities: CodexProviderCapabilitiesV2, | | 113 | + createdAt: now, | | 114 | + updatedAt: now, | | 115 | + lastError: null, | | 116 | + }, | | 117 | + }); | | 118 | + yield* projections.apply({ | | 119 | + id: EventId.make("instance-switch:provider-thread"), | | 120 | + type: "provider-thread.updated", | | 121 | + threadId, | | 122 | + occurredAt: now, | | 123 | + payload: { | | 124 | + id: providerThreadId, | | 125 | + driver: adapter.driver, | | 126 | + providerInstanceId: instanceId, | | 127 | + providerSessionId: sessionId, | | 128 | + appThreadId: threadId, | | 129 | + ownerNodeId: null, | | 130 | + nativeThreadRef: { | | 131 | + driver: adapter.driver, | | 132 | + nativeId: "native:instance-switch", | | 133 | + strength: "strong", | | 134 | + }, | | 135 | + nativeConversationHeadRef: null, | | 136 | + status: "idle", | | 137 | + firstRunOrdinal: 1, | | 138 | + lastRunOrdinal: 1, | | 139 | + handoffIds: [], | | 140 | + forkedFrom: null, | | 141 | + createdAt: now, | | 142 | + updatedAt: now, | | 143 | + }, | | 144 | + }); | | 145 | + if (active) { | | 146 | + yield* projections.apply({ | | 147 | + id: EventId.make("instance-switch:run"), | | 148 | + type: "run.created", | | 149 | + threadId, | | 150 | + occurredAt: now, | | 151 | + payload: { | | 152 | + id: runId, | | 153 | + threadId, | | 154 | + ordinal: 1, | | 155 | + providerInstanceId: instanceId, | | 156 | + modelSelection, | | 157 | + providerThreadId, | | 158 | + userMessageId: MessageId.make("message:instance-switch"), | | 159 | + rootNodeId: nodeId, | | 160 | + activeAttemptId: attemptId, | | 161 | + status: "running", | | 162 | + requestedAt: now, | | 163 | + startedAt: now, | | 164 | + completedAt: null, | | 165 | + checkpointId: null, | | 166 | + contextHandoffId: null, | | 167 | + }, | | 168 | + }); | | 169 | + yield* projections.apply({ | | 170 | + id: EventId.make("instance-switch:turn"), | | 171 | + type: "provider-turn.updated", | | 172 | + threadId, | | 173 | + runId, | | 174 | + occurredAt: now, | | 175 | + payload: { | | 176 | + id: ProviderTurnId.make("provider-turn:instance-switch"), | | 177 | + providerThreadId, | | 178 | + nodeId, | | 179 | + runAttemptId: attemptId, | | 180 | + nativeTurnRef: { driver: adapter.driver, nativeId: "native:turn", strength: "strong" }, | | 181 | + ordinal: 1, | | 182 | + status: "running", | | 183 | + startedAt: now, | | 184 | + completedAt: null, | | 185 | + }, | | 186 | + }); | | 187 | + } | | 188 | + const before = yield* projections.getThreadProjection(threadId); | | 189 | + const sequence = yield* orchestrator.getThreadEventSequence(threadId); | | 190 | + const commandId = CommandId.make("switch-instance"); | | 191 | + const target = { | | 192 | + instanceId: | | 193 | + selectionChange === "instance" ? ProviderInstanceId.make("codex-other") : instanceId, | | 194 | + model: selectionChange === "instance" ? modelSelection.model : "gpt-6", | | 195 | + }; | | 196 | + const outcome = yield* orchestrator | | 197 | + .dispatch({ | | 198 | + type, | | 199 | + commandId, | | 200 | + threadId, | | 201 | + modelSelection: target, | | 202 | + }) | | 203 | + .pipe(Effect.result); | | 204 | + const after = yield* projections.getThreadProjection(threadId); | | 205 | + if (active && selectionChange !== "next-turn") { | | 206 | + assert.deepEqual( | | 207 | + after, | | 208 | + before, | | 209 | + "a refused switch must preserve the turn, session and selection", | | 210 | + ); | | 211 | + assert.deepEqual( | | 212 | + yield* outbox.listByCommandId(commandId), | | 213 | + [], | | 214 | + "a refused switch must not release the provider session", | | 215 | + ); | | 216 | + assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence); | | 217 | + assert.ok(Result.isFailure(outcome)); | | 218 | + assert.instanceOf(outcome.failure, Orchestrator.OrchestratorProviderSelectionBusyError); | | 219 | + } else { | | 220 | + assert.ok(Result.isSuccess(outcome)); | | 221 | + assert.deepEqual(after.thread.modelSelection, target); | | 222 | + assert.equal(after.thread.providerInstanceId, target.instanceId); | | 223 | + assert.deepEqual(after.runs, before.runs); | | 224 | + assert.deepEqual(after.providerTurns, before.providerTurns); | | 225 | + } | | 226 | + }).pipe( | | 227 | + Effect.provide( | | 228 | + Layer.mergeAll( | | 229 | + layerDatabase, | | 230 | + ProjectionStore.layer.pipe(Layer.provide(layerDatabase)), | | 231 | + EffectOutbox.layer.pipe(Layer.provide(layerDatabase)), | | 232 | + ProviderReplayHarness.layerWithRegistry( | | 233 | + { name: "instance-switch" }, | | 234 | + ProviderAdapterRegistry.layerFromAdapters([ | | 235 | + { | | 236 | + ...adapter, | | 237 | + planSelectionTransition: () => | | 238 | + Effect.succeed( | | 239 | + selectionChange === "restart" | | 240 | + ? { type: "restart_session" as const } | | 241 | + : { type: "apply_on_next_turn" as const }, | | 242 | + ), | | 243 | + }, | | 244 | + { ...adapter, instanceId: ProviderInstanceId.make("codex-other") }, | | 245 | + ]), | | 246 | + { databaseLayer: layerDatabase, runEffectWorker: false }, | | 247 | + ), | | 248 | + ), | | 249 | + ), | | 250 | + ), | | 251 | +); | | 252 | + | 65 | 253 | it.effect( | 66 | 254 | "records approval feedback and targets only its runtime request without dispatching a parent turn", | 67 | 255 | () => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +23-1Lines changed: 23 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -291,6 +291,15 @@ export class OrchestratorThreadAboveModeLimitError extends Schema.TaggedError<Or | 291 | 291 | } | 292 | 292 | } | 293 | 293 |
| | 294 | +export class OrchestratorProviderSelectionBusyError extends Schema.TaggedError<OrchestratorProviderSelectionBusyError>()( | | 295 | + "OrchestratorProviderSelectionBusyError", | | 296 | + { commandId: CommandId, threadId: ThreadId }, | | 297 | +) { | | 298 | + override get message(): string { | | 299 | + return `Thread ${this.threadId} has an active turn. Wait for it to finish, then retry the provider selection change.`; | | 300 | + } | | 301 | +} | | 302 | + | 294 | 303 | const isOrchestratorDispatchError = Schema.is(OrchestratorDispatchError); | 295 | 304 |
| 296 | 305 | /** The ceiling refusal under however many dispatch errors wrapped it on the way out. */ | @@ -355,6 +364,7 @@ export const OrchestratorV2Error = Schema.Union([ | 355 | 364 | OrchestratorCommandIdConflictError, | 356 | 365 | OrchestratorSubagentThreadReadOnlyError, | 357 | 366 | OrchestratorThreadAboveModeLimitError, | | 367 | + OrchestratorProviderSelectionBusyError, | 358 | 368 | OrchestratorRuntimeModeAboveCeilingError, | 359 | 369 | ]); | 360 | 370 | export type OrchestratorV2Error = typeof OrchestratorV2Error.Type; | @@ -3832,12 +3842,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 3832 | 3842 | }), | 3833 | 3843 | ), | 3834 | 3844 | ); | 3835 | | - return yield* providerSwitchService | | 3845 | + const plan = yield* providerSwitchService | 3836 | 3846 | .plan({ | 3837 | 3847 | projection: providerContext!, | 3838 | 3848 | targetModelSelection: command.modelSelection, | 3839 | 3849 | }) | 3840 | 3850 | .pipe(mapDispatchError(command)); | | 3851 | + if (plan.instanceChanged || plan.releaseProviderSessionIds.length > 0) { | | 3852 | + const projection = yield* loadProjectionForCommand(command, ["runs"], { | | 3853 | + turnItemTypes: [], | | 3854 | + }); | | 3855 | + if (projection.runs.some(isActiveRun)) { | | 3856 | + return yield* new OrchestratorProviderSelectionBusyError({ | | 3857 | + commandId: command.commandId, | | 3858 | + threadId: command.threadId, | | 3859 | + }); | | 3860 | + } | | 3861 | + } | | 3862 | + return plan; | 3841 | 3863 | }) | 3842 | 3864 | : null; | 3843 | 3865 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadMessageIntake.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ function dispatchWasNotAccepted( | 24 | 24 | case "OrchestratorCommandIdConflictError": | 25 | 25 | case "OrchestratorSubagentThreadReadOnlyError": | 26 | 26 | case "OrchestratorThreadAboveModeLimitError": | | 27 | + case "OrchestratorProviderSelectionBusyError": | 27 | 28 | case "OrchestratorRuntimeModeAboveCeilingError": | 28 | 29 | case "ThreadManagementProviderInstanceDisabledError": | 29 | 30 | return true; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/runtimeLayer.test.ts -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6287,7 +6287,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 6287 | 6287 | it.effect.each([ | 6288 | 6288 | ["server", modelSelection], | 6289 | 6289 | ["provider", modelSelection], | 6290 | | - ["provider", { ...modelSelection, instanceId: alternateInstanceId }], | 6291 | 6290 | ] as const)( | 6292 | 6291 | "delivers a queued monitor update from %s after conversation selection %j", | 6293 | 6292 | ([creationSource, currentSelection]) => | @@ -6400,7 +6399,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { | 6400 | 6399 |
| 6401 | 6400 | it.effect.each([ | 6402 | 6401 | { ...modelSelection, model: "gpt-5.5", options: [{ id: "reasoningEffort", value: "high" }] }, | 6403 | | - { instanceId: alternateInstanceId, model: "gpt-5.5" }, | 6404 | 6402 | ])( | 6405 | 6403 | "starts a queued delegated completion on a selection set while it waited: %j", | 6406 | 6404 | (newerSelection) => | |
|