Skip to content

Commit 19dc33b

Browse files
morgmartnpub1n4y9luxx9y27pz5qz93vr9w8auyk7mmpgwf9gpe9tn4zv4kyhzjqtcntu7npub1d6t84ajeg9skp2609l2k6axgcme8x7g7u7luj352r03hcwreg7lqnxcsex
authored
Persist agent audiences with native inline mentions (#1949)
Signed-off-by: npub1n4y9luxx9y27pz5qz93vr9w8auyk7mmpgwf9gpe9tn4zv4kyhzjqtcntu7 <9d485ff0c62915e08a801162c195c7ef096f6f6143925407255cea2656c4b8a4@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1d6t84ajeg9skp2609l2k6axgcme8x7g7u7luj352r03hcwreg7lqnxcsex <6e967af659416160ab4f2fd56d74c8c6f273791ee7bfc9468a1be37c387947be@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1n4y9luxx9y27pz5qz93vr9w8auyk7mmpgwf9gpe9tn4zv4kyhzjqtcntu7 <9d485ff0c62915e08a801162c195c7ef096f6f6143925407255cea2656c4b8a4@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1d6t84ajeg9skp2609l2k6axgcme8x7g7u7luj352r03hcwreg7lqnxcsex <6e967af659416160ab4f2fd56d74c8c6f273791ee7bfc9468a1be37c387947be@sprout-oss.stage.blox.sqprod.co>
1 parent b1ff682 commit 19dc33b

17 files changed

Lines changed: 986 additions & 25 deletions

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default defineConfig({
5454
"**/composer-tooltip-dismiss.spec.ts",
5555
"**/mentions.spec.ts",
5656
"**/team-mentions.spec.ts",
57+
"**/persistent-agent-audience.spec.ts",
5758
"**/relay-reconnect.spec.ts",
5859
"**/relay-reconnect-affordance.spec.ts",
5960
"**/workflows.spec.ts",

‎desktop/scripts/check-file-sizes.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ const overrides = new Map([
460460
// +3: onLinkShortcutRef wiring (ref decl + editor option + assignment) for
461461
// the ⌘K link-editor shortcut, mirroring the existing onEditLinkRef
462462
// pattern. Queued to split with the rest of this list.
463-
["src/features/messages/ui/MessageComposer.tsx", 1036],
463+
// +35: persistent audience scope/hook wiring and chip component handoff. The
464+
// chip markup lives separately; remaining lines connect existing composer
465+
// send state to the audience store. Queued with the existing split.
466+
["src/features/messages/ui/MessageComposer.tsx", 1091],
464467
// global-agent-config: model-tuning section (BuzzAgentModelTuningFields via
465468
// EditAgentAdvancedFields) + providerValid gate + effectiveProvider derivation
466469
// + globalProvider threading into getPersonaProviderOptions. All load-bearing

‎desktop/src/features/channels/ui/ChannelPane.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ export const ChannelPane = React.memo(function ChannelPane({
754754
<WelcomeComposerBanner state={welcomeComposerBannerState} />
755755
) : null}
756756
<MessageComposer
757+
audienceContext={{ type: "timeline" }}
757758
channelId={activeChannel?.id ?? null}
758759
channelName={activeChannel?.name ?? "channel"}
759760
channelType={activeChannel?.channelType ?? null}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ export function InboxDetailPane({
472472
<div className="pointer-events-none absolute inset-x-0 bottom-0 z-10">
473473
<div className="pointer-events-auto">
474474
<MessageComposer
475+
audienceContext={{
476+
type: "thread",
477+
threadRootId: item.conversationId,
478+
}}
475479
channelId={item.item.channelId}
476480
channelName={item.channelLabel ?? "channel"}
477481
channelType={composerChannelType}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { filterEffectiveExplicitAgentPubkeys } from "./effectiveExplicitAgentPubkeys.ts";
5+
6+
const agentA = "a".repeat(64);
7+
const agentB = "b".repeat(64);
8+
const person = "c".repeat(64);
9+
10+
test("send without inviting excludes removed agents from audience promotion", () => {
11+
assert.deepEqual(
12+
filterEffectiveExplicitAgentPubkeys([agentA, agentB], [agentA, person]),
13+
[agentA],
14+
);
15+
});
16+
17+
test("effective audience promotion keeps authored order and dedupes", () => {
18+
assert.deepEqual(
19+
filterEffectiveExplicitAgentPubkeys(
20+
[agentB.toUpperCase(), agentA, agentB],
21+
[agentA, agentB],
22+
),
23+
[agentB, agentA],
24+
);
25+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function normalizePubkey(pubkey: string): string {
2+
return pubkey.trim().toLowerCase();
3+
}
4+
5+
function uniqueNormalizedPubkeys(pubkeys: Iterable<string>): string[] {
6+
return [...new Set([...pubkeys].map(normalizePubkey))].filter(Boolean);
7+
}
8+
9+
export function filterEffectiveExplicitAgentPubkeys(
10+
explicitAgentPubkeys: Iterable<string>,
11+
effectiveMentionPubkeys: Iterable<string>,
12+
): string[] {
13+
const effectivePubkeys = new Set(
14+
uniqueNormalizedPubkeys(effectiveMentionPubkeys),
15+
);
16+
return uniqueNormalizedPubkeys(explicitAgentPubkeys).filter((pubkey) =>
17+
effectivePubkeys.has(pubkey),
18+
);
19+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
function createStorage() {
5+
const values = new Map();
6+
return {
7+
getItem: (key) => values.get(key) ?? null,
8+
setItem: (key, value) => values.set(key, String(value)),
9+
};
10+
}
11+
12+
const agentA = "a".repeat(64);
13+
const agentB = "b".repeat(64);
14+
const agentC = "c".repeat(64);
15+
const ownerA = "1".repeat(64);
16+
const ownerB = "2".repeat(64);
17+
const storageKey = "buzz:persistent-agent-audiences:v2";
18+
19+
let loadSequence = 0;
20+
21+
async function loadStore(offset = 0) {
22+
globalThis.window = { localStorage: createStorage() };
23+
loadSequence += 1;
24+
return import(
25+
`./persistentAgentAudience.ts?test=${Date.now()}-${offset}-${loadSequence}`
26+
);
27+
}
28+
29+
function savedAudiences() {
30+
return JSON.parse(window.localStorage.getItem(storageKey));
31+
}
32+
33+
test("conversation scopes isolate identities, channels, and threads", async () => {
34+
const store = await loadStore();
35+
const channelA = store.getPersistentAgentAudienceScope({
36+
ownerPubkey: ownerA,
37+
channelId: "channel-a",
38+
});
39+
const channelB = store.getPersistentAgentAudienceScope({
40+
ownerPubkey: ownerA,
41+
channelId: "channel-b",
42+
});
43+
const threadA1 = store.getPersistentAgentAudienceScope({
44+
ownerPubkey: ownerA,
45+
channelId: "channel-a",
46+
threadRootId: "root-1",
47+
});
48+
const threadA2 = store.getPersistentAgentAudienceScope({
49+
ownerPubkey: ownerA,
50+
channelId: "channel-a",
51+
threadRootId: "root-2",
52+
});
53+
const otherIdentity = store.getPersistentAgentAudienceScope({
54+
ownerPubkey: ownerB,
55+
channelId: "channel-a",
56+
});
57+
58+
for (const scope of [channelA, channelB, threadA1, threadA2, otherIdentity]) {
59+
assert.ok(scope);
60+
store.setPersistentAgentAudience(scope, [agentA]);
61+
}
62+
63+
assert.equal(new Set(Object.keys(savedAudiences())).size, 5);
64+
});
65+
66+
test("successful fast send promotes without a persisted draft key", async () => {
67+
const store = await loadStore(1);
68+
const scope = store.getPersistentAgentAudienceScope({
69+
ownerPubkey: ownerA,
70+
channelId: "channel-a",
71+
});
72+
store.setPersistentAgentAudienceEnabled(true);
73+
74+
store.promotePersistentAgentAudience({
75+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
76+
scope,
77+
expectedRevision: store.getPersistentAgentAudienceRevision(scope),
78+
explicitAgentPubkeys: [agentA],
79+
});
80+
81+
assert.deepEqual(savedAudiences(), { [scope]: [agentA] });
82+
});
83+
84+
test("explicit recipients merge and dedupe after successful send", async () => {
85+
const store = await loadStore(2);
86+
const scope = `${ownerA}:channel-a:timeline`;
87+
store.setPersistentAgentAudienceEnabled(true);
88+
store.setPersistentAgentAudience(scope, [agentA]);
89+
const revision = store.getPersistentAgentAudienceRevision(scope);
90+
91+
store.promotePersistentAgentAudience({
92+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
93+
scope,
94+
expectedRevision: revision,
95+
explicitAgentPubkeys: [agentA, agentB],
96+
});
97+
98+
assert.deepEqual(savedAudiences(), { [scope]: [agentA, agentB] });
99+
});
100+
101+
test("successful send makes authored mention order authoritative", async () => {
102+
const store = await loadStore(100);
103+
const scope = `${ownerA}:channel-a:timeline`;
104+
store.setPersistentAgentAudienceEnabled(true);
105+
store.setPersistentAgentAudience(scope, [agentA, agentB]);
106+
107+
store.promotePersistentAgentAudience({
108+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
109+
scope,
110+
expectedRevision: store.getPersistentAgentAudienceRevision(scope),
111+
explicitAgentPubkeys: [agentB, agentA, agentC],
112+
});
113+
114+
assert.deepEqual(savedAudiences(), {
115+
[scope]: [agentB, agentA, agentC],
116+
});
117+
});
118+
119+
test("successful send retains saved targets absent from the draft", async () => {
120+
const store = await loadStore(101);
121+
const scope = `${ownerA}:channel-a:timeline`;
122+
store.setPersistentAgentAudienceEnabled(true);
123+
store.setPersistentAgentAudience(scope, [agentA, agentC]);
124+
125+
store.promotePersistentAgentAudience({
126+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
127+
scope,
128+
expectedRevision: store.getPersistentAgentAudienceRevision(scope),
129+
explicitAgentPubkeys: [agentB, agentA],
130+
});
131+
132+
assert.deepEqual(savedAudiences(), {
133+
[scope]: [agentB, agentA, agentC],
134+
});
135+
});
136+
137+
test("removal while send awaits wins over late success", async () => {
138+
const store = await loadStore(3);
139+
const scope = `${ownerA}:channel-a:timeline`;
140+
store.setPersistentAgentAudienceEnabled(true);
141+
store.setPersistentAgentAudience(scope, [agentA]);
142+
const revisionAtSubmit = store.getPersistentAgentAudienceRevision(scope);
143+
144+
store.removePersistentAgentAudienceMember(scope, agentA);
145+
store.promotePersistentAgentAudience({
146+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
147+
scope,
148+
expectedRevision: revisionAtSubmit,
149+
explicitAgentPubkeys: [agentA],
150+
});
151+
152+
assert.deepEqual(savedAudiences(), { [scope]: [] });
153+
});
154+
155+
test("removing final chip preserves an explicit empty scope", async () => {
156+
const store = await loadStore(4);
157+
const scope = `${ownerA}:channel-a:thread:root`;
158+
store.setPersistentAgentAudience(scope, [agentA]);
159+
store.removePersistentAgentAudienceMember(scope, agentA);
160+
161+
assert.deepEqual(savedAudiences(), { [scope]: [] });
162+
});
163+
164+
test("completion after disabling cannot repopulate audiences", async () => {
165+
const store = await loadStore(5);
166+
const scope = `${ownerA}:channel-a:timeline`;
167+
store.setPersistentAgentAudienceEnabled(true);
168+
store.setPersistentAgentAudience(scope, [agentA]);
169+
const revisionAtSubmit = store.getPersistentAgentAudienceRevision(scope);
170+
store.setPersistentAgentAudienceEnabled(false);
171+
172+
store.promotePersistentAgentAudience({
173+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
174+
scope,
175+
expectedRevision: revisionAtSubmit,
176+
explicitAgentPubkeys: [agentB],
177+
});
178+
179+
assert.deepEqual(savedAudiences(), {});
180+
});
181+
182+
test("invalid, duplicate, and differently-cased pubkeys normalize", async () => {
183+
const store = await loadStore(6);
184+
const scope = `${ownerA}:channel-a:timeline`;
185+
store.setPersistentAgentAudience(scope, [
186+
agentA.toUpperCase(),
187+
agentA,
188+
"bad",
189+
]);
190+
191+
assert.deepEqual(savedAudiences(), { [scope]: [agentA] });
192+
});
193+
194+
test("new recipients retain explicit mention order", async () => {
195+
const store = await loadStore(9);
196+
const scope = `${ownerA}:channel-a:timeline`;
197+
store.setPersistentAgentAudienceEnabled(true);
198+
199+
store.promotePersistentAgentAudience({
200+
expectedGeneration: store.getPersistentAgentAudienceGeneration(),
201+
scope,
202+
expectedRevision: store.getPersistentAgentAudienceRevision(scope),
203+
explicitAgentPubkeys: [agentB, agentA],
204+
});
205+
206+
assert.deepEqual(savedAudiences(), { [scope]: [agentB, agentA] });
207+
});
208+
209+
test("first new-message send resolves its destination after capturing generation", async () => {
210+
const store = await loadStore(7);
211+
const capturedGeneration = store.getPersistentAgentAudienceGeneration();
212+
store.setPersistentAgentAudienceEnabled(true);
213+
const scope = store.getPersistentAgentAudienceScope({
214+
ownerPubkey: ownerA,
215+
channelId: "resolved-dm",
216+
});
217+
218+
store.promotePersistentAgentAudience({
219+
expectedGeneration: capturedGeneration,
220+
expectedRevision: null,
221+
scope,
222+
explicitAgentPubkeys: [agentA],
223+
});
224+
225+
assert.deepEqual(savedAudiences(), { [scope]: [agentA] });
226+
});
227+
228+
test("disable during new-message destination preparation invalidates promotion", async () => {
229+
const store = await loadStore(8);
230+
store.setPersistentAgentAudienceEnabled(true);
231+
const capturedGeneration = store.getPersistentAgentAudienceGeneration();
232+
store.setPersistentAgentAudienceEnabled(false);
233+
store.setPersistentAgentAudienceEnabled(true);
234+
const scope = store.getPersistentAgentAudienceScope({
235+
ownerPubkey: ownerA,
236+
channelId: "resolved-dm",
237+
});
238+
239+
store.promotePersistentAgentAudience({
240+
expectedGeneration: capturedGeneration,
241+
expectedRevision: null,
242+
scope,
243+
explicitAgentPubkeys: [agentA],
244+
});
245+
246+
assert.deepEqual(savedAudiences(), {});
247+
});

0 commit comments

Comments
 (0)