Skip to content

Commit 0dc20fe

Browse files
matt2eclaude
andcommitted
feat(desktop): render the config-nudge card in the inbox detail pane
The Home inbox's selected-thread view rendered agent config nudges as a raw buzz:config-nudge code fence. The pane renders through InboxMessageRow — not MessageRow — which never passed configNudgeAuthorPubkey to Markdown, so computeConfigNudge short-circuited before the trust gate could run. The gate's inputs were also unavailable: HomeView's contextMessages mapping dropped the kind and signerPubkey fields that formatTimelineMessages had already computed, and InboxMessageRow's toTimelineMessage re-fabricated a TimelineMessage without them. Plumb kind and signerPubkey through InboxReply, and extract the two mappings into pure inboxViewHelpers functions — toInboxContextMessage (from HomeView's inline memo) and toTimelineMessage (from InboxMessageRow) — so the field selection is unit-testable. InboxMessageRow now derives the same trust predicate MessageRow uses (workspace-scoped useKnownAgentPubkeys baseline, widened by the surface's agentPubkeys prop carrying HomeView's feed-profile isAgent flags), applies it to the popover bot role, and passes getConfigNudgeAuthorPubkey(message, predicate) into Markdown — the card now renders in the inbox for genuine agent-signed kind-9 nudges. Seam tests run a real event through formatTimelineMessages and both mappings and assert the gate passes for an agent signer, rejects a spoofed actor-tag attribution, and rejects unknown signers. The synthetic no-thread-context fallback message (item preview) still renders ungated — it has no signer — and degrades to the raw fence until the separate graceful-prose-fallback change lands. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 8fbce9e commit 0dc20fe

5 files changed

Lines changed: 219 additions & 46 deletions

File tree

‎desktop/src/features/home/lib/inbox.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,17 @@ export type InboxReply = {
6666
depth?: number;
6767
fullTimestampLabel: string;
6868
id: string;
69+
/** Raw event kind — input to the config-nudge trust gate. */
70+
kind?: number;
6971
parentId?: string | null;
7072
reactions?: TimelineReaction[];
7173
rootId?: string | null;
74+
/**
75+
* Raw event signer, NOT the tag-attributed display author
76+
* (`authorPubkey`). The config-nudge trust gate authenticates against
77+
* this field so `actor`/`p`-tag spoofing can't enable the card.
78+
*/
79+
signerPubkey?: string;
7280
tags?: string[][];
7381
timeLabel?: string;
7482
};

‎desktop/src/features/home/lib/inboxViewHelpers.test.mjs‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4+
import { formatTimelineMessages } from "../../messages/lib/formatTimelineMessages.ts";
5+
import { getConfigNudgeAuthorPubkey } from "../../messages/ui/configNudgeAuthPubkey.ts";
46
import {
57
getContextMessageDepth,
68
getReactionTargetId,
79
isInboxThreadContextEvent,
810
matchesInboxFilter,
11+
toInboxContextMessage,
12+
toTimelineMessage,
913
} from "./inboxViewHelpers.ts";
1014

1115
// --- matchesInboxFilter ---
@@ -256,3 +260,91 @@ test("isInboxThreadContextEvent keeps selected thread root, parent, selected eve
256260
true,
257261
);
258262
});
263+
264+
// --- config-nudge pipeline: toInboxContextMessage → toTimelineMessage ---
265+
//
266+
// The inbox detail pane renders messages through two mappings that a real
267+
// event's `kind` and `signerPubkey` must survive: HomeView's
268+
// toInboxContextMessage and InboxMessageRow's toTimelineMessage. An earlier
269+
// version of the HomeView mapping dropped both fields, structurally
270+
// disabling the config-nudge card on the inbox surface (the raw
271+
// ```buzz:config-nudge fence rendered instead). These tests run a real event
272+
// through formatTimelineMessages and both mappings, then assert against the
273+
// same gate helper InboxMessageRow calls.
274+
275+
const NUDGE_CHANNEL_ID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
276+
const NUDGE_AGENT_SIGNER =
277+
"2222222222222222222222222222222222222222222222222222222222222222";
278+
const NUDGE_HUMAN_SIGNER =
279+
"1111111111111111111111111111111111111111111111111111111111111111";
280+
281+
function makeNudgeEvent(overrides = {}) {
282+
return {
283+
id: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
284+
pubkey: NUDGE_AGENT_SIGNER,
285+
kind: 9,
286+
created_at: 1_700_000_000,
287+
content: "**Fizz** needs configuration.\n\n```buzz:config-nudge\n{}\n```",
288+
tags: [["h", NUDGE_CHANNEL_ID]],
289+
sig: "sig",
290+
...overrides,
291+
};
292+
}
293+
294+
function mapThroughInboxPipeline(event) {
295+
const [formatted] = formatTimelineMessages([event], null, undefined, null);
296+
const contextMessage = toInboxContextMessage(formatted, {
297+
eventById: new Map([[event.id, event]]),
298+
fallbackAuthorPubkey: event.pubkey,
299+
profiles: undefined,
300+
selectedItemId: event.id,
301+
});
302+
return toTimelineMessage(contextMessage);
303+
}
304+
305+
test("inbox mappings preserve kind and signerPubkey so the nudge gate can pass", () => {
306+
const message = mapThroughInboxPipeline(makeNudgeEvent());
307+
308+
assert.equal(message.kind, 9);
309+
assert.equal(message.signerPubkey, NUDGE_AGENT_SIGNER);
310+
assert.equal(
311+
getConfigNudgeAuthorPubkey(
312+
message,
313+
(pubkey) => pubkey === NUDGE_AGENT_SIGNER,
314+
),
315+
NUDGE_AGENT_SIGNER,
316+
);
317+
});
318+
319+
test("inbox mappings keep the raw signer distinct from a spoofed actor tag", () => {
320+
const message = mapThroughInboxPipeline(
321+
makeNudgeEvent({
322+
pubkey: NUDGE_HUMAN_SIGNER,
323+
tags: [
324+
["h", NUDGE_CHANNEL_ID],
325+
["actor", NUDGE_AGENT_SIGNER],
326+
],
327+
}),
328+
);
329+
330+
// The attributed display author resolves to the agent (actor tag), but the
331+
// signer stays human — the gate must reject.
332+
assert.equal(message.pubkey?.toLowerCase(), NUDGE_AGENT_SIGNER);
333+
assert.equal(message.signerPubkey, NUDGE_HUMAN_SIGNER);
334+
assert.equal(
335+
getConfigNudgeAuthorPubkey(
336+
message,
337+
(pubkey) => pubkey === NUDGE_AGENT_SIGNER,
338+
),
339+
undefined,
340+
);
341+
});
342+
343+
test("inbox mappings: unknown signer never enables the card", () => {
344+
const message = mapThroughInboxPipeline(makeNudgeEvent());
345+
346+
assert.equal(
347+
getConfigNudgeAuthorPubkey(message, () => false),
348+
undefined,
349+
);
350+
});

‎desktop/src/features/home/lib/inboxViewHelpers.ts‎

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import type { InboxFilter } from "@/features/home/lib/inbox";
1+
import {
2+
formatInboxFullTimestamp,
3+
type InboxContextMessage,
4+
type InboxFilter,
5+
} from "@/features/home/lib/inbox";
26
import {
37
getChannelIdFromTags,
48
getThreadReference,
59
isBroadcastReply,
610
} from "@/features/messages/lib/threading";
7-
import type { FeedItem, RelayEvent } from "@/shared/api/types";
11+
import type { TimelineMessage } from "@/features/messages/types";
12+
import type {
13+
FeedItem,
14+
RelayEvent,
15+
UserProfileSummary,
16+
} from "@/shared/api/types";
17+
import { resolveMentionProps } from "@/shared/lib/resolveMentionNames";
818

919
function hasThreadReplyTags(tags: string[][]) {
1020
const thread = getThreadReference(tags);
@@ -99,3 +109,75 @@ export function getReactionTargetId(tags: string[][]) {
99109

100110
return null;
101111
}
112+
113+
/**
114+
* Maps a formatted timeline message into the inbox detail pane's context
115+
* message shape. Extracted from HomeView's `contextMessages` memo so the
116+
* field selection is unit-testable: `kind` and `signerPubkey` must survive
117+
* this mapping for the config-nudge trust gate
118+
* (`getConfigNudgeAuthorPubkey`) to run in `InboxMessageRow` — an earlier
119+
* version dropped them, structurally disabling the card on the inbox
120+
* surface.
121+
*/
122+
export function toInboxContextMessage(
123+
message: TimelineMessage,
124+
context: {
125+
eventById: ReadonlyMap<string, RelayEvent>;
126+
fallbackAuthorPubkey: string;
127+
profiles: Record<string, UserProfileSummary> | undefined;
128+
selectedItemId: string;
129+
},
130+
): InboxContextMessage {
131+
const event = context.eventById.get(message.id);
132+
const authorPubkey =
133+
message.pubkey ?? event?.pubkey ?? context.fallbackAuthorPubkey;
134+
const { mentionNames, mentionPubkeysByName } = resolveMentionProps(
135+
message.tags ?? [],
136+
context.profiles,
137+
);
138+
return {
139+
id: message.id,
140+
authorLabel: message.author,
141+
authorPubkey,
142+
avatarUrl: message.avatarUrl ?? null,
143+
content: message.body,
144+
createdAt: message.createdAt,
145+
depth: event
146+
? getContextMessageDepth(event, context.eventById)
147+
: message.depth,
148+
fullTimestampLabel: formatInboxFullTimestamp(message.createdAt),
149+
isSelected: message.id === context.selectedItemId,
150+
kind: message.kind,
151+
mentionNames: mentionNames ?? [],
152+
mentionPubkeysByName,
153+
reactions: message.reactions,
154+
signerPubkey: message.signerPubkey,
155+
tags: message.tags,
156+
timeLabel: message.time,
157+
};
158+
}
159+
160+
/**
161+
* Converts an inbox context message back into the `TimelineMessage` shape
162+
* the shared message components consume (action bar, reactions, and the
163+
* config-nudge gate — `kind` and `signerPubkey` ride through for
164+
* `getConfigNudgeAuthorPubkey`).
165+
*/
166+
export function toTimelineMessage(
167+
message: InboxContextMessage,
168+
): TimelineMessage {
169+
return {
170+
id: message.id,
171+
author: message.authorLabel,
172+
avatarUrl: message.avatarUrl,
173+
body: message.content,
174+
createdAt: 0,
175+
depth: message.depth,
176+
kind: message.kind,
177+
pubkey: message.authorPubkey,
178+
reactions: message.reactions ?? [],
179+
signerPubkey: message.signerPubkey,
180+
tags: message.tags,
181+
time: message.timeLabel ?? message.fullTimestampLabel,
182+
};
183+
}

‎desktop/src/features/home/ui/HomeView.tsx‎

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import {
1818
} from "@/features/home/lib/inbox";
1919
import { useInboxSelectionAnchor } from "@/features/home/useInboxSelectionAnchor";
2020
import {
21-
getContextMessageDepth,
2221
getReactionTargetId,
2322
matchesInboxFilter,
23+
toInboxContextMessage,
2424
} from "@/features/home/lib/inboxViewHelpers";
2525
import { useHomeInboxReadState } from "@/features/home/useHomeInboxReadState";
2626
import { useInboxThreadContext } from "@/features/home/useInboxThreadContext";
@@ -66,7 +66,6 @@ import { KIND_REACTION } from "@/shared/constants/kinds";
6666
import { topChromeInset } from "@/shared/layout/chromeLayout";
6767
import { cn } from "@/shared/lib/cn";
6868
import { normalizePubkey } from "@/shared/lib/pubkey";
69-
import { resolveMentionProps } from "@/shared/lib/resolveMentionNames";
7069
import { useElementWidth } from "@/shared/hooks/use-mobile";
7170
import { useThreadPanelWidth } from "@/shared/hooks/useThreadPanelWidth";
7271
import { AUXILIARY_PANEL_SINGLE_COLUMN_BREAKPOINT_PX } from "@/shared/layout/AuxiliaryPanel";
@@ -475,31 +474,14 @@ export function HomeView({
475474
feedProfiles,
476475
);
477476

478-
return timelineMessages.map((message) => {
479-
const event = eventById.get(message.id);
480-
const authorPubkey =
481-
message.pubkey ?? event?.pubkey ?? selectedItem.item.pubkey;
482-
const { mentionNames, mentionPubkeysByName } = resolveMentionProps(
483-
message.tags ?? [],
484-
feedProfiles,
485-
);
486-
return {
487-
id: message.id,
488-
authorLabel: message.author,
489-
authorPubkey,
490-
avatarUrl: message.avatarUrl ?? null,
491-
content: message.body,
492-
createdAt: message.createdAt,
493-
depth: event ? getContextMessageDepth(event, eventById) : message.depth,
494-
fullTimestampLabel: formatInboxFullTimestamp(message.createdAt),
495-
isSelected: message.id === selectedEventId,
496-
mentionNames: mentionNames ?? [],
497-
mentionPubkeysByName,
498-
reactions: message.reactions,
499-
tags: message.tags,
500-
timeLabel: message.time,
501-
};
502-
});
477+
return timelineMessages.map((message) =>
478+
toInboxContextMessage(message, {
479+
eventById,
480+
fallbackAuthorPubkey: selectedItem.item.pubkey,
481+
profiles: feedProfiles,
482+
selectedItemId: selectedEventId ?? selectedItem.id,
483+
}),
484+
);
503485
}, [
504486
channelMessages,
505487
currentPubkey,

‎desktop/src/features/home/ui/InboxMessageRow.tsx‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as React from "react";
22

3+
import { useKnownAgentPubkeys } from "@/features/agents/useKnownAgentPubkeys";
34
import type { InboxContextMessage } from "@/features/home/lib/inbox";
5+
import { toTimelineMessage } from "@/features/home/lib/inboxViewHelpers";
46
import { formatTimeWithoutDayPeriod } from "@/features/messages/lib/dateFormatters";
57
import type { TimelineMessage } from "@/features/messages/types";
8+
import { getConfigNudgeAuthorPubkey } from "@/features/messages/ui/configNudgeAuthPubkey";
69
import { MessageActionBar } from "@/features/messages/ui/MessageActionBar";
710
import { MessageReactions } from "@/features/messages/ui/MessageReactions";
811
import { useReactionHandler } from "@/features/messages/ui/useReactionHandler";
@@ -17,21 +20,6 @@ export type InboxDisplayMessage = InboxContextMessage & {
1720
depth: number;
1821
};
1922

20-
function toTimelineMessage(message: InboxDisplayMessage): TimelineMessage {
21-
return {
22-
id: message.id,
23-
author: message.authorLabel,
24-
avatarUrl: message.avatarUrl,
25-
body: message.content,
26-
createdAt: 0,
27-
depth: message.depth,
28-
pubkey: message.authorPubkey,
29-
reactions: message.reactions ?? [],
30-
tags: message.tags,
31-
time: message.timeLabel ?? message.fullTimestampLabel,
32-
};
33-
}
34-
3523
type InboxMessageRowProps = {
3624
agentPubkeys?: ReadonlySet<string>;
3725
canReply: boolean;
@@ -76,8 +64,21 @@ export function InboxMessageRow({
7664
errorMessage: reactionErrorMessage,
7765
select: handleReactionSelect,
7866
} = useReactionHandler(timelineMessage, onToggleReaction);
79-
const isAuthorAgent =
80-
agentPubkeys?.has(normalizePubkey(message.authorPubkey)) === true;
67+
// "Is this pubkey an agent" = the workspace-scoped baseline every surface
68+
// shares plus this surface's extras passed via `agentPubkeys` (HomeView
69+
// folds feed-profile `isAgent` flags in). Mirrors MessageRow's predicate.
70+
const knownAgentPubkeys = useKnownAgentPubkeys();
71+
const isKnownAgentPubkey = React.useCallback(
72+
(pubkey: string) => {
73+
const normalized = normalizePubkey(pubkey);
74+
return (
75+
knownAgentPubkeys.has(normalized) ||
76+
agentPubkeys?.has(normalized) === true
77+
);
78+
},
79+
[agentPubkeys, knownAgentPubkeys],
80+
);
81+
const isAuthorAgent = isKnownAgentPubkey(message.authorPubkey);
8182
const profileRole = isAuthorAgent ? "bot" : undefined;
8283
const hoverTimestampLabel = formatTimeWithoutDayPeriod(
8384
message.timeLabel ?? message.fullTimestampLabel,
@@ -183,6 +184,14 @@ export function InboxMessageRow({
183184
emojiOnly &&
184185
"text-4xl leading-tight [&_p]:leading-tight [&_img[data-custom-emoji]]:h-[1.45em] [&_img[data-custom-emoji]]:align-middle [&_button:has(img[data-custom-emoji])]:align-middle",
185186
)}
187+
// Only pass the author pubkey for agent-authored messages so
188+
// config-nudge cards can authenticate the sender. Uses the
189+
// raw event signer (signerPubkey) — not the tag-attributed
190+
// display author — to prevent actor/p-tag spoofing.
191+
configNudgeAuthorPubkey={getConfigNudgeAuthorPubkey(
192+
timelineMessage,
193+
isKnownAgentPubkey,
194+
)}
186195
content={message.content}
187196
customEmoji={customEmoji}
188197
mentionNames={message.mentionNames}

0 commit comments

Comments
 (0)