Skip to content

Commit ec2530b

Browse files
committed
fix(server): reject temporary mode changes that require a restart
1 parent f23ab60 commit ec2530b

3 files changed

Lines changed: 159 additions & 47 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,9 +1707,20 @@ it.effect.each([
17071707
["switch", "other-claude", true, true],
17081708
["switch", "codex", true, false],
17091709
["switch", "other-codex", false, false],
1710+
["runtime-mode", "codex", true, true, false],
1711+
["runtime-mode", "codex", true, false, true],
1712+
["runtime-mode", "codex", true, false, false, false],
1713+
["runtime-mode", "codex", false, false, false],
17101714
] as const)(
17111715
"%s to %s with temporary=%s rejects=%s before changing a live session",
1712-
([operation, target, temporary, rejects]) => {
1716+
([
1717+
operation,
1718+
target,
1719+
temporary,
1720+
rejects,
1721+
supportsRuntimeModeSwitchInSession = true,
1722+
hasSession = true,
1723+
]) => {
17131724
const threadId = ThreadId.make("thread:temporary-provider-switch");
17141725
const otherSelection = { instanceId: ProviderInstanceId.make(target), model: "test" };
17151726
const owned = new Set<ThreadId>(temporary ? [threadId] : []);
@@ -1760,74 +1771,95 @@ it.effect.each([
17601771
});
17611772
const now = yield* DateTime.now;
17621773
const sessionId = ProviderSessionId.make("temporary-switch-session");
1763-
yield* eventSink.write({
1764-
events: [
1765-
{
1766-
id: EventId.make("temporary-switch-session-attached"),
1767-
type: "provider-session.attached",
1768-
threadId,
1769-
occurredAt: now,
1770-
payload: {
1771-
id: sessionId,
1772-
driver: adapter.driver,
1773-
providerInstanceId: instanceId,
1774-
status: "ready",
1775-
cwd: "/repo",
1776-
model: modelSelection.model,
1777-
capabilities: {
1778-
...CodexProviderCapabilitiesV2,
1779-
sessions: {
1780-
...CodexProviderCapabilitiesV2.sessions,
1781-
supportsProviderSwitchingViaHandoff: false,
1774+
if (hasSession)
1775+
yield* eventSink.write({
1776+
events: [
1777+
{
1778+
id: EventId.make("temporary-switch-session-attached"),
1779+
type: "provider-session.attached",
1780+
threadId,
1781+
occurredAt: now,
1782+
payload: {
1783+
id: sessionId,
1784+
driver: adapter.driver,
1785+
providerInstanceId: instanceId,
1786+
status: "ready",
1787+
cwd: "/repo",
1788+
model: modelSelection.model,
1789+
capabilities: {
1790+
...CodexProviderCapabilitiesV2,
1791+
sessions: {
1792+
...CodexProviderCapabilitiesV2.sessions,
1793+
supportsProviderSwitchingViaHandoff: false,
1794+
supportsRuntimeModeSwitchInSession,
1795+
},
17821796
},
1797+
createdAt: now,
1798+
updatedAt: now,
1799+
lastError: null,
17831800
},
1784-
createdAt: now,
1785-
updatedAt: now,
1786-
lastError: null,
17871801
},
1788-
},
1789-
],
1790-
});
1802+
],
1803+
});
17911804
const before = yield* projections.getThreadProviderContext(threadId);
17921805
const sequence = yield* orchestrator.getThreadEventSequence(threadId);
17931806
const commandId = CommandId.make("temporary-switch");
17941807
const refused = yield* Effect.result(
17951808
orchestrator.dispatch(
1796-
operation !== "message"
1809+
operation === "runtime-mode"
17971810
? {
1798-
type: operation === "selection" ? "thread.model-selection.set" : "provider.switch",
1811+
type: "thread.runtime-mode.set",
17991812
commandId,
18001813
threadId,
1801-
modelSelection: otherSelection,
1814+
runtimeMode: "approval-required",
18021815
}
1803-
: {
1804-
type: "message.dispatch",
1805-
commandId,
1806-
threadId,
1807-
modelSelection: otherSelection,
1808-
messageId: MessageId.make("temporary-switch-message"),
1809-
text: "Switch provider",
1810-
attachments: [],
1811-
dispatchMode: { type: "start_immediately" },
1812-
createdBy: "user",
1813-
creationSource: "web",
1814-
},
1816+
: operation !== "message"
1817+
? {
1818+
type:
1819+
operation === "selection" ? "thread.model-selection.set" : "provider.switch",
1820+
commandId,
1821+
threadId,
1822+
modelSelection: otherSelection,
1823+
}
1824+
: {
1825+
type: "message.dispatch",
1826+
commandId,
1827+
threadId,
1828+
modelSelection: otherSelection,
1829+
messageId: MessageId.make("temporary-switch-message"),
1830+
text: "Switch provider",
1831+
attachments: [],
1832+
dispatchMode: { type: "start_immediately" },
1833+
createdBy: "user",
1834+
creationSource: "web",
1835+
},
18151836
),
18161837
);
18171838
assert.equal(Result.isFailure(refused), rejects);
18181839
const projection = yield* orchestrator.getThreadRecords(threadId, ["runs"]);
18191840
assert.isEmpty(projection.runs);
18201841
if (rejects) {
1842+
if (operation === "runtime-mode" && Result.isFailure(refused)) {
1843+
assert.instanceOf(refused.failure, Orchestrator.OrchestratorDispatchError);
1844+
if (refused.failure._tag === "OrchestratorDispatchError") {
1845+
assert.equal(refused.failure.cause, "Keep this thread before using this operation.");
1846+
}
1847+
}
18211848
assert.deepEqual(yield* projections.getThreadProviderContext(threadId), before);
18221849
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
18231850
assert.isEmpty(yield* eventSink.readByCommandId({ commandId }).pipe(Stream.runCollect));
18241851
assert.isEmpty(yield* outbox.listByCommandId(commandId));
18251852
} else {
1826-
assert.deepEqual(projection.thread.modelSelection, otherSelection);
1853+
if (operation === "runtime-mode") {
1854+
assert.equal(projection.thread.runtimeMode, "approval-required");
1855+
assert.deepEqual(projection.thread.modelSelection, modelSelection);
1856+
} else {
1857+
assert.deepEqual(projection.thread.modelSelection, otherSelection);
1858+
}
18271859
if (temporary) {
18281860
assert.equal(
18291861
(yield* projections.getThreadProviderContext(threadId)).providerSessions[0]?.id,
1830-
sessionId,
1862+
hasSession ? sessionId : undefined,
18311863
);
18321864
assert.isEmpty(yield* outbox.listByCommandId(commandId));
18331865
}

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3110,16 +3110,45 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
31103110
(command.type === "thread.metadata.update" &&
31113111
command.worktreePath !== undefined &&
31123112
command.worktreePath !== thread.worktreePath);
3113+
const temporaryRuntimeModeChange =
3114+
command.type === "thread.runtime-mode.set" &&
3115+
Option.isSome(temporaryStorage) &&
3116+
(yield* temporaryStorage.value.isTemporary(command.threadId));
31133117
const providerContext = needsProviderState
31143118
? yield* projectionStore
31153119
.getThreadProviderContext(
31163120
command.threadId,
31173121
command.type === "thread.model-selection.set" || command.type === "provider.switch"
31183122
? command.modelSelection.instanceId
3119-
: undefined,
3123+
: temporaryRuntimeModeChange
3124+
? thread.modelSelection.instanceId
3125+
: undefined,
31203126
)
31213127
.pipe(mapDispatchError(command))
31223128
: null;
3129+
if (temporaryRuntimeModeChange) {
3130+
const pinnedSessionId = providerContext!.providerThreads.find(
3131+
(providerThread) => providerThread.id === thread.activeProviderThreadId,
3132+
)?.providerSessionId;
3133+
const pinnedRuntime =
3134+
pinnedSessionId == null
3135+
? Option.none()
3136+
: yield* providerSessions.get(pinnedSessionId).pipe(mapDispatchError(command));
3137+
if (
3138+
providerContext!.providerSessions.some(
3139+
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
3140+
) ||
3141+
(Option.isSome(pinnedRuntime) &&
3142+
!pinnedRuntime.value.providerSession.capabilities.sessions
3143+
.supportsRuntimeModeSwitchInSession)
3144+
) {
3145+
return yield* new OrchestratorDispatchError({
3146+
commandId: command.commandId,
3147+
commandType: command.type,
3148+
cause: "Keep this thread before using this operation.",
3149+
});
3150+
}
3151+
}
31233152
const providerSwitchPlan =
31243153
command.type === "thread.model-selection.set" || command.type === "provider.switch"
31253154
? yield* Effect.gen(function* () {

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CommandId,
77
CodexSettings,
88
EnvironmentId,
9+
EventId,
910
MessageId,
1011
type ModelSelection,
1112
type OrchestrationV2AppThread,
@@ -940,9 +941,16 @@ it.effect.each([
940941
family: (threadId) => Effect.succeed([threadId]),
941942
});
942943
const registryLayer = Layer.unwrap(
943-
makeProviderAdapter(state, { driver: ProviderDriverKind.make(driver) }).pipe(
944-
Effect.map((adapter) => ProviderAdapterRegistry.layerFromAdapters([adapter])),
945-
),
944+
makeProviderAdapter(state, {
945+
driver: ProviderDriverKind.make(driver),
946+
capabilities: {
947+
...CodexCapabilities,
948+
sessions: {
949+
...CodexCapabilities.sessions,
950+
supportsRuntimeModeSwitchInSession: driver === "codex",
951+
},
952+
},
953+
}).pipe(Effect.map((adapter) => ProviderAdapterRegistry.layerFromAdapters([adapter]))),
946954
);
947955
const engineLayer = ProviderReplayHarness.layerWithRegistry(
948956
{ name: "temporary-session-allocation" },
@@ -952,6 +960,7 @@ it.effect.each([
952960
yield* Effect.gen(function* () {
953961
const orchestrator = yield* Orchestrator.OrchestratorV2;
954962
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
963+
const eventSink = yield* EventSink.EventSinkV2;
955964
const sessions: Array<ProviderAdapter.ProviderAdapterV2SessionRuntime> = [];
956965
const selection = {
957966
...modelSelection,
@@ -1006,6 +1015,48 @@ it.effect.each([
10061015
}),
10071016
sessions[index],
10081017
);
1018+
if (index === 0 && temporary) {
1019+
const providerThread = projection.providerThreads[0]!;
1020+
yield* eventSink.write({
1021+
events: [
1022+
{
1023+
id: EventId.make("allocation-native-thread"),
1024+
type: "provider-thread.updated",
1025+
threadId,
1026+
occurredAt: yield* DateTime.now,
1027+
payload: {
1028+
...providerThread,
1029+
nativeThreadRef: {
1030+
driver: ProviderDriverKind.make(driver),
1031+
nativeId: "native-mode",
1032+
strength: "strong",
1033+
},
1034+
},
1035+
},
1036+
],
1037+
});
1038+
yield* orchestrator.dispatch({
1039+
type: "provider-session.detach",
1040+
commandId: CommandId.make("allocation-detach-session"),
1041+
threadId,
1042+
providerSessionId,
1043+
});
1044+
yield* manager.detach({ providerSessionId, threadId });
1045+
const changed = yield* orchestrator
1046+
.dispatch({
1047+
type: "thread.runtime-mode.set",
1048+
threadId,
1049+
commandId: CommandId.make("allocation-change-detached-mode"),
1050+
runtimeMode: "approval-required",
1051+
})
1052+
.pipe(Effect.exit);
1053+
assert.equal(Exit.isFailure(changed), driver !== "codex");
1054+
assert.equal(
1055+
(yield* orchestrator.getThreadRecords(threadId, [])).thread.runtimeMode,
1056+
driver === "codex" ? "approval-required" : "full-access",
1057+
);
1058+
assert.isTrue(yield* manager.isLive(providerSessionId));
1059+
}
10091060
}
10101061
const expectedOpens = firstTemporary || secondTemporary ? 2 : 1;
10111062
assert.equal((yield* Ref.get(state)).openCount, expectedOpens);

0 commit comments

Comments
 (0)