Skip to content

Commit 07c2be3

Browse files
Wintermutethomaspblock
andcommitted
fix(desktop): address PR #6003 review change requests
- Anchor the inline Projects agent conversation to the accepted opener event (created_at, event_id) instead of a bare visibleAfter timestamp, so unrelated DM history sharing the opener's second is excluded and the opener itself is always included (id-equality short-circuit tolerates the command's post-hoc timestamp). - Make the sidebar "owned" filter surface every project the viewer owns, independent of the Added set. - Dispatch the sidebar-membership change event even when localStorage persistence fails, carrying the computed membership in the event detail; the sidebar listener consumes the detail instead of re-reading storage. - Strip agent-context footers from the last marker (lastIndexOf) so user text containing an earlier marker survives intact. - Reorder the :has() selector groups in components.css so the generic content-surface selector precedes the :root-qualified ones. - Sanitize relay/git-controlled values (project/repo names, repo address, branch, file path, work-item title/id/status) before embedding them in the hidden agent prompt, and disclose them as untrusted context. - Stop fabricating an origin conversation in DiscussionChannels: the author-claimed origin now renders as a channel-only row with no quoted message. Co-authored-by: Thomas Petersen <[email protected]> Signed-off-by: Thomas Petersen <[email protected]>
1 parent 9ae5e5c commit 07c2be3

15 files changed

Lines changed: 430 additions & 308 deletions

‎desktop/src/features/projects/lib/discussionChannels.test.mjs‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
formatNameList,
99
groupDiscussionChannels,
1010
mergeOriginDiscussionChannel,
11-
pickOriginConversationEvent,
12-
relayEventToSearchHit,
1311
repositoryDiscussionQuery,
1412
} from "./discussionChannels.ts";
1513

@@ -58,50 +56,6 @@ test("mergeOriginDiscussionChannel prepends the origin when search missed it", (
5856
assert.equal(mergeOriginDiscussionChannel(discussed, null), discussed);
5957
});
6058

61-
test("relayEventToSearchHit uses the thread root when present", () => {
62-
const hit = relayEventToSearchHit(
63-
{
64-
id: EVENT_ID,
65-
content: "filed it",
66-
kind: 9,
67-
pubkey: ALICE,
68-
created_at: 50,
69-
tags: [
70-
["h", "origin"],
71-
["e", "root-id", "", "root"],
72-
["e", "parent-id", "", "reply"],
73-
],
74-
},
75-
"origin",
76-
"agents",
77-
);
78-
assert.equal(hit.eventId, EVENT_ID);
79-
assert.equal(hit.channelId, "origin");
80-
assert.equal(hit.channelName, "agents");
81-
assert.equal(hit.threadRootId, "root-id");
82-
});
83-
84-
test("pickOriginConversationEvent prefers the author's newest message", () => {
85-
const origin = { channelId: "c1", createdAt: 100, pubkey: ALICE };
86-
const picked = pickOriginConversationEvent(
87-
[
88-
{ pubkey: BOB, created_at: 90, id: "bob" },
89-
{ pubkey: ALICE, created_at: 40, id: "old" },
90-
{ pubkey: ALICE, created_at: 80, id: "alice" },
91-
],
92-
origin,
93-
);
94-
assert.equal(picked?.id, "alice");
95-
assert.equal(
96-
pickOriginConversationEvent(
97-
[{ pubkey: BOB, created_at: 90, id: "bob" }],
98-
origin,
99-
)?.id,
100-
"bob",
101-
);
102-
assert.equal(pickOriginConversationEvent([], origin), null);
103-
});
104-
10559
test("commit queries match full or short hash citations", () => {
10660
const hash = "0123456789abcdef0123456789abcdef01234567";
10761
assert.equal(

‎desktop/src/features/projects/lib/discussionChannels.ts‎

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
*/
1717

1818
import type { SearchHit } from "@/shared/api/searchTypes";
19-
import type { RelayEvent } from "@/shared/api/types";
20-
import {
21-
getChannelIdFromTags,
22-
getThreadReference,
23-
} from "@/features/messages/lib/threading";
2419

2520
export type DiscussionChannel = {
2621
id: string;
@@ -42,8 +37,11 @@ export function entityDiscussionQuery(eventId: string): string {
4237
return eventId;
4338
}
4439

45-
/** Channel the entity was created from (`h` tag). Search will not find that
46-
* thread: those messages predate the share link. */
40+
/** Channel the entity was created from (`h` tag, author-claimed). The tag
41+
* proves only the channel — no event ties any specific message to the
42+
* entity, so the origin renders as a channel-only row: never fetch nearby
43+
* channel traffic to fabricate, quote, or attribute a "spawning"
44+
* conversation. */
4745
export type DiscussionOrigin = {
4846
channelId: string;
4947
createdAt: number;
@@ -72,42 +70,6 @@ export function mergeOriginDiscussionChannel(
7270
];
7371
}
7472

75-
/** Newest origin-author message, else the newest message in the channel. */
76-
export function pickOriginConversationEvent<
77-
T extends { pubkey: string; created_at: number },
78-
>(events: readonly T[], origin: DiscussionOrigin): T | null {
79-
if (events.length === 0) return null;
80-
const author = origin.pubkey.toLowerCase();
81-
const byAuthor = events.filter(
82-
(event) => event.pubkey.toLowerCase() === author,
83-
);
84-
const pool = byAuthor.length > 0 ? byAuthor : events;
85-
return [...pool].sort((left, right) => right.created_at - left.created_at)[0];
86-
}
87-
88-
/** Shape the origin channel event into the hit the conversation panel expects. */
89-
export function relayEventToSearchHit(
90-
event: Pick<
91-
RelayEvent,
92-
"id" | "content" | "kind" | "pubkey" | "created_at" | "tags"
93-
>,
94-
channelId: string,
95-
channelName?: string | null,
96-
): SearchHit {
97-
const thread = getThreadReference(event.tags);
98-
return {
99-
eventId: event.id,
100-
content: event.content,
101-
kind: event.kind,
102-
pubkey: event.pubkey,
103-
channelId: getChannelIdFromTags(event.tags) ?? channelId,
104-
channelName: channelName ?? null,
105-
createdAt: event.created_at,
106-
score: 0,
107-
threadRootId: thread.rootId ?? thread.parentId ?? event.id,
108-
};
109-
}
110-
11173
/**
11274
* Search text matching messages that link a repository or any of its PRs
11375
* and issues: all those links carry `owner=<pubkey>&d=<dtag>`, so the owner

‎desktop/src/features/projects/lib/projectAgentConversation.test.mjs‎

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
22
import { beforeEach, test } from "node:test";
33

44
import {
5+
isAtOrAfterConversationOpener,
56
mergeProjectAgentConversationEvents,
67
restoreProjectsAgentConversation,
78
visibleConversationMessages,
@@ -20,6 +21,10 @@ const AGENT_PUBKEY = "a".repeat(64);
2021
const WORKSPACE_ID = "wss://relay.example.com";
2122
// The user opened the Projects prompt at this instant (epoch seconds).
2223
const PROMPT_AT = 1_752_570_000;
24+
// The relay-accepted event id of the opening prompt. Within the opener's
25+
// second, the timeline orders by ascending id, so ids <= the opener's are
26+
// at-or-after it and ids > it are older history.
27+
const OPENER = { createdAt: PROMPT_AT, eventId: `d${"0".repeat(63)}` };
2328

2429
const AGENT = { pubkey: AGENT_PUBKEY, name: "Brain" };
2530

@@ -31,8 +36,8 @@ const EXISTING_DM = {
3136
lastMessageAt: new Date((PROMPT_AT - 60) * 1_000).toISOString(),
3237
};
3338

34-
function message(createdAt, kind = KIND_STREAM_MESSAGE) {
35-
return { kind, created_at: createdAt, id: `msg-${kind}-${createdAt}` };
39+
function message(createdAt, kind = KIND_STREAM_MESSAGE, id) {
40+
return { kind, created_at: createdAt, id: id ?? `msg-${kind}-${createdAt}` };
3641
}
3742

3843
const store = new Map();
@@ -58,34 +63,21 @@ test("restores exactly the conversation this feature persisted", () => {
5863
stored: {
5964
agentPubkey: AGENT_PUBKEY.toUpperCase(),
6065
channelId: EXISTING_DM.id,
61-
visibleAfter: PROMPT_AT,
66+
opener: OPENER,
6267
},
6368
channels: [EXISTING_DM],
6469
candidates: [AGENT],
6570
});
6671
assert.equal(restored?.channel, EXISTING_DM);
6772
assert.equal(restored?.agent, AGENT);
68-
assert.equal(restored?.visibleAfter, PROMPT_AT);
69-
});
70-
71-
test("a zero cutoff pointer is not restorable (would expose full DM history)", () => {
72-
const restored = restoreProjectsAgentConversation({
73-
stored: {
74-
agentPubkey: AGENT_PUBKEY,
75-
channelId: EXISTING_DM.id,
76-
visibleAfter: 0,
77-
},
78-
channels: [EXISTING_DM],
79-
candidates: [AGENT],
80-
});
81-
assert.equal(restored, null);
73+
assert.deepEqual(restored?.opener, OPENER);
8274
});
8375

8476
test("pointers to unknown channels or agents are not restorable", () => {
8577
const stored = {
8678
agentPubkey: AGENT_PUBKEY,
8779
channelId: EXISTING_DM.id,
88-
visibleAfter: PROMPT_AT,
80+
opener: OPENER,
8981
};
9082
assert.equal(
9183
restoreProjectsAgentConversation({
@@ -111,19 +103,43 @@ test("messages the DM held before the first Projects prompt never appear", () =>
111103
message(PROMPT_AT - 3_600, KIND_STREAM_MESSAGE_V2),
112104
message(PROMPT_AT - 1),
113105
];
114-
const opener = message(PROMPT_AT);
106+
const opener = message(PROMPT_AT, KIND_STREAM_MESSAGE, OPENER.eventId);
115107
const reply = message(PROMPT_AT + 5, KIND_STREAM_MESSAGE_V2);
116108
const nonChatEvent = message(PROMPT_AT + 10, 7);
117109

118110
const visible = visibleConversationMessages(
119111
[reply, ...olderHistory, opener, nonChatEvent],
120-
PROMPT_AT,
112+
OPENER,
121113
);
122114
assert.deepEqual(visible, [opener, reply]);
123115
});
124116

117+
test("unrelated DM history sharing the opener's second is excluded", () => {
118+
// Relay order within one second is ascending id (newest first), so events
119+
// with ids greater than the opener's id are strictly older than it.
120+
const sameSecondOlder = message(
121+
PROMPT_AT,
122+
KIND_STREAM_MESSAGE,
123+
`e${"f".repeat(63)}`,
124+
);
125+
const opener = message(PROMPT_AT, KIND_STREAM_MESSAGE, OPENER.eventId);
126+
const sameSecondNewer = message(
127+
PROMPT_AT,
128+
KIND_STREAM_MESSAGE_V2,
129+
`c${"0".repeat(63)}`,
130+
);
131+
132+
const visible = visibleConversationMessages(
133+
[sameSecondOlder, opener, sameSecondNewer],
134+
OPENER,
135+
);
136+
assert.deepEqual(visible, [opener, sameSecondNewer]);
137+
assert.equal(isAtOrAfterConversationOpener(sameSecondOlder, OPENER), false);
138+
assert.equal(isAtOrAfterConversationOpener(opener, OPENER), true);
139+
});
140+
125141
test("root questions and separately queried replies stay in conversation order", () => {
126-
const firstQuestion = message(PROMPT_AT);
142+
const firstQuestion = message(PROMPT_AT, KIND_STREAM_MESSAGE, OPENER.eventId);
127143
const firstAnswer = message(PROMPT_AT + 2, KIND_STREAM_MESSAGE_V2);
128144
const secondQuestion = message(PROMPT_AT + 4);
129145
const secondAnswer = message(PROMPT_AT + 6, KIND_STREAM_MESSAGE_V2);
@@ -141,23 +157,46 @@ test("root questions and separately queried replies stay in conversation order",
141157
]);
142158
});
143159

144-
test("storage read rejects legacy pointers with a zero cutoff", () => {
160+
test("storage read rejects legacy timestamp-only pointers", () => {
161+
// Pointers written before the opener was event-anchored carry only
162+
// `visibleAfter`. They cannot uphold the same-second isolation invariant,
163+
// so they are not restorable.
145164
globalThis.localStorage.setItem(
146165
`buzz.projects.agentConversation.${encodeURIComponent(WORKSPACE_ID)}`,
147166
JSON.stringify({
148167
agentPubkey: AGENT_PUBKEY,
149168
channelId: EXISTING_DM.id,
150-
visibleAfter: 0,
169+
visibleAfter: PROMPT_AT,
151170
}),
152171
);
153172
assert.equal(readStoredProjectsAgentConversation(WORKSPACE_ID), null);
154173
});
155174

156-
test("storage round-trips prompt-anchored pointers and clears them", () => {
175+
test("storage read rejects malformed opener pointers", () => {
176+
for (const opener of [
177+
{ createdAt: 0, eventId: OPENER.eventId },
178+
{ createdAt: Number.NaN, eventId: OPENER.eventId },
179+
{ createdAt: PROMPT_AT, eventId: "" },
180+
{ createdAt: PROMPT_AT },
181+
null,
182+
]) {
183+
globalThis.localStorage.setItem(
184+
`buzz.projects.agentConversation.${encodeURIComponent(WORKSPACE_ID)}`,
185+
JSON.stringify({
186+
agentPubkey: AGENT_PUBKEY,
187+
channelId: EXISTING_DM.id,
188+
opener,
189+
}),
190+
);
191+
assert.equal(readStoredProjectsAgentConversation(WORKSPACE_ID), null);
192+
}
193+
});
194+
195+
test("storage round-trips opener-anchored pointers and clears them", () => {
157196
const stored = {
158197
agentPubkey: AGENT_PUBKEY,
159198
channelId: EXISTING_DM.id,
160-
visibleAfter: PROMPT_AT,
199+
opener: OPENER,
161200
};
162201
writeStoredProjectsAgentConversation(WORKSPACE_ID, stored);
163202
assert.deepEqual(readStoredProjectsAgentConversation(WORKSPACE_ID), stored);

‎desktop/src/features/projects/lib/projectAgentConversation.ts‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1-
import type { StoredProjectsAgentConversation } from "@/features/projects/lib/projectAgentConversationStorage";
1+
import type {
2+
ProjectsConversationOpener,
3+
StoredProjectsAgentConversation,
4+
} from "@/features/projects/lib/projectAgentConversationStorage";
25
import type { Channel } from "@/shared/api/types";
36
import {
47
KIND_STREAM_MESSAGE,
58
KIND_STREAM_MESSAGE_V2,
69
} from "@/shared/constants/kinds";
710
import { normalizePubkey } from "@/shared/lib/pubkey";
811

12+
/**
13+
* True when `event` is the conversation opener or comes after it in the
14+
* timeline's `(created_at, event_id)` ordering (`compareRelayOrder` in
15+
* `channelWindowStore.ts`). A bare timestamp cannot make this call — every
16+
* unrelated event sharing the opener's second would pass — which is why the
17+
* opener's exact event id participates. Id equality is checked first because
18+
* the send command stamps its response timestamp after the relay round-trip,
19+
* so the persisted `createdAt` may trail the signed event's by a second.
20+
*/
21+
export function isAtOrAfterConversationOpener(
22+
event: { created_at: number; id: string },
23+
opener: ProjectsConversationOpener,
24+
): boolean {
25+
return (
26+
event.id === opener.eventId ||
27+
event.created_at > opener.createdAt ||
28+
(event.created_at === opener.createdAt && event.id <= opener.eventId)
29+
);
30+
}
31+
932
/**
1033
* Restores an inline Projects conversation strictly from a pointer this
1134
* feature persisted earlier. DM channels are reused across the app, so
@@ -22,10 +45,14 @@ export function restoreProjectsAgentConversation<
2245
stored: StoredProjectsAgentConversation | null;
2346
channels: readonly Channel[];
2447
candidates: readonly Agent[];
25-
}): { channel: Channel; agent: Agent; visibleAfter: number } | null {
26-
// A zero cutoff would render the DM's full history; only pointers
27-
// anchored to a concrete Projects prompt are restorable.
28-
if (!stored || stored.visibleAfter <= 0) return null;
48+
}): {
49+
channel: Channel;
50+
agent: Agent;
51+
opener: ProjectsConversationOpener;
52+
} | null {
53+
// Only pointers anchored to a concrete opener event are restorable —
54+
// anything weaker would render DM history that predates the conversation.
55+
if (!stored) return null;
2956
const channel = channels.find(
3057
(candidate) => candidate.id === stored.channelId,
3158
);
@@ -34,23 +61,24 @@ export function restoreProjectsAgentConversation<
3461
(candidate) => candidate.pubkey === agentPubkey,
3562
);
3663
if (!channel || !agent) return null;
37-
return { agent, channel, visibleAfter: stored.visibleAfter };
64+
return { agent, channel, opener: stored.opener };
3865
}
3966

4067
/**
4168
* Chat rows for the inline Projects thread: plain messages only, and nothing
42-
* sent before the conversation cutoff — the backing DM may hold unrelated
43-
* history from ordinary DM usage.
69+
* ordered before the conversation's opener event — the backing DM may hold
70+
* unrelated history from ordinary DM usage, including history from the
71+
* opener's own second.
4472
*/
4573
export function visibleConversationMessages<
46-
Event extends { kind: number; created_at: number },
47-
>(events: readonly Event[], visibleAfter: number): Event[] {
74+
Event extends { kind: number; created_at: number; id: string },
75+
>(events: readonly Event[], opener: ProjectsConversationOpener): Event[] {
4876
return events
4977
.filter(
5078
(event) =>
5179
(event.kind === KIND_STREAM_MESSAGE ||
5280
event.kind === KIND_STREAM_MESSAGE_V2) &&
53-
event.created_at >= visibleAfter,
81+
isAtOrAfterConversationOpener(event, opener),
5482
)
5583
.sort((left, right) => left.created_at - right.created_at);
5684
}

0 commit comments

Comments
 (0)