Skip to content

Commit 936b3d1

Browse files
committed
fix(gateway): order marker-only transcript updates correctly
1 parent dfdcb7d commit 936b3d1

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎src/gateway/server-session-events.test.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ const sessionRow = vi.hoisted(() => ({
1616
const resolveEmbeddedAgentRunProgressStateMock = vi.hoisted(() => vi.fn());
1717
const loadGatewaySessionRowMock = vi.hoisted(() => vi.fn());
1818
const projectChatDisplayMessageMock = vi.hoisted(() => vi.fn((message: unknown) => message));
19+
const listAccessorSessionEntriesReadOnlyMock = vi.hoisted(() => vi.fn());
1920
const loadAccessorSessionEntryReadOnlyMock = vi.hoisted(() => vi.fn());
2021
const loadGatewaySessionEntryReadOnlyMock = vi.hoisted(() => vi.fn());
2122
const readSessionMessageCountAsyncMock = vi.hoisted(() => vi.fn());
23+
const resolveTranscriptSessionKeyBySessionIdMock = vi.hoisted(() => vi.fn());
2224
const runtimeConfigState = vi.hoisted(() => ({ value: {} as Record<string, unknown> }));
2325

2426
vi.mock("../config/io.js", () => ({ getRuntimeConfig: () => runtimeConfigState.value }));
2527
vi.mock("../config/sessions/session-accessor.js", async (importOriginal) => {
2628
const actual = await importOriginal<typeof import("../config/sessions/session-accessor.js")>();
2729
return {
2830
...actual,
31+
listSessionEntriesReadOnly: listAccessorSessionEntriesReadOnlyMock,
2932
loadSessionEntryReadOnly: loadAccessorSessionEntryReadOnlyMock,
33+
resolveTranscriptSessionKeyBySessionId: resolveTranscriptSessionKeyBySessionIdMock,
3034
};
3135
});
3236
vi.mock("./chat-display-projection.js", () => ({
@@ -110,10 +114,12 @@ describe("createTranscriptUpdateBroadcastHandler", () => {
110114
beforeEach(() => {
111115
vi.clearAllMocks();
112116
resolveEmbeddedAgentRunProgressStateMock.mockReturnValue(undefined);
117+
listAccessorSessionEntriesReadOnlyMock.mockReturnValue([]);
113118
loadAccessorSessionEntryReadOnlyMock.mockReturnValue(undefined);
114119
loadGatewaySessionEntryReadOnlyMock.mockReturnValue({ entry: undefined, storePath: "" });
115120
loadGatewaySessionRowMock.mockReturnValue(sessionRow);
116121
readSessionMessageCountAsyncMock.mockResolvedValue(undefined);
122+
resolveTranscriptSessionKeyBySessionIdMock.mockReturnValue(undefined);
117123
loadGatewaySessionRowMock.mockReturnValue(sessionRow);
118124
runtimeConfigState.value = {};
119125
sessionRow.key = "agent:main:main";
@@ -735,6 +741,13 @@ describe("createTranscriptUpdateBroadcastHandler", () => {
735741
secondSessionKey: "agent:main:global",
736742
agentId: "main",
737743
},
744+
{
745+
name: "a marker-only update and its canonical transcript key",
746+
firstSessionKey: "agent:main:main",
747+
secondSessionKey: "agent:main:main",
748+
agentId: "main",
749+
markerOnly: true,
750+
},
738751
{
739752
name: "an ownerless global update in its configured fixed store",
740753
firstSessionKey: "global",
@@ -759,17 +772,27 @@ describe("createTranscriptUpdateBroadcastHandler", () => {
759772
}),
760773
);
761774
loadAccessorSessionEntryReadOnlyMock.mockReturnValue({ sessionId: "sess-main" });
775+
if (scenario.markerOnly) {
776+
listAccessorSessionEntriesReadOnlyMock.mockReturnValue([
777+
{ key: scenario.firstSessionKey, entry: { sessionId: "sess-main" } },
778+
]);
779+
resolveTranscriptSessionKeyBySessionIdMock.mockReturnValue(scenario.firstSessionKey);
780+
}
762781
const { broadcastToConnIds, handler } = createHandler(false);
763782

764783
const firstTask = handler({
765784
message: { role: "assistant", content: [{ type: "text", text: "first" }] },
766785
messageId: "ordered-1",
767-
target: {
768-
agentId: scenario.agentId,
769-
sessionId: "sess-main",
770-
sessionKey: scenario.firstSessionKey,
771-
storePath: "/tmp/explicit-sessions.json",
772-
},
786+
...(scenario.markerOnly
787+
? { sessionFile: `sqlite:${scenario.agentId}:sess-main:/tmp/explicit-sessions.json` }
788+
: {
789+
target: {
790+
agentId: scenario.agentId,
791+
sessionId: "sess-main",
792+
sessionKey: scenario.firstSessionKey,
793+
storePath: "/tmp/explicit-sessions.json",
794+
},
795+
}),
773796
});
774797
await vi.waitFor(() => expect(readSessionMessageCountAsyncMock).toHaveBeenCalledOnce());
775798
const secondTask = handler({

‎src/gateway/server-session-events.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ export function createTranscriptUpdateBroadcastHandler(params: {
152152
? readTranscriptUpdateLifecycleOwner(update)?.lifecycleRevision
153153
: undefined);
154154
const queuedUpdate = lifecycleRevision ? { ...update, lifecycleRevision } : update;
155+
const legacyMarker = parseSqliteSessionFileMarker(update.sessionFile);
155156
const sessionKey =
156157
normalizeOptionalString(update.target?.sessionKey) ??
157-
normalizeOptionalString(update.sessionKey);
158+
normalizeOptionalString(update.sessionKey) ??
159+
(legacyMarker ? resolveTranscriptSessionKeyBySessionId(legacyMarker) : undefined);
158160
let agentId =
159161
normalizeOptionalString(update.target?.agentId) ??
160162
normalizeOptionalString(update.agentId) ??
161-
parseSqliteSessionFileMarker(update.sessionFile)?.agentId;
163+
legacyMarker?.agentId;
162164
if (!agentId && sessionKey?.toLowerCase() === "global") {
163165
const config = getRuntimeConfig();
164166
const persistedOwner = resolvePersistedSessionStoreOwnerForKey(config, sessionKey);

0 commit comments

Comments
 (0)