Skip to content

Commit 05e9030

Browse files
authored
feat(ui): identify sending agents on forwarded messages (#133439)
* feat(ui): identify sending agents on forwarded messages Cross-agent forwarded messages now render the sending agent's avatar (via the existing agentId-keyed avatar cache and roster identity; falls back to hue-stable initials, never the shared logo) instead of the generic forward glyph, which same-agent forwards keep. Attribution labels follow the operator's rules: an agent's main-session source labels as the agent itself (From roboclaw), other sessions label as the session (titler-resolved), prefixed with the agent name only for cross-agent sources (From democlaw — bench). Main-session chips pre-title so the titler stamps only the href. A forwarded message also ends compact direct-thread rendering: the .chat-thread--direct CSS hides all avatars, so a thread gaining another voice now restores identity chrome, matching attributed multi-user threads. The forwarded-attribution row and user-turn entry animation move to concept-owned modules to keep chat-message-group under the line cap; the assertion baseline shrinks by the moved entry. * fix(ui): show the source agent's real avatar on same-agent forwards Forwarded rows now always render the sending agent's avatar: roster avatar for cross-agent sources, the current assistant identity for same-agent sources, initials fallback, and the forward glyph only for unresolvable legacy sources. Startup-JS baseline updated with reason. * refactor(ui): move reply-preview resolution into its own module chat-transcript-projection crossed the 700-line cap after rebasing onto main; the reply-preview concern (loaded-source index type, preview projection, memoized resolver) moves to chat-reply-preview.ts.
1 parent 0517fcb commit 05e9030

18 files changed

Lines changed: 732 additions & 244 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4056,7 +4056,6 @@ ui/src/pages/chat/components/chat-header-session-menu.ts 1
40564056
ui/src/pages/chat/components/chat-message-attachment-availability.ts 1
40574057
ui/src/pages/chat/components/chat-message-bubble.ts 2
40584058
ui/src/pages/chat/components/chat-message-confirmation.ts 2
4059-
ui/src/pages/chat/components/chat-message-group.ts 1
40604059
ui/src/pages/chat/components/chat-message-markdown.ts 3
40614060
ui/src/pages/chat/components/chat-message-media.ts 13
40624061
ui/src/pages/chat/components/chat-message-timestamp.ts 4
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"startupJsGzipBytes": 346185,
3-
"reason": "Per-session colors in the boot path (#132570): sidebar stripe render, color tokens, and menu wiring measured 346185 B, +579 B over the prior baseline and 3 B past its growth allowance; below the maintainer-approved 347323 B ceiling; hard cap and allowances unchanged",
4-
"updatedAt": "2026-08-29"
2+
"startupJsGzipBytes": 346777,
3+
"reason": "Sender-agent avatars on forwarded cross-session messages (PR #133439): agent-avatar resolution, attribution label rules, and direct-thread identity chrome measured 346777 B, +592 B over the prior baseline; below the 358400 B ceiling; hard cap and allowances unchanged",
4+
"updatedAt": "2026-08-30"
55
}

‎docs/web/control-ui.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ Absolute external `http(s)` embed URLs stay blocked by default. To let `[embed u
815815

816816
The chat transcript uses a centered readable frame aligned with the composer. Assistant and tool output stay left-aligned while your own messages stay right-aligned inside that frame. In multi-user sessions (for example a group chat relayed from a channel plugin), messages from other attributed participants render left-aligned with the author's avatar, name, and a stable per-identity color, so only the signed-in viewer's messages read as "mine". When two or more attributed participants are present, assistant replies carry a small "Replying to name" marker naming the participant whose message triggered the turn. System entries such as local slash-command output render as centered notice rows without an avatar.
817817

818-
Messages forwarded by `sessions_send` render as left-aligned speech bubbles with a forward icon and a source-session chip above the message. Select the chip to open the source session; hover it to see session progress. Each source session has a stable bubble tint. Forwarded messages without a known source session show the source agent when available, or a generic forwarded-message label. The receiving agent's own replies remain flat text.
818+
Messages forwarded by `sessions_send` render as left-aligned speech bubbles with a source-session chip above the message. When avatars are shown, messages from a different known agent use that agent's avatar, or initials in a stable identity color if no avatar is available. Same-agent forwards and unknown senders keep the forward icon. Select the chip to open the source session; hover it to see session progress. Each source session has a stable bubble tint. Forwarded messages without a known source session show the source agent when available, or a generic forwarded-message label. The receiving agent's own replies remain flat text.
819819

820820
## Chat message width
821821

‎ui/src/pages/chat/chat-avatar.test.ts‎

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import { render } from "lit";
44
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import { createDeferred } from "../../../../test/helpers/promise.js";
56
import { setAvatarGatewayOrigin } from "../../lib/identity-avatar-context.ts";
6-
import { invalidateChatAvatarCache, refreshChatAvatar, renderChatAvatar } from "./chat-avatar.ts";
7+
import {
8+
invalidateChatAvatarCache,
9+
refreshChatAvatar,
10+
refreshSenderAgentAvatars,
11+
renderChatAvatar,
12+
} from "./chat-avatar.ts";
713
import { makeChatHost } from "./chat-host.test-support.ts";
814

915
function renderAvatar(params: Parameters<typeof renderChatAvatar>) {
@@ -356,6 +362,133 @@ describe("refreshChatAvatar", () => {
356362
});
357363
});
358364

365+
describe("refreshSenderAgentAvatars", () => {
366+
function forwardedMessages(...agentIds: string[]) {
367+
return agentIds.map((agentId) => ({
368+
role: "assistant",
369+
content: "report",
370+
senderSession: { agentId },
371+
}));
372+
}
373+
function senderHost() {
374+
return {
375+
...makeChatHost({ sessionKey: "agent:main:main", settings: { token: "test-token" } }),
376+
agentsList: { defaultId: "main", agents: [{ id: "main" }, { id: "research" }] },
377+
senderAgentAvatars: undefined as ReadonlyMap<string, string | null> | undefined,
378+
};
379+
}
380+
381+
it("shares authenticated sender snapshots with the current-agent cache and skips unknown agents", async () => {
382+
const fetchMock = vi
383+
.spyOn(globalThis, "fetch")
384+
.mockResolvedValueOnce(new Response(JSON.stringify({ avatarUrl: "/avatar/research" })))
385+
.mockResolvedValueOnce(new Response(new Blob(["avatar"])));
386+
vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:research-avatar");
387+
const host = senderHost();
388+
389+
host.chatMessages = forwardedMessages("main", "unknown", "research", "research");
390+
await Promise.all([refreshSenderAgentAvatars(host), refreshSenderAgentAvatars(host)]);
391+
expect(host.senderAgentAvatars).toEqual(new Map([["research", "blob:research-avatar"]]));
392+
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
393+
"/avatar/research?meta=1",
394+
"/avatar/research",
395+
]);
396+
for (const [, init] of fetchMock.mock.calls) {
397+
expect(init?.headers).toEqual({ Authorization: "Bearer test-token" });
398+
}
399+
host.sessionKey = "agent:research:main";
400+
await refreshChatAvatar(host);
401+
expect(host.chatAvatarUrl).toBe("blob:research-avatar");
402+
expect(fetchMock).toHaveBeenCalledTimes(2);
403+
invalidateChatAvatarCache(host);
404+
expect(host.senderAgentAvatars).toBeUndefined();
405+
});
406+
407+
it("loads newly committed forwards and refreshes them after cache invalidation", async () => {
408+
const avatarUrl = "data:image/png;base64,YQ==";
409+
const fetchMock = vi
410+
.spyOn(globalThis, "fetch")
411+
.mockImplementation(async () => new Response(JSON.stringify({ avatarUrl })));
412+
const host = { ...senderHost(), requestUpdate: vi.fn() };
413+
await refreshSenderAgentAvatars(host);
414+
expect(fetchMock).not.toHaveBeenCalled();
415+
expect(host.requestUpdate).not.toHaveBeenCalled();
416+
417+
host.chatMessages = [
418+
{ role: "assistant", content: "report", senderSession: { agentId: "research" } },
419+
];
420+
await refreshSenderAgentAvatars(host);
421+
await vi.waitFor(() => expect(host.senderAgentAvatars?.get("research")).toBe(avatarUrl));
422+
await refreshSenderAgentAvatars(host);
423+
expect(fetchMock).toHaveBeenCalledOnce();
424+
expect(host.requestUpdate).toHaveBeenCalledOnce();
425+
426+
invalidateChatAvatarCache(host);
427+
await refreshSenderAgentAvatars(host);
428+
await vi.waitFor(() => expect(host.senderAgentAvatars?.get("research")).toBe(avatarUrl));
429+
expect(fetchMock).toHaveBeenCalledTimes(2);
430+
host.chatMessages = [];
431+
await refreshSenderAgentAvatars(host);
432+
expect(host.senderAgentAvatars?.size).toBe(0);
433+
invalidateChatAvatarCache(host);
434+
});
435+
436+
it.each(["session", "request", "connection", "roster", "invalidation"])(
437+
"does not publish a sender avatar after its %s changes",
438+
async (change) => {
439+
const response = createDeferred<Response>();
440+
vi.spyOn(globalThis, "fetch").mockReturnValue(response.promise);
441+
const host = senderHost();
442+
host.chatMessages = forwardedMessages("research");
443+
const pending = refreshSenderAgentAvatars(host);
444+
if (change === "session") {
445+
host.sessionKey = "agent:main:other";
446+
}
447+
if (change === "request") {
448+
host.chatMessages = [];
449+
await refreshSenderAgentAvatars(host);
450+
}
451+
if (change === "connection") {
452+
host.connectionEpoch += 1;
453+
}
454+
if (change === "roster") {
455+
host.agentsList = { defaultId: "main", agents: [{ id: "main" }] };
456+
}
457+
if (change === "invalidation") {
458+
invalidateChatAvatarCache(host);
459+
}
460+
response.resolve(new Response(JSON.stringify({ avatarUrl: "data:image/png;base64,YQ==" })));
461+
await pending;
462+
expect(host.senderAgentAvatars?.size ?? 0).toBe(0);
463+
},
464+
);
465+
466+
it("bounds sender loads without evicting the current agent's blob", async () => {
467+
const agents = Array.from({ length: 30 }, (_, i) => ({ id: `sender-${i}` }));
468+
const host = senderHost();
469+
host.agentsList.agents.push(...agents);
470+
const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => {
471+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
472+
return url.endsWith("?meta=1")
473+
? new Response(JSON.stringify({ avatarUrl: url.replace("?meta=1", "") }))
474+
: new Response(new Blob(["avatar"]));
475+
});
476+
let sequence = 0;
477+
vi.spyOn(URL, "createObjectURL").mockImplementation(() => `blob:avatar-${sequence++}`);
478+
const revoke = vi.spyOn(URL, "revokeObjectURL");
479+
await refreshChatAvatar(host);
480+
const currentAvatar = host.chatAvatarUrl;
481+
host.chatMessages = forwardedMessages(...agents.map((agent) => agent.id));
482+
await refreshSenderAgentAvatars(host);
483+
expect(host.senderAgentAvatars?.size).toBe(23);
484+
expect(fetchMock).toHaveBeenCalledTimes(48);
485+
expect(host.chatAvatarUrl).toBe(currentAvatar);
486+
expect(revoke).not.toHaveBeenCalled();
487+
invalidateChatAvatarCache(host);
488+
expect(revoke).toHaveBeenCalledTimes(24);
489+
});
490+
});
491+
359492
describe("attributed sender avatars", () => {
360493
it("restores pending initials when the authenticated sender avatar changes", async () => {
361494
setAvatarGatewayOrigin("https://gateway.example.test", "Bearer profile-token");

0 commit comments

Comments
 (0)