Skip to content

Commit c04bee4

Browse files
authored
fix(ui): resolve collaboration state through the selected session (#133134)
1 parent f1d2c5b commit c04bee4

3 files changed

Lines changed: 65 additions & 14 deletions

File tree

‎ui/src/pages/chat/chat-pane-sharing.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* @vitest-environment-options {"url":"http://chat-pane-sharing.test/"} */
33

44
import { describe, expect, it, vi } from "vitest";
5+
import type { SessionSuggestion } from "../../../../packages/gateway-protocol/src/index.js";
56
import { createDeferred } from "../../../../test/helpers/promise.js";
67
import type { GatewayBrowserClient } from "../../api/gateway.ts";
78
import type {
@@ -152,6 +153,48 @@ const mutations = [
152153
] as const;
153154

154155
describe("chat pane sharing authorization", () => {
156+
it.each(["draft", "shared"] as const)(
157+
"loads selected-global Work suggestions after visibility changes to %s",
158+
async (visibility) => {
159+
const pending: SessionSuggestion = {
160+
id: `pending-${visibility}`,
161+
sessionKey: "agent:work:main",
162+
agentId: "work",
163+
author: { type: "human", id: "alice", label: "Alice" },
164+
text: "still needs review",
165+
createdAt: 1,
166+
state: "pending",
167+
};
168+
const request = vi.fn(async () => ({ suggestions: [pending], role: "owner" as const }));
169+
const { pane, state } = createTestChatPane({
170+
client: createGatewayBrowserClientFixture({ request }),
171+
sessions: {} as SessionCapability,
172+
});
173+
state.sessionKey = "agent:work:main";
174+
state.assistantAgentId = "work";
175+
state.agentsList = { defaultId: "main", mainKey: "main", scope: "global", agents: [] };
176+
state.sessionsResultAgentId = "work";
177+
state.sessionsResult = sharingSessionsResult({
178+
...sessionRow(),
179+
key: "global",
180+
kind: "global",
181+
visibility,
182+
});
183+
pane.presencePayload = {
184+
presence: [{ user: { id: "owner" } }, { user: { id: "alice" } }],
185+
};
186+
187+
await pane.refreshSessionSuggestions();
188+
189+
expect(request).toHaveBeenCalledWith("session.suggestions.list", {
190+
sessionKey: "agent:work:main",
191+
agentId: "work",
192+
});
193+
expect(pane.sessionSuggestions).toEqual([pending]);
194+
expect(pane.sessionSuggestionRole).toBe("owner");
195+
},
196+
);
197+
155198
it("allows read-scoped owners to load sharing data but not mutate it", async () => {
156199
const row = sessionRow();
157200
const request = vi.fn(async (method: string) => {

‎ui/src/pages/chat/chat-pane-sharing.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
type ChatPaneConnectionScope,
3030
} from "./chat-pane-shared.ts";
3131
import { resetSessionCompanion } from "./chat-session-companion.ts";
32-
import { resolveChatAgentId } from "./chat-state-route.ts";
32+
import { resolveChatAgentId, selectedChatSessionRow } from "./chat-state-route.ts";
3333
import { clearTypingActorForSessionMessage } from "./chat-typing-presence.ts";
3434
import {
3535
canManageChatSessionSharing,
@@ -388,9 +388,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
388388
protected async loadSessionSuggestions(requestVersion: number): Promise<void> {
389389
const targetSignature = this.sessionSuggestionTargetSignature;
390390
const scope = this.captureConnectionScope();
391-
const row = scope?.state.sessionsResult?.sessions.find((candidate) =>
392-
areUiSessionKeysEquivalent(candidate.key, scope.state.sessionKey),
393-
);
391+
const row = scope ? selectedChatSessionRow(scope.state) : undefined;
394392
// Solo dormancy intentionally hides persisted rows too; when a second identity
395393
// returns, the presence transition below triggers a fresh authoritative list.
396394
if (
@@ -629,9 +627,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
629627
protected handleSessionTypingEvent(event: SessionTypingEvent): void {
630628
const selfId = this.context.gateway.snapshot.selfUser?.id;
631629
const state = this.state;
632-
const selectedSession = state?.sessionsResult?.sessions.find((row) =>
633-
areUiSessionKeysEquivalent(row.key, state.sessionKey),
634-
);
630+
const selectedSession = state ? selectedChatSessionRow(state) : undefined;
635631
if (
636632
!this.hasMultipleIdentities() ||
637633
event.actor.id === selfId ||
@@ -706,9 +702,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
706702
return;
707703
}
708704
const sessionKey = scope.state.sessionKey;
709-
const sessionId = scope.state.sessionsResult?.sessions.find((row) =>
710-
areUiSessionKeysEquivalent(row.key, sessionKey),
711-
)?.sessionId;
705+
const sessionId = selectedChatSessionRow(scope.state)?.sessionId;
712706
if (!sessionId) {
713707
return;
714708
}

‎ui/src/pages/chat/chat-pane-typing.test.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe("chat pane typing presence", () => {
2121
client: { request: vi.fn() } as unknown as GatewayBrowserClient,
2222
sessions: {} as SessionCapability,
2323
});
24+
state.sessionKey = "agent:work:main";
25+
state.assistantAgentId = "work";
26+
state.agentsList = { defaultId: "main", mainKey: "main", scope: "global", agents: [] };
27+
state.sessionsResultAgentId = "work";
2428
const aliceId = "0d9f4c35-d221-49da-9a3f-b8c73921066b";
2529
pane.presencePayload = {
2630
presence: [{ user: { id: "owner" } }, { user: { id: aliceId } }, { user: { id: "bob" } }],
@@ -30,8 +34,8 @@ describe("chat pane typing presence", () => {
3034
path: "",
3135
sessions: [
3236
{
33-
key: state.sessionKey,
34-
kind: "direct",
37+
key: "global",
38+
kind: "global",
3539
sessionId: "session-a",
3640
updatedAt: 1,
3741
} as GatewaySessionRow,
@@ -130,17 +134,27 @@ describe("chat pane typing presence", () => {
130134
sessions: {} as SessionCapability,
131135
});
132136
pane.presencePayload = { presence: [{ user: { id: "owner" } }, { user: { id: "alice" } }] };
137+
state.sessionKey = "agent:work:main";
138+
state.assistantAgentId = "work";
139+
state.agentsList = { defaultId: "main", mainKey: "main", scope: "global", agents: [] };
140+
state.sessionsResultAgentId = "work";
133141
state.sessionsResult = {
134142
count: 1,
135143
path: "",
136-
sessions: [{ key: state.sessionKey, kind: "direct", sessionId: "session-a", updatedAt: 1 }],
144+
sessions: [{ key: "global", kind: "global", sessionId: "session-a", updatedAt: 1 }],
137145
} as never;
138146

139147
pane.sendTypingState(true, ` prefix${"😀".repeat(300)} `);
140148
expect(request).toHaveBeenNthCalledWith(
141149
1,
142150
"session.typing",
143-
expect.objectContaining({ typing: true, preview: "😀".repeat(300) }),
151+
expect.objectContaining({
152+
sessionKey: "agent:work:main",
153+
sessionId: "session-a",
154+
agentId: "work",
155+
typing: true,
156+
preview: "😀".repeat(300),
157+
}),
144158
);
145159

146160
pane.sendTypingState(false, "must not leak");

0 commit comments

Comments
 (0)