Skip to content

Commit 929ec41

Browse files
committed
fix(gateway): aggregate typing previews across same-actor connections
A boolean-only session.typing update from a second connection of the same actor (another tab or device) erased their live draft preview, because typing liveness aggregated per actor while the broadcast preview came only from the latest request. Preview aggregation now lives with the connection aggregation owner: updateTypingConnections tracks per-connection previews and returns the newest non-empty preview among live connections, so the broadcast keeps the active draft until its connection stops or expires. Regression fails pre-fix (event lost its preview field).
1 parent 72e5d28 commit 929ec41

4 files changed

Lines changed: 178 additions & 11 deletions

File tree

‎src/gateway/server-methods/session-typing-state.test.ts‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import { broadcastTypingThrottled, clearSessionTypingState } from "./session-typing-state.js";
2+
import {
3+
broadcastTypingThrottled,
4+
clearSessionTypingState,
5+
updateTypingConnections,
6+
} from "./session-typing-state.js";
37

48
beforeEach(() => {
59
vi.useFakeTimers();
@@ -12,6 +16,109 @@ afterEach(() => {
1216
vi.useRealTimers();
1317
});
1418

19+
describe("session typing connection state", () => {
20+
it("retains the newest live preview across an actor's connections", () => {
21+
const key = "shared-preview";
22+
23+
expect(
24+
updateTypingConnections({
25+
key,
26+
connectionId: "preview-tab",
27+
typing: true,
28+
preview: "first draft",
29+
now: 10_000,
30+
}),
31+
).toEqual({ typing: true, preview: "first draft" });
32+
expect(
33+
updateTypingConnections({
34+
key,
35+
connectionId: "presence-tab",
36+
typing: true,
37+
now: 10_100,
38+
}),
39+
).toEqual({ typing: true, preview: "first draft" });
40+
expect(
41+
updateTypingConnections({
42+
key,
43+
connectionId: "presence-tab",
44+
typing: true,
45+
preview: "newer draft",
46+
now: 10_200,
47+
}),
48+
).toEqual({ typing: true, preview: "newer draft" });
49+
});
50+
51+
it("removes a stopped connection's preview while preserving another connection's liveness", () => {
52+
const key = "stopped-preview";
53+
54+
updateTypingConnections({
55+
key,
56+
connectionId: "preview-tab",
57+
typing: true,
58+
preview: "draft",
59+
now: 10_000,
60+
});
61+
updateTypingConnections({
62+
key,
63+
connectionId: "presence-tab",
64+
typing: true,
65+
now: 10_100,
66+
});
67+
68+
expect(
69+
updateTypingConnections({
70+
key,
71+
connectionId: "preview-tab",
72+
typing: false,
73+
now: 10_200,
74+
}),
75+
).toEqual({ typing: true });
76+
expect(
77+
updateTypingConnections({
78+
key,
79+
connectionId: "presence-tab",
80+
typing: false,
81+
now: 10_300,
82+
}),
83+
).toEqual({ typing: false });
84+
expect(
85+
updateTypingConnections({
86+
key,
87+
connectionId: "missing-tab",
88+
typing: false,
89+
now: 10_400,
90+
}),
91+
).toEqual({ typing: false });
92+
});
93+
94+
it("expires stale previews while a refreshed boolean-only connection remains live", () => {
95+
const key = "expired-preview";
96+
97+
updateTypingConnections({
98+
key,
99+
connectionId: "preview-tab",
100+
typing: true,
101+
preview: "expired draft",
102+
now: 10_000,
103+
});
104+
updateTypingConnections({
105+
key,
106+
connectionId: "presence-tab",
107+
typing: true,
108+
now: 11_000,
109+
});
110+
111+
expect(
112+
updateTypingConnections({
113+
key,
114+
connectionId: "presence-tab",
115+
typing: true,
116+
now: 12_500,
117+
}),
118+
).toEqual({ typing: true });
119+
});
120+
});
121+
15122
describe("session typing broadcast throttle", () => {
16123
it("broadcasts a changed preview while the actor remains typing", () => {
17124
const emit = vi.fn(() => true);

‎src/gateway/server-methods/session-typing-state.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ type TypingBroadcastState = {
1919
pending?: PendingTypingBroadcast;
2020
timer?: ReturnType<typeof setTimeout>;
2121
};
22+
type TypingConnectionState = { updatedAt: number; preview?: string };
2223

2324
type SessionTypingState = {
2425
broadcasts: Map<string, TypingBroadcastState>;
25-
connections: Map<string, Map<string, number>>;
26+
connections: Map<string, Map<string, TypingConnectionState>>;
2627
};
2728

2829
function clearSessionTypingStateValue(state: SessionTypingState): void {
@@ -157,30 +158,40 @@ export function updateTypingConnections(params: {
157158
key: string;
158159
connectionId: string;
159160
typing: boolean;
161+
preview?: string;
160162
now: number;
161-
}): boolean {
163+
}): { typing: boolean; preview?: string } {
162164
for (const [typingKey, activeConnections] of typingConnections) {
163-
for (const [connectionId, updatedAt] of activeConnections) {
164-
if (params.now - updatedAt >= TYPING_ACTIVE_TTL_MS) {
165+
for (const [connectionId, connection] of activeConnections) {
166+
if (params.now - connection.updatedAt >= TYPING_ACTIVE_TTL_MS) {
165167
activeConnections.delete(connectionId);
166168
}
167169
}
168170
if (activeConnections.size === 0) {
169171
typingConnections.delete(typingKey);
170172
}
171173
}
172-
const connections = typingConnections.get(params.key) ?? new Map<string, number>();
174+
const connections = typingConnections.get(params.key) ?? new Map<string, TypingConnectionState>();
173175
if (params.typing) {
174-
connections.set(params.connectionId, params.now);
176+
connections.set(params.connectionId, {
177+
updatedAt: params.now,
178+
...(params.preview ? { preview: params.preview } : {}),
179+
});
175180
} else {
176181
connections.delete(params.connectionId);
177182
}
178183
if (connections.size === 0) {
179184
typingConnections.delete(params.key);
180-
return false;
185+
return { typing: false };
181186
}
182187
typingConnections.delete(params.key);
183188
typingConnections.set(params.key, connections);
184189
pruneMapToMaxSize(typingConnections, MAX_TYPING_THROTTLE_KEYS);
185-
return true;
190+
let latestPreview: TypingConnectionState | undefined;
191+
for (const connection of connections.values()) {
192+
if (connection.preview && (!latestPreview || connection.updatedAt >= latestPreview.updatedAt)) {
193+
latestPreview = connection;
194+
}
195+
}
196+
return { typing: true, ...(latestPreview?.preview ? { preview: latestPreview.preview } : {}) };
186197
}

‎src/gateway/server-methods/sessions-suggestions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,17 @@ export const sessionSuggestionHandlers: GatewayRequestHandlers = {
583583
]);
584584
const now = Date.now();
585585
const typingKey = `${actor.id}\0${target.agentId}\0${target.canonicalKey}\0${target.entry.sessionId}`;
586-
const effectiveTyping = updateTypingConnections({
586+
const { typing: effectiveTyping, preview } = updateTypingConnections({
587587
key: typingKey,
588588
connectionId: client?.connId ?? actor.id,
589589
typing: params.typing,
590+
...(params.typing && params.preview ? { preview: params.preview } : {}),
590591
now,
591592
});
592593
if (!params.typing && effectiveTyping) {
593594
respond(true, { ok: true, broadcast: false });
594595
return;
595596
}
596-
const preview = effectiveTyping ? params.preview || undefined : undefined;
597597
const broadcast = broadcastTypingThrottled({
598598
key: typingKey,
599599
typing: effectiveTyping,

‎src/gateway/server-methods/sessions-typing.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,55 @@ describe("session typing handler", () => {
142142
});
143143
});
144144

145+
it("keeps a live draft preview when another connection sends boolean-only typing", async () => {
146+
await withOpenClawTestState({ scenario: "minimal" }, async () => {
147+
vi.useFakeTimers();
148+
vi.setSystemTime(40_000);
149+
const sessionKey = "agent:main:shared-preview";
150+
await upsertSessionEntryCore(
151+
{ agentId: "main", sessionKey },
152+
{
153+
sessionId: "session-shared-preview",
154+
updatedAt: 1,
155+
createdActor: { type: "human", id: "owner" },
156+
visibility: "shared",
157+
},
158+
);
159+
mocks.presence = [
160+
{ user: { id: "alice" }, watchedSessions: [sessionKey] },
161+
{ user: { id: "owner" }, watchedSessions: [sessionKey] },
162+
];
163+
const broadcast = vi.fn();
164+
const params = {
165+
sessionKey,
166+
sessionId: "session-shared-preview",
167+
context: context(broadcast),
168+
};
169+
170+
expect(
171+
await callTyping({
172+
...params,
173+
typing: true,
174+
preview: "still drafting",
175+
client: client("alice", "alice-preview-tab"),
176+
}),
177+
).toEqual({ ok: true, broadcast: true });
178+
await vi.advanceTimersByTimeAsync(1_000);
179+
expect(
180+
await callTyping({
181+
...params,
182+
typing: true,
183+
client: client("alice", "alice-presence-tab"),
184+
}),
185+
).toEqual({ ok: true, broadcast: true });
186+
187+
expect(broadcast.mock.calls[1]?.[1]).toMatchObject({
188+
typing: true,
189+
preview: "still drafting",
190+
});
191+
});
192+
});
193+
145194
it.each([
146195
{ agentId: "main", expected: ["agent:main:global"] },
147196
{ agentId: "work", expected: ["agent:work:global"] },

0 commit comments

Comments
 (0)