Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(web): keep picked mentions instead of silently dropping them

shoudi zheng57e5acdcf555
diff --git a/apps/web/src/features/rooms/chat/message-composer.test.tsx b/apps/web/src/features/rooms/chat/message-composer.test.tsx
index 1929f500..bfc83ee4 100644
--- a/apps/web/src/features/rooms/chat/message-composer.test.tsx
+++ b/apps/web/src/features/rooms/chat/message-composer.test.tsx
@@ -17,6 +17,7 @@ import {
formatSkillToken,
} from "./message-composer-references";
import { TEAMHUB_MESSAGE_MAX_LENGTH } from "./use-composer-draft";
+import { createTeamHubMessageContent } from "@/features/teamhub/teamhub-message-content";
describe("MessageComposer", () => {
it("remeasures wrapping after a width change without resubscribing on rerenders", () => {
@@ -1302,7 +1303,7 @@ describe("MessageComposer", () => {
});
});
- it("degrades unavailable restored references to plain text", () => {
+ it("blocks a restored draft that mentions an unavailable Agent", () => {
const onSend = vi.fn();
const staleActor = {
display_name: "Retired Agent",
@@ -1314,12 +1315,6 @@ describe("MessageComposer", () => {
<MessageComposer
agentOptionsReady
documentOptionsReady
- initialDocumentReferences={[
- {
- documentId: "document-deleted",
- token: "@Deleted_plan",
- },
- ]}
initialMentions={[staleActor]}
initialSkillReferences={[
{
@@ -1329,8 +1324,7 @@ describe("MessageComposer", () => {
token: "/retired-skill",
},
]}
- initialValue="@Retired_Agent /retired-skill inspect @Deleted_plan"
- onOpenDocument={vi.fn()}
+ initialValue="@Retired Agent /retired-skill inspect"
onOpenProfile={vi.fn()}
onSend={onSend}
/>,
@@ -1340,13 +1334,142 @@ describe("MessageComposer", () => {
expect(
screen.queryByRole("button", { name: /Retired Agent profile/i }),
).not.toBeInTheDocument();
+ fireEvent.keyDown(editor, { key: "Enter" });
+
+ expect(onSend).not.toHaveBeenCalled();
+ expect(editor.value).toBe("@Retired Agent /retired-skill inspect");
+ expect(
+ screen.getByText(
+ "@Retired Agent is not available in this Room. Remove the mention to send.",
+ ),
+ ).toBeVisible();
+ });
+
+ it("degrades unavailable restored Document references to plain text", () => {
+ const onSend = vi.fn();
+ render(
+ <MessageComposer
+ agentOptionsReady
+ documentOptionsReady
+ initialDocumentReferences={[
+ {
+ documentId: "document-deleted",
+ token: "@Deleted_plan",
+ },
+ ]}
+ initialValue="inspect @Deleted_plan"
+ onOpenDocument={vi.fn()}
+ onSend={onSend}
+ />,
+ );
+ const editor = screen.getByLabelText<HTMLTextAreaElement>("Message Room");
+
expect(
screen.queryByRole("button", { name: /Open Deleted plan/i }),
).not.toBeInTheDocument();
fireEvent.keyDown(editor, { key: "Enter" });
expect(onSend).toHaveBeenCalledWith({
- body: "@Retired_Agent /retired-skill inspect @Deleted_plan",
+ body: "inspect @Deleted_plan",
+ files: [],
+ mentions: [],
+ });
+ });
+
+ it("keeps a mention whose Agent is renamed after the pick", () => {
+ const onSend = vi.fn();
+ const picked = {
+ display_name: "Old Name",
+ id: "actor-renamed",
+ kind: "agent" as const,
+ tenant_id: "tenant-1",
+ };
+ const renamed = { ...picked, display_name: "New Name" };
+ const { rerender } = render(
+ <MessageComposer
+ agentOptionsReady
+ mentionOptions={[picked]}
+ onSend={onSend}
+ />,
+ );
+ const editor = screen.getByLabelText<HTMLTextAreaElement>("Message Room");
+ fireEvent.change(editor, { target: { selectionStart: 1, value: "@" } });
+ fireEvent.keyDown(editor, { key: "Enter" });
+ expect(editor.value).toBe("@Old Name ");
+
+ rerender(
+ <MessageComposer
+ agentOptionsReady
+ mentionOptions={[renamed]}
+ onSend={onSend}
+ />,
+ );
+ fireEvent.change(editor, { target: { value: "@Old Name review" } });
+ fireEvent.keyDown(editor, { key: "Enter" });
+
+ expect(onSend).toHaveBeenCalledWith({
+ body: "@Old Name review",
+ files: [],
+ mentions: [picked],
+ });
+ const sent = onSend.mock.calls[0]![0] as {
+ body: string;
+ mentions: (typeof picked)[];
+ };
+ expect(
+ createTeamHubMessageContent({
+ files: [],
+ mentions: sent.mentions,
+ text: sent.body,
+ }).segments,
+ ).toContainEqual(
+ expect.objectContaining({
+ target: picked.id,
+ type: "reference",
+ }),
+ );
+ });
+
+ it("blocks sending while a picked mention is unavailable until it is removed", () => {
+ const onSend = vi.fn();
+ const picked = {
+ display_name: "Retired Agent",
+ id: "actor-retired",
+ kind: "agent" as const,
+ tenant_id: "tenant-1",
+ };
+ const { rerender } = render(
+ <MessageComposer
+ agentOptionsReady
+ mentionOptions={[picked]}
+ onSend={onSend}
+ />,
+ );
+ const editor = screen.getByLabelText<HTMLTextAreaElement>("Message Room");
+ fireEvent.change(editor, { target: { selectionStart: 1, value: "@" } });
+ fireEvent.keyDown(editor, { key: "Enter" });
+ expect(editor.value).toBe("@Retired Agent ");
+
+ rerender(
+ <MessageComposer agentOptionsReady mentionOptions={[]} onSend={onSend} />,
+ );
+ const error =
+ "@Retired Agent is not available in this Room. Remove the mention to send.";
+ fireEvent.change(editor, { target: { value: "@Retired Agent hi" } });
+ fireEvent.keyDown(editor, { key: "Enter" });
+ expect(onSend).not.toHaveBeenCalled();
+ expect(screen.getByText(error)).toBeVisible();
+
+ fireEvent.change(editor, { target: { value: "@Retired Agent hi there" } });
+ expect(screen.queryByText(error)).not.toBeInTheDocument();
+ fireEvent.keyDown(editor, { key: "Enter" });
+ expect(onSend).not.toHaveBeenCalled();
+ expect(screen.getByText(error)).toBeVisible();
+
+ fireEvent.change(editor, { target: { value: "hi there" } });
+ fireEvent.keyDown(editor, { key: "Enter" });
+ expect(onSend).toHaveBeenCalledWith({
+ body: "hi there",
files: [],
mentions: [],
});
diff --git a/apps/web/src/features/rooms/chat/use-composer-draft.ts b/apps/web/src/features/rooms/chat/use-composer-draft.ts
index cbd72a7d..271c27c0 100644
--- a/apps/web/src/features/rooms/chat/use-composer-draft.ts
+++ b/apps/web/src/features/rooms/chat/use-composer-draft.ts
@@ -13,7 +13,7 @@ import {
type MessageDocumentOption,
type MessageSkillOption,
} from "./message-composer-references";
-import { activeMessageMentions, formatMentionToken } from "./message-mentions";
+import { activeMessageMentions } from "./message-mentions";
import type { ComposerVoiceState } from "./use-composer-voice";
export const TEAMHUB_MESSAGE_MAX_LENGTH = 50_000;
@@ -105,11 +105,11 @@ export function useComposerDraft({
? messageLengthError()
: null,
);
- const { documentReferences, mentions, skillReferences } = resolveReferences(
- nextBody,
- nextDocumentOptions,
- );
- selectedMentionsRef.current = mentions;
+ const { documentReferences, selectedMentions, skillReferences } =
+ resolveReferences(nextBody, nextDocumentOptions);
+ // Unavailable mentions stay selected so a later send still blocks on them
+ // instead of silently sending the token as plain text.
+ selectedMentionsRef.current = selectedMentions;
selectedDocumentsRef.current = documentReferences;
selectedSkillsRef.current = skillReferences;
onDraftStateChange?.({
@@ -119,7 +119,7 @@ export function useComposerDraft({
documentContexts: nextDocumentContexts,
body: nextBody,
documentReferences,
- mentions,
+ mentions: selectedMentions,
skillReferences,
});
}
@@ -219,16 +219,18 @@ function resolveAvailableComposerReferences({
}) {
let pendingAvailability = false;
const activeMentions = activeMessageMentions(body, mentions);
- const availableMentions = activeMentions.flatMap((mention) => {
- const current = mentionOptions.find(
+ const unavailableMentions: TeamHubActorRef[] = [];
+ const availableMentions = activeMentions.filter((mention) => {
+ // Identity is the actor, not its display name: a rename after the pick
+ // keeps the selected mention, whose token is the one in the body.
+ const current = mentionOptions.some(
(option) =>
- option.id === mention.id &&
- option.tenant_id === mention.tenant_id &&
- formatMentionToken(option) === formatMentionToken(mention),
+ option.id === mention.id && option.tenant_id === mention.tenant_id,
);
- if (current) return [current];
+ if (current) return true;
if (agentOptionsReady === false) pendingAvailability = true;
- return agentOptionsReady === true ? [] : [mention];
+ if (agentOptionsReady === true) unavailableMentions.push(mention);
+ return agentOptionsReady !== true;
});
const activeDocuments = activeDocumentReferences(body, documentReferences);
@@ -259,6 +261,8 @@ function resolveAvailableComposerReferences({
documentReferences: availableDocuments,
mentions: availableMentions,
pendingAvailability,
+ selectedMentions: activeMentions,
skillReferences: availableSkills,
+ unavailableMentions,
};
}
diff --git a/apps/web/src/features/rooms/chat/use-composer-send.ts b/apps/web/src/features/rooms/chat/use-composer-send.ts
index 94045fe0..40fabca4 100644
--- a/apps/web/src/features/rooms/chat/use-composer-send.ts
+++ b/apps/web/src/features/rooms/chat/use-composer-send.ts
@@ -87,11 +87,21 @@ export function useComposerSend({
mentions,
pendingAvailability,
skillReferences,
+ unavailableMentions,
} = resolveReferences(normalized);
if (pendingAvailability) {
setMessageError("Message references are still loading. Try again.");
return;
}
+ if (unavailableMentions.length) {
+ const names = unavailableMentions.map(formatMentionToken).join(", ");
+ const verb = unavailableMentions.length === 1 ? "is" : "are";
+ const noun = unavailableMentions.length === 1 ? "mention" : "mentions";
+ setMessageError(
+ `${names} ${verb} not available in this Room. Remove the ${noun} to send.`,
+ );
+ return;
+ }
setSubmitting(true);
const committedAttachments = selectedFiles;