Skip to content

Commit 39137e4

Browse files
committed
fix(opencode): handle reordered model events
1 parent b06a61a commit 39137e4

2 files changed

Lines changed: 125 additions & 34 deletions

File tree

‎apps/server/src/provider/Layers/OpenCodeAdapter.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,74 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
11421142
}),
11431143
);
11441144

1145+
it.effect("captures a stored response model when message metadata arrives after idle", () =>
1146+
Effect.gen(function* () {
1147+
const adapter = yield* OpenCodeAdapter;
1148+
const threadId = asThreadId("thread-opencode-stored-actual-model");
1149+
let releaseEvents: (() => void) | undefined;
1150+
runtimeMock.state.subscribedEventGate = new Promise<void>((resolve) => {
1151+
releaseEvents = resolve;
1152+
});
1153+
runtimeMock.state.subscribedEvents = [
1154+
{
1155+
type: "message.part.updated",
1156+
properties: {
1157+
sessionID: "http://127.0.0.1:9999/session",
1158+
part: {
1159+
id: "part-stored-step-finish",
1160+
sessionID: "http://127.0.0.1:9999/session",
1161+
messageID: "msg-stored-actual-model",
1162+
type: "step-finish",
1163+
reason: "stop",
1164+
modelID: "gpt-5.6-luna",
1165+
cost: 0,
1166+
tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } },
1167+
},
1168+
},
1169+
},
1170+
{
1171+
type: "session.status",
1172+
properties: {
1173+
sessionID: "http://127.0.0.1:9999/session",
1174+
status: { type: "idle" },
1175+
},
1176+
},
1177+
{
1178+
type: "message.updated",
1179+
properties: {
1180+
sessionID: "http://127.0.0.1:9999/session",
1181+
info: { id: "msg-stored-actual-model", role: "assistant" },
1182+
},
1183+
},
1184+
];
1185+
1186+
const eventsFiber = yield* adapter.streamEvents.pipe(
1187+
Stream.filter((event) => event.threadId === threadId),
1188+
Stream.take(5),
1189+
Stream.runCollect,
1190+
Effect.forkChild,
1191+
);
1192+
1193+
yield* adapter.startSession({
1194+
provider: ProviderDriverKind.make("opencode"),
1195+
threadId,
1196+
runtimeMode: "full-access",
1197+
});
1198+
yield* adapter.sendTurn({
1199+
threadId,
1200+
input: "identify the stored model",
1201+
modelSelection: createModelSelection(ProviderInstanceId.make("opencode"), "kedvai/auto"),
1202+
});
1203+
releaseEvents?.();
1204+
1205+
const events = Array.from(yield* Fiber.join(eventsFiber));
1206+
const completions = events.filter((event) => event.type === "turn.completed");
1207+
NodeAssert.equal(completions.length, 2);
1208+
NodeAssert.equal(completions[0]?.payload.actualModel, undefined);
1209+
NodeAssert.equal(completions[1]?.payload.actualModel, "gpt-5.6-luna");
1210+
}),
1211+
);
1212+
11451213
it.effect("does not strip coincidental prefix overlap from OpenCode part deltas", () =>
11461214
Effect.gen(function* () {
11471215
const adapter = yield* OpenCodeAdapter;

‎apps/server/src/provider/Layers/OpenCodeAdapter.ts‎

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,14 @@ interface OpenCodeSessionContext {
221221
readonly pendingPermissions: Map<string, PermissionRequest>;
222222
readonly pendingQuestions: Map<string, QuestionRequest>;
223223
readonly messageRoleById: Map<string, "user" | "assistant">;
224-
readonly assistantTurnIdByMessageId: Map<string, TurnId>;
224+
readonly turnIdByMessageId: Map<string, TurnId>;
225225
readonly partById: Map<string, Part>;
226226
readonly emittedTextByPartId: Map<string, string>;
227227
readonly completedAssistantPartIds: Set<string>;
228228
readonly turns: Array<OpenCodeTurnSnapshot>;
229229
activeTurnId: TurnId | undefined;
230230
activeActualModel: string | undefined;
231-
completedTurnWithoutModel:
232-
| { readonly turnId: TurnId; readonly state: "completed" | "failed" }
233-
| undefined;
231+
readonly completedTurnsWithoutModel: Map<TurnId, "completed" | "failed">;
234232
activeAgent: string | undefined;
235233
activeVariant: string | undefined;
236234
/**
@@ -795,6 +793,39 @@ export function makeOpenCodeAdapter(
795793
}
796794
});
797795

796+
const captureActualModel = Effect.fn("captureActualModel")(function* (
797+
context: OpenCodeSessionContext,
798+
part: Part,
799+
raw: unknown,
800+
) {
801+
const actualModel = actualModelFromPart(part);
802+
const partTurnId = context.turnIdByMessageId.get(part.messageID);
803+
if (!actualModel || !partTurnId) {
804+
return;
805+
}
806+
if (context.activeTurnId === partTurnId) {
807+
context.activeActualModel = actualModel;
808+
return;
809+
}
810+
const completedState = context.completedTurnsWithoutModel.get(partTurnId);
811+
if (!completedState) {
812+
return;
813+
}
814+
context.completedTurnsWithoutModel.delete(partTurnId);
815+
yield* emit({
816+
...(yield* buildEventBase({
817+
threadId: context.session.threadId,
818+
turnId: partTurnId,
819+
raw,
820+
})),
821+
type: "turn.completed",
822+
payload: {
823+
state: completedState,
824+
actualModel,
825+
},
826+
});
827+
});
828+
798829
const handleSubscribedEvent = Effect.fn("handleSubscribedEvent")(function* (
799830
context: OpenCodeSessionContext,
800831
event: OpenCodeSubscribedEvent,
@@ -842,12 +873,13 @@ export function makeOpenCodeAdapter(
842873
context.messageRoleById.set(event.properties.info.id, event.properties.info.role);
843874
if (event.properties.info.role === "assistant") {
844875
if (turnId) {
845-
context.assistantTurnIdByMessageId.set(event.properties.info.id, turnId);
876+
context.turnIdByMessageId.set(event.properties.info.id, turnId);
846877
}
847878
for (const part of context.partById.values()) {
848879
if (part.messageID !== event.properties.info.id) {
849880
continue;
850881
}
882+
yield* captureActualModel(context, part, event);
851883
yield* emitAssistantTextDelta(context, part, turnId, event);
852884
}
853885
}
@@ -856,7 +888,7 @@ export function makeOpenCodeAdapter(
856888

857889
case "message.removed": {
858890
context.messageRoleById.delete(event.properties.messageID);
859-
context.assistantTurnIdByMessageId.delete(event.properties.messageID);
891+
context.turnIdByMessageId.delete(event.properties.messageID);
860892
break;
861893
}
862894

@@ -908,29 +940,13 @@ export function makeOpenCodeAdapter(
908940
case "message.part.updated": {
909941
const part = event.properties.part;
910942
context.partById.set(part.id, part);
943+
if (turnId && !context.turnIdByMessageId.has(part.messageID)) {
944+
context.turnIdByMessageId.set(part.messageID, turnId);
945+
}
911946
const messageRole = messageRoleForPart(context, part);
912947

913-
const actualModel = actualModelFromPart(part);
914-
const partTurnId = context.assistantTurnIdByMessageId.get(part.messageID);
915-
if (messageRole === "assistant" && partTurnId && actualModel) {
916-
if (context.activeTurnId === partTurnId) {
917-
context.activeActualModel = actualModel;
918-
} else if (context.completedTurnWithoutModel?.turnId === partTurnId) {
919-
const completedTurn = context.completedTurnWithoutModel;
920-
context.completedTurnWithoutModel = undefined;
921-
yield* emit({
922-
...(yield* buildEventBase({
923-
threadId: context.session.threadId,
924-
turnId: completedTurn.turnId,
925-
raw: event,
926-
})),
927-
type: "turn.completed",
928-
payload: {
929-
state: completedTurn.state,
930-
actualModel,
931-
},
932-
});
933-
}
948+
if (messageRole === "assistant") {
949+
yield* captureActualModel(context, part, event);
934950
}
935951

936952
if (messageRole === "assistant") {
@@ -1100,9 +1116,11 @@ export function makeOpenCodeAdapter(
11001116
const actualModel = context.activeActualModel;
11011117
context.activeTurnId = undefined;
11021118
context.activeActualModel = undefined;
1103-
context.completedTurnWithoutModel = actualModel
1104-
? undefined
1105-
: { turnId, state: "completed" };
1119+
if (actualModel) {
1120+
context.completedTurnsWithoutModel.delete(turnId);
1121+
} else {
1122+
context.completedTurnsWithoutModel.set(turnId, "completed");
1123+
}
11061124
yield* updateProviderSession(context, { status: "ready" }, { clearActiveTurnId: true });
11071125
yield* emit({
11081126
...(yield* buildEventBase({
@@ -1126,8 +1144,13 @@ export function makeOpenCodeAdapter(
11261144
const actualModel = context.activeActualModel;
11271145
context.activeTurnId = undefined;
11281146
context.activeActualModel = undefined;
1129-
context.completedTurnWithoutModel =
1130-
activeTurnId && !actualModel ? { turnId: activeTurnId, state: "failed" } : undefined;
1147+
if (activeTurnId) {
1148+
if (actualModel) {
1149+
context.completedTurnsWithoutModel.delete(activeTurnId);
1150+
} else {
1151+
context.completedTurnsWithoutModel.set(activeTurnId, "failed");
1152+
}
1153+
}
11311154
yield* updateProviderSession(
11321155
context,
11331156
{
@@ -1430,12 +1453,12 @@ export function makeOpenCodeAdapter(
14301453
partById: new Map(),
14311454
emittedTextByPartId: new Map(),
14321455
messageRoleById: new Map(),
1433-
assistantTurnIdByMessageId: new Map(),
1456+
turnIdByMessageId: new Map(),
14341457
completedAssistantPartIds: new Set(),
14351458
turns: [],
14361459
activeTurnId: undefined,
14371460
activeActualModel: undefined,
1438-
completedTurnWithoutModel: undefined,
1461+
completedTurnsWithoutModel: new Map(),
14391462
activeAgent: undefined,
14401463
activeVariant: undefined,
14411464
stopped: yield* Ref.make(false),

0 commit comments

Comments
 (0)