@recursiv/sdk 0.7.83 → 0.7.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +85 -1
  2. package/dist/assistant/index.d.ts +11 -3
  3. package/dist/assistant/index.js +11 -3
  4. package/dist/assistant/render.d.ts +25 -2
  5. package/dist/assistant/render.js +197 -16
  6. package/dist/assistant/subjectBundle.js +20 -3
  7. package/dist/assistant/turnCaps.d.ts +3 -0
  8. package/dist/assistant/turnCaps.js +141 -0
  9. package/dist/assistant/types.d.ts +223 -7
  10. package/dist/assistant/types.js +15 -0
  11. package/dist/index.d.ts +10 -4
  12. package/dist/index.js +7 -1
  13. package/dist/privacy/facts.generated.d.ts +79 -0
  14. package/dist/privacy/facts.generated.js +93 -0
  15. package/dist/privacy/index.d.ts +48 -0
  16. package/dist/privacy/index.js +42 -0
  17. package/dist/resources/discovery-merge.d.ts +17 -1
  18. package/dist/resources/discovery-merge.js +84 -4
  19. package/dist/resources/discovery-progressive.d.ts +4 -3
  20. package/dist/resources/discovery-progressive.js +11 -8
  21. package/dist/resources/discovery.d.ts +9 -1
  22. package/dist/resources/discovery.js +62 -15
  23. package/dist/resources/feeds.d.ts +15 -6
  24. package/dist/resources/feeds.js +2 -1
  25. package/dist/resources/integrations.d.ts +3 -0
  26. package/dist/resources/privacy.d.ts +13 -0
  27. package/dist/resources/privacy.js +14 -0
  28. package/dist/resources/protocols.d.ts +99 -11
  29. package/dist/resources/protocols.js +45 -0
  30. package/dist/resources/sealed.d.ts +127 -7
  31. package/dist/resources/sealed.js +33 -6
  32. package/dist/resources/settings.d.ts +3 -1
  33. package/dist/resources/settings.js +3 -1
  34. package/dist/social-protocol-ids.d.ts +28 -0
  35. package/dist/social-protocol-ids.js +30 -0
  36. package/dist/types.d.ts +37 -0
  37. package/llm.md +16 -5
  38. package/package.json +6 -1
@@ -0,0 +1,93 @@
1
+ // Generated by scripts/privacy-claims.mjs from docs/privacy-claims.json. Do not edit: CI refuses a hand edit.
2
+ // Change the registry, then run: node scripts/privacy-claims.mjs --write
3
+ export const PRIVACY_REGISTRY_VERSION = 0;
4
+ export const PRIVACY_FACTS = [
5
+ {
6
+ id: "agents.tool_mode.chat_only",
7
+ status: "planned",
8
+ enforcement: "server",
9
+ capability: "web_tools",
10
+ step: "S5",
11
+ says: "An agent in chat_only tool mode is given no tools, so it opens no web page.",
12
+ },
13
+ {
14
+ id: "agents.turn_stamp",
15
+ status: "enforced",
16
+ enforcement: "server",
17
+ capability: null,
18
+ step: "S7",
19
+ says: "Every reply a model writes in an agent chat through the platform's agent turn records, in metadata.ai, the model, its maker and the route, a model swapped in and whether for usage limits or after a failure, and the tools it called.",
20
+ },
21
+ {
22
+ id: "agents.personal_context_sections",
23
+ status: "planned",
24
+ enforcement: "server",
25
+ capability: "activity",
26
+ step: "S26",
27
+ says: "A personal agent's turn carries no follows, upvotes, groups or views of the member unless the member turned that part of their activity on.",
28
+ },
29
+ {
30
+ id: "sealed.attest_before_send",
31
+ status: "enforced",
32
+ enforcement: "device",
33
+ capability: "chat_tier",
34
+ step: "S28",
35
+ says: "The phone seals a Sealed request only to the key of a router it checked against a fresh challenge of its own: the router's report, signed by its AMD hardware, must answer that challenge with debug mode off and firmware at or above a floor, and its software must be a release Tinfoil signed and listed in a public log. If any check fails there is no key to seal to, so nothing is sealed or sent. How long a checked router is used before it is checked again is up to the app.",
36
+ },
37
+ {
38
+ id: "sealed.attest_second_signer",
39
+ status: "planned",
40
+ enforcement: "device",
41
+ capability: "chat_tier",
42
+ step: "S28",
43
+ says: "The phone also requires Recursiv's own signature over the router's release, so a release Tinfoil signed alone is refused.",
44
+ },
45
+ {
46
+ id: "sealed.no_standard_fallback",
47
+ status: "enforced",
48
+ enforcement: "client",
49
+ capability: "chat_tier",
50
+ step: "S28",
51
+ says: "The SDK sends a Sealed request only to the Sealed route, marked Sealed, and only once: a refusal, an error or an answer that is not sealed goes back to the app, and the SDK never sends the request again, to Sealed or to any other route.",
52
+ },
53
+ {
54
+ id: "sealed.gateway_opaque",
55
+ status: "enforced",
56
+ enforcement: "server",
57
+ capability: "chat_tier",
58
+ step: "S28",
59
+ says: "The Sealed gateway passes each Sealed request and answer on as bytes it does not read: it never parses, stores or logs a request or answer body. It refuses a request that carries no EHBP encapsulated key, the mark of a sealed body, and a success answer that carries no well-formed EHBP response nonce, the mark of a sealed answer; it cannot tell from the bytes themselves whether a body is sealed.",
60
+ },
61
+ {
62
+ id: "sealed.dest_tinfoil_only",
63
+ status: "enforced",
64
+ enforcement: "server",
65
+ capability: "chat_tier",
66
+ step: "S28",
67
+ says: "The Sealed gateway sends Sealed requests, and asks for attestation documents, only to the router origins its configuration lists, named exactly, and it lists only https origins of Tinfoil router hosts (inference.tinfoil.sh or router-N.tinfoil.sh). It refuses any other server a client names and follows no redirect.",
68
+ },
69
+ {
70
+ id: "sealed.no_server_history",
71
+ status: "enforced",
72
+ enforcement: "server",
73
+ capability: "sealed_history",
74
+ step: "S29",
75
+ says: "The platform keeps no Sealed chat history: no Sealed route writes a conversation or message row, and the gateway stores no request or answer body.",
76
+ },
77
+ {
78
+ id: "sealed.backup_excluded",
79
+ status: "planned",
80
+ enforcement: "device",
81
+ capability: "sealed_history",
82
+ step: "S29",
83
+ says: "Sealed chats saved on a phone are left out of the phone's backups.",
84
+ },
85
+ {
86
+ id: "sealed.no_push",
87
+ status: "enforced",
88
+ enforcement: "server",
89
+ capability: "chat_tier",
90
+ step: "S29",
91
+ says: "No Sealed route sends a push notification or writes a notification: a Sealed answer goes back only on the request that asked for it.",
92
+ },
93
+ ];
@@ -0,0 +1,48 @@
1
+ /**
2
+ * `@recursiv/sdk/privacy`: the platform privacy facts, generated from the claims registry (docs/privacy-claims.json
3
+ * in recursivlabs/recursiv). Each fact says one thing the platform does with member data and whether this SDK release
4
+ * marks it enforced, which it does only once every test that proves it is real and runs in CI.
5
+ *
6
+ * An app shows a privacy sentence only when the facts it rests on are live: enforced here AND listed by the API it
7
+ * talks to (`sdk.privacy.capabilities()`), so neither a newer SDK nor a newer API can make a claim on its own. A fact
8
+ * held on the phone (`enforcement: 'device'`) needs a third check, the app's own: neither this SDK nor the API can see
9
+ * which native module the phone runs, so the app names the device facts its installed module carries (`deviceFacts`).
10
+ *
11
+ * The types live here, not in the SDK's shared types, so this entry point stands alone.
12
+ */
13
+ import { PRIVACY_FACTS, PRIVACY_REGISTRY_VERSION } from './facts.generated.js';
14
+ export { PRIVACY_FACTS, PRIVACY_REGISTRY_VERSION };
15
+ export type PrivacyFact = (typeof PRIVACY_FACTS)[number];
16
+ export type PrivacyFactId = PrivacyFact['id'];
17
+ /** `planned`: the platform does not yet prove it. `enforced`: every test that proves it is real and runs in CI. */
18
+ export type PrivacyFactStatus = 'planned' | 'enforced';
19
+ /** Where a fact holds: on the server, on the phone (the native module), in the SDK, or in both server and client. */
20
+ export type PrivacyFactEnforcement = 'server' | 'device' | 'client' | 'server_and_client';
21
+ /** GET /privacy/capabilities: the facts the deployed API's build marks enforced, and the commit it runs. */
22
+ export interface PrivacyCapabilities {
23
+ /** The commit the API was built from, or `unknown`. */
24
+ api_commit: string;
25
+ /** Fact ids, sorted. An id this SDK release does not know is never live here. */
26
+ facts: string[];
27
+ }
28
+ /** The fact with this id, or undefined for an id this SDK release does not know. */
29
+ export declare function privacyFact(id: string): PrivacyFact | undefined;
30
+ /** Whether this SDK release marks the fact enforced. An unknown id is not. */
31
+ export declare function isFactEnforced(id: string): boolean;
32
+ export interface LiveFactOptions {
33
+ /**
34
+ * The device facts (`enforcement: 'device'`) the app's installed native module carries, as the app knows from the
35
+ * module revision it ships (for Sealed, @recursiv/e2ee-react-native). A device fact is live only when it is named
36
+ * here too. Left out, no device fact is live. A fact says what the module does, not what the app does with it: for
37
+ * `sealed.attest_before_send` the app chooses how long a checked router is reused, so copy that says the router is
38
+ * checked before every send needs the app to check before every send.
39
+ */
40
+ deviceFacts?: Iterable<string>;
41
+ }
42
+ /**
43
+ * Enforced in this SDK release AND listed by the deployed API, and for a device fact also named in `deviceFacts`.
44
+ * Missing capabilities mean nothing is live.
45
+ */
46
+ export declare function isFactLive(id: string, capabilities: PrivacyCapabilities | null | undefined, options?: LiveFactOptions): boolean;
47
+ /** Every fact that is live for this API (and, for device facts, this app's native module), in registry order. */
48
+ export declare function liveFacts(capabilities: PrivacyCapabilities | null | undefined, options?: LiveFactOptions): PrivacyFactId[];
@@ -0,0 +1,42 @@
1
+ /**
2
+ * `@recursiv/sdk/privacy`: the platform privacy facts, generated from the claims registry (docs/privacy-claims.json
3
+ * in recursivlabs/recursiv). Each fact says one thing the platform does with member data and whether this SDK release
4
+ * marks it enforced, which it does only once every test that proves it is real and runs in CI.
5
+ *
6
+ * An app shows a privacy sentence only when the facts it rests on are live: enforced here AND listed by the API it
7
+ * talks to (`sdk.privacy.capabilities()`), so neither a newer SDK nor a newer API can make a claim on its own. A fact
8
+ * held on the phone (`enforcement: 'device'`) needs a third check, the app's own: neither this SDK nor the API can see
9
+ * which native module the phone runs, so the app names the device facts its installed module carries (`deviceFacts`).
10
+ *
11
+ * The types live here, not in the SDK's shared types, so this entry point stands alone.
12
+ */
13
+ import { PRIVACY_FACTS, PRIVACY_REGISTRY_VERSION } from './facts.generated.js';
14
+ export { PRIVACY_FACTS, PRIVACY_REGISTRY_VERSION };
15
+ const BY_ID = new Map(PRIVACY_FACTS.map((fact) => [fact.id, fact]));
16
+ /** The fact with this id, or undefined for an id this SDK release does not know. */
17
+ export function privacyFact(id) {
18
+ return BY_ID.get(id);
19
+ }
20
+ /** Whether this SDK release marks the fact enforced. An unknown id is not. */
21
+ export function isFactEnforced(id) {
22
+ return BY_ID.get(id)?.status === 'enforced';
23
+ }
24
+ /**
25
+ * Enforced in this SDK release AND listed by the deployed API, and for a device fact also named in `deviceFacts`.
26
+ * Missing capabilities mean nothing is live.
27
+ */
28
+ export function isFactLive(id, capabilities, options = {}) {
29
+ const fact = BY_ID.get(id);
30
+ if (!fact || fact.status !== 'enforced')
31
+ return false;
32
+ if (!Array.isArray(capabilities?.facts) || !capabilities.facts.includes(id))
33
+ return false;
34
+ if (fact.enforcement === 'device')
35
+ return new Set(options.deviceFacts ?? []).has(id);
36
+ return true;
37
+ }
38
+ /** Every fact that is live for this API (and, for device facts, this app's native module), in registry order. */
39
+ export function liveFacts(capabilities, options = {}) {
40
+ const deviceFacts = [...(options.deviceFacts ?? [])];
41
+ return PRIVACY_FACTS.filter((fact) => isFactLive(fact.id, capabilities, { deviceFacts })).map((fact) => fact.id);
42
+ }
@@ -54,6 +54,20 @@ export declare function discoveryRowTitle(row: unknown): string | null;
54
54
  export declare function discoveryLeadable(item: DiscoveryPostItem): boolean;
55
55
  /** A post with a picture or a video of its own that the reader sees unblurred. */
56
56
  export declare function discoveryShowsMedia(item: DiscoveryPostItem): boolean;
57
+ /**
58
+ * What makes an item the same post as another on one screen: its key, and for an open-network post its network and id
59
+ * as the network spells it (a Nostr event id in any case) and each picture or clip file it carries (by address, and by
60
+ * content hash for a file named by one). Two notes that carry one clip are one post to a reader: a bot posting it
61
+ * again, a mirror account, the source index's copy beside the provider's. A Minds post is its key alone.
62
+ */
63
+ export declare function discoveryItemIdentities(item: DiscoveryItem): string[];
64
+ /**
65
+ * The rows with each post shown once: a post item whose identity (discoveryItemIdentities) is in `shown`, or was on an
66
+ * earlier item of the rows, leaves its module, which keeps its spec in step; a module of posts left with none is dropped.
67
+ * A story keeps its headline when its posts go, and rows that are not modules pass as they are. `shown` gains the
68
+ * identities of every post kept.
69
+ */
70
+ export declare function withoutRepeatedDiscoveryItems<T>(rows: readonly T[], shown?: Set<string>): T[];
57
71
  /** A player found this video file (or HLS playlist) will not play: no Discover module promotes it, and it gets no video boost. */
58
72
  export declare function reportDiscoveryVideoUnavailable(url: string): void;
59
73
  /** Forget every report (a test seam, or a new viewer). */
@@ -151,7 +165,9 @@ export declare function weaveDiscoveryArrival<T extends {
151
165
  * that are not posts (a story, a rail, a row of the client's own) take the places they would have had woven in one by
152
166
  * one, and the posts fill the other places by engagement under the network mix (DiscoveryEngagementMerge). The first
153
167
  * post at the top of the feed, or below the line where it turned (`floor`), leads what comes under it (takeLead): never
154
- * a post with no picture or clip that nobody engaged with while the batch holds one with either.
168
+ * a post with no picture or clip that nobody engaged with while the batch holds one with either. A post the rows already
169
+ * show, or that arrives twice (the same open-network post, or another note with the same clip: discoveryItemIdentities),
170
+ * is shown once, where it was first.
155
171
  */
156
172
  export declare function mergeDiscoveryArrival<T extends {
157
173
  id: string;
@@ -130,6 +130,79 @@ export function discoveryLeadable(item) {
130
130
  export function discoveryShowsMedia(item) {
131
131
  return discoveryLeadable(item) && hasMedia(item.post.media);
132
132
  }
133
+ // ── One post, shown once ────────────────────────────────────────────────
134
+ /** A file named by its SHA-256 (Blossom, nostr.build): the same bytes wherever they are served from. */
135
+ const CONTENT_HASH = /\/([0-9a-f]{64})(?:\.[a-z0-9]{1,8})?$/i;
136
+ /**
137
+ * What makes an item the same post as another on one screen: its key, and for an open-network post its network and id
138
+ * as the network spells it (a Nostr event id in any case) and each picture or clip file it carries (by address, and by
139
+ * content hash for a file named by one). Two notes that carry one clip are one post to a reader: a bot posting it
140
+ * again, a mirror account, the source index's copy beside the provider's. A Minds post is its key alone.
141
+ */
142
+ export function discoveryItemIdentities(item) {
143
+ if (item.kind !== 'social')
144
+ return [item.key];
145
+ const post = item.post;
146
+ const identities = [item.key];
147
+ if (typeof post.protocol === 'string' && typeof post.nativeId === 'string' && post.nativeId) {
148
+ identities.push(`post:${post.protocol}:${post.protocol === 'nostr' ? post.nativeId.toLowerCase() : post.nativeId}`);
149
+ }
150
+ for (const entry of Array.isArray(post.media) ? post.media : []) {
151
+ const url = entry?.url;
152
+ if (typeof url !== 'string' || !/^https?:\/\//i.test(url))
153
+ continue;
154
+ const address = url.split('#')[0];
155
+ identities.push(`media:${address}`);
156
+ const hash = CONTENT_HASH.exec(address.split('?')[0])?.[1];
157
+ if (hash)
158
+ identities.push(`blob:${hash.toLowerCase()}`);
159
+ }
160
+ return identities;
161
+ }
162
+ /**
163
+ * The rows with each post shown once: a post item whose identity (discoveryItemIdentities) is in `shown`, or was on an
164
+ * earlier item of the rows, leaves its module, which keeps its spec in step; a module of posts left with none is dropped.
165
+ * A story keeps its headline when its posts go, and rows that are not modules pass as they are. `shown` gains the
166
+ * identities of every post kept.
167
+ */
168
+ export function withoutRepeatedDiscoveryItems(rows, shown = new Set()) {
169
+ const out = [];
170
+ for (const row of rows) {
171
+ if (!isModule(row)) {
172
+ out.push(row);
173
+ continue;
174
+ }
175
+ const items = row.items.filter(item => {
176
+ if (!isPostItem(item))
177
+ return true;
178
+ const identities = discoveryItemIdentities(item);
179
+ if (identities.some(identity => shown.has(identity)))
180
+ return false;
181
+ for (const identity of identities)
182
+ shown.add(identity);
183
+ return true;
184
+ });
185
+ if (items.length === row.items.length) {
186
+ out.push(row);
187
+ continue;
188
+ }
189
+ if (!items.some(item => isPostItem(item) || item.kind === 'cluster'))
190
+ continue;
191
+ out.push(withItems(row, items));
192
+ }
193
+ return out;
194
+ }
195
+ /** The identities of every post the rows show (discoveryItemIdentities). */
196
+ function shownIdentities(rows) {
197
+ const shown = new Set();
198
+ for (const row of rows)
199
+ if (isModule(row))
200
+ for (const item of row.items)
201
+ if (isPostItem(item))
202
+ for (const identity of discoveryItemIdentities(item))
203
+ shown.add(identity);
204
+ return shown;
205
+ }
133
206
  // ── Videos that can play ────────────────────────────────────────────────
134
207
  /** Video files (or playlists) a player found will not play this session: never promoted again. */
135
208
  const UNAVAILABLE_VIDEOS = new Set();
@@ -332,9 +405,12 @@ export function poolDiscoveryGalleries(modules, now, keep) {
332
405
  for (const galleries of themes.values()) {
333
406
  const seen = new Set();
334
407
  const cards = galleries.flatMap((gallery, from) => gallery.items.filter(isPostItem).flatMap((item, index) => {
335
- if (seen.has(item.key))
408
+ // One card per post: a picture two galleries (or two notes) carry is pooled once (discoveryItemIdentities).
409
+ const identities = discoveryItemIdentities(item);
410
+ if (identities.some(identity => seen.has(identity)))
336
411
  return [];
337
- seen.add(item.key);
412
+ for (const identity of identities)
413
+ seen.add(identity);
338
414
  return [{ item, gallery, network: discoveryPostNetwork(item), rank: rankDiscoveryItem(item, reference, now), order: index * galleries.length + from }];
339
415
  })).sort((a, b) => a.rank.tier - b.rank.tier || b.rank.score - a.rank.score || a.order - b.order);
340
416
  const counts = new Map();
@@ -492,12 +568,16 @@ export function weaveDiscoveryArrival(rows, arriving, reached, floor = 0) {
492
568
  * that are not posts (a story, a rail, a row of the client's own) take the places they would have had woven in one by
493
569
  * one, and the posts fill the other places by engagement under the network mix (DiscoveryEngagementMerge). The first
494
570
  * post at the top of the feed, or below the line where it turned (`floor`), leads what comes under it (takeLead): never
495
- * a post with no picture or clip that nobody engaged with while the batch holds one with either.
571
+ * a post with no picture or clip that nobody engaged with while the batch holds one with either. A post the rows already
572
+ * show, or that arrives twice (the same open-network post, or another note with the same clip: discoveryItemIdentities),
573
+ * is shown once, where it was first.
496
574
  */
497
575
  export function mergeDiscoveryArrival(rows, arriving, reached, floor = 0, now = Date.now()) {
498
576
  const at = arrivalPlace(rows, reached, floor);
499
577
  const head = rows.slice(0, at);
500
- const below = at >= rows.length ? [...arriving] : weave(arriving, rows.slice(at));
578
+ // A post already in the rows, or twice in what arrives, is shown once: where it was first (withoutRepeatedDiscoveryItems).
579
+ const fresh = withoutRepeatedDiscoveryItems(arriving, shownIdentities(rows));
580
+ const below = at >= rows.length ? fresh : weave(fresh, rows.slice(at));
501
581
  const posts = below.filter(isDiscoveryPostModule);
502
582
  if (!posts.length)
503
583
  return [...head, ...below];
@@ -77,9 +77,10 @@ export declare const FIRST_SCREEN_MS = 2500;
77
77
  */
78
78
  export declare const VISITOR_FIRST_SCREEN_MS = 800;
79
79
  /**
80
- * A module without the items in `seen` (item keys). Posts already shown by another source's module are not shown again:
81
- * the sessions dedupe within themselves, but across them a story's member posts can also arrive in that network's own
82
- * page. A module that loses items keeps its spec in step. A suggestion already shown leaves its rail, and a rail left
80
+ * A module without the items in `seen` (item keys, or any identity of a post: discoveryItemIdentities). Posts already
81
+ * shown by another source's module are not shown again: the sessions dedupe within themselves, but across them a story's
82
+ * member posts can also arrive in that network's own page, and one clip in two notes. A module that loses items keeps
83
+ * its spec in step. A suggestion already shown leaves its rail, and a rail left
83
84
  * with fewer than two is dropped; a story already shown is dropped whole, and keeps its headline and outlets when only
84
85
  * its posts were shown; a post module with none left is dropped (null).
85
86
  */
@@ -1,5 +1,6 @@
1
1
  import { readFalloverSteps } from './discovery-fallover.js';
2
2
  import { composeFirstScreen } from './discovery-first-screen.js';
3
+ import { discoveryItemIdentities } from './discovery-merge.js';
3
4
  /** How long a round keeps waiting for its slower sources once one of them has answered. */
4
5
  export const DISCOVERY_STRAGGLER_MS = 3000;
5
6
  /**
@@ -22,9 +23,10 @@ const isSuggestion = (item) => item.kind === 'group' || item.kind === 'person' |
22
23
  /** A suggestion rail with fewer is not drawn: the session builds none with fewer either. */
23
24
  const SUGGESTIONS_MIN = 2;
24
25
  /**
25
- * A module without the items in `seen` (item keys). Posts already shown by another source's module are not shown again:
26
- * the sessions dedupe within themselves, but across them a story's member posts can also arrive in that network's own
27
- * page. A module that loses items keeps its spec in step. A suggestion already shown leaves its rail, and a rail left
26
+ * A module without the items in `seen` (item keys, or any identity of a post: discoveryItemIdentities). Posts already
27
+ * shown by another source's module are not shown again: the sessions dedupe within themselves, but across them a story's
28
+ * member posts can also arrive in that network's own page, and one clip in two notes. A module that loses items keeps
29
+ * its spec in step. A suggestion already shown leaves its rail, and a rail left
28
30
  * with fewer than two is dropped; a story already shown is dropped whole, and keeps its headline and outlets when only
29
31
  * its posts were shown; a post module with none left is dropped (null).
30
32
  */
@@ -32,7 +34,7 @@ export function withoutSeenItems(module, seen) {
32
34
  const lead = module.items[0];
33
35
  if (lead?.kind === 'cluster' && seen.has(lead.key))
34
36
  return null;
35
- const items = module.items.filter(item => item.kind === 'cluster' || !seen.has(item.key));
37
+ const items = module.items.filter(item => item.kind === 'cluster' || !discoveryItemIdentities(item).some(identity => seen.has(identity)));
36
38
  if (items.length === module.items.length)
37
39
  return module;
38
40
  if (module.items.some(isSuggestion)) {
@@ -46,14 +48,15 @@ export function withoutSeenItems(module, seen) {
46
48
  module: { ...element, props: { ...element.props, itemKeys: items.map(item => item.key) } } } } };
47
49
  }
48
50
  /**
49
- * Claims the posts a module shows, and only those. A story claims the members it carries, never its whole member list:
50
- * the Minds session reads a story's network members only through its member reader, which may fail, time out or be
51
- * missing. A member the story does not carry still shows in its network's own page.
51
+ * Claims the posts a module shows, and only those, by every identity (discoveryItemIdentities). A story claims the members
52
+ * it carries, never its whole member list: the Minds session reads a story's network members only through its member
53
+ * reader, which may fail, time out or be missing. A member the story does not carry still shows in its network's own page.
52
54
  */
53
55
  function remember(module, seen) {
54
56
  for (const item of module.items)
55
57
  if (isPost(item))
56
- seen.add(item.key);
58
+ for (const identity of discoveryItemIdentities(item))
59
+ seen.add(identity);
57
60
  }
58
61
  /** The other networks a feed over every source reads, in the session's own order. */
59
62
  export function discoveryRemoteSources(networks, source) {
@@ -506,7 +506,15 @@ export interface DiscoveryModule {
506
506
  id: string;
507
507
  presentation: DiscoveryLayout['presentation'] | DiscoveryRecommendationKind | DiscoveryRadarPresentation;
508
508
  source: DiscoveryLayout['source'];
509
- /** Subject of the featured window (a browse-category id or `mixed`), for specific module copy. */
509
+ /**
510
+ * Subject of the module (a browse-category id), for specific module copy ("Videos in Science"). A post module carries
511
+ * one only when its featured window was themed so AND most of its own posts were judged into that category (0.7.84):
512
+ * a window's theme is one judgment of up to six posts taken together, and the module features some of them (one clip,
513
+ * a conversation) or gathers pictures from the whole page, so an open-network clip with no judgment of its own was
514
+ * titled after its neighbours ("Videos in Technology" over a Nostr clip of birds). Today a post is judged into a
515
+ * category when a category feed served it (the read's category filter), so modules on a feed with no category carry
516
+ * none. A radar story carries its cluster's theme. Absent: title the module by its kind alone.
517
+ */
510
518
  theme?: string;
511
519
  items: DiscoveryItem[];
512
520
  spec: {
@@ -1,4 +1,6 @@
1
+ import { SOCIAL_PROTOCOL_IDS } from '../social-protocol-ids.js';
1
2
  import { readFalloverSteps } from './discovery-fallover.js';
3
+ import { discoveryItemIdentities } from './discovery-merge.js';
2
4
  import { createDiscoveryTopShare, createProgressiveFeed } from './discovery-progressive.js';
3
5
  export * from './discovery-fallover.js';
4
6
  export * from './discovery-first-screen.js';
@@ -142,7 +144,8 @@ function recommendationItem(kind, candidate) {
142
144
  return { key: `rec:tag:${(nonEmpty(value.slug) ? value.slug : value.name).toLowerCase()}`, kind: 'tag', tag: value };
143
145
  }
144
146
  const RADAR_KINDS = new Set(['breaking', 'viral', 'story']);
145
- const RADAR_SOCIAL_KINDS = new Set(['bluesky', 'activitypub', 'nostr']);
147
+ /** A radar member's open network (SOCIAL_PROTOCOL_IDS, Farcaster included before the radar holds any); another kind is skipped. */
148
+ const RADAR_SOCIAL_KINDS = new Set(SOCIAL_PROTOCOL_IDS);
146
149
  const httpUrl = (value) => typeof value === 'string' && value.length <= 4096 && /^https?:\/\//i.test(value);
147
150
  const countOf = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : null;
148
151
  const dimension = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? value : undefined;
@@ -271,7 +274,8 @@ export function pickRadarCluster(clusters, page, options = {}) {
271
274
  }
272
275
  return breaking[0] ?? unused[0];
273
276
  }
274
- const TOP_NETWORKS = new Set(['bluesky', 'activitypub', 'nostr']);
277
+ /** A feed top's open-network item or lane (SOCIAL_PROTOCOL_IDS); a session keeps only the networks it reads (`readyTop`). */
278
+ const TOP_NETWORKS = new Set(SOCIAL_PROTOCOL_IDS);
275
279
  const idString = (value) => typeof value === 'string' && value.length > 0 && value.length <= 4096;
276
280
  const counted = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
277
281
  /** The refs an app's seen set may hold for a post: its id, or '<protocol>/<nativeId>' (an ActivityPub id also without https://). */
@@ -339,6 +343,19 @@ function topItemSeenRefs(value) {
339
343
  }
340
344
  const ordinary = { version: 'discovery-layout-v1', presentation: 'posts', source: 'fallback' };
341
345
  const key = (protocol, id) => JSON.stringify([protocol, id]);
346
+ /** The judgment of a read made in `category`: its category filter's selection, when that is the category asked. */
347
+ const judgedInto = (category, filter) => (category && filter?.selected === category ? { judged: category } : {});
348
+ /** The judgment of a read's item at `index`. */
349
+ const judgmentAt = (read, index) => (read.judgments ? read.judgments[index] : read.judged);
350
+ /**
351
+ * A featured module's theme: its lead window's, only when most of the module's own posts were judged into it (see
352
+ * DiscoveryModule.theme). Never `mixed`.
353
+ */
354
+ function judgedTheme(theme, judged) {
355
+ if (!theme || theme === 'mixed' || !judged.length)
356
+ return undefined;
357
+ return judged.filter(category => category === theme).length * 2 > judged.length ? theme : undefined;
358
+ }
342
359
  const presentations = new Set(['posts', 'visual', 'conversation', 'video', 'thread', 'longread']);
343
360
  /** A gallery strip holds up to twelve image posts gathered from the whole page. */
344
361
  export const GALLERY_MAX_ITEMS = 12;
@@ -464,7 +481,7 @@ export class DiscoveryFeedSession {
464
481
  ? { key: key(row.post.protocol, row.post.nativeId), kind: 'social', post: row.post }
465
482
  : { key: key('minds', row.id), kind: 'minds', post: row }));
466
483
  const read = { layout: response.meta.discovery ?? ordinary, partial: response.meta.category_filter?.status === 'partial',
467
- ...(response.meta.compass_filter ? { compass: response.meta.compass_filter } : {}) };
484
+ ...(response.meta.compass_filter ? { compass: response.meta.compass_filter } : {}), ...judgedInto(this.category, response.meta.category_filter) };
468
485
  if (!this.filtering)
469
486
  return { items, ...read };
470
487
  head ??= read;
@@ -518,7 +535,7 @@ export class DiscoveryFeedSession {
518
535
  const kept = data.items.flatMap((post, index) => this.metNetworkPost(post) ? [] : [{ post, index }]);
519
536
  const layout = data.discovery ?? ordinary;
520
537
  return { items: kept.map(({ post }) => ({ key: key(post.protocol, post.nativeId), kind: 'social', post })), layout, partial: data.categoryFilter?.status === 'partial',
521
- ...(kept.length !== data.items.length ? { layouts: kept.map(({ index }) => windowLayout(layout, index)) } : {}) };
538
+ ...(kept.length !== data.items.length ? { layouts: kept.map(({ index }) => windowLayout(layout, index)) } : {}), ...judgedInto(this.category, data.categoryFilter) };
522
539
  }
523
540
  /** An open-network post in the member's seen set (options.seen: '<protocol>/<nativeId>', an ActivityPub id also without https://). */
524
541
  metNetworkPost(post) {
@@ -607,7 +624,8 @@ export class DiscoveryFeedSession {
607
624
  const items = (indexed ? data.items.filter(post => !this.seen.has(key(post.protocol, post.nativeId))) : data.items).filter(post => !this.metNetworkPost(post));
608
625
  if (items.length) {
609
626
  stream.done = !topics.length;
610
- return { items: items.map(post => ({ key: key(post.protocol, post.nativeId), kind: 'social', post })), layout: data.discovery ?? ordinary, partial };
627
+ return { items: items.map(post => ({ key: key(post.protocol, post.nativeId), kind: 'social', post })), layout: data.discovery ?? ordinary, partial,
628
+ ...judgedInto(this.category, data.categoryFilter) };
611
629
  }
612
630
  }
613
631
  while (topics[0] && passedOver() && !topics[0].cursor?.startsWith(INDEX_CURSOR))
@@ -680,17 +698,25 @@ export class DiscoveryFeedSession {
680
698
  // Round-robin before grouping: the largest network cannot occupy every slot.
681
699
  // Each item carries the judgment for its own window of its source response,
682
700
  // so one page can feature several modules instead of one per source batch.
701
+ // A post is shown once by every identity (discoveryItemIdentities): a second note carrying a clip already placed is
702
+ // the same post to a reader, on this page or a later one.
683
703
  const mixed = [];
684
704
  for (let index = 0; index < Math.max(0, ...reads.map(read => read.items.length)); index++)
685
705
  for (const read of reads) {
686
706
  const item = read.items[index];
687
- if (!item || this.seen.has(item.key))
707
+ if (!item)
708
+ continue;
709
+ const identities = discoveryItemIdentities(item);
710
+ if (identities.some(identity => this.seen.has(identity)))
688
711
  continue;
689
- this.seen.add(item.key);
690
- mixed.push({ item, layout: read.layouts?.[index] ?? windowLayout(read.layout, index) });
712
+ for (const identity of identities)
713
+ this.seen.add(identity);
714
+ const judged = judgmentAt(read, index);
715
+ mixed.push({ item, layout: read.layouts?.[index] ?? windowLayout(read.layout, index), ...(judged ? { judged } : {}) });
691
716
  }
692
- // Deduplication memory is bounded independently of the UI's virtual window.
693
- while (this.seen.size > 4096)
717
+ // Deduplication memory is bounded independently of the UI's virtual window: an open-network post holds a few
718
+ // identities (its key, its id and its files), so the bound that held 4096 keys holds four times as many entries.
719
+ while (this.seen.size > 16_384)
694
720
  this.seen.delete(this.seen.values().next().value);
695
721
  const modules = topItems.map((item, index) => this.topModule(this.page, index, item));
696
722
  const page = this.page++;
@@ -707,9 +733,22 @@ export class DiscoveryFeedSession {
707
733
  const onPage = remaining.filter(row => members.has(row.item.key)).map(row => row.item);
708
734
  const carried = new Set(onPage.map(item => item.key));
709
735
  // Members this page did not carry come from the story's own reads; one an earlier page showed stays shown there.
710
- const read = (await this.memberPosts(cluster)).filter(item => !carried.has(item.key) && !this.seen.has(item.key));
736
+ // They are taken one at a time in the story's member order, each adding its identities as it is kept, so two
737
+ // members of one read that carry one clip (a mirror note on another server) show once: the one the story lists first.
738
+ const order = (item) => cluster.memberKeys.indexOf(item.key);
739
+ const read = [];
740
+ for (const item of [...await this.memberPosts(cluster)].sort((a, b) => order(a) - order(b))) {
741
+ if (carried.has(item.key))
742
+ continue;
743
+ const identities = discoveryItemIdentities(item);
744
+ if (identities.some(identity => this.seen.has(identity)))
745
+ continue;
746
+ for (const identity of identities)
747
+ this.seen.add(identity);
748
+ read.push(item);
749
+ }
711
750
  // Under the story, posts keep the server's member order: the lead media member first, then by member score.
712
- clusterItems = [...onPage, ...read].sort((a, b) => cluster.memberKeys.indexOf(a.key) - cluster.memberKeys.indexOf(b.key));
751
+ clusterItems = [...onPage, ...read].sort((a, b) => order(a) - order(b));
713
752
  remaining = remaining.filter(row => !members.has(row.item.key));
714
753
  for (const memberKey of cluster.memberKeys)
715
754
  this.seen.add(memberKey);
@@ -742,7 +781,8 @@ export class DiscoveryFeedSession {
742
781
  const selected = visual ? images : discussion ? conversations : thread ? quotes : [first];
743
782
  const items = selected.map(row => row.item);
744
783
  const type = video ? 'DiscoveryVideo' : visual ? 'DiscoveryGallery' : discussion ? 'DiscoveryConversation' : thread ? 'DiscoveryThread' : longread ? 'DiscoveryLongRead' : 'DiscoveryPosts';
745
- const theme = presentation !== 'posts' && first.layout.theme && first.layout.theme !== 'mixed' ? first.layout.theme : undefined;
784
+ // Titled after its lead window's theme only when most of its own posts were judged into it (DiscoveryModule.theme).
785
+ const theme = presentation !== 'posts' ? judgedTheme(first.layout.theme, selected.map(row => row.judged)) : undefined;
746
786
  modules.push({ id: `discovery-${page}-${index}-${items[0].key}`, presentation, source: presentation === 'posts' ? 'fallback' : 'jev', items, ...(theme ? { theme } : {}),
747
787
  spec: { root: 'module', elements: { module: { type, props: { itemKeys: items.map(item => item.key) }, children: [] } } } });
748
788
  const selectedKeys = new Set(items.map(item => item.key));
@@ -848,7 +888,7 @@ export class DiscoveryFeedSession {
848
888
  }
849
889
  const lanes = new Map();
850
890
  const networks = value.networks && typeof value.networks === 'object' ? value.networks : {};
851
- for (const protocol of TOP_NETWORKS) {
891
+ for (const protocol of SOCIAL_PROTOCOL_IDS) {
852
892
  const entries = networks[protocol];
853
893
  if (!streams.has(protocol) || !Array.isArray(entries))
854
894
  continue;
@@ -917,7 +957,9 @@ export class DiscoveryFeedSession {
917
957
  const read = at >= 0 ? reads[at] : null;
918
958
  if (read) {
919
959
  const kept = read.previews ? [] : read.items;
920
- reads[at] = { ...read, items: [...lane, ...kept], layouts: [...lane.map(() => ordinary), ...kept.map((_item, index) => read.layouts?.[index] ?? windowLayout(read.layout, index))] };
960
+ // The lane's posts lead the read's; they carry no judgment of the read's category.
961
+ reads[at] = { ...read, items: [...lane, ...kept], layouts: [...lane.map(() => ordinary), ...kept.map((_item, index) => read.layouts?.[index] ?? windowLayout(read.layout, index))],
962
+ judgments: [...lane.map(() => undefined), ...kept.map((_item, index) => judgmentAt(read, index))] };
921
963
  }
922
964
  else if (this.streams.some(stream => stream.protocol === protocol))
923
965
  reads.push({ items: lane, layout: ordinary, partial: false });
@@ -930,10 +972,15 @@ export class DiscoveryFeedSession {
930
972
  const found = top.posts.get(row.key) ?? carried.get(row.key);
931
973
  if (!found || this.seen.has(row.key))
932
974
  continue;
975
+ const identities = discoveryItemIdentities(found);
976
+ if (identities.some(identity => this.seen.has(identity)))
977
+ continue;
933
978
  const position = top.positions.get(row.ref) ?? placed.length;
934
979
  placed.push({ ...found, top: { ref: row.ref, position, block: position < 10 ? 'top' : 'below' } });
935
980
  this.seen.add(row.key);
936
981
  this.topKeys.add(row.key);
982
+ for (const identity of identities)
983
+ this.seen.add(identity);
937
984
  }
938
985
  return placed;
939
986
  }