fix(delete): make agent-deleted messages disappear from desktop UI immediately (#918)

Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
Co-authored-by: Bart <bart@sprout.local>
This commit is contained in:
Taylor Ho
2026-06-09 23:02:13 -07:00
committed by GitHub
parent e08937cdde
commit 3e56331e9c
21 changed files with 302 additions and 27 deletions

View File

@@ -0,0 +1,31 @@
import assert from "node:assert/strict";
import test from "node:test";
import { isDmNotifiableKind } from "./isDmNotifiableKind.ts";
// Regression guard for the phantom-DM-notification bug: when kind:5 deletes
// gained an `h` tag, they started matching the live DM subscription. Without
// this gate, deleting a DM message fires a "New message" toast on the other
// side. Reactions (7), Sprout-native deletes (9005), edits (40003), diffs
// (40008), and system messages (40099) hit the same subscription and must
// also be filtered.
test("human-visible message kinds fire DM notifications", () => {
assert.equal(isDmNotifiableKind(9), true, "kind:9 stream message");
assert.equal(isDmNotifiableKind(40002), true, "kind:40002 stream message v2");
assert.equal(isDmNotifiableKind(45001), true, "kind:45001 forum post");
assert.equal(isDmNotifiableKind(45003), true, "kind:45003 forum comment");
});
test("non-message kinds do NOT fire DM notifications", () => {
assert.equal(isDmNotifiableKind(5), false, "kind:5 NIP-09 deletion");
assert.equal(isDmNotifiableKind(7), false, "kind:7 reaction");
assert.equal(
isDmNotifiableKind(9005),
false,
"kind:9005 Sprout-native delete",
);
assert.equal(isDmNotifiableKind(40003), false, "kind:40003 message edit");
assert.equal(isDmNotifiableKind(40008), false, "kind:40008 message diff");
assert.equal(isDmNotifiableKind(40099), false, "kind:40099 system message");
});

View File

@@ -0,0 +1,10 @@
import { CHANNEL_MESSAGE_EVENT_KINDS } from "@/shared/constants/kinds";
const DM_NOTIFIABLE_KINDS = new Set<number>(CHANNEL_MESSAGE_EVENT_KINDS);
// DM OS-notifications gate. The DM subscription matches every `h`-tagged
// event in the channel (kind:5/7/9005/edits/etc.), so we must filter to
// human-visible message kinds before firing a toast.
export function isDmNotifiableKind(kind: number): boolean {
return DM_NOTIFIABLE_KINDS.has(kind);
}

View File

@@ -17,6 +17,8 @@ import {
} from "@/shared/constants/kinds";
import type { Channel, RelayEvent } from "@/shared/api/types";
import { isDmNotifiableKind } from "./isDmNotifiableKind";
export type UseLiveChannelUpdatesOptions = {
currentPubkey?: string;
onDmMessage?: (event: RelayEvent, channel: Channel) => void;
@@ -108,6 +110,11 @@ export function useLiveChannelUpdates(
);
const handleDmEvent = React.useEffectEvent((event: RelayEvent) => {
// Only human-visible message kinds should fire DM notifications.
if (!isDmNotifiableKind(event.kind)) {
return;
}
// Suppress backlog events that predate our subscription — these are
// historical replays, not live messages.
if (event.created_at < dmSubscriptionStartedAtRef.current) {

View File

@@ -83,7 +83,10 @@ export function useDeleteForumPostMutation(channel: Channel | null) {
return useMutation({
mutationFn: async ({ eventId }: { eventId: string }) => {
await deleteMessage(eventId);
if (!channel) {
throw new Error("No channel selected.");
}
await deleteMessage(channel.id, eventId);
},
onSuccess: () => {
if (channel) {
@@ -103,7 +106,10 @@ export function useDeleteForumReplyMutation(
return useMutation({
mutationFn: async ({ eventId }: { eventId: string }) => {
await deleteMessage(eventId);
if (!channel) {
throw new Error("No channel selected.");
}
await deleteMessage(channel.id, eventId);
},
onSuccess: () => {
if (channel) {

View File

@@ -414,9 +414,13 @@ export function HomeView({
if (!selectedItem || !canDelete) {
return;
}
const channelId = selectedItem.item.channelId;
if (!channelId) {
return;
}
setIsDeletingMessage(true);
void deleteMessage(selectedItem.id)
void deleteMessage(channelId, selectedItem.id)
.then(() => {
onRefresh();
})

View File

@@ -462,7 +462,10 @@ export function useDeleteMessageMutation(channel: Channel | null) {
return useMutation<void, Error, { eventId: string }>({
mutationFn: async ({ eventId }) => {
await deleteMessage(eventId);
if (!channel) {
throw new Error("No channel selected.");
}
await deleteMessage(channel.id, eventId);
},
onSuccess: (_data, { eventId }) => {
if (!channel) return;

View File

@@ -0,0 +1,102 @@
import assert from "node:assert/strict";
import test from "node:test";
import { formatTimelineMessages } from "./formatTimelineMessages.ts";
const HEX64_A =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const HEX64_B =
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
const PUBKEY_A =
"1111111111111111111111111111111111111111111111111111111111111111";
const PUBKEY_B =
"2222222222222222222222222222222222222222222222222222222222222222";
const CHANNEL_ID = "36411e44-0e2d-4cfe-bd6e-567eb169db9f";
function streamMessage(overrides = {}) {
return {
id: HEX64_A,
pubkey: PUBKEY_A,
kind: 9,
created_at: 1_700_000_000,
content: "hello world",
tags: [["h", CHANNEL_ID]],
sig: "sig",
...overrides,
};
}
function deletionEvent(kind, targetId, overrides = {}) {
return {
id: HEX64_B,
pubkey: PUBKEY_B,
kind,
created_at: 1_700_000_001,
content: "",
tags: [
["h", CHANNEL_ID],
["e", targetId],
],
sig: "sig",
...overrides,
};
}
test("kind:5 (NIP-09) deletion hides the target message", () => {
const events = [streamMessage(), deletionEvent(5, HEX64_A)];
const out = formatTimelineMessages(events, null, undefined, null);
assert.equal(
out.length,
0,
"the kind:9 message should be filtered out by the kind:5 deletion",
);
});
test("kind:9005 (NIP-29 / Sprout-native) deletion hides the target message", () => {
// This is the actual reported bug: agents emit kind:9005 deletes via the
// CLI. Without recognizing 9005 as a deletion marker the message stayed
// rendered until manual refresh.
const events = [streamMessage(), deletionEvent(9005, HEX64_A)];
const out = formatTimelineMessages(events, null, undefined, null);
assert.equal(
out.length,
0,
"the kind:9 message should be filtered out by the kind:9005 deletion",
);
});
test("non-deletion event kinds do NOT hide the target message", () => {
// Sanity check: only kind:5 and kind:9005 are treated as deletion markers.
// A kind:7 reaction with the same `e` tag must not erase the target.
const reaction = {
id: HEX64_B,
pubkey: PUBKEY_B,
kind: 7,
created_at: 1_700_000_001,
content: "+",
tags: [
["h", CHANNEL_ID],
["e", HEX64_A],
],
sig: "sig",
};
const events = [streamMessage(), reaction];
const out = formatTimelineMessages(events, null, undefined, null);
assert.equal(out.length, 1, "the kind:9 message should still be visible");
});
test("deletion target with non-hex `e` tag value is ignored", () => {
const bogusDeletion = deletionEvent(9005, HEX64_A, {
tags: [
["h", CHANNEL_ID],
["e", "not-hex"],
],
});
const events = [streamMessage(), bogusDeletion];
const out = formatTimelineMessages(events, null, undefined, null);
assert.equal(
out.length,
1,
"malformed deletion tag should not match anything",
);
});

View File

@@ -22,6 +22,7 @@ import {
KIND_JOB_REQUEST,
KIND_JOB_RESULT,
KIND_DELETION,
KIND_NIP29_DELETE_EVENT,
KIND_REACTION,
KIND_STREAM_MESSAGE,
KIND_STREAM_MESSAGE_V2,
@@ -150,7 +151,11 @@ export function formatTimelineMessages(
}
const deletedEventIds = new Set<string>();
for (const event of events) {
if (event.kind !== KIND_DELETION) {
// Both kind:5 and kind:9005 are deletion markers; mirror the relay.
if (
event.kind !== KIND_DELETION &&
event.kind !== KIND_NIP29_DELETE_EVENT
) {
continue;
}